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
35 static char sccsid[] = "@(#)cmd3.c 8.1 (Berkeley) 6/6/93";
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 (void) strcpy(cmd, str);
62 if ((shell = value("SHELL")) == NOSTR)
64 (void) run_command(shell, 0, -1, -1, "-c", cmd, NOSTR);
65 (void) signal(SIGINT, sigint);
71 * Fork an interactive shell.
78 sig_t sigint = signal(SIGINT, SIG_IGN);
81 if ((shell = value("SHELL")) == NOSTR)
83 (void) run_command(shell, 0, -1, -1, NOSTR, NOSTR, NOSTR);
84 (void) signal(SIGINT, sigint);
90 * Expand the shell escape by expanding unescaped !'s into the
91 * last issued command where possible.
100 char bangbuf[BUFSIZ];
101 register char *cp, *cp2;
110 if (n < strlen(lastbang)) {
112 printf("Command buffer overflow\n");
116 strcpy(cp2, lastbang);
117 cp2 += strlen(lastbang);
118 n -= strlen(lastbang);
122 if (*cp == '\\' && cp[1] == '!') {
135 printf("!%s\n", bangbuf);
138 strcpy(str, bangbuf);
139 strncpy(lastbang, bangbuf, 128);
145 * Print out a nice help message from some file or another.
154 if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
158 while ((c = getc(f)) != EOF)
165 * Change user's working directory.
173 if (*arglist == NOSTR)
176 if ((cp = expand(*arglist)) == NOSTR)
189 if (value("Replyall") == NOSTR)
190 return (dorespond(msgvec));
192 return (doRespond(msgvec));
196 * Reply to a list of messages. Extract each name from the
197 * message header and send them off to mail1()
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))) == NOSTR)
217 rcv = skin(nameof(mp, 1));
218 if ((replyto = skin(hfield("reply-to", mp))) != NOSTR)
219 np = extract(replyto, GTO);
220 else if ((cp = skin(hfield("to", mp))) != NOSTR)
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; ap++)
232 np = delname(np, *ap);
233 if (np != NIL && replyto == NOSTR)
234 np = cat(np, extract(rcv, GTO));
235 else if (np == NIL) {
236 if (replyto != NOSTR)
237 printf("Empty reply-to field -- replying to author\n");
238 np = extract(rcv, GTO);
241 if ((head.h_subject = hfield("subject", mp)) == NOSTR)
242 head.h_subject = hfield("subj", mp);
243 head.h_subject = reedit(head.h_subject);
244 if (replyto == NOSTR && (cp = skin(hfield("cc", mp))) != NOSTR) {
245 np = elide(extract(cp, GCC));
246 np = delname(np, myname);
248 for (ap = altnames; *ap; ap++)
249 np = delname(np, *ap);
255 if ((head.h_replyto = getenv("REPLYTO")) == NULL)
256 head.h_replyto = NOSTR;
257 head.h_inreplyto = skin(hfield("message-id", mp));
263 * Modify the subject we are replying to to begin with Re: if
264 * 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 strcpy(newsubj, "Re: ");
280 strcpy(newsubj + 4, subj);
285 * Preserve the named messages, so that they will be sent
286 * back to the system mailbox.
292 register struct message *mp;
293 register int *ip, mesg;
296 printf("Cannot \"preserve\" in edit mode\n");
299 for (ip = msgvec; *ip != 0; ip++) {
301 mp = &message[mesg-1];
302 mp->m_flag |= MPRESERVE;
310 * Mark all given messages as unread.
318 for (ip = msgvec; *ip != 0; ip++) {
319 dot = &message[*ip-1];
320 dot->m_flag &= ~(MREAD|MTOUCH);
321 dot->m_flag |= MSTATUS;
327 * Print the size of each message.
333 register struct message *mp;
334 register int *ip, mesg;
336 for (ip = msgvec; *ip != 0; ip++) {
338 mp = &message[mesg-1];
339 printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size);
345 * Quit quickly. If we are sourcing, just pop the input level
346 * by returning an error.
359 * Set or display a variable value. Syntax is similar to that
366 register struct var *vp;
367 register char *cp, *cp2;
368 char varbuf[BUFSIZ], **ap, **p;
371 if (*arglist == NOSTR) {
372 for (h = 0, s = 1; h < HSHSIZE; h++)
373 for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
375 ap = (char **) salloc(s * sizeof *ap);
376 for (h = 0, p = ap; h < HSHSIZE; h++)
377 for (vp = variables[h]; vp != NOVAR; vp = vp->v_link)
381 for (p = ap; *p != NOSTR; p++)
382 printf("%s\t%s\n", *p, value(*p));
386 for (ap = arglist; *ap != NOSTR; ap++) {
389 while (*cp != '=' && *cp != '\0')
396 if (equal(varbuf, "")) {
397 printf("Non-null variable name required\n");
407 * Unset a bunch of variable values.
413 register struct var *vp, *vp2;
418 for (ap = arglist; *ap != NOSTR; ap++) {
419 if ((vp2 = lookup(*ap)) == NOVAR) {
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 register struct grouphead *gh;
452 register struct group *gp;
455 char **ap, *gname, **p;
457 if (*argv == NOSTR) {
458 for (h = 0, s = 1; h < HSHSIZE; h++)
459 for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
461 ap = (char **) salloc(s * sizeof *ap);
462 for (h = 0, p = ap; h < HSHSIZE; h++)
463 for (gh = groups[h]; gh != NOGRP; gh = gh->g_link)
467 for (p = ap; *p != NOSTR; p++)
471 if (argv[1] == NOSTR) {
477 if ((gh = findgroup(gname)) == NOGRP) {
478 gh = (struct grouphead *) calloc(sizeof *gh, 1);
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 != NOSTR; ap++) {
492 gp = (struct group *) calloc(sizeof *gp, 1);
493 gp->ge_name = vcopy(*ap);
494 gp->ge_link = gh->g_list;
501 * Sort the passed string vecotor into ascending dictionary
511 for (ap = list; *ap != NOSTR; ap++)
515 qsort(list, ap-list, sizeof(*list), diction);
519 * Do a dictionary order comparison of the arguments from
526 return(strcmp(*(char **)a, *(char **)b));
530 * The do nothing command for comments.
542 * Change to another file. With no argument, print information about
547 register char **argv;
550 if (argv[0] == NOSTR) {
554 if (setfile(*argv) < 0)
561 * Expand file names like echo
570 for (ap = argv; *ap != NOSTR; ap++) {
572 if ((cp = expand(cp)) != NOSTR) {
586 if (value("Replyall") == NOSTR)
587 return (doRespond(msgvec));
589 return (dorespond(msgvec));
593 * Reply to a series of messages by simply mailing to the senders
594 * 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))) == NOSTR)
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 == NIL)
619 mp = &message[msgvec[0] - 1];
620 if ((head.h_subject = hfield("subject", mp)) == NOSTR)
621 head.h_subject = hfield("subj", mp);
622 head.h_subject = reedit(head.h_subject);
626 if ((head.h_replyto = getenv("REPLYTO")) == NULL)
627 head.h_replyto = NOSTR;
628 head.h_inreplyto = mid;
634 * Conditional commands. These allow one to parameterize one's
635 * .mailrc and do some things if sending, others if receiving.
644 printf("Illegal nested \"if\"\n");
659 printf("Unrecognized if-keyword: \"%s\"\n", cp);
666 * Implement 'else'. This is pretty simple -- we just
667 * flip over the conditional flag.
675 printf("\"Else\" without matching \"if\"\n");
687 printf("Mail's idea of conditions is screwed up\n");
695 * End of if statement. Just set cond back to anything.
702 printf("\"Endif\" without matching \"if\"\n");
710 * Set the list of alternate names.
717 register char **ap, **ap2, *cp;
719 c = argcount(namelist) + 1;
723 for (ap = altnames; *ap; ap++)
729 free((char *) altnames);
730 altnames = (char **) calloc((unsigned) c, sizeof (char *));
731 for (ap = namelist, ap2 = altnames; *ap; ap++, ap2++) {
732 cp = (char *) calloc((unsigned) strlen(*ap) + 1, sizeof (char));