From bca250b0b2a1df4ceb939b71a269f71501fda024 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 30 Jul 2026 03:21:52 +0000 Subject: Prepare for 2.10.0.0; remove pedantic posix; support loopwhilex -x "" --- src/execline/cd.c | 17 ++++ src/execline/deps-exe/cd | 1 + src/execline/deps-exe/execline-cd | 1 - src/execline/deps-exe/execline-umask | 1 - src/execline/deps-exe/posix-cd | 1 - src/execline/deps-exe/posix-umask | 1 - src/execline/deps-exe/umask | 1 + src/execline/emptyenv.c | 36 ++++---- src/execline/execline-cd.c | 17 ---- src/execline/execline-umask.c | 19 ---- src/execline/loopwhilex.c | 102 +++++++++++--------- src/execline/posix-cd.c | 160 -------------------------------- src/execline/posix-umask.c | 174 ----------------------------------- src/execline/posix-umask.txt | 37 -------- src/execline/redirfd.c | 94 +++++++++---------- src/execline/umask.c | 158 +++++++++++++++++++++++++++++++ src/execline/umask.txt | 37 ++++++++ src/execline/wait.c | 45 ++++----- 18 files changed, 358 insertions(+), 544 deletions(-) create mode 100644 src/execline/cd.c create mode 100644 src/execline/deps-exe/cd delete mode 100644 src/execline/deps-exe/execline-cd delete mode 100644 src/execline/deps-exe/execline-umask delete mode 100644 src/execline/deps-exe/posix-cd delete mode 100644 src/execline/deps-exe/posix-umask create mode 100644 src/execline/deps-exe/umask delete mode 100644 src/execline/execline-cd.c delete mode 100644 src/execline/execline-umask.c delete mode 100644 src/execline/posix-cd.c delete mode 100644 src/execline/posix-umask.c delete mode 100644 src/execline/posix-umask.txt create mode 100644 src/execline/umask.c create mode 100644 src/execline/umask.txt (limited to 'src') diff --git a/src/execline/cd.c b/src/execline/cd.c new file mode 100644 index 0000000..734c375 --- /dev/null +++ b/src/execline/cd.c @@ -0,0 +1,17 @@ +/* ISC license. */ + +#include + +#include +#include + +#define USAGE "cd path prog..." + +int main (int argc, char const *const *argv) +{ + PROG = "cd" ; + if (argc < 3) strerr_dieusage(100, USAGE) ; + if (chdir(argv[1]) == -1) + strerr_diefu2sys(111, "chdir to ", argv[1]) ; + xexec(argv+2) ; +} diff --git a/src/execline/deps-exe/cd b/src/execline/deps-exe/cd new file mode 100644 index 0000000..e7187fe --- /dev/null +++ b/src/execline/deps-exe/cd @@ -0,0 +1 @@ +-lskarnet diff --git a/src/execline/deps-exe/execline-cd b/src/execline/deps-exe/execline-cd deleted file mode 100644 index e7187fe..0000000 --- a/src/execline/deps-exe/execline-cd +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/execline/deps-exe/execline-umask b/src/execline/deps-exe/execline-umask deleted file mode 100644 index e7187fe..0000000 --- a/src/execline/deps-exe/execline-umask +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/execline/deps-exe/posix-cd b/src/execline/deps-exe/posix-cd deleted file mode 100644 index e7187fe..0000000 --- a/src/execline/deps-exe/posix-cd +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/execline/deps-exe/posix-umask b/src/execline/deps-exe/posix-umask deleted file mode 100644 index e7187fe..0000000 --- a/src/execline/deps-exe/posix-umask +++ /dev/null @@ -1 +0,0 @@ --lskarnet diff --git a/src/execline/deps-exe/umask b/src/execline/deps-exe/umask new file mode 100644 index 0000000..e7187fe --- /dev/null +++ b/src/execline/deps-exe/umask @@ -0,0 +1 @@ +-lskarnet diff --git a/src/execline/emptyenv.c b/src/execline/emptyenv.c index ac59697..ed1a893 100644 --- a/src/execline/emptyenv.c +++ b/src/execline/emptyenv.c @@ -19,22 +19,22 @@ enum emptyenv_golb_e { - GOLB_PATH = 0x01, - GOLB_LIB = 0x02, - GOLB_CLEANUP = 0x04, - GOLB_OPT = 0x08, - GOLB_POS = 0x10 + EMPTYENV_GOLB_PATH = 0x01, + EMPTYENV_GOLB_LIB = 0x02, + EMPTYENV_GOLB_CLEANUP = 0x04, + EMPTYENV_GOLB_OPT = 0x08, + EMPTYENV_GOLB_POS = 0x10 } ; int main (int argc, char const *const *argv) { static gol_bool const rgolb[5] = { - { .so = 'p', .lo = "keep-path", .clear = 0, .set = GOLB_PATH }, - { .so = 'l', .lo = "keep-ld-library-path", .clear = 0, .set = GOLB_LIB }, - { .so = 'c', .lo = "cleanup", .clear = 0, .set = GOLB_CLEANUP }, - { .so = 'o', .lo = "pop-elgetopt", .clear = 0, .set = GOLB_OPT }, - { .so = 'P', .lo = "pop-execline", .clear = 0, .set = GOLB_POS } + { .so = 'p', .lo = "keep-path", .clear = 0, .set = EMPTYENV_GOLB_PATH }, + { .so = 'l', .lo = "keep-ld-library-path", .clear = 0, .set = EMPTYENV_GOLB_LIB }, + { .so = 'c', .lo = "cleanup", .clear = 0, .set = EMPTYENV_GOLB_CLEANUP }, + { .so = 'o', .lo = "pop-elgetopt", .clear = 0, .set = EMPTYENV_GOLB_OPT }, + { .so = 'P', .lo = "pop-execline", .clear = 0, .set = EMPTYENV_GOLB_POS } } ; uint64_t wgolb = 0 ; unsigned int golc ; @@ -43,7 +43,7 @@ int main (int argc, char const *const *argv) argc -= golc ; argv += golc ; if (!argc) strerr_dieusage(100, USAGE) ; - if (wgolb & GOLB_CLEANUP) + if (wgolb & EMPTYENV_GOLB_CLEANUP) { static char const *const onebyte = "!?#0" ; char *const *envp = environ ; @@ -88,12 +88,12 @@ int main (int argc, char const *const *argv) strerr_diefu1sys(111, "clean up environment") ; } - else if (wgolb & (GOLB_OPT | GOLB_POS)) + else if (wgolb & (EMPTYENV_GOLB_OPT | EMPTYENV_GOLB_POS)) { static char const *const list[12] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "#", "ELGETOPT_" } ; stralloc sa = STRALLOC_ZERO ; size_t envlen = env_len((char const *const *)environ) ; - int n = el_popenv(&sa, (char const *const *)environ, envlen, wgolb & GOLB_POS ? list : list + 11, (wgolb & GOLB_POS ? 11 : 0) + !!(wgolb & GOLB_OPT)) ; + int n = el_popenv(&sa, (char const *const *)environ, envlen, wgolb & EMPTYENV_GOLB_POS ? list : list + 11, (wgolb & EMPTYENV_GOLB_POS ? 11 : 0) + !!(wgolb & EMPTYENV_GOLB_OPT)) ; if (n < 0) strerr_diefu1sys(111, "pop current execline environment") ; { char const *v[envlen - n + 1] ; @@ -108,18 +108,18 @@ int main (int argc, char const *const *argv) char const *newenv[3] = { 0, 0, 0 } ; char *const *envp = environ ; unsigned int m = 0 ; - wgolb &= GOLB_PATH | GOLB_LIB ; + wgolb &= EMPTYENV_GOLB_PATH | EMPTYENV_GOLB_LIB ; for (; wgolb && *envp ; envp++) { - if (wgolb & GOLB_PATH && !strncmp(*envp, "PATH=", 5)) + if (wgolb & EMPTYENV_GOLB_PATH && !strncmp(*envp, "PATH=", 5)) { newenv[m++] = *envp ; - wgolb &= ~GOLB_PATH ; + wgolb &= ~EMPTYENV_GOLB_PATH ; } - if (wgolb & GOLB_LIB && !strncmp(*envp, "LD_LIBRARY_PATH=", 16)) + if (wgolb & EMPTYENV_GOLB_LIB && !strncmp(*envp, "LD_LIBRARY_PATH=", 16)) { newenv[m++] = *envp ; - wgolb &= ~GOLB_LIB ; + wgolb &= ~EMPTYENV_GOLB_LIB ; } } xexec_e(argv, newenv) ; diff --git a/src/execline/execline-cd.c b/src/execline/execline-cd.c deleted file mode 100644 index 1762708..0000000 --- a/src/execline/execline-cd.c +++ /dev/null @@ -1,17 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include - -#define USAGE "cd path prog..." - -int main (int argc, char const *const *argv) -{ - PROG = "execline-cd" ; - if (argc < 3) strerr_dieusage(100, USAGE) ; - if (chdir(argv[1]) == -1) - strerr_diefu2sys(111, "chdir to ", argv[1]) ; - xexec(argv+2) ; -} diff --git a/src/execline/execline-umask.c b/src/execline/execline-umask.c deleted file mode 100644 index c5aacda..0000000 --- a/src/execline/execline-umask.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include -#include - -#define USAGE "umask value prog..." - -int main (int argc, char const *const *argv) -{ - unsigned int m ; - PROG = "execline-umask" ; - if (argc < 3) strerr_dieusage(100, USAGE) ; - if (!uint_oscan(argv[1], &m)) strerr_dieusage(100, USAGE) ; - umask(m) ; - xexec(argv+2) ; -} diff --git a/src/execline/loopwhilex.c b/src/execline/loopwhilex.c index acae3b3..fdd05b8 100644 --- a/src/execline/loopwhilex.c +++ b/src/execline/loopwhilex.c @@ -1,73 +1,91 @@ /* ISC license. */ +#include +#include #include -#include -#include +#include +#include #include +#include #include #include -#define USAGE "loopwhilex [ -n ] [ -o okcode,okcode,... | -x exitcode,exitcode,... ] prog..." +#define USAGE "loopwhilex [ -o okcode,okcode,... | -x exitcode,exitcode,... ] prog..." #define dieusage() strerr_dieusage(100, USAGE) -static int lw_isok (unsigned short *tab, unsigned int n, int code) +enum lw_gola_e +{ + LW_GOLA_OK, + LW_GOLA_EXCLUDE, + LW_GOLA_N +} ; + +static int lw_uint8_cmp (void const *a, void const *b) +{ + uint8_t aa = *(uint8_t const *)a ; + uint8_t bb = *(uint8_t const *)b ; + return aa < bb ? -1 : aa > bb ; +} + +static size_t lw_scanlist (char const *s, uint8_t *tab) { - unsigned int i = 0 ; - for (; i < n ; i++) if ((unsigned short)code == tab[i]) break ; - return i < n ; + size_t n = 0 ; + while (*s) + { + uint64_t u ; + size_t l = uint64_scan(s, &u) ; + if (!l) break ; + if (u > 256 || n >= 255) dieusage() ; + tab[n++] = u ; + s += l ; + while (*s == ',') s++ ; + } + if (*s) dieusage() ; + qsort(tab, n, 1, &lw_uint8_cmp) ; + return n ; } int main (int argc, char const *const *argv, char const *const *envp) { - int wstat ; - int not = 0, cont = 1, rev = 0 ; - unsigned short okcodes[256] ; - size_t nbc = 0 ; + static gol_arg const rgola[] = + { + { .so = 'o', .lo = "ok-codes", .i = LW_GOLA_OK }, + { .so = 'x', .lo = "exclude-codes", .i = LW_GOLA_EXCLUDE }, + } ; + int rev = 0 ; + char const *wgola[LW_GOLA_N] = { 0 } ; + size_t n = 0 ; + uint8_t codes[256] = { 0 } ; PROG = "loopwhilex" ; + { - subgetopt l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "no:x:", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'n' : not = 1 ; break ; - case 'o' : - rev = 0 ; - if (!ushort_scanlist(okcodes, 256, l.arg, &nbc)) dieusage() ; - break ; - case 'x' : - rev = 1 ; - if (!ushort_scanlist(okcodes, 256, l.arg, &nbc)) dieusage() ; - break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; + unsigned int golc = gol_main(argc, argv, 0, 0, rgola, sizeof(rgola)/sizeof(gol_arg), 0, wgola) ; + argc -= golc ; argv += golc ; } if (!argc) dieusage() ; - - if (!nbc) + if (wgola[LW_GOLA_OK]) n = lw_scanlist(wgola[LW_GOLA_OK], codes) ; + else if (wgola[LW_GOLA_EXCLUDE]) { - okcodes[0] = 0 ; - nbc = 1 ; + n = lw_scanlist(wgola[LW_GOLA_EXCLUDE], codes) ; + rev = 1 ; } - else if (rev) not = !not ; + else n = 1 ; - while (cont) + for (;;) { + int wstat ; pid_t pid = el_spawn0(argv[0], argv, envp) ; if (!pid) { if (errno == ENOENT && argv[0][0] == ' ') - strerr_diefu3x(111, "spawn ", argv[0], ": name begins with a space, are you trying to spawn a block as your loop body?") ; - else strerr_diefu2sys(111, "spawn ", argv[0]) ; + strerr_dief(111, "spawn ", argv[0], ": name begins with a space, are you trying to spawn a block as your loop body?") ; + else strerr_diefusys(111, "spawn ", argv[0]) ; } - if (wait_pid(pid, &wstat) < 0) strerr_diefu1sys(111, "wait_pid") ; - cont = not != lw_isok(okcodes, nbc, wait_estatus(wstat)) ; + if (wait_pid(pid, &wstat) < 0) strerr_diefusys(111, "wait_pid") ; + codes[255] = wait_estatus(wstat) ; + if (rev != !bsearch(codes + 255, codes, n, 1, &lw_uint8_cmp)) break ; } - return wait_estatus(wstat) ; + return codes[255] ; } diff --git a/src/execline/posix-cd.c b/src/execline/posix-cd.c deleted file mode 100644 index e9bfd35..0000000 --- a/src/execline/posix-cd.c +++ /dev/null @@ -1,160 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#define USAGE "posix-cd [ -L | -P ] [ - | path ] [ prog... ]" -#define dieusage() strerr_dieusage(100, USAGE) -#define dienomem() strerr_diefu1sys(111, "stralloc_catb") - -int main (int argc, char const **argv) -{ - int phy = 0 ; - int dopwd = 0 ; - char const *where ; - int got = 0 ; - stralloc sa = STRALLOC_ZERO ; - PROG = "posix-cd" ; - setlocale(LC_ALL, "") ; /* yeah, as if */ - - { - subgetopt l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "LP", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'L' : phy = 0 ; break ; - case 'P' : phy = 1 ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - } - if (!argc) where = getenv("HOME") ; - else - { - where = *argv++ ; - if (!strcmp(where, "-")) - { - where = getenv("OLDPWD") ; - dopwd = 1 ; - } - } - if (!where || !where[0]) dieusage() ; - - if (!(where[0] == '/' || (where[0] == '.' && (!where[1] || where[1] == '/' || (where[1] == '.' && (!where[2] || where[2] == '/')))))) - { - char const *cdpath = getenv("CDPATH") ; - if (cdpath) - { - size_t pos = 0 ; - size_t len = strlen(cdpath) ; - while (pos < len) - { - struct stat st ; - size_t m = byte_chr(cdpath + pos, len - pos, ':') ; - sa.len = 0 ; - if (m) - { - if (!stralloc_catb(&sa, cdpath + pos, m)) dienomem() ; - if (cdpath[pos + m - 1] != '/' && !stralloc_catb(&sa, "/", 1)) dienomem() ; - } - else if (!stralloc_catb(&sa, "./", 2)) dienomem() ; - if (!stralloc_cats(&sa, where) || !stralloc_0(&sa)) dienomem() ; - if (!stat(sa.s, &st) && S_ISDIR(st.st_mode)) - { - got = 1 ; - dopwd = 1 ; - break ; - } - pos += m+1 ; - } - } - } - - if (!got && (!stralloc_cats(&sa, where) || !stralloc_0(&sa))) dienomem() ; - - { - size_t sabase = sa.len ; - if (sagetcwd(&sa) < 0) strerr_diefu1sys(111, "getcwd") ; - if (!stralloc_0(&sa)) dienomem() ; - if (!env_mexec("OLDPWD", sa.s + sabase)) dienomem() ; - sa.len = sabase ; - } - - if (!phy) - { - char const *x = getenv("PWD") ; - if (x && sa.s[0] != '/') - { - size_t len = strlen(x) ; - int doslash = len && x[len-1] != '/' ; - if (!stralloc_insertb(&sa, 0, x, len + doslash)) dienomem() ; - if (doslash) sa.s[len] = '/' ; - } - { - stralloc tmp = STRALLOC_ZERO ; - if (!stralloc_ready(&tmp, sa.len + 2)) dienomem() ; - tmp.len = path_canonicalize(tmp.s, sa.s, 1) ; - if (!tmp.len++) - strerr_diefu4sys(111, "canonicalize ", sa.s, ": problem with ", tmp.s) ; - stralloc_free(&sa) ; - sa = tmp ; - } - if (!env_mexec("PWD", sa.s)) dienomem() ; -#ifdef PATH_MAX - if (sa.len > PATH_MAX && strlen(where) < PATH_MAX && x && *x) - { - size_t len = strlen(x) ; - int hasslash = x[len-1] == '/' ; - if (!strncmp(sa.s, x, len)) - { - if (hasslash || (sa.len > len && sa.s[len] == '/')) - { - sa.len -= len + !hasslash ; - memmove(sa.s, sa.s + len + !hasslash, sa.len) ; - } - } - } -#endif - } - - /* fking finally */ - - if (chdir(sa.s) < 0) - strerr_diefu2sys(111, "chdir to ", where) ; - - /* and there's still more nonsense to do afterwards! */ - - if (phy) - { - sa.len = 0 ; - if (sagetcwd(&sa) < 0) strerr_diefu1sys(111, "getcwd") ; - if (!stralloc_0(&sa)) dienomem() ; - if (!env_mexec("PWD", sa.s)) dienomem() ; - } - - if (dopwd) - { - sa.s[sa.len - 1] = '\n' ; - if (allwrite(1, sa.s, sa.len) < sa.len) - strerr_diefu1sys(111, "write to stdout") ; - } - - xmexec0(argv) ; -} diff --git a/src/execline/posix-umask.c b/src/execline/posix-umask.c deleted file mode 100644 index 69f4b08..0000000 --- a/src/execline/posix-umask.c +++ /dev/null @@ -1,174 +0,0 @@ -/* ISC license. */ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#define USAGE "posix-umask [ -S ] [ mask ] [ prog... ]" -#define dieusage() strerr_dieusage(100, USAGE) -#define dieout() strerr_diefu1sys(111, "write to stdout") - - - /* well, unlike posix-cd, at least this one was fun to write */ - -static inline int pu_output (int sym) -{ - mode_t mode = umask(0) ; - size_t m = 0 ; - char fmt[18] ; - if (sym) - { - unsigned int i = 3 ; - while (i--) - { - unsigned int mask = ~(mode >> (3*i)) ; - fmt[m++] = "ogu"[i] ; fmt[m++] = '=' ; - if (mask & 4) fmt[m++] = 'r' ; - if (mask & 2) fmt[m++] = 'w' ; - if (mask & 1) fmt[m++] = 'x' ; - if (i) fmt[m++] = ',' ; - } - } - else m += uint0_ofmt(fmt, mode, 4) ; - fmt[m++] = '\n' ; - if (buffer_putflush(buffer_1, fmt, m) < 0) dieout() ; - return 0 ; -} - -static void pu_diesyntax (char const *) gccattr_noreturn ; -static void pu_diesyntax (char const *s) -{ - strerr_dief3x(101, "internal parsing error: bad ", s, ". Please submit a bug-report.") ; -} - -static inline uint8_t pu_cclass (char c) -{ - /* char tables may be more efficient, but this is way more readable */ - switch (c) - { - case 0 : return 0 ; - case ',' : return 1 ; - case '+' : - case '-' : - case '=' : return 2 ; - case 'u' : - case 'g' : - case 'o' : return 3 ; - case 'a' : return 4 ; - case 'r' : - case 'w' : - case 'x' : - case 'X' : - case 's' : - case 't' : return 5 ; - default : return 6 ; - } -} - -static inline uint8_t pu_who_value (char c) -{ - switch (c) - { - case 'u' : return 4 ; - case 'g' : return 2 ; - case 'o' : return 1 ; - case 'a' : - case '+' : /* shortcut for when who is empty */ - case '-' : - case '=' : return 7 ; - default : pu_diesyntax("who") ; - } -} - -static inline uint8_t pu_perm_value (char c) -{ - switch (c) - { - case 'r' : return 4 ; - case 'w' : return 2 ; - case 'x' : - case 'X' : return 1 ; - case 's' : - case 't' : return 0 ; - default : pu_diesyntax("perm") ; - } -} - -static inline unsigned int pu_parsemode (char const *s) -{ - static uint16_t const table[5][7] = - { - { 0x005, 0x000, 0x064, 0x021, 0x021, 0x006, 0x006 }, - { 0x005, 0x006, 0x042, 0x021, 0x021, 0x006, 0x006 }, - { 0x005, 0x200, 0x042, 0x083, 0x006, 0x104, 0x006 }, - { 0x805, 0xe00, 0xc42, 0x006, 0x006, 0x006, 0x006 }, - { 0x805, 0xe00, 0xc42, 0x006, 0x006, 0x104, 0x006 } - } ; - unsigned int oldmode = ~umask(0) ; - uint8_t modes[3] = { oldmode & 7, (oldmode >> 3) & 7, (oldmode >> 6) & 7 } ; - uint8_t who = 0 ; - uint8_t perm = 0 ; - uint8_t state = 0 ; - char op = 0 ; - while (state < 5) - { - char c = *s++ ; - uint16_t what = table[state][pu_cclass(c)] ; - state = what & 7 ; - if (what & 0x020) who |= pu_who_value(c) ; - if (what & 0x080) perm = modes[byte_chr("ogu", 3, c)] ; - if (what & 0x100) perm |= pu_perm_value(c) ; - if (what & 0x800) - { - unsigned int i = 3 ; - while (i--) if (who & (1 << i)) - switch (op) - { - case '-' : modes[i] &= ~perm ; break ; - case '+' : modes[i] |= perm ; break ; - case '=' : modes[i] = perm ; break ; - default : pu_diesyntax("op") ; - } - } - if (what & 0x040) op = c ; - if (what & 0x200) who = 0 ; - if (what & 0x400) perm = 0 ; - } - if (state > 5) strerr_dief1x(1, "invalid mode string") ; - return ((unsigned int)modes[2] << 6) | ((unsigned int)modes[1] << 3) | modes[0] ; -} - -int main (int argc, char const **argv) -{ - int sym = 0 ; - unsigned int mode ; - PROG = "posix-umask" ; - setlocale(LC_ALL, "") ; /* totally supported, I swear */ - - { - subgetopt l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "S", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'S' : sym = 1 ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - } - if (!argc) return pu_output(sym) ; - if (!uint0_oscan(argv[0], &mode)) mode = ~pu_parsemode(argv[0]) ; - umask(mode & 00777) ; - xexec0(argv+1) ; -} diff --git a/src/execline/posix-umask.txt b/src/execline/posix-umask.txt deleted file mode 100644 index 5bf49a2..0000000 --- a/src/execline/posix-umask.txt +++ /dev/null @@ -1,37 +0,0 @@ - - parsemode() function for posix-umask - - goal: parse the "u+r,g-wx,o=u" symbolic mode string and convert it -to a numeric value suitable for umask(). - In the purest skarnet.org tradition, we implement the parser via a DFA. - -class | 0 1 2 3 4 5 6 -st\ev | \0 , +-= ugo a rwxXst other ------------------------------------------------------------------------- -START | wo w w -0 | END START OP WHO WHO X X - -WHO | o w w -1 | END X OP WHO WHO X X - -OP | r o c p -2 | END START OP PERMCPY X PERM X - -PERMCPY | ! !rR !Ro -3 | END START OP X X X X - -PERM | ! !rR !Ro p -4 | END START OP X X PERM X ------------------------------------------------------------------------- - -END=5, X=6. -> states: 3 bits -7 actions -> 10 bits total, need uint16_t - - w: 0x020: who |= c - o: 0x040: store op - c: 0x080: copy perm from c - p: 0x100: perm |= c - r: 0x200: reset who - R: 0x400: reset perm - !: 0x800: apply (who, op, perm) change - diff --git a/src/execline/redirfd.c b/src/execline/redirfd.c index c3562f9..c9a7f9e 100644 --- a/src/execline/redirfd.c +++ b/src/execline/redirfd.c @@ -13,47 +13,47 @@ #define USAGE "redirfd -[ r | w | u | a | x ] [ -N | -n ] [ -b ] [ -f ] [ -E | -e ] fd|var file prog..." #define dieusage() strerr_dieusage(100, USAGE) -enum golb_e +enum redirfd_golb_e { - GOLB_READ = 0x0001, - GOLB_WRITE = 0x0002, - GOLB_CREAT = 0x0004, - GOLB_APPEND = 0x0008, - GOLB_TRUNC = 0x0010, - GOLB_EXCL = 0x0020, - GOLB_NONBLOCK = 0x0040, - GOLB_CHANGEMODE = 0x0100, - GOLB_OPEN = 0x0200, - GOLB_AUTOIMPORT = 0x0400, + REDIRFD_GOLB_READ = 0x0001, + REDIRFD_GOLB_WRITE = 0x0002, + REDIRFD_GOLB_CREAT = 0x0004, + REDIRFD_GOLB_APPEND = 0x0008, + REDIRFD_GOLB_TRUNC = 0x0010, + REDIRFD_GOLB_EXCL = 0x0020, + REDIRFD_GOLB_NONBLOCK = 0x0040, + REDIRFD_GOLB_CHANGEMODE = 0x0100, + REDIRFD_GOLB_OPEN = 0x0200, + REDIRFD_GOLB_AUTOIMPORT = 0x0400, } ; int main (int argc, char const *const *argv) { static gol_bool const rgolb[] = { - { .so = 'r', .lo = 0, .clear = GOLB_WRITE | GOLB_APPEND | GOLB_CREAT | GOLB_TRUNC | GOLB_EXCL, .set = GOLB_READ }, - { .so = 'w', .lo = 0, .clear = GOLB_READ | GOLB_APPEND | GOLB_EXCL, .set = GOLB_WRITE | GOLB_CREAT | GOLB_TRUNC }, - { .so = 'u', .lo = 0, .clear = GOLB_APPEND | GOLB_CREAT | GOLB_TRUNC | GOLB_EXCL, .set = GOLB_READ | GOLB_WRITE }, - { .so = 'a', .lo = 0, .clear = GOLB_READ | GOLB_TRUNC | GOLB_EXCL, .set = GOLB_WRITE | GOLB_CREAT | GOLB_APPEND }, - { .so = 'x', .lo = 0, .clear = GOLB_READ | GOLB_APPEND | GOLB_TRUNC, .set = GOLB_WRITE | GOLB_CREAT | GOLB_EXCL }, - { .so = 'N', .lo = "block", .clear = GOLB_NONBLOCK, .set = 0 }, - { .so = 'n', .lo = "nonblock", .clear = 0, .set = GOLB_NONBLOCK }, - { .so = 'b', .lo = "switch-block", .clear = 0, .set = GOLB_CHANGEMODE }, - { .so = 'f', .lo = "open", .clear = 0, .set = GOLB_OPEN }, - { .so = 'e', .lo = "no-autoimport", .clear = GOLB_AUTOIMPORT, .set = 0 }, - { .so = 'E', .lo = "autoimport", .clear = 0, .set = GOLB_AUTOIMPORT }, - { .so = 0, .lo = "no-read", .clear = GOLB_READ, .set = 0 }, - { .so = 0, .lo = "read", .clear = 0, .set = GOLB_READ }, - { .so = 0, .lo = "no-write", .clear = GOLB_WRITE, .set = 0 }, - { .so = 0, .lo = "write", .clear = 0, .set = GOLB_WRITE }, - { .so = 0, .lo = "no-create", .clear = GOLB_CREAT, .set = 0 }, - { .so = 0, .lo = "create", .clear = 0, .set = GOLB_CREAT }, - { .so = 0, .lo = "no-append", .clear = GOLB_APPEND, .set = 0 }, - { .so = 0, .lo = "append", .clear = 0, .set = GOLB_APPEND }, - { .so = 0, .lo = "no-trunc", .clear = GOLB_TRUNC, .set = 0 }, - { .so = 0, .lo = "trunc", .clear = 0, .set = GOLB_TRUNC }, - { .so = 0, .lo = "no-excl", .clear = GOLB_EXCL, .set = 0 }, - { .so = 0, .lo = "excl", .clear = 0, .set = GOLB_EXCL }, + { .so = 'r', .lo = 0, .clear = REDIRFD_GOLB_WRITE | REDIRFD_GOLB_APPEND | REDIRFD_GOLB_CREAT | REDIRFD_GOLB_TRUNC | REDIRFD_GOLB_EXCL, .set = REDIRFD_GOLB_READ }, + { .so = 'w', .lo = 0, .clear = REDIRFD_GOLB_READ | REDIRFD_GOLB_APPEND | REDIRFD_GOLB_EXCL, .set = REDIRFD_GOLB_WRITE | REDIRFD_GOLB_CREAT | REDIRFD_GOLB_TRUNC }, + { .so = 'u', .lo = 0, .clear = REDIRFD_GOLB_APPEND | REDIRFD_GOLB_CREAT | REDIRFD_GOLB_TRUNC | REDIRFD_GOLB_EXCL, .set = REDIRFD_GOLB_READ | REDIRFD_GOLB_WRITE }, + { .so = 'a', .lo = 0, .clear = REDIRFD_GOLB_READ | REDIRFD_GOLB_TRUNC | REDIRFD_GOLB_EXCL, .set = REDIRFD_GOLB_WRITE | REDIRFD_GOLB_CREAT | REDIRFD_GOLB_APPEND }, + { .so = 'x', .lo = 0, .clear = REDIRFD_GOLB_READ | REDIRFD_GOLB_APPEND | REDIRFD_GOLB_TRUNC, .set = REDIRFD_GOLB_WRITE | REDIRFD_GOLB_CREAT | REDIRFD_GOLB_EXCL }, + { .so = 'N', .lo = "block", .clear = REDIRFD_GOLB_NONBLOCK, .set = 0 }, + { .so = 'n', .lo = "nonblock", .clear = 0, .set = REDIRFD_GOLB_NONBLOCK }, + { .so = 'b', .lo = "switch-block", .clear = 0, .set = REDIRFD_GOLB_CHANGEMODE }, + { .so = 'f', .lo = "open", .clear = 0, .set = REDIRFD_GOLB_OPEN }, + { .so = 'e', .lo = "no-autoimport", .clear = REDIRFD_GOLB_AUTOIMPORT, .set = 0 }, + { .so = 'E', .lo = "autoimport", .clear = 0, .set = REDIRFD_GOLB_AUTOIMPORT }, + { .so = 0, .lo = "no-read", .clear = REDIRFD_GOLB_READ, .set = 0 }, + { .so = 0, .lo = "read", .clear = 0, .set = REDIRFD_GOLB_READ }, + { .so = 0, .lo = "no-write", .clear = REDIRFD_GOLB_WRITE, .set = 0 }, + { .so = 0, .lo = "write", .clear = 0, .set = REDIRFD_GOLB_WRITE }, + { .so = 0, .lo = "no-create", .clear = REDIRFD_GOLB_CREAT, .set = 0 }, + { .so = 0, .lo = "create", .clear = 0, .set = REDIRFD_GOLB_CREAT }, + { .so = 0, .lo = "no-append", .clear = REDIRFD_GOLB_APPEND, .set = 0 }, + { .so = 0, .lo = "append", .clear = 0, .set = REDIRFD_GOLB_APPEND }, + { .so = 0, .lo = "no-trunc", .clear = REDIRFD_GOLB_TRUNC, .set = 0 }, + { .so = 0, .lo = "trunc", .clear = 0, .set = REDIRFD_GOLB_TRUNC }, + { .so = 0, .lo = "no-excl", .clear = REDIRFD_GOLB_EXCL, .set = 0 }, + { .so = 0, .lo = "excl", .clear = 0, .set = REDIRFD_GOLB_EXCL }, } ; uint64_t wgolb = 0 ; unsigned int golc ; @@ -65,20 +65,20 @@ int main (int argc, char const *const *argv) golc = gol_main(argc, argv, rgolb, sizeof(rgolb)/sizeof(gol_bool), 0, 0, &wgolb, 0) ; argc -= golc ; argv += golc ; if (argc < 3) dieusage() ; - if (!(wgolb & (GOLB_READ | GOLB_WRITE))) dieusage() ; - if (!(wgolb & GOLB_OPEN)) + if (!(wgolb & (REDIRFD_GOLB_READ | REDIRFD_GOLB_WRITE))) dieusage() ; + if (!(wgolb & REDIRFD_GOLB_OPEN)) if (!uint0_scan(argv[0], &fdto)) dieusage() ; flags = - (wgolb & GOLB_WRITE ? wgolb & GOLB_READ ? O_RDWR : O_WRONLY : O_RDONLY) | - (wgolb & GOLB_CREAT ? O_CREAT : 0) | - (wgolb & GOLB_APPEND ? O_APPEND : 0) | - (wgolb & GOLB_TRUNC ? O_TRUNC : 0) | - (wgolb & GOLB_EXCL ? O_EXCL : 0) | - (wgolb & GOLB_NONBLOCK ? O_NONBLOCK : 0) ; + (wgolb & REDIRFD_GOLB_WRITE ? wgolb & REDIRFD_GOLB_READ ? O_RDWR : O_WRONLY : O_RDONLY) | + (wgolb & REDIRFD_GOLB_CREAT ? O_CREAT : 0) | + (wgolb & REDIRFD_GOLB_APPEND ? O_APPEND : 0) | + (wgolb & REDIRFD_GOLB_TRUNC ? O_TRUNC : 0) | + (wgolb & REDIRFD_GOLB_EXCL ? O_EXCL : 0) | + (wgolb & REDIRFD_GOLB_NONBLOCK ? O_NONBLOCK : 0) ; fd = open3(argv[1], flags, 0666) ; - if (fd == -1 && wgolb & GOLB_WRITE && errno == ENXIO) + if (fd == -1 && wgolb & REDIRFD_GOLB_WRITE && errno == ENXIO) { int fdr = open_read(argv[1]) ; if (fdr == -1) strerr_diefu2sys(111, "open_read ", argv[1]) ; @@ -87,14 +87,14 @@ int main (int argc, char const *const *argv) } if (fd == -1) strerr_diefu2sys(111, "open ", argv[1]) ; - if (wgolb & GOLB_CHANGEMODE) - if ((wgolb & GOLB_NONBLOCK ? ndelay_off(fd) : ndelay_on(fd)) == -1) + if (wgolb & REDIRFD_GOLB_CHANGEMODE) + if ((wgolb & REDIRFD_GOLB_NONBLOCK ? ndelay_off(fd) : ndelay_on(fd)) == -1) strerr_diefu1sys(111, "change blocking mode") ; - if (wgolb & GOLB_OPEN) + if (wgolb & REDIRFD_GOLB_OPEN) { char fmt[UINT_FMT] ; fmt[uint_fmt(fmt, fd)] = 0 ; - el_modif_and_exec(argv+2, argv[0], fmt, !!(wgolb & GOLB_AUTOIMPORT)) ; + el_modif_and_exec(argv+2, argv[0], fmt, !!(wgolb & REDIRFD_GOLB_AUTOIMPORT)) ; } else { diff --git a/src/execline/umask.c b/src/execline/umask.c new file mode 100644 index 0000000..819cf96 --- /dev/null +++ b/src/execline/umask.c @@ -0,0 +1,158 @@ +/* ISC license. */ + +#include +#include + +#include +#include +#include +#include +#include + +#define USAGE "umask [ -S ] [ mask ] [ prog... ]" +#define dieusage() strerr_dieusage(100, USAGE) +#define dieout() strerr_diefu1sys(111, "write to stdout") + +static inline int umask_output (int sym) +{ + mode_t mode = umask(0) ; + size_t m = 0 ; + char fmt[18] ; + if (sym) + { + unsigned int i = 3 ; + while (i--) + { + unsigned int mask = ~(mode >> (3*i)) ; + fmt[m++] = "ogu"[i] ; fmt[m++] = '=' ; + if (mask & 4) fmt[m++] = 'r' ; + if (mask & 2) fmt[m++] = 'w' ; + if (mask & 1) fmt[m++] = 'x' ; + if (i) fmt[m++] = ',' ; + } + } + else m += uint0_ofmt(fmt, mode, 4) ; + fmt[m++] = '\n' ; + if (buffer_putflush(buffer_1, fmt, m) < 0) dieout() ; + return 0 ; +} + +static void umask_diesyntax (char const *) gccattr_noreturn ; +static void umask_diesyntax (char const *s) +{ + strerr_dief(101, "internal parsing error: bad ", s, ". Please submit a bug-report.") ; +} + +static inline uint8_t pu_cclass (char c) +{ + /* char tables may be more efficient, but this is way more readable */ + switch (c) + { + case 0 : return 0 ; + case ',' : return 1 ; + case '+' : + case '-' : + case '=' : return 2 ; + case 'u' : + case 'g' : + case 'o' : return 3 ; + case 'a' : return 4 ; + case 'r' : + case 'w' : + case 'x' : + case 'X' : + case 's' : + case 't' : return 5 ; + default : return 6 ; + } +} + +static inline uint8_t umask_who_value (char c) +{ + switch (c) + { + case 'u' : return 4 ; + case 'g' : return 2 ; + case 'o' : return 1 ; + case 'a' : + case '+' : /* shortcut for when who is empty */ + case '-' : + case '=' : return 7 ; + default : umask_diesyntax("who") ; + } +} + +static inline uint8_t umask_perm_value (char c) +{ + switch (c) + { + case 'r' : return 4 ; + case 'w' : return 2 ; + case 'x' : + case 'X' : return 1 ; + case 's' : + case 't' : return 0 ; + default : umask_diesyntax("perm") ; + } +} + +static inline unsigned int umask_parsemode (char const *s) +{ + static uint16_t const table[5][7] = + { + { 0x005, 0x000, 0x064, 0x021, 0x021, 0x006, 0x006 }, + { 0x005, 0x006, 0x042, 0x021, 0x021, 0x006, 0x006 }, + { 0x005, 0x200, 0x042, 0x083, 0x006, 0x104, 0x006 }, + { 0x805, 0xe00, 0xc42, 0x006, 0x006, 0x006, 0x006 }, + { 0x805, 0xe00, 0xc42, 0x006, 0x006, 0x104, 0x006 } + } ; + unsigned int oldmode = ~umask(0) ; + uint8_t modes[3] = { oldmode & 7, (oldmode >> 3) & 7, (oldmode >> 6) & 7 } ; + uint8_t who = 0 ; + uint8_t perm = 0 ; + uint8_t state = 0 ; + char op = 0 ; + while (state < 5) + { + char c = *s++ ; + uint16_t what = table[state][pu_cclass(c)] ; + state = what & 7 ; + if (what & 0x020) who |= umask_who_value(c) ; + if (what & 0x080) perm = modes[byte_chr("ogu", 3, c)] ; + if (what & 0x100) perm |= umask_perm_value(c) ; + if (what & 0x800) + { + unsigned int i = 3 ; + while (i--) if (who & (1 << i)) + switch (op) + { + case '-' : modes[i] &= ~perm ; break ; + case '+' : modes[i] |= perm ; break ; + case '=' : modes[i] = perm ; break ; + default : umask_diesyntax("op") ; + } + } + if (what & 0x040) op = c ; + if (what & 0x200) who = 0 ; + if (what & 0x400) perm = 0 ; + } + if (state > 5) strerr_dief1x(1, "invalid mode string") ; + return ((unsigned int)modes[2] << 6) | ((unsigned int)modes[1] << 3) | modes[0] ; +} + +int main (int argc, char const **argv) +{ + static gol_bool const rgolb = { .so = 'S', .lo = "symbolic", .clear = 0, .set = 0x01 } ; + uint64_t wgolb = 0 ; + unsigned int mode ; + PROG = "umask" ; + + { + unsigned int golc = gol_main(argc, argv, &rgolb, 1, 0, 0, &wgolb, 0) ; + argc -= golc ; argv += golc ; + } + if (!argc) return umask_output(wgolb & 0x01) ; + if (!uint0_oscan(argv[0], &mode)) mode = ~umask_parsemode(argv[0]) ; + umask(mode & 00777) ; + xexec0(argv+1) ; +} diff --git a/src/execline/umask.txt b/src/execline/umask.txt new file mode 100644 index 0000000..5bf49a2 --- /dev/null +++ b/src/execline/umask.txt @@ -0,0 +1,37 @@ + + parsemode() function for posix-umask + + goal: parse the "u+r,g-wx,o=u" symbolic mode string and convert it +to a numeric value suitable for umask(). + In the purest skarnet.org tradition, we implement the parser via a DFA. + +class | 0 1 2 3 4 5 6 +st\ev | \0 , +-= ugo a rwxXst other +------------------------------------------------------------------------ +START | wo w w +0 | END START OP WHO WHO X X + +WHO | o w w +1 | END X OP WHO WHO X X + +OP | r o c p +2 | END START OP PERMCPY X PERM X + +PERMCPY | ! !rR !Ro +3 | END START OP X X X X + +PERM | ! !rR !Ro p +4 | END START OP X X PERM X +------------------------------------------------------------------------ + +END=5, X=6. -> states: 3 bits +7 actions -> 10 bits total, need uint16_t + + w: 0x020: who |= c + o: 0x040: store op + c: 0x080: copy perm from c + p: 0x100: perm |= c + r: 0x200: reset who + R: 0x400: reset perm + !: 0x800: apply (who, op, perm) change + diff --git a/src/execline/wait.c b/src/execline/wait.c index 6886c0c..d320d54 100644 --- a/src/execline/wait.c +++ b/src/execline/wait.c @@ -15,10 +15,6 @@ #include #include -#ifdef EXECLINE_PEDANTIC_POSIX -#include -#endif - #define USAGE "wait [ -I | -i ] [ -a | -o ] [ -r | -t timeout ] { pids... } [ prog... ]" #define dieusage() strerr_dieusage(100, USAGE) @@ -157,51 +153,48 @@ static int wait_with_timeout (pid_t *pids, unsigned int n, pid_t *returned, ac_f enum wait_golb_e { - GOLB_INSIST = 0x01, - GOLB_JUSTONE = 0x02, - GOLB_REAP = 0x04, + WAIT_GOLB_INSIST = 0x01, + WAIT_GOLB_JUSTONE = 0x02, + WAIT_GOLB_REAP = 0x04, } ; enum wait_gola_e { - GOLA_TIMEOUT, - GOLA_N + WAIT_GOLA_TIMEOUT, + WAIT_GOLA_N } ; int main (int argc, char const **argv) { static gol_bool const rgolb[] = { - { .so = 'I', .lo = "no-insist", .clear = GOLB_INSIST, .set = 0 }, - { .so = 'i', .lo = "insist", .clear = 0, .set = GOLB_INSIST }, - { .so = 'a', .lo = "all", .clear = GOLB_JUSTONE, .set = 0 }, - { .so = 'o', .lo = "one", .clear = 0, .set = GOLB_JUSTONE }, - { .so = 'r', .lo = "reap", .clear = 0, .set = GOLB_REAP }, + { .so = 'I', .lo = "no-insist", .clear = WAIT_GOLB_INSIST, .set = 0 }, + { .so = 'i', .lo = "insist", .clear = 0, .set = WAIT_GOLB_INSIST }, + { .so = 'a', .lo = "all", .clear = WAIT_GOLB_JUSTONE, .set = 0 }, + { .so = 'o', .lo = "one", .clear = 0, .set = WAIT_GOLB_JUSTONE }, + { .so = 'r', .lo = "reap", .clear = 0, .set = WAIT_GOLB_REAP }, } ; static gol_arg const rgola[] = { - { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT }, + { .so = 't', .lo = "timeout", .i = WAIT_GOLA_TIMEOUT }, } ; tain tto = TAIN_INFINITE_RELATIVE ; int argc1 ; int hasblock = 1 ; int e ; uint64_t wgolb = 0 ; - char const *wgola[GOLA_N] = { 0 } ; + char const *wgola[WAIT_GOLA_N] = { 0 } ; unsigned int golc ; pid_t pid = -1 ; PROG = "wait" ; -#ifdef EXECLINE_PEDANTIC_POSIX - setlocale(LC_ALL, "") ; /* but of course, dear POSIX */ -#endif golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ; argc -= golc ; argv += golc ; - if (wgolb & GOLB_REAP) wgola[GOLA_TIMEOUT] = "0" ; - if (wgola[GOLA_TIMEOUT]) + if (wgolb & WAIT_GOLB_REAP) wgola[WAIT_GOLA_TIMEOUT] = "0" ; + if (wgola[WAIT_GOLA_TIMEOUT]) { unsigned int t = 0 ; - if (!uint0_scan(wgola[GOLA_TIMEOUT], &t)) dieusage() ; + if (!uint0_scan(wgola[WAIT_GOLA_TIMEOUT], &t)) dieusage() ; tain_from_millisecs(&tto, t) ; } @@ -223,9 +216,9 @@ int main (int argc, char const **argv) } else pids[0] = 0 ; - e = wgola[GOLA_TIMEOUT] ? /* wait -t30000 whatever */ - wait_with_timeout(pids, n, &pid, argc1 ? &wait_one_from_list_nohang : &wait_one_nohang, &tto, !!(wgolb & GOLB_JUSTONE), !!(wgolb & GOLB_INSIST)) : - wgolb & GOLB_JUSTONE ? + e = wgola[WAIT_GOLA_TIMEOUT] ? /* wait -t30000 whatever */ + wait_with_timeout(pids, n, &pid, argc1 ? &wait_one_from_list_nohang : &wait_one_nohang, &tto, !!(wgolb & WAIT_GOLB_JUSTONE), !!(wgolb & WAIT_GOLB_INSIST)) : + wgolb & WAIT_GOLB_JUSTONE ? argc1 ? wait_one_from_list(pids, n, &pid) : /* wait -o -- 2 3 4 / wait -o -- { 2 3 4 } */ wait_one(&pid) : /* wait -o / wait -o { } */ @@ -234,7 +227,7 @@ int main (int argc, char const **argv) wait_all() ; /* wait / wait { } */ } if (!hasblock) _exit(e >= 0 ? e : 127) ; - if (!(wgolb & GOLB_JUSTONE)) xexec0(argv + argc1 + 1) ; + if (!(wgolb & WAIT_GOLB_JUSTONE)) xexec0(argv + argc1 + 1) ; if (e == -1) xmexec0_n(argv + argc1 + 1, "?\0!", 4, 2) ; { -- cgit v1.3.1