n_idna_to_ascii(): add support for idnkit 2.3
[s-mailx.git] / cmd-tab.c
blob4661f029433f5ffe048a87968613acc86a41d1a8
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 #ifdef HAVE_UISTRINGS
318 fputs(n_progname, n_stdout);
319 fputs(_(
320 " commands -- <msglist> denotes message specifications,\n"
321 "e.g., 1-5, :n or . (current, the \"dot\"), separated by spaces:\n"),
322 n_stdout);
323 fputs(_(
324 "\n"
325 "type <msglist> type (`print') messages (honour `headerpick' etc.)\n"
326 "Type <msglist> like `type' but always show all headers\n"
327 "next goto and type next message\n"
328 "from <msglist> (search and) print header summary for the given list\n"
329 "headers header summary for messages surrounding \"dot\"\n"
330 "delete <msglist> delete messages (can be `undelete'd)\n"),
331 n_stdout);
333 fputs(_(
334 "\n"
335 "save <msglist> folder append messages to folder and mark as saved\n"
336 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
337 "write <msglist> file write message contents to file (prompts for parts)\n"
338 "Reply <msglist> reply to message senders only\n"
339 "reply <msglist> like `Reply', but address all recipients\n"
340 "Lreply <msglist> forced mailing list `reply' (see `mlist')\n"),
341 n_stdout);
343 fputs(_(
344 "\n"
345 "mail <recipients> compose a mail for the given recipients\n"
346 "file folder change to another mailbox\n"
347 "File folder like `file', but open readonly\n"
348 "quit quit and apply changes to the current mailbox\n"
349 "xit or exit like `quit', but discard changes\n"
350 "!shell command shell escape\n"
351 "list [<anything>] all available commands [in search order]\n"),
352 n_stdout);
353 #endif /* HAVE_UISTRINGS */
355 rv = (ferror(n_stdout) != 0);
357 jleave:
358 NYD_LEAVE;
359 return rv;
362 FL char const *
363 n_cmd_isolate(char const *cmd){
364 NYD2_ENTER;
365 while(*cmd != '\0' &&
366 strchr("\\!~|? \t0123456789&%@$^.:/-+*'\",;(`", *cmd) == NULL)
367 ++cmd;
368 NYD2_LEAVE;
369 return n_UNCONST(cmd);
372 FL struct n_cmd_desc const *
373 n_cmd_firstfit(char const *cmd){ /* TODO *hashtable*! linear list search!!! */
374 struct n_cmd_desc const *cdp;
375 NYD2_ENTER;
377 for(cdp = a_ctab_ctable; cdp < &a_ctab_ctable[n_NELEM(a_ctab_ctable)]; ++cdp)
378 if(*cmd == *cdp->cd_name && cdp->cd_func != NULL &&
379 is_prefix(cmd, cdp->cd_name))
380 goto jleave;
381 cdp = NULL;
382 jleave:
383 NYD2_LEAVE;
384 return cdp;
387 FL struct n_cmd_desc const *
388 n_cmd_default(void){
389 struct n_cmd_desc const *cdp;
390 NYD2_ENTER;
392 cdp = &a_ctab_ctable[0];
393 NYD2_LEAVE;
394 return cdp;
397 FL bool_t
398 n_cmd_arg_parse(struct n_cmd_arg_ctx *cacp){
399 struct n_cmd_arg ncap, *lcap;
400 struct str shin_orig, shin;
401 bool_t addca, greedyjoin;
402 void const *cookie;
403 size_t cad_idx, parsed_args;
404 struct n_cmd_arg_desc const *cadp;
405 NYD_ENTER;
407 assert(cacp->cac_inlen == 0 || cacp->cac_indat != NULL);
408 assert(cacp->cac_desc->cad_no > 0);
409 #ifdef HAVE_DEBUG
410 /* C99 */{
411 bool_t opt_seen = FAL0;
413 for(cadp = cacp->cac_desc, cad_idx = 0;
414 cad_idx < cadp->cad_no; ++cad_idx){
415 assert(cadp->cad_ent_flags[cad_idx][0] & n__CMD_ARG_DESC_TYPE_MASK);
416 assert(!opt_seen ||
417 (cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION));
418 if(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION)
419 opt_seen = TRU1;
420 assert(!(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_GREEDY) ||
421 cad_idx + 1 == cadp->cad_no);
424 #endif
426 shin.s = n_UNCONST(cacp->cac_indat); /* "logical" only */
427 shin.l = (cacp->cac_inlen == UIZ_MAX ? strlen(shin.s) : cacp->cac_inlen);
428 shin_orig = shin;
429 cacp->cac_no = 0;
430 cacp->cac_arg = lcap = NULL;
432 cookie = NULL;
433 parsed_args = 0;
434 greedyjoin = FAL0;
436 for(cadp = cacp->cac_desc, cad_idx = 0; shin.l > 0 && cad_idx < cadp->cad_no;
437 ++cad_idx){
438 jredo:
439 memset(&ncap, 0, sizeof ncap);
440 ncap.ca_indat = shin.s;
441 /* >ca_inline once we know */
442 memcpy(&ncap.ca_ent_flags[0], &cadp->cad_ent_flags[cad_idx][0],
443 sizeof ncap.ca_ent_flags);
444 addca = FAL0;
446 switch(ncap.ca_ent_flags[0] & n__CMD_ARG_DESC_TYPE_MASK){
447 case n_CMD_ARG_DESC_STRING:{ /* TODO \ escaping? additional type!? */
448 char /*const*/ *cp = shin.s;
449 size_t i = shin.l;
451 while(i > 0 && blankspacechar(*cp))
452 ++cp, --i;
454 ncap.ca_arg.ca_str.s = cp;
455 while(i > 0 && !blankspacechar(*cp))
456 ++cp, --i;
457 ncap.ca_arg.ca_str.s = savestrbuf(ncap.ca_arg.ca_str.s,
458 ncap.ca_arg.ca_str.l = PTR2SIZE(cp - ncap.ca_arg.ca_str.s));
460 while(i > 0 && blankspacechar(*cp))
461 ++cp, --i;
462 ncap.ca_inlen = PTR2SIZE(cp - ncap.ca_indat);
463 shin.s = cp;
464 shin.l = i;
465 addca = TRU1;
466 }break;
467 default:
468 case n_CMD_ARG_DESC_WYSH:{
469 struct n_string shou, *shoup;
470 enum n_shexp_state shs;
471 ui32_t addflags;
473 if(cad_idx == cadp->cad_no - 1 ||
474 (cadp->cad_ent_flags[cad_idx + 1][0] & n_CMD_ARG_DESC_OPTION))
475 addflags = n_SHEXP_PARSE_META_SEMICOLON;
476 else
477 addflags = n_SHEXP_PARSE_NONE;
479 shoup = n_string_creat_auto(&shou);
480 ncap.ca_arg_flags =
481 shs = n_shexp_parse_token((ncap.ca_ent_flags[1] | addflags |
482 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_LOG),
483 shoup, &shin,
484 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY ? &cookie : NULL));
485 ncap.ca_inlen = PTR2SIZE(shin.s - ncap.ca_indat);
486 if((shs & (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)) ==
487 n_SHEXP_STATE_OUTPUT){
488 if((shs & n_SHEXP_STATE_META_SEMICOLON) && shou.s_len == 0)
489 break;
490 ncap.ca_arg.ca_str.s = n_string_cp(shoup);
491 ncap.ca_arg.ca_str.l = shou.s_len;
492 shoup = n_string_drop_ownership(shoup);
494 n_string_gut(shoup);
496 if(shs & n_SHEXP_STATE_ERR_MASK)
497 goto jerr;
498 if((shs & n_SHEXP_STATE_STOP) &&
499 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_HONOUR_STOP)){
500 if(!(shs & n_SHEXP_STATE_OUTPUT))
501 goto jleave;
502 addca = TRUM1;
503 }else
504 addca = TRU1;
505 }break;
507 ++parsed_args;
509 if(addca){
510 if(greedyjoin == TRU1){ /* TODO speed this up! */
511 char *cp;
512 size_t i;
514 assert(lcap != NULL);
515 i = lcap->ca_arg.ca_str.l;
516 lcap->ca_arg.ca_str.l += 1 + ncap.ca_arg.ca_str.l;
517 cp = salloc(lcap->ca_arg.ca_str.l +1);
518 memcpy(cp, lcap->ca_arg.ca_str.s, i);
519 lcap->ca_arg.ca_str.s = cp;
520 cp[i++] = ' ';
521 memcpy(&cp[i], ncap.ca_arg.ca_str.s, ncap.ca_arg.ca_str.l +1);
522 }else{
523 struct n_cmd_arg *cap;
525 cap = salloc(sizeof *cap);
526 memcpy(cap, &ncap, sizeof ncap);
527 if(lcap == NULL)
528 cacp->cac_arg = cap;
529 else
530 lcap->ca_next = cap;
531 lcap = cap;
532 ++cacp->cac_no;
535 if(addca == TRUM1)
536 goto jleave;
539 if((shin.l > 0 || cookie != NULL) &&
540 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY)){
541 if(!greedyjoin)
542 greedyjoin = ((ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY_JOIN) &&
543 (ncap.ca_ent_flags[0] &
544 (n_CMD_ARG_DESC_STRING | n_CMD_ARG_DESC_WYSH)))
545 ? TRU1 : TRUM1;
546 goto jredo;
550 if(cad_idx < cadp->cad_no &&
551 !(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION))
552 goto jerr;
554 lcap = (struct n_cmd_arg*)-1;
555 jleave:
556 NYD_LEAVE;
557 return (lcap != NULL);
559 jerr:{
560 size_t i;
562 for(i = 0; (i < cadp->cad_no &&
563 !(cadp->cad_ent_flags[i][0] & n_CMD_ARG_DESC_OPTION)); ++i)
566 n_err(_("`%s': parsing stopped after %" PRIuZ " arguments "
567 "(need %" PRIuZ "%s)\n"
568 " Input: %.*s\n"
569 " Stopped: %.*s\n"),
570 cadp->cad_name, parsed_args, i, (i == cadp->cad_no ? n_empty : "+"),
571 (int)shin_orig.l, shin_orig.s,
572 (int)shin.l, shin.s);
574 lcap = NULL;
575 goto jleave;
578 FL void *
579 n_cmd_arg_save_to_heap(struct n_cmd_arg_ctx const *cacp){
580 struct n_cmd_arg *ncap;
581 struct n_cmd_arg_ctx *ncacp;
582 char *buf;
583 struct n_cmd_arg const *cap;
584 size_t len, i;
585 NYD2_ENTER;
587 /* For simplicity, save it all in once chunk, so that it can be thrown away
588 * with a simple n_free() from whoever is concerned */
589 len = sizeof *cacp;
590 for(cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
591 i = cap->ca_arg.ca_str.l +1;
592 i = n_ALIGN(i);
593 len += sizeof(*cap) + i;
595 if(cacp->cac_vput != NULL)
596 len += strlen(cacp->cac_vput) +1;
598 ncacp = n_alloc(len);
599 *ncacp = *cacp;
600 buf = (char*)&ncacp[1];
602 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
603 void *vp;
605 vp = buf;
606 DBG( memset(vp, 0, sizeof *ncap); )
608 if(ncap == NULL)
609 ncacp->cac_arg = vp;
610 else
611 ncap->ca_next = vp;
612 ncap = vp;
613 ncap->ca_next = NULL;
614 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
615 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
616 ncap->ca_arg_flags = cap->ca_arg_flags;
617 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
618 (ncap->ca_arg.ca_str.l = i = cap->ca_arg.ca_str.l) +1);
620 i = n_ALIGN(i);
621 buf += sizeof(*ncap) + i;
624 if(cacp->cac_vput != NULL){
625 ncacp->cac_vput = buf;
626 memcpy(buf, cacp->cac_vput, strlen(cacp->cac_vput) +1);
627 }else
628 ncacp->cac_vput = NULL;
629 NYD2_LEAVE;
630 return ncacp;
633 FL struct n_cmd_arg_ctx *
634 n_cmd_arg_restore_from_heap(void *vp){
635 struct n_cmd_arg *cap, *ncap;
636 struct n_cmd_arg_ctx *cacp, *rv;
637 NYD2_ENTER;
639 rv = n_autorec_alloc(sizeof *rv);
640 cacp = vp;
641 *rv = *cacp;
643 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
644 vp = n_autorec_alloc(sizeof(*ncap) + cap->ca_arg.ca_str.l +1);
645 DBG( memset(vp, 0, sizeof *ncap); )
647 if(ncap == NULL)
648 rv->cac_arg = vp;
649 else
650 ncap->ca_next = vp;
651 ncap = vp;
652 ncap->ca_next = NULL;
653 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
654 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
655 ncap->ca_arg_flags = cap->ca_arg_flags;
656 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
657 (ncap->ca_arg.ca_str.l = cap->ca_arg.ca_str.l) +1);
660 if(cacp->cac_vput != NULL)
661 rv->cac_vput = savestr(cacp->cac_vput);
662 NYD2_LEAVE;
663 return rv;
666 FL int
667 getrawlist(bool_t wysh, char **res_dat, size_t res_size,
668 char const *line, size_t linesize){
669 int res_no;
670 NYD_ENTER;
672 n_pstate &= ~n_PS_ARGLIST_MASK;
674 if(res_size == 0){
675 res_no = -1;
676 goto jleave;
677 }else if(UICMP(z, res_size, >, INT_MAX))
678 res_size = INT_MAX;
679 else
680 --res_size;
681 res_no = 0;
683 if(!wysh){
684 /* And assuming result won't grow input */
685 char c2, c, quotec, *cp2, *linebuf;
687 linebuf = n_lofi_alloc(linesize);
689 for(;;){
690 for(; blankchar(*line); ++line)
692 if(*line == '\0')
693 break;
695 if(UICMP(z, res_no, >=, res_size)){
696 n_err(_("Too many input tokens for result storage\n"));
697 res_no = -1;
698 break;
701 cp2 = linebuf;
702 quotec = '\0';
704 /* TODO v15: complete switch in order mirror known behaviour */
705 while((c = *line++) != '\0'){
706 if(quotec != '\0'){
707 if(c == quotec){
708 quotec = '\0';
709 continue;
710 }else if(c == '\\'){
711 if((c2 = *line++) == quotec)
712 c = c2;
713 else
714 --line;
716 }else if(c == '"' || c == '\''){
717 quotec = c;
718 continue;
719 }else if(c == '\\'){
720 if((c2 = *line++) != '\0')
721 c = c2;
722 else
723 --line;
724 }else if(blankchar(c))
725 break;
726 *cp2++ = c;
729 res_dat[res_no++] = savestrbuf(linebuf, PTR2SIZE(cp2 - linebuf));
730 if(c == '\0')
731 break;
734 n_lofi_free(linebuf);
735 }else{
736 /* sh(1) compat mode. Prepare shell token-wise */
737 struct n_string store;
738 struct str input;
739 void const *cookie;
741 n_string_creat_auto(&store);
742 input.s = n_UNCONST(line);
743 input.l = linesize;
744 cookie = NULL;
746 for(;;){
747 if(UICMP(z, res_no, >=, res_size)){
748 n_err(_("Too many input tokens for result storage\n"));
749 res_no = -1;
750 break;
753 /* C99 */{
754 enum n_shexp_state shs;
756 if((shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
757 (cookie == NULL ? n_SHEXP_PARSE_TRIM_SPACE : 0) |
758 /* TODO not here in old style n_SHEXP_PARSE_IFS_VAR |*/
759 n_SHEXP_PARSE_META_SEMICOLON),
760 &store, &input, &cookie)
761 ) & n_SHEXP_STATE_ERR_MASK){
762 /* Simply ignore Unicode error, just keep the normalized \[Uu] */
763 if((shs & n_SHEXP_STATE_ERR_MASK) != n_SHEXP_STATE_ERR_UNICODE){
764 res_no = -1;
765 break;
769 if(shs & n_SHEXP_STATE_OUTPUT){
770 if(shs & n_SHEXP_STATE_CONTROL)
771 n_pstate |= n_PS_WYSHLIST_SAW_CONTROL;
773 res_dat[res_no++] = n_string_cp(&store);
774 n_string_drop_ownership(&store);
777 if(shs & n_SHEXP_STATE_STOP)
778 break;
782 n_string_gut(&store);
785 if(res_no >= 0)
786 res_dat[(size_t)res_no] = NULL;
787 jleave:
788 NYD_LEAVE;
789 return res_no;
792 /* s-it-mode */