From 02ec07cef25b211a064a1fa5bbafe1c07cd6d14f Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Fri, 5 Aug 2022 21:19:57 +0200 Subject: [PATCH] feat: initial commit --- LICENSE | 22 ++++++++++++++++++++++ README.md | 31 +++++++++++++++++++++++++++++++ init-state | 16 ++++++++++++++++ load-state | 12 ++++++++++++ save-state | 15 +++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 init-state create mode 100755 load-state create mode 100755 save-state 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..5c5147d --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# execline-state + +> in spite of skarnet's "execline maintains no state" + +a simple shift register built on piperw(1) + +### usage + +``` +init-state +load-state +save-state +``` + +```sh +#!/bin/execlineb + +init-state flip +save-state flip "up" +forstdin -E line + load-state flip current + println "! state: $current" + ifelse { s6-test "$line" = "flip" } { + println "! flipping state" + ifelse { s6-test "$current" = "up" } { + save-state flip "down" + } + save-state flip "up" + } + save-state flip "$current" +``` diff --git a/init-state b/init-state new file mode 100755 index 0000000..2d67c56 --- /dev/null +++ b/init-state @@ -0,0 +1,16 @@ +#!/bin/execlineb + +backtick -E top_fd { + pipeline { s6-ls /proc/self/fd } + s6-tail -n 1 +} +backtick -E rdfd { s6-expr ${top_fd} + 1 } +backtick -E wrfd { s6-expr ${top_fd} + 2 } +piperw $rdfd $wrfd + +importas -ui name 1 +shift +export ELSTATE_${name} $rdfd + +elgetpositionals +$@ diff --git a/load-state b/load-state new file mode 100755 index 0000000..3ba34bc --- /dev/null +++ b/load-state @@ -0,0 +1,12 @@ +#!/bin/execlineb + +importas -ui name 1 +importas -ui target 2 +importas -i rdfd ELSTATE_${name} + +fdswap 0 ${rdfd} +shift -n 2 +elgetpositionals +forstdin -E -o 100 ${target} # chosen by fair dice roll +fdswap 0 ${rdfd} +$@ diff --git a/save-state b/save-state new file mode 100755 index 0000000..ed128b8 --- /dev/null +++ b/save-state @@ -0,0 +1,15 @@ +#!/bin/execlineb + +importas -ui name 1 +importas -ui value 2 +importas -i rdfd ELSTATE_${name} +backtick -E wrfd { s6-expr "$rdfd" + 1 } + +foreground { + fdmove -c 1 ${wrfd} + echo "$value" +} +shift -n 2 + +elgetpositionals +$@