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