strings.c: tweak various old char* functions
[s-mailx.git] / cmd-tab.c
blob5d8a55bdfdbfa0377ce3cb14fc11991947f798a8
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_V)
153 rv = n_string_push_cp(rv, _(" | vput modifier"));
154 if(cdp->cd_caflags & n_CMD_ARG_EM)
155 rv = n_string_push_cp(rv, _(" | error in *!*"));
157 if(cdp->cd_caflags & n_CMD_ARG_A)
158 rv = n_string_push_cp(rv, _(" | needs box"));
159 if(cdp->cd_caflags & n_CMD_ARG_I)
160 rv = n_string_push_cp(rv, _(" | ok: batch or interactive"));
161 if(cdp->cd_caflags & n_CMD_ARG_M)
162 rv = n_string_push_cp(rv, _(" | ok: send mode"));
163 if(cdp->cd_caflags & n_CMD_ARG_R)
164 rv = n_string_push_cp(rv, _(" | not ok: compose mode"));
165 if(cdp->cd_caflags & n_CMD_ARG_S)
166 rv = n_string_push_cp(rv, _(" | not ok: during startup"));
167 if(cdp->cd_caflags & n_CMD_ARG_X)
168 rv = n_string_push_cp(rv, _(" | ok: subprocess"));
170 if(cdp->cd_caflags & n_CMD_ARG_G)
171 rv = n_string_push_cp(rv, _(" | gabby history"));
173 cp = n_string_cp(rv);
174 NYD2_LEAVE;
175 return cp;
177 #endif /* HAVE_DOCSTRINGS */
179 static int
180 a_ctab_c_list(void *vp){
181 FILE *fp;
182 struct n_cmd_desc const **cdpa, *cdp, **cdpa_curr;
183 size_t i, l, scrwid;
184 NYD_ENTER;
186 i = n_NELEM(a_ctab_ctable) + n_NELEM(a_ctab_ctable_plus) +1;
187 cdpa = n_autorec_alloc(sizeof(cdp) * i);
189 for(i = 0; i < n_NELEM(a_ctab_ctable); ++i)
190 cdpa[i] = &a_ctab_ctable[i];
191 for(l = 0; l < n_NELEM(a_ctab_ctable_plus); ++i, ++l)
192 cdpa[i] = &a_ctab_ctable_plus[l];
193 cdpa[i] = NULL;
195 if(*(void**)vp == NULL)
196 qsort(cdpa, i, sizeof(*cdpa), &a_ctab__pcmd_cmp);
198 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
199 fp = n_stdout;
201 scrwid = n_SCRNWIDTH_FOR_LISTS;
203 fprintf(fp, _("Commands are:\n"));
204 l = 1;
205 for(i = 0, cdpa_curr = cdpa; (cdp = *cdpa_curr++) != NULL;){
206 char const *pre, *suf;
208 if(cdp->cd_func == NULL)
209 pre = "[", suf = "]";
210 else
211 pre = suf = n_empty;
213 #ifdef HAVE_DOCSTRINGS
214 if(n_poption & n_PO_D_V){
215 fprintf(fp, "%s%s%s\n", pre, cdp->cd_name, suf);
216 ++l;
217 fprintf(fp, " : %s\n", V_(cdp->cd_doc));
218 ++l;
219 fprintf(fp, " : %s\n", a_ctab_cmdinfo(cdp));
220 ++l;
221 }else
222 #endif
224 size_t j;
226 j = strlen(cdp->cd_name);
227 if(*pre != '\0')
228 j += 2;
230 if((i += j + 2) > scrwid){
231 i = j;
232 fprintf(fp, "\n");
233 ++l;
235 fprintf(fp, (*cdpa_curr != NULL ? "%s%s%s, " : "%s%s%s\n"),
236 pre, cdp->cd_name, suf);
240 if(fp != n_stdout){
241 page_or_print(fp, l);
242 Fclose(fp);
244 NYD_LEAVE;
245 return 0;
248 static int
249 a_ctab__pcmd_cmp(void const *s1, void const *s2){
250 struct n_cmd_desc const * const *cdpa1, * const *cdpa2;
251 int rv;
252 NYD2_ENTER;
254 cdpa1 = s1;
255 cdpa2 = s2;
256 rv = strcmp((*cdpa1)->cd_name, (*cdpa2)->cd_name);
257 NYD2_LEAVE;
258 return rv;
261 static int
262 a_ctab_c_help(void *vp){
263 int rv;
264 char const *arg;
265 NYD_ENTER;
267 /* Help for a single command? */
268 if((arg = *(char const**)vp) != NULL){
269 struct n_cmd_desc const *cdp, *cdp_max;
270 struct str const *alias_exp;
271 char const *alias_name;
273 /* Aliases take precedence */
274 if((alias_name = n_commandalias_exists(arg, &alias_exp)) != NULL){
275 fprintf(n_stdout, "%s -> ", arg);
276 arg = alias_exp->s;
279 cdp_max = &(cdp = a_ctab_ctable)[n_NELEM(a_ctab_ctable)];
280 jredo:
281 for(; cdp < cdp_max; ++cdp){
282 if(is_prefix(arg, cdp->cd_name)){
283 fputs(arg, n_stdout);
284 if(strcmp(arg, cdp->cd_name))
285 fprintf(n_stdout, " (%s)", cdp->cd_name);
286 }else
287 continue;
289 #ifdef HAVE_DOCSTRINGS
290 fprintf(n_stdout, ": %s", V_(cdp->cd_doc));
291 if(n_poption & n_PO_D_V)
292 fprintf(n_stdout, "\n : %s", a_ctab_cmdinfo(cdp));
293 #endif
294 putc('\n', n_stdout);
295 rv = 0;
296 goto jleave;
299 if(cdp_max == &a_ctab_ctable[n_NELEM(a_ctab_ctable)]){
300 cdp_max = &(cdp =
301 a_ctab_ctable_plus)[n_NELEM(a_ctab_ctable_plus)];
302 goto jredo;
305 if(alias_name != NULL){
306 fprintf(n_stdout, "%s\n", n_shexp_quote_cp(arg, TRU1));
307 rv = 0;
308 }else{
309 n_err(_("Unknown command: `%s'\n"), arg);
310 rv = 1;
312 }else{
313 /* Very ugly, but take care for compiler supported string lengths :( */
314 fputs(n_progname, n_stdout);
315 fputs(_(
316 " commands -- <msglist> denotes message specifications,\n"
317 "e.g., 1-5, :n or ., separated by spaces:\n"), n_stdout);
318 fputs(_(
319 "\n"
320 "type <msglist> type (`print') messages (honour `headerpick' etc.)\n"
321 "Type <msglist> like `type' but always show all headers\n"
322 "next goto and type next message\n"
323 "from <msglist> (search and) print header summary for the given list\n"
324 "headers header summary for messages surrounding \"dot\"\n"
325 "delete <msglist> delete messages (can be `undelete'd)\n"),
326 n_stdout);
328 fputs(_(
329 "\n"
330 "save <msglist> folder append messages to folder and mark as saved\n"
331 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
332 "write <msglist> file write message contents to file (prompts for parts)\n"
333 "Reply <msglist> reply to message senders only\n"
334 "reply <msglist> like `Reply', but address all recipients\n"
335 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
336 n_stdout);
338 fputs(_(
339 "\n"
340 "mail <recipients> compose a mail for the given recipients\n"
341 "file folder change to another mailbox\n"
342 "File folder like `file', but open readonly\n"
343 "quit quit and apply changes to the current mailbox\n"
344 "xit or exit like `quit', but discard changes\n"
345 "!shell command shell escape\n"
346 "list [<anything>] all available commands [in search order]\n"),
347 n_stdout);
349 rv = (ferror(n_stdout) != 0);
351 jleave:
352 NYD_LEAVE;
353 return rv;
356 FL char const *
357 n_cmd_isolate(char const *cmd){
358 NYD2_ENTER;
359 while(*cmd != '\0' &&
360 strchr("\\!~|? \t0123456789&%@$^.:/-+*'\",;(`", *cmd) == NULL)
361 ++cmd;
362 NYD2_LEAVE;
363 return n_UNCONST(cmd);
366 FL struct n_cmd_desc const *
367 n_cmd_firstfit(char const *cmd){ /* TODO *hashtable*! linear list search!!! */
368 struct n_cmd_desc const *cdp;
369 NYD2_ENTER;
371 for(cdp = a_ctab_ctable; cdp < &a_ctab_ctable[n_NELEM(a_ctab_ctable)]; ++cdp)
372 if(*cmd == *cdp->cd_name && cdp->cd_func != NULL &&
373 is_prefix(cmd, cdp->cd_name))
374 goto jleave;
375 cdp = NULL;
376 jleave:
377 NYD2_LEAVE;
378 return cdp;
381 FL struct n_cmd_desc const *
382 n_cmd_default(void){
383 struct n_cmd_desc const *cdp;
384 NYD2_ENTER;
386 cdp = &a_ctab_ctable[0];
387 NYD2_LEAVE;
388 return cdp;
391 FL bool_t
392 n_cmd_arg_parse(struct n_cmd_arg_ctx *cacp){
393 struct n_cmd_arg ncap, *lcap;
394 struct str shin_orig, shin;
395 bool_t addca, greedyjoin;
396 void const *cookie;
397 size_t cad_idx, parsed_args;
398 struct n_cmd_arg_desc const *cadp;
399 NYD_ENTER;
401 assert(cacp->cac_inlen == 0 || cacp->cac_indat != NULL);
402 assert(cacp->cac_desc->cad_no > 0);
403 #ifdef HAVE_DEBUG
404 /* C99 */{
405 bool_t opt_seen = FAL0;
407 for(cadp = cacp->cac_desc, cad_idx = 0;
408 cad_idx < cadp->cad_no; ++cad_idx){
409 assert(cadp->cad_ent_flags[cad_idx][0] & n__CMD_ARG_DESC_TYPE_MASK);
410 assert(!opt_seen ||
411 (cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION));
412 if(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION)
413 opt_seen = TRU1;
414 assert(!(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_GREEDY) ||
415 cad_idx + 1 == cadp->cad_no);
418 #endif
420 shin.s = n_UNCONST(cacp->cac_indat); /* "logical" only */
421 shin.l = (cacp->cac_inlen == UIZ_MAX ? strlen(shin.s) : cacp->cac_inlen);
422 shin_orig = shin;
423 cacp->cac_no = 0;
424 cacp->cac_arg = lcap = NULL;
426 cookie = NULL;
427 parsed_args = 0;
428 greedyjoin = FAL0;
430 for(cadp = cacp->cac_desc, cad_idx = 0; shin.l > 0 && cad_idx < cadp->cad_no;
431 ++cad_idx){
432 jredo:
433 memset(&ncap, 0, sizeof ncap);
434 ncap.ca_indat = shin.s;
435 /* >ca_inline once we know */
436 memcpy(&ncap.ca_ent_flags[0], &cadp->cad_ent_flags[cad_idx][0],
437 sizeof ncap.ca_ent_flags);
438 addca = FAL0;
440 switch(ncap.ca_ent_flags[0] & n__CMD_ARG_DESC_TYPE_MASK){
441 case n_CMD_ARG_DESC_STRING:{ /* TODO \ escaping? additional type!? */
442 char /*const*/ *cp = shin.s;
443 size_t i = shin.l;
445 while(i > 0 && blankspacechar(*cp))
446 ++cp, --i;
448 ncap.ca_arg.ca_str.s = cp;
449 while(i > 0 && !blankspacechar(*cp))
450 ++cp, --i;
451 ncap.ca_arg.ca_str.s = savestrbuf(ncap.ca_arg.ca_str.s,
452 ncap.ca_arg.ca_str.l = PTR2SIZE(cp - ncap.ca_arg.ca_str.s));
454 while(i > 0 && blankspacechar(*cp))
455 ++cp, --i;
456 ncap.ca_inlen = PTR2SIZE(cp - ncap.ca_indat);
457 shin.s = cp;
458 shin.l = i;
459 addca = TRU1;
460 } break;
461 default:
462 case n_CMD_ARG_DESC_WYSH:{
463 struct n_string shou, *shoup;
464 enum n_shexp_state shs;
465 ui32_t addflags;
467 if(cad_idx == cadp->cad_no - 1 ||
468 (cadp->cad_ent_flags[cad_idx + 1][0] & n_CMD_ARG_DESC_OPTION))
469 addflags = n_SHEXP_PARSE_META_SEMICOLON;
470 else
471 addflags = n_SHEXP_PARSE_NONE;
473 shoup = n_string_creat_auto(&shou);
474 ncap.ca_arg_flags =
475 shs = n_shexp_parse_token((ncap.ca_ent_flags[1] | addflags |
476 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_LOG),
477 shoup, &shin,
478 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY ? &cookie : NULL));
479 ncap.ca_inlen = PTR2SIZE(shin.s - ncap.ca_indat);
480 if((shs & (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)) ==
481 n_SHEXP_STATE_OUTPUT){
482 if((shs & n_SHEXP_STATE_META_SEMICOLON) && shou.s_len == 0)
483 break;
484 ncap.ca_arg.ca_str.s = n_string_cp(shoup);
485 ncap.ca_arg.ca_str.l = shou.s_len;
486 shoup = n_string_drop_ownership(shoup);
488 n_string_gut(shoup);
490 if(shs & n_SHEXP_STATE_ERR_MASK)
491 goto jerr;
492 if((shs & n_SHEXP_STATE_STOP) &&
493 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_HONOUR_STOP)){
494 if(!(shs & n_SHEXP_STATE_OUTPUT))
495 goto jleave;
496 addca = TRUM1;
497 }else
498 addca = TRU1;
499 } break;
501 ++parsed_args;
503 if(addca){
504 if(greedyjoin == TRU1){ /* TODO speed this up! */
505 char *cp;
506 size_t i;
508 assert(lcap != NULL);
509 i = lcap->ca_arg.ca_str.l;
510 lcap->ca_arg.ca_str.l += 1 + ncap.ca_arg.ca_str.l;
511 cp = salloc(lcap->ca_arg.ca_str.l +1);
512 memcpy(cp, lcap->ca_arg.ca_str.s, i);
513 lcap->ca_arg.ca_str.s = cp;
514 cp[i++] = ' ';
515 memcpy(&cp[i], ncap.ca_arg.ca_str.s, ncap.ca_arg.ca_str.l +1);
516 }else{
517 struct n_cmd_arg *cap;
519 cap = salloc(sizeof *cap);
520 memcpy(cap, &ncap, sizeof ncap);
521 if(lcap == NULL)
522 cacp->cac_arg = cap;
523 else
524 lcap->ca_next = cap;
525 lcap = cap;
526 ++cacp->cac_no;
529 if(addca == TRUM1)
530 goto jleave;
533 if((shin.l > 0 || cookie != NULL) &&
534 (ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY)){
535 if(!greedyjoin)
536 greedyjoin = ((ncap.ca_ent_flags[0] & n_CMD_ARG_DESC_GREEDY_JOIN) &&
537 (ncap.ca_ent_flags[0] &
538 (n_CMD_ARG_DESC_STRING | n_CMD_ARG_DESC_WYSH)))
539 ? TRU1 : TRUM1;
540 goto jredo;
544 if(cad_idx < cadp->cad_no &&
545 !(cadp->cad_ent_flags[cad_idx][0] & n_CMD_ARG_DESC_OPTION))
546 goto jerr;
548 lcap = (struct n_cmd_arg*)-1;
549 jleave:
550 NYD_LEAVE;
551 return (lcap != NULL);
553 jerr:{
554 size_t i;
556 for(i = 0; (i < cadp->cad_no &&
557 !(cadp->cad_ent_flags[i][0] & n_CMD_ARG_DESC_OPTION)); ++i)
560 n_err(_("`%s': parsing stopped after %" PRIuZ " arguments "
561 "(need %" PRIuZ "%s)\n"
562 " Input: %.*s\n"
563 " Stopped: %.*s\n"),
564 cadp->cad_name, parsed_args, i, (i == cadp->cad_no ? n_empty : "+"),
565 (int)shin_orig.l, shin_orig.s,
566 (int)shin.l, shin.s);
568 lcap = NULL;
569 goto jleave;
572 FL void *
573 n_cmd_arg_save_to_heap(struct n_cmd_arg_ctx const *cacp){
574 struct n_cmd_arg *ncap;
575 struct n_cmd_arg_ctx *ncacp;
576 char *buf;
577 struct n_cmd_arg const *cap;
578 size_t len, i;
579 NYD2_ENTER;
581 /* For simplicity, save it all in once chunk, so that it can be thrown away
582 * with a simple n_free() from whoever is concerned */
583 len = sizeof *cacp;
584 for(cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
585 i = cap->ca_arg.ca_str.l +1;
586 i = n_ALIGN(i);
587 len += sizeof(*cap) + i;
589 if(cacp->cac_vput != NULL)
590 len += strlen(cacp->cac_vput) +1;
592 ncacp = n_alloc(len);
593 *ncacp = *cacp;
594 buf = (char*)&ncacp[1];
596 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
597 void *vp;
599 vp = buf;
600 DBG( memset(vp, 0, sizeof *ncap); )
602 if(ncap == NULL)
603 ncacp->cac_arg = vp;
604 else
605 ncap->ca_next = vp;
606 ncap = vp;
607 ncap->ca_next = NULL;
608 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
609 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
610 ncap->ca_arg_flags = cap->ca_arg_flags;
611 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
612 (ncap->ca_arg.ca_str.l = i = cap->ca_arg.ca_str.l) +1);
614 i = n_ALIGN(i);
615 buf += sizeof(*ncap) + i;
618 if(cacp->cac_vput != NULL){
619 ncacp->cac_vput = buf;
620 memcpy(buf, cacp->cac_vput, strlen(cacp->cac_vput) +1);
621 }else
622 ncacp->cac_vput = NULL;
623 NYD2_LEAVE;
624 return ncacp;
627 FL struct n_cmd_arg_ctx *
628 n_cmd_arg_restore_from_heap(void *vp){
629 struct n_cmd_arg *cap, *ncap;
630 struct n_cmd_arg_ctx *cacp, *rv;
631 NYD2_ENTER;
633 rv = n_autorec_alloc(sizeof *rv);
634 cacp = vp;
635 *rv = *cacp;
637 for(ncap = NULL, cap = cacp->cac_arg; cap != NULL; cap = cap->ca_next){
638 vp = n_autorec_alloc(sizeof(*ncap) + cap->ca_arg.ca_str.l +1);
639 DBG( memset(vp, 0, sizeof *ncap); )
641 if(ncap == NULL)
642 rv->cac_arg = vp;
643 else
644 ncap->ca_next = vp;
645 ncap = vp;
646 ncap->ca_next = NULL;
647 ncap->ca_ent_flags[0] = cap->ca_ent_flags[0];
648 ncap->ca_ent_flags[1] = cap->ca_ent_flags[1];
649 ncap->ca_arg_flags = cap->ca_arg_flags;
650 memcpy(ncap->ca_arg.ca_str.s = (char*)&ncap[1], cap->ca_arg.ca_str.s,
651 (ncap->ca_arg.ca_str.l = cap->ca_arg.ca_str.l) +1);
654 if(cacp->cac_vput != NULL)
655 rv->cac_vput = savestr(cacp->cac_vput);
656 NYD2_LEAVE;
657 return rv;
660 FL int
661 getrawlist(bool_t wysh, char **res_dat, size_t res_size,
662 char const *line, size_t linesize){
663 int res_no;
664 NYD_ENTER;
666 n_pstate &= ~n_PS_ARGLIST_MASK;
668 if(res_size == 0){
669 res_no = -1;
670 goto jleave;
671 }else if(UICMP(z, res_size, >, INT_MAX))
672 res_size = INT_MAX;
673 else
674 --res_size;
675 res_no = 0;
677 if(!wysh){
678 /* And assuming result won't grow input */
679 char c2, c, quotec, *cp2, *linebuf;
681 linebuf = n_lofi_alloc(linesize);
683 for(;;){
684 for(; blankchar(*line); ++line)
686 if(*line == '\0')
687 break;
689 if(UICMP(z, res_no, >=, res_size)){
690 n_err(_("Too many input tokens for result storage\n"));
691 res_no = -1;
692 break;
695 cp2 = linebuf;
696 quotec = '\0';
698 /* TODO v15: complete switch in order mirror known behaviour */
699 while((c = *line++) != '\0'){
700 if(quotec != '\0'){
701 if(c == quotec){
702 quotec = '\0';
703 continue;
704 }else if(c == '\\'){
705 if((c2 = *line++) == quotec)
706 c = c2;
707 else
708 --line;
710 }else if(c == '"' || c == '\''){
711 quotec = c;
712 continue;
713 }else if(c == '\\'){
714 if((c2 = *line++) != '\0')
715 c = c2;
716 else
717 --line;
718 }else if(blankchar(c))
719 break;
720 *cp2++ = c;
723 res_dat[res_no++] = savestrbuf(linebuf, PTR2SIZE(cp2 - linebuf));
724 if(c == '\0')
725 break;
728 n_lofi_free(linebuf);
729 }else{
730 /* sh(1) compat mode. Prepare shell token-wise */
731 struct n_string store;
732 struct str input;
733 void const *cookie;
735 n_string_creat_auto(&store);
736 input.s = n_UNCONST(line);
737 input.l = linesize;
738 cookie = NULL;
740 for(;;){
741 if(UICMP(z, res_no, >=, res_size)){
742 n_err(_("Too many input tokens for result storage\n"));
743 res_no = -1;
744 break;
747 /* C99 */{
748 enum n_shexp_state shs;
750 if((shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
751 (cookie == NULL ? n_SHEXP_PARSE_TRIM_SPACE : 0) |
752 /* TODO not here in old style n_SHEXP_PARSE_IFS_VAR |*/
753 n_SHEXP_PARSE_META_SEMICOLON),
754 &store, &input, &cookie)
755 ) & n_SHEXP_STATE_ERR_MASK){
756 /* Simply ignore Unicode error, just keep the normalized \[Uu] */
757 if((shs & n_SHEXP_STATE_ERR_MASK) != n_SHEXP_STATE_ERR_UNICODE){
758 res_no = -1;
759 break;
763 if(shs & n_SHEXP_STATE_OUTPUT){
764 if(shs & n_SHEXP_STATE_CONTROL)
765 n_pstate |= n_PS_WYSHLIST_SAW_CONTROL;
767 res_dat[res_no++] = n_string_cp(&store);
768 n_string_drop_ownership(&store);
771 if(shs & n_SHEXP_STATE_STOP)
772 break;
776 n_string_gut(&store);
779 if(res_no >= 0)
780 res_dat[(size_t)res_no] = NULL;
781 jleave:
782 NYD_LEAVE;
783 return res_no;
786 /* s-it-mode */