Very old versions for history.
[opsoft_archive.git] / silentbob / silentbob-1.1 / src / main.cpp
blob6167d3695a83748dfe6e11b40d9f87f7848b7e7e
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include "head.h"
8 #include "wit.h"
9 #include "base.h"
10 #include "the_tt.h"
11 #include "mod.h"
13 extern "C" struct env_t *ENV;
14 struct env_t *ENV;
16 int sb_prname (char *arg)
18 int i_cmd = 0;
19 char * d_ptr;
20 d_ptr = arg + strlen (arg);
21 d_ptr--;
23 while (*d_ptr != '\\' &&
24 d_ptr > arg)
25 d_ptr--;
27 if (*d_ptr == '\\')
28 d_ptr++;
30 if (EQ (d_ptr, "tags"))
31 i_cmd = cmd_tags;
32 else if (EQ (d_ptr, "the_tt"))
33 i_cmd = cmd_the_tt;
34 else if (EQ (d_ptr, "gc_indent"))
35 i_cmd = cmd_indent;
36 else if (EQ (d_ptr, "structs"))
37 i_cmd = cmd_give_structs;
39 return i_cmd;
42 /* MAIN */
43 int main (int argc, char ** argv)
45 int d_global_type = GLOBAL_TYPE_VARIABLE;
46 int n_files = 0;
47 EArray *d_files;
48 DArray * d_opts;
49 int i_cmd = 0;
50 int i;
51 char * exp = NULL; // for cgrep
53 sb_init ();
54 i_cmd = sb_prname (argv[0]);
56 if (argc == 1 && !i_cmd) {
57 usage ();
58 exit (0);
61 d_files = new EArray (32);
62 ENV->d_files = d_files;
63 ENV->language = "C";
64 d_opts = new EArray (argc);
65 ENV->d_opts = d_opts;
66 for (i = 0; i < argc; i++)
67 d_opts->add (argv[i]);
69 for (i = 1; i < d_opts->get_size (); i++) {
70 if (d_opts->get (i)[0] != '-') {
71 n_files++;
72 d_files->add (strdup (d_opts->get (i)));
73 continue;
76 if (EQ (d_opts->get (i), "--help") || EQ (d_opts->get (i), "-h")) {
77 usage ();
78 exit (0);
81 if (EQ (d_opts->get (i), "-u")) {
82 SB_FLSET (SB_FLRTREE);
83 continue;
86 if (EQ (d_opts->get (i), "--verbose")) {
87 SB_FLSET (SB_FLVERBOSE);
88 continue;
91 if (EQ (d_opts->get (i), "--linear") || EQ (d_opts->get (i), "-l")) {
92 SB_FLSET (SB_FLLINEAR);
93 continue;
96 if (EQ (d_opts->get (i), "-C") && i < argc) {
97 i++;
98 chdir (d_opts->get (i));
99 continue;
102 if (EQ (d_opts->get (i), "--linux")) {
103 chdir ("/usr/src/linux");
104 continue;
107 if (EQ (d_opts->get (i), "--file")) {
108 i_cmd = cmd_file;
109 continue;
112 if (EQ (d_opts->get (i), "--the-tt")) {
113 i_cmd = cmd_the_tt;
114 continue;
117 if (EQ (d_opts->get (i), "--give-structs")) {
118 i_cmd = cmd_give_structs;
119 continue;
122 if (EQ (d_opts->get (i), "--no-links")) {
123 SB_FLSET (SB_FLNOLINKS);
124 continue;
127 if (EQ (d_opts->get (i), "--indent")) {
128 i_cmd = cmd_indent;
129 continue;
132 if (EQ (d_opts->get (i), "--tags")) {
133 i_cmd = cmd_tags;
134 continue;
137 if (EQ (d_opts->get (i), "--debug")) {
138 fclose (ENV->d_stream_dbg);
139 ENV->d_stream_dbg = fdopen (fileno (stderr), "w");
140 continue;
143 if (EQ (d_opts->get (i), "--globals")) {
144 d_global_type = GLOBAL_TYPE_TYPEDEF |
145 GLOBAL_TYPE_VARIABLE |
146 GLOBAL_TYPE_STRUCT |
147 GLOBAL_TYPE_FUNCTION |
148 GLOBAL_TYPE_DEFINE |
149 GLOBAL_TYPE_CLASS |
150 GLOBAL_TYPE_NAMESPACE;
151 i_cmd = cmd_globals;
152 continue;
155 if (EQ (d_opts->get (i), "--globals-typedef")) {
156 i_cmd = cmd_globals;
157 d_global_type = GLOBAL_TYPE_TYPEDEF;
158 continue;
161 if (EQ (d_opts->get (i), "--globals-extern")) {
162 i_cmd = cmd_globals;
163 d_global_type = GLOBAL_TYPE_EXTERN;
164 continue;
167 if (EQ (d_opts->get (i), "--globals-function")) {
168 i_cmd = cmd_globals;
169 d_global_type = GLOBAL_TYPE_FUNCTION;
170 continue;
173 if (EQ (d_opts->get (i), "--globals-struct")) {
174 i_cmd = cmd_globals;
175 d_global_type = GLOBAL_TYPE_STRUCT;
176 continue;
179 if (EQ (d_opts->get (i), "--globals-variable")) {
180 i_cmd = cmd_globals;
181 d_global_type = GLOBAL_TYPE_VARIABLE;
182 continue;
185 if (EQ (d_opts->get (i), "--globals-define")) {
186 i_cmd = cmd_globals;
187 d_global_type = GLOBAL_TYPE_DEFINE;
188 continue;
191 if (EQ (d_opts->get (i), "--time-test")) {
192 Dtimer ();
193 continue;
196 if (EQ (d_opts->get (i), "--simulate")) {
197 Dtimer ();
198 SB_FLSET (SB_FLSIMULATE);
199 continue;
202 if (EQ (d_opts->get (i), "--make-ctags")) {
203 i_cmd = cmd_makectags;
204 continue;
207 if (EQ (d_opts->get (i), "--tag-style") || EQ (d_opts->get (i), "-ts")) {
208 SB_FLSET (SB_FLTAGSTYLE);
209 continue;
212 if (EQ (d_opts->get (i), "-L") && (i+1) < argc) {
213 i++;
214 d_files->from_file (d_opts->get (i));
215 d_files->foreach ((Dfunc_t) chomp);
216 n_files = d_files->get_size ();
217 continue;
220 if (EQ (d_opts->get (i), "-V") || EQ (d_opts->get (i), "--version")) {
221 usage ();
222 exit (0);
225 if (EQ (d_opts->get (i), "--call-tags") || EQ (d_opts->get (i), "-ct")) {
226 SB_FLSET (SB_FLTAGSTYLE);
227 i_cmd = cmd_call_tags;
228 continue;
231 if (EQ (d_opts->get (i), "--depth")) {
232 if (++i >= argc)
233 break;
235 ENV->d_depth = atoi (d_opts->get (i));
236 continue;
239 if (EQ (d_opts->get (i), "-fn")) {
240 SB_FLSET (SB_FLFNAMES);
241 continue;
244 if (EQ (d_opts->get (i), "--cgrep")) {
245 if (++i >= argc)
246 break;
248 i_cmd = cmd_cgrep;
249 exp = d_opts->get (i);
252 if (EQ (d_opts->get (i), "-a") || EQ (d_opts->get (i), "--all")) {
253 SB_FLSET (SB_FLALL);
254 continue;
257 if (EQ (d_opts->get (i), "--test") || EQ (d_opts->get (i), "-t")) {
258 SB_FLSET (SB_FLTEST);
259 continue;
262 if (EQ (d_opts->get (i), "--ctags-append")) {
263 SB_FLSET (SB_FLCTAGSAPPEND);
264 continue;
267 if (EQ (d_opts->get (i), "-A")) {
268 if (++i >= argc)
269 break;
271 ENV->cgrep_A = atoi (d_opts->get (i));
272 continue;
275 if (EQ (d_opts->get (i), "-B")) {
276 if (++i >= argc)
277 break;
279 ENV->cgrep_B = atoi (d_opts->get (i));
280 continue;
283 if (EQ (d_opts->get (i), "--plugins-info")) {
284 mods_info ();
285 exit (0);
288 if (EQ (d_opts->get (i), "--tags")) {
289 i_cmd = cmd_tags;
290 continue;
293 if (modding_optparse (&i, 1))
294 continue;
296 /* if (d_opts->get (i)[0] == '-') {
297 fprintf (stderr, "unknown option : %s\n", d_opts->get (i));
298 exit (1);
300 } // for (i = 1; i < argc; i++)
302 for (i = 0; i < argc; i++)
303 modding_optparse (&i, 2);
305 if (NE (ENV->language, "C")) {
306 i = modding_start (i_cmd);
307 exit (i);
310 switch (i_cmd) {
311 case cmd_makectags:
312 make_ctags (d_files);
313 break;
314 case cmd_indent:
315 nogui_indent ();
316 break;
317 case cmd_cgrep:
318 cgrep (d_files, exp);
319 break;
320 case cmd_call_tags:
321 call_tags (d_files);
322 goto out;
323 case cmd_tags:
324 tags (d_files, NULL);
325 goto out;
329 if (i_cmd == cmd_makectags ||
330 i_cmd == cmd_indent ||
331 i_cmd == cmd_cgrep) {
332 print_the_time ();
333 exit (0);
336 for (i = 0; i < n_files; i++) {
337 switch (i_cmd) {
338 case cmd_file:
339 flist_main (d_files->get (i));
340 break;
341 case cmd_the_tt:
342 THE_TT::the_tt_main (d_files->get(i));
343 break;
344 case cmd_give_structs:
345 got_structs (d_files->get (i));
346 break;
347 case cmd_globals:
348 globals (d_files->get(i), d_global_type);
349 break;
353 ENV->sb_cmd = i_cmd;
355 if (i_cmd == cmd_the_tt && !n_files) // THE_TT for stdin
356 THE_TT::the_tt_main ("-");
357 else if (i_cmd == cmd_give_structs && !n_files) {
358 got_structs ("-");
359 print_the_time ();
360 exit (0);
363 if ((i_cmd == cmd_globals || i_cmd == cmd_file) && !n_files) {
364 char d_buf[1024];
366 while (fgets (d_buf, 1023, stdin)) {
367 chomp (d_buf);
368 switch (i_cmd) {
369 case cmd_globals:
370 globals (d_buf, d_global_type);
371 break;
372 case cmd_file:
373 flist_main (d_buf);
374 break;
379 if (i_cmd == cmd_file ||
380 i_cmd == cmd_the_tt ||
381 i_cmd == cmd_give_structs ||
382 i_cmd == cmd_globals)
383 goto out;
385 if (!SB_FLGET (SB_FLRTREE))
386 call_tree (d_files->get (0));
387 else
388 reverse_calltree (d_files->get (0));
390 out:
391 print_the_time ();
393 if (ENV->immune_list)
394 delete ENV->immune_list;
396 d_files->foreach (free);
397 delete d_files;
398 fflush (stdout);
400 return EXIT_SUCCESS;