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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 bool_t a_lex_reset_on_stop
; /* do a reset() if stopped */
96 static sighandler_type a_lex_oldpipe
;
97 static struct a_lex_ghost
*a_lex_ghosts
;
98 /* a_lex_cmd_tab[] after fun protos */
101 static struct a_lex_input_stack
*a_lex_input
;
103 /* Isolate the command from the arguments */
104 static char *a_lex_isolate(char const *comm
);
106 /* Command ghost handling */
107 static int a_lex_c_ghost(void *v
);
108 static int a_lex_c_unghost(void *v
);
110 /* Print a list of all commands */
111 static int a_lex_c_list(void *v
);
113 static int a_lex__pcmd_cmp(void const *s1
, void const *s2
);
116 static int a_lex_c_quit(void *v
);
118 /* Print the binaries compiled-in features */
119 static int a_lex_c_features(void *v
);
121 /* Print the binaries version number */
122 static int a_lex_c_version(void *v
);
124 /* PS_STATE_PENDMASK requires some actions */
125 static void a_lex_update_pstate(void);
127 /* Evaluate a single command.
128 * .le_add_history and .le_new_content will be updated upon success.
129 * Command functions return 0 for success, 1 for error, and -1 for abort.
130 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
131 static int a_lex_evaluate(struct a_lex_eval_ctx
*evp
);
133 /* Get first-fit, or NULL */
134 static struct a_lex_cmd
const *a_lex__firstfit(char const *comm
);
136 /* When we wake up after ^Z, reprint the prompt */
137 static void a_lex_stop(int s
);
139 /* Branch here on hangup signal and simulate "exit" */
140 static void a_lex_hangup(int s
);
142 /* The following gets called on receipt of an interrupt. Close all open files
143 * except 0, 1, 2, and the temporary. Also, unstack all source files */
144 static void a_lex_onintr(int s
);
146 /* Pop the current input back to the previous level. Update the program state.
147 * If the argument is TRUM1 then we don't alert and error out if the stack
148 * doesn't exist at all */
149 static void a_lex_unstack(bool_t eval_error
);
151 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
152 static bool_t
a_lex_source_file(char const *file
, bool_t silent_error
);
154 /* System resource file load()ing or -X command line option array traversal */
155 static bool_t
a_lex_load(struct a_lex_input_stack
*lip
);
157 /* A simplified command loop for recursed state machines */
158 static bool_t
a_commands_recursive(void);
160 /* List of all commands, and list of commands which are specially treated
161 * and deduced in _evaluate(), but we need a list for _c_list() and
162 * print_comm_docstr() */
163 #ifdef HAVE_DOCSTRINGS
168 static struct a_lex_cmd
const a_lex_cmd_tab
[] = {
171 a_lex_special_cmd_tab
[] = {
173 DS(N_("\"Comment command\": ignore remaining (continuable) line")) },
175 DS(N_("Print out the preceding message")) }
180 a_lex_isolate(char const *comm
){
182 while(*comm
!= '\0' &&
183 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm
) == NULL
)
186 return UNCONST(comm
);
190 a_lex_c_ghost(void *v
){
191 struct a_lex_ghost
*lgp
, *gp
;
203 if((fp
= Ftmp(NULL
, "ghost", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
206 for(i
= 0, gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
207 fprintf(fp
, "wysh ghost %s %s\n",
208 gp
->lg_name
, n_shell_quote_cp(gp
->lg_cmd
.s
, TRU1
));
211 page_or_print(fp
, i
);
217 /* Verify the ghost name is a valid one */
218 if(*argv
[0] == '\0' || *a_lex_isolate(argv
[0]) != '\0'){
219 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv
[0]);
224 /* Show command of single ghost? */
226 for(gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
227 if(!strcmp(argv
[0], gp
->lg_name
)){
228 printf("wysh ghost %s %s\n",
229 gp
->lg_name
, n_shell_quote_cp(gp
->lg_cmd
.s
, TRU1
));
232 n_err(_("`ghost': no such alias: \"%s\"\n"), argv
[0]);
237 /* Define command for ghost: verify command content */
238 for(cl
= 0, i
= 1; (cp
= UNCONST(argv
[i
])) != NULL
; ++i
)
240 cl
+= strlen(cp
) +1; /* SP or NUL */
242 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv
[0]);
247 /* If the ghost already exists, recreate */
248 for(lgp
= NULL
, gp
= a_lex_ghosts
; gp
!= NULL
; lgp
= gp
, gp
= gp
->lg_next
)
249 if(!strcmp(gp
->lg_name
, argv
[0])){
251 lgp
->lg_next
= gp
->lg_next
;
253 a_lex_ghosts
= gp
->lg_next
;
258 nl
= strlen(argv
[0]) +1;
259 gp
= smalloc(sizeof(*gp
) - VFIELD_SIZEOF(struct a_lex_ghost
, lg_name
) +
261 gp
->lg_next
= a_lex_ghosts
;
263 memcpy(gp
->lg_name
, argv
[0], nl
);
264 cp
= gp
->lg_cmd
.s
= gp
->lg_name
+ nl
;
267 while(*++argv
!= NULL
)
268 if((i
= strlen(*argv
)) > 0){
269 memcpy(cp
, *argv
, i
);
280 a_lex_c_unghost(void *v
){
281 struct a_lex_ghost
*lgp
, *gp
;
282 char const **argv
, *cp
;
289 while((cp
= *argv
++) != NULL
){
290 if(cp
[0] == '*' && cp
[1] == '\0'){
291 while((gp
= a_lex_ghosts
) != NULL
){
292 a_lex_ghosts
= gp
->lg_next
;
296 for(lgp
= NULL
, gp
= a_lex_ghosts
; gp
!= NULL
;
297 lgp
= gp
, gp
= gp
->lg_next
)
298 if(!strcmp(gp
->lg_name
, cp
)){
300 lgp
->lg_next
= gp
->lg_next
;
302 a_lex_ghosts
= gp
->lg_next
;
306 n_err(_("`unghost': no such alias: \"%s\"\n"), cp
);
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_quit(void *v
){
408 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
409 * Otherwise return -1 to abort command loop */
416 a_lex_c_features(void *v
){
421 printf(_("Features: %s\n"), ok_vlook(features
));
427 a_lex_c_version(void *v
){
432 printf(_("Version %s\n"), ok_vlook(version
));
438 a_lex_update_pstate(void){
441 if(pstate
& PS_SIGWINCH_PEND
){
444 snprintf(buf
, sizeof buf
, "%d", scrnwidth
);
445 ok_vset(COLUMNS
, buf
);
446 snprintf(buf
, sizeof buf
, "%d", scrnheight
);
450 pstate
&= ~PS_PSTATE_PENDMASK
;
455 a_lex_evaluate(struct a_lex_eval_ctx
*evp
){
456 /* xxx old style(9), but also old code */
458 char _wordbuf
[2], *arglist
[MAXARGC
], *cp
, *word
;
459 struct a_lex_ghost
*gp
;
460 struct a_lex_cmd
const *cmd
;
469 line
= evp
->le_line
; /* XXX don't change original (buffer pointer) */
470 assert(line
.s
[line
.l
] == '\0');
471 evp
->le_add_history
= FAL0
;
472 evp
->le_new_content
= NULL
;
474 /* Command ghosts that refer to shell commands or macro expansion restart */
477 /* Strip the white space away from end and beginning of command */
481 for(cp
= &line
.s
[i
-1]; spacechar(*cp
); --cp
)
485 for(cp
= line
.s
; spacechar(*cp
); ++cp
)
487 line
.l
-= PTR2SIZE(cp
- line
.s
);
489 /* Ignore null commands (comments) */
493 /* Handle ! differently to get the correct lexical conventions */
497 /* Isolate the actual command; since it may not necessarily be
498 * separated from the arguments (as in `p1') we need to duplicate it to
499 * be able to create a NUL terminated version.
500 * We must be aware of several special one letter commands here */
501 else if((cp
= a_lex_isolate(cp
)) == arglist
[0] &&
502 (*cp
== '|' || *cp
== '~' || *cp
== '?'))
504 c
= (int)PTR2SIZE(cp
- arglist
[0]);
506 word
= UICMP(z
, c
, <, sizeof _wordbuf
) ? _wordbuf
: salloc(c
+1);
507 memcpy(word
, arglist
[0], c
);
510 /* Look up the command; if not found, bitch.
511 * Normally, a blank command would map to the first command in the
512 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
513 * confusion; act just the same for ghosts */
515 if((pstate
& PS_ROBOT
) || gp
!= NULL
)
517 cmd
= a_lex_cmd_tab
+ 0;
521 /* XXX It may be the argument parse adjuster */
522 if(!wysh
&& c
== sizeof("wysh") -1 && !asccasecmp(word
, "wysh")){
528 /* If this is the first evaluation, check command ghosts */
530 /* TODO relink list head, so it's sorted on usage over time?
531 * TODO in fact, there should be one hashmap over all commands and ghosts
532 * TODO so that the lookup could be made much more efficient than it is
533 * TODO now (two adjacent list searches! */
534 for(gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
535 if(!strcmp(word
, gp
->lg_name
)){
540 line
.s
= salloc(i
+ line
.l
+1);
541 memcpy(line
.s
, gp
->lg_cmd
.s
, i
);
542 memcpy(line
.s
+ i
, cp
, line
.l
);
543 line
.s
[i
+= line
.l
] = '\0';
546 line
.s
= gp
->lg_cmd
.s
;
547 line
.l
= gp
->lg_cmd
.l
;
553 if((cmd
= a_lex__firstfit(word
)) == NULL
|| cmd
->lc_func
== &c_cmdnotsupp
){
556 if(!(s
= condstack_isskip()) || (options
& OPT_D_V
))
557 n_err(_("Unknown command%s: `%s'\n"),
558 (s
? _(" (ignored due to `if' condition)") : ""), word
);
568 /* See if we should execute the command -- if a conditional we always
569 * execute it, otherwise, check the state of cond */
571 if(!(cmd
->lc_argtype
& ARG_F
) && condstack_isskip())
574 /* Process the arguments to the command, depending on the type it expects */
575 if(!(cmd
->lc_argtype
& ARG_M
) && (options
& OPT_SENDMODE
)){
576 n_err(_("May not execute `%s' while sending\n"), cmd
->lc_name
);
579 if((cmd
->lc_argtype
& ARG_S
) && !(pstate
& PS_STARTED
)){
580 n_err(_("May not execute `%s' during startup\n"), cmd
->lc_name
);
583 if((cmd
->lc_argtype
& ARG_I
) &&
584 !(options
& (OPT_INTERACTIVE
| OPT_BATCH_FLAG
))){
585 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
589 if((cmd
->lc_argtype
& ARG_R
) && (pstate
& PS_RECURSED
)){
590 n_err(_("Cannot invoke `%s' when in recursed mode (e.g., composing)\n"),
595 if((cmd
->lc_argtype
& ARG_W
) && !(mb
.mb_perm
& MB_DELE
)){
596 n_err(_("May not execute `%s' -- message file is read only\n"),
600 if((cmd
->lc_argtype
& ARG_A
) && mb
.mb_type
== MB_VOID
){
601 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd
->lc_name
);
605 if(cmd
->lc_argtype
& ARG_O
)
606 OBSOLETE2(_("this command will be removed"), cmd
->lc_name
);
607 if(cmd
->lc_argtype
& ARG_V
)
608 temporary_arg_v_store
= NULL
;
610 if(wysh
&& (cmd
->lc_argtype
& ARG_ARGMASK
) != ARG_WYRALIST
)
611 n_err(_("`wysh' prefix doesn't affect `%s'\n"), cmd
->lc_name
);
612 switch(cmd
->lc_argtype
& ARG_ARGMASK
){
614 /* Message list defaulting to nearest forward legal message */
617 if((c
= getmsglist(cp
, n_msgvec
, cmd
->lc_msgflag
)) < 0)
620 if((n_msgvec
[0] = first(cmd
->lc_msgflag
, cmd
->lc_msgmask
)) != 0)
623 if(n_msgvec
[0] == 0){
624 if(!(pstate
& PS_HOOK_MASK
))
625 printf(_("No applicable messages\n"));
628 e
= (*cmd
->lc_func
)(n_msgvec
);
632 /* Message list with no defaults, but no error if none exist */
633 if(n_msgvec
== NULL
){
635 n_err(_("Invalid use of \"message list\"\n"));
638 if((c
= getmsglist(cp
, n_msgvec
, cmd
->lc_msgflag
)) < 0)
640 e
= (*cmd
->lc_func
)(n_msgvec
);
644 /* Just the straight string, with leading blanks removed */
645 while(whitechar(*cp
))
647 e
= (*cmd
->lc_func
)(cp
);
662 if((c
= getrawlist((c
!= 0), arglist
, NELEM(arglist
), cp
, line
.l
)) < 0){
663 n_err(_("Invalid argument list\n"));
666 if(c
< cmd
->lc_minargs
){
667 n_err(_("`%s' requires at least %d arg(s)\n"),
668 cmd
->lc_name
, cmd
->lc_minargs
);
672 if(c
> cmd
->lc_maxargs
){
673 n_err(_("`%s' takes no more than %d arg(s)\n"),
674 cmd
->lc_name
, cmd
->lc_maxargs
);
678 e
= (*cmd
->lc_func
)(arglist
);
682 /* Just the constant zero, for exiting, eg. */
683 e
= (*cmd
->lc_func
)(0);
687 DBG( n_panic(_("Implementation error: unknown argument type: %d"),
688 cmd
->lc_argtype
& ARG_ARGMASK
); )
692 if(e
== 0 && (cmd
->lc_argtype
& ARG_V
) &&
693 (cp
= temporary_arg_v_store
) != NULL
){
694 temporary_arg_v_store
= NULL
;
695 evp
->le_new_content
= cp
;
698 if(!(cmd
->lc_argtype
& ARG_H
) && !(pstate
& PS_MSGLIST_SAW_NO
))
699 evp
->le_add_history
= TRU1
;
702 /* Exit the current source file on error TODO what a mess! */
704 pstate
&= ~PS_EVAL_ERROR
;
706 pstate
|= PS_EVAL_ERROR
;
707 if(e
< 0 || (pstate
& PS_ROBOT
)){ /* FIXME */
716 if((cmd
->lc_argtype
& ARG_P
) && ok_blook(autoprint
))
718 line
.s
= savestr("type");
719 line
.l
= sizeof("type") -1;
720 gp
= (struct a_lex_ghost
*)-1; /* Avoid `ghost' interpretation */
724 if(!(pstate
& (PS_SOURCING
| PS_HOOK_MASK
)) && !(cmd
->lc_argtype
& ARG_T
))
725 pstate
|= PS_SAW_COMMAND
;
727 pstate
&= ~PS_EVAL_ERROR
;
732 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
738 static struct a_lex_cmd
const *
739 a_lex__firstfit(char const *comm
){ /* TODO *hashtable*! linear list search!!! */
740 struct a_lex_cmd
const *cp
;
743 for(cp
= a_lex_cmd_tab
; PTRCMP(cp
, <, &a_lex_cmd_tab
[NELEM(a_lex_cmd_tab
)]);
745 if(*comm
== *cp
->lc_name
&& is_prefix(comm
, cp
->lc_name
))
755 sighandler_type old_action
;
757 NYD_X
; /* Signal handler */
759 old_action
= safe_signal(s
, SIG_DFL
);
763 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
765 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
767 safe_signal(s
, old_action
);
769 if(a_lex_reset_on_stop
){
770 a_lex_reset_on_stop
= 0;
771 n_TERMCAP_RESUME(TRU1
);
772 siglongjmp(srbuf
, 0); /* FIXME get rid */
778 NYD_X
; /* Signal handler */
786 NYD_X
; /* Signal handler */
788 safe_signal(SIGINT
, a_lex_onintr
);
790 a_lex_unstack(TRUM1
);
792 termios_state_reset();
793 close_all_files(); /* FIXME .. to outer level ONLU! */
800 n_err_sighdl(_("Interrupt\n"));
801 safe_signal(SIGPIPE
, a_lex_oldpipe
);
802 siglongjmp(srbuf
, 0); /* FIXME get rid */
806 a_lex_unstack(bool_t eval_error
){
807 struct a_lex_input_stack
*lip
;
810 if((lip
= a_lex_input
) == NULL
){
811 /* If called from a_lex_onintr(), be silent FIXME */
812 pstate
&= ~(PS_SOURCING
| PS_ROBOT
);
813 if(eval_error
== TRUM1
|| !(pstate
& PS_STARTED
))
818 if(lip
->li_flags
& a_LEX_MACRO
){
819 if(lip
->li_flags
& a_LEX_MACRO_FREE_DATA
){
822 while(*(lp
= &lip
->li_lines
[lip
->li_loff
]) != NULL
){
826 /* Part of lip's memory chunk, then */
827 if(!(lip
->li_flags
& a_LEX_MACRO_CMD
))
831 if(lip
->li_flags
& a_LEX_PIPE
)
832 /* XXX command manager should -TERM then -KILL instead of hoping
833 * XXX for exit of provider due to EPIPE / SIGPIPE */
834 Pclose(lip
->li_file
, TRU1
);
836 Fclose(lip
->li_file
);
839 if(!condstack_take(lip
->li_cond
)){
840 n_err(_("Unmatched `if' at end of %s \"%s\"\n"),
841 ((lip
->li_flags
& a_LEX_MACRO
842 ? (lip
->li_flags
& a_LEX_MACRO_CMD
? _("command") : _("macro"))
843 : _("`source'd file"))),
848 if((a_lex_input
= lip
->li_outer
) == NULL
){
849 pstate
&= ~(PS_SOURCING
| PS_ROBOT
);
851 if((a_lex_input
->li_flags
& (a_LEX_MACRO
| a_LEX_SUPER_MACRO
)) ==
852 (a_LEX_MACRO
| a_LEX_SUPER_MACRO
))
853 pstate
&= ~PS_SOURCING
;
854 assert(pstate
& PS_ROBOT
);
859 if(lip
->li_flags
& a_LEX_FREE
)
862 if(UNLIKELY(a_lex_input
!= NULL
&& eval_error
== TRUM1
))
863 a_lex_unstack(TRUM1
);
869 if(options
& OPT_D_V
)
870 n_alert(_("Stopped %s \"%s\" due to errors%s"),
872 ? (lip
->li_flags
& a_LEX_MACRO
873 ? (lip
->li_flags
& a_LEX_MACRO_CMD
874 ? _("evaluating command") : _("evaluating macro"))
875 : (lip
->li_flags
& a_LEX_PIPE
876 ? _("executing `source'd pipe")
877 : _("loading `source'd file")))
878 : (lip
->li_flags
& a_LEX_MACRO
879 ? (lip
->li_flags
& a_LEX_MACRO_X_OPTION
880 ? _("evaluating command line") : _("evaluating macro"))
881 : _("loading initialization resource"))),
883 (options
& OPT_DEBUG
? "" : _(" (enable *debug* for trace)")));
884 if(lip
->li_flags
& a_LEX_FREE
)
888 if(!(options
& OPT_INTERACTIVE
) && !(pstate
& PS_STARTED
)){
889 if(options
& OPT_D_V
)
890 n_alert(_("Non-interactive, bailing out due to errors "
891 "in startup load phase"));
898 a_lex_source_file(char const *file
, bool_t silent_error
){
899 struct a_lex_input_stack
*lip
;
908 /* Being a command argument file is space-trimmed */
909 if((ispipe
= (!silent_error
&& (nlen
= strlen(file
)) > 0 &&
910 file
[--nlen
] == '|'))){
911 if((fip
= Popen(nbuf
= savestrbuf(file
, nlen
), "r",
912 ok_vlook(SHELL
), NULL
, COMMAND_FD_NULL
)) == NULL
){
913 if(!silent_error
|| (options
& OPT_D_V
))
917 }else if((nbuf
= fexpand(file
, FEXP_LOCAL
)) == NULL
)
919 else if((fip
= Fopen(nbuf
, "r")) == NULL
){
920 if(!silent_error
|| (options
& OPT_D_V
))
925 lip
= smalloc(sizeof(*lip
) -
926 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) +
927 (nlen
= strlen(nbuf
) +1));
928 lip
->li_outer
= a_lex_input
;
930 lip
->li_cond
= condstack_release();
931 lip
->li_flags
= (ispipe
? a_LEX_PIPE
: a_LEX_NONE
) |
932 (a_lex_input
!= NULL
&& (a_lex_input
->li_flags
& a_LEX_SUPER_MACRO
)
933 ? a_LEX_SUPER_MACRO
: 0);
934 memcpy(lip
->li_name
, nbuf
, nlen
);
936 pstate
|= PS_SOURCING
| PS_ROBOT
;
938 a_commands_recursive();
939 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
942 return silent_error
? TRU1
: (fip
!= NULL
);
946 a_lex_load(struct a_lex_input_stack
*lip
){
950 assert(!(pstate
& PS_STARTED
));
951 assert(a_lex_input
== NULL
);
954 * Any errors in the start-up file shall either cause mailx to terminate
955 * with a diagnostic message and a non-zero status or to continue after
956 * writing a diagnostic message, ignoring the remainder of the lines in
957 * the start-up file. */
958 lip
->li_cond
= condstack_release();
960 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
962 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
964 pstate
|= PS_ROBOT
| PS_SOURCING
;
965 if(options
& OPT_D_V
)
966 n_err(_("Loading \"%s\"\n"), lip
->li_name
);
968 if(!(rv
= n_commands())){
969 if(!(options
& OPT_INTERACTIVE
)){
970 if(options
& OPT_D_V
)
971 n_alert(_("Non-interactive program mode, forced exit"));
975 /* PS_EXIT handled by callers */
981 a_commands_recursive(void){
982 struct a_lex_eval_ctx ev
;
986 memset(&ev
, 0, sizeof ev
);
988 /* FIXME sigkondom */
989 n_COLOUR( n_colour_env_push(); )
994 /* Read a line of commands and handle end of file specially */
995 ev
.le_line
.l
= ev
.le_line_size
;
996 n
= n_lex_input(NULL
, TRU1
, &ev
.le_line
.s
, &ev
.le_line
.l
,
998 ev
.le_line_size
= (ui32_t
)ev
.le_line
.l
;
999 ev
.le_line
.l
= (ui32_t
)n
;
1004 if(a_lex_evaluate(&ev
)){
1009 if((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)){
1010 if(exit_status
!= EXIT_OK
)
1015 n_COLOUR( n_colour_env_pop(FAL0
); )
1017 if(ev
.le_line
.s
!= NULL
)
1023 #ifdef HAVE_DOCSTRINGS
1025 n_print_comm_docstr(char const *comm
){
1026 struct a_lex_ghost
const *gp
;
1027 struct a_lex_cmd
const *cp
, *cpmax
;
1031 /* Ghosts take precedence */
1032 for(gp
= a_lex_ghosts
; gp
!= NULL
; gp
= gp
->lg_next
)
1033 if(!strcmp(comm
, gp
->lg_name
)){
1034 printf("%s -> ", comm
);
1035 comm
= gp
->lg_cmd
.s
;
1039 cpmax
= &(cp
= a_lex_cmd_tab
)[NELEM(a_lex_cmd_tab
)];
1041 for(; PTRCMP(cp
, <, cpmax
); ++cp
){
1042 if(!strcmp(comm
, cp
->lc_name
))
1043 printf("%s: %s\n", comm
, V_(cp
->lc_doc
));
1044 else if(is_prefix(comm
, cp
->lc_name
))
1045 printf("%s (%s): %s\n", comm
, cp
->lc_name
, V_(cp
->lc_doc
));
1051 if(!rv
&& PTRCMP(cpmax
, ==, &a_lex_cmd_tab
[NELEM(a_lex_cmd_tab
)])){
1052 cpmax
= &(cp
= a_lex_special_cmd_tab
)[NELEM(a_lex_special_cmd_tab
)];
1056 if(!rv
&& gp
!= NULL
){
1057 printf("\"%s\"\n", comm
);
1063 #endif /* HAVE_DOCSTRINGS */
1066 n_commands(void){ /* FIXME */
1067 struct a_lex_eval_ctx ev
;
1069 bool_t
volatile rv
= TRU1
;
1072 if (!(pstate
& PS_SOURCING
)) {
1073 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
1074 safe_signal(SIGINT
, &a_lex_onintr
);
1075 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
1076 safe_signal(SIGHUP
, &a_lex_hangup
);
1077 /* TODO We do a lot of redundant signal handling, especially
1078 * TODO with the command line editor(s); try to merge this */
1079 safe_signal(SIGTSTP
, &a_lex_stop
);
1080 safe_signal(SIGTTOU
, &a_lex_stop
);
1081 safe_signal(SIGTTIN
, &a_lex_stop
);
1083 a_lex_oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1084 safe_signal(SIGPIPE
, a_lex_oldpipe
);
1086 memset(&ev
, 0, sizeof ev
);
1088 (void)sigsetjmp(srbuf
, 1); /* FIXME get rid */
1090 char *temporary_orig_line
; /* XXX eval_ctx.le_line not yet constant */
1092 n_COLOUR( n_colour_env_pop(TRU1
); )
1094 /* TODO Unless we have our signal manager (or however we do it) child
1095 * TODO processes may have time slots where their execution isn't
1096 * TODO protected by signal handlers (in between start and setup
1097 * TODO completed). close_all_files() is only called from onintr()
1098 * TODO so those may linger possibly forever */
1099 if(!(pstate
& PS_SOURCING
))
1104 temporary_localopts_free(); /* XXX intermediate hack */
1105 sreset((pstate
& PS_SOURCING
) != 0);
1106 if (!(pstate
& PS_SOURCING
)) {
1109 /* TODO Note: this buffer may contain a password. We should redefine
1110 * TODO the code flow which has to do that */
1111 if ((cp
= termios_state
.ts_linebuf
) != NULL
) {
1112 termios_state
.ts_linebuf
= NULL
;
1113 termios_state
.ts_linesize
= 0;
1114 free(cp
); /* TODO pool give-back */
1116 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1117 if (ev
.le_line
.l
> LINESIZE
* 3) {
1118 free(ev
.le_line
.s
); /* TODO pool! but what? */
1119 ev
.le_line
.s
= NULL
;
1120 ev
.le_line
.l
= ev
.le_line_size
= 0;
1124 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
1127 cp
= ok_vlook(newmail
);
1128 if ((options
& OPT_TTYIN
) && cp
!= NULL
) {
1131 /* FIXME TEST WITH NOPOLL ETC. !!! */
1132 n
= (cp
!= NULL
&& strcmp(cp
, "nopoll"));
1133 if ((mb
.mb_type
== MB_FILE
&& !stat(mailname
, &st
) &&
1134 st
.st_size
> mailsize
) ||
1135 (mb
.mb_type
== MB_MAILDIR
&& n
!= 0)) {
1136 size_t odot
= PTR2SIZE(dot
- message
);
1137 ui32_t odid
= (pstate
& PS_DID_PRINT_DOT
);
1139 if (setfile(mailname
,
1141 ((mb
.mb_perm
& MB_DELE
) ? 0 : FEDIT_RDONLY
)) < 0) {
1142 exit_status
|= EXIT_ERR
;
1146 dot
= message
+ odot
;
1151 a_lex_reset_on_stop
= TRU1
;
1152 exit_status
= EXIT_OK
;
1155 /* Read a line of commands and handle end of file specially */
1157 ev
.le_line
.l
= ev
.le_line_size
;
1158 n
= n_lex_input(NULL
, TRU1
, &ev
.le_line
.s
, &ev
.le_line
.l
,
1160 ev
.le_line_size
= (ui32_t
)ev
.le_line
.l
;
1161 ev
.le_line
.l
= (ui32_t
)n
;
1162 a_lex_reset_on_stop
= FAL0
;
1165 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1166 if (!(pstate
& PS_ROBOT
) &&
1167 (options
& OPT_INTERACTIVE
) && ok_blook(ignoreeof
)) {
1168 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1169 n_msleep(500, FAL0
);
1175 temporary_orig_line
= ((pstate
& PS_SOURCING
) ||
1176 !(options
& OPT_INTERACTIVE
)) ? NULL
1177 : savestrbuf(ev
.le_line
.s
, ev
.le_line
.l
);
1178 pstate
&= ~PS_HOOK_MASK
;
1179 if (a_lex_evaluate(&ev
)) {
1180 if (!(pstate
& PS_STARTED
)) /* TODO mess; join PS_EVAL_ERROR.. */
1185 if ((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)) {
1186 if (exit_status
!= EXIT_OK
)
1188 if ((pstate
& (PS_SOURCING
| PS_EVAL_ERROR
)) == PS_EVAL_ERROR
) {
1189 exit_status
= EXIT_ERR
;
1193 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
1194 if (ev
.le_new_content
!= NULL
)
1196 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1197 if (temporary_orig_line
!= NULL
)
1198 n_tty_addhist(temporary_orig_line
, !ev
.le_add_history
);
1201 if(pstate
& PS_EXIT
)
1206 if (ev
.le_line
.s
!= NULL
)
1208 if (pstate
& PS_SOURCING
)
1215 (n_lex_input
)(char const *prompt
, bool_t nl_escape
, char **linebuf
,
1216 size_t *linesize
, char const *string SMALLOC_DEBUG_ARGS
){
1217 /* TODO readline: linebuf pool! */
1218 bool_t doprompt
, dotty
;
1223 /* Special case macro mode: never need to prompt, lines have always been
1224 * unfolded already */
1225 if(a_lex_input
!= NULL
&& (a_lex_input
->li_flags
& a_LEX_MACRO
)){
1226 char **lp
= &a_lex_input
->li_lines
[a_lex_input
->li_loff
];
1228 if(*linebuf
!= NULL
)
1231 if((*linebuf
= *lp
) == NULL
){
1235 ++a_lex_input
->li_loff
;
1236 n
= (int)(*linesize
= strlen(*linebuf
));
1238 if(!(a_lex_input
->li_flags
& a_LEX_MACRO_FREE_DATA
))
1239 *linebuf
= sbufdup(*linebuf
, *linesize
);
1241 if(options
& OPT_D_VV
)
1242 n_err(_("%s %d bytes <%.*s>\n"),
1243 (a_lex_input
->li_flags
& a_LEX_MACRO_X_OPTION
1244 ? _("-X option") : _("MACRO")),
1250 doprompt
= ((pstate
& (PS_STARTED
| PS_ROBOT
)) == PS_STARTED
&&
1251 (options
& OPT_INTERACTIVE
));
1252 dotty
= (doprompt
&& !ok_blook(line_editor_disable
));
1255 else if(prompt
== NULL
)
1256 prompt
= getprompt();
1258 /* Ensure stdout is flushed first anyway */
1259 if(!dotty
&& prompt
== NULL
)
1262 ifile
= (a_lex_input
!= NULL
) ? a_lex_input
->li_file
: stdin
;
1263 assert(ifile
!= NULL
);
1265 for(nold
= n
= 0;;){
1267 assert(ifile
== stdin
);
1268 if(string
!= NULL
&& (n
= (int)strlen(string
)) > 0){
1272 *linesize
= (size_t)n
+ LINESIZE
+1;
1273 *linebuf
= (srealloc
)(*linebuf
, *linesize SMALLOC_DEBUG_ARGSCALL
);
1274 memcpy(*linebuf
, string
, (size_t)n
+1);
1277 /* TODO if nold>0, don't redisplay the entire line!
1278 * TODO needs complete redesign ... */
1279 n
= (n_tty_readline
)(prompt
, linebuf
, linesize
, n
1280 SMALLOC_DEBUG_ARGSCALL
);
1282 if(prompt
!= NULL
) {
1284 fputs(prompt
, stdout
);
1288 n
= (readline_restart
)(ifile
, linebuf
, linesize
, n
1289 SMALLOC_DEBUG_ARGSCALL
);
1291 if(n
> 0 && nold
> 0){
1293 char const *cp
= *linebuf
+ nold
;
1295 while(blankspacechar(*cp
) && nold
+ i
< n
)
1298 memmove(*linebuf
+ nold
, cp
, n
- nold
- i
);
1300 (*linebuf
)[n
] = '\0';
1309 * An unquoted <backslash> at the end of a command line shall
1310 * be discarded and the next line shall continue the command */
1311 if(!nl_escape
|| (*linebuf
)[n
- 1] != '\\')
1313 /* Definitely outside of quotes, thus the quoting rules are so that an
1314 * uneven number of successive backslashs at EOL is a continuation */
1318 for(j
= 1, i
= (size_t)n
- 1; i
-- > 0; ++j
)
1319 if((*linebuf
)[i
] != '\\')
1324 (*linebuf
)[nold
= --n
] = '\0';
1325 if(prompt
!= NULL
&& *prompt
!= '\0')
1326 prompt
= ".. "; /* XXX PS2 .. */
1330 (*linebuf
)[*linesize
= n
] = '\0';
1332 if(options
& OPT_D_VV
)
1333 n_err(_("%s %d bytes <%.*s>\n"),
1334 (!(pstate
& PS_STARTED
) ? "LOAD"
1335 : (pstate
& PS_SOURCING
? "SOURCE" : "READ")),
1339 if (pstate
& PS_PSTATE_PENDMASK
)
1340 a_lex_update_pstate();
1347 n_lex_input_cp_addhist(char const *prompt
, char const *string
, bool_t isgabby
){
1348 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1358 n
= n_lex_input(prompt
, TRU1
, &linebuf
, &linesize
, string
);
1359 if(n
> 0 && *(rv
= savestrbuf(linebuf
, (size_t)n
)) != '\0' &&
1360 (options
& OPT_INTERACTIVE
))
1361 n_tty_addhist(rv
, isgabby
);
1370 n_load(char const *name
){
1371 struct a_lex_input_stack
*lip
;
1376 if(name
== NULL
|| *name
== '\0' || (fip
= Fopen(name
, "r")) == NULL
)
1379 i
= strlen(name
) +1;
1380 lip
= scalloc(1, sizeof(*lip
) -
1381 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) + i
);
1383 lip
->li_flags
= a_LEX_FREE
;
1384 memcpy(lip
->li_name
, name
, i
);
1393 n_load_Xargs(char const **lines
){
1394 static char const name
[] = "-X";
1396 ui8_t buf
[sizeof(struct a_lex_input_stack
) + sizeof name
];
1397 struct a_lex_input_stack
*lip
;
1400 memset(buf
, 0, sizeof buf
);
1402 lip
->li_flags
= a_LEX_MACRO
| a_LEX_MACRO_X_OPTION
| a_LEX_SUPER_MACRO
;
1403 lip
->li_lines
= UNCONST(lines
);
1404 memcpy(lip
->li_name
, name
, sizeof name
);
1407 if(pstate
& PS_EXIT
)
1417 rv
= (a_lex_source_file(*(char**)v
, FAL0
) == TRU1
) ? 0 : 1;
1423 c_source_if(void *v
){ /* XXX obsolete?, support file tests in `if' etc.! */
1427 rv
= (a_lex_source_file(*(char**)v
, TRU1
) != FAL0
) ? 0 : 1;
1433 n_source_macro(char const *name
, char **lines
){
1434 struct a_lex_input_stack
*lip
;
1439 lip
= smalloc(sizeof(*lip
) -
1440 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) +
1441 (i
= strlen(name
) +1));
1442 lip
->li_outer
= a_lex_input
;
1443 lip
->li_file
= NULL
;
1444 lip
->li_cond
= condstack_release();
1445 lip
->li_flags
= a_LEX_FREE
| a_LEX_MACRO
| a_LEX_MACRO_FREE_DATA
|
1446 (a_lex_input
== NULL
|| (a_lex_input
->li_flags
& a_LEX_SUPER_MACRO
)
1447 ? a_LEX_SUPER_MACRO
: 0);
1449 lip
->li_lines
= lines
;
1450 memcpy(lip
->li_name
, name
, i
);
1454 rv
= a_commands_recursive();
1460 n_source_command(char const *cmd
){
1461 struct a_lex_input_stack
*lip
;
1467 cmd
= sbufdup(cmd
, i
++);
1470 lip
= smalloc(sizeof(*lip
) -
1471 VFIELD_SIZEOF(struct a_lex_input_stack
, li_name
) +
1472 ial
+ 2*sizeof(char*));
1473 lip
->li_outer
= a_lex_input
;
1474 lip
->li_file
= NULL
;
1475 lip
->li_cond
= condstack_release();
1476 lip
->li_flags
= a_LEX_FREE
| a_LEX_MACRO
| a_LEX_MACRO_FREE_DATA
|
1478 (a_lex_input
== NULL
|| (a_lex_input
->li_flags
& a_LEX_SUPER_MACRO
)
1479 ? a_LEX_SUPER_MACRO
: 0);
1481 lip
->li_lines
= (void*)(lip
->li_name
+ ial
);
1482 lip
->li_lines
[0] = UNCONST(cmd
); /* dup'ed above */
1483 lip
->li_lines
[1] = NULL
;
1484 memcpy(lip
->li_name
, cmd
, i
);
1488 rv
= a_commands_recursive();
1494 n_source_may_yield_control(void){
1495 return ((options
& OPT_INTERACTIVE
) &&
1496 (pstate
& PS_STARTED
) &&
1497 (!(pstate
& PS_ROBOT
) || (pstate
& PS_RECURSED
)) && /* Ok for ~: */
1498 (a_lex_input
== NULL
|| a_lex_input
->li_outer
== NULL
));