Very old versions for history.
[opsoft_archive.git] / silentbob / silent_bob-1.2 / src / tree.cpp
blob0242a96099d7b592e953f521d4745e71ee3bda84
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
4 *
5 */
7 #include "head.h"
8 #include "the_tt.h"
9 #include "dbg.h"
10 #include "bugs.h"
12 EArray * d_calltags_file;
14 inline bool tree_immune (char * m_fname)
16 return ENV->immune_list->sfind (m_fname) == -1 ? false : true;
20 /* "SilentBob -u" functionality. */
21 void rctree_iter (char * f_name, int n_trip)
23 EArray * d_virused;
24 char m_buf[256];
25 char * S;
26 int len;
27 int i;
28 int a;
30 n_trip ++;
31 if (n_trip > ENV->d_depth)
32 return;
34 /*
35 * 1. Find record in d_calltags_file.
36 * 2. Find _first_ record in d_calltags_file.
37 * 3. Parse record.
38 * 4. Check d_virused list.
39 * 5. If f_name not in ENV->full_list :
40 * 5.1 Add f_name to ENV->full_list.
41 * 5.2 New iteration.
45 snprintf (m_buf, 255, "%s\t", f_name);
46 m_buf[255] = 0;
47 len = strlen (m_buf);
48 i = d_calltags_file->snfind_fast (m_buf);
49 d_virused = new EArray (32);
51 do {
52 i--;
53 if (i < 0)
54 break;
55 S = d_calltags_file->get (i);
56 if (! S)
57 break;
58 } while (! strncmp (S, m_buf, len));
60 do {
61 i++;
62 S = d_calltags_file->get (i);
63 if (! S || strncmp (S, m_buf, len))
64 break;
66 S = strstr (S, "\tby\t");
67 if (S == NULL) {
68 fprintf (ENV->d_stream_dbg, "\tBUG (no \"by\") : %s\n",
69 d_calltags_file->get (i));
70 continue;
73 S += 4;
74 chomp (S);
76 if (d_virused->sfind (S) != -1)
77 continue;
79 d_virused->add (strdup (S));
80 for (a = 0; a < n_trip; a++)
81 fputc ('\t', stdout);
83 tree_echo (n_trip, S);
85 if (ENV->full_list->sfind (S) == -1) {
86 ENV->full_list->add (strdup (S));
87 rctree_iter (S, n_trip);
90 } while (true);
92 d_virused->foreach (free);
93 delete d_virused;
96 void reverse_calltree (char * f_name)
98 assert (f_name == NULL, "Lena 2");
99 if (! f_name)
100 return;
102 tree_echo (0, f_name);
103 d_calltags_file = new EArray;
104 d_calltags_file->from_file ("./call_tags");
105 if (d_calltags_file->get_size () == 0) {
106 bug_nocalltags ();
109 rctree_iter (f_name, 0);
111 d_calltags_file->foreach (free);
112 delete d_calltags_file;
113 fflush (stdout);
116 void tree_echo (int n_trip, char *S)
118 struct d_tag_t * tag;
119 EArray * tags;
121 if (! SB_FLGET (SB_FLFNAMES))
122 goto out_min;
123 else {
124 tags = got_tag_ctags (S);
125 if (! tags || (tags->get_size () == 0))
126 goto out_min;
128 tag = (struct d_tag_t *) tags->get (0);
129 if (! tag)
130 goto out_min;
132 printf ("// %i // %s\t%s", n_trip, S, tag->d_file);
133 if (tags->get_size () > 1)
134 printf (" (more)");
135 fputc ('\n', stdout);
138 return;
140 out_min:
141 printf ("// %i // %s\n", n_trip, S);
144 inline void tree_mkpos (int N)
146 while (N--)
147 fputc ('\t', stdout);
150 /* SilentBob call-tree functionality. */
151 void call_tree (char * f_name, int n_trip)
153 struct tt_state_t * d_tt_state;
154 char * d_ptr, *d_out;
155 EArray * d_virused;
156 DArray * d_words;
157 DArray * d_vars; // hash_t **
158 char * S;
159 char ch;
160 int i;
162 if (n_trip > ENV->d_depth)
163 return;
165 if (n_trip == 1) {
166 tree_echo (0, f_name);
167 if (SB_FLGET (SB_FLLINEAR) || SB_FLGET (SB_FLVERBOSE))
168 nogui_tagsdump (f_name, 0);
171 d_tt_state = get_onett_tag (f_name, &d_ptr);
172 if (! d_tt_state) {
173 if (ENV->full_list->get_size () == 0)
174 bug_nosuch_tag (f_name);
176 return;
179 d_out = d_ptr;
180 d_virused = new EArray (256);
181 d_vars = new DArray (32);
183 ch = t_op (&d_ptr, &d_out);
184 if (ch == 0)
185 return;
187 fault (! d_out);
189 while (true) {
190 ch = t_op (&d_ptr, &d_out);
192 if (ch == 0)
193 break;
195 d_words = split_to_words (d_out);
196 assert (d_words == NULL, "Lena 28");
198 for (i = 0; i < d_words->get_size (); i++) {
199 S = cts ((struct c_word *) d_words->get (i));
201 if (S == NULL)
202 continue;
204 if (d_virused->sfind (S) != -1)
205 continue;
207 if (! SB_FLGET (SB_FLLINEAR))
208 tree_mkpos (n_trip);
210 d_virused->add (strdup (S));
211 tree_echo (n_trip, S);
213 if ((SB_FLGET (SB_FLVERBOSE) || SB_FLGET (SB_FLLINEAR))
214 && !tree_immune (S)) {
215 nogui_tagsdump (S, n_trip);
218 if (ENV->full_list->sfind (S) == -1) {
219 ENV->full_list->add (strdup (S));
220 if (!tree_immune (S))
221 call_tree (S, n_trip+1);
225 d_words->foreach ((Dfunc_t) free_cword);
226 delete d_words;
229 delete d_virused;
230 if (d_vars)
231 d_vars->foreach (free);
232 delete d_vars;