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); ...@@ -241,6 +241,7 @@ void mu_wordsplit_free_words (mu_wordsplit_t *ws);
241 void mu_wordsplit_free_envbuf (mu_wordsplit_t *ws); 241 void mu_wordsplit_free_envbuf (mu_wordsplit_t *ws);
242 242
243 int mu_wordsplit_get_words (mu_wordsplit_t *ws, size_t *wordc, char ***wordv); 243 int mu_wordsplit_get_words (mu_wordsplit_t *ws, size_t *wordc, char ***wordv);
244 int mu_wordsplit_append (struct mu_wordsplit *wsp, int argc, char **argv);
244 245
245 int mu_wordsplit_c_unquote_char (int c); 246 int mu_wordsplit_c_unquote_char (int c);
246 int mu_wordsplit_c_quote_char (int c); 247 int mu_wordsplit_c_quote_char (int c);
......
...@@ -674,6 +674,34 @@ mu_wordsplit_finish (struct mu_wordsplit *wsp) ...@@ -674,6 +674,34 @@ mu_wordsplit_finish (struct mu_wordsplit *wsp)
674 return 0; 674 return 0;
675 } 675 }
676 676
677 int
678 mu_wordsplit_append (struct mu_wordsplit *wsp, int argc, char **argv)
679 {
680 int rc;
681 size_t i;
682
683 rc = alloc_space (wsp, wsp->ws_wordc + argc + 1);
684 if (rc)
685 return rc;
686 for (i = 0; i < argc; i++)
687 {
688 char *newstr = strdup (argv[i]);
689 if (!newstr)
690 {
691 while (i > 0)
692 {
693 free (wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i - 1]);
694 wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i - 1] = NULL;
695 i--;
696 }
697 return _wsplt_nomem (wsp);
698 }
699 wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc + i] = newstr;
700 }
701 wsp->ws_wordc += i;
702 wsp->ws_wordv[wsp->ws_offs + wsp->ws_wordc] = NULL;
703 return 0;
704 }
677 705
678 /* Variable expansion */ 706 /* Variable expansion */
679 static int 707 static int
......
...@@ -624,4 +624,31 @@ EOT ...@@ -624,4 +624,31 @@ EOT
624 ]) 624 ])
625 AT_CLEANUP 625 AT_CLEANUP
626 626
627 TESTWSP([append],[],[-- extra arguments follow],
628 [some words and],
629 [NF: 6
630 0: some
631 1: words
632 2: and
633 3: extra
634 4: arguments
635 5: follow
636 ])
637
638 TESTWSP([append + dooffs + env],[],
639 [dooffs 2 preface words V=2 -- extra arguments follow],
640 [some words and var=$V],
641 [NF: 7 (2)
642 (0): preface
643 (1): words
644 2: some
645 3: words
646 4: and
647 5: var=2
648 6: extra
649 7: arguments
650 8: follow
651 ])
652
653
627 m4_popdef([TESTWSP]) 654 m4_popdef([TESTWSP])
......
...@@ -77,7 +77,7 @@ help () ...@@ -77,7 +77,7 @@ help ()
77 { 77 {
78 size_t i; 78 size_t i;
79 79
80 printf ("usage: %s [options] [VAR=VALUE...]\n", progname); 80 printf ("usage: %s [options] [VAR=VALUE...] [-- EXTRA...]\n", progname);
81 printf ("options are:\n"); 81 printf ("options are:\n");
82 printf (" [-]trimnl\n"); 82 printf (" [-]trimnl\n");
83 printf (" [-]plaintext\n"); 83 printf (" [-]plaintext\n");
...@@ -314,6 +314,8 @@ main (int argc, char **argv) ...@@ -314,6 +314,8 @@ main (int argc, char **argv)
314 size_t fenvidx = 0; 314 size_t fenvidx = 0;
315 size_t fenvmax = sizeof (fenvbase) / sizeof (fenvbase[0]); 315 size_t fenvmax = sizeof (fenvbase) / sizeof (fenvbase[0]);
316 int use_env = env_sys; 316 int use_env = env_sys;
317 int appendc = 0;
318 char **appendv = NULL;
317 319
318 progname = argv[0]; 320 progname = argv[0];
319 321
...@@ -326,6 +328,12 @@ main (int argc, char **argv) ...@@ -326,6 +328,12 @@ main (int argc, char **argv)
326 328
327 if (opt[0] == '-') 329 if (opt[0] == '-')
328 { 330 {
331 if (opt[1] == '-' && opt[2] == 0)
332 {
333 appendc = argc - i - 1;
334 appendv = argv + i + 1;
335 break;
336 }
329 negate = 1; 337 negate = 1;
330 opt++; 338 opt++;
331 } 339 }
...@@ -588,6 +596,17 @@ main (int argc, char **argv) ...@@ -588,6 +596,17 @@ main (int argc, char **argv)
588 offarg = 0; 596 offarg = 0;
589 } 597 }
590 598
599 if (appendc)
600 {
601 rc = mu_wordsplit_append (&ws, appendc, appendv);
602 if (rc)
603 {
604 if (!(wsflags & MU_WRDSF_SHOWERR))
605 mu_wordsplit_perror (&ws);
606 continue;
607 }
608 }
609
591 wsflags |= MU_WRDSF_REUSE | (ws.ws_flags & MU_WRDSF_ENV); 610 wsflags |= MU_WRDSF_REUSE | (ws.ws_flags & MU_WRDSF_ENV);
592 printf ("NF: %lu", (unsigned long) ws.ws_wordc); 611 printf ("NF: %lu", (unsigned long) ws.ws_wordc);
593 if (wsflags & MU_WRDSF_DOOFFS) 612 if (wsflags & MU_WRDSF_DOOFFS)
......