FIX [ee4de6e4] and finally get asccaseprefix() right!
[s-mailx.git] / lex_input.c
blob9b494a48c2f71787fe44f4a41012a05d80b40f0c
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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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 /* TODO For simplicity this is yet _MACRO plus specialization overlay
51 * TODO (_X_OPTION, _CMD) -- these should be types on their own! */
52 a_LEX_MACRO_X_OPTION = 1<<4, /* Macro indeed command line -X option */
53 a_LEX_MACRO_CMD = 1<<5, /* Macro indeed single-line: ~:COMMAND */
54 /* TODO a_LEX_SLICE: the right way to support *on-compose-done-shell* would
55 * TODO be a command_loop object that emits an on_read_line event, and
56 * TODO have a special handler for the compose mode; with that, then,
57 * TODO commands_recursive() would not call lex_evaluate() but
58 * TODO CTX->on_read_line, and lex_evaluate() would be the standard impl.,
59 * TODO whereas the COMMAND ESCAPE switch in collect.c would be another one.
60 * TODO With this generic approach accmacvar.c:call_compose_mode_hook()
61 * TODO could be dropped, and n_source_macro() could become extended,
62 * TODO and/or we would add a n_source_anything(), which would allow special
63 * TODO input handlers, special I/O input and output, special `localopts'
64 * TODO etc., to be glued to the new execution context. And all I/O all
65 * TODO over this software should not use stdin/stdout, but CTX->in/out.
66 * TODO The pstate must be a property of the current execution context, too.
67 * TODO This not today. :( For now we invent a special SLICE execution
68 * TODO context overlay that at least allows to temporarily modify the
69 * TODO global pstate, and the global stdin and stdout pointers. HACK!
70 * TODO This slice thing is very special and has to go again. HACK!!
71 * TODO a_lex_input() will drop it once it sees EOF (HACK!), but care for
72 * TODO jumps must be taken by slice creators. HACK!!! But works. ;} */
73 a_LEX_SLICE = 1<<6,
75 a_LEX_SUPER_MACRO = 1<<16 /* *Not* inheriting PS_SOURCING state */
78 struct a_lex_cmd{
79 char const *lc_name; /* Name of command */
80 int (*lc_func)(void*); /* Implementor of command */
81 enum argtype lc_argtype; /* Arglist type (see below) */
82 si16_t lc_msgflag; /* Required flags of msgs */
83 si16_t lc_msgmask; /* Relevant flags of msgs */
84 #ifdef HAVE_DOCSTRINGS
85 char const *lc_doc; /* One line doc for command */
86 #endif
88 /* Yechh, can't initialize unions */
89 #define lc_minargs lc_msgflag /* Minimum argcount for RAWLIST */
90 #define lc_maxargs lc_msgmask /* Max argcount for RAWLIST */
92 struct a_lex_ghost{
93 struct a_lex_ghost *lg_next;
94 struct str lg_cmd; /* Data follows after .lg_name */
95 char lg_name[n_VFIELD_SIZE(0)];
98 struct a_lex_eval_ctx{
99 struct str le_line; /* The terminated data to _evaluate() */
100 ui32_t le_line_size; /* May be used to store line memory size */
101 bool_t le_is_recursive; /* Evaluation in evaluation? (collect ~:) */
102 ui8_t __dummy[3];
103 bool_t le_add_history; /* Add command to history (TRUM1=gabby)? */
104 char const *le_new_content; /* History: reenter line, start with this */
107 struct a_lex_input_stack{
108 struct a_lex_input_stack *li_outer;
109 FILE *li_file; /* File we were in */
110 void *li_cond; /* Saved state of conditional stack */
111 ui32_t li_flags; /* enum a_lex_input_flags */
112 ui32_t li_loff; /* Pseudo (macro): index in .li_lines */
113 char **li_lines; /* Pseudo content, lines unfolded */
114 void (*li_on_finalize)(void *);
115 void *li_finalize_arg;
116 char li_autorecmem[n_MEMORY_AUTOREC_TYPE_SIZEOF];
117 sigjmp_buf li_cmdrec_jmp; /* TODO one day... for command_recursive */
118 /* SLICE hacks: saved stdin/stdout, saved pstate */
119 FILE *li_slice_stdin;
120 FILE *li_slice_stdout;
121 ui32_t li_slice_options;
122 ui8_t li_slice__dummy[4];
123 char li_name[n_VFIELD_SIZE(0)]; /* Name of file or macro */
125 n_CTA(n_MEMORY_AUTOREC_TYPE_SIZEOF % sizeof(void*) == 0,
126 "Inacceptible size of structure buffer");
128 static sighandler_type a_lex_oldpipe;
129 static struct a_lex_ghost *a_lex_ghosts;
130 /* a_lex_cmd_tab[] after fun protos */
132 /* */
133 static struct a_lex_input_stack *a_lex_input;
135 static sigjmp_buf a_lex_srbuf; /* TODO GET RID */
137 /* Isolate the command from the arguments */
138 static char *a_lex_isolate(char const *comm);
140 /* Command ghost handling */
141 static int a_lex_c_ghost(void *v);
142 static int a_lex_c_unghost(void *v);
144 /* */
145 static char const *a_lex_cmdinfo(struct a_lex_cmd const *lcp);
147 /* Print a list of all commands */
148 static int a_lex_c_list(void *v);
150 static int a_lex__pcmd_cmp(void const *s1, void const *s2);
152 /* `help' / `?' command */
153 static int a_lex_c_help(void *v);
155 /* `exit' and `quit' commands */
156 static int a_lex_c_exit(void *v);
157 static int a_lex_c_quit(void *v);
159 /* Print the binaries version number */
160 static int a_lex_c_version(void *v);
162 static int a_lex__version_cmp(void const *s1, void const *s2);
164 /* PS_STATE_PENDMASK requires some actions */
165 static void a_lex_update_pstate(void);
167 /* Evaluate a single command.
168 * .le_add_history and .le_new_content will be updated upon success.
169 * Command functions return 0 for success, 1 for error, and -1 for abort.
170 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
171 static int a_lex_evaluate(struct a_lex_eval_ctx *evp);
173 /* Get first-fit, or NULL */
174 static struct a_lex_cmd const *a_lex__firstfit(char const *comm);
176 /* Branch here on hangup signal and simulate "exit" */
177 static void a_lex_hangup(int s);
179 /* The following gets called on receipt of an interrupt. Close all open files
180 * except 0, 1, 2, and the temporary. Also, unstack all source files */
181 static void a_lex_onintr(int s);
183 /* Pop the current input back to the previous level. Update the program state.
184 * If the argument is TRUM1 then we don't alert and error out if the stack
185 * doesn't exist at all */
186 static void a_lex_unstack(bool_t eval_error);
188 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
189 static bool_t a_lex_source_file(char const *file, bool_t silent_error);
191 /* System resource file load()ing or -X command line option array traversal */
192 static bool_t a_lex_load(struct a_lex_input_stack *lip);
194 /* A simplified command loop for recursed state machines */
195 static bool_t a_commands_recursive(enum n_lexinput_flags lif);
197 /* List of all commands, and list of commands which are specially treated
198 * and deduced in _evaluate(), but we need a list for _c_list() and
199 * print_comm_docstr() */
200 #ifdef HAVE_DOCSTRINGS
201 # define DS(S) , S
202 #else
203 # define DS(S)
204 #endif
205 static struct a_lex_cmd const a_lex_cmd_tab[] = {
206 #include "cmd_tab.h"
208 a_lex_special_cmd_tab[] = {
209 { "#", NULL, 0, 0, 0
210 DS(N_("Comment command: ignore remaining (continuable) line")) },
211 { "-", NULL, 0, 0, 0
212 DS(N_("Print out the preceding message")) }
214 #undef DS
216 static char *
217 a_lex_isolate(char const *comm){
218 NYD2_ENTER;
219 while(*comm != '\0' &&
220 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
221 ++comm;
222 NYD2_LEAVE;
223 return n_UNCONST(comm);
226 static int
227 a_lex_c_ghost(void *v){
228 struct a_lex_ghost *lgp, *gp;
229 size_t i, cl, nl;
230 char *cp;
231 char const **argv;
232 NYD_ENTER;
234 argv = v;
236 /* Show the list? */
237 if(*argv == NULL){
238 FILE *fp;
240 if((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
241 fp = stdout;
243 for(i = 0, gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
244 fprintf(fp, "wysh ghost %s %s\n",
245 gp->lg_name, n_shexp_quote_cp(gp->lg_cmd.s, TRU1));
247 if(fp != stdout){
248 page_or_print(fp, i);
249 Fclose(fp);
251 goto jleave;
254 /* Verify the ghost name is a valid one */
255 if(*argv[0] == '\0' || *a_lex_isolate(argv[0]) != '\0'){
256 n_err(_("`ghost': can't canonicalize %s\n"),
257 n_shexp_quote_cp(argv[0], FAL0));
258 v = NULL;
259 goto jleave;
262 /* Show command of single ghost? */
263 if(argv[1] == NULL){
264 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
265 if(!strcmp(argv[0], gp->lg_name)){
266 printf("wysh ghost %s %s\n",
267 gp->lg_name, n_shexp_quote_cp(gp->lg_cmd.s, TRU1));
268 goto jleave;
270 n_err(_("`ghost': no such alias: %s\n"), argv[0]);
271 v = NULL;
272 goto jleave;
275 /* Define command for ghost: verify command content */
276 for(cl = 0, i = 1; (cp = n_UNCONST(argv[i])) != NULL; ++i)
277 if(*cp != '\0')
278 cl += strlen(cp) +1; /* SP or NUL */
279 if(cl == 0){
280 n_err(_("`ghost': empty command arguments after %s\n"), argv[0]);
281 v = NULL;
282 goto jleave;
285 /* If the ghost already exists, recreate */
286 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL; lgp = gp, gp = gp->lg_next)
287 if(!strcmp(gp->lg_name, argv[0])){
288 if(lgp != NULL)
289 lgp->lg_next = gp->lg_next;
290 else
291 a_lex_ghosts = gp->lg_next;
292 free(gp);
293 break;
296 nl = strlen(argv[0]) +1;
297 gp = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_ghost, lg_name) + nl + cl);
298 gp->lg_next = a_lex_ghosts;
299 a_lex_ghosts = gp;
300 memcpy(gp->lg_name, argv[0], nl);
301 cp = gp->lg_cmd.s = gp->lg_name + nl;
302 gp->lg_cmd.l = --cl;
304 while(*++argv != NULL)
305 if((i = strlen(*argv)) > 0){
306 memcpy(cp, *argv, i);
307 cp += i;
308 *cp++ = ' ';
310 *--cp = '\0';
311 jleave:
312 NYD_LEAVE;
313 return v == NULL;
316 static int
317 a_lex_c_unghost(void *v){
318 struct a_lex_ghost *lgp, *gp;
319 char const **argv, *cp;
320 int rv;
321 NYD_ENTER;
323 rv = 0;
324 argv = v;
326 while((cp = *argv++) != NULL){
327 if(cp[0] == '*' && cp[1] == '\0'){
328 while((gp = a_lex_ghosts) != NULL){
329 a_lex_ghosts = gp->lg_next;
330 free(gp);
332 }else{
333 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL;
334 lgp = gp, gp = gp->lg_next)
335 if(!strcmp(gp->lg_name, cp)){
336 if(lgp != NULL)
337 lgp->lg_next = gp->lg_next;
338 else
339 a_lex_ghosts = gp->lg_next;
340 free(gp);
341 goto jouter;
343 n_err(_("`unghost': no such alias: %s\n"),
344 n_shexp_quote_cp(cp, FAL0));
345 rv = 1;
346 jouter: ;
349 NYD_LEAVE;
350 return rv;
353 static char const *
354 a_lex_cmdinfo(struct a_lex_cmd const *lcp){
355 struct n_string rvb, *rv;
356 char const *cp;
357 NYD2_ENTER;
359 rv = n_string_creat_auto(&rvb);
360 rv = n_string_reserve(rv, 80);
362 switch(lcp->lc_argtype & ARG_ARGMASK){
363 case ARG_MSGLIST: cp = N_("message-list"); break;
364 case ARG_STRLIST: cp = N_("string data"); break;
365 case ARG_RAWLIST: cp = N_("old-style quoting"); break;
366 case ARG_NOLIST: cp = N_("no arguments"); break;
367 case ARG_NDMLIST: cp = N_("message-list (no default)"); break;
368 case ARG_WYSHLIST: cp = N_("sh(1)ell-style quoting"); break;
369 default: cp = N_("`wysh' for sh(1)ell-style quoting"); break;
371 rv = n_string_push_cp(rv, V_(cp));
373 if(lcp->lc_argtype & (ARG_A | ARG_I | ARG_M | ARG_R | ARG_S | ARG_X))
374 rv = n_string_push_buf(rv, " |", 2);
376 if(lcp->lc_argtype & ARG_A)
377 rv = n_string_push_cp(rv, _(" needs box"));
378 if(lcp->lc_argtype & ARG_I)
379 rv = n_string_push_cp(rv, _(" only interactive"));
380 if(lcp->lc_argtype & ARG_M)
381 rv = n_string_push_cp(rv, _(" send mode"));
382 if(lcp->lc_argtype & ARG_R)
383 rv = n_string_push_cp(rv, _(" no compose mode"));
384 if(lcp->lc_argtype & ARG_S)
385 rv = n_string_push_cp(rv, _(" after startup"));
386 if(lcp->lc_argtype & ARG_X)
387 rv = n_string_push_cp(rv, _(" subprocess"));
389 cp = n_string_cp(rv);
390 NYD2_LEAVE;
391 return cp;
394 static int
395 a_lex_c_list(void *v){
396 FILE *fp;
397 struct a_lex_cmd const **cpa, *cp, **cursor;
398 size_t l, i;
399 NYD_ENTER;
401 i = n_NELEM(a_lex_cmd_tab) + n_NELEM(a_lex_special_cmd_tab) +1;
402 cpa = salloc(sizeof(cp) * i);
404 for(i = 0; i < n_NELEM(a_lex_cmd_tab); ++i)
405 cpa[i] = &a_lex_cmd_tab[i];
406 /* C99 */{
407 size_t j;
409 for(j = 0; j < n_NELEM(a_lex_special_cmd_tab); ++i, ++j)
410 cpa[i] = &a_lex_special_cmd_tab[j];
412 cpa[i] = NULL;
414 /* C99 */{
415 char const *xcp = v;
417 if(*xcp == '\0')
418 qsort(cpa, i, sizeof(xcp), &a_lex__pcmd_cmp);
421 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
422 fp = stdout;
424 fprintf(fp, _("Commands are:\n"));
425 l = 1;
426 for(i = 0, cursor = cpa; (cp = *cursor++) != NULL;){
427 if(cp->lc_func == &c_cmdnotsupp)
428 continue;
429 if(options & OPT_D_V){
430 fprintf(fp, "%s\n", cp->lc_name);
431 ++l;
432 #ifdef HAVE_DOCSTRINGS
433 fprintf(fp, " : %s\n", V_(cp->lc_doc));
434 ++l;
435 #endif
436 fprintf(fp, " : %s\n", a_lex_cmdinfo(cp));
437 ++l;
438 }else{
439 size_t j = strlen(cp->lc_name) + 2;
441 if((i += j) > 72){
442 i = j;
443 fprintf(fp, "\n");
444 ++l;
446 fprintf(fp, (*cursor != NULL ? "%s, " : "%s\n"), cp->lc_name);
450 if(fp != stdout){
451 page_or_print(fp, l);
452 Fclose(fp);
454 NYD_LEAVE;
455 return 0;
458 static int
459 a_lex__pcmd_cmp(void const *s1, void const *s2){
460 struct a_lex_cmd const * const *cp1, * const *cp2;
461 int rv;
462 NYD2_ENTER;
464 cp1 = s1;
465 cp2 = s2;
466 rv = strcmp((*cp1)->lc_name, (*cp2)->lc_name);
467 NYD2_LEAVE;
468 return rv;
471 static int
472 a_lex_c_help(void *v){
473 int rv;
474 char *arg;
475 NYD_ENTER;
477 /* Help for a single command? */
478 if((arg = *(char**)v) != NULL){
479 struct a_lex_ghost const *gp;
480 struct a_lex_cmd const *lcp, *lcpmax;
482 /* Ghosts take precedence */
483 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
484 if(!strcmp(arg, gp->lg_name)){
485 printf("%s -> ", arg);
486 arg = gp->lg_cmd.s;
487 break;
490 lcpmax = &(lcp = a_lex_cmd_tab)[n_NELEM(a_lex_cmd_tab)];
491 jredo:
492 for(; lcp < lcpmax; ++lcp){
493 if(is_prefix(arg, lcp->lc_name)){
494 fputs(arg, stdout);
495 if(strcmp(arg, lcp->lc_name))
496 printf(" (%s)", lcp->lc_name);
497 }else
498 continue;
500 #ifdef HAVE_DOCSTRINGS
501 printf(": %s", V_(lcp->lc_doc));
502 #endif
503 if(options & OPT_D_V)
504 printf("\n : %s", a_lex_cmdinfo(lcp));
505 putchar('\n');
506 rv = 0;
507 goto jleave;
510 if(PTRCMP(lcpmax, ==, &a_lex_cmd_tab[n_NELEM(a_lex_cmd_tab)])){
511 lcpmax = &(lcp =
512 a_lex_special_cmd_tab)[n_NELEM(a_lex_special_cmd_tab)];
513 goto jredo;
516 if(gp != NULL){
517 printf("%s\n", n_shexp_quote_cp(arg, TRU1));
518 rv = 0;
519 }else{
520 n_err(_("Unknown command: `%s'\n"), arg);
521 rv = 1;
523 }else{
524 /* Very ugly, but take care for compiler supported string lengths :( */
525 fputs(progname, stdout);
526 fputs(_(
527 " commands -- <msglist> denotes message specifications,\n"
528 "e.g., 1-5, :n or ., separated by spaces:\n"), stdout);
529 fputs(_(
530 "\n"
531 "type <msglist> type (alias: `print') messages (honour `retain' etc.)\n"
532 "Type <msglist> like `type' but always show all headers\n"
533 "next goto and type next message\n"
534 "from <msglist> (search and) print header summary for the given list\n"
535 "headers header summary for messages surrounding \"dot\"\n"
536 "delete <msglist> delete messages (can be `undelete'd)\n"),
537 stdout);
539 fputs(_(
540 "\n"
541 "save <msglist> folder append messages to folder and mark as saved\n"
542 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
543 "write <msglist> file write message contents to file (prompts for parts)\n"
544 "Reply <msglist> reply to message senders only\n"
545 "reply <msglist> like `Reply', but address all recipients\n"
546 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
547 stdout);
549 fputs(_(
550 "\n"
551 "mail <recipients> compose a mail for the given recipients\n"
552 "file folder change to another mailbox\n"
553 "File folder like `file', but open readonly\n"
554 "quit quit and apply changes to the current mailbox\n"
555 "xit or exit like `quit', but discard changes\n"
556 "!shell command shell escape\n"
557 "list [<anything>] all available commands [in search order]\n"),
558 stdout);
560 rv = (ferror(stdout) != 0);
562 jleave:
563 NYD_LEAVE;
564 return rv;
567 static int
568 a_lex_c_exit(void *v){
569 NYD_ENTER;
570 n_UNUSED(v);
572 if(pstate & PS_STARTED){
573 /* In recursed state, return error to just pop the input level */
574 if(!(pstate & PS_SOURCING)){
575 #ifdef n_HAVE_TCAP
576 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) ==
577 OPT_INTERACTIVE)
578 n_termcap_destroy();
579 #endif
580 exit(EXIT_OK);
583 pstate |= PS_EXIT;
584 NYD_LEAVE;
585 return 0;
588 static int
589 a_lex_c_quit(void *v){
590 NYD_ENTER;
591 n_UNUSED(v);
593 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
594 * Otherwise return -1 to abort command loop */
595 pstate |= PS_EXIT;
596 NYD_LEAVE;
597 return 0;
600 static int
601 a_lex_c_version(void *v){
602 int longest, rv;
603 char *iop;
604 char const *cp, **arr;
605 size_t i, i2;
606 NYD_ENTER;
607 n_UNUSED(v);
609 printf(_("%s version %s\nFeatures included (+) or not (-)\n"),
610 uagent, ok_vlook(version));
612 /* *features* starts with dummy byte to avoid + -> *folder* expansions */
613 i = strlen(cp = &ok_vlook(features)[1]) +1;
614 iop = salloc(i);
615 memcpy(iop, cp, i);
617 arr = salloc(sizeof(cp) * VAL_FEATURES_CNT);
618 for(longest = 0, i = 0; (cp = n_strsep(&iop, ',', TRU1)) != NULL; ++i){
619 arr[i] = cp;
620 i2 = strlen(cp);
621 longest = n_MAX(longest, (int)i2);
623 qsort(arr, i, sizeof(cp), &a_lex__version_cmp);
625 for(++longest, i2 = 0; i-- > 0;){
626 cp = *(arr++);
627 printf("%-*s ", longest, cp);
628 i2 += longest;
629 if(UICMP(z, ++i2 + longest, >=, scrnwidth) || i == 0){
630 i2 = 0;
631 putchar('\n');
635 if((rv = ferror(stdout) != 0))
636 clearerr(stdout);
637 NYD_LEAVE;
638 return rv;
641 static int
642 a_lex__version_cmp(void const *s1, void const *s2){
643 char const * const *cp1, * const *cp2;
644 int rv;
645 NYD2_ENTER;
647 cp1 = s1;
648 cp2 = s2;
649 rv = strcmp(&(*cp1)[1], &(*cp2)[1]);
650 NYD2_LEAVE;
651 return rv;
654 static void
655 a_lex_update_pstate(void){
656 NYD_ENTER;
658 if(pstate & PS_SIGWINCH_PEND){
659 char buf[32];
661 snprintf(buf, sizeof buf, "%d", scrnwidth);
662 ok_vset(COLUMNS, buf);
663 snprintf(buf, sizeof buf, "%d", scrnheight);
664 ok_vset(LINES, buf);
667 pstate &= ~PS_PSTATE_PENDMASK;
668 NYD_LEAVE;
671 static int
672 a_lex_evaluate(struct a_lex_eval_ctx *evp){
673 /* xxx old style(9), but also old code */
674 struct str line;
675 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
676 struct a_lex_ghost *gp;
677 struct a_lex_cmd const *cmd;
678 int c, e;
679 bool_t wysh;
680 NYD_ENTER;
682 wysh = FAL0;
683 e = 1;
684 cmd = NULL;
685 gp = NULL;
686 line = evp->le_line; /* XXX don't change original (buffer pointer) */
687 assert(line.s[line.l] == '\0');
688 evp->le_add_history = FAL0;
689 evp->le_new_content = NULL;
691 /* Command ghosts that refer to shell commands or macro expansion restart */
692 jrestart:
694 /* Strip the white space away from end and beginning of command */
695 if(line.l > 0){
696 size_t i = line.l;
698 for(cp = &line.s[i -1]; spacechar(*cp); --cp)
699 --i;
700 line.l = i;
702 for(cp = line.s; spacechar(*cp); ++cp)
704 line.l -= PTR2SIZE(cp - line.s);
706 /* Ignore null commands (comments) */
707 if(*cp == '#')
708 goto jleave0;
710 /* Handle ! differently to get the correct lexical conventions */
711 arglist[0] = cp;
712 if(*cp == '!')
713 ++cp;
714 /* Isolate the actual command; since it may not necessarily be
715 * separated from the arguments (as in `p1') we need to duplicate it to
716 * be able to create a NUL terminated version.
717 * We must be aware of several special one letter commands here */
718 else if((cp = a_lex_isolate(cp)) == arglist[0] &&
719 (*cp == '|' || *cp == '~' || *cp == '?'))
720 ++cp;
721 c = (int)PTR2SIZE(cp - arglist[0]);
722 line.l -= c;
723 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
724 memcpy(word, arglist[0], c);
725 word[c] = '\0';
727 /* Look up the command; if not found, bitch.
728 * Normally, a blank command would map to the first command in the
729 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
730 * confusion; act just the same for ghosts */
731 if(*word == '\0'){
732 if((pstate & PS_ROBOT) || gp != NULL)
733 goto jleave0;
734 cmd = a_lex_cmd_tab + 0;
735 goto jexec;
738 /* XXX It may be the argument parse adjuster */
739 if(!wysh && c == sizeof("wysh") -1 && !asccasecmp(word, "wysh")){
740 wysh = TRU1;
741 line.s = cp;
742 goto jrestart;
745 /* If this is the first evaluation, check command ghosts */
746 if(gp == NULL){
747 /* TODO relink list head, so it's sorted on usage over time?
748 * TODO in fact, there should be one hashmap over all commands and ghosts
749 * TODO so that the lookup could be made much more efficient than it is
750 * TODO now (two adjacent list searches! */
751 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
752 if(!strcmp(word, gp->lg_name)){
753 if(line.l > 0){
754 size_t i;
756 i = gp->lg_cmd.l;
757 line.s = salloc(i + line.l +1);
758 memcpy(line.s, gp->lg_cmd.s, i);
759 memcpy(line.s + i, cp, line.l);
760 line.s[i += line.l] = '\0';
761 line.l = i;
762 }else{
763 line.s = gp->lg_cmd.s;
764 line.l = gp->lg_cmd.l;
766 goto jrestart;
770 if((cmd = a_lex__firstfit(word)) == NULL || cmd->lc_func == &c_cmdnotsupp){
771 bool_t s;
773 if(!(s = condstack_isskip()) || (options & OPT_D_V))
774 n_err(_("Unknown command%s: `%s'\n"),
775 (s ? _(" (ignored due to `if' condition)") : n_empty), word);
776 if(s)
777 goto jleave0;
778 if(cmd != NULL){
779 c_cmdnotsupp(NULL);
780 cmd = NULL;
782 goto jleave;
785 /* See if we should execute the command -- if a conditional we always
786 * execute it, otherwise, check the state of cond */
787 jexec:
788 if(!(cmd->lc_argtype & ARG_F) && condstack_isskip())
789 goto jleave0;
791 /* Process the arguments to the command, depending on the type it expects */
792 if((cmd->lc_argtype & ARG_I) &&
793 !(options & (OPT_INTERACTIVE | OPT_BATCH_FLAG))){
794 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
795 cmd->lc_name);
796 goto jleave;
798 if(!(cmd->lc_argtype & ARG_M) && (options & OPT_SENDMODE)){
799 n_err(_("May not execute `%s' while sending\n"), cmd->lc_name);
800 goto jleave;
802 if(cmd->lc_argtype & ARG_R){
803 if(pstate & PS_COMPOSE_MODE){
804 /* TODO PS_COMPOSE_MODE: should allow `reply': ~:reply! */
805 n_err(_("Cannot invoke `%s' when in compose mode\n"), cmd->lc_name);
806 goto jleave;
808 /* TODO Nothing should prevent ARG_R in conjunction with
809 * TODO PS_ROBOT|_SOURCING; see a.._may_yield_control()! */
810 if(pstate & (PS_ROBOT | PS_SOURCING)){
811 n_err(_("Cannot invoke `%s' from a macro or during file inclusion\n"),
812 cmd->lc_name);
813 goto jleave;
816 if((cmd->lc_argtype & ARG_S) && !(pstate & PS_STARTED)){
817 n_err(_("May not execute `%s' during startup\n"), cmd->lc_name);
818 goto jleave;
820 if(!(cmd->lc_argtype & ARG_X) && (pstate & PS_COMPOSE_FORKHOOK)){
821 n_err(_("Cannot invoke `%s' from a hook running in a child process\n"),
822 cmd->lc_name);
823 goto jleave;
826 if((cmd->lc_argtype & ARG_A) && mb.mb_type == MB_VOID){
827 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd->lc_name);
828 goto jleave;
830 if((cmd->lc_argtype & ARG_W) && !(mb.mb_perm & MB_DELE)){
831 n_err(_("May not execute `%s' -- message file is read only\n"),
832 cmd->lc_name);
833 goto jleave;
836 if(cmd->lc_argtype & ARG_O)
837 OBSOLETE2(_("this command will be removed"), cmd->lc_name);
838 if(cmd->lc_argtype & ARG_V)
839 temporary_arg_v_store = NULL;
841 if(wysh && (cmd->lc_argtype & ARG_ARGMASK) != ARG_WYRALIST)
842 n_err(_("`wysh' prefix doesn't affect `%s'\n"), cmd->lc_name);
843 /* TODO v15: strip PS_ARGLIST_MASK off, just in case the actual command
844 * TODO doesn't use any of those list commands which strip this mask,
845 * TODO and for now we misuse bits for checking relation to history;
846 * TODO argument state should be property of a per-command carrier instead */
847 pstate &= ~PS_ARGLIST_MASK;
848 switch(cmd->lc_argtype & ARG_ARGMASK){
849 case ARG_MSGLIST:
850 /* Message list defaulting to nearest forward legal message */
851 if(n_msgvec == NULL)
852 goto je96;
853 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
854 break;
855 if(c == 0){
856 if((n_msgvec[0] = first(cmd->lc_msgflag, cmd->lc_msgmask)) != 0)
857 n_msgvec[1] = 0;
859 if(n_msgvec[0] == 0){
860 if(!(pstate & PS_HOOK_MASK))
861 printf(_("No applicable messages\n"));
862 break;
864 e = (*cmd->lc_func)(n_msgvec);
865 break;
867 case ARG_NDMLIST:
868 /* Message list with no defaults, but no error if none exist */
869 if(n_msgvec == NULL){
870 je96:
871 n_err(_("Invalid use of message list\n"));
872 break;
874 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
875 break;
876 e = (*cmd->lc_func)(n_msgvec);
877 break;
879 case ARG_STRLIST:
880 /* Just the straight string, with leading blanks removed */
881 while(whitechar(*cp))
882 ++cp;
883 e = (*cmd->lc_func)(cp);
884 break;
886 case ARG_WYSHLIST:
887 c = 1;
888 if(0){
889 /* FALLTHRU */
890 case ARG_WYRALIST:
891 c = wysh ? 1 : 0;
892 if(0){
893 case ARG_RAWLIST:
894 c = 0;
898 if((c = getrawlist((c != 0), arglist, n_NELEM(arglist), cp, line.l)) < 0){
899 n_err(_("Invalid argument list\n"));
900 break;
902 if(c < cmd->lc_minargs){
903 n_err(_("`%s' requires at least %d arg(s)\n"),
904 cmd->lc_name, cmd->lc_minargs);
905 break;
907 #undef lc_minargs
908 if(c > cmd->lc_maxargs){
909 n_err(_("`%s' takes no more than %d arg(s)\n"),
910 cmd->lc_name, cmd->lc_maxargs);
911 break;
913 #undef lc_maxargs
914 e = (*cmd->lc_func)(arglist);
915 break;
917 case ARG_NOLIST:
918 /* Just the constant zero, for exiting, eg. */
919 e = (*cmd->lc_func)(0);
920 break;
922 default:
923 DBG( n_panic(_("Implementation error: unknown argument type: %d"),
924 cmd->lc_argtype & ARG_ARGMASK); )
925 goto jleave0;
928 if(e == 0 && (cmd->lc_argtype & ARG_V) &&
929 (cp = temporary_arg_v_store) != NULL){
930 temporary_arg_v_store = NULL;
931 evp->le_new_content = cp;
932 goto jleave0;
934 if(!(cmd->lc_argtype & ARG_H))
935 evp->le_add_history = (((cmd->lc_argtype & ARG_G) ||
936 (pstate & PS_MSGLIST_GABBY)) ? TRUM1 : TRU1);
938 jleave:
939 /* C99 */{
940 bool_t reset = !(pstate & PS_ROOT);
942 pstate |= PS_ROOT;
943 ok_vset(_exit_status, (e == 0 ? "0" : "1")); /* TODO num=1 +real value! */
944 if(reset)
945 pstate &= ~PS_ROOT;
948 /* Exit the current source file on error TODO what a mess! */
949 if(e == 0)
950 pstate &= ~PS_EVAL_ERROR;
951 else{
952 pstate |= PS_EVAL_ERROR;
953 if(e < 0 || (pstate & PS_ROBOT)){ /* FIXME */
954 e = 1;
955 goto jret;
957 goto jret0;
960 if(cmd == NULL)
961 goto jret0;
962 if((cmd->lc_argtype & ARG_P) && ok_blook(autoprint))
963 if(visible(dot)){
964 line.s = savestr("type");
965 line.l = sizeof("type") -1;
966 gp = (struct a_lex_ghost*)-1; /* Avoid `ghost' interpretation */
967 goto jrestart;
970 if(!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(cmd->lc_argtype & ARG_T))
971 pstate |= PS_SAW_COMMAND;
972 jleave0:
973 pstate &= ~PS_EVAL_ERROR;
974 jret0:
975 e = 0;
976 jret:
978 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
980 NYD_LEAVE;
981 return e;
984 static struct a_lex_cmd const *
985 a_lex__firstfit(char const *comm){ /* TODO *hashtable*! linear list search!!! */
986 struct a_lex_cmd const *cp;
987 NYD2_ENTER;
989 for(cp = a_lex_cmd_tab;
990 PTRCMP(cp, <, &a_lex_cmd_tab[n_NELEM(a_lex_cmd_tab)]); ++cp)
991 if(*comm == *cp->lc_name && is_prefix(comm, cp->lc_name))
992 goto jleave;
993 cp = NULL;
994 jleave:
995 NYD2_LEAVE;
996 return cp;
999 static void
1000 a_lex_hangup(int s){
1001 NYD_X; /* Signal handler */
1002 n_UNUSED(s);
1003 /* nothing to do? */
1004 exit(EXIT_ERR);
1007 static void
1008 a_lex_onintr(int s){ /* TODO block signals while acting */
1009 NYD_X; /* Signal handler */
1010 n_UNUSED(s);
1012 safe_signal(SIGINT, a_lex_onintr);
1014 termios_state_reset();
1015 close_all_files(); /* FIXME .. of current level ONLU! */
1016 if(image >= 0){
1017 close(image);
1018 image = -1;
1021 a_lex_unstack(TRUM1);
1023 if(interrupts != 1)
1024 n_err_sighdl(_("Interrupt\n"));
1025 safe_signal(SIGPIPE, a_lex_oldpipe);
1026 siglongjmp(a_lex_srbuf, 0); /* FIXME get rid */
1029 static void
1030 a_lex_unstack(bool_t eval_error){
1031 struct a_lex_input_stack *lip;
1032 NYD_ENTER;
1034 if((lip = a_lex_input) == NULL){
1035 n_memory_reset();
1037 /* If called from a_lex_onintr(), be silent FIXME */
1038 pstate &= ~(PS_SOURCING | PS_ROBOT);
1039 if(eval_error == TRUM1 || !(pstate & PS_STARTED))
1040 goto jleave;
1041 goto jerr;
1044 if(lip->li_flags & a_LEX_SLICE){ /* TODO Temporary hack */
1045 stdin = lip->li_slice_stdin;
1046 stdout = lip->li_slice_stdout;
1047 options = lip->li_slice_options;
1048 goto jthe_slice_hack;
1051 if(lip->li_flags & a_LEX_MACRO){
1052 if(lip->li_flags & a_LEX_MACRO_FREE_DATA){
1053 char **lp;
1055 while(*(lp = &lip->li_lines[lip->li_loff]) != NULL){
1056 free(*lp);
1057 ++lip->li_loff;
1059 /* Part of lip's memory chunk, then */
1060 if(!(lip->li_flags & a_LEX_MACRO_CMD))
1061 free(lip->li_lines);
1063 }else{
1064 if(lip->li_flags & a_LEX_PIPE)
1065 /* XXX command manager should -TERM then -KILL instead of hoping
1066 * XXX for exit of provider due to EPIPE / SIGPIPE */
1067 Pclose(lip->li_file, TRU1);
1068 else
1069 Fclose(lip->li_file);
1072 if(!condstack_take(lip->li_cond)){
1073 n_err(_("Unmatched `if' at end of %s %s\n"),
1074 ((lip->li_flags & a_LEX_MACRO
1075 ? (lip->li_flags & a_LEX_MACRO_CMD ? _("command") : _("macro"))
1076 : _("`source'd file"))),
1077 lip->li_name);
1078 eval_error = TRU1;
1081 n_memory_autorec_pop(&lip->li_autorecmem[0]);
1083 jthe_slice_hack:
1084 if(lip->li_on_finalize != NULL)
1085 (*lip->li_on_finalize)(lip->li_finalize_arg);
1087 if((a_lex_input = lip->li_outer) == NULL){
1088 pstate &= ~(PS_SOURCING | PS_ROBOT);
1089 }else{
1090 if((a_lex_input->li_flags & (a_LEX_MACRO | a_LEX_SUPER_MACRO)) ==
1091 (a_LEX_MACRO | a_LEX_SUPER_MACRO))
1092 pstate &= ~PS_SOURCING;
1093 assert(pstate & PS_ROBOT);
1096 if(eval_error)
1097 goto jerr;
1098 jleave:
1099 if(lip != NULL && (lip->li_flags & a_LEX_FREE))
1100 free(lip);
1101 if(n_UNLIKELY(a_lex_input != NULL && eval_error == TRUM1))
1102 a_lex_unstack(TRUM1);
1103 NYD_LEAVE;
1104 return;
1106 jerr:
1107 if(lip != NULL){
1108 if(options & OPT_D_V)
1109 n_alert(_("Stopped %s %s due to errors%s"),
1110 (pstate & PS_STARTED
1111 ? (lip->li_flags & a_LEX_SLICE ? _("sliced in program")
1112 : (lip->li_flags & a_LEX_MACRO
1113 ? (lip->li_flags & a_LEX_MACRO_CMD
1114 ? _("evaluating command") : _("evaluating macro"))
1115 : (lip->li_flags & a_LEX_PIPE
1116 ? _("executing `source'd pipe")
1117 : _("loading `source'd file")))
1119 : (lip->li_flags & a_LEX_MACRO
1120 ? (lip->li_flags & a_LEX_MACRO_X_OPTION
1121 ? _("evaluating command line") : _("evaluating macro"))
1122 : _("loading initialization resource"))),
1123 lip->li_name,
1124 (options & OPT_DEBUG ? n_empty : _(" (enable *debug* for trace)")));
1127 if(!(options & OPT_INTERACTIVE) && !(pstate & PS_STARTED)){
1128 if(options & OPT_D_V)
1129 n_alert(_("Non-interactive, bailing out due to errors "
1130 "in startup load phase"));
1131 exit(EXIT_ERR);
1133 goto jleave;
1136 static bool_t
1137 a_lex_source_file(char const *file, bool_t silent_error){
1138 struct a_lex_input_stack *lip;
1139 size_t nlen;
1140 char *nbuf;
1141 bool_t ispipe;
1142 FILE *fip;
1143 NYD_ENTER;
1145 fip = NULL;
1147 /* Being a command argument file is space-trimmed *//* TODO v15 with
1148 * TODO WYRALIST this is no longer necessary true, and for that we
1149 * TODO don't set _PARSE_TRIMSPACE because we cannot! -> cmd_tab.h!! */
1150 #if 0
1151 ((ispipe = (!silent_error && (nlen = strlen(file)) > 0 &&
1152 file[--nlen] == '|')))
1153 #else
1154 ispipe = FAL0;
1155 if(!silent_error)
1156 for(nlen = strlen(file); nlen > 0;){
1157 char c;
1159 c = file[--nlen];
1160 if(!blankchar(c)){
1161 if(c == '|'){
1162 nbuf = savestrbuf(file, nlen);
1163 ispipe = TRU1;
1164 break;
1168 #endif
1170 if(ispipe){
1171 if((fip = Popen(nbuf /* #if 0 above = savestrbuf(file, nlen)*/, "r",
1172 ok_vlook(SHELL), NULL, COMMAND_FD_NULL)) == NULL){
1173 if(!silent_error || (options & OPT_D_V))
1174 n_perr(nbuf, 0);
1175 goto jleave;
1177 }else if((nbuf = fexpand(file, FEXP_LOCAL)) == NULL)
1178 goto jleave;
1179 else if((fip = Fopen(nbuf, "r")) == NULL){
1180 if(!silent_error || (options & OPT_D_V))
1181 n_perr(nbuf, 0);
1182 goto jleave;
1185 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1186 (nlen = strlen(nbuf) +1));
1187 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1188 lip->li_outer = a_lex_input;
1189 lip->li_file = fip;
1190 lip->li_cond = condstack_release();
1191 n_memory_autorec_push(&lip->li_autorecmem[0]);
1192 lip->li_flags = (ispipe ? a_LEX_FREE | a_LEX_PIPE : a_LEX_FREE) |
1193 (a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1194 ? a_LEX_SUPER_MACRO : 0);
1195 memcpy(lip->li_name, nbuf, nlen);
1197 pstate |= PS_SOURCING | PS_ROBOT;
1198 a_lex_input = lip;
1199 a_commands_recursive(n_LEXINPUT_NONE | n_LEXINPUT_NL_ESC);
1200 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
1201 jleave:
1202 NYD_LEAVE;
1203 return silent_error ? TRU1 : (fip != NULL);
1206 static bool_t
1207 a_lex_load(struct a_lex_input_stack *lip){
1208 bool_t rv;
1209 NYD2_ENTER;
1211 assert(!(pstate & PS_STARTED));
1212 assert(a_lex_input == NULL);
1214 /* POSIX:
1215 * Any errors in the start-up file shall either cause mailx to terminate
1216 * with a diagnostic message and a non-zero status or to continue after
1217 * writing a diagnostic message, ignoring the remainder of the lines in
1218 * the start-up file. */
1219 lip->li_cond = condstack_release();
1220 n_memory_autorec_push(&lip->li_autorecmem[0]);
1222 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
1223 pstate |= PS_ROBOT |
1224 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
1226 pstate |= PS_ROBOT | PS_SOURCING;
1227 if(options & OPT_D_V)
1228 n_err(_("Loading %s\n"), n_shexp_quote_cp(lip->li_name, FAL0));
1229 a_lex_input = lip;
1230 if(!(rv = n_commands())){
1231 if(!(options & OPT_INTERACTIVE)){
1232 if(options & OPT_D_V)
1233 n_alert(_("Non-interactive program mode, forced exit"));
1234 exit(EXIT_ERR);
1237 /* PS_EXIT handled by callers */
1238 NYD2_LEAVE;
1239 return rv;
1242 static void
1243 a_lex__cmdrecint(int sig){ /* TODO one day, we don't need it no more */
1244 NYD_X; /* Signal handler */
1245 n_UNUSED(sig);
1246 siglongjmp(a_lex_input->li_cmdrec_jmp, 1);
1249 static bool_t
1250 a_commands_recursive(enum n_lexinput_flags lif){
1251 volatile int hadint; /* TODO get rid of shitty signal stuff (see signal.c) */
1252 sighandler_type soldhdl;
1253 sigset_t sintset, soldset;
1254 struct a_lex_eval_ctx ev;
1255 bool_t rv;
1256 NYD2_ENTER;
1258 memset(&ev, 0, sizeof ev);
1260 sigfillset(&sintset);
1261 sigprocmask(SIG_BLOCK, &sintset, &soldset);
1262 hadint = FAL0;
1263 if((soldhdl = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN){
1264 safe_signal(SIGINT, &a_lex__cmdrecint);
1265 if(sigsetjmp(a_lex_input->li_cmdrec_jmp, 1)){
1266 hadint = TRU1;
1267 goto jjump;
1270 sigprocmask(SIG_SETMASK, &soldset, NULL);
1272 n_COLOUR( n_colour_env_push(); )
1273 rv = TRU1;
1274 for(;;){
1275 int n;
1277 /* Read a line of commands and handle end of file specially */
1278 ev.le_line.l = ev.le_line_size;
1279 n = n_lex_input(lif, NULL, &ev.le_line.s, &ev.le_line.l,
1280 ev.le_new_content);
1281 ev.le_line_size = (ui32_t)ev.le_line.l;
1282 ev.le_line.l = (ui32_t)n;
1284 if(n < 0)
1285 break;
1287 if(a_lex_evaluate(&ev)){
1288 rv = FAL0;
1289 break;
1291 n_memory_reset();
1293 if((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)){
1294 if(exit_status != EXIT_OK)
1295 break;
1298 jjump: /* TODO */
1299 a_lex_unstack(!rv);
1300 n_COLOUR( n_colour_env_pop(FAL0); )
1302 if(ev.le_line.s != NULL)
1303 free(ev.le_line.s);
1305 if(soldhdl != SIG_IGN)
1306 safe_signal(SIGINT, soldhdl);
1307 NYD2_LEAVE;
1308 if(hadint){
1309 sigprocmask(SIG_SETMASK, &soldset, NULL);
1310 n_raise(SIGINT);
1312 return rv;
1315 FL int
1316 c_cmdnotsupp(void *vp){
1317 NYD_ENTER;
1318 n_UNUSED(vp);
1319 n_err(_("The requested feature is not compiled in\n"));
1320 NYD_LEAVE;
1321 return 1;
1324 FL bool_t
1325 n_commands(void){ /* FIXME */
1326 struct a_lex_eval_ctx ev;
1327 int n;
1328 bool_t volatile rv;
1329 NYD_ENTER;
1331 rv = TRU1;
1333 if (!(pstate & PS_SOURCING)) {
1334 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
1335 safe_signal(SIGINT, &a_lex_onintr);
1336 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1337 safe_signal(SIGHUP, &a_lex_hangup);
1339 a_lex_oldpipe = safe_signal(SIGPIPE, SIG_IGN);
1340 safe_signal(SIGPIPE, a_lex_oldpipe);
1342 memset(&ev, 0, sizeof ev);
1344 (void)sigsetjmp(a_lex_srbuf, 1); /* FIXME get rid */
1345 for (;;) {
1346 char *temporary_orig_line; /* XXX eval_ctx.le_line not yet constant */
1348 n_COLOUR( n_colour_env_pop(TRU1); )
1350 /* TODO Unless we have our signal manager (or however we do it) child
1351 * TODO processes may have time slots where their execution isn't
1352 * TODO protected by signal handlers (in between start and setup
1353 * TODO completed). close_all_files() is only called from onintr()
1354 * TODO so those may linger possibly forever */
1355 if(!(pstate & PS_SOURCING))
1356 close_all_files();
1358 interrupts = 0;
1360 n_memory_reset();
1362 if (!(pstate & PS_SOURCING)) {
1363 char *cp;
1365 /* TODO Note: this buffer may contain a password. We should redefine
1366 * TODO the code flow which has to do that */
1367 if ((cp = termios_state.ts_linebuf) != NULL) {
1368 termios_state.ts_linebuf = NULL;
1369 termios_state.ts_linesize = 0;
1370 free(cp); /* TODO pool give-back */
1372 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1373 if (ev.le_line.l > LINESIZE * 3) {
1374 free(ev.le_line.s); /* TODO pool! but what? */
1375 ev.le_line.s = NULL;
1376 ev.le_line.l = ev.le_line_size = 0;
1380 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1381 char *cp;
1383 cp = ok_vlook(newmail);
1384 if ((options & OPT_TTYIN) && cp != NULL) {
1385 struct stat st;
1387 /* FIXME TEST WITH NOPOLL ETC. !!! */
1388 n = (cp != NULL && strcmp(cp, "nopoll"));
1389 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
1390 st.st_size > mailsize) ||
1391 (mb.mb_type == MB_MAILDIR && n != 0)) {
1392 size_t odot = PTR2SIZE(dot - message);
1393 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
1395 if (setfile(mailname,
1396 FEDIT_NEWMAIL |
1397 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
1398 exit_status |= EXIT_ERR;
1399 rv = FAL0;
1400 break;
1402 dot = message + odot;
1403 pstate |= odid;
1407 exit_status = EXIT_OK;
1410 /* Read a line of commands and handle end of file specially */
1411 jreadline:
1412 ev.le_line.l = ev.le_line_size;
1413 n = n_lex_input(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_NL_ESC, NULL,
1414 &ev.le_line.s, &ev.le_line.l, ev.le_new_content);
1415 ev.le_line_size = (ui32_t)ev.le_line.l;
1416 ev.le_line.l = (ui32_t)n;
1418 if (n < 0) {
1419 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1420 if (!(pstate & PS_ROBOT) &&
1421 (options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
1422 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1423 n_msleep(500, FAL0);
1424 continue;
1426 break;
1429 temporary_orig_line = ((pstate & PS_SOURCING) ||
1430 !(options & OPT_INTERACTIVE)) ? NULL
1431 : savestrbuf(ev.le_line.s, ev.le_line.l);
1432 pstate &= ~PS_HOOK_MASK;
1433 if (a_lex_evaluate(&ev)) {
1434 if (!(pstate & PS_STARTED)) /* TODO mess; join PS_EVAL_ERROR.. */
1435 rv = FAL0;
1436 break;
1439 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
1440 if (exit_status != EXIT_OK)
1441 break;
1442 if ((pstate & (PS_SOURCING | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
1443 exit_status = EXIT_ERR;
1444 break;
1447 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1448 if (ev.le_new_content != NULL)
1449 goto jreadline;
1450 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1451 if (temporary_orig_line != NULL)
1452 n_tty_addhist(temporary_orig_line, (ev.le_add_history != TRU1));
1455 if(pstate & PS_EXIT)
1456 break;
1459 a_lex_unstack(!rv);
1461 if (ev.le_line.s != NULL)
1462 free(ev.le_line.s);
1463 NYD_LEAVE;
1464 return rv;
1467 FL int
1468 (n_lex_input)(enum n_lexinput_flags lif, char const *prompt, char **linebuf,
1469 size_t *linesize, char const *string n_MEMORY_DEBUG_ARGS){
1470 /* TODO readline: linebuf pool!; n_lex_input should return si64_t */
1471 struct n_string xprompt;
1472 FILE *ifile;
1473 bool_t doprompt, dotty;
1474 char const *iftype;
1475 int n, nold;
1476 NYD2_ENTER;
1478 /* Special case macro mode: never need to prompt, lines have always been
1479 * unfolded already */
1480 if(!(lif & n_LEXINPUT_FORCE_STDIN) &&
1481 a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO)){
1482 if(*linebuf != NULL)
1483 free(*linebuf);
1485 if((*linebuf = a_lex_input->li_lines[a_lex_input->li_loff]) == NULL){
1486 *linesize = 0;
1487 n = -1;
1488 goto jleave;
1491 ++a_lex_input->li_loff;
1492 *linesize = strlen(*linebuf);
1493 if(!(a_lex_input->li_flags & a_LEX_MACRO_FREE_DATA))
1494 *linebuf = sbufdup(*linebuf, *linesize);
1496 iftype = (a_lex_input->li_flags & a_LEX_MACRO_X_OPTION)
1497 ? "-X OPTION"
1498 : (a_lex_input->li_flags & a_LEX_MACRO_CMD) ? "CMD" : "MACRO";
1499 n = (int)*linesize;
1500 pstate |= PS_READLINE_NL;
1501 goto jhave_dat;
1503 pstate &= ~PS_READLINE_NL;
1505 iftype = (!(pstate & PS_STARTED) ? "LOAD"
1506 : (pstate & PS_SOURCING) ? "SOURCE" : "READ");
1507 doprompt = ((pstate & (PS_STARTED | PS_ROBOT)) == PS_STARTED &&
1508 (options & OPT_INTERACTIVE));
1509 dotty = (doprompt && !ok_blook(line_editor_disable));
1510 if(!doprompt)
1511 lif |= n_LEXINPUT_PROMPT_NONE;
1512 else{
1513 if(!dotty)
1514 n_string_creat_auto(&xprompt);
1515 if(prompt == NULL)
1516 lif |= n_LEXINPUT_PROMPT_EVAL;
1519 /* Ensure stdout is flushed first anyway */
1520 if(!dotty && (lif & n_LEXINPUT_PROMPT_NONE))
1521 fflush(stdout);
1523 ifile = (a_lex_input != NULL) ? a_lex_input->li_file : stdin;
1524 if(ifile == NULL){
1525 assert((pstate & PS_COMPOSE_FORKHOOK) &&
1526 a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO));
1527 ifile = stdin;
1530 for(nold = n = 0;;){
1531 if(dotty){
1532 assert(ifile == stdin);
1533 if(string != NULL && (n = (int)strlen(string)) > 0){
1534 if(*linesize > 0)
1535 *linesize += n +1;
1536 else
1537 *linesize = (size_t)n + LINESIZE +1;
1538 *linebuf = (n_realloc)(*linebuf, *linesize n_MEMORY_DEBUG_ARGSCALL);
1539 memcpy(*linebuf, string, (size_t)n +1);
1541 string = NULL;
1542 /* TODO if nold>0, don't redisplay the entire line!
1543 * TODO needs complete redesign ... */
1544 n = (n_tty_readline)(lif, prompt, linebuf, linesize, n
1545 n_MEMORY_DEBUG_ARGSCALL);
1546 }else{
1547 if(!(lif & n_LEXINPUT_PROMPT_NONE)){
1548 n_tty_create_prompt(&xprompt, prompt, lif);
1549 if(xprompt.s_len > 0){
1550 fwrite(xprompt.s_dat, 1, xprompt.s_len, stdout);
1551 fflush(stdout);
1555 n = (readline_restart)(ifile, linebuf, linesize, n
1556 n_MEMORY_DEBUG_ARGSCALL);
1558 if(n > 0 && nold > 0){
1559 int i = 0;
1560 char const *cp = *linebuf + nold;
1562 while(blankspacechar(*cp) && nold + i < n)
1563 ++cp, ++i;
1564 if(i > 0){
1565 memmove(*linebuf + nold, cp, n - nold - i);
1566 n -= i;
1567 (*linebuf)[n] = '\0';
1572 if(n <= 0)
1573 break;
1575 /* POSIX says:
1576 * An unquoted <backslash> at the end of a command line shall
1577 * be discarded and the next line shall continue the command */
1578 if(!(lif & n_LEXINPUT_NL_ESC) || (*linebuf)[n - 1] != '\\'){
1579 if(dotty)
1580 pstate |= PS_READLINE_NL;
1581 break;
1583 /* Definitely outside of quotes, thus the quoting rules are so that an
1584 * uneven number of successive backslashs at EOL is a continuation */
1585 if(n > 1){
1586 size_t i, j;
1588 for(j = 1, i = (size_t)n - 1; i-- > 0; ++j)
1589 if((*linebuf)[i] != '\\')
1590 break;
1591 if(!(j & 1))
1592 break;
1594 (*linebuf)[nold = --n] = '\0';
1595 lif |= n_LEXINPUT_NL_FOLLOW;
1598 if(n < 0)
1599 goto jleave;
1600 (*linebuf)[*linesize = n] = '\0';
1602 jhave_dat:
1603 #if 0
1604 if(lif & n_LEXINPUT_DROP_TRAIL_SPC){
1605 char *cp, c;
1606 size_t i;
1608 for(cp = &(*linebuf)[i = (size_t)n];; --i){
1609 c = *--cp;
1610 if(!blankspacechar(c))
1611 break;
1613 (*linebuf)[n = (int)i] = '\0';
1616 if(lif & n_LEXINPUT_DROP_LEAD_SPC){
1617 char *cp, c;
1618 size_t j, i;
1620 for(cp = &(*linebuf)[0], j = (size_t)n, i = 0; i < j; ++i){
1621 c = *cp++;
1622 if(!blankspacechar(c))
1623 break;
1625 if(i > 0){
1626 memcpy(&(*linebuf)[0], &(*linebuf)[i], j -= i);
1627 (*linebuf)[n = (int)j] = '\0';
1630 #endif /* 0 (notyet - must take care for backslash escaped space) */
1632 if(options & OPT_D_VV)
1633 n_err(_("%s %d bytes <%s>\n"), iftype, n, *linebuf);
1634 jleave:
1635 if (pstate & PS_PSTATE_PENDMASK)
1636 a_lex_update_pstate();
1638 /* TODO We need to special case a_LEX_SLICE, since that is not managed by us
1639 * TODO but only established from the outside and we need to drop this
1640 * TODO overlay context somehow */
1641 if(n < 0 && a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SLICE))
1642 a_lex_unstack(FAL0);
1643 NYD2_LEAVE;
1644 return n;
1647 FL char *
1648 n_lex_input_cp(enum n_lexinput_flags lif, char const *prompt,
1649 char const *string){
1650 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1651 size_t linesize;
1652 char *linebuf, *rv;
1653 int n;
1654 NYD2_ENTER;
1656 linesize = 0;
1657 linebuf = NULL;
1658 rv = NULL;
1660 n = n_lex_input(lif, prompt, &linebuf, &linesize, string);
1661 if(n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
1662 (lif & n_LEXINPUT_HIST_ADD) && (options & OPT_INTERACTIVE))
1663 n_tty_addhist(rv, ((lif & n_LEXINPUT_HIST_GABBY) != 0));
1665 if(linebuf != NULL)
1666 free(linebuf);
1667 NYD2_LEAVE;
1668 return rv;
1671 FL int
1672 c_read(void *v){ /* TODO IFS? how? -r */
1673 char const **argv, *cp, *cp2;
1674 int rv;
1675 NYD2_ENTER;
1677 rv = 0;
1678 for(argv = v; (cp = *argv++) != NULL;)
1679 if(!n_shexp_is_valid_varname(cp)){
1680 n_err(_("`read': not a valid variable name: %s\n"),
1681 n_shexp_quote_cp(cp, FAL0));
1682 rv = 1;
1684 if(rv)
1685 goto jleave;
1687 cp = n_lex_input_cp(((pstate & PS_COMPOSE_MODE
1688 ? n_LEXINPUT_CTX_COMPOSE : n_LEXINPUT_CTX_DEFAULT) |
1689 n_LEXINPUT_FORCE_STDIN | n_LEXINPUT_NL_ESC |
1690 n_LEXINPUT_PROMPT_NONE /* XXX POSIX: PS2: yes! */),
1691 NULL, NULL);
1692 if(cp == NULL)
1693 cp = n_empty;
1695 for(argv = v; *argv != NULL; ++argv){
1696 char c;
1698 while(blankchar(*cp))
1699 ++cp;
1700 if(*cp == '\0')
1701 break;
1703 /* The last variable gets the remaining line less trailing IFS */
1704 if(argv[1] == NULL){
1705 for(cp2 = cp; *cp2 != '\0'; ++cp2)
1707 for(; cp2 > cp; --cp2){
1708 c = cp2[-1];
1709 if(!blankchar(c))
1710 break;
1712 }else
1713 for(cp2 = cp; (c = *++cp2) != '\0';)
1714 if(blankchar(c))
1715 break;
1717 vok_vset(*argv, savestrbuf(cp, PTR2SIZE(cp2 - cp)));
1718 cp = cp2;
1721 /* Set the remains to the empty string */
1722 for(; *argv != NULL; ++argv)
1723 vok_vset(*argv, "");
1725 rv = 0;
1726 jleave:
1727 NYD2_LEAVE;
1728 return rv;
1731 FL void
1732 n_load(char const *name){
1733 struct a_lex_input_stack *lip;
1734 size_t i;
1735 FILE *fip;
1736 NYD_ENTER;
1738 if(name == NULL || *name == '\0' || (fip = Fopen(name, "r")) == NULL)
1739 goto jleave;
1741 i = strlen(name) +1;
1742 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) + i);
1743 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1744 lip->li_file = fip;
1745 lip->li_flags = a_LEX_FREE;
1746 memcpy(lip->li_name, name, i);
1748 a_lex_load(lip);
1749 pstate &= ~PS_EXIT;
1750 jleave:
1751 NYD_LEAVE;
1754 FL void
1755 n_load_Xargs(char const **lines, size_t cnt){
1756 static char const name[] = "-X";
1758 ui8_t buf[sizeof(struct a_lex_input_stack) + sizeof name];
1759 char const *srcp, *xsrcp;
1760 char *cp;
1761 size_t imax, i, len;
1762 struct a_lex_input_stack *lip;
1763 NYD_ENTER;
1765 lip = (void*)buf;
1766 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1767 lip->li_flags = a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1768 a_LEX_MACRO_X_OPTION | a_LEX_SUPER_MACRO;
1769 memcpy(lip->li_name, name, sizeof name);
1771 /* The problem being that we want to support reverse solidus newline
1772 * escaping also within multiline -X, i.e., POSIX says:
1773 * An unquoted <backslash> at the end of a command line shall
1774 * be discarded and the next line shall continue the command
1775 * Therefore instead of "lip->li_lines = n_UNCONST(lines)", duplicate the
1776 * entire lines array and set _MACRO_FREE_DATA */
1777 imax = cnt + 1;
1778 lip->li_lines = smalloc(sizeof(*lip->li_lines) * imax);
1780 /* For each of the input lines.. */
1781 for(i = len = 0, cp = NULL; cnt > 0;){
1782 bool_t keep;
1783 size_t j;
1785 if((j = strlen(srcp = *lines)) == 0){
1786 ++lines, --cnt;
1787 continue;
1790 /* Separate one line from a possible multiline input string */
1791 if((xsrcp = memchr(srcp, '\n', j)) != NULL){
1792 *lines = &xsrcp[1];
1793 j = PTR2SIZE(xsrcp - srcp);
1794 }else
1795 ++lines, --cnt;
1797 /* The (separated) string may itself indicate soft newline escaping */
1798 if((keep = (srcp[j - 1] == '\\'))){
1799 size_t xj, xk;
1801 /* Need an uneven number of reverse solidus */
1802 for(xk = 1, xj = j - 1; xj-- > 0; ++xk)
1803 if(srcp[xj] != '\\')
1804 break;
1805 if(xk & 1)
1806 --j;
1807 else
1808 keep = FAL0;
1811 /* Strip any leading WS from follow lines, then */
1812 if(cp != NULL)
1813 while(j > 0 && blankspacechar(*srcp))
1814 ++srcp, --j;
1816 if(j > 0){
1817 if(i + 2 >= imax){ /* TODO need a vector (main.c, here, ++) */
1818 imax += 4;
1819 lip->li_lines = n_realloc(lip->li_lines, sizeof(*lip->li_lines) *
1820 imax);
1822 lip->li_lines[i] = cp = n_realloc(cp, len + j +1);
1823 memcpy(&cp[len], srcp, j);
1824 cp[len += j] = '\0';
1826 if(!keep)
1827 ++i;
1829 if(!keep)
1830 cp = NULL, len = 0;
1832 if(cp != NULL){
1833 assert(i + 1 < imax);
1834 lip->li_lines[i++] = cp;
1836 lip->li_lines[i] = NULL;
1838 a_lex_load(lip);
1839 if(pstate & PS_EXIT)
1840 exit(EXIT_OK);
1841 NYD_LEAVE;
1844 FL int
1845 c_source(void *v){
1846 int rv;
1847 NYD_ENTER;
1849 rv = (a_lex_source_file(*(char**)v, FAL0) == TRU1) ? 0 : 1;
1850 NYD_LEAVE;
1851 return rv;
1854 FL int
1855 c_source_if(void *v){ /* XXX obsolete?, support file tests in `if' etc.! */
1856 int rv;
1857 NYD_ENTER;
1859 rv = (a_lex_source_file(*(char**)v, TRU1) != FAL0) ? 0 : 1;
1860 NYD_LEAVE;
1861 return rv;
1864 FL bool_t
1865 n_source_macro(enum n_lexinput_flags lif, char const *name, char **lines,
1866 void (*on_finalize)(void*), void *finalize_arg){
1867 struct a_lex_input_stack *lip;
1868 size_t i;
1869 int rv;
1870 NYD_ENTER;
1872 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1873 (i = strlen(name) +1));
1874 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1875 lip->li_outer = a_lex_input;
1876 lip->li_file = NULL;
1877 lip->li_cond = condstack_release();
1878 n_memory_autorec_push(&lip->li_autorecmem[0]);
1879 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1880 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1881 ? a_LEX_SUPER_MACRO : 0);
1882 lip->li_lines = lines;
1883 lip->li_on_finalize = on_finalize;
1884 lip->li_finalize_arg = finalize_arg;
1885 memcpy(lip->li_name, name, i);
1887 pstate |= PS_ROBOT;
1888 a_lex_input = lip;
1889 rv = a_commands_recursive(lif);
1890 NYD_LEAVE;
1891 return rv;
1894 FL bool_t
1895 n_source_command(enum n_lexinput_flags lif, char const *cmd){
1896 struct a_lex_input_stack *lip;
1897 size_t i, ial;
1898 bool_t rv;
1899 NYD_ENTER;
1901 i = strlen(cmd) +1;
1902 ial = n_ALIGN(i);
1904 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1905 ial + 2*sizeof(char*));
1906 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1907 lip->li_outer = a_lex_input;
1908 lip->li_cond = condstack_release();
1909 n_memory_autorec_push(&lip->li_autorecmem[0]);
1910 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_CMD |
1911 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1912 ? a_LEX_SUPER_MACRO : 0);
1913 lip->li_lines = (void*)&lip->li_name[ial];
1914 memcpy(lip->li_lines[0] = &lip->li_name[0], cmd, i);
1915 lip->li_lines[1] = NULL;
1917 pstate |= PS_ROBOT;
1918 a_lex_input = lip;
1919 rv = a_commands_recursive(lif);
1920 NYD_LEAVE;
1921 return rv;
1924 FL void
1925 n_source_slice_hack(char const *cmd, FILE *new_stdin, FILE *new_stdout,
1926 ui32_t new_options, void (*on_finalize)(void*), void *finalize_arg){
1927 struct a_lex_input_stack *lip;
1928 size_t i;
1929 NYD_ENTER;
1931 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1932 (i = strlen(cmd) +1));
1933 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1934 lip->li_outer = a_lex_input;
1935 lip->li_file = new_stdin;
1936 lip->li_flags = a_LEX_FREE | a_LEX_SLICE;
1937 lip->li_on_finalize = on_finalize;
1938 lip->li_finalize_arg = finalize_arg;
1939 lip->li_slice_stdin = stdin;
1940 lip->li_slice_stdout = stdout;
1941 lip->li_slice_options = options;
1942 memcpy(lip->li_name, cmd, i);
1944 stdin = new_stdin;
1945 stdout = new_stdout;
1946 options = new_options;
1947 pstate |= PS_ROBOT;
1948 a_lex_input = lip;
1949 NYD_LEAVE;
1952 FL void
1953 n_source_slice_hack_remove_after_jump(void){
1954 a_lex_unstack(FAL0);
1957 FL bool_t
1958 n_source_may_yield_control(void){
1959 return ((options & OPT_INTERACTIVE) &&
1960 (pstate & PS_STARTED) &&
1961 (!(pstate & PS_ROBOT) ||
1962 /* But: ok for ~:, yet: unless in a hook.
1963 * TODO This is obviously hacky in that it depends on _input_stack not
1964 * TODO loosing any flags when creating new contexts... Maybe this
1965 * TODO function should instead walk all up the context stack when
1966 * TODO there is one, and verify neither level prevents yielding! */
1967 ((pstate & PS_COMPOSE_MODE) && (a_lex_input == NULL ||
1968 !(a_lex_input->li_flags & a_LEX_SLICE)))) &&
1969 (a_lex_input == NULL || a_lex_input->li_outer == NULL));
1972 /* s-it-mode */