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 - 2014 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 */
72 static int _lex_inithdr
; /* am printing startup headers */
74 /* Update mailname (if name != NULL) and displayname, return wether displayname
75 * was large enough to swallow mailname */
76 static bool_t
_update_mailname(char const *name
);
77 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_{pre,suf}fix() into one fun! */
78 SINLINE
size_t __narrow_prefix(char const *cp
, size_t maxl
);
79 SINLINE
size_t __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
);
82 /* Isolate the command from the arguments */
83 static char * _lex_isolate(char const *comm
);
85 /* Get first-fit, or NULL */
86 static struct cmd
const * _lex(char const *comm
);
88 /* Command ghost handling */
89 static int _ghost(void *v
);
90 static int _unghost(void *v
);
92 /* Print a list of all commands */
93 static int _pcmdlist(void *v
);
94 static int __pcmd_cmp(void const *s1
, void const *s2
);
96 /* Print the binaries compiled-in features */
97 static int _features(void *v
);
99 /* Print the binaries version number */
100 static int _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 */
109 static struct cmd
const _cmd_tab
[] = {
113 #ifdef HAVE_C90AMEND1
115 __narrow_prefix(char const *cp
, size_t maxl
)
121 for (err
= ok
= i
= 0; i
< maxl
;) {
122 int ml
= mblen(cp
, maxl
- i
);
123 if (ml
< 0) { /* XXX _narrow_prefix(): mblen() error; action? */
124 (void)mblen(NULL
, 0);
142 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
148 for (err
= ok
= i
= 0; cpl
> maxl
|| err
;) {
149 int ml
= mblen(cp
, cpl
);
150 if (ml
< 0) { /* XXX _narrow_suffix(): mblen() error; action? */
151 (void)mblen(NULL
, 0);
168 #endif /* HAVE_C90AMEND1 */
171 _update_mailname(char const *name
)
173 char tbuf
[PATH_MAX
], *mailp
, *dispp
;
178 /* Don't realpath(3) if it's only an update request */
181 enum protocol p
= which_protocol(name
);
182 if (p
== PROTO_FILE
|| p
== PROTO_MAILDIR
) {
183 if (realpath(name
, mailname
) == NULL
) {
184 fprintf(stderr
, tr(151, "Can't canonicalize `%s'\n"), name
);
191 n_strlcpy(mailname
, name
, sizeof(mailname
));
197 /* Don't display an absolute path but "+FOLDER" if under *folder* */
198 if (getfold(tbuf
, sizeof tbuf
)) {
200 if (i
< sizeof(tbuf
) - 1)
202 if (strncmp(tbuf
, mailp
, i
) == 0) {
208 /* We want to see the name of the folder .. on the screen */
210 if (i
< sizeof(displayname
) - 1)
211 memcpy(dispp
, mailp
, i
+ 1);
214 /* Avoid disrupting multibyte sequences (if possible) */
215 #ifndef HAVE_C90AMEND1
216 j
= sizeof(displayname
) / 3 - 1;
217 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
219 j
= __narrow_prefix(mailp
, sizeof(displayname
) / 3);
220 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
221 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
223 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
224 (int)j
, mailp
, mailp
+ i
);
231 _lex_isolate(char const *comm
)
234 while (*comm
!= '\0' &&
235 strchr("~|?&% \t0123456789$^.:/-+*'\",;(`", *comm
) == NULL
)
238 return UNCONST(comm
);
241 static struct cmd
const *
242 _lex(char const *comm
) /* TODO **command hashtable**! linear list search!!! */
244 struct cmd
const *cp
;
247 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
)
248 if (*comm
== *cp
->name
&& is_prefix(comm
, cp
->name
))
259 char const **argv
= (char const **)v
;
260 struct cmd_ghost
*lcg
, *cg
;
266 printf(tr(144, "Command ghosts are:\n"));
267 for (nl
= 0, cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
) {
268 cl
= strlen(cg
->name
) + 5 + cg
->cmd
.l
+ 3;
270 if (UICMP(z
, nl
, >=, scrnwidth
)) {
274 printf((cg
->next
!= NULL
? "%s -> <%s>, " : "%s -> <%s>\n"),
275 cg
->name
, cg
->cmd
.s
);
281 /* Request to add new ghost */
282 if (argv
[1] == NULL
|| argv
[1][0] == '\0' || argv
[2] != NULL
) {
283 fprintf(stderr
, tr(159, "Usage: %s\n"),
284 tr(425, "Define a <ghost> of <command>, or list all ghosts"));
289 /* Check that we can deal with this one */
290 if (argv
[0] == _lex_isolate(argv
[0])) {
291 fprintf(stderr
, tr(151, "Can't canonicalize `%s'\n"), argv
[0]);
296 /* Always recreate */
297 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
298 if (!strcmp(cg
->name
, argv
[0])) {
300 lcg
->next
= cg
->next
;
302 _cmd_ghosts
= cg
->next
;
308 nl
= strlen(argv
[0]) + 1;
309 cl
= strlen(argv
[1]) + 1;
310 cg
= smalloc(sizeof(*cg
) - VFIELD_SIZEOF(struct cmd_ghost
, name
) + nl
+ cl
);
311 cg
->next
= _cmd_ghosts
;
312 memcpy(cg
->name
, argv
[0], nl
);
313 cg
->cmd
.s
= cg
->name
+ nl
;
315 memcpy(cg
->cmd
.s
, argv
[1], cl
);
327 char const **argv
= v
, *cp
;
328 struct cmd_ghost
*lcg
, *cg
;
331 while ((cp
= *argv
++) != NULL
) {
332 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
333 if (!strcmp(cg
->name
, cp
)) {
335 lcg
->next
= cg
->next
;
337 _cmd_ghosts
= cg
->next
;
341 fprintf(stderr
, tr(91, "Unknown command: `%s'\n"), cp
);
351 __pcmd_cmp(void const *s1
, void const *s2
)
353 struct cmd
const * const *c1
= s1
, * const *c2
= s2
;
357 rv
= strcmp((*c1
)->name
, (*c2
)->name
);
365 struct cmd
const **cpa
, *cp
, **cursor
;
370 for (i
= 0; _cmd_tab
[i
].name
!= NULL
; ++i
)
373 cpa
= ac_alloc(sizeof(cp
) * i
);
375 for (i
= 0; (cp
= _cmd_tab
+ i
)->name
!= NULL
; ++i
)
379 qsort(cpa
, i
, sizeof(cp
), &__pcmd_cmp
);
381 printf(tr(14, "Commands are:\n"));
382 for (i
= 0, cursor
= cpa
; (cp
= *cursor
++) != NULL
;) {
384 if (cp
->func
== &c_cmdnotsupp
)
386 j
= strlen(cp
->name
) + 2;
391 printf((*cursor
!= NULL
? "%s, " : "%s\n"), cp
->name
);
404 printf(tr(523, "Features: %s\n"), features
);
414 printf(tr(111, "Version %s\n"), version
);
422 sighandler_type old_action
;
424 NYD_X
; /* Signal handler */
426 old_action
= safe_signal(s
, SIG_DFL
);
430 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
432 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
433 safe_signal(s
, old_action
);
434 if (_reset_on_stop
) {
443 NYD_X
; /* Signal handler */
450 setfile(char const *name
, int nmail
) /* TODO oh my god */
457 int rv
, i
, compressed
= 0, omsgCount
= 0;
464 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
465 * necessary to make a mailbox accessible by a different user, and if that
466 * has happened, let's just let the usual file perms decide */
467 who
= (name
[1] != '\0') ? name
+ 1 : myname
;
468 isedit
= (*name
!= '%' && ((sh
= get_shortcut(name
)) == NULL
||
469 *sh
->sh_long
!= '%'));
470 if ((name
= expand(name
)) == NULL
)
473 switch (which_protocol(name
)) {
477 rv
= maildir_setfile(name
, nmail
, isedit
);
482 rv
= pop3_setfile(name
, nmail
, isedit
);
488 if (nmail
&& mb
.mb_type
== MB_CACHE
)
491 rv
= imap_setfile(name
, nmail
, isedit
);
495 fprintf(stderr
, tr(217, "Cannot handle protocol: %s\n"), name
);
499 /* FIXME this FILE leaks if quit()->edstop() reset()s! This entire code
500 * FIXME here is total crap, below we open(2) the same name again just to
501 * FIXME close it right away etc. The normal thing would be to (1) finalize
502 * FIXME the current box and (2) open the new box; yet, since (2) may fail
503 * FIXME we terribly need our VOID box to make this logic order possible! */
504 if ((ibuf
= Zopen(name
, "r", &compressed
)) == NULL
) {
505 if ((!isedit
&& errno
== ENOENT
) || nmail
) {
514 if (fstat(fileno(ibuf
), &stb
) < 0) {
521 if (S_ISDIR(stb
.st_mode
)) {
527 } else if (S_ISREG(stb
.st_mode
)) {
537 /* Looks like all will be well. We must now relinquish our hold on the
538 * current set of stuff. Must hold signals while we are reading the new
539 * file, else we will ruin the message[] data structure */
541 hold_sigs(); /* TODO note on this one in quit.c:quit() */
542 if (shudclob
&& !nmail
)
545 if (!nmail
&& mb
.mb_sock
.s_fd
>= 0)
549 /* Copy the messages into /tmp and set pointers */
550 flp
.l_type
= F_RDLCK
;
552 flp
.l_whence
= SEEK_SET
;
554 mb
.mb_type
= MB_FILE
;
555 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
| MB_EDIT
;
556 mb
.mb_compressed
= compressed
;
558 if (compressed
& 0200)
561 if ((i
= open(name
, O_WRONLY
)) < 0)
581 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) == -1) {
582 perror("Unable to lock mailbox");
587 fseek(mb
.mb_otf
, 0L, SEEK_END
);
588 fseek(ibuf
, mailsize
, SEEK_SET
);
590 omsgCount
= msgCount
;
592 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) == -1) {
597 mailsize
= fsize(ibuf
);
599 if (nmail
&& UICMP(z
, mailsize
, <=, offset
)) {
603 setptr(ibuf
, offset
);
605 if (nmail
&& mb
.mb_sorted
) {
616 if ((!edit
|| nmail
) && msgCount
== 0) {
619 if (!ok_blook(emptystart
))
621 fprintf(stderr
, tr(88, "No mail for %s\n"), who
);
627 newmailinfo(omsgCount
);
641 newmailinfo(int omsgCount
)
646 for (i
= 0; i
< omsgCount
; i
++)
647 message
[i
].m_flag
&= ~MNEWEST
;
648 if (msgCount
> omsgCount
) {
649 for (i
= omsgCount
; i
< msgCount
; i
++)
650 message
[i
].m_flag
|= MNEWEST
;
651 printf(tr(158, "New mail has arrived.\n"));
652 if (msgCount
- omsgCount
== 1)
653 printf(tr(214, "Loaded 1 new message.\n"));
655 printf(tr(215, "Loaded %d new messages.\n"), msgCount
- omsgCount
);
657 printf(tr(224, "Loaded %d messages.\n"), msgCount
);
658 callhook(mailname
, 1);
660 if (ok_blook(header
))
661 print_headers(omsgCount
+ 1, msgCount
);
674 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
675 safe_signal(SIGINT
, onintr
);
676 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
677 safe_signal(SIGHUP
, hangup
);
678 /* TODO We do a lot of redundant signal handling, especially
679 * TODO with the command line editor(s); try to merge this */
680 safe_signal(SIGTSTP
, stop
);
681 safe_signal(SIGTTOU
, stop
);
682 safe_signal(SIGTTIN
, stop
);
684 _oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
685 safe_signal(SIGPIPE
, _oldpipe
);
687 memset(&ev
, 0, sizeof ev
);
692 handlerstacktop
= NULL
;
694 if (!sourcing
&& (options
& OPT_INTERACTIVE
)) {
697 if ((cp
= ok_vlook(newmail
)) == NULL
)
698 cp
= ok_vlook(autoinc
); /* TODO legacy */
699 if ((options
& OPT_TTYIN
) && (cp
!= NULL
|| mb
.mb_type
== MB_IMAP
)) {
702 n
= (cp
!= NULL
&& strcmp(cp
, "noimap") && strcmp(cp
, "nopoll"));
703 if ((mb
.mb_type
== MB_FILE
&& stat(mailname
, &st
) == 0 &&
704 st
.st_size
> mailsize
) ||
706 (mb
.mb_type
== MB_IMAP
&& imap_newmail(n
) > (cp
== NULL
)) ||
708 (mb
.mb_type
== MB_MAILDIR
&& n
!= 0)) {
709 size_t odot
= PTR2SIZE(dot
- message
);
710 bool_t odid
= did_print_dot
;
712 setfile(mailname
, 1);
713 if (mb
.mb_type
!= MB_IMAP
) {
714 dot
= message
+ odot
;
715 did_print_dot
= odid
;
721 exit_status
= EXIT_OK
;
725 colour_table
= NULL
; /* XXX intermediate hack */
731 /* TODO Note: this buffer may contain a password. We should redefine
732 * TODO the code flow which has to do that */
733 if ((cp
= termios_state
.ts_linebuf
) != NULL
) {
734 termios_state
.ts_linebuf
= NULL
;
735 termios_state
.ts_linesize
= 0;
736 free(cp
); /* TODO pool give-back */
738 /* TODO Due to expand-on-tab of NCL the buffer may grow */
739 if (ev
.ev_line
.l
> LINESIZE
* 3) {
740 free(ev
.ev_line
.s
); /* TODO pool! but what? */
746 /* Read a line of commands and handle end of file specially */
748 n
= readline_input(NULL
, TRU1
, &ev
.ev_line
.s
, &ev
.ev_line
.l
,
759 if ((options
& OPT_INTERACTIVE
) && ok_blook(ignoreeof
)) {
760 printf(tr(89, "Use `quit' to quit.\n"));
769 if (exit_status
!= EXIT_OK
&& (options
& OPT_BATCH_FLAG
) &&
770 ok_blook(batch_exit_on_error
))
772 if (!sourcing
&& (options
& OPT_INTERACTIVE
)) {
773 if (ev
.ev_new_content
!= NULL
)
775 if (ev
.ev_add_history
)
776 tty_addhist(ev
.ev_line
.s
);
780 if (ev
.ev_line
.s
!= NULL
)
788 execute(char *linebuf
, int contxt
, size_t linesize
) /* XXX LEGACY */
792 struct colour_table
*ct_save
;
797 /* TODO Maybe recursion from within collect.c! As long as we don't have
798 * TODO a value carrier that transports the entire state of a recursion
799 * TODO we need to save away also the colour table */
801 ct_save
= colour_table
;
805 memset(&ev
, 0, sizeof ev
);
806 ev
.ev_line
.s
= linebuf
;
807 ev
.ev_line
.l
= linesize
;
808 ev
.ev_is_recursive
= (contxt
!= 0);
812 colour_table
= ct_save
;
819 evaluate(struct eval_ctx
*evp
)
822 char _wordbuf
[2], *arglist
[MAXARGC
], *cp
, *word
;
823 struct cmd_ghost
*cg
= NULL
;
824 struct cmd
const *com
= NULL
;
825 int muvec
[2], c
, e
= 1;
828 line
= evp
->ev_line
; /* XXX don't change original (buffer pointer) */
829 evp
->ev_add_history
= FAL0
;
830 evp
->ev_new_content
= NULL
;
832 /* Command ghosts that refer to shell commands or macro expansion restart */
835 /* Strip the white space away from the beginning of the command */
836 for (cp
= line
.s
; whitechar(*cp
); ++cp
)
838 line
.l
-= PTR2SIZE(cp
- line
.s
);
840 /* Ignore comments */
844 /* Handle ! differently to get the correct lexical conventions */
847 fprintf(stderr
, tr(90, "Can't `!' while sourcing\n"));
851 evp
->ev_add_history
= TRU1
;
855 /* Isolate the actual command; since it may not necessarily be
856 * separated from the arguments (as in `p1') we need to duplicate it to
857 * be able to create a NUL terminated version.
858 * We must be aware of several special one letter commands here */
860 if ((cp
= _lex_isolate(cp
)) == arglist
[0] &&
861 (*cp
== '|' || *cp
== '~' || *cp
== '?'))
863 c
= (int)PTR2SIZE(cp
- arglist
[0]);
865 word
= UICMP(z
, c
, <, sizeof _wordbuf
) ? _wordbuf
: salloc(c
+ 1);
866 memcpy(word
, arglist
[0], c
);
869 /* Look up the command; if not found, bitch.
870 * Normally, a blank command would map to the first command in the
871 * table; while sourcing, however, we ignore blank lines to eliminate
872 * confusion; act just the same for ghosts */
874 if (sourcing
|| cg
!= NULL
)
880 /* If this is the first evaluation, check command ghosts */
882 /* TODO relink list head, so it's sorted on usage over time?
883 * TODO in fact, there should be one hashmap over all commands and ghosts
884 * TODO so that the lookup could be made much more efficient than it is
885 * TODO now (two adjacent list searches! */
886 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
887 if (!strcmp(word
, cg
->name
)) {
889 size_t i
= cg
->cmd
.l
;
890 line
.s
= salloc(i
+ 1 + line
.l
+1);
891 memcpy(line
.s
, cg
->cmd
.s
, i
);
893 memcpy(line
.s
+ i
, cp
, line
.l
);
894 line
.s
[i
+= line
.l
] = '\0';
904 if ((com
= _lex(word
)) == NULL
|| com
->func
== &c_cmdnotsupp
) {
905 fprintf(stderr
, tr(91, "Unknown command: `%s'\n"), word
);
913 /* See if we should execute the command -- if a conditional we always
914 * execute it, otherwise, check the state of cond */
916 if (!(com
->argtype
& ARG_F
)) {
917 if (condstack_isskip())
921 /* Process the arguments to the command, depending on the type he expects.
922 * Default to an error.
923 * If we are sourcing an interactive command, it's an error */
924 if ((options
& OPT_SENDMODE
) && !(com
->argtype
& ARG_M
)) {
925 fprintf(stderr
, tr(92, "May not execute `%s' while sending\n"),
929 if (sourcing
&& (com
->argtype
& ARG_I
)) {
930 fprintf(stderr
, tr(93, "May not execute `%s' while sourcing\n"),
934 if (!(mb
.mb_perm
& MB_DELE
) && (com
->argtype
& ARG_W
)) {
935 fprintf(stderr
, tr(94, "May not execute `%s' -- "
936 "message file is read only\n"), com
->name
);
939 if (evp
->ev_is_recursive
&& (com
->argtype
& ARG_R
)) {
940 fprintf(stderr
, tr(95, "Cannot recursively invoke `%s'\n"), com
->name
);
943 if (mb
.mb_type
== MB_VOID
&& (com
->argtype
& ARG_A
)) {
944 fprintf(stderr
, tr(257, "Cannot execute `%s' without active mailbox\n"),
949 if (com
->argtype
& ARG_V
)
950 temporary_arg_v_store
= NULL
;
952 switch (com
->argtype
& ARG_ARGMASK
) {
954 /* Message list defaulting to nearest forward legal message */
957 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
960 *_msgvec
= first(com
->msgflag
, com
->msgmask
);
966 printf(tr(97, "No applicable messages\n"));
969 e
= (*com
->func
)(_msgvec
);
973 /* Message list with no defaults, but no error if none exist */
976 fprintf(stderr
, tr(96, "Illegal use of `message list'\n"));
979 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
981 e
= (*com
->func
)(_msgvec
);
985 /* Just the straight string, with leading blanks removed */
986 while (whitechar(*cp
))
988 e
= (*com
->func
)(cp
);
993 /* A vector of strings, in shell style */
994 if ((c
= getrawlist(cp
, line
.l
, arglist
, NELEM(arglist
),
995 ((com
->argtype
& ARG_ARGMASK
) == ARG_ECHOLIST
))) < 0)
997 if (c
< com
->minargs
) {
998 fprintf(stderr
, tr(99, "`%s' requires at least %d arg(s)\n"),
999 com
->name
, com
->minargs
);
1002 if (c
> com
->maxargs
) {
1003 fprintf(stderr
, tr(100, "`%s' takes no more than %d arg(s)\n"),
1004 com
->name
, com
->maxargs
);
1007 e
= (*com
->func
)(arglist
);
1011 /* Just the constant zero, for exiting, eg. */
1012 e
= (*com
->func
)(0);
1016 panic(tr(101, "Unknown argument type"));
1019 if (e
== 0 && (com
->argtype
& ARG_V
) &&
1020 (cp
= temporary_arg_v_store
) != NULL
) {
1021 temporary_arg_v_store
= NULL
;
1022 evp
->ev_new_content
= cp
;
1025 if (!(com
->argtype
& ARG_H
) && !list_saw_numbers
)
1026 evp
->ev_add_history
= TRU1
;
1029 /* Exit the current source file on error */
1030 if ((exec_last_comm_error
= (e
!= 0))) {
1031 if (e
< 0 || loading
) {
1041 if ((com
->argtype
& ARG_P
) && ok_blook(autoprint
))
1043 muvec
[0] = (int)PTR2SIZE(dot
- message
+ 1);
1047 if (!sourcing
&& !inhook
&& (com
->argtype
& ARG_T
) == 0)
1050 exec_last_comm_error
= 0;
1064 _msgvec
= scalloc(sz
+ 1, sizeof *_msgvec
);
1071 NYD_X
; /* Signal handler */
1073 if (handlerstacktop
!= NULL
) {
1077 safe_signal(SIGINT
, onintr
);
1085 termios_state_reset();
1092 if (interrupts
!= 1)
1093 fprintf(stderr
, tr(102, "Interrupt\n"));
1094 safe_signal(SIGPIPE
, _oldpipe
);
1099 announce(int printheaders
)
1104 mdot
= newfileinfo();
1107 dot
= &message
[mdot
- 1];
1108 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
1120 int u
, n
, mdot
, d
, s
, hidden
, moved
;
1123 if (mb
.mb_type
== MB_VOID
) {
1129 s
= d
= hidden
= moved
=0;
1130 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
1131 if (mp
->m_flag
& MNEW
)
1133 if ((mp
->m_flag
& MREAD
) == 0)
1135 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
1137 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
1139 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
1141 if (mp
->m_flag
& MHIDDEN
)
1145 /* If displayname gets truncated the user effectively has no option to see
1146 * the full pathname of the mailbox, so print it at least for '? fi' */
1147 printf(tr(103, "\"%s\": "),
1148 (_update_mailname(NULL
) ? displayname
: mailname
));
1150 printf(tr(104, "1 message"));
1152 printf(tr(105, "%d messages"), msgCount
);
1154 printf(tr(106, " %d new"), n
);
1156 printf(tr(107, " %d unread"), u
);
1158 printf(tr(108, " %d deleted"), d
);
1160 printf(tr(109, " %d saved"), s
);
1162 printf(tr(136, " %d moved"), moved
);
1164 printf(tr(139, " %d hidden"), hidden
);
1165 if (mb
.mb_type
== MB_CACHE
)
1166 printf(" [Disconnected]");
1167 else if (mb
.mb_perm
== 0)
1168 printf(tr(110, " [Read only]"));
1181 enum mflag avoid
= MHIDDEN
| MDELETED
;
1185 if (ok_blook(autothread
))
1187 else if ((cp
= ok_vlook(autosort
)) != NULL
) {
1189 mb
.mb_sorted
= sstrdup(cp
);
1193 if (mb
.mb_type
== MB_VOID
) {
1199 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1200 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
1203 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
1204 if (mb
.mb_threaded
) {
1205 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1206 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
1209 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1210 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
1215 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
1216 if (mb
.mb_threaded
) {
1217 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1218 if (mp
->m_flag
& MFLAGGED
)
1221 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1222 if (mp
->m_flag
& MFLAGGED
)
1227 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
1228 if (mb
.mb_threaded
) {
1229 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1230 if (!(mp
->m_flag
& (MREAD
| avoid
)))
1233 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1234 if (!(mp
->m_flag
& (MREAD
| avoid
)))
1239 if ((mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
1240 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
1241 else if (ok_blook(showlast
)) {
1242 if (mb
.mb_threaded
) {
1243 for (mp
= this_in_thread(threadroot
, -1); mp
;
1244 mp
= prev_in_thread(mp
))
1245 if (!(mp
->m_flag
& avoid
))
1247 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
1249 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
1250 if (!(mp
->m_flag
& avoid
))
1252 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
1254 } else if (mb
.mb_threaded
) {
1255 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
1256 if (!(mp
->m_flag
& avoid
))
1258 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1260 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1261 if (!(mp
->m_flag
& avoid
))
1263 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
1264 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1272 initbox(char const *name
)
1277 if (mb
.mb_type
!= MB_VOID
)
1278 n_strlcpy(prevfile
, mailname
, PATH_MAX
);
1280 _update_mailname(name
!= mailname
? name
: NULL
);
1282 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpbox", OF_WRONLY
| OF_HOLDSIGS
, 0600)) ==
1284 perror(tr(87, "temporary mail message file"));
1287 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
1288 perror(tr(87, "temporary mail message file"));
1291 Ftmp_release(&tempMesg
);
1295 if (mb
.mb_sorted
!= NULL
) {
1297 mb
.mb_sorted
= NULL
;
1300 mb
.mb_flags
= MB_NOFLAGS
;
1304 did_print_dot
= FAL0
;
1308 #ifdef HAVE_DOCSTRINGS
1310 print_comm_docstr(char const *comm
)
1313 struct cmd_ghost
*cg
;
1314 struct cmd
const *cp
;
1317 /* Ghosts take precedence */
1318 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
1319 if (!strcmp(comm
, cg
->name
)) {
1320 printf("%s -> <%s>\n", comm
, cg
->cmd
.s
);
1325 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
) {
1326 if (cp
->func
== &c_cmdnotsupp
)
1328 if (!strcmp(comm
, cp
->name
))
1329 printf("%s: %s\n", comm
, tr(cp
->docid
, cp
->doc
));
1330 else if (is_prefix(comm
, cp
->name
))
1331 printf("%s (%s): %s\n", comm
, cp
->name
, tr(cp
->docid
, cp
->doc
));
1343 /* vim:set fenc=utf-8:s-it-mode */