2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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 static char sccsid
[] = "@(#)lex.c 2.86 (gritter) 12/25/06";
54 * Mail -- a mail program
56 * Lexical processing of commands.
60 static sighandler_type oldpipe
;
62 static const struct cmd
*lex(char *Word
);
63 static void stop(int s
);
64 static void hangup(int s
);
67 * Set up editing on the given file name.
68 * If the first character of name is %, we are considered to be
69 * editing the file, otherwise we are reading our mail which has
70 * signficance for mbox and so forth.
72 * newmail: Check for new mail in the current folder only.
75 setfile(char *name
, int newmail
)
78 int i
, compressed
= 0;
81 char *who
= name
[1] ? name
+ 1 : myname
;
88 isedit
= *name
!= '%' && ((sh
= get_shortcut(name
)) == NULL
||
90 if ((name
= expand(name
)) == NULL
)
93 switch (which_protocol(name
)) {
97 return maildir_setfile(name
, newmail
, isedit
);
100 return pop3_setfile(name
, newmail
, isedit
);
104 if (mb
.mb_type
== MB_CACHE
)
106 omsgCount
= msgCount
;
108 return imap_setfile(name
, newmail
, isedit
);
110 fprintf(stderr
, catgets(catd
, CATSET
, 217,
111 "Cannot handle protocol: %s\n"), name
);
114 if ((ibuf
= Zopen(name
, "r", &compressed
)) == NULL
) {
115 if ((!isedit
&& errno
== ENOENT
) || newmail
) {
124 if (fstat(fileno(ibuf
), &stb
) < 0) {
132 if (S_ISDIR(stb
.st_mode
)) {
139 } else if (S_ISREG(stb
.st_mode
)) {
151 * Looks like all will be well. We must now relinquish our
152 * hold on the current set of stuff. Must hold signals
153 * while we are reading the new file, else we will ruin
154 * the message[] data structure.
158 if (shudclob
&& !newmail
)
162 if (!newmail
&& mb
.mb_sock
.s_fd
>= 0)
164 #endif /* HAVE_SOCKETS */
167 * Copy the messages into /tmp
171 flp
.l_type
= F_RDLCK
;
173 flp
.l_whence
= SEEK_SET
;
175 mb
.mb_type
= MB_FILE
;
176 mb
.mb_perm
= Rflag
? 0 : MB_DELE
|MB_EDIT
;
177 mb
.mb_compressed
= compressed
;
179 if (compressed
& 0200)
182 if ((i
= open(name
, O_WRONLY
)) < 0)
202 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) < 0) {
203 perror("Unable to lock mailbox");
207 } else /* newmail */{
208 fseek(mb
.mb_otf
, 0L, SEEK_END
);
209 fseek(ibuf
, mailsize
, SEEK_SET
);
211 omsgCount
= msgCount
;
213 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) < 0)
216 mailsize
= fsize(ibuf
);
217 if (newmail
&& (size_t)mailsize
<= offset
) {
221 setptr(ibuf
, offset
);
223 if (newmail
&& mb
.mb_sorted
) {
231 if ((!edit
|| newmail
) && msgCount
== 0) {
234 if (value("emptystart") == NULL
)
235 nomail
: fprintf(stderr
, catgets(catd
, CATSET
, 88,
236 "No mail for %s\n"), who
);
241 newmailinfo(omsgCount
);
248 newmailinfo(int omsgCount
)
253 for (i
= 0; i
< omsgCount
; i
++)
254 message
[i
].m_flag
&= ~MNEWEST
;
255 if (msgCount
> omsgCount
) {
256 for (i
= omsgCount
; i
< msgCount
; i
++)
257 message
[i
].m_flag
|= MNEWEST
;
258 printf(catgets(catd
, CATSET
, 158, "New mail has arrived.\n"));
259 if (msgCount
- omsgCount
== 1)
260 printf(catgets(catd
, CATSET
, 214,
261 "Loaded 1 new message\n"));
263 printf(catgets(catd
, CATSET
, 215,
264 "Loaded %d new messages\n"),
265 msgCount
- omsgCount
);
267 printf("Loaded %d messages\n", msgCount
);
268 callhook(mailname
, 1);
270 if (value("header")) {
271 if (mb
.mb_type
== MB_IMAP
)
272 imap_getheaders(omsgCount
+1, msgCount
);
273 while (++omsgCount
<= msgCount
)
274 if (visible(&message
[omsgCount
-1]))
275 printhead(omsgCount
, stdout
, 0);
281 static int reset_on_stop
; /* do a reset() if stopped */
284 * Interpret user commands one by one. If standard input is not a tty,
292 char *linebuf
= NULL
, *av
, *nv
;
297 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
298 safe_signal(SIGINT
, onintr
);
299 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
300 safe_signal(SIGHUP
, hangup
);
301 safe_signal(SIGTSTP
, stop
);
302 safe_signal(SIGTTOU
, stop
);
303 safe_signal(SIGTTIN
, stop
);
305 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
306 safe_signal(SIGPIPE
, oldpipe
);
310 handlerstacktop
= NULL
;
312 * Print the prompt, if needed. Clear out
313 * string space, and flush the output.
315 if (!sourcing
&& value("interactive") != NULL
) {
316 av
= (av
= value("autoinc")) ? savestr(av
) : NULL
;
317 nv
= (nv
= value("newmail")) ? savestr(nv
) : NULL
;
318 if (is_a_tty
[0] && (av
!= NULL
|| nv
!= NULL
||
319 mb
.mb_type
== MB_IMAP
)) {
322 n
= (av
&& strcmp(av
, "noimap") &&
323 strcmp(av
, "nopoll")) |
324 (nv
&& strcmp(nv
, "noimap") &&
325 strcmp(nv
, "nopoll"));
327 if ((mb
.mb_type
== MB_FILE
&&
328 stat(mailname
, &st
) == 0 &&
329 st
.st_size
> mailsize
) ||
330 (mb
.mb_type
== MB_IMAP
&&
331 imap_newmail(n
) > x
) ||
332 (mb
.mb_type
== MB_MAILDIR
&&
334 int odot
= dot
- &message
[0];
335 int odid
= did_print_dot
;
337 setfile(mailname
, 1);
338 if (mb
.mb_type
!= MB_IMAP
) {
339 dot
= &message
[odot
];
340 did_print_dot
= odid
;
345 if ((prompt
= value("prompt")) == NULL
)
346 prompt
= value("bsdcompat") ? "& " : "? ";
347 printf("%s", prompt
);
352 * Read a line of commands from the current input
353 * and handle end of file specially.
357 n
= readline_restart(input
, &linebuf
, &linesize
, n
);
360 if (n
== 0 || linebuf
[n
- 1] != '\\')
362 linebuf
[n
- 1] = ' ';
373 if (value("interactive") != NULL
&&
374 value("ignoreeof") != NULL
&&
376 printf(catgets(catd
, CATSET
, 89,
377 "Use \"quit\" to quit.\n"));
384 if (execute(linebuf
, 0, n
))
392 * Execute a single command.
393 * Command functions return 0 for success, 1 for error, and -1
394 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
395 * the interactive command loop.
396 * Contxt is non-zero if called while composing mail.
399 execute(char *linebuf
, int contxt
, size_t linesize
)
402 char *arglist
[MAXARGC
];
403 const struct cmd
*com
= (struct cmd
*)NULL
;
410 * Strip the white space away from the beginning
411 * of the command, then scan out a word, which
412 * consists of anything except digits and white space.
414 * Handle ! escapes differently to get the correct
415 * lexical conventions.
417 word
= ac_alloc(linesize
+ 1);
418 for (cp
= linebuf
; whitechar(*cp
& 0377); cp
++);
421 printf(catgets(catd
, CATSET
, 90,
422 "Can't \"!\" while sourcing\n"));
435 while (*cp
&& strchr(" \t0123456789$^.:/-+*'\",;(`", *cp
)
443 * Look up the command; if not found, bitch.
444 * Normally, a blank command would map to the
445 * first command in the table; while sourcing,
446 * however, we ignore blank lines to eliminate
450 if (sourcing
&& *word
== '\0') {
456 printf(catgets(catd
, CATSET
, 91,
457 "Unknown command: \"%s\"\n"), word
);
462 * See if we should execute the command -- if a conditional
463 * we always execute it, otherwise, check the state of cond.
466 if ((com
->c_argtype
& F
) == 0) {
467 if ((cond
== CRCV
&& !rcvmode
) ||
468 (cond
== CSEND
&& rcvmode
) ||
469 (cond
== CTERM
&& !is_a_tty
[0]) ||
470 (cond
== CNONTERM
&& is_a_tty
[0])) {
477 * Process the arguments to the command, depending
478 * on the type he expects. Default to an error.
479 * If we are sourcing an interactive command, it's
483 if (!rcvmode
&& (com
->c_argtype
& M
) == 0) {
484 printf(catgets(catd
, CATSET
, 92,
485 "May not execute \"%s\" while sending\n"), com
->c_name
);
488 if (sourcing
&& com
->c_argtype
& I
) {
489 printf(catgets(catd
, CATSET
, 93,
490 "May not execute \"%s\" while sourcing\n"),
494 if ((mb
.mb_perm
& MB_DELE
) == 0 && com
->c_argtype
& W
) {
495 printf(catgets(catd
, CATSET
, 94,
496 "May not execute \"%s\" -- message file is read only\n"),
500 if (contxt
&& com
->c_argtype
& R
) {
501 printf(catgets(catd
, CATSET
, 95,
502 "Cannot recursively invoke \"%s\"\n"), com
->c_name
);
505 if (mb
.mb_type
== MB_VOID
&& com
->c_argtype
& A
) {
506 printf(catgets(catd
, CATSET
, 257,
507 "Cannot execute \"%s\" without active mailbox\n"),
511 switch (com
->c_argtype
& ~(F
|P
|I
|M
|T
|W
|R
|A
)) {
514 * A message list defaulting to nearest forward
518 printf(catgets(catd
, CATSET
, 96,
519 "Illegal use of \"message list\"\n"));
522 if ((c
= getmsglist(cp
, msgvec
, com
->c_msgflag
)) < 0)
525 if ((*msgvec
= first(com
->c_msgflag
, com
->c_msgmask
))
531 printf(catgets(catd
, CATSET
, 97,
532 "No applicable messages\n"));
535 e
= (*com
->c_func
)(msgvec
);
540 * A message list with no defaults, but no error
544 printf(catgets(catd
, CATSET
, 98,
545 "Illegal use of \"message list\"\n"));
548 if (getmsglist(cp
, msgvec
, com
->c_msgflag
) < 0)
550 e
= (*com
->c_func
)(msgvec
);
555 * Just the straight string, with
556 * leading blanks removed.
558 while (whitechar(*cp
& 0377))
560 e
= (*com
->c_func
)(cp
);
566 * A vector of strings, in shell style.
568 if ((c
= getrawlist(cp
, linesize
, arglist
,
569 sizeof arglist
/ sizeof *arglist
,
570 (com
->c_argtype
&~(F
|P
|I
|M
|T
|W
|R
|A
))==ECHOLIST
))
573 if (c
< com
->c_minargs
) {
574 printf(catgets(catd
, CATSET
, 99,
575 "%s requires at least %d arg(s)\n"),
576 com
->c_name
, com
->c_minargs
);
579 if (c
> com
->c_maxargs
) {
580 printf(catgets(catd
, CATSET
, 100,
581 "%s takes no more than %d arg(s)\n"),
582 com
->c_name
, com
->c_maxargs
);
585 e
= (*com
->c_func
)(arglist
);
590 * Just the constant zero, for exiting,
593 e
= (*com
->c_func
)(0);
597 panic(catgets(catd
, CATSET
, 101, "Unknown argtype"));
603 * Exit the current source file on
615 if (com
== (struct cmd
*)NULL
)
617 if (value("autoprint") != NULL
&& com
->c_argtype
& P
)
619 muvec
[0] = dot
- &message
[0] + 1;
623 if (!sourcing
&& !inhook
&& (com
->c_argtype
& T
) == 0)
629 * Set the size of the message vector used to construct argument
630 * lists to message list functions.
638 msgvec
= (int *)scalloc((sz
+ 1), sizeof *msgvec
);
642 * Find the correct command in the command table corresponding
643 * to the passed command "word"
646 static const struct cmd
*
649 extern const struct cmd cmdtab
[];
650 const struct cmd
*cp
;
652 for (cp
= &cmdtab
[0]; cp
->c_name
!= NULL
; cp
++)
653 if (is_prefix(Word
, cp
->c_name
))
659 * The following gets called on receipt of an interrupt. This is
660 * to abort printout of a command, mainly.
661 * Dispatching here when command() is inactive crashes rcv.
662 * Close all open files except 0, 1, 2, and the temporary.
663 * Also, unstack all source files.
666 static int inithdr
; /* am printing startup headers */
672 if (handlerstacktop
!= NULL
) {
676 safe_signal(SIGINT
, onintr
);
691 fprintf(stderr
, catgets(catd
, CATSET
, 102, "Interrupt\n"));
692 safe_signal(SIGPIPE
, oldpipe
);
697 * When we wake up after ^Z, reprint the prompt.
702 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
707 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
709 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
710 safe_signal(s
, old_action
);
718 * Branch here on hangup signal and simulate "exit".
730 * Announce the presence of the current Mail version,
731 * give the message count, and print a header listing.
734 announce(int printheaders
)
738 mdot
= newfileinfo();
741 dot
= &message
[mdot
- 1];
742 if (printheaders
&& msgCount
> 0 && value("header") != NULL
) {
750 * Announce information about the file we are editing.
751 * Return a likely place to set dot.
757 int u
, n
, mdot
, d
, s
, hidden
, killed
, moved
;
758 char fname
[PATHSIZE
], zname
[PATHSIZE
], *ename
;
760 if (mb
.mb_type
== MB_VOID
)
763 s
= d
= hidden
= killed
= moved
=0;
764 for (mp
= &message
[0], n
= 0, u
= 0; mp
< &message
[msgCount
]; mp
++) {
765 if (mp
->m_flag
& MNEW
)
767 if ((mp
->m_flag
& MREAD
) == 0)
769 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == (MDELETED
|MSAVED
))
771 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == MDELETED
)
773 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == MSAVED
)
775 if (mp
->m_flag
& MHIDDEN
)
777 if (mp
->m_flag
& MKILL
)
781 if (getfold(fname
, sizeof fname
- 1) >= 0) {
783 if (which_protocol(fname
) != PROTO_IMAP
&&
784 strncmp(fname
, mailname
, strlen(fname
)) == 0) {
785 snprintf(zname
, sizeof zname
, "+%s",
786 mailname
+ strlen(fname
));
790 printf(catgets(catd
, CATSET
, 103, "\"%s\": "), ename
);
792 printf(catgets(catd
, CATSET
, 104, "1 message"));
794 printf(catgets(catd
, CATSET
, 105, "%d messages"), msgCount
);
796 printf(catgets(catd
, CATSET
, 106, " %d new"), n
);
798 printf(catgets(catd
, CATSET
, 107, " %d unread"), u
);
800 printf(catgets(catd
, CATSET
, 108, " %d deleted"), d
);
802 printf(catgets(catd
, CATSET
, 109, " %d saved"), s
);
804 printf(catgets(catd
, CATSET
, 109, " %d moved"), moved
);
806 printf(catgets(catd
, CATSET
, 109, " %d hidden"), hidden
);
808 printf(catgets(catd
, CATSET
, 109, " %d killed"), killed
);
809 if (mb
.mb_type
== MB_CACHE
)
810 printf(" [Disconnected]");
811 else if (mb
.mb_perm
== 0)
812 printf(catgets(catd
, CATSET
, 110, " [Read only]"));
823 enum mflag avoid
= MHIDDEN
|MKILL
|MDELETED
;
826 if (value("autothread"))
828 else if ((cp
= value("autosort")) != NULL
) {
830 mb
.mb_sorted
= sstrdup(cp
);
834 if (mb
.mb_type
== MB_VOID
)
837 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
838 if ((mp
->m_flag
& (MNEWEST
|avoid
)) == MNEWEST
)
840 if (!newmail
|| mp
>= &message
[msgCount
]) {
841 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
843 mp
!= NULL
: mp
< &message
[msgCount
];
845 mp
= next_in_thread(mp
) : mp
++)
846 if ((mp
->m_flag
& (MNEW
|avoid
)) == MNEW
)
849 if (mb
.mb_threaded
? mp
== NULL
: mp
>= &message
[msgCount
])
850 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
851 mb
.mb_threaded
? mp
!= NULL
:
852 mp
< &message
[msgCount
];
853 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++)
854 if (mp
->m_flag
& MFLAGGED
)
856 if (mb
.mb_threaded
? mp
== NULL
: mp
>= &message
[msgCount
])
857 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
858 mb
.mb_threaded
? mp
!= NULL
:
859 mp
< &message
[msgCount
];
860 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++)
861 if ((mp
->m_flag
& (MREAD
|avoid
)) == 0)
863 if (mb
.mb_threaded
? mp
!= NULL
: mp
< &message
[msgCount
])
864 mdot
= mp
- &message
[0] + 1;
865 else if (value("showlast")) {
866 if (mb
.mb_threaded
) {
867 for (mp
= this_in_thread(threadroot
, -1); mp
;
868 mp
= prev_in_thread(mp
))
869 if ((mp
->m_flag
& avoid
) == 0)
871 mdot
= mp
? mp
- &message
[0] + 1 : msgCount
;
873 for (mp
= &message
[msgCount
-1]; mp
>= &message
[0]; mp
--)
874 if ((mp
->m_flag
& avoid
) == 0)
876 mdot
= mp
>= &message
[0] ? mp
-&message
[0]+1 : msgCount
;
878 } else if (mb
.mb_threaded
) {
879 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
880 if ((mp
->m_flag
& avoid
) == 0)
882 mdot
= mp
? mp
- &message
[0] + 1 : 1;
884 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
885 if ((mp
->m_flag
& avoid
) == 0)
887 mdot
= mp
< &message
[msgCount
] ? mp
-&message
[0]+1 : 1;
893 * Print the current version number.
901 printf(catgets(catd
, CATSET
, 111, "Version %s\n"), version
);
906 * Load a file of user definitions.
913 if ((in
= Fopen(name
, "r")) == NULL
)
927 initbox(const char *name
)
932 if (mb
.mb_type
!= MB_VOID
) {
933 strncpy(prevfile
, mailname
, PATHSIZE
);
934 prevfile
[PATHSIZE
-1]='\0';
936 if (name
!= mailname
) {
937 strncpy(mailname
, name
, PATHSIZE
);
938 mailname
[PATHSIZE
-1]='\0';
940 if ((mb
.mb_otf
= Ftemp(&tempMesg
, "Rx", "w", 0600, 0)) == NULL
) {
941 perror(catgets(catd
, CATSET
, 87,
942 "temporary mail message file"));
945 fcntl(fileno(mb
.mb_otf
), F_SETFD
, FD_CLOEXEC
);
946 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", &dummy
)) == NULL
) {
950 fcntl(fileno(mb
.mb_itf
), F_SETFD
, FD_CLOEXEC
);
962 mb
.mb_flags
= MB_NOFLAGS
;