Review: dotlock.c
[s-mailx.git] / main.c
blobff9d5b2575b4e197cfeabfeba0f478e8a5e50e9a
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, char const *Larg);
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;
169 int rv = -1, colon;
170 char const *curp;
171 NYD_ENTER;
173 if ((colon = (optstring[0] == ':')))
174 ++optstring;
175 if (lastp != NULL) {
176 curp = lastp;
177 lastp = 0;
178 } else {
179 if (_oind >= argc || argv[_oind] == 0 || argv[_oind][0] != '-' ||
180 argv[_oind][1] == '\0')
181 goto jleave;
182 if (argv[_oind][1] == '-' && argv[_oind][2] == '\0') {
183 ++_oind;
184 goto jleave;
186 curp = &argv[_oind][1];
189 _oopt = curp[0];
190 while (optstring[0] != '\0') {
191 if (optstring[0] == ':' || optstring[0] != _oopt) {
192 ++optstring;
193 continue;
195 if (optstring[1] == ':') {
196 if (curp[1] != '\0') {
197 _oarg = UNCONST(&curp[1]);
198 ++_oind;
199 } else {
200 if ((_oind += 2) > argc) {
201 if (!colon /*&& _oerr*/) {
202 fprintf(stderr,
203 tr(79, "%s: option requires an argument -- %c\n"),
204 argv[0], (char)_oopt);
206 rv = (colon ? ':' : '?');
207 goto jleave;
209 _oarg = argv[_oind - 1];
211 } else {
212 if (curp[1] != '\0')
213 lastp = &curp[1];
214 else
215 ++_oind;
216 _oarg = NULL;
218 rv = _oopt;
219 goto jleave;
222 if (!colon /*&& opterr*/)
223 fprintf(stderr, tr(78, "%s: illegal option -- %c\n"), argv[0], _oopt);
224 if (curp[1] != '\0')
225 lastp = &curp[1];
226 else
227 ++_oind;
228 _oarg = 0;
229 rv = '?';
230 jleave:
231 NYD_LEAVE;
232 return rv;
234 #endif /* !HAVE_GETOPT */
236 static void
237 _startup(void)
239 char *cp;
240 NYD_ENTER;
242 /* Absolutely the first thing we do is save our egid and set it to the rgid,
243 * so that we can safely run setgid. We use the sgid (saved set-gid) to
244 * allow ourselves to revert to the egid if we want (temporarily) to become
245 * privileged XXX (s-nail-)*dotlock(-program)* [maybe forked<->Unix IPC?] */
246 effectivegid = getegid();
247 realgid = getgid();
248 if (setgid(realgid) < 0) {
249 perror("setgid");
250 exit(1);
253 image = -1;
254 dflpipe = SIG_DFL;
255 #ifndef HAVE_GETOPT
256 _oind = /*_oerr =*/ 1;
257 #endif
259 if ((cp = strrchr(progname, '/')) != NULL)
260 progname = ++cp;
262 /* Set up a reasonable environment.
263 * Figure out whether we are being run interactively,
264 * start the SIGCHLD catcher, and so forth */
265 safe_signal(SIGCHLD, &sigchild);
266 #ifdef HAVE_DEBUG
267 safe_signal(SIGABRT, &_nyd_oncrash);
268 # ifdef SIGBUS
269 safe_signal(SIGBUS, &_nyd_oncrash);
270 # endif
271 safe_signal(SIGFPE, &_nyd_oncrash);
272 safe_signal(SIGILL, &_nyd_oncrash);
273 safe_signal(SIGSEGV, &_nyd_oncrash);
274 #endif
276 if (isatty(STDIN_FILENO)) /* TODO should be isatty(0) && isatty(2)?? */
277 options |= OPT_TTYIN | OPT_INTERACTIVE;
278 if (isatty(STDOUT_FILENO))
279 options |= OPT_TTYOUT;
280 if (IS_TTY_SESSION())
281 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
283 /* Define defaults for internal variables, based on POSIX 2008/Cor 1-2013 */
284 /* noallnet */
285 /* noappend */
286 ok_bset(asksub, TRU1);
287 /* noaskbcc */
288 /* noaskcc */
289 /* noautoprint */
290 /* nobang */
291 /* nocmd */
292 /* nocrt */
293 /* nodebug */
294 /* nodot */
295 /* ok_vset(escape, ESCAPE *"~"*); TODO non-compliant */
296 /* noflipr */
297 /* nofolder */
298 ok_bset(header, TRU1);
299 /* nohold */
300 /* noignore */
301 /* noignoreeof */
302 /* nokeep */
303 /* nokeepsave */
304 /* nometoo */
305 /* noonehop -- Note: we ignore this one */
306 /* nooutfolder */
307 /* nopage */
308 ok_vset(prompt, "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
309 /* noquiet */
310 /* norecord */
311 ok_bset(save, TRU1);
312 /* nosendwait */
313 /* noshowto */
314 /* nosign */
315 /* noSign */
316 /* ok_vset(toplines, "5"); XXX somewhat hmm */
318 #ifdef HAVE_SETLOCALE
319 setlocale(LC_ALL, "");
320 mb_cur_max = MB_CUR_MAX;
321 # ifdef HAVE_NL_LANGINFO
322 if (ok_vlook(ttycharset) == NULL && (cp = nl_langinfo(CODESET)) != NULL)
323 ok_vset(ttycharset, cp);
324 # endif
326 # ifdef HAVE_C90AMEND1
327 if (mb_cur_max > 1) {
328 wchar_t wc;
329 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
330 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
331 utf8 = 1;
332 /* Reset possibly messed up state; luckily this also gives us an
333 * indication wether the encoding has locking shift state sequences */
334 /* TODO temporary - use option bits! */
335 enc_has_state = mbtowc(&wc, NULL, mb_cur_max);
337 # endif
338 #else
339 mb_cur_max = 1;
340 #endif
342 #ifdef HAVE_CATGETS
343 # ifdef NL_CAT_LOCALE
344 catd = catopen(CATNAME, NL_CAT_LOCALE);
345 # else
346 catd = catopen(CATNAME, 0);
347 # endif
348 #endif
350 #ifdef HAVE_ICONV
351 iconvd = (iconv_t)-1;
352 #endif
353 NYD_LEAVE;
356 static size_t
357 _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt)
359 /* Before spreserve(): use our string pool instead of LibC heap */
360 char const **newcpp;
361 NYD_ENTER;
363 newcpp = salloc(sizeof(char*) * newsize);
365 if (oldcnt > 0)
366 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
367 *cpp = newcpp;
368 NYD_LEAVE;
369 return newsize;
372 static void
373 _setup_vars(void)
375 /* Before spreserve(): use our string pool instead of LibC heap */
376 /* XXX further check paths? */
377 char const *cp;
378 uid_t uid;
379 struct passwd *pwuid, *pw;
380 NYD_ENTER;
382 tempdir = ((cp = getenv("TMPDIR")) != NULL) ? savestr(cp) : TMPDIR_FALLBACK;
384 cp = (myname == NULL) ? getenv("USER") : myname;
385 uid = getuid();
386 if ((pwuid = getpwuid(uid)) == NULL)
387 panic(tr(201, "Cannot associate a name with uid %lu"), (ul_it)uid);
388 if (cp == NULL)
389 myname = pwuid->pw_name;
390 else if ((pw = getpwnam(cp)) == NULL)
391 panic(tr(236, "`%s' is not a user of this system"), cp);
392 else {
393 myname = pw->pw_name;
394 if (pw->pw_uid != uid)
395 options |= OPT_u_FLAG;
397 myname = savestr(myname);
398 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
400 if ((cp = getenv("HOME")) == NULL)
401 cp = "."; /* XXX User and Login objects; Login: pw->pw_dir */
402 homedir = savestr(cp);
403 NYD_LEAVE;
406 static void
407 _setscreensize(int is_sighdl)
409 struct termios tbuf;
410 #ifdef TIOCGWINSZ
411 struct winsize ws;
412 #elif defined TIOCGSIZE
413 struct ttysize ts;
414 #endif
415 NYD_ENTER;
417 scrnheight = realscreenheight = scrnwidth = 0;
419 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
420 * a little bit to be able to honour resizes during our lifetime and
421 * only honour it upon first run; abuse *is_sighdl* as an indicator */
422 if (!is_sighdl) {
423 char *cp;
424 long i;
426 if ((cp = getenv("LINES")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
427 scrnheight = realscreenheight = (int)i;
428 if ((cp = getenv("COLUMNS")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
429 scrnwidth = (int)i;
431 if (scrnwidth != 0 && scrnheight != 0)
432 goto jleave;
435 #ifdef TIOCGWINSZ
436 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
437 ws.ws_col = ws.ws_row = 0;
438 #elif defined TIOCGSIZE
439 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) < 0)
440 ts.ts_lines = ts.ts_cols = 0;
441 #endif
443 if (scrnheight == 0) {
444 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) < 0)
445 ? B9600 : cfgetospeed(&tbuf));
447 if (ospeed < B1200)
448 scrnheight = 9;
449 else if (ospeed == B1200)
450 scrnheight = 14;
451 #ifdef TIOCGWINSZ
452 else if (ws.ws_row != 0)
453 scrnheight = ws.ws_row;
454 #elif defined TIOCGSIZE
455 else if (ts.ts_lines != 0)
456 scrnheight = ts.ts_lines;
457 #endif
458 else
459 scrnheight = 24;
461 #if defined TIOCGWINSZ || defined TIOCGSIZE
462 if (0 ==
463 # ifdef TIOCGWINSZ
464 (realscreenheight = ws.ws_row)
465 # else
466 (realscreenheight = ts.ts_lines)
467 # endif
469 realscreenheight = 24;
470 #endif
473 if (scrnwidth == 0 && 0 ==
474 #ifdef TIOCGWINSZ
475 (scrnwidth = ws.ws_col)
476 #elif defined TIOCGSIZE
477 (scrnwidth = ts.ts_cols)
478 #endif
480 scrnwidth = 80;
482 jleave:
483 #ifdef SIGWINCH
484 if (is_sighdl && IS_TTY_SESSION())
485 tty_signal(SIGWINCH);
486 #endif
487 NYD_LEAVE;
490 static sigjmp_buf __hdrjmp; /* XXX */
492 static int
493 _rcv_mode(char const *folder, char const *Larg)
495 char *cp;
496 int i;
497 sighandler_type prevint;
498 NYD_ENTER;
500 if (folder == NULL)
501 folder = "%";
502 else if (*folder == '@') {
503 /* This must be treated specially to make possible invocation like
504 * -A imap -f @mailbox */
505 if ((cp = ok_vlook(folder)) != NULL && which_protocol(cp) == PROTO_IMAP)
506 n_strlcpy(mailname, cp, PATH_MAX);
509 i = setfile(folder, 0);
510 if (i < 0) {
511 exit_status = EXIT_ERR; /* error already reported */
512 goto jleave;
514 if (options & OPT_EXISTONLY) {
515 exit_status = i;
516 goto jleave;
519 if (options & (OPT_HEADERSONLY | OPT_HEADERLIST)) {
520 print_header_summary(Larg);
521 goto jleave;
524 callhook(mailname, 0);
525 if (i > 0 && !ok_blook(emptystart)) {
526 exit_status = EXIT_ERR;
527 goto jleave;
530 if (sigsetjmp(__hdrjmp, 1) == 0) {
531 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
532 safe_signal(SIGINT, _hdrstop);
533 if (!(options & OPT_N_FLAG)) {
534 if (!ok_blook(quiet))
535 printf(tr(140, "%s version %s. Type ? for help.\n"),
536 (ok_blook(bsdcompat) ? "Mail" : uagent), version);
537 announce(1);
538 fflush(stdout);
540 safe_signal(SIGINT, prevint);
543 /* Enter the command loop */
544 if (options & OPT_INTERACTIVE)
545 tty_init();
546 commands();
547 if (options & OPT_INTERACTIVE)
548 tty_destroy();
550 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
551 safe_signal(SIGHUP, SIG_IGN);
552 safe_signal(SIGINT, SIG_IGN);
553 safe_signal(SIGQUIT, SIG_IGN);
555 save_mbox_for_possible_quitstuff();
556 quit();
557 jleave:
558 NYD_LEAVE;
559 return exit_status;
562 static void
563 _hdrstop(int signo)
565 NYD_X; /* Signal handler */
566 UNUSED(signo);
568 fflush(stdout);
569 fprintf(stderr, tr(141, "\nInterrupt\n"));
570 siglongjmp(__hdrjmp, 1);
574 main(int argc, char *argv[])
576 static char const optstr[] = "A:a:Bb:c:DdEeFfHiL:NnO:q:Rr:S:s:tu:Vv~#",
577 usagestr[] =
578 "Synopsis:\n"
579 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
580 "[-b bcc-addr] [-c cc-addr]\n"
581 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
582 "[-S var[=value]]\n"
583 "\t [-s subject] to-addr...\n"
584 " %s [-BDdEeHiNnRv~#] [-A acc] [-L spec-list] [-S var[=value]] "
585 "-f [file]\n"
586 " %s [-BDdEeHiNnRv~#] [-A acc] [-L spec-list] [-S var[=value]] "
587 "[-u user]\n";
589 struct a_arg *a_head = NULL, *a_curr = /* silence CC */ NULL;
590 struct name *to = NULL, *cc = NULL, *bcc = NULL;
591 struct attachment *attach = NULL;
592 char *cp = NULL, *subject = NULL, *qf = NULL, *Aarg = NULL, *Larg = NULL;
593 char const *okey, **oargs = NULL, *folder = NULL;
594 size_t oargs_size = 0, oargs_count = 0, smopts_size = 0;
595 int i;
596 NYD_ENTER;
599 * Start our lengthy setup
602 starting =
603 var_clear_allow_undefined = TRU1;
605 progname = argv[0];
606 _startup();
608 /* Command line parsing */
609 while ((i = _getopt(argc, argv, optstr)) >= 0) {
610 switch (i) {
611 case 'A':
612 /* Execute an account command later on */
613 Aarg = _oarg;
614 break;
615 case 'a':
616 { struct a_arg *nap = ac_alloc(sizeof(struct a_arg));
617 if (a_head == NULL)
618 a_head = nap;
619 else
620 a_curr->aa_next = nap;
621 nap->aa_next = NULL;
622 nap->aa_file = _oarg;
623 a_curr = nap;
625 options |= OPT_SENDMODE;
626 break;
627 case 'B':
628 /* Make 0/1 line buffered */
629 setvbuf(stdin, NULL, _IOLBF, 0);
630 setvbuf(stdout, NULL, _IOLBF, 0);
631 break;
632 case 'b':
633 /* Get Blind Carbon Copy Recipient list */
634 bcc = cat(bcc, checkaddrs(lextract(_oarg, GBCC | GFULL)));
635 options |= OPT_SENDMODE;
636 break;
637 case 'c':
638 /* Get Carbon Copy Recipient list */
639 cc = cat(cc, checkaddrs(lextract(_oarg, GCC | GFULL)));
640 options |= OPT_SENDMODE;
641 break;
642 case 'D':
643 #ifdef HAVE_IMAP
644 okey = "disconnected";
645 goto joarg;
646 #else
647 break;
648 #endif
649 case 'd':
650 okey = "debug";
651 #ifdef HAVE_DEBUG
652 ok_bset(debug, TRU1);
653 #endif
654 goto joarg;
655 case 'E':
656 okey = "skipemptybody";
657 goto joarg;
658 case 'e':
659 options |= OPT_EXISTONLY;
660 break;
661 case 'F':
662 options |= OPT_F_FLAG | OPT_SENDMODE;
663 break;
664 case 'f':
665 /* User is specifying file to "edit" with Mail, as opposed to reading
666 * system mailbox. If no argument is given, we read his mbox file.
667 * Check for remaining arguments later */
668 folder = "&";
669 break;
670 case 'H':
671 options |= OPT_HEADERSONLY;
672 break;
673 case 'i':
674 /* Ignore interrupts */
675 okey = "ignore";
676 goto joarg;
677 case 'L':
678 Larg = optarg;
679 if (*Larg == '"' || *Larg == '\'') { /* TODO list.c:listspec_check() */
680 size_t j = strlen(++Larg);
681 if (j > 0)
682 Larg[j - 1] = '\0';
684 options |= OPT_HEADERLIST;
685 break;
686 case 'N':
687 /* Avoid initial header printing */
688 okey = "noheader";
689 goto joarg;
690 case 'n':
691 /* Don't source "unspecified system start-up file" */
692 options |= OPT_NOSRC;
693 break;
694 case 'O':
695 /* Additional options to pass-through to MTA */
696 if (smopts_count == (size_t)smopts_size)
697 smopts_size = _grow_cpp(&smopts, smopts_size + 8, smopts_count);
698 smopts[smopts_count++] = skin(_oarg);
699 break;
700 case 'q':
701 /* Quote file TODO drop? -Q with real quote?? what ? */
702 if (*_oarg != '-')
703 qf = _oarg;
704 options |= OPT_SENDMODE;
705 break;
706 case 'R':
707 /* Open folders read-only */
708 options |= OPT_R_FLAG;
709 break;
710 case 'r':
711 /* Set From address. */
712 options |= OPT_r_FLAG;
713 if ((option_r_arg = _oarg)[0] != '\0') {
714 struct name *fa = nalloc(_oarg, GFULL);
716 if (is_addr_invalid(fa, 1) || is_fileorpipe_addr(fa)) {
717 fprintf(stderr, tr(271, "Invalid address argument with -r\n"));
718 goto jusage;
720 option_r_arg = fa->n_name;
721 /* TODO -r options is set in smopts, but may
722 * TODO be overwritten by setting from= in
723 * TODO an interactive session!
724 * TODO Maybe disable setting of from?
725 * TODO Warn user? Update manual!! */
726 okey = savecat("from=", fa->n_fullname);
727 goto joarg;
728 /* ..and fa goes even though it is ready :/ */
730 break;
731 case 'S':
732 /* Set variable. We need to do this twice, since the user surely
733 * wants the setting to take effect immediately, but also doesn't want
734 * it to be overwritten from within resource files */
735 { char *a[2];
736 okey = a[0] = _oarg;
737 a[1] = NULL;
738 c_set(a);
740 joarg:
741 if (oargs_count == oargs_size)
742 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
743 oargs[oargs_count++] = okey;
744 break;
745 case 's':
746 /* Subject: */
747 subject = _oarg;
748 options |= OPT_SENDMODE;
749 break;
750 case 't':
751 /* Read defined set of headers from mail to be send */
752 options |= OPT_SENDMODE | OPT_t_FLAG;
753 break;
754 case 'u':
755 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
756 * done as necessary in _setup_vars() above */
757 myname = _oarg;
758 break;
759 case 'V':
760 puts(version);
761 exit(0);
762 /* NOTREACHED */
763 case 'v':
764 /* Be verbose */
765 okey = "verbose";
766 #ifdef HAVE_DEBUG
767 ok_bset(verbose, TRU1);
768 #endif
769 goto joarg;
770 case '~':
771 /* Enable tilde escapes even in non-interactive mode */
772 options |= OPT_TILDE_FLAG;
773 break;
774 case '#':
775 /* Work in batch mode, even if non-interactive */
776 if (oargs_count + 6 >= oargs_size)
777 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
778 oargs[oargs_count + 0] = "dot";
779 oargs[oargs_count + 1] = "emptystart";
780 oargs[oargs_count + 2] = "noheader";
781 oargs[oargs_count + 3] = "quiet";
782 oargs[oargs_count + 4] = "sendwait";
783 oargs[oargs_count + 5] = "MBOX=/dev/null";
784 oargs_count += 6;
785 folder = "/dev/null";
786 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
787 break;
788 case '?':
789 jusage:
790 fprintf(stderr, tr(135, usagestr), progname, progname, progname);
791 exit_status = EXIT_USE;
792 goto jleave;
796 if (folder != NULL) {
797 if (_oind < argc) {
798 if (_oind + 1 < argc) {
799 fprintf(stderr, tr(205, "More than one file given with -f\n"));
800 goto jusage;
802 folder = argv[_oind];
804 } else {
805 for (i = _oind; argv[i]; ++i)
806 to = cat(to, checkaddrs(lextract(argv[i], GTO | GFULL)));
807 if (to != NULL)
808 options |= OPT_SENDMODE;
811 /* Check for inconsistent arguments */
812 if (options & OPT_SENDMODE) {
813 if (folder != NULL) {
814 fprintf(stderr, tr(137, "Cannot give -f and people to send to.\n"));
815 goto jusage;
817 if (myname != NULL) {
818 fprintf(stderr, tr(568,
819 "The -u option cannot be used in send mode\n"));
820 goto jusage;
822 if (!(options & OPT_t_FLAG) && to == NULL) {
823 fprintf(stderr, tr(138,
824 "Send options without primary recipient specified.\n"));
825 goto jusage;
827 if (options & (OPT_HEADERSONLY | OPT_HEADERLIST)) {
828 fprintf(stderr, tr(45,
829 "The -H and -L options cannot be used in send mode.\n"));
830 goto jusage;
832 if (options & OPT_R_FLAG) {
833 fprintf(stderr,
834 tr(235, "The -R option is meaningless in send mode.\n"));
835 goto jusage;
837 } else {
838 if (folder != NULL && myname != NULL) {
839 fprintf(stderr, tr(569,
840 "The options -f and -u are mutually exclusive\n"));
841 goto jusage;
846 * Likely to go, perform more setup
849 _setup_vars();
851 if (options & OPT_INTERACTIVE) {
852 _setscreensize(0);
853 #ifdef SIGWINCH
854 # ifndef TTY_WANTS_SIGWINCH
855 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
856 # endif
857 safe_signal(SIGWINCH, _setscreensize);
858 #endif
859 } else
860 scrnheight = realscreenheight = 24, scrnwidth = 80;
862 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
863 spreserve();
865 if (!(options & OPT_NOSRC) && getenv("NAIL_NO_SYSTEM_RC") == NULL)
866 load(SYSCONFRC);
867 /* *expand() returns a savestr(), but load only uses the file name for
868 * fopen(), so it's safe to do this */
869 if ((cp = getenv("MAILRC")) == NULL && (cp = getenv("NAILRC")) == NULL)
870 cp = UNCONST(MAILRC);
871 load(file_expand(cp));
872 if (getenv("NAIL_EXTRA_RC") == NULL &&
873 (cp = ok_vlook(NAIL_EXTRA_RC)) != NULL)
874 load(file_expand(cp));
876 /* Now we can set the account */
877 if (Aarg != NULL) {
878 char *a[2];
879 a[0] = Aarg;
880 a[1] = NULL;
881 c_account(a);
884 /* Ensure the -S and other command line options take precedence over
885 * anything that may have been placed in resource files */
886 for (i = 0; UICMP(z, i, <, oargs_count); ++i) {
887 char const *a[2];
888 a[0] = oargs[i];
889 a[1] = NULL;
890 c_set(a);
894 * We're finally completely setup and ready to go
897 starting =
898 var_clear_allow_undefined = FAL0;
900 if (options & OPT_DEBUG)
901 fprintf(stderr, tr(199, "user = %s, homedir = %s\n"), myname, homedir);
903 if (!(options & OPT_SENDMODE)) {
904 exit_status = _rcv_mode(folder, Larg);
905 goto jleave;
908 /* Now that full mailx(1)-style file expansion is possible handle the
909 * attachments which we had delayed due to this.
910 * This may use savestr(), but since we won't enter the command loop we
911 * don't need to care about that */
912 while (a_head != NULL) {
913 attach = add_attachment(attach, a_head->aa_file, NULL);
914 if (attach != NULL) {
915 a_curr = a_head;
916 a_head = a_head->aa_next;
917 ac_free(a_curr);
918 } else {
919 perror(a_head->aa_file);
920 exit_status = EXIT_ERR;
921 goto jleave;
925 /* xxx exit_status = EXIT_OK; */
926 if (options & OPT_INTERACTIVE)
927 tty_init();
928 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
929 if (options & OPT_INTERACTIVE)
930 tty_destroy();
931 jleave:
932 NYD_LEAVE;
933 return exit_status;
936 /* vim:set fenc=utf-8:s-it-mode */