Fix false resource release <-> double free (Bob Tennent)..
[s-mailx.git] / lex.c
blobb460a7bd0cb2ad202a6a47ca1398da8c46ec9a8c
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
39 #undef n_FILE
40 #define n_FILE lex
42 #ifndef HAVE_AMALGAMATION
43 # include "nail.h"
44 #endif
46 struct cmd {
47 char const *name; /* Name of command */
48 int (*func)(void*); /* Implementor of command */
49 enum argtype argtype; /* Arglist type (see below) */
50 short msgflag; /* Required flags of msgs */
51 short msgmask; /* Relevant flags of msgs */
52 #ifdef HAVE_DOCSTRINGS
53 char const *doc; /* One line doc for command */
54 #endif
56 /* Yechh, can't initialize unions */
57 #define minargs msgflag /* Minimum argcount for RAWLIST */
58 #define maxargs msgmask /* Max argcount for RAWLIST */
60 struct cmd_ghost {
61 struct cmd_ghost *next;
62 struct str cmd; /* Data follows after .name */
63 char name[VFIELD_SIZE(0)];
66 static int *_msgvec;
67 static int _reset_on_stop; /* do a reset() if stopped */
68 static sighandler_type _oldpipe;
69 static struct cmd_ghost *_cmd_ghosts;
70 /* _cmd_tab[] after fun protos */
71 static int _lex_inithdr; /* am printing startup headers */
73 /* Update mailname (if name != NULL) and displayname, return wether displayname
74 * was large enough to swallow mailname */
75 static bool_t _update_mailname(char const *name);
76 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
77 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
78 #endif
80 /* Isolate the command from the arguments */
81 static char * _lex_isolate(char const *comm);
83 /* Get first-fit, or NULL */
84 static struct cmd const * _lex(char const *comm);
86 /* Command ghost handling */
87 static int _c_ghost(void *v);
88 static int _c_unghost(void *v);
90 /* Print a list of all commands */
91 static int _c_pcmdlist(void *v);
93 static int __pcmd_cmp(void const *s1, void const *s2);
95 /* `quit' command */
96 static int _c_quit(void *v);
98 /* Print the binaries compiled-in features */
99 static int _c_features(void *v);
101 /* Print the binaries version number */
102 static int _c_version(void *v);
104 /* When we wake up after ^Z, reprint the prompt */
105 static void stop(int s);
107 /* Branch here on hangup signal and simulate "exit" */
108 static void hangup(int s);
110 /* List of all commands */
111 static struct cmd const _cmd_tab[] = {
112 #include "cmd_tab.h"
115 #ifdef HAVE_C90AMEND1
116 SINLINE size_t
117 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
119 int err;
120 size_t i, ok;
121 NYD_ENTER;
123 for (err = ok = i = 0; cpl > maxl || err;) {
124 int ml = mblen(cp, cpl);
125 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
126 (void)mblen(NULL, 0);
127 err = 1;
128 ml = 1;
129 } else {
130 if (!err)
131 ok = i;
132 err = 0;
133 if (ml == 0)
134 break;
136 cp += ml;
137 i += ml;
138 cpl -= ml;
140 NYD_LEAVE;
141 return ok;
143 #endif /* HAVE_C90AMEND1 */
145 static bool_t
146 _update_mailname(char const *name)
148 char tbuf[PATH_MAX], *mailp, *dispp;
149 size_t i, j;
150 bool_t rv = TRU1;
151 NYD_ENTER;
153 /* Don't realpath(3) if it's only an update request */
154 if (name != NULL) {
155 #ifdef HAVE_REALPATH
156 enum protocol p = which_protocol(name);
157 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
158 if (realpath(name, mailname) == NULL) {
159 fprintf(stderr, _("Can't canonicalize \"%s\"\n"), name);
160 rv = FAL0;
161 goto jdocopy;
163 } else
164 jdocopy:
165 #endif
166 n_strlcpy(mailname, name, sizeof(mailname));
169 mailp = mailname;
170 dispp = displayname;
172 /* Don't display an absolute path but "+FOLDER" if under *folder* */
173 if (getfold(tbuf, sizeof tbuf)) {
174 i = strlen(tbuf);
175 if (i < sizeof(tbuf) -1)
176 tbuf[i++] = '/';
177 if (!strncmp(tbuf, mailp, i)) {
178 mailp += i;
179 *dispp++ = '+';
183 /* We want to see the name of the folder .. on the screen */
184 i = strlen(mailp);
185 if (i < sizeof(displayname) -1)
186 memcpy(dispp, mailp, i +1);
187 else {
188 rv = FAL0;
189 /* Avoid disrupting multibyte sequences (if possible) */
190 #ifndef HAVE_C90AMEND1
191 j = sizeof(displayname) / 3 - 1;
192 i -= sizeof(displayname) - (1/* + */ + 3) - j;
193 #else
194 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
195 i = j + __narrow_suffix(mailp + j, i - j,
196 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
197 #endif
198 snprintf(dispp, sizeof(displayname), "%.*s...%s",
199 (int)j, mailp, mailp + i);
201 NYD_LEAVE;
202 return rv;
205 static char *
206 _lex_isolate(char const *comm)
208 NYD_ENTER;
209 while (*comm != '\0' &&
210 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm) == NULL)
211 ++comm;
212 NYD_LEAVE;
213 return UNCONST(comm);
216 static struct cmd const *
217 _lex(char const *comm) /* TODO **command hashtable**! linear list search!!! */
219 struct cmd const *cp;
220 NYD_ENTER;
222 for (cp = _cmd_tab; cp->name != NULL; ++cp)
223 if (*comm == *cp->name && is_prefix(comm, cp->name))
224 goto jleave;
225 cp = NULL;
226 jleave:
227 NYD_LEAVE;
228 return cp;
231 static int
232 _c_ghost(void *v)
234 char const **argv = v;
235 struct cmd_ghost *lcg, *cg;
236 size_t i, cl, nl;
237 char *cp;
238 NYD_ENTER;
240 /* Show the list? */
241 if (*argv == NULL) {
242 FILE *fp;
244 if ((fp = Ftmp(NULL, "ghost", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
245 NULL)
246 fp = stdout;
247 for (i = 0, cg = _cmd_ghosts; cg != NULL; cg = cg->next)
248 fprintf(fp, "ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
249 if (fp != stdout) {
250 page_or_print(fp, i);
251 Fclose(fp);
253 goto jleave;
256 /* Verify the ghost name is a valid one */
257 if (*argv[0] == '\0' || *_lex_isolate(argv[0]) != '\0') {
258 fprintf(stderr, _("`ghost': can't canonicalize \"%s\"\n"), argv[0]);
259 v = NULL;
260 goto jleave;
263 /* Show command of single ghost? */
264 if (argv[1] == NULL) {
265 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
266 if (!strcmp(argv[0], cg->name)) {
267 printf("ghost %s \"%s\"\n", cg->name, string_quote(cg->cmd.s));
268 goto jleave;
270 fprintf(stderr, _("`ghost': no such alias: \"%s\"\n"), argv[0]);
271 v = NULL;
272 goto jleave;
275 /* Define command for ghost: verify command content */
276 for (cl = 0, i = 1; (cp = UNCONST(argv[i])) != NULL; ++i)
277 if (*cp != '\0')
278 cl += strlen(cp) + 1; /* SP or NUL */
279 if (cl == 0) {
280 fprintf(stderr, _("`ghost': empty command arguments after \"%s\"\n"),
281 argv[0]);
282 v = NULL;
283 goto jleave;
286 /* If the ghost already exists, recreate */
287 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
288 if (!strcmp(cg->name, argv[0])) {
289 if (lcg != NULL)
290 lcg->next = cg->next;
291 else
292 _cmd_ghosts = cg->next;
293 free(cg);
294 break;
297 nl = strlen(argv[0]) +1;
298 cg = smalloc(sizeof(*cg) - VFIELD_SIZEOF(struct cmd_ghost, name) + nl + cl);
299 cg->next = _cmd_ghosts;
300 _cmd_ghosts = cg;
301 memcpy(cg->name, argv[0], nl);
302 cp = cg->cmd.s = cg->name + nl;
303 cg->cmd.l = --cl;
304 while (*++argv != NULL) {
305 i = strlen(*argv);
306 if (i > 0) {
307 memcpy(cp, *argv, i);
308 cp += i;
309 *cp++ = ' ';
312 *--cp = '\0';
313 jleave:
314 NYD_LEAVE;
315 return (v == NULL);
318 static int
319 _c_unghost(void *v)
321 int rv = 0;
322 char const **argv = v, *cp;
323 struct cmd_ghost *lcg, *cg;
324 NYD_ENTER;
326 while ((cp = *argv++) != NULL) {
327 if (cp[0] == '*' && cp[1] == '\0') {
328 while ((cg = _cmd_ghosts) != NULL) {
329 _cmd_ghosts = cg->next;
330 free(cg);
332 } else {
333 for (lcg = NULL, cg = _cmd_ghosts; cg != NULL; lcg = cg, cg = cg->next)
334 if (!strcmp(cg->name, cp)) {
335 if (lcg != NULL)
336 lcg->next = cg->next;
337 else
338 _cmd_ghosts = cg->next;
339 free(cg);
340 goto jouter;
342 fprintf(stderr, _("`unghost': no such alias: \"%s\"\n"), cp);
343 rv = 1;
344 jouter:
348 NYD_LEAVE;
349 return rv;
352 static int
353 __pcmd_cmp(void const *s1, void const *s2)
355 struct cmd const * const *c1 = s1, * const *c2 = s2;
356 int rv;
357 NYD_ENTER;
359 rv = strcmp((*c1)->name, (*c2)->name);
360 NYD_LEAVE;
361 return rv;
364 static int
365 _c_pcmdlist(void *v)
367 struct cmd const **cpa, *cp, **cursor;
368 size_t i;
369 NYD_ENTER;
370 UNUSED(v);
372 for (i = 0; _cmd_tab[i].name != NULL; ++i)
374 ++i;
375 cpa = ac_alloc(sizeof(cp) * i);
377 for (i = 0; (cp = _cmd_tab + i)->name != NULL; ++i)
378 cpa[i] = cp;
379 cpa[i] = NULL;
381 qsort(cpa, i, sizeof(cp), &__pcmd_cmp);
383 printf(_("Commands are:\n"));
384 for (i = 0, cursor = cpa; (cp = *cursor++) != NULL;) {
385 size_t j;
386 if (cp->func == &c_cmdnotsupp)
387 continue;
388 j = strlen(cp->name) + 2;
389 if ((i += j) > 72) {
390 i = j;
391 printf("\n");
393 printf((*cursor != NULL ? "%s, " : "%s\n"), cp->name);
396 ac_free(cpa);
397 NYD_LEAVE;
398 return 0;
401 static int
402 _c_quit(void *v)
404 int rv;
405 NYD_ENTER;
406 UNUSED(v);
408 /* If we are PS_SOURCING, then return 1 so evaluate() can handle it.
409 * Otherwise return -1 to abort command loop */
410 rv = (pstate & PS_SOURCING) ? 1 : -1;
411 NYD_LEAVE;
412 return rv;
415 static int
416 _c_features(void *v)
418 NYD_ENTER;
419 UNUSED(v);
420 printf(_("Features: %s\n"), ok_vlook(features));
421 NYD_LEAVE;
422 return 0;
425 static int
426 _c_version(void *v)
428 NYD_ENTER;
429 UNUSED(v);
430 printf(_("Version %s\n"), ok_vlook(version));
431 NYD_LEAVE;
432 return 0;
435 static void
436 stop(int s)
438 sighandler_type old_action;
439 sigset_t nset;
440 NYD_X; /* Signal handler */
442 old_action = safe_signal(s, SIG_DFL);
444 sigemptyset(&nset);
445 sigaddset(&nset, s);
446 sigprocmask(SIG_UNBLOCK, &nset, NULL);
447 n_raise(s);
448 sigprocmask(SIG_BLOCK, &nset, NULL);
449 safe_signal(s, old_action);
450 if (_reset_on_stop) {
451 _reset_on_stop = 0;
452 reset(0);
456 static void
457 hangup(int s)
459 NYD_X; /* Signal handler */
460 UNUSED(s);
461 /* nothing to do? */
462 exit(1);
465 FL int
466 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
468 static int shudclob;
470 struct stat stb;
471 struct flock flp;
472 size_t offset;
473 char const *who;
474 int rv, omsgCount = 0;
475 FILE *ibuf = NULL;
476 bool_t isdevnull = FAL0;
477 NYD_ENTER;
479 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
480 * necessary to make a mailbox accessible by a different user, and if that
481 * has happened, let's just let the usual file perms decide */
483 if (name[0] == '%' || ((who = shortcut_expand(name)) != NULL && *who == '%'))
484 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
486 who = (name[1] != '\0') ? name + 1 : myname;
488 if ((name = expand(name)) == NULL)
489 goto jem1;
491 switch (which_protocol(name)) {
492 case PROTO_FILE:
493 if (temporary_protocol_ext != NULL)
494 name = savecat(name, temporary_protocol_ext);
495 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
496 #ifdef HAVE_REALPATH
497 do { /* TODO we need objects, goes away then */
498 char ebuf[PATH_MAX];
499 if (realpath(name, ebuf) != NULL)
500 name = savestr(ebuf);
501 } while (0);
502 #endif
503 break;
504 case PROTO_MAILDIR:
505 shudclob = 1;
506 rv = maildir_setfile(name, fm);
507 goto jleave;
508 #ifdef HAVE_POP3
509 case PROTO_POP3:
510 shudclob = 1;
511 rv = pop3_setfile(name, fm);
512 goto jleave;
513 #endif
514 #ifdef HAVE_IMAP
515 case PROTO_IMAP:
516 shudclob = 1;
517 if ((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE)
518 rv = 1;
519 else
520 rv = imap_setfile(name, fm);
521 goto jleave;
522 #endif
523 default:
524 fprintf(stderr, _("Cannot handle protocol: %s\n"), name);
525 goto jem1;
528 if ((ibuf = Zopen(name, "r")) == NULL) {
529 if (((fm & FEDIT_SYSBOX) && errno == ENOENT) || (fm & FEDIT_NEWMAIL)) {
530 if (fm & FEDIT_NEWMAIL)
531 goto jnonmail;
532 goto jnomail;
534 perror(name);
535 goto jem1;
538 if (fstat(fileno(ibuf), &stb) == -1) {
539 if (fm & FEDIT_NEWMAIL)
540 goto jnonmail;
541 perror("fstat");
542 goto jem1;
545 if (S_ISREG(stb.st_mode) || isdevnull) {
546 /* EMPTY */
547 } else {
548 if (fm & FEDIT_NEWMAIL)
549 goto jnonmail;
550 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
551 perror(name);
552 goto jem1;
555 /* Looks like all will be well. We must now relinquish our hold on the
556 * current set of stuff. Must hold signals while we are reading the new
557 * file, else we will ruin the message[] data structure */
559 hold_sigs(); /* TODO note on this one in quit.c:quit() */
560 if (shudclob && !(fm & FEDIT_NEWMAIL))
561 quit();
562 #ifdef HAVE_SOCKETS
563 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
564 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
565 #endif
567 /* TODO There is no intermediate VOID box we've switched to: name may
568 * TODO point to the same box that we just have written, so any updates
569 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
570 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
571 /* TODO We may uselessly open twice but quit() doesn't say wether we were
572 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
573 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
574 name = mailname;
575 Fclose(ibuf);
577 if ((ibuf = Zopen(name, "r")) == NULL ||
578 fstat(fileno(ibuf), &stb) == -1 || !S_ISREG(stb.st_mode)) {
579 perror(name);
580 rele_sigs();
581 goto jem2;
585 /* Copy the messages into /tmp and set pointers */
586 flp.l_type = F_RDLCK;
587 flp.l_start = 0;
588 flp.l_whence = SEEK_SET;
589 if (!(fm & FEDIT_NEWMAIL)) {
590 mb.mb_type = MB_FILE;
591 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
592 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
593 if (shudclob) {
594 if (mb.mb_itf) {
595 fclose(mb.mb_itf);
596 mb.mb_itf = NULL;
598 if (mb.mb_otf) {
599 fclose(mb.mb_otf);
600 mb.mb_otf = NULL;
603 shudclob = 1;
604 if (fm & FEDIT_SYSBOX)
605 pstate &= ~PS_EDIT;
606 else
607 pstate |= PS_EDIT;
608 initbox(name);
609 offset = 0;
610 flp.l_len = 0;
611 if (!(pstate & PS_EDIT) && fcntl(fileno(ibuf), F_SETLKW, &flp) == -1) {
612 /*TODO dotlock!*/
613 perror("Unable to lock mailbox");
614 rele_sigs();
615 goto jem1;
617 } else {
618 fseek(mb.mb_otf, 0L, SEEK_END);
619 fseek(ibuf, mailsize, SEEK_SET);
620 offset = mailsize;
621 omsgCount = msgCount;
622 flp.l_len = offset;
623 if (!(pstate & PS_EDIT) && fcntl(fileno(ibuf), F_SETLKW, &flp) == -1) {
624 /*TODO dotlock!*/
625 rele_sigs();
626 goto jnonmail;
629 mailsize = fsize(ibuf);
631 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
632 rele_sigs();
633 goto jnonmail;
635 setptr(ibuf, offset);
636 setmsize(msgCount);
637 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
638 mb.mb_threaded = 0;
639 c_sort((void*)-1);
642 Fclose(ibuf);
643 ibuf = NULL;
644 rele_sigs();
645 if (!(fm & FEDIT_NEWMAIL))
646 pstate &= ~PS_SAW_COMMAND;
648 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
649 jnonmail:
650 if (!(fm & FEDIT_NEWMAIL)) {
651 if (!ok_blook(emptystart))
652 jnomail:
653 fprintf(stderr, _("No mail for %s\n"), who);
655 rv = 1;
656 goto jleave;
658 if (fm & FEDIT_NEWMAIL)
659 newmailinfo(omsgCount);
661 rv = 0;
662 jleave:
663 if (ibuf != NULL)
664 Fclose(ibuf);
665 NYD_LEAVE;
666 return rv;
667 jem2:
668 mb.mb_type = MB_VOID;
669 jem1:
670 rv = -1;
671 goto jleave;
674 FL int
675 newmailinfo(int omsgCount)
677 int mdot, i;
678 NYD_ENTER;
680 for (i = 0; i < omsgCount; ++i)
681 message[i].m_flag &= ~MNEWEST;
683 if (msgCount > omsgCount) {
684 for (i = omsgCount; i < msgCount; ++i)
685 message[i].m_flag |= MNEWEST;
686 printf(_("New mail has arrived.\n"));
687 if ((i = msgCount - omsgCount) == 1)
688 printf(_("Loaded 1 new message.\n"));
689 else
690 printf(_("Loaded %d new messages.\n"), i);
691 } else
692 printf(_("Loaded %d messages.\n"), msgCount);
694 check_folder_hook(TRU1);
696 mdot = getmdot(1);
698 if (ok_blook(header))
699 print_headers(omsgCount + 1, msgCount, FAL0);
700 NYD_LEAVE;
701 return mdot;
704 FL bool_t
705 commands(void)
707 struct eval_ctx ev;
708 int n;
709 bool_t volatile rv = TRU1;
710 NYD_ENTER;
712 if (!(pstate & PS_SOURCING)) {
713 if (safe_signal(SIGINT, SIG_IGN) != SIG_IGN)
714 safe_signal(SIGINT, onintr);
715 if (safe_signal(SIGHUP, SIG_IGN) != SIG_IGN)
716 safe_signal(SIGHUP, hangup);
717 /* TODO We do a lot of redundant signal handling, especially
718 * TODO with the command line editor(s); try to merge this */
719 safe_signal(SIGTSTP, stop);
720 safe_signal(SIGTTOU, stop);
721 safe_signal(SIGTTIN, stop);
723 _oldpipe = safe_signal(SIGPIPE, SIG_IGN);
724 safe_signal(SIGPIPE, _oldpipe);
726 memset(&ev, 0, sizeof ev);
728 setexit();
729 for (;;) {
730 char *temporary_orig_line; /* XXX eval_ctx.ev_line not yet constant */
732 /* TODO Unless we have our signal manager (or however we do it) child
733 * TODO processes may have time slots where their execution isn't
734 * TODO protected by signal handlers (in between start and setup
735 * TODO completed). close_all_files() is only called from onintr()
736 * TODO so those may linger possibly forever */
737 if (!(pstate & PS_SOURCING))
738 close_all_files();
740 interrupts = 0;
741 handlerstacktop = NULL;
743 #ifdef HAVE_COLOUR
744 colour_table = NULL; /* XXX intermediate hack */
745 #endif
746 if (temporary_localopts_store != NULL) /* XXX intermediate hack */
747 temporary_localopts_free(); /* XXX intermediate hack */
748 sreset((pstate & PS_SOURCING) != 0);
749 if (!(pstate & PS_SOURCING)) {
750 char *cp;
752 /* TODO Note: this buffer may contain a password. We should redefine
753 * TODO the code flow which has to do that */
754 if ((cp = termios_state.ts_linebuf) != NULL) {
755 termios_state.ts_linebuf = NULL;
756 termios_state.ts_linesize = 0;
757 free(cp); /* TODO pool give-back */
759 /* TODO Due to expand-on-tab of NCL the buffer may grow */
760 if (ev.ev_line.l > LINESIZE * 3) {
761 free(ev.ev_line.s); /* TODO pool! but what? */
762 ev.ev_line.s = NULL;
763 ev.ev_line.l = ev.ev_line_size = 0;
767 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
768 char *cp;
770 cp = ok_vlook(newmail);
771 if ((options & OPT_TTYIN) && (cp != NULL || mb.mb_type == MB_IMAP)) {
772 struct stat st;
774 n = (cp != NULL && strcmp(cp, "noimap") && strcmp(cp, "nopoll"));
775 if ((mb.mb_type == MB_FILE && !stat(mailname, &st) &&
776 st.st_size > mailsize) ||
777 #ifdef HAVE_IMAP
778 (mb.mb_type == MB_IMAP && imap_newmail(n) > (cp == NULL)) ||
779 #endif
780 (mb.mb_type == MB_MAILDIR && n != 0)) {
781 size_t odot = PTR2SIZE(dot - message);
782 ui32_t odid = (pstate & PS_DID_PRINT_DOT);
784 if (setfile(mailname,
785 FEDIT_NEWMAIL |
786 ((mb.mb_perm & MB_DELE) ? 0 : FEDIT_RDONLY)) < 0) {
787 exit_status |= EXIT_ERR;
788 rv = FAL0;
789 break;
791 if (mb.mb_type != MB_IMAP) {
792 dot = message + odot;
793 pstate |= odid;
798 _reset_on_stop = 1;
799 exit_status = EXIT_OK;
802 /* Read a line of commands and handle end of file specially */
803 jreadline:
804 ev.ev_line.l = ev.ev_line_size;
805 n = readline_input(NULL, TRU1, &ev.ev_line.s, &ev.ev_line.l,
806 ev.ev_new_content);
807 ev.ev_line_size = (ui32_t)ev.ev_line.l;
808 ev.ev_line.l = (ui32_t)n;
809 _reset_on_stop = 0;
810 if (n < 0) {
811 /* EOF */
812 if (pstate & PS_LOADING)
813 break;
814 if (pstate & PS_SOURCING) {
815 unstack();
816 continue;
818 if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
819 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
820 continue;
822 break;
825 temporary_orig_line = ((pstate & PS_SOURCING) ||
826 !(options & OPT_INTERACTIVE)) ? NULL
827 : savestrbuf(ev.ev_line.s, ev.ev_line.l);
828 pstate &= ~PS_HOOK_MASK;
829 if (evaluate(&ev)) {
830 if (pstate & PS_LOADING) /* TODO mess; join PS_EVAL_ERROR.. */
831 rv = FAL0;
832 break;
834 if ((options & OPT_BATCH_FLAG) && ok_blook(batch_exit_on_error)) {
835 if (exit_status != EXIT_OK)
836 break;
837 if ((pstate & (PS_IN_LOAD | PS_EVAL_ERROR)) == PS_EVAL_ERROR) {
838 exit_status = EXIT_ERR;
839 break;
842 if (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE)) {
843 if (ev.ev_new_content != NULL)
844 goto jreadline;
845 /* That *can* happen since evaluate() unstack()s on error! */
846 if (temporary_orig_line != NULL)
847 tty_addhist(temporary_orig_line, !ev.ev_add_history);
851 if (ev.ev_line.s != NULL)
852 free(ev.ev_line.s);
853 if (pstate & PS_SOURCING)
854 sreset(FAL0);
855 NYD_LEAVE;
856 return rv;
859 FL int
860 execute(char *linebuf, int contxt, size_t linesize) /* XXX LEGACY */
862 struct eval_ctx ev;
863 #ifdef HAVE_COLOUR
864 struct colour_table *ct_save;
865 #endif
866 char *temporary_orig_line; /* XXX eval_ctx.ev_line not yet constant */
867 int rv;
868 NYD_ENTER;
870 /* TODO Maybe recursion from within collect.c! As long as we don't have
871 * TODO a value carrier that transports the entire state of a recursion
872 * TODO we need to save away also the colour table */
873 #ifdef HAVE_COLOUR
874 ct_save = colour_table;
875 colour_table = NULL;
876 #endif
878 memset(&ev, 0, sizeof ev);
879 ev.ev_line.s = linebuf;
880 ev.ev_line.l = linesize;
881 ev.ev_is_recursive = (contxt != 0);
882 temporary_orig_line = contxt ? savestr(linebuf) : NULL;
883 rv = evaluate(&ev);
885 if (contxt && temporary_orig_line != NULL)
886 tty_addhist(temporary_orig_line, TRU1);
888 #ifdef HAVE_COLOUR
889 colour_table = ct_save;
890 #endif
891 NYD_LEAVE;
892 return rv;
895 FL int
896 evaluate(struct eval_ctx *evp)
898 struct str line;
899 char _wordbuf[2], *arglist[MAXARGC], *cp, *word;
900 struct cmd_ghost *cg = NULL;
901 struct cmd const *com = NULL;
902 int muvec[2], c, e = 1;
903 NYD_ENTER;
905 line = evp->ev_line; /* XXX don't change original (buffer pointer) */
906 evp->ev_add_history = FAL0;
907 evp->ev_new_content = NULL;
909 /* Command ghosts that refer to shell commands or macro expansion restart */
910 jrestart:
912 /* Strip the white space away from the beginning of the command */
913 for (cp = line.s; whitechar(*cp); ++cp)
915 line.l -= PTR2SIZE(cp - line.s);
917 /* Ignore comments */
918 if (*cp == '#')
919 goto jleave0;
921 /* Handle ! differently to get the correct lexical conventions */
922 if (*cp == '!') {
923 if (pstate & PS_SOURCING) {
924 fprintf(stderr, _("Can't `!' while sourcing\n"));
925 goto jleave;
927 c_shell(++cp);
928 evp->ev_add_history = TRU1;
929 goto jleave0;
932 /* Isolate the actual command; since it may not necessarily be
933 * separated from the arguments (as in `p1') we need to duplicate it to
934 * be able to create a NUL terminated version.
935 * We must be aware of several special one letter commands here */
936 arglist[0] = cp;
937 if ((cp = _lex_isolate(cp)) == arglist[0] &&
938 (*cp == '|' || *cp == '~' || *cp == '?'))
939 ++cp;
940 c = (int)PTR2SIZE(cp - arglist[0]);
941 line.l -= c;
942 word = UICMP(z, c, <, sizeof _wordbuf) ? _wordbuf : salloc(c +1);
943 memcpy(word, arglist[0], c);
944 word[c] = '\0';
946 /* Look up the command; if not found, bitch.
947 * Normally, a blank command would map to the first command in the
948 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
949 * confusion; act just the same for ghosts */
950 if (*word == '\0') {
951 if ((pstate & PS_SOURCING) || cg != NULL)
952 goto jleave0;
953 com = _cmd_tab + 0;
954 goto jexec;
957 /* If this is the first evaluation, check command ghosts */
958 if (cg == NULL) {
959 /* TODO relink list head, so it's sorted on usage over time?
960 * TODO in fact, there should be one hashmap over all commands and ghosts
961 * TODO so that the lookup could be made much more efficient than it is
962 * TODO now (two adjacent list searches! */
963 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
964 if (!strcmp(word, cg->name)) {
965 if (line.l > 0) {
966 size_t i = cg->cmd.l;
967 line.s = salloc(i + line.l +1);
968 memcpy(line.s, cg->cmd.s, i);
969 memcpy(line.s + i, cp, line.l);
970 line.s[i += line.l] = '\0';
971 line.l = i;
972 } else {
973 line.s = cg->cmd.s;
974 line.l = cg->cmd.l;
976 goto jrestart;
980 if ((com = _lex(word)) == NULL || com->func == &c_cmdnotsupp) {
981 bool_t s = condstack_isskip();
982 if (!s || (options & OPT_D_V))
983 fprintf(stderr, _("Unknown command%s: `%s'\n"),
984 (s ? _(" (conditionally ignored)") : ""), word);
985 if (s)
986 goto jleave0;
987 if (com != NULL) {
988 c_cmdnotsupp(NULL);
989 com = NULL;
991 goto jleave;
994 /* See if we should execute the command -- if a conditional we always
995 * execute it, otherwise, check the state of cond */
996 jexec:
997 if (!(com->argtype & ARG_F) && condstack_isskip())
998 goto jleave0;
1000 /* Process the arguments to the command, depending on the type it expects,
1001 * default to error. If we're PS_SOURCING an interactive command: error */
1002 if ((options & OPT_SENDMODE) && !(com->argtype & ARG_M)) {
1003 fprintf(stderr, _("May not execute `%s' while sending\n"),
1004 com->name);
1005 goto jleave;
1007 if ((pstate & PS_SOURCING) && (com->argtype & ARG_I)) {
1008 fprintf(stderr, _("May not execute `%s' while sourcing\n"),
1009 com->name);
1010 goto jleave;
1012 if (!(mb.mb_perm & MB_DELE) && (com->argtype & ARG_W)) {
1013 fprintf(stderr, _("May not execute `%s' -- "
1014 "message file is read only\n"), com->name);
1015 goto jleave;
1017 if (evp->ev_is_recursive && (com->argtype & ARG_R)) {
1018 fprintf(stderr, _("Cannot recursively invoke `%s'\n"), com->name);
1019 goto jleave;
1021 if (mb.mb_type == MB_VOID && (com->argtype & ARG_A)) {
1022 fprintf(stderr, _("Cannot execute `%s' without active mailbox\n"),
1023 com->name);
1024 goto jleave;
1026 if (com->argtype & ARG_O)
1027 OBSOLETE2(_("this command will be removed"), com->name);
1029 if (com->argtype & ARG_V)
1030 temporary_arg_v_store = NULL;
1032 switch (com->argtype & ARG_ARGMASK) {
1033 case ARG_MSGLIST:
1034 /* Message list defaulting to nearest forward legal message */
1035 if (_msgvec == NULL)
1036 goto je96;
1037 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1038 break;
1039 if (c == 0) {
1040 *_msgvec = first(com->msgflag, com->msgmask);
1041 if (*_msgvec != 0)
1042 _msgvec[1] = 0;
1044 if (*_msgvec == 0) {
1045 if (!(pstate & PS_HOOK_MASK))
1046 printf(_("No applicable messages\n"));
1047 break;
1049 e = (*com->func)(_msgvec);
1050 break;
1052 case ARG_NDMLIST:
1053 /* Message list with no defaults, but no error if none exist */
1054 if (_msgvec == NULL) {
1055 je96:
1056 fprintf(stderr, _("Invalid use of \"message list\"\n"));
1057 break;
1059 if ((c = getmsglist(cp, _msgvec, com->msgflag)) < 0)
1060 break;
1061 e = (*com->func)(_msgvec);
1062 break;
1064 case ARG_STRLIST:
1065 /* Just the straight string, with leading blanks removed */
1066 while (whitechar(*cp))
1067 ++cp;
1068 e = (*com->func)(cp);
1069 break;
1071 case ARG_RAWLIST:
1072 case ARG_ECHOLIST:
1073 /* A vector of strings, in shell style */
1074 if ((c = getrawlist(cp, line.l, arglist, NELEM(arglist),
1075 ((com->argtype & ARG_ARGMASK) == ARG_ECHOLIST))) < 0)
1076 break;
1077 if (c < com->minargs) {
1078 fprintf(stderr, _("`%s' requires at least %d arg(s)\n"),
1079 com->name, com->minargs);
1080 break;
1082 if (c > com->maxargs) {
1083 fprintf(stderr, _("`%s' takes no more than %d arg(s)\n"),
1084 com->name, com->maxargs);
1085 break;
1087 e = (*com->func)(arglist);
1088 break;
1090 case ARG_NOLIST:
1091 /* Just the constant zero, for exiting, eg. */
1092 e = (*com->func)(0);
1093 break;
1095 default:
1096 panic(_("Unknown argument type"));
1099 if (e == 0 && (com->argtype & ARG_V) &&
1100 (cp = temporary_arg_v_store) != NULL) {
1101 temporary_arg_v_store = NULL;
1102 evp->ev_new_content = cp;
1103 goto jleave0;
1105 if (!(com->argtype & ARG_H) && !(pstate & PS_MSGLIST_SAW_NO))
1106 evp->ev_add_history = TRU1;
1108 jleave:
1109 /* Exit the current source file on error TODO what a mess! */
1110 if (e == 0)
1111 pstate &= ~PS_EVAL_ERROR;
1112 else {
1113 pstate |= PS_EVAL_ERROR;
1114 if (e < 0 || (pstate & PS_LOADING)) {
1115 e = 1;
1116 goto jret;
1118 if (pstate & PS_SOURCING)
1119 unstack();
1120 goto jret0;
1122 if (com == NULL)
1123 goto jret0;
1124 if ((com->argtype & ARG_P) && ok_blook(autoprint))
1125 if (visible(dot)) {
1126 muvec[0] = (int)PTR2SIZE(dot - message + 1);
1127 muvec[1] = 0;
1128 c_type(muvec); /* TODO what if error? re-eval! */
1130 if (!(pstate & (PS_SOURCING | PS_HOOK_MASK)) && !(com->argtype & ARG_T))
1131 pstate |= PS_SAW_COMMAND;
1132 jleave0:
1133 pstate &= ~PS_EVAL_ERROR;
1134 jret0:
1135 e = 0;
1136 jret:
1137 NYD_LEAVE;
1138 return e;
1141 FL void
1142 setmsize(int sz)
1144 NYD_ENTER;
1145 if (_msgvec != NULL)
1146 free(_msgvec);
1147 _msgvec = scalloc(sz + 1, sizeof *_msgvec);
1148 NYD_LEAVE;
1151 FL void
1152 print_header_summary(char const *Larg)
1154 size_t bot, top, i, j;
1155 NYD_ENTER;
1157 if (Larg != NULL) {
1158 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1159 if ((options & (OPT_VERB | OPT_HEADERSONLY)) == OPT_HEADERSONLY) {
1160 freopen("/dev/null", "w", stdout);
1161 freopen("/dev/null", "w", stderr);
1163 assert(_msgvec != NULL);
1164 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), _msgvec, 0) <= 0);
1165 if (options & OPT_HEADERSONLY) {
1166 exit_status = (int)i;
1167 goto jleave;
1169 if (i)
1170 goto jleave;
1171 for (bot = msgCount, top = 0, i = 0; (j = _msgvec[i]) != 0; ++i) {
1172 if (bot > j)
1173 bot = j;
1174 if (top < j)
1175 top = j;
1177 } else
1178 bot = 1, top = msgCount;
1179 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
1180 jleave:
1181 NYD_LEAVE;
1184 FL void
1185 onintr(int s)
1187 NYD_X; /* Signal handler */
1189 if (handlerstacktop != NULL) {
1190 handlerstacktop(s);
1191 return;
1193 safe_signal(SIGINT, onintr);
1194 noreset = 0;
1195 if (!_lex_inithdr)
1196 pstate |= PS_SAW_COMMAND;
1197 _lex_inithdr = 0;
1198 while (pstate & PS_SOURCING)
1199 unstack();
1201 termios_state_reset();
1202 close_all_files();
1204 if (image >= 0) {
1205 close(image);
1206 image = -1;
1208 if (interrupts != 1)
1209 fprintf(stderr, _("Interrupt\n"));
1210 safe_signal(SIGPIPE, _oldpipe);
1211 reset(0);
1214 FL void
1215 announce(int printheaders)
1217 int vec[2], mdot;
1218 NYD_ENTER;
1220 mdot = newfileinfo();
1221 vec[0] = mdot;
1222 vec[1] = 0;
1223 dot = message + mdot - 1;
1224 if (printheaders && msgCount > 0 && ok_blook(header)) {
1225 ++_lex_inithdr;
1226 print_header_group(vec); /* XXX errors? */
1227 _lex_inithdr = 0;
1229 NYD_LEAVE;
1232 FL int
1233 newfileinfo(void)
1235 struct message *mp;
1236 int u, n, mdot, d, s, hidden, moved;
1237 NYD_ENTER;
1239 if (mb.mb_type == MB_VOID) {
1240 mdot = 1;
1241 goto jleave;
1244 mdot = getmdot(0);
1245 s = d = hidden = moved =0;
1246 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
1247 if (mp->m_flag & MNEW)
1248 ++n;
1249 if ((mp->m_flag & MREAD) == 0)
1250 ++u;
1251 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
1252 ++moved;
1253 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
1254 ++d;
1255 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
1256 ++s;
1257 if (mp->m_flag & MHIDDEN)
1258 ++hidden;
1261 /* If displayname gets truncated the user effectively has no option to see
1262 * the full pathname of the mailbox, so print it at least for '? fi' */
1263 printf(_("\"%s\": "),
1264 (_update_mailname(NULL) ? displayname : mailname));
1265 if (msgCount == 1)
1266 printf(_("1 message"));
1267 else
1268 printf(_("%d messages"), msgCount);
1269 if (n > 0)
1270 printf(_(" %d new"), n);
1271 if (u-n > 0)
1272 printf(_(" %d unread"), u);
1273 if (d > 0)
1274 printf(_(" %d deleted"), d);
1275 if (s > 0)
1276 printf(_(" %d saved"), s);
1277 if (moved > 0)
1278 printf(_(" %d moved"), moved);
1279 if (hidden > 0)
1280 printf(_(" %d hidden"), hidden);
1281 if (mb.mb_type == MB_CACHE)
1282 printf(" [Disconnected]");
1283 else if (mb.mb_perm == 0)
1284 printf(_(" [Read only]"));
1285 printf("\n");
1286 jleave:
1287 NYD_LEAVE;
1288 return mdot;
1291 FL int
1292 getmdot(int nmail)
1294 struct message *mp;
1295 char *cp;
1296 int mdot;
1297 enum mflag avoid = MHIDDEN | MDELETED;
1298 NYD_ENTER;
1300 if (!nmail) {
1301 if (ok_blook(autothread)) {
1302 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1303 c_thread(NULL);
1304 } else if ((cp = ok_vlook(autosort)) != NULL) {
1305 if (mb.mb_sorted != NULL)
1306 free(mb.mb_sorted);
1307 mb.mb_sorted = sstrdup(cp);
1308 c_sort(NULL);
1311 if (mb.mb_type == MB_VOID) {
1312 mdot = 1;
1313 goto jleave;
1316 if (nmail)
1317 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1318 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
1319 break;
1321 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
1322 if (mb.mb_threaded) {
1323 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1324 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1325 break;
1326 } else {
1327 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1328 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
1329 break;
1333 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1334 if (mb.mb_threaded) {
1335 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1336 if (mp->m_flag & MFLAGGED)
1337 break;
1338 } else {
1339 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1340 if (mp->m_flag & MFLAGGED)
1341 break;
1345 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
1346 if (mb.mb_threaded) {
1347 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
1348 if (!(mp->m_flag & (MREAD | avoid)))
1349 break;
1350 } else {
1351 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1352 if (!(mp->m_flag & (MREAD | avoid)))
1353 break;
1357 if (nmail &&
1358 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1359 mdot = (int)PTR2SIZE(mp - message + 1);
1360 else if (ok_blook(showlast)) {
1361 if (mb.mb_threaded) {
1362 for (mp = this_in_thread(threadroot, -1); mp;
1363 mp = prev_in_thread(mp))
1364 if (!(mp->m_flag & avoid))
1365 break;
1366 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1367 } else {
1368 for (mp = message + msgCount - 1; mp >= message; --mp)
1369 if (!(mp->m_flag & avoid))
1370 break;
1371 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
1373 } else if (!nmail &&
1374 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
1375 mdot = (int)PTR2SIZE(mp - message + 1);
1376 else if (mb.mb_threaded) {
1377 for (mp = threadroot; mp; mp = next_in_thread(mp))
1378 if (!(mp->m_flag & avoid))
1379 break;
1380 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
1381 } else {
1382 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
1383 if (!(mp->m_flag & avoid))
1384 break;
1385 mdot = PTRCMP(mp, <, message + msgCount)
1386 ? (int)PTR2SIZE(mp - message + 1) : 1;
1388 jleave:
1389 NYD_LEAVE;
1390 return mdot;
1393 FL void
1394 initbox(char const *name)
1396 char *tempMesg;
1397 NYD_ENTER;
1399 if (mb.mb_type != MB_VOID)
1400 n_strlcpy(prevfile, mailname, PATH_MAX);
1402 /* TODO name always NE mailname (but goes away for objects anyway)
1403 * TODO Well, not true no more except that in parens */
1404 _update_mailname((name != mailname) ? name : NULL);
1406 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpbox", OF_WRONLY | OF_HOLDSIGS, 0600)) ==
1407 NULL) {
1408 perror(_("temporary mail message file"));
1409 exit(1);
1411 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
1412 perror(_("temporary mail message file"));
1413 exit(1);
1415 Ftmp_release(&tempMesg);
1417 message_reset();
1418 mb.mb_threaded = 0;
1419 if (mb.mb_sorted != NULL) {
1420 free(mb.mb_sorted);
1421 mb.mb_sorted = NULL;
1423 #ifdef HAVE_IMAP
1424 mb.mb_flags = MB_NOFLAGS;
1425 #endif
1426 dot = prevdot = threadroot = NULL;
1427 pstate &= ~PS_DID_PRINT_DOT;
1428 NYD_LEAVE;
1431 #ifdef HAVE_DOCSTRINGS
1432 FL bool_t
1433 print_comm_docstr(char const *comm)
1435 bool_t rv = FAL0;
1436 struct cmd_ghost *cg;
1437 struct cmd const *cp;
1438 NYD_ENTER;
1440 /* Ghosts take precedence */
1441 for (cg = _cmd_ghosts; cg != NULL; cg = cg->next)
1442 if (!strcmp(comm, cg->name)) {
1443 printf("%s -> <%s>\n", comm, cg->cmd.s);
1444 rv = TRU1;
1445 goto jleave;
1448 for (cp = _cmd_tab; cp->name != NULL; ++cp) {
1449 if (cp->func == &c_cmdnotsupp)
1450 continue;
1451 if (!strcmp(comm, cp->name))
1452 printf("%s: %s\n", comm, V_(cp->doc));
1453 else if (is_prefix(comm, cp->name))
1454 printf("%s (%s): %s\n", comm, cp->name, V_(cp->doc));
1455 else
1456 continue;
1457 rv = TRU1;
1458 break;
1460 jleave:
1461 NYD_LEAVE;
1462 return rv;
1464 #endif
1466 /* s-it-mode */