xref: /netbsd-src/usr.bin/mail/main.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: main.c,v 1.18 2003/08/07 11:14:40 agc 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 4/20/95";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.18 2003/08/07 11:14:40 agc Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #define EXTERN
47 #include "rcv.h"
48 #undef EXTERN
49 
50 #include "extern.h"
51 
52 extern char *version;
53 
54 int	main(int, char **);
55 
56 /*
57  * Mail -- a mail program
58  *
59  * Startup -- interface with user.
60  */
61 
62 jmp_buf	hdrjmp;
63 
64 int
65 main(int argc, char *argv[])
66 {
67 	int i;
68 	struct name *to, *cc, *bcc, *smopts;
69 	char *subject;
70 	char *ef;
71 	char nosrc = 0;
72 	sig_t prevint;
73 	char *rc;
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 = NULL;
92 	to = NULL;
93 	cc = NULL;
94 	bcc = NULL;
95 	smopts = NULL;
96 	subject = NULL;
97 	while ((i = getopt(argc, argv, "~EINT:b:c:dfins:u:v")) != -1) {
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 				warn("%s", 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 			unsetenv("MAIL");
117 			break;
118 		case 'i':
119 			/*
120 			 * User wants to ignore interrupts.
121 			 * Set the variable "ignore"
122 			 */
123 			assign("ignore", "");
124 			break;
125 		case 'd':
126 			debug++;
127 			break;
128 		case 's':
129 			/*
130 			 * Give a subject field for sending from
131 			 * non terminal
132 			 */
133 			subject = optarg;
134 			break;
135 		case 'f':
136 			/*
137 			 * User is specifying file to "edit" with Mail,
138 			 * as opposed to reading system mailbox.
139 			 * If no argument is given after -f, we read his
140 			 * mbox file.
141 			 *
142 			 * getopt() can't handle optional arguments, so here
143 			 * is an ugly hack to get around it.
144 			 */
145 			if ((argv[optind]) && (argv[optind][0] != '-'))
146 				ef = argv[optind++];
147 			else
148 				ef = "&";
149 			break;
150 		case 'n':
151 			/*
152 			 * User doesn't want to source /usr/lib/Mail.rc
153 			 */
154 			nosrc++;
155 			break;
156 		case 'N':
157 			/*
158 			 * Avoid initial header printing.
159 			 */
160 			assign("noheader", "");
161 			break;
162 		case 'v':
163 			/*
164 			 * Send mailer verbose flag
165 			 */
166 			assign("verbose", "");
167 			break;
168 		case 'I':
169 		case '~':
170 			/*
171 			 * We're interactive
172 			 */
173 			assign("interactive", "");
174 			break;
175 		case 'c':
176 			/*
177 			 * Get Carbon Copy Recipient list
178 			 */
179 			cc = cat(cc, nalloc(optarg, GCC));
180 			break;
181 		case 'b':
182 			/*
183 			 * Get Blind Carbon Copy Recipient list
184 			 */
185 			bcc = cat(bcc, nalloc(optarg, GBCC));
186 			break;
187 		case 'E':
188 			/*
189 			 * Don't send empty files.
190 			 */
191 			assign("dontsendempty", "");
192 			break;
193 		case '?':
194 			fputs("\
195 Usage: mail [-EiInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
196             [- sendmail-options ...]\n\
197        mail [-EiInNv] -f [name]\n\
198        mail [-EiInNv] [-u user]\n",
199 				stderr);
200 			exit(1);
201 		}
202 	}
203 	for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
204 		to = cat(to, nalloc(argv[i], GTO));
205 	for (; argv[i]; i++)
206 		smopts = cat(smopts, nalloc(argv[i], 0));
207 	/*
208 	 * Check for inconsistent arguments.
209 	 */
210 	if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL)) {
211 		fputs("You must specify direct recipients with -s, -c, or -b.\n", stderr);
212 		exit(1);
213 	}
214 	if (ef != NULL && to != NULL) {
215 		fprintf(stderr, "Cannot give -f and people to send to.\n");
216 		exit(1);
217 	}
218 	tinit();
219 	setscreensize();
220 	input = stdin;
221 	rcvmode = !to;
222 	spreserve();
223 	if (!nosrc)
224 		load(_PATH_MASTER_RC);
225 	/*
226 	 * Expand returns a savestr, but load only uses the file name
227 	 * for fopen, so it's safe to do this.
228 	 */
229 	if ((rc = getenv("MAILRC")) == 0)
230 		rc = "~/.mailrc";
231 	load(expand(rc));
232 	if (!rcvmode) {
233 		mail(to, cc, bcc, smopts, subject);
234 		/*
235 		 * why wait?
236 		 */
237 		exit(senderr);
238 	}
239 	/*
240 	 * Ok, we are reading mail.
241 	 * Decide whether we are editing a mailbox or reading
242 	 * the system mailbox, and open up the right stuff.
243 	 */
244 	if (ef == NULL)
245 		ef = "%";
246 	if (setfile(ef) < 0)
247 		exit(1);		/* error already reported */
248 	if (setjmp(hdrjmp) == 0) {
249 		if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
250 			signal(SIGINT, hdrstop);
251 		if (value("quiet") == NULL)
252 			printf("Mail version %s.  Type ? for help.\n",
253 				version);
254 		announce();
255 		fflush(stdout);
256 		signal(SIGINT, prevint);
257 	}
258 	commands();
259 	signal(SIGHUP, SIG_IGN);
260 	signal(SIGINT, SIG_IGN);
261 	signal(SIGQUIT, SIG_IGN);
262 	quit();
263 	exit(0);
264 }
265 
266 /*
267  * Interrupt printing of the headers.
268  */
269 void
270 hdrstop(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(void)
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