-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2shell.c
153 lines (136 loc) · 3.73 KB
/
json2shell.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
//
// Created by paul on 5/15/21.
//
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <cjson/cJSON.h>
#include "json2shell.h"
int processTree( cJSON * json )
{
int result = 0;
if ( !cJSON_IsObject(json) )
{
fprintf( stderr, "Internal error: expected object at JSON root.\n");
result = -1;
}
else
{
result = emitObject( json->child );
}
return result;
}
int processArg( char * arg )
{
int result = 0;
int fd = open( arg, O_RDONLY );
if ( fd < 0 )
{
result = errno;
fprintf( stderr, "Unable to open \'%s\' (%d: %s)\n", arg, errno, strerror(errno));
}
else
{
struct stat fileInfo;
if ( fstat( fd, &fileInfo ) < 0 )
{
result = errno;
fprintf( stderr, "Unable to get information about \'%s\' (%d: %s)\n", arg, errno, strerror(errno));
}
else
{
size_t bufferSize = fileInfo.st_size;
char * buffer = calloc( 1, fileInfo.st_size + 1 );
if ( buffer == NULL )
{
fprintf( stderr, "out of memory trying to read \'%s\' (%d: %s)\n", arg, errno, strerror(errno));
}
else
{
if ( read( fd, buffer, bufferSize ) < 0 )
{
result = errno;
fprintf( stderr, "unable to access \'%s\' (%d: %s)\n", arg, errno, strerror(errno));
}
else
{
#if 0
fwrite( buffer, bufferSize, 1, stderr );
fputc( '\n', stderr );
#endif
cJSON * json = cJSON_Parse( buffer );
if ( json == NULL )
{
fprintf( stderr, "failed to parse JSON file \'%s\'", arg );
}
else
{
startFile(arg);
processTree( json );
finishFile(arg);
#if 0
char * jsonAsString = cJSON_Print( json );
fprintf( stdout, "%s\n", jsonAsString );
free( jsonAsString );
#endif
cJSON_Delete( json );
}
}
}
}
}
return result;
}
int main(int argc, char * argv[] )
{
int result = 0;
if ( argc < 2 )
{
fprintf( stderr, "Error: please provide at least one JSON file to process.\n");
result = -1;
}
else
{
#if 0
for ( int i = 1; i < argc && result == 0; i++ )
{
if ( argv[i][0] == '-' && argv[i][2] == '\0' )
{
switch (argv[i][1])
{
case 'm':
/* export an xml tags file for mkvmerge */
global.appMode = modeMKVTags;
break;
case 's':
/* export equivalent shell syntax to define shell variables */
global.appMode = modeShell;
break;
case '-':
/* skip the remaining args */
i = argc;
break;
default:
fprintf( stderr, "Error: don't understand option \'%s\'\n", argv[i] );
break;
}
}
}
#endif
for ( int i = 1; i < argc && result == 0; i++ )
{
#if 0
if ( argv[i][0] != '-' && argv[i][2] != '\0' )
#endif
{
result = processArg( argv[i] );
}
}
}
return result;
}