Ruby plugin !
[opsoft.git] / silentbob / plugins / plugin_ruby.cxx
blob0ceb38387de7077d46cfe872523dac4947e749c1
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 struct env_t *ENV;
20 void ruby_files ()
22 unlink ("./ruby_files");
23 sblib_find ("./", "*.rb", "./ruby_files");
26 char ruby_opt (DArray * d_opts, int * pos)
28 int count;
29 char * S;
31 if (! d_opts || ! pos)
32 return 0;
34 if (EQ (d_opts->get (*pos), "--ruby")) {
35 ENV->language = strdup ("Ruby");
36 return 1;
39 if (EQ (d_opts->get (*pos), "--files")) {
40 ruby_files ();
41 return 1;
44 return 0;
47 char ruby_opt2 (DArray * d_opts, int * pos)
49 return 0;
52 bool ruby_scan_line (const char *S)
54 if (! S)
55 return false;
57 if (strstr (S, "class ") ||
58 strstr (S, "module ") ||
59 strstr (S, "def "))
60 return true;
62 return false;
65 char * ruby_last_word (char * op)
67 char * d_word;
68 char * S;
70 S = op;
71 while (*S) {
72 if (*S == '(' || *S == '=' || *S == '[' || *S == '<')
73 break;
74 S++;
77 while (S[-1] == ' ')
78 S--;
80 *S = 0;
81 d_word = op;
82 while (true) {
83 S = strchr (d_word, ' ');
84 if (S == NULL)
85 break;
86 d_word = S+1;
89 return d_word;
92 char * ruby_tag (char * filename, char * str, int line, FILE * of)
94 char * S;
96 S = ruby_last_word (str);
97 if (! S)
98 return NULL;
100 chomp (S);
101 if (! of)
102 printf ("%s\t%s\t%d\n", S, filename, line);
103 else
104 fprintf (of, "%s\t%s\t%d\n", S, filename, line);
107 void ruby_file (char * f_name)
109 FILE * myfile;
110 char * buf;
112 if (! f_name)
113 return;
115 myfile = fopen (f_name, "r");
116 if (! myfile)
117 return;
119 buf = CNEW (char, 4096);
120 while (fgets (buf, 4096, myfile)) {
121 if (ruby_scan_line (buf))
122 printf ("%s", buf);
125 fclose (myfile);
126 DROP (buf);
129 int ruby_ctags (char * f_name, FILE * of)
131 FILE * myfile;
132 char * buf;
133 int line = 0;
135 if (! f_name)
136 return -1;
138 myfile = fopen (f_name, "r");
139 if (! myfile)
140 return -1;
142 buf = CNEW (char, 4096);
143 while (fgets (buf, 4096, myfile)) {
144 ++line;
145 if (ruby_scan_line (buf))
146 ruby_tag (f_name, buf, line, of);
149 fclose (myfile);
150 DROP (buf);
152 return 0;
155 void ruby_short_info ()
157 printf ("Ruby plugin.");
160 void ruby_long_info ()
162 printf ("Ruby plugin.\n");
163 printf ("Version: 1.0\n");
164 printf ("options: --ruby --files\n");
167 DArray * plugin_init (struct env_t *env)
169 DArray * Ret;
170 mod_t * plug;
172 Ret = new DArray (1);
173 plug = CNEW (mod_t, 1);
174 memset (plug, 0, sizeof (mod_t));
176 plug->Version = strdup ("1.0");
177 plug->short_info = ruby_short_info;
178 plug->long_info = ruby_long_info;
179 plug->opt = ruby_opt;
180 plug->opt2 = ruby_opt2;
181 plug->make_ctags = ruby_ctags;
182 plug->language = strdup ("Ruby");
183 plug->file = ruby_file;
185 ENV->listOptions->add ( "--ruby");
186 ENV->listOptions->add ("--files");
188 Ret->add (LPCHAR (plug));
189 return Ret;