All: clean cmd_tab entry names, prefix with c_
[s-mailx.git] / lex.c
bloba8f5cad4d910f804be2ea66ea160e9c972ad766c
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ (Lexical processing of) Commands, and the event mainloop.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 #include <fcntl.h>
46 struct cmd {
47 char const *name; /* Name of command */
48 int (*func)(void*); /* Implementor of command */
49 enum argtype argtype; /* Arglist type (see below) */
50 short msgflag; /* Required flags of msgs*/
51 short msgmask; /* Relevant flags of msgs */
52 #ifdef HAVE_DOCSTRINGS
53 int docid; /* Translation id of .doc */
54 char const *doc; /* One line doc for command */
55 #endif
57 /* Yechh, can't initialize unions */
58 #define minargs msgflag /* Minimum argcount for RAWLIST */
59 #define maxargs msgmask /* Max argcount for RAWLIST */
61 struct cmd_ghost {
62 struct cmd_ghost *next;
63 struct str cmd; /* Data follows after .name */
64 char name[VFIELD_SIZE(sizeof(size_t))];
67 static int *_msgvec;
68 static int _reset_on_stop; /* do a reset() if stopped */
69 static sighandler_type _oldpipe;
70 static struct cmd_ghost *_cmd_ghosts;
71 /* _cmd_tab[] after fun protos */
72 static int _lex_inithdr; /* am printing startup headers */
74 /* Update mailname (if name != NULL) and displayname, return wether displayname
75 * was large enough to swallow mailname */
76 static bool_t _update_mailname(char const *name);
77 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_{pre,suf}fix() into one fun! */
78 SINLINE size_t __narrow_prefix(char const *cp, size_t maxl);
79 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
80 #endif
82 /* Isolate the command from the arguments */
83 static char * _lex_isolate(char const *comm);
85 /* Get first-fit, or NULL */
86 static struct cmd const * _lex(char const *comm);
88 /* Command ghost handling */
89 static int _ghost(void *v);
90 static int _unghost(void *v);
92 /* Print a list of all commands */
93 static int _pcmdlist(void *v);
94 static int __pcmd_cmp(void const *s1, void const *s2);
96 /* Print the binaries compiled-in features */
97 static int _features(void *v);
99 /* Print the binaries version number */
100 static int _version(void *v);
102 /* When we wake up after ^Z, reprint the prompt */
103 static void stop(int s);
105 /* Branch here on hangup signal and simulate "exit" */
106 static void hangup(int s);
108 /* List of all commands */
109 static struct cmd const _cmd_tab[] = {
110 #include "cmd_tab.h"
113 #ifdef HAVE_C90AMEND1
114 SINLINE size_t
115 __narrow_prefix(char const *cp, size_t maxl)
117 int err;
118 size_t i, ok;
119 NYD_ENTER;
121 for (err = ok = i = 0; i < maxl;) {
122 int ml = mblen(cp, maxl - i);
123 if (ml < 0) { /* XXX _narrow_prefix(): mblen() error; action? */
124 (void)mblen(NULL, 0);
125 err = 1;
126 ml = 1;
127 } else {
128 if (!err)
129 ok = i;
130 err = 0;
131 if (ml == 0)
132 break;
134 cp += ml;
135 i += ml;
137 NYD_LEAVE;
138 return ok;
141 SINLINE size_t
142 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
144 int err;
145 size_t i, ok;
146 NYD_ENTER;
148 for (err = ok = i = 0; cpl > maxl || err;) {
149 int ml = mblen(cp, cpl);
150 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
151 (void)mblen(NULL, 0);
152 err = 1;
153 ml = 1;
154 } else {
155 if (!err)
156 ok = i;
157 err = 0;
158 if (ml == 0)
159 break;
161 cp += ml;
162 i += ml;
163 cpl -= ml;
165 NYD_LEAVE;
166 return ok;
168 #endif /* HAVE_C90AMEND1 */
170 static bool_t
171 _update_mailname(char const *name)
173 char tbuf[PATH_MAX], *mailp, *dispp;
174 size_t i, j;
175 bool_t rv = TRU1;
176 NYD_ENTER;
178 /* Don't realpath(3) if it's only an update request */
179 if (name != NULL) {
180 #ifdef HAVE_REALPATH
181 enum protocol p = which_protocol(name);
182 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
183 if (realpath(name, mailname) == NULL) {
184 fprintf(stderr, tr(151, "Can't canonicalize `%s'\n"), name);
185 rv = FAL0;
186 goto jdocopy;
188 } else
189 jdocopy:
190 #endif
191 n_strlcpy(mailname, name, sizeof(mailname));
194 mailp = mailname;
195 dispp = displayname;
197 /* Don't display an absolute path but "+FOLDER" if under *folder* */
198 if (getfold(tbuf, sizeof tbuf)) {
199 i = strlen(tbuf);
200 if (i < sizeof(tbuf) - 1)
201 tbuf[i++] = '/';
202 if (strncmp(tbuf, mailp, i) == 0) {
203 mailp += i;
204 *dispp++ = '+';
208 /* We want to see the name of the folder .. on the screen */
209 i = strlen(mailp);
210 if (i < sizeof(displayname) - 1)
211 memcpy(dispp, mailp, i + 1);
212 else {
213 rv = FAL0;
214 /* Avoid disrupting multibyte sequences (if possible) */
215 #ifndef HAVE_C90AMEND1
216 j = sizeof(displayname) / 3 - 1;
217 i -= sizeof(displayname) - (1/* + */ + 3) - j;
218 #else
219 j = __narrow_prefix(mailp, sizeof(displayname) / 3);
220 i = j + __narrow_suffix(mailp + j, i - j,
221 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
222 #endif
223 snprintf(dispp, sizeof(displayname), "%.*s...%s",
224 (int)j, mailp, mailp + i);
226 NYD_LEAVE;
227 return rv;
230 static char *
231 _lex_isolate(char const *comm)
233 NYD_ENTER;
234 while (*comm && strchr(" \t0123456789$^.:/-+*'\",;(`", *comm) == NULL)
235 ++comm;
236 NYD_LEAVE;
237 return UNCONST(comm);
240 static struct cmd const *
241 _lex(char const *comm) /* TODO **command hashtable**! linear list search!!! */
243 struct cmd const *cp;
244 NYD_ENTER;
246 for (cp = _cmd_tab; cp->name != NULL; ++cp)
247 if (*comm == *cp->name && is_prefix(comm, cp->name))
248 goto jleave;
249 cp = NULL;
250 jleave:
251 NYD_LEAVE;
252 return cp;
255 static int
256 _ghost(void *v)
258 char const **argv = (char const **)v;
259 struct cmd_ghost *lcg, *cg;
260 size_t nl, cl;
261 NYD_ENTER;
263 /* Show the list? */
264 if (*argv == NULL) {
265 printf(tr(144, "Command ghosts are:\n"));
266 for (nl = 0, cg = _cmd_ghosts; cg != NULL; cg = cg->next) {
267 cl = strlen(cg->name) + 5 + cg->cmd.l + 3;
268 nl += cl;
269 if (UICMP(z, nl, >=, scrnwidth)) {
270 nl = cl;
271 printf("\n");
273 printf((cg->next != NULL ? "%s -> <%s>, " : "%s -> <%s>\n"),
274 cg->name, cg->cmd.s);
276 v = NULL;
277 goto jleave;
280 /* Request to add new ghost */
281 if (argv[1] == NULL || argv[1][0] == '\0' || argv[2] != NULL) {
282 fprintf(stderr, tr(159, "Usage: %s\n"),
283 tr(425, "Define a <ghost> of <command>, or list all ghosts"));
284 v = NULL;
285 goto jleave;
288 /* Check that we can deal with this one */
289 switch (argv[0][0]) {
290 case '|':
291 case '~':
292 case '?':
293 case '#':
294 /* FALLTHRU */
295 case '\0':
296 goto jecanon;
297 default:
298 if (argv[0] == _lex_isolate(argv[0])) {
299 jecanon:
300 fprintf(stderr, tr(151, "Can't canonicalize `%s'\n"), argv[0]);
301 v = NULL;
302 goto jleave;
304 break;
307 /* Always recreate */
308 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
309 if (!strcmp(cg->name, argv[0])) {
310 if (lcg != NULL)
311 lcg->next = cg->next;
312 else
313 _cmd_ghosts = cg->next;
314 free(cg);
315 break;
318 /* Need a new one */
319 nl = strlen(argv[0]) + 1;
320 cl = strlen(argv[1]) + 1;
321 cg = smalloc(sizeof(*cg) - VFIELD_SIZEOF(struct cmd_ghost, name) + nl + cl);
322 cg->next = _cmd_ghosts;
323 memcpy(cg->name, argv[0], nl);
324 cg->cmd.s = cg->name + nl;
325 cg->cmd.l = cl - 1;
326 memcpy(cg->cmd.s, argv[1], cl);
328 _cmd_ghosts = cg;
329 jleave:
330 NYD_LEAVE;
331 return (v == NULL);
334 static int
335 _unghost(void *v)
337 int rv = 0;
338 char const **argv = v, *cp;
339 struct cmd_ghost *lcg, *cg;
340 NYD_ENTER;
342 while ((cp = *argv++) != NULL) {
343 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
344 if (!strcmp(cg->name, cp)) {
345 if (lcg != NULL)
346 lcg->next = cg->next;
347 else
348 _cmd_ghosts = cg->next;
349 free(cg);
350 goto jouter;
352 fprintf(stderr, tr(91, "Unknown command: `%s'\n"), cp);
353 rv = 1;
354 jouter:
357 NYD_LEAVE;
358 return rv;
361 static int
362 __pcmd_cmp(void const *s1, void const *s2)
364 struct cmd const * const *c1 = s1, * const *c2 = s2;
365 int rv;
366 NYD_ENTER;
368 rv = strcmp((*c1)->name, (*c2)->name);
369 NYD_LEAVE;
370 return rv;
373 static int
374 _pcmdlist(void *v)
376 struct cmd const **cpa, *cp, **cursor;
377 size_t i;
378 NYD_ENTER;
379 UNUSED(v);
381 for (i = 0; _cmd_tab[i].name != NULL; ++i)
383 ++i;
384 cpa = ac_alloc(sizeof(cp) * i);
386 for (i = 0; (cp = _cmd_tab + i)->name != NULL; ++i)
387 cpa[i] = cp;
388 cpa[i] = NULL;
390 qsort(cpa, i, sizeof(cp), &__pcmd_cmp);
392 printf(tr(14, "Commands are:\n"));
393 for (i = 0, cursor = cpa; (cp = *cursor++) != NULL;) {
394 size_t j;
395 if (cp->func == &c_cmdnotsupp)
396 continue;
397 j = strlen(cp->name) + 2;
398 if ((i += j) > 72) {
399 i = j;
400 printf("\n");
402 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->name);
405 ac_free(cpa);
406 NYD_LEAVE;
407 return 0;
410 static int
411 _features(void *v)
413 NYD_ENTER;
414 UNUSED(v);
415 printf(tr(523, "Features: %s\n"), features);
416 NYD_LEAVE;
417 return 0;
420 static int
421 _version(void *v)
423 NYD_ENTER;
424 UNUSED(v);
425 printf(tr(111, "Version %s\n"), version);
426 NYD_LEAVE;
427 return 0;
430 static void
431 stop(int s)
433 sighandler_type old_action;
434 sigset_t nset;
435 NYD_X; /* Signal handler */
437 old_action = safe_signal(s, SIG_DFL);
439 sigemptyset(&nset);
440 sigaddset(&nset, s);
441 sigprocmask(SIG_UNBLOCK, &nset, NULL);
442 kill(0, s);
443 sigprocmask(SIG_BLOCK, &nset, NULL);
444 safe_signal(s, old_action);
445 if (_reset_on_stop) {
446 _reset_on_stop = 0;
447 reset(0);
451 static void
452 hangup(int s)
454 NYD_X; /* Signal handler */
455 UNUSED(s);
456 /* nothing to do? */
457 exit(1);
460 FL int
461 setfile(char const *name, int nmail) /* TODO oh my god */
463 static int shudclob;
465 struct stat stb;
466 struct flock flp;
467 FILE *ibuf = NULL;
468 int rv, i, compressed = 0, omsgCount = 0;
469 bool_t isedit;
470 char const *who;
471 size_t offset;
472 struct shortcut *sh;
473 NYD_ENTER;
475 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
476 * necessary to make a mailbox accessible by a different user, and if that
477 * has happened, let's just let the usual file perms decide */
478 who = (name[1] != '\0') ? name + 1 : myname;
479 isedit = (*name != '%' && ((sh = get_shortcut(name)) == NULL ||
480 *sh->sh_long != '%'));
481 if ((name = expand(name)) == NULL)
482 goto jem1;
484 switch (which_protocol(name)) {
485 case PROTO_FILE:
486 break;
487 case PROTO_MAILDIR:
488 rv = maildir_setfile(name, nmail, isedit);
489 goto jleave;
490 #ifdef HAVE_POP3
491 case PROTO_POP3:
492 shudclob = 1;
493 rv = pop3_setfile(name, nmail, isedit);
494 goto jleave;
495 #endif
496 #ifdef HAVE_IMAP
497 case PROTO_IMAP:
498 shudclob = 1;
499 if (nmail && mb.mb_type == MB_CACHE)
500 rv = 1;
501 else
502 rv = imap_setfile(name, nmail, isedit);
503 goto jleave;
504 #endif
505 default:
506 fprintf(stderr, tr(217, "Cannot handle protocol: %s\n"), name);
507 goto jem1;
510 /* FIXME this FILE leaks if quit()->edstop() reset()s! This entire code
511 * FIXME here is total crap, below we open(2) the same name again just to
512 * FIXME close it right away etc. The normal thing would be to (1) finalize
513 * FIXME the current box and (2) open the new box; yet, since (2) may fail
514 * FIXME we terribly need our VOID box to make this logic order possible! */
515 if ((ibuf = Zopen(name, "r", &compressed)) == NULL) {
516 if ((!isedit && errno == ENOENT) || nmail) {
517 if (nmail)
518 goto jnonmail;
519 goto jnomail;
521 perror(name);
522 goto jem1;
525 if (fstat(fileno(ibuf), &stb) < 0) {
526 if (nmail)
527 goto jnonmail;
528 perror("fstat");
529 goto jem1;
532 if (S_ISDIR(stb.st_mode)) {
533 if (nmail)
534 goto jnonmail;
535 errno = EISDIR;
536 perror(name);
537 goto jem1;
538 } else if (S_ISREG(stb.st_mode)) {
539 /*EMPTY*/
540 } else {
541 if (nmail)
542 goto jnonmail;
543 errno = EINVAL;
544 perror(name);
545 goto jem1;
548 /* Looks like all will be well. We must now relinquish our hold on the
549 * current set of stuff. Must hold signals while we are reading the new
550 * file, else we will ruin the message[] data structure */
552 hold_sigs(); /* TODO note on this one in quit.c:quit() */
553 if (shudclob && !nmail)
554 quit();
555 #ifdef HAVE_SOCKETS
556 if (!nmail && mb.mb_sock.s_fd >= 0)
557 sclose(&mb.mb_sock);
558 #endif
560 /* Copy the messages into /tmp and set pointers */
561 flp.l_type = F_RDLCK;
562 flp.l_start = 0;
563 flp.l_whence = SEEK_SET;
564 if (!nmail) {
565 mb.mb_type = MB_FILE;
566 mb.mb_perm = (options & OPT_R_FLAG) ? 0 : MB_DELE | MB_EDIT;
567 mb.mb_compressed = compressed;
568 if (compressed) {
569 if (compressed & 0200)
570 mb.mb_perm = 0;
571 } else {
572 if ((i = open(name, O_WRONLY)) < 0)
573 mb.mb_perm = 0;
574 else
575 close(i);
577 if (shudclob) {
578 if (mb.mb_itf) {
579 fclose(mb.mb_itf);
580 mb.mb_itf = NULL;
582 if (mb.mb_otf) {
583 fclose(mb.mb_otf);
584 mb.mb_otf = NULL;
587 shudclob = 1;
588 edit = isedit;
589 initbox(name);
590 offset = 0;
591 flp.l_len = 0;
592 if (!edit && fcntl(fileno(ibuf), F_SETLKW, &flp) == -1) {
593 perror("Unable to lock mailbox");
594 rele_sigs();
595 goto jem1;
597 } else /* nmail */{
598 fseek(mb.mb_otf, 0L, SEEK_END);
599 fseek(ibuf, mailsize, SEEK_SET);
600 offset = mailsize;
601 omsgCount = msgCount;
602 flp.l_len = offset;
603 if (!edit && fcntl(fileno(ibuf), F_SETLKW, &flp) == -1) {
604 rele_sigs();
605 goto jnonmail;
608 mailsize = fsize(ibuf);
610 if (nmail && UICMP(z, mailsize, <=, offset)) {
611 rele_sigs();
612 goto jnonmail;
614 setptr(ibuf, offset);
615 setmsize(msgCount);
616 if (nmail && mb.mb_sorted) {
617 mb.mb_threaded = 0;
618 c_sort((void*)-1);
621 Fclose(ibuf);
622 ibuf = NULL;
623 rele_sigs();
624 if (!nmail)
625 sawcom = FAL0;
627 if ((!edit || nmail) && msgCount == 0) {
628 jnonmail:
629 if (!nmail) {
630 if (!ok_blook(emptystart))
631 jnomail:
632 fprintf(stderr, tr(88, "No mail for %s\n"), who);
634 rv = 1;
635 goto jleave;
637 if (nmail)
638 newmailinfo(omsgCount);
640 rv = 0;
641 jleave:
642 if (ibuf != NULL)
643 Fclose(ibuf);
644 NYD_LEAVE;
645 return rv;
646 jem1:
647 rv = -1;
648 goto jleave;
651 FL int
652 newmailinfo(int omsgCount)
654 int mdot, i;
655 NYD_ENTER;
657 for (i = 0; i < omsgCount; i++)
658 message[i].m_flag &= ~MNEWEST;
659 if (msgCount > omsgCount) {
660 for (i = omsgCount; i < msgCount; i++)
661 message[i].m_flag |= MNEWEST;
662 printf(tr(158, "New mail has arrived.\n"));
663 if (msgCount - omsgCount == 1)
664 printf(tr(214, "Loaded 1 new message.\n"));
665 else
666 printf(tr(215, "Loaded %d new messages.\n"), msgCount - omsgCount);
667 } else
668 printf(tr(224, "Loaded %d messages.\n"), msgCount);
669 callhook(mailname, 1);
670 mdot = getmdot(1);
671 if (ok_blook(header))
672 print_headers(omsgCount + 1, msgCount);
673 NYD_LEAVE;
674 return mdot;
677 FL void
678 commands(void)
680 struct eval_ctx ev;
681 int n;
682 NYD_ENTER;
684 if (!sourcing) {
685 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
686 safe_signal(SIGINT, onintr);
687 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
688 safe_signal(SIGHUP, hangup);
689 /* TODO We do a lot of redundant signal handling, especially
690 * TODO with the command line editor(s); try to merge this */
691 safe_signal(SIGTSTP, stop);
692 safe_signal(SIGTTOU, stop);
693 safe_signal(SIGTTIN, stop);
695 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
696 safe_signal(SIGPIPE, _oldpipe);
698 memset(&ev, 0, sizeof ev);
700 setexit();
701 for (;;) {
702 interrupts = 0;
703 handlerstacktop = NULL;
705 if (!sourcing && (options & OPT_INTERACTIVE)) {
706 char *cp;
708 if ((cp = ok_vlook(newmail)) == NULL)
709 cp = ok_vlook(autoinc); /* TODO legacy */
710 if ((options & OPT_TTYIN) && (cp != NULL || mb.mb_type == MB_IMAP)) {
711 struct stat st;
713 n = (cp != NULL && strcmp(cp, "noimap") && strcmp(cp, "nopoll"));
714 if ((mb.mb_type == MB_FILE && stat(mailname, &st) == 0 &&
715 st.st_size > mailsize) ||
716 #ifdef HAVE_IMAP
717 (mb.mb_type == MB_IMAP && imap_newmail(n) > (cp == NULL)) ||
718 #endif
719 (mb.mb_type == MB_MAILDIR && n != 0)) {
720 size_t odot = PTR2SIZE(dot - message);
721 bool_t odid = did_print_dot;
723 setfile(mailname, 1);
724 if (mb.mb_type != MB_IMAP) {
725 dot = message + odot;
726 did_print_dot = odid;
731 _reset_on_stop = 1;
732 exit_status = 0;
735 #ifdef HAVE_COLOUR
736 colour_table = NULL; /* XXX intermediate hack */
737 #endif
738 sreset(sourcing);
739 if (!sourcing) {
740 char *cp;
742 /* TODO Note: this buffer may contain a password. We should redefine
743 * TODO the code flow which has to do that */
744 if ((cp = termios_state.ts_linebuf) != NULL) {
745 termios_state.ts_linebuf = NULL;
746 termios_state.ts_linesize = 0;
747 free(cp); /* TODO pool give-back */
749 /* TODO Due to expand-on-tab of NCL the buffer may grow */
750 if (ev.ev_line.l > LINESIZE * 3) {
751 free(ev.ev_line.s); /* TODO pool! but what? */
752 ev.ev_line.s = NULL;
753 ev.ev_line.l = 0;
757 /* Read a line of commands and handle end of file specially */
758 jreadline:
759 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
760 ev.ev_new_content);
761 _reset_on_stop = 0;
762 if (n < 0) {
763 /* EOF */
764 if (loading)
765 break;
766 if (sourcing) {
767 unstack();
768 continue;
770 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
771 printf(tr(89, "Use `quit' to quit.\n"));
772 continue;
774 break;
777 inhook = 0;
778 if (evaluate(&ev))
779 break;
780 if (exit_status != EXIT_OK && (options & OPT_BATCH_FLAG) &&
781 ok_blook(batch_exit_on_error))
782 break;
783 if (!sourcing && (options & OPT_INTERACTIVE)) {
784 if (ev.ev_new_content != NULL)
785 goto jreadline;
786 if (ev.ev_add_history)
787 tty_addhist(ev.ev_line.s);
791 if (ev.ev_line.s != NULL)
792 free(ev.ev_line.s);
793 if (sourcing)
794 sreset(FAL0);
795 NYD_LEAVE;
798 FL int
799 execute(char *linebuf, int contxt, size_t linesize) /* XXX LEGACY */
801 struct eval_ctx ev;
802 #ifdef HAVE_COLOUR
803 struct colour_table *ct_save;
804 #endif
805 int rv;
806 NYD_ENTER;
808 /* TODO Maybe recursion from within collect.c! As long as we don't have
809 * TODO a value carrier that transports the entire state of a recursion
810 * TODO we need to save away also the colour table */
811 #ifdef HAVE_COLOUR
812 ct_save = colour_table;
813 colour_table = NULL;
814 #endif
816 memset(&ev, 0, sizeof ev);
817 ev.ev_line.s = linebuf;
818 ev.ev_line.l = linesize;
819 ev.ev_is_recursive = (contxt != 0);
820 rv = evaluate(&ev);
822 #ifdef HAVE_COLOUR
823 colour_table = ct_save;
824 #endif
825 NYD_LEAVE;
826 return rv;
829 FL int
830 evaluate(struct eval_ctx *evp)
832 struct str line;
833 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
834 struct cmd_ghost *cg = NULL;
835 struct cmd const *com = NULL;
836 int muvec[2], c, e = 1;
837 NYD_ENTER;
839 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
840 evp->ev_add_history = FAL0;
841 evp->ev_new_content = NULL;
843 /* Command ghosts that refer to shell commands or macro expansion restart */
844 jrestart:
846 /* Strip the white space away from the beginning of the command */
847 for (cp = line.s; whitechar(*cp); ++cp)
849 line.l -= PTR2SIZE(cp - line.s);
851 /* Ignore comments */
852 if (*cp == '#')
853 goto jleave0;
855 /* Handle ! differently to get the correct lexical conventions */
856 if (*cp == '!') {
857 if (sourcing) {
858 fprintf(stderr, tr(90, "Can't `!' while sourcing\n"));
859 goto jleave;
861 c_shell(++cp);
862 evp->ev_add_history = TRU1;
863 goto jleave0;
866 /* Isolate the actual command; since it may not necessarily be
867 * separated from the arguments (as in `p1') we need to duplicate it to
868 * be able to create a NUL terminated version.
869 * We must be aware of several special one letter commands here */
870 arglist[0] = cp;
871 switch (*cp) {
872 case '|':
873 case '~':
874 case '?':
875 ++cp;
876 /* FALLTHRU */
877 case '\0':
878 break;
879 default:
880 cp = _lex_isolate(cp);
881 break;
883 c = (int)PTR2SIZE(cp - arglist[0]);
884 line.l -= c;
885 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c + 1);
886 memcpy(word, arglist[0], c);
887 word[c] = '\0';
889 /* Look up the command; if not found, bitch.
890 * Normally, a blank command would map to the first command in the
891 * table; while sourcing, however, we ignore blank lines to eliminate
892 * confusion; act just the same for ghosts */
893 if (*word == '\0') {
894 if (sourcing || cg != NULL)
895 goto jleave0;
896 com = _cmd_tab + 0;
897 goto jexec;
900 /* If this is the first evaluation, check command ghosts */
901 if (cg == NULL) {
902 /* TODO relink list head, so it's sorted on usage over time?
903 * TODO in fact, there should be one hashmap over all commands and ghosts
904 * TODO so that the lookup could be made much more efficient than it is
905 * TODO now (two adjacent list searches! */
906 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
907 if (!strcmp(word, cg->name)) {
908 if (line.l > 0) {
909 size_t i = cg->cmd.l;
910 line.s = salloc(i + 1 + line.l +1);
911 memcpy(line.s, cg->cmd.s, i);
912 line.s[i++] = ' ';
913 memcpy(line.s + i, cp, line.l);
914 line.s[i += line.l] = '\0';
915 line.l = i;
916 } else {
917 line.s = cg->cmd.s;
918 line.l = cg->cmd.l;
920 goto jrestart;
924 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
925 fprintf(stderr, tr(91, "Unknown command: `%s'\n"), word);
926 if (com != NULL) {
927 c_cmdnotsupp(NULL);
928 com = NULL;
930 goto jleave;
933 /* See if we should execute the command -- if a conditional we always
934 * execute it, otherwise, check the state of cond */
935 jexec:
936 if (!(com->argtype & ARG_F)) {
937 switch (cond_state) {
938 case COND_RCV:
939 if (options & OPT_SENDMODE)
940 goto jleave0;
941 break;
942 case COND_SEND:
943 if (!(options & OPT_SENDMODE))
944 goto jleave0;
945 break;
946 case COND_TERM:
947 if (!(options & OPT_TTYIN))
948 goto jleave0;
949 break;
950 case COND_NOTERM:
951 if (options & OPT_TTYIN)
952 goto jleave0;
953 break;
954 case COND_ANY:
955 case COND_EXEC:
956 break;
957 case COND_NOEXEC:
958 goto jleave0;
962 /* Process the arguments to the command, depending on the type he expects.
963 * Default to an error.
964 * If we are sourcing an interactive command, it's an error */
965 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
966 fprintf(stderr, tr(92, "May not execute `%s' while sending\n"),
967 com->name);
968 goto jleave;
970 if (sourcing && (com->argtype & ARG_I)) {
971 fprintf(stderr, tr(93, "May not execute `%s' while sourcing\n"),
972 com->name);
973 goto jleave;
975 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
976 fprintf(stderr, tr(94, "May not execute `%s' -- "
977 "message file is read only\n"), com->name);
978 goto jleave;
980 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
981 fprintf(stderr, tr(95, "Cannot recursively invoke `%s'\n"), com->name);
982 goto jleave;
984 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
985 fprintf(stderr, tr(257, "Cannot execute `%s' without active mailbox\n"),
986 com->name);
987 goto jleave;
990 if (com->argtype & ARG_V)
991 temporary_arg_v_store = NULL;
993 switch (com->argtype & ARG_ARGMASK) {
994 case ARG_MSGLIST:
995 /* Message list defaulting to nearest forward legal message */
996 if (_msgvec == 0)
997 goto je96;
998 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
999 break;
1000 if (c == 0) {
1001 *_msgvec = first(com->msgflag, com->msgmask);
1002 if (*_msgvec != 0)
1003 _msgvec[1] = 0;
1005 if (*_msgvec == 0) {
1006 if (!inhook)
1007 printf(tr(97, "No applicable messages\n"));
1008 break;
1010 e = (*com->func)(_msgvec);
1011 break;
1013 case ARG_NDMLIST:
1014 /* Message list with no defaults, but no error if none exist */
1015 if (_msgvec == 0) {
1016 je96:
1017 fprintf(stderr, tr(96, "Illegal use of `message list'\n"));
1018 break;
1020 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1021 break;
1022 e = (*com->func)(_msgvec);
1023 break;
1025 case ARG_STRLIST:
1026 /* Just the straight string, with leading blanks removed */
1027 while (whitechar(*cp))
1028 ++cp;
1029 e = (*com->func)(cp);
1030 break;
1032 case ARG_RAWLIST:
1033 case ARG_ECHOLIST:
1034 /* A vector of strings, in shell style */
1035 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
1036 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
1037 break;
1038 if (c < com->minargs) {
1039 fprintf(stderr, tr(99, "`%s' requires at least %d arg(s)\n"),
1040 com->name, com->minargs);
1041 break;
1043 if (c > com->maxargs) {
1044 fprintf(stderr, tr(100, "`%s' takes no more than %d arg(s)\n"),
1045 com->name, com->maxargs);
1046 break;
1048 e = (*com->func)(arglist);
1049 break;
1051 case ARG_NOLIST:
1052 /* Just the constant zero, for exiting, eg. */
1053 e = (*com->func)(0);
1054 break;
1056 default:
1057 panic(tr(101, "Unknown argument type"));
1060 if (e == 0 && (com->argtype & ARG_V) &&
1061 (cp = temporary_arg_v_store) != NULL) {
1062 temporary_arg_v_store = NULL;
1063 evp->ev_new_content = cp;
1064 goto jleave0;
1066 if (!(com->argtype & ARG_H) && !list_saw_numbers)
1067 evp->ev_add_history = TRU1;
1069 jleave:
1070 /* Exit the current source file on error */
1071 if ((exec_last_comm_error = (e != 0))) {
1072 if (e < 0 || loading) {
1073 e = 1;
1074 goto jret;
1076 if (sourcing)
1077 unstack();
1078 goto jret0;
1080 if (com == NULL)
1081 goto jret0;
1082 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1083 if (visible(dot)) {
1084 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1085 muvec[1] = 0;
1086 c_type(muvec);
1088 if (!sourcing && !inhook && (com->argtype & ARG_T) == 0)
1089 sawcom = TRU1;
1090 jleave0:
1091 exec_last_comm_error = 0;
1092 jret0:
1093 e = 0;
1094 jret:
1095 NYD_LEAVE;
1096 return e;
1099 FL void
1100 setmsize(int sz)
1102 NYD_ENTER;
1103 if (_msgvec != 0)
1104 free(_msgvec);
1105 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1106 NYD_LEAVE;
1109 FL void
1110 onintr(int s)
1112 NYD_X; /* Signal handler */
1114 if (handlerstacktop != NULL) {
1115 handlerstacktop(s);
1116 return;
1118 safe_signal(SIGINT, onintr);
1119 noreset = 0;
1120 if (!_lex_inithdr)
1121 sawcom = TRU1;
1122 _lex_inithdr = 0;
1123 while (sourcing)
1124 unstack();
1126 termios_state_reset();
1127 close_all_files();
1129 if (image >= 0) {
1130 close(image);
1131 image = -1;
1133 if (interrupts != 1)
1134 fprintf(stderr, tr(102, "Interrupt\n"));
1135 safe_signal(SIGPIPE, _oldpipe);
1136 reset(0);
1139 FL void
1140 announce(int printheaders)
1142 int vec[2], mdot;
1143 NYD_ENTER;
1145 mdot = newfileinfo();
1146 vec[0] = mdot;
1147 vec[1] = 0;
1148 dot = &message[mdot - 1];
1149 if (printheaders && msgCount > 0 && ok_blook(header)) {
1150 ++_lex_inithdr;
1151 c_headers(vec);
1152 _lex_inithdr = 0;
1154 NYD_LEAVE;
1157 FL int
1158 newfileinfo(void)
1160 struct message *mp;
1161 int u, n, mdot, d, s, hidden, moved;
1162 NYD_ENTER;
1164 if (mb.mb_type == MB_VOID) {
1165 mdot = 1;
1166 goto jleave;
1169 mdot = getmdot(0);
1170 s = d = hidden = moved =0;
1171 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1172 if (mp->m_flag & MNEW)
1173 ++n;
1174 if ((mp->m_flag & MREAD) == 0)
1175 ++u;
1176 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1177 ++moved;
1178 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1179 ++d;
1180 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1181 ++s;
1182 if (mp->m_flag & MHIDDEN)
1183 ++hidden;
1186 /* If displayname gets truncated the user effectively has no option to see
1187 * the full pathname of the mailbox, so print it at least for '? fi' */
1188 printf(tr(103, "\"%s\": "),
1189 (_update_mailname(NULL) ? displayname : mailname));
1190 if (msgCount == 1)
1191 printf(tr(104, "1 message"));
1192 else
1193 printf(tr(105, "%d messages"), msgCount);
1194 if (n > 0)
1195 printf(tr(106, " %d new"), n);
1196 if (u-n > 0)
1197 printf(tr(107, " %d unread"), u);
1198 if (d > 0)
1199 printf(tr(108, " %d deleted"), d);
1200 if (s > 0)
1201 printf(tr(109, " %d saved"), s);
1202 if (moved > 0)
1203 printf(tr(136, " %d moved"), moved);
1204 if (hidden > 0)
1205 printf(tr(139, " %d hidden"), hidden);
1206 if (mb.mb_type == MB_CACHE)
1207 printf(" [Disconnected]");
1208 else if (mb.mb_perm == 0)
1209 printf(tr(110, " [Read only]"));
1210 printf("\n");
1211 jleave:
1212 NYD_LEAVE;
1213 return mdot;
1216 FL int
1217 getmdot(int nmail)
1219 struct message *mp;
1220 char *cp;
1221 int mdot;
1222 enum mflag avoid = MHIDDEN | MDELETED;
1223 NYD_ENTER;
1225 if (!nmail) {
1226 if (ok_blook(autothread))
1227 c_thread(NULL);
1228 else if ((cp = ok_vlook(autosort)) != NULL) {
1229 free(mb.mb_sorted);
1230 mb.mb_sorted = sstrdup(cp);
1231 c_sort(NULL);
1234 if (mb.mb_type == MB_VOID) {
1235 mdot = 1;
1236 goto jleave;
1239 if (nmail)
1240 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1241 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1242 break;
1244 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1245 if (mb.mb_threaded) {
1246 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1247 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1248 break;
1249 } else {
1250 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1251 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1252 break;
1256 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1257 if (mb.mb_threaded) {
1258 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1259 if (mp->m_flag & MFLAGGED)
1260 break;
1261 } else {
1262 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1263 if (mp->m_flag & MFLAGGED)
1264 break;
1268 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1269 if (mb.mb_threaded) {
1270 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1271 if (!(mp->m_flag & (MREAD | avoid)))
1272 break;
1273 } else {
1274 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1275 if (!(mp->m_flag & (MREAD | avoid)))
1276 break;
1280 if ((mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1281 mdot = (int)PTR2SIZE(mp - message + 1);
1282 else if (ok_blook(showlast)) {
1283 if (mb.mb_threaded) {
1284 for (mp = this_in_thread(threadroot, -1); mp;
1285 mp = prev_in_thread(mp))
1286 if (!(mp->m_flag & avoid))
1287 break;
1288 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1289 } else {
1290 for (mp = message + msgCount - 1; mp >= message; --mp)
1291 if (!(mp->m_flag & avoid))
1292 break;
1293 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1295 } else if (mb.mb_threaded) {
1296 for (mp = threadroot; mp; mp = next_in_thread(mp))
1297 if (!(mp->m_flag & avoid))
1298 break;
1299 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1300 } else {
1301 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1302 if (!(mp->m_flag & avoid))
1303 break;
1304 mdot = PTRCMP(mp, <, message + msgCount)
1305 ? (int)PTR2SIZE(mp - message + 1) : 1;
1307 jleave:
1308 NYD_LEAVE;
1309 return mdot;
1312 FL void
1313 initbox(char const *name)
1315 char *tempMesg;
1316 NYD_ENTER;
1318 if (mb.mb_type != MB_VOID)
1319 n_strlcpy(prevfile, mailname, PATH_MAX);
1321 _update_mailname(name != mailname ? name : NULL);
1323 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpbox", OF_WRONLY | OF_HOLDSIGS, 0600)) ==
1324 NULL) {
1325 perror(tr(87, "temporary mail message file"));
1326 exit(1);
1328 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1329 perror(tr(87, "temporary mail message file"));
1330 exit(1);
1332 Ftmp_release(&tempMesg);
1334 msgCount = 0;
1335 if (message) {
1336 free(message);
1337 message = NULL;
1338 msgspace = 0;
1340 mb.mb_threaded = 0;
1341 if (mb.mb_sorted != NULL) {
1342 free(mb.mb_sorted);
1343 mb.mb_sorted = NULL;
1345 #ifdef HAVE_IMAP
1346 mb.mb_flags = MB_NOFLAGS;
1347 #endif
1348 prevdot = NULL;
1349 dot = NULL;
1350 did_print_dot = FAL0;
1351 NYD_LEAVE;
1354 #ifdef HAVE_DOCSTRINGS
1355 FL bool_t
1356 print_comm_docstr(char const *comm)
1358 bool_t rv = FAL0;
1359 struct cmd_ghost *cg;
1360 struct cmd const *cp;
1361 NYD_ENTER;
1363 /* Ghosts take precedence */
1364 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
1365 if (!strcmp(comm, cg->name)) {
1366 printf("%s -> <%s>\n", comm, cg->cmd.s);
1367 rv = TRU1;
1368 goto jleave;
1371 for (cp = _cmd_tab; cp->name != NULL; ++cp) {
1372 if (cp->func == &c_cmdnotsupp)
1373 continue;
1374 if (!strcmp(comm, cp->name))
1375 printf("%s: %s\n", comm, tr(cp->docid, cp->doc));
1376 else if (is_prefix(comm, cp->name))
1377 printf("%s (%s): %s\n", comm, cp->name, tr(cp->docid, cp->doc));
1378 else
1379 continue;
1380 rv = TRU1;
1381 break;
1383 jleave:
1384 NYD_LEAVE;
1385 return rv;
1387 #endif
1389 /* vim:set fenc=utf-8:s-it-mode */