1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Startup and initialization.
3 *@ This file is also used to materialize externals.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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
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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <sys/ioctl.h>
46 #ifdef HAVE_NL_LANGINFO
47 # include <langinfo.h>
53 /* Verify that our size_t PRI[du]Z format string has the correct type size */
57 struct a_arg
*aa_next
;
62 struct X_arg
*xa_next
;
64 char xa_cmd_buf
[VFIELD_SIZE(sizeof(size_t))];
67 /* (extern, but not with amalgamation, so define here) */
68 VL
char const weekday_names
[7 + 1][4] = {
69 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
71 VL
char const month_names
[12 + 1][4] = {
72 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
73 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
75 VL
char const uagent
[] = UAGENT
;
76 VL uc_i
const class_char
[] = {
77 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
78 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
79 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
80 C_CNTRL
, C_BLANK
, C_WHITE
, C_SPACE
, C_SPACE
, C_SPACE
, C_CNTRL
, C_CNTRL
,
81 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
82 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
83 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
84 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
85 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
86 C_BLANK
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
87 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
88 C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
89 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
90 C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
,
91 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
92 C_DIGIT
, C_DIGIT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
93 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
94 C_PUNCT
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
,
95 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
96 C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
,
97 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
98 C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
,
99 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
100 C_UPPER
, C_UPPER
, C_UPPER
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
101 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
102 C_PUNCT
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
,
103 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
104 C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
,
105 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
106 C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
,
107 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
108 C_LOWER
, C_LOWER
, C_LOWER
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_CNTRL
111 /* Our own little getopt(3) */
113 static int _oind
, /*_oerr,*/ _oopt
;
115 /* Our own little getopt(3); note --help is special-treated as 'h' */
116 static int _getopt(int argc
, char * const argv
[], char const *optstring
);
118 /* Perform basic startup initialization */
119 static void _startup(void);
122 static size_t _grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
);
124 /* Initialize *tempdir*, *myname*, *homedir* */
125 static void _setup_vars(void);
127 /* We're in an interactive session - compute what the screen size for printing
128 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
129 * We use the following algorithm for the height:
130 * If baud rate < 1200, use 9
131 * If baud rate = 1200, use 14
132 * If baud rate > 1200, use 24 or ws_row
133 * Width is either 80 or ws_col */
134 static void _setscreensize(int is_sighdl
);
136 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
137 * the system mailbox, and open up the right stuff */
138 static int _rcv_mode(char const *folder
, char const *Larg
,
141 /* Interrupt printing of the headers */
142 static void _hdrstop(int signo
);
144 /* -X arg given at least once, evaluate the list in order */
145 static bool_t
_X_arg_eval(struct X_arg
*xhp
);
148 _getopt(int argc
, char * const argv
[], char const *optstring
)
150 static char const *lastp
;
158 if ((colon
= (optstring
[0] == ':')))
164 if (_oind
>= argc
|| argv
[_oind
] == NULL
|| argv
[_oind
][0] != '-' ||
165 argv
[_oind
][1] == '\0')
167 if (argv
[_oind
][1] == '-' && argv
[_oind
][2] == '\0') {
168 /* We need this in for MTA arg detection (easier) ++_oind;*/
171 curp
= &argv
[_oind
][1];
175 while (optstring
[0] != '\0') {
176 if (optstring
[0] == ':' || optstring
[0] != _oopt
) {
180 if (optstring
[1] == ':') {
181 if (curp
[1] != '\0') {
182 _oarg
= UNCONST(curp
+ 1);
185 if ((_oind
+= 2) > argc
) {
186 if (!colon
/*&& _oerr*/) {
187 n_err(_("%s: option requires an argument -- %c\n"),
188 argv
[0], (char)_oopt
);
190 rv
= (colon
? ':' : '?');
193 _oarg
= argv
[_oind
- 1];
205 /* Special support for --help, which is quite common */
206 if (_oopt
== '-' && !strcmp(curp
, "-help") && curp
- 1 == argv
[_oind
]) {
212 /* Definitive error */
213 if (!colon
/*&& opterr*/)
214 n_err(_("%s: invalid option -- %c\n"), argv
[0], _oopt
);
234 _oind
= /*_oerr =*/ 1;
236 if ((cp
= strrchr(progname
, '/')) != NULL
)
239 /* Set up a reasonable environment */
242 safe_signal(SIGABRT
, &_nyd_oncrash
);
244 safe_signal(SIGBUS
, &_nyd_oncrash
);
246 safe_signal(SIGFPE
, &_nyd_oncrash
);
247 safe_signal(SIGILL
, &_nyd_oncrash
);
248 safe_signal(SIGSEGV
, &_nyd_oncrash
);
250 command_manager_start();
252 if (isatty(STDIN_FILENO
)) /* TODO should be isatty(0) && isatty(2)?? */
253 options
|= OPT_TTYIN
| OPT_INTERACTIVE
;
254 if (isatty(STDOUT_FILENO
))
255 options
|= OPT_TTYOUT
;
256 if (IS_TTY_SESSION())
257 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
261 /* Define defaults for internal variables, based on POSIX 2008/Cor 1-2013 */
263 * ./main.c:_startup(), ./nail.rc, ./nail.1:"Initial settings") */
266 ok_bset(asksub
, TRU1
);
275 /* ok_vset(escape, ESCAPE *"~"*); TODO non-compliant */
278 ok_bset(header
, TRU1
);
285 /* noonehop -- Note: we ignore this one */
288 ok_vset(prompt
, "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
296 /* ok_vset(toplines, "5"); XXX somewhat hmm */
298 /* TODO until we have an automatic mechanism for that, set some more
299 * TODO variables so that users see the internal fallback settings
300 * TODO (something like "defval=X,notempty=1") */
304 vp
= env_vlook("SHELL", TRU1
);
305 ok_vset(SHELL
, (vp
!= NULL
? vp
: XSHELL
));
307 vp
= env_vlook("LISTER", TRU1
);
308 ok_vset(LISTER
, (vp
!= NULL
? vp
: XLISTER
));
310 vp
= env_vlook("PAGER", TRU1
);
311 ok_vset(PAGER
, (vp
!= NULL
? vp
: XPAGER
));
313 ok_vset(sendmail
, SENDMAIL
);
314 ok_vset(sendmail_progname
, SENDMAIL_PROGNAME
);
319 #ifdef HAVE_SETLOCALE
320 setlocale(LC_ALL
, "");
321 mb_cur_max
= MB_CUR_MAX
;
322 # ifdef HAVE_NL_LANGINFO
323 if (ok_vlook(ttycharset
) == NULL
&& (cp
= nl_langinfo(CODESET
)) != NULL
)
324 ok_vset(ttycharset
, cp
);
327 # ifdef HAVE_C90AMEND1
328 if (mb_cur_max
> 1) {
330 if (mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
331 mbtowc(&wc
, "\342\202\254", 3) == 3 && wc
== 0x20AC)
332 options
|= OPT_UNICODE
;
333 /* Reset possibly messed up state; luckily this also gives us an
334 * indication wether the encoding has locking shift state sequences */
335 /* TODO temporary - use option bits! */
336 if (mbtowc(&wc
, NULL
, mb_cur_max
))
337 options
|= OPT_ENC_MBSTATE
;
345 iconvd
= (iconv_t
)-1;
351 _grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
)
353 /* Before spreserve(): use our string pool instead of LibC heap */
357 newcpp
= salloc(sizeof(char*) * newsize
);
360 memcpy(newcpp
, *cpp
, oldcnt
* sizeof(char*));
369 /* Before spreserve(): use our string pool instead of LibC heap */
370 /* XXX further check paths? */
373 struct passwd
*pwuid
, *pw
;
376 tempdir
= ((cp
= env_vlook("TMPDIR", TRU1
)) != NULL
)
377 ? savestr(cp
) : TMPDIR_FALLBACK
;
379 cp
= (myname
== NULL
) ? env_vlook("USER", TRU1
) : myname
;
381 user_id
= uid
= getuid();
382 if ((pwuid
= getpwuid(uid
)) == NULL
)
383 n_panic(_("Cannot associate a name with uid %u"), user_id
);
384 if (cp
== NULL
|| *cp
== '\0')
385 myname
= pwuid
->pw_name
;
386 else if ((pw
= getpwnam(cp
)) == NULL
) {
387 n_alert(_("\"%s\" is not a user of this system"), cp
);
390 myname
= pw
->pw_name
;
391 if (pw
->pw_uid
!= uid
)
392 options
|= OPT_u_FLAG
;
394 myname
= savestr(myname
);
395 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
397 if ((cp
= env_vlook("HOME", TRU1
)) == NULL
)
398 cp
= "."; /* XXX User and Login objects; Login: pw->pw_dir */
399 homedir
= savestr(cp
);
404 _setscreensize(int is_sighdl
) /* TODO global policy; int wraps; minvals! */
409 #elif defined TIOCGSIZE
414 scrnheight
= realscreenheight
= scrnwidth
= 0;
416 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
417 * a little bit to be able to honour resizes during our lifetime and
418 * only honour it upon first run; abuse *is_sighdl* as an indicator */
423 if ((cp
= env_vlook("LINES", FAL0
)) != NULL
&&
424 (i
= strtol(cp
, NULL
, 10)) > 0 && i
< INT_MAX
)
425 scrnheight
= realscreenheight
= (int)i
;
426 if ((cp
= env_vlook("COLUMNS", FAL0
)) != NULL
&&
427 (i
= strtol(cp
, NULL
, 10)) > 0 && i
< INT_MAX
)
430 if (scrnwidth
!= 0 && scrnheight
!= 0)
435 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) == -1)
436 ws
.ws_col
= ws
.ws_row
= 0;
437 #elif defined TIOCGSIZE
438 if (ioctl(STDOUT_FILENO
, TIOCGSIZE
, &ws
) == -1)
439 ts
.ts_lines
= ts
.ts_cols
= 0;
442 if (scrnheight
== 0) {
443 speed_t ospeed
= ((tcgetattr(STDOUT_FILENO
, &tbuf
) == -1)
444 ? B9600
: cfgetospeed(&tbuf
));
448 else if (ospeed
== B1200
)
451 else if (ws
.ws_row
!= 0)
452 scrnheight
= ws
.ws_row
;
453 #elif defined TIOCGSIZE
454 else if (ts
.ts_lines
!= 0)
455 scrnheight
= ts
.ts_lines
;
460 #if defined TIOCGWINSZ || defined TIOCGSIZE
463 (realscreenheight
= ws
.ws_row
)
465 (realscreenheight
= ts
.ts_lines
)
468 realscreenheight
= 24;
472 if (scrnwidth
== 0 && 0 ==
474 (scrnwidth
= ws
.ws_col
)
475 #elif defined TIOCGSIZE
476 (scrnwidth
= ts
.ts_cols
)
483 if (is_sighdl
&& IS_TTY_SESSION())
484 tty_signal(SIGWINCH
);
489 static sigjmp_buf __hdrjmp
; /* XXX */
492 _rcv_mode(char const *folder
, char const *Larg
, struct X_arg
*xhp
)
496 sighandler_type prevint
;
501 else if (*folder
== '@') {
502 /* This must be treated specially to make possible invocation like
503 * -A imap -f @mailbox */
504 if ((cp
= ok_vlook(folder
)) != NULL
&& which_protocol(cp
) == PROTO_IMAP
)
505 n_strlcpy(mailname
, cp
, PATH_MAX
);
508 if (options
& OPT_QUICKRUN_MASK
)
512 i
= setfile(folder
, i
);
514 exit_status
= EXIT_ERR
; /* error already reported */
517 if (options
& OPT_QUICKRUN_MASK
) {
519 if (!(options
& OPT_EXISTONLY
) && i
== EXIT_OK
)
520 print_header_summary(Larg
);
523 check_folder_hook(FAL0
);
525 if (i
> 0 && !ok_blook(emptystart
)) {
526 exit_status
= EXIT_ERR
;
530 if (sigsetjmp(__hdrjmp
, 1) == 0) {
531 if ((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
532 safe_signal(SIGINT
, _hdrstop
);
533 if (!(options
& OPT_N_FLAG
)) {
534 if (!ok_blook(quiet
))
535 printf(_("%s version %s. Type ? for help.\n"),
536 (ok_blook(bsdcompat
) ? "Mail" : uagent
), ok_vlook(version
));
540 safe_signal(SIGINT
, prevint
);
543 /* Enter the command loop */
544 if (xhp
== NULL
|| _X_arg_eval(xhp
)) {
545 if (options
& OPT_INTERACTIVE
)
548 if (options
& OPT_INTERACTIVE
)
552 if (mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
) {
553 safe_signal(SIGHUP
, SIG_IGN
);
554 safe_signal(SIGINT
, SIG_IGN
);
555 safe_signal(SIGQUIT
, SIG_IGN
);
558 save_mbox_for_possible_quitstuff();
568 NYD_X
; /* Signal handler */
572 n_err_sighdl(_("\nInterrupt\n"));
573 siglongjmp(__hdrjmp
, 1);
577 _X_arg_eval(struct X_arg
*xhp
) /* TODO error handling not right */
584 while ((xp
= xhp
) != NULL
) {
587 memset(&ev
, 0, sizeof ev
);
588 ev
.ev_line
.s
= xp
->xa_cmd_buf
;
589 ev
.ev_line
.l
= xp
->xa_cmd_len
;
590 ev
.ev_is_recursive
= TRU1
;
591 pstate
&= ~PS_HOOK_MASK
;
592 rv
= (evaluate(&ev
) == 0);
596 if (exit_status
== EXIT_OK
)
597 exit_status
= EXIT_ERR
;
600 if ((options
& OPT_BATCH_FLAG
) && ok_blook(batch_exit_on_error
)) {
601 if (exit_status
!= EXIT_OK
)
603 if (pstate
& PS_EVAL_ERROR
) {
604 exit_status
= EXIT_ERR
;
614 main(int argc
, char *argv
[])
616 static char const optstr
[] = "A:a:Bb:c:DdEeFfHhiL:NnO:q:Rr:S:s:tu:VvX:~#.",
620 " %s [-BDdEFintv~] [-A account]\n"
621 "\t [-a attachment] [-b bcc-address] [-c cc-address]\n"
622 "\t [-q file] [-r from-address] [-S var[=value]...]\n"
623 "\t [-s subject] [-X cmd] [-.] to-address... [-- mta-option...]\n"
624 " %s [-BDdEeHiNnRv~#] [-A account]\n"
625 "\t [-L spec-list] [-r from-address] [-S var[=value]...]\n"
626 "\t [-X cmd] -f [file] [-- mta-option...]\n"
627 " %s [-BDdEeHiNnRv~#] [-A account]\n"
628 "\t [-L spec-list] [-r from-address] [-S var[=value]...]\n"
629 "\t [-u user] [-X cmd] [-- mta-option...]\n"
631 #define _USAGE_ARGS , progname, progname, progname, progname
633 struct X_arg
*X_head
= NULL
, *X_curr
= /* silence CC */ NULL
;
634 struct a_arg
*a_head
= NULL
, *a_curr
= /* silence CC */ NULL
;
635 struct name
*to
= NULL
, *cc
= NULL
, *bcc
= NULL
;
636 struct attachment
*attach
= NULL
;
637 char *cp
= NULL
, *subject
= NULL
, *qf
= NULL
, *Aarg
= NULL
, *Larg
= NULL
;
638 char const *okey
, **oargs
= NULL
, *folder
= NULL
, *emsg
= NULL
;
639 size_t oargs_size
= 0, oargs_count
= 0, smopts_size
= 0;
644 * Start our lengthy setup, finalize by setting PS_STARTED
650 /* Command line parsing
651 * Variable settings need to be done twice, since the user surely wants the
652 * setting to take effect immediately, but also doesn't want it to be
653 * overwritten from within resource files */
654 while ((i
= _getopt(argc
, argv
, optstr
)) >= 0) {
657 /* Execute an account command later on */
661 options
|= OPT_SENDMODE
;
662 { struct a_arg
*nap
= salloc(sizeof(struct a_arg
));
666 a_curr
->aa_next
= nap
;
668 nap
->aa_file
= _oarg
;
673 /* Make 0/1 line buffered */
674 setvbuf(stdin
, NULL
, _IOLBF
, 0);
675 setvbuf(stdout
, NULL
, _IOLBF
, 0);
678 /* Get Blind Carbon Copy Recipient list */
679 options
|= OPT_SENDMODE
;
680 bcc
= cat(bcc
, lextract(_oarg
, GBCC
| GFULL
));
683 /* Get Carbon Copy Recipient list */
684 options
|= OPT_SENDMODE
;
685 cc
= cat(cc
, lextract(_oarg
, GCC
| GFULL
));
689 ok_bset(disconnected
, TRU1
);
690 okey
= "disconnected";
696 ok_bset(debug
, TRU1
);
700 ok_bset(skipemptybody
, TRU1
);
701 okey
= "skipemptybody";
704 options
|= OPT_EXISTONLY
;
707 options
|= OPT_F_FLAG
| OPT_SENDMODE
;
710 /* User is specifying file to "edit" with Mail, as opposed to reading
711 * system mailbox. If no argument is given, we read his mbox file.
712 * Check for remaining arguments later */
716 options
|= OPT_HEADERSONLY
;
719 n_err(V_(usagestr
) _USAGE_ARGS
);
722 /* Ignore interrupts */
723 ok_bset(ignore
, TRU1
);
728 options
|= OPT_HEADERLIST
;
729 if (*Larg
== '"' || *Larg
== '\'') { /* TODO list.c:listspec_check() */
730 size_t j
= strlen(++Larg
);
736 /* Avoid initial header printing */
737 ok_bset(header
, FAL0
);
741 /* Don't source "unspecified system start-up file" */
742 options
|= OPT_NOSRC
;
745 /* Additional options to pass-through to MTA TODO v15-compat legacy */
746 if (smopts_count
== (size_t)smopts_size
)
747 smopts_size
= _grow_cpp(&smopts
, smopts_size
+ 8, smopts_count
);
748 smopts
[smopts_count
++] = _oarg
;
751 /* Quote file TODO drop? -Q with real quote?? what ? */
752 options
|= OPT_SENDMODE
;
757 /* Open folders read-only */
758 options
|= OPT_R_FLAG
;
761 /* Set From address. */
762 options
|= OPT_r_FLAG
;
763 if (_oarg
[0] != '\0') {
764 struct name
*fa
= nalloc(_oarg
, GSKIN
| GFULL
| GFULLEXTRA
);
766 if (is_addr_invalid(fa
, EACM_STRICT
| EACM_NOLOG
)) {
767 emsg
= N_("Invalid address argument with -r");
771 /* TODO -r options is set in smopts, but may
772 * TODO be overwritten by setting from= in
773 * TODO an interactive session!
774 * TODO Maybe disable setting of from?
775 * TODO Warn user? Update manual!! */
776 okey
= savecat("from=", fa
->n_fullname
);
781 /* Set variable (twice) */
788 if (oargs_count
== oargs_size
)
789 oargs_size
= _grow_cpp(&oargs
, oargs_size
+ 8, oargs_count
);
790 oargs
[oargs_count
++] = okey
;
795 options
|= OPT_SENDMODE
;
798 /* Read defined set of headers from mail to be send */
799 options
|= OPT_SENDMODE
| OPT_t_FLAG
;
802 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
803 * done as necessary in _setup_vars() above */
807 puts(ok_vlook(version
));
812 ok_bset(verbose
, TRU1
);
816 size_t l
= strlen(_oarg
);
817 struct X_arg
*nxp
= smalloc(sizeof(*nxp
) -
818 VFIELD_SIZEOF(struct X_arg
, xa_cmd_buf
) + l
+1);
822 X_curr
->xa_next
= nxp
;
826 memcpy(nxp
->xa_cmd_buf
, _oarg
, l
+1);
830 /* Enable tilde escapes even in non-interactive mode */
831 options
|= OPT_TILDE_FLAG
;
834 /* Work in batch mode, even if non-interactive */
835 if (oargs_count
+ 6 >= oargs_size
)
836 oargs_size
= _grow_cpp(&oargs
, oargs_size
+ 8, oargs_count
);
837 /* xxx Setting most of the -# options immediately is useless, so be
838 * selective in what is set immediately */
839 options
|= OPT_TILDE_FLAG
| OPT_BATCH_FLAG
;
840 folder
= "/dev/null";
842 ok_bset(emptystart
, TRU1
);
843 ok_bset(header
, FAL0
);
844 ok_bset(quiet
, TRU1
);
845 ok_bset(sendwait
, TRU1
);
846 ok_vset(MBOX
, folder
);
847 oargs
[oargs_count
+ 0] = "dot";
848 oargs
[oargs_count
+ 1] = "emptystart";
849 oargs
[oargs_count
+ 2] = "noheader";
850 oargs
[oargs_count
+ 3] = "quiet";
851 oargs
[oargs_count
+ 4] = "sendwait";
852 oargs
[oargs_count
+ 5] = "MBOX=/dev/null";
856 options
|= OPT_SENDMODE
;
861 n_err("%s\n", V_(emsg
));
862 n_err(V_(usagestr
) _USAGE_ARGS
);
864 exit_status
= EXIT_USE
;
871 /* The normal arguments may be followed by MTA arguments after a "--";
872 * however, -f may take off an argument, too, and before that.
873 * Since MTA arguments after "--" require *expandargv*, delay parsing off
874 * those options until after the resource files are loaded... */
875 if ((cp
= argv
[i
= _oind
]) == NULL
)
877 else if (cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0')
879 /* OPT_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
880 else if (folder
!= NULL
&& folder
[1] == '\0') {
882 if ((cp
= argv
[++i
]) != NULL
) {
883 if (cp
[0] != '-' || cp
[1] != '-' || cp
[2] != '\0') {
884 emsg
= N_("More than one file given with -f");
890 options
|= OPT_SENDMODE
;
892 to
= cat(to
, lextract(cp
, GTO
| GFULL
));
893 if ((cp
= argv
[++i
]) == NULL
)
895 if (cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0') {
903 /* ...BUT, since we use salloc() for the MTA smopts storage we need to
904 * allocate the necessary space for them before we call spreserve()! */
905 while (argv
[i
] != NULL
)
907 if (smopts_count
+ i
> smopts_size
)
908 DBG(smopts_size
=) _grow_cpp(&smopts
, smopts_count
+ i
+ 1, smopts_count
);
910 /* Check for inconsistent arguments */
911 if (options
& OPT_SENDMODE
) {
912 if (folder
!= NULL
&& !(options
& OPT_BATCH_FLAG
)) {
913 emsg
= N_("Cannot give -f and people to send to.");
916 if (myname
!= NULL
) {
917 emsg
= N_("The -u option cannot be used in send mode");
920 if (!(options
& OPT_t_FLAG
) && to
== NULL
) {
921 emsg
= N_("Send options without primary recipient specified.");
924 if (options
& OPT_EXISTONLY
) {
925 emsg
= N_("The -e option cannot be used in send mode.");
928 if (options
& (OPT_HEADERSONLY
| OPT_HEADERLIST
)) {
929 emsg
= N_("The -H and -L options cannot be used in send mode.");
932 if (options
& OPT_R_FLAG
) {
933 emsg
= N_("The -R option is meaningless in send mode.");
937 if (folder
!= NULL
&& myname
!= NULL
) {
938 emsg
= N_("The options -f and -u are mutually exclusive");
941 if ((options
& OPT_EXISTONLY
) &&
942 (options
& (OPT_HEADERSONLY
| OPT_HEADERLIST
))) {
943 emsg
= N_("The option -e is mutual exclusive with -H and -L");
949 * Likely to go, perform more setup
954 if (options
& OPT_INTERACTIVE
) {
957 # ifndef TTY_WANTS_SIGWINCH
958 if (safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
960 safe_signal(SIGWINCH
, _setscreensize
);
963 scrnheight
= realscreenheight
= 24, scrnwidth
= 80;
965 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
968 if (!(options
& OPT_NOSRC
) && !env_blook("NAIL_NO_SYSTEM_RC", TRU1
))
970 /* *expand() returns a savestr(), but load only uses the file name for
971 * fopen(), so it's safe to do this */
972 if ((cp
= env_vlook("MAILRC", TRU1
)) == NULL
)
973 cp
= UNCONST(MAILRC
);
974 load(file_expand(cp
));
975 if (env_vlook("NAIL_EXTRA_RC", TRU1
) == NULL
&&
976 (cp
= ok_vlook(NAIL_EXTRA_RC
)) != NULL
)
977 load(file_expand(cp
));
979 /* We had to wait until the resource files are loaded but it is time to get
980 * the termcap going, so that *term-ca-mode* won't hide our output for us */
982 if ((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
)
983 termcap_init(); /* TODO program state machine */
986 /* Now we can set the account */
994 /* Ensure the -S and other command line options take precedence over
995 * anything that may have been placed in resource files.
996 * Our "ternary binary" option *verbose* needs special treament */
997 if ((options
& (OPT_VERB
| OPT_VERBVERB
)) == OPT_VERB
)
998 options
&= ~OPT_VERB
;
999 for (i
= 0; UICMP(z
, i
, <, oargs_count
); ++i
) {
1006 /* Additional options to pass-through to MTA, and allowed to do so? */
1007 if ((cp
= ok_vlook(expandargv
)) != NULL
) {
1008 bool_t isfail
= !asccasecmp(cp
, "fail"),
1009 isrestrict
= (!isfail
&& !asccasecmp(cp
, "restrict"));
1011 if ((cp
= argv
[i
= _oind
]) != NULL
) {
1013 (isrestrict
&& !(options
& (OPT_INTERACTIVE
|OPT_TILDE_FLAG
)))) {
1014 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
1015 "using *sendmail-arguments*\n"));
1016 exit_status
= EXIT_USE
| EXIT_SEND_ERROR
;
1020 assert(smopts_count
+ 1 <= smopts_size
);
1021 smopts
[smopts_count
++] = cp
;
1022 } while ((cp
= argv
[++i
]) != NULL
);
1027 * We're finally completely setup and ready to go
1030 pstate
|= PS_STARTED
;
1032 if (options
& OPT_DEBUG
)
1033 n_err(_("user = %s, homedir = %s\n"), myname
, homedir
);
1035 if (!(options
& OPT_SENDMODE
)) {
1036 exit_status
= _rcv_mode(folder
, Larg
, X_head
);
1040 /* xxx exit_status = EXIT_OK; */
1041 if (X_head
!= NULL
&& !_X_arg_eval(X_head
))
1044 /* Now that full mailx(1)-style file expansion is possible handle the
1045 * attachments which we had delayed due to this.
1046 * This may use savestr(), but since we won't enter the command loop we
1047 * don't need to care about that */
1048 for (cp
= NULL
; a_head
!= NULL
;) {
1049 struct attachment
*nahp
, *nap
;
1051 if ((nahp
= add_attachment(attach
, a_head
->aa_file
, &nap
)) != NULL
) {
1053 /* Did we split a charset set name for fixation purposes? */
1055 nap
->a_conv
= AC_FIX_INCS
;
1056 nap
->a_input_charset
= cp
;
1059 a_head
= a_head
->aa_next
;
1064 /* It may not have worked because of an appended character set name, so
1065 * try to split name and charset and retry once */
1066 if (cp
== NULL
&& (cp
= strrchr(a_head
->aa_file
, '=')) != NULL
) {
1067 char *ncp
, *nfp
= savestrbuf(a_head
->aa_file
,
1068 PTR2SIZE(cp
- a_head
->aa_file
));
1070 for (ncp
= ++cp
; *ncp
!= '\0'; ++ncp
)
1071 if (!alnumchar(*ncp
) && !punctchar(*ncp
))
1074 a_head
->aa_file
= nfp
;
1079 n_perr(a_head
->aa_file
, i
);
1080 exit_status
= EXIT_ERR
;
1084 if (options
& OPT_INTERACTIVE
)
1086 mail(to
, cc
, bcc
, subject
, attach
, qf
, ((options
& OPT_F_FLAG
) != 0));
1087 if (options
& OPT_INTERACTIVE
)
1092 if (options
& OPT_INTERACTIVE
)
1104 c_rexit(void *v
) /* TODO program state machine */
1109 if (!(pstate
& PS_SOURCING
)) {
1111 if (options
& OPT_INTERACTIVE
)
1120 /* Source the others in that case! */
1121 #ifdef HAVE_AMALGAMATION
1122 # include "config.h"