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);
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 < (int)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.
171 if (*arglist == NULL) {
176 if ((cp = expand(*arglist)) == NULL)
190 if (value("Replyall") == NULL && value("flipr") == NULL)
191 return (dorespond(msgvec));
193 return (doRespond(msgvec));
197 * Reply to a list of messages. Extract each name from the
198 * message header and send them off to mail1()
201 dorespond(int *msgvec)
204 char *cp, *rcv, *replyto;
209 if (msgvec[1] != 0) {
210 printf("Sorry, can't reply to multiple messages at once\n");
213 mp = &message[msgvec[0] - 1];
216 if ((rcv = skin(hfield("from", mp))) == NULL)
217 rcv = skin(nameof(mp, 1));
218 if ((replyto = skin(hfield("reply-to", mp))) != NULL)
219 np = extract(replyto, GTO);
220 else if ((cp = skin(hfield("to", mp))) != NULL)
221 np = extract(cp, GTO);
226 * Delete my name from the reply list,
227 * and with it, all my alternate names.
229 np = delname(np, myname);
231 for (ap = altnames; *ap != NULL; ap++)
232 np = delname(np, *ap);
233 if (np != NULL && replyto == NULL)
234 np = cat(np, extract(rcv, GTO));
235 else if (np == NULL) {
237 printf("Empty reply-to field -- replying to author\n");
238 np = extract(rcv, GTO);
241 if ((head.h_subject = hfield("subject", mp)) == NULL)
242 head.h_subject = hfield("subj", mp);
243 head.h_subject = reedit(head.h_subject);
244 if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) {
245 np = elide(extract(cp, GCC));
246 np = delname(np, myname);
248 for (ap = altnames; *ap != NULL; ap++)
249 np = delname(np, *ap);
254 head.h_smopts = NULL;
255 head.h_replyto = value("REPLYTO");
256 head.h_inreplyto = skin(hfield("message-id", mp));
262 * Modify the subject we are replying to to begin with Re: if
263 * it does not already.
272 if ((subj[0] == 'r' || subj[0] == 'R') &&
273 (subj[1] == 'e' || subj[1] == 'E') &&
276 newsubj = salloc(strlen(subj) + 5);
277 sprintf(newsubj, "Re: %s", subj);
282 * Preserve the named messages, so that they will be sent
283 * back to the system mailbox.
293 printf("Cannot \"preserve\" in edit mode\n");
296 for (ip = msgvec; *ip != 0; ip++) {
298 mp = &message[mesg-1];
299 mp->m_flag |= MPRESERVE;
307 * Mark all given messages as unread.
315 for (ip = msgvec; *ip != 0; ip++) {
316 dot = &message[*ip-1];
317 dot->m_flag &= ~(MREAD|MTOUCH);
318 dot->m_flag |= MSTATUS;
324 * Print the size of each message.
333 for (ip = msgvec; *ip != 0; ip++) {
335 mp = &message[mesg-1];
336 printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size);
342 * Quit quickly. If we are sourcing, just pop the input level
343 * by returning an error.
355 * Set or display a variable value. Syntax is similar to that
364 char varbuf[BUFSIZ], **ap, **p;
367 if (*arglist == NULL) {
368 for (h = 0, s = 1; h < HSHSIZE; h++)
369 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
371 ap = (char **)salloc(s * sizeof(*ap));
372 for (h = 0, p = ap; h < HSHSIZE; h++)
373 for (vp = variables[h]; vp != NULL; vp = vp->v_link)
377 for (p = ap; *p != NULL; p++)
378 printf("%s\t%s\n", *p, value(*p));
382 for (ap = arglist; *ap != NULL; ap++) {
385 while (cp2 < varbuf + sizeof(varbuf) - 1 && *cp != '=' && *cp != '\0')
392 if (equal(varbuf, "")) {
393 printf("Non-null variable name required\n");
403 * Unset a bunch of variable values.
409 struct var *vp, *vp2;
414 for (ap = arglist; *ap != NULL; ap++) {
415 if ((vp2 = lookup(*ap)) == NULL) {
418 else if (!sourcing) {
419 printf("\"%s\": undefined variable\n", *ap);
425 if (vp2 == variables[h]) {
426 variables[h] = variables[h]->v_link;
432 for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
434 vp->v_link = vp2->v_link;
443 * Put add users to a group.
449 struct grouphead *gh;
451 char **ap, *gname, **p;
455 for (h = 0, s = 1; h < HSHSIZE; h++)
456 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
458 ap = (char **)salloc(s * sizeof(*ap));
459 for (h = 0, p = ap; h < HSHSIZE; h++)
460 for (gh = groups[h]; gh != NULL; gh = gh->g_link)
464 for (p = ap; *p != NULL; p++)
468 if (argv[1] == NULL) {
474 if ((gh = findgroup(gname)) == NULL) {
475 if ((gh = calloc(1, sizeof(*gh))) == NULL)
476 err(1, "Out of memory");
477 gh->g_name = vcopy(gname);
479 gh->g_link = groups[h];
484 * Insert names from the command list into the group.
485 * Who cares if there are duplicates? They get tossed
489 for (ap = argv+1; *ap != NULL; ap++) {
490 if ((gp = calloc(1, sizeof(*gp))) == NULL)
491 err(1, "Out of memory");
492 gp->ge_name = vcopy(*ap);
493 gp->ge_link = gh->g_list;
500 * Sort the passed string vecotor into ascending dictionary
508 for (ap = list; *ap != NULL; ap++)
512 qsort(list, ap-list, sizeof(*list), diction);
516 * Do a dictionary order comparison of the arguments from
520 diction(const void *a, const void *b)
522 return (strcmp(*(const char **)a, *(const char **)b));
526 * The do nothing command for comments.
537 * Change to another file. With no argument, print information about
544 if (argv[0] == NULL) {
548 if (setfile(*argv) < 0)
555 * Expand file names like echo
562 for (ap = argv; *ap != NULL; ap++) {
564 if ((cp = expand(cp)) != NULL) {
577 if (value("Replyall") == NULL && value("flipr") == NULL)
578 return (doRespond(msgvec));
580 return (dorespond(msgvec));
584 * Reply to a series of messages by simply mailing to the senders
585 * and not messing around with the To: and Cc: lists as in normal
589 doRespond(int msgvec[])
597 for (ap = msgvec; *ap != 0; ap++) {
598 mp = &message[*ap - 1];
601 if ((cp = skin(hfield("from", mp))) == NULL)
602 cp = skin(nameof(mp, 2));
603 head.h_to = cat(head.h_to, extract(cp, GTO));
604 mid = skin(hfield("message-id", mp));
606 if (head.h_to == NULL)
608 mp = &message[msgvec[0] - 1];
609 if ((head.h_subject = hfield("subject", mp)) == NULL)
610 head.h_subject = hfield("subj", mp);
611 head.h_subject = reedit(head.h_subject);
614 head.h_smopts = NULL;
615 head.h_replyto = value("REPLYTO");
616 head.h_inreplyto = mid;
622 * Conditional commands. These allow one to parameterize one's
623 * .mailrc and do some things if sending, others if receiving.
631 printf("Illegal nested \"if\"\n");
646 printf("Unrecognized if-keyword: \"%s\"\n", cp);
653 * Implement 'else'. This is pretty simple -- we just
654 * flip over the conditional flag.
662 printf("\"Else\" without matching \"if\"\n");
674 printf("Mail's idea of conditions is screwed up\n");
682 * End of if statement. Just set cond back to anything.
689 printf("\"Endif\" without matching \"if\"\n");
697 * Set the list of alternate names.
700 alternates(char **namelist)
703 char **ap, **ap2, *cp;
705 c = argcount(namelist) + 1;
709 for (ap = altnames; *ap != NULL; ap++)
715 (void)free(altnames);
716 if ((altnames = calloc((unsigned)c, sizeof(char *))) == NULL)
717 err(1, "Out of memory");
718 for (ap = namelist, ap2 = altnames; *ap != NULL; ap++, ap2++) {
719 cp = calloc((unsigned)strlen(*ap) + 1, sizeof(char));