#include "rcv.h"
#ifndef preSVr4
#include <locale.h>
#endif
static void hdrstop(int);
static jmp_buf hdrjmp;
const char *const version = "mailx version 5.0";
int
main(int argc, char **argv)
{
register char *ef;
register int argp;
int mustsend, f, goerr = 0;
void (*prevint)(int);
int loaded = 0;
struct termio tbuf;
struct termios tbufs;
int c;
char *cwd, *mf;
#ifndef preSVr4
(void)setlocale(LC_ALL, "");
#endif
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
#ifdef SIGCONT
sigset(SIGCONT, SIG_DFL);
#endif
rpterr = 0;
progname = argv[0];
if (progname[strlen(progname) - 1] != 'x') {
assign("bsdcompat", "");
}
myegid = getegid();
myrgid = getgid();
myeuid = geteuid();
myruid = getuid();
mypid = getpid();
setgid(myrgid);
setuid(myruid);
inithost();
intty = isatty(0);
if (ioctl(1, TCGETS, &tbufs) < 0) {
if (ioctl(1, TCGETA, &tbuf)==0) {
outtty = 1;
baud = tbuf.c_cflag & CBAUD;
} else
baud = B9600;
} else {
outtty = 1;
baud = cfgetospeed(&tbufs);
}
image = -1;
ef = NOSTR;
argp = -1;
mustsend = 0;
if (argc > 0 && **argv == 'r')
rmail++;
while ((c = getopt(argc, argv, "b:Bc:defFh:HiInNr:s:u:UtT:vV~")) != EOF)
switch (c) {
case 'e':
exitflg++;
break;
case 'r':
mustsend++;
rflag = optarg;
break;
case 'T':
Tflag = optarg;
if ((f = creat(Tflag, TEMPPERM)) < 0) {
perror(Tflag);
exit(1);
}
close(f);
case 'I':
newsflg++;
break;
case 'u':
{
static char u[PATHSIZE];
snprintf(u, sizeof (u), "%s%s", maildir, optarg);
ef = u;
break;
}
case 'i':
assign("ignore", "");
break;
case 'U':
UnUUCP++;
break;
case 'd':
assign("debug", "");
break;
case 'h':
mustsend++;
hflag = atoi(optarg);
if (hflag == 0) {
fprintf(stderr,
gettext("-h needs non-zero number\n"));
goerr++;
}
break;
case 's':
mustsend++;
sflag = optarg;
break;
case 'c':
mustsend++;
cflag = optarg;
break;
case 'b':
mustsend++;
bflag = optarg;
break;
case 'f':
ef = (argc == optind || *argv[optind] == '-')
? "" : argv[optind++];
if (*ef && *ef != '/' && *ef != '+')
cwd = getcwd(NOSTR, PATHSIZE);
break;
case 'F':
Fflag++;
mustsend++;
break;
case 'n':
nosrc++;
break;
case 'N':
noheader++;
break;
case 'H':
Hflag++;
break;
case 'V':
puts(version);
return 0;
case '~':
assign("escapeok", "");
break;
case 'v':
assign("verbose", "");
break;
case 'B':
setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
break;
case 't':
tflag++;
mustsend++;
break;
case '?':
default:
goerr++;
break;
}
if (optind != argc)
argp = optind;
if (newsflg && ef==NOSTR) {
fprintf(stderr, gettext("Need -f with -I flag\n"));
goerr++;
}
if (ef != NOSTR && argp != -1) {
fprintf(stderr,
gettext("Cannot give -f and people to send to.\n"));
goerr++;
}
if (exitflg && (mustsend || argp != -1))
exit(1);
if (tflag && argp != -1) {
fprintf(stderr,
gettext("Ignoring recipients on command line with -t\n"));
argp = -1;
} else if (!tflag && mustsend && argp == -1) {
fprintf(stderr,
gettext("The flags you gave are used only when sending mail.\n"));
goerr++;
}
if (goerr) {
fprintf(stderr,
gettext("Usage: %s -eiIUdFntBNHvV~ -T FILE -u USER -h hops -r address\n"),
progname);
fprintf(stderr,
gettext("\t\t-s SUBJECT -f FILE users\n"));
exit(1);
}
tinit();
input = stdin;
rcvmode = !tflag && argp == -1;
if (!nosrc)
load(MASTER);
if (!rcvmode) {
load(Getf("MAILRC"));
if (tflag)
tmail();
else
mail(&argv[argp]);
exit(senderr ? senderr : rpterr);
}
nstrcpy(origname, PATHSIZE, mailname);
editfile = mailname;
if (ef != NOSTR) {
if (ef == NOSTR || *ef == '\0' || *ef == '+') {
load(Getf("MAILRC"));
loaded++;
}
ef = *ef ? safeexpand(ef) : Getf("MBOX");
nstrcpy(origname, PATHSIZE, ef);
if (ef[0] != '/') {
if (cwd == NOSTR)
cwd = getcwd(NOSTR, PATHSIZE);
nstrcat(cwd, PATHSIZE, "/");
nstrcat(cwd, PATHSIZE, ef);
ef = cwd;
}
editfile = ef;
edit++;
}
if (setfile(editfile, edit) < 0)
exit(1);
if (!loaded)
load(Getf("MAILRC"));
if (msgCount > 0 && !noheader && value("header") != NOSTR) {
if (setjmp(hdrjmp) == 0) {
if ((prevint = sigset(SIGINT, SIG_IGN)) != SIG_IGN)
sigset(SIGINT, hdrstop);
announce();
fflush(stdout);
sigset(SIGINT, prevint);
}
}
if (Hflag || (!edit && msgCount == 0)) {
if (!Hflag) {
fprintf(stderr, gettext("No mail for %s\n"), myname);
Verhogen();
}
fflush(stdout);
exit(rpterr);
}
commands();
sigset(SIGHUP, SIG_IGN);
sigset(SIGINT, SIG_IGN);
sigset(SIGQUIT, SIG_IGN);
if (!outtty)
sigset(SIGPIPE, SIG_IGN);
if (edit)
edstop(0);
else {
quit(0);
Verhogen();
}
return (rpterr);
}
static void
#ifdef __cplusplus
hdrstop(int)
#else
hdrstop(int s)
#endif
{
fflush(stdout);
fprintf(stderr, gettext("\nInterrupt\n"));
# ifdef OLD_BSD_SIGS
sigrelse(SIGINT);
# endif
longjmp(hdrjmp, 1);
}