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