2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid
[] = "@(#)lex.c 2.86 (gritter) 12/25/06";
53 * Mail -- a mail program
55 * Lexical processing of commands.
59 static sighandler_type oldpipe
;
61 static const struct cmd
*lex(char *Word
);
62 static void stop(int s
);
63 static void hangup(int s
);
66 * Set up editing on the given file name.
67 * If the first character of name is %, we are considered to be
68 * editing the file, otherwise we are reading our mail which has
69 * signficance for mbox and so forth.
71 * newmail: Check for new mail in the current folder only.
74 setfile(char *name
, int newmail
)
77 int i
, compressed
= 0;
80 char *who
= name
[1] ? name
+ 1 : myname
;
87 isedit
= *name
!= '%' && ((sh
= get_shortcut(name
)) == NULL
||
89 if ((name
= expand(name
)) == NULL
)
92 switch (which_protocol(name
)) {
96 return maildir_setfile(name
, newmail
, isedit
);
99 return pop3_setfile(name
, newmail
, isedit
);
103 if (mb
.mb_type
== MB_CACHE
)
105 omsgCount
= msgCount
;
107 return imap_setfile(name
, newmail
, isedit
);
109 fprintf(stderr
, catgets(catd
, CATSET
, 217,
110 "Cannot handle protocol: %s\n"), name
);
113 if ((ibuf
= Zopen(name
, "r", &compressed
)) == NULL
) {
114 if ((!isedit
&& errno
== ENOENT
) || newmail
) {
123 if (fstat(fileno(ibuf
), &stb
) < 0) {
131 if (S_ISDIR(stb
.st_mode
)) {
138 } else if (S_ISREG(stb
.st_mode
)) {
150 * Looks like all will be well. We must now relinquish our
151 * hold on the current set of stuff. Must hold signals
152 * while we are reading the new file, else we will ruin
153 * the message[] data structure.
157 if (shudclob
&& !newmail
)
161 if (!newmail
&& mb
.mb_sock
.s_fd
>= 0)
163 #endif /* HAVE_SOCKETS */
166 * Copy the messages into /tmp
170 flp
.l_type
= F_RDLCK
;
172 flp
.l_whence
= SEEK_SET
;
174 mb
.mb_type
= MB_FILE
;
175 mb
.mb_perm
= Rflag
? 0 : MB_DELE
|MB_EDIT
;
176 mb
.mb_compressed
= compressed
;
178 if (compressed
& 0200)
181 if ((i
= open(name
, O_WRONLY
)) < 0)
201 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) < 0) {
202 perror("Unable to lock mailbox");
206 } else /* newmail */{
207 fseek(mb
.mb_otf
, 0L, SEEK_END
);
208 fseek(ibuf
, mailsize
, SEEK_SET
);
210 omsgCount
= msgCount
;
212 if (!edit
&& fcntl(fileno(ibuf
), F_SETLKW
, &flp
) < 0)
215 mailsize
= fsize(ibuf
);
216 if (newmail
&& mailsize
<= offset
) {
220 setptr(ibuf
, offset
);
222 if (newmail
&& mb
.mb_sorted
) {
230 if ((!edit
|| newmail
) && msgCount
== 0) {
233 if (value("emptystart") == NULL
)
234 nomail
: fprintf(stderr
, catgets(catd
, CATSET
, 88,
235 "No mail for %s\n"), who
);
240 newmailinfo(omsgCount
);
247 newmailinfo(int omsgCount
)
252 for (i
= 0; i
< omsgCount
; i
++)
253 message
[i
].m_flag
&= ~MNEWEST
;
254 if (msgCount
> omsgCount
) {
255 for (i
= omsgCount
; i
< msgCount
; i
++)
256 message
[i
].m_flag
|= MNEWEST
;
257 printf(catgets(catd
, CATSET
, 158, "New mail has arrived.\n"));
258 if (msgCount
- omsgCount
== 1)
259 printf(catgets(catd
, CATSET
, 214,
260 "Loaded 1 new message\n"));
262 printf(catgets(catd
, CATSET
, 215,
263 "Loaded %d new messages\n"),
264 msgCount
- omsgCount
);
266 printf("Loaded %d messages\n", msgCount
);
267 callhook(mailname
, 1);
269 if (value("header")) {
270 if (mb
.mb_type
== MB_IMAP
)
271 imap_getheaders(omsgCount
+1, msgCount
);
272 while (++omsgCount
<= msgCount
)
273 if (visible(&message
[omsgCount
-1]))
274 printhead(omsgCount
, stdout
, 0);
280 static int reset_on_stop
; /* do a reset() if stopped */
283 * Interpret user commands one by one. If standard input is not a tty,
291 char *linebuf
= NULL
, *av
, *nv
;
296 if (safe_signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
297 safe_signal(SIGINT
, onintr
);
298 if (safe_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
299 safe_signal(SIGHUP
, hangup
);
300 safe_signal(SIGTSTP
, stop
);
301 safe_signal(SIGTTOU
, stop
);
302 safe_signal(SIGTTIN
, stop
);
304 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
305 safe_signal(SIGPIPE
, oldpipe
);
309 handlerstacktop
= NULL
;
311 * Print the prompt, if needed. Clear out
312 * string space, and flush the output.
314 if (!sourcing
&& value("interactive") != NULL
) {
315 av
= (av
= value("autoinc")) ? savestr(av
) : NULL
;
316 nv
= (nv
= value("newmail")) ? savestr(nv
) : NULL
;
317 if (is_a_tty
[0] && (av
!= NULL
|| nv
!= NULL
||
318 mb
.mb_type
== MB_IMAP
)) {
321 n
= (av
&& strcmp(av
, "noimap") &&
322 strcmp(av
, "nopoll")) |
323 (nv
&& strcmp(nv
, "noimap") &&
324 strcmp(nv
, "nopoll"));
326 if ((mb
.mb_type
== MB_FILE
&&
327 stat(mailname
, &st
) == 0 &&
328 st
.st_size
> mailsize
) ||
329 (mb
.mb_type
== MB_IMAP
&&
330 imap_newmail(n
) > x
) ||
331 (mb
.mb_type
== MB_MAILDIR
&&
333 int odot
= dot
- &message
[0];
334 int odid
= did_print_dot
;
336 setfile(mailname
, 1);
337 if (mb
.mb_type
!= MB_IMAP
) {
338 dot
= &message
[odot
];
339 did_print_dot
= odid
;
344 if ((prompt
= value("prompt")) == NULL
)
345 prompt
= value("bsdcompat") ? "& " : "? ";
346 printf("%s", prompt
);
351 * Read a line of commands from the current input
352 * and handle end of file specially.
356 n
= readline_restart(input
, &linebuf
, &linesize
, n
);
359 if (n
== 0 || linebuf
[n
- 1] != '\\')
361 linebuf
[n
- 1] = ' ';
372 if (value("interactive") != NULL
&&
373 value("ignoreeof") != NULL
&&
375 printf(catgets(catd
, CATSET
, 89,
376 "Use \"quit\" to quit.\n"));
383 if (execute(linebuf
, 0, n
))
391 * Execute a single command.
392 * Command functions return 0 for success, 1 for error, and -1
393 * for abort. A 1 or -1 aborts a load or source. A -1 aborts
394 * the interactive command loop.
395 * Contxt is non-zero if called while composing mail.
398 execute(char *linebuf
, int contxt
, size_t linesize
)
401 char *arglist
[MAXARGC
];
402 const struct cmd
*com
= (struct cmd
*)NULL
;
409 * Strip the white space away from the beginning
410 * of the command, then scan out a word, which
411 * consists of anything except digits and white space.
413 * Handle ! escapes differently to get the correct
414 * lexical conventions.
416 word
= ac_alloc(linesize
+ 1);
417 for (cp
= linebuf
; whitechar(*cp
& 0377); cp
++);
420 printf(catgets(catd
, CATSET
, 90,
421 "Can't \"!\" while sourcing\n"));
434 while (*cp
&& strchr(" \t0123456789$^.:/-+*'\",;(`", *cp
)
442 * Look up the command; if not found, bitch.
443 * Normally, a blank command would map to the
444 * first command in the table; while sourcing,
445 * however, we ignore blank lines to eliminate
449 if (sourcing
&& *word
== '\0') {
455 printf(catgets(catd
, CATSET
, 91,
456 "Unknown command: \"%s\"\n"), word
);
461 * See if we should execute the command -- if a conditional
462 * we always execute it, otherwise, check the state of cond.
465 if ((com
->c_argtype
& F
) == 0) {
466 if ((cond
== CRCV
&& !rcvmode
) ||
467 (cond
== CSEND
&& rcvmode
) ||
468 (cond
== CTERM
&& !is_a_tty
[0]) ||
469 (cond
== CNONTERM
&& is_a_tty
[0])) {
476 * Process the arguments to the command, depending
477 * on the type he expects. Default to an error.
478 * If we are sourcing an interactive command, it's
482 if (!rcvmode
&& (com
->c_argtype
& M
) == 0) {
483 printf(catgets(catd
, CATSET
, 92,
484 "May not execute \"%s\" while sending\n"), com
->c_name
);
487 if (sourcing
&& com
->c_argtype
& I
) {
488 printf(catgets(catd
, CATSET
, 93,
489 "May not execute \"%s\" while sourcing\n"),
493 if ((mb
.mb_perm
& MB_DELE
) == 0 && com
->c_argtype
& W
) {
494 printf(catgets(catd
, CATSET
, 94,
495 "May not execute \"%s\" -- message file is read only\n"),
499 if (contxt
&& com
->c_argtype
& R
) {
500 printf(catgets(catd
, CATSET
, 95,
501 "Cannot recursively invoke \"%s\"\n"), com
->c_name
);
504 if (mb
.mb_type
== MB_VOID
&& com
->c_argtype
& A
) {
505 printf(catgets(catd
, CATSET
, 257,
506 "Cannot execute \"%s\" without active mailbox\n"),
510 switch (com
->c_argtype
& ~(F
|P
|I
|M
|T
|W
|R
|A
)) {
513 * A message list defaulting to nearest forward
517 printf(catgets(catd
, CATSET
, 96,
518 "Illegal use of \"message list\"\n"));
521 if ((c
= getmsglist(cp
, msgvec
, com
->c_msgflag
)) < 0)
524 if ((*msgvec
= first(com
->c_msgflag
, com
->c_msgmask
))
530 printf(catgets(catd
, CATSET
, 97,
531 "No applicable messages\n"));
534 e
= (*com
->c_func
)(msgvec
);
539 * A message list with no defaults, but no error
543 printf(catgets(catd
, CATSET
, 98,
544 "Illegal use of \"message list\"\n"));
547 if (getmsglist(cp
, msgvec
, com
->c_msgflag
) < 0)
549 e
= (*com
->c_func
)(msgvec
);
554 * Just the straight string, with
555 * leading blanks removed.
557 while (whitechar(*cp
& 0377))
559 e
= (*com
->c_func
)(cp
);
565 * A vector of strings, in shell style.
567 if ((c
= getrawlist(cp
, linesize
, arglist
,
568 sizeof arglist
/ sizeof *arglist
,
569 (com
->c_argtype
&~(F
|P
|I
|M
|T
|W
|R
|A
))==ECHOLIST
))
572 if (c
< com
->c_minargs
) {
573 printf(catgets(catd
, CATSET
, 99,
574 "%s requires at least %d arg(s)\n"),
575 com
->c_name
, com
->c_minargs
);
578 if (c
> com
->c_maxargs
) {
579 printf(catgets(catd
, CATSET
, 100,
580 "%s takes no more than %d arg(s)\n"),
581 com
->c_name
, com
->c_maxargs
);
584 e
= (*com
->c_func
)(arglist
);
589 * Just the constant zero, for exiting,
592 e
= (*com
->c_func
)(0);
596 panic(catgets(catd
, CATSET
, 101, "Unknown argtype"));
602 * Exit the current source file on
614 if (com
== (struct cmd
*)NULL
)
616 if (value("autoprint") != NULL
&& com
->c_argtype
& P
)
618 muvec
[0] = dot
- &message
[0] + 1;
622 if (!sourcing
&& !inhook
&& (com
->c_argtype
& T
) == 0)
628 * Set the size of the message vector used to construct argument
629 * lists to message list functions.
637 msgvec
= (int *)scalloc((sz
+ 1), sizeof *msgvec
);
641 * Find the correct command in the command table corresponding
642 * to the passed command "word"
645 static const struct cmd
*
648 extern const struct cmd cmdtab
[];
649 const struct cmd
*cp
;
651 for (cp
= &cmdtab
[0]; cp
->c_name
!= NULL
; cp
++)
652 if (is_prefix(Word
, cp
->c_name
))
658 * The following gets called on receipt of an interrupt. This is
659 * to abort printout of a command, mainly.
660 * Dispatching here when command() is inactive crashes rcv.
661 * Close all open files except 0, 1, 2, and the temporary.
662 * Also, unstack all source files.
665 static int inithdr
; /* am printing startup headers */
671 if (handlerstacktop
!= NULL
) {
675 safe_signal(SIGINT
, onintr
);
690 fprintf(stderr
, catgets(catd
, CATSET
, 102, "Interrupt\n"));
691 safe_signal(SIGPIPE
, oldpipe
);
696 * When we wake up after ^Z, reprint the prompt.
701 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
706 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
708 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
709 safe_signal(s
, old_action
);
717 * Branch here on hangup signal and simulate "exit".
729 * Announce the presence of the current Mail version,
730 * give the message count, and print a header listing.
733 announce(int printheaders
)
737 mdot
= newfileinfo();
740 dot
= &message
[mdot
- 1];
741 if (printheaders
&& msgCount
> 0 && value("header") != NULL
) {
749 * Announce information about the file we are editing.
750 * Return a likely place to set dot.
756 int u
, n
, mdot
, d
, s
, hidden
, killed
, moved
;
757 char fname
[PATHSIZE
], zname
[PATHSIZE
], *ename
;
759 if (mb
.mb_type
== MB_VOID
)
762 s
= d
= hidden
= killed
= moved
=0;
763 for (mp
= &message
[0], n
= 0, u
= 0; mp
< &message
[msgCount
]; mp
++) {
764 if (mp
->m_flag
& MNEW
)
766 if ((mp
->m_flag
& MREAD
) == 0)
768 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == (MDELETED
|MSAVED
))
770 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == MDELETED
)
772 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == MSAVED
)
774 if (mp
->m_flag
& MHIDDEN
)
776 if (mp
->m_flag
& MKILL
)
780 if (getfold(fname
, sizeof fname
- 1) >= 0) {
782 if (which_protocol(fname
) != PROTO_IMAP
&&
783 strncmp(fname
, mailname
, strlen(fname
)) == 0) {
784 snprintf(zname
, sizeof zname
, "+%s",
785 mailname
+ strlen(fname
));
789 printf(catgets(catd
, CATSET
, 103, "\"%s\": "), ename
);
791 printf(catgets(catd
, CATSET
, 104, "1 message"));
793 printf(catgets(catd
, CATSET
, 105, "%d messages"), msgCount
);
795 printf(catgets(catd
, CATSET
, 106, " %d new"), n
);
797 printf(catgets(catd
, CATSET
, 107, " %d unread"), u
);
799 printf(catgets(catd
, CATSET
, 108, " %d deleted"), d
);
801 printf(catgets(catd
, CATSET
, 109, " %d saved"), s
);
803 printf(catgets(catd
, CATSET
, 109, " %d moved"), moved
);
805 printf(catgets(catd
, CATSET
, 109, " %d hidden"), hidden
);
807 printf(catgets(catd
, CATSET
, 109, " %d killed"), killed
);
808 if (mb
.mb_type
== MB_CACHE
)
809 printf(" [Disconnected]");
810 else if (mb
.mb_perm
== 0)
811 printf(catgets(catd
, CATSET
, 110, " [Read only]"));
822 enum mflag avoid
= MHIDDEN
|MKILL
|MDELETED
;
825 if (value("autothread"))
827 else if ((cp
= value("autosort")) != NULL
) {
829 mb
.mb_sorted
= sstrdup(cp
);
833 if (mb
.mb_type
== MB_VOID
)
836 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
837 if ((mp
->m_flag
& (MNEWEST
|avoid
)) == MNEWEST
)
839 if (!newmail
|| mp
>= &message
[msgCount
]) {
840 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
842 mp
!= NULL
: mp
< &message
[msgCount
];
844 mp
= next_in_thread(mp
) : mp
++)
845 if ((mp
->m_flag
& (MNEW
|avoid
)) == MNEW
)
848 if (mb
.mb_threaded
? mp
== NULL
: mp
>= &message
[msgCount
])
849 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
850 mb
.mb_threaded
? mp
!= NULL
:
851 mp
< &message
[msgCount
];
852 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++)
853 if (mp
->m_flag
& MFLAGGED
)
855 if (mb
.mb_threaded
? mp
== NULL
: mp
>= &message
[msgCount
])
856 for (mp
= mb
.mb_threaded
? threadroot
: &message
[0];
857 mb
.mb_threaded
? mp
!= NULL
:
858 mp
< &message
[msgCount
];
859 mb
.mb_threaded
? mp
= next_in_thread(mp
) : mp
++)
860 if ((mp
->m_flag
& (MREAD
|avoid
)) == 0)
862 if (mb
.mb_threaded
? mp
!= NULL
: mp
< &message
[msgCount
])
863 mdot
= mp
- &message
[0] + 1;
864 else if (value("showlast")) {
865 if (mb
.mb_threaded
) {
866 for (mp
= this_in_thread(threadroot
, -1); mp
;
867 mp
= prev_in_thread(mp
))
868 if ((mp
->m_flag
& avoid
) == 0)
870 mdot
= mp
? mp
- &message
[0] + 1 : msgCount
;
872 for (mp
= &message
[msgCount
-1]; mp
>= &message
[0]; mp
--)
873 if ((mp
->m_flag
& avoid
) == 0)
875 mdot
= mp
>= &message
[0] ? mp
-&message
[0]+1 : msgCount
;
877 } else if (mb
.mb_threaded
) {
878 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
879 if ((mp
->m_flag
& avoid
) == 0)
881 mdot
= mp
? mp
- &message
[0] + 1 : 1;
883 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
884 if ((mp
->m_flag
& avoid
) == 0)
886 mdot
= mp
< &message
[msgCount
] ? mp
-&message
[0]+1 : 1;
892 * Print the current version number.
899 printf(catgets(catd
, CATSET
, 111, "Version %s\n"), version
);
904 * Load a file of user definitions.
911 if ((in
= Fopen(name
, "r")) == NULL
)
925 initbox(const char *name
)
930 if (mb
.mb_type
!= MB_VOID
) {
931 strncpy(prevfile
, mailname
, PATHSIZE
);
932 prevfile
[PATHSIZE
-1]='\0';
934 if (name
!= mailname
) {
935 strncpy(mailname
, name
, PATHSIZE
);
936 mailname
[PATHSIZE
-1]='\0';
938 if ((mb
.mb_otf
= Ftemp(&tempMesg
, "Rx", "w", 0600, 0)) == NULL
) {
939 perror(catgets(catd
, CATSET
, 87,
940 "temporary mail message file"));
943 fcntl(fileno(mb
.mb_otf
), F_SETFD
, FD_CLOEXEC
);
944 if ((mb
.mb_itf
= safe_fopen(tempMesg
, "r", &dummy
)) == NULL
) {
948 fcntl(fileno(mb
.mb_itf
), F_SETFD
, FD_CLOEXEC
);
960 mb
.mb_flags
= MB_NOFLAGS
;