FIX: really_rewind() for pre POSIX Issue 7
[s-mailx.git] / lex.c
blob86957240930c94e66eb8caddd6b89227c89438de
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, 0600)) ==
241 NULL)
242 fp = stdout;
243 for (i = 0, cg = _cmd_ghosts; cg != NULL; cg = cg->next)
244 fprintf(fp, "ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
245 if (fp != stdout) {
246 page_or_print(fp, i);
247 Fclose(fp);
249 goto jleave;
252 /* Verify the ghost name is a valid one */
253 if (*argv[0] == '\0' || *_lex_isolate(argv[0]) != '\0') {
254 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
255 v = NULL;
256 goto jleave;
259 /* Show command of single ghost? */
260 if (argv[1] == NULL) {
261 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
262 if (!strcmp(argv[0], cg->name)) {
263 printf("ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
264 goto jleave;
266 n_err(_("`ghost': no such alias: \"%s\"\n"), argv[0]);
267 v = NULL;
268 goto jleave;
271 /* Define command for ghost: verify command content */
272 for (cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
273 if (*cp != '\0')
274 cl += strlen(cp) + 1; /* SP or NUL */
275 if (cl == 0) {
276 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv[0]);
277 v = NULL;
278 goto jleave;
281 /* If the ghost already exists, recreate */
282 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
283 if (!strcmp(cg->name, argv[0])) {
284 if (lcg != NULL)
285 lcg->next = cg->next;
286 else
287 _cmd_ghosts = cg->next;
288 free(cg);
289 break;
292 nl = strlen(argv[0]) +1;
293 cg = smalloc(sizeof(*cg) - VFIELD_SIZEOF(struct cmd_ghost, name) + nl + cl);
294 cg->next = _cmd_ghosts;
295 _cmd_ghosts = cg;
296 memcpy(cg->name, argv[0], nl);
297 cp = cg->cmd.s = cg->name + nl;
298 cg->cmd.l = --cl;
299 while (*++argv != NULL) {
300 i = strlen(*argv);
301 if (i > 0) {
302 memcpy(cp, *argv, i);
303 cp += i;
304 *cp++ = ' ';
307 *--cp = '\0';
308 jleave:
309 NYD_LEAVE;
310 return (v == NULL);
313 static int
314 _c_unghost(void *v)
316 int rv = 0;
317 char const **argv = v, *cp;
318 struct cmd_ghost *lcg, *cg;
319 NYD_ENTER;
321 while ((cp = *argv++) != NULL) {
322 if (cp[0] == '*' && cp[1] == '\0') {
323 while ((cg = _cmd_ghosts) != NULL) {
324 _cmd_ghosts = cg->next;
325 free(cg);
327 } else {
328 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
329 if (!strcmp(cg->name, cp)) {
330 if (lcg != NULL)
331 lcg->next = cg->next;
332 else
333 _cmd_ghosts = cg->next;
334 free(cg);
335 goto jouter;
337 n_err(_("`unghost': no such alias: \"%s\"\n"), cp);
338 rv = 1;
339 jouter:
343 NYD_LEAVE;
344 return rv;
347 static int
348 __pcmd_cmp(void const *s1, void const *s2)
350 struct cmd const * const *c1 = s1, * const *c2 = s2;
351 int rv;
352 NYD_ENTER;
354 rv = strcmp((*c1)->name, (*c2)->name);
355 NYD_LEAVE;
356 return rv;
359 static int
360 _c_pcmdlist(void *v)
362 struct cmd const **cpa, *cp, **cursor;
363 size_t i;
364 NYD_ENTER;
365 UNUSED(v);
367 for (i = 0; _cmd_tab[i].name != NULL; ++i)
369 ++i;
370 cpa = ac_alloc(sizeof(cp) * i);
372 for (i = 0; (cp = _cmd_tab + i)->name != NULL; ++i)
373 cpa[i] = cp;
374 cpa[i] = NULL;
376 qsort(cpa, i, sizeof(cp), &__pcmd_cmp);
378 printf(_("Commands are:\n"));
379 for (i = 0, cursor = cpa; (cp = *cursor++) != NULL;) {
380 size_t j;
381 if (cp->func == &c_cmdnotsupp)
382 continue;
383 j = strlen(cp->name) + 2;
384 if ((i += j) > 72) {
385 i = j;
386 printf("\n");
388 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->name);
391 ac_free(cpa);
392 NYD_LEAVE;
393 return 0;
396 static int
397 _c_quit(void *v)
399 int rv;
400 NYD_ENTER;
401 UNUSED(v);
403 /* If we are PS_SOURCING, then return 1 so evaluate() can handle it.
404 * Otherwise return -1 to abort command loop */
405 rv = (pstate & PS_SOURCING) ? 1 : -1;
406 NYD_LEAVE;
407 return rv;
410 static int
411 _c_features(void *v)
413 NYD_ENTER;
414 UNUSED(v);
415 printf(_("Features: %s\n"), ok_vlook(features));
416 NYD_LEAVE;
417 return 0;
420 static int
421 _c_version(void *v)
423 NYD_ENTER;
424 UNUSED(v);
425 printf(_("Version %s\n"), ok_vlook(version));
426 NYD_LEAVE;
427 return 0;
430 static void
431 stop(int s)
433 sighandler_type old_action;
434 sigset_t nset;
435 NYD_X; /* Signal handler */
437 old_action = safe_signal(s, SIG_DFL);
439 sigemptyset(&nset);
440 sigaddset(&nset, s);
441 sigprocmask(SIG_UNBLOCK, &nset, NULL);
442 n_raise(s);
443 sigprocmask(SIG_BLOCK, &nset, NULL);
444 safe_signal(s, old_action);
445 if (_reset_on_stop) {
446 _reset_on_stop = 0;
447 reset(0);
451 static void
452 hangup(int s)
454 NYD_X; /* Signal handler */
455 UNUSED(s);
456 /* nothing to do? */
457 exit(EXIT_ERR);
460 FL int
461 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
463 static int shudclob;
465 struct stat stb;
466 size_t offset;
467 char const *who;
468 int rv, omsgCount = 0;
469 FILE *ibuf = NULL, *lckfp = NULL;
470 bool_t isdevnull = FAL0;
471 NYD_ENTER;
473 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
474 * necessary to make a mailbox accessible by a different user, and if that
475 * has happened, let's just let the usual file perms decide */
477 if (name[0] == '%' || ((who = shortcut_expand(name)) != NULL && *who == '%'))
478 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
480 who = (name[1] != '\0') ? name + 1 : myname;
482 if ((name = expand(name)) == NULL)
483 goto jem1;
485 switch (which_protocol(name)) {
486 case PROTO_FILE:
487 if (temporary_protocol_ext != NULL)
488 name = savecat(name, temporary_protocol_ext);
489 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
490 #ifdef HAVE_REALPATH
491 do { /* TODO we need objects, goes away then */
492 char ebuf[PATH_MAX];
493 if (realpath(name, ebuf) != NULL)
494 name = savestr(ebuf);
495 } while (0);
496 #endif
497 break;
498 case PROTO_MAILDIR:
499 shudclob = 1;
500 rv = maildir_setfile(name, fm);
501 goto jleave;
502 #ifdef HAVE_POP3
503 case PROTO_POP3:
504 shudclob = 1;
505 rv = pop3_setfile(name, fm);
506 goto jleave;
507 #endif
508 #ifdef HAVE_IMAP
509 case PROTO_IMAP:
510 shudclob = 1;
511 if ((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE)
512 rv = 1;
513 else
514 rv = imap_setfile(name, fm);
515 goto jleave;
516 #endif
517 default:
518 n_err(_("Cannot handle protocol: %s\n"), name);
519 goto jem1;
522 if ((ibuf = Zopen(name, "r")) == NULL) {
523 if (((fm & FEDIT_SYSBOX) && errno == ENOENT) || (fm & FEDIT_NEWMAIL)) {
524 if (fm & FEDIT_NEWMAIL)
525 goto jnonmail;
526 goto jnomail;
528 n_perr(name, 0);
529 goto jem1;
532 if (fstat(fileno(ibuf), &stb) == -1) {
533 if (fm & FEDIT_NEWMAIL)
534 goto jnonmail;
535 n_perr(_("fstat"), 0);
536 goto jem1;
539 if (S_ISREG(stb.st_mode) || isdevnull) {
540 /* EMPTY */
541 } else {
542 if (fm & FEDIT_NEWMAIL)
543 goto jnonmail;
544 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
545 n_perr(name, 0);
546 goto jem1;
549 /* Looks like all will be well. We must now relinquish our hold on the
550 * current set of stuff. Must hold signals while we are reading the new
551 * file, else we will ruin the message[] data structure */
553 hold_sigs(); /* TODO note on this one in quit.c:quit() */
554 if (shudclob && !(fm & FEDIT_NEWMAIL))
555 quit();
556 #ifdef HAVE_SOCKETS
557 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
558 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
559 #endif
561 /* TODO There is no intermediate VOID box we've switched to: name may
562 * TODO point to the same box that we just have written, so any updates
563 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
564 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
565 /* TODO We may uselessly open twice but quit() doesn't say wether we were
566 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
567 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
568 name = mailname;
569 Fclose(ibuf);
571 if ((ibuf = Zopen(name, "r")) == NULL ||
572 fstat(fileno(ibuf), &stb) == -1 ||
573 (!S_ISREG(stb.st_mode) && !isdevnull)) {
574 n_perr(name, 0);
575 rele_sigs();
576 goto jem2;
580 /* Copy the messages into /tmp and set pointers */
581 if (!(fm & FEDIT_NEWMAIL)) {
582 mb.mb_type = MB_FILE;
583 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
584 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
585 if (shudclob) {
586 if (mb.mb_itf) {
587 fclose(mb.mb_itf);
588 mb.mb_itf = NULL;
590 if (mb.mb_otf) {
591 fclose(mb.mb_otf);
592 mb.mb_otf = NULL;
595 shudclob = 1;
596 if (fm & FEDIT_SYSBOX)
597 pstate &= ~PS_EDIT;
598 else
599 pstate |= PS_EDIT;
600 initbox(name);
601 offset = 0;
602 } else {
603 fseek(mb.mb_otf, 0L, SEEK_END);
604 /* TODO Doing this without holding a lock is.. And not err checking.. */
605 fseek(ibuf, mailsize, SEEK_SET);
606 offset = mailsize;
607 omsgCount = msgCount;
610 if (isdevnull)
611 lckfp = (FILE*)-1;
612 else if (!(pstate & PS_EDIT))
613 lckfp = dot_lock(name, fileno(ibuf), FLT_READ, offset,0,
614 (fm & FEDIT_NEWMAIL ? 0 : 1));
615 else if (file_lock(fileno(ibuf), FLT_READ, offset,0,
616 (fm & FEDIT_NEWMAIL ? 0 : 1)))
617 lckfp = (FILE*)-1;
619 if (lckfp == NULL) {
620 if (!(fm & FEDIT_NEWMAIL)) {
621 char const *emsg = (pstate & PS_EDIT)
622 ? N_("Unable to lock mailbox, aborting operation")
623 : N_("Unable to (dot) lock mailbox, aborting operation");
624 n_perr(V_(emsg), 0);
626 rele_sigs();
627 if (!(fm & FEDIT_NEWMAIL))
628 goto jem1;
629 goto jnonmail;
632 mailsize = fsize(ibuf);
634 /* TODO This is too simple minded? We should regenerate an index file
635 * TODO to be able to truly tell wether *anything* has changed! */
636 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
637 rele_sigs();
638 goto jnonmail;
640 setptr(ibuf, offset);
641 setmsize(msgCount);
642 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
643 mb.mb_threaded = 0;
644 c_sort((void*)-1);
647 Fclose(ibuf);
648 ibuf = NULL;
649 if (lckfp != NULL && lckfp != (FILE*)-1) {
650 Pclose(lckfp, FAL0);
651 /*lckfp = NULL;*/
653 rele_sigs();
655 if (!(fm & FEDIT_NEWMAIL))
656 pstate &= ~PS_SAW_COMMAND;
658 if (options & OPT_EXISTONLY) {
659 rv = (msgCount == 0);
660 goto jleave;
663 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
664 if (!(fm & FEDIT_NEWMAIL)) {
665 jnomail:
666 if (!ok_blook(emptystart))
667 n_err(_("No mail for %s\n"), who);
669 jnonmail:
670 rv = 1;
671 goto jleave;
673 if (fm & FEDIT_NEWMAIL)
674 newmailinfo(omsgCount);
676 rv = 0;
677 jleave:
678 if (ibuf != NULL) {
679 Fclose(ibuf);
680 if (lckfp != NULL && lckfp != (FILE*)-1)
681 Pclose(lckfp, FAL0);
683 NYD_LEAVE;
684 return rv;
685 jem2:
686 mb.mb_type = MB_VOID;
687 jem1:
688 rv = -1;
689 goto jleave;
692 FL int
693 newmailinfo(int omsgCount)
695 int mdot, i;
696 NYD_ENTER;
698 for (i = 0; i < omsgCount; ++i)
699 message[i].m_flag &= ~MNEWEST;
701 if (msgCount > omsgCount) {
702 for (i = omsgCount; i < msgCount; ++i)
703 message[i].m_flag |= MNEWEST;
704 printf(_("New mail has arrived.\n"));
705 if ((i = msgCount - omsgCount) == 1)
706 printf(_("Loaded 1 new message.\n"));
707 else
708 printf(_("Loaded %d new messages.\n"), i);
709 } else
710 printf(_("Loaded %d messages.\n"), msgCount);
712 check_folder_hook(TRU1);
714 mdot = getmdot(1);
716 if (ok_blook(header))
717 print_headers(omsgCount + 1, msgCount, FAL0);
718 NYD_LEAVE;
719 return mdot;
722 FL bool_t
723 commands(void)
725 struct eval_ctx ev;
726 int n;
727 bool_t volatile rv = TRU1;
728 NYD_ENTER;
730 if (!(pstate & PS_SOURCING)) {
731 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
732 safe_signal(SIGINT, onintr);
733 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
734 safe_signal(SIGHUP, hangup);
735 /* TODO We do a lot of redundant signal handling, especially
736 * TODO with the command line editor(s); try to merge this */
737 safe_signal(SIGTSTP, stop);
738 safe_signal(SIGTTOU, stop);
739 safe_signal(SIGTTIN, stop);
741 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
742 safe_signal(SIGPIPE, _oldpipe);
744 memset(&ev, 0, sizeof ev);
746 setexit();
747 for (;;) {
748 char *temporary_orig_line; /* XXX eval_ctx.ev_line not yet constant */
750 /* TODO Unless we have our signal manager (or however we do it) child
751 * TODO processes may have time slots where their execution isn't
752 * TODO protected by signal handlers (in between start and setup
753 * TODO completed). close_all_files() is only called from onintr()
754 * TODO so those may linger possibly forever */
755 if (!(pstate & PS_SOURCING))
756 close_all_files();
758 interrupts = 0;
759 handlerstacktop = NULL;
761 #ifdef HAVE_COLOUR
762 colour_table = NULL; /* XXX intermediate hack */
763 #endif
764 if (temporary_localopts_store != NULL) /* XXX intermediate hack */
765 temporary_localopts_free(); /* XXX intermediate hack */
766 sreset((pstate & PS_SOURCING) != 0);
767 if (!(pstate & PS_SOURCING)) {
768 char *cp;
770 /* TODO Note: this buffer may contain a password. We should redefine
771 * TODO the code flow which has to do that */
772 if ((cp = termios_state.ts_linebuf) != NULL) {
773 termios_state.ts_linebuf = NULL;
774 termios_state.ts_linesize = 0;
775 free(cp); /* TODO pool give-back */
777 /* TODO Due to expand-on-tab of NCL the buffer may grow */
778 if (ev.ev_line.l > LINESIZE * 3) {
779 free(ev.ev_line.s); /* TODO pool! but what? */
780 ev.ev_line.s = NULL;
781 ev.ev_line.l = ev.ev_line_size = 0;
785 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
786 char *cp;
788 cp = ok_vlook(newmail);
789 if ((options & OPT_TTYIN) && (cp != NULL || mb.mb_type == MB_IMAP)) {
790 struct stat st;
792 n = (cp != NULL && strcmp(cp, "noimap") && strcmp(cp, "nopoll"));
793 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
794 st.st_size > mailsize) ||
795 #ifdef HAVE_IMAP
796 (mb.mb_type == MB_IMAP && imap_newmail(n) > (cp == NULL)) ||
797 #endif
798 (mb.mb_type == MB_MAILDIR && n != 0)) {
799 size_t odot = PTR2SIZE(dot - message);
800 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
802 if (setfile(mailname,
803 FEDIT_NEWMAIL |
804 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
805 exit_status |= EXIT_ERR;
806 rv = FAL0;
807 break;
809 if (mb.mb_type != MB_IMAP) {
810 dot = message + odot;
811 pstate |= odid;
816 _reset_on_stop = 1;
817 exit_status = EXIT_OK;
820 /* Read a line of commands and handle end of file specially */
821 jreadline:
822 ev.ev_line.l = ev.ev_line_size;
823 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
824 ev.ev_new_content);
825 ev.ev_line_size = (ui32_t)ev.ev_line.l;
826 ev.ev_line.l = (ui32_t)n;
827 _reset_on_stop = 0;
828 if (n < 0) {
829 /* EOF */
830 if (pstate & PS_LOADING)
831 break;
832 if (pstate & PS_SOURCING) {
833 unstack();
834 continue;
836 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
837 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
838 continue;
840 break;
843 temporary_orig_line = ((pstate & PS_SOURCING) ||
844 !(options & OPT_INTERACTIVE)) ? NULL
845 : savestrbuf(ev.ev_line.s, ev.ev_line.l);
846 pstate &= ~PS_HOOK_MASK;
847 if (evaluate(&ev)) {
848 if (pstate & PS_LOADING) /* TODO mess; join PS_EVAL_ERROR.. */
849 rv = FAL0;
850 break;
852 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
853 if (exit_status != EXIT_OK)
854 break;
855 if ((pstate & (PS_IN_LOAD | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
856 exit_status = EXIT_ERR;
857 break;
860 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
861 if (ev.ev_new_content != NULL)
862 goto jreadline;
863 /* That *can* happen since evaluate() unstack()s on error! */
864 if (temporary_orig_line != NULL)
865 tty_addhist(temporary_orig_line, !ev.ev_add_history);
869 if (ev.ev_line.s != NULL)
870 free(ev.ev_line.s);
871 if (pstate & PS_SOURCING)
872 sreset(FAL0);
873 NYD_LEAVE;
874 return rv;
877 FL int
878 execute(char *linebuf, size_t linesize) /* XXX LEGACY */
880 struct eval_ctx ev;
881 #ifdef HAVE_COLOUR
882 struct colour_table *ct_save;
883 #endif
884 int rv;
885 NYD_ENTER;
887 /* TODO Maybe recursion from within collect.c! As long as we don't have
888 * TODO a value carrier that transports the entire state of a recursion
889 * TODO we need to save away also the colour table */
890 #ifdef HAVE_COLOUR
891 ct_save = colour_table;
892 colour_table = NULL;
893 #endif
895 memset(&ev, 0, sizeof ev);
896 ev.ev_line.s = linebuf;
897 ev.ev_line.l = linesize;
898 ev.ev_is_recursive = TRU1;
899 rv = evaluate(&ev);
901 #ifdef HAVE_COLOUR
902 colour_table = ct_save;
903 #endif
904 NYD_LEAVE;
905 return rv;
908 FL int
909 evaluate(struct eval_ctx *evp)
911 struct str line;
912 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
913 struct cmd_ghost *cg = NULL;
914 struct cmd const *com = NULL;
915 int muvec[2], c, e = 1;
916 NYD_ENTER;
918 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
919 evp->ev_add_history = FAL0;
920 evp->ev_new_content = NULL;
922 /* Command ghosts that refer to shell commands or macro expansion restart */
923 jrestart:
925 /* Strip the white space away from the beginning of the command */
926 for (cp = line.s; whitechar(*cp); ++cp)
928 line.l -= PTR2SIZE(cp - line.s);
930 /* Ignore comments */
931 if (*cp == '#')
932 goto jleave0;
934 /* Handle ! differently to get the correct lexical conventions */
935 if (*cp == '!') {
936 if (pstate & PS_SOURCING) {
937 n_err(_("Can't `!' while `source'ing\n"));
938 goto jleave;
940 c_shell(++cp);
941 evp->ev_add_history = TRU1;
942 goto jleave0;
945 /* Isolate the actual command; since it may not necessarily be
946 * separated from the arguments (as in `p1') we need to duplicate it to
947 * be able to create a NUL terminated version.
948 * We must be aware of several special one letter commands here */
949 arglist[0] = cp;
950 if ((cp = _lex_isolate(cp)) == arglist[0] &&
951 (*cp == '|' || *cp == '~' || *cp == '?'))
952 ++cp;
953 c = (int)PTR2SIZE(cp - arglist[0]);
954 line.l -= c;
955 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
956 memcpy(word, arglist[0], c);
957 word[c] = '\0';
959 /* Look up the command; if not found, bitch.
960 * Normally, a blank command would map to the first command in the
961 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
962 * confusion; act just the same for ghosts */
963 if (*word == '\0') {
964 if ((pstate & PS_SOURCING) || cg != NULL)
965 goto jleave0;
966 com = _cmd_tab + 0;
967 goto jexec;
970 /* If this is the first evaluation, check command ghosts */
971 if (cg == NULL) {
972 /* TODO relink list head, so it's sorted on usage over time?
973 * TODO in fact, there should be one hashmap over all commands and ghosts
974 * TODO so that the lookup could be made much more efficient than it is
975 * TODO now (two adjacent list searches! */
976 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
977 if (!strcmp(word, cg->name)) {
978 if (line.l > 0) {
979 size_t i = cg->cmd.l;
980 line.s = salloc(i + line.l +1);
981 memcpy(line.s, cg->cmd.s, i);
982 memcpy(line.s + i, cp, line.l);
983 line.s[i += line.l] = '\0';
984 line.l = i;
985 } else {
986 line.s = cg->cmd.s;
987 line.l = cg->cmd.l;
989 goto jrestart;
993 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
994 bool_t s = condstack_isskip();
995 if (!s || (options & OPT_D_V))
996 n_err(_("Unknown command%s: `%s'\n"),
997 (s ? _(" (conditionally ignored)") : ""), word);
998 if (s)
999 goto jleave0;
1000 if (com != NULL) {
1001 c_cmdnotsupp(NULL);
1002 com = NULL;
1004 goto jleave;
1007 /* See if we should execute the command -- if a conditional we always
1008 * execute it, otherwise, check the state of cond */
1009 jexec:
1010 if (!(com->argtype & ARG_F) && condstack_isskip())
1011 goto jleave0;
1013 /* Process the arguments to the command, depending on the type it expects,
1014 * default to error. If we're PS_SOURCING an interactive command: error */
1015 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
1016 n_err(_("May not execute `%s' while sending\n"), com->name);
1017 goto jleave;
1019 if ((pstate & PS_SOURCING) && (com->argtype & ARG_I)) {
1020 n_err(_("May not execute `%s' while `source'ing\n"), com->name);
1021 goto jleave;
1023 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
1024 n_err(_("May not execute `%s' -- message file is read only\n"),
1025 com->name);
1026 goto jleave;
1028 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
1029 n_err(_("Cannot recursively invoke `%s'\n"), com->name);
1030 goto jleave;
1032 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
1033 n_err(_("Cannot execute `%s' without active mailbox\n"), com->name);
1034 goto jleave;
1036 if (com->argtype & ARG_O)
1037 OBSOLETE2(_("this command will be removed"), com->name);
1039 if (com->argtype & ARG_V)
1040 temporary_arg_v_store = NULL;
1042 switch (com->argtype & ARG_ARGMASK) {
1043 case ARG_MSGLIST:
1044 /* Message list defaulting to nearest forward legal message */
1045 if (_msgvec == NULL)
1046 goto je96;
1047 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1048 break;
1049 if (c == 0) {
1050 *_msgvec = first(com->msgflag, com->msgmask);
1051 if (*_msgvec != 0)
1052 _msgvec[1] = 0;
1054 if (*_msgvec == 0) {
1055 if (!(pstate & PS_HOOK_MASK))
1056 printf(_("No applicable messages\n"));
1057 break;
1059 e = (*com->func)(_msgvec);
1060 break;
1062 case ARG_NDMLIST:
1063 /* Message list with no defaults, but no error if none exist */
1064 if (_msgvec == NULL) {
1065 je96:
1066 n_err(_("Invalid use of \"message list\"\n"));
1067 break;
1069 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1070 break;
1071 e = (*com->func)(_msgvec);
1072 break;
1074 case ARG_STRLIST:
1075 /* Just the straight string, with leading blanks removed */
1076 while (whitechar(*cp))
1077 ++cp;
1078 e = (*com->func)(cp);
1079 break;
1081 case ARG_RAWLIST:
1082 case ARG_ECHOLIST:
1083 /* A vector of strings, in shell style */
1084 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
1085 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
1086 break;
1087 if (c < com->minargs) {
1088 n_err(_("`%s' requires at least %d arg(s)\n"),
1089 com->name, com->minargs);
1090 break;
1092 if (c > com->maxargs) {
1093 n_err(_("`%s' takes no more than %d arg(s)\n"),
1094 com->name, com->maxargs);
1095 break;
1097 e = (*com->func)(arglist);
1098 break;
1100 case ARG_NOLIST:
1101 /* Just the constant zero, for exiting, eg. */
1102 e = (*com->func)(0);
1103 break;
1105 default:
1106 n_panic(_("Unknown argument type"));
1109 if (e == 0 && (com->argtype & ARG_V) &&
1110 (cp = temporary_arg_v_store) != NULL) {
1111 temporary_arg_v_store = NULL;
1112 evp->ev_new_content = cp;
1113 goto jleave0;
1115 if (!(com->argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
1116 evp->ev_add_history = TRU1;
1118 jleave:
1119 /* Exit the current source file on error TODO what a mess! */
1120 if (e == 0)
1121 pstate &= ~PS_EVAL_ERROR;
1122 else {
1123 pstate |= PS_EVAL_ERROR;
1124 if (e < 0 || (pstate & PS_LOADING)) {
1125 e = 1;
1126 goto jret;
1128 if (pstate & PS_SOURCING)
1129 unstack();
1130 goto jret0;
1132 if (com == NULL)
1133 goto jret0;
1134 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1135 if (visible(dot)) {
1136 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1137 muvec[1] = 0;
1138 c_type(muvec); /* TODO what if error? re-eval! */
1140 if (!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(com->argtype & ARG_T))
1141 pstate |= PS_SAW_COMMAND;
1142 jleave0:
1143 pstate &= ~PS_EVAL_ERROR;
1144 jret0:
1145 e = 0;
1146 jret:
1147 NYD_LEAVE;
1148 return e;
1151 FL void
1152 setmsize(int sz)
1154 NYD_ENTER;
1155 if (_msgvec != NULL)
1156 free(_msgvec);
1157 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1158 NYD_LEAVE;
1161 FL void
1162 print_header_summary(char const *Larg)
1164 size_t bot, top, i, j;
1165 NYD_ENTER;
1167 if (Larg != NULL) {
1168 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1169 if ((options & (OPT_VERB | OPT_HEADERSONLY)) == OPT_HEADERSONLY) {
1170 freopen("/dev/null", "w", stdout);
1171 freopen("/dev/null", "w", stderr);
1173 assert(_msgvec != NULL);
1174 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), _msgvec, 0) <= 0);
1175 if (options & OPT_HEADERSONLY) {
1176 exit_status = (int)i;
1177 goto jleave;
1179 if (i)
1180 goto jleave;
1181 for (bot = msgCount, top = 0, i = 0; (j = _msgvec[i]) != 0; ++i) {
1182 if (bot > j)
1183 bot = j;
1184 if (top < j)
1185 top = j;
1187 } else
1188 bot = 1, top = msgCount;
1189 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
1190 jleave:
1191 NYD_LEAVE;
1194 FL void
1195 onintr(int s)
1197 NYD_X; /* Signal handler */
1199 if (handlerstacktop != NULL) {
1200 handlerstacktop(s);
1201 return;
1203 safe_signal(SIGINT, onintr);
1204 noreset = 0;
1205 if (!_lex_inithdr)
1206 pstate |= PS_SAW_COMMAND;
1207 _lex_inithdr = 0;
1208 while (pstate & PS_SOURCING)
1209 unstack();
1211 termios_state_reset();
1212 close_all_files();
1214 if (image >= 0) {
1215 close(image);
1216 image = -1;
1218 if (interrupts != 1)
1219 n_err_sighdl(_("Interrupt\n"));
1220 safe_signal(SIGPIPE, _oldpipe);
1221 reset(0);
1224 FL void
1225 announce(int printheaders)
1227 int vec[2], mdot;
1228 NYD_ENTER;
1230 mdot = newfileinfo();
1231 vec[0] = mdot;
1232 vec[1] = 0;
1233 dot = message + mdot - 1;
1234 if (printheaders && msgCount > 0 && ok_blook(header)) {
1235 ++_lex_inithdr;
1236 print_header_group(vec); /* XXX errors? */
1237 _lex_inithdr = 0;
1239 NYD_LEAVE;
1242 FL int
1243 newfileinfo(void)
1245 struct message *mp;
1246 int u, n, mdot, d, s, hidden, moved;
1247 NYD_ENTER;
1249 if (mb.mb_type == MB_VOID) {
1250 mdot = 1;
1251 goto jleave;
1254 mdot = getmdot(0);
1255 s = d = hidden = moved =0;
1256 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1257 if (mp->m_flag & MNEW)
1258 ++n;
1259 if ((mp->m_flag & MREAD) == 0)
1260 ++u;
1261 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1262 ++moved;
1263 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1264 ++d;
1265 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1266 ++s;
1267 if (mp->m_flag & MHIDDEN)
1268 ++hidden;
1271 /* If displayname gets truncated the user effectively has no option to see
1272 * the full pathname of the mailbox, so print it at least for '? fi' */
1273 printf(_("\"%s\": "),
1274 (_update_mailname(NULL) ? displayname : mailname));
1275 if (msgCount == 1)
1276 printf(_("1 message"));
1277 else
1278 printf(_("%d messages"), msgCount);
1279 if (n > 0)
1280 printf(_(" %d new"), n);
1281 if (u-n > 0)
1282 printf(_(" %d unread"), u);
1283 if (d > 0)
1284 printf(_(" %d deleted"), d);
1285 if (s > 0)
1286 printf(_(" %d saved"), s);
1287 if (moved > 0)
1288 printf(_(" %d moved"), moved);
1289 if (hidden > 0)
1290 printf(_(" %d hidden"), hidden);
1291 if (mb.mb_type == MB_CACHE)
1292 printf(" [Disconnected]");
1293 else if (mb.mb_perm == 0)
1294 printf(_(" [Read only]"));
1295 printf("\n");
1296 jleave:
1297 NYD_LEAVE;
1298 return mdot;
1301 FL int
1302 getmdot(int nmail)
1304 struct message *mp;
1305 char *cp;
1306 int mdot;
1307 enum mflag avoid = MHIDDEN | MDELETED;
1308 NYD_ENTER;
1310 if (!nmail) {
1311 if (ok_blook(autothread)) {
1312 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1313 c_thread(NULL);
1314 } else if ((cp = ok_vlook(autosort)) != NULL) {
1315 if (mb.mb_sorted != NULL)
1316 free(mb.mb_sorted);
1317 mb.mb_sorted = sstrdup(cp);
1318 c_sort(NULL);
1321 if (mb.mb_type == MB_VOID) {
1322 mdot = 1;
1323 goto jleave;
1326 if (nmail)
1327 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1328 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1329 break;
1331 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1332 if (mb.mb_threaded) {
1333 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1334 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1335 break;
1336 } else {
1337 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1338 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1339 break;
1343 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1344 if (mb.mb_threaded) {
1345 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1346 if (mp->m_flag & MFLAGGED)
1347 break;
1348 } else {
1349 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1350 if (mp->m_flag & MFLAGGED)
1351 break;
1355 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1356 if (mb.mb_threaded) {
1357 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1358 if (!(mp->m_flag & (MREAD | avoid)))
1359 break;
1360 } else {
1361 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1362 if (!(mp->m_flag & (MREAD | avoid)))
1363 break;
1367 if (nmail &&
1368 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1369 mdot = (int)PTR2SIZE(mp - message + 1);
1370 else if (ok_blook(showlast)) {
1371 if (mb.mb_threaded) {
1372 for (mp = this_in_thread(threadroot, -1); mp;
1373 mp = prev_in_thread(mp))
1374 if (!(mp->m_flag & avoid))
1375 break;
1376 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1377 } else {
1378 for (mp = message + msgCount - 1; mp >= message; --mp)
1379 if (!(mp->m_flag & avoid))
1380 break;
1381 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1383 } else if (!nmail &&
1384 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1385 mdot = (int)PTR2SIZE(mp - message + 1);
1386 else if (mb.mb_threaded) {
1387 for (mp = threadroot; mp; mp = next_in_thread(mp))
1388 if (!(mp->m_flag & avoid))
1389 break;
1390 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1391 } else {
1392 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1393 if (!(mp->m_flag & avoid))
1394 break;
1395 mdot = PTRCMP(mp, <, message + msgCount)
1396 ? (int)PTR2SIZE(mp - message + 1) : 1;
1398 jleave:
1399 NYD_LEAVE;
1400 return mdot;
1403 FL void
1404 initbox(char const *name)
1406 char *tempMesg;
1407 NYD_ENTER;
1409 if (mb.mb_type != MB_VOID)
1410 n_strlcpy(prevfile, mailname, PATH_MAX);
1412 /* TODO name always NE mailname (but goes away for objects anyway)
1413 * TODO Well, not true no more except that in parens */
1414 _update_mailname((name != mailname) ? name : NULL);
1416 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpbox", OF_WRONLY | OF_HOLDSIGS, 0600)) ==
1417 NULL) {
1418 n_perr(_("temporary mail message file"), 0);
1419 exit(EXIT_ERR);
1421 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1422 n_perr(_("temporary mail message file"), 0);
1423 exit(EXIT_ERR);
1425 Ftmp_release(&tempMesg);
1427 message_reset();
1428 mb.mb_threaded = 0;
1429 if (mb.mb_sorted != NULL) {
1430 free(mb.mb_sorted);
1431 mb.mb_sorted = NULL;
1433 #ifdef HAVE_IMAP
1434 mb.mb_flags = MB_NOFLAGS;
1435 #endif
1436 dot = prevdot = threadroot = NULL;
1437 pstate &= ~PS_DID_PRINT_DOT;
1438 NYD_LEAVE;
1441 #ifdef HAVE_DOCSTRINGS
1442 FL bool_t
1443 print_comm_docstr(char const *comm)
1445 struct cmd_ghost const *cg;
1446 struct cmd const *cp;
1447 bool_t rv = FAL0;
1448 NYD_ENTER;
1450 /* Ghosts take precedence */
1451 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
1452 if (!strcmp(comm, cg->name)) {
1453 printf("%s -> ", comm);
1454 comm = cg->cmd.s;
1455 break;
1458 for (cp = _cmd_tab; cp->name != NULL; ++cp) {
1459 if (cp->func == &c_cmdnotsupp)
1460 continue;
1461 if (!strcmp(comm, cp->name))
1462 printf("%s: %s\n", comm, V_(cp->doc));
1463 else if (is_prefix(comm, cp->name))
1464 printf("%s (%s): %s\n", comm, cp->name, V_(cp->doc));
1465 else
1466 continue;
1467 rv = TRU1;
1468 break;
1471 if (!rv && cg != NULL) {
1472 printf("\"%s\"\n", comm);
1473 rv = TRU1;
1475 NYD_LEAVE;
1476 return rv;
1478 #endif
1480 /* s-it-mode */