config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit 034333b0407d45130d11cf41ca3a2dee05059599
parent 865e659b478b3e418c37ad9ffbda5263358da426
Author: Jacob R. Edwards <n/a>
Date:   Mon, 31 Oct 2022 09:13:47 -0500

Use sh(1) for argument parsing in netquery

Diffstat:
Mlocal/bin/bin/netquery | 63+++++++++++++++++++++++++++++++--------------------------------
1 file changed, 31 insertions(+), 32 deletions(-)

diff --git a/local/bin/bin/netquery b/local/bin/bin/netquery @@ -1,40 +1,39 @@ #!/bin/sh -# Copyright 2021 Jacob R. Edwards +# Copyright 2021, 2022 Jacob R. Edwards # -# Find the url shortcut specified by the first argument and replace -# `$query' with the subsequent arguments concatnated on `+'. +# Generate a query string by appending the given arguments--concatnated +# on plus ('+')--to the matched prefix in netquery's config. +# +# A line in the config might look like this: +# name https://search.domain.domain?q= -awk 'BEGIN { - FS = " "; +if test $# -eq 0; then + echo 'netquery [-name] [word ...]' 1>&2 + exit 1 +fi - i = 1 - if (ARGV[i] ~ /^[-+]/) { - query = substr(ARGV[i], 2) - delete ARGV[i] - ++i; - } - for (; i < ARGC; ++i) { - argv[++j] = ARGV[i]; - delete ARGV[i]; - } +config="${XDG_CONFIG_HOME:-"$HOME"/.config}"/netquery +case "$1" in +(-*) + name="${1#-}" + shift + ;; +esac - home = ENVIRON["XDG_CONFIG_HOME"]; - if (!home) - home = ENVIRON["HOME"] "/.config"; - ARGV[1] = home "/netquery"; - ARGC = 2; +IFS='+' +exec awk -vquery="$*" -vname="$name" -F ' ' \ +'/^(#|$)/ { + next } -/^(#|$)/ { next } +!name || (length($1) == length(name) && index($1, name)) { + s = $2 query + exit +} -! query || match($1, "^" query "$") > 0 { - query = argv[1]; - for (i = 2; i <= length(argv); ++i) - query = query "+" argv[i]; - if (!sub("\\$query", query, $2)) { - printf "%s: No substitution made.\n", $1 > "/dev/stderr"; - exit 1; - } - print $2; - exit 0 -}' "$@" +END { + if (s) + print s + else + exit 1 +}' < "$config"