SEND_TODISP(_ALL)?: append newline if message does have no final one
[s-mailx.git] / lex_input.c
blob9e87fcfaa64dc96e151b112bb7a1b655592a34a2
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"),
216 n_shell_quote_cp(argv[0], FAL0));
217 v = NULL;
218 goto jleave;
221 /* Show command of single ghost? */
222 if(argv[1] == NULL){
223 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
224 if(!strcmp(argv[0], gp->lg_name)){
225 printf("wysh ghost %s %s\n",
226 gp->lg_name, n_shell_quote_cp(gp->lg_cmd.s, TRU1));
227 goto jleave;
229 n_err(_("`ghost': no such alias: %s\n"), argv[0]);
230 v = NULL;
231 goto jleave;
234 /* Define command for ghost: verify command content */
235 for(cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
236 if(*cp != '\0')
237 cl += strlen(cp) +1; /* SP or NUL */
238 if(cl == 0){
239 n_err(_("`ghost': empty command arguments after %s\n"), argv[0]);
240 v = NULL;
241 goto jleave;
244 /* If the ghost already exists, recreate */
245 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL; lgp = gp, gp = gp->lg_next)
246 if(!strcmp(gp->lg_name, argv[0])){
247 if(lgp != NULL)
248 lgp->lg_next = gp->lg_next;
249 else
250 a_lex_ghosts = gp->lg_next;
251 free(gp);
252 break;
255 nl = strlen(argv[0]) +1;
256 gp = smalloc(sizeof(*gp) - VFIELD_SIZEOF(struct a_lex_ghost, lg_name) +
257 nl + cl);
258 gp->lg_next = a_lex_ghosts;
259 a_lex_ghosts = gp;
260 memcpy(gp->lg_name, argv[0], nl);
261 cp = gp->lg_cmd.s = gp->lg_name + nl;
262 gp->lg_cmd.l = --cl;
264 while(*++argv != NULL)
265 if((i = strlen(*argv)) > 0){
266 memcpy(cp, *argv, i);
267 cp += i;
268 *cp++ = ' ';
270 *--cp = '\0';
271 jleave:
272 NYD_LEAVE;
273 return v == NULL;
276 static int
277 a_lex_c_unghost(void *v){
278 struct a_lex_ghost *lgp, *gp;
279 char const **argv, *cp;
280 int rv;
281 NYD_ENTER;
283 rv = 0;
284 argv = v;
286 while((cp = *argv++) != NULL){
287 if(cp[0] == '*' && cp[1] == '\0'){
288 while((gp = a_lex_ghosts) != NULL){
289 a_lex_ghosts = gp->lg_next;
290 free(gp);
292 }else{
293 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL;
294 lgp = gp, gp = gp->lg_next)
295 if(!strcmp(gp->lg_name, cp)){
296 if(lgp != NULL)
297 lgp->lg_next = gp->lg_next;
298 else
299 a_lex_ghosts = gp->lg_next;
300 free(gp);
301 goto jouter;
303 n_err(_("`unghost': no such alias: %s\n"),
304 n_shell_quote_cp(cp, FAL0));
305 rv = 1;
306 jouter: ;
309 NYD_LEAVE;
310 return rv;
313 static int
314 a_lex_c_list(void *v){
315 FILE *fp;
316 struct a_lex_cmd const **cpa, *cp, **cursor;
317 size_t l, i;
318 NYD_ENTER;
320 i = NELEM(a_lex_cmd_tab) + NELEM(a_lex_special_cmd_tab) +1;
321 cpa = salloc(sizeof(cp) * i);
323 for(i = 0; i < NELEM(a_lex_cmd_tab); ++i)
324 cpa[i] = &a_lex_cmd_tab[i];
325 /* C99 */{
326 size_t j;
328 for(j = 0; j < NELEM(a_lex_special_cmd_tab); ++i, ++j)
329 cpa[i] = &a_lex_special_cmd_tab[j];
331 cpa[i] = NULL;
333 /* C99 */{
334 char const *xcp = v;
336 if(*xcp == '\0')
337 qsort(cpa, i, sizeof(xcp), &a_lex__pcmd_cmp);
340 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
341 fp = stdout;
343 fprintf(fp, _("Commands are:\n"));
344 l = 1;
345 for(i = 0, cursor = cpa; (cp = *cursor++) != NULL;){
346 if(cp->lc_func == &c_cmdnotsupp)
347 continue;
348 if(options & OPT_D_V){
349 char const *argt;
351 switch(cp->lc_argtype & ARG_ARGMASK){
352 case ARG_MSGLIST: argt = N_("message-list"); break;
353 case ARG_STRLIST: argt = N_("a \"string\""); break;
354 case ARG_RAWLIST: argt = N_("old-style quoting"); break;
355 case ARG_NOLIST: argt = N_("no arguments"); break;
356 case ARG_NDMLIST: argt = N_("message-list (without a default)"); break;
357 case ARG_WYSHLIST: argt = N_("sh(1)ell-style quoting"); break;
358 default: argt = N_("`wysh' for sh(1)ell-style quoting"); break;
360 #ifdef HAVE_DOCSTRINGS
361 fprintf(fp, _("`%s'. Argument type: %s.\n\t%s\n"),
362 cp->lc_name, V_(argt), V_(cp->lc_doc));
363 l += 2;
364 #else
365 fprintf(fp, "`%s' (%s)\n", cp->lc_name, argt);
366 ++l;
367 #endif
368 }else{
369 size_t j = strlen(cp->lc_name) + 2;
371 if((i += j) > 72){
372 i = j;
373 fprintf(fp, "\n");
374 ++l;
376 fprintf(fp, (*cursor != NULL ? "%s, " : "%s\n"), cp->lc_name);
380 if(fp != stdout){
381 page_or_print(fp, l);
382 Fclose(fp);
384 NYD_LEAVE;
385 return 0;
388 static int
389 a_lex__pcmd_cmp(void const *s1, void const *s2){
390 struct a_lex_cmd const * const *cp1, * const *cp2;
391 int rv;
392 NYD2_ENTER;
394 cp1 = s1;
395 cp2 = s2;
396 rv = strcmp((*cp1)->lc_name, (*cp2)->lc_name);
397 NYD2_LEAVE;
398 return rv;
401 static int
402 a_lex_c_quit(void *v){
403 NYD_ENTER;
404 UNUSED(v);
406 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
407 * Otherwise return -1 to abort command loop */
408 pstate |= PS_EXIT;
409 NYD_LEAVE;
410 return 0;
413 static int
414 a_lex_c_features(void *v){
415 NYD_ENTER;
417 UNUSED(v);
419 printf(_("Features: %s\n"), ok_vlook(features));
420 NYD_LEAVE;
421 return 0;
424 static int
425 a_lex_c_version(void *v){
426 NYD_ENTER;
428 UNUSED(v);
430 printf(_("Version %s\n"), ok_vlook(version));
431 NYD_LEAVE;
432 return 0;
435 static void
436 a_lex_update_pstate(void){
437 NYD_ENTER;
439 if(pstate & PS_SIGWINCH_PEND){
440 char buf[32];
442 snprintf(buf, sizeof buf, "%d", scrnwidth);
443 ok_vset(COLUMNS, buf);
444 snprintf(buf, sizeof buf, "%d", scrnheight);
445 ok_vset(LINES, buf);
448 pstate &= ~PS_PSTATE_PENDMASK;
449 NYD_LEAVE;
452 static int
453 a_lex_evaluate(struct a_lex_eval_ctx *evp){
454 /* xxx old style(9), but also old code */
455 struct str line;
456 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
457 struct a_lex_ghost *gp;
458 struct a_lex_cmd const *cmd;
459 int c, e;
460 bool_t wysh;
461 NYD_ENTER;
463 wysh = FAL0;
464 e = 1;
465 cmd = NULL;
466 gp = NULL;
467 line = evp->le_line; /* XXX don't change original (buffer pointer) */
468 assert(line.s[line.l] == '\0');
469 evp->le_add_history = FAL0;
470 evp->le_new_content = NULL;
472 /* Command ghosts that refer to shell commands or macro expansion restart */
473 jrestart:
475 /* Strip the white space away from end and beginning of command */
476 if(line.l > 0){
477 size_t i = line.l;
479 for(cp = &line.s[i -1]; spacechar(*cp); --cp)
480 --i;
481 line.l = i;
483 for(cp = line.s; spacechar(*cp); ++cp)
485 line.l -= PTR2SIZE(cp - line.s);
487 /* Ignore null commands (comments) */
488 if(*cp == '#')
489 goto jleave0;
491 /* Handle ! differently to get the correct lexical conventions */
492 arglist[0] = cp;
493 if(*cp == '!')
494 ++cp;
495 /* Isolate the actual command; since it may not necessarily be
496 * separated from the arguments (as in `p1') we need to duplicate it to
497 * be able to create a NUL terminated version.
498 * We must be aware of several special one letter commands here */
499 else if((cp = a_lex_isolate(cp)) == arglist[0] &&
500 (*cp == '|' || *cp == '~' || *cp == '?'))
501 ++cp;
502 c = (int)PTR2SIZE(cp - arglist[0]);
503 line.l -= c;
504 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
505 memcpy(word, arglist[0], c);
506 word[c] = '\0';
508 /* Look up the command; if not found, bitch.
509 * Normally, a blank command would map to the first command in the
510 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
511 * confusion; act just the same for ghosts */
512 if(*word == '\0'){
513 if((pstate & PS_ROBOT) || gp != NULL)
514 goto jleave0;
515 cmd = a_lex_cmd_tab + 0;
516 goto jexec;
519 /* XXX It may be the argument parse adjuster */
520 if(!wysh && c == sizeof("wysh") -1 && !asccasecmp(word, "wysh")){
521 wysh = TRU1;
522 line.s = cp;
523 goto jrestart;
526 /* If this is the first evaluation, check command ghosts */
527 if(gp == NULL){
528 /* TODO relink list head, so it's sorted on usage over time?
529 * TODO in fact, there should be one hashmap over all commands and ghosts
530 * TODO so that the lookup could be made much more efficient than it is
531 * TODO now (two adjacent list searches! */
532 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
533 if(!strcmp(word, gp->lg_name)){
534 if(line.l > 0){
535 size_t i;
537 i = gp->lg_cmd.l;
538 line.s = salloc(i + line.l +1);
539 memcpy(line.s, gp->lg_cmd.s, i);
540 memcpy(line.s + i, cp, line.l);
541 line.s[i += line.l] = '\0';
542 line.l = i;
543 }else{
544 line.s = gp->lg_cmd.s;
545 line.l = gp->lg_cmd.l;
547 goto jrestart;
551 if((cmd = a_lex__firstfit(word)) == NULL || cmd->lc_func == &c_cmdnotsupp){
552 bool_t s;
554 if(!(s = condstack_isskip()) || (options & OPT_D_V))
555 n_err(_("Unknown command%s: `%s'\n"),
556 (s ? _(" (ignored due to `if' condition)") : ""), word);
557 if(s)
558 goto jleave0;
559 if(cmd != NULL){
560 c_cmdnotsupp(NULL);
561 cmd = NULL;
563 goto jleave;
566 /* See if we should execute the command -- if a conditional we always
567 * execute it, otherwise, check the state of cond */
568 jexec:
569 if(!(cmd->lc_argtype & ARG_F) && condstack_isskip())
570 goto jleave0;
572 /* Process the arguments to the command, depending on the type it expects */
573 if(!(cmd->lc_argtype & ARG_M) && (options & OPT_SENDMODE)){
574 n_err(_("May not execute `%s' while sending\n"), cmd->lc_name);
575 goto jleave;
577 if((cmd->lc_argtype & ARG_S) && !(pstate & PS_STARTED)){
578 n_err(_("May not execute `%s' during startup\n"), cmd->lc_name);
579 goto jleave;
581 if((cmd->lc_argtype & ARG_I) &&
582 !(options & (OPT_INTERACTIVE | OPT_BATCH_FLAG))){
583 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
584 cmd->lc_name);
585 goto jleave;
587 if((cmd->lc_argtype & ARG_R) && (pstate & PS_RECURSED)){
588 n_err(_("Cannot invoke `%s' when in recursed mode (e.g., composing)\n"),
589 cmd->lc_name);
590 goto jleave;
593 if((cmd->lc_argtype & ARG_W) && !(mb.mb_perm & MB_DELE)){
594 n_err(_("May not execute `%s' -- message file is read only\n"),
595 cmd->lc_name);
596 goto jleave;
598 if((cmd->lc_argtype & ARG_A) && mb.mb_type == MB_VOID){
599 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd->lc_name);
600 goto jleave;
603 if(cmd->lc_argtype & ARG_O)
604 OBSOLETE2(_("this command will be removed"), cmd->lc_name);
605 if(cmd->lc_argtype & ARG_V)
606 temporary_arg_v_store = NULL;
608 if(wysh && (cmd->lc_argtype & ARG_ARGMASK) != ARG_WYRALIST)
609 n_err(_("`wysh' prefix doesn't affect `%s'\n"), cmd->lc_name);
610 switch(cmd->lc_argtype & ARG_ARGMASK){
611 case ARG_MSGLIST:
612 /* Message list defaulting to nearest forward legal message */
613 if(n_msgvec == NULL)
614 goto je96;
615 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
616 break;
617 if(c == 0){
618 if((n_msgvec[0] = first(cmd->lc_msgflag, cmd->lc_msgmask)) != 0)
619 n_msgvec[1] = 0;
621 if(n_msgvec[0] == 0){
622 if(!(pstate & PS_HOOK_MASK))
623 printf(_("No applicable messages\n"));
624 break;
626 e = (*cmd->lc_func)(n_msgvec);
627 break;
629 case ARG_NDMLIST:
630 /* Message list with no defaults, but no error if none exist */
631 if(n_msgvec == NULL){
632 je96:
633 n_err(_("Invalid use of message list\n"));
634 break;
636 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
637 break;
638 e = (*cmd->lc_func)(n_msgvec);
639 break;
641 case ARG_STRLIST:
642 /* Just the straight string, with leading blanks removed */
643 while(whitechar(*cp))
644 ++cp;
645 e = (*cmd->lc_func)(cp);
646 break;
648 case ARG_WYSHLIST:
649 c = 1;
650 if(0){
651 /* FALLTHRU */
652 case ARG_WYRALIST:
653 c = wysh ? 1 : 0;
654 if(0){
655 case ARG_RAWLIST:
656 c = 0;
660 if((c = getrawlist((c != 0), arglist, NELEM(arglist), cp, line.l)) < 0){
661 n_err(_("Invalid argument list\n"));
662 break;
664 if(c < cmd->lc_minargs){
665 n_err(_("`%s' requires at least %d arg(s)\n"),
666 cmd->lc_name, cmd->lc_minargs);
667 break;
669 #undef lc_minargs
670 if(c > cmd->lc_maxargs){
671 n_err(_("`%s' takes no more than %d arg(s)\n"),
672 cmd->lc_name, cmd->lc_maxargs);
673 break;
675 #undef lc_maxargs
676 e = (*cmd->lc_func)(arglist);
677 break;
679 case ARG_NOLIST:
680 /* Just the constant zero, for exiting, eg. */
681 e = (*cmd->lc_func)(0);
682 break;
684 default:
685 DBG( n_panic(_("Implementation error: unknown argument type: %d"),
686 cmd->lc_argtype & ARG_ARGMASK); )
687 goto jleave0;
690 if(e == 0 && (cmd->lc_argtype & ARG_V) &&
691 (cp = temporary_arg_v_store) != NULL){
692 temporary_arg_v_store = NULL;
693 evp->le_new_content = cp;
694 goto jleave0;
696 if(!(cmd->lc_argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
697 evp->le_add_history = TRU1;
699 jleave:
700 /* Exit the current source file on error TODO what a mess! */
701 if(e == 0)
702 pstate &= ~PS_EVAL_ERROR;
703 else{
704 pstate |= PS_EVAL_ERROR;
705 if(e < 0 || (pstate & PS_ROBOT)){ /* FIXME */
706 e = 1;
707 goto jret;
709 goto jret0;
712 if(cmd == NULL)
713 goto jret0;
714 if((cmd->lc_argtype & ARG_P) && ok_blook(autoprint))
715 if(visible(dot)){
716 line.s = savestr("type");
717 line.l = sizeof("type") -1;
718 gp = (struct a_lex_ghost*)-1; /* Avoid `ghost' interpretation */
719 goto jrestart;
722 if(!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(cmd->lc_argtype & ARG_T))
723 pstate |= PS_SAW_COMMAND;
724 jleave0:
725 pstate &= ~PS_EVAL_ERROR;
726 jret0:
727 e = 0;
728 jret:
730 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
732 NYD_LEAVE;
733 return e;
736 static struct a_lex_cmd const *
737 a_lex__firstfit(char const *comm){ /* TODO *hashtable*! linear list search!!! */
738 struct a_lex_cmd const *cp;
739 NYD2_ENTER;
741 for(cp = a_lex_cmd_tab; PTRCMP(cp, <, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)]);
742 ++cp)
743 if(*comm == *cp->lc_name && is_prefix(comm, cp->lc_name))
744 goto jleave;
745 cp = NULL;
746 jleave:
747 NYD2_LEAVE;
748 return cp;
751 static void
752 a_lex_hangup(int s){
753 NYD_X; /* Signal handler */
754 UNUSED(s);
755 /* nothing to do? */
756 exit(EXIT_ERR);
759 static void
760 a_lex_onintr(int s){
761 NYD_X; /* Signal handler */
763 safe_signal(SIGINT, a_lex_onintr);
764 noreset = 0;
765 a_lex_unstack(TRUM1);
767 termios_state_reset();
768 close_all_files(); /* FIXME .. to outer level ONLU! */
770 if(image >= 0){
771 close(image);
772 image = -1;
774 if(interrupts != 1)
775 n_err_sighdl(_("Interrupt\n"));
776 safe_signal(SIGPIPE, a_lex_oldpipe);
777 siglongjmp(srbuf, 0); /* FIXME get rid */
780 static void
781 a_lex_unstack(bool_t eval_error){
782 struct a_lex_input_stack *lip;
783 NYD_ENTER;
785 if((lip = a_lex_input) == NULL){
786 /* If called from a_lex_onintr(), be silent FIXME */
787 pstate &= ~(PS_SOURCING | PS_ROBOT);
788 if(eval_error == TRUM1 || !(pstate & PS_STARTED))
789 goto jleave;
790 goto jerr;
793 if(lip->li_flags & a_LEX_MACRO){
794 if(lip->li_flags & a_LEX_MACRO_FREE_DATA){
795 char **lp;
797 while(*(lp = &lip->li_lines[lip->li_loff]) != NULL){
798 free(*lp);
799 ++lip->li_loff;
801 /* Part of lip's memory chunk, then */
802 if(!(lip->li_flags & a_LEX_MACRO_CMD))
803 free(lip->li_lines);
805 }else{
806 if(lip->li_flags & a_LEX_PIPE)
807 /* XXX command manager should -TERM then -KILL instead of hoping
808 * XXX for exit of provider due to EPIPE / SIGPIPE */
809 Pclose(lip->li_file, TRU1);
810 else
811 Fclose(lip->li_file);
814 if(!condstack_take(lip->li_cond)){
815 n_err(_("Unmatched `if' at end of %s %s\n"),
816 ((lip->li_flags & a_LEX_MACRO
817 ? (lip->li_flags & a_LEX_MACRO_CMD ? _("command") : _("macro"))
818 : _("`source'd file"))),
819 lip->li_name);
820 eval_error = TRU1;
823 if((a_lex_input = lip->li_outer) == NULL){
824 pstate &= ~(PS_SOURCING | PS_ROBOT);
825 }else{
826 if((a_lex_input->li_flags & (a_LEX_MACRO | a_LEX_SUPER_MACRO)) ==
827 (a_LEX_MACRO | a_LEX_SUPER_MACRO))
828 pstate &= ~PS_SOURCING;
829 assert(pstate & PS_ROBOT);
832 if(eval_error)
833 goto jerr;
834 if(lip->li_flags & a_LEX_FREE)
835 free(lip);
836 jleave:
837 if(UNLIKELY(a_lex_input != NULL && eval_error == TRUM1))
838 a_lex_unstack(TRUM1);
839 NYD_LEAVE;
840 return;
842 jerr:
843 if(lip != NULL){
844 if(options & OPT_D_V)
845 n_alert(_("Stopped %s %s due to errors%s"),
846 (pstate & PS_STARTED
847 ? (lip->li_flags & a_LEX_MACRO
848 ? (lip->li_flags & a_LEX_MACRO_CMD
849 ? _("evaluating command") : _("evaluating macro"))
850 : (lip->li_flags & a_LEX_PIPE
851 ? _("executing `source'd pipe")
852 : _("loading `source'd file")))
853 : (lip->li_flags & a_LEX_MACRO
854 ? (lip->li_flags & a_LEX_MACRO_X_OPTION
855 ? _("evaluating command line") : _("evaluating macro"))
856 : _("loading initialization resource"))),
857 lip->li_name,
858 (options & OPT_DEBUG ? "" : _(" (enable *debug* for trace)")));
859 if(lip->li_flags & a_LEX_FREE)
860 free(lip);
863 if(!(options & OPT_INTERACTIVE) && !(pstate & PS_STARTED)){
864 if(options & OPT_D_V)
865 n_alert(_("Non-interactive, bailing out due to errors "
866 "in startup load phase"));
867 exit(EXIT_ERR);
869 goto jleave;
872 static bool_t
873 a_lex_source_file(char const *file, bool_t silent_error){
874 struct a_lex_input_stack *lip;
875 size_t nlen;
876 char *nbuf;
877 bool_t ispipe;
878 FILE *fip;
879 NYD_ENTER;
881 fip = NULL;
883 /* Being a command argument file is space-trimmed */
884 if((ispipe = (!silent_error && (nlen = strlen(file)) > 0 &&
885 file[--nlen] == '|'))){
886 if((fip = Popen(nbuf = savestrbuf(file, nlen), "r",
887 ok_vlook(SHELL), NULL, COMMAND_FD_NULL)) == NULL){
888 if(!silent_error || (options & OPT_D_V))
889 n_perr(nbuf, 0);
890 goto jleave;
892 }else if((nbuf = fexpand(file, FEXP_LOCAL)) == NULL)
893 goto jleave;
894 else if((fip = Fopen(nbuf, "r")) == NULL){
895 if(!silent_error || (options & OPT_D_V))
896 n_perr(nbuf, 0);
897 goto jleave;
900 lip = smalloc(sizeof(*lip) -
901 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
902 (nlen = strlen(nbuf) +1));
903 lip->li_outer = a_lex_input;
904 lip->li_file = fip;
905 lip->li_cond = condstack_release();
906 lip->li_flags = (ispipe ? a_LEX_PIPE : a_LEX_NONE) |
907 (a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
908 ? a_LEX_SUPER_MACRO : 0);
909 memcpy(lip->li_name, nbuf, nlen);
911 pstate |= PS_SOURCING | PS_ROBOT;
912 a_lex_input = lip;
913 a_commands_recursive();
914 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
915 jleave:
916 NYD_LEAVE;
917 return silent_error ? TRU1 : (fip != NULL);
920 static bool_t
921 a_lex_load(struct a_lex_input_stack *lip){
922 bool_t rv;
923 NYD2_ENTER;
925 assert(!(pstate & PS_STARTED));
926 assert(a_lex_input == NULL);
928 /* POSIX:
929 * Any errors in the start-up file shall either cause mailx to terminate
930 * with a diagnostic message and a non-zero status or to continue after
931 * writing a diagnostic message, ignoring the remainder of the lines in
932 * the start-up file. */
933 lip->li_cond = condstack_release();
935 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
936 pstate |= PS_ROBOT |
937 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
939 pstate |= PS_ROBOT | PS_SOURCING;
940 if(options & OPT_D_V)
941 n_err(_("Loading %s\n"), n_shell_quote_cp(lip->li_name, FAL0));
942 a_lex_input = lip;
943 if(!(rv = n_commands())){
944 if(!(options & OPT_INTERACTIVE)){
945 if(options & OPT_D_V)
946 n_alert(_("Non-interactive program mode, forced exit"));
947 exit(EXIT_ERR);
950 /* PS_EXIT handled by callers */
951 NYD2_LEAVE;
952 return rv;
955 static bool_t
956 a_commands_recursive(void){
957 struct a_lex_eval_ctx ev;
958 bool_t rv;
959 NYD2_ENTER;
961 memset(&ev, 0, sizeof ev);
963 /* FIXME sigkondom */
964 n_COLOUR( n_colour_env_push(); )
965 rv = TRU1;
966 for(;;){
967 int n;
969 /* Read a line of commands and handle end of file specially */
970 ev.le_line.l = ev.le_line_size;
971 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
972 ev.le_new_content);
973 ev.le_line_size = (ui32_t)ev.le_line.l;
974 ev.le_line.l = (ui32_t)n;
976 if(n < 0)
977 break;
979 if(a_lex_evaluate(&ev)){
980 rv = FAL0;
981 break;
984 if((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)){
985 if(exit_status != EXIT_OK)
986 break;
989 a_lex_unstack(!rv);
990 n_COLOUR( n_colour_env_pop(FAL0); )
992 if(ev.le_line.s != NULL)
993 free(ev.le_line.s);
994 NYD2_LEAVE;
995 return rv;
998 #ifdef HAVE_DOCSTRINGS
999 FL bool_t
1000 n_print_comm_docstr(char const *comm){
1001 struct a_lex_ghost const *gp;
1002 struct a_lex_cmd const *cp, *cpmax;
1003 bool_t rv = FAL0;
1004 NYD_ENTER;
1006 /* Ghosts take precedence */
1007 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
1008 if(!strcmp(comm, gp->lg_name)){
1009 printf("%s -> ", comm);
1010 comm = gp->lg_cmd.s;
1011 break;
1014 cpmax = &(cp = a_lex_cmd_tab)[NELEM(a_lex_cmd_tab)];
1015 jredo:
1016 for(; PTRCMP(cp, <, cpmax); ++cp){
1017 if(!strcmp(comm, cp->lc_name))
1018 printf("%s: %s\n", comm, V_(cp->lc_doc));
1019 else if(is_prefix(comm, cp->lc_name))
1020 printf("%s (%s): %s\n", comm, cp->lc_name, V_(cp->lc_doc));
1021 else
1022 continue;
1023 rv = TRU1;
1024 break;
1026 if(!rv && PTRCMP(cpmax, ==, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)])){
1027 cpmax = &(cp = a_lex_special_cmd_tab)[NELEM(a_lex_special_cmd_tab)];
1028 goto jredo;
1031 if(!rv && gp != NULL){
1032 printf("%s\n", n_shell_quote_cp(comm, TRU1));
1033 rv = TRU1;
1035 NYD_LEAVE;
1036 return rv;
1038 #endif /* HAVE_DOCSTRINGS */
1040 FL bool_t
1041 n_commands(void){ /* FIXME */
1042 struct a_lex_eval_ctx ev;
1043 int n;
1044 bool_t volatile rv = TRU1;
1045 NYD_ENTER;
1047 if (!(pstate & PS_SOURCING)) {
1048 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
1049 safe_signal(SIGINT, &a_lex_onintr);
1050 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1051 safe_signal(SIGHUP, &a_lex_hangup);
1053 a_lex_oldpipe = safe_signal(SIGPIPE, SIG_IGN);
1054 safe_signal(SIGPIPE, a_lex_oldpipe);
1056 memset(&ev, 0, sizeof ev);
1058 (void)sigsetjmp(srbuf, 1); /* FIXME get rid */
1059 for (;;) {
1060 char *temporary_orig_line; /* XXX eval_ctx.le_line not yet constant */
1062 n_COLOUR( n_colour_env_pop(TRU1); )
1064 /* TODO Unless we have our signal manager (or however we do it) child
1065 * TODO processes may have time slots where their execution isn't
1066 * TODO protected by signal handlers (in between start and setup
1067 * TODO completed). close_all_files() is only called from onintr()
1068 * TODO so those may linger possibly forever */
1069 if(!(pstate & PS_SOURCING))
1070 close_all_files();
1072 interrupts = 0;
1074 temporary_localopts_free(); /* XXX intermediate hack */
1075 sreset((pstate & PS_SOURCING) != 0);
1076 if (!(pstate & PS_SOURCING)) {
1077 char *cp;
1079 /* TODO Note: this buffer may contain a password. We should redefine
1080 * TODO the code flow which has to do that */
1081 if ((cp = termios_state.ts_linebuf) != NULL) {
1082 termios_state.ts_linebuf = NULL;
1083 termios_state.ts_linesize = 0;
1084 free(cp); /* TODO pool give-back */
1086 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1087 if (ev.le_line.l > LINESIZE * 3) {
1088 free(ev.le_line.s); /* TODO pool! but what? */
1089 ev.le_line.s = NULL;
1090 ev.le_line.l = ev.le_line_size = 0;
1094 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1095 char *cp;
1097 cp = ok_vlook(newmail);
1098 if ((options & OPT_TTYIN) && cp != NULL) {
1099 struct stat st;
1101 /* FIXME TEST WITH NOPOLL ETC. !!! */
1102 n = (cp != NULL && strcmp(cp, "nopoll"));
1103 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
1104 st.st_size > mailsize) ||
1105 (mb.mb_type == MB_MAILDIR && n != 0)) {
1106 size_t odot = PTR2SIZE(dot - message);
1107 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
1109 if (setfile(mailname,
1110 FEDIT_NEWMAIL |
1111 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
1112 exit_status |= EXIT_ERR;
1113 rv = FAL0;
1114 break;
1116 dot = message + odot;
1117 pstate |= odid;
1121 exit_status = EXIT_OK;
1124 /* Read a line of commands and handle end of file specially */
1125 jreadline:
1126 ev.le_line.l = ev.le_line_size;
1127 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
1128 ev.le_new_content);
1129 ev.le_line_size = (ui32_t)ev.le_line.l;
1130 ev.le_line.l = (ui32_t)n;
1132 if (n < 0) {
1133 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1134 if (!(pstate & PS_ROBOT) &&
1135 (options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
1136 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1137 n_msleep(500, FAL0);
1138 continue;
1140 break;
1143 temporary_orig_line = ((pstate & PS_SOURCING) ||
1144 !(options & OPT_INTERACTIVE)) ? NULL
1145 : savestrbuf(ev.le_line.s, ev.le_line.l);
1146 pstate &= ~PS_HOOK_MASK;
1147 if (a_lex_evaluate(&ev)) {
1148 if (!(pstate & PS_STARTED)) /* TODO mess; join PS_EVAL_ERROR.. */
1149 rv = FAL0;
1150 break;
1153 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
1154 if (exit_status != EXIT_OK)
1155 break;
1156 if ((pstate & (PS_SOURCING | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
1157 exit_status = EXIT_ERR;
1158 break;
1161 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1162 if (ev.le_new_content != NULL)
1163 goto jreadline;
1164 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1165 if (temporary_orig_line != NULL)
1166 n_tty_addhist(temporary_orig_line, !ev.le_add_history);
1169 if(pstate & PS_EXIT)
1170 break;
1172 a_lex_unstack(!rv);
1174 if (ev.le_line.s != NULL)
1175 free(ev.le_line.s);
1176 if (pstate & PS_SOURCING)
1177 sreset(FAL0);
1178 NYD_LEAVE;
1179 return rv;
1182 FL int
1183 (n_lex_input)(char const *prompt, bool_t nl_escape, char **linebuf,
1184 size_t *linesize, char const *string SMALLOC_DEBUG_ARGS){
1185 /* TODO readline: linebuf pool! */
1186 bool_t doprompt, dotty;
1187 int n, nold;
1188 FILE *ifile;
1189 NYD2_ENTER;
1191 /* Special case macro mode: never need to prompt, lines have always been
1192 * unfolded already */
1193 if(a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO)){
1194 char **lp = &a_lex_input->li_lines[a_lex_input->li_loff];
1196 if(*linebuf != NULL)
1197 free(*linebuf);
1199 if((*linebuf = *lp) == NULL){
1200 *linesize = 0;
1201 n = -1;
1202 }else{
1203 ++a_lex_input->li_loff;
1204 n = (int)(*linesize = strlen(*linebuf));
1206 if(!(a_lex_input->li_flags & a_LEX_MACRO_FREE_DATA))
1207 *linebuf = sbufdup(*linebuf, *linesize);
1209 if(options & OPT_D_VV)
1210 n_err(_("%s %d bytes <%.*s>\n"),
1211 (a_lex_input->li_flags & a_LEX_MACRO_X_OPTION
1212 ? _("-X option") : _("MACRO")),
1213 n, n, *linebuf);
1215 goto jleave;
1218 doprompt = ((pstate & (PS_STARTED | PS_ROBOT)) == PS_STARTED &&
1219 (options & OPT_INTERACTIVE));
1220 dotty = (doprompt && !ok_blook(line_editor_disable));
1221 if(!doprompt)
1222 prompt = NULL;
1223 else if(prompt == NULL)
1224 prompt = getprompt();
1226 /* Ensure stdout is flushed first anyway */
1227 if(!dotty && prompt == NULL)
1228 fflush(stdout);
1230 ifile = (a_lex_input != NULL) ? a_lex_input->li_file : stdin;
1231 assert(ifile != NULL);
1233 for(nold = n = 0;;){
1234 if(dotty){
1235 assert(ifile == stdin);
1236 if(string != NULL && (n = (int)strlen(string)) > 0){
1237 if(*linesize > 0)
1238 *linesize += n +1;
1239 else
1240 *linesize = (size_t)n + LINESIZE +1;
1241 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
1242 memcpy(*linebuf, string, (size_t)n +1);
1244 string = NULL;
1245 /* TODO if nold>0, don't redisplay the entire line!
1246 * TODO needs complete redesign ... */
1247 n = (n_tty_readline)(prompt, linebuf, linesize, n
1248 SMALLOC_DEBUG_ARGSCALL);
1249 }else{
1250 if(prompt != NULL) {
1251 if(*prompt != '\0')
1252 fputs(prompt, stdout);
1253 fflush(stdout);
1256 n = (readline_restart)(ifile, linebuf, linesize, n
1257 SMALLOC_DEBUG_ARGSCALL);
1259 if(n > 0 && nold > 0){
1260 int i = 0;
1261 char const *cp = *linebuf + nold;
1263 while(blankspacechar(*cp) && nold + i < n)
1264 ++cp, ++i;
1265 if(i > 0){
1266 memmove(*linebuf + nold, cp, n - nold - i);
1267 n -= i;
1268 (*linebuf)[n] = '\0';
1273 if(n <= 0)
1274 break;
1276 /* POSIX says:
1277 * An unquoted <backslash> at the end of a command line shall
1278 * be discarded and the next line shall continue the command */
1279 if(!nl_escape || (*linebuf)[n - 1] != '\\')
1280 break;
1281 /* Definitely outside of quotes, thus the quoting rules are so that an
1282 * uneven number of successive backslashs at EOL is a continuation */
1283 if(n > 1){
1284 size_t i, j;
1286 for(j = 1, i = (size_t)n - 1; i-- > 0; ++j)
1287 if((*linebuf)[i] != '\\')
1288 break;
1289 if(!(j & 1))
1290 break;
1292 (*linebuf)[nold = --n] = '\0';
1293 if(prompt != NULL && *prompt != '\0')
1294 prompt = ".. "; /* XXX PS2 .. */
1297 if(n >= 0){
1298 (*linebuf)[*linesize = n] = '\0';
1300 if(options & OPT_D_VV)
1301 n_err(_("%s %d bytes <%.*s>\n"),
1302 (!(pstate & PS_STARTED) ? "LOAD"
1303 : (pstate & PS_SOURCING ? "SOURCE" : "READ")),
1304 n, n, *linebuf);
1306 jleave:
1307 if (pstate & PS_PSTATE_PENDMASK)
1308 a_lex_update_pstate();
1310 NYD2_LEAVE;
1311 return n;
1314 FL char *
1315 n_lex_input_cp_addhist(char const *prompt, char const *string, bool_t isgabby){
1316 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1317 size_t linesize;
1318 char *linebuf, *rv;
1319 int n;
1320 NYD2_ENTER;
1322 linesize = 0;
1323 linebuf = NULL;
1324 rv = NULL;
1326 n = n_lex_input(prompt, TRU1, &linebuf, &linesize, string);
1327 if(n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
1328 (options & OPT_INTERACTIVE))
1329 n_tty_addhist(rv, isgabby);
1331 if(linebuf != NULL)
1332 free(linebuf);
1333 NYD2_LEAVE;
1334 return rv;
1337 FL void
1338 n_load(char const *name){
1339 struct a_lex_input_stack *lip;
1340 size_t i;
1341 FILE *fip;
1342 NYD_ENTER;
1344 if(name == NULL || *name == '\0' || (fip = Fopen(name, "r")) == NULL)
1345 goto jleave;
1347 i = strlen(name) +1;
1348 lip = scalloc(1, sizeof(*lip) -
1349 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) + i);
1350 lip->li_file = fip;
1351 lip->li_flags = a_LEX_FREE;
1352 memcpy(lip->li_name, name, i);
1354 a_lex_load(lip);
1355 pstate &= ~PS_EXIT;
1356 jleave:
1357 NYD_LEAVE;
1360 FL void
1361 n_load_Xargs(char const **lines){
1362 static char const name[] = "-X";
1364 ui8_t buf[sizeof(struct a_lex_input_stack) + sizeof name];
1365 struct a_lex_input_stack *lip;
1366 NYD_ENTER;
1368 memset(buf, 0, sizeof buf);
1369 lip = (void*)buf;
1370 lip->li_flags = a_LEX_MACRO | a_LEX_MACRO_X_OPTION | a_LEX_SUPER_MACRO;
1371 lip->li_lines = UNCONST(lines);
1372 memcpy(lip->li_name, name, sizeof name);
1374 a_lex_load(lip);
1375 if(pstate & PS_EXIT)
1376 exit(EXIT_OK);
1377 NYD_LEAVE;
1380 FL int
1381 c_source(void *v){
1382 int rv;
1383 NYD_ENTER;
1385 rv = (a_lex_source_file(*(char**)v, FAL0) == TRU1) ? 0 : 1;
1386 NYD_LEAVE;
1387 return rv;
1390 FL int
1391 c_source_if(void *v){ /* XXX obsolete?, support file tests in `if' etc.! */
1392 int rv;
1393 NYD_ENTER;
1395 rv = (a_lex_source_file(*(char**)v, TRU1) != FAL0) ? 0 : 1;
1396 NYD_LEAVE;
1397 return rv;
1400 FL bool_t
1401 n_source_macro(char const *name, char **lines){
1402 struct a_lex_input_stack *lip;
1403 size_t i;
1404 int rv;
1405 NYD_ENTER;
1407 lip = smalloc(sizeof(*lip) -
1408 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1409 (i = strlen(name) +1));
1410 lip->li_outer = a_lex_input;
1411 lip->li_file = NULL;
1412 lip->li_cond = condstack_release();
1413 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1414 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1415 ? a_LEX_SUPER_MACRO : 0);
1416 lip->li_loff = 0;
1417 lip->li_lines = lines;
1418 memcpy(lip->li_name, name, i);
1420 pstate |= PS_ROBOT;
1421 a_lex_input = lip;
1422 rv = a_commands_recursive();
1423 NYD_LEAVE;
1424 return rv;
1427 FL bool_t
1428 n_source_command(char const *cmd){
1429 struct a_lex_input_stack *lip;
1430 size_t i, ial;
1431 bool_t rv;
1432 NYD_ENTER;
1434 i = strlen(cmd);
1435 cmd = sbufdup(cmd, i++);
1436 ial = n_ALIGN(i);
1438 lip = smalloc(sizeof(*lip) -
1439 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1440 ial + 2*sizeof(char*));
1441 lip->li_outer = a_lex_input;
1442 lip->li_file = NULL;
1443 lip->li_cond = condstack_release();
1444 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1445 a_LEX_MACRO_CMD |
1446 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1447 ? a_LEX_SUPER_MACRO : 0);
1448 lip->li_loff = 0;
1449 lip->li_lines = (void*)(lip->li_name + ial);
1450 lip->li_lines[0] = UNCONST(cmd); /* dup'ed above */
1451 lip->li_lines[1] = NULL;
1452 memcpy(lip->li_name, cmd, i);
1454 pstate |= PS_ROBOT;
1455 a_lex_input = lip;
1456 rv = a_commands_recursive();
1457 NYD_LEAVE;
1458 return rv;
1461 FL bool_t
1462 n_source_may_yield_control(void){
1463 return ((options & OPT_INTERACTIVE) &&
1464 (pstate & PS_STARTED) &&
1465 (!(pstate & PS_ROBOT) || (pstate & PS_RECURSED)) && /* Ok for ~: */
1466 (a_lex_input == NULL || a_lex_input->li_outer == NULL));
1469 /* s-it-mode */