-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathshell-json
More file actions
573 lines (541 loc) · 13.9 KB
/
Copy pathshell-json
File metadata and controls
573 lines (541 loc) · 13.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
###
### This source is largely based on the implementation of the json
### parser written by Richard Crowley.
###
### Original source: https://github.com/rcrowley/json.sh
if [ -z "${__included_shell_json-}" ]; then
__included_shell_json=1
. shell-error
. shell-string
# Probe read once; its behavior is fixed for the current shell.
__json_read_opt_n=''
__json_read_newline=1
# shellcheck disable=SC2169,SC3045
if { printf 1 | (IFS= read -n 1 -r __json_probe); } 2>/dev/null; then
__json_read_opt_n=1
# bash stops at newlines with -n; ksh includes them in the chunk.
# shellcheck disable=SC2169,SC3045
printf 'a\nb' | (IFS= read -n 3 -r __json_probe; [ "$__json_probe" = a ]) ||
__json_read_newline=
fi
JSON_FOREACH_ABORT=100
JSON_DEBUG=
__json_debug()
{
[ -z "$JSON_DEBUG" ] ||
printf >&2 'json: %s\n' "$*"
}
__json_push_name()
{
eval "__json_name_$__json_name=\"\$1\""
__json_name=$(( $__json_name + 1 ))
}
__json_pop_name()
{
__json_name=$(( $__json_name - 1 ))
eval "unset __json_name_$__json_name"
}
json_get_last_name()
{
eval "$1=\"\${__json_name_$(( $__json_name - 1 ))-}\""
}
json_get_full_name()
{
local __i=0 __v=''
while [ "$__i" -lt "$__json_name" ]; do
eval "__v=\"\$__v${2-/}\${__json_name_$__i-}\""
__i=$(( $__i + 1 ))
done
eval "$1=\"\$__v\""
}
__json_get_stack()
{
local i=0
stack=''
while [ "$i" -lt "$__json_states" ]; do
eval "stack=\"\${stack:+\$stack -> }\${__json_states_$i-}\""
i=$(( $i + 1 ))
done
stack="${stack:-none}"
}
__json_push_state()
{
if [ -n "${JSON_DEBUG-}" ]; then
local stack
__json_get_stack
__json_debug "state: append: $stack -> { $1 }"
fi
__json_state="$1"
eval "__json_states_$__json_states=\"\$1\""
__json_states=$(( $__json_states + 1 ))
}
__json_pop_state()
{
local prev="$__json_state"
__json_states=$(( $__json_states - 1 ))
eval "unset __json_states_$__json_states"
eval "__json_state=\"\${__json_states_$(( $__json_states - 1 ))-}\""
if [ -n "${JSON_DEBUG-}" ]; then
local stack
__json_get_stack
__json_debug "state: remove: $stack // { $prev }"
fi
}
json_foreach_handler()
{
local name=''
json_get_full_name name
printf '%s %s %s\n' "${name:-/}" "$1" "$2"
}
__json_handle()
{
if [ -n "$__json_handle" ]; then
local IFS="$SAVE_IFS"
json_foreach_handler "$1" "$2"
fi
}
# Consume standard input one character at a time to parse JSON.
json_foreach()
{
local __json_l __json_c __json_escape __json_eof __json_buffer __json_state
local __json_name=0 __json_states=0
local __json_handle=1 __json_unicode=0
local __json_controls __json_cr __json_lf='
'
__json_controls="$(printf '\001-\037')"
__json_cr="$(printf '\r')"
local SAVE_IFS="$IFS"
local IFS=''
__json_buffer='' __json_eof='' __json_escape=''
# Accept exactly one top-level value.
__json_push_state 'done'
__json_push_state 'value'
while [ -z "$__json_eof" ]; do
read ${__json_read_opt_n:+-n 9} -r __json_l || __json_eof=1
# read removes the newline delimiter, but it still separates tokens.
if [ -z "$__json_eof" ] && [ -n "$__json_read_newline" ] &&
{ [ -z "$__json_read_opt_n" ] || [ "${#__json_l}" -lt 9 ]; }; then
__json_l="$__json_l$__json_lf"
fi
__shell_string_foreach_prepare __json_l
while __shell_string_foreach_continue __json_l; do
__shell_string_foreach_char __json_c __json_l
__shell_string_foreach_iter __json_l
__json_char || [ $? -ne $JSON_FOREACH_ABORT ] ||
return 0
done
done
__json_c=''
__json_char || [ $? -ne $JSON_FOREACH_ABORT ] ||
return 0
if [ "$__json_state" != 'done' ]; then
local stack
__json_get_stack
fatal "unfinished json: $stack"
fi
}
__json_char()
{
while :; do
case "$__json_state" in
'value')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr"|'')
break
;;
esac
__json_pop_state 'value'
case "$__json_c" in
'[')
__json_push_name 0
__json_push_state 'array'
;;
'{')
__json_push_state 'object'
;;
'"')
__json_push_state 'string'
__json_buffer=
;;
0)
__json_push_state 'number-leading-zero'
__json_buffer="$__json_c"
;;
[1-9])
__json_push_state 'number-leading-nonzero'
__json_buffer="$__json_c"
;;
'-')
__json_push_state 'number-negative'
__json_buffer="$__json_c"
;;
't'|'f')
__json_push_state 'boolean'
__json_buffer=
continue
;;
'n')
__json_push_state 'null'
__json_buffer=
continue
;;
*)
fatal "expected value, got '$__json_c'"
;;
esac
;;
'array')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr")
;;
']')
__json_pop_name
__json_pop_state 'array'
;;
*)
__json_push_state 'array-more'
__json_push_state 'value'
continue
;;
esac
;;
'array-more')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr")
;;
']')
__json_pop_name
__json_pop_state 'array-more'
__json_pop_state 'array'
;;
',')
local last
json_get_last_name last
__json_pop_name
__json_push_name $(( $last + 1 ))
__json_push_state 'value'
;;
*)
fatal "$__json_state unexpected '$__json_c'"
;;
esac
;;
'object')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr")
;;
'"')
__json_handle=
__json_push_state 'object-more'
__json_push_state 'object-name'
continue
;;
'}')
__json_pop_state 'object'
;;
*)
fatal "$__json_state unexpected '$__json_c'"
;;
esac
;;
'object-name')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr")
;;
'"')
__json_pop_state 'object-name'
__json_push_state 'object-colon'
__json_push_state 'string'
__json_buffer=
;;
*)
fatal "$__json_state unexpected '$__json_c'"
;;
esac
;;
'object-colon')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr")
;;
':')
__json_handle=1
__json_push_name "$__json_buffer"
__json_pop_state 'object-colon'
__json_push_state 'value'
;;
*)
fatal "$__json_state unexpected '$__json_c'"
;;
esac
;;
'object-more')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr")
;;
'}')
__json_pop_name
__json_pop_state 'object-more'
__json_pop_state 'object'
;;
',')
__json_pop_name
__json_handle=
__json_push_state 'object-name'
;;
*)
fatal "$__json_state unexpected '$__json_c'"
;;
esac
;;
'boolean')
# Boolean values are multicharacter literals but they're unique
# from their first character. This means the eventual value is
# already known when the "boolean" state is entered so we can
# raise syntax errors as soon as the input goes south.
case "$__json_buffer$__json_c" in
't'|'tr'|'tru'|'f'|'fa'|'fal'|'fals')
__json_buffer="$__json_buffer$__json_c"
;;
'true'|'false')
__json_pop_state 'boolean'
__json_handle boolean "$__json_buffer$__json_c" || [ $? -ne $JSON_FOREACH_ABORT ] ||
return $JSON_FOREACH_ABORT
;;
*)
fatal "$__json_state unexpected '$__json_c'"
;;
esac
;;
'null')
# Null values work exactly like boolean values. See above.
case "$__json_buffer$__json_c" in
'n'|'nu'|'nul')
__json_buffer="$__json_buffer$__json_c"
;;
'null')
__json_pop_state 'null'
__json_handle null "$__json_buffer$__json_c" || [ $? -ne $JSON_FOREACH_ABORT ] ||
return $JSON_FOREACH_ABORT
;;
*)
fatal "$__json_state unexpected '$__json_c'"
;;
esac
;;
'string')
case "$__json_c" in
[$__json_controls]) fatal "unescaped control character in string" ;;
esac
# A consumed escape must not escape the next character,
# even when the escaped character is a backslash.
# shellcheck disable=SC1003
case "$__json_escape$__json_c" in
'\"'|'\/'|'\\')
__json_buffer="$__json_buffer$__json_c"
__json_escape=
;;
'\b'|'\f'|'\n'|'\r'|'\t')
__json_buffer="$__json_buffer\\$__json_c"
__json_escape=
;;
'\u')
__json_buffer="$__json_buffer\\u"
__json_escape=
__json_unicode=4
__json_push_state 'string-unicode'
;;
'\'?*)
fatal "invalid string escape: '$__json_c'"
;;
*'"')
__json_pop_state 'string'
__json_handle string "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
return $JSON_FOREACH_ABORT
;;
*'\')
__json_escape='\'
;;
*)
__json_buffer="$__json_buffer$__json_c"
__json_escape=
;;
esac
;;
'string-unicode')
case "$__json_c" in
[0-9a-fA-F])
__json_buffer="$__json_buffer$__json_c"
__json_unicode=$(( __json_unicode - 1 ))
[ "$__json_unicode" -ne 0 ] || __json_pop_state 'string-unicode'
;;
*)
fatal "expected hexadecimal digit, got '$__json_c'"
;;
esac
;;
'number-negative')
# This is an entrypoint into parsing a number, used when
# the first character consumed is a '-'. From here, a number
# may progress to the "number-leading-nonzero" or
# "number-leading-zero" states.
__json_pop_state 'number-negative'
case "$__json_c" in
'0')
__json_push_state 'number-leading-zero'
__json_buffer="$__json_buffer$__json_c"
;;
[1-9])
__json_push_state 'number-leading-nonzero'
__json_buffer="$__json_buffer$__json_c"
;;
*)
fatal "expected digit after minus, got '$__json_c'"
;;
esac
;;
'number-leading-zero')
# This is an entrypoint into parsing a number, used when
# the first digit consumed is zero. From here, a number
# may remain zero, become a floating-point number by
# consuming a '.', or become scientific-notation by consuming
# an 'E' or 'e'.
__json_pop_state 'number-leading-zero'
case "$__json_c" in
'.')
__json_push_state 'number-fraction'
__json_buffer="$__json_buffer$__json_c"
;;
[eE])
__json_push_state 'number-sci'
__json_buffer="$__json_buffer$__json_c"
;;
[0-9])
fatal "$__json_state unexpected '$__json_buffer$__json_c'"
;;
*)
__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
return $JSON_FOREACH_ABORT
__json_buffer=
continue
;;
esac
;;
'number-leading-nonzero')
# This is an entrypoint into parsing a number, used when
# the first digit consumed is non-zero. From here, a number
# may continue on a positive integer, become a floating-point
# number by consuming a '.', or become scientific-notation by
# consuming an 'E' or 'e'.
case "$__json_c" in
'.')
__json_pop_state 'number-leading-nonzero'
__json_push_state 'number-fraction'
__json_buffer="$__json_buffer$__json_c"
;;
[eE])
__json_pop_state 'number-leading-nonzero'
__json_push_state 'number-sci'
__json_buffer="$__json_buffer$__json_c"
;;
[0-9])
__json_buffer="$__json_buffer$__json_c"
;;
*)
__json_pop_state 'number-leading-nonzero'
__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
return $JSON_FOREACH_ABORT
__json_buffer=
continue
;;
esac
;;
'number-fraction'|'number-sci-first')
case "$__json_c" in
[0-9])
local next='number-float'
[ "$__json_state" != number-sci-first ] || next='number-sci-digits'
__json_pop_state
__json_push_state "$next"
__json_buffer="$__json_buffer$__json_c"
;;
*)
fatal "$__json_state expected digit, got '$__json_c'"
;;
esac
;;
'number-float')
# Numbers that encounter a '.' become floating point and may
# continue consuming digits forever or may become
# scientific-notation.
case "$__json_c" in
[eE])
__json_pop_state 'number-float'
__json_push_state 'number-sci'
__json_buffer="$__json_buffer$__json_c"
;;
[0-9])
__json_buffer="$__json_buffer$__json_c"
;;
*)
__json_pop_state 'number-float'
__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
return $JSON_FOREACH_ABORT
__json_buffer=
continue
;;
esac
;;
'number-sci')
# Numbers that encounter an 'E' or 'e' become
# scientific-notation and consume digits, optionally prefixed
# by a '+' or '-', forever. The actual consumption is
# delegated to the "number-sci-first" and "number-sci-digits"
# states. Any other character immediately following the 'E'
# or 'e' is a syntax error.
__json_pop_state 'number-sci'
case "$__json_c" in
'+'|'-')
__json_push_state 'number-sci-first'
__json_buffer="$__json_buffer$__json_c"
;;
[0-9])
__json_push_state 'number-sci-digits'
__json_buffer="$__json_buffer$__json_c"
;;
*)
fatal "$__json_state unexpected '$__json_buffer$__json_c'"
;;
esac
;;
'number-sci-digits')
# Once in these states, numbers may consume digits forever.
case "$__json_c" in
[0-9])
__json_buffer="$__json_buffer$__json_c"
;;
*)
__json_pop_state 'number-sci-digits'
__json_handle number "$__json_buffer" || [ $? -ne $JSON_FOREACH_ABORT ] ||
return $JSON_FOREACH_ABORT
__json_buffer=
continue
;;
esac
;;
'done')
case "$__json_c" in
' '|' '|"$__json_lf"|"$__json_cr"|'') ;;
*) fatal "unexpected data after json: '$__json_c'" ;;
esac
;;
esac
__json_debug "c='$__json_c'"
break
done
}
fi #__included_shell_json
# vim: tw=200