+ Fixes
[opsoft.git] / silentbob / plugins / plugin_ruby.cxx
blob8ed3ff8d0849ddf29f656257b100529e88d8622c
1 /*
2 * (c) Oleg Puchinin 2008
3 * graycardinalster@gmail.com
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <gclib/gclib.h>
12 #include <gclib/gclib_c.h>
13 #include <mod.h>
14 #include <head.h>
15 #include <dbg.h>
17 extern "C" DArray * plugin_init (struct env_t *env);
18 int ruby_ctags (char * f_name, FILE * of);
19 FILE * ofile;
21 void ruby_files ()
23 unlink ("./ruby_files");
24 sblib_find ("./", "*.rb", "./ruby_files");
27 char ruby_opt (DArray * d_opts, int * pos)
29 if (! d_opts || ! pos)
30 return 0;
32 if (EQ (d_opts->get (*pos), "--ruby")) {
33 ENV->language = strdup ("Ruby");
34 return 1;
37 if (EQ (d_opts->get (*pos), "--files")) {
38 ruby_files ();
39 return 1;
42 return 0;
45 char ruby_opt2 (DArray * d_opts, int * pos)
47 return 0;
50 bool ruby_scan_line (const char *S)
52 char * ptr;
53 if (! S)
54 return false;
56 ptr = strchr (S, '#');
57 if (ptr)
58 *ptr = '\0';
60 if (strstr (S, "class ") ||
61 strstr (S, "module ") ||
62 strstr (S, "def "))
63 return true;
65 return false;
68 char * ruby_last_word (char * op)
70 char * d_word;
71 char * S;
73 S = op;
74 while (*S) {
75 if (*S == '(' || *S == '=' || *S == '[' || *S == '<')
76 break;
77 S++;
80 while (S[-1] == ' ')
81 S--;
83 *S = 0;
84 d_word = op;
85 while (true) {
86 S = strchr (d_word, ' ');
87 if (S == NULL)
88 break;
89 d_word = S+1;
92 return d_word;
95 void ruby_tag (char * filename, char * str, int line, FILE * of)
97 char * S;
99 S = ruby_last_word (str);
100 if (! S)
101 return;
103 chomp (S);
104 if (! of)
105 printf ("%s\t%s\t%d\n", S, filename, line);
106 else
107 fprintf (of, "%s\t%s\t%d\n", S, filename, line);
110 void ruby_file (char * f_name)
112 FILE * myfile;
113 char * buf;
115 if (! f_name)
116 return;
118 myfile = fopen (f_name, "r");
119 if (! myfile)
120 return;
122 printf ("# %s\n", f_name);
123 buf = CNEW (char, 4096);
124 while (fgets (buf, 4096, myfile)) {
125 if (ruby_scan_line (buf))
126 printf ("%s", buf);
129 printf ("\n");
130 fclose (myfile);
131 DROP (buf);
134 void __ruby_files (char * f_name)
136 unlink (f_name);
137 sblib_find ("./", "*.rb", f_name);
141 void ruby_lookup ()
143 int i;
144 DArray * d_array;
146 __ruby_files (ENV->tmp_files);
147 d_array = new DArray (32);
148 d_array->from_file (ENV->tmp_files);
149 d_array->foreach ((Dfunc_t)chomp);
151 for (i = 0; i < d_array->get_size (); ++i) {
152 if (! d_array->get (i))
153 continue;
155 ruby_ctags (d_array->get (i), ofile);
158 unlink (ENV->tmp_files);
159 d_array->foreach (free);
160 delete d_array;
163 int ruby_ctags (char * f_name, FILE * of)
165 FILE * myfile;
166 char * buf;
167 int line = 0;
169 ofile = of;
170 if (! f_name) {
171 ruby_lookup ();
172 return 0;
175 myfile = fopen (f_name, "r");
176 if (! myfile)
177 return -1;
179 buf = CNEW (char, 4096);
180 while (fgets (buf, 4096, myfile)) {
181 ++line;
182 if (ruby_scan_line (buf))
183 ruby_tag (f_name, buf, line, of);
186 fclose (myfile);
187 DROP (buf);
189 return 0;
192 void ruby_short_info ()
194 printf ("Ruby plugin.");
197 void ruby_long_info ()
199 printf ("Ruby plugin.\n");
200 printf ("Version: 1.0\n");
201 printf ("options: --ruby --files --file\n");
202 printf ("Usage: bob --ruby --files\n"
203 "\tbob ./ruby_files --ruby --make-ctags\n"
204 "\tbob <file> --ruby --file\n");
207 DArray * plugin_init (struct env_t *env)
209 DArray * Ret;
210 mod_t * plug;
212 Ret = new DArray (1);
213 plug = CNEW (mod_t, 1);
214 memset (plug, 0, sizeof (mod_t));
216 plug->Version = strdup ("1.0");
217 plug->short_info = ruby_short_info;
218 plug->long_info = ruby_long_info;
219 plug->opt = ruby_opt;
220 plug->opt2 = ruby_opt2;
221 plug->make_ctags = ruby_ctags;
222 plug->language = strdup ("Ruby");
223 plug->file = ruby_file;
225 ENV->listOptions->add ( "--ruby");
226 ENV->listOptions->add ("--files");
228 Ret->add (LPCHAR (plug));
229 return Ret;