From: paul Date: Mon, 5 Oct 2015 23:40:50 +0000 (+0000) Subject: added invalidate function X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=32b5bb9661864905fdb1f99a0dc4cf553034157a;p=shcgi added invalidate function svn path=/trunk/; revision=17 --- diff --git a/misc.sh b/misc.sh index 05a3af5..9d92805 100755 --- a/misc.sh +++ b/misc.sh @@ -38,3 +38,15 @@ validate(){ printf %s\\n "${value}" \ | sed -rn "2q; /^(${regex})\$/{p;q}; a${default}" } + +invalidate(){ + # print default if value matches regex; otherwise print value + value="$1" + regex="$(printf %s\\n "$2" \ + | sed -r 's;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g; s;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g;' + )" # ^^ escape only unescaped slash characters for later insertion + default="$3" + + printf %s\\n "${value}" \ + | sed -rn "2q; /^(${regex})\$/{bd}; p;q; :d;a${default}" +}