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