n_lex_input(): truly support line continuation via backslash..
[s-mailx.git] / lex_input.c
blob05496b99ff19895f26fca496336afd287eb18bac
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 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 */
100 /* */
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);
115 /* `quit' command */
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 /* Evaluate a single command.
125 * .le_add_history and .le_new_content will be updated upon success.
126 * Command functions return 0 for success, 1 for error, and -1 for abort.
127 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
128 static int a_lex_evaluate(struct a_lex_eval_ctx *evp);
130 /* Get first-fit, or NULL */
131 static struct a_lex_cmd const *a_lex__firstfit(char const *comm);
133 /* When we wake up after ^Z, reprint the prompt */
134 static void a_lex_stop(int s);
136 /* Branch here on hangup signal and simulate "exit" */
137 static void a_lex_hangup(int s);
139 /* The following gets called on receipt of an interrupt. Close all open files
140 * except 0, 1, 2, and the temporary. Also, unstack all source files */
141 static void a_lex_onintr(int s);
143 /* Pop the current input back to the previous level. Update the program state.
144 * If the argument is TRUM1 then we don't alert and error out if the stack
145 * doesn't exist at all */
146 static void a_lex_unstack(bool_t eval_error);
148 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
149 static bool_t a_lex_source_file(char const *file, bool_t silent_error);
151 /* System resource file load()ing or -X command line option array traversal */
152 static bool_t a_lex_load(struct a_lex_input_stack *lip);
154 /* A simplified command loop for recursed state machines */
155 static bool_t a_commands_recursive(void);
157 /* List of all commands, and list of commands which are specially treated
158 * and deduced in _evaluate(), but we need a list for _c_list() and
159 * print_comm_docstr() */
160 #ifdef HAVE_DOCSTRINGS
161 # define DS(S) , S
162 #else
163 # define DS(S)
164 #endif
165 static struct a_lex_cmd const a_lex_cmd_tab[] = {
166 #include "cmd_tab.h"
168 a_lex_special_cmd_tab[] = {
169 { "#", NULL, 0, 0, 0
170 DS(N_("\"Comment command\": ignore remaining (continuable) line")) },
171 { "-", NULL, 0, 0, 0
172 DS(N_("Print out the preceding message")) }
174 #undef DS
176 static char *
177 a_lex_isolate(char const *comm){
178 NYD2_ENTER;
179 while(*comm != '\0' &&
180 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
181 ++comm;
182 NYD2_LEAVE;
183 return UNCONST(comm);
186 static int
187 a_lex_c_ghost(void *v){
188 struct a_lex_ghost *lgp, *gp;
189 size_t i, cl, nl;
190 char *cp;
191 char const **argv;
192 NYD_ENTER;
194 argv = v;
196 /* Show the list? */
197 if(*argv == NULL){
198 FILE *fp;
200 if((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
201 fp = stdout;
203 for(i = 0, gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
204 fprintf(fp, "ghost %s \"%s\"\n",
205 gp->lg_name, string_quote(gp->lg_cmd.s));
207 if(fp != stdout){
208 page_or_print(fp, i);
209 Fclose(fp);
211 goto jleave;
214 /* Verify the ghost name is a valid one */
215 if(*argv[0] == '\0' || *a_lex_isolate(argv[0]) != '\0'){
216 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
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("ghost %s \"%s\"\n",
226 gp->lg_name, string_quote(gp->lg_cmd.s));
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"), cp);
304 rv = 1;
305 jouter: ;
308 NYD_LEAVE;
309 return rv;
312 static int
313 a_lex_c_list(void *v){
314 struct a_lex_cmd const **cpa, *cp, **cursor;
315 size_t i;
316 NYD_ENTER;
318 UNUSED(v);
320 i = NELEM(a_lex_cmd_tab) + NELEM(a_lex_special_cmd_tab) + 1;
321 cpa = ac_alloc(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 qsort(cpa, i, sizeof(cp), &a_lex__pcmd_cmp);
335 printf(_("Commands are:\n"));
336 for(i = 0, cursor = cpa; (cp = *cursor++) != NULL;){
337 size_t j;
339 if(cp->lc_func == &c_cmdnotsupp)
340 continue;
341 j = strlen(cp->lc_name) + 2;
342 if((i += j) > 72){
343 i = j;
344 printf("\n");
346 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->lc_name);
349 ac_free(cpa);
350 NYD_LEAVE;
351 return 0;
354 static int
355 a_lex__pcmd_cmp(void const *s1, void const *s2){
356 struct a_lex_cmd const * const *cp1, * const *cp2;
357 int rv;
358 NYD2_ENTER;
360 cp1 = s1;
361 cp2 = s2;
362 rv = strcmp((*cp1)->lc_name, (*cp2)->lc_name);
363 NYD2_LEAVE;
364 return rv;
367 static int
368 a_lex_c_quit(void *v){
369 NYD_ENTER;
370 UNUSED(v);
372 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
373 * Otherwise return -1 to abort command loop */
374 pstate |= PS_EXIT;
375 NYD_LEAVE;
376 return 0;
379 static int
380 a_lex_c_features(void *v){
381 NYD_ENTER;
383 UNUSED(v);
385 printf(_("Features: %s\n"), ok_vlook(features));
386 NYD_LEAVE;
387 return 0;
390 static int
391 a_lex_c_version(void *v){
392 NYD_ENTER;
394 UNUSED(v);
396 printf(_("Version %s\n"), ok_vlook(version));
397 NYD_LEAVE;
398 return 0;
401 static int
402 a_lex_evaluate(struct a_lex_eval_ctx *evp){
403 /* xxx old style(9), but also old code */
404 struct str line;
405 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
406 struct a_lex_ghost *gp;
407 struct a_lex_cmd const *cmd;
408 int c, e;
409 NYD_ENTER;
411 e = 1;
412 cmd = NULL;
413 gp = NULL;
414 line = evp->le_line; /* XXX don't change original (buffer pointer) */
415 assert(line.s[line.l] == '\0');
416 evp->le_add_history = FAL0;
417 evp->le_new_content = NULL;
419 /* Command ghosts that refer to shell commands or macro expansion restart */
420 jrestart:
422 /* Strip the white space away from end and beginning of command */
423 if(line.l > 0){
424 size_t i = line.l;
426 for(cp = &line.s[i -1]; spacechar(*cp); --cp)
427 --i;
428 line.l = i;
430 for(cp = line.s; spacechar(*cp); ++cp)
432 line.l -= PTR2SIZE(cp - line.s);
434 /* Ignore null commands (comments) */
435 if(*cp == '#')
436 goto jleave0;
438 /* Handle ! differently to get the correct lexical conventions */
439 arglist[0] = cp;
440 if(*cp == '!')
441 ++cp;
442 /* Isolate the actual command; since it may not necessarily be
443 * separated from the arguments (as in `p1') we need to duplicate it to
444 * be able to create a NUL terminated version.
445 * We must be aware of several special one letter commands here */
446 else if((cp = a_lex_isolate(cp)) == arglist[0] &&
447 (*cp == '|' || *cp == '~' || *cp == '?'))
448 ++cp;
449 c = (int)PTR2SIZE(cp - arglist[0]);
450 line.l -= c;
451 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
452 memcpy(word, arglist[0], c);
453 word[c] = '\0';
455 /* Look up the command; if not found, bitch.
456 * Normally, a blank command would map to the first command in the
457 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
458 * confusion; act just the same for ghosts */
459 if(*word == '\0'){
460 if((pstate & PS_ROBOT) || gp != NULL)
461 goto jleave0;
462 cmd = a_lex_cmd_tab + 0;
463 goto jexec;
466 /* If this is the first evaluation, check command ghosts */
467 if(gp == NULL){
468 /* TODO relink list head, so it's sorted on usage over time?
469 * TODO in fact, there should be one hashmap over all commands and ghosts
470 * TODO so that the lookup could be made much more efficient than it is
471 * TODO now (two adjacent list searches! */
472 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
473 if(!strcmp(word, gp->lg_name)){
474 if(line.l > 0){
475 size_t i;
477 i = gp->lg_cmd.l;
478 line.s = salloc(i + line.l +1);
479 memcpy(line.s, gp->lg_cmd.s, i);
480 memcpy(line.s + i, cp, line.l);
481 line.s[i += line.l] = '\0';
482 line.l = i;
483 }else{
484 line.s = gp->lg_cmd.s;
485 line.l = gp->lg_cmd.l;
487 goto jrestart;
491 if((cmd = a_lex__firstfit(word)) == NULL || cmd->lc_func == &c_cmdnotsupp){
492 bool_t s;
494 if(!(s = condstack_isskip()) || (options & OPT_D_V))
495 n_err(_("Unknown command%s: `%s'\n"),
496 (s ? _(" (ignored due to `if' condition)") : ""), word);
497 if(s)
498 goto jleave0;
499 if(cmd != NULL){
500 c_cmdnotsupp(NULL);
501 cmd = NULL;
503 goto jleave;
506 /* See if we should execute the command -- if a conditional we always
507 * execute it, otherwise, check the state of cond */
508 jexec:
509 if(!(cmd->lc_argtype & ARG_F) && condstack_isskip())
510 goto jleave0;
512 /* Process the arguments to the command, depending on the type it expects */
513 if(!(cmd->lc_argtype & ARG_M) && (options & OPT_SENDMODE)){
514 n_err(_("May not execute `%s' while sending\n"), cmd->lc_name);
515 goto jleave;
517 if((cmd->lc_argtype & ARG_S) && !(pstate & PS_STARTED)){
518 n_err(_("May not execute `%s' during startup\n"), cmd->lc_name);
519 goto jleave;
521 if((cmd->lc_argtype & ARG_I) &&
522 !(options & (OPT_INTERACTIVE | OPT_BATCH_FLAG))){
523 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
524 cmd->lc_name);
525 goto jleave;
527 if((cmd->lc_argtype & ARG_R) && (pstate & PS_RECURSED)){
528 n_err(_("Cannot invoke `%s' when in recursed mode (e.g., composing)\n"),
529 cmd->lc_name);
530 goto jleave;
533 if((cmd->lc_argtype & ARG_W) && !(mb.mb_perm & MB_DELE)){
534 n_err(_("May not execute `%s' -- message file is read only\n"),
535 cmd->lc_name);
536 goto jleave;
538 if((cmd->lc_argtype & ARG_A) && mb.mb_type == MB_VOID){
539 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd->lc_name);
540 goto jleave;
543 if(cmd->lc_argtype & ARG_O)
544 OBSOLETE2(_("this command will be removed"), cmd->lc_name);
545 if(cmd->lc_argtype & ARG_V)
546 temporary_arg_v_store = NULL;
548 switch(cmd->lc_argtype & ARG_ARGMASK){
549 case ARG_MSGLIST:
550 /* Message list defaulting to nearest forward legal message */
551 if(n_msgvec == NULL)
552 goto je96;
553 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
554 break;
555 if(c == 0){
556 if((n_msgvec[0] = first(cmd->lc_msgflag, cmd->lc_msgmask)) != 0)
557 n_msgvec[1] = 0;
559 if(n_msgvec[0] == 0){
560 if(!(pstate & PS_HOOK_MASK))
561 printf(_("No applicable messages\n"));
562 break;
564 e = (*cmd->lc_func)(n_msgvec);
565 break;
567 case ARG_NDMLIST:
568 /* Message list with no defaults, but no error if none exist */
569 if(n_msgvec == NULL){
570 je96:
571 n_err(_("Invalid use of \"message list\"\n"));
572 break;
574 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
575 break;
576 e = (*cmd->lc_func)(n_msgvec);
577 break;
579 case ARG_STRLIST:
580 /* Just the straight string, with leading blanks removed */
581 while(whitechar(*cp))
582 ++cp;
583 e = (*cmd->lc_func)(cp);
584 break;
586 case ARG_RAWLIST:
587 case ARG_ECHOLIST:
588 /* A vector of strings, in shell style */
589 if((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
590 ((cmd->lc_argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
591 break;
592 if(c < cmd->lc_minargs){
593 n_err(_("`%s' requires at least %d arg(s)\n"),
594 cmd->lc_name, cmd->lc_minargs);
595 break;
597 #undef lc_minargs
598 if(c > cmd->lc_maxargs){
599 n_err(_("`%s' takes no more than %d arg(s)\n"),
600 cmd->lc_name, cmd->lc_maxargs);
601 break;
603 #undef lc_maxargs
604 e = (*cmd->lc_func)(arglist);
605 break;
607 case ARG_NOLIST:
608 /* Just the constant zero, for exiting, eg. */
609 e = (*cmd->lc_func)(0);
610 break;
612 default:
613 DBG( n_panic(_("Implementation error: unknown argument type")); )
614 goto jleave0;
617 if(e == 0 && (cmd->lc_argtype & ARG_V) &&
618 (cp = temporary_arg_v_store) != NULL){
619 temporary_arg_v_store = NULL;
620 evp->le_new_content = cp;
621 goto jleave0;
623 if(!(cmd->lc_argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
624 evp->le_add_history = TRU1;
626 jleave:
627 /* Exit the current source file on error TODO what a mess! */
628 if(e == 0)
629 pstate &= ~PS_EVAL_ERROR;
630 else{
631 pstate |= PS_EVAL_ERROR;
632 if(e < 0 || (pstate & PS_ROBOT)){ /* FIXME */
633 e = 1;
634 goto jret;
636 goto jret0;
639 if(cmd == NULL)
640 goto jret0;
641 if((cmd->lc_argtype & ARG_P) && ok_blook(autoprint))
642 if(visible(dot)){
643 line.s = savestr("type");
644 line.l = sizeof("type") -1;
645 gp = (struct a_lex_ghost*)-1; /* Avoid `ghost' interpretation */
646 goto jrestart;
649 if(!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(cmd->lc_argtype & ARG_T))
650 pstate |= PS_SAW_COMMAND;
651 jleave0:
652 pstate &= ~PS_EVAL_ERROR;
653 jret0:
654 e = 0;
655 jret:
657 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
659 NYD_LEAVE;
660 return e;
663 static struct a_lex_cmd const *
664 a_lex__firstfit(char const *comm){ /* TODO *hashtable*! linear list search!!! */
665 struct a_lex_cmd const *cp;
666 NYD2_ENTER;
668 for(cp = a_lex_cmd_tab; PTRCMP(cp, <, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)]);
669 ++cp)
670 if(*comm == *cp->lc_name && is_prefix(comm, cp->lc_name))
671 goto jleave;
672 cp = NULL;
673 jleave:
674 NYD2_LEAVE;
675 return cp;
678 static void
679 a_lex_stop(int s){
680 sighandler_type old_action;
681 sigset_t nset;
682 NYD_X; /* Signal handler */
684 old_action = safe_signal(s, SIG_DFL);
686 sigemptyset(&nset);
687 sigaddset(&nset, s);
688 sigprocmask(SIG_UNBLOCK, &nset, NULL);
689 n_raise(s);
690 sigprocmask(SIG_BLOCK, &nset, NULL);
692 safe_signal(s, old_action);
694 if(a_lex_reset_on_stop){
695 a_lex_reset_on_stop = 0;
696 n_TERMCAP_RESUME(TRU1);
697 siglongjmp(srbuf, 0); /* FIXME get rid */
701 static void
702 a_lex_hangup(int s){
703 NYD_X; /* Signal handler */
704 UNUSED(s);
705 /* nothing to do? */
706 exit(EXIT_ERR);
709 static void
710 a_lex_onintr(int s){
711 NYD_X; /* Signal handler */
713 safe_signal(SIGINT, a_lex_onintr);
714 noreset = 0;
715 a_lex_unstack(TRUM1);
717 termios_state_reset();
718 close_all_files(); /* FIXME .. to outer level ONLU! */
720 if(image >= 0){
721 close(image);
722 image = -1;
724 if(interrupts != 1)
725 n_err_sighdl(_("Interrupt\n"));
726 safe_signal(SIGPIPE, a_lex_oldpipe);
727 siglongjmp(srbuf, 0); /* FIXME get rid */
730 static void
731 a_lex_unstack(bool_t eval_error){
732 struct a_lex_input_stack *lip;
733 NYD_ENTER;
735 if((lip = a_lex_input) == NULL){
736 /* If called from a_lex_onintr(), be silent FIXME */
737 pstate &= ~(PS_SOURCING | PS_ROBOT);
738 if(eval_error == TRUM1 || !(pstate & PS_STARTED))
739 goto jleave;
740 goto jerr;
743 if(lip->li_flags & a_LEX_MACRO){
744 if(lip->li_flags & a_LEX_MACRO_FREE_DATA){
745 char **lp;
747 while(*(lp = &lip->li_lines[lip->li_loff]) != NULL){
748 free(*lp);
749 ++lip->li_loff;
751 /* Part of lip's memory chunk, then */
752 if(!(lip->li_flags & a_LEX_MACRO_CMD))
753 free(lip->li_lines);
755 }else{
756 if(lip->li_flags & a_LEX_PIPE)
757 /* XXX command manager should -TERM then -KILL instead of hoping
758 * XXX for exit of provider due to EPIPE / SIGPIPE */
759 Pclose(lip->li_file, TRU1);
760 else
761 Fclose(lip->li_file);
764 if(!condstack_take(lip->li_cond)){
765 n_err(_("Unmatched `if' at end of %s \"%s\"\n"),
766 ((lip->li_flags & a_LEX_MACRO
767 ? (lip->li_flags & a_LEX_MACRO_CMD ? _("command") : _("macro"))
768 : _("`source'd file"))),
769 lip->li_name);
770 eval_error = TRU1;
773 if((a_lex_input = lip->li_outer) == NULL){
774 pstate &= ~(PS_SOURCING | PS_ROBOT);
775 }else{
776 if((a_lex_input->li_flags & (a_LEX_MACRO | a_LEX_SUPER_MACRO)) ==
777 (a_LEX_MACRO | a_LEX_SUPER_MACRO))
778 pstate &= ~PS_SOURCING;
779 assert(pstate & PS_ROBOT);
782 if(eval_error)
783 goto jerr;
784 if(lip->li_flags & a_LEX_FREE)
785 free(lip);
786 jleave:
787 if(UNLIKELY(a_lex_input != NULL && eval_error == TRUM1))
788 a_lex_unstack(TRUM1);
789 NYD_LEAVE;
790 return;
792 jerr:
793 if(lip != NULL){
794 if(options & OPT_D_V)
795 n_alert(_("Stopped %s \"%s\" due to errors%s"),
796 (pstate & PS_STARTED
797 ? (lip->li_flags & a_LEX_MACRO
798 ? (lip->li_flags & a_LEX_MACRO_CMD
799 ? _("evaluating command") : _("evaluating macro"))
800 : (lip->li_flags & a_LEX_PIPE
801 ? _("executing `source'd pipe")
802 : _("loading `source'd file")))
803 : (lip->li_flags & a_LEX_MACRO
804 ? (lip->li_flags & a_LEX_MACRO_X_OPTION
805 ? _("evaluating command line") : _("evaluating macro"))
806 : _("loading initialization resource"))),
807 lip->li_name,
808 (options & OPT_DEBUG ? "" : _(" (enable *debug* for trace)")));
809 if(lip->li_flags & a_LEX_FREE)
810 free(lip);
813 if(!(options & OPT_INTERACTIVE) && !(pstate & PS_STARTED)){
814 if(options & OPT_D_V)
815 n_alert(_("Non-interactive, bailing out due to errors "
816 "in startup load phase"));
817 exit(EXIT_ERR);
819 goto jleave;
822 static bool_t
823 a_lex_source_file(char const *file, bool_t silent_error){
824 struct a_lex_input_stack *lip;
825 size_t nlen;
826 char *nbuf;
827 bool_t ispipe;
828 FILE *fip;
829 NYD_ENTER;
831 fip = NULL;
833 /* Being a command argument file is space-trimmed */
834 if((ispipe = (!silent_error && (nlen = strlen(file)) > 0 &&
835 file[--nlen] == '|'))){
836 char const *sh;
838 if((sh = ok_vlook(SHELL)) == NULL)
839 sh = XSHELL;
840 if((fip = Popen(nbuf = savestrbuf(file, nlen), "r",
841 sh, NULL, COMMAND_FD_NULL)) == NULL){
842 if(!silent_error || (options & OPT_D_V))
843 n_perr(nbuf, 0);
844 goto jleave;
846 }else if((nbuf = fexpand(file, FEXP_LOCAL)) == NULL)
847 goto jleave;
848 else if((fip = Fopen(nbuf, "r")) == NULL){
849 if(!silent_error || (options & OPT_D_V))
850 n_perr(nbuf, 0);
851 goto jleave;
854 lip = smalloc(sizeof(*lip) -
855 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
856 (nlen = strlen(nbuf) +1));
857 lip->li_outer = a_lex_input;
858 lip->li_file = fip;
859 lip->li_cond = condstack_release();
860 lip->li_flags = (ispipe ? a_LEX_PIPE : a_LEX_NONE) |
861 (a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
862 ? a_LEX_SUPER_MACRO : 0);
863 memcpy(lip->li_name, nbuf, nlen);
865 pstate |= PS_SOURCING | PS_ROBOT;
866 a_lex_input = lip;
867 a_commands_recursive();
868 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
869 jleave:
870 NYD_LEAVE;
871 return silent_error ? TRU1 : (fip != NULL);
874 static bool_t
875 a_lex_load(struct a_lex_input_stack *lip){
876 bool_t rv;
877 NYD2_ENTER;
879 assert(!(pstate & PS_STARTED));
880 assert(a_lex_input == NULL);
882 /* POSIX:
883 * Any errors in the start-up file shall either cause mailx to terminate
884 * with a diagnostic message and a non-zero status or to continue after
885 * writing a diagnostic message, ignoring the remainder of the lines in
886 * the start-up file. */
887 lip->li_cond = condstack_release();
889 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
890 pstate |= PS_ROBOT |
891 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
893 pstate |= PS_ROBOT | PS_SOURCING;
894 if(options & OPT_D_V)
895 n_err(_("Loading \"%s\"\n"), lip->li_name);
896 a_lex_input = lip;
897 if(!(rv = n_commands())){
898 if(!(options & OPT_INTERACTIVE)){
899 if(options & OPT_D_V)
900 n_alert(_("Non-interactive program mode, forced exit"));
901 exit(EXIT_ERR);
904 /* PS_EXIT handled by callers */
905 NYD2_LEAVE;
906 return rv;
909 static bool_t
910 a_commands_recursive(void){
911 struct a_lex_eval_ctx ev;
912 bool_t rv;
913 NYD2_ENTER;
915 memset(&ev, 0, sizeof ev);
917 /* FIXME sigkondom */
918 n_COLOUR( n_colour_env_push(); )
919 rv = TRU1;
920 for(;;){
921 int n;
923 /* Read a line of commands and handle end of file specially */
924 ev.le_line.l = ev.le_line_size;
925 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
926 ev.le_new_content);
927 ev.le_line_size = (ui32_t)ev.le_line.l;
928 ev.le_line.l = (ui32_t)n;
930 if(n < 0)
931 break;
933 if(a_lex_evaluate(&ev)){
934 rv = FAL0;
935 break;
938 if((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)){
939 if(exit_status != EXIT_OK)
940 break;
943 a_lex_unstack(!rv);
944 n_COLOUR( n_colour_env_pop(FAL0); )
946 if(ev.le_line.s != NULL)
947 free(ev.le_line.s);
948 NYD2_LEAVE;
949 return rv;
952 #ifdef HAVE_DOCSTRINGS
953 FL bool_t
954 n_print_comm_docstr(char const *comm){
955 struct a_lex_ghost const *gp;
956 struct a_lex_cmd const *cp, *cpmax;
957 bool_t rv = FAL0;
958 NYD_ENTER;
960 /* Ghosts take precedence */
961 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
962 if(!strcmp(comm, gp->lg_name)){
963 printf("%s -> ", comm);
964 comm = gp->lg_cmd.s;
965 break;
968 cpmax = &(cp = a_lex_cmd_tab)[NELEM(a_lex_cmd_tab)];
969 jredo:
970 for(; PTRCMP(cp, <, cpmax); ++cp){
971 if(!strcmp(comm, cp->lc_name))
972 printf("%s: %s\n", comm, V_(cp->lc_doc));
973 else if(is_prefix(comm, cp->lc_name))
974 printf("%s (%s): %s\n", comm, cp->lc_name, V_(cp->lc_doc));
975 else
976 continue;
977 rv = TRU1;
978 break;
980 if(!rv && PTRCMP(cpmax, ==, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)])){
981 cpmax = &(cp = a_lex_special_cmd_tab)[NELEM(a_lex_special_cmd_tab)];
982 goto jredo;
985 if(!rv && gp != NULL){
986 printf("\"%s\"\n", comm);
987 rv = TRU1;
989 NYD_LEAVE;
990 return rv;
992 #endif /* HAVE_DOCSTRINGS */
994 FL bool_t
995 n_commands(void){ /* FIXME */
996 struct a_lex_eval_ctx ev;
997 int n;
998 bool_t volatile rv = TRU1;
999 NYD_ENTER;
1001 if (!(pstate & PS_SOURCING)) {
1002 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
1003 safe_signal(SIGINT, &a_lex_onintr);
1004 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1005 safe_signal(SIGHUP, &a_lex_hangup);
1006 /* TODO We do a lot of redundant signal handling, especially
1007 * TODO with the command line editor(s); try to merge this */
1008 safe_signal(SIGTSTP, &a_lex_stop);
1009 safe_signal(SIGTTOU, &a_lex_stop);
1010 safe_signal(SIGTTIN, &a_lex_stop);
1012 a_lex_oldpipe = safe_signal(SIGPIPE, SIG_IGN);
1013 safe_signal(SIGPIPE, a_lex_oldpipe);
1015 memset(&ev, 0, sizeof ev);
1017 (void)sigsetjmp(srbuf, 1); /* FIXME get rid */
1018 for (;;) {
1019 char *temporary_orig_line; /* XXX eval_ctx.le_line not yet constant */
1021 n_COLOUR( n_colour_env_pop(TRU1); )
1023 /* TODO Unless we have our signal manager (or however we do it) child
1024 * TODO processes may have time slots where their execution isn't
1025 * TODO protected by signal handlers (in between start and setup
1026 * TODO completed). close_all_files() is only called from onintr()
1027 * TODO so those may linger possibly forever */
1028 if(!(pstate & PS_SOURCING))
1029 close_all_files();
1031 interrupts = 0;
1033 temporary_localopts_free(); /* XXX intermediate hack */
1034 sreset((pstate & PS_SOURCING) != 0);
1035 if (!(pstate & PS_SOURCING)) {
1036 char *cp;
1038 /* TODO Note: this buffer may contain a password. We should redefine
1039 * TODO the code flow which has to do that */
1040 if ((cp = termios_state.ts_linebuf) != NULL) {
1041 termios_state.ts_linebuf = NULL;
1042 termios_state.ts_linesize = 0;
1043 free(cp); /* TODO pool give-back */
1045 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1046 if (ev.le_line.l > LINESIZE * 3) {
1047 free(ev.le_line.s); /* TODO pool! but what? */
1048 ev.le_line.s = NULL;
1049 ev.le_line.l = ev.le_line_size = 0;
1053 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1054 char *cp;
1056 cp = ok_vlook(newmail);
1057 if ((options & OPT_TTYIN) && cp != NULL) {
1058 struct stat st;
1060 /* FIXME TEST WITH NOPOLL ETC. !!! */
1061 n = (cp != NULL && strcmp(cp, "nopoll"));
1062 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
1063 st.st_size > mailsize) ||
1064 (mb.mb_type == MB_MAILDIR && n != 0)) {
1065 size_t odot = PTR2SIZE(dot - message);
1066 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
1068 if (setfile(mailname,
1069 FEDIT_NEWMAIL |
1070 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
1071 exit_status |= EXIT_ERR;
1072 rv = FAL0;
1073 break;
1075 dot = message + odot;
1076 pstate |= odid;
1080 a_lex_reset_on_stop = TRU1;
1081 exit_status = EXIT_OK;
1084 /* Read a line of commands and handle end of file specially */
1085 jreadline:
1086 ev.le_line.l = ev.le_line_size;
1087 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
1088 ev.le_new_content);
1089 ev.le_line_size = (ui32_t)ev.le_line.l;
1090 ev.le_line.l = (ui32_t)n;
1091 a_lex_reset_on_stop = FAL0;
1093 if (n < 0) {
1094 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1095 if (!(pstate & PS_ROBOT) &&
1096 (options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
1097 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1098 n_msleep(500, FAL0);
1099 continue;
1101 break;
1104 temporary_orig_line = ((pstate & PS_SOURCING) ||
1105 !(options & OPT_INTERACTIVE)) ? NULL
1106 : savestrbuf(ev.le_line.s, ev.le_line.l);
1107 pstate &= ~PS_HOOK_MASK;
1108 if (a_lex_evaluate(&ev)) {
1109 if (!(pstate & PS_STARTED)) /* TODO mess; join PS_EVAL_ERROR.. */
1110 rv = FAL0;
1111 break;
1114 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
1115 if (exit_status != EXIT_OK)
1116 break;
1117 if ((pstate & (PS_SOURCING | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
1118 exit_status = EXIT_ERR;
1119 break;
1122 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1123 if (ev.le_new_content != NULL)
1124 goto jreadline;
1125 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1126 if (temporary_orig_line != NULL)
1127 n_tty_addhist(temporary_orig_line, !ev.le_add_history);
1130 if(pstate & PS_EXIT)
1131 break;
1133 a_lex_unstack(!rv);
1135 if (ev.le_line.s != NULL)
1136 free(ev.le_line.s);
1137 if (pstate & PS_SOURCING)
1138 sreset(FAL0);
1139 NYD_LEAVE;
1140 return rv;
1143 FL int
1144 (n_lex_input)(char const *prompt, bool_t nl_escape, char **linebuf,
1145 size_t *linesize, char const *string SMALLOC_DEBUG_ARGS){
1146 /* TODO readline: linebuf pool! */
1147 bool_t doprompt, dotty;
1148 int n, nold;
1149 FILE *ifile;
1150 NYD2_ENTER;
1152 /* Special case macro mode: never need to prompt, lines have always been
1153 * unfolded already */
1154 if(a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO)){
1155 char **lp = &a_lex_input->li_lines[a_lex_input->li_loff];
1157 if(*linebuf != NULL)
1158 free(*linebuf);
1160 if((*linebuf = *lp) == NULL){
1161 *linesize = 0;
1162 n = -1;
1163 }else{
1164 ++a_lex_input->li_loff;
1165 n = (int)(*linesize = strlen(*linebuf));
1167 if(!(a_lex_input->li_flags & a_LEX_MACRO_FREE_DATA))
1168 *linebuf = sbufdup(*linebuf, *linesize);
1170 if(options & OPT_D_VV)
1171 n_err(_("%s %d bytes <%.*s>\n"),
1172 (a_lex_input->li_flags & a_LEX_MACRO_X_OPTION
1173 ? _("-X option") : _("MACRO")),
1174 n, n, *linebuf);
1176 goto jleave;
1179 doprompt = ((pstate & (PS_STARTED | PS_ROBOT)) == PS_STARTED &&
1180 (options & OPT_INTERACTIVE));
1181 dotty = (doprompt && !ok_blook(line_editor_disable));
1182 if(!doprompt)
1183 prompt = NULL;
1184 else if(prompt == NULL)
1185 prompt = getprompt();
1187 /* Ensure stdout is flushed first anyway */
1188 if(!dotty && prompt == NULL)
1189 fflush(stdout);
1191 ifile = (a_lex_input != NULL) ? a_lex_input->li_file : stdin;
1192 assert(ifile != NULL);
1194 for(nold = n = 0;;){
1195 if(dotty){
1196 assert(ifile == stdin);
1197 if(string != NULL && (n = (int)strlen(string)) > 0){
1198 if(*linesize > 0)
1199 *linesize += n +1;
1200 else
1201 *linesize = (size_t)n + LINESIZE +1;
1202 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
1203 memcpy(*linebuf, string, (size_t)n +1);
1205 string = NULL;
1206 /* TODO if nold>0, don't redisplay the entire line!
1207 * TODO needs complete redesign ... */
1208 n = (n_tty_readline)(prompt, linebuf, linesize, n
1209 SMALLOC_DEBUG_ARGSCALL);
1210 }else{
1211 if(prompt != NULL) {
1212 if(*prompt != '\0')
1213 fputs(prompt, stdout);
1214 fflush(stdout);
1217 n = (readline_restart)(ifile, linebuf, linesize, n
1218 SMALLOC_DEBUG_ARGSCALL);
1220 if(n > 0 && nold > 0){
1221 int i = 0;
1222 char const *cp = *linebuf + nold;
1224 while(blankspacechar(*cp) && nold + i < n)
1225 ++cp, ++i;
1226 if(i > 0){
1227 memmove(*linebuf + nold, cp, n - nold - i);
1228 n -= i;
1229 (*linebuf)[n] = '\0';
1234 if(n <= 0)
1235 break;
1237 /* POSIX says:
1238 * An unquoted <backslash> at the end of a command line shall
1239 * be discarded and the next line shall continue the command */
1240 if(!nl_escape || (*linebuf)[n - 1] != '\\')
1241 break;
1242 /* Definitely outside of quotes, thus the quoting rules are so that an
1243 * uneven number of successive backslashs at EOL is a continuation */
1244 if(n > 1){
1245 size_t i, j;
1247 for(j = 1, i = (size_t)n - 1; i-- > 0; ++j)
1248 if((*linebuf)[i] != '\\')
1249 break;
1250 if(!(j & 1))
1251 break;
1253 (*linebuf)[nold = --n] = '\0';
1254 if(prompt != NULL && *prompt != '\0')
1255 prompt = ".. "; /* XXX PS2 .. */
1258 if(n >= 0){
1259 (*linebuf)[*linesize = n] = '\0';
1261 if(options & OPT_D_VV)
1262 n_err(_("%s %d bytes <%.*s>\n"),
1263 (!(pstate & PS_STARTED) ? "LOAD"
1264 : (pstate & PS_SOURCING ? "SOURCE" : "READ")),
1265 n, n, *linebuf);
1267 jleave:
1268 NYD2_LEAVE;
1269 return n;
1272 FL char *
1273 n_lex_input_cp_addhist(char const *prompt, char const *string, bool_t isgabby){
1274 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1275 size_t linesize;
1276 char *linebuf, *rv;
1277 int n;
1278 NYD2_ENTER;
1280 linesize = 0;
1281 linebuf = NULL;
1282 rv = NULL;
1284 n = n_lex_input(prompt, TRU1, &linebuf, &linesize, string);
1285 if(n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
1286 (options & OPT_INTERACTIVE))
1287 n_tty_addhist(rv, isgabby);
1289 if(linebuf != NULL)
1290 free(linebuf);
1291 NYD2_LEAVE;
1292 return rv;
1295 FL void
1296 n_load(char const *name){
1297 struct a_lex_input_stack *lip;
1298 size_t i;
1299 FILE *fip;
1300 NYD_ENTER;
1302 if(name == NULL || *name == '\0' || (fip = Fopen(name, "r")) == NULL)
1303 goto jleave;
1305 i = strlen(name) +1;
1306 lip = scalloc(1, sizeof(*lip) -
1307 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) + i);
1308 lip->li_file = fip;
1309 lip->li_flags = a_LEX_FREE;
1310 memcpy(lip->li_name, name, i);
1312 a_lex_load(lip);
1313 pstate &= ~PS_EXIT;
1314 jleave:
1315 NYD_LEAVE;
1318 FL void
1319 n_load_Xargs(char const **lines){
1320 static char const name[] = "-X";
1322 ui8_t buf[sizeof(struct a_lex_input_stack) + sizeof name];
1323 struct a_lex_input_stack *lip;
1324 NYD_ENTER;
1326 memset(buf, 0, sizeof buf);
1327 lip = (void*)buf;
1328 lip->li_flags = a_LEX_MACRO | a_LEX_MACRO_X_OPTION | a_LEX_SUPER_MACRO;
1329 lip->li_lines = UNCONST(lines);
1330 memcpy(lip->li_name, name, sizeof name);
1332 a_lex_load(lip);
1333 if(pstate & PS_EXIT)
1334 exit(EXIT_OK);
1335 NYD_LEAVE;
1338 FL int
1339 c_source(void *v){
1340 int rv;
1341 NYD_ENTER;
1343 rv = (a_lex_source_file(*(char**)v, FAL0) == TRU1) ? 0 : 1;
1344 NYD_LEAVE;
1345 return rv;
1348 FL int
1349 c_source_if(void *v){ /* XXX obsolete?, support file tests in `if' etc.! */
1350 int rv;
1351 NYD_ENTER;
1353 rv = (a_lex_source_file(*(char**)v, TRU1) != FAL0) ? 0 : 1;
1354 NYD_LEAVE;
1355 return rv;
1358 FL bool_t
1359 n_source_macro(char const *name, char **lines){
1360 struct a_lex_input_stack *lip;
1361 size_t i;
1362 int rv;
1363 NYD_ENTER;
1365 lip = smalloc(sizeof(*lip) -
1366 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1367 (i = strlen(name) +1));
1368 lip->li_outer = a_lex_input;
1369 lip->li_file = NULL;
1370 lip->li_cond = condstack_release();
1371 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1372 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1373 ? a_LEX_SUPER_MACRO : 0);
1374 lip->li_loff = 0;
1375 lip->li_lines = lines;
1376 memcpy(lip->li_name, name, i);
1378 pstate |= PS_ROBOT;
1379 a_lex_input = lip;
1380 rv = a_commands_recursive();
1381 NYD_LEAVE;
1382 return rv;
1385 FL bool_t
1386 n_source_command(char const *cmd){
1387 struct a_lex_input_stack *lip;
1388 size_t i, ial;
1389 bool_t rv;
1390 NYD_ENTER;
1392 i = strlen(cmd);
1393 cmd = sbufdup(cmd, i++);
1394 ial = n_ALIGN(i);
1396 lip = smalloc(sizeof(*lip) -
1397 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1398 ial + 2*sizeof(char*));
1399 lip->li_outer = a_lex_input;
1400 lip->li_file = NULL;
1401 lip->li_cond = condstack_release();
1402 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1403 a_LEX_MACRO_CMD |
1404 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1405 ? a_LEX_SUPER_MACRO : 0);
1406 lip->li_loff = 0;
1407 lip->li_lines = (void*)(lip->li_name + ial);
1408 lip->li_lines[0] = UNCONST(cmd); /* dup'ed above */
1409 lip->li_lines[1] = NULL;
1410 memcpy(lip->li_name, cmd, i);
1412 pstate |= PS_ROBOT;
1413 a_lex_input = lip;
1414 rv = a_commands_recursive();
1415 NYD_LEAVE;
1416 return rv;
1419 FL bool_t
1420 n_source_may_yield_control(void){
1421 return ((options & OPT_INTERACTIVE) &&
1422 (pstate & PS_STARTED) &&
1423 (!(pstate & PS_ROBOT) || (pstate & PS_RECURSED)) && /* Ok for ~: */
1424 (a_lex_input == NULL || a_lex_input->li_outer == NULL));
1427 /* s-it-mode */