forked from shenyuflying/pgNodeGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode2dot.c
488 lines (391 loc) · 9.19 KB
/
node2dot.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* node2dot
*
* Convert a node tree into dot format
*
* yshen 2016
*
* initial version 2016-9-19 12:00
* last update 2016-9-19 add parmeter --skip-empty --color
*/
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<getopt.h>
#define LENGTH 1024
#define bool char
#define true 1
#define false 0
typedef struct Elem {
char name[LENGTH];
} Elem;
typedef struct Node {
char name[LENGTH];
Elem elems[LENGTH];
char links[LENGTH][LENGTH];
int links_count;
char * color;
} Node;
/*
* node_num and elem_num
*/
int node_num = 0;
int node_cnt = 0;
int elem_num = 0;
#define new(x) (++x)
#define set(var,value) (var=value)
#define get(x) (x)
/*
* a mini-stack
*/
int *stack = NULL;
int top = 0;
int push(int var)
{
stack[top++] = var;
}
int pop()
{
return stack[--top];
}
/*
* globals
*/
Node nodes[LENGTH];
int add_node(int num, char *name);
int add_item(int node_n, int elem_n, char *name);
int add_link(int parent_node_num, int parent_elem_num, int child_node_num);
int print_header(void);
int print_footer(void);
int print_body(void);
char * get_one_name();
char *progname = "node2dot";
/*
* options
*/
bool is_skip_empty = false;
bool is_color = false;
char * skip_node_name = NULL;
void usage()
{
printf("\n\nConvert node tree of postgres to dot format\n"
" yshen 2016\n\n\n"
"Usage:%s [options] <filein >fileout\n"
"Valid Options:\n"
"--skip-empty do not print element if is 0 or null or false\n"
"--skip-node nodename do not print node named nodename\n"
"--color enable color output\n"
"--help show this help message\n"
, progname);
}
int main(int argc, char *argv[])
{
char buffer[LENGTH];
int parent_node_num;
int parent_elem_num;
int child_node_num;
int c;
int digit_optind = 0;
/*
* level indicates if we are in a struct context or not
* if we are inside a struct level > 0 else level <= 0
* if we are outside a struct, keep on consuming stdin
* until we found the '{' marking as the beginning of
* a struct
*
* we add this because
* 1. we can parse more than one node tree in a file
* 2. if there are anything else such as LOG: statement
* we can safely ignore then. in the old days, those
* thing have to be removed by hand.
* */
int level = 0;
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] =
{
/* name has_arg flag val*/
{"help", no_argument, 0, 0 },
{"skip-empty",no_argument, 0, 0 },
{"color", no_argument, 0, 0 },
{"skip-node", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "h",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
if (strcmp("help",long_options[option_index].name) == 0)
{
usage();
exit(0);
}
if (strcmp("skip-empty",long_options[option_index].name) == 0)
{
is_skip_empty = 1;
fprintf(stderr,"%s:skip empty elements\n",progname);
}
if (strcmp("color",long_options[option_index].name) == 0)
{
is_color = true;
fprintf(stderr,"%s:color output\n",progname);
}
if (strcmp("skip-node",long_options[option_index].name) == 0)
{
skip_node_name = strdup(optarg);
fprintf(stderr,"%s:skip mode named:%s\n",progname,skip_node_name);
}
break;
case 'h':
usage();
exit(0);
break;
case '?':
usage();
exit(0);
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
#if 0
if (optind < argc)
{
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
#endif
}
/*
* in previous we alloc stack on stack which raise an link error
* relocation truncated to fit: R_X86_64_PC32 against
* symbol `stack' defined in COMMON section
* to fix this alloc it in heap using malloc
*/
stack = (int*)malloc(sizeof(int)*LENGTH);
print_header();
while (EOF != (c = getc(stdin)))
{
char * name = NULL;
switch(c)
{
case '{': /* a new node*/
level++;
parent_node_num = get(node_num);
parent_elem_num = get(elem_num);
push(parent_node_num); /* push node_num first*/
push(parent_elem_num); /* push elem_num second */
set(elem_num,0); /* the new node elem_num start at 0 */
add_node(new(node_cnt),get_one_name()); /* node_cnt always ++*/
set(node_num,get(node_cnt)); /* set the node_num */
child_node_num = get(node_num);
if (get(node_cnt) != 0)
{
add_link(parent_node_num,
parent_elem_num,
child_node_num); /* if on sub level add links */
}
break;
case '}': /* end of the node */
/* we are not in a struct */
if (level <= 0)
continue;
/* restore the parent */
set(elem_num,pop()); /* pop elem_num first */
set(node_num,pop()); /* pop node_num second */
/* we are out if the stack */
level--;
break;
case ':': /* a new item */
/* we are not in a struct */
if (level <= 0)
continue;
name = get_one_name();
if (name != NULL)
add_item(get(node_num),
new(elem_num),
name);
break;
defalut:
/* if we are not in struct consume input until we meet one */
if (level <= 0)
{
while(EOF!= (c = getc(stdin)) && c !='{' )
;
/* put it back */
ungetc('{',stdin);
}
break;
}
}
print_body();
print_footer();
}
int print_header(void)
{
printf("digraph Query {\n");
printf("size=\"100000,100000\";\n");
printf("rankdir=LR;\n");
printf("node [shape=record];\n");
}
int add_node(int num, char *name)
{
int i,j;
#ifdef DEBUG
printf("add_node(%s)\n", name);
#endif
memcpy(nodes[num].name, name, LENGTH);
if (!is_color)
{
nodes[num].color = "black";
}
else
{
if (strstr(name,"QUERY"))
{
nodes[num].color = "red";
}
else if (strstr(name,"RTE"))
{
nodes[num].color = "blue";
}
else if (strstr(name,"TARGETENTRY"))
{
nodes[num].color = "orange";
}
else if (strstr(name,"RELOPTINFO"))
{
nodes[num].color = "green";
}
else
{
nodes[num].color = "black";
}
}
free(name);
for (i = 0; i < LENGTH; i++)
{
nodes[num].links_count = 0;
/* clear all the item name */
for (j = 0; j < LENGTH; j++)
memset(nodes[num].elems[j].name,0,LENGTH);
}
}
int print_footer(void)
{
printf("\n}");
}
int add_item(int node_n, int elem_n, char *name)
{
#ifdef DEBUG
printf("add_item(node_n=%d, elem_n=%d, name=%s)\n", node_n, elem_n, name);
#endif
memcpy(nodes[node_n].elems[elem_n].name, name, LENGTH);
free(name);
}
/*
* get one node or element name from stdin
* if name is ok return the name
* otherwise return NULL
*/
char * get_one_name()
{
char * buffer = (char*)malloc(LENGTH);
int i=0;
int c=0;
int len=0;
bool found_zero=false, found_non_zero=false;
char * pos;
memset(buffer,0,LENGTH);
while((c=getc(stdin)) != ':' && c!='{' && c!='}')
buffer[i++]=c;
/* push back the token */
ungetc(c,stdin);
len = strlen(buffer);
/* remove any illeagal char of dot format */
for (i = 0; i < len; i++)
{
if (buffer[i]=='"')
buffer[i]=' ';
if (buffer[i]=='<' ||
buffer[i]=='>')
buffer[i]='-';
if (buffer[i] == '0')
found_zero = true;
if (buffer[i] > '1' && buffer[i] < '9')
found_non_zero = true;
}
if ((pos = strstr(buffer,"(")) && !strstr(buffer,")"))
{
pos[0] = ' ';
}
if (is_skip_empty)
{
if (found_zero && !found_non_zero)
{
free(buffer);
return NULL;
}
}
if (is_skip_empty && strstr(buffer,"false"))
{
free(buffer);
return NULL;
}
if (is_skip_empty && strstr(buffer,"--"))
{
free(buffer);
return NULL;
}
return buffer;
}
int print_body(void)
{
int i, j;
/* print the nodes */
for (i = 0; i <= get(node_cnt); i++)
{
if (strcmp(nodes[i].name,"") == 0)
continue;
if (skip_node_name && strstr(nodes[i].name, skip_node_name))
continue;
printf("node%d [shape=record, color=%s, label=\"<f0> %s | ",
i, nodes[i].color, nodes[i].name);
for (j = 1; j < LENGTH; j++)
{
if (strlen(nodes[i].elems[j].name) != 0)
printf("<f%d> %s ", j, nodes[i].elems[j].name);
else
break;
if (strcmp(nodes[i].elems[j+1].name,"") != 0)
printf("| ");
}
printf("\"];\n");
}
/* print the links */
for (i = 0; i < get(node_cnt); i++)
{
if (strcmp(nodes[i].name,"") == 0)
continue;
if (skip_node_name && strstr(nodes[i].name, skip_node_name))
continue;
for (j = 0; j < nodes[i].links_count; j++)
printf("%s", nodes[i].links[j]);
}
}
int add_link(int parent_node_num, int parent_elem_num, int child_node_num)
{
sprintf(nodes[parent_node_num].links[nodes[parent_node_num].links_count++],
"node%d:f%d -> node%d:f%d\n",
parent_node_num,
parent_elem_num,
child_node_num,
(int)0);
}