]>
git.cameronkatri.com Git - apple_cmds.git/blob - mail_cmds/mail/cmd3.c
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.2 (Berkeley) 4/20/95";
38 static const char rcsid
[] =
39 "$FreeBSD: src/usr.bin/mail/cmd3.c,v 1.10 2002/06/30 05:25:06 obrien Exp $";
42 #include <sys/cdefs.h>
48 * Mail -- a mail program
50 * Still more user commands.
54 * Process a shell escape by saving signals, ignoring signals,
61 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
65 if (strlcpy(cmd
, str
, sizeof(cmd
)) >= sizeof(cmd
))
67 if (bangexp(cmd
, sizeof(cmd
)) < 0)
69 if ((sh
= value("SHELL")) == NULL
)
71 (void)run_command(sh
, 0, -1, -1, "-c", cmd
, NULL
);
72 (void)signal(SIGINT
, sigint
);
78 * Fork an interactive shell.
85 sig_t sigint
= signal(SIGINT
, SIG_IGN
);
88 if ((sh
= value("SHELL")) == NULL
)
90 (void)run_command(sh
, 0, -1, -1, NULL
, NULL
, NULL
);
91 (void)signal(SIGINT
, sigint
);
97 * Expand the shell escape by expanding unescaped !'s into the
98 * last issued command where possible.
101 bangexp(str
, strsize
)
105 char bangbuf
[BUFSIZ
];
106 static char lastbang
[BUFSIZ
];
110 if (value("bang") == NULL
)
115 while (*cp
!= '\0') {
117 if (n
< strlen(lastbang
)) {
119 printf("Command buffer overflow\n");
123 if (strlcpy(cp2
, lastbang
, sizeof(bangbuf
) - (cp2
- bangbuf
))
124 >= sizeof(bangbuf
) - (cp2
- bangbuf
))
126 cp2
+= strlen(lastbang
);
127 n
-= strlen(lastbang
);
131 if (*cp
== '\\' && cp
[1] == '!') {
144 printf("!%s\n", bangbuf
);
145 (void)fflush(stdout
);
147 if (strlcpy(str
, bangbuf
, strsize
) >= strsize
)
149 if (strlcpy(lastbang
, bangbuf
, sizeof(lastbang
)) >= sizeof(lastbang
))
155 * Print out a nice help message from some file or another.
164 if ((f
= Fopen(_PATH_HELP
, "r")) == NULL
) {
165 warn("%s", _PATH_HELP
);
168 while ((c
= getc(f
)) != EOF
)
175 * Change user's working directory.
183 if (*arglist
== NULL
) {
188 if ((cp
= expand(*arglist
)) == NULL
)
205 atsign
= strchr(str
,'@');
206 if (atsign
!= NULL
) {
207 str
= savestr(str
); /* copy to modify */
208 atsign
= strchr(str
,'@');
212 printf("file name=%s\n", str);
223 if (value("recordrecip") == NULL
) {
224 assign("recordrecip", "");
227 res
= respond(msgvec
);
230 alist
[0] = "recordrecip";
243 if (value("recordrecip") == NULL
) {
244 assign("recordrecip", "");
247 res
= Respond(msgvec
);
250 alist
[0] = "recordrecip";
261 if (value("Replyall") == NULL
&& value("flipr") == NULL
)
262 return (dorespond(msgvec
));
264 return (doRespond(msgvec
));
268 * Reply to a list of messages. Extract each name from the
269 * message header and send them off to mail1()
276 char *cp
, *rcv
, *replyto
;
281 if (msgvec
[1] != 0) {
282 printf("Sorry, can't reply to multiple messages at once\n");
285 mp
= &message
[msgvec
[0] - 1];
288 if ((rcv
= skin(hfield("from", mp
))) == NULL
)
289 rcv
= skin(nameof(mp
, 1));
290 if ((replyto
= skin(hfield("reply-to", mp
))) != NULL
)
291 np
= extract(replyto
, GTO
);
292 else if ((cp
= skin(hfield("to", mp
))) != NULL
)
293 np
= extract(cp
, GTO
);
298 * Delete my name from the reply list,
299 * and with it, all my alternate names.
301 np
= delname(np
, myname
);
303 for (ap
= altnames
; *ap
!= NULL
; ap
++)
304 np
= delname(np
, *ap
);
305 if (np
!= NULL
&& replyto
== NULL
)
306 np
= cat(np
, extract(rcv
, GTO
));
307 else if (np
== NULL
) {
309 printf("Empty reply-to field -- replying to author\n");
310 np
= extract(rcv
, GTO
);
313 if ((head
.h_subject
= hfield("subject", mp
)) == NULL
)
314 head
.h_subject
= hfield("subj", mp
);
315 head
.h_subject
= reedit(head
.h_subject
);
316 if (replyto
== NULL
&& (cp
= skin(hfield("cc", mp
))) != NULL
) {
317 np
= elide(extract(cp
, GCC
));
318 np
= delname(np
, myname
);
320 for (ap
= altnames
; *ap
!= NULL
; ap
++)
321 np
= delname(np
, *ap
);
326 head
.h_smopts
= NULL
;
327 head
.h_replyto
= value("REPLYTO");
328 head
.h_inreplyto
= skin(hfield("message-id", mp
));
334 * Modify the subject we are replying to to begin with Re: if
335 * it does not already.
345 if ((subj
[0] == 'r' || subj
[0] == 'R') &&
346 (subj
[1] == 'e' || subj
[1] == 'E') &&
349 newsubj
= salloc(strlen(subj
) + 5);
350 sprintf(newsubj
, "Re: %s", subj
);
355 * Preserve the named messages, so that they will be sent
356 * back to the system mailbox.
366 printf("Cannot \"preserve\" in edit mode\n");
369 for (ip
= msgvec
; *ip
!= 0; ip
++) {
371 mp
= &message
[mesg
-1];
372 mp
->m_flag
|= MPRESERVE
;
380 * Mark all given messages as unread.
388 for (ip
= msgvec
; *ip
!= 0; ip
++) {
389 dot
= &message
[*ip
-1];
390 dot
->m_flag
&= ~(MREAD
|MTOUCH
);
391 dot
->m_flag
|= MSTATUS
;
397 * Print the size of each message.
406 for (ip
= msgvec
; *ip
!= 0; ip
++) {
408 mp
= &message
[mesg
-1];
409 printf("%d: %ld/%ld\n", mesg
, mp
->m_lines
, mp
->m_size
);
415 * Quit quickly. If we are sourcing, just pop the input level
416 * by returning an error.
429 * Set or display a variable value. Syntax is similar to that
438 char varbuf
[BUFSIZ
], **ap
, **p
;
442 if (*arglist
== NULL
) {
444 for (h
= 0, s
= 1; h
< HSHSIZE
; h
++)
445 for (vp
= variables
[h
]; vp
!= NULL
; vp
= vp
->v_link
)
447 ap
= (char **)salloc(s
* sizeof(*ap
));
448 for (h
= 0, p
= ap
; h
< HSHSIZE
; h
++)
449 for (vp
= variables
[h
]; vp
!= NULL
; vp
= vp
->v_link
)
453 for (p
= ap
; *p
!= NULL
; p
++) {
455 printval
= value(*p
);
457 if (printval
[0]!='\0') printf("\t\"%s\"", printval
);
464 for (ap
= arglist
; *ap
!= NULL
; ap
++) {
466 stringlength
= strlen(cp
);
467 if (stringlength
> 2 && cp
[0]=='n' && cp
[1]=='o') {
468 /* set no<var> means unset <var> */
472 errs
+= unset(alist
);
475 if (stringlength
== 3 && cp
[0]=='a' && cp
[1]=='s' && cp
[2]=='k') {
476 /* synonym: must convert into "asksub" */
480 while (cp2
< varbuf
+ sizeof(varbuf
) - 1 && *cp
!= '=' && *cp
!= '\0')
487 if (equal(varbuf
, "")) {
488 printf("Non-null variable name required\n");
498 * Unset a bunch of variable values.
504 struct var
*vp
, *vp2
;
510 for (ap
= arglist
; *ap
!= NULL
; ap
++) {
511 stringlength
= strlen(*ap
);
512 if (stringlength
> 2 && (*ap
)[0]=='n' && (*ap
)[1]=='o') {
513 /* no<var> is not in db - only <var> can be in table */
514 printf("\"%s\": variable cannot be unset\n", *ap
);
518 if (stringlength
== 3 && (*ap
)[0]=='a' && (*ap
)[1]=='s' && (*ap
)[2]=='k') {
519 /* synonym: must convert into "asksub" */
522 if ((vp2
= lookup(*ap
)) == NULL
) {
526 fprintf(stderr
,"WARNING: unsetting environment variable: %s\n", *ap
);
527 } else if (!sourcing
) {
528 printf("\"%s\": undefined variable\n", *ap
);
534 if (vp2
== variables
[h
]) {
535 variables
[h
] = variables
[h
]->v_link
;
537 v_free(vp2
->v_value
);
541 for (vp
= variables
[h
]; vp
->v_link
!= vp2
; vp
= vp
->v_link
)
543 vp
->v_link
= vp2
->v_link
;
545 v_free(vp2
->v_value
);
552 * Put add users to a group.
558 struct grouphead
*gh
;
560 char **ap
, *gname
, **p
;
564 for (h
= 0, s
= 1; h
< HSHSIZE
; h
++)
565 for (gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
)
567 ap
= (char **)salloc(s
* sizeof(*ap
));
568 for (h
= 0, p
= ap
; h
< HSHSIZE
; h
++)
569 for (gh
= groups
[h
]; gh
!= NULL
; gh
= gh
->g_link
)
573 for (p
= ap
; *p
!= NULL
; p
++)
577 if (argv
[1] == NULL
) {
583 if ((gh
= findgroup(gname
)) == NULL
) {
584 gh
= calloc(sizeof(*gh
), 1);
585 gh
->g_name
= vcopy(gname
);
587 gh
->g_link
= groups
[h
];
592 * Insert names from the command list into the group.
593 * Who cares if there are duplicates? They get tossed
597 for (ap
= argv
+1; *ap
!= NULL
; ap
++) {
598 gp
= calloc(sizeof(*gp
), 1);
599 gp
->ge_name
= vcopy(*ap
);
600 gp
->ge_link
= gh
->g_list
;
607 * Sort the passed string vecotor into ascending dictionary
616 for (ap
= list
; *ap
!= NULL
; ap
++)
620 qsort(list
, ap
-list
, sizeof(*list
), diction
);
624 * Do a dictionary order comparison of the arguments from
631 return (strcmp(*(const char **)a
, *(const char **)b
));
635 * The do nothing command for comments.
647 * Change to another file. With no argument, print information about
655 if (argv
[0] == NULL
) {
659 if (setfile(*argv
) < 0)
666 * Expand file names like echo
674 for (ap
= argv
; *ap
!= NULL
; ap
++) {
676 if ((cp
= expand(cp
)) != NULL
) {
690 if (value("Replyall") == NULL
&& value("flipr") == NULL
)
691 return (doRespond(msgvec
));
693 return (dorespond(msgvec
));
697 * Reply to a series of messages by simply mailing to the senders
698 * and not messing around with the To: and Cc: lists as in normal
708 char *cp
, *mid
= NULL
;
711 for (ap
= msgvec
; *ap
!= 0; ap
++) {
712 mp
= &message
[*ap
- 1];
715 if ((cp
= skin(hfield("from", mp
))) == NULL
)
716 cp
= skin(nameof(mp
, 2));
717 head
.h_to
= cat(head
.h_to
, extract(cp
, GTO
));
718 mid
= skin(hfield("message-id", mp
));
720 if (head
.h_to
== NULL
)
722 mp
= &message
[msgvec
[0] - 1];
723 if ((head
.h_subject
= hfield("subject", mp
)) == NULL
)
724 head
.h_subject
= hfield("subj", mp
);
725 head
.h_subject
= reedit(head
.h_subject
);
728 head
.h_smopts
= NULL
;
729 head
.h_replyto
= value("REPLYTO");
730 head
.h_inreplyto
= mid
;
736 * Conditional commands. These allow one to parameterize one's
737 * .mailrc and do some things if sending, others if receiving.
746 printf("Illegal nested \"if\"\n");
761 printf("Unrecognized if-keyword: \"%s\"\n", cp
);
768 * Implement 'else'. This is pretty simple -- we just
769 * flip over the conditional flag.
777 printf("\"Else\" without matching \"if\"\n");
789 printf("Mail's idea of conditions is screwed up\n");
797 * End of if statement. Just set cond back to anything.
804 printf("\"Endif\" without matching \"if\"\n");
812 * Set the list of alternate names.
819 char **ap
, **ap2
, *cp
;
821 c
= argcount(namelist
) + 1;
825 for (ap
= altnames
; *ap
!= NULL
; ap
++)
831 (void)free(altnames
);
832 altnames
= calloc((unsigned)c
, sizeof(char *));
833 for (ap
= namelist
, ap2
= altnames
; *ap
!= NULL
; ap
++, ap2
++) {
834 cp
= calloc((unsigned)strlen(*ap
) + 1, sizeof(char));