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
36 static char sccsid[] = "@(#)cmd3.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
46 * Mail -- a mail program
48 * Still more user commands.
52 * Process a shell escape by saving signals, ignoring signals,
59 sig_t sigint = signal(SIGINT, SIG_IGN);
63 if (strlcpy(cmd, str, sizeof(cmd)) >= sizeof(cmd))
65 if (bangexp(cmd, sizeof(cmd)) < 0)
67 if ((shell = value("SHELL")) == NOSTR)
69 (void) run_command(shell, 0, -1, -1, "-c", cmd, NOSTR);
70 (void) signal(SIGINT, sigint);
76 * Fork an interactive shell.
83 sig_t sigint = signal(SIGINT, SIG_IGN);
86 if ((shell = value("SHELL")) == NOSTR)
88 (void) run_command(shell, 0, -1, -1, NOSTR, NOSTR, NOSTR);
89 (void) signal(SIGINT, sigint);
95 * Expand the shell escape by expanding unescaped !'s into the
96 * last issued command where possible.
103 char bangbuf[BUFSIZ];
104 static char lastbang[BUFSIZ];
105 register char *cp, *cp2;
114 if (n < strlen(lastbang)) {
116 printf("Command buffer overflow\n");
120 if (strlcpy(cp2, lastbang, sizeof(bangbuf) - (cp2 - bangbuf))
121 >= sizeof(bangbuf) - (cp2 - bangbuf))
123 cp2 += strlen(lastbang);
124 n -= strlen(lastbang);
128 if (*cp == '\\' && cp[1] == '!') {
141 printf("!%s\n", bangbuf);
144 if (strlcpy(str, bangbuf, strsize) >= strsize)
146 if (strlcpy(lastbang, bangbuf, sizeof(lastbang)) >= sizeof(lastbang))
152 * Print out a nice help message from some file or another.
161 if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
162 warn("%s", _PATH_HELP);
165 while ((c = getc(f)) != EOF)
172 * Change user's working directory.
180 if (*arglist == NOSTR) {
181 if (homedir == NOSTR)
185 if ((cp = expand(*arglist)) == NOSTR)
198 if (value("Replyall") == NOSTR)
199 return (dorespond(msgvec));
201 return (doRespond(msgvec));
205 * Reply to a list of messages. Extract each name from the
206 * message header and send them off to mail1()
213 char *cp, *rcv, *replyto;
218 if (msgvec[1] != 0) {
219 printf("Sorry, can't reply to multiple messages at once\n");
222 mp = &message[msgvec[0] - 1];
225 if ((rcv = skin(hfield("from", mp))) == NOSTR)
226 rcv = skin(nameof(mp, 1));
227 if ((replyto = skin(hfield("reply-to", mp))) != NOSTR)
228 np = extract(replyto, GTO);
229 else if ((cp = skin(hfield("to", mp))) != NOSTR)
230 np = extract(cp, GTO);
235 * Delete my name from the reply list,
236 * and with it, all my alternate names.
238 np = delname(np, myname);
240 for (ap = altnames; *ap; ap++)
241 np = delname(np, *ap);
242 if (np != NIL && replyto == NOSTR)
243 np = cat(np, extract(rcv, GTO));
244 else if (np == NIL) {
245 if (replyto != NOSTR)
246 printf("Empty reply-to field -- replying to author\n");
247 np = extract(rcv, GTO);
250 if ((head.h_subject = hfield("subject", mp)) == NOSTR)
251 head.h_subject = hfield("subj", mp);
252 head.h_subject = reedit(head.h_subject);
253 if (replyto == NOSTR && (cp = skin(hfield("cc", mp))) != NOSTR) {
254 np = elide(extract(cp, GCC));
255 np = delname(np, myname);
257 for (ap = altnames; *ap; ap++)
258 np = delname(np, *ap);
264 if ((head.h_replyto = getenv("REPLYTO")) == NULL)
265 head.h_replyto = NOSTR;
266 head.h_inreplyto = skin(hfield("message-id", mp));
272 * Modify the subject we are replying to to begin with Re: if
273 * it does not already.
283 if ((subj[0] == 'r' || subj[0] == 'R') &&
284 (subj[1] == 'e' || subj[1] == 'E') &&
287 newsubj = salloc(strlen(subj) + 5);
288 sprintf(newsubj, "Re: %s", subj);
293 * Preserve the named messages, so that they will be sent
294 * back to the system mailbox.
300 register struct message *mp;
301 register int *ip, mesg;
304 printf("Cannot \"preserve\" in edit mode\n");
307 for (ip = msgvec; *ip != 0; ip++) {
309 mp = &message[mesg-1];
310 mp->m_flag |= MPRESERVE;
318 * Mark all given messages as unread.
326 for (ip = msgvec; *ip != 0; ip++) {
327 dot = &message[*ip-1];
328 dot->m_flag &= ~(MREAD|MTOUCH);
329 dot->m_flag |= MSTATUS;
335 * Print the size of each message.
341 register struct message *mp;
342 register int *ip, mesg;
344 for (ip = msgvec; *ip != 0; ip++) {
346 mp = &message[mesg-1];
347 printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size);
353 * Quit quickly. If we are sourcing, just pop the input level
354 * by returning an error.
367 * Set or display a variable value. Syntax is similar to that
374 register struct var *vp;
375 register char *cp, *cp2;
376 char varbuf[BUFSIZ], **ap, **p;
379 if (*arglist == NOSTR) {
380 for (h = 0, s = 1; h < HSHSIZE; h++)
381 for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
383 ap = (char **) salloc(s * sizeof *ap);
384 for (h = 0, p = ap; h < HSHSIZE; h++)
385 for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
389 for (p = ap; *p != NOSTR; p++)
390 printf("%s\t%s\n", *p, value(*p));
394 for (ap = arglist; *ap != NOSTR; ap++) {
397 while (cp2 < varbuf + sizeof(varbuf) - 1 && *cp != '=' && *cp != '\0')
404 if (equal(varbuf, "")) {
405 printf("Non-null variable name required\n");
415 * Unset a bunch of variable values.
421 register struct var *vp, *vp2;
426 for (ap = arglist; *ap != NOSTR; ap++) {
427 if ((vp2 = lookup(*ap)) == NOVAR) {
429 printf("\"%s\": undefined variable\n", *ap);
435 if (vp2 == variables[h]) {
436 variables[h] = variables[h]->v_link;
442 for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
444 vp->v_link = vp2->v_link;
453 * Put add users to a group.
459 register struct grouphead *gh;
460 register struct group *gp;
463 char **ap, *gname, **p;
465 if (*argv == NOSTR) {
466 for (h = 0, s = 1; h < HSHSIZE; h++)
467 for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
469 ap = (char **) salloc(s * sizeof *ap);
470 for (h = 0, p = ap; h < HSHSIZE; h++)
471 for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
475 for (p = ap; *p != NOSTR; p++)
479 if (argv[1] == NOSTR) {
485 if ((gh = findgroup(gname)) == NOGRP) {
486 gh = (struct grouphead *) calloc(sizeof *gh, 1);
487 gh->g_name = vcopy(gname);
489 gh->g_link = groups[h];
494 * Insert names from the command list into the group.
495 * Who cares if there are duplicates? They get tossed
499 for (ap = argv+1; *ap != NOSTR; ap++) {
500 gp = (struct group *) calloc(sizeof *gp, 1);
501 gp->ge_name = vcopy(*ap);
502 gp->ge_link = gh->g_list;
509 * Sort the passed string vecotor into ascending dictionary
519 for (ap = list; *ap != NOSTR; ap++)
523 qsort(list, ap-list, sizeof(*list), diction);
527 * Do a dictionary order comparison of the arguments from
534 return(strcmp(*(char **)a, *(char **)b));
538 * The do nothing command for comments.
550 * Change to another file. With no argument, print information about
555 register char **argv;
558 if (argv[0] == NOSTR) {
562 if (setfile(*argv) < 0)
569 * Expand file names like echo
578 for (ap = argv; *ap != NOSTR; ap++) {
580 if ((cp = expand(cp)) != NOSTR) {
594 if (value("Replyall") == NOSTR)
595 return (doRespond(msgvec));
597 return (dorespond(msgvec));
601 * Reply to a series of messages by simply mailing to the senders
602 * and not messing around with the To: and Cc: lists as in normal
616 for (ap = msgvec; *ap != 0; ap++) {
617 mp = &message[*ap - 1];
620 if ((cp = skin(hfield("from", mp))) == NOSTR)
621 cp = skin(nameof(mp, 2));
622 head.h_to = cat(head.h_to, extract(cp, GTO));
623 mid = skin(hfield("message-id", mp));
625 if (head.h_to == NIL)
627 mp = &message[msgvec[0] - 1];
628 if ((head.h_subject = hfield("subject", mp)) == NOSTR)
629 head.h_subject = hfield("subj", mp);
630 head.h_subject = reedit(head.h_subject);
634 if ((head.h_replyto = getenv("REPLYTO")) == NULL)
635 head.h_replyto = NOSTR;
636 head.h_inreplyto = mid;
642 * Conditional commands. These allow one to parameterize one's
643 * .mailrc and do some things if sending, others if receiving.
652 printf("Illegal nested \"if\"\n");
667 printf("Unrecognized if-keyword: \"%s\"\n", cp);
674 * Implement 'else'. This is pretty simple -- we just
675 * flip over the conditional flag.
683 printf("\"Else\" without matching \"if\"\n");
695 printf("Mail's idea of conditions is screwed up\n");
703 * End of if statement. Just set cond back to anything.
710 printf("\"Endif\" without matching \"if\"\n");
718 * Set the list of alternate names.
725 register char **ap, **ap2, *cp;
727 c = argcount(namelist) + 1;
731 for (ap = altnames; *ap; ap++)
737 free((char *) altnames);
738 altnames = (char **) calloc((unsigned) c, sizeof (char *));
739 for (ap = namelist, ap2 = altnames; *ap; ap++, ap2++) {
740 cp = (char *) calloc((unsigned) strlen(*ap) + 1, sizeof (char));