popen.c: use NYD2 where applicable
[s-mailx.git] / main.c
blob863518bd1ae904cb5694dc8ed6dd7d0bdcf6432c
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 static char *_oarg;
116 static int _oind, /*_oerr,*/ _oopt;
118 /* Our own little getopt(3) */
119 static int _getopt(int argc, char * const argv[], char const *optstring);
121 /* Perform basic startup initialization */
122 static void _startup(void);
124 /* Grow a char** */
125 static size_t _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt);
127 /* Initialize *tempdir*, *myname*, *homedir* */
128 static void _setup_vars(void);
130 /* We're in an interactive session - compute what the screen size for printing
131 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
132 * We use the following algorithm for the height:
133 * If baud rate < 1200, use 9
134 * If baud rate = 1200, use 14
135 * If baud rate > 1200, use 24 or ws_row
136 * Width is either 80 or ws_col */
137 static void _setscreensize(int is_sighdl);
139 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
140 * the system mailbox, and open up the right stuff */
141 static int _rcv_mode(char const *folder, char const *Larg);
143 /* Interrupt printing of the headers */
144 static void _hdrstop(int signo);
146 static int
147 _getopt(int argc, char * const argv[], char const *optstring)
149 static char const *lastp;
151 int rv = -1, colon;
152 char const *curp;
153 NYD_ENTER;
155 if ((colon = (optstring[0] == ':')))
156 ++optstring;
157 if (lastp != NULL) {
158 curp = lastp;
159 lastp = 0;
160 } else {
161 if (_oind >= argc || argv[_oind] == NULL || argv[_oind][0] != '-' ||
162 argv[_oind][1] == '\0')
163 goto jleave;
164 if (argv[_oind][1] == '-' && argv[_oind][2] == '\0') {
165 /* We need this in for MTA arg detection (easier) ++_oind;*/
166 goto jleave;
168 curp = &argv[_oind][1];
171 _oopt = curp[0];
172 while (optstring[0] != '\0') {
173 if (optstring[0] == ':' || optstring[0] != _oopt) {
174 ++optstring;
175 continue;
177 if (optstring[1] == ':') {
178 if (curp[1] != '\0') {
179 _oarg = UNCONST(curp + 1);
180 ++_oind;
181 } else {
182 if ((_oind += 2) > argc) {
183 if (!colon /*&& _oerr*/) {
184 fprintf(stderr,
185 _("%s: option requires an argument -- %c\n"),
186 argv[0], (char)_oopt);
188 rv = (colon ? ':' : '?');
189 goto jleave;
191 _oarg = argv[_oind - 1];
193 } else {
194 if (curp[1] != '\0')
195 lastp = curp + 1;
196 else
197 ++_oind;
198 _oarg = NULL;
200 rv = _oopt;
201 goto jleave;
204 if (!colon /*&& opterr*/)
205 fprintf(stderr, _("%s: invalid option -- %c\n"), argv[0], _oopt);
206 if (curp[1] != '\0')
207 lastp = curp + 1;
208 else
209 ++_oind;
210 _oarg = 0;
211 rv = '?';
212 jleave:
213 NYD_LEAVE;
214 return rv;
217 static void
218 _startup(void)
220 char *cp;
221 NYD_ENTER;
223 /* Absolutely the first thing we do is save our egid and set it to the rgid,
224 * so that we can safely run setgid. We use the sgid (saved set-gid) to
225 * allow ourselves to revert to the egid if we want (temporarily) to become
226 * privileged XXX (s-nail-)*dotlock(-program)* [maybe forked<->Unix IPC?] */
227 effectivegid = getegid();
228 realgid = getgid();
229 if (setgid(realgid) == -1) {
230 perror("setgid");
231 exit(1);
234 image = -1;
235 dflpipe = SIG_DFL;
236 _oind = /*_oerr =*/ 1;
238 if ((cp = strrchr(progname, '/')) != NULL)
239 progname = ++cp;
241 /* Set up a reasonable environment */
243 #ifdef HAVE_DEBUG
244 safe_signal(SIGABRT, &_nyd_oncrash);
245 # ifdef SIGBUS
246 safe_signal(SIGBUS, &_nyd_oncrash);
247 # endif
248 safe_signal(SIGFPE, &_nyd_oncrash);
249 safe_signal(SIGILL, &_nyd_oncrash);
250 safe_signal(SIGSEGV, &_nyd_oncrash);
251 #endif
252 command_manager_start();
254 if (isatty(STDIN_FILENO)) /* TODO should be isatty(0) && isatty(2)?? */
255 options |= OPT_TTYIN | OPT_INTERACTIVE;
256 if (isatty(STDOUT_FILENO))
257 options |= OPT_TTYOUT;
258 if (IS_TTY_SESSION())
259 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
261 /* Define defaults for internal variables, based on POSIX 2008/Cor 1-2013.
262 * Note: on change please update ./nail.rc accordingly */
263 /* noallnet */
264 /* noappend */
265 ok_bset(asksub, TRU1);
266 /* noaskbcc */
267 /* noaskcc */
268 /* noautoprint */
269 /* nobang */
270 /* nocmd */
271 /* nocrt */
272 /* nodebug */
273 /* nodot */
274 /* ok_vset(escape, ESCAPE *"~"*); TODO non-compliant */
275 /* noflipr */
276 /* nofolder */
277 ok_bset(header, TRU1);
278 /* nohold */
279 /* noignore */
280 /* noignoreeof */
281 /* nokeep */
282 /* nokeepsave */
283 /* nometoo */
284 /* noonehop -- Note: we ignore this one */
285 /* nooutfolder */
286 /* nopage */
287 ok_vset(prompt, "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
288 /* noquiet */
289 /* norecord */
290 ok_bset(save, TRU1);
291 /* nosendwait */
292 /* noshowto */
293 /* nosign */
294 /* noSign */
295 /* ok_vset(toplines, "5"); XXX somewhat hmm */
297 #ifdef HAVE_SETLOCALE
298 setlocale(LC_ALL, "");
299 mb_cur_max = MB_CUR_MAX;
300 # ifdef HAVE_NL_LANGINFO
301 if (ok_vlook(ttycharset) == NULL && (cp = nl_langinfo(CODESET)) != NULL)
302 ok_vset(ttycharset, cp);
303 # endif
305 # ifdef HAVE_C90AMEND1
306 if (mb_cur_max > 1) {
307 wchar_t wc;
308 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
309 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
310 options |= OPT_UNICODE;
311 /* Reset possibly messed up state; luckily this also gives us an
312 * indication wether the encoding has locking shift state sequences */
313 /* TODO temporary - use option bits! */
314 enc_has_state = mbtowc(&wc, NULL, mb_cur_max);
316 # endif
317 #else
318 mb_cur_max = 1;
319 #endif
321 #ifdef HAVE_ICONV
322 iconvd = (iconv_t)-1;
323 #endif
324 NYD_LEAVE;
327 static size_t
328 _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt)
330 /* Before spreserve(): use our string pool instead of LibC heap */
331 char const **newcpp;
332 NYD_ENTER;
334 newcpp = salloc(sizeof(char*) * newsize);
336 if (oldcnt > 0)
337 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
338 *cpp = newcpp;
339 NYD_LEAVE;
340 return newsize;
343 static void
344 _setup_vars(void)
346 /* Before spreserve(): use our string pool instead of LibC heap */
347 /* XXX further check paths? */
348 char const *cp;
349 uid_t uid;
350 struct passwd *pwuid, *pw;
351 NYD_ENTER;
353 tempdir = ((cp = getenv("TMPDIR")) != NULL) ? savestr(cp) : TMPDIR_FALLBACK;
355 cp = (myname == NULL) ? getenv("USER") : myname;
356 uid = getuid();
357 if ((pwuid = getpwuid(uid)) == NULL)
358 panic(_("Cannot associate a name with uid %lu"), (ul_it)uid);
359 if (cp == NULL)
360 myname = pwuid->pw_name;
361 else if ((pw = getpwnam(cp)) == NULL)
362 panic(_("`%s' is not a user of this system"), cp);
363 else {
364 myname = pw->pw_name;
365 if (pw->pw_uid != uid)
366 options |= OPT_u_FLAG;
368 myname = savestr(myname);
369 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
371 if ((cp = getenv("HOME")) == NULL)
372 cp = "."; /* XXX User and Login objects; Login: pw->pw_dir */
373 homedir = savestr(cp);
374 NYD_LEAVE;
377 static void
378 _setscreensize(int is_sighdl)
380 struct termios tbuf;
381 #ifdef TIOCGWINSZ
382 struct winsize ws;
383 #elif defined TIOCGSIZE
384 struct ttysize ts;
385 #endif
386 NYD_ENTER;
388 scrnheight = realscreenheight = scrnwidth = 0;
390 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
391 * a little bit to be able to honour resizes during our lifetime and
392 * only honour it upon first run; abuse *is_sighdl* as an indicator */
393 if (!is_sighdl) {
394 char *cp;
395 long i;
397 if ((cp = getenv("LINES")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
398 scrnheight = realscreenheight = (int)i;
399 if ((cp = getenv("COLUMNS")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
400 scrnwidth = (int)i;
402 if (scrnwidth != 0 && scrnheight != 0)
403 goto jleave;
406 #ifdef TIOCGWINSZ
407 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
408 ws.ws_col = ws.ws_row = 0;
409 #elif defined TIOCGSIZE
410 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) == -1)
411 ts.ts_lines = ts.ts_cols = 0;
412 #endif
414 if (scrnheight == 0) {
415 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) == -1)
416 ? B9600 : cfgetospeed(&tbuf));
418 if (ospeed < B1200)
419 scrnheight = 9;
420 else if (ospeed == B1200)
421 scrnheight = 14;
422 #ifdef TIOCGWINSZ
423 else if (ws.ws_row != 0)
424 scrnheight = ws.ws_row;
425 #elif defined TIOCGSIZE
426 else if (ts.ts_lines != 0)
427 scrnheight = ts.ts_lines;
428 #endif
429 else
430 scrnheight = 24;
432 #if defined TIOCGWINSZ || defined TIOCGSIZE
433 if (0 ==
434 # ifdef TIOCGWINSZ
435 (realscreenheight = ws.ws_row)
436 # else
437 (realscreenheight = ts.ts_lines)
438 # endif
440 realscreenheight = 24;
441 #endif
444 if (scrnwidth == 0 && 0 ==
445 #ifdef TIOCGWINSZ
446 (scrnwidth = ws.ws_col)
447 #elif defined TIOCGSIZE
448 (scrnwidth = ts.ts_cols)
449 #endif
451 scrnwidth = 80;
453 jleave:
454 #ifdef SIGWINCH
455 if (is_sighdl && IS_TTY_SESSION())
456 tty_signal(SIGWINCH);
457 #endif
458 NYD_LEAVE;
461 static sigjmp_buf __hdrjmp; /* XXX */
463 static int
464 _rcv_mode(char const *folder, char const *Larg)
466 char *cp;
467 int i;
468 sighandler_type prevint;
469 NYD_ENTER;
471 if (folder == NULL)
472 folder = "%";
473 else if (*folder == '@') {
474 /* This must be treated specially to make possible invocation like
475 * -A imap -f @mailbox */
476 if ((cp = ok_vlook(folder)) != NULL && which_protocol(cp) == PROTO_IMAP)
477 n_strlcpy(mailname, cp, PATH_MAX);
480 i = setfile(folder, 0);
481 if (i < 0) {
482 exit_status = EXIT_ERR; /* error already reported */
483 goto jleave;
485 if (options & OPT_EXISTONLY) {
486 exit_status = i;
487 goto jleave;
489 if (options & (OPT_HEADERSONLY | OPT_HEADERLIST)) {
490 if ((exit_status = i) == EXIT_OK)
491 print_header_summary(Larg);
492 goto jleave;
495 callhook(mailname, 0);
496 if (i > 0 && !ok_blook(emptystart)) {
497 exit_status = EXIT_ERR;
498 goto jleave;
501 if (sigsetjmp(__hdrjmp, 1) == 0) {
502 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
503 safe_signal(SIGINT, _hdrstop);
504 if (!(options & OPT_N_FLAG)) {
505 if (!ok_blook(quiet))
506 printf(_("%s version %s. Type ? for help.\n"),
507 (ok_blook(bsdcompat) ? "Mail" : uagent), version);
508 announce(1);
509 fflush(stdout);
511 safe_signal(SIGINT, prevint);
514 /* Enter the command loop */
515 if (options & OPT_INTERACTIVE)
516 tty_init();
517 commands();
518 if (options & OPT_INTERACTIVE)
519 tty_destroy();
521 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
522 safe_signal(SIGHUP, SIG_IGN);
523 safe_signal(SIGINT, SIG_IGN);
524 safe_signal(SIGQUIT, SIG_IGN);
526 save_mbox_for_possible_quitstuff();
527 quit();
528 jleave:
529 NYD_LEAVE;
530 return exit_status;
533 static void
534 _hdrstop(int signo)
536 NYD_X; /* Signal handler */
537 UNUSED(signo);
539 fflush(stdout);
540 fprintf(stderr, _("\nInterrupt\n"));
541 siglongjmp(__hdrjmp, 1);
545 main(int argc, char *argv[])
547 static char const optstr[] = "A:a:Bb:c:DdEeFfHiL:NnO:q:Rr:S:s:tu:Vv~#",
548 usagestr[] =
549 " Synopsis:\n"
550 " %s [-BDdEFintv~] [-A account]\n"
551 "\t [-a attachment] [-b bcc-address] [-c cc-address]\n"
552 "\t [-q file] [-r from-address] [-S var[=value]...]\n"
553 "\t [-s subject] to-address... [-- mta-option...]\n"
554 " %s [-BDdEeHiNnRv~#] [-A account]\n"
555 "\t [-L spec-list] [-S var[=value]...] -f [file] [-- mta-option...]\n"
556 " %s [-BDdEeHiNnRv~#] [-A account]\n"
557 "\t [-L spec-list] [-S var[=value]...] [-u user] [-- mta-option...]\n"
560 struct a_arg *a_head = NULL, *a_curr = /* silence CC */ NULL;
561 struct name *to = NULL, *cc = NULL, *bcc = NULL;
562 struct attachment *attach = NULL;
563 char *cp = NULL, *subject = NULL, *qf = NULL, *Aarg = NULL, *Larg = NULL;
564 char const *okey, **oargs = NULL, *folder = NULL;
565 size_t oargs_size = 0, oargs_count = 0, smopts_size = 0;
566 int i;
567 NYD_ENTER;
570 * Start our lengthy setup
573 starting =
574 var_clear_allow_undefined = TRU1;
576 progname = argv[0];
577 _startup();
579 /* Command line parsing
580 * Variable settings need to be done twice, since the user surely wants the
581 * setting to take effect immediately, but also doesn't want it to be
582 * overwritten from within resource files */
583 while ((i = _getopt(argc, argv, optstr)) >= 0) {
584 switch (i) {
585 case 'A':
586 /* Execute an account command later on */
587 Aarg = _oarg;
588 break;
589 case 'a':
590 options |= OPT_SENDMODE;
591 { struct a_arg *nap = ac_alloc(sizeof(struct a_arg));
592 if (a_head == NULL)
593 a_head = nap;
594 else
595 a_curr->aa_next = nap;
596 nap->aa_next = NULL;
597 nap->aa_file = _oarg;
598 a_curr = nap;
600 break;
601 case 'B':
602 /* Make 0/1 line buffered */
603 setvbuf(stdin, NULL, _IOLBF, 0);
604 setvbuf(stdout, NULL, _IOLBF, 0);
605 break;
606 case 'b':
607 /* Get Blind Carbon Copy Recipient list */
608 options |= OPT_SENDMODE;
609 bcc = cat(bcc, checkaddrs(lextract(_oarg, GBCC | GFULL)));
610 break;
611 case 'c':
612 /* Get Carbon Copy Recipient list */
613 options |= OPT_SENDMODE;
614 cc = cat(cc, checkaddrs(lextract(_oarg, GCC | GFULL)));
615 break;
616 case 'D':
617 #ifdef HAVE_IMAP
618 ok_bset(disconnected, TRU1);
619 okey = "disconnected";
620 goto joarg;
621 #else
622 break;
623 #endif
624 case 'd':
625 ok_bset(debug, TRU1);
626 okey = "debug";
627 goto joarg;
628 case 'E':
629 ok_bset(skipemptybody, TRU1);
630 okey = "skipemptybody";
631 goto joarg;
632 case 'e':
633 options |= OPT_EXISTONLY;
634 break;
635 case 'F':
636 options |= OPT_F_FLAG | OPT_SENDMODE;
637 break;
638 case 'f':
639 /* User is specifying file to "edit" with Mail, as opposed to reading
640 * system mailbox. If no argument is given, we read his mbox file.
641 * Check for remaining arguments later */
642 folder = "&";
643 break;
644 case 'H':
645 options |= OPT_HEADERSONLY;
646 break;
647 case 'i':
648 /* Ignore interrupts */
649 ok_bset(ignore, TRU1);
650 okey = "ignore";
651 goto joarg;
652 case 'L':
653 Larg = _oarg;
654 options |= OPT_HEADERLIST;
655 if (*Larg == '"' || *Larg == '\'') { /* TODO list.c:listspec_check() */
656 size_t j = strlen(++Larg);
657 if (j > 0)
658 Larg[j - 1] = '\0';
660 break;
661 case 'N':
662 /* Avoid initial header printing */
663 ok_bset(header, FAL0);
664 okey = "noheader";
665 goto joarg;
666 case 'n':
667 /* Don't source "unspecified system start-up file" */
668 options |= OPT_NOSRC;
669 break;
670 case 'O':
671 /* Additional options to pass-through to MTA TODO v15-compat legacy */
672 if (smopts_count == (size_t)smopts_size)
673 smopts_size = _grow_cpp(&smopts, smopts_size + 8, smopts_count);
674 smopts[smopts_count++] = _oarg;
675 break;
676 case 'q':
677 /* Quote file TODO drop? -Q with real quote?? what ? */
678 options |= OPT_SENDMODE;
679 if (*_oarg != '-')
680 qf = _oarg;
681 break;
682 case 'R':
683 /* Open folders read-only */
684 options |= OPT_R_FLAG;
685 break;
686 case 'r':
687 /* Set From address. */
688 options |= OPT_r_FLAG;
689 if ((option_r_arg = _oarg)[0] != '\0') {
690 struct name *fa = nalloc(_oarg, GFULL);
692 if (is_addr_invalid(fa, 1) || is_fileorpipe_addr(fa)) {
693 fprintf(stderr, _("Invalid address argument with -r\n"));
694 goto jusage;
696 option_r_arg = fa->n_name;
697 /* TODO -r options is set in smopts, but may
698 * TODO be overwritten by setting from= in
699 * TODO an interactive session!
700 * TODO Maybe disable setting of from?
701 * TODO Warn user? Update manual!! */
702 okey = savecat("from=", fa->n_fullname);
703 goto joarg;
704 /* ..and fa goes even though it is ready :/ */
706 break;
707 case 'S':
708 /* Set variable (twice) */
709 { char *a[2];
710 okey = a[0] = _oarg;
711 a[1] = NULL;
712 c_set(a);
714 joarg:
715 if (oargs_count == oargs_size)
716 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
717 oargs[oargs_count++] = okey;
718 break;
719 case 's':
720 /* Subject: */
721 subject = _oarg;
722 options |= OPT_SENDMODE;
723 break;
724 case 't':
725 /* Read defined set of headers from mail to be send */
726 options |= OPT_SENDMODE | OPT_t_FLAG;
727 break;
728 case 'u':
729 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
730 * done as necessary in _setup_vars() above */
731 myname = _oarg;
732 break;
733 case 'V':
734 puts(version);
735 exit(0);
736 /* NOTREACHED */
737 case 'v':
738 /* Be verbose */
739 ok_bset(verbose, TRU1);
740 okey = "verbose";
741 goto joarg;
742 case '~':
743 /* Enable tilde escapes even in non-interactive mode */
744 options |= OPT_TILDE_FLAG;
745 break;
746 case '#':
747 /* Work in batch mode, even if non-interactive */
748 if (oargs_count + 6 >= oargs_size)
749 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_count);
750 /* xxx Setting most of the -# options immediately is useless, so be
751 * selective in what is set immediately */
752 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
753 folder = "/dev/null";
754 ok_bset(dot, TRU1);
755 ok_bset(emptystart, TRU1);
756 ok_bset(header, FAL0);
757 ok_bset(quiet, TRU1);
758 ok_bset(sendwait, TRU1);
759 ok_vset(MBOX, folder);
760 oargs[oargs_count + 0] = "dot";
761 oargs[oargs_count + 1] = "emptystart";
762 oargs[oargs_count + 2] = "noheader";
763 oargs[oargs_count + 3] = "quiet";
764 oargs[oargs_count + 4] = "sendwait";
765 oargs[oargs_count + 5] = "MBOX=/dev/null";
766 oargs_count += 6;
767 break;
768 case '?':
769 jusage:
770 fprintf(stderr, _(usagestr), progname, progname, progname);
771 exit_status = EXIT_USE;
772 goto jleave;
776 /* The normal arguments may be followed by MTA arguments after `--';
777 * however, -f may take off an argument, too, and before that */
778 if ((cp = argv[i = _oind]) == NULL)
780 else if (cp[0] == '-' && cp[1] == '-' && cp[2] == '\0')
781 cp = argv[++i];
782 /* OPT_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
783 else if (folder != NULL && folder[1] == '\0') {
784 folder = cp;
785 if ((cp = argv[++i]) != NULL) {
786 if (cp[0] != '-' || cp[1] != '-' || cp[2] != '\0') {
787 fprintf(stderr, _("More than one file given with -f\n"));
788 goto jusage;
790 cp = argv[++i];
792 } else {
793 for (;;) {
794 to = cat(to, checkaddrs(lextract(cp, GTO | GFULL)));
795 if ((cp = argv[++i]) == NULL)
796 break;
797 if (cp[0] == '-' && cp[1] == '-' && cp[2] == '\0') {
798 cp = argv[++i];
799 break;
802 if (to != NULL)
803 options |= OPT_SENDMODE;
806 /* Additional options to pass-through to MTA? */
807 while (cp != NULL) {
808 if (smopts_count == (size_t)smopts_size)
809 smopts_size = _grow_cpp(&smopts, smopts_size + 8,
810 smopts_count);
811 smopts[smopts_count++] = cp;
812 cp = argv[++i];
815 /* Check for inconsistent arguments */
816 if (options & OPT_SENDMODE) {
817 if (folder != NULL && !(options & OPT_BATCH_FLAG)) {
818 fprintf(stderr, _("Cannot give -f and people to send to.\n"));
819 goto jusage;
821 if (myname != NULL) {
822 fprintf(stderr, _(
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, _(
828 "Send options without primary recipient specified.\n"));
829 goto jusage;
831 if (options & (OPT_HEADERSONLY | OPT_HEADERLIST)) {
832 fprintf(stderr, _(
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 _("The -R option is meaningless in send mode.\n"));
839 goto jusage;
841 } else {
842 if (folder != NULL && myname != NULL) {
843 fprintf(stderr, _(
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, _("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 */