THANKS: Michael Convey
[s-mailx.git] / lex.c
blobf74a60f16d3f3e66a1a7896f1bf37cab2105c7be
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 (i < sizeof(tbuf) -1)
172 tbuf[i++] = '/';
173 if (!strncmp(tbuf, mailp, i)) {
174 mailp += i;
175 *dispp++ = '+';
179 /* We want to see the name of the folder .. on the screen */
180 i = strlen(mailp);
181 if (i < sizeof(displayname) -1)
182 memcpy(dispp, mailp, i +1);
183 else {
184 rv = FAL0;
185 /* Avoid disrupting multibyte sequences (if possible) */
186 #ifndef HAVE_C90AMEND1
187 j = sizeof(displayname) / 3 - 1;
188 i -= sizeof(displayname) - (1/* + */ + 3) - j;
189 #else
190 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
191 i = j + __narrow_suffix(mailp + j, i - j,
192 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
193 #endif
194 snprintf(dispp, sizeof(displayname), "%.*s...%s",
195 (int)j, mailp, mailp + i);
197 NYD_LEAVE;
198 return rv;
201 static char *
202 _lex_isolate(char const *comm)
204 NYD_ENTER;
205 while (*comm != '\0' &&
206 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
207 ++comm;
208 NYD_LEAVE;
209 return UNCONST(comm);
212 static struct cmd const *
213 _lex(char const *comm) /* TODO **command hashtable**! linear list search!!! */
215 struct cmd const *cp;
216 NYD_ENTER;
218 for (cp = _cmd_tab; cp->name != NULL; ++cp)
219 if (*comm == *cp->name && is_prefix(comm, cp->name))
220 goto jleave;
221 cp = NULL;
222 jleave:
223 NYD_LEAVE;
224 return cp;
227 static int
228 _c_ghost(void *v)
230 char const **argv = v;
231 struct cmd_ghost *lcg, *cg;
232 size_t i, cl, nl;
233 char *cp;
234 NYD_ENTER;
236 /* Show the list? */
237 if (*argv == NULL) {
238 FILE *fp;
240 if ((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
241 fp = stdout;
242 for (i = 0, cg = _cmd_ghosts; cg != NULL; cg = cg->next)
243 fprintf(fp, "ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
244 if (fp != stdout) {
245 page_or_print(fp, i);
246 Fclose(fp);
248 goto jleave;
251 /* Verify the ghost name is a valid one */
252 if (*argv[0] == '\0' || *_lex_isolate(argv[0]) != '\0') {
253 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
254 v = NULL;
255 goto jleave;
258 /* Show command of single ghost? */
259 if (argv[1] == NULL) {
260 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
261 if (!strcmp(argv[0], cg->name)) {
262 printf("ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
263 goto jleave;
265 n_err(_("`ghost': no such alias: \"%s\"\n"), argv[0]);
266 v = NULL;
267 goto jleave;
270 /* Define command for ghost: verify command content */
271 for (cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
272 if (*cp != '\0')
273 cl += strlen(cp) + 1; /* SP or NUL */
274 if (cl == 0) {
275 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv[0]);
276 v = NULL;
277 goto jleave;
280 /* If the ghost already exists, recreate */
281 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
282 if (!strcmp(cg->name, argv[0])) {
283 if (lcg != NULL)
284 lcg->next = cg->next;
285 else
286 _cmd_ghosts = cg->next;
287 free(cg);
288 break;
291 nl = strlen(argv[0]) +1;
292 cg = smalloc(sizeof(*cg) - VFIELD_SIZEOF(struct cmd_ghost, name) + nl + cl);
293 cg->next = _cmd_ghosts;
294 _cmd_ghosts = cg;
295 memcpy(cg->name, argv[0], nl);
296 cp = cg->cmd.s = cg->name + nl;
297 cg->cmd.l = --cl;
298 while (*++argv != NULL) {
299 i = strlen(*argv);
300 if (i > 0) {
301 memcpy(cp, *argv, i);
302 cp += i;
303 *cp++ = ' ';
306 *--cp = '\0';
307 jleave:
308 NYD_LEAVE;
309 return (v == NULL);
312 static int
313 _c_unghost(void *v)
315 int rv = 0;
316 char const **argv = v, *cp;
317 struct cmd_ghost *lcg, *cg;
318 NYD_ENTER;
320 while ((cp = *argv++) != NULL) {
321 if (cp[0] == '*' && cp[1] == '\0') {
322 while ((cg = _cmd_ghosts) != NULL) {
323 _cmd_ghosts = cg->next;
324 free(cg);
326 } else {
327 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
328 if (!strcmp(cg->name, cp)) {
329 if (lcg != NULL)
330 lcg->next = cg->next;
331 else
332 _cmd_ghosts = cg->next;
333 free(cg);
334 goto jouter;
336 n_err(_("`unghost': no such alias: \"%s\"\n"), cp);
337 rv = 1;
338 jouter:
342 NYD_LEAVE;
343 return rv;
346 static int
347 __pcmd_cmp(void const *s1, void const *s2)
349 struct cmd const * const *c1 = s1, * const *c2 = s2;
350 int rv;
351 NYD_ENTER;
353 rv = strcmp((*c1)->name, (*c2)->name);
354 NYD_LEAVE;
355 return rv;
358 static int
359 _c_pcmdlist(void *v)
361 struct cmd const **cpa, *cp, **cursor;
362 size_t i;
363 NYD_ENTER;
364 UNUSED(v);
366 for (i = 0; _cmd_tab[i].name != NULL; ++i)
368 ++i;
369 cpa = ac_alloc(sizeof(cp) * i);
371 for (i = 0; (cp = _cmd_tab + i)->name != NULL; ++i)
372 cpa[i] = cp;
373 cpa[i] = NULL;
375 qsort(cpa, i, sizeof(cp), &__pcmd_cmp);
377 printf(_("Commands are:\n"));
378 for (i = 0, cursor = cpa; (cp = *cursor++) != NULL;) {
379 size_t j;
380 if (cp->func == &c_cmdnotsupp)
381 continue;
382 j = strlen(cp->name) + 2;
383 if ((i += j) > 72) {
384 i = j;
385 printf("\n");
387 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->name);
390 ac_free(cpa);
391 NYD_LEAVE;
392 return 0;
395 static int
396 _c_quit(void *v)
398 int rv;
399 NYD_ENTER;
400 UNUSED(v);
402 /* If we are PS_SOURCING, then return 1 so evaluate() can handle it.
403 * Otherwise return -1 to abort command loop */
404 rv = (pstate & PS_SOURCING) ? 1 : -1;
405 NYD_LEAVE;
406 return rv;
409 static int
410 _c_features(void *v)
412 NYD_ENTER;
413 UNUSED(v);
414 printf(_("Features: %s\n"), ok_vlook(features));
415 NYD_LEAVE;
416 return 0;
419 static int
420 _c_version(void *v)
422 NYD_ENTER;
423 UNUSED(v);
424 printf(_("Version %s\n"), ok_vlook(version));
425 NYD_LEAVE;
426 return 0;
429 static void
430 stop(int s)
432 sighandler_type old_action;
433 sigset_t nset;
434 NYD_X; /* Signal handler */
436 old_action = safe_signal(s, SIG_DFL);
438 sigemptyset(&nset);
439 sigaddset(&nset, s);
440 sigprocmask(SIG_UNBLOCK, &nset, NULL);
441 n_raise(s);
442 sigprocmask(SIG_BLOCK, &nset, NULL);
443 safe_signal(s, old_action);
444 if (_reset_on_stop) {
445 _reset_on_stop = 0;
446 reset(0);
450 static void
451 hangup(int s)
453 NYD_X; /* Signal handler */
454 UNUSED(s);
455 /* nothing to do? */
456 exit(EXIT_ERR);
459 FL int
460 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
462 static int shudclob;
464 struct stat stb;
465 size_t offset;
466 char const *who;
467 int rv, omsgCount = 0;
468 FILE *ibuf = NULL, *lckfp = NULL;
469 bool_t isdevnull = FAL0;
470 NYD_ENTER;
472 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
473 * necessary to make a mailbox accessible by a different user, and if that
474 * has happened, let's just let the usual file perms decide */
476 if (name[0] == '%' || ((who = shortcut_expand(name)) != NULL && *who == '%'))
477 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
479 who = (name[1] != '\0') ? name + 1 : myname;
481 if ((name = expand(name)) == NULL)
482 goto jem1;
484 switch (which_protocol(name)) {
485 case PROTO_FILE:
486 if (temporary_protocol_ext != NULL)
487 name = savecat(name, temporary_protocol_ext);
488 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
489 #ifdef HAVE_REALPATH
490 do { /* TODO we need objects, goes away then */
491 char ebuf[PATH_MAX];
492 if (realpath(name, ebuf) != NULL)
493 name = savestr(ebuf);
494 } while (0);
495 #endif
496 break;
497 case PROTO_MAILDIR:
498 shudclob = 1;
499 rv = maildir_setfile(name, fm);
500 goto jleave;
501 #ifdef HAVE_POP3
502 case PROTO_POP3:
503 shudclob = 1;
504 rv = pop3_setfile(name, fm);
505 goto jleave;
506 #endif
507 #ifdef HAVE_IMAP
508 case PROTO_IMAP:
509 shudclob = 1;
510 if ((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE)
511 rv = 1;
512 else
513 rv = imap_setfile(name, fm);
514 goto jleave;
515 #endif
516 default:
517 n_err(_("Cannot handle protocol: %s\n"), name);
518 goto jem1;
521 if ((ibuf = Zopen(name, "r")) == NULL) {
522 if (((fm & FEDIT_SYSBOX) && errno == ENOENT) || (fm & FEDIT_NEWMAIL)) {
523 if (fm & FEDIT_NEWMAIL)
524 goto jnonmail;
525 goto jnomail;
527 n_perr(name, 0);
528 goto jem1;
531 if (fstat(fileno(ibuf), &stb) == -1) {
532 if (fm & FEDIT_NEWMAIL)
533 goto jnonmail;
534 n_perr(_("fstat"), 0);
535 goto jem1;
538 if (S_ISREG(stb.st_mode) || isdevnull) {
539 /* EMPTY */
540 } else {
541 if (fm & FEDIT_NEWMAIL)
542 goto jnonmail;
543 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
544 n_perr(name, 0);
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 && !(fm & FEDIT_NEWMAIL))
554 quit();
555 #ifdef HAVE_SOCKETS
556 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
557 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
558 #endif
560 /* TODO There is no intermediate VOID box we've switched to: name may
561 * TODO point to the same box that we just have written, so any updates
562 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
563 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
564 /* TODO We may uselessly open twice but quit() doesn't say wether we were
565 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
566 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
567 name = mailname;
568 Fclose(ibuf);
570 if ((ibuf = Zopen(name, "r")) == NULL ||
571 fstat(fileno(ibuf), &stb) == -1 ||
572 (!S_ISREG(stb.st_mode) && !isdevnull)) {
573 n_perr(name, 0);
574 rele_sigs();
575 goto jem2;
579 /* Copy the messages into /tmp and set pointers */
580 if (!(fm & FEDIT_NEWMAIL)) {
581 mb.mb_type = MB_FILE;
582 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
583 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
584 if (shudclob) {
585 if (mb.mb_itf) {
586 fclose(mb.mb_itf);
587 mb.mb_itf = NULL;
589 if (mb.mb_otf) {
590 fclose(mb.mb_otf);
591 mb.mb_otf = NULL;
594 shudclob = 1;
595 if (fm & FEDIT_SYSBOX)
596 pstate &= ~PS_EDIT;
597 else
598 pstate |= PS_EDIT;
599 initbox(name);
600 offset = 0;
601 } else {
602 fseek(mb.mb_otf, 0L, SEEK_END);
603 /* TODO Doing this without holding a lock is.. And not err checking.. */
604 fseek(ibuf, mailsize, SEEK_SET);
605 offset = mailsize;
606 omsgCount = msgCount;
609 if (isdevnull)
610 lckfp = (FILE*)-1;
611 else if (!(pstate & PS_EDIT))
612 lckfp = dot_lock(name, fileno(ibuf), FLT_READ, offset,0,
613 (fm & FEDIT_NEWMAIL ? 0 : 1));
614 else if (file_lock(fileno(ibuf), FLT_READ, offset,0,
615 (fm & FEDIT_NEWMAIL ? 0 : 1)))
616 lckfp = (FILE*)-1;
618 if (lckfp == NULL) {
619 if (!(fm & FEDIT_NEWMAIL)) {
620 char const *emsg = (pstate & PS_EDIT)
621 ? N_("Unable to lock mailbox, aborting operation")
622 : N_("Unable to (dot) lock mailbox, aborting operation");
623 n_perr(V_(emsg), 0);
625 rele_sigs();
626 if (!(fm & FEDIT_NEWMAIL))
627 goto jem1;
628 goto jnonmail;
631 mailsize = fsize(ibuf);
633 /* TODO This is too simple minded? We should regenerate an index file
634 * TODO to be able to truly tell wether *anything* has changed! */
635 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
636 rele_sigs();
637 goto jnonmail;
639 setptr(ibuf, offset);
640 setmsize(msgCount);
641 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
642 mb.mb_threaded = 0;
643 c_sort((void*)-1);
646 Fclose(ibuf);
647 ibuf = NULL;
648 if (lckfp != NULL && lckfp != (FILE*)-1) {
649 Pclose(lckfp, FAL0);
650 /*lckfp = NULL;*/
652 rele_sigs();
654 if (!(fm & FEDIT_NEWMAIL))
655 pstate &= ~PS_SAW_COMMAND;
657 if (options & OPT_EXISTONLY) {
658 rv = (msgCount == 0);
659 goto jleave;
662 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
663 if (!(fm & FEDIT_NEWMAIL)) {
664 jnomail:
665 if (!ok_blook(emptystart))
666 n_err(_("No mail for %s\n"), who);
668 jnonmail:
669 rv = 1;
670 goto jleave;
672 if (fm & FEDIT_NEWMAIL)
673 newmailinfo(omsgCount);
675 rv = 0;
676 jleave:
677 if (ibuf != NULL) {
678 Fclose(ibuf);
679 if (lckfp != NULL && lckfp != (FILE*)-1)
680 Pclose(lckfp, FAL0);
682 NYD_LEAVE;
683 return rv;
684 jem2:
685 mb.mb_type = MB_VOID;
686 jem1:
687 rv = -1;
688 goto jleave;
691 FL int
692 newmailinfo(int omsgCount)
694 int mdot, i;
695 NYD_ENTER;
697 for (i = 0; i < omsgCount; ++i)
698 message[i].m_flag &= ~MNEWEST;
700 if (msgCount > omsgCount) {
701 for (i = omsgCount; i < msgCount; ++i)
702 message[i].m_flag |= MNEWEST;
703 printf(_("New mail has arrived.\n"));
704 if ((i = msgCount - omsgCount) == 1)
705 printf(_("Loaded 1 new message.\n"));
706 else
707 printf(_("Loaded %d new messages.\n"), i);
708 } else
709 printf(_("Loaded %d messages.\n"), msgCount);
711 check_folder_hook(TRU1);
713 mdot = getmdot(1);
715 if (ok_blook(header))
716 print_headers(omsgCount + 1, msgCount, FAL0);
717 NYD_LEAVE;
718 return mdot;
721 FL bool_t
722 commands(void)
724 struct eval_ctx ev;
725 int n;
726 bool_t volatile rv = TRU1;
727 NYD_ENTER;
729 if (!(pstate & PS_SOURCING)) {
730 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
731 safe_signal(SIGINT, onintr);
732 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
733 safe_signal(SIGHUP, hangup);
734 /* TODO We do a lot of redundant signal handling, especially
735 * TODO with the command line editor(s); try to merge this */
736 safe_signal(SIGTSTP, stop);
737 safe_signal(SIGTTOU, stop);
738 safe_signal(SIGTTIN, stop);
740 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
741 safe_signal(SIGPIPE, _oldpipe);
743 memset(&ev, 0, sizeof ev);
745 setexit();
746 for (;;) {
747 char *temporary_orig_line; /* XXX eval_ctx.ev_line not yet constant */
749 /* TODO Unless we have our signal manager (or however we do it) child
750 * TODO processes may have time slots where their execution isn't
751 * TODO protected by signal handlers (in between start and setup
752 * TODO completed). close_all_files() is only called from onintr()
753 * TODO so those may linger possibly forever */
754 if (!(pstate & PS_SOURCING))
755 close_all_files();
757 interrupts = 0;
758 handlerstacktop = NULL;
760 #ifdef HAVE_COLOUR
761 colour_table = NULL; /* XXX intermediate hack */
762 #endif
763 if (temporary_localopts_store != NULL) /* XXX intermediate hack */
764 temporary_localopts_free(); /* XXX intermediate hack */
765 sreset((pstate & PS_SOURCING) != 0);
766 if (!(pstate & PS_SOURCING)) {
767 char *cp;
769 /* TODO Note: this buffer may contain a password. We should redefine
770 * TODO the code flow which has to do that */
771 if ((cp = termios_state.ts_linebuf) != NULL) {
772 termios_state.ts_linebuf = NULL;
773 termios_state.ts_linesize = 0;
774 free(cp); /* TODO pool give-back */
776 /* TODO Due to expand-on-tab of NCL the buffer may grow */
777 if (ev.ev_line.l > LINESIZE * 3) {
778 free(ev.ev_line.s); /* TODO pool! but what? */
779 ev.ev_line.s = NULL;
780 ev.ev_line.l = ev.ev_line_size = 0;
784 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
785 char *cp;
787 cp = ok_vlook(newmail);
788 if ((options & OPT_TTYIN) && (cp != NULL || mb.mb_type == MB_IMAP)) {
789 struct stat st;
791 n = (cp != NULL && strcmp(cp, "noimap") && strcmp(cp, "nopoll"));
792 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
793 st.st_size > mailsize) ||
794 #ifdef HAVE_IMAP
795 (mb.mb_type == MB_IMAP && imap_newmail(n) > (cp == NULL)) ||
796 #endif
797 (mb.mb_type == MB_MAILDIR && n != 0)) {
798 size_t odot = PTR2SIZE(dot - message);
799 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
801 if (setfile(mailname,
802 FEDIT_NEWMAIL |
803 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
804 exit_status |= EXIT_ERR;
805 rv = FAL0;
806 break;
808 if (mb.mb_type != MB_IMAP) {
809 dot = message + odot;
810 pstate |= odid;
815 _reset_on_stop = 1;
816 exit_status = EXIT_OK;
819 /* Read a line of commands and handle end of file specially */
820 jreadline:
821 ev.ev_line.l = ev.ev_line_size;
822 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
823 ev.ev_new_content);
824 ev.ev_line_size = (ui32_t)ev.ev_line.l;
825 ev.ev_line.l = (ui32_t)n;
826 _reset_on_stop = 0;
827 if (n < 0) {
828 /* EOF */
829 if (pstate & PS_LOADING)
830 break;
831 if (pstate & PS_SOURCING) {
832 unstack();
833 continue;
835 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
836 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
837 continue;
839 break;
842 temporary_orig_line = ((pstate & PS_SOURCING) ||
843 !(options & OPT_INTERACTIVE)) ? NULL
844 : savestrbuf(ev.ev_line.s, ev.ev_line.l);
845 pstate &= ~PS_HOOK_MASK;
846 if (evaluate(&ev)) {
847 if (pstate & PS_LOADING) /* TODO mess; join PS_EVAL_ERROR.. */
848 rv = FAL0;
849 break;
851 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
852 if (exit_status != EXIT_OK)
853 break;
854 if ((pstate & (PS_IN_LOAD | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
855 exit_status = EXIT_ERR;
856 break;
859 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
860 if (ev.ev_new_content != NULL)
861 goto jreadline;
862 /* That *can* happen since evaluate() unstack()s on error! */
863 if (temporary_orig_line != NULL)
864 tty_addhist(temporary_orig_line, !ev.ev_add_history);
868 if (ev.ev_line.s != NULL)
869 free(ev.ev_line.s);
870 if (pstate & PS_SOURCING)
871 sreset(FAL0);
872 NYD_LEAVE;
873 return rv;
876 FL int
877 execute(char *linebuf, size_t linesize) /* XXX LEGACY */
879 struct eval_ctx ev;
880 #ifdef HAVE_COLOUR
881 struct colour_table *ct_save;
882 #endif
883 int rv;
884 NYD_ENTER;
886 /* TODO Maybe recursion from within collect.c! As long as we don't have
887 * TODO a value carrier that transports the entire state of a recursion
888 * TODO we need to save away also the colour table */
889 #ifdef HAVE_COLOUR
890 ct_save = colour_table;
891 colour_table = NULL;
892 #endif
894 memset(&ev, 0, sizeof ev);
895 ev.ev_line.s = linebuf;
896 ev.ev_line.l = linesize;
897 ev.ev_is_recursive = TRU1;
898 rv = evaluate(&ev);
900 #ifdef HAVE_COLOUR
901 colour_table = ct_save;
902 #endif
903 NYD_LEAVE;
904 return rv;
907 FL int
908 evaluate(struct eval_ctx *evp)
910 struct str line;
911 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
912 struct cmd_ghost *cg = NULL;
913 struct cmd const *com = NULL;
914 int muvec[2], c, e = 1;
915 NYD_ENTER;
917 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
918 evp->ev_add_history = FAL0;
919 evp->ev_new_content = NULL;
921 /* Command ghosts that refer to shell commands or macro expansion restart */
922 jrestart:
924 /* Strip the white space away from the beginning of the command */
925 for (cp = line.s; whitechar(*cp); ++cp)
927 line.l -= PTR2SIZE(cp - line.s);
929 /* Ignore comments */
930 if (*cp == '#')
931 goto jleave0;
933 /* Handle ! differently to get the correct lexical conventions */
934 if (*cp == '!') {
935 if (pstate & PS_SOURCING) {
936 n_err(_("Can't `!' while `source'ing\n"));
937 goto jleave;
939 c_shell(++cp);
940 evp->ev_add_history = TRU1;
941 goto jleave0;
944 /* Isolate the actual command; since it may not necessarily be
945 * separated from the arguments (as in `p1') we need to duplicate it to
946 * be able to create a NUL terminated version.
947 * We must be aware of several special one letter commands here */
948 arglist[0] = cp;
949 if ((cp = _lex_isolate(cp)) == arglist[0] &&
950 (*cp == '|' || *cp == '~' || *cp == '?'))
951 ++cp;
952 c = (int)PTR2SIZE(cp - arglist[0]);
953 line.l -= c;
954 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
955 memcpy(word, arglist[0], c);
956 word[c] = '\0';
958 /* Look up the command; if not found, bitch.
959 * Normally, a blank command would map to the first command in the
960 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
961 * confusion; act just the same for ghosts */
962 if (*word == '\0') {
963 if ((pstate & PS_SOURCING) || cg != NULL)
964 goto jleave0;
965 com = _cmd_tab + 0;
966 goto jexec;
969 /* If this is the first evaluation, check command ghosts */
970 if (cg == NULL) {
971 /* TODO relink list head, so it's sorted on usage over time?
972 * TODO in fact, there should be one hashmap over all commands and ghosts
973 * TODO so that the lookup could be made much more efficient than it is
974 * TODO now (two adjacent list searches! */
975 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
976 if (!strcmp(word, cg->name)) {
977 if (line.l > 0) {
978 size_t i = cg->cmd.l;
979 line.s = salloc(i + line.l +1);
980 memcpy(line.s, cg->cmd.s, i);
981 memcpy(line.s + i, cp, line.l);
982 line.s[i += line.l] = '\0';
983 line.l = i;
984 } else {
985 line.s = cg->cmd.s;
986 line.l = cg->cmd.l;
988 goto jrestart;
992 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
993 bool_t s = condstack_isskip();
994 if (!s || (options & OPT_D_V))
995 n_err(_("Unknown command%s: `%s'\n"),
996 (s ? _(" (conditionally ignored)") : ""), word);
997 if (s)
998 goto jleave0;
999 if (com != NULL) {
1000 c_cmdnotsupp(NULL);
1001 com = NULL;
1003 goto jleave;
1006 /* See if we should execute the command -- if a conditional we always
1007 * execute it, otherwise, check the state of cond */
1008 jexec:
1009 if (!(com->argtype & ARG_F) && condstack_isskip())
1010 goto jleave0;
1012 /* Process the arguments to the command, depending on the type it expects,
1013 * default to error. If we're PS_SOURCING an interactive command: error */
1014 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
1015 n_err(_("May not execute `%s' while sending\n"), com->name);
1016 goto jleave;
1018 if ((pstate & PS_SOURCING) && (com->argtype & ARG_I)) {
1019 n_err(_("May not execute `%s' while `source'ing\n"), com->name);
1020 goto jleave;
1022 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
1023 n_err(_("May not execute `%s' -- message file is read only\n"),
1024 com->name);
1025 goto jleave;
1027 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
1028 n_err(_("Cannot recursively invoke `%s'\n"), com->name);
1029 goto jleave;
1031 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
1032 n_err(_("Cannot execute `%s' without active mailbox\n"), com->name);
1033 goto jleave;
1035 if (com->argtype & ARG_O)
1036 OBSOLETE2(_("this command will be removed"), com->name);
1038 if (com->argtype & ARG_V)
1039 temporary_arg_v_store = NULL;
1041 switch (com->argtype & ARG_ARGMASK) {
1042 case ARG_MSGLIST:
1043 /* Message list defaulting to nearest forward legal message */
1044 if (_msgvec == NULL)
1045 goto je96;
1046 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1047 break;
1048 if (c == 0) {
1049 *_msgvec = first(com->msgflag, com->msgmask);
1050 if (*_msgvec != 0)
1051 _msgvec[1] = 0;
1053 if (*_msgvec == 0) {
1054 if (!(pstate & PS_HOOK_MASK))
1055 printf(_("No applicable messages\n"));
1056 break;
1058 e = (*com->func)(_msgvec);
1059 break;
1061 case ARG_NDMLIST:
1062 /* Message list with no defaults, but no error if none exist */
1063 if (_msgvec == NULL) {
1064 je96:
1065 n_err(_("Invalid use of \"message list\"\n"));
1066 break;
1068 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1069 break;
1070 e = (*com->func)(_msgvec);
1071 break;
1073 case ARG_STRLIST:
1074 /* Just the straight string, with leading blanks removed */
1075 while (whitechar(*cp))
1076 ++cp;
1077 e = (*com->func)(cp);
1078 break;
1080 case ARG_RAWLIST:
1081 case ARG_ECHOLIST:
1082 /* A vector of strings, in shell style */
1083 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
1084 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
1085 break;
1086 if (c < com->minargs) {
1087 n_err(_("`%s' requires at least %d arg(s)\n"),
1088 com->name, com->minargs);
1089 break;
1091 if (c > com->maxargs) {
1092 n_err(_("`%s' takes no more than %d arg(s)\n"),
1093 com->name, com->maxargs);
1094 break;
1096 e = (*com->func)(arglist);
1097 break;
1099 case ARG_NOLIST:
1100 /* Just the constant zero, for exiting, eg. */
1101 e = (*com->func)(0);
1102 break;
1104 default:
1105 n_panic(_("Unknown argument type"));
1108 if (e == 0 && (com->argtype & ARG_V) &&
1109 (cp = temporary_arg_v_store) != NULL) {
1110 temporary_arg_v_store = NULL;
1111 evp->ev_new_content = cp;
1112 goto jleave0;
1114 if (!(com->argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
1115 evp->ev_add_history = TRU1;
1117 jleave:
1118 /* Exit the current source file on error TODO what a mess! */
1119 if (e == 0)
1120 pstate &= ~PS_EVAL_ERROR;
1121 else {
1122 pstate |= PS_EVAL_ERROR;
1123 if (e < 0 || (pstate & PS_LOADING)) {
1124 e = 1;
1125 goto jret;
1127 if (pstate & PS_SOURCING)
1128 unstack();
1129 goto jret0;
1131 if (com == NULL)
1132 goto jret0;
1133 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1134 if (visible(dot)) {
1135 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1136 muvec[1] = 0;
1137 c_type(muvec); /* TODO what if error? re-eval! */
1139 if (!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(com->argtype & ARG_T))
1140 pstate |= PS_SAW_COMMAND;
1141 jleave0:
1142 pstate &= ~PS_EVAL_ERROR;
1143 jret0:
1144 e = 0;
1145 jret:
1146 NYD_LEAVE;
1147 return e;
1150 FL void
1151 setmsize(int sz)
1153 NYD_ENTER;
1154 if (_msgvec != NULL)
1155 free(_msgvec);
1156 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1157 NYD_LEAVE;
1160 FL void
1161 print_header_summary(char const *Larg)
1163 size_t bot, top, i, j;
1164 NYD_ENTER;
1166 if (Larg != NULL) {
1167 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1168 if ((options & (OPT_VERB | OPT_HEADERSONLY)) == OPT_HEADERSONLY) {
1169 freopen("/dev/null", "w", stdout);
1170 freopen("/dev/null", "w", stderr);
1172 assert(_msgvec != NULL);
1173 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), _msgvec, 0) <= 0);
1174 if (options & OPT_HEADERSONLY) {
1175 exit_status = (int)i;
1176 goto jleave;
1178 if (i)
1179 goto jleave;
1180 for (bot = msgCount, top = 0, i = 0; (j = _msgvec[i]) != 0; ++i) {
1181 if (bot > j)
1182 bot = j;
1183 if (top < j)
1184 top = j;
1186 } else
1187 bot = 1, top = msgCount;
1188 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
1189 jleave:
1190 NYD_LEAVE;
1193 FL void
1194 onintr(int s)
1196 NYD_X; /* Signal handler */
1198 if (handlerstacktop != NULL) {
1199 handlerstacktop(s);
1200 return;
1202 safe_signal(SIGINT, onintr);
1203 noreset = 0;
1204 if (!_lex_inithdr)
1205 pstate |= PS_SAW_COMMAND;
1206 _lex_inithdr = 0;
1207 while (pstate & PS_SOURCING)
1208 unstack();
1210 termios_state_reset();
1211 close_all_files();
1213 if (image >= 0) {
1214 close(image);
1215 image = -1;
1217 if (interrupts != 1)
1218 n_err_sighdl(_("Interrupt\n"));
1219 safe_signal(SIGPIPE, _oldpipe);
1220 reset(0);
1223 FL void
1224 announce(int printheaders)
1226 int vec[2], mdot;
1227 NYD_ENTER;
1229 mdot = newfileinfo();
1230 vec[0] = mdot;
1231 vec[1] = 0;
1232 dot = message + mdot - 1;
1233 if (printheaders && msgCount > 0 && ok_blook(header)) {
1234 ++_lex_inithdr;
1235 print_header_group(vec); /* XXX errors? */
1236 _lex_inithdr = 0;
1238 NYD_LEAVE;
1241 FL int
1242 newfileinfo(void)
1244 struct message *mp;
1245 int u, n, mdot, d, s, hidden, moved;
1246 NYD_ENTER;
1248 if (mb.mb_type == MB_VOID) {
1249 mdot = 1;
1250 goto jleave;
1253 mdot = getmdot(0);
1254 s = d = hidden = moved =0;
1255 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1256 if (mp->m_flag & MNEW)
1257 ++n;
1258 if ((mp->m_flag & MREAD) == 0)
1259 ++u;
1260 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1261 ++moved;
1262 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1263 ++d;
1264 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1265 ++s;
1266 if (mp->m_flag & MHIDDEN)
1267 ++hidden;
1270 /* If displayname gets truncated the user effectively has no option to see
1271 * the full pathname of the mailbox, so print it at least for '? fi' */
1272 printf(_("\"%s\": "),
1273 (_update_mailname(NULL) ? displayname : mailname));
1274 if (msgCount == 1)
1275 printf(_("1 message"));
1276 else
1277 printf(_("%d messages"), msgCount);
1278 if (n > 0)
1279 printf(_(" %d new"), n);
1280 if (u-n > 0)
1281 printf(_(" %d unread"), u);
1282 if (d > 0)
1283 printf(_(" %d deleted"), d);
1284 if (s > 0)
1285 printf(_(" %d saved"), s);
1286 if (moved > 0)
1287 printf(_(" %d moved"), moved);
1288 if (hidden > 0)
1289 printf(_(" %d hidden"), hidden);
1290 if (mb.mb_type == MB_CACHE)
1291 printf(" [Disconnected]");
1292 else if (mb.mb_perm == 0)
1293 printf(_(" [Read only]"));
1294 printf("\n");
1295 jleave:
1296 NYD_LEAVE;
1297 return mdot;
1300 FL int
1301 getmdot(int nmail)
1303 struct message *mp;
1304 char *cp;
1305 int mdot;
1306 enum mflag avoid = MHIDDEN | MDELETED;
1307 NYD_ENTER;
1309 if (!nmail) {
1310 if (ok_blook(autothread)) {
1311 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1312 c_thread(NULL);
1313 } else if ((cp = ok_vlook(autosort)) != NULL) {
1314 if (mb.mb_sorted != NULL)
1315 free(mb.mb_sorted);
1316 mb.mb_sorted = sstrdup(cp);
1317 c_sort(NULL);
1320 if (mb.mb_type == MB_VOID) {
1321 mdot = 1;
1322 goto jleave;
1325 if (nmail)
1326 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1327 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1328 break;
1330 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1331 if (mb.mb_threaded) {
1332 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1333 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1334 break;
1335 } else {
1336 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1337 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1338 break;
1342 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1343 if (mb.mb_threaded) {
1344 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1345 if (mp->m_flag & MFLAGGED)
1346 break;
1347 } else {
1348 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1349 if (mp->m_flag & MFLAGGED)
1350 break;
1354 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1355 if (mb.mb_threaded) {
1356 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1357 if (!(mp->m_flag & (MREAD | avoid)))
1358 break;
1359 } else {
1360 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1361 if (!(mp->m_flag & (MREAD | avoid)))
1362 break;
1366 if (nmail &&
1367 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1368 mdot = (int)PTR2SIZE(mp - message + 1);
1369 else if (ok_blook(showlast)) {
1370 if (mb.mb_threaded) {
1371 for (mp = this_in_thread(threadroot, -1); mp;
1372 mp = prev_in_thread(mp))
1373 if (!(mp->m_flag & avoid))
1374 break;
1375 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1376 } else {
1377 for (mp = message + msgCount - 1; mp >= message; --mp)
1378 if (!(mp->m_flag & avoid))
1379 break;
1380 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1382 } else if (!nmail &&
1383 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1384 mdot = (int)PTR2SIZE(mp - message + 1);
1385 else if (mb.mb_threaded) {
1386 for (mp = threadroot; mp; mp = next_in_thread(mp))
1387 if (!(mp->m_flag & avoid))
1388 break;
1389 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1390 } else {
1391 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1392 if (!(mp->m_flag & avoid))
1393 break;
1394 mdot = PTRCMP(mp, <, message + msgCount)
1395 ? (int)PTR2SIZE(mp - message + 1) : 1;
1397 jleave:
1398 NYD_LEAVE;
1399 return mdot;
1402 FL void
1403 initbox(char const *name)
1405 char *tempMesg;
1406 NYD_ENTER;
1408 if (mb.mb_type != MB_VOID)
1409 n_strlcpy(prevfile, mailname, PATH_MAX);
1411 /* TODO name always NE mailname (but goes away for objects anyway)
1412 * TODO Well, not true no more except that in parens */
1413 _update_mailname((name != mailname) ? name : NULL);
1415 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
1416 NULL) {
1417 n_perr(_("temporary mail message file"), 0);
1418 exit(EXIT_ERR);
1420 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1421 n_perr(_("temporary mail message file"), 0);
1422 exit(EXIT_ERR);
1424 Ftmp_release(&tempMesg);
1426 message_reset();
1427 mb.mb_threaded = 0;
1428 if (mb.mb_sorted != NULL) {
1429 free(mb.mb_sorted);
1430 mb.mb_sorted = NULL;
1432 #ifdef HAVE_IMAP
1433 mb.mb_flags = MB_NOFLAGS;
1434 #endif
1435 dot = prevdot = threadroot = NULL;
1436 pstate &= ~PS_DID_PRINT_DOT;
1437 NYD_LEAVE;
1440 #ifdef HAVE_DOCSTRINGS
1441 FL bool_t
1442 print_comm_docstr(char const *comm)
1444 struct cmd_ghost const *cg;
1445 struct cmd const *cp;
1446 bool_t rv = FAL0;
1447 NYD_ENTER;
1449 /* Ghosts take precedence */
1450 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
1451 if (!strcmp(comm, cg->name)) {
1452 printf("%s -> ", comm);
1453 comm = cg->cmd.s;
1454 break;
1457 for (cp = _cmd_tab; cp->name != NULL; ++cp) {
1458 if (cp->func == &c_cmdnotsupp)
1459 continue;
1460 if (!strcmp(comm, cp->name))
1461 printf("%s: %s\n", comm, V_(cp->doc));
1462 else if (is_prefix(comm, cp->name))
1463 printf("%s (%s): %s\n", comm, cp->name, V_(cp->doc));
1464 else
1465 continue;
1466 rv = TRU1;
1467 break;
1470 if (!rv && cg != NULL) {
1471 printf("\"%s\"\n", comm);
1472 rv = TRU1;
1474 NYD_LEAVE;
1475 return rv;
1477 #endif
1479 /* s-it-mode */