Permalink
Browse files

pop feature

  • Loading branch information...
1 parent 6fd8632 commit 6021f8b862e65c40bd9aa4c7d11f8910e7039c55 @keenerd committed Jul 23, 2011
Showing with 15 additions and 4 deletions.
  1. +6 −1 jshon.1
  2. +9 −3 jshon.c
View
@@ -7,7 +7,7 @@
.Nd JSON parser for the shell
.Sh SYNOPSIS
.Nm jshon
--[t|l|k|u|a] -s value -[e|i] index -m index,value
+-[t|l|k|u|p|a] -s value -[e|i] index -m index,value
.Sh DESCRIPTION
.Nm
parses, reads and creates JSON. It is designed to be as usable as possible from within the shell and replaces fragile adhoc parsers made from grep/sed/awk.
@@ -68,6 +68,11 @@ in the middle of them.
.Pp
\& jshon -e b -e 3 -u -> none
.Pp
+.It Cm -p
+(pop) pops the last manipulation from the stack, rewinding the history. Useful for extracting multiple values from one object.
+.Pp
+\& jshon -e c -e d -u -p -e e -u -> 4 5
+.Pp
.It Cm -m index,value
(modify) edits an element in a list or a dict. Value must be escaped. If value is remove, delete the index. If value is true/false/null, insert the primitive type. Quoting strings is optional, except for the ambiguous case of remove/true/false/null. Lists support several magic indexes. Negative numbers wrap around the back, and append will add a new element after the last.
.Pp
View
12 jshon.c
@@ -22,6 +22,7 @@
-e(xtract) index -> only works on dict, list
-s(tring) value -> adds json escapes
-u(nstring) -> removes json escapes
+ -p(op) -> pop/undo the last manipulation
-m(odify) index,value -> only works on dict, list
index can be append, value can be remove
no commas in index
@@ -217,8 +218,8 @@ char* remove_jsonp_callback(char* in, int* rows_skipped, int* cols_skipped)
#define JSON_WHITE(x) ((x) == 0x20 || (x) == 0x9 || (x) == 0xA || (x) == 0xD)
#define JSON_IDENTIFIER(x) (isalnum(x) || (x) == '$' || (x) == '_' || (x) == '.')
- char *first = in;
- char *last = in + strlen(in) - 1;
+ char* first = in;
+ char* last = in + strlen(in) - 1;
// skip over whitespace and semicolons at the end
while (first < last && (JSON_WHITE(*last) || *last == ';'))
@@ -532,7 +533,7 @@ void debug_map()
}
int main (int argc, char *argv[])
-#define ALL_OPTIONS "PStlkuae:s:m:i:"
+#define ALL_OPTIONS "PStlkupae:s:m:i:"
{
char* content = "";
char* arg1 = "";
@@ -629,6 +630,11 @@ int main (int argc, char *argv[])
printf("%s\n", unstring(PEEK));
output = 0;
break;
+ case 'p': // pop stack
+ json = POP;
+ json_decref(json);
+ output = 1;
+ break;
case 's': // escape string
arg1 = (char*) strdup(optarg);
PUSH(json_string(arg1));

0 comments on commit 6021f8b

Please sign in to comment.