Fixes
[opsoft.git] / silentbob / plugins / plugin_ruby.cxx
blob829184a161ed1d70a2627b0bccd3b1cb9ad83cae
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 void ruby_tag (char * filename, char * str, int line, FILE * of)
70 char * S;
72 S = ruby_last_word (str);
73 if (! S)
74 return;
76 chomp (S);
77 if (! of)
78 printf ("%s\t%s\t%d\n", S, filename, line);
79 else
80 fprintf (of, "%s\t%s\t%d\n", S, filename, line);
83 void ruby_file (char * f_name)
85 FILE * myfile;
86 char * buf;
88 if (! f_name)
89 return;
91 myfile = fopen (f_name, "r");
92 if (! myfile)
93 return;
95 printf ("# %s\n", f_name);
96 buf = CNEW (char, 4096);
97 while (fgets (buf, 4096, myfile)) {
98 if (ruby_scan_line (buf))
99 printf ("%s", buf);
102 printf ("\n");
103 fclose (myfile);
104 DROP (buf);
107 void __ruby_files (char * f_name)
109 unlink (f_name);
110 sblib_find ("./", "*.rb", f_name);
114 void ruby_lookup ()
116 int i;
117 DArray * d_array;
119 __ruby_files (ENV->tmp_files);
120 d_array = new DArray (32);
121 d_array->from_file (ENV->tmp_files);
122 d_array->foreach ((Dfunc_t)chomp);
124 for (i = 0; i < d_array->get_size (); ++i) {
125 if (! d_array->get (i))
126 continue;
128 ruby_ctags (d_array->get (i), ofile);
131 unlink (ENV->tmp_files);
132 d_array->foreach (free);
133 delete d_array;
136 int ruby_ctags (char * f_name, FILE * of)
138 FILE * myfile;
139 char * buf;
140 int line = 0;
142 ofile = of;
143 if (! f_name) {
144 ruby_lookup ();
145 return 0;
148 myfile = fopen (f_name, "r");
149 if (! myfile)
150 return -1;
152 buf = CNEW (char, 4096);
153 while (fgets (buf, 4096, myfile)) {
154 ++line;
155 if (ruby_scan_line (buf))
156 ruby_tag (f_name, buf, line, of);
159 fclose (myfile);
160 DROP (buf);
162 return 0;
165 void ruby_short_info ()
167 printf ("Ruby language plugin.");
170 void ruby_long_info ()
172 printf ("Ruby plugin.\n");
173 printf ("Version: 1.0\n");
174 printf ("options: --ruby --files --file\n");
175 printf ("Usage: bob --ruby --files\n"
176 "\tbob ./ruby_files --ruby --make-ctags\n"
177 "\tbob <file> --ruby --file\n");
180 DArray * plugin_init (struct env_t *env)
182 DArray * Ret;
183 mod_t * plug;
185 Ret = new DArray (1);
186 plug = CNEW (mod_t, 1);
187 memset (plug, 0, sizeof (mod_t));
189 plug->Version = strdup ("1.0");
190 plug->short_info = ruby_short_info;
191 plug->long_info = ruby_long_info;
192 plug->opt = ruby_opt;
193 plug->opt2 = ruby_opt2;
194 plug->make_ctags = ruby_ctags;
195 plug->language = strdup ("Ruby");
196 plug->file = ruby_file;
198 ENV->listOptions->add ( "--ruby");
199 ENV->listOptions->add ("--files");
201 Ret->add (LPCHAR (plug));
202 return Ret;