xref: /netbsd-src/usr.bin/mail/main.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: main.c,v 1.9 1998/10/08 17:36:56 wsanchez 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.9 1998/10/08 17:36:56 wsanchez 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 			break;
121 		case 'i':
122 			/*
123 			 * User wants to ignore interrupts.
124 			 * Set the variable "ignore"
125 			 */
126 			assign("ignore", "");
127 			break;
128 		case 'd':
129 			debug++;
130 			break;
131 		case 's':
132 			/*
133 			 * Give a subject field for sending from
134 			 * non terminal
135 			 */
136 			subject = optarg;
137 			break;
138 		case 'f':
139 			/*
140 			 * User is specifying file to "edit" with Mail,
141 			 * as opposed to reading system mailbox.
142 			 * If no argument is given after -f, we read his
143 			 * mbox file.
144 			 *
145 			 * getopt() can't handle optional arguments, so here
146 			 * is an ugly hack to get around it.
147 			 */
148 			if ((argv[optind]) && (argv[optind][0] != '-'))
149 				ef = argv[optind++];
150 			else
151 				ef = "&";
152 			break;
153 		case 'n':
154 			/*
155 			 * User doesn't want to source /usr/lib/Mail.rc
156 			 */
157 			nosrc++;
158 			break;
159 		case 'N':
160 			/*
161 			 * Avoid initial header printing.
162 			 */
163 			assign("noheader", "");
164 			break;
165 		case 'v':
166 			/*
167 			 * Send mailer verbose flag
168 			 */
169 			assign("verbose", "");
170 			break;
171 		case 'I':
172 			/*
173 			 * We're interactive
174 			 */
175 			assign("interactive", "");
176 			break;
177 		case 'c':
178 			/*
179 			 * Get Carbon Copy Recipient list
180 			 */
181 			cc = cat(cc, nalloc(optarg, GCC));
182 			break;
183 		case 'b':
184 			/*
185 			 * Get Blind Carbon Copy Recipient list
186 			 */
187 			bcc = cat(bcc, nalloc(optarg, GBCC));
188 			break;
189 		case '?':
190 			fputs("\
191 Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
192             [- sendmail-options ...]\n\
193        mail [-iInNv] -f [name]\n\
194        mail [-iInNv] [-u user]\n",
195 				stderr);
196 			exit(1);
197 		}
198 	}
199 	for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
200 		to = cat(to, nalloc(argv[i], GTO));
201 	for (; argv[i]; i++)
202 		smopts = cat(smopts, nalloc(argv[i], 0));
203 	/*
204 	 * Check for inconsistent arguments.
205 	 */
206 	if (to == NIL && (subject != NOSTR || cc != NIL || bcc != NIL)) {
207 		fputs("You must specify direct recipients with -s, -c, or -b.\n", stderr);
208 		exit(1);
209 	}
210 	if (ef != NOSTR && to != NIL) {
211 		fprintf(stderr, "Cannot give -f and people to send to.\n");
212 		exit(1);
213 	}
214 	tinit();
215 	setscreensize();
216 	input = stdin;
217 	rcvmode = !to;
218 	spreserve();
219 	if (!nosrc)
220 		load(_PATH_MASTER_RC);
221 	/*
222 	 * Expand returns a savestr, but load only uses the file name
223 	 * for fopen, so it's safe to do this.
224 	 */
225 	if ((rc = getenv("MAILRC")) == 0)
226 		rc = "~/.mailrc";
227 	load(expand(rc));
228 	if (!rcvmode) {
229 		mail(to, cc, bcc, smopts, subject);
230 		/*
231 		 * why wait?
232 		 */
233 		exit(senderr);
234 	}
235 	/*
236 	 * Ok, we are reading mail.
237 	 * Decide whether we are editing a mailbox or reading
238 	 * the system mailbox, and open up the right stuff.
239 	 */
240 	if (ef == NOSTR)
241 		ef = "%";
242 	if (setfile(ef) < 0)
243 		exit(1);		/* error already reported */
244 	if (setjmp(hdrjmp) == 0) {
245 		extern char *version;
246 
247 		if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
248 			signal(SIGINT, hdrstop);
249 		if (value("quiet") == NOSTR)
250 			printf("Mail version %s.  Type ? for help.\n",
251 				version);
252 		announce();
253 		fflush(stdout);
254 		signal(SIGINT, prevint);
255 	}
256 	commands();
257 	signal(SIGHUP, SIG_IGN);
258 	signal(SIGINT, SIG_IGN);
259 	signal(SIGQUIT, SIG_IGN);
260 	quit();
261 	exit(0);
262 }
263 
264 /*
265  * Interrupt printing of the headers.
266  */
267 void
268 hdrstop(signo)
269 	int signo;
270 {
271 
272 	fflush(stdout);
273 	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()
287 {
288 	struct termios tbuf;
289 	struct winsize ws;
290 	speed_t ospeed;
291 
292 	if (ioctl(1, TIOCGWINSZ, (char *) &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