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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef HAVE_AMALGAMATION
45 char const *name
; /* Name of command */
46 int (*func
)(void*); /* Implementor of command */
47 enum argtype argtype
; /* Arglist type (see below) */
48 short msgflag
; /* Required flags of msgs */
49 short msgmask
; /* Relevant flags of msgs */
50 #ifdef HAVE_DOCSTRINGS
51 char const *doc
; /* One line doc for command */
54 /* Yechh, can't initialize unions */
55 #define minargs msgflag /* Minimum argcount for RAWLIST */
56 #define maxargs msgmask /* Max argcount for RAWLIST */
59 struct cmd_ghost
*next
;
60 struct str cmd
; /* Data follows after .name */
61 char name
[VFIELD_SIZE(0)];
65 static int _reset_on_stop
; /* do a reset() if stopped */
66 static sighandler_type _oldpipe
;
67 static struct cmd_ghost
*_cmd_ghosts
;
68 /* _cmd_tab[] after fun protos */
69 static int _lex_inithdr
; /* am printing startup headers */
71 /* Update mailname (if name != NULL) and displayname, return wether displayname
72 * was large enough to swallow mailname */
73 static bool_t
_update_mailname(char const *name
);
74 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
75 SINLINE
size_t __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
);
78 /* Isolate the command from the arguments */
79 static char * _lex_isolate(char const *comm
);
81 /* Get first-fit, or NULL */
82 static struct cmd
const * _lex(char const *comm
);
84 /* Command ghost handling */
85 static int _c_ghost(void *v
);
86 static int _c_unghost(void *v
);
88 /* Print a list of all commands */
89 static int _c_list(void *v
);
91 static int __pcmd_cmp(void const *s1
, void const *s2
);
94 static int _c_quit(void *v
);
96 /* Print the binaries compiled-in features */
97 static int _c_features(void *v
);
99 /* Print the binaries version number */
100 static int _c_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, and list of commands which are specially treated
109 * and deduced in execute(), but we need a list for _c_list() and
110 * print_comm_docstr() */
111 #ifdef HAVE_DOCSTRINGS
116 static struct cmd
const _cmd_tab
[] = {
119 _special_cmd_tab
[] = {
121 DS(N_("\"Comment command\": ignore remaining (continuable) line")) },
123 DS(N_("Print out the preceding message")) }
127 #ifdef HAVE_C90AMEND1
129 __narrow_suffix(char const *cp
, size_t cpl
, size_t maxl
)
135 for (err
= ok
= i
= 0; cpl
> maxl
|| err
;) {
136 int ml
= mblen(cp
, cpl
);
137 if (ml
< 0) { /* XXX _narrow_suffix(): mblen() error; action? */
138 (void)mblen(NULL
, 0);
155 #endif /* HAVE_C90AMEND1 */
158 _update_mailname(char const *name
)
160 char tbuf
[PATH_MAX
], *mailp
, *dispp
;
165 /* Don't realpath(3) if it's only an update request */
168 enum protocol p
= which_protocol(name
);
169 if (p
== PROTO_FILE
|| p
== PROTO_MAILDIR
) {
170 if (realpath(name
, mailname
) == NULL
) {
171 n_err(_("Can't canonicalize \"%s\"\n"), name
);
178 n_strscpy(mailname
, name
, sizeof(mailname
));
184 /* Don't display an absolute path but "+FOLDER" if under *folder* */
185 if (getfold(tbuf
, sizeof tbuf
)) {
187 if (i
< sizeof(tbuf
) -1)
189 if (!strncmp(tbuf
, mailp
, i
)) {
195 /* We want to see the name of the folder .. on the screen */
197 if (i
< sizeof(displayname
) -1)
198 memcpy(dispp
, mailp
, i
+1);
201 /* Avoid disrupting multibyte sequences (if possible) */
202 #ifndef HAVE_C90AMEND1
203 j
= sizeof(displayname
) / 3 - 1;
204 i
-= sizeof(displayname
) - (1/* + */ + 3) - j
;
206 j
= field_detect_clip(sizeof(displayname
) / 3, mailp
, i
);
207 i
= j
+ __narrow_suffix(mailp
+ j
, i
- j
,
208 sizeof(displayname
) - (1/* + */ + 3 + 1) - j
);
210 snprintf(dispp
, sizeof(displayname
), "%.*s...%s",
211 (int)j
, mailp
, mailp
+ i
);
218 _lex_isolate(char const *comm
)
221 while (*comm
!= '\0' &&
222 strchr("~|? \t0123456789&%@$^.:/-+*'\",;(`", *comm
) == NULL
)
225 return UNCONST(comm
);
228 static struct cmd
const *
229 _lex(char const *comm
) /* TODO **command hashtable**! linear list search!!! */
231 struct cmd
const *cp
, *cpmax
;
234 for (cp
= cpmax
= _cmd_tab
, cpmax
+= NELEM(_cmd_tab
); cp
!= cpmax
; ++cp
)
235 if (*comm
== *cp
->name
&& is_prefix(comm
, cp
->name
))
246 char const **argv
= v
;
247 struct cmd_ghost
*lcg
, *cg
;
256 if ((fp
= Ftmp(NULL
, "ghost", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
)
258 for (i
= 0, cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
259 fprintf(fp
, "ghost %s \"%s\"\n", cg
->name
, string_quote(cg
->cmd
.s
));
261 page_or_print(fp
, i
);
267 /* Verify the ghost name is a valid one */
268 if (*argv
[0] == '\0' || *_lex_isolate(argv
[0]) != '\0') {
269 n_err(_("`ghost': can't canonicalize \"%s\"\n"), argv
[0]);
274 /* Show command of single ghost? */
275 if (argv
[1] == NULL
) {
276 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
277 if (!strcmp(argv
[0], cg
->name
)) {
278 printf("ghost %s \"%s\"\n", cg
->name
, string_quote(cg
->cmd
.s
));
281 n_err(_("`ghost': no such alias: \"%s\"\n"), argv
[0]);
286 /* Define command for ghost: verify command content */
287 for (cl
= 0, i
= 1; (cp
= UNCONST(argv
[i
])) != NULL
; ++i
)
289 cl
+= strlen(cp
) + 1; /* SP or NUL */
291 n_err(_("`ghost': empty command arguments after \"%s\"\n"), argv
[0]);
296 /* If the ghost already exists, 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
;
307 nl
= strlen(argv
[0]) +1;
308 cg
= smalloc(sizeof(*cg
) - VFIELD_SIZEOF(struct cmd_ghost
, name
) + nl
+ cl
);
309 cg
->next
= _cmd_ghosts
;
311 memcpy(cg
->name
, argv
[0], nl
);
312 cp
= cg
->cmd
.s
= cg
->name
+ nl
;
314 while (*++argv
!= NULL
) {
317 memcpy(cp
, *argv
, i
);
332 char const **argv
= v
, *cp
;
333 struct cmd_ghost
*lcg
, *cg
;
336 while ((cp
= *argv
++) != NULL
) {
337 if (cp
[0] == '*' && cp
[1] == '\0') {
338 while ((cg
= _cmd_ghosts
) != NULL
) {
339 _cmd_ghosts
= cg
->next
;
343 for (lcg
= NULL
, cg
= _cmd_ghosts
; cg
!= NULL
; lcg
= cg
, cg
= cg
->next
)
344 if (!strcmp(cg
->name
, cp
)) {
346 lcg
->next
= cg
->next
;
348 _cmd_ghosts
= cg
->next
;
352 n_err(_("`unghost': no such alias: \"%s\"\n"), cp
);
363 __pcmd_cmp(void const *s1
, void const *s2
)
365 struct cmd
const * const *c1
= s1
, * const *c2
= s2
;
369 rv
= strcmp((*c1
)->name
, (*c2
)->name
);
377 struct cmd
const **cpa
, *cp
, **cursor
;
382 i
= NELEM(_cmd_tab
) + NELEM(_special_cmd_tab
) + 1;
383 cpa
= ac_alloc(sizeof(cp
) * i
);
385 for (i
= 0; i
< NELEM(_cmd_tab
); ++i
)
386 cpa
[i
] = _cmd_tab
+ i
;
390 for (j
= 0; j
< NELEM(_special_cmd_tab
); ++i
, ++j
)
391 cpa
[i
] = _special_cmd_tab
+ j
;
395 qsort(cpa
, i
, sizeof(cp
), &__pcmd_cmp
);
397 printf(_("Commands are:\n"));
398 for (i
= 0, cursor
= cpa
; (cp
= *cursor
++) != NULL
;) {
400 if (cp
->func
== &c_cmdnotsupp
)
402 j
= strlen(cp
->name
) + 2;
407 printf((*cursor
!= NULL
? "%s, " : "%s\n"), cp
->name
);
422 /* If we are PS_SOURCING, then return 1 so evaluate() can handle it.
423 * Otherwise return -1 to abort command loop */
424 rv
= (pstate
& PS_SOURCING
) ? 1 : -1;
434 printf(_("Features: %s\n"), ok_vlook(features
));
444 printf(_("Version %s\n"), ok_vlook(version
));
452 sighandler_type old_action
;
454 NYD_X
; /* Signal handler */
456 old_action
= safe_signal(s
, SIG_DFL
);
460 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
462 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
463 safe_signal(s
, old_action
);
464 if (_reset_on_stop
) {
466 n_TERMCAP_RESUME(TRU1
);
474 NYD_X
; /* Signal handler */
481 setfile(char const *name
, enum fedit_mode fm
) /* TODO oh my god */
483 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
484 * necessary to make a mailbox accessible by a different user, and if that
485 * has happened, let's just let the usual file perms decide */
491 int rv
, omsgCount
= 0;
492 FILE *ibuf
= NULL
, *lckfp
= NULL
;
493 bool_t isdevnull
= FAL0
;
496 pstate
&= ~PS_SETFILE_OPENED
;
498 if (name
[0] == '%' || ((who
= shortcut_expand(name
)) != NULL
&& *who
== '%'))
499 fm
|= FEDIT_SYSBOX
; /* TODO fexpand() needs to tell is-valid-user! */
501 who
= (name
[1] != '\0') ? name
+ 1 : myname
;
503 if ((name
= expand(name
)) == NULL
)
506 switch (which_protocol(name
)) {
508 if (temporary_protocol_ext
!= NULL
)
509 name
= savecat(name
, temporary_protocol_ext
);
510 isdevnull
= ((options
& OPT_BATCH_FLAG
) && !strcmp(name
, "/dev/null"));
512 do { /* TODO we need objects, goes away then */
514 if (realpath(name
, ebuf
) != NULL
)
515 name
= savestr(ebuf
);
522 rv
= maildir_setfile(name
, fm
);
527 rv
= pop3_setfile(name
, fm
);
531 n_err(_("Cannot handle protocol: %s\n"), name
);
535 if ((ibuf
= Zopen(name
, "r")) == NULL
) {
536 if ((fm
& FEDIT_SYSBOX
) && errno
== ENOENT
) {
537 if (strcmp(who
, myname
) && getpwnam(who
) == NULL
) {
538 n_err(_("\"%s\" is not a user of this system\n"), who
);
541 if (!(options
& OPT_QUICKRUN_MASK
) && ok_blook(bsdcompat
))
542 n_err(_("No mail for %s\n"), who
);
543 if (fm
& FEDIT_NEWMAIL
)
545 if (ok_blook(emptystart
)) {
546 if (!(options
& OPT_QUICKRUN_MASK
) && !ok_blook(bsdcompat
))
548 /* We must avoid returning -1 and causing program exit */
549 mb
.mb_type
= MB_VOID
;
554 } else if (fm
& FEDIT_NEWMAIL
)
560 if (fstat(fileno(ibuf
), &stb
) == -1) {
561 if (fm
& FEDIT_NEWMAIL
)
563 n_perr(_("fstat"), 0);
567 if (S_ISREG(stb
.st_mode
) || isdevnull
) {
570 if (fm
& FEDIT_NEWMAIL
)
572 errno
= S_ISDIR(stb
.st_mode
) ? EISDIR
: EINVAL
;
577 /* Looks like all will be well. We must now relinquish our hold on the
578 * current set of stuff. Must hold signals while we are reading the new
579 * file, else we will ruin the message[] data structure */
581 hold_sigs(); /* TODO note on this one in quit.c:quit() */
582 if (shudclob
&& !(fm
& FEDIT_NEWMAIL
))
585 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_sock
.s_fd
>= 0)
586 sclose(&mb
.mb_sock
); /* TODO sorry? VMAILFS->close(), thank you */
589 /* TODO There is no intermediate VOID box we've switched to: name may
590 * TODO point to the same box that we just have written, so any updates
591 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
592 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
593 /* TODO We may uselessly open twice but quit() doesn't say wether we were
594 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
595 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name
, mailname
)) {
599 if ((ibuf
= Zopen(name
, "r")) == NULL
||
600 fstat(fileno(ibuf
), &stb
) == -1 ||
601 (!S_ISREG(stb
.st_mode
) && !isdevnull
)) {
608 /* Copy the messages into /tmp and set pointers */
609 if (!(fm
& FEDIT_NEWMAIL
)) {
610 mb
.mb_type
= MB_FILE
;
611 mb
.mb_perm
= (((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
) ||
612 access(name
, W_OK
) < 0) ? 0 : MB_DELE
| MB_EDIT
);
624 if (fm
& FEDIT_SYSBOX
)
631 fseek(mb
.mb_otf
, 0L, SEEK_END
);
632 /* TODO Doing this without holding a lock is.. And not err checking.. */
633 fseek(ibuf
, mailsize
, SEEK_SET
);
635 omsgCount
= msgCount
;
640 else if (!(pstate
& PS_EDIT
))
641 lckfp
= dot_lock(name
, fileno(ibuf
), FLT_READ
, offset
,0,
642 (fm
& FEDIT_NEWMAIL
? 0 : 1));
643 else if (file_lock(fileno(ibuf
), FLT_READ
, offset
,0,
644 (fm
& FEDIT_NEWMAIL
? 0 : 1)))
648 if (!(fm
& FEDIT_NEWMAIL
)) {
649 char const *emsg
= (pstate
& PS_EDIT
)
650 ? N_("Unable to lock mailbox, aborting operation")
651 : N_("Unable to (dot) lock mailbox, aborting operation");
655 if (!(fm
& FEDIT_NEWMAIL
))
660 mailsize
= fsize(ibuf
);
662 /* TODO This is too simple minded? We should regenerate an index file
663 * TODO to be able to truly tell wether *anything* has changed! */
664 if ((fm
& FEDIT_NEWMAIL
) && UICMP(z
, mailsize
, <=, offset
)) {
668 setptr(ibuf
, offset
);
670 if ((fm
& FEDIT_NEWMAIL
) && mb
.mb_sorted
) {
677 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1) {
683 if (!(fm
& FEDIT_NEWMAIL
)) {
684 pstate
&= ~PS_SAW_COMMAND
;
685 pstate
|= PS_SETFILE_OPENED
;
688 if ((options
& OPT_EXISTONLY
) && !(options
& OPT_HEADERLIST
)) {
689 rv
= (msgCount
== 0);
693 if ((!(pstate
& PS_EDIT
) || (fm
& FEDIT_NEWMAIL
)) && msgCount
== 0) {
694 if (!(fm
& FEDIT_NEWMAIL
)) {
695 if (!ok_blook(emptystart
))
696 n_err(_("No mail for %s\n"), who
);
701 if (fm
& FEDIT_NEWMAIL
)
702 newmailinfo(omsgCount
);
708 if (lckfp
!= NULL
&& lckfp
!= (FILE*)-1)
714 mb
.mb_type
= MB_VOID
;
721 newmailinfo(int omsgCount
)
726 for (i
= 0; i
< omsgCount
; ++i
)
727 message
[i
].m_flag
&= ~MNEWEST
;
729 if (msgCount
> omsgCount
) {
730 for (i
= omsgCount
; i
< msgCount
; ++i
)
731 message
[i
].m_flag
|= MNEWEST
;
732 printf(_("New mail has arrived.\n"));
733 if ((i
= msgCount
- omsgCount
) == 1)
734 printf(_("Loaded 1 new message.\n"));
736 printf(_("Loaded %d new messages.\n"), i
);
738 printf(_("Loaded %d messages.\n"), msgCount
);
740 check_folder_hook(TRU1
);
744 if (ok_blook(header
))
745 print_headers(omsgCount
+ 1, msgCount
, FAL0
);
755 bool_t
volatile rv
= TRU1
;
758 if (!(pstate
& PS_SOURCING
)) {
759 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
760 safe_signal(SIGINT
, onintr
);
761 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
762 safe_signal(SIGHUP
, hangup
);
763 /* TODO We do a lot of redundant signal handling, especially
764 * TODO with the command line editor(s); try to merge this */
765 safe_signal(SIGTSTP
, stop
);
766 safe_signal(SIGTTOU
, stop
);
767 safe_signal(SIGTTIN
, stop
);
769 _oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
770 safe_signal(SIGPIPE
, _oldpipe
);
772 memset(&ev
, 0, sizeof ev
);
776 char *temporary_orig_line
; /* XXX eval_ctx.ev_line not yet constant */
778 n_COLOUR( n_colour_env_pop(TRU1
); )
780 /* TODO Unless we have our signal manager (or however we do it) child
781 * TODO processes may have time slots where their execution isn't
782 * TODO protected by signal handlers (in between start and setup
783 * TODO completed). close_all_files() is only called from onintr()
784 * TODO so those may linger possibly forever */
785 if (!(pstate
& PS_SOURCING
))
789 handlerstacktop
= NULL
;
791 temporary_localopts_free(); /* XXX intermediate hack */
792 sreset((pstate
& PS_SOURCING
) != 0);
793 if (!(pstate
& PS_SOURCING
)) {
796 /* TODO Note: this buffer may contain a password. We should redefine
797 * TODO the code flow which has to do that */
798 if ((cp
= termios_state
.ts_linebuf
) != NULL
) {
799 termios_state
.ts_linebuf
= NULL
;
800 termios_state
.ts_linesize
= 0;
801 free(cp
); /* TODO pool give-back */
803 /* TODO Due to expand-on-tab of NCL the buffer may grow */
804 if (ev
.ev_line
.l
> LINESIZE
* 3) {
805 free(ev
.ev_line
.s
); /* TODO pool! but what? */
807 ev
.ev_line
.l
= ev
.ev_line_size
= 0;
811 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
814 cp
= ok_vlook(newmail
);
815 if ((options
& OPT_TTYIN
) && cp
!= NULL
) {
818 n
= (cp
!= NULL
&& strcmp(cp
, "nopoll"));
819 if ((mb
.mb_type
== MB_FILE
&& !stat(mailname
, &st
) &&
820 st
.st_size
> mailsize
) ||
821 (mb
.mb_type
== MB_MAILDIR
&& n
!= 0)) {
822 size_t odot
= PTR2SIZE(dot
- message
);
823 ui32_t odid
= (pstate
& PS_DID_PRINT_DOT
);
825 if (setfile(mailname
,
827 ((mb
.mb_perm
& MB_DELE
) ? 0 : FEDIT_RDONLY
)) < 0) {
828 exit_status
|= EXIT_ERR
;
832 dot
= message
+ odot
;
838 exit_status
= EXIT_OK
;
841 /* Read a line of commands and handle end of file specially */
843 ev
.ev_line
.l
= ev
.ev_line_size
;
844 n
= readline_input(NULL
, TRU1
, &ev
.ev_line
.s
, &ev
.ev_line
.l
,
846 ev
.ev_line_size
= (ui32_t
)ev
.ev_line
.l
;
847 ev
.ev_line
.l
= (ui32_t
)n
;
851 if (pstate
& PS_LOADING
)
853 if (pstate
& PS_SOURCING
) {
857 if ((options
& OPT_INTERACTIVE
) && ok_blook(ignoreeof
)) {
858 printf(_("*ignoreeof* set, use `quit' to quit.\n"));
864 temporary_orig_line
= ((pstate
& PS_SOURCING
) ||
865 !(options
& OPT_INTERACTIVE
)) ? NULL
866 : savestrbuf(ev
.ev_line
.s
, ev
.ev_line
.l
);
867 pstate
&= ~PS_HOOK_MASK
;
869 if (pstate
& PS_LOADING
) /* TODO mess; join PS_EVAL_ERROR.. */
873 if ((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)) {
874 if (exit_status
!= EXIT_OK
)
876 if ((pstate
& (PS_IN_LOAD
| PS_EVAL_ERROR
)) == PS_EVAL_ERROR
) {
877 exit_status
= EXIT_ERR
;
881 if (!(pstate
& PS_SOURCING
) && (options
& OPT_INTERACTIVE
)) {
882 if (ev
.ev_new_content
!= NULL
)
884 /* That *can* happen since evaluate() unstack()s on error! */
885 if (temporary_orig_line
!= NULL
)
886 n_tty_addhist(temporary_orig_line
, !ev
.ev_add_history
);
890 if (ev
.ev_line
.s
!= NULL
)
892 if (pstate
& PS_SOURCING
)
899 execute(char *linebuf
, size_t linesize
) /* XXX LEGACY */
905 memset(&ev
, 0, sizeof ev
);
906 ev
.ev_line
.s
= linebuf
;
907 ev
.ev_line
.l
= linesize
;
908 ev
.ev_is_recursive
= TRU1
;
909 n_COLOUR( n_colour_env_push(); )
911 n_COLOUR( n_colour_env_pop(FAL0
); )
918 evaluate(struct eval_ctx
*evp
)
921 char _wordbuf
[2], *arglist
[MAXARGC
], *cp
, *word
;
922 struct cmd_ghost
*cg
= NULL
;
923 struct cmd
const *com
= NULL
;
924 int muvec
[2], c
, e
= 1;
927 line
= evp
->ev_line
; /* XXX don't change original (buffer pointer) */
928 evp
->ev_add_history
= FAL0
;
929 evp
->ev_new_content
= NULL
;
931 /* Command ghosts that refer to shell commands or macro expansion restart */
934 /* Strip the white space away from the beginning of the command */
935 for (cp
= line
.s
; whitechar(*cp
); ++cp
)
937 line
.l
-= PTR2SIZE(cp
- line
.s
);
939 /* Ignore comments */
943 /* Handle ! differently to get the correct lexical conventions */
945 if (pstate
& PS_SOURCING
) {
946 n_err(_("Can't `!' while `source'ing\n"));
950 evp
->ev_add_history
= TRU1
;
954 /* Isolate the actual command; since it may not necessarily be
955 * separated from the arguments (as in `p1') we need to duplicate it to
956 * be able to create a NUL terminated version.
957 * We must be aware of several special one letter commands here */
959 if ((cp
= _lex_isolate(cp
)) == arglist
[0] &&
960 (*cp
== '|' || *cp
== '~' || *cp
== '?'))
962 c
= (int)PTR2SIZE(cp
- arglist
[0]);
964 word
= UICMP(z
, c
, <, sizeof _wordbuf
) ? _wordbuf
: salloc(c
+1);
965 memcpy(word
, arglist
[0], c
);
968 /* Look up the command; if not found, bitch.
969 * Normally, a blank command would map to the first command in the
970 * table; while PS_SOURCING, however, we ignore blank lines to eliminate
971 * confusion; act just the same for ghosts */
973 if ((pstate
& PS_SOURCING
) || cg
!= NULL
)
979 /* If this is the first evaluation, check command ghosts */
981 /* TODO relink list head, so it's sorted on usage over time?
982 * TODO in fact, there should be one hashmap over all commands and ghosts
983 * TODO so that the lookup could be made much more efficient than it is
984 * TODO now (two adjacent list searches! */
985 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
986 if (!strcmp(word
, cg
->name
)) {
988 size_t i
= cg
->cmd
.l
;
989 line
.s
= salloc(i
+ line
.l
+1);
990 memcpy(line
.s
, cg
->cmd
.s
, i
);
991 memcpy(line
.s
+ i
, cp
, line
.l
);
992 line
.s
[i
+= line
.l
] = '\0';
1002 if ((com
= _lex(word
)) == NULL
|| com
->func
== &c_cmdnotsupp
) {
1003 bool_t s
= condstack_isskip();
1004 if (!s
|| (options
& OPT_D_V
))
1005 n_err(_("Unknown command%s: `%s'\n"),
1006 (s
? _(" (conditionally ignored)") : ""), word
);
1016 /* See if we should execute the command -- if a conditional we always
1017 * execute it, otherwise, check the state of cond */
1019 if (!(com
->argtype
& ARG_F
) && condstack_isskip())
1022 /* Process the arguments to the command, depending on the type it expects,
1023 * default to error. If we're PS_SOURCING an interactive command: error */
1024 if ((options
& OPT_SENDMODE
) && !(com
->argtype
& ARG_M
)) {
1025 n_err(_("May not execute `%s' while sending\n"), com
->name
);
1028 if ((pstate
& PS_SOURCING
) && (com
->argtype
& ARG_I
)) {
1029 n_err(_("May not execute `%s' while `source'ing\n"), com
->name
);
1032 if (!(mb
.mb_perm
& MB_DELE
) && (com
->argtype
& ARG_W
)) {
1033 n_err(_("May not execute `%s' -- message file is read only\n"),
1037 if (evp
->ev_is_recursive
&& (com
->argtype
& ARG_R
)) {
1038 n_err(_("Cannot recursively invoke `%s'\n"), com
->name
);
1041 if (mb
.mb_type
== MB_VOID
&& (com
->argtype
& ARG_A
)) {
1042 n_err(_("Cannot execute `%s' without active mailbox\n"), com
->name
);
1045 if (com
->argtype
& ARG_O
)
1046 OBSOLETE2(_("this command will be removed"), com
->name
);
1048 if (com
->argtype
& ARG_V
)
1049 temporary_arg_v_store
= NULL
;
1051 switch (com
->argtype
& ARG_ARGMASK
) {
1053 /* Message list defaulting to nearest forward legal message */
1054 if (_msgvec
== NULL
)
1056 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
1059 *_msgvec
= first(com
->msgflag
, com
->msgmask
);
1063 if (*_msgvec
== 0) {
1064 if (!(pstate
& PS_HOOK_MASK
))
1065 printf(_("No applicable messages\n"));
1068 e
= (*com
->func
)(_msgvec
);
1072 /* Message list with no defaults, but no error if none exist */
1073 if (_msgvec
== NULL
) {
1075 n_err(_("Invalid use of \"message list\"\n"));
1078 if ((c
= getmsglist(cp
, _msgvec
, com
->msgflag
)) < 0)
1080 e
= (*com
->func
)(_msgvec
);
1084 /* Just the straight string, with leading blanks removed */
1085 while (whitechar(*cp
))
1087 e
= (*com
->func
)(cp
);
1092 /* A vector of strings, in shell style */
1093 if ((c
= getrawlist(cp
, line
.l
, arglist
, NELEM(arglist
),
1094 ((com
->argtype
& ARG_ARGMASK
) == ARG_ECHOLIST
))) < 0)
1096 if (c
< com
->minargs
) {
1097 n_err(_("`%s' requires at least %d arg(s)\n"),
1098 com
->name
, com
->minargs
);
1101 if (c
> com
->maxargs
) {
1102 n_err(_("`%s' takes no more than %d arg(s)\n"),
1103 com
->name
, com
->maxargs
);
1106 e
= (*com
->func
)(arglist
);
1110 /* Just the constant zero, for exiting, eg. */
1111 e
= (*com
->func
)(0);
1115 n_panic(_("Unknown argument type"));
1118 if (e
== 0 && (com
->argtype
& ARG_V
) &&
1119 (cp
= temporary_arg_v_store
) != NULL
) {
1120 temporary_arg_v_store
= NULL
;
1121 evp
->ev_new_content
= cp
;
1124 if (!(com
->argtype
& ARG_H
) && !(pstate
& PS_MSGLIST_SAW_NO
))
1125 evp
->ev_add_history
= TRU1
;
1128 /* Exit the current source file on error TODO what a mess! */
1130 pstate
&= ~PS_EVAL_ERROR
;
1132 pstate
|= PS_EVAL_ERROR
;
1133 if (e
< 0 || (pstate
& PS_LOADING
)) {
1137 if (pstate
& PS_SOURCING
)
1143 if ((com
->argtype
& ARG_P
) && ok_blook(autoprint
))
1145 muvec
[0] = (int)PTR2SIZE(dot
- message
+ 1);
1147 c_type(muvec
); /* TODO what if error? re-eval! */
1149 if (!(pstate
& (PS_SOURCING
| PS_HOOK_MASK
)) && !(com
->argtype
& ARG_T
))
1150 pstate
|= PS_SAW_COMMAND
;
1152 pstate
&= ~PS_EVAL_ERROR
;
1164 if (_msgvec
!= NULL
)
1166 _msgvec
= scalloc(sz
+ 1, sizeof *_msgvec
);
1171 print_header_summary(char const *Larg
)
1173 size_t bot
, top
, i
, j
;
1177 /* Avoid any messages XXX add a make_mua_silent() and use it? */
1178 if ((options
& (OPT_VERB
| OPT_EXISTONLY
)) == OPT_EXISTONLY
) {
1179 freopen("/dev/null", "w", stdout
);
1180 freopen("/dev/null", "w", stderr
);
1182 assert(_msgvec
!= NULL
);
1183 i
= (getmsglist(/*TODO make arg const */UNCONST(Larg
), _msgvec
, 0) <= 0);
1184 if (options
& OPT_EXISTONLY
) {
1185 exit_status
= (int)i
;
1190 for (bot
= msgCount
, top
= 0, i
= 0; (j
= _msgvec
[i
]) != 0; ++i
) {
1197 bot
= 1, top
= msgCount
;
1198 print_headers(bot
, top
, (Larg
!= NULL
)); /* TODO should take iterator!! */
1206 NYD_X
; /* Signal handler */
1208 if (handlerstacktop
!= NULL
) {
1212 safe_signal(SIGINT
, onintr
);
1215 pstate
|= PS_SAW_COMMAND
;
1217 while (pstate
& PS_SOURCING
)
1220 termios_state_reset();
1227 if (interrupts
!= 1)
1228 n_err_sighdl(_("Interrupt\n"));
1229 safe_signal(SIGPIPE
, _oldpipe
);
1234 announce(int printheaders
)
1239 mdot
= newfileinfo();
1242 dot
= message
+ mdot
- 1;
1243 if (printheaders
&& msgCount
> 0 && ok_blook(header
)) {
1245 print_header_group(vec
); /* XXX errors? */
1255 int u
, n
, mdot
, d
, s
, hidden
, moved
;
1258 if (mb
.mb_type
== MB_VOID
) {
1264 s
= d
= hidden
= moved
=0;
1265 for (mp
= message
, n
= 0, u
= 0; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
1266 if (mp
->m_flag
& MNEW
)
1268 if ((mp
->m_flag
& MREAD
) == 0)
1270 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == (MDELETED
| MSAVED
))
1272 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MDELETED
)
1274 if ((mp
->m_flag
& (MDELETED
| MSAVED
)) == MSAVED
)
1276 if (mp
->m_flag
& MHIDDEN
)
1280 /* If displayname gets truncated the user effectively has no option to see
1281 * the full pathname of the mailbox, so print it at least for '? fi' */
1282 printf(_("\"%s\": "),
1283 (_update_mailname(NULL
) ? displayname
: mailname
));
1285 printf(_("1 message"));
1287 printf(_("%d messages"), msgCount
);
1289 printf(_(" %d new"), n
);
1291 printf(_(" %d unread"), u
);
1293 printf(_(" %d deleted"), d
);
1295 printf(_(" %d saved"), s
);
1297 printf(_(" %d moved"), moved
);
1299 printf(_(" %d hidden"), hidden
);
1300 else if (mb
.mb_perm
== 0)
1301 printf(_(" [Read only]"));
1314 enum mflag avoid
= MHIDDEN
| MDELETED
;
1318 if (ok_blook(autothread
)) {
1319 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
1321 } else if ((cp
= ok_vlook(autosort
)) != NULL
) {
1322 if (mb
.mb_sorted
!= NULL
)
1324 mb
.mb_sorted
= sstrdup(cp
);
1328 if (mb
.mb_type
== MB_VOID
) {
1334 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1335 if ((mp
->m_flag
& (MNEWEST
| avoid
)) == MNEWEST
)
1338 if (!nmail
|| PTRCMP(mp
, >=, message
+ msgCount
)) {
1339 if (mb
.mb_threaded
) {
1340 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1341 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
1344 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1345 if ((mp
->m_flag
& (MNEW
| avoid
)) == MNEW
)
1350 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
1351 if (mb
.mb_threaded
) {
1352 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1353 if (mp
->m_flag
& MFLAGGED
)
1356 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1357 if (mp
->m_flag
& MFLAGGED
)
1362 if ((mb
.mb_threaded
? (mp
== NULL
) : PTRCMP(mp
, >=, message
+ msgCount
))) {
1363 if (mb
.mb_threaded
) {
1364 for (mp
= threadroot
; mp
!= NULL
; mp
= next_in_thread(mp
))
1365 if (!(mp
->m_flag
& (MREAD
| avoid
)))
1368 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1369 if (!(mp
->m_flag
& (MREAD
| avoid
)))
1375 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
1376 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
1377 else if (ok_blook(showlast
)) {
1378 if (mb
.mb_threaded
) {
1379 for (mp
= this_in_thread(threadroot
, -1); mp
;
1380 mp
= prev_in_thread(mp
))
1381 if (!(mp
->m_flag
& avoid
))
1383 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
1385 for (mp
= message
+ msgCount
- 1; mp
>= message
; --mp
)
1386 if (!(mp
->m_flag
& avoid
))
1388 mdot
= (mp
>= message
) ? (int)PTR2SIZE(mp
- message
+ 1) : msgCount
;
1390 } else if (!nmail
&&
1391 (mb
.mb_threaded
? (mp
!= NULL
) : PTRCMP(mp
, <, message
+ msgCount
)))
1392 mdot
= (int)PTR2SIZE(mp
- message
+ 1);
1393 else if (mb
.mb_threaded
) {
1394 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
1395 if (!(mp
->m_flag
& avoid
))
1397 mdot
= (mp
!= NULL
) ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1399 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
1400 if (!(mp
->m_flag
& avoid
))
1402 mdot
= PTRCMP(mp
, <, message
+ msgCount
)
1403 ? (int)PTR2SIZE(mp
- message
+ 1) : 1;
1411 initbox(char const *name
)
1416 if (mb
.mb_type
!= MB_VOID
)
1417 n_strscpy(prevfile
, mailname
, PATH_MAX
);
1419 /* TODO name always NE mailname (but goes away for objects anyway)
1420 * TODO Well, not true no more except that in parens */
1421 _update_mailname((name
!= mailname
) ? name
: NULL
);
1423 if ((mb
.mb_otf
= Ftmp(&tempMesg
, "tmpmbox", OF_WRONLY
| OF_HOLDSIGS
)) ==
1425 n_perr(_("temporary mail message file"), 0);
1428 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", NULL
)) == NULL
) {
1429 n_perr(_("temporary mail message file"), 0);
1432 Ftmp_release(&tempMesg
);
1436 if (mb
.mb_sorted
!= NULL
) {
1438 mb
.mb_sorted
= NULL
;
1440 dot
= prevdot
= threadroot
= NULL
;
1441 pstate
&= ~PS_DID_PRINT_DOT
;
1445 #ifdef HAVE_DOCSTRINGS
1447 print_comm_docstr(char const *comm
)
1449 struct cmd_ghost
const *cg
;
1450 struct cmd
const *cp
, *cpmax
;
1454 /* Ghosts take precedence */
1455 for (cg
= _cmd_ghosts
; cg
!= NULL
; cg
= cg
->next
)
1456 if (!strcmp(comm
, cg
->name
)) {
1457 printf("%s -> ", comm
);
1462 cpmax
= (cp
= _cmd_tab
) + NELEM(_cmd_tab
);
1464 for (; cp
< cpmax
; ++cp
) {
1465 if (!strcmp(comm
, cp
->name
))
1466 printf("%s: %s\n", comm
, V_(cp
->doc
));
1467 else if (is_prefix(comm
, cp
->name
))
1468 printf("%s (%s): %s\n", comm
, cp
->name
, V_(cp
->doc
));
1474 if (!rv
&& cpmax
== _cmd_tab
+ NELEM(_cmd_tab
)) {
1475 cpmax
= (cp
= _special_cmd_tab
) + NELEM(_special_cmd_tab
);
1479 if (!rv
&& cg
!= NULL
) {
1480 printf("\"%s\"\n", comm
);