Show the Content-Description:, as applicable
[s-mailx.git] / lex_input.c
blobdc98effd7d98aabd9494897b2ec29c5f7ae0fa04
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 - 2016 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 /* `quit' command */
156 static int a_lex_c_quit(void *v);
158 /* Print the binaries version number */
159 static int a_lex_c_version(void *v);
161 static int a_lex__version_cmp(void const *s1, void const *s2);
163 /* PS_STATE_PENDMASK requires some actions */
164 static void a_lex_update_pstate(void);
166 /* Evaluate a single command.
167 * .le_add_history and .le_new_content will be updated upon success.
168 * Command functions return 0 for success, 1 for error, and -1 for abort.
169 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
170 static int a_lex_evaluate(struct a_lex_eval_ctx *evp);
172 /* Get first-fit, or NULL */
173 static struct a_lex_cmd const *a_lex__firstfit(char const *comm);
175 /* Branch here on hangup signal and simulate "exit" */
176 static void a_lex_hangup(int s);
178 /* The following gets called on receipt of an interrupt. Close all open files
179 * except 0, 1, 2, and the temporary. Also, unstack all source files */
180 static void a_lex_onintr(int s);
182 /* Pop the current input back to the previous level. Update the program state.
183 * If the argument is TRUM1 then we don't alert and error out if the stack
184 * doesn't exist at all */
185 static void a_lex_unstack(bool_t eval_error);
187 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
188 static bool_t a_lex_source_file(char const *file, bool_t silent_error);
190 /* System resource file load()ing or -X command line option array traversal */
191 static bool_t a_lex_load(struct a_lex_input_stack *lip);
193 /* A simplified command loop for recursed state machines */
194 static bool_t a_commands_recursive(enum n_lexinput_flags lif);
196 /* List of all commands, and list of commands which are specially treated
197 * and deduced in _evaluate(), but we need a list for _c_list() and
198 * print_comm_docstr() */
199 #ifdef HAVE_DOCSTRINGS
200 # define DS(S) , S
201 #else
202 # define DS(S)
203 #endif
204 static struct a_lex_cmd const a_lex_cmd_tab[] = {
205 #include "cmd_tab.h"
207 a_lex_special_cmd_tab[] = {
208 { "#", NULL, 0, 0, 0
209 DS(N_("Comment command: ignore remaining (continuable) line")) },
210 { "-", NULL, 0, 0, 0
211 DS(N_("Print out the preceding message")) }
213 #undef DS
215 static char *
216 a_lex_isolate(char const *comm){
217 NYD2_ENTER;
218 while(*comm != '\0' &&
219 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
220 ++comm;
221 NYD2_LEAVE;
222 return n_UNCONST(comm);
225 static int
226 a_lex_c_ghost(void *v){
227 struct a_lex_ghost *lgp, *gp;
228 size_t i, cl, nl;
229 char *cp;
230 char const **argv;
231 NYD_ENTER;
233 argv = v;
235 /* Show the list? */
236 if(*argv == NULL){
237 FILE *fp;
239 if((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
240 fp = stdout;
242 for(i = 0, gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
243 fprintf(fp, "wysh ghost %s %s\n",
244 gp->lg_name, n_shexp_quote_cp(gp->lg_cmd.s, TRU1));
246 if(fp != stdout){
247 page_or_print(fp, i);
248 Fclose(fp);
250 goto jleave;
253 /* Verify the ghost name is a valid one */
254 if(*argv[0] == '\0' || *a_lex_isolate(argv[0]) != '\0'){
255 n_err(_("`ghost': can't canonicalize %s\n"),
256 n_shexp_quote_cp(argv[0], FAL0));
257 v = NULL;
258 goto jleave;
261 /* Show command of single ghost? */
262 if(argv[1] == NULL){
263 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
264 if(!strcmp(argv[0], gp->lg_name)){
265 printf("wysh ghost %s %s\n",
266 gp->lg_name, n_shexp_quote_cp(gp->lg_cmd.s, TRU1));
267 goto jleave;
269 n_err(_("`ghost': no such alias: %s\n"), argv[0]);
270 v = NULL;
271 goto jleave;
274 /* Define command for ghost: verify command content */
275 for(cl = 0, i = 1; (cp = n_UNCONST(argv[i])) != NULL; ++i)
276 if(*cp != '\0')
277 cl += strlen(cp) +1; /* SP or NUL */
278 if(cl == 0){
279 n_err(_("`ghost': empty command arguments after %s\n"), argv[0]);
280 v = NULL;
281 goto jleave;
284 /* If the ghost already exists, recreate */
285 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL; lgp = gp, gp = gp->lg_next)
286 if(!strcmp(gp->lg_name, argv[0])){
287 if(lgp != NULL)
288 lgp->lg_next = gp->lg_next;
289 else
290 a_lex_ghosts = gp->lg_next;
291 free(gp);
292 break;
295 nl = strlen(argv[0]) +1;
296 gp = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_ghost, lg_name) + nl + cl);
297 gp->lg_next = a_lex_ghosts;
298 a_lex_ghosts = gp;
299 memcpy(gp->lg_name, argv[0], nl);
300 cp = gp->lg_cmd.s = gp->lg_name + nl;
301 gp->lg_cmd.l = --cl;
303 while(*++argv != NULL)
304 if((i = strlen(*argv)) > 0){
305 memcpy(cp, *argv, i);
306 cp += i;
307 *cp++ = ' ';
309 *--cp = '\0';
310 jleave:
311 NYD_LEAVE;
312 return v == NULL;
315 static int
316 a_lex_c_unghost(void *v){
317 struct a_lex_ghost *lgp, *gp;
318 char const **argv, *cp;
319 int rv;
320 NYD_ENTER;
322 rv = 0;
323 argv = v;
325 while((cp = *argv++) != NULL){
326 if(cp[0] == '*' && cp[1] == '\0'){
327 while((gp = a_lex_ghosts) != NULL){
328 a_lex_ghosts = gp->lg_next;
329 free(gp);
331 }else{
332 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL;
333 lgp = gp, gp = gp->lg_next)
334 if(!strcmp(gp->lg_name, cp)){
335 if(lgp != NULL)
336 lgp->lg_next = gp->lg_next;
337 else
338 a_lex_ghosts = gp->lg_next;
339 free(gp);
340 goto jouter;
342 n_err(_("`unghost': no such alias: %s\n"),
343 n_shexp_quote_cp(cp, FAL0));
344 rv = 1;
345 jouter: ;
348 NYD_LEAVE;
349 return rv;
352 static char const *
353 a_lex_cmdinfo(struct a_lex_cmd const *lcp){
354 struct n_string rvb, *rv;
355 char const *cp;
356 NYD2_ENTER;
358 rv = n_string_creat_auto(&rvb);
359 rv = n_string_reserve(rv, 80);
361 switch(lcp->lc_argtype & ARG_ARGMASK){
362 case ARG_MSGLIST: cp = N_("message-list"); break;
363 case ARG_STRLIST: cp = N_("string data"); break;
364 case ARG_RAWLIST: cp = N_("old-style quoting"); break;
365 case ARG_NOLIST: cp = N_("no arguments"); break;
366 case ARG_NDMLIST: cp = N_("message-list (no default)"); break;
367 case ARG_WYSHLIST: cp = N_("sh(1)ell-style quoting"); break;
368 default: cp = N_("`wysh' for sh(1)ell-style quoting"); break;
370 rv = n_string_push_cp(rv, V_(cp));
372 if(lcp->lc_argtype & (ARG_A | ARG_I | ARG_M | ARG_R | ARG_S | ARG_X))
373 rv = n_string_push_buf(rv, " |", 2);
375 if(lcp->lc_argtype & ARG_A)
376 rv = n_string_push_cp(rv, _(" needs box"));
377 if(lcp->lc_argtype & ARG_I)
378 rv = n_string_push_cp(rv, _(" only interactive"));
379 if(lcp->lc_argtype & ARG_M)
380 rv = n_string_push_cp(rv, _(" send mode"));
381 if(lcp->lc_argtype & ARG_R)
382 rv = n_string_push_cp(rv, _(" no compose mode"));
383 if(lcp->lc_argtype & ARG_S)
384 rv = n_string_push_cp(rv, _(" after startup"));
385 if(lcp->lc_argtype & ARG_X)
386 rv = n_string_push_cp(rv, _(" subprocess"));
388 cp = n_string_cp(rv);
389 NYD2_LEAVE;
390 return cp;
393 static int
394 a_lex_c_list(void *v){
395 FILE *fp;
396 struct a_lex_cmd const **cpa, *cp, **cursor;
397 size_t l, i;
398 NYD_ENTER;
400 i = n_NELEM(a_lex_cmd_tab) + n_NELEM(a_lex_special_cmd_tab) +1;
401 cpa = salloc(sizeof(cp) * i);
403 for(i = 0; i < n_NELEM(a_lex_cmd_tab); ++i)
404 cpa[i] = &a_lex_cmd_tab[i];
405 /* C99 */{
406 size_t j;
408 for(j = 0; j < n_NELEM(a_lex_special_cmd_tab); ++i, ++j)
409 cpa[i] = &a_lex_special_cmd_tab[j];
411 cpa[i] = NULL;
413 /* C99 */{
414 char const *xcp = v;
416 if(*xcp == '\0')
417 qsort(cpa, i, sizeof(xcp), &a_lex__pcmd_cmp);
420 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
421 fp = stdout;
423 fprintf(fp, _("Commands are:\n"));
424 l = 1;
425 for(i = 0, cursor = cpa; (cp = *cursor++) != NULL;){
426 if(cp->lc_func == &c_cmdnotsupp)
427 continue;
428 if(options & OPT_D_V){
429 fprintf(fp, "%s\n", cp->lc_name);
430 ++l;
431 #ifdef HAVE_DOCSTRINGS
432 fprintf(fp, " : %s\n", V_(cp->lc_doc));
433 ++l;
434 #endif
435 fprintf(fp, " : %s\n", a_lex_cmdinfo(cp));
436 ++l;
437 }else{
438 size_t j = strlen(cp->lc_name) + 2;
440 if((i += j) > 72){
441 i = j;
442 fprintf(fp, "\n");
443 ++l;
445 fprintf(fp, (*cursor != NULL ? "%s, " : "%s\n"), cp->lc_name);
449 if(fp != stdout){
450 page_or_print(fp, l);
451 Fclose(fp);
453 NYD_LEAVE;
454 return 0;
457 static int
458 a_lex__pcmd_cmp(void const *s1, void const *s2){
459 struct a_lex_cmd const * const *cp1, * const *cp2;
460 int rv;
461 NYD2_ENTER;
463 cp1 = s1;
464 cp2 = s2;
465 rv = strcmp((*cp1)->lc_name, (*cp2)->lc_name);
466 NYD2_LEAVE;
467 return rv;
470 static int
471 a_lex_c_help(void *v){
472 int rv;
473 char *arg;
474 NYD_ENTER;
476 /* Help for a single command? */
477 if((arg = *(char**)v) != NULL){
478 struct a_lex_ghost const *gp;
479 struct a_lex_cmd const *lcp, *lcpmax;
481 /* Ghosts take precedence */
482 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
483 if(!strcmp(arg, gp->lg_name)){
484 printf("%s -> ", arg);
485 arg = gp->lg_cmd.s;
486 break;
489 lcpmax = &(lcp = a_lex_cmd_tab)[n_NELEM(a_lex_cmd_tab)];
490 jredo:
491 for(; lcp < lcpmax; ++lcp){
492 if(is_prefix(arg, lcp->lc_name)){
493 fputs(arg, stdout);
494 if(strcmp(arg, lcp->lc_name))
495 printf(" (%s)", lcp->lc_name);
496 }else
497 continue;
499 #ifdef HAVE_DOCSTRINGS
500 printf(": %s", V_(lcp->lc_doc));
501 #endif
502 if(options & OPT_D_V)
503 printf("\n : %s", a_lex_cmdinfo(lcp));
504 putchar('\n');
505 rv = 0;
506 goto jleave;
509 if(PTRCMP(lcpmax, ==, &a_lex_cmd_tab[n_NELEM(a_lex_cmd_tab)])){
510 lcpmax = &(lcp =
511 a_lex_special_cmd_tab)[n_NELEM(a_lex_special_cmd_tab)];
512 goto jredo;
515 if(gp != NULL){
516 printf("%s\n", n_shexp_quote_cp(arg, TRU1));
517 rv = 0;
518 }else{
519 n_err(_("Unknown command: `%s'\n"), arg);
520 rv = 1;
522 }else{
523 /* Very ugly, but take care for compiler supported string lengths :( */
524 fputs(progname, stdout);
525 fputs(_(
526 " commands -- <msglist> denotes message specifications,\n"
527 "e.g., 1-5, :n or ., separated by spaces:\n"), stdout);
528 fputs(_(
529 "\n"
530 "type <msglist> type (alias: `print') messages (honour `retain' etc.)\n"
531 "Type <msglist> like `type' but always show all headers\n"
532 "next goto and type next message\n"
533 "from <msglist> (search and) print header summary for the given list\n"
534 "headers header summary for messages surrounding \"dot\"\n"
535 "delete <msglist> delete messages (can be `undelete'd)\n"),
536 stdout);
538 fputs(_(
539 "\n"
540 "save <msglist> folder append messages to folder and mark as saved\n"
541 "copy <msglist> folder like `save', but don't mark them (`move' moves)\n"
542 "write <msglist> file write message contents to file (prompts for parts)\n"
543 "Reply <msglist> reply to message senders only\n"
544 "reply <msglist> like `Reply', but address all recipients\n"
545 "Lreply <msglist> forced mailing-list `reply' (see `mlist')\n"),
546 stdout);
548 fputs(_(
549 "\n"
550 "mail <recipients> compose a mail for the given recipients\n"
551 "file folder change to another mailbox\n"
552 "File folder like `file', but open readonly\n"
553 "quit quit and apply changes to the current mailbox\n"
554 "xit or exit like `quit', but discard changes\n"
555 "!shell command shell escape\n"
556 "list [<anything>] all available commands [in search order]\n"),
557 stdout);
559 rv = (ferror(stdout) != 0);
561 jleave:
562 NYD_LEAVE;
563 return rv;
566 static int
567 a_lex_c_quit(void *v){
568 NYD_ENTER;
569 n_UNUSED(v);
571 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
572 * Otherwise return -1 to abort command loop */
573 pstate |= PS_EXIT;
574 NYD_LEAVE;
575 return 0;
578 static int
579 a_lex_c_version(void *v){
580 int longest, rv;
581 char *iop;
582 char const *cp, **arr;
583 size_t i, i2;
584 NYD_ENTER;
585 n_UNUSED(v);
587 printf(_("%s version %s\nFeatures included (+) or not (-)\n"),
588 uagent, ok_vlook(version));
590 /* *features* starts with dummy byte to avoid + -> *folder* expansions */
591 i = strlen(cp = &ok_vlook(features)[1]) +1;
592 iop = salloc(i);
593 memcpy(iop, cp, i);
595 arr = salloc(sizeof(cp) * VAL_FEATURES_CNT);
596 for(longest = 0, i = 0; (cp = n_strsep(&iop, ',', TRU1)) != NULL; ++i){
597 arr[i] = cp;
598 i2 = strlen(cp);
599 longest = n_MAX(longest, (int)i2);
601 qsort(arr, i, sizeof(cp), &a_lex__version_cmp);
603 for(++longest, i2 = 0; i-- > 0;){
604 cp = *(arr++);
605 printf("%-*s ", longest, cp);
606 i2 += longest;
607 if(UICMP(z, ++i2 + longest, >=, scrnwidth) || i == 0){
608 i2 = 0;
609 putchar('\n');
613 if((rv = ferror(stdout) != 0))
614 clearerr(stdout);
615 NYD_LEAVE;
616 return rv;
619 static int
620 a_lex__version_cmp(void const *s1, void const *s2){
621 char const * const *cp1, * const *cp2;
622 int rv;
623 NYD2_ENTER;
625 cp1 = s1;
626 cp2 = s2;
627 rv = strcmp(&(*cp1)[1], &(*cp2)[1]);
628 NYD2_LEAVE;
629 return rv;
632 static void
633 a_lex_update_pstate(void){
634 NYD_ENTER;
636 if(pstate & PS_SIGWINCH_PEND){
637 char buf[32];
639 snprintf(buf, sizeof buf, "%d", scrnwidth);
640 ok_vset(COLUMNS, buf);
641 snprintf(buf, sizeof buf, "%d", scrnheight);
642 ok_vset(LINES, buf);
645 pstate &= ~PS_PSTATE_PENDMASK;
646 NYD_LEAVE;
649 static int
650 a_lex_evaluate(struct a_lex_eval_ctx *evp){
651 /* xxx old style(9), but also old code */
652 struct str line;
653 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
654 struct a_lex_ghost *gp;
655 struct a_lex_cmd const *cmd;
656 int c, e;
657 bool_t wysh;
658 NYD_ENTER;
660 wysh = FAL0;
661 e = 1;
662 cmd = NULL;
663 gp = NULL;
664 line = evp->le_line; /* XXX don't change original (buffer pointer) */
665 assert(line.s[line.l] == '\0');
666 evp->le_add_history = FAL0;
667 evp->le_new_content = NULL;
669 /* Command ghosts that refer to shell commands or macro expansion restart */
670 jrestart:
672 /* Strip the white space away from end and beginning of command */
673 if(line.l > 0){
674 size_t i = line.l;
676 for(cp = &line.s[i -1]; spacechar(*cp); --cp)
677 --i;
678 line.l = i;
680 for(cp = line.s; spacechar(*cp); ++cp)
682 line.l -= PTR2SIZE(cp - line.s);
684 /* Ignore null commands (comments) */
685 if(*cp == '#')
686 goto jleave0;
688 /* Handle ! differently to get the correct lexical conventions */
689 arglist[0] = cp;
690 if(*cp == '!')
691 ++cp;
692 /* Isolate the actual command; since it may not necessarily be
693 * separated from the arguments (as in `p1') we need to duplicate it to
694 * be able to create a NUL terminated version.
695 * We must be aware of several special one letter commands here */
696 else if((cp = a_lex_isolate(cp)) == arglist[0] &&
697 (*cp == '|' || *cp == '~' || *cp == '?'))
698 ++cp;
699 c = (int)PTR2SIZE(cp - arglist[0]);
700 line.l -= c;
701 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
702 memcpy(word, arglist[0], c);
703 word[c] = '\0';
705 /* Look up the command; if not found, bitch.
706 * Normally, a blank command would map to the first command in the
707 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
708 * confusion; act just the same for ghosts */
709 if(*word == '\0'){
710 if((pstate & PS_ROBOT) || gp != NULL)
711 goto jleave0;
712 cmd = a_lex_cmd_tab + 0;
713 goto jexec;
716 /* XXX It may be the argument parse adjuster */
717 if(!wysh && c == sizeof("wysh") -1 && !asccasecmp(word, "wysh")){
718 wysh = TRU1;
719 line.s = cp;
720 goto jrestart;
723 /* If this is the first evaluation, check command ghosts */
724 if(gp == NULL){
725 /* TODO relink list head, so it's sorted on usage over time?
726 * TODO in fact, there should be one hashmap over all commands and ghosts
727 * TODO so that the lookup could be made much more efficient than it is
728 * TODO now (two adjacent list searches! */
729 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
730 if(!strcmp(word, gp->lg_name)){
731 if(line.l > 0){
732 size_t i;
734 i = gp->lg_cmd.l;
735 line.s = salloc(i + line.l +1);
736 memcpy(line.s, gp->lg_cmd.s, i);
737 memcpy(line.s + i, cp, line.l);
738 line.s[i += line.l] = '\0';
739 line.l = i;
740 }else{
741 line.s = gp->lg_cmd.s;
742 line.l = gp->lg_cmd.l;
744 goto jrestart;
748 if((cmd = a_lex__firstfit(word)) == NULL || cmd->lc_func == &c_cmdnotsupp){
749 bool_t s;
751 if(!(s = condstack_isskip()) || (options & OPT_D_V))
752 n_err(_("Unknown command%s: `%s'\n"),
753 (s ? _(" (ignored due to `if' condition)") : n_empty), word);
754 if(s)
755 goto jleave0;
756 if(cmd != NULL){
757 c_cmdnotsupp(NULL);
758 cmd = NULL;
760 goto jleave;
763 /* See if we should execute the command -- if a conditional we always
764 * execute it, otherwise, check the state of cond */
765 jexec:
766 if(!(cmd->lc_argtype & ARG_F) && condstack_isskip())
767 goto jleave0;
769 /* Process the arguments to the command, depending on the type it expects */
770 if((cmd->lc_argtype & ARG_I) &&
771 !(options & (OPT_INTERACTIVE | OPT_BATCH_FLAG))){
772 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
773 cmd->lc_name);
774 goto jleave;
776 if(!(cmd->lc_argtype & ARG_M) && (options & OPT_SENDMODE)){
777 n_err(_("May not execute `%s' while sending\n"), cmd->lc_name);
778 goto jleave;
780 if(cmd->lc_argtype & ARG_R){
781 if(pstate & PS_COMPOSE_MODE){
782 /* TODO PS_COMPOSE_MODE: should allow `reply': ~:reply! */
783 n_err(_("Cannot invoke `%s' when in compose mode\n"), cmd->lc_name);
784 goto jleave;
786 /* TODO Nothing should prevent ARG_R in conjunction with
787 * TODO PS_ROBOT|_SOURCING; see a.._may_yield_control()! */
788 if(pstate & (PS_ROBOT | PS_SOURCING)){
789 n_err(_("Cannot invoke `%s' from a macro or during file inclusion\n"),
790 cmd->lc_name);
791 goto jleave;
794 if((cmd->lc_argtype & ARG_S) && !(pstate & PS_STARTED)){
795 n_err(_("May not execute `%s' during startup\n"), cmd->lc_name);
796 goto jleave;
798 if(!(cmd->lc_argtype & ARG_X) && (pstate & PS_COMPOSE_FORKHOOK)){
799 n_err(_("Cannot invoke `%s' from a hook running in a child process\n"),
800 cmd->lc_name);
801 goto jleave;
804 if((cmd->lc_argtype & ARG_A) && mb.mb_type == MB_VOID){
805 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd->lc_name);
806 goto jleave;
808 if((cmd->lc_argtype & ARG_W) && !(mb.mb_perm & MB_DELE)){
809 n_err(_("May not execute `%s' -- message file is read only\n"),
810 cmd->lc_name);
811 goto jleave;
814 if(cmd->lc_argtype & ARG_O)
815 OBSOLETE2(_("this command will be removed"), cmd->lc_name);
816 if(cmd->lc_argtype & ARG_V)
817 temporary_arg_v_store = NULL;
819 if(wysh && (cmd->lc_argtype & ARG_ARGMASK) != ARG_WYRALIST)
820 n_err(_("`wysh' prefix doesn't affect `%s'\n"), cmd->lc_name);
821 /* TODO v15: strip PS_ARGLIST_MASK off, just in case the actual command
822 * TODO doesn't use any of those list commands which strip this mask,
823 * TODO and for now we misuse bits for checking relation to history;
824 * TODO argument state should be property of a per-command carrier instead */
825 pstate &= ~PS_ARGLIST_MASK;
826 switch(cmd->lc_argtype & ARG_ARGMASK){
827 case ARG_MSGLIST:
828 /* Message list defaulting to nearest forward legal message */
829 if(n_msgvec == NULL)
830 goto je96;
831 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
832 break;
833 if(c == 0){
834 if((n_msgvec[0] = first(cmd->lc_msgflag, cmd->lc_msgmask)) != 0)
835 n_msgvec[1] = 0;
837 if(n_msgvec[0] == 0){
838 if(!(pstate & PS_HOOK_MASK))
839 printf(_("No applicable messages\n"));
840 break;
842 e = (*cmd->lc_func)(n_msgvec);
843 break;
845 case ARG_NDMLIST:
846 /* Message list with no defaults, but no error if none exist */
847 if(n_msgvec == NULL){
848 je96:
849 n_err(_("Invalid use of message list\n"));
850 break;
852 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
853 break;
854 e = (*cmd->lc_func)(n_msgvec);
855 break;
857 case ARG_STRLIST:
858 /* Just the straight string, with leading blanks removed */
859 while(whitechar(*cp))
860 ++cp;
861 e = (*cmd->lc_func)(cp);
862 break;
864 case ARG_WYSHLIST:
865 c = 1;
866 if(0){
867 /* FALLTHRU */
868 case ARG_WYRALIST:
869 c = wysh ? 1 : 0;
870 if(0){
871 case ARG_RAWLIST:
872 c = 0;
876 if((c = getrawlist((c != 0), arglist, n_NELEM(arglist), cp, line.l)) < 0){
877 n_err(_("Invalid argument list\n"));
878 break;
880 if(c < cmd->lc_minargs){
881 n_err(_("`%s' requires at least %d arg(s)\n"),
882 cmd->lc_name, cmd->lc_minargs);
883 break;
885 #undef lc_minargs
886 if(c > cmd->lc_maxargs){
887 n_err(_("`%s' takes no more than %d arg(s)\n"),
888 cmd->lc_name, cmd->lc_maxargs);
889 break;
891 #undef lc_maxargs
892 e = (*cmd->lc_func)(arglist);
893 break;
895 case ARG_NOLIST:
896 /* Just the constant zero, for exiting, eg. */
897 e = (*cmd->lc_func)(0);
898 break;
900 default:
901 DBG( n_panic(_("Implementation error: unknown argument type: %d"),
902 cmd->lc_argtype & ARG_ARGMASK); )
903 goto jleave0;
906 if(e == 0 && (cmd->lc_argtype & ARG_V) &&
907 (cp = temporary_arg_v_store) != NULL){
908 temporary_arg_v_store = NULL;
909 evp->le_new_content = cp;
910 goto jleave0;
912 if(!(cmd->lc_argtype & ARG_H))
913 evp->le_add_history = (((cmd->lc_argtype & ARG_G) ||
914 (pstate & PS_MSGLIST_GABBY)) ? TRUM1 : TRU1);
916 jleave:
917 /* C99 */{
918 bool_t reset = !(pstate & PS_ROOT);
920 pstate |= PS_ROOT;
921 ok_vset(_exit_status, (e == 0 ? "0" : "1")); /* TODO num=1 +real value! */
922 if(reset)
923 pstate &= ~PS_ROOT;
926 /* Exit the current source file on error TODO what a mess! */
927 if(e == 0)
928 pstate &= ~PS_EVAL_ERROR;
929 else{
930 pstate |= PS_EVAL_ERROR;
931 if(e < 0 || (pstate & PS_ROBOT)){ /* FIXME */
932 e = 1;
933 goto jret;
935 goto jret0;
938 if(cmd == NULL)
939 goto jret0;
940 if((cmd->lc_argtype & ARG_P) && ok_blook(autoprint))
941 if(visible(dot)){
942 line.s = savestr("type");
943 line.l = sizeof("type") -1;
944 gp = (struct a_lex_ghost*)-1; /* Avoid `ghost' interpretation */
945 goto jrestart;
948 if(!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(cmd->lc_argtype & ARG_T))
949 pstate |= PS_SAW_COMMAND;
950 jleave0:
951 pstate &= ~PS_EVAL_ERROR;
952 jret0:
953 e = 0;
954 jret:
956 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
958 NYD_LEAVE;
959 return e;
962 static struct a_lex_cmd const *
963 a_lex__firstfit(char const *comm){ /* TODO *hashtable*! linear list search!!! */
964 struct a_lex_cmd const *cp;
965 NYD2_ENTER;
967 for(cp = a_lex_cmd_tab;
968 PTRCMP(cp, <, &a_lex_cmd_tab[n_NELEM(a_lex_cmd_tab)]); ++cp)
969 if(*comm == *cp->lc_name && is_prefix(comm, cp->lc_name))
970 goto jleave;
971 cp = NULL;
972 jleave:
973 NYD2_LEAVE;
974 return cp;
977 static void
978 a_lex_hangup(int s){
979 NYD_X; /* Signal handler */
980 n_UNUSED(s);
981 /* nothing to do? */
982 exit(EXIT_ERR);
985 static void
986 a_lex_onintr(int s){ /* TODO block signals while acting */
987 NYD_X; /* Signal handler */
988 n_UNUSED(s);
990 safe_signal(SIGINT, a_lex_onintr);
992 termios_state_reset();
993 close_all_files(); /* FIXME .. of current level ONLU! */
994 if(image >= 0){
995 close(image);
996 image = -1;
999 a_lex_unstack(TRUM1);
1001 if(interrupts != 1)
1002 n_err_sighdl(_("Interrupt\n"));
1003 safe_signal(SIGPIPE, a_lex_oldpipe);
1004 siglongjmp(a_lex_srbuf, 0); /* FIXME get rid */
1007 static void
1008 a_lex_unstack(bool_t eval_error){
1009 struct a_lex_input_stack *lip;
1010 NYD_ENTER;
1012 if((lip = a_lex_input) == NULL){
1013 n_memory_reset();
1015 /* If called from a_lex_onintr(), be silent FIXME */
1016 pstate &= ~(PS_SOURCING | PS_ROBOT);
1017 if(eval_error == TRUM1 || !(pstate & PS_STARTED))
1018 goto jleave;
1019 goto jerr;
1022 if(lip->li_flags & a_LEX_SLICE){ /* TODO Temporary hack */
1023 stdin = lip->li_slice_stdin;
1024 stdout = lip->li_slice_stdout;
1025 options = lip->li_slice_options;
1026 goto jthe_slice_hack;
1029 if(lip->li_flags & a_LEX_MACRO){
1030 if(lip->li_flags & a_LEX_MACRO_FREE_DATA){
1031 char **lp;
1033 while(*(lp = &lip->li_lines[lip->li_loff]) != NULL){
1034 free(*lp);
1035 ++lip->li_loff;
1037 /* Part of lip's memory chunk, then */
1038 if(!(lip->li_flags & a_LEX_MACRO_CMD))
1039 free(lip->li_lines);
1041 }else{
1042 if(lip->li_flags & a_LEX_PIPE)
1043 /* XXX command manager should -TERM then -KILL instead of hoping
1044 * XXX for exit of provider due to EPIPE / SIGPIPE */
1045 Pclose(lip->li_file, TRU1);
1046 else
1047 Fclose(lip->li_file);
1050 if(!condstack_take(lip->li_cond)){
1051 n_err(_("Unmatched `if' at end of %s %s\n"),
1052 ((lip->li_flags & a_LEX_MACRO
1053 ? (lip->li_flags & a_LEX_MACRO_CMD ? _("command") : _("macro"))
1054 : _("`source'd file"))),
1055 lip->li_name);
1056 eval_error = TRU1;
1059 n_memory_autorec_pop(&lip->li_autorecmem[0]);
1061 jthe_slice_hack:
1062 if(lip->li_on_finalize != NULL)
1063 (*lip->li_on_finalize)(lip->li_finalize_arg);
1065 if((a_lex_input = lip->li_outer) == NULL){
1066 pstate &= ~(PS_SOURCING | PS_ROBOT);
1067 }else{
1068 if((a_lex_input->li_flags & (a_LEX_MACRO | a_LEX_SUPER_MACRO)) ==
1069 (a_LEX_MACRO | a_LEX_SUPER_MACRO))
1070 pstate &= ~PS_SOURCING;
1071 assert(pstate & PS_ROBOT);
1074 if(eval_error)
1075 goto jerr;
1076 jleave:
1077 if(lip != NULL && (lip->li_flags & a_LEX_FREE))
1078 free(lip);
1079 if(n_UNLIKELY(a_lex_input != NULL && eval_error == TRUM1))
1080 a_lex_unstack(TRUM1);
1081 NYD_LEAVE;
1082 return;
1084 jerr:
1085 if(lip != NULL){
1086 if(options & OPT_D_V)
1087 n_alert(_("Stopped %s %s due to errors%s"),
1088 (pstate & PS_STARTED
1089 ? (lip->li_flags & a_LEX_SLICE ? _("sliced in program")
1090 : (lip->li_flags & a_LEX_MACRO
1091 ? (lip->li_flags & a_LEX_MACRO_CMD
1092 ? _("evaluating command") : _("evaluating macro"))
1093 : (lip->li_flags & a_LEX_PIPE
1094 ? _("executing `source'd pipe")
1095 : _("loading `source'd file")))
1097 : (lip->li_flags & a_LEX_MACRO
1098 ? (lip->li_flags & a_LEX_MACRO_X_OPTION
1099 ? _("evaluating command line") : _("evaluating macro"))
1100 : _("loading initialization resource"))),
1101 lip->li_name,
1102 (options & OPT_DEBUG ? n_empty : _(" (enable *debug* for trace)")));
1105 if(!(options & OPT_INTERACTIVE) && !(pstate & PS_STARTED)){
1106 if(options & OPT_D_V)
1107 n_alert(_("Non-interactive, bailing out due to errors "
1108 "in startup load phase"));
1109 exit(EXIT_ERR);
1111 goto jleave;
1114 static bool_t
1115 a_lex_source_file(char const *file, bool_t silent_error){
1116 struct a_lex_input_stack *lip;
1117 size_t nlen;
1118 char *nbuf;
1119 bool_t ispipe;
1120 FILE *fip;
1121 NYD_ENTER;
1123 fip = NULL;
1125 /* Being a command argument file is space-trimmed *//* TODO v15 with
1126 * TODO WYRALIST this is no longer necessary true, and for that we
1127 * TODO don't set _PARSE_TRIMSPACE because we cannot! -> cmd_tab.h!! */
1128 #if 0
1129 ((ispipe = (!silent_error && (nlen = strlen(file)) > 0 &&
1130 file[--nlen] == '|')))
1131 #else
1132 ispipe = FAL0;
1133 if(!silent_error)
1134 for(nlen = strlen(file); nlen > 0;){
1135 char c;
1137 c = file[--nlen];
1138 if(!blankchar(c)){
1139 if(c == '|'){
1140 nbuf = savestrbuf(file, nlen);
1141 ispipe = TRU1;
1142 break;
1146 #endif
1148 if(ispipe){
1149 if((fip = Popen(nbuf /* #if 0 above = savestrbuf(file, nlen)*/, "r",
1150 ok_vlook(SHELL), NULL, COMMAND_FD_NULL)) == NULL){
1151 if(!silent_error || (options & OPT_D_V))
1152 n_perr(nbuf, 0);
1153 goto jleave;
1155 }else if((nbuf = fexpand(file, FEXP_LOCAL)) == NULL)
1156 goto jleave;
1157 else if((fip = Fopen(nbuf, "r")) == NULL){
1158 if(!silent_error || (options & OPT_D_V))
1159 n_perr(nbuf, 0);
1160 goto jleave;
1163 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1164 (nlen = strlen(nbuf) +1));
1165 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1166 lip->li_outer = a_lex_input;
1167 lip->li_file = fip;
1168 lip->li_cond = condstack_release();
1169 n_memory_autorec_push(&lip->li_autorecmem[0]);
1170 lip->li_flags = (ispipe ? a_LEX_FREE | a_LEX_PIPE : a_LEX_FREE) |
1171 (a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1172 ? a_LEX_SUPER_MACRO : 0);
1173 memcpy(lip->li_name, nbuf, nlen);
1175 pstate |= PS_SOURCING | PS_ROBOT;
1176 a_lex_input = lip;
1177 a_commands_recursive(n_LEXINPUT_NONE | n_LEXINPUT_NL_ESC);
1178 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
1179 jleave:
1180 NYD_LEAVE;
1181 return silent_error ? TRU1 : (fip != NULL);
1184 static bool_t
1185 a_lex_load(struct a_lex_input_stack *lip){
1186 bool_t rv;
1187 NYD2_ENTER;
1189 assert(!(pstate & PS_STARTED));
1190 assert(a_lex_input == NULL);
1192 /* POSIX:
1193 * Any errors in the start-up file shall either cause mailx to terminate
1194 * with a diagnostic message and a non-zero status or to continue after
1195 * writing a diagnostic message, ignoring the remainder of the lines in
1196 * the start-up file. */
1197 lip->li_cond = condstack_release();
1198 n_memory_autorec_push(&lip->li_autorecmem[0]);
1200 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
1201 pstate |= PS_ROBOT |
1202 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
1204 pstate |= PS_ROBOT | PS_SOURCING;
1205 if(options & OPT_D_V)
1206 n_err(_("Loading %s\n"), n_shexp_quote_cp(lip->li_name, FAL0));
1207 a_lex_input = lip;
1208 if(!(rv = n_commands())){
1209 if(!(options & OPT_INTERACTIVE)){
1210 if(options & OPT_D_V)
1211 n_alert(_("Non-interactive program mode, forced exit"));
1212 exit(EXIT_ERR);
1215 /* PS_EXIT handled by callers */
1216 NYD2_LEAVE;
1217 return rv;
1220 static void
1221 a_lex__cmdrecint(int sig){ /* TODO one day, we don't need it no more */
1222 NYD_X; /* Signal handler */
1223 n_UNUSED(sig);
1224 siglongjmp(a_lex_input->li_cmdrec_jmp, 1);
1227 static bool_t
1228 a_commands_recursive(enum n_lexinput_flags lif){
1229 volatile int hadint; /* TODO get rid of shitty signal stuff (see signal.c) */
1230 sighandler_type soldhdl;
1231 sigset_t sintset, soldset;
1232 struct a_lex_eval_ctx ev;
1233 bool_t rv;
1234 NYD2_ENTER;
1236 memset(&ev, 0, sizeof ev);
1238 sigfillset(&sintset);
1239 sigprocmask(SIG_BLOCK, &sintset, &soldset);
1240 hadint = FAL0;
1241 if((soldhdl = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN){
1242 safe_signal(SIGINT, &a_lex__cmdrecint);
1243 if(sigsetjmp(a_lex_input->li_cmdrec_jmp, 1)){
1244 hadint = TRU1;
1245 goto jjump;
1248 sigprocmask(SIG_SETMASK, &soldset, NULL);
1250 n_COLOUR( n_colour_env_push(); )
1251 rv = TRU1;
1252 for(;;){
1253 int n;
1255 /* Read a line of commands and handle end of file specially */
1256 ev.le_line.l = ev.le_line_size;
1257 n = n_lex_input(lif, NULL, &ev.le_line.s, &ev.le_line.l,
1258 ev.le_new_content);
1259 ev.le_line_size = (ui32_t)ev.le_line.l;
1260 ev.le_line.l = (ui32_t)n;
1262 if(n < 0)
1263 break;
1265 if(a_lex_evaluate(&ev)){
1266 rv = FAL0;
1267 break;
1269 n_memory_reset();
1271 if((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)){
1272 if(exit_status != EXIT_OK)
1273 break;
1276 jjump: /* TODO */
1277 a_lex_unstack(!rv);
1278 n_COLOUR( n_colour_env_pop(FAL0); )
1280 if(ev.le_line.s != NULL)
1281 free(ev.le_line.s);
1283 if(soldhdl != SIG_IGN)
1284 safe_signal(SIGINT, soldhdl);
1285 NYD2_LEAVE;
1286 if(hadint){
1287 sigprocmask(SIG_SETMASK, &soldset, NULL);
1288 n_raise(SIGINT);
1290 return rv;
1293 FL int
1294 c_cmdnotsupp(void *vp){
1295 NYD_ENTER;
1296 n_UNUSED(vp);
1297 n_err(_("The requested feature is not compiled in\n"));
1298 NYD_LEAVE;
1299 return 1;
1302 FL bool_t
1303 n_commands(void){ /* FIXME */
1304 struct a_lex_eval_ctx ev;
1305 int n;
1306 bool_t volatile rv;
1307 NYD_ENTER;
1309 rv = TRU1;
1311 if (!(pstate & PS_SOURCING)) {
1312 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
1313 safe_signal(SIGINT, &a_lex_onintr);
1314 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1315 safe_signal(SIGHUP, &a_lex_hangup);
1317 a_lex_oldpipe = safe_signal(SIGPIPE, SIG_IGN);
1318 safe_signal(SIGPIPE, a_lex_oldpipe);
1320 memset(&ev, 0, sizeof ev);
1322 (void)sigsetjmp(a_lex_srbuf, 1); /* FIXME get rid */
1323 for (;;) {
1324 char *temporary_orig_line; /* XXX eval_ctx.le_line not yet constant */
1326 n_COLOUR( n_colour_env_pop(TRU1); )
1328 /* TODO Unless we have our signal manager (or however we do it) child
1329 * TODO processes may have time slots where their execution isn't
1330 * TODO protected by signal handlers (in between start and setup
1331 * TODO completed). close_all_files() is only called from onintr()
1332 * TODO so those may linger possibly forever */
1333 if(!(pstate & PS_SOURCING))
1334 close_all_files();
1336 interrupts = 0;
1338 n_memory_reset();
1340 if (!(pstate & PS_SOURCING)) {
1341 char *cp;
1343 /* TODO Note: this buffer may contain a password. We should redefine
1344 * TODO the code flow which has to do that */
1345 if ((cp = termios_state.ts_linebuf) != NULL) {
1346 termios_state.ts_linebuf = NULL;
1347 termios_state.ts_linesize = 0;
1348 free(cp); /* TODO pool give-back */
1350 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1351 if (ev.le_line.l > LINESIZE * 3) {
1352 free(ev.le_line.s); /* TODO pool! but what? */
1353 ev.le_line.s = NULL;
1354 ev.le_line.l = ev.le_line_size = 0;
1358 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1359 char *cp;
1361 cp = ok_vlook(newmail);
1362 if ((options & OPT_TTYIN) && cp != NULL) {
1363 struct stat st;
1365 /* FIXME TEST WITH NOPOLL ETC. !!! */
1366 n = (cp != NULL && strcmp(cp, "nopoll"));
1367 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
1368 st.st_size > mailsize) ||
1369 (mb.mb_type == MB_MAILDIR && n != 0)) {
1370 size_t odot = PTR2SIZE(dot - message);
1371 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
1373 if (setfile(mailname,
1374 FEDIT_NEWMAIL |
1375 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
1376 exit_status |= EXIT_ERR;
1377 rv = FAL0;
1378 break;
1380 dot = message + odot;
1381 pstate |= odid;
1385 exit_status = EXIT_OK;
1388 /* Read a line of commands and handle end of file specially */
1389 jreadline:
1390 ev.le_line.l = ev.le_line_size;
1391 n = n_lex_input(n_LEXINPUT_CTX_DEFAULT | n_LEXINPUT_NL_ESC, NULL,
1392 &ev.le_line.s, &ev.le_line.l, ev.le_new_content);
1393 ev.le_line_size = (ui32_t)ev.le_line.l;
1394 ev.le_line.l = (ui32_t)n;
1396 if (n < 0) {
1397 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1398 if (!(pstate & PS_ROBOT) &&
1399 (options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
1400 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1401 n_msleep(500, FAL0);
1402 continue;
1404 break;
1407 temporary_orig_line = ((pstate & PS_SOURCING) ||
1408 !(options & OPT_INTERACTIVE)) ? NULL
1409 : savestrbuf(ev.le_line.s, ev.le_line.l);
1410 pstate &= ~PS_HOOK_MASK;
1411 if (a_lex_evaluate(&ev)) {
1412 if (!(pstate & PS_STARTED)) /* TODO mess; join PS_EVAL_ERROR.. */
1413 rv = FAL0;
1414 break;
1417 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
1418 if (exit_status != EXIT_OK)
1419 break;
1420 if ((pstate & (PS_SOURCING | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
1421 exit_status = EXIT_ERR;
1422 break;
1425 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1426 if (ev.le_new_content != NULL)
1427 goto jreadline;
1428 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1429 if (temporary_orig_line != NULL)
1430 n_tty_addhist(temporary_orig_line, (ev.le_add_history != TRU1));
1433 if(pstate & PS_EXIT)
1434 break;
1437 a_lex_unstack(!rv);
1439 if (ev.le_line.s != NULL)
1440 free(ev.le_line.s);
1441 NYD_LEAVE;
1442 return rv;
1445 FL int
1446 (n_lex_input)(enum n_lexinput_flags lif, char const *prompt, char **linebuf,
1447 size_t *linesize, char const *string n_MEMORY_DEBUG_ARGS){
1448 /* TODO readline: linebuf pool!; n_lex_input should return si64_t */
1449 struct n_string xprompt;
1450 FILE *ifile;
1451 bool_t doprompt, dotty;
1452 char const *iftype;
1453 int n, nold;
1454 NYD2_ENTER;
1456 /* Special case macro mode: never need to prompt, lines have always been
1457 * unfolded already */
1458 if(!(lif & n_LEXINPUT_FORCE_STDIN) &&
1459 a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO)){
1460 if(*linebuf != NULL)
1461 free(*linebuf);
1463 if((*linebuf = a_lex_input->li_lines[a_lex_input->li_loff]) == NULL){
1464 *linesize = 0;
1465 n = -1;
1466 goto jleave;
1469 ++a_lex_input->li_loff;
1470 *linesize = strlen(*linebuf);
1471 if(!(a_lex_input->li_flags & a_LEX_MACRO_FREE_DATA))
1472 *linebuf = sbufdup(*linebuf, *linesize);
1474 iftype = (a_lex_input->li_flags & a_LEX_MACRO_X_OPTION)
1475 ? "-X OPTION"
1476 : (a_lex_input->li_flags & a_LEX_MACRO_CMD) ? "CMD" : "MACRO";
1477 n = (int)*linesize;
1478 pstate |= PS_READLINE_NL;
1479 goto jhave_dat;
1481 pstate &= ~PS_READLINE_NL;
1483 iftype = (!(pstate & PS_STARTED) ? "LOAD"
1484 : (pstate & PS_SOURCING) ? "SOURCE" : "READ");
1485 doprompt = ((pstate & (PS_STARTED | PS_ROBOT)) == PS_STARTED &&
1486 (options & OPT_INTERACTIVE));
1487 dotty = (doprompt && !ok_blook(line_editor_disable));
1488 if(!doprompt)
1489 lif |= n_LEXINPUT_PROMPT_NONE;
1490 else{
1491 if(!dotty)
1492 n_string_creat_auto(&xprompt);
1493 if(prompt == NULL)
1494 lif |= n_LEXINPUT_PROMPT_EVAL;
1497 /* Ensure stdout is flushed first anyway */
1498 if(!dotty && (lif & n_LEXINPUT_PROMPT_NONE))
1499 fflush(stdout);
1501 ifile = (a_lex_input != NULL) ? a_lex_input->li_file : stdin;
1502 if(ifile == NULL){
1503 assert((pstate & PS_COMPOSE_FORKHOOK) &&
1504 a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO));
1505 ifile = stdin;
1508 for(nold = n = 0;;){
1509 if(dotty){
1510 assert(ifile == stdin);
1511 if(string != NULL && (n = (int)strlen(string)) > 0){
1512 if(*linesize > 0)
1513 *linesize += n +1;
1514 else
1515 *linesize = (size_t)n + LINESIZE +1;
1516 *linebuf = (n_realloc)(*linebuf, *linesize n_MEMORY_DEBUG_ARGSCALL);
1517 memcpy(*linebuf, string, (size_t)n +1);
1519 string = NULL;
1520 /* TODO if nold>0, don't redisplay the entire line!
1521 * TODO needs complete redesign ... */
1522 n = (n_tty_readline)(lif, prompt, linebuf, linesize, n
1523 n_MEMORY_DEBUG_ARGSCALL);
1524 }else{
1525 if(!(lif & n_LEXINPUT_PROMPT_NONE)){
1526 n_tty_create_prompt(&xprompt, prompt, lif);
1527 if(xprompt.s_len > 0){
1528 fwrite(xprompt.s_dat, 1, xprompt.s_len, stdout);
1529 fflush(stdout);
1533 n = (readline_restart)(ifile, linebuf, linesize, n
1534 n_MEMORY_DEBUG_ARGSCALL);
1536 if(n > 0 && nold > 0){
1537 int i = 0;
1538 char const *cp = *linebuf + nold;
1540 while(blankspacechar(*cp) && nold + i < n)
1541 ++cp, ++i;
1542 if(i > 0){
1543 memmove(*linebuf + nold, cp, n - nold - i);
1544 n -= i;
1545 (*linebuf)[n] = '\0';
1550 if(n <= 0)
1551 break;
1553 /* POSIX says:
1554 * An unquoted <backslash> at the end of a command line shall
1555 * be discarded and the next line shall continue the command */
1556 if(!(lif & n_LEXINPUT_NL_ESC) || (*linebuf)[n - 1] != '\\'){
1557 if(dotty)
1558 pstate |= PS_READLINE_NL;
1559 break;
1561 /* Definitely outside of quotes, thus the quoting rules are so that an
1562 * uneven number of successive backslashs at EOL is a continuation */
1563 if(n > 1){
1564 size_t i, j;
1566 for(j = 1, i = (size_t)n - 1; i-- > 0; ++j)
1567 if((*linebuf)[i] != '\\')
1568 break;
1569 if(!(j & 1))
1570 break;
1572 (*linebuf)[nold = --n] = '\0';
1573 lif |= n_LEXINPUT_NL_FOLLOW;
1576 if(n < 0)
1577 goto jleave;
1578 (*linebuf)[*linesize = n] = '\0';
1580 jhave_dat:
1581 #if 0
1582 if(lif & n_LEXINPUT_DROP_TRAIL_SPC){
1583 char *cp, c;
1584 size_t i;
1586 for(cp = &(*linebuf)[i = (size_t)n];; --i){
1587 c = *--cp;
1588 if(!blankspacechar(c))
1589 break;
1591 (*linebuf)[n = (int)i] = '\0';
1594 if(lif & n_LEXINPUT_DROP_LEAD_SPC){
1595 char *cp, c;
1596 size_t j, i;
1598 for(cp = &(*linebuf)[0], j = (size_t)n, i = 0; i < j; ++i){
1599 c = *cp++;
1600 if(!blankspacechar(c))
1601 break;
1603 if(i > 0){
1604 memcpy(&(*linebuf)[0], &(*linebuf)[i], j -= i);
1605 (*linebuf)[n = (int)j] = '\0';
1608 #endif /* 0 (notyet - must take care for backslash escaped space) */
1610 if(options & OPT_D_VV)
1611 n_err(_("%s %d bytes <%s>\n"), iftype, n, *linebuf);
1612 jleave:
1613 if (pstate & PS_PSTATE_PENDMASK)
1614 a_lex_update_pstate();
1616 /* TODO We need to special case a_LEX_SLICE, since that is not managed by us
1617 * TODO but only established from the outside and we need to drop this
1618 * TODO overlay context somehow */
1619 if(n < 0 && a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SLICE))
1620 a_lex_unstack(FAL0);
1621 NYD2_LEAVE;
1622 return n;
1625 FL char *
1626 n_lex_input_cp(enum n_lexinput_flags lif, char const *prompt,
1627 char const *string){
1628 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1629 size_t linesize;
1630 char *linebuf, *rv;
1631 int n;
1632 NYD2_ENTER;
1634 linesize = 0;
1635 linebuf = NULL;
1636 rv = NULL;
1638 n = n_lex_input(lif, prompt, &linebuf, &linesize, string);
1639 if(n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
1640 (lif & n_LEXINPUT_HIST_ADD) && (options & OPT_INTERACTIVE))
1641 n_tty_addhist(rv, ((lif & n_LEXINPUT_HIST_GABBY) != 0));
1643 if(linebuf != NULL)
1644 free(linebuf);
1645 NYD2_LEAVE;
1646 return rv;
1649 FL int
1650 c_read(void *v){ /* TODO IFS? how? -r */
1651 char const **argv, *cp, *cp2;
1652 int rv;
1653 NYD2_ENTER;
1655 rv = 0;
1656 for(argv = v; (cp = *argv++) != NULL;)
1657 if(!n_shexp_is_valid_varname(cp)){
1658 n_err(_("`read': not a valid variable name: %s\n"),
1659 n_shexp_quote_cp(cp, FAL0));
1660 rv = 1;
1662 if(rv)
1663 goto jleave;
1665 cp = n_lex_input_cp(((pstate & PS_COMPOSE_MODE
1666 ? n_LEXINPUT_CTX_COMPOSE : n_LEXINPUT_CTX_DEFAULT) |
1667 n_LEXINPUT_FORCE_STDIN | n_LEXINPUT_NL_ESC |
1668 n_LEXINPUT_PROMPT_NONE /* XXX POSIX: PS2: yes! */),
1669 NULL, NULL);
1670 if(cp == NULL)
1671 cp = n_empty;
1673 for(argv = v; *argv != NULL; ++argv){
1674 char c;
1676 while(blankchar(*cp))
1677 ++cp;
1678 if(*cp == '\0')
1679 break;
1681 /* The last variable gets the remaining line less trailing IFS */
1682 if(argv[1] == NULL){
1683 for(cp2 = cp; *cp2 != '\0'; ++cp2)
1685 for(; cp2 > cp; --cp2){
1686 c = cp2[-1];
1687 if(!blankchar(c))
1688 break;
1690 }else
1691 for(cp2 = cp; (c = *++cp2) != '\0';)
1692 if(blankchar(c))
1693 break;
1695 vok_vset(*argv, savestrbuf(cp, PTR2SIZE(cp2 - cp)));
1696 cp = cp2;
1699 /* Set the remains to the empty string */
1700 for(; *argv != NULL; ++argv)
1701 vok_vset(*argv, "");
1703 rv = 0;
1704 jleave:
1705 NYD2_LEAVE;
1706 return rv;
1709 FL void
1710 n_load(char const *name){
1711 struct a_lex_input_stack *lip;
1712 size_t i;
1713 FILE *fip;
1714 NYD_ENTER;
1716 if(name == NULL || *name == '\0' || (fip = Fopen(name, "r")) == NULL)
1717 goto jleave;
1719 i = strlen(name) +1;
1720 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) + i);
1721 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1722 lip->li_file = fip;
1723 lip->li_flags = a_LEX_FREE;
1724 memcpy(lip->li_name, name, i);
1726 a_lex_load(lip);
1727 pstate &= ~PS_EXIT;
1728 jleave:
1729 NYD_LEAVE;
1732 FL void
1733 n_load_Xargs(char const **lines, size_t cnt){
1734 static char const name[] = "-X";
1736 ui8_t buf[sizeof(struct a_lex_input_stack) + sizeof name];
1737 char const *srcp, *xsrcp;
1738 char *cp;
1739 size_t imax, i, len;
1740 struct a_lex_input_stack *lip;
1741 NYD_ENTER;
1743 lip = (void*)buf;
1744 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1745 lip->li_flags = a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1746 a_LEX_MACRO_X_OPTION | a_LEX_SUPER_MACRO;
1747 memcpy(lip->li_name, name, sizeof name);
1749 /* The problem being that we want to support reverse solidus newline
1750 * escaping also within multiline -X, i.e., POSIX says:
1751 * An unquoted <backslash> at the end of a command line shall
1752 * be discarded and the next line shall continue the command
1753 * Therefore instead of "lip->li_lines = n_UNCONST(lines)", duplicate the
1754 * entire lines array and set _MACRO_FREE_DATA */
1755 imax = cnt + 1;
1756 lip->li_lines = smalloc(sizeof(*lip->li_lines) * imax);
1758 /* For each of the input lines.. */
1759 for(i = len = 0, cp = NULL; cnt > 0;){
1760 bool_t keep;
1761 size_t j;
1763 if((j = strlen(srcp = *lines)) == 0){
1764 ++lines, --cnt;
1765 continue;
1768 /* Separate one line from a possible multiline input string */
1769 if((xsrcp = memchr(srcp, '\n', j)) != NULL){
1770 *lines = &xsrcp[1];
1771 j = PTR2SIZE(xsrcp - srcp);
1772 }else
1773 ++lines, --cnt;
1775 /* The (separated) string may itself indicate soft newline escaping */
1776 if((keep = (srcp[j - 1] == '\\'))){
1777 size_t xj, xk;
1779 /* Need an uneven number of reverse solidus */
1780 for(xk = 1, xj = j - 1; xj-- > 0; ++xk)
1781 if(srcp[xj] != '\\')
1782 break;
1783 if(xk & 1)
1784 --j;
1785 else
1786 keep = FAL0;
1789 /* Strip any leading WS from follow lines, then */
1790 if(cp != NULL)
1791 while(j > 0 && blankspacechar(*srcp))
1792 ++srcp, --j;
1794 if(j > 0){
1795 if(i + 2 >= imax){ /* TODO need a vector (main.c, here, ++) */
1796 imax += 4;
1797 lip->li_lines = n_realloc(lip->li_lines, sizeof(*lip->li_lines) *
1798 imax);
1800 lip->li_lines[i] = cp = n_realloc(cp, len + j +1);
1801 memcpy(&cp[len], srcp, j);
1802 cp[len += j] = '\0';
1804 if(!keep)
1805 ++i;
1807 if(!keep)
1808 cp = NULL, len = 0;
1810 if(cp != NULL){
1811 assert(i + 1 < imax);
1812 lip->li_lines[i++] = cp;
1814 lip->li_lines[i] = NULL;
1816 a_lex_load(lip);
1817 if(pstate & PS_EXIT)
1818 exit(EXIT_OK);
1819 NYD_LEAVE;
1822 FL int
1823 c_source(void *v){
1824 int rv;
1825 NYD_ENTER;
1827 rv = (a_lex_source_file(*(char**)v, FAL0) == TRU1) ? 0 : 1;
1828 NYD_LEAVE;
1829 return rv;
1832 FL int
1833 c_source_if(void *v){ /* XXX obsolete?, support file tests in `if' etc.! */
1834 int rv;
1835 NYD_ENTER;
1837 rv = (a_lex_source_file(*(char**)v, TRU1) != FAL0) ? 0 : 1;
1838 NYD_LEAVE;
1839 return rv;
1842 FL bool_t
1843 n_source_macro(enum n_lexinput_flags lif, char const *name, char **lines,
1844 void (*on_finalize)(void*), void *finalize_arg){
1845 struct a_lex_input_stack *lip;
1846 size_t i;
1847 int rv;
1848 NYD_ENTER;
1850 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1851 (i = strlen(name) +1));
1852 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1853 lip->li_outer = a_lex_input;
1854 lip->li_file = NULL;
1855 lip->li_cond = condstack_release();
1856 n_memory_autorec_push(&lip->li_autorecmem[0]);
1857 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1858 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1859 ? a_LEX_SUPER_MACRO : 0);
1860 lip->li_lines = lines;
1861 lip->li_on_finalize = on_finalize;
1862 lip->li_finalize_arg = finalize_arg;
1863 memcpy(lip->li_name, name, i);
1865 pstate |= PS_ROBOT;
1866 a_lex_input = lip;
1867 rv = a_commands_recursive(lif);
1868 NYD_LEAVE;
1869 return rv;
1872 FL bool_t
1873 n_source_command(enum n_lexinput_flags lif, char const *cmd){
1874 struct a_lex_input_stack *lip;
1875 size_t i, ial;
1876 bool_t rv;
1877 NYD_ENTER;
1879 i = strlen(cmd) +1;
1880 ial = n_ALIGN(i);
1882 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1883 ial + 2*sizeof(char*));
1884 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1885 lip->li_outer = a_lex_input;
1886 lip->li_cond = condstack_release();
1887 n_memory_autorec_push(&lip->li_autorecmem[0]);
1888 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_CMD |
1889 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1890 ? a_LEX_SUPER_MACRO : 0);
1891 lip->li_lines = (void*)&lip->li_name[ial];
1892 memcpy(lip->li_lines[0] = &lip->li_name[0], cmd, i);
1893 lip->li_lines[1] = NULL;
1895 pstate |= PS_ROBOT;
1896 a_lex_input = lip;
1897 rv = a_commands_recursive(lif);
1898 NYD_LEAVE;
1899 return rv;
1902 FL void
1903 n_source_slice_hack(char const *cmd, FILE *new_stdin, FILE *new_stdout,
1904 ui32_t new_options, void (*on_finalize)(void*), void *finalize_arg){
1905 struct a_lex_input_stack *lip;
1906 size_t i;
1907 NYD_ENTER;
1909 lip = smalloc(n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name) +
1910 (i = strlen(cmd) +1));
1911 memset(lip, 0, n_VSTRUCT_SIZEOF(struct a_lex_input_stack, li_name));
1912 lip->li_outer = a_lex_input;
1913 lip->li_file = new_stdin;
1914 lip->li_flags = a_LEX_FREE | a_LEX_SLICE;
1915 lip->li_on_finalize = on_finalize;
1916 lip->li_finalize_arg = finalize_arg;
1917 lip->li_slice_stdin = stdin;
1918 lip->li_slice_stdout = stdout;
1919 lip->li_slice_options = options;
1920 memcpy(lip->li_name, cmd, i);
1922 stdin = new_stdin;
1923 stdout = new_stdout;
1924 options = new_options;
1925 pstate |= PS_ROBOT;
1926 a_lex_input = lip;
1927 NYD_LEAVE;
1930 FL void
1931 n_source_slice_hack_remove_after_jump(void){
1932 a_lex_unstack(FAL0);
1935 FL bool_t
1936 n_source_may_yield_control(void){
1937 return ((options & OPT_INTERACTIVE) &&
1938 (pstate & PS_STARTED) &&
1939 (!(pstate & PS_ROBOT) ||
1940 /* But: ok for ~:, yet: unless in a hook.
1941 * TODO This is obviously hacky in that it depends on _input_stack not
1942 * TODO loosing any flags when creating new contexts... Maybe this
1943 * TODO function should instead walk all up the context stack when
1944 * TODO there is one, and verify neither level prevents yielding! */
1945 ((pstate & PS_COMPOSE_MODE) && (a_lex_input == NULL ||
1946 !(a_lex_input->li_flags & a_LEX_SLICE)))) &&
1947 (a_lex_input == NULL || a_lex_input->li_outer == NULL));
1950 /* s-it-mode */