main.c revision 1.25 1 1.23 christos /* $NetBSD: main.c,v 1.25 2006/11/28 18:45:32 christos Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.3 deraadt * Copyright (c) 1980, 1993
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.18 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.8 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.8 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
35 1.8 lukem The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.5 christos #if 0
40 1.6 tls static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95";
41 1.5 christos #else
42 1.23 christos __RCSID("$NetBSD: main.c,v 1.25 2006/11/28 18:45:32 christos Exp $");
43 1.5 christos #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.9 wsanchez #define EXTERN
47 1.1 cgd #include "rcv.h"
48 1.9 wsanchez #undef EXTERN
49 1.24 christos #include <assert.h>
50 1.23 christos #include <util.h>
51 1.9 wsanchez
52 1.3 deraadt #include "extern.h"
53 1.1 cgd
54 1.23 christos #ifdef USE_EDITLINE
55 1.22 christos #include "complete.h"
56 1.22 christos #endif
57 1.24 christos #include "format.h"
58 1.23 christos #ifdef MIME_SUPPORT
59 1.23 christos #include "mime.h"
60 1.23 christos #endif
61 1.25 christos #ifdef THREAD_SUPPORT
62 1.25 christos #include "thread.h"
63 1.25 christos #endif
64 1.8 lukem
65 1.1 cgd /*
66 1.1 cgd * Mail -- a mail program
67 1.1 cgd *
68 1.1 cgd * Startup -- interface with user.
69 1.1 cgd */
70 1.1 cgd
71 1.25 christos static jmp_buf hdrjmp;
72 1.25 christos
73 1.25 christos /*
74 1.25 christos * Interrupt printing of the headers.
75 1.25 christos */
76 1.25 christos /*ARGSUSED*/
77 1.25 christos static void
78 1.25 christos hdrstop(int signo __unused)
79 1.25 christos {
80 1.1 cgd
81 1.25 christos (void)fflush(stdout);
82 1.25 christos (void)fprintf(stderr, "\nInterrupt\n");
83 1.25 christos longjmp(hdrjmp, 1);
84 1.25 christos }
85 1.25 christos
86 1.25 christos /*
87 1.25 christos * Compute what the screen size for printing headers should be.
88 1.25 christos * We use the following algorithm for the height:
89 1.25 christos * If baud rate < 1200, use 9
90 1.25 christos * If baud rate = 1200, use 14
91 1.25 christos * If baud rate > 1200, use 24 or ws_row
92 1.25 christos * Width is either 80 or ws_col;
93 1.25 christos */
94 1.25 christos PUBLIC void
95 1.25 christos setscreensize(void)
96 1.25 christos {
97 1.25 christos struct termios tbuf;
98 1.25 christos struct winsize ws;
99 1.25 christos speed_t ospeed;
100 1.25 christos char *cp;
101 1.25 christos
102 1.25 christos if (ioctl(1, TIOCGWINSZ, &ws) < 0)
103 1.25 christos ws.ws_col = ws.ws_row = 0;
104 1.25 christos if (tcgetattr(1, &tbuf) < 0)
105 1.25 christos ospeed = 9600;
106 1.25 christos else
107 1.25 christos ospeed = cfgetospeed(&tbuf);
108 1.25 christos if (ospeed < 1200)
109 1.25 christos screenheight = 9;
110 1.25 christos else if (ospeed == 1200)
111 1.25 christos screenheight = 14;
112 1.25 christos else if (ws.ws_row != 0)
113 1.25 christos screenheight = ws.ws_row;
114 1.25 christos else
115 1.25 christos screenheight = 24;
116 1.25 christos if ((realscreenheight = ws.ws_row) == 0)
117 1.25 christos realscreenheight = 24;
118 1.25 christos if ((screenwidth = ws.ws_col) == 0)
119 1.25 christos screenwidth = 80;
120 1.25 christos /*
121 1.25 christos * Possible overrides from the rcfile.
122 1.25 christos */
123 1.25 christos if ((cp = value(ENAME_SCREENWIDTH)) != NULL) {
124 1.25 christos int width;
125 1.25 christos width = *cp ? atoi(cp) : 0;
126 1.25 christos if (width >= 0)
127 1.25 christos screenwidth = width;
128 1.25 christos }
129 1.25 christos if ((cp = value(ENAME_SCREENHEIGHT)) != NULL) {
130 1.25 christos int height;
131 1.25 christos height = *cp ? atoi(cp) : 0;
132 1.25 christos if (height >= 0) {
133 1.25 christos realscreenheight = height;
134 1.25 christos screenheight = height;
135 1.25 christos }
136 1.25 christos }
137 1.25 christos }
138 1.22 christos
139 1.22 christos /*
140 1.22 christos * Break up a white-space or comma delimited name list so that aliases
141 1.22 christos * can get expanded. Without this, the CC: or BCC: list is broken too
142 1.22 christos * late for alias expansion to occur.
143 1.22 christos */
144 1.25 christos PUBLIC struct name *
145 1.22 christos lexpand(char *str, int ntype)
146 1.22 christos {
147 1.22 christos char *list;
148 1.22 christos struct name *np = NULL;
149 1.22 christos char *word, *p;
150 1.22 christos
151 1.23 christos list = estrdup(str);
152 1.22 christos word = list;
153 1.23 christos for (word = list; *word; word = p) {
154 1.23 christos while (isblank((unsigned char)*word))
155 1.22 christos continue;
156 1.22 christos for (p = word;
157 1.23 christos *p && !isblank((unsigned char)*p) && *p != ',';
158 1.23 christos p++)
159 1.22 christos continue;
160 1.22 christos if (*p)
161 1.22 christos *p++ = '\0';
162 1.22 christos np = cat(np, nalloc(word, ntype));
163 1.22 christos }
164 1.22 christos
165 1.22 christos free(list);
166 1.22 christos return np;
167 1.22 christos }
168 1.22 christos
169 1.25 christos PUBLIC int
170 1.13 wiz main(int argc, char *argv[])
171 1.1 cgd {
172 1.8 lukem int i;
173 1.1 cgd struct name *to, *cc, *bcc, *smopts;
174 1.23 christos #ifdef MIME_SUPPORT
175 1.24 christos struct name *attach_optargs;
176 1.24 christos struct name *attach_end;
177 1.23 christos #endif
178 1.1 cgd char *subject;
179 1.19 christos const char *ef;
180 1.1 cgd char nosrc = 0;
181 1.1 cgd sig_t prevint;
182 1.19 christos const char *rc;
183 1.24 christos int volatile Hflag;
184 1.1 cgd
185 1.1 cgd /*
186 1.1 cgd * Set up a reasonable environment.
187 1.1 cgd * Figure out whether we are being run interactively,
188 1.1 cgd * start the SIGCHLD catcher, and so forth.
189 1.1 cgd */
190 1.16 wiz (void)signal(SIGCHLD, sigchild);
191 1.1 cgd if (isatty(0))
192 1.25 christos assign(ENAME_INTERACTIVE, "");
193 1.1 cgd image = -1;
194 1.1 cgd /*
195 1.1 cgd * Now, determine how we are being used.
196 1.1 cgd * We successively pick off - flags.
197 1.1 cgd * If there is anything left, it is the base of the list
198 1.1 cgd * of users to mail to. Argp will be set to point to the
199 1.1 cgd * first of these users.
200 1.1 cgd */
201 1.14 wiz ef = NULL;
202 1.15 wiz to = NULL;
203 1.15 wiz cc = NULL;
204 1.15 wiz bcc = NULL;
205 1.15 wiz smopts = NULL;
206 1.14 wiz subject = NULL;
207 1.24 christos Hflag = 0;
208 1.23 christos #ifdef MIME_SUPPORT
209 1.24 christos attach_optargs = NULL;
210 1.24 christos attach_end = NULL;
211 1.24 christos while ((i = getopt(argc, argv, ":~EH:INT:a:b:c:dfins:u:v")) != -1)
212 1.23 christos #else
213 1.24 christos while ((i = getopt(argc, argv, ":~EH:INT:b:c:dfins:u:v")) != -1)
214 1.23 christos #endif
215 1.22 christos {
216 1.1 cgd switch (i) {
217 1.1 cgd case 'T':
218 1.1 cgd /*
219 1.1 cgd * Next argument is temp file to write which
220 1.1 cgd * articles have been read/deleted for netnews.
221 1.1 cgd */
222 1.1 cgd Tflag = optarg;
223 1.1 cgd if ((i = creat(Tflag, 0600)) < 0) {
224 1.17 wiz warn("%s", Tflag);
225 1.1 cgd exit(1);
226 1.1 cgd }
227 1.20 christos (void)close(i);
228 1.1 cgd break;
229 1.23 christos #ifdef MIME_SUPPORT
230 1.24 christos case 'a': {
231 1.24 christos struct name *np;
232 1.24 christos np = nalloc(optarg, 0);
233 1.24 christos if (attach_end == NULL)
234 1.24 christos attach_optargs = np;
235 1.24 christos else {
236 1.24 christos np->n_blink = attach_end;
237 1.24 christos attach_end->n_flink = np;
238 1.24 christos }
239 1.24 christos attach_end = np;
240 1.23 christos break;
241 1.24 christos }
242 1.23 christos #endif
243 1.1 cgd case 'u':
244 1.1 cgd /*
245 1.1 cgd * Next argument is person to pretend to be.
246 1.1 cgd */
247 1.1 cgd myname = optarg;
248 1.20 christos (void)unsetenv("MAIL");
249 1.1 cgd break;
250 1.1 cgd case 'i':
251 1.1 cgd /*
252 1.1 cgd * User wants to ignore interrupts.
253 1.1 cgd * Set the variable "ignore"
254 1.1 cgd */
255 1.25 christos assign(ENAME_IGNORE, "");
256 1.1 cgd break;
257 1.1 cgd case 'd':
258 1.1 cgd debug++;
259 1.1 cgd break;
260 1.1 cgd case 's':
261 1.1 cgd /*
262 1.1 cgd * Give a subject field for sending from
263 1.1 cgd * non terminal
264 1.1 cgd */
265 1.1 cgd subject = optarg;
266 1.1 cgd break;
267 1.1 cgd case 'f':
268 1.1 cgd /*
269 1.1 cgd * User is specifying file to "edit" with Mail,
270 1.1 cgd * as opposed to reading system mailbox.
271 1.1 cgd * If no argument is given after -f, we read his
272 1.1 cgd * mbox file.
273 1.1 cgd *
274 1.1 cgd * getopt() can't handle optional arguments, so here
275 1.1 cgd * is an ugly hack to get around it.
276 1.1 cgd */
277 1.1 cgd if ((argv[optind]) && (argv[optind][0] != '-'))
278 1.1 cgd ef = argv[optind++];
279 1.1 cgd else
280 1.1 cgd ef = "&";
281 1.1 cgd break;
282 1.24 christos case 'H':
283 1.24 christos /*
284 1.24 christos * Print out the headers and quit.
285 1.24 christos */
286 1.24 christos Hflag = get_Hflag(argv);
287 1.24 christos break;
288 1.1 cgd case 'n':
289 1.1 cgd /*
290 1.1 cgd * User doesn't want to source /usr/lib/Mail.rc
291 1.1 cgd */
292 1.1 cgd nosrc++;
293 1.1 cgd break;
294 1.1 cgd case 'N':
295 1.1 cgd /*
296 1.1 cgd * Avoid initial header printing.
297 1.1 cgd */
298 1.25 christos assign(ENAME_NOHEADER, "");
299 1.1 cgd break;
300 1.1 cgd case 'v':
301 1.1 cgd /*
302 1.1 cgd * Send mailer verbose flag
303 1.1 cgd */
304 1.25 christos assign(ENAME_VERBOSE, "");
305 1.1 cgd break;
306 1.1 cgd case 'I':
307 1.11 christos case '~':
308 1.1 cgd /*
309 1.1 cgd * We're interactive
310 1.1 cgd */
311 1.25 christos assign(ENAME_INTERACTIVE, "");
312 1.1 cgd break;
313 1.1 cgd case 'c':
314 1.1 cgd /*
315 1.1 cgd * Get Carbon Copy Recipient list
316 1.1 cgd */
317 1.22 christos cc = cat(cc, lexpand(optarg, GCC));
318 1.1 cgd break;
319 1.1 cgd case 'b':
320 1.1 cgd /*
321 1.1 cgd * Get Blind Carbon Copy Recipient list
322 1.1 cgd */
323 1.22 christos bcc = cat(bcc, lexpand(optarg, GBCC));
324 1.22 christos
325 1.22 christos break;
326 1.11 christos case 'E':
327 1.11 christos /*
328 1.11 christos * Don't send empty files.
329 1.11 christos */
330 1.25 christos assign(ENAME_DONTSENDEMPTY, "");
331 1.11 christos break;
332 1.24 christos case ':':
333 1.24 christos /*
334 1.24 christos * An optarg was expected but not found.
335 1.24 christos */
336 1.24 christos if (optopt == 'H') {
337 1.24 christos Hflag = get_Hflag(NULL);
338 1.24 christos break;
339 1.24 christos }
340 1.24 christos (void)fprintf(stderr,
341 1.24 christos "%s: option requires an argument -- %c\n",
342 1.24 christos getprogname(), optopt);
343 1.24 christos
344 1.24 christos /* FALLTHROUGH */
345 1.1 cgd case '?':
346 1.24 christos /*
347 1.24 christos * An unknown option flag. We need to do the
348 1.24 christos * error message.
349 1.24 christos */
350 1.24 christos if (optopt != '?')
351 1.24 christos (void)fprintf(stderr,
352 1.24 christos "%s: unknown option -- %c\n", getprogname(),
353 1.24 christos optopt);
354 1.23 christos #ifdef MIME_SUPPORT
355 1.23 christos (void)fputs("\
356 1.23 christos Usage: mail [-EiInv] [-s subject] [-a file] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
357 1.23 christos [- sendmail-options ...]\n\
358 1.25 christos mail [-EiInNv] [-H[colon-modifier]] -f [name]\n\
359 1.25 christos mail [-EiInNv] [-H[colon-modifier]] [-u user]\n",
360 1.23 christos stderr);
361 1.23 christos #else /* MIME_SUPPORT */
362 1.20 christos (void)fputs("\
363 1.11 christos Usage: mail [-EiInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
364 1.1 cgd [- sendmail-options ...]\n\
365 1.25 christos mail [-EiInNv] [-H[colon-modifier]] -f [name]\n\
366 1.25 christos mail [-EiInNv] [-H[colon-modifier]] [-u user]\n",
367 1.1 cgd stderr);
368 1.23 christos #endif /* MIME_SUPPORT */
369 1.23 christos
370 1.1 cgd exit(1);
371 1.1 cgd }
372 1.1 cgd }
373 1.1 cgd for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
374 1.1 cgd to = cat(to, nalloc(argv[i], GTO));
375 1.1 cgd for (; argv[i]; i++)
376 1.22 christos smopts = cat(smopts, nalloc(argv[i], GSMOPTS));
377 1.1 cgd /*
378 1.1 cgd * Check for inconsistent arguments.
379 1.1 cgd */
380 1.21 christos if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
381 1.21 christos errx(1, "You must specify direct recipients with -s, -c, or -b.");
382 1.15 wiz if (ef != NULL && to != NULL) {
383 1.21 christos errx(1, "Cannot give -f and people to send to.");
384 1.1 cgd }
385 1.24 christos if (Hflag != 0 && to != NULL)
386 1.24 christos errx(EXIT_FAILURE, "Cannot give -H and people to send to.");
387 1.23 christos #ifdef MIME_SUPPORT
388 1.24 christos if (attach_optargs != NULL && to == NULL)
389 1.24 christos errx(EXIT_FAILURE, "Cannot give -a without people to send to.");
390 1.23 christos #endif
391 1.24 christos tinit(); /* must be done before loading the rcfile */
392 1.1 cgd input = stdin;
393 1.25 christos mailmode = Hflag ? mm_hdrsonly :
394 1.25 christos to ? mm_sending : mm_receiving;
395 1.25 christos
396 1.1 cgd spreserve();
397 1.1 cgd if (!nosrc)
398 1.1 cgd load(_PATH_MASTER_RC);
399 1.1 cgd /*
400 1.1 cgd * Expand returns a savestr, but load only uses the file name
401 1.1 cgd * for fopen, so it's safe to do this.
402 1.1 cgd */
403 1.6 tls if ((rc = getenv("MAILRC")) == 0)
404 1.6 tls rc = "~/.mailrc";
405 1.6 tls load(expand(rc));
406 1.25 christos setscreensize(); /* do this after loading the rcfile */
407 1.22 christos
408 1.23 christos #ifdef USE_EDITLINE
409 1.22 christos /* this is after loading the MAILRC so we can use value() */
410 1.23 christos init_editline();
411 1.22 christos #endif
412 1.22 christos
413 1.25 christos switch (mailmode) {
414 1.25 christos case mm_sending:
415 1.25 christos (void)mail(to, cc, bcc, smopts, subject,
416 1.25 christos mime_attach_optargs(attach_optargs));
417 1.1 cgd /*
418 1.1 cgd * why wait?
419 1.1 cgd */
420 1.1 cgd exit(senderr);
421 1.25 christos break; /* XXX - keep lint happy */
422 1.25 christos
423 1.25 christos case mm_receiving:
424 1.25 christos case mm_hdrsonly:
425 1.25 christos /*
426 1.25 christos * Ok, we are reading mail.
427 1.25 christos * Decide whether we are editing a mailbox or reading
428 1.25 christos * the system mailbox, and open up the right stuff.
429 1.25 christos */
430 1.25 christos if (ef == NULL)
431 1.25 christos ef = "%";
432 1.25 christos if (setfile(ef) < 0)
433 1.25 christos exit(1); /* error already reported */
434 1.25 christos if (setjmp(hdrjmp) == 0) {
435 1.25 christos if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
436 1.25 christos (void)signal(SIGINT, hdrstop);
437 1.25 christos if (value(ENAME_QUIET) == NULL)
438 1.25 christos (void)printf("Mail version %s. Type ? for help.\n",
439 1.25 christos version);
440 1.25 christos if (mailmode == mm_hdrsonly)
441 1.25 christos show_headers_and_exit(Hflag); /* NORETURN */
442 1.25 christos announce();
443 1.25 christos (void)fflush(stdout);
444 1.25 christos (void)signal(SIGINT, prevint);
445 1.25 christos }
446 1.25 christos commands();
447 1.25 christos (void)signal(SIGHUP, SIG_IGN);
448 1.25 christos (void)signal(SIGINT, SIG_IGN);
449 1.25 christos (void)signal(SIGQUIT, SIG_IGN);
450 1.25 christos quit();
451 1.25 christos break;
452 1.25 christos
453 1.25 christos default:
454 1.25 christos assert(/*CONSTCOND*/0);
455 1.25 christos break;
456 1.1 cgd }
457 1.25 christos
458 1.20 christos return 0;
459 1.1 cgd }
460