}
static void
-export_value(struct list_head *matches, const char *prefix)
+print_separator(const char *sep, int *sc, int sl)
+{
+ if (*sc > 0)
+ {
+ switch (sep[(*sc - 1) % sl])
+ {
+ case '"':
+ printf("'\"'");
+ break;
+
+ case '\'':
+ printf("\"'\"");
+ break;
+
+ case ' ':
+ printf("\\ ");
+ break;
+
+ default:
+ printf("%c", sep[(*sc - 1) % sl]);
+ }
+ }
+
+ (*sc)++;
+}
+
+static void
+export_value(struct list_head *matches, const char *prefix, const char *sep)
{
int n, len;
- bool first = true;
+ int sc = 0, sl = strlen(sep);
struct match_item *item;
if (list_empty(matches))
if (!val)
continue;
- if (!first)
- printf("\\ ");
-
+ print_separator(sep, &sc, sl);
print_string(key);
- first = false;
}
break;
for (n = 0, len = json_object_array_length(item->jsobj);
n < len; n++)
{
- if (!first)
- printf("\\ ");
-
+ print_separator(sep, &sc, sl);
printf("%d", n);
- first = false;
}
break;
case json_type_boolean:
- if (!first)
- printf("\\ ");
+ print_separator(sep, &sc, sl);
printf("%d", json_object_get_boolean(item->jsobj));
break;
case json_type_int:
- if (!first)
- printf("\\ ");
+ print_separator(sep, &sc, sl);
printf("%d", json_object_get_int(item->jsobj));
break;
case json_type_double:
- if (!first)
- printf("\\ ");
+ print_separator(sep, &sc, sl);
printf("%f", json_object_get_double(item->jsobj));
break;
case json_type_string:
- if (!first)
- printf("\\ ");
+ print_separator(sep, &sc, sl);
print_string(json_object_get_string(item->jsobj));
break;
case json_type_null:
break;
}
-
- first = false;
}
printf("; ");
case -4:
fprintf(stderr, "Unexpected character\n");
break;
-
+
case -3:
fprintf(stderr, "String or label literal too long\n");
break;
}
static bool
-filter_json(int opt, struct json_object *jsobj, char *expr)
+filter_json(int opt, struct json_object *jsobj, char *expr, const char *sep)
{
struct jp_state *state;
const char *prefix = NULL;
break;
default:
- export_value(&matches, prefix);
+ export_value(&matches, prefix, sep);
break;
}
int opt, rv = 0;
FILE *input = stdin;
struct json_object *jsobj = NULL;
- const char *jserr = NULL, *source = NULL;
+ const char *jserr = NULL, *source = NULL, *separator = " ";
- while ((opt = getopt(argc, argv, "i:s:e:t:q")) != -1)
+ while ((opt = getopt(argc, argv, "i:s:e:t:F:q")) != -1)
{
switch (opt)
{
source = optarg;
break;
+ case 'F':
+ if (optarg && *optarg)
+ separator = optarg;
+ break;
+
case 't':
case 'e':
if (!jsobj)
}
}
- if (!filter_json(opt, jsobj, optarg))
+ if (!filter_json(opt, jsobj, optarg, separator))
rv = 1;
break;