-
Notifications
You must be signed in to change notification settings - Fork 0
/
jp.c
202 lines (181 loc) · 4.85 KB
/
jp.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
// JP - search json with pcre2
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <argp.h>
#include "json.h"
const char *argp_program_version = "jp 1.0";
static char doc[] = "Search json with pcre2\nWhen [FILE] is -, read standard input";
static char args_doc[] = "FILE PCRE2_EXP";
static struct argp_option options[] =
{
{ "keys", 'k', 0, 0, "Search keys"},
{ "values", 'v', 0, 0, "Show results values"},
{ 0 }
};
static error_t parse_opt(int key, char * arg, struct argp_state * state)
{
struct arguments * arguments = state->input;
switch (key)
{
case 'k':
arguments->keys = 1;
break;
case 'v':
arguments->values = 1;
break;
case ARGP_KEY_ARG:
if (arguments->arg_count == 1)
{
arguments->pcre2 = arg;
}
else if (arguments->arg_count == 2)
{
if (*arg == '-')
{
arguments->stdin = 1;
}
else
{
arguments->filename = arg;
}
}
(arguments->arg_count)--;
break;
case ARGP_KEY_END:
{
if (arguments->arg_count >= 1)
{
argp_failure (state, 1, 0, "too few arguments");
}
else if (arguments->arg_count < 0)
{
argp_failure (state, 1, 0, "too many arguments");
}
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, "file" };
int main(int argc, char *argv[])
{
struct arguments arguments;
arguments.keys = 0;
arguments.values = 0;
arguments.stdin = 0;
arguments.arg_count = 2;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
FILE * fp;
struct stat filestatus;
int file_size;
char * file_contents;
json_char * json;
json_value * value;
char *str;
char *str_tmp;
if (arguments.stdin)
{
int c;
int i;
int size = 10;
str = malloc(size*sizeof(char));
for(i=0; (c=getchar()) !='\0' && c != EOF; ++i)
{
if( i == size)
{
size = 2*size;
str = realloc(str, size*sizeof(char));
if(str == NULL)
{
printf("Error Unable to Grow String! :(");
exit(-1);
}
}
str[i] = c;
}
if(i == size)
{
str_tmp = realloc(str, (size+1)*sizeof(char));
if(str_tmp == NULL)
{
printf("Error Unable to Grow String! :(");
exit(-1);
}
str = str_tmp;
}
str[i] = '\0';
json = (json_char*)str;
value = json_parse(json,size,arguments);
if (value == NULL)
{
fprintf(stderr, "Unable to parse data\n");
free(file_contents);
exit(1);
}
}
else
{
if ( stat(arguments.filename, &filestatus) != 0)
{
fprintf(stderr, "File %s not found\n", arguments.filename);
return 1;
}
file_size = filestatus.st_size;
file_contents = (char*)malloc(filestatus.st_size);
if ( file_contents == NULL)
{
fprintf(stderr, "Memory error: unable to allocate %d bytes\n", file_size);
return 1;
}
fp = fopen(arguments.filename, "rt");
if (fp == NULL)
{
fprintf(stderr, "Error opening file %s\n", arguments.filename);
fclose(fp);
free(file_contents);
return 1;
}
if ( fread(file_contents, file_size, 1, fp) != 1 )
{
fprintf(stderr, "Error reading file %s\n", arguments.filename);
fclose(fp);
free(file_contents);
return 1;
}
fclose(fp);
json = (json_char*)file_contents;
value = json_parse(json,file_size,arguments);
if (value == NULL)
{
fprintf(stderr, "Unable to parse data\n");
free(file_contents);
exit(1);
}
}
for (int i=0; i < value->jsonpath_results->jps_found_count; i++)
{
if (value->jsonpath_results->show_values)
{
printf("JP %d: %s\n", i, value->jsonpath_results->jps[i]);
printf("Value %d: %s\n", i, value->jsonpath_results->values[i]);
}
else
{
printf("%s\n", value->jsonpath_results->jps[i]);
}
}
json_value_free(value);
if (arguments.stdin)
{
free(str);
}
else
{
free(file_contents);
}
return 0;
}