2023-03-08 04:06:43 +00:00
|
|
|
/*
|
2024-01-05 00:32:03 +00:00
|
|
|
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
|
2023-03-08 04:06:43 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2023-03-08 19:49:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
2023-03-08 04:06:43 +00:00
|
|
|
|
Use `Makefile`s instead of a custom script (#38)
This pull request also requires the use of the external [Cytoplasm](/Telodendria/Cytoplasm) repository by removing the in-tree copy of Cytoplasm. The increased modularity requires a little more complex build process, but is overall better. Closes #19
The appropriate documentation has been updated. Closes #18
---
Please review the developer certificate of origin:
1. The contribution was created in whole or in part by me, and I have
the right to submit it under the open source licenses of the
Telodendria project; or
1. The contribution is based upon a previous work that, to the best of
my knowledge, is covered under an appropriate open source license and
I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
Telodendria project license; or
1. The contribution was provided directly to me by some other person
who certified (1), (2), or (3), and I have not modified it.
1. I understand and agree that this project and the contribution are
made public and that a record of the contribution—including all
personal information I submit with it—is maintained indefinitely
and may be redistributed consistent with this project or the open
source licenses involved.
- [x] I have read the Telodendria Project development certificate of
origin, and I certify that I have permission to submit this patch
under the conditions specified in it.
Reviewed-on: https://git.telodendria.io/Telodendria/Telodendria/pulls/38
2023-11-01 16:27:45 +00:00
|
|
|
#include <Cytoplasm/Args.h>
|
|
|
|
#include <Cytoplasm/Array.h>
|
|
|
|
#include <Cytoplasm/HashMap.h>
|
|
|
|
#include <Cytoplasm/Str.h>
|
|
|
|
#include <Cytoplasm/Memory.h>
|
2023-06-21 02:37:56 +00:00
|
|
|
|
Use `Makefile`s instead of a custom script (#38)
This pull request also requires the use of the external [Cytoplasm](/Telodendria/Cytoplasm) repository by removing the in-tree copy of Cytoplasm. The increased modularity requires a little more complex build process, but is overall better. Closes #19
The appropriate documentation has been updated. Closes #18
---
Please review the developer certificate of origin:
1. The contribution was created in whole or in part by me, and I have
the right to submit it under the open source licenses of the
Telodendria project; or
1. The contribution is based upon a previous work that, to the best of
my knowledge, is covered under an appropriate open source license and
I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
Telodendria project license; or
1. The contribution was provided directly to me by some other person
who certified (1), (2), or (3), and I have not modified it.
1. I understand and agree that this project and the contribution are
made public and that a record of the contribution—including all
personal information I submit with it—is maintained indefinitely
and may be redistributed consistent with this project or the open
source licenses involved.
- [x] I have read the Telodendria Project development certificate of
origin, and I certify that I have permission to submit this patch
under the conditions specified in it.
Reviewed-on: https://git.telodendria.io/Telodendria/Telodendria/pulls/38
2023-11-01 16:27:45 +00:00
|
|
|
#include <Cytoplasm/Json.h>
|
2023-06-21 02:37:56 +00:00
|
|
|
#include <CanonicalJson.h>
|
2023-03-08 04:06:43 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
#define FLAG_ENCODE (1 << 0)
|
|
|
|
#define FLAG_SELECT (1 << 1)
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(char *prog)
|
2023-03-08 04:06:43 +00:00
|
|
|
{
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPrintf(StreamStderr(), "Usage: %s [-s query|-e str]\n", prog);
|
2023-03-08 20:31:49 +00:00
|
|
|
}
|
2023-03-08 04:06:43 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
static void
|
2023-06-21 02:37:56 +00:00
|
|
|
query(char *select, HashMap * json, int canonical)
|
2023-03-08 20:31:49 +00:00
|
|
|
{
|
|
|
|
char *key;
|
2023-05-23 20:34:27 +00:00
|
|
|
JsonValue *rootVal = JsonValueObject(json);
|
|
|
|
JsonValue *val = rootVal;
|
2023-05-26 22:11:07 +00:00
|
|
|
Array *cleanUp = ArrayCreate();
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
ArrayAdd(cleanUp, rootVal);
|
2023-03-08 04:06:43 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
key = strtok(select, "->");
|
2023-03-08 04:06:43 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
while (key)
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
char keyName[128];
|
|
|
|
size_t arrInd;
|
|
|
|
int expectArr = 0;
|
|
|
|
int func = 0;
|
2023-03-08 19:49:06 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
expectArr = (sscanf(key, "%127[^[][%lu]", keyName, &arrInd) == 2);
|
2023-03-08 19:49:06 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
if (keyName[0] == '@')
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-05-06 22:34:36 +00:00
|
|
|
if (StrEquals(keyName + 1, "length"))
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-08-13 03:11:40 +00:00
|
|
|
UInt64 len;
|
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
switch (JsonValueType(val))
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
case JSON_ARRAY:
|
2023-08-13 03:11:40 +00:00
|
|
|
if (sizeof(size_t) == sizeof(UInt64))
|
|
|
|
{
|
|
|
|
size_t slen = ArraySize(JsonValueAsArray(val));
|
|
|
|
UInt32 high = slen >> 32;
|
|
|
|
UInt32 low = slen;
|
|
|
|
|
|
|
|
len = UInt64Create(high, low);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = UInt64Create(0, ArraySize(JsonValueAsArray(val)));
|
|
|
|
}
|
|
|
|
|
|
|
|
val = JsonValueInteger(len);
|
2023-05-26 22:11:07 +00:00
|
|
|
ArrayAdd(cleanUp, val);
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
|
|
|
case JSON_STRING:
|
2023-08-13 03:11:40 +00:00
|
|
|
if (sizeof(size_t) == sizeof(UInt64))
|
|
|
|
{
|
|
|
|
size_t slen = strlen(JsonValueAsString(val));
|
|
|
|
UInt32 high = slen >> 32;
|
|
|
|
UInt32 low = slen;
|
|
|
|
|
|
|
|
len = UInt64Create(high, low);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = UInt64Create(0, strlen(JsonValueAsString(val)));
|
|
|
|
}
|
|
|
|
|
|
|
|
val = JsonValueInteger(len);
|
2023-05-26 22:11:07 +00:00
|
|
|
ArrayAdd(cleanUp, val);
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
val = NULL;
|
|
|
|
break;
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
2023-03-08 20:31:49 +00:00
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (JsonValueType(val) == JSON_OBJECT && StrEquals(keyName + 1, "keys"))
|
2023-03-08 20:31:49 +00:00
|
|
|
{
|
|
|
|
HashMap *obj = JsonValueAsObject(val);
|
|
|
|
Array *arr = ArrayCreate();
|
|
|
|
char *k;
|
|
|
|
void *v;
|
2023-03-08 19:49:06 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
while (HashMapIterate(obj, &k, &v))
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
ArrayAdd(arr, JsonValueString(k));
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
val = JsonValueArray(arr);
|
2023-05-26 22:11:07 +00:00
|
|
|
ArrayAdd(cleanUp, val);
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
2023-05-06 22:34:36 +00:00
|
|
|
else if (JsonValueType(val) == JSON_STRING && StrEquals(keyName + 1, "decode"))
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPrintf(StreamStdout(), "%s\n", JsonValueAsString(val));
|
2023-03-08 20:31:49 +00:00
|
|
|
val = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2023-03-08 19:49:06 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
func = 1;
|
|
|
|
}
|
2023-03-08 22:47:40 +00:00
|
|
|
else if (keyName[0] == '^')
|
2023-03-08 20:31:49 +00:00
|
|
|
{
|
|
|
|
if (JsonValueType(val) == JSON_OBJECT)
|
|
|
|
{
|
|
|
|
HashMap *obj = JsonValueAsObject(val);
|
2023-03-08 19:49:06 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
JsonValueFree(HashMapDelete(obj, keyName + 1));
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
2023-03-08 20:31:49 +00:00
|
|
|
else if (JsonValueType(val) == JSON_ARRAY)
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (sscanf(keyName + 1, "%lu", &i) == 1)
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
JsonValueFree(ArrayDelete(JsonValueAsArray(val), i));
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 20:31:49 +00:00
|
|
|
else
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
val = NULL;
|
|
|
|
break;
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
func = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
{
|
|
|
|
if (JsonValueType(val) == JSON_OBJECT)
|
|
|
|
{
|
|
|
|
val = HashMapGet(JsonValueAsObject(val), keyName);
|
|
|
|
}
|
|
|
|
else
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
val = NULL;
|
2023-03-08 19:49:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-03-08 20:31:49 +00:00
|
|
|
}
|
2023-03-08 19:49:06 +00:00
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
if (expectArr && JsonValueType(val) == JSON_ARRAY)
|
|
|
|
{
|
|
|
|
val = ArrayGet(JsonValueAsArray(val), arrInd);
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
if (!val)
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
key = strtok(NULL, "->");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
{
|
2023-06-21 02:37:56 +00:00
|
|
|
if (canonical)
|
|
|
|
{
|
|
|
|
CanonicalJsonEncodeValue(val, StreamStdout());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JsonEncodeValue(val, StreamStdout(), JSON_PRETTY);
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPutc(StreamStdout(), '\n');
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
2023-05-23 20:34:27 +00:00
|
|
|
|
2023-05-26 22:11:07 +00:00
|
|
|
for (i = 0; i < ArraySize(cleanUp); i++)
|
|
|
|
{
|
|
|
|
JsonValueFree(ArrayGet(cleanUp, i));
|
|
|
|
}
|
|
|
|
ArrayFree(cleanUp);
|
2023-03-08 20:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-06-21 02:37:56 +00:00
|
|
|
encode(char *str, int canonical)
|
2023-03-08 20:31:49 +00:00
|
|
|
{
|
|
|
|
JsonValue *val = JsonValueString(str);
|
|
|
|
|
2023-06-21 02:37:56 +00:00
|
|
|
if (canonical)
|
|
|
|
{
|
|
|
|
CanonicalJsonEncodeValue(val, StreamStdout());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JsonEncodeValue(val, StreamStdout(), JSON_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2023-03-08 20:31:49 +00:00
|
|
|
JsonValueFree(val);
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPutc(StreamStdout(), '\n');
|
2023-03-08 20:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-05-15 22:38:52 +00:00
|
|
|
Main(Array * args)
|
2023-03-08 20:31:49 +00:00
|
|
|
{
|
2023-05-23 17:53:19 +00:00
|
|
|
HashMap *json = NULL;
|
2023-03-08 20:31:49 +00:00
|
|
|
int flag = 0;
|
|
|
|
int ch;
|
2023-03-09 02:22:13 +00:00
|
|
|
char *input = NULL;
|
2023-05-14 22:15:48 +00:00
|
|
|
ArgParseState arg;
|
2023-03-08 20:31:49 +00:00
|
|
|
|
2023-06-21 02:37:56 +00:00
|
|
|
int canonical = 0;
|
|
|
|
|
2023-05-14 22:15:48 +00:00
|
|
|
ArgParseStateInit(&arg);
|
2023-06-21 02:37:56 +00:00
|
|
|
while ((ch = ArgParse(&arg, args, "cs:e:")) != -1)
|
2023-03-08 20:31:49 +00:00
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
flag = FLAG_SELECT;
|
2023-05-14 22:15:48 +00:00
|
|
|
input = arg.optArg;
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
flag = FLAG_ENCODE;
|
2023-05-14 22:15:48 +00:00
|
|
|
input = arg.optArg;
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
2023-06-21 02:37:56 +00:00
|
|
|
case 'c':
|
|
|
|
canonical = 1;
|
|
|
|
break;
|
2023-03-08 20:31:49 +00:00
|
|
|
default:
|
2023-05-14 22:15:48 +00:00
|
|
|
usage(ArrayGet(args, 0));
|
2023-03-08 20:31:49 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag != FLAG_ENCODE)
|
|
|
|
{
|
2023-03-18 14:32:09 +00:00
|
|
|
json = JsonDecode(StreamStdin());
|
2023-03-08 20:31:49 +00:00
|
|
|
|
|
|
|
if (!json)
|
|
|
|
{
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPuts(StreamStderr(), "Malformed JSON.\n");
|
2023-03-08 20:31:49 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (flag)
|
2023-03-08 19:49:06 +00:00
|
|
|
{
|
2023-03-08 20:31:49 +00:00
|
|
|
case FLAG_SELECT:
|
2023-07-15 17:57:21 +00:00
|
|
|
query(input, json, canonical); /* This will implicitly
|
|
|
|
* free json */
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
|
|
|
case FLAG_ENCODE:
|
2023-06-21 02:37:56 +00:00
|
|
|
encode(input, canonical);
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-06-21 02:37:56 +00:00
|
|
|
if (canonical)
|
|
|
|
{
|
|
|
|
CanonicalJsonEncode(json, StreamStdout());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JsonEncode(json, StreamStdout(), JSON_PRETTY);
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
StreamPutc(StreamStdout(), '\n');
|
2023-05-23 22:43:37 +00:00
|
|
|
JsonFree(json);
|
2023-03-08 20:31:49 +00:00
|
|
|
break;
|
2023-03-08 19:49:06 +00:00
|
|
|
}
|
2023-03-08 04:06:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|