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>.
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
42 #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(0)];
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 _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
);
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
[] = {
115 #ifdef HAVE_C90AMEND1
117 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
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);
143 #endif /* HAVE_C90AMEND1 */
146 _update_mailname(char const *name
)
148 char tbuf
[PATH_MAX
], *mailp
, *dispp
;
153 /* Don't realpath(3) if it's only an update request */
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
);
166 n_strlcpy(mailname
, name
, sizeof(mailname
));
172 /* Don't display an absolute path but "+FOLDER" if under *folder* */
173 if (getfold(tbuf
, sizeof tbuf
)) {
175 if (i
< sizeof(tbuf
) -1)
177 if (!strncmp(tbuf
, mailp
, i
)) {
183 /* We want to see the name of the folder .. on the screen */
185 if (i
< sizeof(displayname
) -1)
186 memcpy(dispp
, mailp
, i
+1);
189 /* Avoid disrupting multibyte sequences (if possible) */
190 #ifndef HAVE_C90AMEND1
191 j
= sizeof(displayname
) / 3 - 1;
192 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
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
);
198 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
199 (int)j
, mailp
, mailp
+ i
);
206 _lex_isolate(char const *comm
)
209 while (*comm
!= '\0' &&
210 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm
) == NULL
)
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
;
222 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
)
223 if (*comm
== *cp
->name
&& is_prefix(comm
, cp
->name
))
234 char const **argv
= v
;
235 struct cmd_ghost
*lcg
, *cg
;
244 if ((fp
= Ftmp(NULL
, "ghost", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
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
));
250 page_or_print(fp
, i
);
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]);
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
));
270 fprintf(stderr
, _("`ghost': no such alias: \"%s\"\n"), argv
[0]);
275 /* Define command for ghost: verify command content */
276 for (cl
= 0, i
= 1; (cp
= UNCONST(argv
[i
])) != NULL
; ++i
)
278 cl
+= strlen(cp
) + 1; /* SP or NUL */
280 fprintf(stderr
, _("`ghost': empty command arguments after \"%s\"\n"),
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])) {
290 lcg
->next
= cg
->next
;
292 _cmd_ghosts
= cg
->next
;
297 nl
= strlen(argv
[0]) +1;
298 cg
= smalloc(sizeof(*cg
) - VFIELD_SIZEOF(struct cmd_ghost
, name
) + nl
+ cl
);
299 cg
->next
= _cmd_ghosts
;
301 memcpy(cg
->name
, argv
[0], nl
);
302 cp
= cg
->cmd
.s
= cg
->name
+ nl
;
304 while (*++argv
!= NULL
) {
307 memcpy(cp
, *argv
, i
);
322 char const **argv
= v
, *cp
;
323 struct cmd_ghost
*lcg
, *cg
;
326 while ((cp
= *argv
++) != NULL
) {
327 if (cp
[0] == '*' && cp
[1] == '\0') {
328 while ((cg
= _cmd_ghosts
) != NULL
) {
329 _cmd_ghosts
= cg
->next
;
333 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
334 if (!strcmp(cg
->name
, cp
)) {
336 lcg
->next
= cg
->next
;
338 _cmd_ghosts
= cg
->next
;
342 fprintf(stderr
, _("`unghost': no such alias: \"%s\"\n"), cp
);
353 __pcmd_cmp(void const *s1
, void const *s2
)
355 struct cmd
const * const *c1
= s1
, * const *c2
= s2
;
359 rv
= strcmp((*c1
)->name
, (*c2
)->name
);
367 struct cmd
const **cpa
, *cp
, **cursor
;
372 for (i
= 0; _cmd_tab
[i
].name
!= NULL
; ++i
)
375 cpa
= ac_alloc(sizeof(cp
) * i
);
377 for (i
= 0; (cp
= _cmd_tab
+ i
)->name
!= NULL
; ++i
)
381 qsort(cpa
, i
, sizeof(cp
), &__pcmd_cmp
);
383 printf(_("Commands are:\n"));
384 for (i
= 0, cursor
= cpa
; (cp
= *cursor
++) != NULL
;) {
386 if (cp
->func
== &c_cmdnotsupp
)
388 j
= strlen(cp
->name
) + 2;
393 printf((*cursor
!= NULL
? "%s, " : "%s\n"), cp
->name
);
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;
420 printf(_("Features: %s\n"), ok_vlook(features
));
430 printf(_("Version %s\n"), ok_vlook(version
));
438 sighandler_type old_action
;
440 NYD_X
; /* Signal handler */
442 old_action
= safe_signal(s
, SIG_DFL
);
446 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
448 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
449 safe_signal(s
, old_action
);
450 if (_reset_on_stop
) {
459 NYD_X
; /* Signal handler */
466 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
474 int rv
, omsgCount
= 0;
476 bool_t isdevnull
= FAL0
;
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
)
491 switch (which_protocol(name
)) {
493 if (temporary_protocol_ext
!= NULL
)
494 name
= savecat(name
, temporary_protocol_ext
);
495 isdevnull
= ((options
& OPT_BATCH_FLAG
) && !strcmp(name
, "/dev/null"));
497 do { /* TODO we need objects, goes away then */
499 if (realpath(name
, ebuf
) != NULL
)
500 name
= savestr(ebuf
);
506 rv
= maildir_setfile(name
, fm
);
511 rv
= pop3_setfile(name
, fm
);
517 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_CACHE
)
520 rv
= imap_setfile(name
, fm
);
524 fprintf(stderr
, _("Cannot handle protocol: %s\n"), name
);
528 if ((ibuf
= Zopen(name
, "r")) == NULL
) {
529 if (((fm
& FEDIT_SYSBOX
) && errno
== ENOENT
) || (fm
& FEDIT_NEWMAIL
)) {
530 if (fm
& FEDIT_NEWMAIL
)
538 if (fstat(fileno(ibuf
), &stb
) == -1) {
539 if (fm
& FEDIT_NEWMAIL
)
545 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
548 if (fm
& FEDIT_NEWMAIL
)
550 errno
= S_ISDIR(stb
.st_mode
) ? EISDIR
: EINVAL
;
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
))
563 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
564 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
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
)) {
577 if ((ibuf
= Zopen(name
, "r")) == NULL
||
578 fstat(fileno(ibuf
), &stb
) == -1 || !S_ISREG(stb
.st_mode
)) {
585 /* Copy the messages into /tmp and set pointers */
586 flp
.l_type
= F_RDLCK
;
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
);
604 if (fm
& FEDIT_SYSBOX
)
611 if (!(pstate
& PS_EDIT
) && fcntl(fileno(ibuf
), F_SETLKW
, &flp
) == -1) {
613 perror("Unable to lock mailbox");
618 fseek(mb
.mb_otf
, 0L, SEEK_END
);
619 fseek(ibuf
, mailsize
, SEEK_SET
);
621 omsgCount
= msgCount
;
623 if (!(pstate
& PS_EDIT
) && fcntl(fileno(ibuf
), F_SETLKW
, &flp
) == -1) {
629 mailsize
= fsize(ibuf
);
631 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
635 setptr(ibuf
, offset
);
637 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
645 if (!(fm
& FEDIT_NEWMAIL
))
646 pstate
&= ~PS_SAW_COMMAND
;
648 if ((!(pstate
& PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)) && msgCount
== 0) {
650 if (!(fm
& FEDIT_NEWMAIL
)) {
651 if (!ok_blook(emptystart
))
653 fprintf(stderr
, _("No mail for %s\n"), who
);
658 if (fm
& FEDIT_NEWMAIL
)
659 newmailinfo(omsgCount
);
668 mb
.mb_type
= MB_VOID
;
675 newmailinfo(int omsgCount
)
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"));
690 printf(_("Loaded %d new messages.\n"), i
);
692 printf(_("Loaded %d messages.\n"), msgCount
);
694 check_folder_hook(TRU1
);
698 if (ok_blook(header
))
699 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
709 bool_t
volatile rv
= TRU1
;
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
);
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
))
741 handlerstacktop
= NULL
;
744 colour_table
= NULL
; /* XXX intermediate hack */
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
)) {
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? */
763 ev
.ev_line
.l
= ev
.ev_line_size
= 0;
767 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
770 cp
= ok_vlook(newmail
);
771 if ((options
& OPT_TTYIN
) && (cp
!= NULL
|| mb
.mb_type
== MB_IMAP
)) {
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
) ||
778 (mb
.mb_type
== MB_IMAP
&& imap_newmail(n
) > (cp
== NULL
)) ||
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
,
786 ((mb
.mb_perm
& MB_DELE
) ? 0 : FEDIT_RDONLY
)) < 0) {
787 exit_status
|= EXIT_ERR
;
791 if (mb
.mb_type
!= MB_IMAP
) {
792 dot
= message
+ odot
;
799 exit_status
= EXIT_OK
;
802 /* Read a line of commands and handle end of file specially */
804 ev
.ev_line
.l
= ev
.ev_line_size
;
805 n
= readline_input(NULL
, TRU1
, &ev
.ev_line
.s
, &ev
.ev_line
.l
,
807 ev
.ev_line_size
= (ui32_t
)ev
.ev_line
.l
;
808 ev
.ev_line
.l
= (ui32_t
)n
;
812 if (pstate
& PS_LOADING
)
814 if (pstate
& PS_SOURCING
) {
818 if ((options
& OPT_INTERACTIVE
) && ok_blook(ignoreeof
)) {
819 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
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
;
830 if (pstate
& PS_LOADING
) /* TODO mess; join PS_EVAL_ERROR.. */
834 if ((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)) {
835 if (exit_status
!= EXIT_OK
)
837 if ((pstate
& (PS_IN_LOAD
| PS_EVAL_ERROR
)) == PS_EVAL_ERROR
) {
838 exit_status
= EXIT_ERR
;
842 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
843 if (ev
.ev_new_content
!= NULL
)
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
)
853 if (pstate
& PS_SOURCING
)
860 execute(char *linebuf
, int contxt
, size_t linesize
) /* XXX LEGACY */
864 struct colour_table
*ct_save
;
866 char *temporary_orig_line
; /* XXX eval_ctx.ev_line not yet constant */
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 */
874 ct_save
= colour_table
;
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
;
885 if (contxt
&& temporary_orig_line
!= NULL
)
886 tty_addhist(temporary_orig_line
, TRU1
);
889 colour_table
= ct_save
;
896 evaluate(struct eval_ctx
*evp
)
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;
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 */
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 */
921 /* Handle ! differently to get the correct lexical conventions */
923 if (pstate
& PS_SOURCING
) {
924 fprintf(stderr
, _("Can't `!' while sourcing\n"));
928 evp
->ev_add_history
= TRU1
;
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 */
937 if ((cp
= _lex_isolate(cp
)) == arglist
[0] &&
938 (*cp
== '|' || *cp
== '~' || *cp
== '?'))
940 c
= (int)PTR2SIZE(cp
- arglist
[0]);
942 word
= UICMP(z
, c
, <, sizeof _wordbuf
) ? _wordbuf
: salloc(c
+1);
943 memcpy(word
, arglist
[0], c
);
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 */
951 if ((pstate
& PS_SOURCING
) || cg
!= NULL
)
957 /* If this is the first evaluation, check command ghosts */
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
)) {
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';
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
);
994 /* See if we should execute the command -- if a conditional we always
995 * execute it, otherwise, check the state of cond */
997 if (!(com
->argtype
& ARG_F
) && condstack_isskip())
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"),
1007 if ((pstate
& PS_SOURCING
) && (com
->argtype
& ARG_I
)) {
1008 fprintf(stderr
, _("May not execute `%s' while sourcing\n"),
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
);
1017 if (evp
->ev_is_recursive
&& (com
->argtype
& ARG_R
)) {
1018 fprintf(stderr
, _("Cannot recursively invoke `%s'\n"), com
->name
);
1021 if (mb
.mb_type
== MB_VOID
&& (com
->argtype
& ARG_A
)) {
1022 fprintf(stderr
, _("Cannot execute `%s' without active mailbox\n"),
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
) {
1034 /* Message list defaulting to nearest forward legal message */
1035 if (_msgvec
== NULL
)
1037 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
1040 *_msgvec
= first(com
->msgflag
, com
->msgmask
);
1044 if (*_msgvec
== 0) {
1045 if (!(pstate
& PS_HOOK_MASK
))
1046 printf(_("No applicable messages\n"));
1049 e
= (*com
->func
)(_msgvec
);
1053 /* Message list with no defaults, but no error if none exist */
1054 if (_msgvec
== NULL
) {
1056 fprintf(stderr
, _("Invalid use of \"message list\"\n"));
1059 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
1061 e
= (*com
->func
)(_msgvec
);
1065 /* Just the straight string, with leading blanks removed */
1066 while (whitechar(*cp
))
1068 e
= (*com
->func
)(cp
);
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)
1077 if (c
< com
->minargs
) {
1078 fprintf(stderr
, _("`%s' requires at least %d arg(s)\n"),
1079 com
->name
, com
->minargs
);
1082 if (c
> com
->maxargs
) {
1083 fprintf(stderr
, _("`%s' takes no more than %d arg(s)\n"),
1084 com
->name
, com
->maxargs
);
1087 e
= (*com
->func
)(arglist
);
1091 /* Just the constant zero, for exiting, eg. */
1092 e
= (*com
->func
)(0);
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
;
1105 if (!(com
->argtype
& ARG_H
) && !(pstate
& PS_MSGLIST_SAW_NO
))
1106 evp
->ev_add_history
= TRU1
;
1109 /* Exit the current source file on error TODO what a mess! */
1111 pstate
&= ~PS_EVAL_ERROR
;
1113 pstate
|= PS_EVAL_ERROR
;
1114 if (e
< 0 || (pstate
& PS_LOADING
)) {
1118 if (pstate
& PS_SOURCING
)
1124 if ((com
->argtype
& ARG_P
) && ok_blook(autoprint
))
1126 muvec
[0] = (int)PTR2SIZE(dot
- message
+ 1);
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
;
1133 pstate
&= ~PS_EVAL_ERROR
;
1145 if (_msgvec
!= NULL
)
1147 _msgvec
= scalloc(sz
+ 1, sizeof *_msgvec
);
1152 print_header_summary(char const *Larg
)
1154 size_t bot
, top
, i
, j
;
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
;
1171 for (bot
= msgCount
, top
= 0, i
= 0; (j
= _msgvec
[i
]) != 0; ++i
) {
1178 bot
= 1, top
= msgCount
;
1179 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
1187 NYD_X
; /* Signal handler */
1189 if (handlerstacktop
!= NULL
) {
1193 safe_signal(SIGINT
, onintr
);
1196 pstate
|= PS_SAW_COMMAND
;
1198 while (pstate
& PS_SOURCING
)
1201 termios_state_reset();
1208 if (interrupts
!= 1)
1209 fprintf(stderr
, _("Interrupt\n"));
1210 safe_signal(SIGPIPE
, _oldpipe
);
1215 announce(int printheaders
)
1220 mdot
= newfileinfo();
1223 dot
= message
+ mdot
- 1;
1224 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
1226 print_header_group(vec
); /* XXX errors? */
1236 int u
, n
, mdot
, d
, s
, hidden
, moved
;
1239 if (mb
.mb_type
== MB_VOID
) {
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
)
1249 if ((mp
->m_flag
& MREAD
) == 0)
1251 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
1253 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
1255 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
1257 if (mp
->m_flag
& MHIDDEN
)
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
));
1266 printf(_("1 message"));
1268 printf(_("%d messages"), msgCount
);
1270 printf(_(" %d new"), n
);
1272 printf(_(" %d unread"), u
);
1274 printf(_(" %d deleted"), d
);
1276 printf(_(" %d saved"), s
);
1278 printf(_(" %d moved"), moved
);
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]"));
1297 enum mflag avoid
= MHIDDEN
| MDELETED
;
1301 if (ok_blook(autothread
)) {
1302 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1304 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
1305 if (mb
.mb_sorted
!= NULL
)
1307 mb
.mb_sorted
= sstrdup(cp
);
1311 if (mb
.mb_type
== MB_VOID
) {
1317 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1318 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
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
)
1327 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1328 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
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
)
1339 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1340 if (mp
->m_flag
& MFLAGGED
)
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
)))
1351 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1352 if (!(mp
->m_flag
& (MREAD
| avoid
)))
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
))
1366 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
1368 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
1369 if (!(mp
->m_flag
& avoid
))
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
))
1380 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1382 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1383 if (!(mp
->m_flag
& avoid
))
1385 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
1386 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1394 initbox(char const *name
)
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)) ==
1408 perror(_("temporary mail message file"));
1411 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
1412 perror(_("temporary mail message file"));
1415 Ftmp_release(&tempMesg
);
1419 if (mb
.mb_sorted
!= NULL
) {
1421 mb
.mb_sorted
= NULL
;
1424 mb
.mb_flags
= MB_NOFLAGS
;
1426 dot
= prevdot
= threadroot
= NULL
;
1427 pstate
&= ~PS_DID_PRINT_DOT
;
1431 #ifdef HAVE_DOCSTRINGS
1433 print_comm_docstr(char const *comm
)
1436 struct cmd_ghost
*cg
;
1437 struct cmd
const *cp
;
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
);
1448 for (cp
= _cmd_tab
; cp
->name
!= NULL
; ++cp
) {
1449 if (cp
->func
== &c_cmdnotsupp
)
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
));