-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
162 lines (143 loc) · 3.45 KB
/
main.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
#include "main.h"
#define STDIN_OPTION_POSITION 1
#define STANDARD_OPTION_POSITION 2
void read_lines_and_render(FILE *fp)
{
int CUR_LINE_NUM = 0;
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, fp)) != -1)
{
CUR_LINE_NUM++;
if (strcmp(line, "\n") && line[0] != ';')
{
#ifdef CLI
if (SHOW_LOG > 1)
{
int line_s = strlen(line) + 3;
char sep[line_s];
memset(sep, '-', line_s);
sep[line_s] = '\0';
printf(KGRN"%s\n", sep);
printf(KBLU" +"KCYN" %s\n"RESET, line);
}
#endif
line[strlen(line)-1] = '\0';
parse_line(line, CUR_LINE_NUM);
}
}
render();
fclose(fp);
if (line)
free(line);
}
void readfile(char *filename)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("file not found \n");
exit(0);
}
read_lines_and_render(fp);
}
void read_stdin()
{
FILE *fp = fdopen(STDIN_FILENO, "r");
if (fp == NULL)
{
printf("stdin not found \n");
exit(0);
}
read_lines_and_render(fp);
}
int main(int argc, char *argv[])
{
SHOW_LOG = 0;
prefix = NULL;
suffix = NULL;
printRaw = false;
bool is_text = false;
bool is_stdin_disabled = isatty(0);
#ifdef CHECK_UPDATE
if (argc == 1)
{
check_version();
printf("sequence-diagram-cli %s\ngithub@Ja-sonYun, email: jason@abex.dev\ngithub: https://github.com/Ja-sonYun/sequence-diagram-cli\nwebsite: https://abex.dev\n", VERSION);
return 0;
}
printf("*** sequence-diagram-cli %s, github@Ja-sonYun ***\n", VERSION);
#endif
int option_starting_pos = is_stdin_disabled ? STANDARD_OPTION_POSITION : STDIN_OPTION_POSITION;
for (int i = option_starting_pos; i < argc; i++)
{
if (!strncmp(argv[i], "log=", 4))
{
if (strlen(argv[i]) == 5 && (argv[i][4] == 1 || argv[i][4 == 0]))
{
SHOW_LOG = argv[i][4] - 48;
}
else
{
printf(KRED"wrong log option!\n"RESET);
}
}
else if (!strcmp(argv[i], "al"))
{
is_text = true;
}
else if (!strcmp(argv[i], "raw"))
{
printRaw = true;
}
else if (!strncmp(argv[i], "prefix=", 7))
{
if (strlen(argv[i]) > 7)
{
prefix = &argv[i][7];
}
else
{
printf(KRED"wrong prefix option!\n"RESET);
}
}
else if (!strncmp(argv[i], "suffix=", 7))
{
if (strlen(argv[i]) > 7)
{
suffix = &argv[i][7];
}
else
{
printf(KRED"wrong suffix option!\n"RESET);
}
}
else
{
printf(KRED"wrong option!\n"RESET);
}
}
set_style(is_text);
bool file_exists = access(argv[1], F_OK) == 0;
if (!file_exists && is_stdin_disabled)
{
printf("file not found \n");
}
else if (file_exists && !is_stdin_disabled)
{
printf("Undefined behaviour: stdin and file specified\n");
}
else if (is_stdin_disabled)
{
readfile(argv[1]);
}
else
{
read_stdin();
}
#ifdef CHECK_UPDATE
check_version();
#endif
return 0;
}