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