#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)cmd2.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: cmd2.c,v 1.8 1997/10/19 05:03:03 lukem Exp $");
#endif
#endif
#include "rcv.h"
#include "extern.h"
static int igcomp __P((const void *, const void *));
int
next(v)
void *v;
{
int *msgvec = v;
struct message *mp;
int *ip, *ip2;
int list[2], mdot;
if (*msgvec != 0) {
mdot = dot - &message[0] + 1;
for (ip = msgvec; *ip != 0; ip++)
if (*ip > mdot)
break;
if (*ip == 0)
ip = msgvec;
ip2 = ip;
do {
mp = &message[*ip2 - 1];
if ((mp->m_flag & MDELETED) == 0) {
dot = mp;
goto hitit;
}
if (*ip2 != 0)
ip2++;
if (*ip2 == 0)
ip2 = msgvec;
} while (ip2 != ip);
printf("No messages applicable\n");
return(1);
}
if (!sawcom)
goto hitit;
for (mp = dot+1; mp < &message[msgCount]; mp++)
if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
break;
if (mp >= &message[msgCount]) {
printf("At EOF\n");
return(0);
}
dot = mp;
hitit:
list[0] = dot - &message[0] + 1;
list[1] = 0;
return(type(list));
}
int
save(v)
void *v;
{
char *str = v;
return save1(str, 1, "save", saveignore);
}
int
copycmd(v)
void *v;
{
char *str = v;
return save1(str, 0, "copy", saveignore);
}
int
save1(str, mark, cmd, ignore)
char str[];
int mark;
char *cmd;
struct ignoretab *ignore;
{
int *ip;
struct message *mp;
char *file, *disp;
int f, *msgvec;
FILE *obuf;
msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
if ((file = snarf(str, &f)) == NOSTR)
return(1);
if (!f) {
*msgvec = first(0, MMNORM);
if (*msgvec == 0) {
printf("No messages to %s.\n", cmd);
return(1);
}
msgvec[1] = 0;
}
if (f && getmsglist(str, msgvec, 0) < 0)
return(1);
if ((file = expand(file)) == NOSTR)
return(1);
printf("\"%s\" ", file);
fflush(stdout);
if (access(file, 0) >= 0)
disp = "[Appended]";
else
disp = "[New file]";
if ((obuf = Fopen(file, "a")) == NULL) {
perror(NOSTR);
return(1);
}
for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
mp = &message[*ip - 1];
touch(mp);
if (send(mp, obuf, ignore, NOSTR) < 0) {
perror(file);
Fclose(obuf);
return(1);
}
if (mark)
mp->m_flag |= MSAVED;
}
fflush(obuf);
if (ferror(obuf))
perror(file);
Fclose(obuf);
printf("%s\n", disp);
return(0);
}
int
swrite(v)
void *v;
{
char *str = v;
return save1(str, 1, "write", ignoreall);
}
char *
snarf(linebuf, flag)
char linebuf[];
int *flag;
{
char *cp;
*flag = 1;
cp = strlen(linebuf) + linebuf - 1;
while (cp > linebuf && isspace(*cp))
cp--;
*++cp = 0;
while (cp > linebuf && !isspace(*cp))
cp--;
if (*cp == '\0') {
printf("No file specified.\n");
return(NOSTR);
}
if (isspace(*cp))
*cp++ = 0;
else
*flag = 0;
return(cp);
}
int
delete(v)
void *v;
{
int *msgvec = v;
delm(msgvec);
return 0;
}
int
deltype(v)
void *v;
{
int *msgvec = v;
int list[2];
int lastdot;
lastdot = dot - &message[0] + 1;
if (delm(msgvec) >= 0) {
list[0] = dot - &message[0] + 1;
if (list[0] > lastdot) {
touch(dot);
list[1] = 0;
return(type(list));
}
printf("At EOF\n");
} else
printf("No more messages\n");
return(0);
}
int
delm(msgvec)
int *msgvec;
{
struct message *mp;
int *ip;
int last;
last = 0;
for (ip = msgvec; *ip != 0; ip++) {
mp = &message[*ip - 1];
touch(mp);
mp->m_flag |= MDELETED|MTOUCH;
mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
last = *ip;
}
if (last != 0) {
dot = &message[last-1];
last = first(0, MDELETED);
if (last != 0) {
dot = &message[last-1];
return(0);
}
else {
dot = &message[0];
return(-1);
}
}
return(-1);
}
int
undeletecmd(v)
void *v;
{
int *msgvec = v;
struct message *mp;
int *ip;
for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
mp = &message[*ip - 1];
touch(mp);
dot = mp;
mp->m_flag &= ~MDELETED;
}
return 0;
}
int
core(v)
void *v;
{
int pid;
extern union wait wait_status;
switch (pid = vfork()) {
case -1:
perror("fork");
return(1);
case 0:
abort();
_exit(1);
}
printf("Okie dokie");
fflush(stdout);
wait_child(pid);
if (wait_status.w_coredump)
printf(" -- Core dumped.\n");
else
printf(" -- Can't dump core.\n");
return 0;
}
int
clobber(v)
void *v;
{
char **argv = v;
int times;
if (argv[0] == 0)
times = 1;
else
times = (atoi(argv[0]) + 511) / 512;
clob1(times);
return 0;
}
void
clob1(n)
int n;
{
char buf[512];
char *cp;
if (n <= 0)
return;
for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
;
clob1(n - 1);
}
int
retfield(v)
void *v;
{
char **list = v;
return ignore1(list, ignore + 1, "retained");
}
int
igfield(v)
void *v;
{
char **list = v;
return ignore1(list, ignore, "ignored");
}
int
saveretfield(v)
void *v;
{
char **list = v;
return ignore1(list, saveignore + 1, "retained");
}
int
saveigfield(v)
void *v;
{
char **list = v;
return ignore1(list, saveignore, "ignored");
}
int
ignore1(list, tab, which)
char *list[];
struct ignoretab *tab;
char *which;
{
char field[LINESIZE];
int h;
struct ignore *igp;
char **ap;
if (*list == NOSTR)
return igshow(tab, which);
for (ap = list; *ap != 0; ap++) {
istrcpy(field, *ap);
if (member(field, tab))
continue;
h = hash(field);
igp = (struct ignore *) calloc(1, sizeof (struct ignore));
igp->i_field = calloc((unsigned) strlen(field) + 1,
sizeof (char));
strcpy(igp->i_field, field);
igp->i_link = tab->i_head[h];
tab->i_head[h] = igp;
tab->i_count++;
}
return 0;
}
int
igshow(tab, which)
struct ignoretab *tab;
char *which;
{
int h;
struct ignore *igp;
char **ap, **ring;
if (tab->i_count == 0) {
printf("No fields currently being %s.\n", which);
return 0;
}
ring = (char **) salloc((tab->i_count + 1) * sizeof (char *));
ap = ring;
for (h = 0; h < HSHSIZE; h++)
for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
*ap++ = igp->i_field;
*ap = 0;
qsort(ring, tab->i_count, sizeof (char *), igcomp);
for (ap = ring; *ap != 0; ap++)
printf("%s\n", *ap);
return 0;
}
static int
igcomp(l, r)
const void *l, *r;
{
return (strcmp(*(char **)l, *(char **)r));
}