Commit 46d7640f 46d7640ffc83a8b809845099839990c9a2cf70f6 by Sergey Poznyakoff

New wordsplit call: mu_worsplit_append

* include/mailutils/wordsplit.h (mu_wordsplit_append): New proto.
* libmailutils/string/wordsplit.c (mu_wordsplit_append): New function.
* libmailutils/tests/wordsplit.at: Two more tests cases for
mu_wordsplit_append.
* libmailutils/tests/wsp.c: Accept extra arguments to append using
the new function.
1 parent 58cc7c8f
......@@ -241,6 +241,7 @@ void mu_wordsplit_free_words (mu_wordsplit_t *ws);
void mu_wordsplit_free_envbuf (mu_wordsplit_t *ws);
int mu_wordsplit_get_words (mu_wordsplit_t *ws, size_t *wordc, char ***wordv);
int mu_wordsplit_append (struct mu_wordsplit *wsp, int argc, char **argv);
int mu_wordsplit_c_unquote_char (int c);
int mu_wordsplit_c_quote_char (int c);
......
......@@ -674,7 +674,35 @@ mu_wordsplit_finish (struct mu_wordsplit *wsp)
return 0;
}
int
mu_wordsplit_append (struct mu_wordsplit *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 mu_wordsplit *wsp,
......
......@@ -623,5 +623,32 @@ EOT
[no files match pattern dir/*.d
])
AT_CLEANUP
TESTWSP([append],[],[-- extra arguments follow],
[some words and],
[NF: 6
0: some
1: words
2: and
3: extra
4: arguments
5: follow
])
TESTWSP([append + dooffs + env],[],
[dooffs 2 preface words V=2 -- extra arguments follow],
[some words and var=$V],
[NF: 7 (2)
(0): preface
(1): words
2: some
3: words
4: and
5: var=2
6: extra
7: arguments
8: follow
])
m4_popdef([TESTWSP])
......
......@@ -77,7 +77,7 @@ help ()
{
size_t i;
printf ("usage: %s [options] [VAR=VALUE...]\n", progname);
printf ("usage: %s [options] [VAR=VALUE...] [-- EXTRA...]\n", progname);
printf ("options are:\n");
printf (" [-]trimnl\n");
printf (" [-]plaintext\n");
......@@ -314,6 +314,8 @@ main (int argc, char **argv)
size_t fenvidx = 0;
size_t fenvmax = sizeof (fenvbase) / sizeof (fenvbase[0]);
int use_env = env_sys;
int appendc = 0;
char **appendv = NULL;
progname = argv[0];
......@@ -326,6 +328,12 @@ main (int argc, char **argv)
if (opt[0] == '-')
{
if (opt[1] == '-' && opt[2] == 0)
{
appendc = argc - i - 1;
appendv = argv + i + 1;
break;
}
negate = 1;
opt++;
}
......@@ -588,6 +596,17 @@ main (int argc, char **argv)
offarg = 0;
}
if (appendc)
{
rc = mu_wordsplit_append (&ws, appendc, appendv);
if (rc)
{
if (!(wsflags & MU_WRDSF_SHOWERR))
mu_wordsplit_perror (&ws);
continue;
}
}
wsflags |= MU_WRDSF_REUSE | (ws.ws_flags & MU_WRDSF_ENV);
printf ("NF: %lu", (unsigned long) ws.ws_wordc);
if (wsflags & MU_WRDSF_DOOFFS)
......