NetBSD/usr.bin/mail/cmd3.c

731 lines
14 KiB
C
Raw Normal View History

/* $NetBSD: cmd3.c,v 1.19 2002/08/12 02:42:52 itojun Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-06-29 09:09:04 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
1996-12-28 10:10:57 +03:00
static char sccsid[] = "@(#)cmd3.c 8.2 (Berkeley) 4/20/95";
#else
__RCSID("$NetBSD: cmd3.c,v 1.19 2002/08/12 02:42:52 itojun Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include "rcv.h"
1994-06-29 09:09:04 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
/*
* Mail -- a mail program
*
* Still more user commands.
*/
2002-03-02 17:59:35 +03:00
static int diction(const void *, const void *);
1993-03-21 12:45:37 +03:00
/*
* Process a shell escape by saving signals, ignoring signals,
* and forking a sh -c
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
shell(void *v)
1993-03-21 12:45:37 +03:00
{
char *str = v;
1993-03-21 12:45:37 +03:00
sig_t sigint = signal(SIGINT, SIG_IGN);
2002-03-02 18:27:51 +03:00
char *shellcmd;
1993-03-21 12:45:37 +03:00
char cmd[BUFSIZ];
2002-03-06 00:18:14 +03:00
(void)strcpy(cmd, str);
1993-03-21 12:45:37 +03:00
if (bangexp(cmd) < 0)
return 1;
if ((shellcmd = value("SHELL")) == NULL)
2002-03-02 18:27:51 +03:00
shellcmd = _PATH_CSHELL;
2002-03-06 00:18:14 +03:00
(void)run_command(shellcmd, 0, -1, -1, "-c", cmd, NULL);
(void)signal(SIGINT, sigint);
1993-03-21 12:45:37 +03:00
printf("!\n");
return 0;
}
/*
* Fork an interactive shell.
*/
/*ARGSUSED*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
dosh(void *v)
1993-03-21 12:45:37 +03:00
{
sig_t sigint = signal(SIGINT, SIG_IGN);
2002-03-02 18:27:51 +03:00
char *shellcmd;
1993-03-21 12:45:37 +03:00
if ((shellcmd = value("SHELL")) == NULL)
2002-03-02 18:27:51 +03:00
shellcmd = _PATH_CSHELL;
2002-03-06 00:18:14 +03:00
(void)run_command(shellcmd, 0, -1, -1, NULL, NULL, NULL);
(void)signal(SIGINT, sigint);
1993-03-21 12:45:37 +03:00
putchar('\n');
return 0;
}
/*
* Expand the shell escape by expanding unescaped !'s into the
* last issued command where possible.
*/
char lastbang[128];
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
bangexp(char *str)
1993-03-21 12:45:37 +03:00
{
char bangbuf[BUFSIZ];
char *cp, *cp2;
int n;
1993-03-21 12:45:37 +03:00
int changed = 0;
cp = str;
cp2 = bangbuf;
n = BUFSIZ;
while (*cp) {
if (*cp == '!') {
if (n < strlen(lastbang)) {
overf:
printf("Command buffer overflow\n");
return(-1);
}
changed++;
strcpy(cp2, lastbang);
cp2 += strlen(lastbang);
n -= strlen(lastbang);
cp++;
continue;
}
if (*cp == '\\' && cp[1] == '!') {
if (--n <= 1)
goto overf;
*cp2++ = '!';
cp += 2;
changed++;
}
if (--n <= 1)
goto overf;
*cp2++ = *cp++;
}
*cp2 = 0;
if (changed) {
printf("!%s\n", bangbuf);
fflush(stdout);
}
strcpy(str, bangbuf);
strncpy(lastbang, bangbuf, 128);
lastbang[127] = 0;
return(0);
}
/*
* Print out a nice help message from some file or another.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
help(void *v)
1993-03-21 12:45:37 +03:00
{
int c;
FILE *f;
1993-03-21 12:45:37 +03:00
if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
2002-03-06 00:29:30 +03:00
warn(_PATH_HELP);
1993-03-21 12:45:37 +03:00
return(1);
}
while ((c = getc(f)) != EOF)
putchar(c);
Fclose(f);
return(0);
}
/*
* Change user's working directory.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
schdir(void *v)
1993-03-21 12:45:37 +03:00
{
char **arglist = v;
1993-03-21 12:45:37 +03:00
char *cp;
if (*arglist == NULL)
1993-03-21 12:45:37 +03:00
cp = homedir;
else
if ((cp = expand(*arglist)) == NULL)
1993-03-21 12:45:37 +03:00
return(1);
if (chdir(cp) < 0) {
2002-03-06 00:29:30 +03:00
warn("%s", cp);
1993-03-21 12:45:37 +03:00
return(1);
}
return 0;
}
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
respond(void *v)
1993-03-21 12:45:37 +03:00
{
int *msgvec = v;
if (value("Replyall") == NULL)
1993-03-21 12:45:37 +03:00
return (_respond(msgvec));
else
return (_Respond(msgvec));
}
/*
* Reply to a list of messages. Extract each name from the
* message header and send them off to mail1()
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
_respond(int *msgvec)
1993-03-21 12:45:37 +03:00
{
struct message *mp;
char *cp, *rcv, *replyto;
char **ap;
struct name *np;
struct header head;
if (msgvec[1] != 0) {
printf("Sorry, can't reply to multiple messages at once\n");
return(1);
}
mp = &message[msgvec[0] - 1];
touch(mp);
dot = mp;
if ((rcv = skin(hfield("from", mp))) == NULL)
1993-03-21 12:45:37 +03:00
rcv = skin(nameof(mp, 1));
if ((replyto = skin(hfield("reply-to", mp))) != NULL)
1993-03-21 12:45:37 +03:00
np = extract(replyto, GTO);
else if ((cp = skin(hfield("to", mp))) != NULL)
1993-03-21 12:45:37 +03:00
np = extract(cp, GTO);
else
np = NULL;
1993-03-21 12:45:37 +03:00
np = elide(np);
/*
* Delete my name from the reply list,
* and with it, all my alternate names.
*/
np = delname(np, myname);
if (altnames)
for (ap = altnames; *ap; ap++)
np = delname(np, *ap);
if (np != NULL && replyto == NULL)
1993-03-21 12:45:37 +03:00
np = cat(np, extract(rcv, GTO));
else if (np == NULL) {
if (replyto != NULL)
1993-03-21 12:45:37 +03:00
printf("Empty reply-to field -- replying to author\n");
np = extract(rcv, GTO);
}
head.h_to = np;
if ((head.h_subject = hfield("subject", mp)) == NULL)
1993-03-21 12:45:37 +03:00
head.h_subject = hfield("subj", mp);
head.h_subject = reedit(head.h_subject);
if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) {
1993-03-21 12:45:37 +03:00
np = elide(extract(cp, GCC));
np = delname(np, myname);
if (altnames != 0)
for (ap = altnames; *ap; ap++)
np = delname(np, *ap);
head.h_cc = np;
} else
head.h_cc = NULL;
head.h_bcc = NULL;
head.h_smopts = NULL;
1993-03-21 12:45:37 +03:00
mail1(&head, 1);
return(0);
}
/*
* Modify the subject we are replying to to begin with Re: if
* it does not already.
*/
char *
2002-03-02 17:59:35 +03:00
reedit(char *subj)
1993-03-21 12:45:37 +03:00
{
char *newsubj;
if (subj == NULL)
return NULL;
1993-03-21 12:45:37 +03:00
if ((subj[0] == 'r' || subj[0] == 'R') &&
(subj[1] == 'e' || subj[1] == 'E') &&
subj[2] == ':')
return subj;
newsubj = salloc(strlen(subj) + 5);
strcpy(newsubj, "Re: ");
strcpy(newsubj + 4, subj);
return newsubj;
}
/*
* Preserve the named messages, so that they will be sent
* back to the system mailbox.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
preserve(void *v)
1993-03-21 12:45:37 +03:00
{
int *msgvec = v;
struct message *mp;
int *ip, mesg;
1993-03-21 12:45:37 +03:00
if (edit) {
printf("Cannot \"preserve\" in edit mode\n");
return(1);
}
1997-05-17 23:47:50 +04:00
for (ip = msgvec; *ip != 0; ip++) {
1993-03-21 12:45:37 +03:00
mesg = *ip;
mp = &message[mesg-1];
mp->m_flag |= MPRESERVE;
mp->m_flag &= ~MBOX;
dot = mp;
}
return(0);
}
/*
* Mark all given messages as unread.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
unread(void *v)
1993-03-21 12:45:37 +03:00
{
int *msgvec = v;
int *ip;
1993-03-21 12:45:37 +03:00
1997-05-17 23:47:50 +04:00
for (ip = msgvec; *ip != 0; ip++) {
1993-03-21 12:45:37 +03:00
dot = &message[*ip-1];
dot->m_flag &= ~(MREAD|MTOUCH);
dot->m_flag |= MSTATUS;
}
return(0);
}
/*
* Print the size of each message.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
messize(void *v)
1993-03-21 12:45:37 +03:00
{
int *msgvec = v;
struct message *mp;
int *ip, mesg;
1993-03-21 12:45:37 +03:00
1997-05-17 23:47:50 +04:00
for (ip = msgvec; *ip != 0; ip++) {
1993-03-21 12:45:37 +03:00
mesg = *ip;
mp = &message[mesg-1];
printf("%d: %ld/%ld\n", mesg, mp->m_blines, mp->m_size);
1993-03-21 12:45:37 +03:00
}
return(0);
}
/*
* Quit quickly. If we are sourcing, just pop the input level
* by returning an error.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
rexit(void *v)
1993-03-21 12:45:37 +03:00
{
if (sourcing)
return(1);
exit(0);
1993-03-21 12:45:37 +03:00
/*NOTREACHED*/
}
/*
* Set or display a variable value. Syntax is similar to that
* of csh.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
set(void *v)
1993-03-21 12:45:37 +03:00
{
char **arglist = v;
struct var *vp;
char *cp, *cp2;
1993-03-21 12:45:37 +03:00
char varbuf[BUFSIZ], **ap, **p;
int errs, h, s;
if (*arglist == NULL) {
1993-03-21 12:45:37 +03:00
for (h = 0, s = 1; h < HSHSIZE; h++)
for (vp = variables[h]; vp != NULL; vp = vp->v_link)
1993-03-21 12:45:37 +03:00
s++;
ap = (char **) salloc(s * sizeof *ap);
for (h = 0, p = ap; h < HSHSIZE; h++)
for (vp = variables[h]; vp != NULL; vp = vp->v_link)
1993-03-21 12:45:37 +03:00
*p++ = vp->v_name;
*p = NULL;
1993-03-21 12:45:37 +03:00
sort(ap);
for (p = ap; *p != NULL; p++)
1993-03-21 12:45:37 +03:00
printf("%s\t%s\n", *p, value(*p));
return(0);
}
errs = 0;
for (ap = arglist; *ap != NULL; ap++) {
1993-03-21 12:45:37 +03:00
cp = *ap;
cp2 = varbuf;
while (*cp != '=' && *cp != '\0')
*cp2++ = *cp++;
*cp2 = '\0';
if (*cp == '\0')
cp = "";
else
cp++;
if (equal(varbuf, "")) {
printf("Non-null variable name required\n");
errs++;
continue;
}
assign(varbuf, cp);
}
return(errs);
}
/*
* Unset a bunch of variable values.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
unset(void *v)
1993-03-21 12:45:37 +03:00
{
char **arglist = v;
struct var *vp, *vp2;
1993-03-21 12:45:37 +03:00
int errs, h;
char **ap;
errs = 0;
for (ap = arglist; *ap != NULL; ap++) {
if ((vp2 = lookup(*ap)) == NULL) {
if (getenv(*ap)) {
unsetenv(*ap);
} else if (!sourcing) {
1993-03-21 12:45:37 +03:00
printf("\"%s\": undefined variable\n", *ap);
errs++;
}
continue;
}
h = hash(*ap);
if (vp2 == variables[h]) {
variables[h] = variables[h]->v_link;
v_free(vp2->v_name);
v_free(vp2->v_value);
1994-05-22 06:57:35 +04:00
free((char *)vp2);
1993-03-21 12:45:37 +03:00
continue;
}
for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
;
vp->v_link = vp2->v_link;
v_free(vp2->v_name);
v_free(vp2->v_value);
1994-05-22 06:57:35 +04:00
free((char *) vp2);
1993-03-21 12:45:37 +03:00
}
return(errs);
}
/*
* Put add users to a group.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
group(void *v)
1993-03-21 12:45:37 +03:00
{
char **argv = v;
struct grouphead *gh;
struct group *gp;
int h;
1993-03-21 12:45:37 +03:00
int s;
char **ap, *gname, **p;
if (*argv == NULL) {
1993-03-21 12:45:37 +03:00
for (h = 0, s = 1; h < HSHSIZE; h++)
for (gh = groups[h]; gh != NULL; gh = gh->g_link)
1993-03-21 12:45:37 +03:00
s++;
ap = (char **) salloc(s * sizeof *ap);
for (h = 0, p = ap; h < HSHSIZE; h++)
for (gh = groups[h]; gh != NULL; gh = gh->g_link)
1993-03-21 12:45:37 +03:00
*p++ = gh->g_name;
*p = NULL;
1993-03-21 12:45:37 +03:00
sort(ap);
for (p = ap; *p != NULL; p++)
1993-03-21 12:45:37 +03:00
printgroup(*p);
return(0);
}
if (argv[1] == NULL) {
1993-03-21 12:45:37 +03:00
printgroup(*argv);
return(0);
}
gname = *argv;
h = hash(gname);
if ((gh = findgroup(gname)) == NULL) {
gh = (struct grouphead *) calloc(1, sizeof *gh);
1993-03-21 12:45:37 +03:00
gh->g_name = vcopy(gname);
gh->g_list = NULL;
1993-03-21 12:45:37 +03:00
gh->g_link = groups[h];
groups[h] = gh;
}
/*
* Insert names from the command list into the group.
* Who cares if there are duplicates? They get tossed
* later anyway.
*/
for (ap = argv+1; *ap != NULL; ap++) {
gp = (struct group *) calloc(1, sizeof *gp);
1993-03-21 12:45:37 +03:00
gp->ge_name = vcopy(*ap);
gp->ge_link = gh->g_list;
gh->g_list = gp;
}
return(0);
}
/*
* Sort the passed string vecotor into ascending dictionary
* order.
*/
1994-06-29 09:09:04 +04:00
void
2002-03-02 17:59:35 +03:00
sort(char **list)
1993-03-21 12:45:37 +03:00
{
char **ap;
1993-03-21 12:45:37 +03:00
for (ap = list; *ap != NULL; ap++)
1993-03-21 12:45:37 +03:00
;
if (ap-list < 2)
return;
1994-06-29 09:09:04 +04:00
qsort(list, ap-list, sizeof(*list), diction);
1993-03-21 12:45:37 +03:00
}
/*
* Do a dictionary order comparison of the arguments from
* qsort.
*/
static int
2002-03-02 17:59:35 +03:00
diction(const void *a, const void *b)
1993-03-21 12:45:37 +03:00
{
1994-06-29 09:09:04 +04:00
return(strcmp(*(char **)a, *(char **)b));
1993-03-21 12:45:37 +03:00
}
/*
* The do nothing command for comments.
*/
/*ARGSUSED*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
null(void *v)
1993-03-21 12:45:37 +03:00
{
return 0;
}
/*
* Change to another file. With no argument, print information about
* the current file.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
file(void *v)
1993-03-21 12:45:37 +03:00
{
char **argv = v;
1993-03-21 12:45:37 +03:00
if (argv[0] == NULL) {
1996-12-28 10:10:57 +03:00
newfileinfo(0);
1993-03-21 12:45:37 +03:00
return 0;
}
if (setfile(*argv) < 0)
return 1;
announce();
return 0;
}
/*
* Expand file names like echo
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
echo(void *v)
1993-03-21 12:45:37 +03:00
{
char **argv = v;
char **ap;
char *cp;
1993-03-21 12:45:37 +03:00
for (ap = argv; *ap != NULL; ap++) {
1993-03-21 12:45:37 +03:00
cp = *ap;
if ((cp = expand(cp)) != NULL) {
1993-03-21 12:45:37 +03:00
if (ap != argv)
putchar(' ');
printf("%s", cp);
}
}
putchar('\n');
return 0;
}
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
Respond(void *v)
1993-03-21 12:45:37 +03:00
{
int *msgvec = v;
if (value("Replyall") == NULL)
1993-03-21 12:45:37 +03:00
return (_Respond(msgvec));
else
return (_respond(msgvec));
}
/*
* Reply to a series of messages by simply mailing to the senders
* and not messing around with the To: and Cc: lists as in normal
* reply.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
_Respond(int msgvec[])
1993-03-21 12:45:37 +03:00
{
struct header head;
struct message *mp;
int *ap;
char *cp;
1993-03-21 12:45:37 +03:00
head.h_to = NULL;
1993-03-21 12:45:37 +03:00
for (ap = msgvec; *ap != 0; ap++) {
mp = &message[*ap - 1];
touch(mp);
dot = mp;
if ((cp = skin(hfield("from", mp))) == NULL)
1993-03-21 12:45:37 +03:00
cp = skin(nameof(mp, 2));
head.h_to = cat(head.h_to, extract(cp, GTO));
}
if (head.h_to == NULL)
1993-03-21 12:45:37 +03:00
return 0;
mp = &message[msgvec[0] - 1];
if ((head.h_subject = hfield("subject", mp)) == NULL)
1993-03-21 12:45:37 +03:00
head.h_subject = hfield("subj", mp);
head.h_subject = reedit(head.h_subject);
head.h_cc = NULL;
head.h_bcc = NULL;
head.h_smopts = NULL;
1993-03-21 12:45:37 +03:00
mail1(&head, 1);
return 0;
}
/*
* Conditional commands. These allow one to parameterize one's
* .mailrc and do some things if sending, others if receiving.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
ifcmd(void *v)
1993-03-21 12:45:37 +03:00
{
char **argv = v;
char *cp;
1993-03-21 12:45:37 +03:00
if (cond != CANY) {
printf("Illegal nested \"if\"\n");
return(1);
}
cond = CANY;
cp = argv[0];
switch (*cp) {
case 'r': case 'R':
cond = CRCV;
break;
case 's': case 'S':
cond = CSEND;
break;
default:
printf("Unrecognized if-keyword: \"%s\"\n", cp);
return(1);
}
return(0);
}
/*
* Implement 'else'. This is pretty simple -- we just
* flip over the conditional flag.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
elsecmd(void *v)
1993-03-21 12:45:37 +03:00
{
switch (cond) {
case CANY:
printf("\"Else\" without matching \"if\"\n");
return(1);
case CSEND:
cond = CRCV;
break;
case CRCV:
cond = CSEND;
break;
default:
printf("Mail's idea of conditions is screwed up\n");
cond = CANY;
break;
}
return(0);
}
/*
* End of if statement. Just set cond back to anything.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
endifcmd(void *v)
1993-03-21 12:45:37 +03:00
{
if (cond == CANY) {
printf("\"Endif\" without matching \"if\"\n");
return(1);
}
cond = CANY;
return(0);
}
/*
* Set the list of alternate names.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
alternates(void *v)
1993-03-21 12:45:37 +03:00
{
char **namelist = v;
int c;
char **ap, **ap2, *cp;
1993-03-21 12:45:37 +03:00
c = argcount(namelist) + 1;
if (c == 1) {
if (altnames == 0)
return(0);
for (ap = altnames; *ap; ap++)
printf("%s ", *ap);
printf("\n");
return(0);
}
if (altnames != 0)
1994-05-22 06:57:35 +04:00
free((char *) altnames);
1993-03-21 12:45:37 +03:00
altnames = (char **) calloc((unsigned) c, sizeof (char *));
for (ap = namelist, ap2 = altnames; *ap; ap++, ap2++) {
cp = (char *) calloc((unsigned) strlen(*ap) + 1, sizeof (char));
strcpy(cp, *ap);
*ap2 = cp;
}
*ap2 = 0;
return(0);
}