+ rmake ruby script
[opsoft.git] / silentbob / plugins / plugin_ruby.cxx
blob656b0e3d38b694650bae24ea326afa35dcc080fd
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 int count;
30 char * S;
32 if (! d_opts || ! pos)
33 return 0;
35 if (EQ (d_opts->get (*pos), "--ruby")) {
36 ENV->language = strdup ("Ruby");
37 return 1;
40 if (EQ (d_opts->get (*pos), "--files")) {
41 ruby_files ();
42 return 1;
45 return 0;
48 char ruby_opt2 (DArray * d_opts, int * pos)
50 return 0;
53 bool ruby_scan_line (const char *S)
55 char * ptr;
56 if (! S)
57 return false;
59 ptr = strchr (S, '#');
60 if (ptr)
61 *ptr = '\0';
63 if (strstr (S, "class ") ||
64 strstr (S, "module ") ||
65 strstr (S, "def "))
66 return true;
68 return false;
71 char * ruby_last_word (char * op)
73 char * d_word;
74 char * S;
76 S = op;
77 while (*S) {
78 if (*S == '(' || *S == '=' || *S == '[' || *S == '<')
79 break;
80 S++;
83 while (S[-1] == ' ')
84 S--;
86 *S = 0;
87 d_word = op;
88 while (true) {
89 S = strchr (d_word, ' ');
90 if (S == NULL)
91 break;
92 d_word = S+1;
95 return d_word;
98 char * ruby_tag (char * filename, char * str, int line, FILE * of)
100 char * S;
102 S = ruby_last_word (str);
103 if (! S)
104 return NULL;
106 chomp (S);
107 if (! of)
108 printf ("%s\t%s\t%d\n", S, filename, line);
109 else
110 fprintf (of, "%s\t%s\t%d\n", S, filename, line);
113 void ruby_file (char * f_name)
115 FILE * myfile;
116 char * buf;
118 if (! f_name)
119 return;
121 myfile = fopen (f_name, "r");
122 if (! myfile)
123 return;
125 printf ("# %s\n", f_name);
126 buf = CNEW (char, 4096);
127 while (fgets (buf, 4096, myfile)) {
128 if (ruby_scan_line (buf))
129 printf ("%s", buf);
132 printf ("\n");
133 fclose (myfile);
134 DROP (buf);
137 void __ruby_files (char * f_name)
139 unlink (f_name);
140 sblib_find ("./", "*.rb", f_name);
144 void ruby_lookup ()
146 int i;
147 DArray * d_array;
149 __ruby_files (ENV->tmp_files);
150 d_array = new DArray (32);
151 d_array->from_file (ENV->tmp_files);
152 d_array->foreach ((Dfunc_t)chomp);
154 for (i = 0; i < d_array->get_size (); ++i) {
155 if (! d_array->get (i))
156 continue;
158 ruby_ctags (d_array->get (i), ofile);
161 unlink (ENV->tmp_files);
162 d_array->foreach (free);
163 delete d_array;
166 int ruby_ctags (char * f_name, FILE * of)
168 FILE * myfile;
169 char * buf;
170 int line = 0;
172 ofile = of;
173 if (! f_name) {
174 ruby_lookup ();
175 return 0;
178 myfile = fopen (f_name, "r");
179 if (! myfile)
180 return -1;
182 buf = CNEW (char, 4096);
183 while (fgets (buf, 4096, myfile)) {
184 ++line;
185 if (ruby_scan_line (buf))
186 ruby_tag (f_name, buf, line, of);
189 fclose (myfile);
190 DROP (buf);
192 return 0;
195 void ruby_short_info ()
197 printf ("Ruby plugin.");
200 void ruby_long_info ()
202 printf ("Ruby plugin.\n");
203 printf ("Version: 1.0\n");
204 printf ("options: --ruby --files --file\n");
205 printf ("Usage: bob --ruby --files\n"
206 "\tbob ./ruby_files --ruby --make-ctags\n"
207 "\tbob <file> --ruby --file\n");
210 DArray * plugin_init (struct env_t *env)
212 DArray * Ret;
213 mod_t * plug;
215 Ret = new DArray (1);
216 plug = CNEW (mod_t, 1);
217 memset (plug, 0, sizeof (mod_t));
219 plug->Version = strdup ("1.0");
220 plug->short_info = ruby_short_info;
221 plug->long_info = ruby_long_info;
222 plug->opt = ruby_opt;
223 plug->opt2 = ruby_opt2;
224 plug->make_ctags = ruby_ctags;
225 plug->language = strdup ("Ruby");
226 plug->file = ruby_file;
228 ENV->listOptions->add ( "--ruby");
229 ENV->listOptions->add ("--files");
231 Ret->add (LPCHAR (plug));
232 return Ret;