-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitShell.c
120 lines (104 loc) · 2.19 KB
/
emitShell.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
//
// Created by paul on 5/19/21.
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <time.h>
#include <time.h>
#include <cjson/cJSON.h>
#include <memory.h>
#include "json2shell.h"
static int emitQuotedString( const char * str )
{
const char * p;
char * q;
int quotedCount = strlen(str) + 2;
for ( p = str; *p != '\0'; ++p )
{
switch (*p)
{
case '"':
quotedCount++;
break;
}
}
char * quotedString = malloc(quotedCount);
if ( quotedString != NULL )
{
p = str;
q = quotedString;
*q++ = '"';
while (*p != '\0' )
{
if (*p == '"')
{
*q++ = '\\';
}
*q++ = *p++;
}
*q++ = '"';
*q = '\0';
printf( "%s", quotedString );
free( quotedString );
}
}
static int emitShellArray(cJSON * j )
{
printf( "( ");
for ( cJSON * p = j; p != NULL; p = p->next )
{
emitQuotedString( p->valuestring );
fputc( ' ', stdout );
}
printf( ")");
return 0;
}
int startFile(const char * path)
{
(void)path;
return 0;
}
int emitObject( cJSON * json )
{
int result = 0;
for ( cJSON * j = json; j != NULL; j = j->next )
{
if ( cJSON_IsString( j ) )
{
printf( "%s=", j->string);
emitQuotedString( j->valuestring );
}
else if ( cJSON_IsNumber( j ))
{
printf( "%s=%g", j->string, j->valuedouble);
}
else if ( cJSON_IsBool( j ))
{
printf( "%s=%d", j->string, j->valueint);
}
else if ( cJSON_IsArray( j ))
{
printf( "%s=", j->string);
result = emitShellArray( j->child );
}
else if ( cJSON_IsObject( j ))
{
fprintf( stderr, "internal error: nested objects not supported.\n");
result = -1;
}
fputc('\n',stdout);
}
return result;
}
int finishFile(const char * path)
{
(void)path;
return 0;
}