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 $
35 * $DragonFly: src/usr.bin/mail/cmd3.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
42 * Mail -- a mail program
44 * Still more user commands.
48 * Process a shell escape by saving signals, ignoring signals,
55 sig_t sigint = signal(SIGINT, SIG_IGN);
59 if (strlcpy(cmd, str, sizeof(cmd)) >= sizeof(cmd))
61 if (bangexp(cmd, sizeof(cmd)) < 0)
63 if ((sh = value("SHELL")) == NULL)
65 (void)run_command(sh, 0, -1, -1, "-c", cmd, NULL);
66 (void)signal(SIGINT, sigint);
72 * Fork an interactive shell.
79 sig_t sigint = signal(SIGINT, SIG_IGN);
82 if ((sh = value("SHELL")) == NULL)
84 (void)run_command(sh, 0, -1, -1, NULL, NULL, NULL);
85 (void)signal(SIGINT, sigint);
91 * Expand the shell escape by expanding unescaped !'s into the
92 * last issued command where possible.
100 static char lastbang[BUFSIZ];
107 while (*cp != '\0') {
109 if (n < strlen(lastbang)) {
111 printf("Command buffer overflow\n");
115 if (strlcpy(cp2, lastbang, sizeof(bangbuf) - (cp2 - bangbuf))
116 >= sizeof(bangbuf) - (cp2 - bangbuf))
118 cp2 += strlen(lastbang);
119 n -= strlen(lastbang);
123 if (*cp == '\\' && cp[1] == '!') {
136 printf("!%s\n", bangbuf);
137 (void)fflush(stdout);
139 if (strlcpy(str, bangbuf, strsize) >= strsize)
141 if (strlcpy(lastbang, bangbuf, sizeof(lastbang)) >= sizeof(lastbang))
147 * Print out a nice help message from some file or another.
156 if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
157 warn("%s", _PATH_HELP);
160 while ((c = getc(f)) != EOF)
167 * Change user's working directory.
175 if (*arglist == NULL) {
180 if ((cp = expand(*arglist)) == NULL)
193 if (value("Replyall") == NULL && value("flipr") == NULL)
194 return (dorespond(msgvec));
196 return (doRespond(msgvec));
200 * Reply to a list of messages. Extract each name from the
201 * message header and send them off to mail1()
208 char *cp, *rcv, *replyto;
213 if (msgvec[1] != 0) {
214 printf("Sorry, can't reply to multiple messages at once\n");
217 mp = &message[msgvec[0] - 1];
220 if ((rcv = skin(hfield("from", mp))) == NULL)
221 rcv = skin(nameof(mp, 1));
222 if ((replyto = skin(hfield("reply-to", mp))) != NULL)
223 np = extract(replyto, GTO);
224 else if ((cp = skin(hfield("to", mp))) != NULL)
225 np = extract(cp, GTO);
230 * Delete my name from the reply list,
231 * and with it, all my alternate names.
233 np = delname(np, myname);
235 for (ap = altnames; *ap != NULL; ap++)
236 np = delname(np, *ap);
237 if (np != NULL && replyto == NULL)
238 np = cat(np, extract(rcv, GTO));
239 else if (np == NULL) {
241 printf("Empty reply-to field -- replying to author\n");
242 np = extract(rcv, GTO);
245 if ((head.h_subject = hfield("subject", mp)) == NULL)
246 head.h_subject = hfield("subj", mp);
247 head.h_subject = reedit(head.h_subject);
248 if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) {
249 np = elide(extract(cp, GCC));
250 np = delname(np, myname);
252 for (ap = altnames; *ap != NULL; ap++)
253 np = delname(np, *ap);
258 head.h_smopts = NULL;
259 head.h_replyto = value("REPLYTO");
260 head.h_inreplyto = skin(hfield("message-id", mp));
266 * Modify the subject we are replying to to begin with Re: if
267 * it does not already.
277 if ((subj[0] == 'r' || subj[0] == 'R') &&
278 (subj[1] == 'e' || subj[1] == 'E') &&
281 newsubj = salloc(strlen(subj) + 5);
282 sprintf(newsubj, "Re: %s", subj);
287 * Preserve the named messages, so that they will be sent
288 * back to the system mailbox.
298 printf("Cannot \"preserve\" in edit mode\n");
301 for (ip = msgvec; *ip != 0; ip++) {
303 mp = &message[mesg-1];
304 mp->m_flag |= MPRESERVE;
312 * Mark all given messages as unread.
320 for (ip = msgvec; *ip != 0; ip++) {
321 dot = &message[*ip-1];
322 dot->m_flag &= ~(MREAD|MTOUCH);
323 dot->m_flag |= MSTATUS;
329 * Print the size of each message.
338 for (ip = msgvec; *ip != 0; ip++) {
340 mp = &message[mesg-1];
341 printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size);
347 * Quit quickly. If we are sourcing, just pop the input level
348 * by returning an error.
361 * Set or display a variable value. Syntax is similar to that
370 char varbuf[BUFSIZ], **ap, **p;
373 if (*arglist == NULL) {
374 for (h = 0, s = 1; h < HSHSIZE; h++)
375 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
377 ap = (char **)salloc(s * sizeof(*ap));
378 for (h = 0, p = ap; h < HSHSIZE; h++)
379 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
383 for (p = ap; *p != NULL; p++)
384 printf("%s\t%s\n", *p, value(*p));
388 for (ap = arglist; *ap != NULL; ap++) {
391 while (cp2 < varbuf + sizeof(varbuf) - 1 && *cp != '=' && *cp != '\0')
398 if (equal(varbuf, "")) {
399 printf("Non-null variable name required\n");
409 * Unset a bunch of variable values.
415 struct var *vp, *vp2;
420 for (ap = arglist; *ap != NULL; ap++) {
421 if ((vp2 = lookup(*ap)) == NULL) {
424 else if (!sourcing) {
425 printf("\"%s\": undefined variable\n", *ap);
431 if (vp2 == variables[h]) {
432 variables[h] = variables[h]->v_link;
438 for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
440 vp->v_link = vp2->v_link;
449 * Put add users to a group.
455 struct grouphead *gh;
457 char **ap, *gname, **p;
461 for (h = 0, s = 1; h < HSHSIZE; h++)
462 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
464 ap = (char **)salloc(s * sizeof(*ap));
465 for (h = 0, p = ap; h < HSHSIZE; h++)
466 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
470 for (p = ap; *p != NULL; p++)
474 if (argv[1] == NULL) {
480 if ((gh = findgroup(gname)) == NULL) {
481 gh = calloc(sizeof(*gh), 1);
482 gh->g_name = vcopy(gname);
484 gh->g_link = groups[h];
489 * Insert names from the command list into the group.
490 * Who cares if there are duplicates? They get tossed
494 for (ap = argv+1; *ap != NULL; ap++) {
495 gp = calloc(sizeof(*gp), 1);
496 gp->ge_name = vcopy(*ap);
497 gp->ge_link = gh->g_list;
504 * Sort the passed string vecotor into ascending dictionary
513 for (ap = list; *ap != NULL; ap++)
517 qsort(list, ap-list, sizeof(*list), diction);
521 * Do a dictionary order comparison of the arguments from
528 return (strcmp(*(const char **)a, *(const char **)b));
532 * The do nothing command for comments.
544 * Change to another file. With no argument, print information about
552 if (argv[0] == NULL) {
556 if (setfile(*argv) < 0)
563 * Expand file names like echo
571 for (ap = argv; *ap != NULL; ap++) {
573 if ((cp = expand(cp)) != NULL) {
587 if (value("Replyall") == NULL && value("flipr") == NULL)
588 return (doRespond(msgvec));
590 return (dorespond(msgvec));
594 * Reply to a series of messages by simply mailing to the senders
595 * and not messing around with the To: and Cc: lists as in normal
608 for (ap = msgvec; *ap != 0; ap++) {
609 mp = &message[*ap - 1];
612 if ((cp = skin(hfield("from", mp))) == NULL)
613 cp = skin(nameof(mp, 2));
614 head.h_to = cat(head.h_to, extract(cp, GTO));
615 mid = skin(hfield("message-id", mp));
617 if (head.h_to == NULL)
619 mp = &message[msgvec[0] - 1];
620 if ((head.h_subject = hfield("subject", mp)) == NULL)
621 head.h_subject = hfield("subj", mp);
622 head.h_subject = reedit(head.h_subject);
625 head.h_smopts = NULL;
626 head.h_replyto = value("REPLYTO");
627 head.h_inreplyto = mid;
633 * Conditional commands. These allow one to parameterize one's
634 * .mailrc and do some things if sending, others if receiving.
643 printf("Illegal nested \"if\"\n");
658 printf("Unrecognized if-keyword: \"%s\"\n", cp);
665 * Implement 'else'. This is pretty simple -- we just
666 * flip over the conditional flag.
674 printf("\"Else\" without matching \"if\"\n");
686 printf("Mail's idea of conditions is screwed up\n");
694 * End of if statement. Just set cond back to anything.
701 printf("\"Endif\" without matching \"if\"\n");
709 * Set the list of alternate names.
716 char **ap, **ap2, *cp;
718 c = argcount(namelist) + 1;
722 for (ap = altnames; *ap != NULL; ap++)
728 (void)free(altnames);
729 altnames = calloc((unsigned)c, sizeof(char *));
730 for (ap = namelist, ap2 = altnames; *ap != NULL; ap++, ap2++) {
731 cp = calloc((unsigned)strlen(*ap) + 1, sizeof(char));