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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)cmd3.c 8.2 (Berkeley) 4/20/95
30 * $FreeBSD: src/usr.bin/mail/cmd3.c,v 1.4.6.4 2003/01/06 05:46:03 mikeh Exp $
37 * Mail -- a mail program
39 * Still more user commands.
43 * Process a shell escape by saving signals, ignoring signals,
49 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
53 if (strlcpy(cmd
, str
, sizeof(cmd
)) >= sizeof(cmd
))
55 if (bangexp(cmd
, sizeof(cmd
)) < 0)
57 if ((sh
= value("SHELL")) == NULL
)
59 run_command(sh
, 0, -1, -1, "-c", cmd
, NULL
);
60 signal(SIGINT
, sigint
);
66 * Fork an interactive shell.
72 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
75 if ((sh
= value("SHELL")) == NULL
)
77 run_command(sh
, 0, -1, -1, NULL
, NULL
, NULL
);
78 signal(SIGINT
, sigint
);
84 * Expand the shell escape by expanding unescaped !'s into the
85 * last issued command where possible.
88 bangexp(char *str
, size_t strsize
)
91 static char lastbang
[BUFSIZ
];
100 if (n
< strlen(lastbang
)) {
102 printf("Command buffer overflow\n");
106 if (strlcpy(cp2
, lastbang
, sizeof(bangbuf
) - (cp2
- bangbuf
))
107 >= sizeof(bangbuf
) - (cp2
- bangbuf
))
109 cp2
+= strlen(lastbang
);
110 n
-= strlen(lastbang
);
114 if (*cp
== '\\' && cp
[1] == '!') {
127 printf("!%s\n", bangbuf
);
130 if (strlcpy(str
, bangbuf
, strsize
) >= strsize
)
132 if (strlcpy(lastbang
, bangbuf
, sizeof(lastbang
)) >= sizeof(lastbang
))
138 * Print out a nice help message from some file or another.
147 if ((f
= Fopen(_PATH_HELP
, "r")) == NULL
) {
148 warn("%s", _PATH_HELP
);
151 while ((c
= getc(f
)) != EOF
)
158 * Change user's working directory.
161 schdir(char **arglist
)
165 if (*arglist
== NULL
) {
170 if ((cp
= expand(*arglist
)) == NULL
)
182 if (value("Replyall") == NULL
&& value("flipr") == NULL
)
183 return (dorespond(msgvec
));
185 return (doRespond(msgvec
));
189 * Reply to a list of messages. Extract each name from the
190 * message header and send them off to mail1()
193 dorespond(int *msgvec
)
196 char *cp
, *rcv
, *replyto
;
201 if (msgvec
[1] != 0) {
202 printf("Sorry, can't reply to multiple messages at once\n");
205 mp
= &message
[msgvec
[0] - 1];
208 if ((rcv
= skin(hfield("from", mp
))) == NULL
)
209 rcv
= skin(nameof(mp
, 1));
210 if ((replyto
= skin(hfield("reply-to", mp
))) != NULL
)
211 np
= extract(replyto
, GTO
);
212 else if ((cp
= skin(hfield("to", mp
))) != NULL
)
213 np
= extract(cp
, GTO
);
218 * Delete my name from the reply list,
219 * and with it, all my alternate names.
221 np
= delname(np
, myname
);
223 for (ap
= altnames
; *ap
!= NULL
; ap
++)
224 np
= delname(np
, *ap
);
225 if (np
!= NULL
&& replyto
== NULL
)
226 np
= cat(np
, extract(rcv
, GTO
));
227 else if (np
== NULL
) {
229 printf("Empty reply-to field -- replying to author\n");
230 np
= extract(rcv
, GTO
);
233 if ((head
.h_subject
= hfield("subject", mp
)) == NULL
)
234 head
.h_subject
= hfield("subj", mp
);
235 head
.h_subject
= reedit(head
.h_subject
);
236 if (replyto
== NULL
&& (cp
= skin(hfield("cc", mp
))) != NULL
) {
237 np
= elide(extract(cp
, GCC
));
238 np
= delname(np
, myname
);
240 for (ap
= altnames
; *ap
!= NULL
; ap
++)
241 np
= delname(np
, *ap
);
246 head
.h_smopts
= NULL
;
247 head
.h_replyto
= value("REPLYTO");
248 head
.h_inreplyto
= skin(hfield("message-id", mp
));
254 * Modify the subject we are replying to to begin with Re: if
255 * it does not already.
264 if ((subj
[0] == 'r' || subj
[0] == 'R') &&
265 (subj
[1] == 'e' || subj
[1] == 'E') &&
268 newsubj
= salloc(strlen(subj
) + 5);
269 sprintf(newsubj
, "Re: %s", subj
);
274 * Preserve the named messages, so that they will be sent
275 * back to the system mailbox.
278 preserve(int *msgvec
)
284 printf("Cannot \"preserve\" in edit mode\n");
287 for (ip
= msgvec
; *ip
!= 0; ip
++) {
289 mp
= &message
[mesg
-1];
290 mp
->m_flag
|= MPRESERVE
;
298 * Mark all given messages as unread.
305 for (ip
= msgvec
; *ip
!= 0; ip
++) {
306 dot
= &message
[*ip
-1];
307 dot
->m_flag
&= ~(MREAD
|MTOUCH
);
308 dot
->m_flag
|= MSTATUS
;
314 * Print the size of each message.
322 for (ip
= msgvec
; *ip
!= 0; ip
++) {
324 mp
= &message
[mesg
-1];
325 printf("%d: %ld/%ld\n", mesg
, mp
->m_lines
, mp
->m_size
);
331 * Quit quickly. If we are sourcing, just pop the input level
332 * by returning an error.
344 * Set or display a variable value. Syntax is similar to that
352 char varbuf
[BUFSIZ
], **ap
, **p
;
355 if (*arglist
== NULL
) {
356 for (h
= 0, s
= 1; h
< HSHSIZE
; h
++)
357 for (vp
= variables
[h
]; vp
!= NULL
; vp
= vp
->v_link
)
359 ap
= (char **)salloc(s
* sizeof(*ap
));
360 for (h
= 0, p
= ap
; h
< HSHSIZE
; h
++)
361 for (vp
= variables
[h
]; vp
!= NULL
; vp
= vp
->v_link
)
365 for (p
= ap
; *p
!= NULL
; p
++)
366 printf("%s\t%s\n", *p
, value(*p
));
370 for (ap
= arglist
; *ap
!= NULL
; ap
++) {
373 while (cp2
< varbuf
+ sizeof(varbuf
) - 1 && *cp
!= '=' && *cp
!= '\0')
380 if (equal(varbuf
, "")) {
381 printf("Non-null variable name required\n");
391 * Unset a bunch of variable values.
394 unset(char **arglist
)
396 struct var
*vp
, *vp2
;
401 for (ap
= arglist
; *ap
!= NULL
; ap
++) {
402 if ((vp2
= lookup(*ap
)) == NULL
) {
405 else if (!sourcing
) {
406 printf("\"%s\": undefined variable\n", *ap
);
412 if (vp2
== variables
[h
]) {
413 variables
[h
] = variables
[h
]->v_link
;
419 for (vp
= variables
[h
]; vp
->v_link
!= vp2
; vp
= vp
->v_link
)
421 vp
->v_link
= vp2
->v_link
;
430 * Put add users to a group.
435 struct grouphead
*gh
;
437 char **ap
, *gname
, **p
;
441 for (h
= 0, s
= 1; h
< HSHSIZE
; h
++)
442 for (gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
)
444 ap
= (char **)salloc(s
* sizeof(*ap
));
445 for (h
= 0, p
= ap
; h
< HSHSIZE
; h
++)
446 for (gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
)
450 for (p
= ap
; *p
!= NULL
; p
++)
454 if (argv
[1] == NULL
) {
460 if ((gh
= findgroup(gname
)) == NULL
) {
461 gh
= calloc(sizeof(*gh
), 1);
462 gh
->g_name
= vcopy(gname
);
464 gh
->g_link
= groups
[h
];
469 * Insert names from the command list into the group.
470 * Who cares if there are duplicates? They get tossed
474 for (ap
= argv
+1; *ap
!= NULL
; ap
++) {
475 gp
= calloc(sizeof(*gp
), 1);
476 gp
->ge_name
= vcopy(*ap
);
477 gp
->ge_link
= gh
->g_list
;
484 * Sort the passed string vecotor into ascending dictionary
492 for (ap
= list
; *ap
!= NULL
; ap
++)
496 qsort(list
, ap
-list
, sizeof(*list
), diction
);
500 * Do a dictionary order comparison of the arguments from
504 diction(const void *a
, const void *b
)
506 return (strcmp(*(const char **)a
, *(const char **)b
));
510 * The do nothing command for comments.
521 * Change to another file. With no argument, print information about
527 if (argv
[0] == NULL
) {
531 if (setfile(*argv
) < 0)
538 * Expand file names like echo
545 for (ap
= argv
; *ap
!= NULL
; ap
++) {
547 if ((cp
= expand(cp
)) != NULL
) {
560 if (value("Replyall") == NULL
&& value("flipr") == NULL
)
561 return (doRespond(msgvec
));
563 return (dorespond(msgvec
));
567 * Reply to a series of messages by simply mailing to the senders
568 * and not messing around with the To: and Cc: lists as in normal
572 doRespond(int *msgvec
)
580 for (ap
= msgvec
; *ap
!= 0; ap
++) {
581 mp
= &message
[*ap
- 1];
584 if ((cp
= skin(hfield("from", mp
))) == NULL
)
585 cp
= skin(nameof(mp
, 2));
586 head
.h_to
= cat(head
.h_to
, extract(cp
, GTO
));
587 mid
= skin(hfield("message-id", mp
));
589 if (head
.h_to
== NULL
)
591 mp
= &message
[msgvec
[0] - 1];
592 if ((head
.h_subject
= hfield("subject", mp
)) == NULL
)
593 head
.h_subject
= hfield("subj", mp
);
594 head
.h_subject
= reedit(head
.h_subject
);
597 head
.h_smopts
= NULL
;
598 head
.h_replyto
= value("REPLYTO");
599 head
.h_inreplyto
= mid
;
605 * Conditional commands. These allow one to parameterize one's
606 * .mailrc and do some things if sending, others if receiving.
614 printf("Illegal nested \"if\"\n");
629 printf("Unrecognized if-keyword: \"%s\"\n", cp
);
636 * Implement 'else'. This is pretty simple -- we just
637 * flip over the conditional flag.
644 printf("\"Else\" without matching \"if\"\n");
656 printf("Mail's idea of conditions is screwed up\n");
664 * End of if statement. Just set cond back to anything.
670 printf("\"Endif\" without matching \"if\"\n");
678 * Set the list of alternate names.
681 alternates(char **namelist
)
684 char **ap
, **ap2
, *cp
;
686 c
= argcount(namelist
) + 1;
690 for (ap
= altnames
; *ap
!= NULL
; ap
++)
697 altnames
= calloc((unsigned)c
, sizeof(char *));
698 for (ap
= namelist
, ap2
= altnames
; *ap
!= NULL
; ap
++, ap2
++) {
699 cp
= calloc((unsigned)strlen(*ap
) + 1, sizeof(char));