1 /* $OpenBSD: main.c,v 1.19 2004/04/19 10:17:18 jmc Exp $ */
2 /* $NetBSD: main.c,v 1.7 1997/05/13 06:15:57 mikel Exp $ */
3
4 /*
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static const char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95";
42 #else
43 static const char rcsid[] = "$OpenBSD: main.c,v 1.19 2004/04/19 10:17:18 jmc Exp $";
44 #endif
45 #endif /* not lint */
46
47 #include "rcv.h"
48 #include <fcntl.h>
49 #include <sys/ioctl.h>
50 #include "extern.h"
51
52 __dead void usage(void);
53 int main(int, char **);
54
55 /*
56 * Mail -- a mail program
57 *
58 * Startup -- interface with user.
59 */
60
61 int
main(int argc,char ** argv)62 main(int argc, char **argv)
63 {
64 int i;
65 struct name *to, *cc, *bcc, *smopts;
66 char *subject;
67 char *ef;
68 char nosrc = 0;
69 char *rc;
70 extern const char version[];
71
72 /*
73 * Set up a reasonable environment.
74 * Figure out whether we are being run interactively,
75 * start the SIGCHLD catcher, and so forth.
76 */
77 (void)signal(SIGCHLD, sigchild);
78 (void)signal(SIGPIPE, SIG_IGN);
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, "INT: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 err(1, "%s", Tflag);
105 (void)close(i);
106 break;
107 case 'u':
108 /*
109 * Next argument is person to pretend to be.
110 */
111 if (strlen(optarg) >= MAXLOGNAME)
112 errx(1, "username `%s' too long", optarg);
113 unsetenv("MAIL");
114 myname = optarg;
115 uflag = 1;
116 break;
117 case 'i':
118 /*
119 * User wants to ignore interrupts.
120 * Set the variable "ignore"
121 */
122 assign("ignore", "");
123 break;
124 case 'd':
125 debug++;
126 break;
127 case 's':
128 /*
129 * Give a subject field for sending from
130 * non terminal
131 */
132 subject = optarg;
133 break;
134 case 'f':
135 /*
136 * User is specifying file to "edit" with Mail,
137 * as opposed to reading system mailbox.
138 * If no argument is given after -f, we read his
139 * mbox file.
140 *
141 * getopt() can't handle optional arguments, so here
142 * is an ugly hack to get around it.
143 */
144 if ((argv[optind]) && (argv[optind][0] != '-'))
145 ef = argv[optind++];
146 else
147 ef = "&";
148 break;
149 case 'n':
150 /*
151 * User doesn't want to source /usr/lib/Mail.rc
152 */
153 nosrc++;
154 break;
155 case 'N':
156 /*
157 * Avoid initial header printing.
158 */
159 assign("noheader", "");
160 break;
161 case 'v':
162 /*
163 * Send mailer verbose flag
164 */
165 assign("verbose", "");
166 break;
167 case 'I':
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 default:
186 usage();
187 /*NOTREACHED*/
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 == NULL && (subject != NULL || cc != NULL || bcc != NULL))
198 errx(1, "You must specify direct recipients with -s, -c, or -b");
199 if (ef != NULL && to != NULL)
200 errx(1, "Cannot give -f and people to send to");
201 /*
202 * Block SIGINT except where we install an explicit handler for it.
203 */
204 sigemptyset(&intset);
205 sigaddset(&intset, SIGINT);
206 (void)sigprocmask(SIG_BLOCK, &intset, NULL);
207 /*
208 * Initialization.
209 */
210 tinit();
211 setscreensize();
212 input = stdin;
213 rcvmode = !to;
214 spreserve();
215 if (!nosrc)
216 load(_PATH_MASTER_RC);
217 /*
218 * Expand returns a savestr, but load only uses the file name
219 * for fopen, so it's safe to do this.
220 */
221 if ((rc = getenv("MAILRC")) == 0)
222 rc = "~/.mailrc";
223 load(expand(rc));
224 if (!rcvmode) {
225 mail(to, cc, bcc, smopts, subject);
226 /*
227 * why wait?
228 */
229 exit(senderr);
230 }
231 /*
232 * Ok, we are reading mail.
233 * Decide whether we are editing a mailbox or reading
234 * the system mailbox, and open up the right stuff.
235 */
236 if (ef == NULL)
237 ef = "%";
238 if (setfile(ef) < 0)
239 exit(1); /* error already reported */
240
241 if (value("quiet") == NULL)
242 (void)printf("Mail version %s. Type ? for help.\n",
243 version);
244 announce();
245 (void)fflush(stdout);
246 commands();
247 (void)ignoresig(SIGHUP, NULL, NULL);
248 (void)ignoresig(SIGINT, NULL, NULL);
249 (void)ignoresig(SIGQUIT, NULL, NULL);
250 quit();
251 exit(0);
252 }
253
254 /*
255 * Compute what the screen size for printing headers should be.
256 * We use the following algorithm for the height:
257 * If baud rate < 1200, use 9
258 * If baud rate = 1200, use 14
259 * If baud rate > 1200, use 24 or ws_row
260 * Width is either 80 or ws_col;
261 */
262 void
setscreensize(void)263 setscreensize(void)
264 {
265 struct termios tbuf;
266 struct winsize ws;
267 speed_t ospeed;
268
269 if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
270 ws.ws_col = ws.ws_row = 0;
271 if (tcgetattr(1, &tbuf) < 0)
272 ospeed = 9600;
273 else
274 ospeed = cfgetospeed(&tbuf);
275 if (ospeed < B1200)
276 screenheight = 9;
277 else if (ospeed == B1200)
278 screenheight = 14;
279 else if (ws.ws_row != 0)
280 screenheight = ws.ws_row;
281 else
282 screenheight = 24;
283 if ((realscreenheight = ws.ws_row) == 0)
284 realscreenheight = 24;
285 if ((screenwidth = ws.ws_col) == 0)
286 screenwidth = 80;
287 }
288
289 __dead void
usage(void)290 usage(void)
291 {
292
293 fprintf(stderr, "usage: %s [-Iinv] [-b list] [-c list] "
294 "[-s subject] to-addr [...]\n", __progname);
295 fprintf(stderr, " %*s [-sendmail-options [...]]\n",
296 (int)strlen(__progname), "");
297 fprintf(stderr, " %s [-IiNnv] -f [name]\n", __progname);
298 fprintf(stderr, " %s [-IiNnv] [-u user]\n", __progname);
299 exit(1);
300 }
301