Review: imap_search.c
[s-mailx.git] / lex.c
blob60122929f88a03233d1b0fdd17356b74efc2abad
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)) {
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 != '\0' &&
235 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
236 ++comm;
237 NYD_LEAVE;
238 return UNCONST(comm);
241 static struct cmd const *
242 _lex(char const *comm) /* TODO **command hashtable**! linear list search!!! */
244 struct cmd const *cp;
245 NYD_ENTER;
247 for (cp = _cmd_tab; cp->name != NULL; ++cp)
248 if (*comm == *cp->name && is_prefix(comm, cp->name))
249 goto jleave;
250 cp = NULL;
251 jleave:
252 NYD_LEAVE;
253 return cp;
256 static int
257 _ghost(void *v)
259 char const **argv = (char const **)v;
260 struct cmd_ghost *lcg, *cg;
261 size_t nl, cl;
262 NYD_ENTER;
264 /* Show the list? */
265 if (*argv == NULL) {
266 printf(tr(144, "Command ghosts are:\n"));
267 for (nl = 0, cg = _cmd_ghosts; cg != NULL; cg = cg->next) {
268 cl = strlen(cg->name) + 5 + cg->cmd.l + 3;
269 nl += cl;
270 if (UICMP(z, nl, >=, scrnwidth)) {
271 nl = cl;
272 printf("\n");
274 printf((cg->next != NULL ? "%s -> <%s>, " : "%s -> <%s>\n"),
275 cg->name, cg->cmd.s);
277 v = NULL;
278 goto jleave;
281 /* Request to add new ghost */
282 if (argv[1] == NULL || argv[1][0] == '\0' || argv[2] != NULL) {
283 fprintf(stderr, tr(159, "Usage: %s\n"),
284 tr(425, "Define a <ghost> of <command>, or list all ghosts"));
285 v = NULL;
286 goto jleave;
289 /* Check that we can deal with this one */
290 if (argv[0] == _lex_isolate(argv[0])) {
291 fprintf(stderr, tr(151, "Can't canonicalize `%s'\n"), argv[0]);
292 v = NULL;
293 goto jleave;
296 /* Always recreate */
297 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
298 if (!strcmp(cg->name, argv[0])) {
299 if (lcg != NULL)
300 lcg->next = cg->next;
301 else
302 _cmd_ghosts = cg->next;
303 free(cg);
304 break;
307 /* Need a new one */
308 nl = strlen(argv[0]) + 1;
309 cl = strlen(argv[1]) + 1;
310 cg = smalloc(sizeof(*cg) - VFIELD_SIZEOF(struct cmd_ghost, name) + nl + cl);
311 cg->next = _cmd_ghosts;
312 memcpy(cg->name, argv[0], nl);
313 cg->cmd.s = cg->name + nl;
314 cg->cmd.l = cl - 1;
315 memcpy(cg->cmd.s, argv[1], cl);
317 _cmd_ghosts = cg;
318 jleave:
319 NYD_LEAVE;
320 return (v == NULL);
323 static int
324 _unghost(void *v)
326 int rv = 0;
327 char const **argv = v, *cp;
328 struct cmd_ghost *lcg, *cg;
329 NYD_ENTER;
331 while ((cp = *argv++) != NULL) {
332 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
333 if (!strcmp(cg->name, cp)) {
334 if (lcg != NULL)
335 lcg->next = cg->next;
336 else
337 _cmd_ghosts = cg->next;
338 free(cg);
339 goto jouter;
341 fprintf(stderr, tr(91, "Unknown command: `%s'\n"), cp);
342 rv = 1;
343 jouter:
346 NYD_LEAVE;
347 return rv;
350 static int
351 __pcmd_cmp(void const *s1, void const *s2)
353 struct cmd const * const *c1 = s1, * const *c2 = s2;
354 int rv;
355 NYD_ENTER;
357 rv = strcmp((*c1)->name, (*c2)->name);
358 NYD_LEAVE;
359 return rv;
362 static int
363 _pcmdlist(void *v)
365 struct cmd const **cpa, *cp, **cursor;
366 size_t i;
367 NYD_ENTER;
368 UNUSED(v);
370 for (i = 0; _cmd_tab[i].name != NULL; ++i)
372 ++i;
373 cpa = ac_alloc(sizeof(cp) * i);
375 for (i = 0; (cp = _cmd_tab + i)->name != NULL; ++i)
376 cpa[i] = cp;
377 cpa[i] = NULL;
379 qsort(cpa, i, sizeof(cp), &__pcmd_cmp);
381 printf(tr(14, "Commands are:\n"));
382 for (i = 0, cursor = cpa; (cp = *cursor++) != NULL;) {
383 size_t j;
384 if (cp->func == &c_cmdnotsupp)
385 continue;
386 j = strlen(cp->name) + 2;
387 if ((i += j) > 72) {
388 i = j;
389 printf("\n");
391 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->name);
394 ac_free(cpa);
395 NYD_LEAVE;
396 return 0;
399 static int
400 _features(void *v)
402 NYD_ENTER;
403 UNUSED(v);
404 printf(tr(523, "Features: %s\n"), features);
405 NYD_LEAVE;
406 return 0;
409 static int
410 _version(void *v)
412 NYD_ENTER;
413 UNUSED(v);
414 printf(tr(111, "Version %s\n"), version);
415 NYD_LEAVE;
416 return 0;
419 static void
420 stop(int s)
422 sighandler_type old_action;
423 sigset_t nset;
424 NYD_X; /* Signal handler */
426 old_action = safe_signal(s, SIG_DFL);
428 sigemptyset(&nset);
429 sigaddset(&nset, s);
430 sigprocmask(SIG_UNBLOCK, &nset, NULL);
431 kill(0, s);
432 sigprocmask(SIG_BLOCK, &nset, NULL);
433 safe_signal(s, old_action);
434 if (_reset_on_stop) {
435 _reset_on_stop = 0;
436 reset(0);
440 static void
441 hangup(int s)
443 NYD_X; /* Signal handler */
444 UNUSED(s);
445 /* nothing to do? */
446 exit(1);
449 FL int
450 setfile(char const *name, int nmail) /* TODO oh my god */
452 static int shudclob;
454 struct stat stb;
455 struct flock flp;
456 FILE *ibuf = NULL;
457 int rv, i, compressed = 0, omsgCount = 0;
458 bool_t isedit;
459 char const *who;
460 size_t offset;
461 struct shortcut *sh;
462 NYD_ENTER;
464 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
465 * necessary to make a mailbox accessible by a different user, and if that
466 * has happened, let's just let the usual file perms decide */
467 who = (name[1] != '\0') ? name + 1 : myname;
468 isedit = (*name != '%' && ((sh = get_shortcut(name)) == NULL ||
469 *sh->sh_long != '%'));
470 if ((name = expand(name)) == NULL)
471 goto jem1;
473 switch (which_protocol(name)) {
474 case PROTO_FILE:
475 break;
476 case PROTO_MAILDIR:
477 rv = maildir_setfile(name, nmail, isedit);
478 goto jleave;
479 #ifdef HAVE_POP3
480 case PROTO_POP3:
481 shudclob = 1;
482 rv = pop3_setfile(name, nmail, isedit);
483 goto jleave;
484 #endif
485 #ifdef HAVE_IMAP
486 case PROTO_IMAP:
487 shudclob = 1;
488 if (nmail && mb.mb_type == MB_CACHE)
489 rv = 1;
490 else
491 rv = imap_setfile(name, nmail, isedit);
492 goto jleave;
493 #endif
494 default:
495 fprintf(stderr, tr(217, "Cannot handle protocol: %s\n"), name);
496 goto jem1;
499 /* FIXME this FILE leaks if quit()->edstop() reset()s! This entire code
500 * FIXME here is total crap, below we open(2) the same name again just to
501 * FIXME close it right away etc. The normal thing would be to (1) finalize
502 * FIXME the current box and (2) open the new box; yet, since (2) may fail
503 * FIXME we terribly need our VOID box to make this logic order possible! */
504 if ((ibuf = Zopen(name, "r", &compressed)) == NULL) {
505 if ((!isedit && errno == ENOENT) || nmail) {
506 if (nmail)
507 goto jnonmail;
508 goto jnomail;
510 perror(name);
511 goto jem1;
514 if (fstat(fileno(ibuf), &stb) < 0) {
515 if (nmail)
516 goto jnonmail;
517 perror("fstat");
518 goto jem1;
521 if (S_ISREG(stb.st_mode) ||
522 (options & OPT_BATCH_FLAG && !strcmp(name, "/dev/null"))) {
523 /* EMPTY */
524 } else {
525 if (nmail)
526 goto jnonmail;
527 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
528 perror(name);
529 goto jem1;
532 /* Looks like all will be well. We must now relinquish our hold on the
533 * current set of stuff. Must hold signals while we are reading the new
534 * file, else we will ruin the message[] data structure */
536 hold_sigs(); /* TODO note on this one in quit.c:quit() */
537 if (shudclob && !nmail)
538 quit();
539 #ifdef HAVE_SOCKETS
540 if (!nmail && mb.mb_sock.s_fd >= 0)
541 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
542 #endif
544 /* Copy the messages into /tmp and set pointers */
545 flp.l_type = F_RDLCK;
546 flp.l_start = 0;
547 flp.l_whence = SEEK_SET;
548 if (!nmail) {
549 mb.mb_type = MB_FILE;
550 mb.mb_perm = (options & OPT_R_FLAG) ? 0 : MB_DELE | MB_EDIT;
551 mb.mb_compressed = compressed;
552 if (compressed) {
553 if (compressed & 0200)
554 mb.mb_perm = 0;
555 } else {
556 if ((i = open(name, O_WRONLY)) < 0)
557 mb.mb_perm = 0;
558 else
559 close(i);
561 if (shudclob) {
562 if (mb.mb_itf) {
563 fclose(mb.mb_itf);
564 mb.mb_itf = NULL;
566 if (mb.mb_otf) {
567 fclose(mb.mb_otf);
568 mb.mb_otf = NULL;
571 shudclob = 1;
572 edit = isedit;
573 initbox(name);
574 offset = 0;
575 flp.l_len = 0;
576 if (!edit && fcntl(fileno(ibuf), F_SETLKW, &flp) == -1) {
577 perror("Unable to lock mailbox");
578 rele_sigs();
579 goto jem1;
581 } else /* nmail */{
582 fseek(mb.mb_otf, 0L, SEEK_END);
583 fseek(ibuf, mailsize, SEEK_SET);
584 offset = mailsize;
585 omsgCount = msgCount;
586 flp.l_len = offset;
587 if (!edit && fcntl(fileno(ibuf), F_SETLKW, &flp) == -1) {
588 rele_sigs();
589 goto jnonmail;
592 mailsize = fsize(ibuf);
594 if (nmail && UICMP(z, mailsize, <=, offset)) {
595 rele_sigs();
596 goto jnonmail;
598 setptr(ibuf, offset);
599 setmsize(msgCount);
600 if (nmail && mb.mb_sorted) {
601 mb.mb_threaded = 0;
602 c_sort((void*)-1);
605 Fclose(ibuf);
606 ibuf = NULL;
607 rele_sigs();
608 if (!nmail)
609 sawcom = FAL0;
611 if ((!edit || nmail) && msgCount == 0) {
612 jnonmail:
613 if (!nmail) {
614 if (!ok_blook(emptystart))
615 jnomail:
616 fprintf(stderr, tr(88, "No mail for %s\n"), who);
618 rv = 1;
619 goto jleave;
621 if (nmail)
622 newmailinfo(omsgCount);
624 rv = 0;
625 jleave:
626 if (ibuf != NULL)
627 Fclose(ibuf);
628 NYD_LEAVE;
629 return rv;
630 jem1:
631 rv = -1;
632 goto jleave;
635 FL int
636 newmailinfo(int omsgCount)
638 int mdot, i;
639 NYD_ENTER;
641 for (i = 0; i < omsgCount; i++)
642 message[i].m_flag &= ~MNEWEST;
643 if (msgCount > omsgCount) {
644 for (i = omsgCount; i < msgCount; i++)
645 message[i].m_flag |= MNEWEST;
646 printf(tr(158, "New mail has arrived.\n"));
647 if (msgCount - omsgCount == 1)
648 printf(tr(214, "Loaded 1 new message.\n"));
649 else
650 printf(tr(215, "Loaded %d new messages.\n"), msgCount - omsgCount);
651 } else
652 printf(tr(224, "Loaded %d messages.\n"), msgCount);
653 callhook(mailname, 1);
654 mdot = getmdot(1);
655 if (ok_blook(header))
656 print_headers(omsgCount + 1, msgCount, FAL0);
657 NYD_LEAVE;
658 return mdot;
661 FL void
662 commands(void)
664 struct eval_ctx ev;
665 int n;
666 NYD_ENTER;
668 if (!sourcing) {
669 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
670 safe_signal(SIGINT, onintr);
671 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
672 safe_signal(SIGHUP, hangup);
673 /* TODO We do a lot of redundant signal handling, especially
674 * TODO with the command line editor(s); try to merge this */
675 safe_signal(SIGTSTP, stop);
676 safe_signal(SIGTTOU, stop);
677 safe_signal(SIGTTIN, stop);
679 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
680 safe_signal(SIGPIPE, _oldpipe);
682 memset(&ev, 0, sizeof ev);
684 setexit();
685 for (;;) {
686 interrupts = 0;
687 handlerstacktop = NULL;
689 if (!sourcing && (options & OPT_INTERACTIVE)) {
690 char *cp;
692 if ((cp = ok_vlook(newmail)) == NULL)
693 cp = ok_vlook(autoinc); /* TODO legacy */
694 if ((options & OPT_TTYIN) && (cp != NULL || mb.mb_type == MB_IMAP)) {
695 struct stat st;
697 n = (cp != NULL && strcmp(cp, "noimap") && strcmp(cp, "nopoll"));
698 if ((mb.mb_type == MB_FILE && stat(mailname, &st) == 0 &&
699 st.st_size > mailsize) ||
700 #ifdef HAVE_IMAP
701 (mb.mb_type == MB_IMAP && imap_newmail(n) > (cp == NULL)) ||
702 #endif
703 (mb.mb_type == MB_MAILDIR && n != 0)) {
704 size_t odot = PTR2SIZE(dot - message);
705 bool_t odid = did_print_dot;
707 setfile(mailname, 1);
708 if (mb.mb_type != MB_IMAP) {
709 dot = message + odot;
710 did_print_dot = odid;
715 _reset_on_stop = 1;
716 exit_status = EXIT_OK;
719 #ifdef HAVE_COLOUR
720 colour_table = NULL; /* XXX intermediate hack */
721 #endif
722 sreset(sourcing);
723 if (!sourcing) {
724 char *cp;
726 /* TODO Note: this buffer may contain a password. We should redefine
727 * TODO the code flow which has to do that */
728 if ((cp = termios_state.ts_linebuf) != NULL) {
729 termios_state.ts_linebuf = NULL;
730 termios_state.ts_linesize = 0;
731 free(cp); /* TODO pool give-back */
733 /* TODO Due to expand-on-tab of NCL the buffer may grow */
734 if (ev.ev_line.l > LINESIZE * 3) {
735 free(ev.ev_line.s); /* TODO pool! but what? */
736 ev.ev_line.s = NULL;
737 ev.ev_line.l = 0;
741 /* Read a line of commands and handle end of file specially */
742 jreadline:
743 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
744 ev.ev_new_content);
745 _reset_on_stop = 0;
746 if (n < 0) {
747 /* EOF */
748 if (loading)
749 break;
750 if (sourcing) {
751 unstack();
752 continue;
754 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
755 printf(tr(89, "Use `quit' to quit.\n"));
756 continue;
758 break;
761 inhook = 0;
762 if (evaluate(&ev))
763 break;
764 if (exit_status != EXIT_OK && (options & OPT_BATCH_FLAG) &&
765 ok_blook(batch_exit_on_error))
766 break;
767 if (!sourcing && (options & OPT_INTERACTIVE)) {
768 if (ev.ev_new_content != NULL)
769 goto jreadline;
770 if (ev.ev_add_history)
771 tty_addhist(ev.ev_line.s);
775 if (ev.ev_line.s != NULL)
776 free(ev.ev_line.s);
777 if (sourcing)
778 sreset(FAL0);
779 NYD_LEAVE;
782 FL int
783 execute(char *linebuf, int contxt, size_t linesize) /* XXX LEGACY */
785 struct eval_ctx ev;
786 #ifdef HAVE_COLOUR
787 struct colour_table *ct_save;
788 #endif
789 int rv;
790 NYD_ENTER;
792 /* TODO Maybe recursion from within collect.c! As long as we don't have
793 * TODO a value carrier that transports the entire state of a recursion
794 * TODO we need to save away also the colour table */
795 #ifdef HAVE_COLOUR
796 ct_save = colour_table;
797 colour_table = NULL;
798 #endif
800 memset(&ev, 0, sizeof ev);
801 ev.ev_line.s = linebuf;
802 ev.ev_line.l = linesize;
803 ev.ev_is_recursive = (contxt != 0);
804 rv = evaluate(&ev);
806 #ifdef HAVE_COLOUR
807 colour_table = ct_save;
808 #endif
809 NYD_LEAVE;
810 return rv;
813 FL int
814 evaluate(struct eval_ctx *evp)
816 struct str line;
817 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
818 struct cmd_ghost *cg = NULL;
819 struct cmd const *com = NULL;
820 int muvec[2], c, e = 1;
821 NYD_ENTER;
823 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
824 evp->ev_add_history = FAL0;
825 evp->ev_new_content = NULL;
827 /* Command ghosts that refer to shell commands or macro expansion restart */
828 jrestart:
830 /* Strip the white space away from the beginning of the command */
831 for (cp = line.s; whitechar(*cp); ++cp)
833 line.l -= PTR2SIZE(cp - line.s);
835 /* Ignore comments */
836 if (*cp == '#')
837 goto jleave0;
839 /* Handle ! differently to get the correct lexical conventions */
840 if (*cp == '!') {
841 if (sourcing) {
842 fprintf(stderr, tr(90, "Can't `!' while sourcing\n"));
843 goto jleave;
845 c_shell(++cp);
846 evp->ev_add_history = TRU1;
847 goto jleave0;
850 /* Isolate the actual command; since it may not necessarily be
851 * separated from the arguments (as in `p1') we need to duplicate it to
852 * be able to create a NUL terminated version.
853 * We must be aware of several special one letter commands here */
854 arglist[0] = cp;
855 if ((cp = _lex_isolate(cp)) == arglist[0] &&
856 (*cp == '|' || *cp == '~' || *cp == '?'))
857 ++cp;
858 c = (int)PTR2SIZE(cp - arglist[0]);
859 line.l -= c;
860 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c + 1);
861 memcpy(word, arglist[0], c);
862 word[c] = '\0';
864 /* Look up the command; if not found, bitch.
865 * Normally, a blank command would map to the first command in the
866 * table; while sourcing, however, we ignore blank lines to eliminate
867 * confusion; act just the same for ghosts */
868 if (*word == '\0') {
869 if (sourcing || cg != NULL)
870 goto jleave0;
871 com = _cmd_tab + 0;
872 goto jexec;
875 /* If this is the first evaluation, check command ghosts */
876 if (cg == NULL) {
877 /* TODO relink list head, so it's sorted on usage over time?
878 * TODO in fact, there should be one hashmap over all commands and ghosts
879 * TODO so that the lookup could be made much more efficient than it is
880 * TODO now (two adjacent list searches! */
881 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
882 if (!strcmp(word, cg->name)) {
883 if (line.l > 0) {
884 size_t i = cg->cmd.l;
885 line.s = salloc(i + 1 + line.l +1);
886 memcpy(line.s, cg->cmd.s, i);
887 line.s[i++] = ' ';
888 memcpy(line.s + i, cp, line.l);
889 line.s[i += line.l] = '\0';
890 line.l = i;
891 } else {
892 line.s = cg->cmd.s;
893 line.l = cg->cmd.l;
895 goto jrestart;
899 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
900 fprintf(stderr, tr(91, "Unknown command: `%s'\n"), word);
901 if (com != NULL) {
902 c_cmdnotsupp(NULL);
903 com = NULL;
905 goto jleave;
908 /* See if we should execute the command -- if a conditional we always
909 * execute it, otherwise, check the state of cond */
910 jexec:
911 if (!(com->argtype & ARG_F)) {
912 if (condstack_isskip())
913 goto jleave0;
916 /* Process the arguments to the command, depending on the type he expects.
917 * Default to an error.
918 * If we are sourcing an interactive command, it's an error */
919 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
920 fprintf(stderr, tr(92, "May not execute `%s' while sending\n"),
921 com->name);
922 goto jleave;
924 if (sourcing && (com->argtype & ARG_I)) {
925 fprintf(stderr, tr(93, "May not execute `%s' while sourcing\n"),
926 com->name);
927 goto jleave;
929 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
930 fprintf(stderr, tr(94, "May not execute `%s' -- "
931 "message file is read only\n"), com->name);
932 goto jleave;
934 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
935 fprintf(stderr, tr(95, "Cannot recursively invoke `%s'\n"), com->name);
936 goto jleave;
938 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
939 fprintf(stderr, tr(257, "Cannot execute `%s' without active mailbox\n"),
940 com->name);
941 goto jleave;
944 if (com->argtype & ARG_V)
945 temporary_arg_v_store = NULL;
947 switch (com->argtype & ARG_ARGMASK) {
948 case ARG_MSGLIST:
949 /* Message list defaulting to nearest forward legal message */
950 if (_msgvec == NULL)
951 goto je96;
952 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
953 break;
954 if (c == 0) {
955 *_msgvec = first(com->msgflag, com->msgmask);
956 if (*_msgvec != 0)
957 _msgvec[1] = 0;
959 if (*_msgvec == 0) {
960 if (!inhook)
961 printf(tr(97, "No applicable messages\n"));
962 break;
964 e = (*com->func)(_msgvec);
965 break;
967 case ARG_NDMLIST:
968 /* Message list with no defaults, but no error if none exist */
969 if (_msgvec == NULL) {
970 je96:
971 fprintf(stderr, tr(96, "Illegal use of `message list'\n"));
972 break;
974 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
975 break;
976 e = (*com->func)(_msgvec);
977 break;
979 case ARG_STRLIST:
980 /* Just the straight string, with leading blanks removed */
981 while (whitechar(*cp))
982 ++cp;
983 e = (*com->func)(cp);
984 break;
986 case ARG_RAWLIST:
987 case ARG_ECHOLIST:
988 /* A vector of strings, in shell style */
989 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
990 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
991 break;
992 if (c < com->minargs) {
993 fprintf(stderr, tr(99, "`%s' requires at least %d arg(s)\n"),
994 com->name, com->minargs);
995 break;
997 if (c > com->maxargs) {
998 fprintf(stderr, tr(100, "`%s' takes no more than %d arg(s)\n"),
999 com->name, com->maxargs);
1000 break;
1002 e = (*com->func)(arglist);
1003 break;
1005 case ARG_NOLIST:
1006 /* Just the constant zero, for exiting, eg. */
1007 e = (*com->func)(0);
1008 break;
1010 default:
1011 panic(tr(101, "Unknown argument type"));
1014 if (e == 0 && (com->argtype & ARG_V) &&
1015 (cp = temporary_arg_v_store) != NULL) {
1016 temporary_arg_v_store = NULL;
1017 evp->ev_new_content = cp;
1018 goto jleave0;
1020 if (!(com->argtype & ARG_H) && !list_saw_numbers)
1021 evp->ev_add_history = TRU1;
1023 jleave:
1024 /* Exit the current source file on error */
1025 if ((exec_last_comm_error = (e != 0))) {
1026 if (e < 0 || loading) {
1027 e = 1;
1028 goto jret;
1030 if (sourcing)
1031 unstack();
1032 goto jret0;
1034 if (com == NULL)
1035 goto jret0;
1036 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1037 if (visible(dot)) {
1038 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1039 muvec[1] = 0;
1040 c_type(muvec);
1042 if (!sourcing && !inhook && (com->argtype & ARG_T) == 0)
1043 sawcom = TRU1;
1044 jleave0:
1045 exec_last_comm_error = 0;
1046 jret0:
1047 e = 0;
1048 jret:
1049 NYD_LEAVE;
1050 return e;
1053 FL void
1054 setmsize(int sz)
1056 NYD_ENTER;
1057 if (_msgvec != 0)
1058 free(_msgvec);
1059 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1060 NYD_LEAVE;
1063 FL void
1064 print_header_summary(char const *Larg)
1066 size_t bot, top, i, j;
1067 NYD_ENTER;
1069 if (Larg != NULL) {
1070 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1071 if ((options & (OPT_VERBOSE | OPT_HEADERSONLY)) == OPT_HEADERSONLY) {
1072 freopen("/dev/null", "w", stdout);
1073 freopen("/dev/null", "w", stderr);
1075 if (getmsglist(/*TODO make arg const */UNCONST(Larg), _msgvec, 0) <= 0) {
1076 if (options & OPT_HEADERSONLY)
1077 exit_status = 1;
1078 goto jleave;
1080 if (options & OPT_HEADERSONLY) {
1081 exit_status = 0;
1082 goto jleave;
1084 for (bot = msgCount, top = 0, i = 0; (j = _msgvec[i]) != 0; ++i) {
1085 if (bot > j)
1086 bot = j;
1087 if (top < j)
1088 top = j;
1090 } else
1091 bot = 1, top = msgCount;
1092 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
1093 jleave:
1094 NYD_LEAVE;
1097 FL void
1098 onintr(int s)
1100 NYD_X; /* Signal handler */
1102 if (handlerstacktop != NULL) {
1103 handlerstacktop(s);
1104 return;
1106 safe_signal(SIGINT, onintr);
1107 noreset = 0;
1108 if (!_lex_inithdr)
1109 sawcom = TRU1;
1110 _lex_inithdr = 0;
1111 while (sourcing)
1112 unstack();
1114 termios_state_reset();
1115 close_all_files();
1117 if (image >= 0) {
1118 close(image);
1119 image = -1;
1121 if (interrupts != 1)
1122 fprintf(stderr, tr(102, "Interrupt\n"));
1123 safe_signal(SIGPIPE, _oldpipe);
1124 reset(0);
1127 FL void
1128 announce(int printheaders)
1130 int vec[2], mdot;
1131 NYD_ENTER;
1133 mdot = newfileinfo();
1134 vec[0] = mdot;
1135 vec[1] = 0;
1136 dot = &message[mdot - 1];
1137 if (printheaders && msgCount > 0 && ok_blook(header)) {
1138 ++_lex_inithdr;
1139 c_headers(vec);
1140 _lex_inithdr = 0;
1142 NYD_LEAVE;
1145 FL int
1146 newfileinfo(void)
1148 struct message *mp;
1149 int u, n, mdot, d, s, hidden, moved;
1150 NYD_ENTER;
1152 if (mb.mb_type == MB_VOID) {
1153 mdot = 1;
1154 goto jleave;
1157 mdot = getmdot(0);
1158 s = d = hidden = moved =0;
1159 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1160 if (mp->m_flag & MNEW)
1161 ++n;
1162 if ((mp->m_flag & MREAD) == 0)
1163 ++u;
1164 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1165 ++moved;
1166 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1167 ++d;
1168 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1169 ++s;
1170 if (mp->m_flag & MHIDDEN)
1171 ++hidden;
1174 /* If displayname gets truncated the user effectively has no option to see
1175 * the full pathname of the mailbox, so print it at least for '? fi' */
1176 printf(tr(103, "\"%s\": "),
1177 (_update_mailname(NULL) ? displayname : mailname));
1178 if (msgCount == 1)
1179 printf(tr(104, "1 message"));
1180 else
1181 printf(tr(105, "%d messages"), msgCount);
1182 if (n > 0)
1183 printf(tr(106, " %d new"), n);
1184 if (u-n > 0)
1185 printf(tr(107, " %d unread"), u);
1186 if (d > 0)
1187 printf(tr(108, " %d deleted"), d);
1188 if (s > 0)
1189 printf(tr(109, " %d saved"), s);
1190 if (moved > 0)
1191 printf(tr(136, " %d moved"), moved);
1192 if (hidden > 0)
1193 printf(tr(139, " %d hidden"), hidden);
1194 if (mb.mb_type == MB_CACHE)
1195 printf(" [Disconnected]");
1196 else if (mb.mb_perm == 0)
1197 printf(tr(110, " [Read only]"));
1198 printf("\n");
1199 jleave:
1200 NYD_LEAVE;
1201 return mdot;
1204 FL int
1205 getmdot(int nmail)
1207 struct message *mp;
1208 char *cp;
1209 int mdot;
1210 enum mflag avoid = MHIDDEN | MDELETED;
1211 NYD_ENTER;
1213 if (!nmail) {
1214 if (ok_blook(autothread))
1215 c_thread(NULL);
1216 else if ((cp = ok_vlook(autosort)) != NULL) {
1217 free(mb.mb_sorted);
1218 mb.mb_sorted = sstrdup(cp);
1219 c_sort(NULL);
1222 if (mb.mb_type == MB_VOID) {
1223 mdot = 1;
1224 goto jleave;
1227 if (nmail)
1228 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1229 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1230 break;
1232 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1233 if (mb.mb_threaded) {
1234 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1235 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1236 break;
1237 } else {
1238 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1239 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1240 break;
1244 if ((mb.mb_threaded ? (mp == NULL) : 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 & MFLAGGED)
1248 break;
1249 } else {
1250 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1251 if (mp->m_flag & MFLAGGED)
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 & (MREAD | avoid)))
1260 break;
1261 } else {
1262 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1263 if (!(mp->m_flag & (MREAD | avoid)))
1264 break;
1268 if ((mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1269 mdot = (int)PTR2SIZE(mp - message + 1);
1270 else if (ok_blook(showlast)) {
1271 if (mb.mb_threaded) {
1272 for (mp = this_in_thread(threadroot, -1); mp;
1273 mp = prev_in_thread(mp))
1274 if (!(mp->m_flag & avoid))
1275 break;
1276 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1277 } else {
1278 for (mp = message + msgCount - 1; mp >= message; --mp)
1279 if (!(mp->m_flag & avoid))
1280 break;
1281 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1283 } else if (mb.mb_threaded) {
1284 for (mp = threadroot; mp; mp = next_in_thread(mp))
1285 if (!(mp->m_flag & avoid))
1286 break;
1287 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1288 } else {
1289 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1290 if (!(mp->m_flag & avoid))
1291 break;
1292 mdot = PTRCMP(mp, <, message + msgCount)
1293 ? (int)PTR2SIZE(mp - message + 1) : 1;
1295 jleave:
1296 NYD_LEAVE;
1297 return mdot;
1300 FL void
1301 initbox(char const *name)
1303 char *tempMesg;
1304 NYD_ENTER;
1306 if (mb.mb_type != MB_VOID)
1307 n_strlcpy(prevfile, mailname, PATH_MAX);
1309 _update_mailname(name != mailname ? name : NULL);
1311 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpbox", OF_WRONLY | OF_HOLDSIGS, 0600)) ==
1312 NULL) {
1313 perror(tr(87, "temporary mail message file"));
1314 exit(1);
1316 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1317 perror(tr(87, "temporary mail message file"));
1318 exit(1);
1320 Ftmp_release(&tempMesg);
1322 message_reset();
1323 mb.mb_threaded = 0;
1324 if (mb.mb_sorted != NULL) {
1325 free(mb.mb_sorted);
1326 mb.mb_sorted = NULL;
1328 #ifdef HAVE_IMAP
1329 mb.mb_flags = MB_NOFLAGS;
1330 #endif
1331 prevdot = NULL;
1332 dot = NULL;
1333 did_print_dot = FAL0;
1334 NYD_LEAVE;
1337 #ifdef HAVE_DOCSTRINGS
1338 FL bool_t
1339 print_comm_docstr(char const *comm)
1341 bool_t rv = FAL0;
1342 struct cmd_ghost *cg;
1343 struct cmd const *cp;
1344 NYD_ENTER;
1346 /* Ghosts take precedence */
1347 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
1348 if (!strcmp(comm, cg->name)) {
1349 printf("%s -> <%s>\n", comm, cg->cmd.s);
1350 rv = TRU1;
1351 goto jleave;
1354 for (cp = _cmd_tab; cp->name != NULL; ++cp) {
1355 if (cp->func == &c_cmdnotsupp)
1356 continue;
1357 if (!strcmp(comm, cp->name))
1358 printf("%s: %s\n", comm, tr(cp->docid, cp->doc));
1359 else if (is_prefix(comm, cp->name))
1360 printf("%s (%s): %s\n", comm, cp->name, tr(cp->docid, cp->doc));
1361 else
1362 continue;
1363 rv = TRU1;
1364 break;
1366 jleave:
1367 NYD_LEAVE;
1368 return rv;
1370 #endif
1372 /* vim:set fenc=utf-8:s-it-mode */