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 * 4. 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
32 static char sccsid[] = "@(#)cmd3.c 8.2 (Berkeley) 4/20/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
42 * Mail -- a mail program
44 * Still more user commands.
48 * Process a shell escape by saving signals, ignoring signals,
54 sig_t sigint = signal(SIGINT, SIG_IGN);
58 if (strlcpy(cmd, str, sizeof(cmd)) >= sizeof(cmd))
60 if (bangexp(cmd, sizeof(cmd)) < 0)
62 if ((sh = value("SHELL")) == NULL)
64 (void)run_command(sh, 0, -1, -1, "-c", cmd, NULL);
65 (void)signal(SIGINT, sigint);
71 * Fork an interactive shell.
75 dosh(char *str __unused)
77 sig_t sigint = signal(SIGINT, SIG_IGN);
80 if ((sh = value("SHELL")) == NULL)
82 (void)run_command(sh, 0, -1, -1, NULL, NULL, NULL);
83 (void)signal(SIGINT, sigint);
89 * Expand the shell escape by expanding unescaped !'s into the
90 * last issued command where possible.
93 bangexp(char *str, size_t strsize)
96 static char lastbang[BUFSIZ];
103 while (*cp != '\0') {
105 if (n < strlen(lastbang)) {
107 printf("Command buffer overflow\n");
111 if (strlcpy(cp2, lastbang, sizeof(bangbuf) - (cp2 - bangbuf))
112 >= sizeof(bangbuf) - (cp2 - bangbuf))
114 cp2 += strlen(lastbang);
115 n -= strlen(lastbang);
119 if (*cp == '\\' && cp[1] == '!') {
132 printf("!%s\n", bangbuf);
133 (void)fflush(stdout);
135 if (strlcpy(str, bangbuf, strsize) >= strsize)
137 if (strlcpy(lastbang, bangbuf, sizeof(lastbang)) >= sizeof(lastbang))
143 * Print out a nice help message from some file or another.
152 if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
153 warn("%s", _PATH_HELP);
156 while ((c = getc(f)) != EOF)
163 * Change user's working directory.
166 schdir(char **arglist)
170 if (*arglist == NULL) {
175 if ((cp = expand(*arglist)) == NULL)
187 if (value("Replyall") == NULL && value("flipr") == NULL)
188 return (dorespond(msgvec));
190 return (doRespond(msgvec));
194 * Reply to a list of messages. Extract each name from the
195 * message header and send them off to mail1()
198 dorespond(int *msgvec)
201 char *cp, *rcv, *replyto;
206 if (msgvec[1] != 0) {
207 printf("Sorry, can't reply to multiple messages at once\n");
210 mp = &message[msgvec[0] - 1];
213 if ((rcv = skin(hfield("from", mp))) == NULL)
214 rcv = skin(nameof(mp, 1));
215 if ((replyto = skin(hfield("reply-to", mp))) != NULL)
216 np = extract(replyto, GTO);
217 else if ((cp = skin(hfield("to", mp))) != NULL)
218 np = extract(cp, GTO);
223 * Delete my name from the reply list,
224 * and with it, all my alternate names.
226 np = delname(np, myname);
228 for (ap = altnames; *ap != NULL; ap++)
229 np = delname(np, *ap);
230 if (np != NULL && replyto == NULL)
231 np = cat(np, extract(rcv, GTO));
232 else if (np == NULL) {
234 printf("Empty reply-to field -- replying to author\n");
235 np = extract(rcv, GTO);
238 if ((head.h_subject = hfield("subject", mp)) == NULL)
239 head.h_subject = hfield("subj", mp);
240 head.h_subject = reedit(head.h_subject);
241 if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) {
242 np = elide(extract(cp, GCC));
243 np = delname(np, myname);
245 for (ap = altnames; *ap != NULL; ap++)
246 np = delname(np, *ap);
251 head.h_smopts = NULL;
252 head.h_replyto = value("REPLYTO");
253 head.h_inreplyto = skin(hfield("message-id", mp));
259 * Modify the subject we are replying to to begin with Re: if
260 * it does not already.
269 if ((subj[0] == 'r' || subj[0] == 'R') &&
270 (subj[1] == 'e' || subj[1] == 'E') &&
273 newsubj = salloc(strlen(subj) + 5);
274 sprintf(newsubj, "Re: %s", subj);
279 * Preserve the named messages, so that they will be sent
280 * back to the system mailbox.
283 preserve(int *msgvec)
289 printf("Cannot \"preserve\" in edit mode\n");
292 for (ip = msgvec; *ip != 0; ip++) {
294 mp = &message[mesg-1];
295 mp->m_flag |= MPRESERVE;
303 * Mark all given messages as unread.
310 for (ip = msgvec; *ip != 0; ip++) {
311 dot = &message[*ip-1];
312 dot->m_flag &= ~(MREAD|MTOUCH);
313 dot->m_flag |= MSTATUS;
319 * Print the size of each message.
327 for (ip = msgvec; *ip != 0; ip++) {
329 mp = &message[mesg-1];
330 printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size);
336 * Quit quickly. If we are sourcing, just pop the input level
337 * by returning an error.
340 rexit(int e __unused)
349 * Set or display a variable value. Syntax is similar to that
357 char varbuf[BUFSIZ], **ap, **p;
360 if (*arglist == NULL) {
361 for (h = 0, s = 1; h < HSHSIZE; h++)
362 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
364 ap = (char **)salloc(s * sizeof(*ap));
365 for (h = 0, p = ap; h < HSHSIZE; h++)
366 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
370 for (p = ap; *p != NULL; p++)
371 printf("%s\t%s\n", *p, value(*p));
375 for (ap = arglist; *ap != NULL; ap++) {
378 while (cp2 < varbuf + sizeof(varbuf) - 1 && *cp != '=' && *cp != '\0')
385 if (equal(varbuf, "")) {
386 printf("Non-null variable name required\n");
396 * Unset a bunch of variable values.
399 unset(char **arglist)
401 struct var *vp, *vp2;
406 for (ap = arglist; *ap != NULL; ap++) {
407 if ((vp2 = lookup(*ap)) == NULL) {
410 else if (!sourcing) {
411 printf("\"%s\": undefined variable\n", *ap);
417 if (vp2 == variables[h]) {
418 variables[h] = variables[h]->v_link;
424 for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
426 vp->v_link = vp2->v_link;
435 * Put add users to a group.
440 struct grouphead *gh;
442 char **ap, *gname, **p;
446 for (h = 0, s = 1; h < HSHSIZE; h++)
447 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
449 ap = (char **)salloc(s * sizeof(*ap));
450 for (h = 0, p = ap; h < HSHSIZE; h++)
451 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
455 for (p = ap; *p != NULL; p++)
459 if (argv[1] == NULL) {
465 if ((gh = findgroup(gname)) == NULL) {
466 if ((gh = calloc(1, sizeof(*gh))) == NULL)
467 err(1, "Out of memory");
468 gh->g_name = vcopy(gname);
470 gh->g_link = groups[h];
475 * Insert names from the command list into the group.
476 * Who cares if there are duplicates? They get tossed
480 for (ap = argv+1; *ap != NULL; ap++) {
481 if ((gp = calloc(1, sizeof(*gp))) == NULL)
482 err(1, "Out of memory");
483 gp->ge_name = vcopy(*ap);
484 gp->ge_link = gh->g_list;
491 * Sort the passed string vecotor into ascending dictionary
499 for (ap = list; *ap != NULL; ap++)
503 qsort(list, ap-list, sizeof(*list), diction);
507 * Do a dictionary order comparison of the arguments from
511 diction(const void *a, const void *b)
513 return (strcmp(*(const char **)a, *(const char **)b));
517 * The do nothing command for comments.
528 * Change to another file. With no argument, print information about
535 if (argv[0] == NULL) {
539 if (setfile(*argv) < 0)
546 * Expand file names like echo
553 for (ap = argv; *ap != NULL; ap++) {
555 if ((cp = expand(cp)) != NULL) {
568 if (value("Replyall") == NULL && value("flipr") == NULL)
569 return (doRespond(msgvec));
571 return (dorespond(msgvec));
575 * Reply to a series of messages by simply mailing to the senders
576 * and not messing around with the To: and Cc: lists as in normal
580 doRespond(int msgvec[])
588 for (ap = msgvec; *ap != 0; ap++) {
589 mp = &message[*ap - 1];
592 if ((cp = skin(hfield("from", mp))) == NULL)
593 cp = skin(nameof(mp, 2));
594 head.h_to = cat(head.h_to, extract(cp, GTO));
595 mid = skin(hfield("message-id", mp));
597 if (head.h_to == NULL)
599 mp = &message[msgvec[0] - 1];
600 if ((head.h_subject = hfield("subject", mp)) == NULL)
601 head.h_subject = hfield("subj", mp);
602 head.h_subject = reedit(head.h_subject);
605 head.h_smopts = NULL;
606 head.h_replyto = value("REPLYTO");
607 head.h_inreplyto = mid;
613 * Conditional commands. These allow one to parameterize one's
614 * .mailrc and do some things if sending, others if receiving.
622 printf("Illegal nested \"if\"\n");
637 printf("Unrecognized if-keyword: \"%s\"\n", cp);
644 * Implement 'else'. This is pretty simple -- we just
645 * flip over the conditional flag.
653 printf("\"Else\" without matching \"if\"\n");
665 printf("Mail's idea of conditions is screwed up\n");
673 * End of if statement. Just set cond back to anything.
680 printf("\"Endif\" without matching \"if\"\n");
688 * Set the list of alternate names.
691 alternates(char **namelist)
694 char **ap, **ap2, *cp;
696 c = argcount(namelist) + 1;
700 for (ap = altnames; *ap != NULL; ap++)
706 (void)free(altnames);
707 if ((altnames = calloc((unsigned)c, sizeof(char *))) == NULL)
708 err(1, "Out of memory");
709 for (ap = namelist, ap2 = altnames; *ap != NULL; ap++, ap2++) {
710 cp = calloc((unsigned)strlen(*ap) + 1, sizeof(char));