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