-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
61 lines (52 loc) · 1.49 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
/* See one of the index files for license and other details. */
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <stdbool.h>
#include <getopt.h>
#include "sanity.h"
#include "json.h"
#include "xml.h"
#include "ksh.h"
const char usage[]="usage: jsoncvt [-Akx] [label]\n"
"example: jsoncvt -x mydata <foo.json >foo.xml\n";
int
main( int argc, char *argv[] )
{
/* output is our driver, pointing to the routine indicated by the
* command line option for different output languages. XML and
* ksh93 are supported at present. */
bool (*output)( FILE *, const jvalue * ) = writexml;
int opt;
while(( opt = getopt( argc, argv, "Akx" )) != EOF )
switch( opt ) {
case 'A':
usemap = true;
break;
case 'k':
output = writeksh;
break;
case 'x':
output = writexml;
break;
default:
fputs( usage, stderr );
return 2;
}
argc -= optind;
argv += optind;
if( argc > 1 ) {
err( "too many arguments" );
return 2;
}
/* Okay, now that we know which output driver to use, pull in the
* JSON data into a parse tree. If the parse was successful, label
* it by setting its top value name to something from the command
* line. Print it out, and go home. */
jvalue *j = jparse( stdin );
if( !j )
return 1;
j->n = estrdup( argc > 0 ? argv[0] : "foobar" );
(*output)( stdout, j );
jdel( j );
return 0;
}