-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
194 lines (177 loc) · 4.69 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
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
static void
yperror (const char *s, const char *tok)
{
fprintf(stderr, "Oops! expected %s (last seen: %s)\n", s, tok);
}
static void
modname_chk (const char *modname)
{
char *file = strdup(G.filename);
char *name = basename(file);
char *p = strrchr(name, '.');
*p = '\0';
if (!STREQ(name, modname)) {
fprintf(stderr, "Error: module name '%s' not same as file name '%s'\n",
modname, name);
G.errcnt++;
}
}
static void
linkage_seen (const char *stmt)
{
G.linkage_seen = true;
if (G.meta_seen) {
fprintf(stderr, "Error: linkage-statement (%s) should be "
"placed before meta-stmts(org/contact/desc/rev)", stmt);
G.errcnt++;
}
if (G.revision_seen) {
fprintf(stderr, "Error: linkage-statement (%s) should be "
"placed before revisions\n", stmt);
G.errcnt++;
}
}
static void
meta_seen (const char *stmt)
{
G.meta_seen = true;
if (G.revision_seen) {
fprintf(stderr, "Error: meta-statement (%s) should be "
"before revision(s)\n", stmt);
}
}
static void
revision_seen ()
{
G.revision_seen = true;
}
static void
revision_date_chk (const char *r)
{
static char last_rev[12] = {0};
if (last_rev[0] == '\0') {
strcpy(last_rev, r);
} else {
if (strcmp(last_rev, r) < 1) {
fprintf(stderr, "revision dates not in reverse chronological"
" order (%s > %s)\n", r, last_rev);
G.errcnt++;
}
}
}
static void
ensure_single (int c, const char *m)
{
#define MAX 10
static int a[MAX] = {0};
assert(c < MAX);
a[c]++;
if (a[c] > 1) {
fprintf(stderr, "Error: %s can occur only once\n", m);
G.errcnt++;
}
}
static void
yyerror (char *message)
{
fprintf(stderr, "%s:%d: %s", G.filename, G.linenumber, message);
if (yyctx->__text[0]) fprintf(stderr, " near token '%s'", yyctx->__text);
if (yyctx->__pos < yyctx->__limit || !feof(G.fp)) {
yyctx->__buf[yyctx->__limit]= '\0';
fprintf(stderr, " before text \"");
while (yyctx->__pos < yyctx->__limit) {
if ('\n' == yyctx->__buf[yyctx->__pos]
|| '\r' == yyctx->__buf[yyctx->__pos]) break;
fputc(yyctx->__buf[yyctx->__pos++], stderr);
}
if (yyctx->__pos == yyctx->__limit) {
int c;
while ((c = fgetc(G.fp)) != EOF && c != '\n' && c != '\r')
fputc(c, stderr);
}
fputc('\"', stderr);
}
fprintf(stderr, "\n");
exit(1);
}
/* Command-line option handling */
static struct option long_options[] =
{
{"path" , required_argument, 0, 'p'}, // TODO
{"output", required_argument, 0, 'o'},
{"help" , no_argument , 0, 'h'},
{0, 0, 0, 0}
};
struct opt_help_ {
char *arg_help;
char *long_help;
} opt_help[] = {
{"<path>", "Path to find module/submodules (colon delimited list)"}, /* TODO */
{"<file>", "Write output to file"} ,
{NULL , "Display this help and exit"} ,
{NULL, NULL}
};
static
void print_usage (char *s)
{
int i = 0;
printf("\nUsage: %s [OPTIONS] [FILE]+\n\n", basename(s));
while (long_options[i].name) {
printf(" -%c, --%-15s %-18s %s\n", long_options[i].val,
long_options[i].name,
opt_help[i].arg_help ? opt_help[i].arg_help : "",
opt_help[i].long_help);
i++;
}
printf("\n");
}
int
main (int argc, char *argv[])
{
int i, gc;
int option_index = 0;
while (1) {
gc = getopt_long(argc, argv, "ho:p:", long_options, &option_index);
if (gc == -1) /* Detect the end of the options. */
break;
switch (gc) {
/* TODO */
case 'o':
break;
case 'p':
break;
case '?':
/* getopt_long already printed an error message. */
case 'h':
print_usage(argv[0]);
exit(0);
default:
abort();
}
}
if (optind >= argc) {
fprintf(stderr, "No input files\n");
print_usage(argv[0]);
exit(0);
}
for (i = optind; i < argc; i++) {
G.fp = fopen(argv[i], "r");
if (!G.fp) {
perror(argv[i]);
exit(1);
}
G.filename = argv[i];
if (!yang_parse()) {
yyerror("Syntax error");
fclose(G.fp);
return 1;
}
fclose(G.fp);
}
if (G.errcnt > 0) {
fprintf(stderr, "%d errors seen\n", G.errcnt);
} else {
printf("Parse OK\n");
}
return 0;
}