+ Fixes
[opsoft.git] / silentbob / plugins / plugin_ruby_kinds.cxx
blob650d19ec0aa51124844ebc070e93eab361e0e07b
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 int ruby_kinds_file (char * f_name);
20 int ruby_kinds ();
21 FILE * ofile;
22 int rkinds = 0;
24 namespace Ruby
26 enum {
27 Class = (1<<0),
28 Module = (1<<1),
29 Function = (1<<2)
33 void ruby_set_rkinds (char *S)
35 while (*S) {
36 switch (*S) {
37 case 'c':
38 rkinds |= Ruby::Class;
39 break;
40 case 'm':
41 rkinds |= Ruby::Module;
42 break;
43 case 'f':
44 rkinds |= Ruby::Function;
45 break;
47 ++S;
51 char ruby_kinds_opt (DArray * d_opts, int * pos)
53 if (! d_opts || ! pos)
54 return 0;
56 if (EQ (d_opts->get (*pos), "--ruby")) {
57 ENV->language = (char *) "Ruby";
58 return 1;
61 if (NE (ENV->language, "Ruby"))
62 return 0;
64 if (EQ (d_opts->get (*pos), "--kinds")) {
65 if (++(*pos) >= d_opts->get_size ())
66 return 0;
67 return 1;
70 return 0;
73 char ruby_kinds_opt2 (DArray * d_opts, int * pos)
75 if (EQ (d_opts->get (*pos), "--kinds")) {
76 if (++(*pos) >= d_opts->get_size ())
77 return 0;
78 ruby_set_rkinds (d_opts->get(*pos));
79 ruby_kinds ();
80 exit (0);
83 return 0;
86 int ruby_kinds_scan_line (const char *S)
88 char * ptr;
89 if (! S)
90 return 0;
92 ptr = strchr (S, '#');
93 if (ptr)
94 *ptr = '\0';
96 if (strstr (S, "class "))
97 return Ruby::Class;
98 if (strstr (S, "module "))
99 return Ruby::Module;
100 if (strstr (S, "def "))
101 return Ruby::Function;
103 return 0;
106 char * ruby_kinds_tag (char * str)
108 char * S;
109 S = ruby_last_word (str);
110 if (! S)
111 return NULL;
112 chomp (S);
113 return S;
116 int ruby_kinds ()
118 int i;
119 DArray * files;
121 files = ENV->d_files;
122 for (i = 0; i < files->get_size (); ++i)
123 ruby_kinds_file (files->get (i));
125 return 0;
128 int ruby_kinds_file (char * f_name)
130 char * buf;
131 int line;
132 FILE * myfile;
133 char * S;
134 int kind;
136 myfile = fopen (f_name, "r");
137 if (! myfile)
138 return -1;
140 line = 0;
141 buf = CNEW (char, 4096);
142 while (fgets (buf, 4096, myfile)) {
143 ++line;
144 kind = ruby_kinds_scan_line (buf);
145 if (! kind)
146 continue;
148 S = ruby_kinds_tag (buf);
149 if (kind & rkinds)
150 printf ("%s\n", S);
153 fclose (myfile);
154 DROP (buf);
155 return 0;
158 void ruby_kinds_short_info ()
160 printf ("Ruby kinds plugin.");
163 void ruby_kinds_long_info ()
165 printf ("Ruby kinds plugin.\n");
166 printf ("Version: 1.0\n");
167 printf ("options: --kinds\n");
168 printf ("Usage: bob_ruby <files> --kinds <kinds>\n"
169 "\t\t kinds: c - class, f - function, m - module\n ");
172 DArray * plugin_init (struct env_t *env)
174 DArray * Ret;
175 mod_t * plug;
177 Ret = new DArray (1);
178 plug = CNEW (mod_t, 1);
179 memset (plug, 0, sizeof (mod_t));
181 plug->Version = strdup ("1.0");
182 plug->short_info = ruby_kinds_short_info;
183 plug->long_info = ruby_kinds_long_info;
184 plug->opt = ruby_kinds_opt;
185 plug->opt2 = ruby_kinds_opt2;
187 ENV->listOptions->add ( "--ruby");
189 Ret->add (LPCHAR (plug));
190 return Ret;