Very old versions for history.
[opsoft_archive.git] / silentbob / silent_bob / tree.cpp
blob84efb2855d0ddafdd90fd656eaace3ffa888e3d1
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
4 *
5 */
7 #include "functions.h"
8 #include "dbg.h"
9 #include <sys/mman.h>
11 EArray * d_calltags_file;
14 * "SilentBob -u" functionality.
15 * KOI8-R, RUS:
17 * òÅËÕÒÓÉ×ÎÏ ÉÓËÁÔØ ÚÁÐÉÓÉ × ÆÁÊÌÅ call_tags.
18 * æÕÎËÃÉÑ ×ÙÚÙ×ÁÅÔÓÑ ÔÅÍ, ÞÔÏ ÚÎÁÞÉÔÓÑ × ÚÁÐÉÓÉ ÐÏÓÌÅ "by".
22 void rctree_iter (char * f_name, int n_trip)
24 EArray * d_virused;
25 char m_buf[256];
26 char * S;
27 int len;
28 int i;
29 int a;
31 n_trip ++;
32 if (n_trip > d_depth)
33 return;
35 /*
36 * 1. Find record in d_calltags_file.
37 * 2. Find _first_ record in d_calltags_file.
38 * 3. Parse record.
39 * 4. Check d_virused list.
40 * 5. If f_name not in full_list :
41 * 5.1 Add f_name to full_list.
42 * 5.2 New iteration.
46 snprintf (m_buf, 255, "%s\t", f_name);
47 m_buf[255] = 0;
48 len = strlen (m_buf);
49 i = d_calltags_file->snfind_fast (m_buf);
50 d_virused = new EArray (32);
52 do {
53 i--;
54 if (i < 0)
55 break;
56 S = d_calltags_file->get (i);
57 if (! S)
58 break;
59 } while (! strncmp (S, m_buf, len));
61 do {
62 i++;
63 S = d_calltags_file->get (i);
64 if (! S || strncmp (S, m_buf, len))
65 break;
67 S = strstr (S, "\tby\t");
68 if (S == NULL) {
69 fprintf (d_stream_dbg, "\tBUG (no \"by\") : %s\n",
70 d_calltags_file->get (i));
71 continue;
74 S += 4;
75 chomp (S);
77 if (d_virused->sfind (S) != -1)
78 continue;
80 d_virused->add (strdup (S));
81 for (a = 0; a < n_trip; a++)
82 fputc ('\t', stdout);
84 tree_echo (n_trip, S);
86 if (full_list->sfind (S) == -1) {
87 full_list->add (strdup (S));
88 rctree_iter (S, n_trip);
91 } while (true);
93 d_virused->foreach (free);
94 delete d_virused;
97 void reverse_calltree (char * f_name)
99 assert (f_name == NULL, "Lena 2");
100 if (! f_name)
101 return;
103 tree_echo (0, f_name);
104 d_calltags_file = new EArray;
105 d_calltags_file->from_file ("./call_tags");
106 if (d_calltags_file->get_size () == 0) {
107 bug_nocalltags ();
110 rctree_iter (f_name, 0);
112 d_calltags_file->foreach (free);
113 delete d_calltags_file;
114 fflush (stdout);
117 void tree_echo (int n_trip, char *S)
119 struct d_tag_t * tag;
120 EArray * tags;
122 if (! SB_FLGET (SB_FLFNAMES))
123 goto out_min;
124 else {
125 tags = got_tag_ctags (S);
126 if (! tags || (tags->get_size () == 0))
127 goto out_min;
129 tag = (struct d_tag_t *) tags->get (0);
130 if (! tag)
131 goto out_min;
133 printf ("// %i // %s\t%s", n_trip, S, tag->d_file);
134 if (tags->get_size () > 1)
135 printf (" (more)");
136 fputc ('\n', stdout);
139 return;
141 out_min:
142 printf ("// %i // %s\n", n_trip, S);
145 inline void tree_mkpos (int N)
147 while (N--)
148 fputc ('\t', stdout);
152 * SilentBob call-tree functionality.
154 * KOI8-r, RUS:
155 * òÅËÕÒÓÉ×ÎÏ ÏÂÒÁÂÏÔÁÔØ ÆÕÎËÃÉÉ. ðÏÌÕÞÉÔØ ÆÕÎËÃÉÀ × ÆÁÊÌ ./silent_bob.tmp,
156 * ÐÒÏÐÕÓÔÉÔØ ÞÅÒÅÚ THE_TT, ÒÁÚÂÉÔØ ÎÁ ÏÐÅÒÁÔÏÒÙ. äÌÑ ËÁÖÄÏÇÏ ÏÐÅÒÁÔÏÒÁ
157 * ÓÌÏ×Ï + ÓËÏÂËÁ - ×ÙÚÏ×. éÓËÌÀÞÅÎÉÅ - ÏÂÙÞÎÙÅ óÉ ÏÐÅÒÁÔÏÒÙ.
161 void call_tree (char * f_name, int n_trip)
163 struct tt_state_t * d_tt_state;
164 char * d_ptr, *d_out;
165 EArray * d_virused;
166 DArray * d_words;
167 DArray * d_vars; // hash_t **
168 struct hash_t * one;
169 char * S;
170 char ch;
171 int i;
173 if (n_trip > d_depth)
174 return;
176 if (n_trip == 1) {
177 tree_echo (0, f_name);
178 if (SB_FLGET (SB_FLLINEAR) || SB_FLGET (SB_FLVERBOSE))
179 nogui_tagsdump (f_name, 0);
182 d_tt_state = get_onett_tag (f_name, &d_ptr);
183 if (! d_tt_state) {
184 if (full_list->get_size () == 0)
185 bug_nosuch_tag (f_name);
187 return;
190 d_out = d_ptr;
191 d_virused = new EArray (256);
192 d_vars = new DArray (32);
194 ch = t_op (&d_ptr, &d_out);
195 if (ch == 0)
196 return;
198 fault (! d_out);
200 while (true) {
201 ch = t_op (&d_ptr, &d_out);
203 if (ch == 0)
204 break;
206 d_words = split_to_words (d_out);
207 assert (d_words == NULL, "Lena 28");
209 for (i = 0; i < d_words->get_size (); i++) {
210 S = cts ((struct c_word *) d_words->get (i));
212 if (S == NULL)
213 continue;
215 if (d_virused->sfind (S) != -1)
216 continue;
218 if (! SB_FLGET (SB_FLLINEAR))
219 tree_mkpos (n_trip);
221 d_virused->add (strdup (S));
222 tree_echo (n_trip, S);
224 if ((SB_FLGET (SB_FLVERBOSE) || SB_FLGET (SB_FLLINEAR))
225 && !tree_immune (S)) {
226 nogui_tagsdump (S, n_trip);
229 if (full_list->sfind (S) == -1) {
230 full_list->add (strdup (S));
231 if (!tree_immune (S))
232 call_tree (S, n_trip+1);
236 d_words->foreach ((Dfunc_t) free_cword);
237 delete d_words;
240 delete d_virused;
241 if (d_vars)
242 d_vars->foreach (free);
243 delete d_vars;