summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2021-08-11 01:34:55 +0000
committerLaurent Bercot <ska@appnovation.com>2021-08-11 01:34:55 +0000
commit64d63f8fd06e5370694f760093c69cdc69652447 (patch)
tree50f711e11e19bb72e1401af1ebb5bb8e189bdacb
parent524ebfde1d08edc64ba4b9075d23f7ed3dbc640c (diff)
downloadexecline-64d63f8fd06e5370694f760093c69cdc69652447.tar.xz
Add case (yet untested)
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--NEWS1
-rw-r--r--doc/case.html93
-rw-r--r--doc/index.html1
-rw-r--r--doc/trap.html6
-rw-r--r--package/deps.mak3
-rw-r--r--package/modes13
-rw-r--r--package/targets.mak1
-rw-r--r--src/execline/case.c123
-rw-r--r--src/execline/deps-exe/case2
9 files changed, 235 insertions, 8 deletions
diff --git a/NEWS b/NEWS
index 7941c37..c1f0a36 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ In 2.8.1.0
----------
- Adaptation to skalibs-2.11.0.0.
+ - New binary: case.
In 2.8.0.1
diff --git a/doc/case.html b/doc/case.html
new file mode 100644
index 0000000..5340c07
--- /dev/null
+++ b/doc/case.html
@@ -0,0 +1,93 @@
+<html>
+ <head>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <meta http-equiv="Content-Language" content="en" />
+ <title>execline: the case command</title>
+ <meta name="Description" content="execline: the case command" />
+ <meta name="Keywords" content="execline command case regex grep" />
+ <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
+</head>
+<body>
+
+<p>
+<a href="index.html">execline</a><br />
+<a href="//skarnet.org/software/">Software</a><br />
+<a href="//skarnet.org/">skarnet.org</a>
+</p>
+
+<h1> The <tt>case</tt> program </h1>
+
+<p>
+<tt>case</tt> compares a value against a series of regular expressions,
+and executes into a program depending on the first expression the value
+matches.
+</p>
+
+<h2> Interface </h2>
+
+<p>
+ In an <a href="execlineb.html">execlineb</a> script:
+</p>
+
+<pre>
+ case [ -E | -e | -i | -n ] <em>value</em>
+ {
+ [ regex { <em>prog...</em> } ]
+ [ regex { <em>prog...</em> } ]
+ ...
+ }
+ <em>progdefault...</em>
+</pre>
+
+<ul>
+ <li> <tt>case</tt> reads an argument <em>value</em> and a sequence of
+directives in a <a href="el_semicolon.html">block</a>. </li>
+ <li> Each directive is a regular expression followed by a block. </li>
+ <li> <tt>case</tt> matches <em>value</em> against the regular expressions
+in the order they are given. </li>
+ <li> As soon as <em>value</em> matches a <em>regex</em>, <tt>case</tt>
+executes into the <em>prog...</em> command line that immediately follows
+the matched regex. </li>
+ <li> If <em>value</em> matches no <em>regex</em>, <tt>case</tt>
+eventually execs into <em>progdefault...</em>. </li>
+</ul>
+
+<h2> Options </h2>
+
+<ul>
+ <li> <tt>-e</tt>&nbsp;: Interpret the <em>regex</em> words as
+<a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03">basic
+regular expressions</a>. </li>
+ <li> <tt>-E</tt>&nbsp;: Interpret the <em>regex</em> words as
+<a href="https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04">extended
+regular expressions</a>. This is the default. </li>
+ <li> <tt>-i</tt>&nbsp;: Perform case-insensitive matches. </li>
+ <li> <tt>-n</tt>&nbsp;: Do not transmit the matching expression and
+subexpressions to <em>prog...</em> via the environment. This is the default. </li>
+ <li> <tt>-N</tt>&nbsp;: Make the matching expression and
+subexpressions available to <em>prog</em>'s environment. </li>
+</ul>
+
+<h2> Subexpression matching </h2>
+
+<p>
+ If the <tt>-N</tt> option has been given, and <em>value</em> matches a <em>regex</em>,
+then <tt>case</tt> will run <em>prog</em> with a modified environment:
+</p>
+
+<ul>
+ <li> The <tt>0</tt> variable will contain the <em>regex</em> that <em>value</em> matched. </li>
+ <li> The <tt>#</tt> variable will contain the number of subexpressions in <em>regex</em>. </li>
+ <li> For every integer <em>i</em> between 1 and the number of subexpressions (included), the
+variable <em>i</em> contains the part of <em>value</em> that matched the <em>i</em>th subexpression
+in <em>regex</em>. </li>
+</ul>
+
+<p>
+ To retrieve that information into your command line in an execline script, you can use the
+<a href="elgetpositionals.html">elgetpositionals</a> program.
+</p>
+
+</body>
+</html>
diff --git a/doc/index.html b/doc/index.html
index 035e0eb..7dae446 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -167,6 +167,7 @@ to your installation: the shebang lines for your system might be something like
<ul>
<li><a href="foreground.html">The <tt>foreground</tt> program</a></li>
<li><a href="background.html">The <tt>background</tt> program</a></li>
+<li><a href="case.html">The <tt>case</tt> program</a></li>
<li><a href="if.html">The <tt>if</tt> program</a></li>
<li><a href="ifelse.html">The <tt>ifelse</tt> program</a></li>
<li><a href="ifte.html">The <tt>ifte</tt> program</a></li>
diff --git a/doc/trap.html b/doc/trap.html
index 9351c93..7fd0df3 100644
--- a/doc/trap.html
+++ b/doc/trap.html
@@ -1,6 +1,6 @@
<html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <head>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="en" />
<title>execline: the trap command</title>
@@ -18,8 +18,10 @@
<h1> The <tt>trap</tt> program </h1>
+<p>
<tt>trap</tt> traps signals and runs a variety of commands according
to the signals it catches.
+</p>
<h2> Interface </h2>
diff --git a/package/deps.mak b/package/deps.mak
index 6bfce8e..17d654e 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -4,6 +4,7 @@
src/execline/background.o src/execline/background.lo: src/execline/background.c src/include/execline/execline.h
src/execline/backtick.o src/execline/backtick.lo: src/execline/backtick.c src/include/execline/execline.h
+src/execline/case.o src/execline/case.lo: src/execline/case.c src/include/execline/execline.h
src/execline/define.o src/execline/define.lo: src/execline/define.c src/include-local/exlsn.h
src/execline/dollarat.o src/execline/dollarat.lo: src/execline/dollarat.c
src/execline/elgetopt.o src/execline/elgetopt.lo: src/execline/elgetopt.c src/include/execline/execline.h
@@ -80,6 +81,8 @@ background: EXTRA_LIBS := -lskarnet ${SPAWN_LIB}
background: src/execline/background.o ${LIBEXECLINE}
backtick: EXTRA_LIBS := -lskarnet ${SPAWN_LIB}
backtick: src/execline/backtick.o ${LIBEXECLINE}
+case: EXTRA_LIBS := -lskarnet
+case: src/execline/case.o ${LIBEXECLINE}
define: EXTRA_LIBS := -lskarnet
define: src/execline/define.o ${LIBEXECLINE}
dollarat: EXTRA_LIBS := -lskarnet
diff --git a/package/modes b/package/modes
index 16a064a..17c52ce 100644
--- a/package/modes
+++ b/package/modes
@@ -1,13 +1,13 @@
background 0755
backtick 0755
-execline-cd 0755
+case 0755
define 0755
dollarat 0755
elgetopt 0755
elgetpositionals 0755
elglob 0755
-multidefine 0755
-multisubstitute 0755
+execline-cd 0755
+execline-umask 0755
emptyenv 0755
envfile 0755
exec 0755
@@ -33,16 +33,17 @@ ifte 0755
ifthenelse 0755
importas 0755
loopwhilex 0755
+multidefine 0755
+multisubstitute 0755
piperw 0755
pipeline 0755
+posix-cd 0755
+posix-umask 0755
redirfd 0755
runblock 0755
shift 0755
trap 0755
tryexec 0755
-execline-umask 0755
unexport 0755
wait 0755
withstdinas 0755
-posix-cd 0755
-posix-umask 0755
diff --git a/package/targets.mak b/package/targets.mak
index 423f464..a0e5970 100644
--- a/package/targets.mak
+++ b/package/targets.mak
@@ -1,6 +1,7 @@
BIN_TARGETS := \
background \
backtick \
+case \
define \
dollarat \
elgetopt \
diff --git a/src/execline/case.c b/src/execline/case.c
new file mode 100644
index 0000000..8fdf21e
--- /dev/null
+++ b/src/execline/case.c
@@ -0,0 +1,123 @@
+/* ISC license. */
+
+#include <string.h>
+#include <regex.h>
+
+#include <skalibs/gccattributes.h>
+#include <skalibs/types.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/exec.h>
+
+#include <execline/execline.h>
+
+#define USAGE "case [ -e | -E ] [ -n | -N ] [ -i ] value { re1 { prog1... } re2 { prog2... } ... } progdefault... "
+#define dieusage() strerr_dieusage(100, USAGE)
+
+static void execit (char const *const *argv, char const *expr, char const *s, regmatch_t const *pmatch, size_t nsub, int flagnosub) gccattr_noreturn ;
+static void execit (char const *const *argv, char const *expr, char const *s, regmatch_t const *pmatch, size_t nsub, int flagnosub)
+{
+ if (flagnosub) xexec0(argv) ;
+ else
+ {
+ size_t exprlen = strlen(expr) ;
+ size_t fmtlen = exprlen + 6 ;
+ for (size_t i = 0 ; i < nsub ; i++)
+ fmtlen += uint_fmt(0, i+1) + 2 + pmatch[i].rm_eo - pmatch[i].rm_so ;
+ {
+ size_t m = 0 ;
+ char fmt[fmtlen] ;
+ fmt[m++] = '#' ; fmt[m++] = '=' ;
+ m += uint_fmt(fmt + m, nsub) ;
+ fmt[m++] = 0 ;
+ fmt[m++] = '0' ; fmt[m++] = '=' ;
+ memcpy(fmt + m, expr, exprlen + 1) ; m += exprlen + 1 ;
+ for (size_t i = 0 ; i < nsub ; i++)
+ {
+ m += uint_fmt(fmt + m, i+1) ;
+ fmt[m++] = '=' ;
+ memcpy(fmt + m, s + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so) ;
+ fmt[m++] = 0 ;
+ }
+ xmexec0_n(argv, fmt, fmtlen, 2 + nsub) ;
+ }
+ }
+}
+
+int main (int argc, char const **argv, char const *const *envp)
+{
+ int flagextended = 1 ;
+ int flagicase = 0 ;
+ int flagnosub = 1 ;
+ int argc1 ;
+ unsigned int i = 0 ;
+ char const *s ;
+ PROG = "case" ;
+ {
+ subgetopt l = SUBGETOPT_ZERO ;
+ for (;;)
+ {
+ int opt = subgetopt_r(argc, argv, "eEnNi", &l) ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'e' : flagextended = 0 ; break ;
+ case 'E' : flagextended = 1 ; break ;
+ case 'N' : flagnosub = 0 ; break ;
+ case 'n' : flagnosub = 1 ; break ;
+ case 'i' : flagicase = 1 ; break ;
+ default : dieusage() ;
+ }
+ }
+ argc -= l.ind ; argv += l.ind ;
+ }
+ if (argc-- < 2) dieusage() ;
+ s = *argv++ ;
+
+ argc1 = el_semicolon(argv) ;
+ if (argc1 >= argc) strerr_dief1x(100, "unterminated case block") ;
+
+ while (i < argc1)
+ {
+ char const *expr = argv[i++] ;
+ int argc2 ;
+ regex_t re ;
+ if (i == argc1) strerr_dief1x(100, "malformed case block") ;
+ argc2 = el_semicolon(argv + i) ;
+ if (i + argc2 >= argc1) strerr_dief1x(100, "unterminated regex block") ;
+ {
+ int r ;
+ size_t len = strlen(expr) ;
+ char tmp[len+3] ;
+ tmp[0] = '^' ;
+ memcpy(tmp + 1, expr, len) ;
+ tmp[1+len] = '$' ;
+ tmp[2+len] = 0 ;
+ r = regcomp(&re, tmp, (flagextended ? REG_EXTENDED : 0) | (flagicase ? REG_ICASE : 0) | (flagnosub ? REG_NOSUB : 0) | REG_NEWLINE) ;
+ if (r)
+ {
+ char buf[256] ;
+ regerror(r, &re, buf, 256) ;
+ strerr_diefu4x(r == REG_ESPACE ? 111 : 100, "regcomp \"^", argv[i], "$\": ", buf) ;
+ }
+ }
+ {
+ regmatch_t pmatch[re.re_nsub && !flagnosub ? re.re_nsub : 1] ;
+ int r = regexec(&re, s, re.re_nsub, pmatch, 0) ;
+ if (!r)
+ {
+ argv[i + argc2] = 0 ;
+ execit(argv + i, expr, s, pmatch, flagnosub ? 0 : re.re_nsub, flagnosub) ;
+ }
+ if (r != REG_NOMATCH)
+ {
+ char buf[256] ;
+ regerror(r, &re, buf, 256) ;
+ strerr_diefu6x(111, "match string \"", s, "\" against regex \"", expr, "\": ", buf) ;
+ }
+ }
+ regfree(&re) ;
+ i += argc2 + 1 ;
+ }
+ xexec0(argv + argc1 + 1) ;
+}
diff --git a/src/execline/deps-exe/case b/src/execline/deps-exe/case
new file mode 100644
index 0000000..97021b5
--- /dev/null
+++ b/src/execline/deps-exe/case
@@ -0,0 +1,2 @@
+${LIBEXECLINE}
+-lskarnet