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
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
42 #define n_FILE cmd_tab
44 #ifndef HAVE_AMALGAMATION
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
);
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 */
63 static struct n_cmd_desc
const a_ctab_ctable
[] = {
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
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")) }
83 #ifdef HAVE_DOCSTRINGS
85 a_ctab_cmdinfo(struct n_cmd_desc
const *cdp
){
86 struct n_string rvb
, *rv
;
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");
97 case n_CMD_ARG_TYPE_STRING
:
98 case n_CMD_ARG_TYPE_RAWDAT
:
99 cp
= N_("string data");
101 case n_CMD_ARG_TYPE_RAWLIST
:
102 cp
= N_("old-style quoting");
104 case n_CMD_ARG_TYPE_NDMLIST
:
105 cp
= N_("message-list (no default)");
107 case n_CMD_ARG_TYPE_WYRA
:
108 cp
= N_("`wysh' for sh(1)ell-style quoting");
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");
116 case n_CMD_ARG_TYPE_ARG
:{
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
){
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"));
137 case n_CMD_ARG_DESC_WYSH
:
138 rv
= n_string_push_cp(rv
, _("eval"));
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
, ']');
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
);
180 #endif /* HAVE_DOCSTRINGS */
183 a_ctab_c_list(void *vp
){
185 struct n_cmd_desc
const **cdpa
, *cdp
, **cdpa_curr
;
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
];
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
)
204 scrwid
= n_SCRNWIDTH_FOR_LISTS
;
206 fprintf(fp
, _("Commands are:\n"));
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
= "]";
216 #ifdef HAVE_DOCSTRINGS
217 if(n_poption
& n_PO_D_V
){
218 fprintf(fp
, "%s%s%s\n", pre
, cdp
->cd_name
, suf
);
220 fprintf(fp
, " : %s\n", V_(cdp
->cd_doc
));
222 fprintf(fp
, " : %s\n", a_ctab_cmdinfo(cdp
));
229 j
= strlen(cdp
->cd_name
);
233 if((i
+= j
+ 2) > scrwid
){
238 fprintf(fp
, (*cdpa_curr
!= NULL
? "%s%s%s, " : "%s%s%s\n"),
239 pre
, cdp
->cd_name
, suf
);
244 page_or_print(fp
, l
);
252 a_ctab__pcmd_cmp(void const *s1
, void const *s2
){
253 struct n_cmd_desc
const * const *cdpa1
, * const *cdpa2
;
259 rv
= strcmp((*cdpa1
)->cd_name
, (*cdpa2
)->cd_name
);
265 a_ctab_c_help(void *vp
){
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
);
282 cdp_max
= &(cdp
= a_ctab_ctable
)[n_NELEM(a_ctab_ctable
)];
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
);
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
));
297 putc('\n', n_stdout
);
302 if(cdp_max
== &a_ctab_ctable
[n_NELEM(a_ctab_ctable
)]){
304 a_ctab_ctable_plus
)[n_NELEM(a_ctab_ctable_plus
)];
308 if(alias_name
!= NULL
){
309 fprintf(n_stdout
, "%s\n", n_shexp_quote_cp(arg
, TRU1
));
312 n_err(_("Unknown command: `%s'\n"), arg
);
316 /* Very ugly, but take care for compiler supported string lengths :( */
317 fputs(n_progname
, n_stdout
);
319 " commands -- <msglist> denotes message specifications,\n"
320 "e.g., 1-5, :n or . (current, the \"dot\"), separated by spaces:\n"),
324 "type <msglist> type (`print') messages (honour `headerpick' etc.)\n"
325 "Type <msglist> like `type' but always show all headers\n"
326 "next goto and type next message\n"
327 "from <msglist> (search and) print header summary for the given list\n"
328 "headers header summary for messages surrounding \"dot\"\n"
329 "delete <msglist> delete messages (can be `undelete'd)\n"),
334 "save <msglist> folder append messages to folder and mark as saved\n"
335 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
336 "write <msglist> file write message contents to file (prompts for parts)\n"
337 "Reply <msglist> reply to message senders only\n"
338 "reply <msglist> like `Reply', but address all recipients\n"
339 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
344 "mail <recipients> compose a mail for the given recipients\n"
345 "file folder change to another mailbox\n"
346 "File folder like `file', but open readonly\n"
347 "quit quit and apply changes to the current mailbox\n"
348 "xit or exit like `quit', but discard changes\n"
349 "!shell command shell escape\n"
350 "list [<anything>] all available commands [in search order]\n"),
353 rv
= (ferror(n_stdout
) != 0);
361 n_cmd_isolate(char const *cmd
){
363 while(*cmd
!= '\0' &&
364 strchr("\\!~|? \t0123456789&%@$^.:/-+*'\",;(`", *cmd
) == NULL
)
367 return n_UNCONST(cmd
);
370 FL
struct n_cmd_desc
const *
371 n_cmd_firstfit(char const *cmd
){ /* TODO *hashtable*! linear list search!!! */
372 struct n_cmd_desc
const *cdp
;
375 for(cdp
= a_ctab_ctable
; cdp
< &a_ctab_ctable
[n_NELEM(a_ctab_ctable
)]; ++cdp
)
376 if(*cmd
== *cdp
->cd_name
&& cdp
->cd_func
!= NULL
&&
377 is_prefix(cmd
, cdp
->cd_name
))
385 FL
struct n_cmd_desc
const *
387 struct n_cmd_desc
const *cdp
;
390 cdp
= &a_ctab_ctable
[0];
396 n_cmd_arg_parse(struct n_cmd_arg_ctx
*cacp
){
397 struct n_cmd_arg ncap
, *lcap
;
398 struct str shin_orig
, shin
;
399 bool_t addca
, greedyjoin
;
401 size_t cad_idx
, parsed_args
;
402 struct n_cmd_arg_desc
const *cadp
;
405 assert(cacp
->cac_inlen
== 0 || cacp
->cac_indat
!= NULL
);
406 assert(cacp
->cac_desc
->cad_no
> 0);
409 bool_t opt_seen
= FAL0
;
411 for(cadp
= cacp
->cac_desc
, cad_idx
= 0;
412 cad_idx
< cadp
->cad_no
; ++cad_idx
){
413 assert(cadp
->cad_ent_flags
[cad_idx
][0] & n__CMD_ARG_DESC_TYPE_MASK
);
415 (cadp
->cad_ent_flags
[cad_idx
][0] & n_CMD_ARG_DESC_OPTION
));
416 if(cadp
->cad_ent_flags
[cad_idx
][0] & n_CMD_ARG_DESC_OPTION
)
418 assert(!(cadp
->cad_ent_flags
[cad_idx
][0] & n_CMD_ARG_DESC_GREEDY
) ||
419 cad_idx
+ 1 == cadp
->cad_no
);
424 shin
.s
= n_UNCONST(cacp
->cac_indat
); /* "logical" only */
425 shin
.l
= (cacp
->cac_inlen
== UIZ_MAX
? strlen(shin
.s
) : cacp
->cac_inlen
);
428 cacp
->cac_arg
= lcap
= NULL
;
434 for(cadp
= cacp
->cac_desc
, cad_idx
= 0; shin
.l
> 0 && cad_idx
< cadp
->cad_no
;
437 memset(&ncap
, 0, sizeof ncap
);
438 ncap
.ca_indat
= shin
.s
;
439 /* >ca_inline once we know */
440 memcpy(&ncap
.ca_ent_flags
[0], &cadp
->cad_ent_flags
[cad_idx
][0],
441 sizeof ncap
.ca_ent_flags
);
444 switch(ncap
.ca_ent_flags
[0] & n__CMD_ARG_DESC_TYPE_MASK
){
445 case n_CMD_ARG_DESC_STRING
:{ /* TODO \ escaping? additional type!? */
446 char /*const*/ *cp
= shin
.s
;
449 while(i
> 0 && blankspacechar(*cp
))
452 ncap
.ca_arg
.ca_str
.s
= cp
;
453 while(i
> 0 && !blankspacechar(*cp
))
455 ncap
.ca_arg
.ca_str
.s
= savestrbuf(ncap
.ca_arg
.ca_str
.s
,
456 ncap
.ca_arg
.ca_str
.l
= PTR2SIZE(cp
- ncap
.ca_arg
.ca_str
.s
));
458 while(i
> 0 && blankspacechar(*cp
))
460 ncap
.ca_inlen
= PTR2SIZE(cp
- ncap
.ca_indat
);
466 case n_CMD_ARG_DESC_WYSH
:{
467 struct n_string shou
, *shoup
;
468 enum n_shexp_state shs
;
471 if(cad_idx
== cadp
->cad_no
- 1 ||
472 (cadp
->cad_ent_flags
[cad_idx
+ 1][0] & n_CMD_ARG_DESC_OPTION
))
473 addflags
= n_SHEXP_PARSE_META_SEMICOLON
;
475 addflags
= n_SHEXP_PARSE_NONE
;
477 shoup
= n_string_creat_auto(&shou
);
479 shs
= n_shexp_parse_token((ncap
.ca_ent_flags
[1] | addflags
|
480 n_SHEXP_PARSE_TRIM_SPACE
| n_SHEXP_PARSE_LOG
),
482 (ncap
.ca_ent_flags
[0] & n_CMD_ARG_DESC_GREEDY
? &cookie
: NULL
));
483 ncap
.ca_inlen
= PTR2SIZE(shin
.s
- ncap
.ca_indat
);
484 if((shs
& (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_ERR_MASK
)) ==
485 n_SHEXP_STATE_OUTPUT
){
486 if((shs
& n_SHEXP_STATE_META_SEMICOLON
) && shou
.s_len
== 0)
488 ncap
.ca_arg
.ca_str
.s
= n_string_cp(shoup
);
489 ncap
.ca_arg
.ca_str
.l
= shou
.s_len
;
490 shoup
= n_string_drop_ownership(shoup
);
494 if(shs
& n_SHEXP_STATE_ERR_MASK
)
496 if((shs
& n_SHEXP_STATE_STOP
) &&
497 (ncap
.ca_ent_flags
[0] & n_CMD_ARG_DESC_HONOUR_STOP
)){
498 if(!(shs
& n_SHEXP_STATE_OUTPUT
))
508 if(greedyjoin
== TRU1
){ /* TODO speed this up! */
512 assert(lcap
!= NULL
);
513 i
= lcap
->ca_arg
.ca_str
.l
;
514 lcap
->ca_arg
.ca_str
.l
+= 1 + ncap
.ca_arg
.ca_str
.l
;
515 cp
= salloc(lcap
->ca_arg
.ca_str
.l
+1);
516 memcpy(cp
, lcap
->ca_arg
.ca_str
.s
, i
);
517 lcap
->ca_arg
.ca_str
.s
= cp
;
519 memcpy(&cp
[i
], ncap
.ca_arg
.ca_str
.s
, ncap
.ca_arg
.ca_str
.l
+1);
521 struct n_cmd_arg
*cap
;
523 cap
= salloc(sizeof *cap
);
524 memcpy(cap
, &ncap
, sizeof ncap
);
537 if((shin
.l
> 0 || cookie
!= NULL
) &&
538 (ncap
.ca_ent_flags
[0] & n_CMD_ARG_DESC_GREEDY
)){
540 greedyjoin
= ((ncap
.ca_ent_flags
[0] & n_CMD_ARG_DESC_GREEDY_JOIN
) &&
541 (ncap
.ca_ent_flags
[0] &
542 (n_CMD_ARG_DESC_STRING
| n_CMD_ARG_DESC_WYSH
)))
548 if(cad_idx
< cadp
->cad_no
&&
549 !(cadp
->cad_ent_flags
[cad_idx
][0] & n_CMD_ARG_DESC_OPTION
))
552 lcap
= (struct n_cmd_arg
*)-1;
555 return (lcap
!= NULL
);
560 for(i
= 0; (i
< cadp
->cad_no
&&
561 !(cadp
->cad_ent_flags
[i
][0] & n_CMD_ARG_DESC_OPTION
)); ++i
)
564 n_err(_("`%s': parsing stopped after %" PRIuZ
" arguments "
565 "(need %" PRIuZ
"%s)\n"
568 cadp
->cad_name
, parsed_args
, i
, (i
== cadp
->cad_no
? n_empty
: "+"),
569 (int)shin_orig
.l
, shin_orig
.s
,
570 (int)shin
.l
, shin
.s
);
577 n_cmd_arg_save_to_heap(struct n_cmd_arg_ctx
const *cacp
){
578 struct n_cmd_arg
*ncap
;
579 struct n_cmd_arg_ctx
*ncacp
;
581 struct n_cmd_arg
const *cap
;
585 /* For simplicity, save it all in once chunk, so that it can be thrown away
586 * with a simple n_free() from whoever is concerned */
588 for(cap
= cacp
->cac_arg
; cap
!= NULL
; cap
= cap
->ca_next
){
589 i
= cap
->ca_arg
.ca_str
.l
+1;
591 len
+= sizeof(*cap
) + i
;
593 if(cacp
->cac_vput
!= NULL
)
594 len
+= strlen(cacp
->cac_vput
) +1;
596 ncacp
= n_alloc(len
);
598 buf
= (char*)&ncacp
[1];
600 for(ncap
= NULL
, cap
= cacp
->cac_arg
; cap
!= NULL
; cap
= cap
->ca_next
){
604 DBG( memset(vp
, 0, sizeof *ncap
); )
611 ncap
->ca_next
= NULL
;
612 ncap
->ca_ent_flags
[0] = cap
->ca_ent_flags
[0];
613 ncap
->ca_ent_flags
[1] = cap
->ca_ent_flags
[1];
614 ncap
->ca_arg_flags
= cap
->ca_arg_flags
;
615 memcpy(ncap
->ca_arg
.ca_str
.s
= (char*)&ncap
[1], cap
->ca_arg
.ca_str
.s
,
616 (ncap
->ca_arg
.ca_str
.l
= i
= cap
->ca_arg
.ca_str
.l
) +1);
619 buf
+= sizeof(*ncap
) + i
;
622 if(cacp
->cac_vput
!= NULL
){
623 ncacp
->cac_vput
= buf
;
624 memcpy(buf
, cacp
->cac_vput
, strlen(cacp
->cac_vput
) +1);
626 ncacp
->cac_vput
= NULL
;
631 FL
struct n_cmd_arg_ctx
*
632 n_cmd_arg_restore_from_heap(void *vp
){
633 struct n_cmd_arg
*cap
, *ncap
;
634 struct n_cmd_arg_ctx
*cacp
, *rv
;
637 rv
= n_autorec_alloc(sizeof *rv
);
641 for(ncap
= NULL
, cap
= cacp
->cac_arg
; cap
!= NULL
; cap
= cap
->ca_next
){
642 vp
= n_autorec_alloc(sizeof(*ncap
) + cap
->ca_arg
.ca_str
.l
+1);
643 DBG( memset(vp
, 0, sizeof *ncap
); )
650 ncap
->ca_next
= NULL
;
651 ncap
->ca_ent_flags
[0] = cap
->ca_ent_flags
[0];
652 ncap
->ca_ent_flags
[1] = cap
->ca_ent_flags
[1];
653 ncap
->ca_arg_flags
= cap
->ca_arg_flags
;
654 memcpy(ncap
->ca_arg
.ca_str
.s
= (char*)&ncap
[1], cap
->ca_arg
.ca_str
.s
,
655 (ncap
->ca_arg
.ca_str
.l
= cap
->ca_arg
.ca_str
.l
) +1);
658 if(cacp
->cac_vput
!= NULL
)
659 rv
->cac_vput
= savestr(cacp
->cac_vput
);
665 getrawlist(bool_t wysh
, char **res_dat
, size_t res_size
,
666 char const *line
, size_t linesize
){
670 n_pstate
&= ~n_PS_ARGLIST_MASK
;
675 }else if(UICMP(z
, res_size
, >, INT_MAX
))
682 /* And assuming result won't grow input */
683 char c2
, c
, quotec
, *cp2
, *linebuf
;
685 linebuf
= n_lofi_alloc(linesize
);
688 for(; blankchar(*line
); ++line
)
693 if(UICMP(z
, res_no
, >=, res_size
)){
694 n_err(_("Too many input tokens for result storage\n"));
702 /* TODO v15: complete switch in order mirror known behaviour */
703 while((c
= *line
++) != '\0'){
709 if((c2
= *line
++) == quotec
)
714 }else if(c
== '"' || c
== '\''){
718 if((c2
= *line
++) != '\0')
722 }else if(blankchar(c
))
727 res_dat
[res_no
++] = savestrbuf(linebuf
, PTR2SIZE(cp2
- linebuf
));
732 n_lofi_free(linebuf
);
734 /* sh(1) compat mode. Prepare shell token-wise */
735 struct n_string store
;
739 n_string_creat_auto(&store
);
740 input
.s
= n_UNCONST(line
);
745 if(UICMP(z
, res_no
, >=, res_size
)){
746 n_err(_("Too many input tokens for result storage\n"));
752 enum n_shexp_state shs
;
754 if((shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
755 (cookie
== NULL
? n_SHEXP_PARSE_TRIM_SPACE
: 0) |
756 /* TODO not here in old style n_SHEXP_PARSE_IFS_VAR |*/
757 n_SHEXP_PARSE_META_SEMICOLON
),
758 &store
, &input
, &cookie
)
759 ) & n_SHEXP_STATE_ERR_MASK
){
760 /* Simply ignore Unicode error, just keep the normalized \[Uu] */
761 if((shs
& n_SHEXP_STATE_ERR_MASK
) != n_SHEXP_STATE_ERR_UNICODE
){
767 if(shs
& n_SHEXP_STATE_OUTPUT
){
768 if(shs
& n_SHEXP_STATE_CONTROL
)
769 n_pstate
|= n_PS_WYSHLIST_SAW_CONTROL
;
771 res_dat
[res_no
++] = n_string_cp(&store
);
772 n_string_drop_ownership(&store
);
775 if(shs
& n_SHEXP_STATE_STOP
)
780 n_string_gut(&store
);
784 res_dat
[(size_t)res_no
] = NULL
;