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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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>
54 struct a_arg
*aa_next
;
58 /* (extern, but not with amalgamation, so define here) */
59 VL
char const n_weekday_names
[7 + 1][4] = {
60 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
62 VL
char const n_month_names
[12 + 1][4] = {
63 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
64 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
66 VL
char const n_uagent
[sizeof VAL_UAGENT
] = VAL_UAGENT
;
67 VL
char const n_error
[sizeof n_ERROR
] = N_(n_ERROR
);
68 VL
char const n_unirepl
[sizeof n_UNIREPL
] = n_UNIREPL
;
69 VL
char const n_empty
[1] = "";
70 VL
char const n_0
[2] = "0";
71 VL
char const n_1
[2] = "1";
72 VL
char const n_m1
[3] = "-1";
73 VL ui16_t
const n_class_char
[1 + 0x7F] = {
74 #define a_BC C_BLANK | C_CNTRL
75 #define a_SC C_SPACE | C_CNTRL
76 #define a_WC C_WHITE | C_CNTRL
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
, a_BC
, a_WC
, a_SC
, a_SC
, a_SC
, 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
,
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 /* Our own little getopt(3) */
115 static char const *a_main_oarg
;
116 static int a_main_oind
, /*_oerr,*/ a_main_oopt
;
118 /* Our own little getopt(3); note --help is special-treated as 'h' */
119 static int a_main_getopt(int argc
, char * const argv
[], char const *optstring
);
121 /* Perform basic startup initialization */
122 static void a_main_startup(void);
125 static size_t a_main_grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
);
127 /* Setup some variables which we require to be valid / verified */
128 static void a_main_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 a_main_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 a_main_rcv_mode(char const *folder
, char const *Larg
);
143 /* Interrupt printing of the headers */
144 static void a_main_hdrstop(int signo
);
147 a_main_getopt(int argc
, char * const argv
[], char const *optstring
){
148 static char const *lastp
;
157 /*if((colon = (optstring[0] == ':')))
164 if(a_main_oind
>= argc
|| argv
[a_main_oind
] == NULL
||
165 argv
[a_main_oind
][0] != '-' || argv
[a_main_oind
][1] == '\0')
167 if(argv
[a_main_oind
][1] == '-' && argv
[a_main_oind
][2] == '\0'){
168 /* We need this in for MTA arg detection (easier) ++a_main_oind;*/
171 curp
= &argv
[a_main_oind
][1];
174 for(a_main_oopt
= curp
[0]; optstring
[0] != '\0';){
175 if(optstring
[0] != a_main_oopt
){
176 optstring
+= 1 + (optstring
[1] == ':');
180 if(optstring
[1] == ':'){
182 a_main_oarg
= n_UNCONST(curp
+ 1);
185 if((a_main_oind
+= 2) > argc
){
186 /*if(!colon *//*&& _oerr*//*)*/{
187 n_err(_("%s: option requires an argument -- %c\n"),
188 argv
[0], (char)a_main_oopt
);
190 rv
= (/*colon ? ':' :*/ '?');
193 a_main_oarg
= argv
[a_main_oind
- 1];
205 /* Special support for --help, which is quite common */
206 if(a_main_oopt
== '-' && !strcmp(curp
, "-help") &&
207 &curp
[-1] == argv
[a_main_oind
]){
213 /* Definitive error */
214 /*if(!colon *//*&& opterr*//*)*/
215 n_err(_("%s: invalid option -- %c\n"), argv
[0], a_main_oopt
);
228 a_main_startup(void){
237 a_main_oind
= /*_oerr =*/ 1;
239 if((cp
= strrchr(n_progname
, '/')) != NULL
)
243 safe_signal(SIGABRT
, &_nyd_oncrash
);
245 safe_signal(SIGBUS
, &_nyd_oncrash
);
247 safe_signal(SIGFPE
, &_nyd_oncrash
);
248 safe_signal(SIGILL
, &_nyd_oncrash
);
249 safe_signal(SIGSEGV
, &_nyd_oncrash
);
252 /* Initialize our input, loop and command machinery */
255 /* Set up a reasonable environment */
257 /* TODO This is wrong: interactive is STDIN/STDERR for a POSIX sh(1).
258 * TODO For now we get this wrong, all over the place, as this software
259 * TODO has always been developed with stdout as an output channel.
260 * TODO Start doing it right for at least explicit terminal-related things,
261 * TODO but v15 should use ONLY this, also for terminal input! */
262 if(isatty(STDIN_FILENO
)){
263 n_psonce
|= n_PSO_TTYIN
;
264 #if defined HAVE_MLE || defined HAVE_TERMCAP
265 n_tty_fp
= fdopen(fileno(n_stdin
), "w");
266 setvbuf(n_tty_fp
, NULL
, _IOLBF
, 0);
269 if(isatty(STDOUT_FILENO
))
270 n_psonce
|= n_PSO_TTYOUT
;
271 if((n_psonce
& (n_PSO_TTYIN
| n_PSO_TTYOUT
)) ==
272 (n_PSO_TTYIN
| n_PSO_TTYOUT
)){
273 n_psonce
|= n_PSO_INTERACTIVE
;
274 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
277 /* STDOUT is always line buffered from our point of view */
278 setvbuf(n_stdout
, NULL
, _IOLBF
, 0);
284 #ifndef HAVE_SETLOCALE
287 setlocale(LC_ALL
, n_empty
);
288 n_mb_cur_max
= MB_CUR_MAX
;
289 # ifdef HAVE_NL_LANGINFO
290 if((cp
= nl_langinfo(CODESET
)) != NULL
)
291 ok_vset(ttycharset
, cp
);
294 # ifdef HAVE_C90AMEND1
295 if(n_mb_cur_max
> 1){
296 # ifdef HAVE_ALWAYS_UNICODE_LOCALE
297 n_psonce
|= n_PSO_UNICODE
;
300 if(mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
301 mbtowc(&wc
, "\342\202\254", 3) == 3 && wc
== 0x20AC)
302 n_psonce
|= n_PSO_UNICODE
;
303 /* Reset possibly messed up state; luckily this also gives us an
304 * indication whether the encoding has locking shift state sequences */
305 if(mbtowc(&wc
, NULL
, n_mb_cur_max
))
306 n_psonce
|= n_PSO_ENC_MBSTATE
;
313 iconvd
= (iconv_t
)-1;
319 a_main_grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
){
320 /* Just use auto-reclaimed storage, it will be preserved */
324 newcpp
= salloc(sizeof(char*) * newsize
);
327 memcpy(newcpp
, *cpp
, oldcnt
* sizeof(char*));
334 a_main_setup_vars(void){
335 struct passwd
*pwuid
;
339 n_group_id
= getgid();
340 if((pwuid
= getpwuid(n_user_id
= getuid())) == NULL
)
341 n_panic(_("Cannot associate a name with uid %lu"), (ul_i
)n_user_id
);
347 if(!(doenv
= (ep
= ok_vlook(LOGNAME
)) == NULL
) &&
348 (doenv
= strcmp(pwuid
->pw_name
, ep
)))
349 n_err(_("Warning: $LOGNAME (%s) not identical to user (%s)!\n"),
352 n_pstate
|= n_PS_ROOT
;
353 ok_vset(LOGNAME
, pwuid
->pw_name
);
354 n_pstate
&= ~n_PS_ROOT
;
357 if((ep
= ok_vlook(USER
)) != NULL
&& strcmp(pwuid
->pw_name
, ep
)){
358 n_err(_("Warning: $USER (%s) not identical to user (%s)!\n"),
360 n_pstate
|= n_PS_ROOT
;
361 ok_vset(USER
, pwuid
->pw_name
);
362 n_pstate
&= ~n_PS_ROOT
;
366 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
368 /* Ensure some variables get loaded and/or verified */
370 /* This is not automatized just as $TMPDIR is for the initial setting, since
371 * we have the pwuid at hand and can simply use it! See accmacvar.c! */
372 if((cp
= ok_vlook(HOME
)) == NULL
){
374 n_pstate
|= n_PS_ROOT
;
376 n_pstate
&= ~n_PS_ROOT
;
379 (void)ok_vlook(TMPDIR
);
380 (void)ok_blook(POSIXLY_CORRECT
);
385 a_main_setscreensize(int is_sighdl
){/* TODO globl policy; int wraps; minvals! */
389 #elif defined TIOCGSIZE
394 n_scrnheight
= n_realscreenheight
= n_scrnwidth
= 0;
396 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
397 * a little bit to be able to honour resizes during our lifetime and
398 * only honour it upon first run; abuse *is_sighdl* as an indicator */
402 /* We manage those variables for our child processes, so ensure they
403 * are up to date, always */
404 assert(n_psonce
& n_PSO_INTERACTIVE
);
405 n_pstate
|= n_PS_SIGWINCH_PEND
;
407 if((cp
= ok_vlook(LINES
)) != NULL
){
408 n_idec_ui32_cp(&n_scrnheight
, cp
, 0, NULL
);
409 n_realscreenheight
= n_scrnheight
;
411 if((cp
= ok_vlook(COLUMNS
)) != NULL
)
412 n_idec_ui32_cp(&n_scrnwidth
, cp
, 0, NULL
);
414 if(n_scrnwidth
!= 0 && n_scrnheight
!= 0)
419 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) == -1)
420 ws
.ws_col
= ws
.ws_row
= 0;
421 #elif defined TIOCGSIZE
422 if(ioctl(STDOUT_FILENO
, TIOCGSIZE
, &ws
) == -1)
423 ts
.ts_lines
= ts
.ts_cols
= 0;
426 if(n_scrnheight
== 0){
429 ospeed
= ((tcgetattr(fileno(n_tty_fp
), &tbuf
) == -1)
430 ? B9600
: cfgetospeed(&tbuf
));
434 else if(ospeed
== B1200
)
437 else if(ws
.ws_row
!= 0)
438 n_scrnheight
= ws
.ws_row
;
439 #elif defined TIOCGSIZE
440 else if(ts
.ts_lines
!= 0)
441 n_scrnheight
= ts
.ts_lines
;
446 #if defined TIOCGWINSZ || defined TIOCGSIZE
449 (n_realscreenheight
= ws
.ws_row
)
451 (n_realscreenheight
= ts
.ts_lines
)
454 n_realscreenheight
= 24;
458 if(n_scrnwidth
== 0 && 0 ==
460 (n_scrnwidth
= ws
.ws_col
)
461 #elif defined TIOCGSIZE
462 (n_scrnwidth
= ts
.ts_cols
)
470 assert(n_psonce
& n_PSO_INTERACTIVE
);
471 n_pstate
|= n_PS_SIGWINCH_PEND
; /* XXX Not atomic */
472 n_tty_signal(SIGWINCH
);
478 static sigjmp_buf a_main__hdrjmp
; /* XXX */
481 a_main_rcv_mode(char const *folder
, char const *Larg
){
483 sighandler_type prevint
;
489 i
= (n_poption
& n_PO_QUICKRUN_MASK
) ? FEDIT_RDONLY
: FEDIT_NONE
;
490 i
= setfile(folder
, i
);
492 n_exit_status
= n_EXIT_ERR
; /* error already reported */
495 if(n_poption
& n_PO_QUICKRUN_MASK
){
497 if(i
== n_EXIT_OK
&& (!(n_poption
& n_PO_EXISTONLY
) ||
498 (n_poption
& n_PO_HEADERLIST
)))
499 print_header_summary(Larg
);
502 temporary_folder_hook_check(FAL0
);
504 if(i
> 0 && !ok_blook(emptystart
)){
505 n_exit_status
= n_EXIT_ERR
;
509 if(sigsetjmp(a_main__hdrjmp
, 1) == 0){
510 if((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
511 safe_signal(SIGINT
, &a_main_hdrstop
);
512 if(ok_blook(header
)){
514 fprintf(n_stdout
, _("%s version %s. Type `?' for help\n"),
515 (ok_blook(bsdcompat
) ? "Mail" : n_uagent
), ok_vlook(version
));
519 safe_signal(SIGINT
, prevint
);
522 /* Enter the command loop */
523 if(n_psonce
& n_PSO_INTERACTIVE
)
526 if(!(n_psonce
& n_PSO_XIT
)){
527 if(n_psonce
& n_PSO_INTERACTIVE
)
530 if(mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
){
531 safe_signal(SIGHUP
, SIG_IGN
);
532 safe_signal(SIGINT
, SIG_IGN
);
533 safe_signal(SIGQUIT
, SIG_IGN
);
536 save_mbox_for_possible_quitstuff();
541 return n_exit_status
;
545 a_main_hdrstop(int signo
){
546 NYD_X
; /* Signal handler */
550 n_err_sighdl(_("\nInterrupt\n"));
551 siglongjmp(a_main__hdrjmp
, 1);
555 main(int argc
, char *argv
[]){
556 /* Keep in SYNC: ./nail.1:"SYNOPSIS, main() */
558 optstr
[] = "A:a:Bb:c:dEeFfHhiL:M:m:NnO:q:Rr:S:s:tu:VvX:::~#.",
563 " %s [-BdEFintv~] [-: spec] [-A account]\n"
564 "\t [-a attachment] [-b bcc-addr] [-c cc-addr]\n"
565 "\t [-M type | -m file | -q file | -t]\n"
566 "\t [-r from-addr] [-S var[=value]..]\n"
567 "\t [-s subject] [-X cmd] [-.] to-addr.. [-- mta-option..]\n"
569 " %s [-BdEeHiNnRv~] [-: spec] [-A account]\n"
570 "\t [-L spec-list] [-r from-addr] [-S var[=value]..]\n"
571 "\t [-u user] [-X cmd] [-- mta-option..]\n"
573 " %s [-BdEeHiNnRv~#] [-: spec] [-A account] -f\n"
574 "\t [-L spec-list] [-r from-addr] [-S var[=value]..]\n"
575 "\t [-X cmd] [file] [-- mta-option..]\n"
577 #define _USAGE_ARGS , n_progname, n_progname, n_progname, n_progname
586 a_RF_ALL
= a_RF_SYSTEM
| a_RF_USER
588 size_t oargs_size
, oargs_cnt
, Xargs_size
, Xargs_cnt
, smopts_size
;
589 char const *Aarg
, *emsg
, *folder
, *Larg
, *okey
, **oargs
, *qf
,
590 *subject
, *uarg
, **Xargs
;
591 struct attachment
*attach
;
592 struct name
*to
, *cc
, *bcc
;
593 struct a_arg
*a_head
, *a_curr
;
597 n_UNINIT(a_curr
, NULL
);
598 to
= cc
= bcc
= NULL
;
600 Aarg
= emsg
= folder
= Larg
= okey
= qf
= subject
= uarg
= NULL
;
601 oargs
= Xargs
= NULL
;
602 oargs_size
= oargs_cnt
= Xargs_size
= Xargs_cnt
= smopts_size
= 0;
606 * Start our lengthy setup, finalize by setting n_PSO_STARTED
609 n_progname
= argv
[0];
612 /* Command line parsing
613 * Variable settings need to be done twice, since the user surely wants the
614 * setting to take effect immediately, but also doesn't want it to be
615 * overwritten from within resource files */
616 while((i
= a_main_getopt(argc
, argv
, optstr
)) >= 0){
619 /* Execute an account command later on */
625 n_psonce
|= n_PSO_SENDMODE
;
626 nap
= salloc(sizeof(struct a_arg
));
630 a_curr
->aa_next
= nap
;
632 nap
->aa_file
= a_main_oarg
;
636 n_OBSOLETE(_("-B is obsolete, please use -# as necessary"));
639 /* Get Blind Carbon Copy Recipient list */
640 n_psonce
|= n_PSO_SENDMODE
;
641 bcc
= cat(bcc
, lextract(a_main_oarg
, GBCC
| GFULL
));
644 /* Get Carbon Copy Recipient list */
645 n_psonce
|= n_PSO_SENDMODE
;
646 cc
= cat(cc
, lextract(a_main_oarg
, GCC
| GFULL
));
653 n_OBSOLETE(_("-E will be removed, please use \"-Sskipemptybody\""));
654 ok_bset(skipemptybody
);
655 okey
= "skipemptybody";
658 n_poption
|= n_PO_EXISTONLY
;
661 n_poption
|= n_PO_F_FLAG
;
662 n_psonce
|= n_PSO_SENDMODE
;
665 /* User is specifying file to "edit" with Mail, as opposed to reading
666 * system mailbox. If no argument is given, we read his mbox file.
667 * Check for remaining arguments later */
671 n_poption
|= n_PO_HEADERSONLY
;
674 n_err(V_(usagestr
) _USAGE_ARGS
);
677 /* Ignore interrupts */
683 n_poption
|= n_PO_HEADERLIST
;
684 if(*Larg
== '"' || *Larg
== '\''){ /* TODO list.c:listspec_check() */
689 cp
= savestrbuf(Larg
, --j
);
695 if(qf
!= NULL
&& (!(n_poption
& n_PO_Mm_FLAG
) || qf
!= (char*)-1))
697 n_poption_arg_Mm
= a_main_oarg
;
702 if(qf
!= NULL
&& (!(n_poption
& n_PO_Mm_FLAG
) || qf
== (char*)-1))
706 n_poption
|= n_PO_Mm_FLAG
;
707 n_psonce
|= n_PSO_SENDMODE
;
710 /* Avoid initial header printing */
715 /* Don't source "unspecified system start-up file" */
716 if(resfiles
& a_RF_SET
){
717 emsg
= N_("-n cannot be used in conjunction with -:");
720 resfiles
= a_RF_USER
;
723 /* Additional options to pass-through to MTA TODO v15-compat legacy */
724 if(n_smopts_cnt
== smopts_size
)
725 smopts_size
= a_main_grow_cpp(&n_smopts
, smopts_size
+ 8,
727 n_smopts
[n_smopts_cnt
++] = a_main_oarg
;
730 if(qf
!= NULL
&& (n_poption
& n_PO_Mm_FLAG
)){
732 emsg
= N_("Only one of -M, -m or -q may be given");
735 /* Quote file TODO drop? -Q with real quote?? what ? */
736 n_psonce
|= n_PSO_SENDMODE
;
737 /* Allow for now, we have to special check validity of -q- later on! */
738 qf
= (a_main_oarg
[0] == '-' && a_main_oarg
[1] == '\0')
739 ? (char*)-1 : a_main_oarg
;
742 /* Open folders read-only */
743 n_poption
|= n_PO_R_FLAG
;
746 /* Set From address. */
747 n_poption
|= n_PO_r_FLAG
;
748 if(a_main_oarg
[0] != '\0'){
751 fa
= nalloc(a_main_oarg
, GSKIN
| GFULL
| GFULLEXTRA
);
752 if(is_addr_invalid(fa
, EACM_STRICT
| EACM_NOLOG
)){
753 emsg
= N_("Invalid address argument with -r");
756 n_poption_arg_r
= fa
;
757 /* TODO -r options is set in n_smopts, but may
758 * TODO be overwritten by setting from= in
759 * TODO an interactive session!
760 * TODO Maybe disable setting of from?
761 * TODO Warn user? Update manual!! */
762 okey
= savecat("from=", fa
->n_fullname
);
767 /* Set variable (TODO twice [only if successful]) */
771 okey
= a
[0] = a_main_oarg
;
773 n_pstate
|= n_PS_ROBOT
;
775 n_pstate
&= ~n_PS_ROBOT
;
780 if(oargs_cnt
== oargs_size
)
781 oargs_size
= a_main_grow_cpp(&oargs
, oargs_size
+ 8, oargs_cnt
);
782 oargs
[oargs_cnt
++] = okey
;
785 /* Subject:; take care for Debian #419840 and strip any \r and \n */
786 if(n_anyof_cp("\n\r", subject
= a_main_oarg
)){
787 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
788 for(subject
= cp
= savestr(a_main_oarg
); *cp
!= '\0'; ++cp
)
789 if(*cp
== '\n' || *cp
== '\r')
792 n_psonce
|= n_PSO_SENDMODE
;
795 /* Read defined set of headers from mail to be sent */
796 n_poption
|= n_PO_t_FLAG
;
797 n_psonce
|= n_PSO_SENDMODE
;
800 uarg
= savecat("%", a_main_oarg
);
803 fprintf(n_stdout
, _("%s version %s\n"), n_uagent
, ok_vlook(version
));
804 n_exit_status
= n_EXIT_OK
;
812 /* Add to list of commands to exec before entering normal operation */
813 if(Xargs_cnt
== Xargs_size
)
814 Xargs_size
= a_main_grow_cpp(&Xargs
, Xargs_size
+ 8, Xargs_cnt
);
815 Xargs
[Xargs_cnt
++] = a_main_oarg
;
818 /* Control which resource files shall be loaded */
819 if(!(resfiles
& (a_RF_SET
| a_RF_SYSTEM
))){
820 emsg
= N_("-n cannot be used in conjunction with -:");
824 while((i
= *a_main_oarg
++) != '\0')
826 case 'S': case 's': resfiles
|= a_RF_SYSTEM
; break;
827 case 'U': case 'u': resfiles
|= a_RF_USER
; break;
828 case ':': case '/': resfiles
&= ~a_RF_ALL
; break;
830 emsg
= N_("Invalid argument of -:");
835 /* Enable command escapes even in non-interactive mode */
836 n_poption
|= n_PO_TILDE_FLAG
;
839 /* Work in batch mode, even if non-interactive */
840 if(!(n_psonce
& n_PSO_INTERACTIVE
))
841 setvbuf(n_stdin
, NULL
, _IOLBF
, 0);
842 n_poption
|= n_PO_TILDE_FLAG
| n_PO_BATCH_FLAG
;
843 folder
= "/dev/null";
844 if(oargs_cnt
+ 10 >= oargs_size
)
845 oargs_size
= a_main_grow_cpp(&oargs
, oargs_size
+ 11, oargs_cnt
);
847 ok_vset(MAIL
, folder
), oargs
[oargs_cnt
++] = "MAIL=/dev/null";
848 ok_vset(MBOX
, folder
), oargs
[oargs_cnt
++] = "MBOX=/dev/null";
849 ok_bset(emptystart
), oargs
[oargs_cnt
++] = "emptystart";
850 ok_bclear(errexit
), oargs
[oargs_cnt
++] = "noerrexit";
851 ok_bclear(header
), oargs
[oargs_cnt
++] = "noheader";
853 ok_vset(inbox
, folder
), oargs
[oargs_cnt
++] = "inbox=/dev/null";
854 ok_bclear(posix
), oargs
[oargs_cnt
++] = "noposix";
855 ok_bset(quiet
), oargs
[oargs_cnt
++] = "quiet";
856 ok_bset(sendwait
), oargs
[oargs_cnt
++] = "sendwait";
857 ok_bset(typescript_mode
), oargs
[oargs_cnt
++] = "typescript-mode";
861 n_psonce
|= n_PSO_SENDMODE
;
866 n_err("%s\n", V_(emsg
));
867 n_err(V_(usagestr
) _USAGE_ARGS
);
869 n_exit_status
= n_EXIT_USE
;
876 /* The normal arguments may be followed by MTA arguments after a "--";
877 * however, -f may take off an argument, too, and before that.
878 * Since MTA arguments after "--" require *expandargv*, delay parsing off
879 * those options until after the resource files are loaded... */
880 if((cp
= argv
[i
= a_main_oind
]) == NULL
)
882 else if(cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0')
884 /* n_PO_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
885 else if(folder
!= NULL
&& /*folder[0] == '&' &&*/ folder
[1] == '\0'){
887 if((cp
= argv
[++i
]) != NULL
){
888 if(cp
[0] != '-' || cp
[1] != '-' || cp
[2] != '\0'){
889 emsg
= N_("More than one file given with -f");
895 n_psonce
|= n_PSO_SENDMODE
;
897 to
= cat(to
, lextract(cp
, GTO
| GFULL
));
898 if((cp
= argv
[++i
]) == NULL
)
900 if(cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0'){
908 /* ...BUT, since we use salloc() for the MTA n_smopts storage we need to
909 * allocate the necessary space for them before we fixate that storage! */
910 while(argv
[i
] != NULL
)
912 if(n_smopts_cnt
+ i
> smopts_size
)
914 a_main_grow_cpp(&n_smopts
, n_smopts_cnt
+ i
+ 1, n_smopts_cnt
);
916 /* Check for inconsistent arguments, fix some temporaries */
917 if(n_psonce
& n_PSO_SENDMODE
){
918 /* XXX This is only because BATCH_FLAG sets *folder*=/dev/null
919 * XXX in order to function. Ideally that would not be needed */
920 if(folder
!= NULL
&& !(n_poption
& n_PO_BATCH_FLAG
)){
921 emsg
= N_("Cannot give -f and people to send to.");
925 emsg
= N_("The -u option cannot be used in send mode");
928 if(!(n_poption
& n_PO_t_FLAG
) && to
== NULL
){
929 emsg
= N_("Send options without primary recipient specified.");
932 if((n_poption
& n_PO_t_FLAG
) && qf
!= NULL
){
933 emsg
= N_("The -M, -m, -q and -t options are mutual exclusive.");
936 if(n_poption
& (n_PO_EXISTONLY
| n_PO_HEADERSONLY
| n_PO_HEADERLIST
)){
937 emsg
= N_("The -e, -H and -L options cannot be used in send mode.");
940 if(n_poption
& n_PO_R_FLAG
){
941 emsg
= N_("The -R option is meaningless in send mode.");
945 if(n_psonce
& n_PSO_INTERACTIVE
){
947 if(!(n_poption
& n_PO_Mm_FLAG
))
948 emsg
= N_("-q can't use standard input when interactive.\n");
953 if(uarg
!= NULL
&& folder
!= NULL
){
954 emsg
= N_("The options -u and -f (and -#) are mutually exclusive");
957 if((n_poption
& (n_PO_EXISTONLY
| n_PO_HEADERSONLY
)) ==
958 (n_PO_EXISTONLY
| n_PO_HEADERSONLY
)){
959 emsg
= N_("The options -e and -H are mutual exclusive");
962 if((n_poption
& (n_PO_HEADERSONLY
| n_PO_HEADERLIST
) /* TODO OBSOLETE */
963 ) == (n_PO_HEADERSONLY
| n_PO_HEADERLIST
))
964 n_OBSOLETE(_("please use \"-e -L xy\" instead of \"-H -L xy\""));
971 * Likely to go, perform more setup
976 if(n_psonce
& n_PSO_INTERACTIVE
){
977 a_main_setscreensize(0);
979 # ifndef TTY_WANTS_SIGWINCH
980 if(safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
982 safe_signal(SIGWINCH
, &a_main_setscreensize
);
985 n_scrnheight
= n_realscreenheight
= 24, n_scrnwidth
= 80;
987 /* Fixate the current memory pool snapshot.
988 * Memory is auto-reclaimed from now on */
989 n_memory_pool_fixate();
991 /* load() any resource files */
992 if(resfiles
& a_RF_ALL
){
993 /* *expand() returns a savestr(), but load only uses the file name for
994 * fopen(), so it's safe to do this */
995 if(resfiles
& a_RF_SYSTEM
){
998 if((nload
= ok_blook(NAIL_NO_SYSTEM_RC
)))
999 n_OBSOLETE(_("Please use $MAILX_NO_SYSTEM_RC instead of "
1000 "$NAIL_NO_SYSTEM_RC"));
1001 if(!nload
&& !ok_blook(MAILX_NO_SYSTEM_RC
)){
1002 n_go_load(VAL_SYSCONFDIR
"/" VAL_SYSCONFRC
);
1003 if(n_psonce
& n_PSO_EXIT_MASK
)
1008 if(resfiles
& a_RF_USER
){
1009 n_go_load(fexpand(ok_vlook(MAILRC
), FEXP_LOCAL
| FEXP_NOPROTO
));
1010 if(n_psonce
& n_PSO_EXIT_MASK
)
1014 if((cp
= ok_vlook(NAIL_EXTRA_RC
)) != NULL
)
1015 n_OBSOLETE(_("Please use *mailx-extra-rc*, not *NAIL_EXTRA_RC*"));
1016 if(cp
!= NULL
|| (cp
= ok_vlook(mailx_extra_rc
)) != NULL
){
1017 n_go_load(fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
));
1018 if(n_psonce
& n_PSO_EXIT_MASK
)
1023 /* Ensure the -S and other command line options take precedence over
1024 * anything that may have been placed in resource files.
1025 * Our "ternary binary" option *verbose* needs special treament */
1026 if((n_poption
& (n_PO_VERB
| n_PO_VERBVERB
)) == n_PO_VERB
)
1027 n_poption
&= ~n_PO_VERB
;
1028 /* ..and be silent when unsetting an undefined variable */
1029 n_pstate
|= n_PS_ROBOT
;
1030 for(i
= 0; UICMP(z
, i
, <, oargs_cnt
); ++i
){
1037 n_pstate
&= ~n_PS_ROBOT
;
1039 /* Cause possible umask(2), now that any setting is established, and before
1040 * we change accounts, evaluate commands etc. */
1041 (void)ok_vlook(umask
);
1043 /* Additional options to pass-through to MTA, and allowed to do so? */
1044 if((cp
= ok_vlook(expandargv
)) != NULL
){
1045 bool_t isfail
, isrestrict
;
1047 isfail
= !asccasecmp(cp
, "fail");
1048 isrestrict
= (!isfail
&& !asccasecmp(cp
, "restrict"));
1050 if((n_poption
& n_PO_D_V
) && !isfail
&& !isrestrict
&& *cp
!= '\0')
1051 n_err(_("Unknown *expandargv* value: %s\n"), cp
);
1053 if((cp
= argv
[i
= a_main_oind
]) != NULL
){
1054 if(isfail
|| (isrestrict
&& (!(n_poption
& n_PO_TILDE_FLAG
) ||
1055 !(n_psonce
& n_PSO_INTERACTIVE
)))){
1056 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
1057 "using *sendmail-arguments*\n"));
1058 n_exit_status
= n_EXIT_USE
| n_EXIT_SEND_ERROR
;
1062 assert(n_smopts_cnt
+ 1 <= smopts_size
);
1063 n_smopts
[n_smopts_cnt
++] = cp
;
1064 }while((cp
= argv
[++i
]) != NULL
);
1068 /* We had to wait until the resource files are loaded and any command line
1069 * setting has been restored, but get the termcap up and going before we
1070 * switch account or running commands */
1072 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_QUICKRUN_MASK
))
1076 /* Now we can set the account */
1085 /* "load()" commands given on command line */
1087 n_go_Xargs(Xargs
, Xargs_cnt
);
1088 if(n_psonce
& n_PSO_EXIT_MASK
)
1093 if(n_poption
& n_PO_Mm_FLAG
){
1094 if(qf
== (char*)-1){
1095 if(!mime_type_check_mtname(n_poption_arg_Mm
)){
1096 n_err(_("Could not find `mimetype' for -M argument: %s\n"),
1098 n_exit_status
= n_EXIT_ERR
;
1101 }else if((n_poption_arg_Mm
= mime_type_classify_filename(qf
)) == NULL
){
1102 n_err(_("Could not `mimetype'-classify -m argument: %s\n"),
1103 n_shexp_quote_cp(qf
, FAL0
));
1104 n_exit_status
= n_EXIT_ERR
;
1106 }else if(!asccasecmp(n_poption_arg_Mm
, "text/plain")) /* TODO no: magic */
1107 n_poption_arg_Mm
= NULL
;
1111 * We're finally completely setup and ready to go
1113 n_psonce
|= n_PSO_STARTED
;
1115 if(!(n_psonce
& n_PSO_SENDMODE
)){
1116 n_exit_status
= a_main_rcv_mode(folder
, Larg
);
1120 /* Now that full mailx(1)-style file expansion is possible handle the
1121 * attachments which we had delayed due to this.
1122 * This may use savestr(), but since we won't enter the command loop we
1123 * don't need to care about that */
1124 for(; a_head
!= NULL
; a_head
= a_head
->aa_next
){
1125 enum n_attach_error aerr
;
1127 attach
= n_attachment_append(attach
, a_head
->aa_file
, &aerr
, NULL
);
1128 if(aerr
!= n_ATTACH_ERR_NONE
){
1129 n_exit_status
= n_EXIT_ERR
;
1134 if(n_psonce
& n_PSO_INTERACTIVE
)
1136 mail(to
, cc
, bcc
, subject
, attach
, qf
, ((n_poption
& n_PO_F_FLAG
) != 0));
1137 if((n_psonce
& (n_PSO_XIT
| n_PSO_INTERACTIVE
)) == n_PSO_INTERACTIVE
)
1141 /* Be aware of identical code for `exit' command! */
1143 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_QUICKRUN_MASK
))
1144 n_termcap_destroy();
1148 #ifdef HAVE_MEMORY_DEBUG
1149 n_memory_pool_pop(NULL
);
1151 #if (defined HAVE_DEBUG || defined HAVE_DEVEL)
1155 return n_exit_status
;
1158 /* Source the others in that case! */
1159 #ifdef HAVE_AMALGAMATION
1160 # include "config.h"