FIX "," message specification (since [1c4b8c9], v14.8.4)..
[s-mailx.git] / lex.c
blobbb72fe56ee3476131e435e24aea95eb783c80771
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 /* For at least substdate() users */
486 time_current_update(&time_current, FAL0);
488 switch (which_protocol(name)) {
489 case PROTO_FILE:
490 if (temporary_protocol_ext != NULL)
491 name = savecat(name, temporary_protocol_ext);
492 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
493 #ifdef HAVE_REALPATH
494 do { /* TODO we need objects, goes away then */
495 char ebuf[PATH_MAX];
496 if (realpath(name, ebuf) != NULL)
497 name = savestr(ebuf);
498 } while (0);
499 #endif
500 break;
501 case PROTO_MAILDIR:
502 shudclob = 1;
503 rv = maildir_setfile(name, fm);
504 goto jleave;
505 #ifdef HAVE_POP3
506 case PROTO_POP3:
507 shudclob = 1;
508 rv = pop3_setfile(name, fm);
509 goto jleave;
510 #endif
511 #ifdef HAVE_IMAP
512 case PROTO_IMAP:
513 shudclob = 1;
514 if ((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE)
515 rv = 1;
516 else
517 rv = imap_setfile(name, fm);
518 goto jleave;
519 #endif
520 default:
521 n_err(_("Cannot handle protocol: %s\n"), name);
522 goto jem1;
525 if ((ibuf = Zopen(name, "r")) == NULL) {
526 if (((fm & FEDIT_SYSBOX) && errno == ENOENT) || (fm & FEDIT_NEWMAIL)) {
527 if (fm & FEDIT_NEWMAIL)
528 goto jnonmail;
529 goto jnomail;
531 n_perr(name, 0);
532 goto jem1;
535 if (fstat(fileno(ibuf), &stb) == -1) {
536 if (fm & FEDIT_NEWMAIL)
537 goto jnonmail;
538 n_perr(_("fstat"), 0);
539 goto jem1;
542 if (S_ISREG(stb.st_mode) || isdevnull) {
543 /* EMPTY */
544 } else {
545 if (fm & FEDIT_NEWMAIL)
546 goto jnonmail;
547 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
548 n_perr(name, 0);
549 goto jem1;
552 /* Looks like all will be well. We must now relinquish our hold on the
553 * current set of stuff. Must hold signals while we are reading the new
554 * file, else we will ruin the message[] data structure */
556 hold_sigs(); /* TODO note on this one in quit.c:quit() */
557 if (shudclob && !(fm & FEDIT_NEWMAIL))
558 quit();
559 #ifdef HAVE_SOCKETS
560 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
561 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
562 #endif
564 /* TODO There is no intermediate VOID box we've switched to: name may
565 * TODO point to the same box that we just have written, so any updates
566 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
567 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
568 /* TODO We may uselessly open twice but quit() doesn't say wether we were
569 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
570 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
571 name = mailname;
572 Fclose(ibuf);
574 if ((ibuf = Zopen(name, "r")) == NULL ||
575 fstat(fileno(ibuf), &stb) == -1 ||
576 (!S_ISREG(stb.st_mode) && !isdevnull)) {
577 n_perr(name, 0);
578 rele_sigs();
579 goto jem2;
583 /* Copy the messages into /tmp and set pointers */
584 if (!(fm & FEDIT_NEWMAIL)) {
585 mb.mb_type = MB_FILE;
586 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
587 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
588 if (shudclob) {
589 if (mb.mb_itf) {
590 fclose(mb.mb_itf);
591 mb.mb_itf = NULL;
593 if (mb.mb_otf) {
594 fclose(mb.mb_otf);
595 mb.mb_otf = NULL;
598 shudclob = 1;
599 if (fm & FEDIT_SYSBOX)
600 pstate &= ~PS_EDIT;
601 else
602 pstate |= PS_EDIT;
603 initbox(name);
604 offset = 0;
605 } else {
606 fseek(mb.mb_otf, 0L, SEEK_END);
607 /* TODO Doing this without holding a lock is.. And not err checking.. */
608 fseek(ibuf, mailsize, SEEK_SET);
609 offset = mailsize;
610 omsgCount = msgCount;
613 if (isdevnull)
614 lckfp = (FILE*)-1;
615 else if (!(pstate & PS_EDIT))
616 lckfp = dot_lock(name, fileno(ibuf), FLT_READ, offset,0,
617 (fm & FEDIT_NEWMAIL ? 0 : 1));
618 else if (file_lock(fileno(ibuf), FLT_READ, offset,0,
619 (fm & FEDIT_NEWMAIL ? 0 : 1)))
620 lckfp = (FILE*)-1;
622 if (lckfp == NULL) {
623 if (!(fm & FEDIT_NEWMAIL)) {
624 char const *emsg = (pstate & PS_EDIT)
625 ? N_("Unable to lock mailbox, aborting operation")
626 : N_("Unable to (dot) lock mailbox, aborting operation");
627 n_perr(V_(emsg), 0);
629 rele_sigs();
630 if (!(fm & FEDIT_NEWMAIL))
631 goto jem1;
632 goto jnonmail;
635 mailsize = fsize(ibuf);
637 /* TODO This is too simple minded? We should regenerate an index file
638 * TODO to be able to truly tell wether *anything* has changed! */
639 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
640 rele_sigs();
641 goto jnonmail;
643 setptr(ibuf, offset);
644 setmsize(msgCount);
645 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
646 mb.mb_threaded = 0;
647 c_sort((void*)-1);
650 Fclose(ibuf);
651 ibuf = NULL;
652 if (lckfp != NULL && lckfp != (FILE*)-1) {
653 Pclose(lckfp, FAL0);
654 /*lckfp = NULL;*/
656 rele_sigs();
658 if (!(fm & FEDIT_NEWMAIL))
659 pstate &= ~PS_SAW_COMMAND;
661 if (options & OPT_EXISTONLY) {
662 rv = (msgCount == 0);
663 goto jleave;
666 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
667 if (!(fm & FEDIT_NEWMAIL)) {
668 jnomail:
669 if (!ok_blook(emptystart))
670 n_err(_("No mail for %s\n"), who);
672 jnonmail:
673 rv = 1;
674 goto jleave;
676 if (fm & FEDIT_NEWMAIL)
677 newmailinfo(omsgCount);
679 rv = 0;
680 jleave:
681 if (ibuf != NULL) {
682 Fclose(ibuf);
683 if (lckfp != NULL && lckfp != (FILE*)-1)
684 Pclose(lckfp, FAL0);
686 NYD_LEAVE;
687 return rv;
688 jem2:
689 mb.mb_type = MB_VOID;
690 jem1:
691 rv = -1;
692 goto jleave;
695 FL int
696 newmailinfo(int omsgCount)
698 int mdot, i;
699 NYD_ENTER;
701 for (i = 0; i < omsgCount; ++i)
702 message[i].m_flag &= ~MNEWEST;
704 if (msgCount > omsgCount) {
705 for (i = omsgCount; i < msgCount; ++i)
706 message[i].m_flag |= MNEWEST;
707 printf(_("New mail has arrived.\n"));
708 if ((i = msgCount - omsgCount) == 1)
709 printf(_("Loaded 1 new message.\n"));
710 else
711 printf(_("Loaded %d new messages.\n"), i);
712 } else
713 printf(_("Loaded %d messages.\n"), msgCount);
715 check_folder_hook(TRU1);
717 mdot = getmdot(1);
719 if (ok_blook(header))
720 print_headers(omsgCount + 1, msgCount, FAL0);
721 NYD_LEAVE;
722 return mdot;
725 FL bool_t
726 commands(void)
728 struct eval_ctx ev;
729 int n;
730 bool_t volatile rv = TRU1;
731 NYD_ENTER;
733 if (!(pstate & PS_SOURCING)) {
734 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
735 safe_signal(SIGINT, onintr);
736 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
737 safe_signal(SIGHUP, hangup);
738 /* TODO We do a lot of redundant signal handling, especially
739 * TODO with the command line editor(s); try to merge this */
740 safe_signal(SIGTSTP, stop);
741 safe_signal(SIGTTOU, stop);
742 safe_signal(SIGTTIN, stop);
744 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
745 safe_signal(SIGPIPE, _oldpipe);
747 memset(&ev, 0, sizeof ev);
749 setexit();
750 for (;;) {
751 char *temporary_orig_line; /* XXX eval_ctx.ev_line not yet constant */
753 /* TODO Unless we have our signal manager (or however we do it) child
754 * TODO processes may have time slots where their execution isn't
755 * TODO protected by signal handlers (in between start and setup
756 * TODO completed). close_all_files() is only called from onintr()
757 * TODO so those may linger possibly forever */
758 if (!(pstate & PS_SOURCING))
759 close_all_files();
761 interrupts = 0;
762 handlerstacktop = NULL;
764 #ifdef HAVE_COLOUR
765 colour_table = NULL; /* XXX intermediate hack */
766 #endif
767 if (temporary_localopts_store != NULL) /* XXX intermediate hack */
768 temporary_localopts_free(); /* XXX intermediate hack */
769 sreset((pstate & PS_SOURCING) != 0);
770 if (!(pstate & PS_SOURCING)) {
771 char *cp;
773 /* TODO Note: this buffer may contain a password. We should redefine
774 * TODO the code flow which has to do that */
775 if ((cp = termios_state.ts_linebuf) != NULL) {
776 termios_state.ts_linebuf = NULL;
777 termios_state.ts_linesize = 0;
778 free(cp); /* TODO pool give-back */
780 /* TODO Due to expand-on-tab of NCL the buffer may grow */
781 if (ev.ev_line.l > LINESIZE * 3) {
782 free(ev.ev_line.s); /* TODO pool! but what? */
783 ev.ev_line.s = NULL;
784 ev.ev_line.l = ev.ev_line_size = 0;
788 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
789 char *cp;
791 cp = ok_vlook(newmail);
792 if ((options & OPT_TTYIN) && (cp != NULL || mb.mb_type == MB_IMAP)) {
793 struct stat st;
795 n = (cp != NULL && strcmp(cp, "noimap") && strcmp(cp, "nopoll"));
796 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
797 st.st_size > mailsize) ||
798 #ifdef HAVE_IMAP
799 (mb.mb_type == MB_IMAP && imap_newmail(n) > (cp == NULL)) ||
800 #endif
801 (mb.mb_type == MB_MAILDIR && n != 0)) {
802 size_t odot = PTR2SIZE(dot - message);
803 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
805 if (setfile(mailname,
806 FEDIT_NEWMAIL |
807 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
808 exit_status |= EXIT_ERR;
809 rv = FAL0;
810 break;
812 if (mb.mb_type != MB_IMAP) {
813 dot = message + odot;
814 pstate |= odid;
819 _reset_on_stop = 1;
820 exit_status = EXIT_OK;
823 /* Read a line of commands and handle end of file specially */
824 jreadline:
825 ev.ev_line.l = ev.ev_line_size;
826 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
827 ev.ev_new_content);
828 ev.ev_line_size = (ui32_t)ev.ev_line.l;
829 ev.ev_line.l = (ui32_t)n;
830 _reset_on_stop = 0;
831 if (n < 0) {
832 /* EOF */
833 if (pstate & PS_LOADING)
834 break;
835 if (pstate & PS_SOURCING) {
836 unstack();
837 continue;
839 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
840 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
841 continue;
843 break;
846 temporary_orig_line = ((pstate & PS_SOURCING) ||
847 !(options & OPT_INTERACTIVE)) ? NULL
848 : savestrbuf(ev.ev_line.s, ev.ev_line.l);
849 pstate &= ~PS_HOOK_MASK;
850 if (evaluate(&ev)) {
851 if (pstate & PS_LOADING) /* TODO mess; join PS_EVAL_ERROR.. */
852 rv = FAL0;
853 break;
855 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
856 if (exit_status != EXIT_OK)
857 break;
858 if ((pstate & (PS_IN_LOAD | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
859 exit_status = EXIT_ERR;
860 break;
863 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
864 if (ev.ev_new_content != NULL)
865 goto jreadline;
866 /* That *can* happen since evaluate() unstack()s on error! */
867 if (temporary_orig_line != NULL)
868 tty_addhist(temporary_orig_line, !ev.ev_add_history);
872 if (ev.ev_line.s != NULL)
873 free(ev.ev_line.s);
874 if (pstate & PS_SOURCING)
875 sreset(FAL0);
876 NYD_LEAVE;
877 return rv;
880 FL int
881 execute(char *linebuf, size_t linesize) /* XXX LEGACY */
883 struct eval_ctx ev;
884 #ifdef HAVE_COLOUR
885 struct colour_table *ct_save;
886 #endif
887 int rv;
888 NYD_ENTER;
890 /* TODO Maybe recursion from within collect.c! As long as we don't have
891 * TODO a value carrier that transports the entire state of a recursion
892 * TODO we need to save away also the colour table */
893 #ifdef HAVE_COLOUR
894 ct_save = colour_table;
895 colour_table = NULL;
896 #endif
898 memset(&ev, 0, sizeof ev);
899 ev.ev_line.s = linebuf;
900 ev.ev_line.l = linesize;
901 ev.ev_is_recursive = TRU1;
902 rv = evaluate(&ev);
904 #ifdef HAVE_COLOUR
905 colour_table = ct_save;
906 #endif
907 NYD_LEAVE;
908 return rv;
911 FL int
912 evaluate(struct eval_ctx *evp)
914 struct str line;
915 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
916 struct cmd_ghost *cg = NULL;
917 struct cmd const *com = NULL;
918 int muvec[2], c, e = 1;
919 NYD_ENTER;
921 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
922 evp->ev_add_history = FAL0;
923 evp->ev_new_content = NULL;
925 /* Command ghosts that refer to shell commands or macro expansion restart */
926 jrestart:
928 /* Strip the white space away from the beginning of the command */
929 for (cp = line.s; whitechar(*cp); ++cp)
931 line.l -= PTR2SIZE(cp - line.s);
933 /* Ignore comments */
934 if (*cp == '#')
935 goto jleave0;
937 /* Handle ! differently to get the correct lexical conventions */
938 if (*cp == '!') {
939 if (pstate & PS_SOURCING) {
940 n_err(_("Can't `!' while `source'ing\n"));
941 goto jleave;
943 c_shell(++cp);
944 evp->ev_add_history = TRU1;
945 goto jleave0;
948 /* Isolate the actual command; since it may not necessarily be
949 * separated from the arguments (as in `p1') we need to duplicate it to
950 * be able to create a NUL terminated version.
951 * We must be aware of several special one letter commands here */
952 arglist[0] = cp;
953 if ((cp = _lex_isolate(cp)) == arglist[0] &&
954 (*cp == '|' || *cp == '~' || *cp == '?'))
955 ++cp;
956 c = (int)PTR2SIZE(cp - arglist[0]);
957 line.l -= c;
958 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
959 memcpy(word, arglist[0], c);
960 word[c] = '\0';
962 /* Look up the command; if not found, bitch.
963 * Normally, a blank command would map to the first command in the
964 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
965 * confusion; act just the same for ghosts */
966 if (*word == '\0') {
967 if ((pstate & PS_SOURCING) || cg != NULL)
968 goto jleave0;
969 com = _cmd_tab + 0;
970 goto jexec;
973 /* If this is the first evaluation, check command ghosts */
974 if (cg == NULL) {
975 /* TODO relink list head, so it's sorted on usage over time?
976 * TODO in fact, there should be one hashmap over all commands and ghosts
977 * TODO so that the lookup could be made much more efficient than it is
978 * TODO now (two adjacent list searches! */
979 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
980 if (!strcmp(word, cg->name)) {
981 if (line.l > 0) {
982 size_t i = cg->cmd.l;
983 line.s = salloc(i + line.l +1);
984 memcpy(line.s, cg->cmd.s, i);
985 memcpy(line.s + i, cp, line.l);
986 line.s[i += line.l] = '\0';
987 line.l = i;
988 } else {
989 line.s = cg->cmd.s;
990 line.l = cg->cmd.l;
992 goto jrestart;
996 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
997 bool_t s = condstack_isskip();
998 if (!s || (options & OPT_D_V))
999 n_err(_("Unknown command%s: `%s'\n"),
1000 (s ? _(" (conditionally ignored)") : ""), word);
1001 if (s)
1002 goto jleave0;
1003 if (com != NULL) {
1004 c_cmdnotsupp(NULL);
1005 com = NULL;
1007 goto jleave;
1010 /* See if we should execute the command -- if a conditional we always
1011 * execute it, otherwise, check the state of cond */
1012 jexec:
1013 if (!(com->argtype & ARG_F) && condstack_isskip())
1014 goto jleave0;
1016 /* Process the arguments to the command, depending on the type it expects,
1017 * default to error. If we're PS_SOURCING an interactive command: error */
1018 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
1019 n_err(_("May not execute `%s' while sending\n"), com->name);
1020 goto jleave;
1022 if ((pstate & PS_SOURCING) && (com->argtype & ARG_I)) {
1023 n_err(_("May not execute `%s' while `source'ing\n"), com->name);
1024 goto jleave;
1026 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
1027 n_err(_("May not execute `%s' -- message file is read only\n"),
1028 com->name);
1029 goto jleave;
1031 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
1032 n_err(_("Cannot recursively invoke `%s'\n"), com->name);
1033 goto jleave;
1035 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
1036 n_err(_("Cannot execute `%s' without active mailbox\n"), com->name);
1037 goto jleave;
1039 if (com->argtype & ARG_O)
1040 OBSOLETE2(_("this command will be removed"), com->name);
1042 if (com->argtype & ARG_V)
1043 temporary_arg_v_store = NULL;
1045 switch (com->argtype & ARG_ARGMASK) {
1046 case ARG_MSGLIST:
1047 /* Message list defaulting to nearest forward legal message */
1048 if (_msgvec == NULL)
1049 goto je96;
1050 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1051 break;
1052 if (c == 0) {
1053 *_msgvec = first(com->msgflag, com->msgmask);
1054 if (*_msgvec != 0)
1055 _msgvec[1] = 0;
1057 if (*_msgvec == 0) {
1058 if (!(pstate & PS_HOOK_MASK))
1059 printf(_("No applicable messages\n"));
1060 break;
1062 e = (*com->func)(_msgvec);
1063 break;
1065 case ARG_NDMLIST:
1066 /* Message list with no defaults, but no error if none exist */
1067 if (_msgvec == NULL) {
1068 je96:
1069 n_err(_("Invalid use of \"message list\"\n"));
1070 break;
1072 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1073 break;
1074 e = (*com->func)(_msgvec);
1075 break;
1077 case ARG_STRLIST:
1078 /* Just the straight string, with leading blanks removed */
1079 while (whitechar(*cp))
1080 ++cp;
1081 e = (*com->func)(cp);
1082 break;
1084 case ARG_RAWLIST:
1085 case ARG_ECHOLIST:
1086 /* A vector of strings, in shell style */
1087 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
1088 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
1089 break;
1090 if (c < com->minargs) {
1091 n_err(_("`%s' requires at least %d arg(s)\n"),
1092 com->name, com->minargs);
1093 break;
1095 if (c > com->maxargs) {
1096 n_err(_("`%s' takes no more than %d arg(s)\n"),
1097 com->name, com->maxargs);
1098 break;
1100 e = (*com->func)(arglist);
1101 break;
1103 case ARG_NOLIST:
1104 /* Just the constant zero, for exiting, eg. */
1105 e = (*com->func)(0);
1106 break;
1108 default:
1109 n_panic(_("Unknown argument type"));
1112 if (e == 0 && (com->argtype & ARG_V) &&
1113 (cp = temporary_arg_v_store) != NULL) {
1114 temporary_arg_v_store = NULL;
1115 evp->ev_new_content = cp;
1116 goto jleave0;
1118 if (!(com->argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
1119 evp->ev_add_history = TRU1;
1121 jleave:
1122 /* Exit the current source file on error TODO what a mess! */
1123 if (e == 0)
1124 pstate &= ~PS_EVAL_ERROR;
1125 else {
1126 pstate |= PS_EVAL_ERROR;
1127 if (e < 0 || (pstate & PS_LOADING)) {
1128 e = 1;
1129 goto jret;
1131 if (pstate & PS_SOURCING)
1132 unstack();
1133 goto jret0;
1135 if (com == NULL)
1136 goto jret0;
1137 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1138 if (visible(dot)) {
1139 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1140 muvec[1] = 0;
1141 c_type(muvec); /* TODO what if error? re-eval! */
1143 if (!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(com->argtype & ARG_T))
1144 pstate |= PS_SAW_COMMAND;
1145 jleave0:
1146 pstate &= ~PS_EVAL_ERROR;
1147 jret0:
1148 e = 0;
1149 jret:
1150 NYD_LEAVE;
1151 return e;
1154 FL void
1155 setmsize(int sz)
1157 NYD_ENTER;
1158 if (_msgvec != NULL)
1159 free(_msgvec);
1160 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1161 NYD_LEAVE;
1164 FL void
1165 print_header_summary(char const *Larg)
1167 size_t bot, top, i, j;
1168 NYD_ENTER;
1170 if (Larg != NULL) {
1171 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1172 if ((options & (OPT_VERB | OPT_HEADERSONLY)) == OPT_HEADERSONLY) {
1173 freopen("/dev/null", "w", stdout);
1174 freopen("/dev/null", "w", stderr);
1176 assert(_msgvec != NULL);
1177 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), _msgvec, 0) <= 0);
1178 if (options & OPT_HEADERSONLY) {
1179 exit_status = (int)i;
1180 goto jleave;
1182 if (i)
1183 goto jleave;
1184 for (bot = msgCount, top = 0, i = 0; (j = _msgvec[i]) != 0; ++i) {
1185 if (bot > j)
1186 bot = j;
1187 if (top < j)
1188 top = j;
1190 } else
1191 bot = 1, top = msgCount;
1192 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
1193 jleave:
1194 NYD_LEAVE;
1197 FL void
1198 onintr(int s)
1200 NYD_X; /* Signal handler */
1202 if (handlerstacktop != NULL) {
1203 handlerstacktop(s);
1204 return;
1206 safe_signal(SIGINT, onintr);
1207 noreset = 0;
1208 if (!_lex_inithdr)
1209 pstate |= PS_SAW_COMMAND;
1210 _lex_inithdr = 0;
1211 while (pstate & PS_SOURCING)
1212 unstack();
1214 termios_state_reset();
1215 close_all_files();
1217 if (image >= 0) {
1218 close(image);
1219 image = -1;
1221 if (interrupts != 1)
1222 n_err_sighdl(_("Interrupt\n"));
1223 safe_signal(SIGPIPE, _oldpipe);
1224 reset(0);
1227 FL void
1228 announce(int printheaders)
1230 int vec[2], mdot;
1231 NYD_ENTER;
1233 mdot = newfileinfo();
1234 vec[0] = mdot;
1235 vec[1] = 0;
1236 dot = message + mdot - 1;
1237 if (printheaders && msgCount > 0 && ok_blook(header)) {
1238 ++_lex_inithdr;
1239 print_header_group(vec); /* XXX errors? */
1240 _lex_inithdr = 0;
1242 NYD_LEAVE;
1245 FL int
1246 newfileinfo(void)
1248 struct message *mp;
1249 int u, n, mdot, d, s, hidden, moved;
1250 NYD_ENTER;
1252 if (mb.mb_type == MB_VOID) {
1253 mdot = 1;
1254 goto jleave;
1257 mdot = getmdot(0);
1258 s = d = hidden = moved =0;
1259 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1260 if (mp->m_flag & MNEW)
1261 ++n;
1262 if ((mp->m_flag & MREAD) == 0)
1263 ++u;
1264 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1265 ++moved;
1266 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1267 ++d;
1268 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1269 ++s;
1270 if (mp->m_flag & MHIDDEN)
1271 ++hidden;
1274 /* If displayname gets truncated the user effectively has no option to see
1275 * the full pathname of the mailbox, so print it at least for '? fi' */
1276 printf(_("\"%s\": "),
1277 (_update_mailname(NULL) ? displayname : mailname));
1278 if (msgCount == 1)
1279 printf(_("1 message"));
1280 else
1281 printf(_("%d messages"), msgCount);
1282 if (n > 0)
1283 printf(_(" %d new"), n);
1284 if (u-n > 0)
1285 printf(_(" %d unread"), u);
1286 if (d > 0)
1287 printf(_(" %d deleted"), d);
1288 if (s > 0)
1289 printf(_(" %d saved"), s);
1290 if (moved > 0)
1291 printf(_(" %d moved"), moved);
1292 if (hidden > 0)
1293 printf(_(" %d hidden"), hidden);
1294 if (mb.mb_type == MB_CACHE)
1295 printf(" [Disconnected]");
1296 else if (mb.mb_perm == 0)
1297 printf(_(" [Read only]"));
1298 printf("\n");
1299 jleave:
1300 NYD_LEAVE;
1301 return mdot;
1304 FL int
1305 getmdot(int nmail)
1307 struct message *mp;
1308 char *cp;
1309 int mdot;
1310 enum mflag avoid = MHIDDEN | MDELETED;
1311 NYD_ENTER;
1313 if (!nmail) {
1314 if (ok_blook(autothread)) {
1315 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1316 c_thread(NULL);
1317 } else if ((cp = ok_vlook(autosort)) != NULL) {
1318 if (mb.mb_sorted != NULL)
1319 free(mb.mb_sorted);
1320 mb.mb_sorted = sstrdup(cp);
1321 c_sort(NULL);
1324 if (mb.mb_type == MB_VOID) {
1325 mdot = 1;
1326 goto jleave;
1329 if (nmail)
1330 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1331 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1332 break;
1334 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1335 if (mb.mb_threaded) {
1336 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1337 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1338 break;
1339 } else {
1340 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1341 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1342 break;
1346 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1347 if (mb.mb_threaded) {
1348 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1349 if (mp->m_flag & MFLAGGED)
1350 break;
1351 } else {
1352 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1353 if (mp->m_flag & MFLAGGED)
1354 break;
1358 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1359 if (mb.mb_threaded) {
1360 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1361 if (!(mp->m_flag & (MREAD | avoid)))
1362 break;
1363 } else {
1364 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1365 if (!(mp->m_flag & (MREAD | avoid)))
1366 break;
1370 if (nmail &&
1371 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1372 mdot = (int)PTR2SIZE(mp - message + 1);
1373 else if (ok_blook(showlast)) {
1374 if (mb.mb_threaded) {
1375 for (mp = this_in_thread(threadroot, -1); mp;
1376 mp = prev_in_thread(mp))
1377 if (!(mp->m_flag & avoid))
1378 break;
1379 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1380 } else {
1381 for (mp = message + msgCount - 1; mp >= message; --mp)
1382 if (!(mp->m_flag & avoid))
1383 break;
1384 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1386 } else if (!nmail &&
1387 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1388 mdot = (int)PTR2SIZE(mp - message + 1);
1389 else if (mb.mb_threaded) {
1390 for (mp = threadroot; mp; mp = next_in_thread(mp))
1391 if (!(mp->m_flag & avoid))
1392 break;
1393 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1394 } else {
1395 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1396 if (!(mp->m_flag & avoid))
1397 break;
1398 mdot = PTRCMP(mp, <, message + msgCount)
1399 ? (int)PTR2SIZE(mp - message + 1) : 1;
1401 jleave:
1402 NYD_LEAVE;
1403 return mdot;
1406 FL void
1407 initbox(char const *name)
1409 char *tempMesg;
1410 NYD_ENTER;
1412 if (mb.mb_type != MB_VOID)
1413 n_strlcpy(prevfile, mailname, PATH_MAX);
1415 /* TODO name always NE mailname (but goes away for objects anyway)
1416 * TODO Well, not true no more except that in parens */
1417 _update_mailname((name != mailname) ? name : NULL);
1419 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpbox", OF_WRONLY | OF_HOLDSIGS, 0600)) ==
1420 NULL) {
1421 n_perr(_("temporary mail message file"), 0);
1422 exit(EXIT_ERR);
1424 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1425 n_perr(_("temporary mail message file"), 0);
1426 exit(EXIT_ERR);
1428 Ftmp_release(&tempMesg);
1430 message_reset();
1431 mb.mb_active = MB_NONE;
1432 mb.mb_threaded = 0;
1433 if (mb.mb_sorted != NULL) {
1434 free(mb.mb_sorted);
1435 mb.mb_sorted = NULL;
1437 #ifdef HAVE_IMAP
1438 mb.mb_flags = MB_NOFLAGS;
1439 #endif
1440 dot = prevdot = threadroot = NULL;
1441 pstate &= ~PS_DID_PRINT_DOT;
1442 NYD_LEAVE;
1445 #ifdef HAVE_DOCSTRINGS
1446 FL bool_t
1447 print_comm_docstr(char const *comm)
1449 struct cmd_ghost const *cg;
1450 struct cmd const *cp;
1451 bool_t rv = FAL0;
1452 NYD_ENTER;
1454 /* Ghosts take precedence */
1455 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
1456 if (!strcmp(comm, cg->name)) {
1457 printf("%s -> ", comm);
1458 comm = cg->cmd.s;
1459 break;
1462 for (cp = _cmd_tab; cp->name != NULL; ++cp) {
1463 if (cp->func == &c_cmdnotsupp)
1464 continue;
1465 if (!strcmp(comm, cp->name))
1466 printf("%s: %s\n", comm, V_(cp->doc));
1467 else if (is_prefix(comm, cp->name))
1468 printf("%s (%s): %s\n", comm, cp->name, V_(cp->doc));
1469 else
1470 continue;
1471 rv = TRU1;
1472 break;
1475 if (!rv && cg != NULL) {
1476 printf("\"%s\"\n", comm);
1477 rv = TRU1;
1479 NYD_LEAVE;
1480 return rv;
1482 #endif
1484 /* s-it-mode */