nail.1: -X: note all are joined and treated as a unit
[s-mailx.git] / lex_input.c
blobb869ab333d8427342b393d579060378b7f8c9a9a
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 a_LEX_MACRO_X_OPTION = 1<<4, /* Macro indeed command line -X option */
51 a_LEX_MACRO_CMD = 1<<5, /* Macro indeed single-line: ~:COMMAND */
53 a_LEX_SUPER_MACRO = 1<<16 /* *Not* inheriting PS_SOURCING state */
56 struct a_lex_cmd{
57 char const *lc_name; /* Name of command */
58 int (*lc_func)(void*); /* Implementor of command */
59 enum argtype lc_argtype; /* Arglist type (see below) */
60 si16_t lc_msgflag; /* Required flags of msgs */
61 si16_t lc_msgmask; /* Relevant flags of msgs */
62 #ifdef HAVE_DOCSTRINGS
63 char const *lc_doc; /* One line doc for command */
64 #endif
66 /* Yechh, can't initialize unions */
67 #define lc_minargs lc_msgflag /* Minimum argcount for RAWLIST */
68 #define lc_maxargs lc_msgmask /* Max argcount for RAWLIST */
70 struct a_lex_ghost{
71 struct a_lex_ghost *lg_next;
72 struct str lg_cmd; /* Data follows after .lg_name */
73 char lg_name[VFIELD_SIZE(0)];
76 struct a_lex_eval_ctx{
77 struct str le_line; /* The terminated data to _evaluate() */
78 ui32_t le_line_size; /* May be used to store line memory size */
79 bool_t le_is_recursive; /* Evaluation in evaluation? (collect ~:) */
80 ui8_t __dummy[3];
81 bool_t le_add_history; /* Enter (final) command in history? */
82 char const *le_new_content; /* History: reenter line, start with this */
85 struct a_lex_input_stack{
86 struct a_lex_input_stack *li_outer;
87 FILE *li_file; /* File we were in */
88 void *li_cond; /* Saved state of conditional stack */
89 ui32_t li_flags; /* enum a_lex_input_flags */
90 ui32_t li_loff; /* Pseudo (macro): index in .li_lines */
91 char **li_lines; /* Pseudo content, lines unfolded */
92 char li_name[VFIELD_SIZE(0)]; /* Name of file or macro */
95 static sighandler_type a_lex_oldpipe;
96 static struct a_lex_ghost *a_lex_ghosts;
97 /* a_lex_cmd_tab[] after fun protos */
99 /* */
100 static struct a_lex_input_stack *a_lex_input;
102 /* Isolate the command from the arguments */
103 static char *a_lex_isolate(char const *comm);
105 /* Command ghost handling */
106 static int a_lex_c_ghost(void *v);
107 static int a_lex_c_unghost(void *v);
109 /* Print a list of all commands */
110 static int a_lex_c_list(void *v);
112 static int a_lex__pcmd_cmp(void const *s1, void const *s2);
114 /* `quit' command */
115 static int a_lex_c_quit(void *v);
117 /* Print the binaries version number */
118 static int a_lex_c_version(void *v);
120 static int a_lex__version_cmp(void const *s1, void const *s2);
122 /* PS_STATE_PENDMASK requires some actions */
123 static void a_lex_update_pstate(void);
125 /* Evaluate a single command.
126 * .le_add_history and .le_new_content will be updated upon success.
127 * Command functions return 0 for success, 1 for error, and -1 for abort.
128 * 1 or -1 aborts a load or source, a -1 aborts the interactive command loop */
129 static int a_lex_evaluate(struct a_lex_eval_ctx *evp);
131 /* Get first-fit, or NULL */
132 static struct a_lex_cmd const *a_lex__firstfit(char const *comm);
134 /* Branch here on hangup signal and simulate "exit" */
135 static void a_lex_hangup(int s);
137 /* The following gets called on receipt of an interrupt. Close all open files
138 * except 0, 1, 2, and the temporary. Also, unstack all source files */
139 static void a_lex_onintr(int s);
141 /* Pop the current input back to the previous level. Update the program state.
142 * If the argument is TRUM1 then we don't alert and error out if the stack
143 * doesn't exist at all */
144 static void a_lex_unstack(bool_t eval_error);
146 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
147 static bool_t a_lex_source_file(char const *file, bool_t silent_error);
149 /* System resource file load()ing or -X command line option array traversal */
150 static bool_t a_lex_load(struct a_lex_input_stack *lip);
152 /* A simplified command loop for recursed state machines */
153 static bool_t a_commands_recursive(enum n_lexinput_flags lif);
155 /* List of all commands, and list of commands which are specially treated
156 * and deduced in _evaluate(), but we need a list for _c_list() and
157 * print_comm_docstr() */
158 #ifdef HAVE_DOCSTRINGS
159 # define DS(S) , S
160 #else
161 # define DS(S)
162 #endif
163 static struct a_lex_cmd const a_lex_cmd_tab[] = {
164 #include "cmd_tab.h"
166 a_lex_special_cmd_tab[] = {
167 { "#", NULL, 0, 0, 0
168 DS(N_("Comment command: ignore remaining (continuable) line")) },
169 { "-", NULL, 0, 0, 0
170 DS(N_("Print out the preceding message")) }
172 #undef DS
174 static char *
175 a_lex_isolate(char const *comm){
176 NYD2_ENTER;
177 while(*comm != '\0' &&
178 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
179 ++comm;
180 NYD2_LEAVE;
181 return UNCONST(comm);
184 static int
185 a_lex_c_ghost(void *v){
186 struct a_lex_ghost *lgp, *gp;
187 size_t i, cl, nl;
188 char *cp;
189 char const **argv;
190 NYD_ENTER;
192 argv = v;
194 /* Show the list? */
195 if(*argv == NULL){
196 FILE *fp;
198 if((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
199 fp = stdout;
201 for(i = 0, gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
202 fprintf(fp, "wysh ghost %s %s\n",
203 gp->lg_name, n_shexp_quote_cp(gp->lg_cmd.s, TRU1));
205 if(fp != stdout){
206 page_or_print(fp, i);
207 Fclose(fp);
209 goto jleave;
212 /* Verify the ghost name is a valid one */
213 if(*argv[0] == '\0' || *a_lex_isolate(argv[0]) != '\0'){
214 n_err(_("`ghost': can't canonicalize %s\n"),
215 n_shexp_quote_cp(argv[0], FAL0));
216 v = NULL;
217 goto jleave;
220 /* Show command of single ghost? */
221 if(argv[1] == NULL){
222 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
223 if(!strcmp(argv[0], gp->lg_name)){
224 printf("wysh ghost %s %s\n",
225 gp->lg_name, n_shexp_quote_cp(gp->lg_cmd.s, TRU1));
226 goto jleave;
228 n_err(_("`ghost': no such alias: %s\n"), argv[0]);
229 v = NULL;
230 goto jleave;
233 /* Define command for ghost: verify command content */
234 for(cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
235 if(*cp != '\0')
236 cl += strlen(cp) +1; /* SP or NUL */
237 if(cl == 0){
238 n_err(_("`ghost': empty command arguments after %s\n"), argv[0]);
239 v = NULL;
240 goto jleave;
243 /* If the ghost already exists, recreate */
244 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL; lgp = gp, gp = gp->lg_next)
245 if(!strcmp(gp->lg_name, argv[0])){
246 if(lgp != NULL)
247 lgp->lg_next = gp->lg_next;
248 else
249 a_lex_ghosts = gp->lg_next;
250 free(gp);
251 break;
254 nl = strlen(argv[0]) +1;
255 gp = smalloc(sizeof(*gp) - VFIELD_SIZEOF(struct a_lex_ghost, lg_name) +
256 nl + cl);
257 gp->lg_next = a_lex_ghosts;
258 a_lex_ghosts = gp;
259 memcpy(gp->lg_name, argv[0], nl);
260 cp = gp->lg_cmd.s = gp->lg_name + nl;
261 gp->lg_cmd.l = --cl;
263 while(*++argv != NULL)
264 if((i = strlen(*argv)) > 0){
265 memcpy(cp, *argv, i);
266 cp += i;
267 *cp++ = ' ';
269 *--cp = '\0';
270 jleave:
271 NYD_LEAVE;
272 return v == NULL;
275 static int
276 a_lex_c_unghost(void *v){
277 struct a_lex_ghost *lgp, *gp;
278 char const **argv, *cp;
279 int rv;
280 NYD_ENTER;
282 rv = 0;
283 argv = v;
285 while((cp = *argv++) != NULL){
286 if(cp[0] == '*' && cp[1] == '\0'){
287 while((gp = a_lex_ghosts) != NULL){
288 a_lex_ghosts = gp->lg_next;
289 free(gp);
291 }else{
292 for(lgp = NULL, gp = a_lex_ghosts; gp != NULL;
293 lgp = gp, gp = gp->lg_next)
294 if(!strcmp(gp->lg_name, cp)){
295 if(lgp != NULL)
296 lgp->lg_next = gp->lg_next;
297 else
298 a_lex_ghosts = gp->lg_next;
299 free(gp);
300 goto jouter;
302 n_err(_("`unghost': no such alias: %s\n"),
303 n_shexp_quote_cp(cp, FAL0));
304 rv = 1;
305 jouter: ;
308 NYD_LEAVE;
309 return rv;
312 static int
313 a_lex_c_list(void *v){
314 FILE *fp;
315 struct a_lex_cmd const **cpa, *cp, **cursor;
316 size_t l, i;
317 NYD_ENTER;
319 i = NELEM(a_lex_cmd_tab) + NELEM(a_lex_special_cmd_tab) +1;
320 cpa = salloc(sizeof(cp) * i);
322 for(i = 0; i < NELEM(a_lex_cmd_tab); ++i)
323 cpa[i] = &a_lex_cmd_tab[i];
324 /* C99 */{
325 size_t j;
327 for(j = 0; j < NELEM(a_lex_special_cmd_tab); ++i, ++j)
328 cpa[i] = &a_lex_special_cmd_tab[j];
330 cpa[i] = NULL;
332 /* C99 */{
333 char const *xcp = v;
335 if(*xcp == '\0')
336 qsort(cpa, i, sizeof(xcp), &a_lex__pcmd_cmp);
339 if((fp = Ftmp(NULL, "list", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
340 fp = stdout;
342 fprintf(fp, _("Commands are:\n"));
343 l = 1;
344 for(i = 0, cursor = cpa; (cp = *cursor++) != NULL;){
345 if(cp->lc_func == &c_cmdnotsupp)
346 continue;
347 if(options & OPT_D_V){
348 char const *argt;
350 switch(cp->lc_argtype & ARG_ARGMASK){
351 case ARG_MSGLIST: argt = N_("message-list"); break;
352 case ARG_STRLIST: argt = N_("a \"string\""); break;
353 case ARG_RAWLIST: argt = N_("old-style quoting"); break;
354 case ARG_NOLIST: argt = N_("no arguments"); break;
355 case ARG_NDMLIST: argt = N_("message-list (without a default)"); break;
356 case ARG_WYSHLIST: argt = N_("sh(1)ell-style quoting"); break;
357 default: argt = N_("`wysh' for sh(1)ell-style quoting"); break;
359 #ifdef HAVE_DOCSTRINGS
360 fprintf(fp, _("`%s'. Argument type: %s.\n\t%s\n"),
361 cp->lc_name, V_(argt), V_(cp->lc_doc));
362 l += 2;
363 #else
364 fprintf(fp, "`%s' (%s)\n", cp->lc_name, argt);
365 ++l;
366 #endif
367 }else{
368 size_t j = strlen(cp->lc_name) + 2;
370 if((i += j) > 72){
371 i = j;
372 fprintf(fp, "\n");
373 ++l;
375 fprintf(fp, (*cursor != NULL ? "%s, " : "%s\n"), cp->lc_name);
379 if(fp != stdout){
380 page_or_print(fp, l);
381 Fclose(fp);
383 NYD_LEAVE;
384 return 0;
387 static int
388 a_lex__pcmd_cmp(void const *s1, void const *s2){
389 struct a_lex_cmd const * const *cp1, * const *cp2;
390 int rv;
391 NYD2_ENTER;
393 cp1 = s1;
394 cp2 = s2;
395 rv = strcmp((*cp1)->lc_name, (*cp2)->lc_name);
396 NYD2_LEAVE;
397 return rv;
400 static int
401 a_lex_c_quit(void *v){
402 NYD_ENTER;
403 UNUSED(v);
405 /* If we are PS_SOURCING, then return 1 so _evaluate() can handle it.
406 * Otherwise return -1 to abort command loop */
407 pstate |= PS_EXIT;
408 NYD_LEAVE;
409 return 0;
412 static int
413 a_lex_c_version(void *v){
414 int longest, rv;
415 char *iop;
416 char const *cp, **arr;
417 size_t i, i2;
418 NYD_ENTER;
419 UNUSED(v);
421 printf(_("%s version %s\nFeatures included (+) or not (-)\n"),
422 uagent, ok_vlook(version));
424 /* *features* starts with dummy byte to avoid + -> *folder* expansions */
425 i = strlen(cp = &ok_vlook(features)[1]) +1;
426 iop = salloc(i);
427 memcpy(iop, cp, i);
429 arr = salloc(sizeof(cp) * VAL_FEATURES_CNT);
430 for(longest = 0, i = 0; (cp = n_strsep(&iop, ',', TRU1)) != NULL; ++i){
431 arr[i] = cp;
432 i2 = strlen(cp);
433 longest = MAX(longest, (int)i2);
435 qsort(arr, i, sizeof(cp), &a_lex__version_cmp);
437 for(++longest, i2 = 0; i-- > 0;){
438 cp = *(arr++);
439 printf("%-*s ", longest, cp);
440 i2 += longest;
441 if(UICMP(z, ++i2 + longest, >=, scrnwidth) || i == 0){
442 i2 = 0;
443 putchar('\n');
447 if((rv = ferror(stdout) != 0))
448 clearerr(stdout);
449 NYD_LEAVE;
450 return rv;
453 static int
454 a_lex__version_cmp(void const *s1, void const *s2){
455 char const * const *cp1, * const *cp2;
456 int rv;
457 NYD2_ENTER;
459 cp1 = s1;
460 cp2 = s2;
461 rv = strcmp(&(*cp1)[1], &(*cp2)[1]);
462 NYD2_LEAVE;
463 return rv;
466 static void
467 a_lex_update_pstate(void){
468 NYD_ENTER;
470 if(pstate & PS_SIGWINCH_PEND){
471 char buf[32];
473 snprintf(buf, sizeof buf, "%d", scrnwidth);
474 ok_vset(COLUMNS, buf);
475 snprintf(buf, sizeof buf, "%d", scrnheight);
476 ok_vset(LINES, buf);
479 pstate &= ~PS_PSTATE_PENDMASK;
480 NYD_LEAVE;
483 static int
484 a_lex_evaluate(struct a_lex_eval_ctx *evp){
485 /* xxx old style(9), but also old code */
486 struct str line;
487 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
488 struct a_lex_ghost *gp;
489 struct a_lex_cmd const *cmd;
490 int c, e;
491 bool_t wysh;
492 NYD_ENTER;
494 wysh = FAL0;
495 e = 1;
496 cmd = NULL;
497 gp = NULL;
498 line = evp->le_line; /* XXX don't change original (buffer pointer) */
499 assert(line.s[line.l] == '\0');
500 evp->le_add_history = FAL0;
501 evp->le_new_content = NULL;
503 /* Command ghosts that refer to shell commands or macro expansion restart */
504 jrestart:
506 /* Strip the white space away from end and beginning of command */
507 if(line.l > 0){
508 size_t i = line.l;
510 for(cp = &line.s[i -1]; spacechar(*cp); --cp)
511 --i;
512 line.l = i;
514 for(cp = line.s; spacechar(*cp); ++cp)
516 line.l -= PTR2SIZE(cp - line.s);
518 /* Ignore null commands (comments) */
519 if(*cp == '#')
520 goto jleave0;
522 /* Handle ! differently to get the correct lexical conventions */
523 arglist[0] = cp;
524 if(*cp == '!')
525 ++cp;
526 /* Isolate the actual command; since it may not necessarily be
527 * separated from the arguments (as in `p1') we need to duplicate it to
528 * be able to create a NUL terminated version.
529 * We must be aware of several special one letter commands here */
530 else if((cp = a_lex_isolate(cp)) == arglist[0] &&
531 (*cp == '|' || *cp == '~' || *cp == '?'))
532 ++cp;
533 c = (int)PTR2SIZE(cp - arglist[0]);
534 line.l -= c;
535 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
536 memcpy(word, arglist[0], c);
537 word[c] = '\0';
539 /* Look up the command; if not found, bitch.
540 * Normally, a blank command would map to the first command in the
541 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
542 * confusion; act just the same for ghosts */
543 if(*word == '\0'){
544 if((pstate & PS_ROBOT) || gp != NULL)
545 goto jleave0;
546 cmd = a_lex_cmd_tab + 0;
547 goto jexec;
550 /* XXX It may be the argument parse adjuster */
551 if(!wysh && c == sizeof("wysh") -1 && !asccasecmp(word, "wysh")){
552 wysh = TRU1;
553 line.s = cp;
554 goto jrestart;
557 /* If this is the first evaluation, check command ghosts */
558 if(gp == NULL){
559 /* TODO relink list head, so it's sorted on usage over time?
560 * TODO in fact, there should be one hashmap over all commands and ghosts
561 * TODO so that the lookup could be made much more efficient than it is
562 * TODO now (two adjacent list searches! */
563 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
564 if(!strcmp(word, gp->lg_name)){
565 if(line.l > 0){
566 size_t i;
568 i = gp->lg_cmd.l;
569 line.s = salloc(i + line.l +1);
570 memcpy(line.s, gp->lg_cmd.s, i);
571 memcpy(line.s + i, cp, line.l);
572 line.s[i += line.l] = '\0';
573 line.l = i;
574 }else{
575 line.s = gp->lg_cmd.s;
576 line.l = gp->lg_cmd.l;
578 goto jrestart;
582 if((cmd = a_lex__firstfit(word)) == NULL || cmd->lc_func == &c_cmdnotsupp){
583 bool_t s;
585 if(!(s = condstack_isskip()) || (options & OPT_D_V))
586 n_err(_("Unknown command%s: `%s'\n"),
587 (s ? _(" (ignored due to `if' condition)") : ""), word);
588 if(s)
589 goto jleave0;
590 if(cmd != NULL){
591 c_cmdnotsupp(NULL);
592 cmd = NULL;
594 goto jleave;
597 /* See if we should execute the command -- if a conditional we always
598 * execute it, otherwise, check the state of cond */
599 jexec:
600 if(!(cmd->lc_argtype & ARG_F) && condstack_isskip())
601 goto jleave0;
603 /* Process the arguments to the command, depending on the type it expects */
604 if(!(cmd->lc_argtype & ARG_M) && (options & OPT_SENDMODE)){
605 n_err(_("May not execute `%s' while sending\n"), cmd->lc_name);
606 goto jleave;
608 if((cmd->lc_argtype & ARG_S) && !(pstate & PS_STARTED)){
609 n_err(_("May not execute `%s' during startup\n"), cmd->lc_name);
610 goto jleave;
612 if((cmd->lc_argtype & ARG_I) &&
613 !(options & (OPT_INTERACTIVE | OPT_BATCH_FLAG))){
614 n_err(_("May not execute `%s' unless interactive or in batch mode\n"),
615 cmd->lc_name);
616 goto jleave;
618 if((cmd->lc_argtype & ARG_R) && (pstate & PS_RECURSED)){
619 n_err(_("Cannot invoke `%s' when in recursed mode (e.g., composing)\n"),
620 cmd->lc_name);
621 goto jleave;
624 if((cmd->lc_argtype & ARG_W) && !(mb.mb_perm & MB_DELE)){
625 n_err(_("May not execute `%s' -- message file is read only\n"),
626 cmd->lc_name);
627 goto jleave;
629 if((cmd->lc_argtype & ARG_A) && mb.mb_type == MB_VOID){
630 n_err(_("Cannot execute `%s' without active mailbox\n"), cmd->lc_name);
631 goto jleave;
634 if(cmd->lc_argtype & ARG_O)
635 OBSOLETE2(_("this command will be removed"), cmd->lc_name);
636 if(cmd->lc_argtype & ARG_V)
637 temporary_arg_v_store = NULL;
639 if(wysh && (cmd->lc_argtype & ARG_ARGMASK) != ARG_WYRALIST)
640 n_err(_("`wysh' prefix doesn't affect `%s'\n"), cmd->lc_name);
641 /* TODO v15: strip PS_ARGLIST_MASK off, just in case the actual command
642 * TODO doesn't use any of those list commands which strip this mask,
643 * TODO and for now we misuse bits for checking relation to history;
644 * TODO argument state should be property of a per-command carrier instead */
645 pstate &= ~PS_ARGLIST_MASK;
646 switch(cmd->lc_argtype & ARG_ARGMASK){
647 case ARG_MSGLIST:
648 /* Message list defaulting to nearest forward legal message */
649 if(n_msgvec == NULL)
650 goto je96;
651 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
652 break;
653 if(c == 0){
654 if((n_msgvec[0] = first(cmd->lc_msgflag, cmd->lc_msgmask)) != 0)
655 n_msgvec[1] = 0;
657 if(n_msgvec[0] == 0){
658 if(!(pstate & PS_HOOK_MASK))
659 printf(_("No applicable messages\n"));
660 break;
662 e = (*cmd->lc_func)(n_msgvec);
663 break;
665 case ARG_NDMLIST:
666 /* Message list with no defaults, but no error if none exist */
667 if(n_msgvec == NULL){
668 je96:
669 n_err(_("Invalid use of message list\n"));
670 break;
672 if((c = getmsglist(cp, n_msgvec, cmd->lc_msgflag)) < 0)
673 break;
674 e = (*cmd->lc_func)(n_msgvec);
675 break;
677 case ARG_STRLIST:
678 /* Just the straight string, with leading blanks removed */
679 while(whitechar(*cp))
680 ++cp;
681 e = (*cmd->lc_func)(cp);
682 break;
684 case ARG_WYSHLIST:
685 c = 1;
686 if(0){
687 /* FALLTHRU */
688 case ARG_WYRALIST:
689 c = wysh ? 1 : 0;
690 if(0){
691 case ARG_RAWLIST:
692 c = 0;
696 if((c = getrawlist((c != 0), arglist, NELEM(arglist), cp, line.l)) < 0){
697 n_err(_("Invalid argument list\n"));
698 break;
700 if(c < cmd->lc_minargs){
701 n_err(_("`%s' requires at least %d arg(s)\n"),
702 cmd->lc_name, cmd->lc_minargs);
703 break;
705 #undef lc_minargs
706 if(c > cmd->lc_maxargs){
707 n_err(_("`%s' takes no more than %d arg(s)\n"),
708 cmd->lc_name, cmd->lc_maxargs);
709 break;
711 #undef lc_maxargs
712 e = (*cmd->lc_func)(arglist);
713 break;
715 case ARG_NOLIST:
716 /* Just the constant zero, for exiting, eg. */
717 e = (*cmd->lc_func)(0);
718 break;
720 default:
721 DBG( n_panic(_("Implementation error: unknown argument type: %d"),
722 cmd->lc_argtype & ARG_ARGMASK); )
723 goto jleave0;
726 if(e == 0 && (cmd->lc_argtype & ARG_V) &&
727 (cp = temporary_arg_v_store) != NULL){
728 temporary_arg_v_store = NULL;
729 evp->le_new_content = cp;
730 goto jleave0;
732 /* TODO v15 with PS_MSGLIST_SAW_NO the history entry is at least gabby */
733 if(!(cmd->lc_argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
734 evp->le_add_history = TRU1;
736 jleave:
737 /* Exit the current source file on error TODO what a mess! */
738 if(e == 0)
739 pstate &= ~PS_EVAL_ERROR;
740 else{
741 pstate |= PS_EVAL_ERROR;
742 if(e < 0 || (pstate & PS_ROBOT)){ /* FIXME */
743 e = 1;
744 goto jret;
746 goto jret0;
749 if(cmd == NULL)
750 goto jret0;
751 if((cmd->lc_argtype & ARG_P) && ok_blook(autoprint))
752 if(visible(dot)){
753 line.s = savestr("type");
754 line.l = sizeof("type") -1;
755 gp = (struct a_lex_ghost*)-1; /* Avoid `ghost' interpretation */
756 goto jrestart;
759 if(!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(cmd->lc_argtype & ARG_T))
760 pstate |= PS_SAW_COMMAND;
761 jleave0:
762 pstate &= ~PS_EVAL_ERROR;
763 jret0:
764 e = 0;
765 jret:
767 fprintf(stderr, "a_lex_evaluate returns %d for <%s>\n",e,line.s);
769 NYD_LEAVE;
770 return e;
773 static struct a_lex_cmd const *
774 a_lex__firstfit(char const *comm){ /* TODO *hashtable*! linear list search!!! */
775 struct a_lex_cmd const *cp;
776 NYD2_ENTER;
778 for(cp = a_lex_cmd_tab; PTRCMP(cp, <, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)]);
779 ++cp)
780 if(*comm == *cp->lc_name && is_prefix(comm, cp->lc_name))
781 goto jleave;
782 cp = NULL;
783 jleave:
784 NYD2_LEAVE;
785 return cp;
788 static void
789 a_lex_hangup(int s){
790 NYD_X; /* Signal handler */
791 UNUSED(s);
792 /* nothing to do? */
793 exit(EXIT_ERR);
796 static void
797 a_lex_onintr(int s){
798 NYD_X; /* Signal handler */
799 UNUSED(s);
801 safe_signal(SIGINT, a_lex_onintr);
802 noreset = 0;
803 a_lex_unstack(TRUM1);
805 termios_state_reset();
806 close_all_files(); /* FIXME .. to outer level ONLU! */
808 if(image >= 0){
809 close(image);
810 image = -1;
812 if(interrupts != 1)
813 n_err_sighdl(_("Interrupt\n"));
814 safe_signal(SIGPIPE, a_lex_oldpipe);
815 siglongjmp(srbuf, 0); /* FIXME get rid */
818 static void
819 a_lex_unstack(bool_t eval_error){
820 struct a_lex_input_stack *lip;
821 NYD_ENTER;
823 if((lip = a_lex_input) == NULL){
824 /* If called from a_lex_onintr(), be silent FIXME */
825 pstate &= ~(PS_SOURCING | PS_ROBOT);
826 if(eval_error == TRUM1 || !(pstate & PS_STARTED))
827 goto jleave;
828 goto jerr;
831 if(lip->li_flags & a_LEX_MACRO){
832 if(lip->li_flags & a_LEX_MACRO_FREE_DATA){
833 char **lp;
835 while(*(lp = &lip->li_lines[lip->li_loff]) != NULL){
836 free(*lp);
837 ++lip->li_loff;
839 /* Part of lip's memory chunk, then */
840 if(!(lip->li_flags & a_LEX_MACRO_CMD))
841 free(lip->li_lines);
843 }else{
844 if(lip->li_flags & a_LEX_PIPE)
845 /* XXX command manager should -TERM then -KILL instead of hoping
846 * XXX for exit of provider due to EPIPE / SIGPIPE */
847 Pclose(lip->li_file, TRU1);
848 else
849 Fclose(lip->li_file);
852 if(!condstack_take(lip->li_cond)){
853 n_err(_("Unmatched `if' at end of %s %s\n"),
854 ((lip->li_flags & a_LEX_MACRO
855 ? (lip->li_flags & a_LEX_MACRO_CMD ? _("command") : _("macro"))
856 : _("`source'd file"))),
857 lip->li_name);
858 eval_error = TRU1;
861 if((a_lex_input = lip->li_outer) == NULL){
862 pstate &= ~(PS_SOURCING | PS_ROBOT);
863 }else{
864 if((a_lex_input->li_flags & (a_LEX_MACRO | a_LEX_SUPER_MACRO)) ==
865 (a_LEX_MACRO | a_LEX_SUPER_MACRO))
866 pstate &= ~PS_SOURCING;
867 assert(pstate & PS_ROBOT);
870 if(eval_error)
871 goto jerr;
872 if(lip->li_flags & a_LEX_FREE)
873 free(lip);
874 jleave:
875 if(UNLIKELY(a_lex_input != NULL && eval_error == TRUM1))
876 a_lex_unstack(TRUM1);
877 NYD_LEAVE;
878 return;
880 jerr:
881 if(lip != NULL){
882 if(options & OPT_D_V)
883 n_alert(_("Stopped %s %s due to errors%s"),
884 (pstate & PS_STARTED
885 ? (lip->li_flags & a_LEX_MACRO
886 ? (lip->li_flags & a_LEX_MACRO_CMD
887 ? _("evaluating command") : _("evaluating macro"))
888 : (lip->li_flags & a_LEX_PIPE
889 ? _("executing `source'd pipe")
890 : _("loading `source'd file")))
891 : (lip->li_flags & a_LEX_MACRO
892 ? (lip->li_flags & a_LEX_MACRO_X_OPTION
893 ? _("evaluating command line") : _("evaluating macro"))
894 : _("loading initialization resource"))),
895 lip->li_name,
896 (options & OPT_DEBUG ? "" : _(" (enable *debug* for trace)")));
897 if(lip->li_flags & a_LEX_FREE)
898 free(lip);
901 if(!(options & OPT_INTERACTIVE) && !(pstate & PS_STARTED)){
902 if(options & OPT_D_V)
903 n_alert(_("Non-interactive, bailing out due to errors "
904 "in startup load phase"));
905 exit(EXIT_ERR);
907 goto jleave;
910 static bool_t
911 a_lex_source_file(char const *file, bool_t silent_error){
912 struct a_lex_input_stack *lip;
913 size_t nlen;
914 char *nbuf;
915 bool_t ispipe;
916 FILE *fip;
917 NYD_ENTER;
919 fip = NULL;
921 /* Being a command argument file is space-trimmed */
922 if((ispipe = (!silent_error && (nlen = strlen(file)) > 0 &&
923 file[--nlen] == '|'))){
924 if((fip = Popen(nbuf = savestrbuf(file, nlen), "r",
925 ok_vlook(SHELL), NULL, COMMAND_FD_NULL)) == NULL){
926 if(!silent_error || (options & OPT_D_V))
927 n_perr(nbuf, 0);
928 goto jleave;
930 }else if((nbuf = fexpand(file, FEXP_LOCAL)) == NULL)
931 goto jleave;
932 else if((fip = Fopen(nbuf, "r")) == NULL){
933 if(!silent_error || (options & OPT_D_V))
934 n_perr(nbuf, 0);
935 goto jleave;
938 lip = smalloc(sizeof(*lip) -
939 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
940 (nlen = strlen(nbuf) +1));
941 lip->li_outer = a_lex_input;
942 lip->li_file = fip;
943 lip->li_cond = condstack_release();
944 lip->li_flags = (ispipe ? a_LEX_FREE | a_LEX_PIPE : a_LEX_FREE) |
945 (a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
946 ? a_LEX_SUPER_MACRO : 0);
947 memcpy(lip->li_name, nbuf, nlen);
949 pstate |= PS_SOURCING | PS_ROBOT;
950 a_lex_input = lip;
951 a_commands_recursive(n_LEXINPUT_NONE | n_LEXINPUT_NL_ESC);
952 /* FIXME return TRUM1 if file can't be opened, FAL0 on eval error */
953 jleave:
954 NYD_LEAVE;
955 return silent_error ? TRU1 : (fip != NULL);
958 static bool_t
959 a_lex_load(struct a_lex_input_stack *lip){
960 bool_t rv;
961 NYD2_ENTER;
963 assert(!(pstate & PS_STARTED));
964 assert(a_lex_input == NULL);
966 /* POSIX:
967 * Any errors in the start-up file shall either cause mailx to terminate
968 * with a diagnostic message and a non-zero status or to continue after
969 * writing a diagnostic message, ignoring the remainder of the lines in
970 * the start-up file. */
971 lip->li_cond = condstack_release();
973 /* FIXME won't work for now (PS_ROBOT needs PS_SOURCING anyway)
974 pstate |= PS_ROBOT |
975 (lip->li_flags & a_LEX_MACRO_X_OPTION ? 0 : PS_SOURCING);
977 pstate |= PS_ROBOT | PS_SOURCING;
978 if(options & OPT_D_V)
979 n_err(_("Loading %s\n"), n_shexp_quote_cp(lip->li_name, FAL0));
980 a_lex_input = lip;
981 if(!(rv = n_commands())){
982 if(!(options & OPT_INTERACTIVE)){
983 if(options & OPT_D_V)
984 n_alert(_("Non-interactive program mode, forced exit"));
985 exit(EXIT_ERR);
988 /* PS_EXIT handled by callers */
989 NYD2_LEAVE;
990 return rv;
993 static bool_t
994 a_commands_recursive(enum n_lexinput_flags lif){
995 struct a_lex_eval_ctx ev;
996 bool_t rv;
997 NYD2_ENTER;
999 memset(&ev, 0, sizeof ev);
1001 /* FIXME sigkondom */
1002 n_COLOUR( n_colour_env_push(); )
1003 rv = TRU1;
1004 for(;;){
1005 int n;
1007 /* Read a line of commands and handle end of file specially */
1008 ev.le_line.l = ev.le_line_size;
1009 n = n_lex_input(lif, NULL, &ev.le_line.s, &ev.le_line.l,
1010 ev.le_new_content);
1011 ev.le_line_size = (ui32_t)ev.le_line.l;
1012 ev.le_line.l = (ui32_t)n;
1014 if(n < 0)
1015 break;
1017 if(a_lex_evaluate(&ev)){
1018 rv = FAL0;
1019 break;
1022 if((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)){
1023 if(exit_status != EXIT_OK)
1024 break;
1027 a_lex_unstack(!rv);
1028 n_COLOUR( n_colour_env_pop(FAL0); )
1030 if(ev.le_line.s != NULL)
1031 free(ev.le_line.s);
1032 NYD2_LEAVE;
1033 return rv;
1036 #ifdef HAVE_DOCSTRINGS
1037 FL bool_t
1038 n_print_comm_docstr(char const *comm){
1039 struct a_lex_ghost const *gp;
1040 struct a_lex_cmd const *cp, *cpmax;
1041 bool_t rv = FAL0;
1042 NYD_ENTER;
1044 /* Ghosts take precedence */
1045 for(gp = a_lex_ghosts; gp != NULL; gp = gp->lg_next)
1046 if(!strcmp(comm, gp->lg_name)){
1047 printf("%s -> ", comm);
1048 comm = gp->lg_cmd.s;
1049 break;
1052 cpmax = &(cp = a_lex_cmd_tab)[NELEM(a_lex_cmd_tab)];
1053 jredo:
1054 for(; PTRCMP(cp, <, cpmax); ++cp){
1055 if(!strcmp(comm, cp->lc_name))
1056 printf("%s: %s\n", comm, V_(cp->lc_doc));
1057 else if(is_prefix(comm, cp->lc_name))
1058 printf("%s (%s): %s\n", comm, cp->lc_name, V_(cp->lc_doc));
1059 else
1060 continue;
1061 rv = TRU1;
1062 break;
1064 if(!rv && PTRCMP(cpmax, ==, &a_lex_cmd_tab[NELEM(a_lex_cmd_tab)])){
1065 cpmax = &(cp = a_lex_special_cmd_tab)[NELEM(a_lex_special_cmd_tab)];
1066 goto jredo;
1069 if(!rv && gp != NULL){
1070 printf("%s\n", n_shexp_quote_cp(comm, TRU1));
1071 rv = TRU1;
1073 NYD_LEAVE;
1074 return rv;
1076 #endif /* HAVE_DOCSTRINGS */
1078 FL bool_t
1079 n_commands(void){ /* FIXME */
1080 struct a_lex_eval_ctx ev;
1081 int n;
1082 bool_t volatile rv = TRU1;
1083 NYD_ENTER;
1085 if (!(pstate & PS_SOURCING)) {
1086 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
1087 safe_signal(SIGINT, &a_lex_onintr);
1088 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1089 safe_signal(SIGHUP, &a_lex_hangup);
1091 a_lex_oldpipe = safe_signal(SIGPIPE, SIG_IGN);
1092 safe_signal(SIGPIPE, a_lex_oldpipe);
1094 memset(&ev, 0, sizeof ev);
1096 (void)sigsetjmp(srbuf, 1); /* FIXME get rid */
1097 for (;;) {
1098 char *temporary_orig_line; /* XXX eval_ctx.le_line not yet constant */
1100 n_COLOUR( n_colour_env_pop(TRU1); )
1102 /* TODO Unless we have our signal manager (or however we do it) child
1103 * TODO processes may have time slots where their execution isn't
1104 * TODO protected by signal handlers (in between start and setup
1105 * TODO completed). close_all_files() is only called from onintr()
1106 * TODO so those may linger possibly forever */
1107 if(!(pstate & PS_SOURCING))
1108 close_all_files();
1110 interrupts = 0;
1112 temporary_localopts_free(); /* XXX intermediate hack */
1113 sreset((pstate & PS_SOURCING) != 0);
1114 if (!(pstate & PS_SOURCING)) {
1115 char *cp;
1117 /* TODO Note: this buffer may contain a password. We should redefine
1118 * TODO the code flow which has to do that */
1119 if ((cp = termios_state.ts_linebuf) != NULL) {
1120 termios_state.ts_linebuf = NULL;
1121 termios_state.ts_linesize = 0;
1122 free(cp); /* TODO pool give-back */
1124 /* TODO Due to expand-on-tab of NCL the buffer may grow */
1125 if (ev.le_line.l > LINESIZE * 3) {
1126 free(ev.le_line.s); /* TODO pool! but what? */
1127 ev.le_line.s = NULL;
1128 ev.le_line.l = ev.le_line_size = 0;
1132 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1133 char *cp;
1135 cp = ok_vlook(newmail);
1136 if ((options & OPT_TTYIN) && cp != NULL) {
1137 struct stat st;
1139 /* FIXME TEST WITH NOPOLL ETC. !!! */
1140 n = (cp != NULL && strcmp(cp, "nopoll"));
1141 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
1142 st.st_size > mailsize) ||
1143 (mb.mb_type == MB_MAILDIR && n != 0)) {
1144 size_t odot = PTR2SIZE(dot - message);
1145 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
1147 if (setfile(mailname,
1148 FEDIT_NEWMAIL |
1149 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
1150 exit_status |= EXIT_ERR;
1151 rv = FAL0;
1152 break;
1154 dot = message + odot;
1155 pstate |= odid;
1159 exit_status = EXIT_OK;
1162 /* Read a line of commands and handle end of file specially */
1163 jreadline:
1164 ev.le_line.l = ev.le_line_size;
1165 n = n_lex_input(n_LEXINPUT_CTX_BASE | n_LEXINPUT_NL_ESC, NULL,
1166 &ev.le_line.s, &ev.le_line.l, ev.le_new_content);
1167 ev.le_line_size = (ui32_t)ev.le_line.l;
1168 ev.le_line.l = (ui32_t)n;
1170 if (n < 0) {
1171 /* FIXME did unstack() when PS_SOURCING, only break with PS_LOADING*/
1172 if (!(pstate & PS_ROBOT) &&
1173 (options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
1174 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
1175 n_msleep(500, FAL0);
1176 continue;
1178 break;
1181 temporary_orig_line = ((pstate & PS_SOURCING) ||
1182 !(options & OPT_INTERACTIVE)) ? NULL
1183 : savestrbuf(ev.le_line.s, ev.le_line.l);
1184 pstate &= ~PS_HOOK_MASK;
1185 if (a_lex_evaluate(&ev)) {
1186 if (!(pstate & PS_STARTED)) /* TODO mess; join PS_EVAL_ERROR.. */
1187 rv = FAL0;
1188 break;
1191 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
1192 if (exit_status != EXIT_OK)
1193 break;
1194 if ((pstate & (PS_SOURCING | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
1195 exit_status = EXIT_ERR;
1196 break;
1199 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
1200 if (ev.le_new_content != NULL)
1201 goto jreadline;
1202 /* *Can* happen since _evaluate() n_unstack()s on error! XXX no more */
1203 if (temporary_orig_line != NULL)
1204 n_tty_addhist(temporary_orig_line, !ev.le_add_history);
1207 if(pstate & PS_EXIT)
1208 break;
1210 a_lex_unstack(!rv);
1212 if (ev.le_line.s != NULL)
1213 free(ev.le_line.s);
1214 if (pstate & PS_SOURCING)
1215 sreset(FAL0);
1216 NYD_LEAVE;
1217 return rv;
1220 FL int
1221 (n_lex_input)(enum n_lexinput_flags lif, char const *prompt, char **linebuf,
1222 size_t *linesize, char const *string SMALLOC_DEBUG_ARGS){
1223 /* TODO readline: linebuf pool!; n_lex_input should return si64_t */
1224 FILE *ifile;
1225 bool_t doprompt, dotty;
1226 char const *iftype;
1227 int n, nold;
1228 NYD2_ENTER;
1230 /* Special case macro mode: never need to prompt, lines have always been
1231 * unfolded already */
1232 if(a_lex_input != NULL && (a_lex_input->li_flags & a_LEX_MACRO)){
1233 size_t i;
1234 bool_t dodup;
1235 char **lp, *cp;
1237 if(*linebuf != NULL)
1238 free(*linebuf);
1240 lp = &a_lex_input->li_lines[a_lex_input->li_loff];
1241 if((*linebuf = *lp) == NULL){
1242 *linesize = 0;
1243 n = -1;
1244 goto jleave;
1247 dodup = !(a_lex_input->li_flags & a_LEX_MACRO_FREE_DATA);
1248 for(cp = *linebuf;; ++cp)
1249 if(*cp == '\0'){
1250 ++a_lex_input->li_loff;
1251 i = PTR2SIZE(cp - *linebuf);
1252 break;
1253 }else if(*cp == '\n'){
1254 i = PTR2SIZE(cp - *linebuf);
1255 if(dodup)
1256 *lp = &cp[1];
1257 else{
1258 *lp = sstrdup(&cp[1]);
1259 cp = *linebuf;
1260 *linebuf = sbufdup(*linebuf, i);
1261 free(cp);
1263 break;
1265 *linesize = i;
1266 if(dodup)
1267 *linebuf = sbufdup(*linebuf, i);
1269 iftype = (a_lex_input->li_flags & a_LEX_MACRO_X_OPTION)
1270 ? "-X OPTION" : "MACRO";
1271 n = (int)*linesize;
1272 pstate |= PS_READLINE_NL;
1273 goto jhave_dat;
1275 pstate &= ~PS_READLINE_NL;
1277 iftype = (!(pstate & PS_STARTED) ? "LOAD"
1278 : (pstate & PS_SOURCING) ? "SOURCE" : "READ");
1279 doprompt = ((pstate & (PS_STARTED | PS_ROBOT)) == PS_STARTED &&
1280 (options & OPT_INTERACTIVE));
1281 dotty = (doprompt && !ok_blook(line_editor_disable));
1282 if(!doprompt)
1283 prompt = NULL;
1284 else if(prompt == NULL)
1285 prompt = getprompt();
1287 /* Ensure stdout is flushed first anyway */
1288 if(!dotty && prompt == NULL)
1289 fflush(stdout);
1291 ifile = (a_lex_input != NULL) ? a_lex_input->li_file : stdin;
1292 assert(ifile != NULL);
1294 for(nold = n = 0;;){
1295 if(dotty){
1296 assert(ifile == stdin);
1297 if(string != NULL && (n = (int)strlen(string)) > 0){
1298 if(*linesize > 0)
1299 *linesize += n +1;
1300 else
1301 *linesize = (size_t)n + LINESIZE +1;
1302 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
1303 memcpy(*linebuf, string, (size_t)n +1);
1305 string = NULL;
1306 /* TODO if nold>0, don't redisplay the entire line!
1307 * TODO needs complete redesign ... */
1308 n = (n_tty_readline)(lif, prompt, linebuf, linesize, n
1309 SMALLOC_DEBUG_ARGSCALL);
1310 }else{
1311 if(prompt != NULL) {
1312 if(*prompt != '\0')
1313 fputs(prompt, stdout);
1314 fflush(stdout);
1317 n = (readline_restart)(ifile, linebuf, linesize, n
1318 SMALLOC_DEBUG_ARGSCALL);
1320 if(n > 0 && nold > 0){
1321 int i = 0;
1322 char const *cp = *linebuf + nold;
1324 while(blankspacechar(*cp) && nold + i < n)
1325 ++cp, ++i;
1326 if(i > 0){
1327 memmove(*linebuf + nold, cp, n - nold - i);
1328 n -= i;
1329 (*linebuf)[n] = '\0';
1334 if(n <= 0)
1335 break;
1337 /* POSIX says:
1338 * An unquoted <backslash> at the end of a command line shall
1339 * be discarded and the next line shall continue the command */
1340 if(!(lif & n_LEXINPUT_NL_ESC) || (*linebuf)[n - 1] != '\\'){
1341 if(dotty)
1342 pstate |= PS_READLINE_NL;
1343 break;
1345 /* Definitely outside of quotes, thus the quoting rules are so that an
1346 * uneven number of successive backslashs at EOL is a continuation */
1347 if(n > 1){
1348 size_t i, j;
1350 for(j = 1, i = (size_t)n - 1; i-- > 0; ++j)
1351 if((*linebuf)[i] != '\\')
1352 break;
1353 if(!(j & 1))
1354 break;
1356 (*linebuf)[nold = --n] = '\0';
1357 if(prompt != NULL && *prompt != '\0')
1358 prompt = ".. "; /* XXX PS2 .. */
1361 if(n < 0)
1362 goto jleave;
1363 (*linebuf)[*linesize = n] = '\0';
1365 jhave_dat:
1366 #if 0
1367 if(lif & n_LEXINPUT_DROP_TRAIL_SPC){
1368 char *cp, c;
1369 size_t i;
1371 for(cp = &(*linebuf)[i = (size_t)n];; --i){
1372 c = *--cp;
1373 if(!blankspacechar(c))
1374 break;
1376 (*linebuf)[n = (int)i] = '\0';
1379 if(lif & n_LEXINPUT_DROP_LEAD_SPC){
1380 char *cp, c;
1381 size_t j, i;
1383 for(cp = &(*linebuf)[0], j = (size_t)n, i = 0; i < j; ++i){
1384 c = *cp++;
1385 if(!blankspacechar(c))
1386 break;
1388 if(i > 0){
1389 memcpy(&(*linebuf)[0], &(*linebuf)[i], j -= i);
1390 (*linebuf)[n = (int)j] = '\0';
1393 #endif /* 0 (notyet - must take care for backslash escaped space) */
1395 if(options & OPT_D_VV)
1396 n_err(_("%s %d bytes <%s>\n"), iftype, n, *linebuf);
1397 jleave:
1398 if (pstate & PS_PSTATE_PENDMASK)
1399 a_lex_update_pstate();
1400 NYD2_LEAVE;
1401 return n;
1404 FL char *
1405 n_lex_input_cp(enum n_lexinput_flags lif, char const *prompt,
1406 char const *string){
1407 /* FIXME n_lex_input_cp_addhist(): leaks on sigjmp without linepool */
1408 size_t linesize;
1409 char *linebuf, *rv;
1410 int n;
1411 NYD2_ENTER;
1413 linesize = 0;
1414 linebuf = NULL;
1415 rv = NULL;
1417 n = n_lex_input(lif, prompt, &linebuf, &linesize, string);
1418 if(n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
1419 (lif & n_LEXINPUT_HIST_ADD) && (options & OPT_INTERACTIVE))
1420 n_tty_addhist(rv, ((lif & n_LEXINPUT_HIST_GABBY) != 0));
1422 if(linebuf != NULL)
1423 free(linebuf);
1424 NYD2_LEAVE;
1425 return rv;
1428 FL void
1429 n_load(char const *name){
1430 struct a_lex_input_stack *lip;
1431 size_t i;
1432 FILE *fip;
1433 NYD_ENTER;
1435 if(name == NULL || *name == '\0' || (fip = Fopen(name, "r")) == NULL)
1436 goto jleave;
1438 i = strlen(name) +1;
1439 lip = scalloc(1, sizeof(*lip) -
1440 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) + i);
1441 lip->li_file = fip;
1442 lip->li_flags = a_LEX_FREE;
1443 memcpy(lip->li_name, name, i);
1445 a_lex_load(lip);
1446 pstate &= ~PS_EXIT;
1447 jleave:
1448 NYD_LEAVE;
1451 FL void
1452 n_load_Xargs(char const **lines){
1453 static char const name[] = "-X";
1455 ui8_t buf[sizeof(struct a_lex_input_stack) + sizeof name];
1456 struct a_lex_input_stack *lip;
1457 NYD_ENTER;
1459 memset(buf, 0, sizeof buf);
1460 lip = (void*)buf;
1461 lip->li_flags = a_LEX_MACRO | a_LEX_MACRO_X_OPTION | a_LEX_SUPER_MACRO;
1462 lip->li_lines = UNCONST(lines);
1463 memcpy(lip->li_name, name, sizeof name);
1465 a_lex_load(lip);
1466 if(pstate & PS_EXIT)
1467 exit(EXIT_OK);
1468 NYD_LEAVE;
1471 FL int
1472 c_source(void *v){
1473 int rv;
1474 NYD_ENTER;
1476 rv = (a_lex_source_file(*(char**)v, FAL0) == TRU1) ? 0 : 1;
1477 NYD_LEAVE;
1478 return rv;
1481 FL int
1482 c_source_if(void *v){ /* XXX obsolete?, support file tests in `if' etc.! */
1483 int rv;
1484 NYD_ENTER;
1486 rv = (a_lex_source_file(*(char**)v, TRU1) != FAL0) ? 0 : 1;
1487 NYD_LEAVE;
1488 return rv;
1491 FL bool_t
1492 n_source_macro(enum n_lexinput_flags lif, char const *name, char **lines){
1493 struct a_lex_input_stack *lip;
1494 size_t i;
1495 int rv;
1496 NYD_ENTER;
1498 lip = smalloc(sizeof(*lip) -
1499 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1500 (i = strlen(name) +1));
1501 lip->li_outer = a_lex_input;
1502 lip->li_file = NULL;
1503 lip->li_cond = condstack_release();
1504 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1505 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1506 ? a_LEX_SUPER_MACRO : 0);
1507 lip->li_loff = 0;
1508 lip->li_lines = lines;
1509 memcpy(lip->li_name, name, i);
1511 pstate |= PS_ROBOT;
1512 a_lex_input = lip;
1513 rv = a_commands_recursive(lif);
1514 NYD_LEAVE;
1515 return rv;
1518 FL bool_t
1519 n_source_command(enum n_lexinput_flags lif, char const *cmd){
1520 struct a_lex_input_stack *lip;
1521 size_t i, ial;
1522 bool_t rv;
1523 NYD_ENTER;
1525 i = strlen(cmd);
1526 cmd = sbufdup(cmd, i++);
1527 ial = n_ALIGN(i);
1529 lip = smalloc(sizeof(*lip) -
1530 VFIELD_SIZEOF(struct a_lex_input_stack, li_name) +
1531 ial + 2*sizeof(char*));
1532 lip->li_outer = a_lex_input;
1533 lip->li_file = NULL;
1534 lip->li_cond = condstack_release();
1535 lip->li_flags = a_LEX_FREE | a_LEX_MACRO | a_LEX_MACRO_FREE_DATA |
1536 a_LEX_MACRO_CMD |
1537 (a_lex_input == NULL || (a_lex_input->li_flags & a_LEX_SUPER_MACRO)
1538 ? a_LEX_SUPER_MACRO : 0);
1539 lip->li_loff = 0;
1540 lip->li_lines = (void*)(lip->li_name + ial);
1541 lip->li_lines[0] = UNCONST(cmd); /* dup'ed above */
1542 lip->li_lines[1] = NULL;
1543 memcpy(lip->li_name, cmd, i);
1545 pstate |= PS_ROBOT;
1546 a_lex_input = lip;
1547 rv = a_commands_recursive(lif);
1548 NYD_LEAVE;
1549 return rv;
1552 FL bool_t
1553 n_source_may_yield_control(void){
1554 return ((options & OPT_INTERACTIVE) &&
1555 (pstate & PS_STARTED) &&
1556 (!(pstate & PS_ROBOT) || (pstate & PS_RECURSED)) && /* Ok for ~: */
1557 (a_lex_input == NULL || a_lex_input->li_outer == NULL));
1560 /* s-it-mode */