enum okeys: new flags, additions and removals
[s-mailx.git] / lex_input.c
blob571b1e6e72260e6e5112111b2c547c97b0101948
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 /* PS_STATE_PENDMASK requires some actions */
125 static void a_lex_update_pstate(void);
127 /* Evaluate a single command.
128 * .le_add_history and .le_new_content will be updated upon success.
129 * Command functions return 0 for success, 1 for error, and -1 for abort.
130 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
131 static int a_lex_evaluate(struct a_lex_eval_ctx *evp);
133 /* Get first-fit, or NULL */
134 static struct a_lex_cmd const *a_lex__firstfit(char const *comm);
136 /* When we wake up after ^Z, reprint the prompt */
137 static void a_lex_stop(int s);
139 /* Branch here on hangup signal and simulate "exit" */
140 static void a_lex_hangup(int s);
142 /* The following gets called on receipt of an interrupt. Close all open files
143 * except 0, 1, 2, and the temporary. Also, unstack all source files */
144 static void a_lex_onintr(int s);
146 /* Pop the current input back to the previous level. Update the program state.
147 * If the argument is TRUM1 then we don't alert and error out if the stack
148 * doesn't exist at all */
149 static void a_lex_unstack(bool_t eval_error);
151 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
152 static bool_t a_lex_source_file(char const *file, bool_t silent_error);
154 /* System resource file load()ing or -X command line option array traversal */
155 static bool_t a_lex_load(struct a_lex_input_stack *lip);
157 /* A simplified command loop for recursed state machines */
158 static bool_t a_commands_recursive(void);
160 /* List of all commands, and list of commands which are specially treated
161 * and deduced in _evaluate(), but we need a list for _c_list() and
162 * print_comm_docstr() */
163 #ifdef HAVE_DOCSTRINGS
164 # define DS(S) , S
165 #else
166 # define DS(S)
167 #endif
168 static struct a_lex_cmd const a_lex_cmd_tab[] = {
169 #include "cmd_tab.h"
171 a_lex_special_cmd_tab[] = {
172 { "#", NULL, 0, 0, 0
173 DS(N_("\"Comment command\": ignore remaining (continuable) line")) },
174 { "-", NULL, 0, 0, 0
175 DS(N_("Print out the preceding message")) }
177 #undef DS
179 static char *
180 a_lex_isolate(char const *comm){
181 NYD2_ENTER;
182 while(*comm != '\0' &&
183 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
184 ++comm;
185 NYD2_LEAVE;
186 return UNCONST(comm);
189 static int
190 a_lex_c_ghost(void *v){
191 struct a_lex_ghost *lgp, *gp;
192 size_t i, cl, nl;
193 char *cp;
194 char const **argv;
195 NYD_ENTER;
197 argv = v;
199 /* Show the list? */
200 if(*argv == NULL){
201 FILE *fp;
203 if((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
204 fp = stdout;
206 for(i = 0, gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
207 fprintf(fp, "ghost %s \"%s\"\n",
208 gp->lg_name, string_quote(gp->lg_cmd.s));
210 if(fp != stdout){
211 page_or_print(fp, i);
212 Fclose(fp);
214 goto jleave;
217 /* Verify the ghost name is a valid one */
218 if(*argv[0] == '\0' || *a_lex_isolate(argv[0]) != '\0'){
219 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
220 v = NULL;
221 goto jleave;
224 /* Show command of single ghost? */
225 if(argv[1] == NULL){
226 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
227 if(!strcmp(argv[0], gp->lg_name)){
228 printf("ghost %s \"%s\"\n",
229 gp->lg_name, string_quote(gp->lg_cmd.s));
230 goto jleave;
232 n_err(_("`ghost': no such alias: \"%s\"\n"), argv[0]);
233 v = NULL;
234 goto jleave;
237 /* Define command for ghost: verify command content */
238 for(cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
239 if(*cp != '\0')
240 cl += strlen(cp) +1; /* SP or NUL */
241 if(cl == 0){
242 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv[0]);
243 v = NULL;
244 goto jleave;
247 /* If the ghost already exists, recreate */
248 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL; lgp = gp, gp = gp->lg_next)
249 if(!strcmp(gp->lg_name, argv[0])){
250 if(lgp != NULL)
251 lgp->lg_next = gp->lg_next;
252 else
253 a_lex_ghosts = gp->lg_next;
254 free(gp);
255 break;
258 nl = strlen(argv[0]) +1;
259 gp = smalloc(sizeof(*gp) - VFIELD_SIZEOF(struct a_lex_ghost, lg_name) +
260 nl + cl);
261 gp->lg_next = a_lex_ghosts;
262 a_lex_ghosts = gp;
263 memcpy(gp->lg_name, argv[0], nl);
264 cp = gp->lg_cmd.s = gp->lg_name + nl;
265 gp->lg_cmd.l = --cl;
267 while(*++argv != NULL)
268 if((i = strlen(*argv)) > 0){
269 memcpy(cp, *argv, i);
270 cp += i;
271 *cp++ = ' ';
273 *--cp = '\0';
274 jleave:
275 NYD_LEAVE;
276 return v == NULL;
279 static int
280 a_lex_c_unghost(void *v){
281 struct a_lex_ghost *lgp, *gp;
282 char const **argv, *cp;
283 int rv;
284 NYD_ENTER;
286 rv = 0;
287 argv = v;
289 while((cp = *argv++) != NULL){
290 if(cp[0] == '*' && cp[1] == '\0'){
291 while((gp = a_lex_ghosts) != NULL){
292 a_lex_ghosts = gp->lg_next;
293 free(gp);
295 }else{
296 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL;
297 lgp = gp, gp = gp->lg_next)
298 if(!strcmp(gp->lg_name, cp)){
299 if(lgp != NULL)
300 lgp->lg_next = gp->lg_next;
301 else
302 a_lex_ghosts = gp->lg_next;
303 free(gp);
304 goto jouter;
306 n_err(_("`unghost': no such alias: \"%s\"\n"), cp);
307 rv = 1;
308 jouter: ;
311 NYD_LEAVE;
312 return rv;
315 static int
316 a_lex_c_list(void *v){
317 struct a_lex_cmd const **cpa, *cp, **cursor;
318 size_t i;
319 NYD_ENTER;
321 UNUSED(v);
323 i = NELEM(a_lex_cmd_tab) + NELEM(a_lex_special_cmd_tab) + 1;
324 cpa = ac_alloc(sizeof(cp) * i);
326 for(i = 0; i < NELEM(a_lex_cmd_tab); ++i)
327 cpa[i] = &a_lex_cmd_tab[i];
328 /* C99 */{
329 size_t j;
331 for(j = 0; j < NELEM(a_lex_special_cmd_tab); ++i, ++j)
332 cpa[i] = &a_lex_special_cmd_tab[j];
334 cpa[i] = NULL;
336 qsort(cpa, i, sizeof(cp), &a_lex__pcmd_cmp);
338 printf(_("Commands are:\n"));
339 for(i = 0, cursor = cpa; (cp = *cursor++) != NULL;){
340 size_t j;
342 if(cp->lc_func == &c_cmdnotsupp)
343 continue;
344 j = strlen(cp->lc_name) + 2;
345 if((i += j) > 72){
346 i = j;
347 printf("\n");
349 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->lc_name);
352 ac_free(cpa);
353 NYD_LEAVE;
354 return 0;
357 static int
358 a_lex__pcmd_cmp(void const *s1, void const *s2){
359 struct a_lex_cmd const * const *cp1, * const *cp2;
360 int rv;
361 NYD2_ENTER;
363 cp1 = s1;
364 cp2 = s2;
365 rv = strcmp((*cp1)->lc_name, (*cp2)->lc_name);
366 NYD2_LEAVE;
367 return rv;
370 static int
371 a_lex_c_quit(void *v){
372 NYD_ENTER;
373 UNUSED(v);
375 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
376 * Otherwise return -1 to abort command loop */
377 pstate |= PS_EXIT;
378 NYD_LEAVE;
379 return 0;
382 static int
383 a_lex_c_features(void *v){
384 NYD_ENTER;
386 UNUSED(v);
388 printf(_("Features: %s\n"), ok_vlook(features));
389 NYD_LEAVE;
390 return 0;
393 static int
394 a_lex_c_version(void *v){
395 NYD_ENTER;
397 UNUSED(v);
399 printf(_("Version %s\n"), ok_vlook(version));
400 NYD_LEAVE;
401 return 0;
404 static void
405 a_lex_update_pstate(void){
406 NYD_ENTER;
408 if(pstate & PS_SIGWINCH_PEND){
409 char buf[32];
411 snprintf(buf, sizeof buf, "%d", scrnwidth);
412 ok_vset(COLUMNS, buf);
413 snprintf(buf, sizeof buf, "%d", scrnheight);
414 ok_vset(LINES, buf);
417 pstate &= ~PS_PSTATE_PENDMASK;
418 NYD_LEAVE;
421 static int
422 a_lex_evaluate(struct a_lex_eval_ctx *evp){
423 /* xxx old style(9), but also old code */
424 struct str line;
425 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
426 struct a_lex_ghost *gp;
427 struct a_lex_cmd const *cmd;
428 int c, e;
429 NYD_ENTER;
431 e = 1;
432 cmd = NULL;
433 gp = NULL;
434 line = evp->le_line; /* XXX don't change original (buffer pointer) */
435 assert(line.s[line.l] == '\0');
436 evp->le_add_history = FAL0;
437 evp->le_new_content = NULL;
439 /* Command ghosts that refer to shell commands or macro expansion restart */
440 jrestart:
442 /* Strip the white space away from end and beginning of command */
443 if(line.l > 0){
444 size_t i = line.l;
446 for(cp = &line.s[i -1]; spacechar(*cp); --cp)
447 --i;
448 line.l = i;
450 for(cp = line.s; spacechar(*cp); ++cp)
452 line.l -= PTR2SIZE(cp - line.s);
454 /* Ignore null commands (comments) */
455 if(*cp == '#')
456 goto jleave0;
458 /* Handle ! differently to get the correct lexical conventions */
459 arglist[0] = cp;
460 if(*cp == '!')
461 ++cp;
462 /* Isolate the actual command; since it may not necessarily be
463 * separated from the arguments (as in `p1') we need to duplicate it to
464 * be able to create a NUL terminated version.
465 * We must be aware of several special one letter commands here */
466 else if((cp = a_lex_isolate(cp)) == arglist[0] &&
467 (*cp == '|' || *cp == '~' || *cp == '?'))
468 ++cp;
469 c = (int)PTR2SIZE(cp - arglist[0]);
470 line.l -= c;
471 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
472 memcpy(word, arglist[0], c);
473 word[c] = '\0';
475 /* Look up the command; if not found, bitch.
476 * Normally, a blank command would map to the first command in the
477 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
478 * confusion; act just the same for ghosts */
479 if(*word == '\0'){
480 if((pstate & PS_ROBOT) || gp != NULL)
481 goto jleave0;
482 cmd = a_lex_cmd_tab + 0;
483 goto jexec;
486 /* If this is the first evaluation, check command ghosts */
487 if(gp == NULL){
488 /* TODO relink list head, so it's sorted on usage over time?
489 * TODO in fact, there should be one hashmap over all commands and ghosts
490 * TODO so that the lookup could be made much more efficient than it is
491 * TODO now (two adjacent list searches! */
492 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
493 if(!strcmp(word, gp->lg_name)){
494 if(line.l > 0){
495 size_t i;
497 i = gp->lg_cmd.l;
498 line.s = salloc(i + line.l +1);
499 memcpy(line.s, gp->lg_cmd.s, i);
500 memcpy(line.s + i, cp, line.l);
501 line.s[i += line.l] = '\0';
502 line.l = i;
503 }else{
504 line.s = gp->lg_cmd.s;
505 line.l = gp->lg_cmd.l;
507 goto jrestart;
511 if((cmd = a_lex__firstfit(word)) == NULL || cmd->lc_func == &c_cmdnotsupp){
512 bool_t s;
514 if(!(s = condstack_isskip()) || (options & OPT_D_V))
515 n_err(_("Unknown command%s: `%s'\n"),
516 (s ? _(" (ignored due to `if' condition)") : ""), word);
517 if(s)
518 goto jleave0;
519 if(cmd != NULL){
520 c_cmdnotsupp(NULL);
521 cmd = NULL;
523 goto jleave;
526 /* See if we should execute the command -- if a conditional we always
527 * execute it, otherwise, check the state of cond */
528 jexec:
529 if(!(cmd->lc_argtype & ARG_F) && condstack_isskip())
530 goto jleave0;
532 /* Process the arguments to the command, depending on the type it expects */
533 if(!(cmd->lc_argtype & ARG_M) && (options & OPT_SENDMODE)){
534 n_err(_("May not execute `%s' while sending\n"), cmd->lc_name);
535 goto jleave;
537 if((cmd->lc_argtype & ARG_S) && !(pstate & PS_STARTED)){
538 n_err(_("May not execute `%s' during startup\n"), cmd->lc_name);
539 goto jleave;
541 if((cmd->lc_argtype & ARG_I) &&
542 !(options & (OPT_INTERACTIVE | OPT_BATCH_FLAG))){
543 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
544 cmd->lc_name);
545 goto jleave;
547 if((cmd->lc_argtype & ARG_R) && (pstate & PS_RECURSED)){
548 n_err(_("Cannot invoke `%s' when in recursed mode (e.g., composing)\n"),
549 cmd->lc_name);
550 goto jleave;
553 if((cmd->lc_argtype & ARG_W) && !(mb.mb_perm & MB_DELE)){
554 n_err(_("May not execute `%s' -- message file is read only\n"),
555 cmd->lc_name);
556 goto jleave;
558 if((cmd->lc_argtype & ARG_A) && mb.mb_type == MB_VOID){
559 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd->lc_name);
560 goto jleave;
563 if(cmd->lc_argtype & ARG_O)
564 OBSOLETE2(_("this command will be removed"), cmd->lc_name);
565 if(cmd->lc_argtype & ARG_V)
566 temporary_arg_v_store = NULL;
568 switch(cmd->lc_argtype & ARG_ARGMASK){
569 case ARG_MSGLIST:
570 /* Message list defaulting to nearest forward legal message */
571 if(n_msgvec == NULL)
572 goto je96;
573 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
574 break;
575 if(c == 0){
576 if((n_msgvec[0] = first(cmd->lc_msgflag, cmd->lc_msgmask)) != 0)
577 n_msgvec[1] = 0;
579 if(n_msgvec[0] == 0){
580 if(!(pstate & PS_HOOK_MASK))
581 printf(_("No applicable messages\n"));
582 break;
584 e = (*cmd->lc_func)(n_msgvec);
585 break;
587 case ARG_NDMLIST:
588 /* Message list with no defaults, but no error if none exist */
589 if(n_msgvec == NULL){
590 je96:
591 n_err(_("Invalid use of \"message list\"\n"));
592 break;
594 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
595 break;
596 e = (*cmd->lc_func)(n_msgvec);
597 break;
599 case ARG_STRLIST:
600 /* Just the straight string, with leading blanks removed */
601 while(whitechar(*cp))
602 ++cp;
603 e = (*cmd->lc_func)(cp);
604 break;
606 case ARG_RAWLIST:
607 case ARG_ECHOLIST:
608 /* A vector of strings, in shell style */
609 if((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
610 ((cmd->lc_argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
611 break;
612 if(c < cmd->lc_minargs){
613 n_err(_("`%s' requires at least %d arg(s)\n"),
614 cmd->lc_name, cmd->lc_minargs);
615 break;
617 #undef lc_minargs
618 if(c > cmd->lc_maxargs){
619 n_err(_("`%s' takes no more than %d arg(s)\n"),
620 cmd->lc_name, cmd->lc_maxargs);
621 break;
623 #undef lc_maxargs
624 e = (*cmd->lc_func)(arglist);
625 break;
627 case ARG_NOLIST:
628 /* Just the constant zero, for exiting, eg. */
629 e = (*cmd->lc_func)(0);
630 break;
632 default:
633 DBG( n_panic(_("Implementation error: unknown argument type")); )
634 goto jleave0;
637 if(e == 0 && (cmd->lc_argtype & ARG_V) &&
638 (cp = temporary_arg_v_store) != NULL){
639 temporary_arg_v_store = NULL;
640 evp->le_new_content = cp;
641 goto jleave0;
643 if(!(cmd->lc_argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
644 evp->le_add_history = TRU1;
646 jleave:
647 /* Exit the current source file on error TODO what a mess! */
648 if(e == 0)
649 pstate &= ~PS_EVAL_ERROR;
650 else{
651 pstate |= PS_EVAL_ERROR;
652 if(e < 0 || (pstate & PS_ROBOT)){ /* FIXME */
653 e = 1;
654 goto jret;
656 goto jret0;
659 if(cmd == NULL)
660 goto jret0;
661 if((cmd->lc_argtype & ARG_P) && ok_blook(autoprint))
662 if(visible(dot)){
663 line.s = savestr("type");
664 line.l = sizeof("type") -1;
665 gp = (struct a_lex_ghost*)-1; /* Avoid `ghost' interpretation */
666 goto jrestart;
669 if(!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(cmd->lc_argtype & ARG_T))
670 pstate |= PS_SAW_COMMAND;
671 jleave0:
672 pstate &= ~PS_EVAL_ERROR;
673 jret0:
674 e = 0;
675 jret:
677 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
679 NYD_LEAVE;
680 return e;
683 static struct a_lex_cmd const *
684 a_lex__firstfit(char const *comm){ /* TODO *hashtable*! linear list search!!! */
685 struct a_lex_cmd const *cp;
686 NYD2_ENTER;
688 for(cp = a_lex_cmd_tab; PTRCMP(cp, <, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)]);
689 ++cp)
690 if(*comm == *cp->lc_name && is_prefix(comm, cp->lc_name))
691 goto jleave;
692 cp = NULL;
693 jleave:
694 NYD2_LEAVE;
695 return cp;
698 static void
699 a_lex_stop(int s){
700 sighandler_type old_action;
701 sigset_t nset;
702 NYD_X; /* Signal handler */
704 old_action = safe_signal(s, SIG_DFL);
706 sigemptyset(&nset);
707 sigaddset(&nset, s);
708 sigprocmask(SIG_UNBLOCK, &nset, NULL);
709 n_raise(s);
710 sigprocmask(SIG_BLOCK, &nset, NULL);
712 safe_signal(s, old_action);
714 if(a_lex_reset_on_stop){
715 a_lex_reset_on_stop = 0;
716 n_TERMCAP_RESUME(TRU1);
717 siglongjmp(srbuf, 0); /* FIXME get rid */
721 static void
722 a_lex_hangup(int s){
723 NYD_X; /* Signal handler */
724 UNUSED(s);
725 /* nothing to do? */
726 exit(EXIT_ERR);
729 static void
730 a_lex_onintr(int s){
731 NYD_X; /* Signal handler */
733 safe_signal(SIGINT, a_lex_onintr);
734 noreset = 0;
735 a_lex_unstack(TRUM1);
737 termios_state_reset();
738 close_all_files(); /* FIXME .. to outer level ONLU! */
740 if(image >= 0){
741 close(image);
742 image = -1;
744 if(interrupts != 1)
745 n_err_sighdl(_("Interrupt\n"));
746 safe_signal(SIGPIPE, a_lex_oldpipe);
747 siglongjmp(srbuf, 0); /* FIXME get rid */
750 static void
751 a_lex_unstack(bool_t eval_error){
752 struct a_lex_input_stack *lip;
753 NYD_ENTER;
755 if((lip = a_lex_input) == NULL){
756 /* If called from a_lex_onintr(), be silent FIXME */
757 pstate &= ~(PS_SOURCING | PS_ROBOT);
758 if(eval_error == TRUM1 || !(pstate & PS_STARTED))
759 goto jleave;
760 goto jerr;
763 if(lip->li_flags & a_LEX_MACRO){
764 if(lip->li_flags & a_LEX_MACRO_FREE_DATA){
765 char **lp;
767 while(*(lp = &lip->li_lines[lip->li_loff]) != NULL){
768 free(*lp);
769 ++lip->li_loff;
771 /* Part of lip's memory chunk, then */
772 if(!(lip->li_flags & a_LEX_MACRO_CMD))
773 free(lip->li_lines);
775 }else{
776 if(lip->li_flags & a_LEX_PIPE)
777 /* XXX command manager should -TERM then -KILL instead of hoping
778 * XXX for exit of provider due to EPIPE / SIGPIPE */
779 Pclose(lip->li_file, TRU1);
780 else
781 Fclose(lip->li_file);
784 if(!condstack_take(lip->li_cond)){
785 n_err(_("Unmatched `if' at end of %s \"%s\"\n"),
786 ((lip->li_flags & a_LEX_MACRO
787 ? (lip->li_flags & a_LEX_MACRO_CMD ? _("command") : _("macro"))
788 : _("`source'd file"))),
789 lip->li_name);
790 eval_error = TRU1;
793 if((a_lex_input = lip->li_outer) == NULL){
794 pstate &= ~(PS_SOURCING | PS_ROBOT);
795 }else{
796 if((a_lex_input->li_flags & (a_LEX_MACRO | a_LEX_SUPER_MACRO)) ==
797 (a_LEX_MACRO | a_LEX_SUPER_MACRO))
798 pstate &= ~PS_SOURCING;
799 assert(pstate & PS_ROBOT);
802 if(eval_error)
803 goto jerr;
804 if(lip->li_flags & a_LEX_FREE)
805 free(lip);
806 jleave:
807 if(UNLIKELY(a_lex_input != NULL && eval_error == TRUM1))
808 a_lex_unstack(TRUM1);
809 NYD_LEAVE;
810 return;
812 jerr:
813 if(lip != NULL){
814 if(options & OPT_D_V)
815 n_alert(_("Stopped %s \"%s\" due to errors%s"),
816 (pstate & PS_STARTED
817 ? (lip->li_flags & a_LEX_MACRO
818 ? (lip->li_flags & a_LEX_MACRO_CMD
819 ? _("evaluating command") : _("evaluating macro"))
820 : (lip->li_flags & a_LEX_PIPE
821 ? _("executing `source'd pipe")
822 : _("loading `source'd file")))
823 : (lip->li_flags & a_LEX_MACRO
824 ? (lip->li_flags & a_LEX_MACRO_X_OPTION
825 ? _("evaluating command line") : _("evaluating macro"))
826 : _("loading initialization resource"))),
827 lip->li_name,
828 (options & OPT_DEBUG ? "" : _(" (enable *debug* for trace)")));
829 if(lip->li_flags & a_LEX_FREE)
830 free(lip);
833 if(!(options & OPT_INTERACTIVE) && !(pstate & PS_STARTED)){
834 if(options & OPT_D_V)
835 n_alert(_("Non-interactive, bailing out due to errors "
836 "in startup load phase"));
837 exit(EXIT_ERR);
839 goto jleave;
842 static bool_t
843 a_lex_source_file(char const *file, bool_t silent_error){
844 struct a_lex_input_stack *lip;
845 size_t nlen;
846 char *nbuf;
847 bool_t ispipe;
848 FILE *fip;
849 NYD_ENTER;
851 fip = NULL;
853 /* Being a command argument file is space-trimmed */
854 if((ispipe = (!silent_error && (nlen = strlen(file)) > 0 &&
855 file[--nlen] == '|'))){
856 char const *sh;
858 if((sh = ok_vlook(SHELL)) == NULL)
859 sh = XSHELL;
860 if((fip = Popen(nbuf = savestrbuf(file, nlen), "r",
861 sh, NULL, COMMAND_FD_NULL)) == NULL){
862 if(!silent_error || (options & OPT_D_V))
863 n_perr(nbuf, 0);
864 goto jleave;
866 }else if((nbuf = fexpand(file, FEXP_LOCAL)) == NULL)
867 goto jleave;
868 else if((fip = Fopen(nbuf, "r")) == NULL){
869 if(!silent_error || (options & OPT_D_V))
870 n_perr(nbuf, 0);
871 goto jleave;
874 lip = smalloc(sizeof(*lip) -
875 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
876 (nlen = strlen(nbuf) +1));
877 lip->li_outer = a_lex_input;
878 lip->li_file = fip;
879 lip->li_cond = condstack_release();
880 lip->li_flags = (ispipe ? a_LEX_PIPE : a_LEX_NONE) |
881 (a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
882 ? a_LEX_SUPER_MACRO : 0);
883 memcpy(lip->li_name, nbuf, nlen);
885 pstate |= PS_SOURCING | PS_ROBOT;
886 a_lex_input = lip;
887 a_commands_recursive();
888 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
889 jleave:
890 NYD_LEAVE;
891 return silent_error ? TRU1 : (fip != NULL);
894 static bool_t
895 a_lex_load(struct a_lex_input_stack *lip){
896 bool_t rv;
897 NYD2_ENTER;
899 assert(!(pstate & PS_STARTED));
900 assert(a_lex_input == NULL);
902 /* POSIX:
903 * Any errors in the start-up file shall either cause mailx to terminate
904 * with a diagnostic message and a non-zero status or to continue after
905 * writing a diagnostic message, ignoring the remainder of the lines in
906 * the start-up file. */
907 lip->li_cond = condstack_release();
909 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
910 pstate |= PS_ROBOT |
911 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
913 pstate |= PS_ROBOT | PS_SOURCING;
914 if(options & OPT_D_V)
915 n_err(_("Loading \"%s\"\n"), lip->li_name);
916 a_lex_input = lip;
917 if(!(rv = n_commands())){
918 if(!(options & OPT_INTERACTIVE)){
919 if(options & OPT_D_V)
920 n_alert(_("Non-interactive program mode, forced exit"));
921 exit(EXIT_ERR);
924 /* PS_EXIT handled by callers */
925 NYD2_LEAVE;
926 return rv;
929 static bool_t
930 a_commands_recursive(void){
931 struct a_lex_eval_ctx ev;
932 bool_t rv;
933 NYD2_ENTER;
935 memset(&ev, 0, sizeof ev);
937 /* FIXME sigkondom */
938 n_COLOUR( n_colour_env_push(); )
939 rv = TRU1;
940 for(;;){
941 int n;
943 /* Read a line of commands and handle end of file specially */
944 ev.le_line.l = ev.le_line_size;
945 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
946 ev.le_new_content);
947 ev.le_line_size = (ui32_t)ev.le_line.l;
948 ev.le_line.l = (ui32_t)n;
950 if(n < 0)
951 break;
953 if(a_lex_evaluate(&ev)){
954 rv = FAL0;
955 break;
958 if((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)){
959 if(exit_status != EXIT_OK)
960 break;
963 a_lex_unstack(!rv);
964 n_COLOUR( n_colour_env_pop(FAL0); )
966 if(ev.le_line.s != NULL)
967 free(ev.le_line.s);
968 NYD2_LEAVE;
969 return rv;
972 #ifdef HAVE_DOCSTRINGS
973 FL bool_t
974 n_print_comm_docstr(char const *comm){
975 struct a_lex_ghost const *gp;
976 struct a_lex_cmd const *cp, *cpmax;
977 bool_t rv = FAL0;
978 NYD_ENTER;
980 /* Ghosts take precedence */
981 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
982 if(!strcmp(comm, gp->lg_name)){
983 printf("%s -> ", comm);
984 comm = gp->lg_cmd.s;
985 break;
988 cpmax = &(cp = a_lex_cmd_tab)[NELEM(a_lex_cmd_tab)];
989 jredo:
990 for(; PTRCMP(cp, <, cpmax); ++cp){
991 if(!strcmp(comm, cp->lc_name))
992 printf("%s: %s\n", comm, V_(cp->lc_doc));
993 else if(is_prefix(comm, cp->lc_name))
994 printf("%s (%s): %s\n", comm, cp->lc_name, V_(cp->lc_doc));
995 else
996 continue;
997 rv = TRU1;
998 break;
1000 if(!rv && PTRCMP(cpmax, ==, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)])){
1001 cpmax = &(cp = a_lex_special_cmd_tab)[NELEM(a_lex_special_cmd_tab)];
1002 goto jredo;
1005 if(!rv && gp != NULL){
1006 printf("\"%s\"\n", comm);
1007 rv = TRU1;
1009 NYD_LEAVE;
1010 return rv;
1012 #endif /* HAVE_DOCSTRINGS */
1014 FL bool_t
1015 n_commands(void){ /* FIXME */
1016 struct a_lex_eval_ctx ev;
1017 int n;
1018 bool_t volatile rv = TRU1;
1019 NYD_ENTER;
1021 if (!(pstate & PS_SOURCING)) {
1022 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
1023 safe_signal(SIGINT, &a_lex_onintr);
1024 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1025 safe_signal(SIGHUP, &a_lex_hangup);
1026 /* TODO We do a lot of redundant signal handling, especially
1027 * TODO with the command line editor(s); try to merge this */
1028 safe_signal(SIGTSTP, &a_lex_stop);
1029 safe_signal(SIGTTOU, &a_lex_stop);
1030 safe_signal(SIGTTIN, &a_lex_stop);
1032 a_lex_oldpipe = safe_signal(SIGPIPE, SIG_IGN);
1033 safe_signal(SIGPIPE, a_lex_oldpipe);
1035 memset(&ev, 0, sizeof ev);
1037 (void)sigsetjmp(srbuf, 1); /* FIXME get rid */
1038 for (;;) {
1039 char *temporary_orig_line; /* XXX eval_ctx.le_line not yet constant */
1041 n_COLOUR( n_colour_env_pop(TRU1); )
1043 /* TODO Unless we have our signal manager (or however we do it) child
1044 * TODO processes may have time slots where their execution isn't
1045 * TODO protected by signal handlers (in between start and setup
1046 * TODO completed). close_all_files() is only called from onintr()
1047 * TODO so those may linger possibly forever */
1048 if(!(pstate & PS_SOURCING))
1049 close_all_files();
1051 interrupts = 0;
1053 temporary_localopts_free(); /* XXX intermediate hack */
1054 sreset((pstate & PS_SOURCING) != 0);
1055 if (!(pstate & PS_SOURCING)) {
1056 char *cp;
1058 /* TODO Note: this buffer may contain a password. We should redefine
1059 * TODO the code flow which has to do that */
1060 if ((cp = termios_state.ts_linebuf) != NULL) {
1061 termios_state.ts_linebuf = NULL;
1062 termios_state.ts_linesize = 0;
1063 free(cp); /* TODO pool give-back */
1065 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1066 if (ev.le_line.l > LINESIZE * 3) {
1067 free(ev.le_line.s); /* TODO pool! but what? */
1068 ev.le_line.s = NULL;
1069 ev.le_line.l = ev.le_line_size = 0;
1073 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1074 char *cp;
1076 cp = ok_vlook(newmail);
1077 if ((options & OPT_TTYIN) && cp != NULL) {
1078 struct stat st;
1080 /* FIXME TEST WITH NOPOLL ETC. !!! */
1081 n = (cp != NULL && strcmp(cp, "nopoll"));
1082 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
1083 st.st_size > mailsize) ||
1084 (mb.mb_type == MB_MAILDIR && n != 0)) {
1085 size_t odot = PTR2SIZE(dot - message);
1086 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
1088 if (setfile(mailname,
1089 FEDIT_NEWMAIL |
1090 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
1091 exit_status |= EXIT_ERR;
1092 rv = FAL0;
1093 break;
1095 dot = message + odot;
1096 pstate |= odid;
1100 a_lex_reset_on_stop = TRU1;
1101 exit_status = EXIT_OK;
1104 /* Read a line of commands and handle end of file specially */
1105 jreadline:
1106 ev.le_line.l = ev.le_line_size;
1107 n = n_lex_input(NULL, TRU1, &ev.le_line.s, &ev.le_line.l,
1108 ev.le_new_content);
1109 ev.le_line_size = (ui32_t)ev.le_line.l;
1110 ev.le_line.l = (ui32_t)n;
1111 a_lex_reset_on_stop = FAL0;
1113 if (n < 0) {
1114 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1115 if (!(pstate & PS_ROBOT) &&
1116 (options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
1117 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1118 n_msleep(500, FAL0);
1119 continue;
1121 break;
1124 temporary_orig_line = ((pstate & PS_SOURCING) ||
1125 !(options & OPT_INTERACTIVE)) ? NULL
1126 : savestrbuf(ev.le_line.s, ev.le_line.l);
1127 pstate &= ~PS_HOOK_MASK;
1128 if (a_lex_evaluate(&ev)) {
1129 if (!(pstate & PS_STARTED)) /* TODO mess; join PS_EVAL_ERROR.. */
1130 rv = FAL0;
1131 break;
1134 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
1135 if (exit_status != EXIT_OK)
1136 break;
1137 if ((pstate & (PS_SOURCING | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
1138 exit_status = EXIT_ERR;
1139 break;
1142 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1143 if (ev.le_new_content != NULL)
1144 goto jreadline;
1145 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1146 if (temporary_orig_line != NULL)
1147 n_tty_addhist(temporary_orig_line, !ev.le_add_history);
1150 if(pstate & PS_EXIT)
1151 break;
1153 a_lex_unstack(!rv);
1155 if (ev.le_line.s != NULL)
1156 free(ev.le_line.s);
1157 if (pstate & PS_SOURCING)
1158 sreset(FAL0);
1159 NYD_LEAVE;
1160 return rv;
1163 FL int
1164 (n_lex_input)(char const *prompt, bool_t nl_escape, char **linebuf,
1165 size_t *linesize, char const *string SMALLOC_DEBUG_ARGS){
1166 /* TODO readline: linebuf pool! */
1167 bool_t doprompt, dotty;
1168 int n, nold;
1169 FILE *ifile;
1170 NYD2_ENTER;
1172 /* Special case macro mode: never need to prompt, lines have always been
1173 * unfolded already */
1174 if(a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO)){
1175 char **lp = &a_lex_input->li_lines[a_lex_input->li_loff];
1177 if(*linebuf != NULL)
1178 free(*linebuf);
1180 if((*linebuf = *lp) == NULL){
1181 *linesize = 0;
1182 n = -1;
1183 }else{
1184 ++a_lex_input->li_loff;
1185 n = (int)(*linesize = strlen(*linebuf));
1187 if(!(a_lex_input->li_flags & a_LEX_MACRO_FREE_DATA))
1188 *linebuf = sbufdup(*linebuf, *linesize);
1190 if(options & OPT_D_VV)
1191 n_err(_("%s %d bytes <%.*s>\n"),
1192 (a_lex_input->li_flags & a_LEX_MACRO_X_OPTION
1193 ? _("-X option") : _("MACRO")),
1194 n, n, *linebuf);
1196 goto jleave;
1199 doprompt = ((pstate & (PS_STARTED | PS_ROBOT)) == PS_STARTED &&
1200 (options & OPT_INTERACTIVE));
1201 dotty = (doprompt && !ok_blook(line_editor_disable));
1202 if(!doprompt)
1203 prompt = NULL;
1204 else if(prompt == NULL)
1205 prompt = getprompt();
1207 /* Ensure stdout is flushed first anyway */
1208 if(!dotty && prompt == NULL)
1209 fflush(stdout);
1211 ifile = (a_lex_input != NULL) ? a_lex_input->li_file : stdin;
1212 assert(ifile != NULL);
1214 for(nold = n = 0;;){
1215 if(dotty){
1216 assert(ifile == stdin);
1217 if(string != NULL && (n = (int)strlen(string)) > 0){
1218 if(*linesize > 0)
1219 *linesize += n +1;
1220 else
1221 *linesize = (size_t)n + LINESIZE +1;
1222 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
1223 memcpy(*linebuf, string, (size_t)n +1);
1225 string = NULL;
1226 /* TODO if nold>0, don't redisplay the entire line!
1227 * TODO needs complete redesign ... */
1228 n = (n_tty_readline)(prompt, linebuf, linesize, n
1229 SMALLOC_DEBUG_ARGSCALL);
1230 }else{
1231 if(prompt != NULL) {
1232 if(*prompt != '\0')
1233 fputs(prompt, stdout);
1234 fflush(stdout);
1237 n = (readline_restart)(ifile, linebuf, linesize, n
1238 SMALLOC_DEBUG_ARGSCALL);
1240 if(n > 0 && nold > 0){
1241 int i = 0;
1242 char const *cp = *linebuf + nold;
1244 while(blankspacechar(*cp) && nold + i < n)
1245 ++cp, ++i;
1246 if(i > 0){
1247 memmove(*linebuf + nold, cp, n - nold - i);
1248 n -= i;
1249 (*linebuf)[n] = '\0';
1254 if(n <= 0)
1255 break;
1257 /* POSIX says:
1258 * An unquoted <backslash> at the end of a command line shall
1259 * be discarded and the next line shall continue the command */
1260 if(!nl_escape || (*linebuf)[n - 1] != '\\')
1261 break;
1262 /* Definitely outside of quotes, thus the quoting rules are so that an
1263 * uneven number of successive backslashs at EOL is a continuation */
1264 if(n > 1){
1265 size_t i, j;
1267 for(j = 1, i = (size_t)n - 1; i-- > 0; ++j)
1268 if((*linebuf)[i] != '\\')
1269 break;
1270 if(!(j & 1))
1271 break;
1273 (*linebuf)[nold = --n] = '\0';
1274 if(prompt != NULL && *prompt != '\0')
1275 prompt = ".. "; /* XXX PS2 .. */
1278 if(n >= 0){
1279 (*linebuf)[*linesize = n] = '\0';
1281 if(options & OPT_D_VV)
1282 n_err(_("%s %d bytes <%.*s>\n"),
1283 (!(pstate & PS_STARTED) ? "LOAD"
1284 : (pstate & PS_SOURCING ? "SOURCE" : "READ")),
1285 n, n, *linebuf);
1287 jleave:
1288 if (pstate & PS_PSTATE_PENDMASK)
1289 a_lex_update_pstate();
1291 NYD2_LEAVE;
1292 return n;
1295 FL char *
1296 n_lex_input_cp_addhist(char const *prompt, char const *string, bool_t isgabby){
1297 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1298 size_t linesize;
1299 char *linebuf, *rv;
1300 int n;
1301 NYD2_ENTER;
1303 linesize = 0;
1304 linebuf = NULL;
1305 rv = NULL;
1307 n = n_lex_input(prompt, TRU1, &linebuf, &linesize, string);
1308 if(n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
1309 (options & OPT_INTERACTIVE))
1310 n_tty_addhist(rv, isgabby);
1312 if(linebuf != NULL)
1313 free(linebuf);
1314 NYD2_LEAVE;
1315 return rv;
1318 FL void
1319 n_load(char const *name){
1320 struct a_lex_input_stack *lip;
1321 size_t i;
1322 FILE *fip;
1323 NYD_ENTER;
1325 if(name == NULL || *name == '\0' || (fip = Fopen(name, "r")) == NULL)
1326 goto jleave;
1328 i = strlen(name) +1;
1329 lip = scalloc(1, sizeof(*lip) -
1330 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) + i);
1331 lip->li_file = fip;
1332 lip->li_flags = a_LEX_FREE;
1333 memcpy(lip->li_name, name, i);
1335 a_lex_load(lip);
1336 pstate &= ~PS_EXIT;
1337 jleave:
1338 NYD_LEAVE;
1341 FL void
1342 n_load_Xargs(char const **lines){
1343 static char const name[] = "-X";
1345 ui8_t buf[sizeof(struct a_lex_input_stack) + sizeof name];
1346 struct a_lex_input_stack *lip;
1347 NYD_ENTER;
1349 memset(buf, 0, sizeof buf);
1350 lip = (void*)buf;
1351 lip->li_flags = a_LEX_MACRO | a_LEX_MACRO_X_OPTION | a_LEX_SUPER_MACRO;
1352 lip->li_lines = UNCONST(lines);
1353 memcpy(lip->li_name, name, sizeof name);
1355 a_lex_load(lip);
1356 if(pstate & PS_EXIT)
1357 exit(EXIT_OK);
1358 NYD_LEAVE;
1361 FL int
1362 c_source(void *v){
1363 int rv;
1364 NYD_ENTER;
1366 rv = (a_lex_source_file(*(char**)v, FAL0) == TRU1) ? 0 : 1;
1367 NYD_LEAVE;
1368 return rv;
1371 FL int
1372 c_source_if(void *v){ /* XXX obsolete?, support file tests in `if' etc.! */
1373 int rv;
1374 NYD_ENTER;
1376 rv = (a_lex_source_file(*(char**)v, TRU1) != FAL0) ? 0 : 1;
1377 NYD_LEAVE;
1378 return rv;
1381 FL bool_t
1382 n_source_macro(char const *name, char **lines){
1383 struct a_lex_input_stack *lip;
1384 size_t i;
1385 int rv;
1386 NYD_ENTER;
1388 lip = smalloc(sizeof(*lip) -
1389 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1390 (i = strlen(name) +1));
1391 lip->li_outer = a_lex_input;
1392 lip->li_file = NULL;
1393 lip->li_cond = condstack_release();
1394 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1395 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1396 ? a_LEX_SUPER_MACRO : 0);
1397 lip->li_loff = 0;
1398 lip->li_lines = lines;
1399 memcpy(lip->li_name, name, i);
1401 pstate |= PS_ROBOT;
1402 a_lex_input = lip;
1403 rv = a_commands_recursive();
1404 NYD_LEAVE;
1405 return rv;
1408 FL bool_t
1409 n_source_command(char const *cmd){
1410 struct a_lex_input_stack *lip;
1411 size_t i, ial;
1412 bool_t rv;
1413 NYD_ENTER;
1415 i = strlen(cmd);
1416 cmd = sbufdup(cmd, i++);
1417 ial = n_ALIGN(i);
1419 lip = smalloc(sizeof(*lip) -
1420 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1421 ial + 2*sizeof(char*));
1422 lip->li_outer = a_lex_input;
1423 lip->li_file = NULL;
1424 lip->li_cond = condstack_release();
1425 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1426 a_LEX_MACRO_CMD |
1427 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1428 ? a_LEX_SUPER_MACRO : 0);
1429 lip->li_loff = 0;
1430 lip->li_lines = (void*)(lip->li_name + ial);
1431 lip->li_lines[0] = UNCONST(cmd); /* dup'ed above */
1432 lip->li_lines[1] = NULL;
1433 memcpy(lip->li_name, cmd, i);
1435 pstate |= PS_ROBOT;
1436 a_lex_input = lip;
1437 rv = a_commands_recursive();
1438 NYD_LEAVE;
1439 return rv;
1442 FL bool_t
1443 n_source_may_yield_control(void){
1444 return ((options & OPT_INTERACTIVE) &&
1445 (pstate & PS_STARTED) &&
1446 (!(pstate & PS_ROBOT) || (pstate & PS_RECURSED)) && /* Ok for ~: */
1447 (a_lex_input == NULL || a_lex_input->li_outer == NULL));
1450 /* s-it-mode */