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