url_servbyname() -> n_servbyname(); "support" file://
[s-mailx.git] / lex_input.c
blobc17b09208398b689c72701d5ccd5580ae9821653
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>.
8 */
9 /*
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
15 * are met:
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
35 * SUCH DAMAGE.
37 #undef n_FILE
38 #define n_FILE lex_input
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 enum a_lex_input_flags{
45 a_LEX_NONE,
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 */
56 struct a_lex_cmd{
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 */
64 #endif
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 */
70 struct a_lex_ghost{
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 ~:) */
80 ui8_t __dummy[3];
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 */
99 /* */
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 /* `quit' command */
115 static int a_lex_c_quit(void *v);
117 /* Print the binaries compiled-in features */
118 static int a_lex_c_features(void *v);
120 /* Print the binaries version number */
121 static int a_lex_c_version(void *v);
123 /* PS_STATE_PENDMASK requires some actions */
124 static void a_lex_update_pstate(void);
126 /* Evaluate a single command.
127 * .le_add_history and .le_new_content will be updated upon success.
128 * Command functions return 0 for success, 1 for error, and -1 for abort.
129 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
130 static int a_lex_evaluate(struct a_lex_eval_ctx *evp);
132 /* Get first-fit, or NULL */
133 static struct a_lex_cmd const *a_lex__firstfit(char const *comm);
135 /* Branch here on hangup signal and simulate "exit" */
136 static void a_lex_hangup(int s);
138 /* The following gets called on receipt of an interrupt. Close all open files
139 * except 0, 1, 2, and the temporary. Also, unstack all source files */
140 static void a_lex_onintr(int s);
142 /* Pop the current input back to the previous level. Update the program state.
143 * If the argument is TRUM1 then we don't alert and error out if the stack
144 * doesn't exist at all */
145 static void a_lex_unstack(bool_t eval_error);
147 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
148 static bool_t a_lex_source_file(char const *file, bool_t silent_error);
150 /* System resource file load()ing or -X command line option array traversal */
151 static bool_t a_lex_load(struct a_lex_input_stack *lip);
153 /* A simplified command loop for recursed state machines */
154 static bool_t a_commands_recursive(void);
156 /* List of all commands, and list of commands which are specially treated
157 * and deduced in _evaluate(), but we need a list for _c_list() and
158 * print_comm_docstr() */
159 #ifdef HAVE_DOCSTRINGS
160 # define DS(S) , S
161 #else
162 # define DS(S)
163 #endif
164 static struct a_lex_cmd const a_lex_cmd_tab[] = {
165 #include "cmd_tab.h"
167 a_lex_special_cmd_tab[] = {
168 { "#", NULL, 0, 0, 0
169 DS(N_("\"Comment command\": ignore remaining (continuable) line")) },
170 { "-", NULL, 0, 0, 0
171 DS(N_("Print out the preceding message")) }
173 #undef DS
175 static char *
176 a_lex_isolate(char const *comm){
177 NYD2_ENTER;
178 while(*comm != '\0' &&
179 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
180 ++comm;
181 NYD2_LEAVE;
182 return UNCONST(comm);
185 static int
186 a_lex_c_ghost(void *v){
187 struct a_lex_ghost *lgp, *gp;
188 size_t i, cl, nl;
189 char *cp;
190 char const **argv;
191 NYD_ENTER;
193 argv = v;
195 /* Show the list? */
196 if(*argv == NULL){
197 FILE *fp;
199 if((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
200 fp = stdout;
202 for(i = 0, gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
203 fprintf(fp, "wysh ghost %s %s\n",
204 gp->lg_name, n_shell_quote_cp(gp->lg_cmd.s, TRU1));
206 if(fp != stdout){
207 page_or_print(fp, i);
208 Fclose(fp);
210 goto jleave;
213 /* Verify the ghost name is a valid one */
214 if(*argv[0] == '\0' || *a_lex_isolate(argv[0]) != '\0'){
215 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
216 v = NULL;
217 goto jleave;
220 /* Show command of single ghost? */
221 if(argv[1] == NULL){
222 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
223 if(!strcmp(argv[0], gp->lg_name)){
224 printf("wysh ghost %s %s\n",
225 gp->lg_name, n_shell_quote_cp(gp->lg_cmd.s, TRU1));
226 goto jleave;
228 n_err(_("`ghost': no such alias: \"%s\"\n"), argv[0]);
229 v = NULL;
230 goto jleave;
233 /* Define command for ghost: verify command content */
234 for(cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
235 if(*cp != '\0')
236 cl += strlen(cp) +1; /* SP or NUL */
237 if(cl == 0){
238 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv[0]);
239 v = NULL;
240 goto jleave;
243 /* If the ghost already exists, recreate */
244 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL; lgp = gp, gp = gp->lg_next)
245 if(!strcmp(gp->lg_name, argv[0])){
246 if(lgp != NULL)
247 lgp->lg_next = gp->lg_next;
248 else
249 a_lex_ghosts = gp->lg_next;
250 free(gp);
251 break;
254 nl = strlen(argv[0]) +1;
255 gp = smalloc(sizeof(*gp) - VFIELD_SIZEOF(struct a_lex_ghost, lg_name) +
256 nl + cl);
257 gp->lg_next = a_lex_ghosts;
258 a_lex_ghosts = gp;
259 memcpy(gp->lg_name, argv[0], nl);
260 cp = gp->lg_cmd.s = gp->lg_name + nl;
261 gp->lg_cmd.l = --cl;
263 while(*++argv != NULL)
264 if((i = strlen(*argv)) > 0){
265 memcpy(cp, *argv, i);
266 cp += i;
267 *cp++ = ' ';
269 *--cp = '\0';
270 jleave:
271 NYD_LEAVE;
272 return v == NULL;
275 static int
276 a_lex_c_unghost(void *v){
277 struct a_lex_ghost *lgp, *gp;
278 char const **argv, *cp;
279 int rv;
280 NYD_ENTER;
282 rv = 0;
283 argv = v;
285 while((cp = *argv++) != NULL){
286 if(cp[0] == '*' && cp[1] == '\0'){
287 while((gp = a_lex_ghosts) != NULL){
288 a_lex_ghosts = gp->lg_next;
289 free(gp);
291 }else{
292 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL;
293 lgp = gp, gp = gp->lg_next)
294 if(!strcmp(gp->lg_name, cp)){
295 if(lgp != NULL)
296 lgp->lg_next = gp->lg_next;
297 else
298 a_lex_ghosts = gp->lg_next;
299 free(gp);
300 goto jouter;
302 n_err(_("`unghost': no such alias: \"%s\"\n"), cp);
303 rv = 1;
304 jouter: ;
307 NYD_LEAVE;
308 return rv;
311 static int
312 a_lex_c_list(void *v){
313 FILE *fp;
314 struct a_lex_cmd const **cpa, *cp, **cursor;
315 size_t l, i;
316 NYD_ENTER;
318 i = NELEM(a_lex_cmd_tab) + NELEM(a_lex_special_cmd_tab) +1;
319 cpa = salloc(sizeof(cp) * i);
321 for(i = 0; i < NELEM(a_lex_cmd_tab); ++i)
322 cpa[i] = &a_lex_cmd_tab[i];
323 /* C99 */{
324 size_t j;
326 for(j = 0; j < NELEM(a_lex_special_cmd_tab); ++i, ++j)
327 cpa[i] = &a_lex_special_cmd_tab[j];
329 cpa[i] = NULL;
331 /* C99 */{
332 char const *xcp = v;
334 if(*xcp == '\0')
335 qsort(cpa, i, sizeof(xcp), &a_lex__pcmd_cmp);
338 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
339 fp = stdout;
341 fprintf(fp, _("Commands are:\n"));
342 l = 1;
343 for(i = 0, cursor = cpa; (cp = *cursor++) != NULL;){
344 if(cp->lc_func == &c_cmdnotsupp)
345 continue;
346 if(options & OPT_D_V){
347 char const *argt;
349 switch(cp->lc_argtype & ARG_ARGMASK){
350 case ARG_MSGLIST: argt = N_("message-list"); break;
351 case ARG_STRLIST: argt = N_("a \"string\""); break;
352 case ARG_RAWLIST: argt = N_("old-style quoting"); break;
353 case ARG_NOLIST: argt = N_("no arguments"); break;
354 case ARG_NDMLIST: argt = N_("message-list (without a default)"); break;
355 case ARG_WYSHLIST: argt = N_("sh(1)ell-style quoting"); break;
356 default: argt = N_("`wysh' for sh(1)ell-style quoting"); break;
358 #ifdef HAVE_DOCSTRINGS
359 fprintf(fp, _("`%s'. Argument type: %s.\n\t%s\n"),
360 cp->lc_name, V_(argt), V_(cp->lc_doc));
361 l += 2;
362 #else
363 fprintf(fp, "`%s' (%s)\n", cp->lc_name, argt);
364 ++l;
365 #endif
366 }else{
367 size_t j = strlen(cp->lc_name) + 2;
369 if((i += j) > 72){
370 i = j;
371 fprintf(fp, "\n");
372 ++l;
374 fprintf(fp, (*cursor != NULL ? "%s, " : "%s\n"), cp->lc_name);
378 if(fp != stdout){
379 page_or_print(fp, l);
380 Fclose(fp);
382 NYD_LEAVE;
383 return 0;
386 static int
387 a_lex__pcmd_cmp(void const *s1, void const *s2){
388 struct a_lex_cmd const * const *cp1, * const *cp2;
389 int rv;
390 NYD2_ENTER;
392 cp1 = s1;
393 cp2 = s2;
394 rv = strcmp((*cp1)->lc_name, (*cp2)->lc_name);
395 NYD2_LEAVE;
396 return rv;
399 static int
400 a_lex_c_quit(void *v){
401 NYD_ENTER;
402 UNUSED(v);
404 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
405 * Otherwise return -1 to abort command loop */
406 pstate |= PS_EXIT;
407 NYD_LEAVE;
408 return 0;
411 static int
412 a_lex_c_features(void *v){
413 NYD_ENTER;
415 UNUSED(v);
417 printf(_("Features: %s\n"), ok_vlook(features));
418 NYD_LEAVE;
419 return 0;
422 static int
423 a_lex_c_version(void *v){
424 NYD_ENTER;
426 UNUSED(v);
428 printf(_("Version %s\n"), ok_vlook(version));
429 NYD_LEAVE;
430 return 0;
433 static void
434 a_lex_update_pstate(void){
435 NYD_ENTER;
437 if(pstate & PS_SIGWINCH_PEND){
438 char buf[32];
440 snprintf(buf, sizeof buf, "%d", scrnwidth);
441 ok_vset(COLUMNS, buf);
442 snprintf(buf, sizeof buf, "%d", scrnheight);
443 ok_vset(LINES, buf);
446 pstate &= ~PS_PSTATE_PENDMASK;
447 NYD_LEAVE;
450 static int
451 a_lex_evaluate(struct a_lex_eval_ctx *evp){
452 /* xxx old style(9), but also old code */
453 struct str line;
454 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
455 struct a_lex_ghost *gp;
456 struct a_lex_cmd const *cmd;
457 int c, e;
458 bool_t wysh;
459 NYD_ENTER;
461 wysh = FAL0;
462 e = 1;
463 cmd = NULL;
464 gp = NULL;
465 line = evp->le_line; /* XXX don't change original (buffer pointer) */
466 assert(line.s[line.l] == '\0');
467 evp->le_add_history = FAL0;
468 evp->le_new_content = NULL;
470 /* Command ghosts that refer to shell commands or macro expansion restart */
471 jrestart:
473 /* Strip the white space away from end and beginning of command */
474 if(line.l > 0){
475 size_t i = line.l;
477 for(cp = &line.s[i -1]; spacechar(*cp); --cp)
478 --i;
479 line.l = i;
481 for(cp = line.s; spacechar(*cp); ++cp)
483 line.l -= PTR2SIZE(cp - line.s);
485 /* Ignore null commands (comments) */
486 if(*cp == '#')
487 goto jleave0;
489 /* Handle ! differently to get the correct lexical conventions */
490 arglist[0] = cp;
491 if(*cp == '!')
492 ++cp;
493 /* Isolate the actual command; since it may not necessarily be
494 * separated from the arguments (as in `p1') we need to duplicate it to
495 * be able to create a NUL terminated version.
496 * We must be aware of several special one letter commands here */
497 else if((cp = a_lex_isolate(cp)) == arglist[0] &&
498 (*cp == '|' || *cp == '~' || *cp == '?'))
499 ++cp;
500 c = (int)PTR2SIZE(cp - arglist[0]);
501 line.l -= c;
502 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
503 memcpy(word, arglist[0], c);
504 word[c] = '\0';
506 /* Look up the command; if not found, bitch.
507 * Normally, a blank command would map to the first command in the
508 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
509 * confusion; act just the same for ghosts */
510 if(*word == '\0'){
511 if((pstate & PS_ROBOT) || gp != NULL)
512 goto jleave0;
513 cmd = a_lex_cmd_tab + 0;
514 goto jexec;
517 /* XXX It may be the argument parse adjuster */
518 if(!wysh && c == sizeof("wysh") -1 && !asccasecmp(word, "wysh")){
519 wysh = TRU1;
520 line.s = cp;
521 goto jrestart;
524 /* If this is the first evaluation, check command ghosts */
525 if(gp == NULL){
526 /* TODO relink list head, so it's sorted on usage over time?
527 * TODO in fact, there should be one hashmap over all commands and ghosts
528 * TODO so that the lookup could be made much more efficient than it is
529 * TODO now (two adjacent list searches! */
530 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
531 if(!strcmp(word, gp->lg_name)){
532 if(line.l > 0){
533 size_t i;
535 i = gp->lg_cmd.l;
536 line.s = salloc(i + line.l +1);
537 memcpy(line.s, gp->lg_cmd.s, i);
538 memcpy(line.s + i, cp, line.l);
539 line.s[i += line.l] = '\0';
540 line.l = i;
541 }else{
542 line.s = gp->lg_cmd.s;
543 line.l = gp->lg_cmd.l;
545 goto jrestart;
549 if((cmd = a_lex__firstfit(word)) == NULL || cmd->lc_func == &c_cmdnotsupp){
550 bool_t s;
552 if(!(s = condstack_isskip()) || (options & OPT_D_V))
553 n_err(_("Unknown command%s: `%s'\n"),
554 (s ? _(" (ignored due to `if' condition)") : ""), word);
555 if(s)
556 goto jleave0;
557 if(cmd != NULL){
558 c_cmdnotsupp(NULL);
559 cmd = NULL;
561 goto jleave;
564 /* See if we should execute the command -- if a conditional we always
565 * execute it, otherwise, check the state of cond */
566 jexec:
567 if(!(cmd->lc_argtype & ARG_F) && condstack_isskip())
568 goto jleave0;
570 /* Process the arguments to the command, depending on the type it expects */
571 if(!(cmd->lc_argtype & ARG_M) && (options & OPT_SENDMODE)){
572 n_err(_("May not execute `%s' while sending\n"), cmd->lc_name);
573 goto jleave;
575 if((cmd->lc_argtype & ARG_S) && !(pstate & PS_STARTED)){
576 n_err(_("May not execute `%s' during startup\n"), cmd->lc_name);
577 goto jleave;
579 if((cmd->lc_argtype & ARG_I) &&
580 !(options & (OPT_INTERACTIVE | OPT_BATCH_FLAG))){
581 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
582 cmd->lc_name);
583 goto jleave;
585 if((cmd->lc_argtype & ARG_R) && (pstate & PS_RECURSED)){
586 n_err(_("Cannot invoke `%s' when in recursed mode (e.g., composing)\n"),
587 cmd->lc_name);
588 goto jleave;
591 if((cmd->lc_argtype & ARG_W) && !(mb.mb_perm & MB_DELE)){
592 n_err(_("May not execute `%s' -- message file is read only\n"),
593 cmd->lc_name);
594 goto jleave;
596 if((cmd->lc_argtype & ARG_A) && mb.mb_type == MB_VOID){
597 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd->lc_name);
598 goto jleave;
601 if(cmd->lc_argtype & ARG_O)
602 OBSOLETE2(_("this command will be removed"), cmd->lc_name);
603 if(cmd->lc_argtype & ARG_V)
604 temporary_arg_v_store = NULL;
606 if(wysh && (cmd->lc_argtype & ARG_ARGMASK) != ARG_WYRALIST)
607 n_err(_("`wysh' prefix doesn't affect `%s'\n"), cmd->lc_name);
608 switch(cmd->lc_argtype & ARG_ARGMASK){
609 case ARG_MSGLIST:
610 /* Message list defaulting to nearest forward legal message */
611 if(n_msgvec == NULL)
612 goto je96;
613 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
614 break;
615 if(c == 0){
616 if((n_msgvec[0] = first(cmd->lc_msgflag, cmd->lc_msgmask)) != 0)
617 n_msgvec[1] = 0;
619 if(n_msgvec[0] == 0){
620 if(!(pstate & PS_HOOK_MASK))
621 printf(_("No applicable messages\n"));
622 break;
624 e = (*cmd->lc_func)(n_msgvec);
625 break;
627 case ARG_NDMLIST:
628 /* Message list with no defaults, but no error if none exist */
629 if(n_msgvec == NULL){
630 je96:
631 n_err(_("Invalid use of \"message list\"\n"));
632 break;
634 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
635 break;
636 e = (*cmd->lc_func)(n_msgvec);
637 break;
639 case ARG_STRLIST:
640 /* Just the straight string, with leading blanks removed */
641 while(whitechar(*cp))
642 ++cp;
643 e = (*cmd->lc_func)(cp);
644 break;
646 case ARG_WYSHLIST:
647 c = 1;
648 if(0){
649 /* FALLTHRU */
650 case ARG_WYRALIST:
651 c = wysh ? 1 : 0;
652 if(0){
653 case ARG_RAWLIST:
654 c = 0;
658 if((c = getrawlist((c != 0), arglist, NELEM(arglist), cp, line.l)) < 0){
659 n_err(_("Invalid argument list\n"));
660 break;
662 if(c < cmd->lc_minargs){
663 n_err(_("`%s' requires at least %d arg(s)\n"),
664 cmd->lc_name, cmd->lc_minargs);
665 break;
667 #undef lc_minargs
668 if(c > cmd->lc_maxargs){
669 n_err(_("`%s' takes no more than %d arg(s)\n"),
670 cmd->lc_name, cmd->lc_maxargs);
671 break;
673 #undef lc_maxargs
674 e = (*cmd->lc_func)(arglist);
675 break;
677 case ARG_NOLIST:
678 /* Just the constant zero, for exiting, eg. */
679 e = (*cmd->lc_func)(0);
680 break;
682 default:
683 DBG( n_panic(_("Implementation error: unknown argument type: %d"),
684 cmd->lc_argtype & ARG_ARGMASK); )
685 goto jleave0;
688 if(e == 0 && (cmd->lc_argtype & ARG_V) &&
689 (cp = temporary_arg_v_store) != NULL){
690 temporary_arg_v_store = NULL;
691 evp->le_new_content = cp;
692 goto jleave0;
694 if(!(cmd->lc_argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
695 evp->le_add_history = TRU1;
697 jleave:
698 /* Exit the current source file on error TODO what a mess! */
699 if(e == 0)
700 pstate &= ~PS_EVAL_ERROR;
701 else{
702 pstate |= PS_EVAL_ERROR;
703 if(e < 0 || (pstate & PS_ROBOT)){ /* FIXME */
704 e = 1;
705 goto jret;
707 goto jret0;
710 if(cmd == NULL)
711 goto jret0;
712 if((cmd->lc_argtype & ARG_P) && ok_blook(autoprint))
713 if(visible(dot)){
714 line.s = savestr("type");
715 line.l = sizeof("type") -1;
716 gp = (struct a_lex_ghost*)-1; /* Avoid `ghost' interpretation */
717 goto jrestart;
720 if(!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(cmd->lc_argtype & ARG_T))
721 pstate |= PS_SAW_COMMAND;
722 jleave0:
723 pstate &= ~PS_EVAL_ERROR;
724 jret0:
725 e = 0;
726 jret:
728 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
730 NYD_LEAVE;
731 return e;
734 static struct a_lex_cmd const *
735 a_lex__firstfit(char const *comm){ /* TODO *hashtable*! linear list search!!! */
736 struct a_lex_cmd const *cp;
737 NYD2_ENTER;
739 for(cp = a_lex_cmd_tab; PTRCMP(cp, <, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)]);
740 ++cp)
741 if(*comm == *cp->lc_name && is_prefix(comm, cp->lc_name))
742 goto jleave;
743 cp = NULL;
744 jleave:
745 NYD2_LEAVE;
746 return cp;
749 static void
750 a_lex_hangup(int s){
751 NYD_X; /* Signal handler */
752 UNUSED(s);
753 /* nothing to do? */
754 exit(EXIT_ERR);
757 static void
758 a_lex_onintr(int s){
759 NYD_X; /* Signal handler */
761 safe_signal(SIGINT, a_lex_onintr);
762 noreset = 0;
763 a_lex_unstack(TRUM1);
765 termios_state_reset();
766 close_all_files(); /* FIXME .. to outer level ONLU! */
768 if(image >= 0){
769 close(image);
770 image = -1;
772 if(interrupts != 1)
773 n_err_sighdl(_("Interrupt\n"));
774 safe_signal(SIGPIPE, a_lex_oldpipe);
775 siglongjmp(srbuf, 0); /* FIXME get rid */
778 static void
779 a_lex_unstack(bool_t eval_error){
780 struct a_lex_input_stack *lip;
781 NYD_ENTER;
783 if((lip = a_lex_input) == NULL){
784 /* If called from a_lex_onintr(), be silent FIXME */
785 pstate &= ~(PS_SOURCING | PS_ROBOT);
786 if(eval_error == TRUM1 || !(pstate & PS_STARTED))
787 goto jleave;
788 goto jerr;
791 if(lip->li_flags & a_LEX_MACRO){
792 if(lip->li_flags & a_LEX_MACRO_FREE_DATA){
793 char **lp;
795 while(*(lp = &lip->li_lines[lip->li_loff]) != NULL){
796 free(*lp);
797 ++lip->li_loff;
799 /* Part of lip's memory chunk, then */
800 if(!(lip->li_flags & a_LEX_MACRO_CMD))
801 free(lip->li_lines);
803 }else{
804 if(lip->li_flags & a_LEX_PIPE)
805 /* XXX command manager should -TERM then -KILL instead of hoping
806 * XXX for exit of provider due to EPIPE / SIGPIPE */
807 Pclose(lip->li_file, TRU1);
808 else
809 Fclose(lip->li_file);
812 if(!condstack_take(lip->li_cond)){
813 n_err(_("Unmatched `if' at end of %s \"%s\"\n"),
814 ((lip->li_flags & a_LEX_MACRO
815 ? (lip->li_flags & a_LEX_MACRO_CMD ? _("command") : _("macro"))
816 : _("`source'd file"))),
817 lip->li_name);
818 eval_error = TRU1;
821 if((a_lex_input = lip->li_outer) == NULL){
822 pstate &= ~(PS_SOURCING | PS_ROBOT);
823 }else{
824 if((a_lex_input->li_flags & (a_LEX_MACRO | a_LEX_SUPER_MACRO)) ==
825 (a_LEX_MACRO | a_LEX_SUPER_MACRO))
826 pstate &= ~PS_SOURCING;
827 assert(pstate & PS_ROBOT);
830 if(eval_error)
831 goto jerr;
832 if(lip->li_flags & a_LEX_FREE)
833 free(lip);
834 jleave:
835 if(UNLIKELY(a_lex_input != NULL && eval_error == TRUM1))
836 a_lex_unstack(TRUM1);
837 NYD_LEAVE;
838 return;
840 jerr:
841 if(lip != NULL){
842 if(options & OPT_D_V)
843 n_alert(_("Stopped %s \"%s\" due to errors%s"),
844 (pstate & PS_STARTED
845 ? (lip->li_flags & a_LEX_MACRO
846 ? (lip->li_flags & a_LEX_MACRO_CMD
847 ? _("evaluating command") : _("evaluating macro"))
848 : (lip->li_flags & a_LEX_PIPE
849 ? _("executing `source'd pipe")
850 : _("loading `source'd file")))
851 : (lip->li_flags & a_LEX_MACRO
852 ? (lip->li_flags & a_LEX_MACRO_X_OPTION
853 ? _("evaluating command line") : _("evaluating macro"))
854 : _("loading initialization resource"))),
855 lip->li_name,
856 (options & OPT_DEBUG ? "" : _(" (enable *debug* for trace)")));
857 if(lip->li_flags & a_LEX_FREE)
858 free(lip);
861 if(!(options & OPT_INTERACTIVE) && !(pstate & PS_STARTED)){
862 if(options & OPT_D_V)
863 n_alert(_("Non-interactive, bailing out due to errors "
864 "in startup load phase"));
865 exit(EXIT_ERR);
867 goto jleave;
870 static bool_t
871 a_lex_source_file(char const *file, bool_t silent_error){
872 struct a_lex_input_stack *lip;
873 size_t nlen;
874 char *nbuf;
875 bool_t ispipe;
876 FILE *fip;
877 NYD_ENTER;
879 fip = NULL;
881 /* Being a command argument file is space-trimmed */
882 if((ispipe = (!silent_error && (nlen = strlen(file)) > 0 &&
883 file[--nlen] == '|'))){
884 if((fip = Popen(nbuf = savestrbuf(file, nlen), "r",
885 ok_vlook(SHELL), NULL, COMMAND_FD_NULL)) == NULL){
886 if(!silent_error || (options & OPT_D_V))
887 n_perr(nbuf, 0);
888 goto jleave;
890 }else if((nbuf = fexpand(file, FEXP_LOCAL)) == NULL)
891 goto jleave;
892 else if((fip = Fopen(nbuf, "r")) == NULL){
893 if(!silent_error || (options & OPT_D_V))
894 n_perr(nbuf, 0);
895 goto jleave;
898 lip = smalloc(sizeof(*lip) -
899 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
900 (nlen = strlen(nbuf) +1));
901 lip->li_outer = a_lex_input;
902 lip->li_file = fip;
903 lip->li_cond = condstack_release();
904 lip->li_flags = (ispipe ? a_LEX_PIPE : a_LEX_NONE) |
905 (a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
906 ? a_LEX_SUPER_MACRO : 0);
907 memcpy(lip->li_name, nbuf, nlen);
909 pstate |= PS_SOURCING | PS_ROBOT;
910 a_lex_input = lip;
911 a_commands_recursive();
912 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
913 jleave:
914 NYD_LEAVE;
915 return silent_error ? TRU1 : (fip != NULL);
918 static bool_t
919 a_lex_load(struct a_lex_input_stack *lip){
920 bool_t rv;
921 NYD2_ENTER;
923 assert(!(pstate & PS_STARTED));
924 assert(a_lex_input == NULL);
926 /* POSIX:
927 * Any errors in the start-up file shall either cause mailx to terminate
928 * with a diagnostic message and a non-zero status or to continue after
929 * writing a diagnostic message, ignoring the remainder of the lines in
930 * the start-up file. */
931 lip->li_cond = condstack_release();
933 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
934 pstate |= PS_ROBOT |
935 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
937 pstate |= PS_ROBOT | PS_SOURCING;
938 if(options & OPT_D_V)
939 n_err(_("Loading \"%s\"\n"), lip->li_name);
940 a_lex_input = lip;
941 if(!(rv = n_commands())){
942 if(!(options & OPT_INTERACTIVE)){
943 if(options & OPT_D_V)
944 n_alert(_("Non-interactive program mode, forced exit"));
945 exit(EXIT_ERR);
948 /* PS_EXIT handled by callers */
949 NYD2_LEAVE;
950 return rv;
953 static bool_t
954 a_commands_recursive(void){
955 struct a_lex_eval_ctx ev;
956 bool_t rv;
957 NYD2_ENTER;
959 memset(&ev, 0, sizeof ev);
961 /* FIXME sigkondom */
962 n_COLOUR( n_colour_env_push(); )
963 rv = TRU1;
964 for(;;){
965 int n;
967 /* Read a line of commands and handle end of file specially */
968 ev.le_line.l = ev.le_line_size;
969 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
970 ev.le_new_content);
971 ev.le_line_size = (ui32_t)ev.le_line.l;
972 ev.le_line.l = (ui32_t)n;
974 if(n < 0)
975 break;
977 if(a_lex_evaluate(&ev)){
978 rv = FAL0;
979 break;
982 if((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)){
983 if(exit_status != EXIT_OK)
984 break;
987 a_lex_unstack(!rv);
988 n_COLOUR( n_colour_env_pop(FAL0); )
990 if(ev.le_line.s != NULL)
991 free(ev.le_line.s);
992 NYD2_LEAVE;
993 return rv;
996 #ifdef HAVE_DOCSTRINGS
997 FL bool_t
998 n_print_comm_docstr(char const *comm){
999 struct a_lex_ghost const *gp;
1000 struct a_lex_cmd const *cp, *cpmax;
1001 bool_t rv = FAL0;
1002 NYD_ENTER;
1004 /* Ghosts take precedence */
1005 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
1006 if(!strcmp(comm, gp->lg_name)){
1007 printf("%s -> ", comm);
1008 comm = gp->lg_cmd.s;
1009 break;
1012 cpmax = &(cp = a_lex_cmd_tab)[NELEM(a_lex_cmd_tab)];
1013 jredo:
1014 for(; PTRCMP(cp, <, cpmax); ++cp){
1015 if(!strcmp(comm, cp->lc_name))
1016 printf("%s: %s\n", comm, V_(cp->lc_doc));
1017 else if(is_prefix(comm, cp->lc_name))
1018 printf("%s (%s): %s\n", comm, cp->lc_name, V_(cp->lc_doc));
1019 else
1020 continue;
1021 rv = TRU1;
1022 break;
1024 if(!rv && PTRCMP(cpmax, ==, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)])){
1025 cpmax = &(cp = a_lex_special_cmd_tab)[NELEM(a_lex_special_cmd_tab)];
1026 goto jredo;
1029 if(!rv && gp != NULL){
1030 printf("\"%s\"\n", comm);
1031 rv = TRU1;
1033 NYD_LEAVE;
1034 return rv;
1036 #endif /* HAVE_DOCSTRINGS */
1038 FL bool_t
1039 n_commands(void){ /* FIXME */
1040 struct a_lex_eval_ctx ev;
1041 int n;
1042 bool_t volatile rv = TRU1;
1043 NYD_ENTER;
1045 if (!(pstate & PS_SOURCING)) {
1046 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
1047 safe_signal(SIGINT, &a_lex_onintr);
1048 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1049 safe_signal(SIGHUP, &a_lex_hangup);
1051 a_lex_oldpipe = safe_signal(SIGPIPE, SIG_IGN);
1052 safe_signal(SIGPIPE, a_lex_oldpipe);
1054 memset(&ev, 0, sizeof ev);
1056 (void)sigsetjmp(srbuf, 1); /* FIXME get rid */
1057 for (;;) {
1058 char *temporary_orig_line; /* XXX eval_ctx.le_line not yet constant */
1060 n_COLOUR( n_colour_env_pop(TRU1); )
1062 /* TODO Unless we have our signal manager (or however we do it) child
1063 * TODO processes may have time slots where their execution isn't
1064 * TODO protected by signal handlers (in between start and setup
1065 * TODO completed). close_all_files() is only called from onintr()
1066 * TODO so those may linger possibly forever */
1067 if(!(pstate & PS_SOURCING))
1068 close_all_files();
1070 interrupts = 0;
1072 temporary_localopts_free(); /* XXX intermediate hack */
1073 sreset((pstate & PS_SOURCING) != 0);
1074 if (!(pstate & PS_SOURCING)) {
1075 char *cp;
1077 /* TODO Note: this buffer may contain a password. We should redefine
1078 * TODO the code flow which has to do that */
1079 if ((cp = termios_state.ts_linebuf) != NULL) {
1080 termios_state.ts_linebuf = NULL;
1081 termios_state.ts_linesize = 0;
1082 free(cp); /* TODO pool give-back */
1084 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1085 if (ev.le_line.l > LINESIZE * 3) {
1086 free(ev.le_line.s); /* TODO pool! but what? */
1087 ev.le_line.s = NULL;
1088 ev.le_line.l = ev.le_line_size = 0;
1092 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1093 char *cp;
1095 cp = ok_vlook(newmail);
1096 if ((options & OPT_TTYIN) && cp != NULL) {
1097 struct stat st;
1099 /* FIXME TEST WITH NOPOLL ETC. !!! */
1100 n = (cp != NULL && strcmp(cp, "nopoll"));
1101 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
1102 st.st_size > mailsize) ||
1103 (mb.mb_type == MB_MAILDIR && n != 0)) {
1104 size_t odot = PTR2SIZE(dot - message);
1105 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
1107 if (setfile(mailname,
1108 FEDIT_NEWMAIL |
1109 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
1110 exit_status |= EXIT_ERR;
1111 rv = FAL0;
1112 break;
1114 dot = message + odot;
1115 pstate |= odid;
1119 exit_status = EXIT_OK;
1122 /* Read a line of commands and handle end of file specially */
1123 jreadline:
1124 ev.le_line.l = ev.le_line_size;
1125 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
1126 ev.le_new_content);
1127 ev.le_line_size = (ui32_t)ev.le_line.l;
1128 ev.le_line.l = (ui32_t)n;
1130 if (n < 0) {
1131 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1132 if (!(pstate & PS_ROBOT) &&
1133 (options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
1134 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1135 n_msleep(500, FAL0);
1136 continue;
1138 break;
1141 temporary_orig_line = ((pstate & PS_SOURCING) ||
1142 !(options & OPT_INTERACTIVE)) ? NULL
1143 : savestrbuf(ev.le_line.s, ev.le_line.l);
1144 pstate &= ~PS_HOOK_MASK;
1145 if (a_lex_evaluate(&ev)) {
1146 if (!(pstate & PS_STARTED)) /* TODO mess; join PS_EVAL_ERROR.. */
1147 rv = FAL0;
1148 break;
1151 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
1152 if (exit_status != EXIT_OK)
1153 break;
1154 if ((pstate & (PS_SOURCING | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
1155 exit_status = EXIT_ERR;
1156 break;
1159 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1160 if (ev.le_new_content != NULL)
1161 goto jreadline;
1162 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1163 if (temporary_orig_line != NULL)
1164 n_tty_addhist(temporary_orig_line, !ev.le_add_history);
1167 if(pstate & PS_EXIT)
1168 break;
1170 a_lex_unstack(!rv);
1172 if (ev.le_line.s != NULL)
1173 free(ev.le_line.s);
1174 if (pstate & PS_SOURCING)
1175 sreset(FAL0);
1176 NYD_LEAVE;
1177 return rv;
1180 FL int
1181 (n_lex_input)(char const *prompt, bool_t nl_escape, char **linebuf,
1182 size_t *linesize, char const *string SMALLOC_DEBUG_ARGS){
1183 /* TODO readline: linebuf pool! */
1184 bool_t doprompt, dotty;
1185 int n, nold;
1186 FILE *ifile;
1187 NYD2_ENTER;
1189 /* Special case macro mode: never need to prompt, lines have always been
1190 * unfolded already */
1191 if(a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO)){
1192 char **lp = &a_lex_input->li_lines[a_lex_input->li_loff];
1194 if(*linebuf != NULL)
1195 free(*linebuf);
1197 if((*linebuf = *lp) == NULL){
1198 *linesize = 0;
1199 n = -1;
1200 }else{
1201 ++a_lex_input->li_loff;
1202 n = (int)(*linesize = strlen(*linebuf));
1204 if(!(a_lex_input->li_flags & a_LEX_MACRO_FREE_DATA))
1205 *linebuf = sbufdup(*linebuf, *linesize);
1207 if(options & OPT_D_VV)
1208 n_err(_("%s %d bytes <%.*s>\n"),
1209 (a_lex_input->li_flags & a_LEX_MACRO_X_OPTION
1210 ? _("-X option") : _("MACRO")),
1211 n, n, *linebuf);
1213 goto jleave;
1216 doprompt = ((pstate & (PS_STARTED | PS_ROBOT)) == PS_STARTED &&
1217 (options & OPT_INTERACTIVE));
1218 dotty = (doprompt && !ok_blook(line_editor_disable));
1219 if(!doprompt)
1220 prompt = NULL;
1221 else if(prompt == NULL)
1222 prompt = getprompt();
1224 /* Ensure stdout is flushed first anyway */
1225 if(!dotty && prompt == NULL)
1226 fflush(stdout);
1228 ifile = (a_lex_input != NULL) ? a_lex_input->li_file : stdin;
1229 assert(ifile != NULL);
1231 for(nold = n = 0;;){
1232 if(dotty){
1233 assert(ifile == stdin);
1234 if(string != NULL && (n = (int)strlen(string)) > 0){
1235 if(*linesize > 0)
1236 *linesize += n +1;
1237 else
1238 *linesize = (size_t)n + LINESIZE +1;
1239 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
1240 memcpy(*linebuf, string, (size_t)n +1);
1242 string = NULL;
1243 /* TODO if nold>0, don't redisplay the entire line!
1244 * TODO needs complete redesign ... */
1245 n = (n_tty_readline)(prompt, linebuf, linesize, n
1246 SMALLOC_DEBUG_ARGSCALL);
1247 }else{
1248 if(prompt != NULL) {
1249 if(*prompt != '\0')
1250 fputs(prompt, stdout);
1251 fflush(stdout);
1254 n = (readline_restart)(ifile, linebuf, linesize, n
1255 SMALLOC_DEBUG_ARGSCALL);
1257 if(n > 0 && nold > 0){
1258 int i = 0;
1259 char const *cp = *linebuf + nold;
1261 while(blankspacechar(*cp) && nold + i < n)
1262 ++cp, ++i;
1263 if(i > 0){
1264 memmove(*linebuf + nold, cp, n - nold - i);
1265 n -= i;
1266 (*linebuf)[n] = '\0';
1271 if(n <= 0)
1272 break;
1274 /* POSIX says:
1275 * An unquoted <backslash> at the end of a command line shall
1276 * be discarded and the next line shall continue the command */
1277 if(!nl_escape || (*linebuf)[n - 1] != '\\')
1278 break;
1279 /* Definitely outside of quotes, thus the quoting rules are so that an
1280 * uneven number of successive backslashs at EOL is a continuation */
1281 if(n > 1){
1282 size_t i, j;
1284 for(j = 1, i = (size_t)n - 1; i-- > 0; ++j)
1285 if((*linebuf)[i] != '\\')
1286 break;
1287 if(!(j & 1))
1288 break;
1290 (*linebuf)[nold = --n] = '\0';
1291 if(prompt != NULL && *prompt != '\0')
1292 prompt = ".. "; /* XXX PS2 .. */
1295 if(n >= 0){
1296 (*linebuf)[*linesize = n] = '\0';
1298 if(options & OPT_D_VV)
1299 n_err(_("%s %d bytes <%.*s>\n"),
1300 (!(pstate & PS_STARTED) ? "LOAD"
1301 : (pstate & PS_SOURCING ? "SOURCE" : "READ")),
1302 n, n, *linebuf);
1304 jleave:
1305 if (pstate & PS_PSTATE_PENDMASK)
1306 a_lex_update_pstate();
1308 NYD2_LEAVE;
1309 return n;
1312 FL char *
1313 n_lex_input_cp_addhist(char const *prompt, char const *string, bool_t isgabby){
1314 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1315 size_t linesize;
1316 char *linebuf, *rv;
1317 int n;
1318 NYD2_ENTER;
1320 linesize = 0;
1321 linebuf = NULL;
1322 rv = NULL;
1324 n = n_lex_input(prompt, TRU1, &linebuf, &linesize, string);
1325 if(n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
1326 (options & OPT_INTERACTIVE))
1327 n_tty_addhist(rv, isgabby);
1329 if(linebuf != NULL)
1330 free(linebuf);
1331 NYD2_LEAVE;
1332 return rv;
1335 FL void
1336 n_load(char const *name){
1337 struct a_lex_input_stack *lip;
1338 size_t i;
1339 FILE *fip;
1340 NYD_ENTER;
1342 if(name == NULL || *name == '\0' || (fip = Fopen(name, "r")) == NULL)
1343 goto jleave;
1345 i = strlen(name) +1;
1346 lip = scalloc(1, sizeof(*lip) -
1347 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) + i);
1348 lip->li_file = fip;
1349 lip->li_flags = a_LEX_FREE;
1350 memcpy(lip->li_name, name, i);
1352 a_lex_load(lip);
1353 pstate &= ~PS_EXIT;
1354 jleave:
1355 NYD_LEAVE;
1358 FL void
1359 n_load_Xargs(char const **lines){
1360 static char const name[] = "-X";
1362 ui8_t buf[sizeof(struct a_lex_input_stack) + sizeof name];
1363 struct a_lex_input_stack *lip;
1364 NYD_ENTER;
1366 memset(buf, 0, sizeof buf);
1367 lip = (void*)buf;
1368 lip->li_flags = a_LEX_MACRO | a_LEX_MACRO_X_OPTION | a_LEX_SUPER_MACRO;
1369 lip->li_lines = UNCONST(lines);
1370 memcpy(lip->li_name, name, sizeof name);
1372 a_lex_load(lip);
1373 if(pstate & PS_EXIT)
1374 exit(EXIT_OK);
1375 NYD_LEAVE;
1378 FL int
1379 c_source(void *v){
1380 int rv;
1381 NYD_ENTER;
1383 rv = (a_lex_source_file(*(char**)v, FAL0) == TRU1) ? 0 : 1;
1384 NYD_LEAVE;
1385 return rv;
1388 FL int
1389 c_source_if(void *v){ /* XXX obsolete?, support file tests in `if' etc.! */
1390 int rv;
1391 NYD_ENTER;
1393 rv = (a_lex_source_file(*(char**)v, TRU1) != FAL0) ? 0 : 1;
1394 NYD_LEAVE;
1395 return rv;
1398 FL bool_t
1399 n_source_macro(char const *name, char **lines){
1400 struct a_lex_input_stack *lip;
1401 size_t i;
1402 int rv;
1403 NYD_ENTER;
1405 lip = smalloc(sizeof(*lip) -
1406 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1407 (i = strlen(name) +1));
1408 lip->li_outer = a_lex_input;
1409 lip->li_file = NULL;
1410 lip->li_cond = condstack_release();
1411 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1412 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1413 ? a_LEX_SUPER_MACRO : 0);
1414 lip->li_loff = 0;
1415 lip->li_lines = lines;
1416 memcpy(lip->li_name, name, i);
1418 pstate |= PS_ROBOT;
1419 a_lex_input = lip;
1420 rv = a_commands_recursive();
1421 NYD_LEAVE;
1422 return rv;
1425 FL bool_t
1426 n_source_command(char const *cmd){
1427 struct a_lex_input_stack *lip;
1428 size_t i, ial;
1429 bool_t rv;
1430 NYD_ENTER;
1432 i = strlen(cmd);
1433 cmd = sbufdup(cmd, i++);
1434 ial = n_ALIGN(i);
1436 lip = smalloc(sizeof(*lip) -
1437 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1438 ial + 2*sizeof(char*));
1439 lip->li_outer = a_lex_input;
1440 lip->li_file = NULL;
1441 lip->li_cond = condstack_release();
1442 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1443 a_LEX_MACRO_CMD |
1444 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1445 ? a_LEX_SUPER_MACRO : 0);
1446 lip->li_loff = 0;
1447 lip->li_lines = (void*)(lip->li_name + ial);
1448 lip->li_lines[0] = UNCONST(cmd); /* dup'ed above */
1449 lip->li_lines[1] = NULL;
1450 memcpy(lip->li_name, cmd, i);
1452 pstate |= PS_ROBOT;
1453 a_lex_input = lip;
1454 rv = a_commands_recursive();
1455 NYD_LEAVE;
1456 return rv;
1459 FL bool_t
1460 n_source_may_yield_control(void){
1461 return ((options & OPT_INTERACTIVE) &&
1462 (pstate & PS_STARTED) &&
1463 (!(pstate & PS_ROBOT) || (pstate & PS_RECURSED)) && /* Ok for ~: */
1464 (a_lex_input == NULL || a_lex_input->li_outer == NULL));
1467 /* s-it-mode */