From c349fae24a3370dc7b5dc3f9367c586ce0438cb0 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Fri, 5 Aug 2022 22:47:29 +0200 Subject: [PATCH] feat: initial commit --- LICENSE | 22 ++++++++++++++++++++++ README.md | 24 ++++++++++++++++++++++++ json | 17 +++++++++++++++++ json-defines | 35 +++++++++++++++++++++++++++++++++++ json-split-entries | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 json create mode 100755 json-defines create mode 100755 json-split-entries diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..470dc1a --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2022 ptrcnull + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6d7c197 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# execline-json + +> why would you even do that + +json parser written (mostly) in execline + +requirements: +- execline +- [execline-state](https://git.ddd.rip/ptrcnull/execline-state) +- s6-portable-utils +- sed +- tr +- cut (with delimiter/fields support) + +### usage + +```sh +#!/bin/execlineb + +backtick -E users_json { curl https://gitlab.com/api/v4/users?username=ptrcnull } +json users "$users_json" +foreground { s6-echo "${users[0]}" } +s6-echo "${users[0][id]}" +``` diff --git a/json b/json new file mode 100755 index 0000000..fab47a2 --- /dev/null +++ b/json @@ -0,0 +1,17 @@ +#!/bin/execlineb + +multisubstitute { + importas -ui name 1 + importas -ui input 2 +} + +backtick defines { + backtick -E cleaned { pipeline { s6-echo "$input" } tr -d "\\n" } + json-defines "$name" "$cleaned" +} +importas -uisd "\n" defines defines + +shift -n 2 +elgetpositionals +$defines +$@ diff --git a/json-defines b/json-defines new file mode 100755 index 0000000..2ae02ff --- /dev/null +++ b/json-defines @@ -0,0 +1,35 @@ +#!/bin/execlineb + +multisubstitute { + importas -ui name 1 + importas -ui input 2 +} + +foreground { + s6-echo "define +${name} +${input}" +} + +case "$input" { + "\\{.*\\}" { + pipeline { s6-unquote -d "{}" "$input" } + pipeline { json-split-entries } + forstdin -E entry + backtick -E quoted_key { pipeline { s6-echo "${entry}" } cut -d ":" -f1 } + backtick -E value { pipeline { s6-echo $entry } cut -d ":" -f2- } + backtick -E key { s6-unquote "$quoted_key" } + json-defines "${name}[${key}]" "$value" + } + + "\\[.*\\]" { + pipeline { s6-unquote -d "[]" $input } + pipeline { json-split-entries } + pipeline { s6-grep -n . } + forstdin -E entry + backtick -E lua_index { pipeline { s6-echo $entry } cut -d ":" -f1 } + backtick -I -E index { s6-expr "$lua_index" - 1 } + backtick -E value { pipeline { s6-echo $entry } cut -d ":" -f2- } + json-defines "${name}[${index}]" "$value" + } +} diff --git a/json-split-entries b/json-split-entries new file mode 100755 index 0000000..c012cda --- /dev/null +++ b/json-split-entries @@ -0,0 +1,32 @@ +#!/bin/execlineb + +pipeline { sed "s/./\\0\\n/g" } +init-state level +save-state level 0 +foreground { + forstdin -E char + case "$char" { + "(\\{|\\[)" { + load-state level cur + backtick -I -E new { s6-expr "$cur" + 1 } + save-state level $new + s6-echo -n "$char" + } + "(\\}|\\])" { + load-state level cur + backtick -I -E new { s6-expr "$cur" - 1 } + save-state level $new + s6-echo -n "$char" + } + "," { + load-state level cur + save-state level "$cur" + ifelse { s6-test "$cur" = 0 } { + s6-echo + } + s6-echo -n "$char" + } + } + s6-echo -n "$char" +} +s6-echo