From 3c3373a233f67bb566c8578fb97edf651e2e5f8b Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 8 Jun 2022 02:05:39 -0500 Subject: [PATCH] Reorder functions into logical groups (or at least easier to explain). --- toys/pending/sh.c | 155 +++++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 78 deletions(-) diff --git a/toys/pending/sh.c b/toys/pending/sh.c index ce9b5e18..8d70f85a 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -318,6 +318,8 @@ GLOBALS( // Prototype because $($($(blah))) nests, leading to run->parse->run loop int do_source(char *name, FILE *ff); +// functions contain pipelines contain functions: prototype because loop +static void free_pipeline(void *pipeline); // ordered for greedy matching, so >&; becomes >& ; not > &; // making these const means I need to typecast the const away later to @@ -365,6 +367,13 @@ void debug_show_fds() closedir(X); } +static char **nospace(char **ss) +{ + while (isspace(**ss)) ++*ss; + + return ss; +} + // append to array with null terminator and realloc as necessary static void arg_add(struct sh_arg *arg, char *data) { @@ -457,13 +466,6 @@ static struct sh_vars *addvar(char *s, struct sh_fcall *ff) return ff->vars+ff->varslen++; } -static char **nospace(char **ss) -{ - while (isspace(**ss)) ++*ss; - - return ss; -} - /* 15L ( [ . -> ++ -- 14R (all prefix operators) @@ -542,6 +544,59 @@ static int recalculate(long long *dd, char **ss, int lvl) return 1; } +// Return length of utf8 char @s fitting in len, writing value into *cc +int getutf8(char *s, int len, int *cc) +{ + unsigned wc; + + if (len<0) wc = len = 0; + else if (1>(len = utf8towc(&wc, s, len))) wc = *s, len = 1; + if (cc) *cc = wc; + + return len; +} + +// utf8 strchr: return wide char matched at wc from chrs, or 0 if not matched +// if len, save length of next wc (whether or not it's in list) +static int utf8chr(char *wc, char *chrs, int *len) +{ + unsigned wc1, wc2; + int ll; + + if (len) *len = 1; + if (!*wc) return 0; + if (0<(ll = utf8towc(&wc1, wc, 99))) { + if (len) *len = ll; + while (*chrs) { + if(1>(ll = utf8towc(&wc2, chrs, 99))) chrs++; + else { + if (wc1 == wc2) return wc1; + chrs += ll; + } + } + } + + return 0; +} + +// return length of match found at this point (try is null terminated array) +static int anystart(char *s, char **try) +{ + char *ss = s; + + while (*try) if (strstart(&s, *try++)) return s-ss; + + return 0; +} + +// does this entire string match one of the strings in try[] +static int anystr(char *s, char **try) +{ + while (*try) if (!strcmp(s, *try++)) return 1; + + return 0; +} + static int calculate(long long *ll, char *equation) { char *ss = equation; @@ -556,18 +611,6 @@ static int calculate(long long *ll, char *equation) return 1; } -// Return length of utf8 char @s fitting in len, writing value into *cc -int getutf8(char *s, int len, int *cc) -{ - unsigned wc; - - if (len<0) wc = len = 0; - else if (1>(len = utf8towc(&wc, s, len))) wc = *s, len = 1; - if (cc) *cc = wc; - - return len; -} - // get value of variable starting at s. static char *getvar(char *s) { @@ -824,24 +867,6 @@ static char *declarep(struct sh_vars *var) return ss; } -// return length of match found at this point (try is null terminated array) -static int anystart(char *s, char **try) -{ - char *ss = s; - - while (*try) if (strstart(&s, *try++)) return s-ss; - - return 0; -} - -// does this entire string match one of the strings in try[] -static int anystr(char *s, char **try) -{ - while (*try) if (!strcmp(s, *try++)) return 1; - - return 0; -} - // return length of valid prefix that could go before redirect static int redir_prefix(char *word) { @@ -982,6 +1007,21 @@ static int save_redirect(int **rd, int from, int to) return 0; } +// restore displaced filehandles, closing high filehandles they were copied to +static void unredirect(int *urd) +{ + int *rr = urd+1, i; + + if (!urd) return; + + for (i = 0; i<*urd; i++, rr += 2) if (rr[0] != -1) { + // No idea what to do about fd exhaustion here, so Steinbach's Guideline. + dup2(rr[0], rr[1]); + close(rr[0]); + } + free(urd); +} + // TODO: waitpid(WNOHANG) to clean up zombies and catch background& ending static void subshell_callback(char **argv) { @@ -1021,21 +1061,6 @@ static char *pl2str(struct sh_pipeline *pl, int one) // TODO handle functions } -// restore displaced filehandles, closing high filehandles they were copied to -static void unredirect(int *urd) -{ - int *rr = urd+1, i; - - if (!urd) return; - - for (i = 0; i<*urd; i++, rr += 2) if (rr[0] != -1) { - // No idea what to do about fd exhaustion here, so Steinbach's Guideline. - dup2(rr[0], rr[1]); - close(rr[0]); - } - free(urd); -} - static struct sh_blockstack *clear_block(struct sh_blockstack *blk) { memset(blk, 0, sizeof(*blk)); @@ -1089,9 +1114,6 @@ static void call_function(void) TT.ff->ifs = TT.ff->next->ifs; } -// functions contain pipelines contain functions: prototype because loop -static void free_pipeline(void *pipeline); - static void free_function(struct sh_function *funky) { if (--funky->refcount) return; @@ -1213,29 +1235,6 @@ static int pipe_subshell(char *s, int len, int out) return pipes[out]; } -// utf8 strchr: return wide char matched at wc from chrs, or 0 if not matched -// if len, save length of next wc (whether or not it's in list) -static int utf8chr(char *wc, char *chrs, int *len) -{ - unsigned wc1, wc2; - int ll; - - if (len) *len = 1; - if (!*wc) return 0; - if (0<(ll = utf8towc(&wc1, wc, 99))) { - if (len) *len = ll; - while (*chrs) { - if(1>(ll = utf8towc(&wc2, chrs, 99))) chrs++; - else { - if (wc1 == wc2) return wc1; - chrs += ll; - } - } - } - - return 0; -} - // grab variable or special param (ala $$) up to len bytes. Return value. // set *used to length consumed. Does not handle $* and $@ char *getvar_special(char *str, int len, int *used, struct arg_list **delete) -- 2.39.2