1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ (Lexical processing of) Commands, and the event mainloop.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
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
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
40 #ifndef HAVE_AMALGAMATION
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 int docid
; /* Translation id of .doc */
54 char const *doc
; /* One line doc for command */
57 /* Yechh, can't initialize unions */
58 #define minargs msgflag /* Minimum argcount for RAWLIST */
59 #define maxargs msgmask /* Max argcount for RAWLIST */
62 struct cmd_ghost
*next
;
63 struct str cmd
; /* Data follows after .name */
64 char name
[VFIELD_SIZE(sizeof(size_t))];
68 static int _reset_on_stop
; /* do a reset() if stopped */
69 static sighandler_type _oldpipe
;
70 static struct cmd_ghost
*_cmd_ghosts
;
71 /* _cmd_tab[] after fun protos */
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_{pre,suf}fix() into one fun! */
77 SINLINE
size_t __narrow_prefix(char const *cp
, size_t maxl
);
78 SINLINE
size_t __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
);
81 /* Isolate the command from the arguments */
82 static char * _lex_isolate(char const *comm
);
84 /* Get first-fit, or NULL */
85 static struct cmd
const * _lex(char const *comm
);
87 /* Command ghost handling */
88 static int _ghost(void *v
);
89 static int _unghost(void *v
);
91 /* Print a list of all commands */
92 static int _pcmdlist(void *v
);
93 static int __pcmd_cmp(void const *s1
, void const *s2
);
95 /* Print the binaries compiled-in features */
96 static int _features(void *v
);
98 /* Print the binaries version number */
99 static int _version(void *v
);
101 static void stop(int s
);
102 static void hangup(int s
);
104 /* List of all commands */
105 static struct cmd
const _cmd_tab
[] = {
109 #ifdef HAVE_C90AMEND1
111 __narrow_prefix(char const *cp
, size_t maxl
)
116 for (err
= ok
= i
= 0; i
< maxl
;) {
117 int ml
= mblen(cp
, maxl
- i
);
118 if (ml
< 0) { /* XXX _narrow_prefix(): mblen() error; action? */
119 (void)mblen(NULL
, 0);
136 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
141 for (err
= ok
= i
= 0; cpl
> maxl
|| err
;) {
142 int ml
= mblen(cp
, cpl
);
143 if (ml
< 0) { /* XXX _narrow_suffix(): mblen() error; action? */
144 (void)mblen(NULL
, 0);
160 #endif /* HAVE_C90AMEND1 */
163 _update_mailname(char const *name
)
165 char tbuf
[MAXPATHLEN
], *mailp
, *dispp
;
169 /* Don't realpath(3) if it's only an update request */
172 enum protocol p
= which_protocol(name
);
173 if (p
== PROTO_FILE
|| p
== PROTO_MAILDIR
) {
174 if (realpath(name
, mailname
) == NULL
) {
175 fprintf(stderr
, tr(151, "Can't canonicalize `%s'\n"), name
);
181 n_strlcpy(mailname
, name
, sizeof(mailname
));
187 /* Don't display an absolute path but "+FOLDER" if under *folder* */
188 if (getfold(tbuf
, sizeof tbuf
)) {
190 if (i
< sizeof(tbuf
) - 1)
192 if (strncmp(tbuf
, mailp
, i
) == 0) {
198 /* We want to see the name of the folder .. on the screen */
200 if ((rv
= (i
< sizeof(displayname
) - 1)))
201 memcpy(dispp
, mailp
, i
+ 1);
203 /* Avoid disrupting multibyte sequences (if possible) */
204 #ifndef HAVE_C90AMEND1
205 j
= sizeof(displayname
) / 3 - 1;
206 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
208 j
= __narrow_prefix(mailp
, sizeof(displayname
) / 3);
209 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
210 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
212 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
213 (int)j
, mailp
, mailp
+ i
);
222 _lex_isolate(char const *comm
)
224 while (*comm
&& strchr(" \t0123456789$^.:/-+*'\",;(`", *comm
) == NULL
)
226 return UNCONST(comm
);
229 static struct cmd
const *
230 _lex(char const *comm
)
232 struct cmd
const *cp
;
234 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
)
235 if (is_prefix(comm
, cp
->name
))
245 char const **argv
= (char const **)v
;
246 struct cmd_ghost
*lcg
, *cg
;
251 printf(tr(144, "Command ghosts are:\n"));
252 for (nl
= 0, cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
) {
253 cl
= strlen(cg
->name
) + 5 + cg
->cmd
.l
+ 3;
254 if ((nl
+= cl
) >= (size_t)scrnwidth
) {
258 printf((cg
->next
!= NULL
? "%s -> <%s>, " : "%s -> <%s>\n"),
259 cg
->name
, cg
->cmd
.s
);
265 /* Request to add new ghost */
266 if (argv
[1] == NULL
|| argv
[1][0] == '\0' || argv
[2] != NULL
) {
267 fprintf(stderr
, tr(159, "Usage: %s\n"),
268 tr(425, "Define a <ghost> of <command>, or list all ghosts"));
273 /* Check that we can deal with this one */
274 switch (argv
[0][0]) {
283 if (argv
[0] == _lex_isolate(argv
[0])) {
285 fprintf(stderr
, tr(151, "Can't canonicalize `%s'\n"), argv
[0]);
292 /* Always recreate */
293 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
294 if (strcmp(cg
->name
, argv
[0]) == 0) {
296 lcg
->next
= cg
->next
;
298 _cmd_ghosts
= cg
->next
;
304 nl
= strlen(argv
[0]) + 1;
305 cl
= strlen(argv
[1]) + 1;
306 cg
= smalloc(sizeof(*cg
) - VFIELD_SIZEOF(struct cmd_ghost
, name
) + nl
+ cl
);
307 cg
->next
= _cmd_ghosts
;
308 memcpy(cg
->name
, argv
[0], nl
);
309 cg
->cmd
.s
= cg
->name
+ nl
;
311 memcpy(cg
->cmd
.s
, argv
[1], cl
);
322 char const **argv
= v
, *cp
;
323 struct cmd_ghost
*lcg
, *cg
;
325 while ((cp
= *argv
++) != NULL
) {
326 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
327 if (strcmp(cg
->name
, cp
) == 0) {
329 lcg
->next
= cg
->next
;
331 _cmd_ghosts
= cg
->next
;
335 fprintf(stderr
, tr(91, "Unknown command: `%s'\n"), cp
);
344 __pcmd_cmp(void const *s1
, void const *s2
)
346 struct cmd
const * const *c1
= s1
, * const *c2
= s2
;
347 return (strcmp((*c1
)->name
, (*c2
)->name
));
353 struct cmd
const **cpa
, *cp
, **cursor
;
357 for (i
= 0; _cmd_tab
[i
].name
!= NULL
; ++i
)
360 cpa
= ac_alloc(sizeof(cp
) * i
);
362 for (i
= 0; (cp
= _cmd_tab
+ i
)->name
!= NULL
; ++i
)
366 qsort(cpa
, i
, sizeof(cp
), &__pcmd_cmp
);
368 printf(tr(14, "Commands are:\n"));
369 for (i
= 0, cursor
= cpa
; (cp
= *cursor
++) != NULL
;) {
371 if (cp
->func
== &ccmdnotsupp
)
373 j
= strlen(cp
->name
) + 2;
378 printf((*cursor
!= NULL
? "%s, " : "%s\n"), cp
->name
);
389 printf(tr(523, "Features: %s\n"), features
);
397 printf(tr(111, "Version %s\n"), version
);
402 * Set up editing on the given file name.
403 * If the first character of name is %, we are considered to be
404 * editing the file, otherwise we are reading our mail which has
405 * signficance for mbox and so forth.
407 * nmail: Check for new mail in the current folder only.
410 setfile(char const *name
, int nmail
)
413 int i
, compressed
= 0;
416 char const *who
= name
[1] ? name
+ 1 : myname
;
423 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
424 * necessary to make a mailbox accessible by a different user, and if that
425 * has happened, let's just let the usual file perms decide */
426 isedit
= (*name
!= '%' && ((sh
= get_shortcut(name
)) == NULL
||
427 *sh
->sh_long
!= '%'));
428 if ((name
= expand(name
)) == NULL
)
431 switch (which_protocol(name
)) {
435 return (maildir_setfile(name
, nmail
, isedit
));
439 return (pop3_setfile(name
, nmail
, isedit
));
445 if (mb
.mb_type
== MB_CACHE
)
448 return imap_setfile(name
, nmail
, isedit
);
451 fprintf(stderr
, tr(217, "Cannot handle protocol: %s\n"), name
);
455 if ((ibuf
= Zopen(name
, "r", &compressed
)) == NULL
) {
456 if ((!isedit
&& errno
== ENOENT
) || nmail
) {
465 if (fstat(fileno(ibuf
), &stb
) < 0) {
473 if (S_ISDIR(stb
.st_mode
)) {
480 } else if (S_ISREG(stb
.st_mode
)) {
492 * Looks like all will be well. We must now relinquish our
493 * hold on the current set of stuff. Must hold signals
494 * while we are reading the new file, else we will ruin
495 * the message[] data structure.
498 hold_sigs(); /* TODO note on this one in quit.c:quit() */
499 if (shudclob
&& !nmail
)
502 if (!nmail
&& mb
.mb_sock
.s_fd
>= 0)
507 * Copy the messages into /tmp
511 flp
.l_type
= F_RDLCK
;
513 flp
.l_whence
= SEEK_SET
;
515 mb
.mb_type
= MB_FILE
;
516 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
|MB_EDIT
;
517 mb
.mb_compressed
= compressed
;
519 if (compressed
& 0200)
522 if ((i
= open(name
, O_WRONLY
)) < 0)
542 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) < 0) {
543 perror("Unable to lock mailbox");
548 fseek(mb
.mb_otf
, 0L, SEEK_END
);
549 fseek(ibuf
, mailsize
, SEEK_SET
);
551 omsgCount
= msgCount
;
553 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) < 0)
556 mailsize
= fsize(ibuf
);
557 if (nmail
&& (size_t)mailsize
<= offset
) {
561 setptr(ibuf
, offset
);
563 if (nmail
&& mb
.mb_sorted
) {
571 if ((!edit
|| nmail
) && msgCount
== 0) {
574 if (value("emptystart") == NULL
)
575 nomail
: fprintf(stderr
, tr(88, "No mail for %s\n"),
581 newmailinfo(omsgCount
);
586 newmailinfo(int omsgCount
)
591 for (i
= 0; i
< omsgCount
; i
++)
592 message
[i
].m_flag
&= ~MNEWEST
;
593 if (msgCount
> omsgCount
) {
594 for (i
= omsgCount
; i
< msgCount
; i
++)
595 message
[i
].m_flag
|= MNEWEST
;
596 printf(tr(158, "New mail has arrived.\n"));
597 if (msgCount
- omsgCount
== 1)
598 printf(tr(214, "Loaded 1 new message.\n"));
600 printf(tr(215, "Loaded %d new messages.\n"),
601 msgCount
- omsgCount
);
603 printf(tr(224, "Loaded %d messages.\n"), msgCount
);
604 callhook(mailname
, 1);
607 print_headers(omsgCount
+ 1, msgCount
);
612 * Interpret user commands one by one. If standard input is not a tty,
619 char *linebuf
= NULL
, *av
, *nv
;
626 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
627 safe_signal(SIGINT
, onintr
);
628 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
629 safe_signal(SIGHUP
, hangup
);
630 /* TODO We do a lot of redundant signal handling, especially
631 * TODO with the command line editor(s); try to merge this */
632 safe_signal(SIGTSTP
, stop
);
633 safe_signal(SIGTTOU
, stop
);
634 safe_signal(SIGTTIN
, stop
);
636 _oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
637 safe_signal(SIGPIPE
, _oldpipe
);
642 handlerstacktop
= NULL
;
644 * Print the prompt, if needed. Clear out
645 * string space, and flush the output.
647 if (! sourcing
&& (options
& OPT_INTERACTIVE
)) {
648 av
= (av
= value("autoinc")) ? savestr(av
) : NULL
;
649 nv
= (nv
= value("newmail")) ? savestr(nv
) : NULL
;
650 if ((options
& OPT_TTYIN
) &&
651 (av
!= NULL
|| nv
!= NULL
||
652 mb
.mb_type
== MB_IMAP
)) {
655 n
= (av
&& strcmp(av
, "noimap") &&
656 strcmp(av
, "nopoll")) |
657 (nv
&& strcmp(nv
, "noimap") &&
658 strcmp(nv
, "nopoll"));
662 if ((mb
.mb_type
== MB_FILE
&&
663 stat(mailname
, &st
) == 0 &&
664 st
.st_size
> mailsize
) ||
666 (mb
.mb_type
== MB_IMAP
&&
667 imap_newmail(n
) > x
) ||
669 (mb
.mb_type
== MB_MAILDIR
&&
671 int odot
= dot
- &message
[0];
672 bool_t odid
= did_print_dot
;
674 setfile(mailname
, 1);
675 if (mb
.mb_type
!= MB_IMAP
) {
676 dot
= &message
[odot
];
677 did_print_dot
= odid
;
687 /* TODO Note: this buffer may contain a password
688 * TODO We should redefine the code flow which has
690 if ((nv
= termios_state
.ts_linebuf
) != NULL
) {
691 termios_state
.ts_linebuf
= NULL
;
692 termios_state
.ts_linesize
= 0;
693 free(nv
); /* TODO pool give-back */
695 /* TODO Due to expand-on-tab of our line editor the
696 * TODO buffer may grow */
697 if (linesize
> LINESIZE
* 3) {
698 free(linebuf
); /* TODO pool! but what? */
705 * Read a line of commands from the current input
706 * and handle end of file specially.
708 n
= readline_input(LNED_LF_ESC
| LNED_HIST_ADD
, NULL
,
709 &linebuf
, &linesize
);
719 if ((options
& OPT_INTERACTIVE
) &&
720 value("ignoreeof") && ++eofloop
< 25) {
721 printf(tr(89, "Use `quit' to quit.\n"));
729 if (execute(linebuf
, 0, n
))
731 if (exit_status
!= EXIT_OK
&& (options
& OPT_BATCH_FLAG
) &&
732 boption("batch-exit-on-error"))
743 * Execute a single command.
744 * Command functions return 0 for success, 1 for error, and -1
745 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
746 * the interactive command loop.
747 * Contxt is non-zero if called while composing mail.
750 execute(char *linebuf
, int contxt
, size_t linesize
)
752 char _wordbuf
[2], *arglist
[MAXARGC
], *cp
, *word
;
753 struct cmd_ghost
*cg
= NULL
;
754 struct cmd
const *com
= NULL
;
755 int muvec
[2], c
, e
= 1;
757 /* Command ghosts that refer to shell commands or macro expansion restart */
760 /* Strip the white space away from the beginning of the command */
761 for (cp
= linebuf
; whitechar(*cp
); ++cp
)
763 linesize
-= (size_t)(cp
- linebuf
);
765 /* Ignore comments */
769 /* Handle ! differently to get the correct lexical conventions */
772 fprintf(stderr
, tr(90, "Can't `!' while sourcing\n"));
779 /* Isolate the actual command; since it may not necessarily be
780 * separated from the arguments (as in `p1') we need to duplicate it to
781 * be able to create a NUL terminated version.
782 * We must be aware of special one letter commands here */
793 cp
= _lex_isolate(cp
);
796 c
= (int)(cp
- arglist
[0]);
798 word
= (c
< (int)sizeof _wordbuf
) ? _wordbuf
: salloc(c
+ 1);
799 memcpy(word
, arglist
[0], c
);
802 /* Look up the command; if not found, bitch.
803 * Normally, a blank command would map to the first command in the
804 * table; while sourcing, however, we ignore blank lines to eliminate
805 * confusion; act just the same for ghosts */
806 if ((sourcing
|| cg
!= NULL
) && *word
== '\0')
809 /* If this is the first evaluation, check command ghosts */
811 /* TODO relink list head, so it's sort over time on usage? */
812 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
813 if (strcmp(word
, cg
->name
) == 0) {
815 size_t i
= cg
->cmd
.l
;
816 linebuf
= salloc(i
+ 1 + linesize
);
817 memcpy(linebuf
, cg
->cmd
.s
, i
);
819 memcpy(linebuf
+ i
, cp
, linesize
);
820 linebuf
[i
+= linesize
] = '\0';
824 linesize
= cg
->cmd
.l
;
832 if (com
== NULL
|| com
->func
== &ccmdnotsupp
) {
833 fprintf(stderr
, tr(91, "Unknown command: `%s'\n"), word
);
842 * See if we should execute the command -- if a conditional
843 * we always execute it, otherwise, check the state of cond.
845 if ((com
->argtype
& F
) == 0) {
846 if ((cond
== CRCV
&& (options
& OPT_SENDMODE
)) ||
847 (cond
== CSEND
&& ! (options
& OPT_SENDMODE
)) ||
848 (cond
== CTERM
&& ! (options
& OPT_TTYIN
)) ||
849 (cond
== CNONTERM
&& (options
& OPT_TTYIN
)))
854 * Process the arguments to the command, depending
855 * on the type he expects. Default to an error.
856 * If we are sourcing an interactive command, it's
859 if ((options
& OPT_SENDMODE
) && (com
->argtype
& M
) == 0) {
860 fprintf(stderr
, tr(92,
861 "May not execute `%s' while sending\n"),
865 if (sourcing
&& com
->argtype
& I
) {
866 fprintf(stderr
, tr(93,
867 "May not execute `%s' while sourcing\n"),
871 if ((mb
.mb_perm
& MB_DELE
) == 0 && com
->argtype
& W
) {
872 fprintf(stderr
, tr(94, "May not execute `%s' -- "
873 "message file is read only\n"),
877 if (contxt
&& com
->argtype
& R
) {
878 fprintf(stderr
, tr(95,
879 "Cannot recursively invoke `%s'\n"), com
->name
);
882 if (mb
.mb_type
== MB_VOID
&& com
->argtype
& A
) {
883 fprintf(stderr
, tr(257,
884 "Cannot execute `%s' without active mailbox\n"),
889 switch (com
->argtype
& ~(F
|P
|I
|M
|T
|W
|R
|A
)) {
891 /* Message list defaulting to nearest forward legal message */
894 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
897 *_msgvec
= first(com
->msgflag
, com
->msgmask
);
903 printf(tr(97, "No applicable messages\n"));
906 e
= (*com
->func
)(_msgvec
);
910 /* Message list with no defaults, but no error if none exist */
913 fprintf(stderr
, tr(96,
914 "Illegal use of `message list'\n"));
917 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
919 e
= (*com
->func
)(_msgvec
);
923 /* Just the straight string, with leading blanks removed */
924 while (whitechar(*cp
))
926 e
= (*com
->func
)(cp
);
931 /* A vector of strings, in shell style */
932 if ((c
= getrawlist(cp
, linesize
, arglist
,
933 sizeof arglist
/ sizeof *arglist
,
934 (com
->argtype
&~(F
|P
|I
|M
|T
|W
|R
|A
)) == ECHOLIST
)
937 if (c
< com
->minargs
) {
938 fprintf(stderr
, tr(99,
939 "`%s' requires at least %d arg(s)\n"),
940 com
->name
, com
->minargs
);
943 if (c
> com
->maxargs
) {
944 fprintf(stderr
, tr(100,
945 "`%s' takes no more than %d arg(s)\n"),
946 com
->name
, com
->maxargs
);
949 e
= (*com
->func
)(arglist
);
953 /* Just the constant zero, for exiting, eg. */
958 panic(tr(101, "Unknown argument type"));
962 /* Exit the current source file on error */
963 if ((exec_last_comm_error
= (e
!= 0))) {
972 if (com
== (struct cmd
*)NULL
)
974 if (boption("autoprint") && com
->argtype
& P
)
976 muvec
[0] = dot
- &message
[0] + 1;
980 if (! sourcing
&& ! inhook
&& (com
->argtype
& T
) == 0)
987 * Set the size of the message vector used to construct argument
988 * lists to message list functions.
996 _msgvec
= (int*)scalloc(sz
+ 1, sizeof *_msgvec
);
1000 * The following gets called on receipt of an interrupt. This is
1001 * to abort printout of a command, mainly.
1002 * Dispatching here when command() is inactive crashes rcv.
1003 * Close all open files except 0, 1, 2, and the temporary.
1004 * Also, unstack all source files.
1007 static int inithdr
; /* am printing startup headers */
1013 if (handlerstacktop
!= NULL
) {
1017 safe_signal(SIGINT
, onintr
);
1025 termios_state_reset();
1032 if (interrupts
!= 1)
1033 fprintf(stderr
, tr(102, "Interrupt\n"));
1034 safe_signal(SIGPIPE
, _oldpipe
);
1039 * When we wake up after ^Z, reprint the prompt.
1044 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
1048 sigaddset(&nset
, s
);
1049 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
1051 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
1052 safe_signal(s
, old_action
);
1053 if (_reset_on_stop
) {
1060 * Branch here on hangup signal and simulate "exit".
1067 /* nothing to do? */
1072 * Announce the presence of the current Mail version,
1073 * give the message count, and print a header listing.
1076 announce(int printheaders
)
1080 mdot
= newfileinfo();
1083 dot
= &message
[mdot
- 1];
1084 if (printheaders
&& msgCount
> 0 && value("header") != NULL
) {
1092 * Announce information about the file we are editing.
1093 * Return a likely place to set dot.
1099 int u
, n
, mdot
, d
, s
, hidden
, moved
;
1101 if (mb
.mb_type
== MB_VOID
)
1104 s
= d
= hidden
= moved
=0;
1105 for (mp
= &message
[0], n
= 0, u
= 0; mp
< &message
[msgCount
]; mp
++) {
1106 if (mp
->m_flag
& MNEW
)
1108 if ((mp
->m_flag
& MREAD
) == 0)
1110 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == (MDELETED
|MSAVED
))
1112 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == MDELETED
)
1114 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == MSAVED
)
1116 if (mp
->m_flag
& MHIDDEN
)
1120 /* If displayname gets truncated the user effectively has no option to see
1121 * the full pathname of the mailbox, so print it at least for '? fi' */
1122 printf(tr(103, "\"%s\": "),
1123 (_update_mailname(NULL
) ? displayname
: mailname
));
1125 printf(tr(104, "1 message"));
1127 printf(tr(105, "%d messages"), msgCount
);
1129 printf(tr(106, " %d new"), n
);
1131 printf(tr(107, " %d unread"), u
);
1133 printf(tr(108, " %d deleted"), d
);
1135 printf(tr(109, " %d saved"), s
);
1137 printf(tr(136, " %d moved"), moved
);
1139 printf(tr(139, " %d hidden"), hidden
);
1140 if (mb
.mb_type
== MB_CACHE
)
1141 printf(" [Disconnected]");
1142 else if (mb
.mb_perm
== 0)
1143 printf(tr(110, " [Read only]"));
1154 enum mflag avoid
= MHIDDEN
|MDELETED
;
1157 if (value("autothread"))
1159 else if ((cp
= value("autosort")) != NULL
) {
1161 mb
.mb_sorted
= sstrdup(cp
);
1165 if (mb
.mb_type
== MB_VOID
)
1168 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
1169 if ((mp
->m_flag
& (MNEWEST
|avoid
)) == MNEWEST
)
1171 if (!nmail
|| mp
>= &message
[msgCount
]) {
1172 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1174 mp
!= NULL
: mp
< &message
[msgCount
];
1176 mp
= next_in_thread(mp
) : mp
++)
1177 if ((mp
->m_flag
& (MNEW
|avoid
)) == MNEW
)
1180 if (mb
.mb_threaded
? mp
== NULL
: mp
>= &message
[msgCount
])
1181 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1182 mb
.mb_threaded
? mp
!= NULL
:
1183 mp
< &message
[msgCount
];
1184 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++)
1185 if (mp
->m_flag
& MFLAGGED
)
1187 if (mb
.mb_threaded
? mp
== NULL
: mp
>= &message
[msgCount
])
1188 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
1189 mb
.mb_threaded
? mp
!= NULL
:
1190 mp
< &message
[msgCount
];
1191 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++)
1192 if ((mp
->m_flag
& (MREAD
|avoid
)) == 0)
1194 if (mb
.mb_threaded
? mp
!= NULL
: mp
< &message
[msgCount
])
1195 mdot
= mp
- &message
[0] + 1;
1196 else if (value("showlast")) {
1197 if (mb
.mb_threaded
) {
1198 for (mp
= this_in_thread(threadroot
, -1); mp
;
1199 mp
= prev_in_thread(mp
))
1200 if ((mp
->m_flag
& avoid
) == 0)
1202 mdot
= mp
? mp
- &message
[0] + 1 : msgCount
;
1204 for (mp
= &message
[msgCount
-1]; mp
>= &message
[0]; mp
--)
1205 if ((mp
->m_flag
& avoid
) == 0)
1207 mdot
= mp
>= &message
[0] ? mp
-&message
[0]+1 : msgCount
;
1209 } else if (mb
.mb_threaded
) {
1210 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
1211 if ((mp
->m_flag
& avoid
) == 0)
1213 mdot
= mp
? mp
- &message
[0] + 1 : 1;
1215 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
1216 if ((mp
->m_flag
& avoid
) == 0)
1218 mdot
= mp
< &message
[msgCount
] ? mp
-&message
[0]+1 : 1;
1224 initbox(const char *name
)
1229 if (mb
.mb_type
!= MB_VOID
)
1230 (void)n_strlcpy(prevfile
, mailname
, MAXPATHLEN
);
1231 _update_mailname(name
!= mailname
? name
: NULL
);
1232 if ((mb
.mb_otf
= Ftemp(&tempMesg
, "tmpbox", "w", 0600, 0)) == NULL
) {
1233 perror(tr(87, "temporary mail message file"));
1236 (void)fcntl(fileno(mb
.mb_otf
), F_SETFD
, FD_CLOEXEC
);
1237 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", &dummy
)) == NULL
) {
1238 perror(tr(87, "temporary mail message file"));
1241 (void)fcntl(fileno(mb
.mb_itf
), F_SETFD
, FD_CLOEXEC
);
1251 if (mb
.mb_sorted
!= NULL
) {
1253 mb
.mb_sorted
= NULL
;
1256 mb
.mb_flags
= MB_NOFLAGS
;
1260 did_print_dot
= FAL0
;
1263 #ifdef HAVE_DOCSTRINGS
1265 print_comm_docstr(char const *comm
)
1268 struct cmd_ghost
*cg
;
1269 struct cmd
const *cp
;
1271 /* Ghosts take precedence */
1272 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
1273 if (strcmp(comm
, cg
->name
) == 0) {
1274 printf("%s -> <%s>\n", comm
, cg
->cmd
.s
);
1279 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
) {
1280 if (cp
->func
== &ccmdnotsupp
)
1282 if (strcmp(comm
, cp
->name
) == 0)
1283 printf("%s: %s\n", comm
, tr(cp
->docid
, cp
->doc
));
1284 else if (is_prefix(comm
, cp
->name
))
1285 printf("%s (%s): %s\n", comm
, cp
->name
, tr(cp
->docid
, cp
->doc
));
1296 /* vim:set fenc=utf-8:s-it-mode (TODO only partial true) */