forked from amboar/tceetree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tceetree.c
365 lines (320 loc) · 9.27 KB
/
tceetree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* This source code is released for free distribution under the terms of the MIT
* License (MIT):
*
* Copyright (c) 2014, Fabio Visona'
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _ALL_IN_ONE
#include "defines.h"
#include "gettree.h"
#include "outtree.h"
#include "slib.h"
#include "ttree.h"
#include "ttreeparam.h"
#endif // _ALL_IN_ONE
const char sversion[] = "1.0.1"; // software version
const char sdefaultoutfile[] = "tceetree.out"; // default output file
// setting of string parameters
int paramstr(char **sout, char const *sin) { return slibcpy(sout, sin, -3); }
// setting of string array parameters
int paramstrarr(char **sout, int *outidx, int maxnum, char const *sin,
char const *errmsg)
{
if (*outidx >= maxnum) {
printf(errmsg, maxnum);
return -3;
}
return slibcpy(&sout[(*outidx)++], sin, -3);
}
// setting of default parameters
void paramdefault(treeparam_t *ptreeparam)
{
memset(ptreeparam, 0, sizeof(treeparam_t));
ptreeparam->fdepth =
-1; // default for called functions depth is maximum
paramstr(&ptreeparam->infile,
"cscope.out"); // if no input file is specified, default is
// "cscope.out"
paramstr(&ptreeparam->outfile, sdefaultoutfile); // default output file
paramstr(&ptreeparam->shortdbfile, ""); // default shortened output file
ptreeparam->outtype =
TREEOUT_GRAPHVIZ; // default is output for graphviz
}
// parameter cross checks
int paramcrosscheck(treeparam_t *ptreeparam)
{
if (strcmp(ptreeparam->infile, ptreeparam->outfile) == 0) {
printf(
"\nThe input file cannot be the same as the output file\n");
return -1;
}
if (strcmp(ptreeparam->infile, ptreeparam->shortdbfile) == 0) {
printf("\nThe input file cannot be the same as the shortened "
"cscope output "
"file\n");
return -1;
}
if (strcmp(ptreeparam->outfile, ptreeparam->shortdbfile) == 0) {
printf("\nThe output file cannot be the same as the shortened "
"cscope "
"output file\n");
return -1;
}
return 0;
}
// free parameters memory
void paramfree(treeparam_t *ptreeparam)
{
int i;
free(ptreeparam->infile);
free(ptreeparam->outfile);
free(ptreeparam->shortdbfile);
free(ptreeparam->callp);
for (i = 0; i < ptreeparam->rootno; i++)
free(ptreeparam->root[i]);
for (i = 0; i < ptreeparam->excludfno; i++)
free(ptreeparam->excludf[i]);
}
// print usage help
void usage(void)
{
printf("\n");
printf("Usage: tceetree [-c <depth>] [-C <depth>] [-d <file>] [-f] "
"[-F] [-h]\n"
" [-i <file>] [-o <file>] [-p <function>] [-r "
"<root>]\n"
" [-s <style>] [-v] [-V] [-x <function>]\n\n");
printf("-c <depth> Depth of tree for called functions: default is "
"max.\n");
printf("-C <depth> Depth of tree for calling functions: default is "
"0.\n");
printf("-d <file> Output a shortened cscope output file: default "
"is no "
"output.\n"
" The shortened file includes only function "
"information and "
"can\n"
" be used as input (-i) for following calls to "
"tceetree to\n"
" increase speed on big projects.\n");
printf("-f Print the file name where the call is near to "
"branch.\n");
printf("-F Group functions into one cluster for each source "
"file.\n");
printf("-h Print this help.\n");
printf(
"-i <file> Input cscope output file: default is cscope.out.\n");
printf("-o <file> Output file for graphviz: default is %s.\n",
sdefaultoutfile);
printf("-p <function> Highlight call path till function.\n");
printf(
"-r <root> Root function of tree: default is main. This option "
"may occur\n"
" more than once for multiple roots (max %d).\n",
TT_MAXROOTS);
printf("-s <style> Style for highlight call path:\n"
" - 0 = red color (default);\n"
" - 1 = blue color;\n"
" - 2 = green color;\n"
" - 3 = bold;\n"
" - 4 = dashed;\n"
" - 5 = dotted.\n");
printf("-v Print version.\n");
printf("-V Verbose output.\n");
printf(
"-x <function> Function to be excluded from tree. This option may "
"occur more\n"
" than once for multiple functions (max %d).\n"
" -x %s is a special case for excluding all library\n"
" functions, i.e. not found defined in any file.\n",
TT_MAXEXCLUDF, TT_LIBRARY);
}
// decoding of inline parameters
int usage_opt(char const *sopt, treeparam_t *ptreeparam)
{
static char curopt =
0; // holds option currently being parsed (e.g. 'd')
int iErr = 0;
int isoptval; // = 1 when decoding value of option
if (curopt == 0 && (sopt[0] != '-' || strlen(sopt) != 2))
iErr = -1;
else {
isoptval = (curopt != 0);
if (!isoptval)
curopt = sopt[1];
switch (curopt) {
case 'c':
if (isoptval) {
if (strcmp(sopt, "max") == 0)
ptreeparam->fdepth = -1;
else if (sscanf(sopt, "%d",
&ptreeparam->fdepth) != 1 ||
ptreeparam->fdepth < 0) {
printf("\nCallees depth must be a "
"number >= 0 or \"max\"\n");
iErr = -3;
}
curopt = 0;
}
break;
case 'C':
if (isoptval) {
if (strcmp(sopt, "max") == 0)
ptreeparam->bdepth = -1;
else if (sscanf(sopt, "%d",
&ptreeparam->bdepth) != 1 ||
ptreeparam->bdepth < 0) {
printf("\nCallers depth must be a "
"number >= 0 or \"max\"\n");
iErr = -3;
}
curopt = 0;
}
break;
case 'd':
if (isoptval) {
iErr = paramstr(&ptreeparam->shortdbfile, sopt);
curopt = 0;
}
break;
case 'f':
ptreeparam->printfile = 1;
curopt = 0;
break;
case 'F':
ptreeparam->doclusters = 1;
curopt = 0;
break;
case 'h':
usage();
iErr = -2;
curopt = 0;
break;
case 'i':
if (isoptval) {
iErr = paramstr(&ptreeparam->infile, sopt);
curopt = 0;
}
break;
case 'o':
if (isoptval) {
iErr = paramstr(&ptreeparam->outfile, sopt);
curopt = 0;
}
break;
case 'p':
if (isoptval) {
iErr = paramstr(&ptreeparam->callp, sopt);
curopt = 0;
}
break;
case 'r':
if (isoptval) {
iErr = paramstrarr(ptreeparam->root,
&ptreeparam->rootno,
TT_MAXROOTS, sopt,
"\nThe maximum number of "
"root functions is %d\n");
curopt = 0;
}
break;
case 's':
if (isoptval) {
if (sscanf(sopt, "%d", &ptreeparam->hlstyle) !=
1 ||
ptreeparam->hlstyle < 0 ||
ptreeparam->hlstyle >= TT_MAXSTYLES) {
printf("\nHighlight style must be a "
"number >= 0 and < %d\n",
TT_MAXSTYLES);
iErr = -3;
}
curopt = 0;
}
break;
case 'v':
printf("\n%s\n", sversion);
iErr = -2;
curopt = 0;
break;
case 'V':
ptreeparam->verbose = 1;
curopt = 0;
break;
case 'x':
if (isoptval) {
iErr = paramstrarr(
ptreeparam->excludf, &ptreeparam->excludfno,
TT_MAXEXCLUDF, sopt, "\nThe maximum number "
"of excluded "
"functions is %d\n");
curopt = 0;
}
break;
default:
iErr = -1;
break;
}
}
if (iErr == -1)
printf("\nInvalid option %s\n", sopt);
return iErr;
}
int main(int argc, char *argv[])
{
treeparam_t treeparam;
ttree_t *ttree;
int i;
int iErr = 0;
// set parameters to default
paramdefault(&treeparam);
// decode inline options
for (i = 1; i < argc; i++) {
iErr = usage_opt(argv[i], &treeparam);
if (iErr != 0)
break;
}
if (iErr == 0)
iErr = paramcrosscheck(&treeparam);
if (iErr == 0) {
if (treeparam.rootno == 0) {
paramstr(&treeparam.root[0],
"main"); // if no root is specified then start
// from main
treeparam.rootno = 1;
}
ttree = ttreeinit(); // initialize tree
// read cscope file and get the whole tree
iErr = gettree(ttree, &treeparam);
if (iErr == 0)
// make subtree output according to options
iErr = outtree(ttree, &treeparam);
ttreedestroy(ttree); // free tree memory
}
if (iErr == -2)
iErr = 0;
paramfree(&treeparam); // free parameters memory
//// printf("\ntceetree returns with: %d\n", iErr);
return iErr;
}