Home | History | Annotate | Line # | Download | only in mail
main.c revision 1.3
      1  1.1      cgd /*
      2  1.3  deraadt  * Copyright (c) 1980, 1993
      3  1.3  deraadt  *	The Regents of the University of California.  All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.3  deraadt static char copyright[] =
     36  1.3  deraadt "@(#) Copyright (c) 1980, 1993\n\
     37  1.3  deraadt 	The Regents of the University of California.  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.3  deraadt static char sccsid[] = "from: @(#)main.c	8.1 (Berkeley) 6/6/93";
     42  1.2  mycroft static char rcsid[] = "$Id: main.c,v 1.3 1994/06/29 05:09:34 deraadt Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #include "rcv.h"
     46  1.3  deraadt #include <fcntl.h>
     47  1.3  deraadt #include "extern.h"
     48  1.1      cgd 
     49  1.1      cgd /*
     50  1.1      cgd  * Mail -- a mail program
     51  1.1      cgd  *
     52  1.1      cgd  * Startup -- interface with user.
     53  1.1      cgd  */
     54  1.1      cgd 
     55  1.1      cgd jmp_buf	hdrjmp;
     56  1.1      cgd 
     57  1.3  deraadt int
     58  1.1      cgd main(argc, argv)
     59  1.3  deraadt 	int argc;
     60  1.3  deraadt 	char *argv[];
     61  1.1      cgd {
     62  1.1      cgd 	register int i;
     63  1.1      cgd 	struct name *to, *cc, *bcc, *smopts;
     64  1.1      cgd 	char *subject;
     65  1.1      cgd 	char *ef;
     66  1.1      cgd 	char nosrc = 0;
     67  1.1      cgd 	void hdrstop();
     68  1.1      cgd 	sig_t prevint;
     69  1.1      cgd 	void sigchild();
     70  1.1      cgd 
     71  1.1      cgd 	/*
     72  1.1      cgd 	 * Set up a reasonable environment.
     73  1.1      cgd 	 * Figure out whether we are being run interactively,
     74  1.1      cgd 	 * start the SIGCHLD catcher, and so forth.
     75  1.1      cgd 	 */
     76  1.1      cgd 	(void) signal(SIGCHLD, sigchild);
     77  1.1      cgd 	if (isatty(0))
     78  1.1      cgd 		assign("interactive", "");
     79  1.1      cgd 	image = -1;
     80  1.1      cgd 	/*
     81  1.1      cgd 	 * Now, determine how we are being used.
     82  1.1      cgd 	 * We successively pick off - flags.
     83  1.1      cgd 	 * If there is anything left, it is the base of the list
     84  1.1      cgd 	 * of users to mail to.  Argp will be set to point to the
     85  1.1      cgd 	 * first of these users.
     86  1.1      cgd 	 */
     87  1.1      cgd 	ef = NOSTR;
     88  1.1      cgd 	to = NIL;
     89  1.1      cgd 	cc = NIL;
     90  1.1      cgd 	bcc = NIL;
     91  1.1      cgd 	smopts = NIL;
     92  1.1      cgd 	subject = NOSTR;
     93  1.1      cgd 	while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != EOF) {
     94  1.1      cgd 		switch (i) {
     95  1.1      cgd 		case 'T':
     96  1.1      cgd 			/*
     97  1.1      cgd 			 * Next argument is temp file to write which
     98  1.1      cgd 			 * articles have been read/deleted for netnews.
     99  1.1      cgd 			 */
    100  1.1      cgd 			Tflag = optarg;
    101  1.1      cgd 			if ((i = creat(Tflag, 0600)) < 0) {
    102  1.1      cgd 				perror(Tflag);
    103  1.1      cgd 				exit(1);
    104  1.1      cgd 			}
    105  1.1      cgd 			close(i);
    106  1.1      cgd 			break;
    107  1.1      cgd 		case 'u':
    108  1.1      cgd 			/*
    109  1.1      cgd 			 * Next argument is person to pretend to be.
    110  1.1      cgd 			 */
    111  1.1      cgd 			myname = optarg;
    112  1.1      cgd 			break;
    113  1.1      cgd 		case 'i':
    114  1.1      cgd 			/*
    115  1.1      cgd 			 * User wants to ignore interrupts.
    116  1.1      cgd 			 * Set the variable "ignore"
    117  1.1      cgd 			 */
    118  1.1      cgd 			assign("ignore", "");
    119  1.1      cgd 			break;
    120  1.1      cgd 		case 'd':
    121  1.1      cgd 			debug++;
    122  1.1      cgd 			break;
    123  1.1      cgd 		case 's':
    124  1.1      cgd 			/*
    125  1.1      cgd 			 * Give a subject field for sending from
    126  1.1      cgd 			 * non terminal
    127  1.1      cgd 			 */
    128  1.1      cgd 			subject = optarg;
    129  1.1      cgd 			break;
    130  1.1      cgd 		case 'f':
    131  1.1      cgd 			/*
    132  1.1      cgd 			 * User is specifying file to "edit" with Mail,
    133  1.1      cgd 			 * as opposed to reading system mailbox.
    134  1.1      cgd 			 * If no argument is given after -f, we read his
    135  1.1      cgd 			 * mbox file.
    136  1.1      cgd 			 *
    137  1.1      cgd 			 * getopt() can't handle optional arguments, so here
    138  1.1      cgd 			 * is an ugly hack to get around it.
    139  1.1      cgd 			 */
    140  1.1      cgd 			if ((argv[optind]) && (argv[optind][0] != '-'))
    141  1.1      cgd 				ef = argv[optind++];
    142  1.1      cgd 			else
    143  1.1      cgd 				ef = "&";
    144  1.1      cgd 			break;
    145  1.1      cgd 		case 'n':
    146  1.1      cgd 			/*
    147  1.1      cgd 			 * User doesn't want to source /usr/lib/Mail.rc
    148  1.1      cgd 			 */
    149  1.1      cgd 			nosrc++;
    150  1.1      cgd 			break;
    151  1.1      cgd 		case 'N':
    152  1.1      cgd 			/*
    153  1.1      cgd 			 * Avoid initial header printing.
    154  1.1      cgd 			 */
    155  1.1      cgd 			assign("noheader", "");
    156  1.1      cgd 			break;
    157  1.1      cgd 		case 'v':
    158  1.1      cgd 			/*
    159  1.1      cgd 			 * Send mailer verbose flag
    160  1.1      cgd 			 */
    161  1.1      cgd 			assign("verbose", "");
    162  1.1      cgd 			break;
    163  1.1      cgd 		case 'I':
    164  1.1      cgd 			/*
    165  1.1      cgd 			 * We're interactive
    166  1.1      cgd 			 */
    167  1.1      cgd 			assign("interactive", "");
    168  1.1      cgd 			break;
    169  1.1      cgd 		case 'c':
    170  1.1      cgd 			/*
    171  1.1      cgd 			 * Get Carbon Copy Recipient list
    172  1.1      cgd 			 */
    173  1.1      cgd 			cc = cat(cc, nalloc(optarg, GCC));
    174  1.1      cgd 			break;
    175  1.1      cgd 		case 'b':
    176  1.1      cgd 			/*
    177  1.1      cgd 			 * Get Blind Carbon Copy Recipient list
    178  1.1      cgd 			 */
    179  1.1      cgd 			bcc = cat(bcc, nalloc(optarg, GBCC));
    180  1.1      cgd 			break;
    181  1.1      cgd 		case '?':
    182  1.1      cgd 			fputs("\
    183  1.1      cgd Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
    184  1.1      cgd             [- sendmail-options ...]\n\
    185  1.1      cgd        mail [-iInNv] -f [name]\n\
    186  1.1      cgd        mail [-iInNv] [-u user]\n",
    187  1.1      cgd 				stderr);
    188  1.1      cgd 			exit(1);
    189  1.1      cgd 		}
    190  1.1      cgd 	}
    191  1.1      cgd 	for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
    192  1.1      cgd 		to = cat(to, nalloc(argv[i], GTO));
    193  1.1      cgd 	for (; argv[i]; i++)
    194  1.1      cgd 		smopts = cat(smopts, nalloc(argv[i], 0));
    195  1.1      cgd 	/*
    196  1.1      cgd 	 * Check for inconsistent arguments.
    197  1.1      cgd 	 */
    198  1.1      cgd 	if (to == NIL && (subject != NOSTR || cc != NIL || bcc != NIL)) {
    199  1.1      cgd 		fputs("You must specify direct recipients with -s, -c, or -b.\n", stderr);
    200  1.1      cgd 		exit(1);
    201  1.1      cgd 	}
    202  1.1      cgd 	if (ef != NOSTR && to != NIL) {
    203  1.1      cgd 		fprintf(stderr, "Cannot give -f and people to send to.\n");
    204  1.1      cgd 		exit(1);
    205  1.1      cgd 	}
    206  1.1      cgd 	tinit();
    207  1.1      cgd 	setscreensize();
    208  1.1      cgd 	input = stdin;
    209  1.1      cgd 	rcvmode = !to;
    210  1.1      cgd 	spreserve();
    211  1.1      cgd 	if (!nosrc)
    212  1.1      cgd 		load(_PATH_MASTER_RC);
    213  1.1      cgd 	/*
    214  1.1      cgd 	 * Expand returns a savestr, but load only uses the file name
    215  1.1      cgd 	 * for fopen, so it's safe to do this.
    216  1.1      cgd 	 */
    217  1.1      cgd 	load(expand("~/.mailrc"));
    218  1.1      cgd 	if (!rcvmode) {
    219  1.1      cgd 		mail(to, cc, bcc, smopts, subject);
    220  1.1      cgd 		/*
    221  1.1      cgd 		 * why wait?
    222  1.1      cgd 		 */
    223  1.1      cgd 		exit(senderr);
    224  1.1      cgd 	}
    225  1.1      cgd 	/*
    226  1.1      cgd 	 * Ok, we are reading mail.
    227  1.1      cgd 	 * Decide whether we are editing a mailbox or reading
    228  1.1      cgd 	 * the system mailbox, and open up the right stuff.
    229  1.1      cgd 	 */
    230  1.1      cgd 	if (ef == NOSTR)
    231  1.1      cgd 		ef = "%";
    232  1.1      cgd 	if (setfile(ef) < 0)
    233  1.1      cgd 		exit(1);		/* error already reported */
    234  1.1      cgd 	if (setjmp(hdrjmp) == 0) {
    235  1.1      cgd 		extern char *version;
    236  1.1      cgd 
    237  1.1      cgd 		if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
    238  1.1      cgd 			signal(SIGINT, hdrstop);
    239  1.1      cgd 		if (value("quiet") == NOSTR)
    240  1.1      cgd 			printf("Mail version %s.  Type ? for help.\n",
    241  1.1      cgd 				version);
    242  1.1      cgd 		announce();
    243  1.1      cgd 		fflush(stdout);
    244  1.1      cgd 		signal(SIGINT, prevint);
    245  1.1      cgd 	}
    246  1.1      cgd 	commands();
    247  1.1      cgd 	signal(SIGHUP, SIG_IGN);
    248  1.1      cgd 	signal(SIGINT, SIG_IGN);
    249  1.1      cgd 	signal(SIGQUIT, SIG_IGN);
    250  1.1      cgd 	quit();
    251  1.1      cgd 	exit(0);
    252  1.1      cgd }
    253  1.1      cgd 
    254  1.1      cgd /*
    255  1.1      cgd  * Interrupt printing of the headers.
    256  1.1      cgd  */
    257  1.1      cgd void
    258  1.3  deraadt hdrstop(signo)
    259  1.3  deraadt 	int signo;
    260  1.1      cgd {
    261  1.1      cgd 
    262  1.1      cgd 	fflush(stdout);
    263  1.1      cgd 	fprintf(stderr, "\nInterrupt\n");
    264  1.1      cgd 	longjmp(hdrjmp, 1);
    265  1.1      cgd }
    266  1.1      cgd 
    267  1.1      cgd /*
    268  1.1      cgd  * Compute what the screen size for printing headers should be.
    269  1.1      cgd  * We use the following algorithm for the height:
    270  1.1      cgd  *	If baud rate < 1200, use  9
    271  1.1      cgd  *	If baud rate = 1200, use 14
    272  1.1      cgd  *	If baud rate > 1200, use 24 or ws_row
    273  1.1      cgd  * Width is either 80 or ws_col;
    274  1.1      cgd  */
    275  1.3  deraadt void
    276  1.1      cgd setscreensize()
    277  1.1      cgd {
    278  1.1      cgd 	struct sgttyb tbuf;
    279  1.1      cgd 	struct winsize ws;
    280  1.1      cgd 
    281  1.1      cgd 	if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
    282  1.1      cgd 		ws.ws_col = ws.ws_row = 0;
    283  1.1      cgd 	if (ioctl(1, TIOCGETP, &tbuf) < 0)
    284  1.1      cgd 		tbuf.sg_ospeed = B9600;
    285  1.1      cgd 	if (tbuf.sg_ospeed < B1200)
    286  1.1      cgd 		screenheight = 9;
    287  1.1      cgd 	else if (tbuf.sg_ospeed == B1200)
    288  1.1      cgd 		screenheight = 14;
    289  1.1      cgd 	else if (ws.ws_row != 0)
    290  1.1      cgd 		screenheight = ws.ws_row;
    291  1.1      cgd 	else
    292  1.1      cgd 		screenheight = 24;
    293  1.1      cgd 	if ((realscreenheight = ws.ws_row) == 0)
    294  1.1      cgd 		realscreenheight = 24;
    295  1.1      cgd 	if ((screenwidth = ws.ws_col) == 0)
    296  1.1      cgd 		screenwidth = 80;
    297  1.1      cgd }
    298