static int node_expand (struct wordsplit *wsp, struct wordsplit_node *node, int (*beg_p) (int), int (*ws_exp_fn) (struct wordsplit *wsp, const char *str, size_t len, struct wordsplit_node **ptail, const char **pend, int flg)) { const char *str = wsnode_ptr (wsp, node); size_t slen = wsnode_len (node); const char *end = str + slen; const char *p; size_t off = 0; struct wordsplit_node *tail = node; for (p = str; p < end; p++) { if (*p == '\\') { p++; continue; } if (*p == '$' && beg_p (p[1])) { size_t n = p - str; if (tail != node) tail->flags |= _WSNF_JOIN; if (node_split_prefix (wsp, &tail, node, off, n, _WSNF_JOIN)) return 1; p++; if (ws_exp_fn (wsp, p, slen - n, &tail, &p, node->flags & (_WSNF_JOIN | _WSNF_QUOTE))) return 1; off += p - str + 1; str = p + 1; } } if (p > str) { if (tail != node) tail->flags |= _WSNF_JOIN; if (node_split_prefix (wsp, &tail, node, off, p - str, node->flags & (_WSNF_JOIN|_WSNF_QUOTE))) return 1; } if (tail != node) { wsnode_remove (wsp, node); wsnode_free (node); } return 0; }
static void wordsplit_free_nodes (struct wordsplit *wsp) { struct wordsplit_node *p; for (p = wsp->ws_head; p;) { struct wordsplit_node *next = p->next; wsnode_free (p); p = next; } wsp->ws_head = wsp->ws_tail = NULL; }
static int coalesce_segment (struct wordsplit *wsp, struct wordsplit_node *node) { struct wordsplit_node *p, *end; size_t len = 0; char *buf, *cur; int stop; for (p = node; p && (p->flags & _WSNF_JOIN); p = p->next) { len += wsnode_len (p); } if (p) len += wsnode_len (p); end = p; buf = malloc (len + 1); if (!buf) return _wsplt_nomem (wsp); cur = buf; p = node; for (stop = 0; !stop;) { struct wordsplit_node *next = p->next; const char *str = wsnode_ptr (wsp, p); size_t slen = wsnode_len (p); memcpy (cur, str, slen); cur += slen; if (p != node) { node->flags |= p->flags & _WSNF_QUOTE; wsnode_remove (wsp, p); stop = p == end; wsnode_free (p); } p = next; } *cur = 0; node->flags &= ~_WSNF_JOIN; if (node->flags & _WSNF_WORD) free (node->v.word); else node->flags |= _WSNF_WORD; node->v.word = buf; return 0; }
static int node_expand_vars (struct wordsplit *wsp, struct wordsplit_node *node) { const char *str = wsnode_ptr (wsp, node); size_t slen = wsnode_len (node); const char *end = str + slen; const char *p; size_t off = 0; struct wordsplit_node *tail = node; for (p = str; p < end; p++) { if (*p == '\\') { p++; continue; } if (*p == '$') { size_t n = p - str; if (tail != node) tail->flags |= _WSNF_JOIN; if (node_split_prefix (wsp, &tail, node, off, n, _WSNF_JOIN)) return 1; p++; if (expvar (wsp, p, slen - n, &tail, &p, node->flags & (_WSNF_JOIN | _WSNF_QUOTE))) return 1; off += p - str + 1; str = p + 1; } } if (p > str) { if (tail != node) tail->flags |= _WSNF_JOIN; if (node_split_prefix (wsp, &tail, node, off, p - str, node->flags & _WSNF_JOIN)) return 1; } if (tail != node) { wsnode_remove (wsp, node); wsnode_free (node); } return 0; }
/* Remove NULL lists */ static void wsnode_nullelim (struct wordsplit *wsp) { struct wordsplit_node *p; for (p = wsp->ws_head; p;) { struct wordsplit_node *next = p->next; if (p->flags & _WSNF_NULL) { wsnode_remove (wsp, p); wsnode_free (p); } p = next; } }
static int wordsplit_pathexpand (struct wordsplit *wsp) { struct wordsplit_node *p, *next; char *pattern = NULL; size_t patsize = 0; size_t slen; int flags = 0; #ifdef GLOB_PERIOD if (wsp->ws_options & WRDSO_DOTGLOB) flags = GLOB_PERIOD; #endif for (p = wsp->ws_head; p; p = next) { const char *str; next = p->next; if (p->flags & _WSNF_QUOTE) continue; str = wsnode_ptr (wsp, p); slen = wsnode_len (p); if (isglob (str, slen)) { int i; glob_t g; struct wordsplit_node *prev; if (slen + 1 > patsize) { char *p = realloc (pattern, slen + 1); if (!p) return _wsplt_nomem (wsp); pattern = p; patsize = slen + 1; } 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) { wsnode_remove (wsp, p); wsnode_free (p); } else if (wsp->ws_options & WRDSO_FAILGLOB) { char buf[128]; if (wsp->ws_errno == WRDSE_USERERR) free (wsp->ws_usererr); snprintf (buf, sizeof (buf), _("no files match pattern %s"), pattern); free (pattern); wsp->ws_usererr = strdup (buf); if (!wsp->ws_usererr) return _wsplt_nomem (wsp); else return _wsplt_seterr (wsp, WRDSE_USERERR); } continue; default: free (pattern); return _wsplt_seterr (wsp, WRDSE_GLOBERR); } prev = p; for (i = 0; i < g.gl_pathc; i++) { struct wordsplit_node *newnode; char *newstr; if (wsnode_new (wsp, &newnode)) return 1; newstr = strdup (g.gl_pathv[i]); if (!newstr) return _wsplt_nomem (wsp); newnode->v.word = newstr; newnode->flags |= _WSNF_WORD|_WSNF_QUOTE; wsnode_insert (wsp, newnode, prev, 0); prev = newnode; } globfree (&g); wsnode_remove (wsp, p); wsnode_free (p); } } free (pattern); return 0; }