`addrcodec': trim input, cannot do otherwise; fix IDNA with [f3852f88]
[s-mailx.git] / cmd-tab.c
blob6c2ae66a259cf0b5c6664a5644111f47826d67ee
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ n_cmd_firstfit(): the table of commands + `help' and `list'.
3 *@ And n_cmd_arg_parse(), the (new) argument list parser. TODO this is
4 *@ TODO too stupid yet, however: it should fully support subcommands, too, so
5 *@ TODO that, e.g., "vexpr regex" arguments can be fully prepared by the
6 *@ TODO generic parser. But at least a bit.
7 *@ TODO See cmd-tab.h for sort and speedup TODOs.
9 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
11 /* Command table and getrawlist() also:
12 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
14 * Copyright (c) 1980, 1993
15 * The Regents of the University of California. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
41 #undef n_FILE
42 #define n_FILE cmd_tab
44 #ifndef HAVE_AMALGAMATION
45 # include "nail.h"
46 #endif
48 /* Create a multiline info string about all known additional infos for lcp */
49 #ifdef HAVE_DOCSTRINGS
50 static char const *a_ctab_cmdinfo(struct n_cmd_desc const *cdp);
51 #endif
53 /* Print a list of all commands */
54 static int a_ctab_c_list(void *vp);
56 static int a_ctab__pcmd_cmp(void const *s1, void const *s2);
58 /* `help' / `?' command */
59 static int a_ctab_c_help(void *vp);
61 /* List of all commands; but first their n_cmd_arg_desc instances */
62 #include "cmd-tab.h"
63 static struct n_cmd_desc const a_ctab_ctable[] = {
64 #include "cmd-tab.h"
67 /* And a list of things which are special to the lexer in go.c, so that we can
68 * provide help and list them.
69 * This cross-file relationship is a bit unfortunate.. */
70 #ifdef HAVE_DOCSTRINGS
71 # define DS(S) , S
72 #else
73 # define DS(S)
74 #endif
75 static struct n_cmd_desc const a_ctab_ctable_plus[] = {
76 { n_ns, (int(*)(void*))-1, n_CMD_ARG_TYPE_STRING, 0, 0, NULL
77 DS(N_("Comment command: ignore remaining (continuable) line")) },
78 { "-", (int(*)(void*))-1, n_CMD_ARG_TYPE_WYSH, 0, 0, NULL
79 DS(N_("Print out the preceding message")) }
81 #undef DS
83 #ifdef HAVE_DOCSTRINGS
84 static char const *
85 a_ctab_cmdinfo(struct n_cmd_desc const *cdp){
86 struct n_string rvb, *rv;
87 char const *cp;
88 NYD2_ENTER;
90 rv = n_string_creat_auto(&rvb);
91 rv = n_string_reserve(rv, 80);
93 switch(cdp->cd_caflags & n_CMD_ARG_TYPE_MASK){
94 case n_CMD_ARG_TYPE_MSGLIST:
95 cp = N_("message-list");
96 break;
97 case n_CMD_ARG_TYPE_STRING:
98 case n_CMD_ARG_TYPE_RAWDAT:
99 cp = N_("string data");
100 break;
101 case n_CMD_ARG_TYPE_RAWLIST:
102 cp = N_("old-style quoting");
103 break;
104 case n_CMD_ARG_TYPE_NDMLIST:
105 cp = N_("message-list (no default)");
106 break;
107 case n_CMD_ARG_TYPE_WYRA:
108 cp = N_("`wysh' for sh(1)ell-style quoting");
109 break;
110 case n_CMD_ARG_TYPE_WYSH:
111 cp = (cdp->cd_minargs == 0 && cdp->cd_maxargs == 0)
112 ? N_("sh(1)ell-style quoting (takes no arguments)")
113 : N_("sh(1)ell-style quoting");
114 break;
115 default:
116 case n_CMD_ARG_TYPE_ARG:{
117 ui32_t flags;
118 size_t i;
119 struct n_cmd_arg_desc const *cadp;
121 rv = n_string_push_cp(rv, _("argument tokens: "));
123 for(cadp = cdp->cd_cadp, i = 0; i < cadp->cad_no; ++i){
124 if(i != 0)
125 rv = n_string_push_c(rv, ',');
127 flags = cadp->cad_ent_flags[i][0];
128 if(flags & n_CMD_ARG_DESC_OPTION)
129 rv = n_string_push_c(rv, '[');
130 if(flags & n_CMD_ARG_DESC_GREEDY)
131 rv = n_string_push_c(rv, ':');
132 switch(flags & n__CMD_ARG_DESC_TYPE_MASK){
133 case n_CMD_ARG_DESC_STRING:
134 rv = n_string_push_cp(rv, _("raw"));
135 break;
136 default:
137 case n_CMD_ARG_DESC_WYSH:
138 rv = n_string_push_cp(rv, _("eval"));
139 break;
141 if(flags & n_CMD_ARG_DESC_GREEDY)
142 rv = n_string_push_c(rv, ':');
143 if(flags & n_CMD_ARG_DESC_OPTION)
144 rv = n_string_push_c(rv, ']');
146 cp = NULL;
147 }break;
149 if(cp != NULL)
150 rv = n_string_push_cp(rv, V_(cp));
152 /* Note: on updates, change the manual! */
153 if(cdp->cd_caflags & n_CMD_ARG_L)
154 rv = n_string_push_cp(rv, _(" | `local'"));
155 if(cdp->cd_caflags & n_CMD_ARG_V)
156 rv = n_string_push_cp(rv, _(" | `vput'"));
157 if(cdp->cd_caflags & n_CMD_ARG_EM)
158 rv = n_string_push_cp(rv, _(" | *!*"));
160 if(cdp->cd_caflags & n_CMD_ARG_A)
161 rv = n_string_push_cp(rv, _(" | needs box"));
162 if(cdp->cd_caflags & n_CMD_ARG_I)
163 rv = n_string_push_cp(rv, _(" | ok: batch/interactive"));
164 if(cdp->cd_caflags & n_CMD_ARG_M)
165 rv = n_string_push_cp(rv, _(" | ok: send mode"));
166 if(cdp->cd_caflags & n_CMD_ARG_R)
167 rv = n_string_push_cp(rv, _(" | not ok: compose mode"));
168 if(cdp->cd_caflags & n_CMD_ARG_S)
169 rv = n_string_push_cp(rv, _(" | not ok: startup"));
170 if(cdp->cd_caflags & n_CMD_ARG_X)
171 rv = n_string_push_cp(rv, _(" | ok: subprocess"));
173 if(cdp->cd_caflags & n_CMD_ARG_G)
174 rv = n_string_push_cp(rv, _(" | gabby"));
176 cp = n_string_cp(rv);
177 NYD2_LEAVE;
178 return cp;
180 #endif /* HAVE_DOCSTRINGS */
182 static int
183 a_ctab_c_list(void *vp){
184 FILE *fp;
185 struct n_cmd_desc const **cdpa, *cdp, **cdpa_curr;
186 size_t i, l, scrwid;
187 NYD_ENTER;
189 i = n_NELEM(a_ctab_ctable) + n_NELEM(a_ctab_ctable_plus) +1;
190 cdpa = n_autorec_alloc(sizeof(cdp) * i);
192 for(i = 0; i < n_NELEM(a_ctab_ctable); ++i)
193 cdpa[i] = &a_ctab_ctable[i];
194 for(l = 0; l < n_NELEM(a_ctab_ctable_plus); ++i, ++l)
195 cdpa[i] = &a_ctab_ctable_plus[l];
196 cdpa[i] = NULL;
198 if(*(void**)vp == NULL)
199 qsort(cdpa, i, sizeof(*cdpa), &a_ctab__pcmd_cmp);
201 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
202 fp = n_stdout;
204 scrwid = n_SCRNWIDTH_FOR_LISTS;
206 fprintf(fp, _("Commands are:\n"));
207 l = 1;
208 for(i = 0, cdpa_curr = cdpa; (cdp = *cdpa_curr++) != NULL;){
209 char const *pre, *suf;
211 if(cdp->cd_func == NULL)
212 pre = "[", suf = "]";
213 else
214 pre = suf = n_empty;
216 #ifdef HAVE_DOCSTRINGS
217 if(n_poption & n_PO_D_V){
218 fprintf(fp, "%s%s%s\n", pre, cdp->cd_name, suf);
219 ++l;
220 fprintf(fp, " : %s\n", V_(cdp->cd_doc));
221 ++l;
222 fprintf(fp, " : %s\n", a_ctab_cmdinfo(cdp));
223 ++l;
224 }else
225 #endif
227 size_t j;
229 j = strlen(cdp->cd_name);
230 if(*pre != '\0')
231 j += 2;
233 if((i += j + 2) > scrwid){
234 i = j;
235 fprintf(fp, "\n");
236 ++l;
238 fprintf(fp, (*cdpa_curr != NULL ? "%s%s%s, " : "%s%s%s\n"),
239 pre, cdp->cd_name, suf);
243 if(fp != n_stdout){
244 page_or_print(fp, l);
245 Fclose(fp);
247 NYD_LEAVE;
248 return 0;
251 static int
252 a_ctab__pcmd_cmp(void const *s1, void const *s2){
253 struct n_cmd_desc const * const *cdpa1, * const *cdpa2;
254 int rv;
255 NYD2_ENTER;
257 cdpa1 = s1;
258 cdpa2 = s2;
259 rv = strcmp((*cdpa1)->cd_name, (*cdpa2)->cd_name);
260 NYD2_LEAVE;
261 return rv;
264 static int
265 a_ctab_c_help(void *vp){
266 int rv;
267 char const *arg;
268 NYD_ENTER;
270 /* Help for a single command? */
271 if((arg = *(char const**)vp) != NULL){
272 struct n_cmd_desc const *cdp, *cdp_max;
273 struct str const *alias_exp;
274 char const *alias_name;
276 /* Aliases take precedence */
277 if((alias_name = n_commandalias_exists(arg, &alias_exp)) != NULL){
278 fprintf(n_stdout, "%s -> ", arg);
279 arg = alias_exp->s;
282 cdp_max = &(cdp = a_ctab_ctable)[n_NELEM(a_ctab_ctable)];
283 jredo:
284 for(; cdp < cdp_max; ++cdp){
285 if(is_prefix(arg, cdp->cd_name)){
286 fputs(arg, n_stdout);
287 if(strcmp(arg, cdp->cd_name))
288 fprintf(n_stdout, " (%s)", cdp->cd_name);
289 }else
290 continue;
292 #ifdef HAVE_DOCSTRINGS
293 fprintf(n_stdout, ": %s", V_(cdp->cd_doc));
294 if(n_poption & n_PO_D_V)
295 fprintf(n_stdout, "\n : %s", a_ctab_cmdinfo(cdp));
296 #endif
297 putc('\n', n_stdout);
298 rv = 0;
299 goto jleave;
302 if(cdp_max == &a_ctab_ctable[n_NELEM(a_ctab_ctable)]){
303 cdp_max = &(cdp =
304 a_ctab_ctable_plus)[n_NELEM(a_ctab_ctable_plus)];
305 goto jredo;
308 if(alias_name != NULL){
309 fprintf(n_stdout, "%s\n", n_shexp_quote_cp(arg, TRU1));
310 rv = 0;
311 }else{
312 n_err(_("Unknown command: `%s'\n"), arg);
313 rv = 1;
315 }else{
316 /* Very ugly, but take care for compiler supported string lengths :( */
317 fputs(n_progname, n_stdout);
318 fputs(_(
319 " commands -- <msglist> denotes message specifications,\n"
320 "e.g., 1-5, :n or ., separated by spaces:\n"), n_stdout);
321 fputs(_(
322 "\n"
323 "type <msglist> type (`print') messages (honour `headerpick' etc.)\n"
324 "Type <msglist> like `type' but always show all headers\n"
325 "next goto and type next message\n"
326 "from <msglist> (search and) print header summary for the given list\n"
327 "headers header summary for messages surrounding \"dot\"\n"
328 "delete <msglist> delete messages (can be `undelete'd)\n"),
329 n_stdout);
331 fputs(_(
332 "\n"
333 "save <msglist> folder append messages to folder and mark as saved\n"
334 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
335 "write <msglist> file write message contents to file (prompts for parts)\n"
336 "Reply <msglist> reply to message senders only\n"
337 "reply <msglist> like `Reply', but address all recipients\n"
338 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
339 n_stdout);
341 fputs(_(
342 "\n"
343 "mail <recipients> compose a mail for the given recipients\n"
344 "file folder change to another mailbox\n"
345 "File folder like `file', but open readonly\n"
346 "quit quit and apply changes to the current mailbox\n"
347 "xit or exit like `quit', but discard changes\n"
348 "!shell command shell escape\n"
349 "list [<anything>] all available commands [in search order]\n"),
350 n_stdout);
352 rv = (ferror(n_stdout) != 0);
354 jleave:
355 NYD_LEAVE;
356 return rv;
359 FL char const *
360 n_cmd_isolate(char const *cmd){
361 NYD2_ENTER;
362 while(*cmd != '\0' &&
363 strchr("\\!~|? \t0123456789&%@$^.:/-+*'\",;(`", *cmd) == NULL)
364 ++cmd;
365 NYD2_LEAVE;
366 return n_UNCONST(cmd);
369 FL struct n_cmd_desc const *
370 n_cmd_firstfit(char const *cmd){ /* TODO *hashtable*! linear list search!!! */
371 struct n_cmd_desc const *cdp;
372 NYD2_ENTER;
374 for(cdp = a_ctab_ctable; cdp < &a_ctab_ctable[n_NELEM(a_ctab_ctable)]; ++cdp)
375 if(*cmd == *cdp->cd_name && cdp->cd_func != NULL &&
376 is_prefix(cmd, cdp->cd_name))
377 goto jleave;
378 cdp = NULL;
379 jleave:
380 NYD2_LEAVE;
381 return cdp;
384 FL struct n_cmd_desc const *
385 n_cmd_default(void){
386 struct n_cmd_desc const *cdp;
387 NYD2_ENTER;
389 cdp = &a_ctab_ctable[0];
390 NYD2_LEAVE;
391 return cdp;
394 FL bool_t
395 n_cmd_arg_parse(struct n_cmd_arg_ctx *cacp){
396 struct n_cmd_arg ncap, *lcap;
397 struct str shin_orig, shin;
398 bool_t addca, greedyjoin;
399 void const *cookie;
400 size_t cad_idx, parsed_args;
401 struct n_cmd_arg_desc const *cadp;
402 NYD_ENTER;
404 assert(cacp->cac_inlen == 0 || cacp->cac_indat != NULL);
405 assert(cacp->cac_desc->cad_no > 0);
406 #ifdef HAVE_DEBUG
407 /* C99 */{
408 bool_t opt_seen = FAL0;
410 for(cadp = cacp->cac_desc, cad_idx = 0;
411 cad_idx < cadp->cad_no; ++cad_idx){
412 assert(cadp->cad_ent_flags[cad_idx][0] & n__CMD_ARG_DESC_TYPE_MASK);
413 assert(!opt_seen ||
414 (cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION));
415 if(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION)
416 opt_seen = TRU1;
417 assert(!(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_GREEDY) ||
418 cad_idx + 1 == cadp->cad_no);
421 #endif
423 shin.s = n_UNCONST(cacp->cac_indat); /* "logical" only */
424 shin.l = (cacp->cac_inlen == UIZ_MAX ? strlen(shin.s) : cacp->cac_inlen);
425 shin_orig = shin;
426 cacp->cac_no = 0;
427 cacp->cac_arg = lcap = NULL;
429 cookie = NULL;
430 parsed_args = 0;
431 greedyjoin = FAL0;
433 for(cadp = cacp->cac_desc, cad_idx = 0; shin.l > 0 && cad_idx < cadp->cad_no;
434 ++cad_idx){
435 jredo:
436 memset(&ncap, 0, sizeof ncap);
437 ncap.ca_indat = shin.s;
438 /* >ca_inline once we know */
439 memcpy(&ncap.ca_ent_flags[0], &cadp->cad_ent_flags[cad_idx][0],
440 sizeof ncap.ca_ent_flags);
441 addca = FAL0;
443 switch(ncap.ca_ent_flags[0] & n__CMD_ARG_DESC_TYPE_MASK){
444 case n_CMD_ARG_DESC_STRING:{ /* TODO \ escaping? additional type!? */
445 char /*const*/ *cp = shin.s;
446 size_t i = shin.l;
448 while(i > 0 && blankspacechar(*cp))
449 ++cp, --i;
451 ncap.ca_arg.ca_str.s = cp;
452 while(i > 0 && !blankspacechar(*cp))
453 ++cp, --i;
454 ncap.ca_arg.ca_str.s = savestrbuf(ncap.ca_arg.ca_str.s,
455 ncap.ca_arg.ca_str.l = PTR2SIZE(cp - ncap.ca_arg.ca_str.s));
457 while(i > 0 && blankspacechar(*cp))
458 ++cp, --i;
459 ncap.ca_inlen = PTR2SIZE(cp - ncap.ca_indat);
460 shin.s = cp;
461 shin.l = i;
462 addca = TRU1;
463 }break;
464 default:
465 case n_CMD_ARG_DESC_WYSH:{
466 struct n_string shou, *shoup;
467 enum n_shexp_state shs;
468 ui32_t addflags;
470 if(cad_idx == cadp->cad_no - 1 ||
471 (cadp->cad_ent_flags[cad_idx + 1][0] & n_CMD_ARG_DESC_OPTION))
472 addflags = n_SHEXP_PARSE_META_SEMICOLON;
473 else
474 addflags = n_SHEXP_PARSE_NONE;
476 shoup = n_string_creat_auto(&shou);
477 ncap.ca_arg_flags =
478 shs = n_shexp_parse_token((ncap.ca_ent_flags[1] | addflags |
479 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_LOG),
480 shoup, &shin,
481 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY ? &cookie : NULL));
482 ncap.ca_inlen = PTR2SIZE(shin.s - ncap.ca_indat);
483 if((shs & (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)) ==
484 n_SHEXP_STATE_OUTPUT){
485 if((shs & n_SHEXP_STATE_META_SEMICOLON) && shou.s_len == 0)
486 break;
487 ncap.ca_arg.ca_str.s = n_string_cp(shoup);
488 ncap.ca_arg.ca_str.l = shou.s_len;
489 shoup = n_string_drop_ownership(shoup);
491 n_string_gut(shoup);
493 if(shs & n_SHEXP_STATE_ERR_MASK)
494 goto jerr;
495 if((shs & n_SHEXP_STATE_STOP) &&
496 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_HONOUR_STOP)){
497 if(!(shs & n_SHEXP_STATE_OUTPUT))
498 goto jleave;
499 addca = TRUM1;
500 }else
501 addca = TRU1;
502 }break;
504 ++parsed_args;
506 if(addca){
507 if(greedyjoin == TRU1){ /* TODO speed this up! */
508 char *cp;
509 size_t i;
511 assert(lcap != NULL);
512 i = lcap->ca_arg.ca_str.l;
513 lcap->ca_arg.ca_str.l += 1 + ncap.ca_arg.ca_str.l;
514 cp = salloc(lcap->ca_arg.ca_str.l +1);
515 memcpy(cp, lcap->ca_arg.ca_str.s, i);
516 lcap->ca_arg.ca_str.s = cp;
517 cp[i++] = ' ';
518 memcpy(&cp[i], ncap.ca_arg.ca_str.s, ncap.ca_arg.ca_str.l +1);
519 }else{
520 struct n_cmd_arg *cap;
522 cap = salloc(sizeof *cap);
523 memcpy(cap, &ncap, sizeof ncap);
524 if(lcap == NULL)
525 cacp->cac_arg = cap;
526 else
527 lcap->ca_next = cap;
528 lcap = cap;
529 ++cacp->cac_no;
532 if(addca == TRUM1)
533 goto jleave;
536 if((shin.l > 0 || cookie != NULL) &&
537 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY)){
538 if(!greedyjoin)
539 greedyjoin = ((ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY_JOIN) &&
540 (ncap.ca_ent_flags[0] &
541 (n_CMD_ARG_DESC_STRING | n_CMD_ARG_DESC_WYSH)))
542 ? TRU1 : TRUM1;
543 goto jredo;
547 if(cad_idx < cadp->cad_no &&
548 !(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION))
549 goto jerr;
551 lcap = (struct n_cmd_arg*)-1;
552 jleave:
553 NYD_LEAVE;
554 return (lcap != NULL);
556 jerr:{
557 size_t i;
559 for(i = 0; (i < cadp->cad_no &&
560 !(cadp->cad_ent_flags[i][0] & n_CMD_ARG_DESC_OPTION)); ++i)
563 n_err(_("`%s': parsing stopped after %" PRIuZ " arguments "
564 "(need %" PRIuZ "%s)\n"
565 " Input: %.*s\n"
566 " Stopped: %.*s\n"),
567 cadp->cad_name, parsed_args, i, (i == cadp->cad_no ? n_empty : "+"),
568 (int)shin_orig.l, shin_orig.s,
569 (int)shin.l, shin.s);
571 lcap = NULL;
572 goto jleave;
575 FL void *
576 n_cmd_arg_save_to_heap(struct n_cmd_arg_ctx const *cacp){
577 struct n_cmd_arg *ncap;
578 struct n_cmd_arg_ctx *ncacp;
579 char *buf;
580 struct n_cmd_arg const *cap;
581 size_t len, i;
582 NYD2_ENTER;
584 /* For simplicity, save it all in once chunk, so that it can be thrown away
585 * with a simple n_free() from whoever is concerned */
586 len = sizeof *cacp;
587 for(cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
588 i = cap->ca_arg.ca_str.l +1;
589 i = n_ALIGN(i);
590 len += sizeof(*cap) + i;
592 if(cacp->cac_vput != NULL)
593 len += strlen(cacp->cac_vput) +1;
595 ncacp = n_alloc(len);
596 *ncacp = *cacp;
597 buf = (char*)&ncacp[1];
599 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
600 void *vp;
602 vp = buf;
603 DBG( memset(vp, 0, sizeof *ncap); )
605 if(ncap == NULL)
606 ncacp->cac_arg = vp;
607 else
608 ncap->ca_next = vp;
609 ncap = vp;
610 ncap->ca_next = NULL;
611 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
612 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
613 ncap->ca_arg_flags = cap->ca_arg_flags;
614 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
615 (ncap->ca_arg.ca_str.l = i = cap->ca_arg.ca_str.l) +1);
617 i = n_ALIGN(i);
618 buf += sizeof(*ncap) + i;
621 if(cacp->cac_vput != NULL){
622 ncacp->cac_vput = buf;
623 memcpy(buf, cacp->cac_vput, strlen(cacp->cac_vput) +1);
624 }else
625 ncacp->cac_vput = NULL;
626 NYD2_LEAVE;
627 return ncacp;
630 FL struct n_cmd_arg_ctx *
631 n_cmd_arg_restore_from_heap(void *vp){
632 struct n_cmd_arg *cap, *ncap;
633 struct n_cmd_arg_ctx *cacp, *rv;
634 NYD2_ENTER;
636 rv = n_autorec_alloc(sizeof *rv);
637 cacp = vp;
638 *rv = *cacp;
640 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
641 vp = n_autorec_alloc(sizeof(*ncap) + cap->ca_arg.ca_str.l +1);
642 DBG( memset(vp, 0, sizeof *ncap); )
644 if(ncap == NULL)
645 rv->cac_arg = vp;
646 else
647 ncap->ca_next = vp;
648 ncap = vp;
649 ncap->ca_next = NULL;
650 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
651 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
652 ncap->ca_arg_flags = cap->ca_arg_flags;
653 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
654 (ncap->ca_arg.ca_str.l = cap->ca_arg.ca_str.l) +1);
657 if(cacp->cac_vput != NULL)
658 rv->cac_vput = savestr(cacp->cac_vput);
659 NYD2_LEAVE;
660 return rv;
663 FL int
664 getrawlist(bool_t wysh, char **res_dat, size_t res_size,
665 char const *line, size_t linesize){
666 int res_no;
667 NYD_ENTER;
669 n_pstate &= ~n_PS_ARGLIST_MASK;
671 if(res_size == 0){
672 res_no = -1;
673 goto jleave;
674 }else if(UICMP(z, res_size, >, INT_MAX))
675 res_size = INT_MAX;
676 else
677 --res_size;
678 res_no = 0;
680 if(!wysh){
681 /* And assuming result won't grow input */
682 char c2, c, quotec, *cp2, *linebuf;
684 linebuf = n_lofi_alloc(linesize);
686 for(;;){
687 for(; blankchar(*line); ++line)
689 if(*line == '\0')
690 break;
692 if(UICMP(z, res_no, >=, res_size)){
693 n_err(_("Too many input tokens for result storage\n"));
694 res_no = -1;
695 break;
698 cp2 = linebuf;
699 quotec = '\0';
701 /* TODO v15: complete switch in order mirror known behaviour */
702 while((c = *line++) != '\0'){
703 if(quotec != '\0'){
704 if(c == quotec){
705 quotec = '\0';
706 continue;
707 }else if(c == '\\'){
708 if((c2 = *line++) == quotec)
709 c = c2;
710 else
711 --line;
713 }else if(c == '"' || c == '\''){
714 quotec = c;
715 continue;
716 }else if(c == '\\'){
717 if((c2 = *line++) != '\0')
718 c = c2;
719 else
720 --line;
721 }else if(blankchar(c))
722 break;
723 *cp2++ = c;
726 res_dat[res_no++] = savestrbuf(linebuf, PTR2SIZE(cp2 - linebuf));
727 if(c == '\0')
728 break;
731 n_lofi_free(linebuf);
732 }else{
733 /* sh(1) compat mode. Prepare shell token-wise */
734 struct n_string store;
735 struct str input;
736 void const *cookie;
738 n_string_creat_auto(&store);
739 input.s = n_UNCONST(line);
740 input.l = linesize;
741 cookie = NULL;
743 for(;;){
744 if(UICMP(z, res_no, >=, res_size)){
745 n_err(_("Too many input tokens for result storage\n"));
746 res_no = -1;
747 break;
750 /* C99 */{
751 enum n_shexp_state shs;
753 if((shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
754 (cookie == NULL ? n_SHEXP_PARSE_TRIM_SPACE : 0) |
755 /* TODO not here in old style n_SHEXP_PARSE_IFS_VAR |*/
756 n_SHEXP_PARSE_META_SEMICOLON),
757 &store, &input, &cookie)
758 ) & n_SHEXP_STATE_ERR_MASK){
759 /* Simply ignore Unicode error, just keep the normalized \[Uu] */
760 if((shs & n_SHEXP_STATE_ERR_MASK) != n_SHEXP_STATE_ERR_UNICODE){
761 res_no = -1;
762 break;
766 if(shs & n_SHEXP_STATE_OUTPUT){
767 if(shs & n_SHEXP_STATE_CONTROL)
768 n_pstate |= n_PS_WYSHLIST_SAW_CONTROL;
770 res_dat[res_no++] = n_string_cp(&store);
771 n_string_drop_ownership(&store);
774 if(shs & n_SHEXP_STATE_STOP)
775 break;
779 n_string_gut(&store);
782 if(res_no >= 0)
783 res_dat[(size_t)res_no] = NULL;
784 jleave:
785 NYD_LEAVE;
786 return res_no;
789 /* s-it-mode */