Home | History | Annotate | Line # | Download | only in mail
main.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1980 Regents of the University of California.
      3  1.1  cgd  * 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.1  cgd char copyright[] =
     36  1.1  cgd "@(#) Copyright (c) 1980 Regents of the University of California.\n\
     37  1.1  cgd  All rights reserved.\n";
     38  1.1  cgd #endif /* not lint */
     39  1.1  cgd 
     40  1.1  cgd #ifndef lint
     41  1.1  cgd static char sccsid[] = "@(#)main.c	5.28 (Berkeley) 4/1/91";
     42  1.1  cgd #endif /* not lint */
     43  1.1  cgd 
     44  1.1  cgd #include "rcv.h"
     45  1.1  cgd #include <sys/stat.h>
     46  1.1  cgd 
     47  1.1  cgd /*
     48  1.1  cgd  * Mail -- a mail program
     49  1.1  cgd  *
     50  1.1  cgd  * Startup -- interface with user.
     51  1.1  cgd  */
     52  1.1  cgd 
     53  1.1  cgd jmp_buf	hdrjmp;
     54  1.1  cgd 
     55  1.1  cgd main(argc, argv)
     56  1.1  cgd 	char **argv;
     57  1.1  cgd {
     58  1.1  cgd 	register int i;
     59  1.1  cgd 	struct name *to, *cc, *bcc, *smopts;
     60  1.1  cgd 	char *subject;
     61  1.1  cgd 	char *ef;
     62  1.1  cgd 	char nosrc = 0;
     63  1.1  cgd 	void hdrstop();
     64  1.1  cgd 	sig_t prevint;
     65  1.1  cgd 	extern int getopt(), optind, opterr;
     66  1.1  cgd 	extern char *optarg;
     67  1.1  cgd 	void sigchild();
     68  1.1  cgd 
     69  1.1  cgd 	/*
     70  1.1  cgd 	 * Set up a reasonable environment.
     71  1.1  cgd 	 * Figure out whether we are being run interactively,
     72  1.1  cgd 	 * start the SIGCHLD catcher, and so forth.
     73  1.1  cgd 	 */
     74  1.1  cgd 	(void) signal(SIGCHLD, sigchild);
     75  1.1  cgd 	if (isatty(0))
     76  1.1  cgd 		assign("interactive", "");
     77  1.1  cgd 	image = -1;
     78  1.1  cgd 	/*
     79  1.1  cgd 	 * Now, determine how we are being used.
     80  1.1  cgd 	 * We successively pick off - flags.
     81  1.1  cgd 	 * If there is anything left, it is the base of the list
     82  1.1  cgd 	 * of users to mail to.  Argp will be set to point to the
     83  1.1  cgd 	 * first of these users.
     84  1.1  cgd 	 */
     85  1.1  cgd 	ef = NOSTR;
     86  1.1  cgd 	to = NIL;
     87  1.1  cgd 	cc = NIL;
     88  1.1  cgd 	bcc = NIL;
     89  1.1  cgd 	smopts = NIL;
     90  1.1  cgd 	subject = NOSTR;
     91  1.1  cgd 	while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != EOF) {
     92  1.1  cgd 		switch (i) {
     93  1.1  cgd 		case 'T':
     94  1.1  cgd 			/*
     95  1.1  cgd 			 * Next argument is temp file to write which
     96  1.1  cgd 			 * articles have been read/deleted for netnews.
     97  1.1  cgd 			 */
     98  1.1  cgd 			Tflag = optarg;
     99  1.1  cgd 			if ((i = creat(Tflag, 0600)) < 0) {
    100  1.1  cgd 				perror(Tflag);
    101  1.1  cgd 				exit(1);
    102  1.1  cgd 			}
    103  1.1  cgd 			close(i);
    104  1.1  cgd 			break;
    105  1.1  cgd 		case 'u':
    106  1.1  cgd 			/*
    107  1.1  cgd 			 * Next argument is person to pretend to be.
    108  1.1  cgd 			 */
    109  1.1  cgd 			myname = optarg;
    110  1.1  cgd 			break;
    111  1.1  cgd 		case 'i':
    112  1.1  cgd 			/*
    113  1.1  cgd 			 * User wants to ignore interrupts.
    114  1.1  cgd 			 * Set the variable "ignore"
    115  1.1  cgd 			 */
    116  1.1  cgd 			assign("ignore", "");
    117  1.1  cgd 			break;
    118  1.1  cgd 		case 'd':
    119  1.1  cgd 			debug++;
    120  1.1  cgd 			break;
    121  1.1  cgd 		case 's':
    122  1.1  cgd 			/*
    123  1.1  cgd 			 * Give a subject field for sending from
    124  1.1  cgd 			 * non terminal
    125  1.1  cgd 			 */
    126  1.1  cgd 			subject = optarg;
    127  1.1  cgd 			break;
    128  1.1  cgd 		case 'f':
    129  1.1  cgd 			/*
    130  1.1  cgd 			 * User is specifying file to "edit" with Mail,
    131  1.1  cgd 			 * as opposed to reading system mailbox.
    132  1.1  cgd 			 * If no argument is given after -f, we read his
    133  1.1  cgd 			 * mbox file.
    134  1.1  cgd 			 *
    135  1.1  cgd 			 * getopt() can't handle optional arguments, so here
    136  1.1  cgd 			 * is an ugly hack to get around it.
    137  1.1  cgd 			 */
    138  1.1  cgd 			if ((argv[optind]) && (argv[optind][0] != '-'))
    139  1.1  cgd 				ef = argv[optind++];
    140  1.1  cgd 			else
    141  1.1  cgd 				ef = "&";
    142  1.1  cgd 			break;
    143  1.1  cgd 		case 'n':
    144  1.1  cgd 			/*
    145  1.1  cgd 			 * User doesn't want to source /usr/lib/Mail.rc
    146  1.1  cgd 			 */
    147  1.1  cgd 			nosrc++;
    148  1.1  cgd 			break;
    149  1.1  cgd 		case 'N':
    150  1.1  cgd 			/*
    151  1.1  cgd 			 * Avoid initial header printing.
    152  1.1  cgd 			 */
    153  1.1  cgd 			assign("noheader", "");
    154  1.1  cgd 			break;
    155  1.1  cgd 		case 'v':
    156  1.1  cgd 			/*
    157  1.1  cgd 			 * Send mailer verbose flag
    158  1.1  cgd 			 */
    159  1.1  cgd 			assign("verbose", "");
    160  1.1  cgd 			break;
    161  1.1  cgd 		case 'I':
    162  1.1  cgd 			/*
    163  1.1  cgd 			 * We're interactive
    164  1.1  cgd 			 */
    165  1.1  cgd 			assign("interactive", "");
    166  1.1  cgd 			break;
    167  1.1  cgd 		case 'c':
    168  1.1  cgd 			/*
    169  1.1  cgd 			 * Get Carbon Copy Recipient list
    170  1.1  cgd 			 */
    171  1.1  cgd 			cc = cat(cc, nalloc(optarg, GCC));
    172  1.1  cgd 			break;
    173  1.1  cgd 		case 'b':
    174  1.1  cgd 			/*
    175  1.1  cgd 			 * Get Blind Carbon Copy Recipient list
    176  1.1  cgd 			 */
    177  1.1  cgd 			bcc = cat(bcc, nalloc(optarg, GBCC));
    178  1.1  cgd 			break;
    179  1.1  cgd 		case '?':
    180  1.1  cgd 			fputs("\
    181  1.1  cgd Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
    182  1.1  cgd             [- sendmail-options ...]\n\
    183  1.1  cgd        mail [-iInNv] -f [name]\n\
    184  1.1  cgd        mail [-iInNv] [-u user]\n",
    185  1.1  cgd 				stderr);
    186  1.1  cgd 			exit(1);
    187  1.1  cgd 		}
    188  1.1  cgd 	}
    189  1.1  cgd 	for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
    190  1.1  cgd 		to = cat(to, nalloc(argv[i], GTO));
    191  1.1  cgd 	for (; argv[i]; i++)
    192  1.1  cgd 		smopts = cat(smopts, nalloc(argv[i], 0));
    193  1.1  cgd 	/*
    194  1.1  cgd 	 * Check for inconsistent arguments.
    195  1.1  cgd 	 */
    196  1.1  cgd 	if (to == NIL && (subject != NOSTR || cc != NIL || bcc != NIL)) {
    197  1.1  cgd 		fputs("You must specify direct recipients with -s, -c, or -b.\n", stderr);
    198  1.1  cgd 		exit(1);
    199  1.1  cgd 	}
    200  1.1  cgd 	if (ef != NOSTR && to != NIL) {
    201  1.1  cgd 		fprintf(stderr, "Cannot give -f and people to send to.\n");
    202  1.1  cgd 		exit(1);
    203  1.1  cgd 	}
    204  1.1  cgd 	tinit();
    205  1.1  cgd 	setscreensize();
    206  1.1  cgd 	input = stdin;
    207  1.1  cgd 	rcvmode = !to;
    208  1.1  cgd 	spreserve();
    209  1.1  cgd 	if (!nosrc)
    210  1.1  cgd 		load(_PATH_MASTER_RC);
    211  1.1  cgd 	/*
    212  1.1  cgd 	 * Expand returns a savestr, but load only uses the file name
    213  1.1  cgd 	 * for fopen, so it's safe to do this.
    214  1.1  cgd 	 */
    215  1.1  cgd 	load(expand("~/.mailrc"));
    216  1.1  cgd 	if (!rcvmode) {
    217  1.1  cgd 		mail(to, cc, bcc, smopts, subject);
    218  1.1  cgd 		/*
    219  1.1  cgd 		 * why wait?
    220  1.1  cgd 		 */
    221  1.1  cgd 		exit(senderr);
    222  1.1  cgd 	}
    223  1.1  cgd 	/*
    224  1.1  cgd 	 * Ok, we are reading mail.
    225  1.1  cgd 	 * Decide whether we are editing a mailbox or reading
    226  1.1  cgd 	 * the system mailbox, and open up the right stuff.
    227  1.1  cgd 	 */
    228  1.1  cgd 	if (ef == NOSTR)
    229  1.1  cgd 		ef = "%";
    230  1.1  cgd 	if (setfile(ef) < 0)
    231  1.1  cgd 		exit(1);		/* error already reported */
    232  1.1  cgd 	if (setjmp(hdrjmp) == 0) {
    233  1.1  cgd 		extern char *version;
    234  1.1  cgd 
    235  1.1  cgd 		if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
    236  1.1  cgd 			signal(SIGINT, hdrstop);
    237  1.1  cgd 		if (value("quiet") == NOSTR)
    238  1.1  cgd 			printf("Mail version %s.  Type ? for help.\n",
    239  1.1  cgd 				version);
    240  1.1  cgd 		announce();
    241  1.1  cgd 		fflush(stdout);
    242  1.1  cgd 		signal(SIGINT, prevint);
    243  1.1  cgd 	}
    244  1.1  cgd 	commands();
    245  1.1  cgd 	signal(SIGHUP, SIG_IGN);
    246  1.1  cgd 	signal(SIGINT, SIG_IGN);
    247  1.1  cgd 	signal(SIGQUIT, SIG_IGN);
    248  1.1  cgd 	quit();
    249  1.1  cgd 	exit(0);
    250  1.1  cgd }
    251  1.1  cgd 
    252  1.1  cgd /*
    253  1.1  cgd  * Interrupt printing of the headers.
    254  1.1  cgd  */
    255  1.1  cgd void
    256  1.1  cgd hdrstop()
    257  1.1  cgd {
    258  1.1  cgd 
    259  1.1  cgd 	fflush(stdout);
    260  1.1  cgd 	fprintf(stderr, "\nInterrupt\n");
    261  1.1  cgd 	longjmp(hdrjmp, 1);
    262  1.1  cgd }
    263  1.1  cgd 
    264  1.1  cgd /*
    265  1.1  cgd  * Compute what the screen size for printing headers should be.
    266  1.1  cgd  * We use the following algorithm for the height:
    267  1.1  cgd  *	If baud rate < 1200, use  9
    268  1.1  cgd  *	If baud rate = 1200, use 14
    269  1.1  cgd  *	If baud rate > 1200, use 24 or ws_row
    270  1.1  cgd  * Width is either 80 or ws_col;
    271  1.1  cgd  */
    272  1.1  cgd setscreensize()
    273  1.1  cgd {
    274  1.1  cgd 	struct sgttyb tbuf;
    275  1.1  cgd 	struct winsize ws;
    276  1.1  cgd 
    277  1.1  cgd 	if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
    278  1.1  cgd 		ws.ws_col = ws.ws_row = 0;
    279  1.1  cgd 	if (ioctl(1, TIOCGETP, &tbuf) < 0)
    280  1.1  cgd 		tbuf.sg_ospeed = B9600;
    281  1.1  cgd 	if (tbuf.sg_ospeed < B1200)
    282  1.1  cgd 		screenheight = 9;
    283  1.1  cgd 	else if (tbuf.sg_ospeed == B1200)
    284  1.1  cgd 		screenheight = 14;
    285  1.1  cgd 	else if (ws.ws_row != 0)
    286  1.1  cgd 		screenheight = ws.ws_row;
    287  1.1  cgd 	else
    288  1.1  cgd 		screenheight = 24;
    289  1.1  cgd 	if ((realscreenheight = ws.ws_row) == 0)
    290  1.1  cgd 		realscreenheight = 24;
    291  1.1  cgd 	if ((screenwidth = ws.ws_col) == 0)
    292  1.1  cgd 		screenwidth = 80;
    293  1.1  cgd }
    294