IMAP: [a65afca] (Add *imap-delim{,-{{,USER@}HOST}}*) for real now
[s-mailx.git] / lex.c
blob8a639237056e9ff841c2cdcf2d5fb16700b745c2
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ (Lexical processing of) Commands, and the (blocking) "event mainloop".
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE lex
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 struct cmd {
43 char const *name; /* Name of command */
44 int (*func)(void*); /* Implementor of command */
45 enum argtype argtype; /* Arglist type (see below) */
46 short msgflag; /* Required flags of msgs */
47 short msgmask; /* Relevant flags of msgs */
48 #ifdef HAVE_DOCSTRINGS
49 char const *doc; /* One line doc for command */
50 #endif
52 /* Yechh, can't initialize unions */
53 #define minargs msgflag /* Minimum argcount for RAWLIST */
54 #define maxargs msgmask /* Max argcount for RAWLIST */
56 struct cmd_ghost {
57 struct cmd_ghost *next;
58 struct str cmd; /* Data follows after .name */
59 char name[VFIELD_SIZE(0)];
62 static int *_msgvec;
63 static int _reset_on_stop; /* do a reset() if stopped */
64 static sighandler_type _oldpipe;
65 static struct cmd_ghost *_cmd_ghosts;
66 /* _cmd_tab[] after fun protos */
67 static int _lex_inithdr; /* am printing startup headers */
69 /* Update mailname (if name != NULL) and displayname, return wether displayname
70 * was large enough to swallow mailname */
71 static bool_t _update_mailname(char const *name);
72 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
73 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
74 #endif
76 /* Isolate the command from the arguments */
77 static char * _lex_isolate(char const *comm);
79 /* Get first-fit, or NULL */
80 static struct cmd const * _lex(char const *comm);
82 /* Command ghost handling */
83 static int _c_ghost(void *v);
84 static int _c_unghost(void *v);
86 /* Print a list of all commands */
87 static int _c_pcmdlist(void *v);
89 static int __pcmd_cmp(void const *s1, void const *s2);
91 /* `quit' command */
92 static int _c_quit(void *v);
94 /* Print the binaries compiled-in features */
95 static int _c_features(void *v);
97 /* Print the binaries version number */
98 static int _c_version(void *v);
100 /* When we wake up after ^Z, reprint the prompt */
101 static void stop(int s);
103 /* Branch here on hangup signal and simulate "exit" */
104 static void hangup(int s);
106 /* List of all commands */
107 static struct cmd const _cmd_tab[] = {
108 #include "cmd_tab.h"
111 #ifdef HAVE_C90AMEND1
112 SINLINE size_t
113 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
115 int err;
116 size_t i, ok;
117 NYD_ENTER;
119 for (err = ok = i = 0; cpl > maxl || err;) {
120 int ml = mblen(cp, cpl);
121 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
122 (void)mblen(NULL, 0);
123 err = 1;
124 ml = 1;
125 } else {
126 if (!err)
127 ok = i;
128 err = 0;
129 if (ml == 0)
130 break;
132 cp += ml;
133 i += ml;
134 cpl -= ml;
136 NYD_LEAVE;
137 return ok;
139 #endif /* HAVE_C90AMEND1 */
141 static bool_t
142 _update_mailname(char const *name)
144 char tbuf[PATH_MAX], *mailp, *dispp;
145 size_t i, j;
146 bool_t rv = TRU1;
147 NYD_ENTER;
149 /* Don't realpath(3) if it's only an update request */
150 if (name != NULL) {
151 #ifdef HAVE_REALPATH
152 enum protocol p = which_protocol(name);
153 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
154 if (realpath(name, mailname) == NULL) {
155 n_err(_("Can't canonicalize \"%s\"\n"), name);
156 rv = FAL0;
157 goto jdocopy;
159 } else
160 jdocopy:
161 #endif
162 n_strlcpy(mailname, name, sizeof(mailname));
165 mailp = mailname;
166 dispp = displayname;
168 /* Don't display an absolute path but "+FOLDER" if under *folder* */
169 if (getfold(tbuf, sizeof tbuf)) {
170 i = strlen(tbuf);
171 if (!strncmp(tbuf, mailp, i)) {
172 mailp += i;
173 *dispp++ = '+';
177 /* We want to see the name of the folder .. on the screen */
178 i = strlen(mailp);
179 if (i < sizeof(displayname) -1)
180 memcpy(dispp, mailp, i +1);
181 else {
182 rv = FAL0;
183 /* Avoid disrupting multibyte sequences (if possible) */
184 #ifndef HAVE_C90AMEND1
185 j = sizeof(displayname) / 3 - 1;
186 i -= sizeof(displayname) - (1/* + */ + 3) - j;
187 #else
188 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
189 i = j + __narrow_suffix(mailp + j, i - j,
190 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
191 #endif
192 snprintf(dispp, sizeof(displayname), "%.*s...%s",
193 (int)j, mailp, mailp + i);
195 NYD_LEAVE;
196 return rv;
199 static char *
200 _lex_isolate(char const *comm)
202 NYD_ENTER;
203 while (*comm != '\0' &&
204 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
205 ++comm;
206 NYD_LEAVE;
207 return UNCONST(comm);
210 static struct cmd const *
211 _lex(char const *comm) /* TODO **command hashtable**! linear list search!!! */
213 struct cmd const *cp;
214 NYD_ENTER;
216 for (cp = _cmd_tab; cp->name != NULL; ++cp)
217 if (*comm == *cp->name && is_prefix(comm, cp->name))
218 goto jleave;
219 cp = NULL;
220 jleave:
221 NYD_LEAVE;
222 return cp;
225 static int
226 _c_ghost(void *v)
228 char const **argv = v;
229 struct cmd_ghost *lcg, *cg;
230 size_t i, cl, nl;
231 char *cp;
232 NYD_ENTER;
234 /* Show the list? */
235 if (*argv == NULL) {
236 FILE *fp;
238 if ((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
239 NULL)
240 fp = stdout;
241 for (i = 0, cg = _cmd_ghosts; cg != NULL; cg = cg->next)
242 fprintf(fp, "ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
243 if (fp != stdout) {
244 page_or_print(fp, i);
245 Fclose(fp);
247 goto jleave;
250 /* Verify the ghost name is a valid one */
251 if (*argv[0] == '\0' || *_lex_isolate(argv[0]) != '\0') {
252 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
253 v = NULL;
254 goto jleave;
257 /* Show command of single ghost? */
258 if (argv[1] == NULL) {
259 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
260 if (!strcmp(argv[0], cg->name)) {
261 printf("ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
262 goto jleave;
264 n_err(_("`ghost': no such alias: \"%s\"\n"), argv[0]);
265 v = NULL;
266 goto jleave;
269 /* Define command for ghost: verify command content */
270 for (cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
271 if (*cp != '\0')
272 cl += strlen(cp) + 1; /* SP or NUL */
273 if (cl == 0) {
274 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv[0]);
275 v = NULL;
276 goto jleave;
279 /* If the ghost already exists, recreate */
280 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
281 if (!strcmp(cg->name, argv[0])) {
282 if (lcg != NULL)
283 lcg->next = cg->next;
284 else
285 _cmd_ghosts = cg->next;
286 free(cg);
287 break;
290 nl = strlen(argv[0]) +1;
291 cg = smalloc(sizeof(*cg) - VFIELD_SIZEOF(struct cmd_ghost, name) + nl + cl);
292 cg->next = _cmd_ghosts;
293 _cmd_ghosts = cg;
294 memcpy(cg->name, argv[0], nl);
295 cp = cg->cmd.s = cg->name + nl;
296 cg->cmd.l = --cl;
297 while (*++argv != NULL) {
298 i = strlen(*argv);
299 if (i > 0) {
300 memcpy(cp, *argv, i);
301 cp += i;
302 *cp++ = ' ';
305 *--cp = '\0';
306 jleave:
307 NYD_LEAVE;
308 return (v == NULL);
311 static int
312 _c_unghost(void *v)
314 int rv = 0;
315 char const **argv = v, *cp;
316 struct cmd_ghost *lcg, *cg;
317 NYD_ENTER;
319 while ((cp = *argv++) != NULL) {
320 if (cp[0] == '*' && cp[1] == '\0') {
321 while ((cg = _cmd_ghosts) != NULL) {
322 _cmd_ghosts = cg->next;
323 free(cg);
325 } else {
326 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
327 if (!strcmp(cg->name, cp)) {
328 if (lcg != NULL)
329 lcg->next = cg->next;
330 else
331 _cmd_ghosts = cg->next;
332 free(cg);
333 goto jouter;
335 n_err(_("`unghost': no such alias: \"%s\"\n"), cp);
336 rv = 1;
337 jouter:
341 NYD_LEAVE;
342 return rv;
345 static int
346 __pcmd_cmp(void const *s1, void const *s2)
348 struct cmd const * const *c1 = s1, * const *c2 = s2;
349 int rv;
350 NYD_ENTER;
352 rv = strcmp((*c1)->name, (*c2)->name);
353 NYD_LEAVE;
354 return rv;
357 static int
358 _c_pcmdlist(void *v)
360 struct cmd const **cpa, *cp, **cursor;
361 size_t i;
362 NYD_ENTER;
363 UNUSED(v);
365 for (i = 0; _cmd_tab[i].name != NULL; ++i)
367 ++i;
368 cpa = ac_alloc(sizeof(cp) * i);
370 for (i = 0; (cp = _cmd_tab + i)->name != NULL; ++i)
371 cpa[i] = cp;
372 cpa[i] = NULL;
374 qsort(cpa, i, sizeof(cp), &__pcmd_cmp);
376 printf(_("Commands are:\n"));
377 for (i = 0, cursor = cpa; (cp = *cursor++) != NULL;) {
378 size_t j;
379 if (cp->func == &c_cmdnotsupp)
380 continue;
381 j = strlen(cp->name) + 2;
382 if ((i += j) > 72) {
383 i = j;
384 printf("\n");
386 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->name);
389 ac_free(cpa);
390 NYD_LEAVE;
391 return 0;
394 static int
395 _c_quit(void *v)
397 int rv;
398 NYD_ENTER;
399 UNUSED(v);
401 /* If we are PS_SOURCING, then return 1 so evaluate() can handle it.
402 * Otherwise return -1 to abort command loop */
403 rv = (pstate & PS_SOURCING) ? 1 : -1;
404 NYD_LEAVE;
405 return rv;
408 static int
409 _c_features(void *v)
411 NYD_ENTER;
412 UNUSED(v);
413 printf(_("Features: %s\n"), ok_vlook(features));
414 NYD_LEAVE;
415 return 0;
418 static int
419 _c_version(void *v)
421 NYD_ENTER;
422 UNUSED(v);
423 printf(_("Version %s\n"), ok_vlook(version));
424 NYD_LEAVE;
425 return 0;
428 static void
429 stop(int s)
431 sighandler_type old_action;
432 sigset_t nset;
433 NYD_X; /* Signal handler */
435 old_action = safe_signal(s, SIG_DFL);
437 sigemptyset(&nset);
438 sigaddset(&nset, s);
439 sigprocmask(SIG_UNBLOCK, &nset, NULL);
440 n_raise(s);
441 sigprocmask(SIG_BLOCK, &nset, NULL);
442 safe_signal(s, old_action);
443 if (_reset_on_stop) {
444 _reset_on_stop = 0;
445 reset(0);
449 static void
450 hangup(int s)
452 NYD_X; /* Signal handler */
453 UNUSED(s);
454 /* nothing to do? */
455 exit(EXIT_ERR);
458 FL int
459 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
461 static int shudclob;
463 struct stat stb;
464 size_t offset;
465 char const *who;
466 int rv, omsgCount = 0;
467 FILE *ibuf = NULL, *lckfp = NULL;
468 bool_t isdevnull = FAL0;
469 NYD_ENTER;
471 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
472 * necessary to make a mailbox accessible by a different user, and if that
473 * has happened, let's just let the usual file perms decide */
475 if (name[0] == '%' || ((who = shortcut_expand(name)) != NULL && *who == '%'))
476 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
478 who = (name[1] != '\0') ? name + 1 : myname;
480 if ((name = expand(name)) == NULL)
481 goto jem1;
483 /* For at least substdate() users */
484 time_current_update(&time_current, FAL0);
486 switch (which_protocol(name)) {
487 case PROTO_FILE:
488 if (temporary_protocol_ext != NULL)
489 name = savecat(name, temporary_protocol_ext);
490 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
491 #ifdef HAVE_REALPATH
492 do { /* TODO we need objects, goes away then */
493 char ebuf[PATH_MAX];
494 if (realpath(name, ebuf) != NULL)
495 name = savestr(ebuf);
496 } while (0);
497 #endif
498 break;
499 case PROTO_MAILDIR:
500 shudclob = 1;
501 rv = maildir_setfile(name, fm);
502 goto jleave;
503 #ifdef HAVE_POP3
504 case PROTO_POP3:
505 shudclob = 1;
506 rv = pop3_setfile(name, fm);
507 goto jleave;
508 #endif
509 #ifdef HAVE_IMAP
510 case PROTO_IMAP:
511 shudclob = 1;
512 if ((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE)
513 rv = 1;
514 else
515 rv = imap_setfile(name, fm);
516 goto jleave;
517 #endif
518 default:
519 n_err(_("Cannot handle protocol: %s\n"), name);
520 goto jem1;
523 if ((ibuf = Zopen(name, "r")) == NULL) {
524 if (((fm & FEDIT_SYSBOX) && errno == ENOENT) || (fm & FEDIT_NEWMAIL)) {
525 if (fm & FEDIT_NEWMAIL)
526 goto jnonmail;
527 goto jnomail;
529 n_perr(name, 0);
530 goto jem1;
533 if (fstat(fileno(ibuf), &stb) == -1) {
534 if (fm & FEDIT_NEWMAIL)
535 goto jnonmail;
536 n_perr(_("fstat"), 0);
537 goto jem1;
540 if (S_ISREG(stb.st_mode) || isdevnull) {
541 /* EMPTY */
542 } else {
543 if (fm & FEDIT_NEWMAIL)
544 goto jnonmail;
545 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
546 n_perr(name, 0);
547 goto jem1;
550 /* Looks like all will be well. We must now relinquish our hold on the
551 * current set of stuff. Must hold signals while we are reading the new
552 * file, else we will ruin the message[] data structure */
554 hold_sigs(); /* TODO note on this one in quit.c:quit() */
555 if (shudclob && !(fm & FEDIT_NEWMAIL))
556 quit();
557 #ifdef HAVE_SOCKETS
558 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
559 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
560 #endif
562 /* TODO There is no intermediate VOID box we've switched to: name may
563 * TODO point to the same box that we just have written, so any updates
564 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
565 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
566 /* TODO We may uselessly open twice but quit() doesn't say wether we were
567 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
568 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
569 name = mailname;
570 Fclose(ibuf);
572 if ((ibuf = Zopen(name, "r")) == NULL ||
573 fstat(fileno(ibuf), &stb) == -1 ||
574 (!S_ISREG(stb.st_mode) && !isdevnull)) {
575 n_perr(name, 0);
576 rele_sigs();
577 goto jem2;
581 /* Copy the messages into /tmp and set pointers */
582 if (!(fm & FEDIT_NEWMAIL)) {
583 mb.mb_type = MB_FILE;
584 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
585 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
586 if (shudclob) {
587 if (mb.mb_itf) {
588 fclose(mb.mb_itf);
589 mb.mb_itf = NULL;
591 if (mb.mb_otf) {
592 fclose(mb.mb_otf);
593 mb.mb_otf = NULL;
596 shudclob = 1;
597 if (fm & FEDIT_SYSBOX)
598 pstate &= ~PS_EDIT;
599 else
600 pstate |= PS_EDIT;
601 initbox(name);
602 offset = 0;
603 } else {
604 fseek(mb.mb_otf, 0L, SEEK_END);
605 /* TODO Doing this without holding a lock is.. And not err checking.. */
606 fseek(ibuf, mailsize, SEEK_SET);
607 offset = mailsize;
608 omsgCount = msgCount;
611 if (isdevnull)
612 lckfp = (FILE*)-1;
613 else if (!(pstate & PS_EDIT))
614 lckfp = dot_lock(name, fileno(ibuf), FLT_READ, offset,0,
615 (fm & FEDIT_NEWMAIL ? 0 : 1));
616 else if (file_lock(fileno(ibuf), FLT_READ, offset,0,
617 (fm & FEDIT_NEWMAIL ? 0 : 1)))
618 lckfp = (FILE*)-1;
620 if (lckfp == NULL) {
621 if (!(fm & FEDIT_NEWMAIL)) {
622 char const *emsg = (pstate & PS_EDIT)
623 ? N_("Unable to lock mailbox, aborting operation")
624 : N_("Unable to (dot) lock mailbox, aborting operation");
625 n_perr(V_(emsg), 0);
627 rele_sigs();
628 if (!(fm & FEDIT_NEWMAIL))
629 goto jem1;
630 goto jnonmail;
633 mailsize = fsize(ibuf);
635 /* TODO This is too simple minded? We should regenerate an index file
636 * TODO to be able to truly tell wether *anything* has changed! */
637 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
638 rele_sigs();
639 goto jnonmail;
641 setptr(ibuf, offset);
642 setmsize(msgCount);
643 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
644 mb.mb_threaded = 0;
645 c_sort((void*)-1);
648 Fclose(ibuf);
649 ibuf = NULL;
650 if (lckfp != NULL && lckfp != (FILE*)-1) {
651 Pclose(lckfp, FAL0);
652 /*lckfp = NULL;*/
654 rele_sigs();
656 if (!(fm & FEDIT_NEWMAIL))
657 pstate &= ~PS_SAW_COMMAND;
659 if (options & OPT_EXISTONLY) {
660 rv = (msgCount == 0);
661 goto jleave;
664 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
665 if (!(fm & FEDIT_NEWMAIL)) {
666 jnomail:
667 if (!ok_blook(emptystart))
668 n_err(_("No mail for %s\n"), who);
670 jnonmail:
671 rv = 1;
672 goto jleave;
674 if (fm & FEDIT_NEWMAIL)
675 newmailinfo(omsgCount);
677 rv = 0;
678 jleave:
679 if (ibuf != NULL) {
680 Fclose(ibuf);
681 if (lckfp != NULL && lckfp != (FILE*)-1)
682 Pclose(lckfp, FAL0);
684 NYD_LEAVE;
685 return rv;
686 jem2:
687 mb.mb_type = MB_VOID;
688 jem1:
689 rv = -1;
690 goto jleave;
693 FL int
694 newmailinfo(int omsgCount)
696 int mdot, i;
697 NYD_ENTER;
699 for (i = 0; i < omsgCount; ++i)
700 message[i].m_flag &= ~MNEWEST;
702 if (msgCount > omsgCount) {
703 for (i = omsgCount; i < msgCount; ++i)
704 message[i].m_flag |= MNEWEST;
705 printf(_("New mail has arrived.\n"));
706 if ((i = msgCount - omsgCount) == 1)
707 printf(_("Loaded 1 new message.\n"));
708 else
709 printf(_("Loaded %d new messages.\n"), i);
710 } else
711 printf(_("Loaded %d messages.\n"), msgCount);
713 check_folder_hook(TRU1);
715 mdot = getmdot(1);
717 if (ok_blook(header))
718 print_headers(omsgCount + 1, msgCount, FAL0);
719 NYD_LEAVE;
720 return mdot;
723 FL bool_t
724 commands(void)
726 struct eval_ctx ev;
727 int n;
728 bool_t volatile rv = TRU1;
729 NYD_ENTER;
731 if (!(pstate & PS_SOURCING)) {
732 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
733 safe_signal(SIGINT, onintr);
734 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
735 safe_signal(SIGHUP, hangup);
736 /* TODO We do a lot of redundant signal handling, especially
737 * TODO with the command line editor(s); try to merge this */
738 safe_signal(SIGTSTP, stop);
739 safe_signal(SIGTTOU, stop);
740 safe_signal(SIGTTIN, stop);
742 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
743 safe_signal(SIGPIPE, _oldpipe);
745 memset(&ev, 0, sizeof ev);
747 setexit();
748 for (;;) {
749 char *temporary_orig_line; /* XXX eval_ctx.ev_line not yet constant */
751 /* TODO Unless we have our signal manager (or however we do it) child
752 * TODO processes may have time slots where their execution isn't
753 * TODO protected by signal handlers (in between start and setup
754 * TODO completed). close_all_files() is only called from onintr()
755 * TODO so those may linger possibly forever */
756 if (!(pstate & PS_SOURCING))
757 close_all_files();
759 interrupts = 0;
760 handlerstacktop = NULL;
762 #ifdef HAVE_COLOUR
763 colour_table = NULL; /* XXX intermediate hack */
764 #endif
765 if (temporary_localopts_store != NULL) /* XXX intermediate hack */
766 temporary_localopts_free(); /* XXX intermediate hack */
767 sreset((pstate & PS_SOURCING) != 0);
768 if (!(pstate & PS_SOURCING)) {
769 char *cp;
771 /* TODO Note: this buffer may contain a password. We should redefine
772 * TODO the code flow which has to do that */
773 if ((cp = termios_state.ts_linebuf) != NULL) {
774 termios_state.ts_linebuf = NULL;
775 termios_state.ts_linesize = 0;
776 free(cp); /* TODO pool give-back */
778 /* TODO Due to expand-on-tab of NCL the buffer may grow */
779 if (ev.ev_line.l > LINESIZE * 3) {
780 free(ev.ev_line.s); /* TODO pool! but what? */
781 ev.ev_line.s = NULL;
782 ev.ev_line.l = ev.ev_line_size = 0;
786 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
787 char *cp;
789 cp = ok_vlook(newmail);
790 if ((options & OPT_TTYIN) && (cp != NULL || mb.mb_type == MB_IMAP)) {
791 struct stat st;
793 n = (cp != NULL && strcmp(cp, "noimap") && strcmp(cp, "nopoll"));
794 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
795 st.st_size > mailsize) ||
796 #ifdef HAVE_IMAP
797 (mb.mb_type == MB_IMAP && imap_newmail(n) > (cp == NULL)) ||
798 #endif
799 (mb.mb_type == MB_MAILDIR && n != 0)) {
800 size_t odot = PTR2SIZE(dot - message);
801 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
803 if (setfile(mailname,
804 FEDIT_NEWMAIL |
805 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
806 exit_status |= EXIT_ERR;
807 rv = FAL0;
808 break;
810 if (mb.mb_type != MB_IMAP) {
811 dot = message + odot;
812 pstate |= odid;
817 _reset_on_stop = 1;
818 exit_status = EXIT_OK;
821 /* Read a line of commands and handle end of file specially */
822 jreadline:
823 ev.ev_line.l = ev.ev_line_size;
824 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
825 ev.ev_new_content);
826 ev.ev_line_size = (ui32_t)ev.ev_line.l;
827 ev.ev_line.l = (ui32_t)n;
828 _reset_on_stop = 0;
829 if (n < 0) {
830 /* EOF */
831 if (pstate & PS_LOADING)
832 break;
833 if (pstate & PS_SOURCING) {
834 unstack();
835 continue;
837 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
838 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
839 continue;
841 break;
844 temporary_orig_line = ((pstate & PS_SOURCING) ||
845 !(options & OPT_INTERACTIVE)) ? NULL
846 : savestrbuf(ev.ev_line.s, ev.ev_line.l);
847 pstate &= ~PS_HOOK_MASK;
848 if (evaluate(&ev)) {
849 if (pstate & PS_LOADING) /* TODO mess; join PS_EVAL_ERROR.. */
850 rv = FAL0;
851 break;
853 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
854 if (exit_status != EXIT_OK)
855 break;
856 if ((pstate & (PS_IN_LOAD | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
857 exit_status = EXIT_ERR;
858 break;
861 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
862 if (ev.ev_new_content != NULL)
863 goto jreadline;
864 /* That *can* happen since evaluate() unstack()s on error! */
865 if (temporary_orig_line != NULL)
866 tty_addhist(temporary_orig_line, !ev.ev_add_history);
870 if (ev.ev_line.s != NULL)
871 free(ev.ev_line.s);
872 if (pstate & PS_SOURCING)
873 sreset(FAL0);
874 NYD_LEAVE;
875 return rv;
878 FL int
879 execute(char *linebuf, size_t linesize) /* XXX LEGACY */
881 struct eval_ctx ev;
882 #ifdef HAVE_COLOUR
883 struct colour_table *ct_save;
884 #endif
885 int rv;
886 NYD_ENTER;
888 /* TODO Maybe recursion from within collect.c! As long as we don't have
889 * TODO a value carrier that transports the entire state of a recursion
890 * TODO we need to save away also the colour table */
891 #ifdef HAVE_COLOUR
892 ct_save = colour_table;
893 colour_table = NULL;
894 #endif
896 memset(&ev, 0, sizeof ev);
897 ev.ev_line.s = linebuf;
898 ev.ev_line.l = linesize;
899 ev.ev_is_recursive = TRU1;
900 rv = evaluate(&ev);
902 #ifdef HAVE_COLOUR
903 colour_table = ct_save;
904 #endif
905 NYD_LEAVE;
906 return rv;
909 FL int
910 evaluate(struct eval_ctx *evp)
912 struct str line;
913 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
914 struct cmd_ghost *cg = NULL;
915 struct cmd const *com = NULL;
916 int muvec[2], c, e = 1;
917 NYD_ENTER;
919 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
920 evp->ev_add_history = FAL0;
921 evp->ev_new_content = NULL;
923 /* Command ghosts that refer to shell commands or macro expansion restart */
924 jrestart:
926 /* Strip the white space away from the beginning of the command */
927 for (cp = line.s; whitechar(*cp); ++cp)
929 line.l -= PTR2SIZE(cp - line.s);
931 /* Ignore comments */
932 if (*cp == '#')
933 goto jleave0;
935 /* Handle ! differently to get the correct lexical conventions */
936 if (*cp == '!') {
937 if (pstate & PS_SOURCING) {
938 n_err(_("Can't `!' while `source'ing\n"));
939 goto jleave;
941 c_shell(++cp);
942 evp->ev_add_history = TRU1;
943 goto jleave0;
946 /* Isolate the actual command; since it may not necessarily be
947 * separated from the arguments (as in `p1') we need to duplicate it to
948 * be able to create a NUL terminated version.
949 * We must be aware of several special one letter commands here */
950 arglist[0] = cp;
951 if ((cp = _lex_isolate(cp)) == arglist[0] &&
952 (*cp == '|' || *cp == '~' || *cp == '?'))
953 ++cp;
954 c = (int)PTR2SIZE(cp - arglist[0]);
955 line.l -= c;
956 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
957 memcpy(word, arglist[0], c);
958 word[c] = '\0';
960 /* Look up the command; if not found, bitch.
961 * Normally, a blank command would map to the first command in the
962 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
963 * confusion; act just the same for ghosts */
964 if (*word == '\0') {
965 if ((pstate & PS_SOURCING) || cg != NULL)
966 goto jleave0;
967 com = _cmd_tab + 0;
968 goto jexec;
971 /* If this is the first evaluation, check command ghosts */
972 if (cg == NULL) {
973 /* TODO relink list head, so it's sorted on usage over time?
974 * TODO in fact, there should be one hashmap over all commands and ghosts
975 * TODO so that the lookup could be made much more efficient than it is
976 * TODO now (two adjacent list searches! */
977 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
978 if (!strcmp(word, cg->name)) {
979 if (line.l > 0) {
980 size_t i = cg->cmd.l;
981 line.s = salloc(i + line.l +1);
982 memcpy(line.s, cg->cmd.s, i);
983 memcpy(line.s + i, cp, line.l);
984 line.s[i += line.l] = '\0';
985 line.l = i;
986 } else {
987 line.s = cg->cmd.s;
988 line.l = cg->cmd.l;
990 goto jrestart;
994 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
995 bool_t s = condstack_isskip();
996 if (!s || (options & OPT_D_V))
997 n_err(_("Unknown command%s: `%s'\n"),
998 (s ? _(" (conditionally ignored)") : ""), word);
999 if (s)
1000 goto jleave0;
1001 if (com != NULL) {
1002 c_cmdnotsupp(NULL);
1003 com = NULL;
1005 goto jleave;
1008 /* See if we should execute the command -- if a conditional we always
1009 * execute it, otherwise, check the state of cond */
1010 jexec:
1011 if (!(com->argtype & ARG_F) && condstack_isskip())
1012 goto jleave0;
1014 /* Process the arguments to the command, depending on the type it expects,
1015 * default to error. If we're PS_SOURCING an interactive command: error */
1016 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
1017 n_err(_("May not execute `%s' while sending\n"), com->name);
1018 goto jleave;
1020 if ((pstate & PS_SOURCING) && (com->argtype & ARG_I)) {
1021 n_err(_("May not execute `%s' while `source'ing\n"), com->name);
1022 goto jleave;
1024 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
1025 n_err(_("May not execute `%s' -- message file is read only\n"),
1026 com->name);
1027 goto jleave;
1029 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
1030 n_err(_("Cannot recursively invoke `%s'\n"), com->name);
1031 goto jleave;
1033 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
1034 n_err(_("Cannot execute `%s' without active mailbox\n"), com->name);
1035 goto jleave;
1037 if (com->argtype & ARG_O)
1038 OBSOLETE2(_("this command will be removed"), com->name);
1040 if (com->argtype & ARG_V)
1041 temporary_arg_v_store = NULL;
1043 switch (com->argtype & ARG_ARGMASK) {
1044 case ARG_MSGLIST:
1045 /* Message list defaulting to nearest forward legal message */
1046 if (_msgvec == NULL)
1047 goto je96;
1048 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1049 break;
1050 if (c == 0) {
1051 *_msgvec = first(com->msgflag, com->msgmask);
1052 if (*_msgvec != 0)
1053 _msgvec[1] = 0;
1055 if (*_msgvec == 0) {
1056 if (!(pstate & PS_HOOK_MASK))
1057 printf(_("No applicable messages\n"));
1058 break;
1060 e = (*com->func)(_msgvec);
1061 break;
1063 case ARG_NDMLIST:
1064 /* Message list with no defaults, but no error if none exist */
1065 if (_msgvec == NULL) {
1066 je96:
1067 n_err(_("Invalid use of \"message list\"\n"));
1068 break;
1070 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1071 break;
1072 e = (*com->func)(_msgvec);
1073 break;
1075 case ARG_STRLIST:
1076 /* Just the straight string, with leading blanks removed */
1077 while (whitechar(*cp))
1078 ++cp;
1079 e = (*com->func)(cp);
1080 break;
1082 case ARG_RAWLIST:
1083 case ARG_ECHOLIST:
1084 /* A vector of strings, in shell style */
1085 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
1086 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
1087 break;
1088 if (c < com->minargs) {
1089 n_err(_("`%s' requires at least %d arg(s)\n"),
1090 com->name, com->minargs);
1091 break;
1093 if (c > com->maxargs) {
1094 n_err(_("`%s' takes no more than %d arg(s)\n"),
1095 com->name, com->maxargs);
1096 break;
1098 e = (*com->func)(arglist);
1099 break;
1101 case ARG_NOLIST:
1102 /* Just the constant zero, for exiting, eg. */
1103 e = (*com->func)(0);
1104 break;
1106 default:
1107 n_panic(_("Unknown argument type"));
1110 if (e == 0 && (com->argtype & ARG_V) &&
1111 (cp = temporary_arg_v_store) != NULL) {
1112 temporary_arg_v_store = NULL;
1113 evp->ev_new_content = cp;
1114 goto jleave0;
1116 if (!(com->argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
1117 evp->ev_add_history = TRU1;
1119 jleave:
1120 /* Exit the current source file on error TODO what a mess! */
1121 if (e == 0)
1122 pstate &= ~PS_EVAL_ERROR;
1123 else {
1124 pstate |= PS_EVAL_ERROR;
1125 if (e < 0 || (pstate & PS_LOADING)) {
1126 e = 1;
1127 goto jret;
1129 if (pstate & PS_SOURCING)
1130 unstack();
1131 goto jret0;
1133 if (com == NULL)
1134 goto jret0;
1135 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1136 if (visible(dot)) {
1137 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1138 muvec[1] = 0;
1139 c_type(muvec); /* TODO what if error? re-eval! */
1141 if (!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(com->argtype & ARG_T))
1142 pstate |= PS_SAW_COMMAND;
1143 jleave0:
1144 pstate &= ~PS_EVAL_ERROR;
1145 jret0:
1146 e = 0;
1147 jret:
1148 NYD_LEAVE;
1149 return e;
1152 FL void
1153 setmsize(int sz)
1155 NYD_ENTER;
1156 if (_msgvec != NULL)
1157 free(_msgvec);
1158 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1159 NYD_LEAVE;
1162 FL void
1163 print_header_summary(char const *Larg)
1165 size_t bot, top, i, j;
1166 NYD_ENTER;
1168 if (Larg != NULL) {
1169 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1170 if ((options & (OPT_VERB | OPT_HEADERSONLY)) == OPT_HEADERSONLY) {
1171 freopen("/dev/null", "w", stdout);
1172 freopen("/dev/null", "w", stderr);
1174 assert(_msgvec != NULL);
1175 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), _msgvec, 0) <= 0);
1176 if (options & OPT_HEADERSONLY) {
1177 exit_status = (int)i;
1178 goto jleave;
1180 if (i)
1181 goto jleave;
1182 for (bot = msgCount, top = 0, i = 0; (j = _msgvec[i]) != 0; ++i) {
1183 if (bot > j)
1184 bot = j;
1185 if (top < j)
1186 top = j;
1188 } else
1189 bot = 1, top = msgCount;
1190 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
1191 jleave:
1192 NYD_LEAVE;
1195 FL void
1196 onintr(int s)
1198 NYD_X; /* Signal handler */
1200 if (handlerstacktop != NULL) {
1201 handlerstacktop(s);
1202 return;
1204 safe_signal(SIGINT, onintr);
1205 noreset = 0;
1206 if (!_lex_inithdr)
1207 pstate |= PS_SAW_COMMAND;
1208 _lex_inithdr = 0;
1209 while (pstate & PS_SOURCING)
1210 unstack();
1212 termios_state_reset();
1213 close_all_files();
1215 if (image >= 0) {
1216 close(image);
1217 image = -1;
1219 if (interrupts != 1)
1220 n_err_sighdl(_("Interrupt\n"));
1221 safe_signal(SIGPIPE, _oldpipe);
1222 reset(0);
1225 FL void
1226 announce(int printheaders)
1228 int vec[2], mdot;
1229 NYD_ENTER;
1231 mdot = newfileinfo();
1232 vec[0] = mdot;
1233 vec[1] = 0;
1234 dot = message + mdot - 1;
1235 if (printheaders && msgCount > 0 && ok_blook(header)) {
1236 ++_lex_inithdr;
1237 print_header_group(vec); /* XXX errors? */
1238 _lex_inithdr = 0;
1240 NYD_LEAVE;
1243 FL int
1244 newfileinfo(void)
1246 struct message *mp;
1247 int u, n, mdot, d, s, hidden, moved;
1248 NYD_ENTER;
1250 if (mb.mb_type == MB_VOID) {
1251 mdot = 1;
1252 goto jleave;
1255 mdot = getmdot(0);
1256 s = d = hidden = moved =0;
1257 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1258 if (mp->m_flag & MNEW)
1259 ++n;
1260 if ((mp->m_flag & MREAD) == 0)
1261 ++u;
1262 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1263 ++moved;
1264 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1265 ++d;
1266 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1267 ++s;
1268 if (mp->m_flag & MHIDDEN)
1269 ++hidden;
1272 /* If displayname gets truncated the user effectively has no option to see
1273 * the full pathname of the mailbox, so print it at least for '? fi' */
1274 printf(_("\"%s\": "),
1275 (_update_mailname(NULL) ? displayname : mailname));
1276 if (msgCount == 1)
1277 printf(_("1 message"));
1278 else
1279 printf(_("%d messages"), msgCount);
1280 if (n > 0)
1281 printf(_(" %d new"), n);
1282 if (u-n > 0)
1283 printf(_(" %d unread"), u);
1284 if (d > 0)
1285 printf(_(" %d deleted"), d);
1286 if (s > 0)
1287 printf(_(" %d saved"), s);
1288 if (moved > 0)
1289 printf(_(" %d moved"), moved);
1290 if (hidden > 0)
1291 printf(_(" %d hidden"), hidden);
1292 if (mb.mb_type == MB_CACHE)
1293 printf(" [Disconnected]");
1294 else if (mb.mb_perm == 0)
1295 printf(_(" [Read only]"));
1296 printf("\n");
1297 jleave:
1298 NYD_LEAVE;
1299 return mdot;
1302 FL int
1303 getmdot(int nmail)
1305 struct message *mp;
1306 char *cp;
1307 int mdot;
1308 enum mflag avoid = MHIDDEN | MDELETED;
1309 NYD_ENTER;
1311 if (!nmail) {
1312 if (ok_blook(autothread)) {
1313 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1314 c_thread(NULL);
1315 } else if ((cp = ok_vlook(autosort)) != NULL) {
1316 if (mb.mb_sorted != NULL)
1317 free(mb.mb_sorted);
1318 mb.mb_sorted = sstrdup(cp);
1319 c_sort(NULL);
1322 if (mb.mb_type == MB_VOID) {
1323 mdot = 1;
1324 goto jleave;
1327 if (nmail)
1328 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1329 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1330 break;
1332 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1333 if (mb.mb_threaded) {
1334 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1335 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1336 break;
1337 } else {
1338 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1339 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1340 break;
1344 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1345 if (mb.mb_threaded) {
1346 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1347 if (mp->m_flag & MFLAGGED)
1348 break;
1349 } else {
1350 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1351 if (mp->m_flag & MFLAGGED)
1352 break;
1356 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1357 if (mb.mb_threaded) {
1358 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1359 if (!(mp->m_flag & (MREAD | avoid)))
1360 break;
1361 } else {
1362 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1363 if (!(mp->m_flag & (MREAD | avoid)))
1364 break;
1368 if (nmail &&
1369 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1370 mdot = (int)PTR2SIZE(mp - message + 1);
1371 else if (ok_blook(showlast)) {
1372 if (mb.mb_threaded) {
1373 for (mp = this_in_thread(threadroot, -1); mp;
1374 mp = prev_in_thread(mp))
1375 if (!(mp->m_flag & avoid))
1376 break;
1377 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1378 } else {
1379 for (mp = message + msgCount - 1; mp >= message; --mp)
1380 if (!(mp->m_flag & avoid))
1381 break;
1382 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1384 } else if (!nmail &&
1385 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1386 mdot = (int)PTR2SIZE(mp - message + 1);
1387 else if (mb.mb_threaded) {
1388 for (mp = threadroot; mp; mp = next_in_thread(mp))
1389 if (!(mp->m_flag & avoid))
1390 break;
1391 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1392 } else {
1393 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1394 if (!(mp->m_flag & avoid))
1395 break;
1396 mdot = PTRCMP(mp, <, message + msgCount)
1397 ? (int)PTR2SIZE(mp - message + 1) : 1;
1399 jleave:
1400 NYD_LEAVE;
1401 return mdot;
1404 FL void
1405 initbox(char const *name)
1407 char *tempMesg;
1408 NYD_ENTER;
1410 if (mb.mb_type != MB_VOID)
1411 n_strlcpy(prevfile, mailname, PATH_MAX);
1413 /* TODO name always NE mailname (but goes away for objects anyway)
1414 * TODO Well, not true no more except that in parens */
1415 _update_mailname((name != mailname) ? name : NULL);
1417 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpbox", OF_WRONLY | OF_HOLDSIGS, 0600)) ==
1418 NULL) {
1419 n_perr(_("temporary mail message file"), 0);
1420 exit(EXIT_ERR);
1422 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1423 n_perr(_("temporary mail message file"), 0);
1424 exit(EXIT_ERR);
1426 Ftmp_release(&tempMesg);
1428 message_reset();
1429 mb.mb_active = MB_NONE;
1430 mb.mb_threaded = 0;
1431 if (mb.mb_sorted != NULL) {
1432 free(mb.mb_sorted);
1433 mb.mb_sorted = NULL;
1435 #ifdef HAVE_IMAP
1436 mb.mb_flags = MB_NOFLAGS;
1437 #endif
1438 dot = prevdot = threadroot = NULL;
1439 pstate &= ~PS_DID_PRINT_DOT;
1440 NYD_LEAVE;
1443 #ifdef HAVE_DOCSTRINGS
1444 FL bool_t
1445 print_comm_docstr(char const *comm)
1447 struct cmd_ghost const *cg;
1448 struct cmd const *cp;
1449 bool_t rv = FAL0;
1450 NYD_ENTER;
1452 /* Ghosts take precedence */
1453 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
1454 if (!strcmp(comm, cg->name)) {
1455 printf("%s -> ", comm);
1456 comm = cg->cmd.s;
1457 break;
1460 for (cp = _cmd_tab; cp->name != NULL; ++cp) {
1461 if (cp->func == &c_cmdnotsupp)
1462 continue;
1463 if (!strcmp(comm, cp->name))
1464 printf("%s: %s\n", comm, V_(cp->doc));
1465 else if (is_prefix(comm, cp->name))
1466 printf("%s (%s): %s\n", comm, cp->name, V_(cp->doc));
1467 else
1468 continue;
1469 rv = TRU1;
1470 break;
1473 if (!rv && cg != NULL) {
1474 printf("\"%s\"\n", comm);
1475 rv = TRU1;
1477 NYD_LEAVE;
1478 return rv;
1480 #endif
1482 /* s-it-mode */