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