2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)cmd3.c 8.2 (Berkeley) 4/20/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
44 * Mail -- a mail program
46 * Still more user commands.
50 * Process a shell escape by saving signals, ignoring signals,
56 sig_t sigint = signal(SIGINT, SIG_IGN);
60 if (strlcpy(cmd, str, sizeof(cmd)) >= sizeof(cmd))
62 if (bangexp(cmd, sizeof(cmd)) < 0)
64 if ((sh = value("SHELL")) == NULL)
66 (void)run_command(sh, 0, -1, -1, "-c", cmd, NULL);
67 (void)signal(SIGINT, sigint);
73 * Fork an interactive shell.
77 dosh(char *str __unused)
79 sig_t sigint = signal(SIGINT, SIG_IGN);
82 if ((sh = value("SHELL")) == NULL)
84 (void)run_command(sh, 0, -1, -1, NULL);
85 (void)signal(SIGINT, sigint);
91 * Expand the shell escape by expanding unescaped !'s into the
92 * last issued command where possible.
95 bangexp(char *str, size_t strsize)
98 static char lastbang[BUFSIZ];
105 while (*cp != '\0') {
107 if (n < (int)strlen(lastbang)) {
109 printf("Command buffer overflow\n");
113 if (strlcpy(cp2, lastbang, sizeof(bangbuf) - (cp2 - bangbuf))
114 >= sizeof(bangbuf) - (cp2 - bangbuf))
116 cp2 += strlen(lastbang);
117 n -= strlen(lastbang);
121 if (*cp == '\\' && cp[1] == '!') {
134 printf("!%s\n", bangbuf);
135 (void)fflush(stdout);
137 if (strlcpy(str, bangbuf, strsize) >= strsize)
139 if (strlcpy(lastbang, bangbuf, sizeof(lastbang)) >= sizeof(lastbang))
145 * Print out a nice help message from some file or another.
154 if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
155 warn("%s", _PATH_HELP);
158 while ((c = getc(f)) != EOF)
165 * Change user's working directory.
173 if (*arglist == NULL) {
178 if ((cp = expand(*arglist)) == NULL)
192 if (value("Replyall") == NULL && value("flipr") == NULL)
193 return (dorespond(msgvec));
195 return (doRespond(msgvec));
199 * Reply to a list of messages. Extract each name from the
200 * message header and send them off to mail1()
203 dorespond(int *msgvec)
206 char *cp, *rcv, *replyto;
211 if (msgvec[1] != 0) {
212 printf("Sorry, can't reply to multiple messages at once\n");
215 mp = &message[msgvec[0] - 1];
218 if ((rcv = skin(hfield("from", mp))) == NULL)
219 rcv = skin(nameof(mp, 1));
220 if ((replyto = skin(hfield("reply-to", mp))) != NULL)
221 np = extract(replyto, GTO);
222 else if ((cp = skin(hfield("to", mp))) != NULL)
223 np = extract(cp, GTO);
228 * Delete my name from the reply list,
229 * and with it, all my alternate names.
231 np = delname(np, myname);
233 for (ap = altnames; *ap != NULL; ap++)
234 np = delname(np, *ap);
235 if (np != NULL && replyto == NULL)
236 np = cat(np, extract(rcv, GTO));
237 else if (np == NULL) {
239 printf("Empty reply-to field -- replying to author\n");
240 np = extract(rcv, GTO);
243 if ((head.h_subject = hfield("subject", mp)) == NULL)
244 head.h_subject = hfield("subj", mp);
245 head.h_subject = reedit(head.h_subject);
246 if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) {
247 np = elide(extract(cp, GCC));
248 np = delname(np, myname);
250 for (ap = altnames; *ap != NULL; ap++)
251 np = delname(np, *ap);
256 head.h_smopts = NULL;
257 head.h_replyto = value("REPLYTO");
258 head.h_inreplyto = skin(hfield("message-id", mp));
264 * Modify the message subject to begin with "Re:" if
265 * it does not already.
274 if ((subj[0] == 'r' || subj[0] == 'R') &&
275 (subj[1] == 'e' || subj[1] == 'E') &&
278 newsubj = salloc(strlen(subj) + 5);
279 sprintf(newsubj, "Re: %s", subj);
284 * Preserve the named messages, so that they will be sent
285 * back to the system mailbox.
295 printf("Cannot \"preserve\" in edit mode\n");
298 for (ip = msgvec; *ip != 0; ip++) {
300 mp = &message[mesg-1];
301 mp->m_flag |= MPRESERVE;
309 * Mark all given messages as unread.
317 for (ip = msgvec; *ip != 0; ip++) {
318 dot = &message[*ip-1];
319 dot->m_flag &= ~(MREAD|MTOUCH);
320 dot->m_flag |= MSTATUS;
326 * Print the size of each message.
335 for (ip = msgvec; *ip != 0; ip++) {
337 mp = &message[mesg-1];
338 printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size);
344 * Quit quickly. If we are sourcing, just pop the input level
345 * by returning an error.
357 * Set or display a variable value. Syntax is similar to that
366 char varbuf[BUFSIZ], **ap, **p;
369 if (*arglist == NULL) {
370 for (h = 0, s = 1; h < HSHSIZE; h++)
371 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
373 ap = (char **)salloc(s * sizeof(*ap));
374 for (h = 0, p = ap; h < HSHSIZE; h++)
375 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
379 for (p = ap; *p != NULL; p++)
380 printf("%s\t%s\n", *p, value(*p));
384 for (ap = arglist; *ap != NULL; ap++) {
387 while (cp2 < varbuf + sizeof(varbuf) - 1 && *cp != '=' && *cp != '\0')
394 if (equal(varbuf, "")) {
395 printf("Non-null variable name required\n");
405 * Unset a bunch of variable values.
411 struct var *vp, *vp2;
416 for (ap = arglist; *ap != NULL; ap++) {
417 if ((vp2 = lookup(*ap)) == NULL) {
420 else if (!sourcing) {
421 printf("\"%s\": undefined variable\n", *ap);
427 if (vp2 == variables[h]) {
428 variables[h] = variables[h]->v_link;
434 for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
436 vp->v_link = vp2->v_link;
445 * Put add users to a group.
451 struct grouphead *gh;
453 char **ap, *gname, **p;
457 for (h = 0, s = 1; h < HSHSIZE; h++)
458 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
460 ap = (char **)salloc(s * sizeof(*ap));
461 for (h = 0, p = ap; h < HSHSIZE; h++)
462 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
466 for (p = ap; *p != NULL; p++)
470 if (argv[1] == NULL) {
476 if ((gh = findgroup(gname)) == NULL) {
477 if ((gh = calloc(1, sizeof(*gh))) == NULL)
478 err(1, "Out of memory");
479 gh->g_name = vcopy(gname);
481 gh->g_link = groups[h];
486 * Insert names from the command list into the group.
487 * Who cares if there are duplicates? They get tossed
491 for (ap = argv+1; *ap != NULL; ap++) {
492 if ((gp = calloc(1, sizeof(*gp))) == NULL)
493 err(1, "Out of memory");
494 gp->ge_name = vcopy(*ap);
495 gp->ge_link = gh->g_list;
502 * Sort the passed string vecotor into ascending dictionary
510 for (ap = list; *ap != NULL; ap++)
514 qsort(list, ap-list, sizeof(*list), diction);
518 * Do a dictionary order comparison of the arguments from
522 diction(const void *a, const void *b)
524 return (strcmp(*(const char **)a, *(const char **)b));
528 * The do nothing command for comments.
539 * Change to another file. With no argument, print information about
546 if (argv[0] == NULL) {
550 if (setfile(*argv) < 0)
557 * Expand file names like echo
564 for (ap = argv; *ap != NULL; ap++) {
566 if ((cp = expand(cp)) != NULL) {
579 if (value("Replyall") == NULL && value("flipr") == NULL)
580 return (doRespond(msgvec));
582 return (dorespond(msgvec));
586 * Reply to a series of messages by simply mailing to the senders
587 * and not messing around with the To: and Cc: lists as in normal
591 doRespond(int msgvec[])
599 for (ap = msgvec; *ap != 0; ap++) {
600 mp = &message[*ap - 1];
603 if ((cp = skin(hfield("from", mp))) == NULL)
604 cp = skin(nameof(mp, 2));
605 head.h_to = cat(head.h_to, extract(cp, GTO));
606 mid = skin(hfield("message-id", mp));
608 if (head.h_to == NULL)
610 mp = &message[msgvec[0] - 1];
611 if ((head.h_subject = hfield("subject", mp)) == NULL)
612 head.h_subject = hfield("subj", mp);
613 head.h_subject = reedit(head.h_subject);
616 head.h_smopts = NULL;
617 head.h_replyto = value("REPLYTO");
618 head.h_inreplyto = mid;
624 * Conditional commands. These allow one to parameterize one's
625 * .mailrc and do some things if sending, others if receiving.
633 printf("Illegal nested \"if\"\n");
648 printf("Unrecognized if-keyword: \"%s\"\n", cp);
655 * Implement 'else'. This is pretty simple -- we just
656 * flip over the conditional flag.
664 printf("\"Else\" without matching \"if\"\n");
676 printf("Mail's idea of conditions is screwed up\n");
684 * End of if statement. Just set cond back to anything.
691 printf("\"Endif\" without matching \"if\"\n");
699 * Set the list of alternate names.
702 alternates(char **namelist)
705 char **ap, **ap2, *cp;
707 c = argcount(namelist) + 1;
711 for (ap = altnames; *ap != NULL; ap++)
717 (void)free(altnames);
718 if ((altnames = calloc((unsigned)c, sizeof(char *))) == NULL)
719 err(1, "Out of memory");
720 for (ap = namelist, ap2 = altnames; *ap != NULL; ap++, ap2++) {
721 cp = calloc((unsigned)strlen(*ap) + 1, sizeof(char));