Actually do support MIME for all custom headers
[s-mailx.git] / lex.c
blob40e568193db39b49c6c1e197711aa215101f5a17
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 #include <pwd.h>
44 struct cmd {
45 char const *name; /* Name of command */
46 int (*func)(void*); /* Implementor of command */
47 enum argtype argtype; /* Arglist type (see below) */
48 short msgflag; /* Required flags of msgs */
49 short msgmask; /* Relevant flags of msgs */
50 #ifdef HAVE_DOCSTRINGS
51 char const *doc; /* One line doc for command */
52 #endif
54 /* Yechh, can't initialize unions */
55 #define minargs msgflag /* Minimum argcount for RAWLIST */
56 #define maxargs msgmask /* Max argcount for RAWLIST */
58 struct cmd_ghost {
59 struct cmd_ghost *next;
60 struct str cmd; /* Data follows after .name */
61 char name[VFIELD_SIZE(0)];
64 static int *_msgvec;
65 static int _reset_on_stop; /* do a reset() if stopped */
66 static sighandler_type _oldpipe;
67 static struct cmd_ghost *_cmd_ghosts;
68 /* _cmd_tab[] after fun protos */
69 static int _lex_inithdr; /* am printing startup headers */
71 /* Update mailname (if name != NULL) and displayname, return wether displayname
72 * was large enough to swallow mailname */
73 static bool_t _update_mailname(char const *name);
74 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
75 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
76 #endif
78 /* Isolate the command from the arguments */
79 static char * _lex_isolate(char const *comm);
81 /* Get first-fit, or NULL */
82 static struct cmd const * _lex(char const *comm);
84 /* Command ghost handling */
85 static int _c_ghost(void *v);
86 static int _c_unghost(void *v);
88 /* Print a list of all commands */
89 static int _c_list(void *v);
91 static int __pcmd_cmp(void const *s1, void const *s2);
93 /* `quit' command */
94 static int _c_quit(void *v);
96 /* Print the binaries compiled-in features */
97 static int _c_features(void *v);
99 /* Print the binaries version number */
100 static int _c_version(void *v);
102 /* When we wake up after ^Z, reprint the prompt */
103 static void stop(int s);
105 /* Branch here on hangup signal and simulate "exit" */
106 static void hangup(int s);
108 /* List of all commands, and list of commands which are specially treated
109 * and deduced in execute(), but we need a list for _c_list() and
110 * print_comm_docstr() */
111 #ifdef HAVE_DOCSTRINGS
112 # define DS(S) , S
113 #else
114 # define DS(S)
115 #endif
116 static struct cmd const _cmd_tab[] = {
117 #include "cmd_tab.h"
119 _special_cmd_tab[] = {
120 { "#", NULL, 0, 0, 0
121 DS(N_("\"Comment command\": ignore remaining (continuable) line")) },
122 { "-", NULL, 0, 0, 0
123 DS(N_("Print out the preceding message")) }
125 #undef DS
127 #ifdef HAVE_C90AMEND1
128 SINLINE size_t
129 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
131 int err;
132 size_t i, ok;
133 NYD_ENTER;
135 for (err = ok = i = 0; cpl > maxl || err;) {
136 int ml = mblen(cp, cpl);
137 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
138 (void)mblen(NULL, 0);
139 err = 1;
140 ml = 1;
141 } else {
142 if (!err)
143 ok = i;
144 err = 0;
145 if (ml == 0)
146 break;
148 cp += ml;
149 i += ml;
150 cpl -= ml;
152 NYD_LEAVE;
153 return ok;
155 #endif /* HAVE_C90AMEND1 */
157 static bool_t
158 _update_mailname(char const *name)
160 char tbuf[PATH_MAX], *mailp, *dispp;
161 size_t i, j;
162 bool_t rv = TRU1;
163 NYD_ENTER;
165 /* Don't realpath(3) if it's only an update request */
166 if (name != NULL) {
167 #ifdef HAVE_REALPATH
168 enum protocol p = which_protocol(name);
169 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
170 if (realpath(name, mailname) == NULL) {
171 n_err(_("Can't canonicalize \"%s\"\n"), name);
172 rv = FAL0;
173 goto jdocopy;
175 } else
176 jdocopy:
177 #endif
178 n_strscpy(mailname, name, sizeof(mailname));
181 mailp = mailname;
182 dispp = displayname;
184 /* Don't display an absolute path but "+FOLDER" if under *folder* */
185 if (getfold(tbuf, sizeof tbuf)) {
186 i = strlen(tbuf);
187 if (i < sizeof(tbuf) -1)
188 tbuf[i++] = '/';
189 if (!strncmp(tbuf, mailp, i)) {
190 mailp += i;
191 *dispp++ = '+';
195 /* We want to see the name of the folder .. on the screen */
196 i = strlen(mailp);
197 if (i < sizeof(displayname) -1)
198 memcpy(dispp, mailp, i +1);
199 else {
200 rv = FAL0;
201 /* Avoid disrupting multibyte sequences (if possible) */
202 #ifndef HAVE_C90AMEND1
203 j = sizeof(displayname) / 3 - 1;
204 i -= sizeof(displayname) - (1/* + */ + 3) - j;
205 #else
206 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
207 i = j + __narrow_suffix(mailp + j, i - j,
208 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
209 #endif
210 snprintf(dispp, sizeof(displayname), "%.*s...%s",
211 (int)j, mailp, mailp + i);
213 NYD_LEAVE;
214 return rv;
217 static char *
218 _lex_isolate(char const *comm)
220 NYD_ENTER;
221 while (*comm != '\0' &&
222 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
223 ++comm;
224 NYD_LEAVE;
225 return UNCONST(comm);
228 static struct cmd const *
229 _lex(char const *comm) /* TODO **command hashtable**! linear list search!!! */
231 struct cmd const *cp, *cpmax;
232 NYD_ENTER;
234 for (cp = cpmax = _cmd_tab, cpmax += NELEM(_cmd_tab); cp != cpmax; ++cp)
235 if (*comm == *cp->name && is_prefix(comm, cp->name))
236 goto jleave;
237 cp = NULL;
238 jleave:
239 NYD_LEAVE;
240 return cp;
243 static int
244 _c_ghost(void *v)
246 char const **argv = v;
247 struct cmd_ghost *lcg, *cg;
248 size_t i, cl, nl;
249 char *cp;
250 NYD_ENTER;
252 /* Show the list? */
253 if (*argv == NULL) {
254 FILE *fp;
256 if ((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
257 fp = stdout;
258 for (i = 0, cg = _cmd_ghosts; cg != NULL; cg = cg->next)
259 fprintf(fp, "ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
260 if (fp != stdout) {
261 page_or_print(fp, i);
262 Fclose(fp);
264 goto jleave;
267 /* Verify the ghost name is a valid one */
268 if (*argv[0] == '\0' || *_lex_isolate(argv[0]) != '\0') {
269 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
270 v = NULL;
271 goto jleave;
274 /* Show command of single ghost? */
275 if (argv[1] == NULL) {
276 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
277 if (!strcmp(argv[0], cg->name)) {
278 printf("ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
279 goto jleave;
281 n_err(_("`ghost': no such alias: \"%s\"\n"), argv[0]);
282 v = NULL;
283 goto jleave;
286 /* Define command for ghost: verify command content */
287 for (cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
288 if (*cp != '\0')
289 cl += strlen(cp) + 1; /* SP or NUL */
290 if (cl == 0) {
291 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv[0]);
292 v = NULL;
293 goto jleave;
296 /* If the ghost already exists, recreate */
297 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
298 if (!strcmp(cg->name, argv[0])) {
299 if (lcg != NULL)
300 lcg->next = cg->next;
301 else
302 _cmd_ghosts = cg->next;
303 free(cg);
304 break;
307 nl = strlen(argv[0]) +1;
308 cg = smalloc(sizeof(*cg) - VFIELD_SIZEOF(struct cmd_ghost, name) + nl + cl);
309 cg->next = _cmd_ghosts;
310 _cmd_ghosts = cg;
311 memcpy(cg->name, argv[0], nl);
312 cp = cg->cmd.s = cg->name + nl;
313 cg->cmd.l = --cl;
314 while (*++argv != NULL) {
315 i = strlen(*argv);
316 if (i > 0) {
317 memcpy(cp, *argv, i);
318 cp += i;
319 *cp++ = ' ';
322 *--cp = '\0';
323 jleave:
324 NYD_LEAVE;
325 return (v == NULL);
328 static int
329 _c_unghost(void *v)
331 int rv = 0;
332 char const **argv = v, *cp;
333 struct cmd_ghost *lcg, *cg;
334 NYD_ENTER;
336 while ((cp = *argv++) != NULL) {
337 if (cp[0] == '*' && cp[1] == '\0') {
338 while ((cg = _cmd_ghosts) != NULL) {
339 _cmd_ghosts = cg->next;
340 free(cg);
342 } else {
343 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
344 if (!strcmp(cg->name, cp)) {
345 if (lcg != NULL)
346 lcg->next = cg->next;
347 else
348 _cmd_ghosts = cg->next;
349 free(cg);
350 goto jouter;
352 n_err(_("`unghost': no such alias: \"%s\"\n"), cp);
353 rv = 1;
354 jouter:
358 NYD_LEAVE;
359 return rv;
362 static int
363 __pcmd_cmp(void const *s1, void const *s2)
365 struct cmd const * const *c1 = s1, * const *c2 = s2;
366 int rv;
367 NYD_ENTER;
369 rv = strcmp((*c1)->name, (*c2)->name);
370 NYD_LEAVE;
371 return rv;
374 static int
375 _c_list(void *v)
377 struct cmd const **cpa, *cp, **cursor;
378 size_t i;
379 NYD_ENTER;
380 UNUSED(v);
382 i = NELEM(_cmd_tab) + NELEM(_special_cmd_tab) + 1;
383 cpa = ac_alloc(sizeof(cp) * i);
385 for (i = 0; i < NELEM(_cmd_tab); ++i)
386 cpa[i] = _cmd_tab + i;
388 size_t j;
390 for (j = 0; j < NELEM(_special_cmd_tab); ++i, ++j)
391 cpa[i] = _special_cmd_tab + j;
393 cpa[i] = NULL;
395 qsort(cpa, i, sizeof(cp), &__pcmd_cmp);
397 printf(_("Commands are:\n"));
398 for (i = 0, cursor = cpa; (cp = *cursor++) != NULL;) {
399 size_t j;
400 if (cp->func == &c_cmdnotsupp)
401 continue;
402 j = strlen(cp->name) + 2;
403 if ((i += j) > 72) {
404 i = j;
405 printf("\n");
407 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->name);
410 ac_free(cpa);
411 NYD_LEAVE;
412 return 0;
415 static int
416 _c_quit(void *v)
418 int rv;
419 NYD_ENTER;
420 UNUSED(v);
422 /* If we are PS_SOURCING, then return 1 so evaluate() can handle it.
423 * Otherwise return -1 to abort command loop */
424 rv = (pstate & PS_SOURCING) ? 1 : -1;
425 NYD_LEAVE;
426 return rv;
429 static int
430 _c_features(void *v)
432 NYD_ENTER;
433 UNUSED(v);
434 printf(_("Features: %s\n"), ok_vlook(features));
435 NYD_LEAVE;
436 return 0;
439 static int
440 _c_version(void *v)
442 NYD_ENTER;
443 UNUSED(v);
444 printf(_("Version %s\n"), ok_vlook(version));
445 NYD_LEAVE;
446 return 0;
449 static void
450 stop(int s)
452 sighandler_type old_action;
453 sigset_t nset;
454 NYD_X; /* Signal handler */
456 old_action = safe_signal(s, SIG_DFL);
458 sigemptyset(&nset);
459 sigaddset(&nset, s);
460 sigprocmask(SIG_UNBLOCK, &nset, NULL);
461 n_raise(s);
462 sigprocmask(SIG_BLOCK, &nset, NULL);
463 safe_signal(s, old_action);
464 if (_reset_on_stop) {
465 _reset_on_stop = 0;
466 reset(0);
470 static void
471 hangup(int s)
473 NYD_X; /* Signal handler */
474 UNUSED(s);
475 /* nothing to do? */
476 exit(EXIT_ERR);
479 FL int
480 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
482 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
483 * necessary to make a mailbox accessible by a different user, and if that
484 * has happened, let's just let the usual file perms decide */
485 static int shudclob;
487 struct stat stb;
488 size_t offset;
489 char const *who;
490 int rv, omsgCount = 0;
491 FILE *ibuf = NULL, *lckfp = NULL;
492 bool_t isdevnull = FAL0;
493 NYD_ENTER;
495 pstate &= ~PS_SETFILE_OPENED;
497 if (name[0] == '%' || ((who = shortcut_expand(name)) != NULL && *who == '%'))
498 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
500 who = (name[1] != '\0') ? name + 1 : myname;
502 if ((name = expand(name)) == NULL)
503 goto jem1;
505 switch (which_protocol(name)) {
506 case PROTO_FILE:
507 if (temporary_protocol_ext != NULL)
508 name = savecat(name, temporary_protocol_ext);
509 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
510 #ifdef HAVE_REALPATH
511 do { /* TODO we need objects, goes away then */
512 char ebuf[PATH_MAX];
513 if (realpath(name, ebuf) != NULL)
514 name = savestr(ebuf);
515 } while (0);
516 #endif
517 rv = 1;
518 break;
519 case PROTO_MAILDIR:
520 shudclob = 1;
521 rv = maildir_setfile(name, fm);
522 goto jleave;
523 #ifdef HAVE_POP3
524 case PROTO_POP3:
525 shudclob = 1;
526 rv = pop3_setfile(name, fm);
527 goto jleave;
528 #endif
529 default:
530 n_err(_("Cannot handle protocol: %s\n"), name);
531 goto jem1;
534 if ((ibuf = Zopen(name, "r")) == NULL) {
535 if ((fm & FEDIT_SYSBOX) && errno == ENOENT) {
536 if (strcmp(who, myname) && getpwnam(who) == NULL) {
537 n_err(_("\"%s\" is not a user of this system\n"), who);
538 goto jem2;
540 if (!(options & OPT_QUICKRUN_MASK) && ok_blook(bsdcompat))
541 n_err(_("No mail for %s\n"), who);
542 if (fm & FEDIT_NEWMAIL)
543 goto jleave;
544 if (ok_blook(emptystart)) {
545 if (!(options & OPT_QUICKRUN_MASK) && !ok_blook(bsdcompat))
546 n_perr(name, 0);
547 /* We must avoid returning -1 and causing program exit */
548 mb.mb_type = MB_VOID;
549 rv = 1;
550 goto jleave;
552 goto jem2;
553 } else if (fm & FEDIT_NEWMAIL)
554 goto jleave;
555 n_perr(name, 0);
556 goto jem1;
559 if (fstat(fileno(ibuf), &stb) == -1) {
560 if (fm & FEDIT_NEWMAIL)
561 goto jleave;
562 n_perr(_("fstat"), 0);
563 goto jem1;
566 if (S_ISREG(stb.st_mode) || isdevnull) {
567 /* EMPTY */
568 } else {
569 if (fm & FEDIT_NEWMAIL)
570 goto jleave;
571 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
572 n_perr(name, 0);
573 goto jem1;
576 /* Looks like all will be well. We must now relinquish our hold on the
577 * current set of stuff. Must hold signals while we are reading the new
578 * file, else we will ruin the message[] data structure */
580 hold_sigs(); /* TODO note on this one in quit.c:quit() */
581 if (shudclob && !(fm & FEDIT_NEWMAIL))
582 quit();
583 #ifdef HAVE_SOCKETS
584 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
585 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
586 #endif
588 /* TODO There is no intermediate VOID box we've switched to: name may
589 * TODO point to the same box that we just have written, so any updates
590 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
591 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
592 /* TODO We may uselessly open twice but quit() doesn't say wether we were
593 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
594 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
595 name = mailname;
596 Fclose(ibuf);
598 if ((ibuf = Zopen(name, "r")) == NULL ||
599 fstat(fileno(ibuf), &stb) == -1 ||
600 (!S_ISREG(stb.st_mode) && !isdevnull)) {
601 n_perr(name, 0);
602 rele_sigs();
603 goto jem2;
607 /* Copy the messages into /tmp and set pointers */
608 if (!(fm & FEDIT_NEWMAIL)) {
609 mb.mb_type = MB_FILE;
610 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
611 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
612 if (shudclob) {
613 if (mb.mb_itf) {
614 fclose(mb.mb_itf);
615 mb.mb_itf = NULL;
617 if (mb.mb_otf) {
618 fclose(mb.mb_otf);
619 mb.mb_otf = NULL;
622 shudclob = 1;
623 if (fm & FEDIT_SYSBOX)
624 pstate &= ~PS_EDIT;
625 else
626 pstate |= PS_EDIT;
627 initbox(name);
628 offset = 0;
629 } else {
630 fseek(mb.mb_otf, 0L, SEEK_END);
631 /* TODO Doing this without holding a lock is.. And not err checking.. */
632 fseek(ibuf, mailsize, SEEK_SET);
633 offset = mailsize;
634 omsgCount = msgCount;
637 if (isdevnull)
638 lckfp = (FILE*)-1;
639 else if (!(pstate & PS_EDIT))
640 lckfp = dot_lock(name, fileno(ibuf), FLT_READ, offset,0,
641 (fm & FEDIT_NEWMAIL ? 0 : 1));
642 else if (file_lock(fileno(ibuf), FLT_READ, offset,0,
643 (fm & FEDIT_NEWMAIL ? 0 : 1)))
644 lckfp = (FILE*)-1;
646 if (lckfp == NULL) {
647 if (!(fm & FEDIT_NEWMAIL)) {
648 char const *emsg = (pstate & PS_EDIT)
649 ? N_("Unable to lock mailbox, aborting operation")
650 : N_("Unable to (dot) lock mailbox, aborting operation");
651 n_perr(V_(emsg), 0);
653 rele_sigs();
654 if (!(fm & FEDIT_NEWMAIL))
655 goto jem1;
656 goto jleave;
659 mailsize = fsize(ibuf);
661 /* TODO This is too simple minded? We should regenerate an index file
662 * TODO to be able to truly tell wether *anything* has changed! */
663 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
664 rele_sigs();
665 goto jleave;
667 setptr(ibuf, offset);
668 setmsize(msgCount);
669 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
670 mb.mb_threaded = 0;
671 c_sort((void*)-1);
674 Fclose(ibuf);
675 ibuf = NULL;
676 if (lckfp != NULL && lckfp != (FILE*)-1) {
677 Pclose(lckfp, FAL0);
678 /*lckfp = NULL;*/
680 rele_sigs();
682 if (!(fm & FEDIT_NEWMAIL)) {
683 pstate &= ~PS_SAW_COMMAND;
684 pstate |= PS_SETFILE_OPENED;
687 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
688 rv = (msgCount == 0);
689 goto jleave;
692 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
693 if (!(fm & FEDIT_NEWMAIL)) {
694 if (!ok_blook(emptystart))
695 n_err(_("No mail for %s\n"), who);
697 goto jleave;
700 if (fm & FEDIT_NEWMAIL)
701 newmailinfo(omsgCount);
703 rv = 0;
704 jleave:
705 if (ibuf != NULL) {
706 Fclose(ibuf);
707 if (lckfp != NULL && lckfp != (FILE*)-1)
708 Pclose(lckfp, FAL0);
710 NYD_LEAVE;
711 return rv;
712 jem2:
713 mb.mb_type = MB_VOID;
714 jem1:
715 rv = -1;
716 goto jleave;
719 FL int
720 newmailinfo(int omsgCount)
722 int mdot, i;
723 NYD_ENTER;
725 for (i = 0; i < omsgCount; ++i)
726 message[i].m_flag &= ~MNEWEST;
728 if (msgCount > omsgCount) {
729 for (i = omsgCount; i < msgCount; ++i)
730 message[i].m_flag |= MNEWEST;
731 printf(_("New mail has arrived.\n"));
732 if ((i = msgCount - omsgCount) == 1)
733 printf(_("Loaded 1 new message.\n"));
734 else
735 printf(_("Loaded %d new messages.\n"), i);
736 } else
737 printf(_("Loaded %d messages.\n"), msgCount);
739 check_folder_hook(TRU1);
741 mdot = getmdot(1);
743 if (ok_blook(header))
744 print_headers(omsgCount + 1, msgCount, FAL0);
745 NYD_LEAVE;
746 return mdot;
749 FL bool_t
750 commands(void)
752 struct eval_ctx ev;
753 int n;
754 bool_t volatile rv = TRU1;
755 NYD_ENTER;
757 if (!(pstate & PS_SOURCING)) {
758 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
759 safe_signal(SIGINT, onintr);
760 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
761 safe_signal(SIGHUP, hangup);
762 /* TODO We do a lot of redundant signal handling, especially
763 * TODO with the command line editor(s); try to merge this */
764 safe_signal(SIGTSTP, stop);
765 safe_signal(SIGTTOU, stop);
766 safe_signal(SIGTTIN, stop);
768 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
769 safe_signal(SIGPIPE, _oldpipe);
771 memset(&ev, 0, sizeof ev);
773 setexit();
774 for (;;) {
775 char *temporary_orig_line; /* XXX eval_ctx.ev_line not yet constant */
777 n_COLOUR( n_colour_env_pop(TRU1); )
779 /* TODO Unless we have our signal manager (or however we do it) child
780 * TODO processes may have time slots where their execution isn't
781 * TODO protected by signal handlers (in between start and setup
782 * TODO completed). close_all_files() is only called from onintr()
783 * TODO so those may linger possibly forever */
784 if (!(pstate & PS_SOURCING))
785 close_all_files();
787 interrupts = 0;
788 handlerstacktop = NULL;
790 if (temporary_localopts_store != NULL) /* XXX intermediate hack */
791 temporary_localopts_free(); /* XXX intermediate hack */
792 sreset((pstate & PS_SOURCING) != 0);
793 if (!(pstate & PS_SOURCING)) {
794 char *cp;
796 /* TODO Note: this buffer may contain a password. We should redefine
797 * TODO the code flow which has to do that */
798 if ((cp = termios_state.ts_linebuf) != NULL) {
799 termios_state.ts_linebuf = NULL;
800 termios_state.ts_linesize = 0;
801 free(cp); /* TODO pool give-back */
803 /* TODO Due to expand-on-tab of NCL the buffer may grow */
804 if (ev.ev_line.l > LINESIZE * 3) {
805 free(ev.ev_line.s); /* TODO pool! but what? */
806 ev.ev_line.s = NULL;
807 ev.ev_line.l = ev.ev_line_size = 0;
811 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
812 char *cp;
814 cp = ok_vlook(newmail);
815 if ((options & OPT_TTYIN) && cp != NULL) {
816 struct stat st;
818 n = (cp != NULL && strcmp(cp, "nopoll"));
819 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
820 st.st_size > mailsize) ||
821 (mb.mb_type == MB_MAILDIR && n != 0)) {
822 size_t odot = PTR2SIZE(dot - message);
823 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
825 if (setfile(mailname,
826 FEDIT_NEWMAIL |
827 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
828 exit_status |= EXIT_ERR;
829 rv = FAL0;
830 break;
832 dot = message + odot;
833 pstate |= odid;
837 _reset_on_stop = 1;
838 exit_status = EXIT_OK;
841 /* Read a line of commands and handle end of file specially */
842 jreadline:
843 ev.ev_line.l = ev.ev_line_size;
844 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
845 ev.ev_new_content);
846 ev.ev_line_size = (ui32_t)ev.ev_line.l;
847 ev.ev_line.l = (ui32_t)n;
848 _reset_on_stop = 0;
849 if (n < 0) {
850 /* EOF */
851 if (pstate & PS_LOADING)
852 break;
853 if (pstate & PS_SOURCING) {
854 unstack();
855 continue;
857 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
858 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
859 continue;
861 break;
864 temporary_orig_line = ((pstate & PS_SOURCING) ||
865 !(options & OPT_INTERACTIVE)) ? NULL
866 : savestrbuf(ev.ev_line.s, ev.ev_line.l);
867 pstate &= ~PS_HOOK_MASK;
868 if (evaluate(&ev)) {
869 if (pstate & PS_LOADING) /* TODO mess; join PS_EVAL_ERROR.. */
870 rv = FAL0;
871 break;
873 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
874 if (exit_status != EXIT_OK)
875 break;
876 if ((pstate & (PS_IN_LOAD | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
877 exit_status = EXIT_ERR;
878 break;
881 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
882 if (ev.ev_new_content != NULL)
883 goto jreadline;
884 /* That *can* happen since evaluate() unstack()s on error! */
885 if (temporary_orig_line != NULL)
886 n_tty_addhist(temporary_orig_line, !ev.ev_add_history);
890 if (ev.ev_line.s != NULL)
891 free(ev.ev_line.s);
892 if (pstate & PS_SOURCING)
893 sreset(FAL0);
894 NYD_LEAVE;
895 return rv;
898 FL int
899 execute(char *linebuf, size_t linesize) /* XXX LEGACY */
901 struct eval_ctx ev;
902 int rv;
903 NYD_ENTER;
905 memset(&ev, 0, sizeof ev);
906 ev.ev_line.s = linebuf;
907 ev.ev_line.l = linesize;
908 ev.ev_is_recursive = TRU1;
909 n_COLOUR( n_colour_env_push(); )
910 rv = evaluate(&ev);
911 n_COLOUR( n_colour_env_pop(FAL0); )
913 NYD_LEAVE;
914 return rv;
917 FL int
918 evaluate(struct eval_ctx *evp)
920 struct str line;
921 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
922 struct cmd_ghost *cg = NULL;
923 struct cmd const *com = NULL;
924 int muvec[2], c, e = 1;
925 NYD_ENTER;
927 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
928 evp->ev_add_history = FAL0;
929 evp->ev_new_content = NULL;
931 /* Command ghosts that refer to shell commands or macro expansion restart */
932 jrestart:
934 /* Strip the white space away from the beginning of the command */
935 for (cp = line.s; whitechar(*cp); ++cp)
937 line.l -= PTR2SIZE(cp - line.s);
939 /* Ignore comments */
940 if (*cp == '#')
941 goto jleave0;
943 /* Handle ! differently to get the correct lexical conventions */
944 if (*cp == '!') {
945 if (pstate & PS_SOURCING) {
946 n_err(_("Can't `!' while `source'ing\n"));
947 goto jleave;
949 c_shell(++cp);
950 evp->ev_add_history = TRU1;
951 goto jleave0;
954 /* Isolate the actual command; since it may not necessarily be
955 * separated from the arguments (as in `p1') we need to duplicate it to
956 * be able to create a NUL terminated version.
957 * We must be aware of several special one letter commands here */
958 arglist[0] = cp;
959 if ((cp = _lex_isolate(cp)) == arglist[0] &&
960 (*cp == '|' || *cp == '~' || *cp == '?'))
961 ++cp;
962 c = (int)PTR2SIZE(cp - arglist[0]);
963 line.l -= c;
964 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
965 memcpy(word, arglist[0], c);
966 word[c] = '\0';
968 /* Look up the command; if not found, bitch.
969 * Normally, a blank command would map to the first command in the
970 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
971 * confusion; act just the same for ghosts */
972 if (*word == '\0') {
973 if ((pstate & PS_SOURCING) || cg != NULL)
974 goto jleave0;
975 com = _cmd_tab + 0;
976 goto jexec;
979 /* If this is the first evaluation, check command ghosts */
980 if (cg == NULL) {
981 /* TODO relink list head, so it's sorted on usage over time?
982 * TODO in fact, there should be one hashmap over all commands and ghosts
983 * TODO so that the lookup could be made much more efficient than it is
984 * TODO now (two adjacent list searches! */
985 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
986 if (!strcmp(word, cg->name)) {
987 if (line.l > 0) {
988 size_t i = cg->cmd.l;
989 line.s = salloc(i + line.l +1);
990 memcpy(line.s, cg->cmd.s, i);
991 memcpy(line.s + i, cp, line.l);
992 line.s[i += line.l] = '\0';
993 line.l = i;
994 } else {
995 line.s = cg->cmd.s;
996 line.l = cg->cmd.l;
998 goto jrestart;
1002 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
1003 bool_t s = condstack_isskip();
1004 if (!s || (options & OPT_D_V))
1005 n_err(_("Unknown command%s: `%s'\n"),
1006 (s ? _(" (conditionally ignored)") : ""), word);
1007 if (s)
1008 goto jleave0;
1009 if (com != NULL) {
1010 c_cmdnotsupp(NULL);
1011 com = NULL;
1013 goto jleave;
1016 /* See if we should execute the command -- if a conditional we always
1017 * execute it, otherwise, check the state of cond */
1018 jexec:
1019 if (!(com->argtype & ARG_F) && condstack_isskip())
1020 goto jleave0;
1022 /* Process the arguments to the command, depending on the type it expects,
1023 * default to error. If we're PS_SOURCING an interactive command: error */
1024 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
1025 n_err(_("May not execute `%s' while sending\n"), com->name);
1026 goto jleave;
1028 if ((pstate & PS_SOURCING) && (com->argtype & ARG_I)) {
1029 n_err(_("May not execute `%s' while `source'ing\n"), com->name);
1030 goto jleave;
1032 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
1033 n_err(_("May not execute `%s' -- message file is read only\n"),
1034 com->name);
1035 goto jleave;
1037 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
1038 n_err(_("Cannot recursively invoke `%s'\n"), com->name);
1039 goto jleave;
1041 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
1042 n_err(_("Cannot execute `%s' without active mailbox\n"), com->name);
1043 goto jleave;
1045 if (com->argtype & ARG_O)
1046 OBSOLETE2(_("this command will be removed"), com->name);
1048 if (com->argtype & ARG_V)
1049 temporary_arg_v_store = NULL;
1051 switch (com->argtype & ARG_ARGMASK) {
1052 case ARG_MSGLIST:
1053 /* Message list defaulting to nearest forward legal message */
1054 if (_msgvec == NULL)
1055 goto je96;
1056 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1057 break;
1058 if (c == 0) {
1059 *_msgvec = first(com->msgflag, com->msgmask);
1060 if (*_msgvec != 0)
1061 _msgvec[1] = 0;
1063 if (*_msgvec == 0) {
1064 if (!(pstate & PS_HOOK_MASK))
1065 printf(_("No applicable messages\n"));
1066 break;
1068 e = (*com->func)(_msgvec);
1069 break;
1071 case ARG_NDMLIST:
1072 /* Message list with no defaults, but no error if none exist */
1073 if (_msgvec == NULL) {
1074 je96:
1075 n_err(_("Invalid use of \"message list\"\n"));
1076 break;
1078 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1079 break;
1080 e = (*com->func)(_msgvec);
1081 break;
1083 case ARG_STRLIST:
1084 /* Just the straight string, with leading blanks removed */
1085 while (whitechar(*cp))
1086 ++cp;
1087 e = (*com->func)(cp);
1088 break;
1090 case ARG_RAWLIST:
1091 case ARG_ECHOLIST:
1092 /* A vector of strings, in shell style */
1093 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
1094 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
1095 break;
1096 if (c < com->minargs) {
1097 n_err(_("`%s' requires at least %d arg(s)\n"),
1098 com->name, com->minargs);
1099 break;
1101 if (c > com->maxargs) {
1102 n_err(_("`%s' takes no more than %d arg(s)\n"),
1103 com->name, com->maxargs);
1104 break;
1106 e = (*com->func)(arglist);
1107 break;
1109 case ARG_NOLIST:
1110 /* Just the constant zero, for exiting, eg. */
1111 e = (*com->func)(0);
1112 break;
1114 default:
1115 n_panic(_("Unknown argument type"));
1118 if (e == 0 && (com->argtype & ARG_V) &&
1119 (cp = temporary_arg_v_store) != NULL) {
1120 temporary_arg_v_store = NULL;
1121 evp->ev_new_content = cp;
1122 goto jleave0;
1124 if (!(com->argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
1125 evp->ev_add_history = TRU1;
1127 jleave:
1128 /* Exit the current source file on error TODO what a mess! */
1129 if (e == 0)
1130 pstate &= ~PS_EVAL_ERROR;
1131 else {
1132 pstate |= PS_EVAL_ERROR;
1133 if (e < 0 || (pstate & PS_LOADING)) {
1134 e = 1;
1135 goto jret;
1137 if (pstate & PS_SOURCING)
1138 unstack();
1139 goto jret0;
1141 if (com == NULL)
1142 goto jret0;
1143 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1144 if (visible(dot)) {
1145 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1146 muvec[1] = 0;
1147 c_type(muvec); /* TODO what if error? re-eval! */
1149 if (!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(com->argtype & ARG_T))
1150 pstate |= PS_SAW_COMMAND;
1151 jleave0:
1152 pstate &= ~PS_EVAL_ERROR;
1153 jret0:
1154 e = 0;
1155 jret:
1156 NYD_LEAVE;
1157 return e;
1160 FL void
1161 setmsize(int sz)
1163 NYD_ENTER;
1164 if (_msgvec != NULL)
1165 free(_msgvec);
1166 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1167 NYD_LEAVE;
1170 FL void
1171 print_header_summary(char const *Larg)
1173 size_t bot, top, i, j;
1174 NYD_ENTER;
1176 if (Larg != NULL) {
1177 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1178 if ((options & (OPT_VERB | OPT_EXISTONLY)) == OPT_EXISTONLY) {
1179 freopen("/dev/null", "w", stdout);
1180 freopen("/dev/null", "w", stderr);
1182 assert(_msgvec != NULL);
1183 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), _msgvec, 0) <= 0);
1184 if (options & OPT_EXISTONLY) {
1185 exit_status = (int)i;
1186 goto jleave;
1188 if (i)
1189 goto jleave;
1190 for (bot = msgCount, top = 0, i = 0; (j = _msgvec[i]) != 0; ++i) {
1191 if (bot > j)
1192 bot = j;
1193 if (top < j)
1194 top = j;
1196 } else
1197 bot = 1, top = msgCount;
1198 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
1199 jleave:
1200 NYD_LEAVE;
1203 FL void
1204 onintr(int s)
1206 NYD_X; /* Signal handler */
1208 if (handlerstacktop != NULL) {
1209 handlerstacktop(s);
1210 return;
1212 safe_signal(SIGINT, onintr);
1213 noreset = 0;
1214 if (!_lex_inithdr)
1215 pstate |= PS_SAW_COMMAND;
1216 _lex_inithdr = 0;
1217 while (pstate & PS_SOURCING)
1218 unstack();
1220 termios_state_reset();
1221 close_all_files();
1223 if (image >= 0) {
1224 close(image);
1225 image = -1;
1227 if (interrupts != 1)
1228 n_err_sighdl(_("Interrupt\n"));
1229 safe_signal(SIGPIPE, _oldpipe);
1230 reset(0);
1233 FL void
1234 announce(int printheaders)
1236 int vec[2], mdot;
1237 NYD_ENTER;
1239 mdot = newfileinfo();
1240 vec[0] = mdot;
1241 vec[1] = 0;
1242 dot = message + mdot - 1;
1243 if (printheaders && msgCount > 0 && ok_blook(header)) {
1244 ++_lex_inithdr;
1245 print_header_group(vec); /* XXX errors? */
1246 _lex_inithdr = 0;
1248 NYD_LEAVE;
1251 FL int
1252 newfileinfo(void)
1254 struct message *mp;
1255 int u, n, mdot, d, s, hidden, moved;
1256 NYD_ENTER;
1258 if (mb.mb_type == MB_VOID) {
1259 mdot = 1;
1260 goto jleave;
1263 mdot = getmdot(0);
1264 s = d = hidden = moved =0;
1265 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1266 if (mp->m_flag & MNEW)
1267 ++n;
1268 if ((mp->m_flag & MREAD) == 0)
1269 ++u;
1270 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1271 ++moved;
1272 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1273 ++d;
1274 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1275 ++s;
1276 if (mp->m_flag & MHIDDEN)
1277 ++hidden;
1280 /* If displayname gets truncated the user effectively has no option to see
1281 * the full pathname of the mailbox, so print it at least for '? fi' */
1282 printf(_("\"%s\": "),
1283 (_update_mailname(NULL) ? displayname : mailname));
1284 if (msgCount == 1)
1285 printf(_("1 message"));
1286 else
1287 printf(_("%d messages"), msgCount);
1288 if (n > 0)
1289 printf(_(" %d new"), n);
1290 if (u-n > 0)
1291 printf(_(" %d unread"), u);
1292 if (d > 0)
1293 printf(_(" %d deleted"), d);
1294 if (s > 0)
1295 printf(_(" %d saved"), s);
1296 if (moved > 0)
1297 printf(_(" %d moved"), moved);
1298 if (hidden > 0)
1299 printf(_(" %d hidden"), hidden);
1300 else if (mb.mb_perm == 0)
1301 printf(_(" [Read only]"));
1302 printf("\n");
1303 jleave:
1304 NYD_LEAVE;
1305 return mdot;
1308 FL int
1309 getmdot(int nmail)
1311 struct message *mp;
1312 char *cp;
1313 int mdot;
1314 enum mflag avoid = MHIDDEN | MDELETED;
1315 NYD_ENTER;
1317 if (!nmail) {
1318 if (ok_blook(autothread)) {
1319 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1320 c_thread(NULL);
1321 } else if ((cp = ok_vlook(autosort)) != NULL) {
1322 if (mb.mb_sorted != NULL)
1323 free(mb.mb_sorted);
1324 mb.mb_sorted = sstrdup(cp);
1325 c_sort(NULL);
1328 if (mb.mb_type == MB_VOID) {
1329 mdot = 1;
1330 goto jleave;
1333 if (nmail)
1334 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1335 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1336 break;
1338 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1339 if (mb.mb_threaded) {
1340 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1341 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1342 break;
1343 } else {
1344 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1345 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1346 break;
1350 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1351 if (mb.mb_threaded) {
1352 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1353 if (mp->m_flag & MFLAGGED)
1354 break;
1355 } else {
1356 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1357 if (mp->m_flag & MFLAGGED)
1358 break;
1362 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1363 if (mb.mb_threaded) {
1364 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1365 if (!(mp->m_flag & (MREAD | avoid)))
1366 break;
1367 } else {
1368 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1369 if (!(mp->m_flag & (MREAD | avoid)))
1370 break;
1374 if (nmail &&
1375 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1376 mdot = (int)PTR2SIZE(mp - message + 1);
1377 else if (ok_blook(showlast)) {
1378 if (mb.mb_threaded) {
1379 for (mp = this_in_thread(threadroot, -1); mp;
1380 mp = prev_in_thread(mp))
1381 if (!(mp->m_flag & avoid))
1382 break;
1383 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1384 } else {
1385 for (mp = message + msgCount - 1; mp >= message; --mp)
1386 if (!(mp->m_flag & avoid))
1387 break;
1388 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1390 } else if (!nmail &&
1391 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1392 mdot = (int)PTR2SIZE(mp - message + 1);
1393 else if (mb.mb_threaded) {
1394 for (mp = threadroot; mp; mp = next_in_thread(mp))
1395 if (!(mp->m_flag & avoid))
1396 break;
1397 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1398 } else {
1399 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1400 if (!(mp->m_flag & avoid))
1401 break;
1402 mdot = PTRCMP(mp, <, message + msgCount)
1403 ? (int)PTR2SIZE(mp - message + 1) : 1;
1405 jleave:
1406 NYD_LEAVE;
1407 return mdot;
1410 FL void
1411 initbox(char const *name)
1413 char *tempMesg;
1414 NYD_ENTER;
1416 if (mb.mb_type != MB_VOID)
1417 n_strscpy(prevfile, mailname, PATH_MAX);
1419 /* TODO name always NE mailname (but goes away for objects anyway)
1420 * TODO Well, not true no more except that in parens */
1421 _update_mailname((name != mailname) ? name : NULL);
1423 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
1424 NULL) {
1425 n_perr(_("temporary mail message file"), 0);
1426 exit(EXIT_ERR);
1428 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1429 n_perr(_("temporary mail message file"), 0);
1430 exit(EXIT_ERR);
1432 Ftmp_release(&tempMesg);
1434 message_reset();
1435 mb.mb_threaded = 0;
1436 if (mb.mb_sorted != NULL) {
1437 free(mb.mb_sorted);
1438 mb.mb_sorted = NULL;
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, *cpmax;
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 cpmax = (cp = _cmd_tab) + NELEM(_cmd_tab);
1463 jredo:
1464 for (; cp < cpmax; ++cp) {
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;
1474 if (!rv && cpmax == _cmd_tab + NELEM(_cmd_tab)) {
1475 cpmax = (cp = _special_cmd_tab) + NELEM(_special_cmd_tab);
1476 goto jredo;
1479 if (!rv && cg != NULL) {
1480 printf("\"%s\"\n", comm);
1481 rv = TRU1;
1483 NYD_LEAVE;
1484 return rv;
1486 #endif
1488 /* s-it-mode */