mk-conf.sh, uninstall: avoid (faulty) shell error message
[s-mailx.git] / main.c
blob1591305c645eb8f8d6f9b51e90e1f29fdfc31cdf
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: illegal 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 /* noallnet */
278 /* noappend */
279 ok_bset(asksub, TRU1);
280 /* noaskbcc */
281 /* noaskcc */
282 /* noautoprint */
283 /* nobang */
284 /* nocmd */
285 /* nocrt */
286 /* nodebug */
287 /* nodot */
288 /* ok_vset(escape, ESCAPE *"~"*); TODO non-compliant */
289 /* noflipr */
290 /* nofolder */
291 ok_bset(header, TRU1);
292 /* nohold */
293 /* noignore */
294 /* noignoreeof */
295 /* nokeep */
296 /* nokeepsave */
297 /* nometoo */
298 /* noonehop -- Note: we ignore this one */
299 /* nooutfolder */
300 /* nopage */
301 ok_vset(prompt, "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
302 /* noquiet */
303 /* norecord */
304 ok_bset(save, TRU1);
305 /* nosendwait */
306 /* noshowto */
307 /* nosign */
308 /* noSign */
309 /* ok_vset(toplines, "5"); XXX somewhat hmm */
311 #ifdef HAVE_SETLOCALE
312 setlocale(LC_ALL, "");
313 mb_cur_max = MB_CUR_MAX;
314 # ifdef HAVE_NL_LANGINFO
315 if (ok_vlook(ttycharset) == NULL && (cp = nl_langinfo(CODESET)) != NULL)
316 ok_vset(ttycharset, cp);
317 # endif
319 # ifdef HAVE_C90AMEND1
320 if (mb_cur_max > 1) {
321 wchar_t wc;
322 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
323 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
324 options |= OPT_UNICODE;
325 /* Reset possibly messed up state; luckily this also gives us an
326 * indication wether the encoding has locking shift state sequences */
327 /* TODO temporary - use option bits! */
328 enc_has_state = mbtowc(&wc, NULL, mb_cur_max);
330 # endif
331 #else
332 mb_cur_max = 1;
333 #endif
335 #ifdef HAVE_CATGETS
336 # ifdef NL_CAT_LOCALE
337 catd = catopen(CATNAME, NL_CAT_LOCALE);
338 # else
339 catd = catopen(CATNAME, 0);
340 # endif
341 #endif
343 #ifdef HAVE_ICONV
344 iconvd = (iconv_t)-1;
345 #endif
346 NYD_LEAVE;
349 static size_t
350 _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt)
352 /* Before spreserve(): use our string pool instead of LibC heap */
353 char const **newcpp;
354 NYD_ENTER;
356 newcpp = salloc(sizeof(char*) * newsize);
358 if (oldcnt > 0)
359 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
360 *cpp = newcpp;
361 NYD_LEAVE;
362 return newsize;
365 static void
366 _setup_vars(void)
368 /* Before spreserve(): use our string pool instead of LibC heap */
369 /* XXX further check paths? */
370 char const *cp;
371 uid_t uid;
372 struct passwd *pwuid, *pw;
373 NYD_ENTER;
375 tempdir = ((cp = getenv("TMPDIR")) != NULL) ? savestr(cp) : TMPDIR_FALLBACK;
377 cp = (myname == NULL) ? getenv("USER") : myname;
378 uid = getuid();
379 if ((pwuid = getpwuid(uid)) == NULL)
380 panic(tr(201, "Cannot associate a name with uid %lu"), (ul_it)uid);
381 if (cp == NULL)
382 myname = pwuid->pw_name;
383 else if ((pw = getpwnam(cp)) == NULL)
384 panic(tr(236, "`%s' is not a user of this system"), cp);
385 else {
386 myname = pw->pw_name;
387 if (pw->pw_uid != uid)
388 options |= OPT_u_FLAG;
390 myname = savestr(myname);
391 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
393 if ((cp = getenv("HOME")) == NULL)
394 cp = "."; /* XXX User and Login objects; Login: pw->pw_dir */
395 homedir = savestr(cp);
396 NYD_LEAVE;
399 static void
400 _setscreensize(int is_sighdl)
402 struct termios tbuf;
403 #ifdef TIOCGWINSZ
404 struct winsize ws;
405 #elif defined TIOCGSIZE
406 struct ttysize ts;
407 #endif
408 NYD_ENTER;
410 scrnheight = realscreenheight = scrnwidth = 0;
412 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
413 * a little bit to be able to honour resizes during our lifetime and
414 * only honour it upon first run; abuse *is_sighdl* as an indicator */
415 if (!is_sighdl) {
416 char *cp;
417 long i;
419 if ((cp = getenv("LINES")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
420 scrnheight = realscreenheight = (int)i;
421 if ((cp = getenv("COLUMNS")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
422 scrnwidth = (int)i;
424 if (scrnwidth != 0 && scrnheight != 0)
425 goto jleave;
428 #ifdef TIOCGWINSZ
429 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
430 ws.ws_col = ws.ws_row = 0;
431 #elif defined TIOCGSIZE
432 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) == -1)
433 ts.ts_lines = ts.ts_cols = 0;
434 #endif
436 if (scrnheight == 0) {
437 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) == -1)
438 ? B9600 : cfgetospeed(&tbuf));
440 if (ospeed < B1200)
441 scrnheight = 9;
442 else if (ospeed == B1200)
443 scrnheight = 14;
444 #ifdef TIOCGWINSZ
445 else if (ws.ws_row != 0)
446 scrnheight = ws.ws_row;
447 #elif defined TIOCGSIZE
448 else if (ts.ts_lines != 0)
449 scrnheight = ts.ts_lines;
450 #endif
451 else
452 scrnheight = 24;
454 #if defined TIOCGWINSZ || defined TIOCGSIZE
455 if (0 ==
456 # ifdef TIOCGWINSZ
457 (realscreenheight = ws.ws_row)
458 # else
459 (realscreenheight = ts.ts_lines)
460 # endif
462 realscreenheight = 24;
463 #endif
466 if (scrnwidth == 0 && 0 ==
467 #ifdef TIOCGWINSZ
468 (scrnwidth = ws.ws_col)
469 #elif defined TIOCGSIZE
470 (scrnwidth = ts.ts_cols)
471 #endif
473 scrnwidth = 80;
475 jleave:
476 #ifdef SIGWINCH
477 if (is_sighdl && IS_TTY_SESSION())
478 tty_signal(SIGWINCH);
479 #endif
480 NYD_LEAVE;
483 static sigjmp_buf __hdrjmp; /* XXX */
485 static int
486 _rcv_mode(char const *folder, char const *Larg)
488 char *cp;
489 int i;
490 sighandler_type prevint;
491 NYD_ENTER;
493 if (folder == NULL)
494 folder = "%";
495 else if (*folder == '@') {
496 /* This must be treated specially to make possible invocation like
497 * -A imap -f @mailbox */
498 if ((cp = ok_vlook(folder)) != NULL && which_protocol(cp) == PROTO_IMAP)
499 n_strlcpy(mailname, cp, PATH_MAX);
502 i = setfile(folder, 0);
503 if (i < 0) {
504 exit_status = EXIT_ERR; /* error already reported */
505 goto jleave;
507 if (options & OPT_EXISTONLY) {
508 exit_status = i;
509 goto jleave;
511 if (options & (OPT_HEADERSONLY | OPT_HEADERLIST)) {
512 if ((exit_status = i) == EXIT_OK)
513 print_header_summary(Larg);
514 goto jleave;
517 callhook(mailname, 0);
518 if (i > 0 && !ok_blook(emptystart)) {
519 exit_status = EXIT_ERR;
520 goto jleave;
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 jleave:
551 NYD_LEAVE;
552 return exit_status;
555 static void
556 _hdrstop(int signo)
558 NYD_X; /* Signal handler */
559 UNUSED(signo);
561 fflush(stdout);
562 fprintf(stderr, tr(141, "\nInterrupt\n"));
563 siglongjmp(__hdrjmp, 1);
567 main(int argc, char *argv[])
569 static char const optstr[] = "A:a:Bb:c:DdEeFfHiL:NnO:q:Rr:S:s:tu:Vv~#",
570 usagestr[] =
571 "Synopsis:\n"
572 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
573 "[-b bcc-addr] [-c cc-addr]\n"
574 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
575 "[-S var[=value]]\n"
576 "\t [-s subject] to-addr...\n"
577 " %s [-BDdEeHiNnRv~#] [-A acc] [-L spec-list] [-S var[=value]] "
578 "-f [file]\n"
579 " %s [-BDdEeHiNnRv~#] [-A acc] [-L spec-list] [-S var[=value]] "
580 "[-u user]\n";
582 struct a_arg *a_head = NULL, *a_curr = /* silence CC */ NULL;
583 struct name *to = NULL, *cc = NULL, *bcc = NULL;
584 struct attachment *attach = NULL;
585 char *cp = NULL, *subject = NULL, *qf = NULL, *Aarg = NULL, *Larg = NULL;
586 char const *okey, **oargs = NULL, *folder = NULL;
587 size_t oargs_size = 0, oargs_count = 0, smopts_size = 0;
588 int i;
589 NYD_ENTER;
592 * Start our lengthy setup
595 starting =
596 var_clear_allow_undefined = TRU1;
598 progname = argv[0];
599 _startup();
601 /* Command line parsing
602 * Variable settings need to be done twice, since the user surely wants the
603 * setting to take effect immediately, but also doesn't want it to be
604 * overwritten from within resource files */
605 while ((i = _getopt(argc, argv, optstr)) >= 0) {
606 switch (i) {
607 case 'A':
608 /* Execute an account command later on */
609 Aarg = _oarg;
610 break;
611 case 'a':
612 options |= OPT_SENDMODE;
613 { struct a_arg *nap = ac_alloc(sizeof(struct a_arg));
614 if (a_head == NULL)
615 a_head = nap;
616 else
617 a_curr->aa_next = nap;
618 nap->aa_next = NULL;
619 nap->aa_file = _oarg;
620 a_curr = nap;
622 break;
623 case 'B':
624 /* Make 0/1 line buffered */
625 setvbuf(stdin, NULL, _IOLBF, 0);
626 setvbuf(stdout, NULL, _IOLBF, 0);
627 break;
628 case 'b':
629 /* Get Blind Carbon Copy Recipient list */
630 options |= OPT_SENDMODE;
631 bcc = cat(bcc, checkaddrs(lextract(_oarg, GBCC | GFULL)));
632 break;
633 case 'c':
634 /* Get Carbon Copy Recipient list */
635 options |= OPT_SENDMODE;
636 cc = cat(cc, checkaddrs(lextract(_oarg, GCC | GFULL)));
637 break;
638 case 'D':
639 #ifdef HAVE_IMAP
640 ok_bset(disconnected, TRU1);
641 okey = "disconnected";
642 goto joarg;
643 #else
644 break;
645 #endif
646 case 'd':
647 ok_bset(debug, TRU1);
648 okey = "debug";
649 goto joarg;
650 case 'E':
651 ok_bset(skipemptybody, TRU1);
652 okey = "skipemptybody";
653 goto joarg;
654 case 'e':
655 options |= OPT_EXISTONLY;
656 break;
657 case 'F':
658 options |= OPT_F_FLAG | OPT_SENDMODE;
659 break;
660 case 'f':
661 /* User is specifying file to "edit" with Mail, as opposed to reading
662 * system mailbox. If no argument is given, we read his mbox file.
663 * Check for remaining arguments later */
664 folder = "&";
665 break;
666 case 'H':
667 options |= OPT_HEADERSONLY;
668 break;
669 case 'i':
670 /* Ignore interrupts */
671 ok_bset(ignore, TRU1);
672 okey = "ignore";
673 goto joarg;
674 case 'L':
675 Larg = _oarg;
676 options |= OPT_HEADERLIST;
677 if (*Larg == '"' || *Larg == '\'') { /* TODO list.c:listspec_check() */
678 size_t j = strlen(++Larg);
679 if (j > 0)
680 Larg[j - 1] = '\0';
682 break;
683 case 'N':
684 /* Avoid initial header printing */
685 ok_bset(header, FAL0);
686 okey = "noheader";
687 goto joarg;
688 case 'n':
689 /* Don't source "unspecified system start-up file" */
690 options |= OPT_NOSRC;
691 break;
692 case 'O':
693 /* Additional options to pass-through to MTA */
694 if (smopts_count == (size_t)smopts_size)
695 smopts_size = _grow_cpp(&smopts, smopts_size + 8, smopts_count);
696 smopts[smopts_count++] = skin(_oarg);
697 break;
698 case 'q':
699 /* Quote file TODO drop? -Q with real quote?? what ? */
700 options |= OPT_SENDMODE;
701 if (*_oarg != '-')
702 qf = _oarg;
703 break;
704 case 'R':
705 /* Open folders read-only */
706 options |= OPT_R_FLAG;
707 break;
708 case 'r':
709 /* Set From address. */
710 options |= OPT_r_FLAG;
711 if ((option_r_arg = _oarg)[0] != '\0') {
712 struct name *fa = nalloc(_oarg, GFULL);
714 if (is_addr_invalid(fa, 1) || is_fileorpipe_addr(fa)) {
715 fprintf(stderr, tr(271, "Invalid address argument with -r\n"));
716 goto jusage;
718 option_r_arg = fa->n_name;
719 /* TODO -r options is set in smopts, but may
720 * TODO be overwritten by setting from= in
721 * TODO an interactive session!
722 * TODO Maybe disable setting of from?
723 * TODO Warn user? Update manual!! */
724 okey = savecat("from=", fa->n_fullname);
725 goto joarg;
726 /* ..and fa goes even though it is ready :/ */
728 break;
729 case 'S':
730 /* Set variable (twice) */
731 { char *a[2];
732 okey = a[0] = _oarg;
733 a[1] = NULL;
734 c_set(a);
736 joarg:
737 if (oargs_count == oargs_size)
738 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
739 oargs[oargs_count++] = okey;
740 break;
741 case 's':
742 /* Subject: */
743 subject = _oarg;
744 options |= OPT_SENDMODE;
745 break;
746 case 't':
747 /* Read defined set of headers from mail to be send */
748 options |= OPT_SENDMODE | OPT_t_FLAG;
749 break;
750 case 'u':
751 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
752 * done as necessary in _setup_vars() above */
753 myname = _oarg;
754 break;
755 case 'V':
756 puts(version);
757 exit(0);
758 /* NOTREACHED */
759 case 'v':
760 /* Be verbose */
761 ok_bset(verbose, TRU1);
762 okey = "verbose";
763 goto joarg;
764 case '~':
765 /* Enable tilde escapes even in non-interactive mode */
766 options |= OPT_TILDE_FLAG;
767 break;
768 case '#':
769 /* Work in batch mode, even if non-interactive */
770 if (oargs_count + 6 >= oargs_size)
771 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
772 /* xxx Setting most of the -# options immediately is useless, so be
773 * selective in what is set immediately */
774 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
775 folder = "/dev/null";
776 ok_bset(dot, TRU1);
777 ok_bset(emptystart, TRU1);
778 ok_bset(header, FAL0);
779 ok_bset(quiet, TRU1);
780 ok_bset(sendwait, TRU1);
781 ok_vset(MBOX, folder);
782 oargs[oargs_count + 0] = "dot";
783 oargs[oargs_count + 1] = "emptystart";
784 oargs[oargs_count + 2] = "noheader";
785 oargs[oargs_count + 3] = "quiet";
786 oargs[oargs_count + 4] = "sendwait";
787 oargs[oargs_count + 5] = "MBOX=/dev/null";
788 oargs_count += 6;
789 break;
790 case '?':
791 jusage:
792 fprintf(stderr, tr(135, usagestr), progname, progname, progname);
793 exit_status = EXIT_USE;
794 goto jleave;
798 /* OPT_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
799 if (folder != NULL && folder[1] == '\0') {
800 if (_oind < argc) {
801 if (_oind + 1 < argc) {
802 fprintf(stderr, tr(205, "More than one file given with -f\n"));
803 goto jusage;
805 folder = argv[_oind];
807 } else {
808 for (i = _oind; argv[i]; ++i)
809 to = cat(to, checkaddrs(lextract(argv[i], GTO | GFULL)));
810 if (to != NULL)
811 options |= OPT_SENDMODE;
814 /* Check for inconsistent arguments */
815 if (options & OPT_SENDMODE) {
816 if (folder != NULL && !(options & OPT_BATCH_FLAG)) {
817 fprintf(stderr, tr(137, "Cannot give -f and people to send to.\n"));
818 goto jusage;
820 if (myname != NULL) {
821 fprintf(stderr, tr(568,
822 "The -u option cannot be used in send mode\n"));
823 goto jusage;
825 if (!(options & OPT_t_FLAG) && to == NULL) {
826 fprintf(stderr, tr(138,
827 "Send options without primary recipient specified.\n"));
828 goto jusage;
830 if (options & (OPT_HEADERSONLY | OPT_HEADERLIST)) {
831 fprintf(stderr, tr(45,
832 "The -H and -L options cannot be used in send mode.\n"));
833 goto jusage;
835 if (options & OPT_R_FLAG) {
836 fprintf(stderr,
837 tr(235, "The -R option is meaningless in send mode.\n"));
838 goto jusage;
840 } else {
841 if (folder != NULL && myname != NULL) {
842 fprintf(stderr, tr(569,
843 "The options -f and -u are mutually exclusive\n"));
844 goto jusage;
849 * Likely to go, perform more setup
852 _setup_vars();
854 if (options & OPT_INTERACTIVE) {
855 _setscreensize(0);
856 #ifdef SIGWINCH
857 # ifndef TTY_WANTS_SIGWINCH
858 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
859 # endif
860 safe_signal(SIGWINCH, _setscreensize);
861 #endif
862 } else
863 scrnheight = realscreenheight = 24, scrnwidth = 80;
865 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
866 spreserve();
868 if (!(options & OPT_NOSRC) && getenv("NAIL_NO_SYSTEM_RC") == NULL)
869 load(SYSCONFRC);
870 /* *expand() returns a savestr(), but load only uses the file name for
871 * fopen(), so it's safe to do this */
872 if ((cp = getenv("MAILRC")) == NULL && (cp = getenv("NAILRC")) == NULL)
873 cp = UNCONST(MAILRC);
874 load(file_expand(cp));
875 if (getenv("NAIL_EXTRA_RC") == NULL &&
876 (cp = ok_vlook(NAIL_EXTRA_RC)) != NULL)
877 load(file_expand(cp));
879 /* Now we can set the account */
880 if (Aarg != NULL) {
881 char *a[2];
882 a[0] = Aarg;
883 a[1] = NULL;
884 c_account(a);
887 /* Ensure the -S and other command line options take precedence over
888 * anything that may have been placed in resource files.
889 * Our "ternary binary" option *verbose* needs special treament */
890 if ((options & (OPT_VERB | OPT_VERBVERB)) == OPT_VERB)
891 options &= ~OPT_VERB;
892 for (i = 0; UICMP(z, i, <, oargs_count); ++i) {
893 char const *a[2];
894 a[0] = oargs[i];
895 a[1] = NULL;
896 c_set(a);
900 * We're finally completely setup and ready to go
903 starting =
904 var_clear_allow_undefined = FAL0;
906 if (options & OPT_DEBUG)
907 fprintf(stderr, tr(199, "user = %s, homedir = %s\n"), myname, homedir);
909 if (!(options & OPT_SENDMODE)) {
910 exit_status = _rcv_mode(folder, Larg);
911 goto jleave;
914 /* Now that full mailx(1)-style file expansion is possible handle the
915 * attachments which we had delayed due to this.
916 * This may use savestr(), but since we won't enter the command loop we
917 * don't need to care about that */
918 while (a_head != NULL) {
919 attach = add_attachment(attach, a_head->aa_file, NULL);
920 if (attach != NULL) {
921 a_curr = a_head;
922 a_head = a_head->aa_next;
923 ac_free(a_curr);
924 } else {
925 perror(a_head->aa_file);
926 exit_status = EXIT_ERR;
927 goto jleave;
931 /* xxx exit_status = EXIT_OK; */
932 if (options & OPT_INTERACTIVE)
933 tty_init();
934 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
935 if (options & OPT_INTERACTIVE)
936 tty_destroy();
937 jleave:
938 NYD_LEAVE;
939 return exit_status;
942 /* vim:set fenc=utf-8:s-it-mode */