nail.h: struct message.m_spamscore: ui_it -> ui32_t
[s-mailx.git] / main.c
blob216f688d8b9df6d7800b7da46f2cba877acf846d
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Startup -- interface with user.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
41 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
42 * correct several buffer overruns (at least one ot them was exploitable).
43 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
44 * ---
45 * Note: We set egid to realgid ... and only if we need the egid we will
46 * switch back temporary. Nevertheless, I do not like seg faults.
47 * Werner Fink, <werner@suse.de>
50 #ifndef HAVE_AMALGAMATION
51 # define _MAIN_SOURCE
52 # include "nail.h"
53 #endif
55 #include <sys/ioctl.h>
57 #include <fcntl.h>
58 #include <pwd.h>
60 #ifdef HAVE_NL_LANGINFO
61 # include <langinfo.h>
62 #endif
63 #ifdef HAVE_SETLOCALE
64 # include <locale.h>
65 #endif
67 #include "version.h"
69 struct a_arg {
70 struct a_arg *aa_next;
71 char *aa_file;
74 /* (extern, but not with amalgamation, so define here) */
75 VL char const weekday_names[7 + 1][4] = {
76 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
78 VL char const month_names[12 + 1][4] = {
79 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
80 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
82 VL char const uagent[] = UAGENT;
83 VL char const version[] = VERSION;
84 /*VL char const features[]; The "feature string" comes from config.h */
85 VL uc_it const class_char[] = {
86 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
87 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
88 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
89 C_CNTRL, C_BLANK, C_WHITE, C_SPACE, C_SPACE, C_SPACE, C_CNTRL, C_CNTRL,
90 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
91 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
92 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
93 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
94 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
95 C_BLANK, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
96 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
97 C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
98 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
99 C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL,
100 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
101 C_DIGIT, C_DIGIT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
102 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
103 C_PUNCT, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
104 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
105 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
106 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
107 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
108 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
109 C_UPPER, C_UPPER, C_UPPER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
110 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
111 C_PUNCT, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
112 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
113 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
114 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
115 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
116 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
117 C_LOWER, C_LOWER, C_LOWER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_CNTRL
120 /* getopt(3) fallback implementation */
121 #ifdef HAVE_GETOPT
122 # define _oarg optarg
123 # define _oind optind
124 /*# define _oerr opterr*/
125 # define _oopt optopt
126 #else
127 static char *_oarg;
128 static int _oind, /*_oerr,*/ _oopt;
129 #endif
131 /* getopt(3) fallback implementation */
132 #ifdef HAVE_GETOPT
133 # define _getopt getopt
134 #else
135 static int _getopt(int argc, char *const argv[], char const *optstring);
136 #endif
138 /* Perform basic startup initialization */
139 static void _startup(void);
141 /* Grow a char** */
142 static size_t _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt);
144 /* Initialize *tempdir*, *myname*, *homedir* */
145 static void _setup_vars(void);
147 /* We're in an interactive session - compute what the screen size for printing
148 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
149 * We use the following algorithm for the height:
150 * If baud rate < 1200, use 9
151 * If baud rate = 1200, use 14
152 * If baud rate > 1200, use 24 or ws_row
153 * Width is either 80 or ws_col */
154 static void _setscreensize(int is_sighdl);
156 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
157 * the system mailbox, and open up the right stuff */
158 static int _rcv_mode(char const *folder);
160 /* Interrupt printing of the headers */
161 static void _hdrstop(int signo);
163 #ifndef HAVE_GETOPT
164 static int
165 _getopt(int argc, char * const argv[], char const *optstring)
167 static char const *lastp;
168 int rv = -1, colon;
169 char const *curp;
170 NYD_ENTER;
172 if ((colon = (optstring[0] == ':')))
173 ++optstring;
174 if (lastp != NULL) {
175 curp = lastp;
176 lastp = 0;
177 } else {
178 if (_oind >= argc || argv[_oind] == 0 || argv[_oind][0] != '-' ||
179 argv[_oind][1] == '\0')
180 goto jleave;
181 if (argv[_oind][1] == '-' && argv[_oind][2] == '\0') {
182 ++_oind;
183 goto jleave;
185 curp = &argv[_oind][1];
188 _oopt = curp[0];
189 while (optstring[0] != '\0') {
190 if (optstring[0] == ':' || optstring[0] != _oopt) {
191 ++optstring;
192 continue;
194 if (optstring[1] == ':') {
195 if (curp[1] != '\0') {
196 _oarg = UNCONST(&curp[1]);
197 ++_oind;
198 } else {
199 if ((_oind += 2) > argc) {
200 if (!colon /*&& _oerr*/) {
201 fprintf(stderr,
202 tr(79, "%s: option requires an argument -- %c\n"),
203 argv[0], (char)_oopt);
205 rv = (colon ? ':' : '?');
206 goto jleave;
208 _oarg = argv[_oind - 1];
210 } else {
211 if (curp[1] != '\0')
212 lastp = &curp[1];
213 else
214 ++_oind;
215 _oarg = NULL;
217 rv = _oopt;
218 goto jleave;
221 if (!colon /*&& opterr*/)
222 fprintf(stderr, tr(78, "%s: illegal option -- %c\n"), argv[0], _oopt);
223 if (curp[1] != '\0')
224 lastp = &curp[1];
225 else
226 ++_oind;
227 _oarg = 0;
228 rv = '?';
229 jleave:
230 NYD_LEAVE;
231 return rv;
233 #endif /* !HAVE_GETOPT */
235 static void
236 _startup(void)
238 char *cp;
239 NYD_ENTER;
241 /* Absolutely the first thing we do is save our egid and set it to the rgid,
242 * so that we can safely run setgid. We use the sgid (saved set-gid) to
243 * allow ourselves to revert to the egid if we want (temporarily) to become
244 * privileged XXX (s-nail-)*dotlock(-program)* [maybe forked<->Unix IPC?] */
245 effectivegid = getegid();
246 realgid = getgid();
247 if (setgid(realgid) < 0) {
248 perror("setgid");
249 exit(1);
252 image = -1;
253 dflpipe = SIG_DFL;
254 #ifndef HAVE_GETOPT
255 _oind = /*_oerr =*/ 1;
256 #endif
258 if ((cp = strrchr(progname, '/')) != NULL)
259 progname = ++cp;
261 /* Set up a reasonable environment.
262 * Figure out whether we are being run interactively,
263 * start the SIGCHLD catcher, and so forth */
264 safe_signal(SIGCHLD, &sigchild);
265 #ifdef HAVE_DEBUG
266 safe_signal(SIGABRT, &_nyd_oncrash);
267 # ifdef SIGBUS
268 safe_signal(SIGBUS, &_nyd_oncrash);
269 # endif
270 safe_signal(SIGFPE, &_nyd_oncrash);
271 safe_signal(SIGILL, &_nyd_oncrash);
272 safe_signal(SIGSEGV, &_nyd_oncrash);
273 #endif
275 if (isatty(STDIN_FILENO)) /* TODO should be isatty(0) && isatty(2)?? */
276 options |= OPT_TTYIN | OPT_INTERACTIVE;
277 if (isatty(STDOUT_FILENO))
278 options |= OPT_TTYOUT;
279 if (IS_TTY_SESSION())
280 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
282 /* Define defaults for internal variables, based on POSIX 2008/Cor 1-2013 */
283 /* noallnet */
284 /* noappend */
285 ok_bset(asksub, TRU1);
286 /* noaskbcc */
287 /* noaskcc */
288 /* noautoprint */
289 /* nobang */
290 /* nocmd */
291 /* nocrt */
292 /* nodebug */
293 /* nodot */
294 /* ok_vset(escape, ESCAPE *"~"*); TODO non-compliant */
295 /* noflipr */
296 /* nofolder */
297 ok_bset(header, TRU1);
298 /* nohold */
299 /* noignore */
300 /* noignoreeof */
301 /* nokeep */
302 /* nokeepsave */
303 /* nometoo */
304 /* noonehop -- Note: we ignore this one */
305 /* nooutfolder */
306 /* nopage */
307 ok_vset(prompt, "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
308 /* noquiet */
309 /* norecord */
310 ok_bset(save, TRU1);
311 /* nosendwait */
312 /* noshowto */
313 /* nosign */
314 /* noSign */
315 /* ok_vset(toplines, "5"); XXX somewhat hmm */
317 #ifdef HAVE_SETLOCALE
318 setlocale(LC_ALL, "");
319 mb_cur_max = MB_CUR_MAX;
320 # ifdef HAVE_NL_LANGINFO
321 if (ok_vlook(ttycharset) == NULL && (cp = nl_langinfo(CODESET)) != NULL)
322 ok_vset(ttycharset, cp);
323 # endif
325 # ifdef HAVE_C90AMEND1
326 if (mb_cur_max > 1) {
327 wchar_t wc;
328 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
329 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
330 utf8 = 1;
331 /* Reset possibly messed up state; luckily this also gives us an
332 * indication wether the encoding has locking shift state sequences */
333 /* TODO temporary - use option bits! */
334 enc_has_state = mbtowc(&wc, NULL, mb_cur_max);
336 # endif
337 #else
338 mb_cur_max = 1;
339 #endif
341 #ifdef HAVE_CATGETS
342 # ifdef NL_CAT_LOCALE
343 catd = catopen(CATNAME, NL_CAT_LOCALE);
344 # else
345 catd = catopen(CATNAME, 0);
346 # endif
347 #endif
349 #ifdef HAVE_ICONV
350 iconvd = (iconv_t)-1;
351 #endif
352 NYD_LEAVE;
355 static size_t
356 _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt)
358 /* Before spreserve(): use our string pool instead of LibC heap */
359 char const **newcpp;
360 NYD_ENTER;
362 newcpp = salloc(sizeof(char*) * newsize);
364 if (oldcnt > 0)
365 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
366 *cpp = newcpp;
367 NYD_LEAVE;
368 return newsize;
371 static void
372 _setup_vars(void)
374 /* Before spreserve(): use our string pool instead of LibC heap */
375 /* XXX further check paths? */
376 char const *cp;
377 uid_t uid;
378 struct passwd *pwuid, *pw;
379 NYD_ENTER;
381 tempdir = ((cp = getenv("TMPDIR")) != NULL) ? savestr(cp) : TMPDIR_FALLBACK;
383 cp = (myname == NULL) ? getenv("USER") : myname;
384 uid = getuid();
385 if ((pwuid = getpwuid(uid)) == NULL)
386 panic(tr(201, "Cannot associate a name with uid %lu"), (ul_it)uid);
387 if (cp == NULL)
388 myname = pwuid->pw_name;
389 else if ((pw = getpwnam(cp)) == NULL)
390 panic(tr(236, "`%s' is not a user of this system"), cp);
391 else {
392 myname = pw->pw_name;
393 if (pw->pw_uid != uid)
394 options |= OPT_u_FLAG;
396 myname = savestr(myname);
397 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
399 if ((cp = getenv("HOME")) == NULL)
400 cp = "."; /* XXX User and Login objects; Login: pw->pw_dir */
401 homedir = savestr(cp);
402 NYD_LEAVE;
405 static void
406 _setscreensize(int is_sighdl)
408 struct termios tbuf;
409 #ifdef TIOCGWINSZ
410 struct winsize ws;
411 #elif defined TIOCGSIZE
412 struct ttysize ts;
413 #endif
414 NYD_ENTER;
416 scrnheight = realscreenheight = scrnwidth = 0;
418 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
419 * a little bit to be able to honour resizes during our lifetime and
420 * only honour it upon first run; abuse *is_sighdl* as an indicator */
421 if (!is_sighdl) {
422 char *cp;
423 long i;
425 if ((cp = getenv("LINES")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
426 scrnheight = realscreenheight = (int)i;
427 if ((cp = getenv("COLUMNS")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
428 scrnwidth = (int)i;
430 if (scrnwidth != 0 && scrnheight != 0)
431 goto jleave;
434 #ifdef TIOCGWINSZ
435 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
436 ws.ws_col = ws.ws_row = 0;
437 #elif defined TIOCGSIZE
438 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) < 0)
439 ts.ts_lines = ts.ts_cols = 0;
440 #endif
442 if (scrnheight == 0) {
443 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) < 0)
444 ? B9600 : cfgetospeed(&tbuf));
446 if (ospeed < B1200)
447 scrnheight = 9;
448 else if (ospeed == B1200)
449 scrnheight = 14;
450 #ifdef TIOCGWINSZ
451 else if (ws.ws_row != 0)
452 scrnheight = ws.ws_row;
453 #elif defined TIOCGSIZE
454 else if (ts.ts_lines != 0)
455 scrnheight = ts.ts_lines;
456 #endif
457 else
458 scrnheight = 24;
460 #if defined TIOCGWINSZ || defined TIOCGSIZE
461 if (0 ==
462 # ifdef TIOCGWINSZ
463 (realscreenheight = ws.ws_row)
464 # else
465 (realscreenheight = ts.ts_lines)
466 # endif
468 realscreenheight = 24;
469 #endif
472 if (scrnwidth == 0 && 0 ==
473 #ifdef TIOCGWINSZ
474 (scrnwidth = ws.ws_col)
475 #elif defined TIOCGSIZE
476 (scrnwidth = ts.ts_cols)
477 #endif
479 scrnwidth = 80;
481 jleave:
482 #ifdef SIGWINCH
483 if (is_sighdl && IS_TTY_SESSION())
484 tty_signal(SIGWINCH);
485 #endif
486 NYD_LEAVE;
489 static sigjmp_buf __hdrjmp; /* XXX */
491 static int
492 _rcv_mode(char const *folder)
494 char *cp;
495 int i;
496 sighandler_type prevint;
497 NYD_ENTER;
499 if (folder == NULL)
500 folder = "%";
501 else if (*folder == '@') {
502 /* This must be treated specially to make possible invocation like
503 * -A imap -f @mailbox */
504 if ((cp = ok_vlook(folder)) != NULL && which_protocol(cp) == PROTO_IMAP)
505 n_strlcpy(mailname, cp, PATH_MAX);
508 i = setfile(folder, 0);
509 if (i < 0)
510 exit(1); /* error already reported */
511 if (options & OPT_EXISTONLY)
512 exit(i);
514 if (options & OPT_HEADERSONLY) {
515 print_headers(1, msgCount);
516 exit(exit_status);
519 callhook(mailname, 0);
520 if (i > 0 && !ok_blook(emptystart))
521 exit(1);
523 if (sigsetjmp(__hdrjmp, 1) == 0) {
524 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
525 safe_signal(SIGINT, _hdrstop);
526 if (!(options & OPT_N_FLAG)) {
527 if (!ok_blook(quiet))
528 printf(tr(140, "%s version %s. Type ? for help.\n"),
529 (ok_blook(bsdcompat) ? "Mail" : uagent), version);
530 announce(1);
531 fflush(stdout);
533 safe_signal(SIGINT, prevint);
536 /* Enter the command loop */
537 if (options & OPT_INTERACTIVE)
538 tty_init();
539 commands();
540 if (options & OPT_INTERACTIVE)
541 tty_destroy();
543 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
544 safe_signal(SIGHUP, SIG_IGN);
545 safe_signal(SIGINT, SIG_IGN);
546 safe_signal(SIGQUIT, SIG_IGN);
548 save_mbox_for_possible_quitstuff();
549 quit();
550 NYD_LEAVE;
551 return exit_status;
554 static void
555 _hdrstop(int signo)
557 UNUSED(signo);
559 fflush(stdout);
560 fprintf(stderr, tr(141, "\nInterrupt\n"));
561 siglongjmp(__hdrjmp, 1);
565 main(int argc, char *argv[])
567 static char const optstr[] = "A:a:Bb:c:DdEeFfHiNnO:q:Rr:S:s:tu:Vv~#",
568 usagestr[] =
569 "Synopsis:\n"
570 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
571 "[-b bcc-addr] [-c cc-addr]\n"
572 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
573 "[-S var[=value]]\n"
574 "\t [-s subject] to-addr...\n"
575 " %s [-BDdEeHiNnRv~#] [-A acct] "
576 "[-S var[=value]] -f [file]\n"
577 " %s [-BDdEeiNnRv~#] [-A acc] [-S var[=value]] [-u user]\n";
579 struct a_arg *a_head = NULL, *a_curr = /* silence CC */ NULL;
580 struct name *to = NULL, *cc = NULL, *bcc = NULL;
581 struct attachment *attach = NULL;
582 char *cp = NULL, *subject = NULL, *qf = NULL, *Aflag = NULL;
583 char const *okey, **oargs = NULL, *folder = NULL;
584 size_t oargs_size = 0, oargs_count = 0, smopts_size = 0;
585 int i;
586 NYD_ENTER;
589 * Start our lengthy setup
592 starting =
593 var_clear_allow_undefined = TRU1;
595 progname = argv[0];
596 _startup();
598 /* Command line parsing */
599 while ((i = _getopt(argc, argv, optstr)) >= 0) {
600 switch (i) {
601 case 'A':
602 /* Execute an account command later on */
603 Aflag = _oarg;
604 break;
605 case 'a':
606 { struct a_arg *nap = ac_alloc(sizeof(struct a_arg));
607 if (a_head == NULL)
608 a_head = nap;
609 else
610 a_curr->aa_next = nap;
611 nap->aa_next = NULL;
612 nap->aa_file = _oarg;
613 a_curr = nap;
615 options |= OPT_SENDMODE;
616 break;
617 case 'B':
618 /* Make 0/1 line buffered */
619 setvbuf(stdin, NULL, _IOLBF, 0);
620 setvbuf(stdout, NULL, _IOLBF, 0);
621 break;
622 case 'b':
623 /* Get Blind Carbon Copy Recipient list */
624 bcc = cat(bcc, checkaddrs(lextract(_oarg, GBCC | GFULL)));
625 options |= OPT_SENDMODE;
626 break;
627 case 'c':
628 /* Get Carbon Copy Recipient list */
629 cc = cat(cc, checkaddrs(lextract(_oarg, GCC | GFULL)));
630 options |= OPT_SENDMODE;
631 break;
632 case 'D':
633 #ifdef HAVE_IMAP
634 okey = "disconnected";
635 goto joarg;
636 #else
637 break;
638 #endif
639 case 'd':
640 okey = "debug";
641 #ifdef HAVE_DEBUG
642 ok_bset(debug, TRU1);
643 #endif
644 goto joarg;
645 case 'E':
646 okey = "skipemptybody";
647 goto joarg;
648 case 'e':
649 options |= OPT_EXISTONLY;
650 break;
651 case 'F':
652 options |= OPT_F_FLAG | OPT_SENDMODE;
653 break;
654 case 'f':
655 /* User is specifying file to "edit" with Mail, as opposed to reading
656 * system mailbox. If no argument is given, we read his mbox file.
657 * Check for remaining arguments later */
658 folder = "&";
659 break;
660 case 'H':
661 options |= OPT_HEADERSONLY;
662 break;
663 case 'i':
664 /* Ignore interrupts */
665 okey = "ignore";
666 goto joarg;
667 case 'N':
668 /* Avoid initial header printing */
669 okey = "noheader";
670 goto joarg;
671 case 'n':
672 /* Don't source "unspecified system start-up file" */
673 options |= OPT_NOSRC;
674 break;
675 case 'O':
676 /* Additional options to pass-through to MTA */
677 if (smopts_count == (size_t)smopts_size)
678 smopts_size = _grow_cpp(&smopts, smopts_size + 8, smopts_count);
679 smopts[smopts_count++] = skin(_oarg);
680 break;
681 case 'q':
682 /* Quote file TODO drop? -Q with real quote?? what ? */
683 if (*_oarg != '-')
684 qf = _oarg;
685 options |= OPT_SENDMODE;
686 break;
687 case 'R':
688 /* Open folders read-only */
689 options |= OPT_R_FLAG;
690 break;
691 case 'r':
692 /* Set From address. */
693 options |= OPT_r_FLAG;
694 if ((option_r_arg = _oarg)[0] != '\0') {
695 struct name *fa = nalloc(_oarg, GFULL);
697 if (is_addr_invalid(fa, 1) || is_fileorpipe_addr(fa)) {
698 fprintf(stderr, tr(271, "Invalid address argument with -r\n"));
699 goto jusage;
701 option_r_arg = fa->n_name;
702 /* TODO -r options is set in smopts, but may
703 * TODO be overwritten by setting from= in
704 * TODO an interactive session!
705 * TODO Maybe disable setting of from?
706 * TODO Warn user? Update manual!! */
707 okey = savecat("from=", fa->n_fullname);
708 goto joarg;
709 /* ..and fa goes even though it is ready :/ */
711 break;
712 case 'S':
713 /* Set variable. We need to do this twice, since the user surely
714 * wants the setting to take effect immediately, but also doesn't want
715 * it to be overwritten from within resource files */
716 { char *a[2];
717 okey = a[0] = _oarg;
718 a[1] = NULL;
719 set(a);
721 joarg:
722 if (oargs_count == oargs_size)
723 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
724 oargs[oargs_count++] = okey;
725 break;
726 case 's':
727 /* Subject: */
728 subject = _oarg;
729 options |= OPT_SENDMODE;
730 break;
731 case 't':
732 /* Read defined set of headers from mail to be send */
733 options |= OPT_SENDMODE | OPT_t_FLAG;
734 break;
735 case 'u':
736 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
737 * done as necessary in _setup_vars() above */
738 myname = _oarg;
739 break;
740 case 'V':
741 puts(version);
742 exit(0);
743 /* NOTREACHED */
744 case 'v':
745 /* Be verbose */
746 okey = "verbose";
747 #ifdef HAVE_DEBUG
748 ok_bset(verbose, TRU1);
749 #endif
750 goto joarg;
751 case '~':
752 /* Enable tilde escapes even in non-interactive mode */
753 options |= OPT_TILDE_FLAG;
754 break;
755 case '#':
756 /* Work in batch mode, even if non-interactive */
757 if (oargs_count + 6 >= oargs_size)
758 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
759 oargs[oargs_count + 0] = "dot";
760 oargs[oargs_count + 1] = "emptystart";
761 oargs[oargs_count + 2] = "noheader";
762 oargs[oargs_count + 3] = "quiet";
763 oargs[oargs_count + 4] = "sendwait";
764 oargs[oargs_count + 5] = "MBOX=/dev/null";
765 oargs_count += 6;
766 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
767 break;
768 case '?':
769 jusage:
770 fprintf(stderr, tr(135, usagestr), progname, progname, progname);
771 exit(2);
775 if (folder != NULL) {
776 if (_oind < argc) {
777 if (_oind + 1 < argc) {
778 fprintf(stderr, tr(205, "More than one file given with -f\n"));
779 goto jusage;
781 folder = argv[_oind];
783 } else {
784 for (i = _oind; argv[i]; ++i)
785 to = cat(to, checkaddrs(lextract(argv[i], GTO | GFULL)));
786 if (to != NULL)
787 options |= OPT_SENDMODE;
790 /* Check for inconsistent arguments */
791 if (folder != NULL && to != NULL) {
792 fprintf(stderr, tr(137, "Cannot give -f and people to send to.\n"));
793 goto jusage;
795 if ((options & (OPT_SENDMODE | OPT_t_FLAG)) == OPT_SENDMODE && to == NULL) {
796 fprintf(stderr, tr(138,
797 "Send options without primary recipient specified.\n"));
798 goto jusage;
800 if ((options & OPT_R_FLAG) && to != NULL) {
801 fprintf(stderr, tr(235, "The -R option is meaningless in send mode.\n"));
802 goto jusage;
806 * Likely to go, perform more setup
809 _setup_vars();
811 if (options & OPT_INTERACTIVE) {
812 _setscreensize(0);
813 #ifdef SIGWINCH
814 # ifndef TTY_WANTS_SIGWINCH
815 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
816 # endif
817 safe_signal(SIGWINCH, _setscreensize);
818 #endif
819 } else
820 scrnheight = realscreenheight = 24, scrnwidth = 80;
822 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
823 spreserve();
825 if (!(options & OPT_NOSRC) && getenv("NAIL_NO_SYSTEM_RC") == NULL)
826 load(SYSCONFRC);
827 /* *expand() returns a savestr(), but load only uses the file name for
828 * fopen(), so it's safe to do this */
829 if ((cp = getenv("MAILRC")) == NULL && (cp = getenv("NAILRC")) == NULL)
830 cp = UNCONST(MAILRC);
831 load(file_expand(cp));
832 if (getenv("NAIL_EXTRA_RC") == NULL &&
833 (cp = ok_vlook(NAIL_EXTRA_RC)) != NULL)
834 load(file_expand(cp));
836 /* Now we can set the account */
837 if (Aflag != NULL) {
838 char *a[2];
839 a[0] = Aflag;
840 a[1] = NULL;
841 c_account(a);
844 /* Ensure the -S and other command line options take precedence over
845 * anything that may have been placed in resource files */
846 for (i = 0; UICMP(z, i, <, oargs_count); ++i) {
847 char const *a[2];
848 a[0] = oargs[i];
849 a[1] = NULL;
850 set(a);
854 * We're finally completely setup and ready to go
857 starting =
858 var_clear_allow_undefined = FAL0;
860 if (options & OPT_DEBUG)
861 fprintf(stderr, tr(199, "user = %s, homedir = %s\n"), myname, homedir);
863 if (!(options & OPT_SENDMODE)) {
864 exit_status = _rcv_mode(folder);
865 goto jleave;
868 /* Now that full mailx(1)-style file expansion is possible handle the
869 * attachments which we had delayed due to this.
870 * This may use savestr(), but since we won't enter the command loop we
871 * don't need to care about that */
872 while (a_head != NULL) {
873 attach = add_attachment(attach, a_head->aa_file, NULL);
874 if (attach != NULL) {
875 a_curr = a_head;
876 a_head = a_head->aa_next;
877 ac_free(a_curr);
878 } else {
879 perror(a_head->aa_file);
880 exit(1);
884 /* xxx exit_status = EXIT_OK; */
885 if (options & OPT_INTERACTIVE)
886 tty_init();
887 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
888 if (options & OPT_INTERACTIVE)
889 tty_destroy();
890 jleave:
891 NYD_LEAVE;
892 return exit_status;
895 /* vim:set fenc=utf-8:s-it-mode */