Newer
Older
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
/*
** ast_print.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Sat Mar 25 23:11:01 2006 Seblu
** Last update Fri Sep 1 00:31:28 2006 Seblu
*/
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ast.h"
#define NODE_TYPE_COUNT 14
typedef void (*print_fct)(s_ast_node *, FILE *, unsigned int *);
struct ast_print_switch
{
e_node_type type;
print_fct fct;
};
struct ast_print_switch print_table[NODE_TYPE_COUNT] =
{
{T_IF, NULL}, // ast_if_print},
{T_FOR, NULL}, //ast_for_print},
{T_WHILE, NULL}, //ast_while_print},
{T_UNTIL, NULL}, //ast_until_print},
{T_CMD, ast_cmd_print},
{T_AND, NULL}, //ast_and_print},
{T_OR, NULL}, //ast_or_print},
{T_SUBSHELL, NULL}, //ast_subshell_print},
{T_FUNCDEC, NULL}, //ast_funcdec_print},
{T_BANG, NULL}, //ast_bang_print},
{T_PIPE, NULL}, //ast_pipe_print},
{T_SEPAND, NULL}, //ast_sepand_print},
{T_SEP, NULL}, //ast_sep_print},
{T_CASE, NULL} //ast_sepand_print}
};
static char *newastfilename(void);
void ast_print(s_ast_node *ast, const char *filename)
{
FILE *fs;
unsigned int index;
if (ast == NULL)
return;
//open file stream
if (!filename)
filename = newastfilename();
if (!(fs = fopen(filename, "w")))
return;
//write dot header
fprintf(fs, "digraph \"42sh-ast\" {\n");
fprintf(fs, "node [fontname=Vera, color=lightblue2, style=filled];\n");
//start ast node wrinting
ast_print_node(ast, fs, &index);
//write dot foot and close
fprintf(fs, "}");
fclose(fs);
}
void ast_print_node(s_ast_node *ast, FILE *fs, unsigned int *node_id)
{
for (register int i = 0; i < NODE_TYPE_COUNT; ++i)
if (print_table[i].type == ast->type)
(print_table[i].fct)(ast, fs, node_id);
}
static char *newastfilename(void)
{
static char buf[PATH_MAX];
time_t st;
struct tm *t;
struct stat buf2;
int more = 0;
st = time(NULL);
t = localtime(&st);
do
snprintf(buf, 256, "/tmp/42sh-ast-%d-%d-%d-%d-%d-%d--%d.dot", 1900 + t->tm_year,
1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, more++);
while (stat(buf, &buf2) != -1 && more < 50);
return buf;
}