+ Fixes
[opsoft.git] / silentbob / src / callTags.cxx
blob18f279284cf90b7423708633e997d49c60cd3052
1 /*
2 * (c) Oleg Puchinin 2006,2007
3 * graycardinalster@gmail.com
4 *
5 */
7 #include <head.h>
8 #include <wit.h>
9 #include <the_tt.h>
10 #include <dbg.h>
11 #include <fcntl.h>
12 #include <TT.h>
14 int callTagsFile (char * fileName)
16 char * functionName = NULL;
17 char m_name[256];
18 char * oldName = NULL;
19 char * ptr = NULL;
20 bool b_inmacro = false;
21 char * S = NULL;
22 int i;
23 TT * tt = NULL;
24 DArray * d_words = NULL;
26 tt = new TT;
27 if (tt->loadFile (fileName) < 0) {
28 delete tt;
29 return -1;
32 tt->init ();
34 while (true) {
35 tt->nextOperator ();
36 if (tt->ch == 0)
37 break;
39 ptr = tt->op ();
41 if ((ptr[0] == '#') && def_test (ptr)) {
42 if (macro_name (ptr, m_name))
43 functionName = m_name;
44 b_inmacro = true;
47 if (! tt->bracketDepth) {
48 if (tt->ch == '\n' && ptr[strlen(ptr) - 1] != '\\') {
49 b_inmacro = false;
50 functionName = oldName;
54 if (tt->wit () == OT::Function ) {
55 functionName = ww_last_word (tt->op ());
56 oldName = functionName;
59 if (! tt->bracketDepth)
60 continue;
62 d_words = split_to_words (ptr);
63 for (i = 0; i < d_words->get_size (); i++) {
64 S = cts ((c_word *) d_words->get (i));
65 if (! S)
66 continue;
68 if (functionName != NULL) {
69 printf ("%s\t%s\t%i\t;\tby\t%s\n", S,
70 tt->tt->fileName,
71 tt->tt->attachment[ENV->t_op_no].pair_line+1,
72 functionName);
73 } else {
74 printf ("%s\t%s\t%i\n", S,
75 tt->tt->fileName,
76 tt->tt->attachment[ENV->t_op_no].pair_line+1);
80 d_words->foreach ((Dfunc_t) free_cword);
81 delete d_words;
84 fflush (stdout);
85 delete tt;
86 return 0;
89 int call_tags_multi (EArray * d_files)
91 char m_buf[512];
92 __djob_t * j;
93 int i;
95 if (! d_files)
96 return -1;
98 d_files->strings_to_file (ENV->tmp_files);
99 split_tmp_files ();
101 for (i = 0; i < ENV->max_proc; ++i) {
102 j = ENV->proc_list->fork ();
103 if (j->child) {
104 sprintf (m_buf, "silent_bob -L %s%i --thread --call-tags", ENV->tmp_files, i);
105 exit (execlp ("sh", "sh", "-c", m_buf, NULL));
107 usleep (500);
110 while ((j = ENV->proc_list->wait_all ()) && j)
111 Dexec_done (j);
113 mk_tags ((char *) "./call_tags", NULL);
114 remove_tmp_files ();
115 return 0;
118 /// Точка входа для --call-tags
119 int call_tags (EArray * d_files)
121 int fd;
122 int i;
124 if (!d_files || d_files->get_size () == 0) {
125 fprintf (stderr, "No such files.\n");
126 return -1;
129 if (! SB_FLGET (SB_FLTHREAD))
130 unlink (ENV->tmp_tags);
132 if (ENV->max_proc > 1)
133 return call_tags_multi (d_files);
135 fd = open (ENV->tmp_tags, O_APPEND | O_WRONLY);
136 if (fd < 0) {
137 fd = open (ENV->tmp_tags, O_CREAT | O_APPEND | O_WRONLY, 0666);
138 if (fd < 0)
139 return -1;
142 dup2 (fd, 1);
143 for (i = 0; i < d_files->get_size (); i++)
144 callTagsFile (d_files->get (i));
146 close (fd);
147 fclose (stdout);
148 if (! SB_FLGET (SB_FLTHREAD)) {
149 mk_tags ((char *) "./call_tags", NULL);
150 remove_tmp_files ();
153 return 0;