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 - 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 char const *doc
; /* One line doc for command */
56 /* Yechh, can't initialize unions */
57 #define minargs msgflag /* Minimum argcount for RAWLIST */
58 #define maxargs msgmask /* Max argcount for RAWLIST */
61 struct cmd_ghost
*next
;
62 struct str cmd
; /* Data follows after .name */
63 char name
[VFIELD_SIZE(sizeof(size_t))];
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
);
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 _ghost(void *v
);
88 static int _unghost(void *v
);
90 /* Print a list of all commands */
91 static int _pcmdlist(void *v
);
92 static int __pcmd_cmp(void const *s1
, void const *s2
);
94 /* Print the binaries compiled-in features */
95 static int _features(void *v
);
97 /* Print the binaries version number */
98 static int _version(void *v
);
100 /* When we wake up after ^Z, reprint the prompt */
101 static void stop(int s
);
103 /* Branch here on hangup signal and simulate "exit" */
104 static void hangup(int s
);
106 /* List of all commands */
107 static struct cmd
const _cmd_tab
[] = {
111 #ifdef HAVE_C90AMEND1
113 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
119 for (err
= ok
= i
= 0; cpl
> maxl
|| err
;) {
120 int ml
= mblen(cp
, cpl
);
121 if (ml
< 0) { /* XXX _narrow_suffix(): mblen() error; action? */
122 (void)mblen(NULL
, 0);
139 #endif /* HAVE_C90AMEND1 */
142 _update_mailname(char const *name
)
144 char tbuf
[PATH_MAX
], *mailp
, *dispp
;
149 /* Don't realpath(3) if it's only an update request */
152 enum protocol p
= which_protocol(name
);
153 if (p
== PROTO_FILE
|| p
== PROTO_MAILDIR
) {
154 if (realpath(name
, mailname
) == NULL
) {
155 fprintf(stderr
, _("Can't canonicalize `%s'\n"), name
);
162 n_strlcpy(mailname
, name
, sizeof(mailname
));
168 /* Don't display an absolute path but "+FOLDER" if under *folder* */
169 if (getfold(tbuf
, sizeof tbuf
)) {
171 if (i
< sizeof(tbuf
) -1)
173 if (!strncmp(tbuf
, mailp
, i
)) {
179 /* We want to see the name of the folder .. on the screen */
181 if (i
< sizeof(displayname
) -1)
182 memcpy(dispp
, mailp
, i
+1);
185 /* Avoid disrupting multibyte sequences (if possible) */
186 #ifndef HAVE_C90AMEND1
187 j
= sizeof(displayname
) / 3 - 1;
188 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
190 j
= field_detect_clip(sizeof(displayname
) / 3, mailp
, i
);
191 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
192 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
194 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
195 (int)j
, mailp
, mailp
+ i
);
202 _lex_isolate(char const *comm
)
205 while (*comm
!= '\0' &&
206 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm
) == NULL
)
209 return UNCONST(comm
);
212 static struct cmd
const *
213 _lex(char const *comm
) /* TODO **command hashtable**! linear list search!!! */
215 struct cmd
const *cp
;
218 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
)
219 if (*comm
== *cp
->name
&& is_prefix(comm
, cp
->name
))
230 char const **argv
= v
;
231 struct cmd_ghost
*lcg
, *cg
;
237 printf(_("Command ghosts are:\n"));
238 for (nl
= 0, cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
) {
239 cl
= strlen(cg
->name
) + 5 + cg
->cmd
.l
+ 3;
241 if (UICMP(z
, nl
, >=, scrnwidth
)) {
245 printf((cg
->next
!= NULL
? "%s -> <%s>, " : "%s -> <%s>\n"),
246 cg
->name
, cg
->cmd
.s
);
252 /* Request to add new ghost */
253 if (argv
[1] == NULL
|| argv
[1][0] == '\0' || argv
[2] != NULL
) {
254 fprintf(stderr
, _("Usage: %s\n"),
255 _("Define a <ghost> of <command>, or list all ghosts"));
260 /* Check that we can deal with this one */
261 if (*argv
[0] == '\0' || *_lex_isolate(argv
[0]) != '\0') {
262 fprintf(stderr
, _("Can't canonicalize `%s'\n"), argv
[0]);
267 /* Always recreate */
268 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
269 if (!strcmp(cg
->name
, argv
[0])) {
271 lcg
->next
= cg
->next
;
273 _cmd_ghosts
= cg
->next
;
279 nl
= strlen(argv
[0]) +1;
280 cl
= strlen(argv
[1]) +1;
281 cg
= smalloc(sizeof(*cg
) - VFIELD_SIZEOF(struct cmd_ghost
, name
) + nl
+ cl
);
282 cg
->next
= _cmd_ghosts
;
283 memcpy(cg
->name
, argv
[0], nl
);
284 cg
->cmd
.s
= cg
->name
+ nl
;
286 memcpy(cg
->cmd
.s
, argv
[1], cl
);
298 char const **argv
= v
, *cp
;
299 struct cmd_ghost
*lcg
, *cg
;
302 while ((cp
= *argv
++) != NULL
) {
303 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
304 if (!strcmp(cg
->name
, cp
)) {
306 lcg
->next
= cg
->next
;
308 _cmd_ghosts
= cg
->next
;
312 fprintf(stderr
, _("Unknown command: `%s'\n"), cp
);
322 __pcmd_cmp(void const *s1
, void const *s2
)
324 struct cmd
const * const *c1
= s1
, * const *c2
= s2
;
328 rv
= strcmp((*c1
)->name
, (*c2
)->name
);
336 struct cmd
const **cpa
, *cp
, **cursor
;
341 for (i
= 0; _cmd_tab
[i
].name
!= NULL
; ++i
)
344 cpa
= ac_alloc(sizeof(cp
) * i
);
346 for (i
= 0; (cp
= _cmd_tab
+ i
)->name
!= NULL
; ++i
)
350 qsort(cpa
, i
, sizeof(cp
), &__pcmd_cmp
);
352 printf(_("Commands are:\n"));
353 for (i
= 0, cursor
= cpa
; (cp
= *cursor
++) != NULL
;) {
355 if (cp
->func
== &c_cmdnotsupp
)
357 j
= strlen(cp
->name
) + 2;
362 printf((*cursor
!= NULL
? "%s, " : "%s\n"), cp
->name
);
375 printf(_("Features: %s\n"), features
);
385 printf(_("Version %s\n"), version
);
393 sighandler_type old_action
;
395 NYD_X
; /* Signal handler */
397 old_action
= safe_signal(s
, SIG_DFL
);
401 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
403 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
404 safe_signal(s
, old_action
);
405 if (_reset_on_stop
) {
414 NYD_X
; /* Signal handler */
421 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
428 int rv
, i
, compressed
= 0, omsgCount
= 0;
434 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
435 * necessary to make a mailbox accessible by a different user, and if that
436 * has happened, let's just let the usual file perms decide */
437 who
= (name
[1] != '\0') ? name
+ 1 : myname
;
440 if (name
[0] == '%' ||
441 ((sh
= get_shortcut(name
)) != NULL
&& *sh
->sh_long
== '%'))
442 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
444 if (name
[0] == '%' && (name
[1] == '\0' || name
[1] == ':'))
446 else if ((sh
= get_shortcut(name
)) != NULL
) {
447 char const *x
= sh
->sh_long
;
449 if (x
[0] == '%' && (x
[1] == '\0' || x
[1] == ':'))
453 if ((name
= expand(name
)) == NULL
)
456 switch (which_protocol(name
)) {
460 rv
= maildir_setfile(name
, fm
);
465 rv
= pop3_setfile(name
, fm
);
471 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_CACHE
)
474 rv
= imap_setfile(name
, fm
);
478 fprintf(stderr
, _("Cannot handle protocol: %s\n"), name
);
482 /* FIXME this FILE leaks if quit()->edstop() reset()s! This entire code
483 * FIXME here is total crap, below we open(2) the same name again just to
484 * FIXME close it right away etc. The normal thing would be to (1) finalize
485 * FIXME the current box and (2) open the new box; yet, since (2) may fail
486 * FIXME we terribly need our VOID box to make this logic order possible! */
487 if ((ibuf
= Zopen(name
, "r", &compressed
)) == NULL
) {
488 if (((fm
& FEDIT_SYSBOX
) && errno
== ENOENT
) || (fm
& FEDIT_NEWMAIL
)) {
489 if (fm
& FEDIT_NEWMAIL
)
497 if (fstat(fileno(ibuf
), &stb
) == -1) {
498 if (fm
& FEDIT_NEWMAIL
)
504 if (S_ISREG(stb
.st_mode
) ||
505 ((options
& OPT_BATCH_FLAG
) && !strcmp(name
, "/dev/null"))) {
508 if (fm
& FEDIT_NEWMAIL
)
510 errno
= S_ISDIR(stb
.st_mode
) ? EISDIR
: EINVAL
;
515 /* Looks like all will be well. We must now relinquish our hold on the
516 * current set of stuff. Must hold signals while we are reading the new
517 * file, else we will ruin the message[] data structure */
519 hold_sigs(); /* TODO note on this one in quit.c:quit() */
520 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
))
523 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
524 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
527 /* Copy the messages into /tmp and set pointers */
528 flp
.l_type
= F_RDLCK
;
530 flp
.l_whence
= SEEK_SET
;
531 if (!(fm
& FEDIT_NEWMAIL
)) {
532 mb
.mb_type
= MB_FILE
;
533 mb
.mb_perm
= (((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
))
534 ? 0 : MB_DELE
| MB_EDIT
);
535 mb
.mb_compressed
= compressed
;
537 if (compressed
& 0200)
540 if ((i
= open(name
, O_WRONLY
)) == -1)
556 edit
= !(fm
& FEDIT_SYSBOX
);
560 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) == -1) {/*TODO dotlock!*/
561 perror("Unable to lock mailbox");
565 } else /* FEDIT_NEWMAIL */{
566 fseek(mb
.mb_otf
, 0L, SEEK_END
);
567 fseek(ibuf
, mailsize
, SEEK_SET
);
569 omsgCount
= msgCount
;
571 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) == -1) {/*TODO dotlock!*/
576 mailsize
= fsize(ibuf
);
578 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
582 setptr(ibuf
, offset
);
584 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
592 if (!(fm
& FEDIT_NEWMAIL
))
595 if ((!edit
|| (fm
& FEDIT_NEWMAIL
)) && msgCount
== 0) {
597 if (!(fm
& FEDIT_NEWMAIL
)) {
598 if (!ok_blook(emptystart
))
600 fprintf(stderr
, _("No mail for %s\n"), who
);
605 if (fm
& FEDIT_NEWMAIL
)
606 newmailinfo(omsgCount
);
620 newmailinfo(int omsgCount
)
625 for (i
= 0; i
< omsgCount
; ++i
)
626 message
[i
].m_flag
&= ~MNEWEST
;
627 if (msgCount
> omsgCount
) {
628 for (i
= omsgCount
; i
< msgCount
; ++i
)
629 message
[i
].m_flag
|= MNEWEST
;
630 printf(_("New mail has arrived.\n"));
631 if ((i
= msgCount
- omsgCount
) == 1)
632 printf(_("Loaded 1 new message.\n"));
634 printf(_("Loaded %d new messages.\n"), i
);
636 printf(_("Loaded %d messages.\n"), msgCount
);
637 callhook(mailname
, 1);
639 if (ok_blook(header
))
640 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
653 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
654 safe_signal(SIGINT
, onintr
);
655 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
656 safe_signal(SIGHUP
, hangup
);
657 /* TODO We do a lot of redundant signal handling, especially
658 * TODO with the command line editor(s); try to merge this */
659 safe_signal(SIGTSTP
, stop
);
660 safe_signal(SIGTTOU
, stop
);
661 safe_signal(SIGTTIN
, stop
);
663 _oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
664 safe_signal(SIGPIPE
, _oldpipe
);
666 memset(&ev
, 0, sizeof ev
);
671 handlerstacktop
= NULL
;
673 if (!sourcing
&& (options
& OPT_INTERACTIVE
)) {
676 cp
= ok_vlook(newmail
);
677 if ((options
& OPT_TTYIN
) && (cp
!= NULL
|| mb
.mb_type
== MB_IMAP
)) {
680 n
= (cp
!= NULL
&& strcmp(cp
, "noimap") && strcmp(cp
, "nopoll"));
681 if ((mb
.mb_type
== MB_FILE
&& !stat(mailname
, &st
) &&
682 st
.st_size
> mailsize
) ||
684 (mb
.mb_type
== MB_IMAP
&& imap_newmail(n
) > (cp
== NULL
)) ||
686 (mb
.mb_type
== MB_MAILDIR
&& n
!= 0)) {
687 size_t odot
= PTR2SIZE(dot
- message
);
688 bool_t odid
= did_print_dot
;
690 setfile(mailname
, 1);
691 if (mb
.mb_type
!= MB_IMAP
) {
692 dot
= message
+ odot
;
693 did_print_dot
= odid
;
699 exit_status
= EXIT_OK
;
703 colour_table
= NULL
; /* XXX intermediate hack */
705 if (temporary_localopts_store
!= NULL
) /* XXX intermediate hack */
706 temporary_localopts_free(); /* XXX intermediate hack */
711 /* TODO Note: this buffer may contain a password. We should redefine
712 * TODO the code flow which has to do that */
713 if ((cp
= termios_state
.ts_linebuf
) != NULL
) {
714 termios_state
.ts_linebuf
= NULL
;
715 termios_state
.ts_linesize
= 0;
716 free(cp
); /* TODO pool give-back */
718 /* TODO Due to expand-on-tab of NCL the buffer may grow */
719 if (ev
.ev_line
.l
> LINESIZE
* 3) {
720 free(ev
.ev_line
.s
); /* TODO pool! but what? */
726 /* Read a line of commands and handle end of file specially */
728 ev
.ev_line
.l
= ev
.ev_line_size
;
729 n
= readline_input(NULL
, TRU1
, &ev
.ev_line
.s
, &ev
.ev_line
.l
,
731 ev
.ev_line_size
= (ui32_t
)ev
.ev_line
.l
;
732 ev
.ev_line
.l
= (ui32_t
)n
;
742 if ((options
& OPT_INTERACTIVE
) && ok_blook(ignoreeof
)) {
743 printf(_("Use `quit' to quit.\n"));
752 if ((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)) {
753 if (exit_status
!= EXIT_OK
)
755 /* TODO *batch-exit-on-error*: sourcing and loading MUST BE FLAGS!
756 * TODO the current behaviour is suboptimal AT BEST! */
757 if (exec_last_comm_error
!= 0 && !sourcing
&& !loading
) {
758 exit_status
= EXIT_ERR
;
762 if (!sourcing
&& (options
& OPT_INTERACTIVE
)) {
763 if (ev
.ev_new_content
!= NULL
)
765 tty_addhist(ev
.ev_line
.s
, !ev
.ev_add_history
);
769 if (ev
.ev_line
.s
!= NULL
)
777 execute(char *linebuf
, int contxt
, size_t linesize
) /* XXX LEGACY */
781 struct colour_table
*ct_save
;
786 /* TODO Maybe recursion from within collect.c! As long as we don't have
787 * TODO a value carrier that transports the entire state of a recursion
788 * TODO we need to save away also the colour table */
790 ct_save
= colour_table
;
794 memset(&ev
, 0, sizeof ev
);
795 ev
.ev_line
.s
= linebuf
;
796 ev
.ev_line
.l
= linesize
;
797 ev
.ev_is_recursive
= (contxt
!= 0);
801 tty_addhist(ev
.ev_line
.s
, TRU1
);
804 colour_table
= ct_save
;
811 evaluate(struct eval_ctx
*evp
)
814 char _wordbuf
[2], *arglist
[MAXARGC
], *cp
, *word
;
815 struct cmd_ghost
*cg
= NULL
;
816 struct cmd
const *com
= NULL
;
817 int muvec
[2], c
, e
= 1;
820 line
= evp
->ev_line
; /* XXX don't change original (buffer pointer) */
821 evp
->ev_add_history
= FAL0
;
822 evp
->ev_new_content
= NULL
;
824 /* Command ghosts that refer to shell commands or macro expansion restart */
827 /* Strip the white space away from the beginning of the command */
828 for (cp
= line
.s
; whitechar(*cp
); ++cp
)
830 line
.l
-= PTR2SIZE(cp
- line
.s
);
832 /* Ignore comments */
836 /* Handle ! differently to get the correct lexical conventions */
839 fprintf(stderr
, _("Can't `!' while sourcing\n"));
843 evp
->ev_add_history
= TRU1
;
847 /* Isolate the actual command; since it may not necessarily be
848 * separated from the arguments (as in `p1') we need to duplicate it to
849 * be able to create a NUL terminated version.
850 * We must be aware of several special one letter commands here */
852 if ((cp
= _lex_isolate(cp
)) == arglist
[0] &&
853 (*cp
== '|' || *cp
== '~' || *cp
== '?'))
855 c
= (int)PTR2SIZE(cp
- arglist
[0]);
857 word
= UICMP(z
, c
, <, sizeof _wordbuf
) ? _wordbuf
: salloc(c
+1);
858 memcpy(word
, arglist
[0], c
);
861 /* Look up the command; if not found, bitch.
862 * Normally, a blank command would map to the first command in the
863 * table; while sourcing, however, we ignore blank lines to eliminate
864 * confusion; act just the same for ghosts */
866 if (sourcing
|| cg
!= NULL
)
872 /* If this is the first evaluation, check command ghosts */
874 /* TODO relink list head, so it's sorted on usage over time?
875 * TODO in fact, there should be one hashmap over all commands and ghosts
876 * TODO so that the lookup could be made much more efficient than it is
877 * TODO now (two adjacent list searches! */
878 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
879 if (!strcmp(word
, cg
->name
)) {
881 size_t i
= cg
->cmd
.l
;
882 line
.s
= salloc(i
+ 1 + line
.l
+1);
883 memcpy(line
.s
, cg
->cmd
.s
, i
);
885 memcpy(line
.s
+ i
, cp
, line
.l
);
886 line
.s
[i
+= line
.l
] = '\0';
896 if ((com
= _lex(word
)) == NULL
|| com
->func
== &c_cmdnotsupp
) {
897 fprintf(stderr
, _("Unknown command: `%s'\n"), word
);
905 /* See if we should execute the command -- if a conditional we always
906 * execute it, otherwise, check the state of cond */
908 if (!(com
->argtype
& ARG_F
) && condstack_isskip())
911 /* Process the arguments to the command, depending on the type it expects,
912 * default to error. If we're sourcing an interactive command: error */
913 if ((options
& OPT_SENDMODE
) && !(com
->argtype
& ARG_M
)) {
914 fprintf(stderr
, _("May not execute `%s' while sending\n"),
918 if (sourcing
&& (com
->argtype
& ARG_I
)) {
919 fprintf(stderr
, _("May not execute `%s' while sourcing\n"),
923 if (!(mb
.mb_perm
& MB_DELE
) && (com
->argtype
& ARG_W
)) {
924 fprintf(stderr
, _("May not execute `%s' -- "
925 "message file is read only\n"), com
->name
);
928 if (evp
->ev_is_recursive
&& (com
->argtype
& ARG_R
)) {
929 fprintf(stderr
, _("Cannot recursively invoke `%s'\n"), com
->name
);
932 if (mb
.mb_type
== MB_VOID
&& (com
->argtype
& ARG_A
)) {
933 fprintf(stderr
, _("Cannot execute `%s' without active mailbox\n"),
938 if (com
->argtype
& ARG_V
)
939 temporary_arg_v_store
= NULL
;
941 switch (com
->argtype
& ARG_ARGMASK
) {
943 /* Message list defaulting to nearest forward legal message */
946 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
949 *_msgvec
= first(com
->msgflag
, com
->msgmask
);
955 printf(_("No applicable messages\n"));
958 e
= (*com
->func
)(_msgvec
);
962 /* Message list with no defaults, but no error if none exist */
963 if (_msgvec
== NULL
) {
965 fprintf(stderr
, _("Invalid use of `message list'\n"));
968 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
970 e
= (*com
->func
)(_msgvec
);
974 /* Just the straight string, with leading blanks removed */
975 while (whitechar(*cp
))
977 e
= (*com
->func
)(cp
);
982 /* A vector of strings, in shell style */
983 if ((c
= getrawlist(cp
, line
.l
, arglist
, NELEM(arglist
),
984 ((com
->argtype
& ARG_ARGMASK
) == ARG_ECHOLIST
))) < 0)
986 if (c
< com
->minargs
) {
987 fprintf(stderr
, _("`%s' requires at least %d arg(s)\n"),
988 com
->name
, com
->minargs
);
991 if (c
> com
->maxargs
) {
992 fprintf(stderr
, _("`%s' takes no more than %d arg(s)\n"),
993 com
->name
, com
->maxargs
);
996 e
= (*com
->func
)(arglist
);
1000 /* Just the constant zero, for exiting, eg. */
1001 e
= (*com
->func
)(0);
1005 panic(_("Unknown argument type"));
1008 if (e
== 0 && (com
->argtype
& ARG_V
) &&
1009 (cp
= temporary_arg_v_store
) != NULL
) {
1010 temporary_arg_v_store
= NULL
;
1011 evp
->ev_new_content
= cp
;
1014 if (!(com
->argtype
& ARG_H
) && !list_saw_numbers
)
1015 evp
->ev_add_history
= TRU1
;
1018 /* Exit the current source file on error */
1019 if ((exec_last_comm_error
= (e
!= 0))) {
1020 if (e
< 0 || loading
) {
1030 if ((com
->argtype
& ARG_P
) && ok_blook(autoprint
))
1032 muvec
[0] = (int)PTR2SIZE(dot
- message
+ 1);
1034 c_type(muvec
); /* TODO what if error? re-eval! */
1036 if (!sourcing
&& !inhook
&& !(com
->argtype
& ARG_T
))
1039 exec_last_comm_error
= 0;
1051 if (_msgvec
!= NULL
)
1053 _msgvec
= scalloc(sz
+ 1, sizeof *_msgvec
);
1058 print_header_summary(char const *Larg
)
1060 size_t bot
, top
, i
, j
;
1064 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1065 if ((options
& (OPT_VERB
| OPT_HEADERSONLY
)) == OPT_HEADERSONLY
) {
1066 freopen("/dev/null", "w", stdout
);
1067 freopen("/dev/null", "w", stderr
);
1069 assert(_msgvec
!= NULL
);
1070 i
= (getmsglist(/*TODO make arg const */UNCONST(Larg
), _msgvec
, 0) <= 0);
1071 if (options
& OPT_HEADERSONLY
) {
1072 exit_status
= (int)i
;
1077 for (bot
= msgCount
, top
= 0, i
= 0; (j
= _msgvec
[i
]) != 0; ++i
) {
1084 bot
= 1, top
= msgCount
;
1085 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
1093 NYD_X
; /* Signal handler */
1095 if (handlerstacktop
!= NULL
) {
1099 safe_signal(SIGINT
, onintr
);
1107 termios_state_reset();
1114 if (interrupts
!= 1)
1115 fprintf(stderr
, _("Interrupt\n"));
1116 safe_signal(SIGPIPE
, _oldpipe
);
1121 announce(int printheaders
)
1126 mdot
= newfileinfo();
1129 dot
= message
+ mdot
- 1;
1130 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
1132 c_headers(vec
); /* XXX errors? */
1142 int u
, n
, mdot
, d
, s
, hidden
, moved
;
1145 if (mb
.mb_type
== MB_VOID
) {
1151 s
= d
= hidden
= moved
=0;
1152 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
1153 if (mp
->m_flag
& MNEW
)
1155 if ((mp
->m_flag
& MREAD
) == 0)
1157 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
1159 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
1161 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
1163 if (mp
->m_flag
& MHIDDEN
)
1167 /* If displayname gets truncated the user effectively has no option to see
1168 * the full pathname of the mailbox, so print it at least for '? fi' */
1169 printf(_("\"%s\": "),
1170 (_update_mailname(NULL
) ? displayname
: mailname
));
1172 printf(_("1 message"));
1174 printf(_("%d messages"), msgCount
);
1176 printf(_(" %d new"), n
);
1178 printf(_(" %d unread"), u
);
1180 printf(_(" %d deleted"), d
);
1182 printf(_(" %d saved"), s
);
1184 printf(_(" %d moved"), moved
);
1186 printf(_(" %d hidden"), hidden
);
1187 if (mb
.mb_type
== MB_CACHE
)
1188 printf(" [Disconnected]");
1189 else if (mb
.mb_perm
== 0)
1190 printf(_(" [Read only]"));
1203 enum mflag avoid
= MHIDDEN
| MDELETED
;
1207 if (ok_blook(autothread
))
1209 else if ((cp
= ok_vlook(autosort
)) != NULL
) {
1211 mb
.mb_sorted
= sstrdup(cp
);
1215 if (mb
.mb_type
== MB_VOID
) {
1221 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1222 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
1225 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
1226 if (mb
.mb_threaded
) {
1227 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1228 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
1231 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1232 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
1237 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
1238 if (mb
.mb_threaded
) {
1239 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1240 if (mp
->m_flag
& MFLAGGED
)
1243 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1244 if (mp
->m_flag
& MFLAGGED
)
1249 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
1250 if (mb
.mb_threaded
) {
1251 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1252 if (!(mp
->m_flag
& (MREAD
| avoid
)))
1255 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1256 if (!(mp
->m_flag
& (MREAD
| avoid
)))
1261 if ((mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
1262 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
1263 else if (ok_blook(showlast
)) {
1264 if (mb
.mb_threaded
) {
1265 for (mp
= this_in_thread(threadroot
, -1); mp
;
1266 mp
= prev_in_thread(mp
))
1267 if (!(mp
->m_flag
& avoid
))
1269 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
1271 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
1272 if (!(mp
->m_flag
& avoid
))
1274 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
1276 } else if (mb
.mb_threaded
) {
1277 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
1278 if (!(mp
->m_flag
& avoid
))
1280 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1282 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1283 if (!(mp
->m_flag
& avoid
))
1285 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
1286 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1294 initbox(char const *name
)
1299 if (mb
.mb_type
!= MB_VOID
)
1300 n_strlcpy(prevfile
, mailname
, PATH_MAX
);
1302 _update_mailname((name
!= mailname
) ? name
: NULL
);
1304 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpbox", OF_WRONLY
| OF_HOLDSIGS
, 0600)) ==
1306 perror(_("temporary mail message file"));
1309 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
1310 perror(_("temporary mail message file"));
1313 Ftmp_release(&tempMesg
);
1317 if (mb
.mb_sorted
!= NULL
) {
1319 mb
.mb_sorted
= NULL
;
1322 mb
.mb_flags
= MB_NOFLAGS
;
1326 did_print_dot
= FAL0
;
1330 #ifdef HAVE_DOCSTRINGS
1332 print_comm_docstr(char const *comm
)
1335 struct cmd_ghost
*cg
;
1336 struct cmd
const *cp
;
1339 /* Ghosts take precedence */
1340 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
1341 if (!strcmp(comm
, cg
->name
)) {
1342 printf("%s -> <%s>\n", comm
, cg
->cmd
.s
);
1347 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
) {
1348 if (cp
->func
== &c_cmdnotsupp
)
1350 if (!strcmp(comm
, cp
->name
))
1351 printf("%s: %s\n", comm
, V_(cp
->doc
));
1352 else if (is_prefix(comm
, cp
->name
))
1353 printf("%s (%s): %s\n", comm
, cp
->name
, V_(cp
->doc
));
1365 /* vim:set fenc=utf-8:s-it-mode */