2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)cmd3.c 8.2 (Berkeley) 4/20/95
34 * $FreeBSD: src/usr.bin/mail/cmd3.c,v 1.4.6.4 2003/01/06 05:46:03 mikeh Exp $
41 * Mail -- a mail program
43 * Still more user commands.
47 * Process a shell escape by saving signals, ignoring signals,
53 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
57 if (strlcpy(cmd
, str
, sizeof(cmd
)) >= sizeof(cmd
))
59 if (bangexp(cmd
, sizeof(cmd
)) < 0)
61 if ((sh
= value("SHELL")) == NULL
)
63 run_command(sh
, 0, -1, -1, "-c", cmd
, NULL
);
64 signal(SIGINT
, sigint
);
70 * Fork an interactive shell.
76 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
79 if ((sh
= value("SHELL")) == NULL
)
81 run_command(sh
, 0, -1, -1, NULL
, NULL
, NULL
);
82 signal(SIGINT
, sigint
);
88 * Expand the shell escape by expanding unescaped !'s into the
89 * last issued command where possible.
92 bangexp(char *str
, size_t strsize
)
95 static char lastbang
[BUFSIZ
];
102 while (*cp
!= '\0') {
104 if (n
< strlen(lastbang
)) {
106 printf("Command buffer overflow\n");
110 if (strlcpy(cp2
, lastbang
, sizeof(bangbuf
) - (cp2
- bangbuf
))
111 >= sizeof(bangbuf
) - (cp2
- bangbuf
))
113 cp2
+= strlen(lastbang
);
114 n
-= strlen(lastbang
);
118 if (*cp
== '\\' && cp
[1] == '!') {
131 printf("!%s\n", bangbuf
);
134 if (strlcpy(str
, bangbuf
, strsize
) >= strsize
)
136 if (strlcpy(lastbang
, bangbuf
, sizeof(lastbang
)) >= sizeof(lastbang
))
142 * Print out a nice help message from some file or another.
151 if ((f
= Fopen(_PATH_HELP
, "r")) == NULL
) {
152 warn("%s", _PATH_HELP
);
155 while ((c
= getc(f
)) != EOF
)
162 * Change user's working directory.
165 schdir(char **arglist
)
169 if (*arglist
== NULL
) {
174 if ((cp
= expand(*arglist
)) == NULL
)
186 if (value("Replyall") == NULL
&& value("flipr") == NULL
)
187 return (dorespond(msgvec
));
189 return (doRespond(msgvec
));
193 * Reply to a list of messages. Extract each name from the
194 * message header and send them off to mail1()
197 dorespond(int *msgvec
)
200 char *cp
, *rcv
, *replyto
;
205 if (msgvec
[1] != 0) {
206 printf("Sorry, can't reply to multiple messages at once\n");
209 mp
= &message
[msgvec
[0] - 1];
212 if ((rcv
= skin(hfield("from", mp
))) == NULL
)
213 rcv
= skin(nameof(mp
, 1));
214 if ((replyto
= skin(hfield("reply-to", mp
))) != NULL
)
215 np
= extract(replyto
, GTO
);
216 else if ((cp
= skin(hfield("to", mp
))) != NULL
)
217 np
= extract(cp
, GTO
);
222 * Delete my name from the reply list,
223 * and with it, all my alternate names.
225 np
= delname(np
, myname
);
227 for (ap
= altnames
; *ap
!= NULL
; ap
++)
228 np
= delname(np
, *ap
);
229 if (np
!= NULL
&& replyto
== NULL
)
230 np
= cat(np
, extract(rcv
, GTO
));
231 else if (np
== NULL
) {
233 printf("Empty reply-to field -- replying to author\n");
234 np
= extract(rcv
, GTO
);
237 if ((head
.h_subject
= hfield("subject", mp
)) == NULL
)
238 head
.h_subject
= hfield("subj", mp
);
239 head
.h_subject
= reedit(head
.h_subject
);
240 if (replyto
== NULL
&& (cp
= skin(hfield("cc", mp
))) != NULL
) {
241 np
= elide(extract(cp
, GCC
));
242 np
= delname(np
, myname
);
244 for (ap
= altnames
; *ap
!= NULL
; ap
++)
245 np
= delname(np
, *ap
);
250 head
.h_smopts
= NULL
;
251 head
.h_replyto
= value("REPLYTO");
252 head
.h_inreplyto
= skin(hfield("message-id", mp
));
258 * Modify the subject we are replying to to begin with Re: if
259 * it does not already.
268 if ((subj
[0] == 'r' || subj
[0] == 'R') &&
269 (subj
[1] == 'e' || subj
[1] == 'E') &&
272 newsubj
= salloc(strlen(subj
) + 5);
273 sprintf(newsubj
, "Re: %s", subj
);
278 * Preserve the named messages, so that they will be sent
279 * back to the system mailbox.
282 preserve(int *msgvec
)
288 printf("Cannot \"preserve\" in edit mode\n");
291 for (ip
= msgvec
; *ip
!= 0; ip
++) {
293 mp
= &message
[mesg
-1];
294 mp
->m_flag
|= MPRESERVE
;
302 * Mark all given messages as unread.
309 for (ip
= msgvec
; *ip
!= 0; ip
++) {
310 dot
= &message
[*ip
-1];
311 dot
->m_flag
&= ~(MREAD
|MTOUCH
);
312 dot
->m_flag
|= MSTATUS
;
318 * Print the size of each message.
326 for (ip
= msgvec
; *ip
!= 0; ip
++) {
328 mp
= &message
[mesg
-1];
329 printf("%d: %ld/%ld\n", mesg
, mp
->m_lines
, mp
->m_size
);
335 * Quit quickly. If we are sourcing, just pop the input level
336 * by returning an error.
348 * Set or display a variable value. Syntax is similar to that
356 char varbuf
[BUFSIZ
], **ap
, **p
;
359 if (*arglist
== NULL
) {
360 for (h
= 0, s
= 1; h
< HSHSIZE
; h
++)
361 for (vp
= variables
[h
]; vp
!= NULL
; vp
= vp
->v_link
)
363 ap
= (char **)salloc(s
* sizeof(*ap
));
364 for (h
= 0, p
= ap
; h
< HSHSIZE
; h
++)
365 for (vp
= variables
[h
]; vp
!= NULL
; vp
= vp
->v_link
)
369 for (p
= ap
; *p
!= NULL
; p
++)
370 printf("%s\t%s\n", *p
, value(*p
));
374 for (ap
= arglist
; *ap
!= NULL
; ap
++) {
377 while (cp2
< varbuf
+ sizeof(varbuf
) - 1 && *cp
!= '=' && *cp
!= '\0')
384 if (equal(varbuf
, "")) {
385 printf("Non-null variable name required\n");
395 * Unset a bunch of variable values.
398 unset(char **arglist
)
400 struct var
*vp
, *vp2
;
405 for (ap
= arglist
; *ap
!= NULL
; ap
++) {
406 if ((vp2
= lookup(*ap
)) == NULL
) {
409 else if (!sourcing
) {
410 printf("\"%s\": undefined variable\n", *ap
);
416 if (vp2
== variables
[h
]) {
417 variables
[h
] = variables
[h
]->v_link
;
423 for (vp
= variables
[h
]; vp
->v_link
!= vp2
; vp
= vp
->v_link
)
425 vp
->v_link
= vp2
->v_link
;
434 * Put add users to a group.
439 struct grouphead
*gh
;
441 char **ap
, *gname
, **p
;
445 for (h
= 0, s
= 1; h
< HSHSIZE
; h
++)
446 for (gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
)
448 ap
= (char **)salloc(s
* sizeof(*ap
));
449 for (h
= 0, p
= ap
; h
< HSHSIZE
; h
++)
450 for (gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
)
454 for (p
= ap
; *p
!= NULL
; p
++)
458 if (argv
[1] == NULL
) {
464 if ((gh
= findgroup(gname
)) == NULL
) {
465 gh
= calloc(sizeof(*gh
), 1);
466 gh
->g_name
= vcopy(gname
);
468 gh
->g_link
= groups
[h
];
473 * Insert names from the command list into the group.
474 * Who cares if there are duplicates? They get tossed
478 for (ap
= argv
+1; *ap
!= NULL
; ap
++) {
479 gp
= calloc(sizeof(*gp
), 1);
480 gp
->ge_name
= vcopy(*ap
);
481 gp
->ge_link
= gh
->g_list
;
488 * Sort the passed string vecotor into ascending dictionary
496 for (ap
= list
; *ap
!= NULL
; ap
++)
500 qsort(list
, ap
-list
, sizeof(*list
), diction
);
504 * Do a dictionary order comparison of the arguments from
508 diction(const void *a
, const void *b
)
510 return (strcmp(*(const char **)a
, *(const char **)b
));
514 * The do nothing command for comments.
525 * Change to another file. With no argument, print information about
531 if (argv
[0] == NULL
) {
535 if (setfile(*argv
) < 0)
542 * Expand file names like echo
549 for (ap
= argv
; *ap
!= NULL
; ap
++) {
551 if ((cp
= expand(cp
)) != NULL
) {
564 if (value("Replyall") == NULL
&& value("flipr") == NULL
)
565 return (doRespond(msgvec
));
567 return (dorespond(msgvec
));
571 * Reply to a series of messages by simply mailing to the senders
572 * and not messing around with the To: and Cc: lists as in normal
576 doRespond(int *msgvec
)
584 for (ap
= msgvec
; *ap
!= 0; ap
++) {
585 mp
= &message
[*ap
- 1];
588 if ((cp
= skin(hfield("from", mp
))) == NULL
)
589 cp
= skin(nameof(mp
, 2));
590 head
.h_to
= cat(head
.h_to
, extract(cp
, GTO
));
591 mid
= skin(hfield("message-id", mp
));
593 if (head
.h_to
== NULL
)
595 mp
= &message
[msgvec
[0] - 1];
596 if ((head
.h_subject
= hfield("subject", mp
)) == NULL
)
597 head
.h_subject
= hfield("subj", mp
);
598 head
.h_subject
= reedit(head
.h_subject
);
601 head
.h_smopts
= NULL
;
602 head
.h_replyto
= value("REPLYTO");
603 head
.h_inreplyto
= mid
;
609 * Conditional commands. These allow one to parameterize one's
610 * .mailrc and do some things if sending, others if receiving.
618 printf("Illegal nested \"if\"\n");
633 printf("Unrecognized if-keyword: \"%s\"\n", cp
);
640 * Implement 'else'. This is pretty simple -- we just
641 * flip over the conditional flag.
648 printf("\"Else\" without matching \"if\"\n");
660 printf("Mail's idea of conditions is screwed up\n");
668 * End of if statement. Just set cond back to anything.
674 printf("\"Endif\" without matching \"if\"\n");
682 * Set the list of alternate names.
685 alternates(char **namelist
)
688 char **ap
, **ap2
, *cp
;
690 c
= argcount(namelist
) + 1;
694 for (ap
= altnames
; *ap
!= NULL
; ap
++)
701 altnames
= calloc((unsigned)c
, sizeof(char *));
702 for (ap
= namelist
, ap2
= altnames
; *ap
!= NULL
; ap
++, ap2
++) {
703 cp
= calloc((unsigned)strlen(*ap
) + 1, sizeof(char));