Introduce *version-hexnum*
[s-mailx.git] / cmd-tab.c
blobe407bbd5157fc91bb98e270af18a52cd774107d4
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 - 2017 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 if(cdp->cd_caflags & n_CMD_ARG_L)
153 rv = n_string_push_cp(rv, _(" | local modifier"));
154 if(cdp->cd_caflags & n_CMD_ARG_V)
155 rv = n_string_push_cp(rv, _(" | vput modifier"));
156 if(cdp->cd_caflags & n_CMD_ARG_EM)
157 rv = n_string_push_cp(rv, _(" | error in *!*"));
159 if(cdp->cd_caflags & n_CMD_ARG_A)
160 rv = n_string_push_cp(rv, _(" | needs box"));
161 if(cdp->cd_caflags & n_CMD_ARG_I)
162 rv = n_string_push_cp(rv, _(" | ok: batch or interactive"));
163 if(cdp->cd_caflags & n_CMD_ARG_M)
164 rv = n_string_push_cp(rv, _(" | ok: send mode"));
165 if(cdp->cd_caflags & n_CMD_ARG_R)
166 rv = n_string_push_cp(rv, _(" | not ok: compose mode"));
167 if(cdp->cd_caflags & n_CMD_ARG_S)
168 rv = n_string_push_cp(rv, _(" | not ok: during startup"));
169 if(cdp->cd_caflags & n_CMD_ARG_X)
170 rv = n_string_push_cp(rv, _(" | ok: subprocess"));
172 if(cdp->cd_caflags & n_CMD_ARG_G)
173 rv = n_string_push_cp(rv, _(" | gabby history"));
175 cp = n_string_cp(rv);
176 NYD2_LEAVE;
177 return cp;
179 #endif /* HAVE_DOCSTRINGS */
181 static int
182 a_ctab_c_list(void *vp){
183 FILE *fp;
184 struct n_cmd_desc const **cdpa, *cdp, **cdpa_curr;
185 size_t i, l, scrwid;
186 NYD_ENTER;
188 i = n_NELEM(a_ctab_ctable) + n_NELEM(a_ctab_ctable_plus) +1;
189 cdpa = n_autorec_alloc(sizeof(cdp) * i);
191 for(i = 0; i < n_NELEM(a_ctab_ctable); ++i)
192 cdpa[i] = &a_ctab_ctable[i];
193 for(l = 0; l < n_NELEM(a_ctab_ctable_plus); ++i, ++l)
194 cdpa[i] = &a_ctab_ctable_plus[l];
195 cdpa[i] = NULL;
197 if(*(void**)vp == NULL)
198 qsort(cdpa, i, sizeof(*cdpa), &a_ctab__pcmd_cmp);
200 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
201 fp = n_stdout;
203 scrwid = n_SCRNWIDTH_FOR_LISTS;
205 fprintf(fp, _("Commands are:\n"));
206 l = 1;
207 for(i = 0, cdpa_curr = cdpa; (cdp = *cdpa_curr++) != NULL;){
208 char const *pre, *suf;
210 if(cdp->cd_func == NULL)
211 pre = "[", suf = "]";
212 else
213 pre = suf = n_empty;
215 #ifdef HAVE_DOCSTRINGS
216 if(n_poption & n_PO_D_V){
217 fprintf(fp, "%s%s%s\n", pre, cdp->cd_name, suf);
218 ++l;
219 fprintf(fp, " : %s\n", V_(cdp->cd_doc));
220 ++l;
221 fprintf(fp, " : %s\n", a_ctab_cmdinfo(cdp));
222 ++l;
223 }else
224 #endif
226 size_t j;
228 j = strlen(cdp->cd_name);
229 if(*pre != '\0')
230 j += 2;
232 if((i += j + 2) > scrwid){
233 i = j;
234 fprintf(fp, "\n");
235 ++l;
237 fprintf(fp, (*cdpa_curr != NULL ? "%s%s%s, " : "%s%s%s\n"),
238 pre, cdp->cd_name, suf);
242 if(fp != n_stdout){
243 page_or_print(fp, l);
244 Fclose(fp);
246 NYD_LEAVE;
247 return 0;
250 static int
251 a_ctab__pcmd_cmp(void const *s1, void const *s2){
252 struct n_cmd_desc const * const *cdpa1, * const *cdpa2;
253 int rv;
254 NYD2_ENTER;
256 cdpa1 = s1;
257 cdpa2 = s2;
258 rv = strcmp((*cdpa1)->cd_name, (*cdpa2)->cd_name);
259 NYD2_LEAVE;
260 return rv;
263 static int
264 a_ctab_c_help(void *vp){
265 int rv;
266 char const *arg;
267 NYD_ENTER;
269 /* Help for a single command? */
270 if((arg = *(char const**)vp) != NULL){
271 struct n_cmd_desc const *cdp, *cdp_max;
272 struct str const *alias_exp;
273 char const *alias_name;
275 /* Aliases take precedence */
276 if((alias_name = n_commandalias_exists(arg, &alias_exp)) != NULL){
277 fprintf(n_stdout, "%s -> ", arg);
278 arg = alias_exp->s;
281 cdp_max = &(cdp = a_ctab_ctable)[n_NELEM(a_ctab_ctable)];
282 jredo:
283 for(; cdp < cdp_max; ++cdp){
284 if(is_prefix(arg, cdp->cd_name)){
285 fputs(arg, n_stdout);
286 if(strcmp(arg, cdp->cd_name))
287 fprintf(n_stdout, " (%s)", cdp->cd_name);
288 }else
289 continue;
291 #ifdef HAVE_DOCSTRINGS
292 fprintf(n_stdout, ": %s", V_(cdp->cd_doc));
293 if(n_poption & n_PO_D_V)
294 fprintf(n_stdout, "\n : %s", a_ctab_cmdinfo(cdp));
295 #endif
296 putc('\n', n_stdout);
297 rv = 0;
298 goto jleave;
301 if(cdp_max == &a_ctab_ctable[n_NELEM(a_ctab_ctable)]){
302 cdp_max = &(cdp =
303 a_ctab_ctable_plus)[n_NELEM(a_ctab_ctable_plus)];
304 goto jredo;
307 if(alias_name != NULL){
308 fprintf(n_stdout, "%s\n", n_shexp_quote_cp(arg, TRU1));
309 rv = 0;
310 }else{
311 n_err(_("Unknown command: `%s'\n"), arg);
312 rv = 1;
314 }else{
315 /* Very ugly, but take care for compiler supported string lengths :( */
316 fputs(n_progname, n_stdout);
317 fputs(_(
318 " commands -- <msglist> denotes message specifications,\n"
319 "e.g., 1-5, :n or ., separated by spaces:\n"), n_stdout);
320 fputs(_(
321 "\n"
322 "type <msglist> type (`print') messages (honour `headerpick' etc.)\n"
323 "Type <msglist> like `type' but always show all headers\n"
324 "next goto and type next message\n"
325 "from <msglist> (search and) print header summary for the given list\n"
326 "headers header summary for messages surrounding \"dot\"\n"
327 "delete <msglist> delete messages (can be `undelete'd)\n"),
328 n_stdout);
330 fputs(_(
331 "\n"
332 "save <msglist> folder append messages to folder and mark as saved\n"
333 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
334 "write <msglist> file write message contents to file (prompts for parts)\n"
335 "Reply <msglist> reply to message senders only\n"
336 "reply <msglist> like `Reply', but address all recipients\n"
337 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
338 n_stdout);
340 fputs(_(
341 "\n"
342 "mail <recipients> compose a mail for the given recipients\n"
343 "file folder change to another mailbox\n"
344 "File folder like `file', but open readonly\n"
345 "quit quit and apply changes to the current mailbox\n"
346 "xit or exit like `quit', but discard changes\n"
347 "!shell command shell escape\n"
348 "list [<anything>] all available commands [in search order]\n"),
349 n_stdout);
351 rv = (ferror(n_stdout) != 0);
353 jleave:
354 NYD_LEAVE;
355 return rv;
358 FL char const *
359 n_cmd_isolate(char const *cmd){
360 NYD2_ENTER;
361 while(*cmd != '\0' &&
362 strchr("\\!~|? \t0123456789&%@$^.:/-+*'\",;(`", *cmd) == NULL)
363 ++cmd;
364 NYD2_LEAVE;
365 return n_UNCONST(cmd);
368 FL struct n_cmd_desc const *
369 n_cmd_firstfit(char const *cmd){ /* TODO *hashtable*! linear list search!!! */
370 struct n_cmd_desc const *cdp;
371 NYD2_ENTER;
373 for(cdp = a_ctab_ctable; cdp < &a_ctab_ctable[n_NELEM(a_ctab_ctable)]; ++cdp)
374 if(*cmd == *cdp->cd_name && cdp->cd_func != NULL &&
375 is_prefix(cmd, cdp->cd_name))
376 goto jleave;
377 cdp = NULL;
378 jleave:
379 NYD2_LEAVE;
380 return cdp;
383 FL struct n_cmd_desc const *
384 n_cmd_default(void){
385 struct n_cmd_desc const *cdp;
386 NYD2_ENTER;
388 cdp = &a_ctab_ctable[0];
389 NYD2_LEAVE;
390 return cdp;
393 FL bool_t
394 n_cmd_arg_parse(struct n_cmd_arg_ctx *cacp){
395 struct n_cmd_arg ncap, *lcap;
396 struct str shin_orig, shin;
397 bool_t addca, greedyjoin;
398 void const *cookie;
399 size_t cad_idx, parsed_args;
400 struct n_cmd_arg_desc const *cadp;
401 NYD_ENTER;
403 assert(cacp->cac_inlen == 0 || cacp->cac_indat != NULL);
404 assert(cacp->cac_desc->cad_no > 0);
405 #ifdef HAVE_DEBUG
406 /* C99 */{
407 bool_t opt_seen = FAL0;
409 for(cadp = cacp->cac_desc, cad_idx = 0;
410 cad_idx < cadp->cad_no; ++cad_idx){
411 assert(cadp->cad_ent_flags[cad_idx][0] & n__CMD_ARG_DESC_TYPE_MASK);
412 assert(!opt_seen ||
413 (cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION));
414 if(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION)
415 opt_seen = TRU1;
416 assert(!(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_GREEDY) ||
417 cad_idx + 1 == cadp->cad_no);
420 #endif
422 shin.s = n_UNCONST(cacp->cac_indat); /* "logical" only */
423 shin.l = (cacp->cac_inlen == UIZ_MAX ? strlen(shin.s) : cacp->cac_inlen);
424 shin_orig = shin;
425 cacp->cac_no = 0;
426 cacp->cac_arg = lcap = NULL;
428 cookie = NULL;
429 parsed_args = 0;
430 greedyjoin = FAL0;
432 for(cadp = cacp->cac_desc, cad_idx = 0; shin.l > 0 && cad_idx < cadp->cad_no;
433 ++cad_idx){
434 jredo:
435 memset(&ncap, 0, sizeof ncap);
436 ncap.ca_indat = shin.s;
437 /* >ca_inline once we know */
438 memcpy(&ncap.ca_ent_flags[0], &cadp->cad_ent_flags[cad_idx][0],
439 sizeof ncap.ca_ent_flags);
440 addca = FAL0;
442 switch(ncap.ca_ent_flags[0] & n__CMD_ARG_DESC_TYPE_MASK){
443 case n_CMD_ARG_DESC_STRING:{ /* TODO \ escaping? additional type!? */
444 char /*const*/ *cp = shin.s;
445 size_t i = shin.l;
447 while(i > 0 && blankspacechar(*cp))
448 ++cp, --i;
450 ncap.ca_arg.ca_str.s = cp;
451 while(i > 0 && !blankspacechar(*cp))
452 ++cp, --i;
453 ncap.ca_arg.ca_str.s = savestrbuf(ncap.ca_arg.ca_str.s,
454 ncap.ca_arg.ca_str.l = PTR2SIZE(cp - ncap.ca_arg.ca_str.s));
456 while(i > 0 && blankspacechar(*cp))
457 ++cp, --i;
458 ncap.ca_inlen = PTR2SIZE(cp - ncap.ca_indat);
459 shin.s = cp;
460 shin.l = i;
461 addca = TRU1;
462 }break;
463 default:
464 case n_CMD_ARG_DESC_WYSH:{
465 struct n_string shou, *shoup;
466 enum n_shexp_state shs;
467 ui32_t addflags;
469 if(cad_idx == cadp->cad_no - 1 ||
470 (cadp->cad_ent_flags[cad_idx + 1][0] & n_CMD_ARG_DESC_OPTION))
471 addflags = n_SHEXP_PARSE_META_SEMICOLON;
472 else
473 addflags = n_SHEXP_PARSE_NONE;
475 shoup = n_string_creat_auto(&shou);
476 ncap.ca_arg_flags =
477 shs = n_shexp_parse_token((ncap.ca_ent_flags[1] | addflags |
478 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_LOG),
479 shoup, &shin,
480 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY ? &cookie : NULL));
481 ncap.ca_inlen = PTR2SIZE(shin.s - ncap.ca_indat);
482 if((shs & (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)) ==
483 n_SHEXP_STATE_OUTPUT){
484 if((shs & n_SHEXP_STATE_META_SEMICOLON) && shou.s_len == 0)
485 break;
486 ncap.ca_arg.ca_str.s = n_string_cp(shoup);
487 ncap.ca_arg.ca_str.l = shou.s_len;
488 shoup = n_string_drop_ownership(shoup);
490 n_string_gut(shoup);
492 if(shs & n_SHEXP_STATE_ERR_MASK)
493 goto jerr;
494 if((shs & n_SHEXP_STATE_STOP) &&
495 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_HONOUR_STOP)){
496 if(!(shs & n_SHEXP_STATE_OUTPUT))
497 goto jleave;
498 addca = TRUM1;
499 }else
500 addca = TRU1;
501 }break;
503 ++parsed_args;
505 if(addca){
506 if(greedyjoin == TRU1){ /* TODO speed this up! */
507 char *cp;
508 size_t i;
510 assert(lcap != NULL);
511 i = lcap->ca_arg.ca_str.l;
512 lcap->ca_arg.ca_str.l += 1 + ncap.ca_arg.ca_str.l;
513 cp = salloc(lcap->ca_arg.ca_str.l +1);
514 memcpy(cp, lcap->ca_arg.ca_str.s, i);
515 lcap->ca_arg.ca_str.s = cp;
516 cp[i++] = ' ';
517 memcpy(&cp[i], ncap.ca_arg.ca_str.s, ncap.ca_arg.ca_str.l +1);
518 }else{
519 struct n_cmd_arg *cap;
521 cap = salloc(sizeof *cap);
522 memcpy(cap, &ncap, sizeof ncap);
523 if(lcap == NULL)
524 cacp->cac_arg = cap;
525 else
526 lcap->ca_next = cap;
527 lcap = cap;
528 ++cacp->cac_no;
531 if(addca == TRUM1)
532 goto jleave;
535 if((shin.l > 0 || cookie != NULL) &&
536 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY)){
537 if(!greedyjoin)
538 greedyjoin = ((ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY_JOIN) &&
539 (ncap.ca_ent_flags[0] &
540 (n_CMD_ARG_DESC_STRING | n_CMD_ARG_DESC_WYSH)))
541 ? TRU1 : TRUM1;
542 goto jredo;
546 if(cad_idx < cadp->cad_no &&
547 !(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION))
548 goto jerr;
550 lcap = (struct n_cmd_arg*)-1;
551 jleave:
552 NYD_LEAVE;
553 return (lcap != NULL);
555 jerr:{
556 size_t i;
558 for(i = 0; (i < cadp->cad_no &&
559 !(cadp->cad_ent_flags[i][0] & n_CMD_ARG_DESC_OPTION)); ++i)
562 n_err(_("`%s': parsing stopped after %" PRIuZ " arguments "
563 "(need %" PRIuZ "%s)\n"
564 " Input: %.*s\n"
565 " Stopped: %.*s\n"),
566 cadp->cad_name, parsed_args, i, (i == cadp->cad_no ? n_empty : "+"),
567 (int)shin_orig.l, shin_orig.s,
568 (int)shin.l, shin.s);
570 lcap = NULL;
571 goto jleave;
574 FL void *
575 n_cmd_arg_save_to_heap(struct n_cmd_arg_ctx const *cacp){
576 struct n_cmd_arg *ncap;
577 struct n_cmd_arg_ctx *ncacp;
578 char *buf;
579 struct n_cmd_arg const *cap;
580 size_t len, i;
581 NYD2_ENTER;
583 /* For simplicity, save it all in once chunk, so that it can be thrown away
584 * with a simple n_free() from whoever is concerned */
585 len = sizeof *cacp;
586 for(cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
587 i = cap->ca_arg.ca_str.l +1;
588 i = n_ALIGN(i);
589 len += sizeof(*cap) + i;
591 if(cacp->cac_vput != NULL)
592 len += strlen(cacp->cac_vput) +1;
594 ncacp = n_alloc(len);
595 *ncacp = *cacp;
596 buf = (char*)&ncacp[1];
598 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
599 void *vp;
601 vp = buf;
602 DBG( memset(vp, 0, sizeof *ncap); )
604 if(ncap == NULL)
605 ncacp->cac_arg = vp;
606 else
607 ncap->ca_next = vp;
608 ncap = vp;
609 ncap->ca_next = NULL;
610 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
611 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
612 ncap->ca_arg_flags = cap->ca_arg_flags;
613 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
614 (ncap->ca_arg.ca_str.l = i = cap->ca_arg.ca_str.l) +1);
616 i = n_ALIGN(i);
617 buf += sizeof(*ncap) + i;
620 if(cacp->cac_vput != NULL){
621 ncacp->cac_vput = buf;
622 memcpy(buf, cacp->cac_vput, strlen(cacp->cac_vput) +1);
623 }else
624 ncacp->cac_vput = NULL;
625 NYD2_LEAVE;
626 return ncacp;
629 FL struct n_cmd_arg_ctx *
630 n_cmd_arg_restore_from_heap(void *vp){
631 struct n_cmd_arg *cap, *ncap;
632 struct n_cmd_arg_ctx *cacp, *rv;
633 NYD2_ENTER;
635 rv = n_autorec_alloc(sizeof *rv);
636 cacp = vp;
637 *rv = *cacp;
639 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
640 vp = n_autorec_alloc(sizeof(*ncap) + cap->ca_arg.ca_str.l +1);
641 DBG( memset(vp, 0, sizeof *ncap); )
643 if(ncap == NULL)
644 rv->cac_arg = vp;
645 else
646 ncap->ca_next = vp;
647 ncap = vp;
648 ncap->ca_next = NULL;
649 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
650 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
651 ncap->ca_arg_flags = cap->ca_arg_flags;
652 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
653 (ncap->ca_arg.ca_str.l = cap->ca_arg.ca_str.l) +1);
656 if(cacp->cac_vput != NULL)
657 rv->cac_vput = savestr(cacp->cac_vput);
658 NYD2_LEAVE;
659 return rv;
662 FL int
663 getrawlist(bool_t wysh, char **res_dat, size_t res_size,
664 char const *line, size_t linesize){
665 int res_no;
666 NYD_ENTER;
668 n_pstate &= ~n_PS_ARGLIST_MASK;
670 if(res_size == 0){
671 res_no = -1;
672 goto jleave;
673 }else if(UICMP(z, res_size, >, INT_MAX))
674 res_size = INT_MAX;
675 else
676 --res_size;
677 res_no = 0;
679 if(!wysh){
680 /* And assuming result won't grow input */
681 char c2, c, quotec, *cp2, *linebuf;
683 linebuf = n_lofi_alloc(linesize);
685 for(;;){
686 for(; blankchar(*line); ++line)
688 if(*line == '\0')
689 break;
691 if(UICMP(z, res_no, >=, res_size)){
692 n_err(_("Too many input tokens for result storage\n"));
693 res_no = -1;
694 break;
697 cp2 = linebuf;
698 quotec = '\0';
700 /* TODO v15: complete switch in order mirror known behaviour */
701 while((c = *line++) != '\0'){
702 if(quotec != '\0'){
703 if(c == quotec){
704 quotec = '\0';
705 continue;
706 }else if(c == '\\'){
707 if((c2 = *line++) == quotec)
708 c = c2;
709 else
710 --line;
712 }else if(c == '"' || c == '\''){
713 quotec = c;
714 continue;
715 }else if(c == '\\'){
716 if((c2 = *line++) != '\0')
717 c = c2;
718 else
719 --line;
720 }else if(blankchar(c))
721 break;
722 *cp2++ = c;
725 res_dat[res_no++] = savestrbuf(linebuf, PTR2SIZE(cp2 - linebuf));
726 if(c == '\0')
727 break;
730 n_lofi_free(linebuf);
731 }else{
732 /* sh(1) compat mode. Prepare shell token-wise */
733 struct n_string store;
734 struct str input;
735 void const *cookie;
737 n_string_creat_auto(&store);
738 input.s = n_UNCONST(line);
739 input.l = linesize;
740 cookie = NULL;
742 for(;;){
743 if(UICMP(z, res_no, >=, res_size)){
744 n_err(_("Too many input tokens for result storage\n"));
745 res_no = -1;
746 break;
749 /* C99 */{
750 enum n_shexp_state shs;
752 if((shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
753 (cookie == NULL ? n_SHEXP_PARSE_TRIM_SPACE : 0) |
754 /* TODO not here in old style n_SHEXP_PARSE_IFS_VAR |*/
755 n_SHEXP_PARSE_META_SEMICOLON),
756 &store, &input, &cookie)
757 ) & n_SHEXP_STATE_ERR_MASK){
758 /* Simply ignore Unicode error, just keep the normalized \[Uu] */
759 if((shs & n_SHEXP_STATE_ERR_MASK) != n_SHEXP_STATE_ERR_UNICODE){
760 res_no = -1;
761 break;
765 if(shs & n_SHEXP_STATE_OUTPUT){
766 if(shs & n_SHEXP_STATE_CONTROL)
767 n_pstate |= n_PS_WYSHLIST_SAW_CONTROL;
769 res_dat[res_no++] = n_string_cp(&store);
770 n_string_drop_ownership(&store);
773 if(shs & n_SHEXP_STATE_STOP)
774 break;
778 n_string_gut(&store);
781 if(res_no >= 0)
782 res_dat[(size_t)res_no] = NULL;
783 jleave:
784 NYD_LEAVE;
785 return res_no;
788 /* s-it-mode */