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