diff --git a/include/wordsplit.h b/include/wordsplit.h index d7eb26f..d4975b3 100644 --- a/include/wordsplit.h +++ b/include/wordsplit.h @@ -30,16 +30,16 @@ typedef struct wordsplit wordsplit_t; must be set (or unset, if starting with !) in ws_flags (if starting with WRDSF_) or ws_options (if starting with WRDSO_) to initialize or use the given member. - + If not redefined explicitly, most of them are set to some reasonable default value upon entry to wordsplit(). */ -struct wordsplit +struct wordsplit { size_t ws_wordc; /* [Output] Number of words in ws_wordv. */ char **ws_wordv; /* [Output] Array of parsed out words. */ size_t ws_offs; /* [Input] (WRDSF_DOOFFS) Number of initial elements in ws_wordv to fill with NULLs. */ - size_t ws_wordn; /* Number of elements ws_wordv can accomodate. */ + size_t ws_wordn; /* Number of elements ws_wordv can accomodate. */ int ws_flags; /* [Input] Flags passed to wordsplit. */ int ws_options; /* [Input] (WRDSF_OPTIONS) Additional options. */ @@ -53,15 +53,15 @@ struct wordsplit const char *ws_escape[2]; /* [Input] (WRDSF_ESCAPE) Characters to be escaped with backslash. */ void (*ws_alloc_die) (wordsplit_t *wsp); - /* [Input] (WRDSF_ALLOC_DIE) Function called when + /* [Input] (WRDSF_ALLOC_DIE) Function called when out of memory. Must not return. */ void (*ws_error) (const char *, ...) - __attribute__ ((__format__ (__printf__, 1, 2))); - /* [Input] (WRDSF_ERROR) Function used for error + __attribute__ ((__format__ (__printf__, 1, 2))); + /* [Input] (WRDSF_ERROR) Function used for error reporting */ void (*ws_debug) (const char *, ...) - __attribute__ ((__format__ (__printf__, 1, 2))); - /* [Input] (WRDSF_DEBUG) Function used for debug + __attribute__ ((__format__ (__printf__, 1, 2))); + /* [Input] (WRDSF_DEBUG) Function used for debug output. */ const char **ws_env; /* [Input] (WRDSF_ENV, !WRDSF_NOVAR) Array of environment variables. */ @@ -80,14 +80,14 @@ struct wordsplit parameters */ size_t ws_paramc; /* Number of positional parameters */ - /* Temporary storage for parameters. Works similarly to ws_enbuf. + /* Temporary storage for parameters. Works similarly to ws_enbuf. */ char **ws_parambuf; size_t ws_paramidx; size_t ws_paramsiz; - + int (*ws_getvar) (char **ret, const char *var, size_t len, void *clos); - /* [Input] (WRDSF_GETVAR, !WRDSF_NOVAR) Looks up + /* [Input] (WRDSF_GETVAR, !WRDSF_NOVAR) Looks up the name VAR (LEN bytes long) in the table of variables and if found returns in memory location pointed to by RET the value of that @@ -96,13 +96,13 @@ struct wordsplit on error. User-specific errors can be returned by storing the error diagnostic string in RET and returning WRDSE_USERERR. - Whatever is stored in RET, it must be allocated + Whatever is stored in RET, it must be allocated using malloc(3). */ void *ws_closure; /* [Input] (WRDSF_CLOSURE) Passed as the CLOS argument to ws_getvar and ws_command. */ int (*ws_command) (char **ret, const char *cmd, size_t len, char **argv, - void *clos); - /* [Input] (!WRDSF_NOCMD) Returns in the memory + void *clos); + /* [Input] (!WRDSF_NOCMD) Returns in the memory location pointed to by RET the expansion of the command CMD (LEN bytes long). On input, ARGV contains CMD split out to words. @@ -110,7 +110,7 @@ struct wordsplit See ws_getvar for a discussion of possible return values. */ - const char *ws_input; /* Input string (the S argument to wordsplit. */ + const char *ws_input; /* Input string (the S argument to wordsplit. */ size_t ws_len; /* Length of ws_input. */ size_t ws_endp; /* Points past the last processed byte in ws_input. */ @@ -119,7 +119,7 @@ struct wordsplit the error, if ws_errno is WRDSE_USERERR. Must be allocated with malloc(3). */ struct wordsplit_node *ws_head, *ws_tail; - /* Doubly-linked list of parsed out nodes. */ + /* Doubly-linked list of parsed out nodes. */ int ws_lvl; /* Invocation nesting level. */ }; @@ -242,9 +242,9 @@ struct wordsplit parameter) */ #define WRDSO_PARAM_NEGIDX 0x00008000 -#define WRDSO_BSKEEP WRDSO_BSKEEP_WORD -#define WRDSO_OESC WRDSO_OESC_WORD -#define WRDSO_XESC WRDSO_XESC_WORD +#define WRDSO_BSKEEP WRDSO_BSKEEP_WORD +#define WRDSO_OESC WRDSO_OESC_WORD +#define WRDSO_XESC WRDSO_XESC_WORD /* Indices into ws_escape */ #define WRDSX_WORD 0 diff --git a/src/wordsplit.c b/src/wordsplit.c index 05c3643..f563725 100644 --- a/src/wordsplit.c +++ b/src/wordsplit.c @@ -70,7 +70,7 @@ _wsplt_alloc_die (struct wordsplit *wsp) abort (); } -static void +static void _wsplt_error (const char *fmt, ...) { va_list ap; @@ -91,7 +91,7 @@ _wsplt_seterr (struct wordsplit *wsp, int ec) wordsplit_perror (wsp); return ec; } - + static int _wsplt_nomem (struct wordsplit *wsp) { @@ -122,7 +122,7 @@ _wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss, int flags, int finalize) { int rc; - + wss->ws_delim = wsp->ws_delim; wss->ws_debug = wsp->ws_debug; wss->ws_error = wsp->ws_error; @@ -146,12 +146,12 @@ _wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss, } wss->ws_options = wsp->ws_options; - + flags |= WRDSF_DELIM - | WRDSF_ALLOC_DIE - | WRDSF_ERROR - | WRDSF_DEBUG - | (wsp->ws_flags & (WRDSF_SHOWDBG | WRDSF_SHOWERR | WRDSF_OPTIONS)); + | WRDSF_ALLOC_DIE + | WRDSF_ERROR + | WRDSF_DEBUG + | (wsp->ws_flags & (WRDSF_SHOWDBG | WRDSF_SHOWERR | WRDSF_OPTIONS)); rc = wordsplit_init (wss, str, len, flags); if (rc) @@ -205,7 +205,7 @@ wordsplit_init0 (struct wordsplit *wsp) } char wordsplit_c_escape_tab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\tv\v"; - + static int wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, int flags) @@ -278,8 +278,8 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, { wsp->ws_escape[WRDSX_WORD] = wordsplit_c_escape_tab; wsp->ws_escape[WRDSX_QUOTE] = wordsplit_c_escape_tab; - wsp->ws_options |= WRDSO_OESC_QUOTE | WRDSO_OESC_WORD - | WRDSO_XESC_QUOTE | WRDSO_XESC_WORD; + wsp->ws_options |= WRDSO_OESC_QUOTE | WRDSO_OESC_WORD + | WRDSO_XESC_QUOTE | WRDSO_XESC_WORD; } else { @@ -296,16 +296,16 @@ wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, } wsp->ws_paramidx = wsp->ws_paramsiz = 0; wsp->ws_parambuf = NULL; - + wsp->ws_endp = 0; wsp->ws_wordi = 0; if (wsp->ws_flags & WRDSF_REUSE) wordsplit_free_nodes (wsp); wsp->ws_head = wsp->ws_tail = NULL; - + wordsplit_init0 (wsp); - + return 0; } @@ -591,7 +591,7 @@ coalesce_segment (struct wordsplit *wsp, struct wordsplit_node *node) if (!(node->flags & _WSNF_JOIN)) return 0; - + for (p = node; p && (p->flags & _WSNF_JOIN); p = p->next) { len += wsnode_len (p); @@ -721,11 +721,11 @@ wordsplit_finish (struct wordsplit *wsp) Nodes of type _WSNF_DELIM get inserted to the node list if either WRDSF_RETURN_DELIMS flag or WRDSO_MAXWORDS option is set. - + The following cases should be distinguished: 1. If both WRDSF_SQUEEZE_DELIMS and WRDSF_RETURN_DELIMS are set, compress - any runs of similar delimiter nodes to a single node. The nodes are + any runs of similar delimiter nodes to a single node. The nodes are 'similar' if they point to the same delimiter character. If WRDSO_MAXWORDS option is set, stop compressing when @@ -733,12 +733,12 @@ wordsplit_finish (struct wordsplit *wsp) a single last node. 2. If WRDSO_MAXWORDS option is set, but WRDSF_RETURN_DELIMS is not, - remove any delimiter nodes. Stop operation when + remove any delimiter nodes. Stop operation when ws_wordi + 1 == ws_maxwords, and coalesce the rest of nodes into a single last node. 3. If incremental operation is in progress, restart the loop any time - a delimiter node is about to be returned, unless WRDSF_RETURN_DELIMS + a delimiter node is about to be returned, unless WRDSF_RETURN_DELIMS is set. */ again: @@ -785,7 +785,7 @@ wordsplit_finish (struct wordsplit *wsp) continue; } } - else + else { if (delim) { @@ -858,8 +858,8 @@ wordsplit_finish (struct wordsplit *wsp) char *newstr = malloc (slen + 1); /* Assign newstr first, even if it is NULL. This way - wordsplit_free will work even if we return - nomem later. */ + wordsplit_free will work even if we return + nomem later. */ wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = newstr; if (!newstr) return _wsplt_nomem (wsp); @@ -972,7 +972,7 @@ find_closing_paren (const char *str, size_t i, size_t len, size_t *poff, break; } break; - + case '"': state = st_dquote; break; @@ -1052,7 +1052,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, { int n = (wsp->ws_flags & WRDSF_ENV_KV) ? 2 : 1; char *v; - + if (wsp->ws_envidx + n >= wsp->ws_envsiz) { size_t sz; @@ -1069,7 +1069,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, for (; wsp->ws_env[i]; i++) ; } - + sz = i + n + 1; newenv = calloc (sz, sizeof(newenv[0])); @@ -1088,7 +1088,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, } } newenv[j] = NULL; - + wsp->ws_envbuf = newenv; wsp->ws_envidx = i; wsp->ws_envsiz = sz; @@ -1109,7 +1109,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, else { size_t n = wsp->ws_envsiz; - + if ((size_t) -1 / 3 * 2 / sizeof (wsp->ws_envbuf[0]) <= n) return _wsplt_nomem (wsp); n += (n + 1) / 2; @@ -1121,7 +1121,7 @@ wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, wsp->ws_env = (const char**) wsp->ws_envbuf; } } - + if (wsp->ws_flags & WRDSF_ENV_KV) { /* A key-value pair environment */ @@ -1158,7 +1158,7 @@ int wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) { char *v; - + if (param_idx < 0) return WRDSE_BADPARAM; if (param_idx == wsp->ws_paramc) @@ -1167,7 +1167,7 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) if (!wsp->ws_parambuf) { size_t i; - + parambuf = calloc ((size_t)param_idx + 1, sizeof (parambuf[0])); if (!parambuf) return _wsplt_nomem (wsp); @@ -1183,7 +1183,7 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) return _wsplt_nomem (wsp); } } - + wsp->ws_parambuf = parambuf; wsp->ws_paramidx = param_idx; wsp->ws_paramsiz = param_idx + 1; @@ -1191,7 +1191,7 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) else { size_t n = wsp->ws_paramsiz; - + if ((size_t) -1 / 3 * 2 / sizeof (wsp->ws_parambuf[0]) <= n) return _wsplt_nomem (wsp); n += (n + 1) / 2; @@ -1208,11 +1208,11 @@ wsplt_assign_param (struct wordsplit *wsp, int param_idx, char *value) } else if (param_idx > wsp->ws_paramc) return WRDSE_BADPARAM; - + v = strdup (value); if (!v) return _wsplt_nomem (wsp); - + free (wsp->ws_parambuf[param_idx]); wsp->ws_parambuf[param_idx] = v; return WRDSE_OK; @@ -1247,10 +1247,10 @@ expand_paramv (struct wordsplit *wsp, struct wordsplit_node **ptail, int flg, { struct wordsplit ws; int wsflags = WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_QUOTE - | (WSP_RETURN_DELIMS (wsp) ? WRDSF_RETURN_DELIMS : 0) - | (q ? WRDSF_NOSPLIT : 0); + | (WSP_RETURN_DELIMS (wsp) ? WRDSF_RETURN_DELIMS : 0) + | (q ? WRDSF_NOSPLIT : 0); size_t i; - + for (i = 0; i < wsp->ws_paramc; i++) { struct wordsplit_node *np; @@ -1272,7 +1272,7 @@ expand_paramv (struct wordsplit *wsp, struct wordsplit_node **ptail, int flg, *ptail = np; np->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg; np->v.word = ws.ws_wordv[0]; - + ws.ws_wordv[0] = NULL; } else @@ -1283,7 +1283,7 @@ expand_paramv (struct wordsplit *wsp, struct wordsplit_node **ptail, int flg, *ptail = ws.ws_tail; ws.ws_head = ws.ws_tail = NULL; } - + wsflags |= WRDSF_REUSE; } if (wsflags & WRDSF_REUSE) @@ -1305,7 +1305,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, struct wordsplit ws; int is_param = 0; long param_idx = 0; - + if (ISVARBEG (str[0])) { for (i = 1; i < len; i++) @@ -1358,7 +1358,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, if (str[i] == ':') { size_t j; - + defstr = str + i + 1; if (find_closing_paren (str, i + 1, len, &j, "{}")) return _wsplt_seterr (wsp, WRDSE_CBRACE); @@ -1374,7 +1374,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, else if (strchr ("-+?=", str[i])) { size_t j; - + defstr = str + i; if (find_closing_paren (str, i, len, &j, "{}")) return _wsplt_seterr (wsp, WRDSE_CBRACE); @@ -1400,10 +1400,10 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, return expvar_recover (wsp, str - 1, ptail, pend, flg); } } - + if (is_param && str[0] == '-') param_idx = wsp->ws_paramc - param_idx; - + if (i == len) return _wsplt_seterr (wsp, WRDSE_CBRACE); } @@ -1456,7 +1456,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, else rc = WRDSE_UNDEF; } - + if (rc == WRDSE_OK && (!value || value[0] == 0) && defstr && defstr[-1] == ':') @@ -1465,7 +1465,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, rc = WRDSE_UNDEF; } } - + switch (rc) { case WRDSE_OK: @@ -1485,7 +1485,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, wordsplit_free (&ws); } break; - + case WRDSE_UNDEF: if (defstr) { @@ -1505,7 +1505,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, value = ws.ws_wordv[0]; ws.ws_wordv[0] = NULL; wordsplit_free (&ws); - + if (defstr[-1] == '=') { if (is_param) @@ -1519,7 +1519,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, return rc; } } - else + else { if (*defstr == '?') { @@ -1567,7 +1567,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, } } break; - + case WRDSE_NOSPACE: return _wsplt_nomem (wsp); @@ -1606,7 +1606,7 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, { struct wordsplit ws; int rc; - + rc = _wsplt_subsplit (wsp, &ws, value, strlen (value), WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_QUOTE @@ -1711,7 +1711,7 @@ node_expand (struct wordsplit *wsp, struct wordsplit_node *node, } return 0; } - + /* Remove NULL nodes from the list */ static void wsnode_nullelim (struct wordsplit *wsp) @@ -1765,7 +1765,7 @@ expcmd (struct wordsplit *wsp, const char *str, size_t len, char *value; struct wordsplit_node *newnode; struct wordsplit ws; - + str++; len--; @@ -1785,7 +1785,7 @@ expcmd (struct wordsplit *wsp, const char *str, size_t len, } rc = wsp->ws_command (&value, str, j, ws.ws_wordv, wsp->ws_closure); wordsplit_free (&ws); - + if (rc == WRDSE_NOSPACE) return _wsplt_nomem (wsp); else if (rc) @@ -1893,13 +1893,13 @@ wordsplit_trimws (struct wordsplit *wsp) ; p->v.segm.beg = n; } - + while (p->next && (p->flags & _WSNF_JOIN)) p = p->next; - + if (p->flags & _WSNF_QUOTE) continue; - + /* Trim trailing whitespace */ for (n = p->v.segm.end; n > p->v.segm.beg && ISWS (wsp->ws_input[n - 1]); n--); @@ -1918,7 +1918,7 @@ wordsplit_tildexpand (struct wordsplit *wsp) struct wordsplit_node *p; char *uname = NULL; size_t usize = 0; - + for (p = wsp->ws_head; p; p = p->next) { const char *str; @@ -1933,7 +1933,7 @@ wordsplit_tildexpand (struct wordsplit *wsp) size_t slen = wsnode_len (p); struct passwd *pw; char *newstr; - + for (i = 1; i < slen && str[i] != '/'; i++) ; if (i == slen) @@ -2009,7 +2009,7 @@ wordsplit_pathexpand (struct wordsplit *wsp) if (wsp->ws_options & WRDSO_DOTGLOB) flags = GLOB_PERIOD; #endif - + for (p = wsp->ws_head; p; p = next) { const char *str; @@ -2027,7 +2027,7 @@ wordsplit_pathexpand (struct wordsplit *wsp) int i; glob_t g; struct wordsplit_node *prev; - + if (slen + 1 > patsize) { char *p = realloc (pattern, slen + 1); @@ -2038,16 +2038,16 @@ wordsplit_pathexpand (struct wordsplit *wsp) } memcpy (pattern, str, slen); pattern[slen] = 0; - + switch (glob (pattern, flags, NULL, &g)) { case 0: break; - + case GLOB_NOSPACE: free (pattern); return _wsplt_nomem (wsp); - + case GLOB_NOMATCH: if (wsp->ws_options & WRDSO_NULLGLOB) { @@ -2069,7 +2069,7 @@ wordsplit_pathexpand (struct wordsplit *wsp) return _wsplt_seterr (wsp, WRDSE_USERERR); } continue; - + default: free (pattern); return _wsplt_seterr (wsp, WRDSE_GLOBERR); @@ -2080,7 +2080,7 @@ wordsplit_pathexpand (struct wordsplit *wsp) { struct wordsplit_node *newnode; char *newstr; - + if (wsnode_new (wsp, &newnode)) return 1; newstr = strdup (g.gl_pathv[i]); @@ -2196,7 +2196,7 @@ scan_word (struct wordsplit *wsp, size_t start, int consume_all) int join = 0; int flags = 0; struct wordsplit_node *np = wsp->ws_tail; - + size_t i = start; if (i >= len) @@ -2287,7 +2287,7 @@ scan_word (struct wordsplit *wsp, size_t start, int consume_all) wsp->ws_endp = i; if (wsp->ws_flags & WRDSF_INCREMENTAL) return _WRDS_EOF; - + if (consume_all) { if (!np) @@ -2298,7 +2298,7 @@ scan_word (struct wordsplit *wsp, size_t start, int consume_all) np = np->next; } } - + return _WRDS_OK; } @@ -2562,7 +2562,7 @@ wordsplit_process_list (struct wordsplit *wsp, size_t start) if (wsp->ws_flags & WRDSF_SHOWDBG) wsp->ws_debug (_("(%02d) Input:%.*s;"), wsp->ws_lvl, (int) wsp->ws_len, wsp->ws_input); - + if ((wsp->ws_flags & WRDSF_NOSPLIT) || ((wsp->ws_options & WRDSO_MAXWORDS) && wsp->ws_wordi + 1 == wsp->ws_maxwords)) @@ -2623,7 +2623,7 @@ wordsplit_process_list (struct wordsplit *wsp, size_t start) static int wordsplit_run (const char *command, size_t length, struct wordsplit *wsp, - int flags, int lvl) + int flags, int lvl) { int rc; size_t start; @@ -2659,8 +2659,8 @@ wordsplit_run (const char *command, size_t length, struct wordsplit *wsp, } int -wordsplit_len (const char *command, size_t length, struct wordsplit *wsp, - int flags) +wordsplit_len (const char *command, size_t length, struct wordsplit *wsp, + int flags) { return wordsplit_run (command, length, wsp, flags, 0); } @@ -2800,4 +2800,3 @@ wordsplit_perror (struct wordsplit *wsp) wsp->ws_error ("%s", wordsplit_strerror (wsp)); } } -