1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Command input, lexing and evaluation, resource file loading and `source'ing.
3 *@ TODO PS_ROBOT requires yet PS_SOURCING, which REALLY sucks.
4 *@ TODO Commands and ghosts deserve a hashmap. Or so.
6 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
10 * Copyright (c) 1980, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #define n_FILE lex_input
40 #ifndef HAVE_AMALGAMATION
44 enum a_lex_input_flags
{
46 a_LEX_FREE
= 1<<0, /* Structure was allocated, free() it */
47 a_LEX_PIPE
= 1<<1, /* Open on a pipe */
48 a_LEX_MACRO
= 1<<2, /* Running a macro */
49 a_LEX_MACRO_FREE_DATA
= 1<<3, /* Lines are allocated, free(3) once done */
50 a_LEX_MACRO_X_OPTION
= 1<<4, /* Macro indeed command line -X option */
51 a_LEX_MACRO_CMD
= 1<<5, /* Macro indeed single-line: ~:COMMAND */
53 a_LEX_SUPER_MACRO
= 1<<16 /* *Not* inheriting PS_SOURCING state */
57 char const *lc_name
; /* Name of command */
58 int (*lc_func
)(void*); /* Implementor of command */
59 enum argtype lc_argtype
; /* Arglist type (see below) */
60 si16_t lc_msgflag
; /* Required flags of msgs */
61 si16_t lc_msgmask
; /* Relevant flags of msgs */
62 #ifdef HAVE_DOCSTRINGS
63 char const *lc_doc
; /* One line doc for command */
66 /* Yechh, can't initialize unions */
67 #define lc_minargs lc_msgflag /* Minimum argcount for RAWLIST */
68 #define lc_maxargs lc_msgmask /* Max argcount for RAWLIST */
71 struct a_lex_ghost
*lg_next
;
72 struct str lg_cmd
; /* Data follows after .lg_name */
73 char lg_name
[VFIELD_SIZE(0)];
76 struct a_lex_eval_ctx
{
77 struct str le_line
; /* The terminated data to _evaluate() */
78 ui32_t le_line_size
; /* May be used to store line memory size */
79 bool_t le_is_recursive
; /* Evaluation in evaluation? (collect ~:) */
81 bool_t le_add_history
; /* Enter (final) command in history? */
82 char const *le_new_content
; /* History: reenter line, start with this */
85 struct a_lex_input_stack
{
86 struct a_lex_input_stack
*li_outer
;
87 FILE *li_file
; /* File we were in */
88 void *li_cond
; /* Saved state of conditional stack */
89 ui32_t li_flags
; /* enum a_lex_input_flags */
90 ui32_t li_loff
; /* Pseudo (macro): index in .li_lines */
91 char **li_lines
; /* Pseudo content, lines unfolded */
92 char li_name
[VFIELD_SIZE(0)]; /* Name of file or macro */
95 static sighandler_type a_lex_oldpipe
;
96 static struct a_lex_ghost
*a_lex_ghosts
;
97 /* a_lex_cmd_tab[] after fun protos */
100 static struct a_lex_input_stack
*a_lex_input
;
102 /* Isolate the command from the arguments */
103 static char *a_lex_isolate(char const *comm
);
105 /* Command ghost handling */
106 static int a_lex_c_ghost(void *v
);
107 static int a_lex_c_unghost(void *v
);
109 /* Print a list of all commands */
110 static int a_lex_c_list(void *v
);
112 static int a_lex__pcmd_cmp(void const *s1
, void const *s2
);
114 /* `help' / `?' command */
115 static int a_lex_c_help(void *v
);
118 static int a_lex_c_quit(void *v
);
120 /* Print the binaries version number */
121 static int a_lex_c_version(void *v
);
123 static int a_lex__version_cmp(void const *s1
, void const *s2
);
125 /* PS_STATE_PENDMASK requires some actions */
126 static void a_lex_update_pstate(void);
128 /* Evaluate a single command.
129 * .le_add_history and .le_new_content will be updated upon success.
130 * Command functions return 0 for success, 1 for error, and -1 for abort.
131 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
132 static int a_lex_evaluate(struct a_lex_eval_ctx
*evp
);
134 /* Get first-fit, or NULL */
135 static struct a_lex_cmd
const *a_lex__firstfit(char const *comm
);
137 /* Branch here on hangup signal and simulate "exit" */
138 static void a_lex_hangup(int s
);
140 /* The following gets called on receipt of an interrupt. Close all open files
141 * except 0, 1, 2, and the temporary. Also, unstack all source files */
142 static void a_lex_onintr(int s
);
144 /* Pop the current input back to the previous level. Update the program state.
145 * If the argument is TRUM1 then we don't alert and error out if the stack
146 * doesn't exist at all */
147 static void a_lex_unstack(bool_t eval_error
);
149 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
150 static bool_t
a_lex_source_file(char const *file
, bool_t silent_error
);
152 /* System resource file load()ing or -X command line option array traversal */
153 static bool_t
a_lex_load(struct a_lex_input_stack
*lip
);
155 /* A simplified command loop for recursed state machines */
156 static bool_t
a_commands_recursive(enum n_lexinput_flags lif
);
158 /* List of all commands, and list of commands which are specially treated
159 * and deduced in _evaluate(), but we need a list for _c_list() and
160 * print_comm_docstr() */
161 #ifdef HAVE_DOCSTRINGS
166 static struct a_lex_cmd
const a_lex_cmd_tab
[] = {
169 a_lex_special_cmd_tab
[] = {
171 DS(N_("Comment command: ignore remaining (continuable) line")) },
173 DS(N_("Print out the preceding message")) }
178 a_lex_isolate(char const *comm
){
180 while(*comm
!= '\0' &&
181 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm
) == NULL
)
184 return UNCONST(comm
);
188 a_lex_c_ghost(void *v
){
189 struct a_lex_ghost
*lgp
, *gp
;
201 if((fp
= Ftmp(NULL
, "ghost", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
204 for(i
= 0, gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
205 fprintf(fp
, "wysh ghost %s %s\n",
206 gp
->lg_name
, n_shexp_quote_cp(gp
->lg_cmd
.s
, TRU1
));
209 page_or_print(fp
, i
);
215 /* Verify the ghost name is a valid one */
216 if(*argv
[0] == '\0' || *a_lex_isolate(argv
[0]) != '\0'){
217 n_err(_("`ghost': can't canonicalize %s\n"),
218 n_shexp_quote_cp(argv
[0], FAL0
));
223 /* Show command of single ghost? */
225 for(gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
226 if(!strcmp(argv
[0], gp
->lg_name
)){
227 printf("wysh ghost %s %s\n",
228 gp
->lg_name
, n_shexp_quote_cp(gp
->lg_cmd
.s
, TRU1
));
231 n_err(_("`ghost': no such alias: %s\n"), argv
[0]);
236 /* Define command for ghost: verify command content */
237 for(cl
= 0, i
= 1; (cp
= UNCONST(argv
[i
])) != NULL
; ++i
)
239 cl
+= strlen(cp
) +1; /* SP or NUL */
241 n_err(_("`ghost': empty command arguments after %s\n"), argv
[0]);
246 /* If the ghost already exists, recreate */
247 for(lgp
= NULL
, gp
= a_lex_ghosts
; gp
!= NULL
; lgp
= gp
, gp
= gp
->lg_next
)
248 if(!strcmp(gp
->lg_name
, argv
[0])){
250 lgp
->lg_next
= gp
->lg_next
;
252 a_lex_ghosts
= gp
->lg_next
;
257 nl
= strlen(argv
[0]) +1;
258 gp
= smalloc(sizeof(*gp
) - VFIELD_SIZEOF(struct a_lex_ghost
, lg_name
) +
260 gp
->lg_next
= a_lex_ghosts
;
262 memcpy(gp
->lg_name
, argv
[0], nl
);
263 cp
= gp
->lg_cmd
.s
= gp
->lg_name
+ nl
;
266 while(*++argv
!= NULL
)
267 if((i
= strlen(*argv
)) > 0){
268 memcpy(cp
, *argv
, i
);
279 a_lex_c_unghost(void *v
){
280 struct a_lex_ghost
*lgp
, *gp
;
281 char const **argv
, *cp
;
288 while((cp
= *argv
++) != NULL
){
289 if(cp
[0] == '*' && cp
[1] == '\0'){
290 while((gp
= a_lex_ghosts
) != NULL
){
291 a_lex_ghosts
= gp
->lg_next
;
295 for(lgp
= NULL
, gp
= a_lex_ghosts
; gp
!= NULL
;
296 lgp
= gp
, gp
= gp
->lg_next
)
297 if(!strcmp(gp
->lg_name
, cp
)){
299 lgp
->lg_next
= gp
->lg_next
;
301 a_lex_ghosts
= gp
->lg_next
;
305 n_err(_("`unghost': no such alias: %s\n"),
306 n_shexp_quote_cp(cp
, FAL0
));
316 a_lex_c_list(void *v
){
318 struct a_lex_cmd
const **cpa
, *cp
, **cursor
;
322 i
= NELEM(a_lex_cmd_tab
) + NELEM(a_lex_special_cmd_tab
) +1;
323 cpa
= salloc(sizeof(cp
) * i
);
325 for(i
= 0; i
< NELEM(a_lex_cmd_tab
); ++i
)
326 cpa
[i
] = &a_lex_cmd_tab
[i
];
330 for(j
= 0; j
< NELEM(a_lex_special_cmd_tab
); ++i
, ++j
)
331 cpa
[i
] = &a_lex_special_cmd_tab
[j
];
339 qsort(cpa
, i
, sizeof(xcp
), &a_lex__pcmd_cmp
);
342 if((fp
= Ftmp(NULL
, "list", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
345 fprintf(fp
, _("Commands are:\n"));
347 for(i
= 0, cursor
= cpa
; (cp
= *cursor
++) != NULL
;){
348 if(cp
->lc_func
== &c_cmdnotsupp
)
350 if(options
& OPT_D_V
){
353 switch(cp
->lc_argtype
& ARG_ARGMASK
){
354 case ARG_MSGLIST
: argt
= N_("message-list"); break;
355 case ARG_STRLIST
: argt
= N_("a \"string\""); break;
356 case ARG_RAWLIST
: argt
= N_("old-style quoting"); break;
357 case ARG_NOLIST
: argt
= N_("no arguments"); break;
358 case ARG_NDMLIST
: argt
= N_("message-list (without a default)"); break;
359 case ARG_WYSHLIST
: argt
= N_("sh(1)ell-style quoting"); break;
360 default: argt
= N_("`wysh' for sh(1)ell-style quoting"); break;
362 #ifdef HAVE_DOCSTRINGS
363 fprintf(fp
, _("`%s'. Argument type: %s.\n\t%s\n"),
364 cp
->lc_name
, V_(argt
), V_(cp
->lc_doc
));
367 fprintf(fp
, "`%s' (%s)\n", cp
->lc_name
, argt
);
371 size_t j
= strlen(cp
->lc_name
) + 2;
378 fprintf(fp
, (*cursor
!= NULL
? "%s, " : "%s\n"), cp
->lc_name
);
383 page_or_print(fp
, l
);
391 a_lex__pcmd_cmp(void const *s1
, void const *s2
){
392 struct a_lex_cmd
const * const *cp1
, * const *cp2
;
398 rv
= strcmp((*cp1
)->lc_name
, (*cp2
)->lc_name
);
404 a_lex_c_help(void *v
){
409 /* Help for a single command? */
410 if((arg
= *(char**)v
) != NULL
){
411 struct a_lex_ghost
const *gp
;
412 struct a_lex_cmd
const *cp
, *cpmax
;
414 /* Ghosts take precedence */
415 for(gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
416 if(!strcmp(arg
, gp
->lg_name
)){
417 printf("%s -> ", arg
);
422 cpmax
= &(cp
= a_lex_cmd_tab
)[NELEM(a_lex_cmd_tab
)];
424 for(; PTRCMP(cp
, <, cpmax
); ++cp
){
425 #ifdef HAVE_DOCSTRINGS
426 # define a_DS V_(cp->lc_doc)
430 if(!strcmp(arg
, cp
->lc_name
))
431 printf("%s: %s", arg
, a_DS
);
432 else if(is_prefix(arg
, cp
->lc_name
))
433 printf("%s (%s): %s", arg
, cp
->lc_name
, a_DS
);
437 if(options
& OPT_D_V
){
438 switch(cp
->lc_argtype
& ARG_ARGMASK
){
439 case ARG_MSGLIST
: arg
= N_("message-list"); break;
440 case ARG_STRLIST
: arg
= N_("a \"string\""); break;
441 case ARG_RAWLIST
: arg
= N_("old-style quoting"); break;
442 case ARG_NOLIST
: arg
= N_("no arguments"); break;
443 case ARG_NDMLIST
: arg
= N_("message-list (no default)"); break;
444 case ARG_WYSHLIST
: arg
= N_("sh(1)ell-style quoting"); break;
445 default: arg
= N_("`wysh' for sh(1)ell-style quoting"); break;
447 #ifdef HAVE_DOCSTRINGS
448 printf(_("\n\tArgument type: %s"), V_(arg
));
450 printf(_("argument type: %s"), V_(arg
));
459 if(PTRCMP(cpmax
, ==, &a_lex_cmd_tab
[NELEM(a_lex_cmd_tab
)])){
460 cpmax
= &(cp
= a_lex_special_cmd_tab
)[NELEM(a_lex_special_cmd_tab
)];
465 printf("%s\n", n_shexp_quote_cp(arg
, TRU1
));
468 n_err(_("Unknown command: `%s'\n"), arg
);
472 /* Very ugly, but take care for compiler supported string lengths :( */
473 fputs(progname
, stdout
);
475 " commands -- <msglist> denotes message specifications,\n"
476 "e.g., 1-5, :n or ., separated by spaces:\n"), stdout
);
479 "type <msglist> type (alias: `print') messages (honour `retain' etc.)\n"
480 "Type <msglist> like `type' but always show all headers\n"
481 "next goto and type next message\n"
482 "from <msglist> (search and) print header summary for the given list\n"
483 "headers header summary for messages surrounding \"dot\"\n"
484 "delete <msglist> delete messages (can be `undelete'd)\n"),
489 "save <msglist> folder append messages to folder and mark as saved\n"
490 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
491 "write <msglist> file write message contents to file (prompts for parts)\n"
492 "Reply <msglist> reply to message senders only\n"
493 "reply <msglist> like `Reply', but address all recipients\n"
494 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
499 "mail <recipients> compose a mail for the given recipients\n"
500 "file folder change to another mailbox\n"
501 "File folder like `file', but open readonly\n"
502 "quit quit and apply changes to the current mailbox\n"
503 "xit or exit like `quit', but discard changes\n"
504 "!shell command shell escape\n"
505 "list [<anything>] all available commands [in search order]\n"),
508 rv
= (ferror(stdout
) != 0);
516 a_lex_c_quit(void *v
){
520 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
521 * Otherwise return -1 to abort command loop */
528 a_lex_c_version(void *v
){
531 char const *cp
, **arr
;
536 printf(_("%s version %s\nFeatures included (+) or not (-)\n"),
537 uagent
, ok_vlook(version
));
539 /* *features* starts with dummy byte to avoid + -> *folder* expansions */
540 i
= strlen(cp
= &ok_vlook(features
)[1]) +1;
544 arr
= salloc(sizeof(cp
) * VAL_FEATURES_CNT
);
545 for(longest
= 0, i
= 0; (cp
= n_strsep(&iop
, ',', TRU1
)) != NULL
; ++i
){
548 longest
= MAX(longest
, (int)i2
);
550 qsort(arr
, i
, sizeof(cp
), &a_lex__version_cmp
);
552 for(++longest
, i2
= 0; i
-- > 0;){
554 printf("%-*s ", longest
, cp
);
556 if(UICMP(z
, ++i2
+ longest
, >=, scrnwidth
) || i
== 0){
562 if((rv
= ferror(stdout
) != 0))
569 a_lex__version_cmp(void const *s1
, void const *s2
){
570 char const * const *cp1
, * const *cp2
;
576 rv
= strcmp(&(*cp1
)[1], &(*cp2
)[1]);
582 a_lex_update_pstate(void){
585 if(pstate
& PS_SIGWINCH_PEND
){
588 snprintf(buf
, sizeof buf
, "%d", scrnwidth
);
589 ok_vset(COLUMNS
, buf
);
590 snprintf(buf
, sizeof buf
, "%d", scrnheight
);
594 pstate
&= ~PS_PSTATE_PENDMASK
;
599 a_lex_evaluate(struct a_lex_eval_ctx
*evp
){
600 /* xxx old style(9), but also old code */
602 char _wordbuf
[2], *arglist
[MAXARGC
], *cp
, *word
;
603 struct a_lex_ghost
*gp
;
604 struct a_lex_cmd
const *cmd
;
613 line
= evp
->le_line
; /* XXX don't change original (buffer pointer) */
614 assert(line
.s
[line
.l
] == '\0');
615 evp
->le_add_history
= FAL0
;
616 evp
->le_new_content
= NULL
;
618 /* Command ghosts that refer to shell commands or macro expansion restart */
621 /* Strip the white space away from end and beginning of command */
625 for(cp
= &line
.s
[i
-1]; spacechar(*cp
); --cp
)
629 for(cp
= line
.s
; spacechar(*cp
); ++cp
)
631 line
.l
-= PTR2SIZE(cp
- line
.s
);
633 /* Ignore null commands (comments) */
637 /* Handle ! differently to get the correct lexical conventions */
641 /* Isolate the actual command; since it may not necessarily be
642 * separated from the arguments (as in `p1') we need to duplicate it to
643 * be able to create a NUL terminated version.
644 * We must be aware of several special one letter commands here */
645 else if((cp
= a_lex_isolate(cp
)) == arglist
[0] &&
646 (*cp
== '|' || *cp
== '~' || *cp
== '?'))
648 c
= (int)PTR2SIZE(cp
- arglist
[0]);
650 word
= UICMP(z
, c
, <, sizeof _wordbuf
) ? _wordbuf
: salloc(c
+1);
651 memcpy(word
, arglist
[0], c
);
654 /* Look up the command; if not found, bitch.
655 * Normally, a blank command would map to the first command in the
656 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
657 * confusion; act just the same for ghosts */
659 if((pstate
& PS_ROBOT
) || gp
!= NULL
)
661 cmd
= a_lex_cmd_tab
+ 0;
665 /* XXX It may be the argument parse adjuster */
666 if(!wysh
&& c
== sizeof("wysh") -1 && !asccasecmp(word
, "wysh")){
672 /* If this is the first evaluation, check command ghosts */
674 /* TODO relink list head, so it's sorted on usage over time?
675 * TODO in fact, there should be one hashmap over all commands and ghosts
676 * TODO so that the lookup could be made much more efficient than it is
677 * TODO now (two adjacent list searches! */
678 for(gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
679 if(!strcmp(word
, gp
->lg_name
)){
684 line
.s
= salloc(i
+ line
.l
+1);
685 memcpy(line
.s
, gp
->lg_cmd
.s
, i
);
686 memcpy(line
.s
+ i
, cp
, line
.l
);
687 line
.s
[i
+= line
.l
] = '\0';
690 line
.s
= gp
->lg_cmd
.s
;
691 line
.l
= gp
->lg_cmd
.l
;
697 if((cmd
= a_lex__firstfit(word
)) == NULL
|| cmd
->lc_func
== &c_cmdnotsupp
){
700 if(!(s
= condstack_isskip()) || (options
& OPT_D_V
))
701 n_err(_("Unknown command%s: `%s'\n"),
702 (s
? _(" (ignored due to `if' condition)") : ""), word
);
712 /* See if we should execute the command -- if a conditional we always
713 * execute it, otherwise, check the state of cond */
715 if(!(cmd
->lc_argtype
& ARG_F
) && condstack_isskip())
718 /* Process the arguments to the command, depending on the type it expects */
719 if(!(cmd
->lc_argtype
& ARG_M
) && (options
& OPT_SENDMODE
)){
720 n_err(_("May not execute `%s' while sending\n"), cmd
->lc_name
);
723 if((cmd
->lc_argtype
& ARG_S
) && !(pstate
& PS_STARTED
)){
724 n_err(_("May not execute `%s' during startup\n"), cmd
->lc_name
);
727 if((cmd
->lc_argtype
& ARG_I
) &&
728 !(options
& (OPT_INTERACTIVE
| OPT_BATCH_FLAG
))){
729 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
733 if((cmd
->lc_argtype
& ARG_R
) && (pstate
& PS_RECURSED
)){
734 n_err(_("Cannot invoke `%s' when in recursed mode (e.g., composing)\n"),
739 if((cmd
->lc_argtype
& ARG_W
) && !(mb
.mb_perm
& MB_DELE
)){
740 n_err(_("May not execute `%s' -- message file is read only\n"),
744 if((cmd
->lc_argtype
& ARG_A
) && mb
.mb_type
== MB_VOID
){
745 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd
->lc_name
);
749 if(cmd
->lc_argtype
& ARG_O
)
750 OBSOLETE2(_("this command will be removed"), cmd
->lc_name
);
751 if(cmd
->lc_argtype
& ARG_V
)
752 temporary_arg_v_store
= NULL
;
754 if(wysh
&& (cmd
->lc_argtype
& ARG_ARGMASK
) != ARG_WYRALIST
)
755 n_err(_("`wysh' prefix doesn't affect `%s'\n"), cmd
->lc_name
);
756 /* TODO v15: strip PS_ARGLIST_MASK off, just in case the actual command
757 * TODO doesn't use any of those list commands which strip this mask,
758 * TODO and for now we misuse bits for checking relation to history;
759 * TODO argument state should be property of a per-command carrier instead */
760 pstate
&= ~PS_ARGLIST_MASK
;
761 switch(cmd
->lc_argtype
& ARG_ARGMASK
){
763 /* Message list defaulting to nearest forward legal message */
766 if((c
= getmsglist(cp
, n_msgvec
, cmd
->lc_msgflag
)) < 0)
769 if((n_msgvec
[0] = first(cmd
->lc_msgflag
, cmd
->lc_msgmask
)) != 0)
772 if(n_msgvec
[0] == 0){
773 if(!(pstate
& PS_HOOK_MASK
))
774 printf(_("No applicable messages\n"));
777 e
= (*cmd
->lc_func
)(n_msgvec
);
781 /* Message list with no defaults, but no error if none exist */
782 if(n_msgvec
== NULL
){
784 n_err(_("Invalid use of message list\n"));
787 if((c
= getmsglist(cp
, n_msgvec
, cmd
->lc_msgflag
)) < 0)
789 e
= (*cmd
->lc_func
)(n_msgvec
);
793 /* Just the straight string, with leading blanks removed */
794 while(whitechar(*cp
))
796 e
= (*cmd
->lc_func
)(cp
);
811 if((c
= getrawlist((c
!= 0), arglist
, NELEM(arglist
), cp
, line
.l
)) < 0){
812 n_err(_("Invalid argument list\n"));
815 if(c
< cmd
->lc_minargs
){
816 n_err(_("`%s' requires at least %d arg(s)\n"),
817 cmd
->lc_name
, cmd
->lc_minargs
);
821 if(c
> cmd
->lc_maxargs
){
822 n_err(_("`%s' takes no more than %d arg(s)\n"),
823 cmd
->lc_name
, cmd
->lc_maxargs
);
827 e
= (*cmd
->lc_func
)(arglist
);
831 /* Just the constant zero, for exiting, eg. */
832 e
= (*cmd
->lc_func
)(0);
836 DBG( n_panic(_("Implementation error: unknown argument type: %d"),
837 cmd
->lc_argtype
& ARG_ARGMASK
); )
841 if(e
== 0 && (cmd
->lc_argtype
& ARG_V
) &&
842 (cp
= temporary_arg_v_store
) != NULL
){
843 temporary_arg_v_store
= NULL
;
844 evp
->le_new_content
= cp
;
847 /* TODO v15 with PS_MSGLIST_GABBY the history entry is at least gabby */
848 if(!(cmd
->lc_argtype
& ARG_H
) && !(pstate
& PS_MSGLIST_GABBY
))
849 evp
->le_add_history
= TRU1
;
852 /* Exit the current source file on error TODO what a mess! */
854 pstate
&= ~PS_EVAL_ERROR
;
856 pstate
|= PS_EVAL_ERROR
;
857 if(e
< 0 || (pstate
& PS_ROBOT
)){ /* FIXME */
866 if((cmd
->lc_argtype
& ARG_P
) && ok_blook(autoprint
))
868 line
.s
= savestr("type");
869 line
.l
= sizeof("type") -1;
870 gp
= (struct a_lex_ghost
*)-1; /* Avoid `ghost' interpretation */
874 if(!(pstate
& (PS_SOURCING
| PS_HOOK_MASK
)) && !(cmd
->lc_argtype
& ARG_T
))
875 pstate
|= PS_SAW_COMMAND
;
877 pstate
&= ~PS_EVAL_ERROR
;
882 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
888 static struct a_lex_cmd
const *
889 a_lex__firstfit(char const *comm
){ /* TODO *hashtable*! linear list search!!! */
890 struct a_lex_cmd
const *cp
;
893 for(cp
= a_lex_cmd_tab
; PTRCMP(cp
, <, &a_lex_cmd_tab
[NELEM(a_lex_cmd_tab
)]);
895 if(*comm
== *cp
->lc_name
&& is_prefix(comm
, cp
->lc_name
))
905 NYD_X
; /* Signal handler */
913 NYD_X
; /* Signal handler */
916 safe_signal(SIGINT
, a_lex_onintr
);
918 a_lex_unstack(TRUM1
);
920 termios_state_reset();
921 close_all_files(); /* FIXME .. to outer level ONLU! */
928 n_err_sighdl(_("Interrupt\n"));
929 safe_signal(SIGPIPE
, a_lex_oldpipe
);
930 siglongjmp(srbuf
, 0); /* FIXME get rid */
934 a_lex_unstack(bool_t eval_error
){
935 struct a_lex_input_stack
*lip
;
938 if((lip
= a_lex_input
) == NULL
){
939 /* If called from a_lex_onintr(), be silent FIXME */
940 pstate
&= ~(PS_SOURCING
| PS_ROBOT
);
941 if(eval_error
== TRUM1
|| !(pstate
& PS_STARTED
))
946 if(lip
->li_flags
& a_LEX_MACRO
){
947 if(lip
->li_flags
& a_LEX_MACRO_FREE_DATA
){
950 while(*(lp
= &lip
->li_lines
[lip
->li_loff
]) != NULL
){
954 /* Part of lip's memory chunk, then */
955 if(!(lip
->li_flags
& a_LEX_MACRO_CMD
))
959 if(lip
->li_flags
& a_LEX_PIPE
)
960 /* XXX command manager should -TERM then -KILL instead of hoping
961 * XXX for exit of provider due to EPIPE / SIGPIPE */
962 Pclose(lip
->li_file
, TRU1
);
964 Fclose(lip
->li_file
);
967 if(!condstack_take(lip
->li_cond
)){
968 n_err(_("Unmatched `if' at end of %s %s\n"),
969 ((lip
->li_flags
& a_LEX_MACRO
970 ? (lip
->li_flags
& a_LEX_MACRO_CMD
? _("command") : _("macro"))
971 : _("`source'd file"))),
976 if((a_lex_input
= lip
->li_outer
) == NULL
){
977 pstate
&= ~(PS_SOURCING
| PS_ROBOT
);
979 if((a_lex_input
->li_flags
& (a_LEX_MACRO
| a_LEX_SUPER_MACRO
)) ==
980 (a_LEX_MACRO
| a_LEX_SUPER_MACRO
))
981 pstate
&= ~PS_SOURCING
;
982 assert(pstate
& PS_ROBOT
);
987 if(lip
->li_flags
& a_LEX_FREE
)
990 if(UNLIKELY(a_lex_input
!= NULL
&& eval_error
== TRUM1
))
991 a_lex_unstack(TRUM1
);
997 if(options
& OPT_D_V
)
998 n_alert(_("Stopped %s %s due to errors%s"),
1000 ? (lip
->li_flags
& a_LEX_MACRO
1001 ? (lip
->li_flags
& a_LEX_MACRO_CMD
1002 ? _("evaluating command") : _("evaluating macro"))
1003 : (lip
->li_flags
& a_LEX_PIPE
1004 ? _("executing `source'd pipe")
1005 : _("loading `source'd file")))
1006 : (lip
->li_flags
& a_LEX_MACRO
1007 ? (lip
->li_flags
& a_LEX_MACRO_X_OPTION
1008 ? _("evaluating command line") : _("evaluating macro"))
1009 : _("loading initialization resource"))),
1011 (options
& OPT_DEBUG
? "" : _(" (enable *debug* for trace)")));
1012 if(lip
->li_flags
& a_LEX_FREE
)
1016 if(!(options
& OPT_INTERACTIVE
) && !(pstate
& PS_STARTED
)){
1017 if(options
& OPT_D_V
)
1018 n_alert(_("Non-interactive, bailing out due to errors "
1019 "in startup load phase"));
1026 a_lex_source_file(char const *file
, bool_t silent_error
){
1027 struct a_lex_input_stack
*lip
;
1036 /* Being a command argument file is space-trimmed *//* TODO v15 with
1037 * TODO WYRALIST this is no longer necessary true, and for that we
1038 * TODO don't set _PARSE_TRIMSPACE because we cannot! -> cmd_tab.h!! */
1040 ((ispipe
= (!silent_error
&& (nlen
= strlen(file
)) > 0 &&
1041 file
[--nlen
] == '|')))
1045 for(nlen
= strlen(file
); nlen
> 0;){
1051 nbuf
= savestrbuf(file
, nlen
);
1060 if((fip
= Popen(nbuf
/* #if 0 above = savestrbuf(file, nlen)*/, "r",
1061 ok_vlook(SHELL
), NULL
, COMMAND_FD_NULL
)) == NULL
){
1062 if(!silent_error
|| (options
& OPT_D_V
))
1066 }else if((nbuf
= fexpand(file
, FEXP_LOCAL
)) == NULL
)
1068 else if((fip
= Fopen(nbuf
, "r")) == NULL
){
1069 if(!silent_error
|| (options
& OPT_D_V
))
1074 lip
= smalloc(sizeof(*lip
) -
1075 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) +
1076 (nlen
= strlen(nbuf
) +1));
1077 lip
->li_outer
= a_lex_input
;
1079 lip
->li_cond
= condstack_release();
1080 lip
->li_flags
= (ispipe
? a_LEX_FREE
| a_LEX_PIPE
: a_LEX_FREE
) |
1081 (a_lex_input
!= NULL
&& (a_lex_input
->li_flags
& a_LEX_SUPER_MACRO
)
1082 ? a_LEX_SUPER_MACRO
: 0);
1083 memcpy(lip
->li_name
, nbuf
, nlen
);
1085 pstate
|= PS_SOURCING
| PS_ROBOT
;
1087 a_commands_recursive(n_LEXINPUT_NONE
| n_LEXINPUT_NL_ESC
);
1088 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
1091 return silent_error
? TRU1
: (fip
!= NULL
);
1095 a_lex_load(struct a_lex_input_stack
*lip
){
1099 assert(!(pstate
& PS_STARTED
));
1100 assert(a_lex_input
== NULL
);
1103 * Any errors in the start-up file shall either cause mailx to terminate
1104 * with a diagnostic message and a non-zero status or to continue after
1105 * writing a diagnostic message, ignoring the remainder of the lines in
1106 * the start-up file. */
1107 lip
->li_cond
= condstack_release();
1109 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
1110 pstate |= PS_ROBOT |
1111 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
1113 pstate
|= PS_ROBOT
| PS_SOURCING
;
1114 if(options
& OPT_D_V
)
1115 n_err(_("Loading %s\n"), n_shexp_quote_cp(lip
->li_name
, FAL0
));
1117 if(!(rv
= n_commands())){
1118 if(!(options
& OPT_INTERACTIVE
)){
1119 if(options
& OPT_D_V
)
1120 n_alert(_("Non-interactive program mode, forced exit"));
1124 /* PS_EXIT handled by callers */
1130 a_commands_recursive(enum n_lexinput_flags lif
){
1131 struct a_lex_eval_ctx ev
;
1135 memset(&ev
, 0, sizeof ev
);
1137 /* FIXME sigkondom */
1138 n_COLOUR( n_colour_env_push(); )
1143 /* Read a line of commands and handle end of file specially */
1144 ev
.le_line
.l
= ev
.le_line_size
;
1145 n
= n_lex_input(lif
, NULL
, &ev
.le_line
.s
, &ev
.le_line
.l
,
1147 ev
.le_line_size
= (ui32_t
)ev
.le_line
.l
;
1148 ev
.le_line
.l
= (ui32_t
)n
;
1153 if(a_lex_evaluate(&ev
)){
1158 if((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)){
1159 if(exit_status
!= EXIT_OK
)
1164 n_COLOUR( n_colour_env_pop(FAL0
); )
1166 if(ev
.le_line
.s
!= NULL
)
1173 n_commands(void){ /* FIXME */
1174 struct a_lex_eval_ctx ev
;
1176 bool_t
volatile rv
= TRU1
;
1179 if (!(pstate
& PS_SOURCING
)) {
1180 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
1181 safe_signal(SIGINT
, &a_lex_onintr
);
1182 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
1183 safe_signal(SIGHUP
, &a_lex_hangup
);
1185 a_lex_oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1186 safe_signal(SIGPIPE
, a_lex_oldpipe
);
1188 memset(&ev
, 0, sizeof ev
);
1190 (void)sigsetjmp(srbuf
, 1); /* FIXME get rid */
1192 char *temporary_orig_line
; /* XXX eval_ctx.le_line not yet constant */
1194 n_COLOUR( n_colour_env_pop(TRU1
); )
1196 /* TODO Unless we have our signal manager (or however we do it) child
1197 * TODO processes may have time slots where their execution isn't
1198 * TODO protected by signal handlers (in between start and setup
1199 * TODO completed). close_all_files() is only called from onintr()
1200 * TODO so those may linger possibly forever */
1201 if(!(pstate
& PS_SOURCING
))
1206 temporary_localopts_free(); /* XXX intermediate hack */
1207 sreset((pstate
& PS_SOURCING
) != 0);
1208 if (!(pstate
& PS_SOURCING
)) {
1211 /* TODO Note: this buffer may contain a password. We should redefine
1212 * TODO the code flow which has to do that */
1213 if ((cp
= termios_state
.ts_linebuf
) != NULL
) {
1214 termios_state
.ts_linebuf
= NULL
;
1215 termios_state
.ts_linesize
= 0;
1216 free(cp
); /* TODO pool give-back */
1218 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1219 if (ev
.le_line
.l
> LINESIZE
* 3) {
1220 free(ev
.le_line
.s
); /* TODO pool! but what? */
1221 ev
.le_line
.s
= NULL
;
1222 ev
.le_line
.l
= ev
.le_line_size
= 0;
1226 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
1229 cp
= ok_vlook(newmail
);
1230 if ((options
& OPT_TTYIN
) && cp
!= NULL
) {
1233 /* FIXME TEST WITH NOPOLL ETC. !!! */
1234 n
= (cp
!= NULL
&& strcmp(cp
, "nopoll"));
1235 if ((mb
.mb_type
== MB_FILE
&& !stat(mailname
, &st
) &&
1236 st
.st_size
> mailsize
) ||
1237 (mb
.mb_type
== MB_MAILDIR
&& n
!= 0)) {
1238 size_t odot
= PTR2SIZE(dot
- message
);
1239 ui32_t odid
= (pstate
& PS_DID_PRINT_DOT
);
1241 if (setfile(mailname
,
1243 ((mb
.mb_perm
& MB_DELE
) ? 0 : FEDIT_RDONLY
)) < 0) {
1244 exit_status
|= EXIT_ERR
;
1248 dot
= message
+ odot
;
1253 exit_status
= EXIT_OK
;
1256 /* Read a line of commands and handle end of file specially */
1258 ev
.le_line
.l
= ev
.le_line_size
;
1259 n
= n_lex_input(n_LEXINPUT_CTX_BASE
| n_LEXINPUT_NL_ESC
, NULL
,
1260 &ev
.le_line
.s
, &ev
.le_line
.l
, ev
.le_new_content
);
1261 ev
.le_line_size
= (ui32_t
)ev
.le_line
.l
;
1262 ev
.le_line
.l
= (ui32_t
)n
;
1265 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1266 if (!(pstate
& PS_ROBOT
) &&
1267 (options
& OPT_INTERACTIVE
) && ok_blook(ignoreeof
)) {
1268 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1269 n_msleep(500, FAL0
);
1275 temporary_orig_line
= ((pstate
& PS_SOURCING
) ||
1276 !(options
& OPT_INTERACTIVE
)) ? NULL
1277 : savestrbuf(ev
.le_line
.s
, ev
.le_line
.l
);
1278 pstate
&= ~PS_HOOK_MASK
;
1279 if (a_lex_evaluate(&ev
)) {
1280 if (!(pstate
& PS_STARTED
)) /* TODO mess; join PS_EVAL_ERROR.. */
1285 if ((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)) {
1286 if (exit_status
!= EXIT_OK
)
1288 if ((pstate
& (PS_SOURCING
| PS_EVAL_ERROR
)) == PS_EVAL_ERROR
) {
1289 exit_status
= EXIT_ERR
;
1293 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
1294 if (ev
.le_new_content
!= NULL
)
1296 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1297 if (temporary_orig_line
!= NULL
)
1298 n_tty_addhist(temporary_orig_line
, !ev
.le_add_history
);
1301 if(pstate
& PS_EXIT
)
1306 if (ev
.le_line
.s
!= NULL
)
1308 if (pstate
& PS_SOURCING
)
1315 (n_lex_input
)(enum n_lexinput_flags lif
, char const *prompt
, char **linebuf
,
1316 size_t *linesize
, char const *string SMALLOC_DEBUG_ARGS
){
1317 /* TODO readline: linebuf pool!; n_lex_input should return si64_t */
1319 bool_t doprompt
, dotty
;
1324 /* Special case macro mode: never need to prompt, lines have always been
1325 * unfolded already */
1326 if(a_lex_input
!= NULL
&& (a_lex_input
->li_flags
& a_LEX_MACRO
)){
1327 if(*linebuf
!= NULL
)
1330 if((*linebuf
= a_lex_input
->li_lines
[a_lex_input
->li_loff
]) == NULL
){
1336 ++a_lex_input
->li_loff
;
1337 *linesize
= strlen(*linebuf
);
1338 if(!(a_lex_input
->li_flags
& a_LEX_MACRO_FREE_DATA
))
1339 *linebuf
= sbufdup(*linebuf
, *linesize
);
1341 iftype
= (a_lex_input
->li_flags
& a_LEX_MACRO_X_OPTION
)
1342 ? "-X OPTION" : "MACRO";
1344 pstate
|= PS_READLINE_NL
;
1347 pstate
&= ~PS_READLINE_NL
;
1349 iftype
= (!(pstate
& PS_STARTED
) ? "LOAD"
1350 : (pstate
& PS_SOURCING
) ? "SOURCE" : "READ");
1351 doprompt
= ((pstate
& (PS_STARTED
| PS_ROBOT
)) == PS_STARTED
&&
1352 (options
& OPT_INTERACTIVE
));
1353 dotty
= (doprompt
&& !ok_blook(line_editor_disable
));
1356 else if(prompt
== NULL
)
1357 prompt
= getprompt();
1359 /* Ensure stdout is flushed first anyway */
1360 if(!dotty
&& prompt
== NULL
)
1363 ifile
= (a_lex_input
!= NULL
) ? a_lex_input
->li_file
: stdin
;
1364 assert(ifile
!= NULL
);
1366 for(nold
= n
= 0;;){
1368 assert(ifile
== stdin
);
1369 if(string
!= NULL
&& (n
= (int)strlen(string
)) > 0){
1373 *linesize
= (size_t)n
+ LINESIZE
+1;
1374 *linebuf
= (srealloc
)(*linebuf
, *linesize SMALLOC_DEBUG_ARGSCALL
);
1375 memcpy(*linebuf
, string
, (size_t)n
+1);
1378 /* TODO if nold>0, don't redisplay the entire line!
1379 * TODO needs complete redesign ... */
1380 n
= (n_tty_readline
)(lif
, prompt
, linebuf
, linesize
, n
1381 SMALLOC_DEBUG_ARGSCALL
);
1383 if(prompt
!= NULL
) {
1385 fputs(prompt
, stdout
);
1389 n
= (readline_restart
)(ifile
, linebuf
, linesize
, n
1390 SMALLOC_DEBUG_ARGSCALL
);
1392 if(n
> 0 && nold
> 0){
1394 char const *cp
= *linebuf
+ nold
;
1396 while(blankspacechar(*cp
) && nold
+ i
< n
)
1399 memmove(*linebuf
+ nold
, cp
, n
- nold
- i
);
1401 (*linebuf
)[n
] = '\0';
1410 * An unquoted <backslash> at the end of a command line shall
1411 * be discarded and the next line shall continue the command */
1412 if(!(lif
& n_LEXINPUT_NL_ESC
) || (*linebuf
)[n
- 1] != '\\'){
1414 pstate
|= PS_READLINE_NL
;
1417 /* Definitely outside of quotes, thus the quoting rules are so that an
1418 * uneven number of successive backslashs at EOL is a continuation */
1422 for(j
= 1, i
= (size_t)n
- 1; i
-- > 0; ++j
)
1423 if((*linebuf
)[i
] != '\\')
1428 (*linebuf
)[nold
= --n
] = '\0';
1429 if(prompt
!= NULL
&& *prompt
!= '\0')
1430 prompt
= ".. "; /* XXX PS2 .. */
1435 (*linebuf
)[*linesize
= n
] = '\0';
1439 if(lif
& n_LEXINPUT_DROP_TRAIL_SPC
){
1443 for(cp
= &(*linebuf
)[i
= (size_t)n
];; --i
){
1445 if(!blankspacechar(c
))
1448 (*linebuf
)[n
= (int)i
] = '\0';
1451 if(lif
& n_LEXINPUT_DROP_LEAD_SPC
){
1455 for(cp
= &(*linebuf
)[0], j
= (size_t)n
, i
= 0; i
< j
; ++i
){
1457 if(!blankspacechar(c
))
1461 memcpy(&(*linebuf
)[0], &(*linebuf
)[i
], j
-= i
);
1462 (*linebuf
)[n
= (int)j
] = '\0';
1465 #endif /* 0 (notyet - must take care for backslash escaped space) */
1467 if(options
& OPT_D_VV
)
1468 n_err(_("%s %d bytes <%s>\n"), iftype
, n
, *linebuf
);
1470 if (pstate
& PS_PSTATE_PENDMASK
)
1471 a_lex_update_pstate();
1477 n_lex_input_cp(enum n_lexinput_flags lif
, char const *prompt
,
1478 char const *string
){
1479 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1489 n
= n_lex_input(lif
, prompt
, &linebuf
, &linesize
, string
);
1490 if(n
> 0 && *(rv
= savestrbuf(linebuf
, (size_t)n
)) != '\0' &&
1491 (lif
& n_LEXINPUT_HIST_ADD
) && (options
& OPT_INTERACTIVE
))
1492 n_tty_addhist(rv
, ((lif
& n_LEXINPUT_HIST_GABBY
) != 0));
1501 n_load(char const *name
){
1502 struct a_lex_input_stack
*lip
;
1507 if(name
== NULL
|| *name
== '\0' || (fip
= Fopen(name
, "r")) == NULL
)
1510 i
= strlen(name
) +1;
1511 lip
= scalloc(1, sizeof(*lip
) -
1512 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) + i
);
1514 lip
->li_flags
= a_LEX_FREE
;
1515 memcpy(lip
->li_name
, name
, i
);
1524 n_load_Xargs(char const **lines
){
1525 static char const name
[] = "-X";
1527 ui8_t buf
[sizeof(struct a_lex_input_stack
) + sizeof name
];
1528 char const *srcp
, *xsrcp
;
1530 size_t imax
, i
, len
;
1531 struct a_lex_input_stack
*lip
;
1534 memset(buf
, 0, sizeof buf
);
1536 lip
->li_flags
= a_LEX_MACRO
| a_LEX_MACRO_FREE_DATA
|
1537 a_LEX_MACRO_X_OPTION
| a_LEX_SUPER_MACRO
;
1538 memcpy(lip
->li_name
, name
, sizeof name
);
1540 /* The problem being that we want to support reverse solidus newline
1541 * escaping also within multiline -X, i.e., POSIX says:
1542 * An unquoted <backslash> at the end of a command line shall
1543 * be discarded and the next line shall continue the command
1544 * Therefore instead of "lip->li_lines = UNCONST(lines)", duplicate the
1545 * entire lines array and set _MACRO_FREE_DATA */
1546 for(imax
= 0; lines
[imax
++] != NULL
;)
1548 lip
->li_lines
= smalloc(sizeof(*lip
->li_lines
) * imax
);
1550 /* For each of the input lines.. */
1551 for(i
= len
= 0, cp
= NULL
; (srcp
= *lines
) != NULL
;){
1555 if((j
= strlen(srcp
)) == 0){
1560 /* Separate one line from a possible multiline input string */
1561 if((xsrcp
= memchr(srcp
, '\n', j
)) != NULL
){
1563 j
= PTR2SIZE(xsrcp
- srcp
);
1567 /* The (separated) string may itself indicate soft newline escaping */
1568 if((keep
= (srcp
[j
- 1] == '\\'))){
1571 /* Need an uneven number of reverse solidus */
1572 for(xk
= 1, xj
= j
- 1; xj
-- > 0; ++xk
)
1573 if(srcp
[xj
] != '\\')
1581 /* Strip any leading WS from follow lines, then */
1583 while(j
> 0 && blankspacechar(*srcp
))
1587 if(i
+ 2 >= imax
){ /* TODO need a vector (main.c, here, ++) */
1589 lip
->li_lines
= srealloc(lip
->li_lines
, sizeof(*lip
->li_lines
) *
1592 lip
->li_lines
[i
] = cp
= srealloc(cp
, len
+ j
+1);
1593 memcpy(&cp
[len
], srcp
, j
);
1594 cp
[len
+= j
] = '\0';
1603 assert(i
+ 1 < imax
);
1604 lip
->li_lines
[i
++] = cp
;
1606 lip
->li_lines
[i
] = NULL
;
1609 if(pstate
& PS_EXIT
)
1619 rv
= (a_lex_source_file(*(char**)v
, FAL0
) == TRU1
) ? 0 : 1;
1625 c_source_if(void *v
){ /* XXX obsolete?, support file tests in `if' etc.! */
1629 rv
= (a_lex_source_file(*(char**)v
, TRU1
) != FAL0
) ? 0 : 1;
1635 n_source_macro(enum n_lexinput_flags lif
, char const *name
, char **lines
){
1636 struct a_lex_input_stack
*lip
;
1641 lip
= smalloc(sizeof(*lip
) -
1642 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) +
1643 (i
= strlen(name
) +1));
1644 lip
->li_outer
= a_lex_input
;
1645 lip
->li_file
= NULL
;
1646 lip
->li_cond
= condstack_release();
1647 lip
->li_flags
= a_LEX_FREE
| a_LEX_MACRO
| a_LEX_MACRO_FREE_DATA
|
1648 (a_lex_input
== NULL
|| (a_lex_input
->li_flags
& a_LEX_SUPER_MACRO
)
1649 ? a_LEX_SUPER_MACRO
: 0);
1651 lip
->li_lines
= lines
;
1652 memcpy(lip
->li_name
, name
, i
);
1656 rv
= a_commands_recursive(lif
);
1662 n_source_command(enum n_lexinput_flags lif
, char const *cmd
){
1663 struct a_lex_input_stack
*lip
;
1669 cmd
= sbufdup(cmd
, i
++);
1672 lip
= smalloc(sizeof(*lip
) -
1673 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) +
1674 ial
+ 2*sizeof(char*));
1675 lip
->li_outer
= a_lex_input
;
1676 lip
->li_file
= NULL
;
1677 lip
->li_cond
= condstack_release();
1678 lip
->li_flags
= a_LEX_FREE
| a_LEX_MACRO
| a_LEX_MACRO_FREE_DATA
|
1680 (a_lex_input
== NULL
|| (a_lex_input
->li_flags
& a_LEX_SUPER_MACRO
)
1681 ? a_LEX_SUPER_MACRO
: 0);
1683 lip
->li_lines
= (void*)(lip
->li_name
+ ial
);
1684 lip
->li_lines
[0] = UNCONST(cmd
); /* dup'ed above */
1685 lip
->li_lines
[1] = NULL
;
1686 memcpy(lip
->li_name
, cmd
, i
);
1690 rv
= a_commands_recursive(lif
);
1696 n_source_may_yield_control(void){
1697 return ((options
& OPT_INTERACTIVE
) &&
1698 (pstate
& PS_STARTED
) &&
1699 (!(pstate
& PS_ROBOT
) || (pstate
& PS_RECURSED
)) && /* Ok for ~: */
1700 (a_lex_input
== NULL
|| a_lex_input
->li_outer
== NULL
));