-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
135 lines (113 loc) · 3.05 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
/*
jsonval - validates JSON files for well-formedness
Copyright (C) 2007 Ben Spencer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "config.h"
#include "json_parser.h"
#include "json_scanner.h"
extern int yyparse(yyscan_t scanner);
unsigned char parse_and_report(const char *);
int xml_output = 0;
void usage() {
printf("Usage: %s [options] [/path/to/file]\n", PACKAGE);
printf("\t\t(if file is not specified or -, read from stdin)\n");
printf("\nOPTIONS:\n");
printf("-x\t\tOutput in jslint-esque XML\n");
printf("-h\t\tShow this help\n");
printf("-v\t\tShow version number\n");
exit(0);
}
int main(int argc, char *argv[])
{
unsigned char err = 0;
int c;
while((c = getopt(argc, argv, "hvx")) != -1) {
switch(c) {
case 'h':
usage();
break;
case 'v':
printf("%s %s\n", PACKAGE, PACKAGE_VERSION);
exit(0);
break;
case 'x':
xml_output = 1;
break;
}
}
if(xml_output) printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<jslint>\n");
if((argc - optind) == 0) err = parse_and_report(NULL);
else {
unsigned int i;
for(i = optind; i < argc; i++) {
if(parse_and_report(argv[i])) err = 1;
}
}
if(xml_output) printf("</jslint>\n");
return err;
}
unsigned char parse_and_report(const char *filename)
{
yyscan_t scanner;
FILE *f;
unsigned char err = 0;
if(filename == NULL || strcmp(filename, "-") == 0) {
f = stdin;
filename = "stdin";
} else {
f = fopen(filename, "r");
if(!f) {
perror(filename);
return 1;
}
}
yylex_init(&scanner);
yyset_in(f, scanner);
yyset_extra((void *) filename, scanner);
#ifdef DEBUG
yyset_debug(1, scanner);
#endif
if(xml_output) printf("\t<file name=\"%s\">\n", filename);
switch(yyparse(scanner)) {
case 0:
if(!xml_output) printf("%s: valid JSON\n", filename);
break;
case 1:
if(!xml_output) printf("%s: parsing failed\n", filename);
err = 1;
break;
case 2:
fprintf(stderr, "Out of memory\n");
exit(1);
break;
}
if(xml_output) printf("\t</file>\n");
yylex_destroy(scanner);
if(f != stdin) fclose(f);
return err;
}
void yyerror(yyscan_t scanner, const char *error)
{
if(xml_output) {
printf("\t\t<issue line=\"%d\" char=\"\" issue=\"%s\" evidence=\"\"/>\n",
yyget_lineno(scanner), error);
} else {
fprintf(stderr, "%s: %s at line %d\n",
(char *) yyget_extra(scanner),
error, yyget_lineno(scanner));
}
}