diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2019-07-10 10:09:45 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2019-07-10 10:09:45 +0300 |
commit | d73567ebdf054eb6de6dea72250ef164e4c50eec (patch) | |
tree | c8d48504b02786e13215923b3e2654fe42275f0d /src | |
parent | ee35adccec058a5a8cc62f5030b9a925168236d6 (diff) | |
download | grecs-d73567ebdf054eb6de6dea72250ef164e4c50eec.tar.gz grecs-d73567ebdf054eb6de6dea72250ef164e4c50eec.tar.bz2 |
Import wordsplit as a submodule
* .gitmodules: New module: wordsplit
* Makefile.am: Distribute wordsplit files.
* am/grecs.m4 (GRECS_INCLUDES): Add wordsplit directory
* doc/Makefile.am (dist_man_MANS): Distribute wordsplit/wordsplit.3
* doc/wordsplit.3: Remove.
* include/Makefile.am (GRECS_HDR): Add wordsplit/wordsplit.
* include/wordsplit.h: Remove.
* src/Make-inst.am: Define nodist_libgrecs_la_SOURCES.
* src/Make-shared.am: Likewise.
* src/Make-static.am: Define nodist_libgrecs_a_SOURCES.
* src/Make.am (GRECS_SRC): Remove wordsplit.c
(NODIST_GRECS_SRC): Define to wordsplit.c
(VPATH): Add the wordsplit directory.
* src/wordsplit.c: Remove.
* tests/wordsplit.at: Remove.
* tests/wsp.c: Remove.
* tests/Makefile.am: Build separate testsuite for wordsplit. Use VPATH
to build wsp.
* tests/.gitignore: Update.
Diffstat (limited to 'src')
-rw-r--r-- | src/Make-inst.am | 4 | ||||
-rw-r--r-- | src/Make-shared.am | 3 | ||||
-rw-r--r-- | src/Make-static.am | 3 | ||||
-rw-r--r-- | src/Make.am | 6 | ||||
-rw-r--r-- | src/wordsplit.c | 2892 |
5 files changed, 11 insertions, 2897 deletions
diff --git a/src/Make-inst.am b/src/Make-inst.am index a4b6aea..1eb04bb 100644 --- a/src/Make-inst.am +++ b/src/Make-inst.am @@ -1,4 +1,4 @@ # This file is part of grecs - Gray's Extensible Configuration System -# Copyright (C) 2007-2016 Sergey Poznyakoff +# Copyright (C) 2007-2019 Sergey Poznyakoff # # Grecs is free software; you can redistribute it and/or modify @@ -20,2 +20,4 @@ libgrecs_la_SOURCES = $(GRECS_SRC) m4datadir = $(datadir)/aclocal dist_m4data_DATA = libgrecs.m4 +nodist_libgrecs_la_SOURCES = $(NODIST_GRECS_SRC) + diff --git a/src/Make-shared.am b/src/Make-shared.am index 9a762a4..f080e46 100644 --- a/src/Make-shared.am +++ b/src/Make-shared.am @@ -1,4 +1,4 @@ # This file is part of grecs - Gray's Extensible Configuration System -# Copyright (C) 2007-2016 Sergey Poznyakoff +# Copyright (C) 2007-2019 Sergey Poznyakoff # # Grecs is free software; you can redistribute it and/or modify @@ -17,3 +17,4 @@ include Make.am noinst_LTLIBRARIES=libgrecs.la libgrecs_la_SOURCES = $(GRECS_SRC) +nodist_libgrecs_la_SOURCES = $(NODIST_GRECS_SRC) diff --git a/src/Make-static.am b/src/Make-static.am index 2688b40..1f4cd3e 100644 --- a/src/Make-static.am +++ b/src/Make-static.am @@ -1,4 +1,4 @@ # This file is part of grecs - Gray's Extensible Configuration System -# Copyright (C) 2007-2016 Sergey Poznyakoff +# Copyright (C) 2007-2019 Sergey Poznyakoff # # Grecs is free software; you can redistribute it and/or modify @@ -17,4 +17,5 @@ include Make.am noinst_LIBRARIES=libgrecs.a libgrecs_a_SOURCES = $(GRECS_SRC) +nodist_libgrecs_a_SOURCES = $(NODIST_GRECS_SRC) diff --git a/src/Make.am b/src/Make.am index 9fa7688..5199991 100644 --- a/src/Make.am +++ b/src/Make.am @@ -1,4 +1,4 @@ # This file is part of grecs - Gray's Extensible Configuration System -# Copyright (C) 2007-2016 Sergey Poznyakoff +# Copyright (C) 2007-2019 Sergey Poznyakoff # # Grecs is free software; you can redistribute it and/or modify @@ -70,5 +70,4 @@ GRECS_SRC = \ txtacc.c\ version.c\ - wordsplit.c\ $(GRECS_JSON)\ $(GRECS_PARSER_BIND)\ @@ -77,4 +76,7 @@ GRECS_SRC = \ $(GRECS_PARSER_META1) +VPATH += $(top_srcdir)/@GRECS_SUBDIR@/wordsplit +NODIST_GRECS_SRC = wordsplit.c + if GRECS_COND_SOCKADDR_LIST GRECS_SRC += sockaddr.c diff --git a/src/wordsplit.c b/src/wordsplit.c deleted file mode 100644 index 9179a87..0000000 --- a/src/wordsplit.c +++ /dev/null @@ -1,2892 +0,0 @@ -/* wordsplit - a word splitter - Copyright (C) 2009-2019 Sergey Poznyakoff - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 3 of the License, or (at your - option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program. If not, see <http://www.gnu.org/licenses/>. */ - -#ifdef HAVE_CONFIG_H -# include <config.h> -#endif - -#include <errno.h> -#include <ctype.h> -#include <unistd.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <stdarg.h> -#include <pwd.h> -#include <glob.h> -#include <limits.h> - -#if ENABLE_NLS -# include <gettext.h> -#else -# define gettext(msgid) msgid -#endif -#define _(msgid) gettext (msgid) -#define N_(msgid) msgid - -#include <wordsplit.h> - -#define ISWS(c) ((c)==' '||(c)=='\t'||(c)=='\n') -#define ISDELIM(ws,c) \ - (strchr ((ws)->ws_delim, (c)) != NULL) -#define ISPUNCT(c) (strchr("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",(c))!=NULL) -#define ISUPPER(c) ('A' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'Z') -#define ISLOWER(c) ('a' <= ((unsigned) (c)) && ((unsigned) (c)) <= 'z') -#define ISALPHA(c) (ISUPPER(c) || ISLOWER(c)) -#define ISDIGIT(c) ('0' <= ((unsigned) (c)) && ((unsigned) (c)) <= '9') -#define ISXDIGIT(c) (strchr("abcdefABCDEF", c)!=NULL) -#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c)) -#define ISPRINT(c) (' ' <= ((unsigned) (c)) && ((unsigned) (c)) <= 127) - -#define ISVARBEG(c) (ISALPHA(c) || c == '_') -#define ISVARCHR(c) (ISALNUM(c) || c == '_') - -#define WSP_RETURN_DELIMS(wsp) \ - ((wsp)->ws_flags & WRDSF_RETURN_DELIMS || ((wsp)->ws_options & WRDSO_MAXWORDS)) - -#define to_num(c) \ - (ISDIGIT(c) ? c - '0' : (ISXDIGIT(c) ? toupper(c) - 'A' + 10 : 255 )) - -#define ALLOC_INIT 128 -#define ALLOC_INCR 128 - -static void -_wsplt_alloc_die (struct wordsplit *wsp) -{ - wsp->ws_error ("%s", _("memory exhausted")); - abort (); -} - -static void -_wsplt_error (const char *fmt, ...) -{ - va_list ap; - - va_start (ap, fmt); - vfprintf (stderr, fmt, ap); - va_end (ap); - fputc ('\n', stderr); -} - -static void wordsplit_free_nodes (struct wordsplit *); - -static int -_wsplt_seterr (struct wordsplit *wsp, int ec) -{ - wsp->ws_errno = ec; - if (wsp->ws_flags & WRDSF_SHOWERR) - wordsplit_perror (wsp); - return ec; -} - -static int -_wsplt_nomem (struct wordsplit *wsp) -{ - errno = ENOMEM; - wsp->ws_errno = WRDSE_NOSPACE; - if (wsp->ws_flags & WRDSF_ENOMEMABRT) - wsp->ws_alloc_die (wsp); - if (wsp->ws_flags & WRDSF_SHOWERR) - wordsplit_perror (wsp); - if (!(wsp->ws_flags & WRDSF_REUSE)) - wordsplit_free (wsp); - wordsplit_free_nodes (wsp); - return wsp->ws_errno; -} - -static void -_wsplt_store_errctx (struct wordsplit *wsp, char const *str, size_t len) -{ - free (wsp->ws_errctx); - wsp->ws_errctx = malloc (len + 1); - if (!wsp->ws_errctx) - { - wsp->ws_error ("%s", - _("memory exhausted while trying to store error context")); - } - else - { - memcpy (wsp->ws_errctx, str, len); - wsp->ws_errctx[len] = 0; - } -} - -static inline int -_wsplt_setctxerr (struct wordsplit *wsp, int ec, char const *str, size_t len) -{ - _wsplt_store_errctx (wsp, str, len); - return _wsplt_seterr (wsp, ec); -} - -static int wordsplit_run (const char *command, size_t length, - struct wordsplit *wsp, - int flags, int lvl); - -static int wordsplit_init (struct wordsplit *wsp, const char *input, size_t len, - int flags); -static int wordsplit_process_list (struct wordsplit *wsp, size_t start); -static int wordsplit_finish (struct wordsplit *wsp); - -static int -_wsplt_subsplit (struct wordsplit *wsp, struct wordsplit *wss, - char const *str, int len, - int flags, int finalize) -{ - int rc; - - wss->ws_delim = wsp->ws_delim; - wss->ws_debug = wsp->ws_debug; - wss->ws_error = wsp->ws_error; - wss->ws_alloc_die = wsp->ws_alloc_die; - - if (!(flags & WRDSF_NOVAR)) - { - wss->ws_env = wsp->ws_env; - wss->ws_getvar = wsp->ws_getvar; - flags |= wsp->ws_flags & (WRDSF_ENV | WRDSF_ENV_KV | WRDSF_GETVAR); - } - if (!(flags & WRDSF_NOCMD)) - { - wss->ws_command = wsp->ws_command; - } - - if ((flags & (WRDSF_NOVAR|WRDSF_NOCMD)) != (WRDSF_NOVAR|WRDSF_NOCMD)) - { - wss->ws_closure = wsp->ws_closure; - flags |= wsp->ws_flags & WRDSF_CLOSURE; - } - - 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)); - - rc = wordsplit_init (wss, str, len, flags); - if (rc) - return rc; - wss->ws_lvl = wsp->ws_lvl + 1; - rc = wordsplit_process_list (wss, 0); - if (rc) - { - wordsplit_free_nodes (wss); - return rc; - } - if (finalize) - { - rc = wordsplit_finish (wss); - wordsplit_free_nodes (wss); - } - return rc; -} - -static void -_wsplt_seterr_sub (struct wordsplit *wsp, struct wordsplit *wss) -{ - /* Clear user-defined error */ - if (wsp->ws_errno == WRDSE_USERERR) - free (wsp->ws_usererr); - /* Copy error state */ - wsp->ws_errno = wss->ws_errno; - if (wss->ws_errno == WRDSE_USERERR) - { - wsp->ws_usererr = wss->ws_usererr; - wss->ws_errno = WRDSE_EOF; - wss->ws_usererr = NULL; - } - /* Copy error context */ - free (wsp->ws_errctx); - wsp->ws_errctx = wss->ws_errctx; - wss->ws_errctx = NULL; -} - -static void -wordsplit_init0 (struct wordsplit *wsp) -{ - if (wsp->ws_flags & WRDSF_REUSE) - { - if (!(wsp->ws_flags & WRDSF_APPEND)) - wordsplit_free_words (wsp); - wordsplit_clearerr (wsp); - } - else - { - wsp->ws_wordv = NULL; - wsp->ws_wordc = 0; - wsp->ws_wordn = 0; - } - - wsp->ws_errno = 0; -} - -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) -{ - wsp->ws_flags = flags; - - if (!(wsp->ws_flags & WRDSF_ALLOC_DIE)) - wsp->ws_alloc_die = _wsplt_alloc_die; - if (!(wsp->ws_flags & WRDSF_ERROR)) - wsp->ws_error = _wsplt_error; - - if (!(wsp->ws_flags & WRDSF_NOVAR)) - { - /* These will be initialized on first variable assignment */ - wsp->ws_envidx = wsp->ws_envsiz = 0; - wsp->ws_envbuf = NULL; - } - - if (!(wsp->ws_flags & WRDSF_NOCMD)) - { - if (!wsp->ws_command) - { - _wsplt_seterr (wsp, WRDSE_USAGE); - errno = EINVAL; - return wsp->ws_errno; - } - } - - if (wsp->ws_flags & WRDSF_SHOWDBG) - { - if (!(wsp->ws_flags & WRDSF_DEBUG)) - { - if (wsp->ws_flags & WRDSF_ERROR) - wsp->ws_debug = wsp->ws_error; - else if (wsp->ws_flags & WRDSF_SHOWERR) - wsp->ws_debug = _wsplt_error; - else - wsp->ws_flags &= ~WRDSF_SHOWDBG; - } - } - - wsp->ws_input = input; - wsp->ws_len = len; - - if (!(wsp->ws_flags & WRDSF_DOOFFS)) - wsp->ws_offs = 0; - - if (!(wsp->ws_flags & WRDSF_DELIM)) - wsp->ws_delim = " \t\n"; - - wsp->ws_sep[0] = wsp->ws_delim[0]; - wsp->ws_sep[1] = 0; - - if (!(wsp->ws_flags & WRDSF_COMMENT)) - wsp->ws_comment = NULL; - - if (!(wsp->ws_flags & WRDSF_CLOSURE)) - wsp->ws_closure = NULL; - - if (!(wsp->ws_flags & WRDSF_OPTIONS)) - wsp->ws_options = 0; - - if (wsp->ws_flags & WRDSF_ESCAPE) - { - if (!wsp->ws_escape[WRDSX_WORD]) - wsp->ws_escape[WRDSX_WORD] = ""; - if (!wsp->ws_escape[WRDSX_QUOTE]) - wsp->ws_escape[WRDSX_QUOTE] = ""; - } - else - { - if (wsp->ws_flags & WRDSF_CESCAPES) - { - 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; - } - else - { - wsp->ws_escape[WRDSX_WORD] = ""; - wsp->ws_escape[WRDSX_QUOTE] = "\\\\\"\""; - wsp->ws_options |= WRDSO_BSKEEP_QUOTE; - } - } - - if (!(wsp->ws_options & WRDSO_PARAMV)) - { - wsp->ws_paramv = NULL; - wsp->ws_paramc = 0; - } - 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; - - wsp->ws_errctx = NULL; - - wordsplit_init0 (wsp); - - return 0; -} - -static int -alloc_space (struct wordsplit *wsp, size_t count) -{ - size_t offs = (wsp->ws_flags & WRDSF_DOOFFS) ? wsp->ws_offs : 0; - char **ptr; - size_t newalloc; - - if (wsp->ws_wordv == NULL) - { - newalloc = offs + count > ALLOC_INIT ? count : ALLOC_INIT; - ptr = calloc (newalloc, sizeof (ptr[0])); - } - else if (wsp->ws_wordn < offs + wsp->ws_wordc + count) - { - newalloc = offs + wsp->ws_wordc + - (count > ALLOC_INCR ? count : ALLOC_INCR); - ptr = realloc (wsp->ws_wordv, newalloc * sizeof (ptr[0])); - } - else - return 0; - - if (ptr) - { - wsp->ws_wordn = newalloc; - wsp->ws_wordv = ptr; - } - else - return _wsplt_nomem (wsp); - return 0; -} - - -/* Node state flags */ -#define _WSNF_NULL 0x01 /* null node (a noop) */ -#define _WSNF_WORD 0x02 /* node contains word in v.word */ -#define _WSNF_QUOTE 0x04 /* text is quoted */ -#define _WSNF_NOEXPAND 0x08 /* text is not subject to expansion */ -#define _WSNF_JOIN 0x10 /* node must be joined with the next node */ -#define _WSNF_SEXP 0x20 /* is a sed expression */ -#define _WSNF_DELIM 0x40 /* node is a delimiter */ -#define _WSNF_CONST 0x80 /* with _WSNF_WORD: v.word is constant */ -#define _WSNF_EMPTYOK 0x0100 /* special flag indicating that - wordsplit_add_segm must add the - segment even if it is empty */ - -struct wordsplit_node -{ - struct wordsplit_node *prev; /* Previous element */ - struct wordsplit_node *next; /* Next element */ - int flags; /* Node flags */ - union - { - struct - { - size_t beg; /* Start of word in ws_input */ - size_t end; /* End of word in ws_input */ - } segm; - char *word; - } v; -}; - -static const char * -wsnode_flagstr (int flags) -{ - static char retbuf[7]; - char *p = retbuf; - - if (flags & _WSNF_WORD) - *p++ = 'w'; - else if (flags & _WSNF_NULL) - *p++ = 'n'; - else - *p++ = '-'; - if (flags & _WSNF_QUOTE) - *p++ = 'q'; - else - *p++ = '-'; - if (flags & _WSNF_NOEXPAND) - *p++ = 'E'; - else - *p++ = '-'; - if (flags & _WSNF_JOIN) - *p++ = 'j'; - else - *p++ = '-'; - if (flags & _WSNF_SEXP) - *p++ = 's'; - else - *p++ = '-'; - if (flags & _WSNF_DELIM) - *p++ = 'd'; - else - *p++ = '-'; - *p = 0; - return retbuf; -} - -static const char * -wsnode_ptr (struct wordsplit *wsp, struct wordsplit_node *p) -{ - if (p->flags & _WSNF_NULL) - return ""; - else if (p->flags & _WSNF_WORD) - return p->v.word; - else - return wsp->ws_input + p->v.segm.beg; -} - -static size_t -wsnode_len (struct wordsplit_node *p) -{ - if (p->flags & _WSNF_NULL) - return 0; - else if (p->flags & _WSNF_WORD) - return strlen (p->v.word); - else - return p->v.segm.end - p->v.segm.beg; -} - -static int -wsnode_new (struct wordsplit *wsp, struct wordsplit_node **pnode) -{ - struct wordsplit_node *node = calloc (1, sizeof (*node)); - if (!node) - return _wsplt_nomem (wsp); - *pnode = node; - return 0; -} - -static void -wsnode_free (struct wordsplit_node *p) -{ - if ((p->flags & (_WSNF_WORD|_WSNF_CONST)) == _WSNF_WORD) - free (p->v.word); - free (p); -} - -static void -wsnode_append (struct wordsplit *wsp, struct wordsplit_node *node) -{ - node->next = NULL; - node->prev = wsp->ws_tail; - if (wsp->ws_tail) - wsp->ws_tail->next = node; - else - wsp->ws_head = node; - wsp->ws_tail = node; -} - -static void -wsnode_remove (struct wordsplit *wsp, struct wordsplit_node *node) -{ - struct wordsplit_node *p; - - p = node->prev; - if (p) - { - p->next = node->next; - if (!node->next) - p->flags &= ~_WSNF_JOIN; - } - else - wsp->ws_head = node->next; - - p = node->next; - if (p) - p->prev = node->prev; - else - wsp->ws_tail = node->prev; - - node->next = node->prev = NULL; -} - -static struct wordsplit_node * -wsnode_tail (struct wordsplit_node *p) -{ - while (p && p->next) - p = p->next; - return p; -} - -static void -wsnode_insert (struct wordsplit *wsp, struct wordsplit_node *node, - struct wordsplit_node *anchor, int before) -{ - if (!wsp->ws_head) - { - node->next = node->prev = NULL; - wsp->ws_head = wsp->ws_tail = node; - } - else if (before) - { - if (anchor->prev) - wsnode_insert (wsp, node, anchor->prev, 0); - else - { - struct wordsplit_node *tail = wsnode_tail (node); - node->prev = NULL; - tail->next = anchor; - anchor->prev = tail; - wsp->ws_head = node; - } - } - else - { - struct wordsplit_node *p; - struct wordsplit_node *tail = wsnode_tail (node); - - p = anchor->next; - if (p) - p->prev = tail; - else - wsp->ws_tail = tail; - tail->next = p; - node->prev = anchor; - anchor->next = node; - } -} - -static int -wordsplit_add_segm (struct wordsplit *wsp, size_t beg, size_t end, int flg) -{ - struct wordsplit_node *node; - int rc; - - if (end == beg && !(flg & _WSNF_EMPTYOK)) - return 0; - rc = wsnode_new (wsp, &node); - if (rc) - return rc; - node->flags = flg & ~(_WSNF_WORD | _WSNF_EMPTYOK); - node->v.segm.beg = beg; - node->v.segm.end = end; - wsnode_append (wsp, 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 void -wordsplit_dump_nodes (struct wordsplit *wsp) -{ - struct wordsplit_node *p; - int n = 0; - - for (p = wsp->ws_head, n = 0; p; p = p->next, n++) - { - if (p->flags & _WSNF_WORD) - wsp->ws_debug ("(%02d) %4d: %p: %#04x (%s):%s;", - wsp->ws_lvl, - n, p, p->flags, wsnode_flagstr (p->flags), p->v.word); - else - wsp->ws_debug ("(%02d) %4d: %p: %#04x (%s):%.*s;", - wsp->ws_lvl, - n, p, p->flags, wsnode_flagstr (p->flags), - (int) (p->v.segm.end - p->v.segm.beg), - wsp->ws_input + p->v.segm.beg); - } -} - -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; - - if (!(node->flags & _WSNF_JOIN)) - return 0; - - 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 void wordsplit_string_unquote_copy (struct wordsplit *ws, int inquote, - char *dst, const char *src, - size_t n); - -static int -wsnode_quoteremoval (struct wordsplit *wsp) -{ - struct wordsplit_node *p; - - for (p = wsp->ws_head; p; p = p->next) - { - const char *str = wsnode_ptr (wsp, p); - size_t slen = wsnode_len (p); - int unquote; - - if (wsp->ws_flags & WRDSF_QUOTE) - unquote = !(p->flags & _WSNF_NOEXPAND); - else - unquote = 0; - - if (unquote) - { - if (!(p->flags & _WSNF_WORD)) - { - char *newstr = malloc (slen + 1); - if (!newstr) - return _wsplt_nomem (wsp); - memcpy (newstr, str, slen); - newstr[slen] = 0; - p->v.word = newstr; - p->flags |= _WSNF_WORD; - } - - wordsplit_string_unquote_copy (wsp, p->flags & _WSNF_QUOTE, - p->v.word, str, slen); - } - } - return 0; -} - -static int -wsnode_coalesce (struct wordsplit *wsp) -{ - struct wordsplit_node *p; - - for (p = wsp->ws_head; p; p = p->next) - { - if (p->flags & _WSNF_JOIN) - if (coalesce_segment (wsp, p)) - return 1; - } - return 0; -} - -static int -wsnode_tail_coalesce (struct wordsplit *wsp, struct wordsplit_node *p) -{ - if (p->next) - { - struct wordsplit_node *np = p; - while (np && np->next) - { - np->flags |= _WSNF_JOIN; - np = np->next; - } - if (coalesce_segment (wsp, p)) - return 1; - } - return 0; -} - -static size_t skip_delim (struct wordsplit *wsp); - -static int -wordsplit_finish (struct wordsplit *wsp) -{ - struct wordsplit_node *p; - size_t n; - int delim; - - /* Postprocess delimiters. It would be rather simple, if it weren't for - the incremental operation. - - 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 - 'similar' if they point to the same delimiter character. - - If WRDSO_MAXWORDS option is set, stop compressing when - ws_wordi + 1 == ws_maxwords, and coalesce the rest of nodes into - a single last node. - - 2. If WRDSO_MAXWORDS option is set, but WRDSF_RETURN_DELIMS is not, - 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 - is set. - */ - again: - delim = 0; /* Delimiter being processed (if any) */ - n = 0; /* Number of words processed so far */ - p = wsp->ws_head; /* Current node */ - - while (p) - { - struct wordsplit_node *next = p->next; - if (p->flags & _WSNF_DELIM) - { - if (wsp->ws_flags & WRDSF_RETURN_DELIMS) - { - if (wsp->ws_flags & WRDSF_SQUEEZE_DELIMS) - { - char const *s = wsnode_ptr (wsp, p); - if (delim) - { - if (delim == *s) - { - wsnode_remove (wsp, p); - p = next; - continue; - } - else - { - delim = 0; - n++; /* Count this node; it will be returned */ - } - } - else - { - delim = *s; - p = next; - continue; - } - } - } - else if (wsp->ws_options & WRDSO_MAXWORDS) - { - wsnode_remove (wsp, p); - p = next; - continue; - } - } - else - { - if (delim) - { - /* Last node was a delimiter or a compressed run of delimiters; - Count it, and clear the delimiter marker */ - n++; - delim = 0; - } - if (wsp->ws_options & WRDSO_MAXWORDS) - { - if (wsp->ws_wordi + n + 1 == wsp->ws_maxwords) - break; - } - } - n++; - if (wsp->ws_flags & WRDSF_INCREMENTAL) - p = NULL; /* Break the loop */ - else - p = next; - } - - if (p) - { - /* We're here if WRDSO_MAXWORDS is in effect and wsp->ws_maxwords - words have already been collected. Reconstruct a single final - node from the remaining nodes. */ - if (wsnode_tail_coalesce (wsp, p)) - return wsp->ws_errno; - n++; - } - - if (n == 0) - { - /* The loop above have eliminated all nodes. */ - if (wsp->ws_flags & WRDSF_INCREMENTAL) - { - /* Restart the processing, if there's any input left. */ - if (wsp->ws_endp < wsp->ws_len) - { - int rc; - if (wsp->ws_flags & WRDSF_SHOWDBG) - wsp->ws_debug (_("Restarting")); - rc = wordsplit_process_list (wsp, skip_delim (wsp)); - if (rc) - return rc; - } - else - { - wsp->ws_errno = WRDSE_EOF; - return WRDSE_EOF; - } - goto again; - } - - if (wsp->ws_flags & WRDSF_NOSPLIT) - { - if (wordsplit_add_segm (wsp, 0, 0, _WSNF_EMPTYOK)) - return wsp->ws_errno; - n = 1; - } - } - - if (alloc_space (wsp, n + 1)) - return wsp->ws_errno; - - while (wsp->ws_head) - { - const char *str = wsnode_ptr (wsp, wsp->ws_head); - size_t slen = wsnode_len (wsp->ws_head); - 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. */ - wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = newstr; - if (!newstr) - return _wsplt_nomem (wsp); - memcpy (newstr, str, slen); - newstr[slen] = 0; - - wsnode_remove (wsp, wsp->ws_head); - - wsp->ws_wordc++; - wsp->ws_wordi++; - - if (wsp->ws_flags & WRDSF_INCREMENTAL) - break; - } - wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL; - return 0; -} - -int -wordsplit_append (wordsplit_t *wsp, int argc, char **argv) -{ - int rc; - size_t i; - - rc = alloc_space (wsp, wsp->ws_wordc + argc + 1); - if (rc) - return rc; - for (i = 0; i < argc; i++) - { - char *newstr = strdup (argv[i]); - if (!newstr) - { - while (i > 0) - { - free (wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i - 1]); - wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i - 1] = NULL; - i--; - } - return _wsplt_nomem (wsp); - } - wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i] = newstr; - } - wsp->ws_wordc += i; - wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL; - return 0; -} - -/* Variable expansion */ -static int -node_split_prefix (struct wordsplit *wsp, - struct wordsplit_node **ptail, - struct wordsplit_node *node, - size_t beg, size_t len, int flg) -{ - struct wordsplit_node *newnode; - - if (len == 0) - return 0; - if (wsnode_new (wsp, &newnode)) - return 1; - wsnode_insert (wsp, newnode, *ptail, 0); - if (node->flags & _WSNF_WORD) - { - const char *str = wsnode_ptr (wsp, node); - char *newstr = malloc (len + 1); - if (!newstr) - return _wsplt_nomem (wsp); - memcpy (newstr, str + beg, len); - newstr[len] = 0; - newnode->flags = _WSNF_WORD; - newnode->v.word = newstr; - } - else - { - newnode->v.segm.beg = node->v.segm.beg + beg; - newnode->v.segm.end = newnode->v.segm.beg + len; - } - newnode->flags |= flg; - *ptail = newnode; - return 0; -} - -static int -find_closing_paren (const char *str, size_t i, size_t len, size_t *poff, - char const *paren) -{ - enum { st_init, st_squote, st_dquote } state = st_init; - size_t level = 1; - - for (; i < len; i++) - { - switch (state) - { - case st_init: - switch (str[i]) - { - default: - if (str[i] == paren[0]) - { - level++; - break; - } - else if (str[i] == paren[1]) - { - if (--level == 0) - { - *poff = i; - return 0; - } - break; - } - break; - - case '"': - state = st_dquote; - break; - - case '\'': - state = st_squote; - break; - } - break; - - case st_squote: - if (str[i] == '\'') - state = st_init; - break; - - case st_dquote: - if (str[i] == '\\') - i++; - else if (str[i] == '"') - state = st_init; - break; - } - } - return 1; -} - -static char const * -wsplt_env_find (struct wordsplit *wsp, const char *name, size_t len) -{ - size_t i; - - if (!wsp->ws_env) - return NULL; - if (wsp->ws_flags & WRDSF_ENV_KV) - { - /* A key-value pair environment */ - for (i = 0; wsp->ws_env[i]; i++) - { - size_t elen = strlen (wsp->ws_env[i]); - if (elen == len && memcmp (wsp->ws_env[i], name, elen) == 0) - return wsp->ws_env[i + 1]; - /* Skip the value. Break the loop if it is NULL. */ - i++; - if (wsp->ws_env[i] == NULL) - break; - } - } - else - { - /* Usual (A=B) environment. */ - for (i = 0; wsp->ws_env[i]; i++) - { - size_t j; - const char *var = wsp->ws_env[i]; - - for (j = 0; j < len; j++) - if (name[j] != var[j]) - break; - if (j == len && var[j] == '=') - return var + j + 1; - } - } - return NULL; -} - -static int -wsplt_env_lookup (struct wordsplit *wsp, const char *name, size_t len, - char **ret) -{ - if (wsp->ws_flags & WRDSF_ENV) - { - char const *val = wsplt_env_find (wsp, name, len); - if (val) - { - char *retval = strdup (val); - if (!retval) - return WRDSE_NOSPACE; - *ret = retval; - return WRDSE_OK; - } - } - return WRDSE_UNDEF; -} - -static int -wsplt_env_getvar (struct wordsplit *wsp, const char *name, size_t len, - char **ret) -{ - return wsp->ws_getvar (ret, name, len, wsp->ws_closure); -} - -static int -wsplt_assign_var (struct wordsplit *wsp, const char *name, size_t namelen, - char const *value) -{ - int n = (wsp->ws_flags & WRDSF_ENV_KV) ? 2 : 1; - char *v; - - if (wsp->ws_envidx + n >= wsp->ws_envsiz) - { - size_t sz; - char **newenv; - - if (!wsp->ws_envbuf) - { - if (wsp->ws_flags & WRDSF_ENV) - { - size_t i = 0, j; - - if (wsp->ws_env) |