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 ui16_t
const n_class_char
[1 + 0x7F] = {
71 #define a_BC C_BLANK | C_CNTRL
72 #define a_SC C_SPACE | C_CNTRL
73 #define a_WC C_WHITE | C_CNTRL
74 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
75 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
76 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
77 C_CNTRL
, a_BC
, a_WC
, a_SC
, a_SC
, a_SC
, C_CNTRL
, C_CNTRL
,
78 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
79 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
80 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
81 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) */
112 static char const *a_main_oarg
;
113 static int a_main_oind
, /*_oerr,*/ a_main_oopt
;
115 /* Our own little getopt(3); note --help is special-treated as 'h' */
116 static int a_main_getopt(int argc
, char * const argv
[], char const *optstring
);
118 /* Perform basic startup initialization */
119 static void a_main_startup(void);
122 static size_t a_main_grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
);
124 /* Setup some variables which we require to be valid / verified */
125 static void a_main_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 a_main_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 a_main_rcv_mode(char const *folder
, char const *Larg
);
140 /* Interrupt printing of the headers */
141 static void a_main_hdrstop(int signo
);
144 a_main_getopt(int argc
, char * const argv
[], char const *optstring
){
145 static char const *lastp
;
154 /*if((colon = (optstring[0] == ':')))
161 if(a_main_oind
>= argc
|| argv
[a_main_oind
] == NULL
||
162 argv
[a_main_oind
][0] != '-' || argv
[a_main_oind
][1] == '\0')
164 if(argv
[a_main_oind
][1] == '-' && argv
[a_main_oind
][2] == '\0'){
165 /* We need this in for MTA arg detection (easier) ++a_main_oind;*/
168 curp
= &argv
[a_main_oind
][1];
171 for(a_main_oopt
= curp
[0]; optstring
[0] != '\0';){
172 if(optstring
[0] != a_main_oopt
){
173 optstring
+= 1 + (optstring
[1] == ':');
177 if(optstring
[1] == ':'){
179 a_main_oarg
= n_UNCONST(curp
+ 1);
182 if((a_main_oind
+= 2) > argc
){
183 /*if(!colon *//*&& _oerr*//*)*/{
184 n_err(_("%s: option requires an argument -- %c\n"),
185 argv
[0], (char)a_main_oopt
);
187 rv
= (/*colon ? ':' :*/ '?');
190 a_main_oarg
= argv
[a_main_oind
- 1];
202 /* Special support for --help, which is quite common */
203 if(a_main_oopt
== '-' && !strcmp(curp
, "-help") &&
204 &curp
[-1] == argv
[a_main_oind
]){
210 /* Definitive error */
211 /*if(!colon *//*&& opterr*//*)*/
212 n_err(_("%s: invalid option -- %c\n"), argv
[0], a_main_oopt
);
225 a_main_startup(void){
234 a_main_oind
= /*_oerr =*/ 1;
236 if((cp
= strrchr(n_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 /* TODO This is wrong: interactive is STDIN/STDERR for a POSIX sh(1).
253 * TODO For now we get this wrong, all over the place, as this software
254 * TODO has always been developed with stdout as an output channel.
255 * TODO Start doing it right for at least explicit terminal-related things,
256 * TODO but v15 should use ONLY this, also for terminal input! */
257 if(isatty(STDIN_FILENO
)){
258 n_psonce
|= n_PSO_TTYIN
;
259 #if defined HAVE_MLE || defined HAVE_TERMCAP
260 n_tty_fp
= fdopen(fileno(n_stdin
), "w");
261 setvbuf(n_tty_fp
, NULL
, _IOLBF
, 0);
264 if(isatty(STDOUT_FILENO
))
265 n_psonce
|= n_PSO_TTYOUT
;
266 if((n_psonce
& (n_PSO_TTYIN
| n_PSO_TTYOUT
)) ==
267 (n_PSO_TTYIN
| n_PSO_TTYOUT
)){
268 n_psonce
|= n_PSO_INTERACTIVE
;
269 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
272 /* STDOUT is always line buffered from our point of view */
273 setvbuf(n_stdout
, NULL
, _IOLBF
, 0);
279 #ifndef HAVE_SETLOCALE
282 setlocale(LC_ALL
, n_empty
);
283 n_mb_cur_max
= MB_CUR_MAX
;
284 # ifdef HAVE_NL_LANGINFO
285 if((cp
= nl_langinfo(CODESET
)) != NULL
)
286 ok_vset(ttycharset
, cp
);
289 # ifdef HAVE_C90AMEND1
290 if(n_mb_cur_max
> 1){
291 # ifdef HAVE_ALWAYS_UNICODE_LOCALE
292 n_psonce
|= n_PSO_UNICODE
;
295 if(mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
296 mbtowc(&wc
, "\342\202\254", 3) == 3 && wc
== 0x20AC)
297 n_psonce
|= n_PSO_UNICODE
;
298 /* Reset possibly messed up state; luckily this also gives us an
299 * indication whether the encoding has locking shift state sequences */
300 if(mbtowc(&wc
, NULL
, n_mb_cur_max
))
301 n_psonce
|= n_PSO_ENC_MBSTATE
;
308 iconvd
= (iconv_t
)-1;
314 a_main_grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
){
315 /* Just use auto-reclaimed storage, it will be preserved */
319 newcpp
= salloc(sizeof(char*) * newsize
);
322 memcpy(newcpp
, *cpp
, oldcnt
* sizeof(char*));
329 a_main_setup_vars(void){
330 struct passwd
*pwuid
;
334 n_group_id
= getgid();
335 if((pwuid
= getpwuid(n_user_id
= getuid())) == NULL
)
336 n_panic(_("Cannot associate a name with uid %lu"), (ul_i
)n_user_id
);
342 if(!(doenv
= (ep
= ok_vlook(LOGNAME
)) == NULL
) &&
343 (doenv
= strcmp(pwuid
->pw_name
, ep
)))
344 n_err(_("Warning: $LOGNAME (%s) not identical to user (%s)!\n"),
347 n_pstate
|= n_PS_ROOT
;
348 ok_vset(LOGNAME
, pwuid
->pw_name
);
349 n_pstate
&= ~n_PS_ROOT
;
352 if((ep
= ok_vlook(USER
)) != NULL
&& strcmp(pwuid
->pw_name
, ep
)){
353 n_err(_("Warning: $USER (%s) not identical to user (%s)!\n"),
355 n_pstate
|= n_PS_ROOT
;
356 ok_vset(USER
, pwuid
->pw_name
);
357 n_pstate
&= ~n_PS_ROOT
;
361 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
364 if((cp
= ok_vlook(HOME
)) == NULL
||
365 !is_dir(cp
) || access(cp
, R_OK
| W_OK
| X_OK
)){
367 n_pstate
|= n_PS_ROOT
;
369 n_pstate
&= ~n_PS_ROOT
;
372 cp
= ok_vlook(TMPDIR
);
374 if(!is_dir(cp
) || access(cp
, R_OK
| W_OK
| X_OK
))
377 /* Ensure some variables get loaded */
378 (void)ok_blook(POSIXLY_CORRECT
);
383 a_main_setscreensize(int is_sighdl
){/* TODO globl policy; int wraps; minvals! */
387 #elif defined TIOCGSIZE
392 n_scrnheight
= n_realscreenheight
= n_scrnwidth
= 0;
394 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
395 * a little bit to be able to honour resizes during our lifetime and
396 * only honour it upon first run; abuse *is_sighdl* as an indicator */
400 /* We manage those variables for our child processes, so ensure they
401 * are up to date, always */
402 if(n_psonce
& n_PSO_INTERACTIVE
)
403 n_pstate
|= n_PS_SIGWINCH_PEND
;
405 if((cp
= ok_vlook(LINES
)) != NULL
)
406 n_scrnheight
= n_realscreenheight
= (int)strtoul(cp
, NULL
, 0); /*TODO*/
407 if((cp
= ok_vlook(COLUMNS
)) != NULL
)
408 n_scrnwidth
= (int)strtoul(cp
, NULL
, 0); /* TODO posui32= not posnum */
410 if(n_scrnwidth
!= 0 && n_scrnheight
!= 0)
415 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) == -1)
416 ws
.ws_col
= ws
.ws_row
= 0;
417 #elif defined TIOCGSIZE
418 if(ioctl(STDOUT_FILENO
, TIOCGSIZE
, &ws
) == -1)
419 ts
.ts_lines
= ts
.ts_cols
= 0;
422 if(n_scrnheight
== 0){
425 ospeed
= ((tcgetattr(fileno(n_tty_fp
), &tbuf
) == -1)
426 ? B9600
: cfgetospeed(&tbuf
));
430 else if(ospeed
== B1200
)
433 else if(ws
.ws_row
!= 0)
434 n_scrnheight
= ws
.ws_row
;
435 #elif defined TIOCGSIZE
436 else if(ts
.ts_lines
!= 0)
437 n_scrnheight
= ts
.ts_lines
;
442 #if defined TIOCGWINSZ || defined TIOCGSIZE
445 (n_realscreenheight
= ws
.ws_row
)
447 (n_realscreenheight
= ts
.ts_lines
)
450 n_realscreenheight
= 24;
454 if(n_scrnwidth
== 0 && 0 ==
456 (n_scrnwidth
= ws
.ws_col
)
457 #elif defined TIOCGSIZE
458 (n_scrnwidth
= ts
.ts_cols
)
466 n_pstate
|= n_PS_SIGWINCH_PEND
; /* XXX Not atomic */
467 if(n_psonce
& n_PSO_INTERACTIVE
)
468 n_tty_signal(SIGWINCH
);
474 static sigjmp_buf a_main__hdrjmp
; /* XXX */
477 a_main_rcv_mode(char const *folder
, char const *Larg
){
479 sighandler_type prevint
;
485 i
= (n_poption
& n_PO_QUICKRUN_MASK
) ? FEDIT_RDONLY
: FEDIT_NONE
;
486 i
= setfile(folder
, i
);
488 n_exit_status
= n_EXIT_ERR
; /* error already reported */
491 if(n_poption
& n_PO_QUICKRUN_MASK
){
493 if(i
== n_EXIT_OK
&& (!(n_poption
& n_PO_EXISTONLY
) ||
494 (n_poption
& n_PO_HEADERLIST
)))
495 print_header_summary(Larg
);
498 temporary_folder_hook_check(FAL0
);
500 if(i
> 0 && !ok_blook(emptystart
)){
501 n_exit_status
= n_EXIT_ERR
;
505 if(sigsetjmp(a_main__hdrjmp
, 1) == 0){
506 if((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
507 safe_signal(SIGINT
, &a_main_hdrstop
);
508 if(!(n_poption
& n_PO_N_FLAG
)){
510 fprintf(n_stdout
, _("%s version %s. Type `?' for help\n"),
511 (ok_blook(bsdcompat
) ? "Mail" : n_uagent
), ok_vlook(version
));
515 safe_signal(SIGINT
, prevint
);
518 /* Enter the command loop */
519 if(n_psonce
& n_PSO_INTERACTIVE
)
522 if(n_psonce
& n_PSO_INTERACTIVE
)
525 if(mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
){
526 safe_signal(SIGHUP
, SIG_IGN
);
527 safe_signal(SIGINT
, SIG_IGN
);
528 safe_signal(SIGQUIT
, SIG_IGN
);
531 save_mbox_for_possible_quitstuff();
535 return n_exit_status
;
539 a_main_hdrstop(int signo
){
540 NYD_X
; /* Signal handler */
544 n_err_sighdl(_("\nInterrupt\n"));
545 siglongjmp(a_main__hdrjmp
, 1);
549 main(int argc
, char *argv
[]){
550 /* Keep in SYNC: ./nail.1:"SYNOPSIS, main() */
552 optstr
[] = "A:a:Bb:c:dEeFfHhiL:M:m:NnO:q:Rr:S:s:tu:VvX:::~#.",
557 " %s [-BdEFintv~] [-: spec] [-A account]\n"
558 "\t [-a attachment] [-b bcc-addr] [-c cc-addr]\n"
559 "\t [-M type | -m file | -q file | -t]\n"
560 "\t [-r from-addr] [-S var[=value]..]\n"
561 "\t [-s subject] [-X cmd] [-.] to-addr.. [-- mta-option..]\n"
563 " %s [-BdEeHiNnRv~] [-: spec] [-A account]\n"
564 "\t [-L spec-list] [-r from-addr] [-S var[=value]..]\n"
565 "\t [-u user] [-X cmd] [-- mta-option..]\n"
567 " %s [-BdEeHiNnRv~#] [-: spec] [-A account] -f\n"
568 "\t [-L spec-list] [-r from-addr] [-S var[=value]..]\n"
569 "\t [-X cmd] [file] [-- mta-option..]\n"
571 #define _USAGE_ARGS , n_progname, n_progname, n_progname, n_progname
580 a_RF_ALL
= a_RF_SYSTEM
| a_RF_USER
582 size_t oargs_size
, oargs_cnt
, Xargs_size
, Xargs_cnt
, smopts_size
;
583 char const *Aarg
, *emsg
, *folder
, *Larg
, *okey
, **oargs
, *qf
,
584 *subject
, *uarg
, **Xargs
;
585 struct attachment
*attach
;
586 struct name
*to
, *cc
, *bcc
;
587 struct a_arg
*a_head
, *a_curr
;
591 n_UNINIT(a_curr
, NULL
);
592 to
= cc
= bcc
= NULL
;
594 Aarg
= emsg
= folder
= Larg
= okey
= qf
= subject
= uarg
= NULL
;
595 oargs
= Xargs
= NULL
;
596 oargs_size
= oargs_cnt
= Xargs_size
= Xargs_cnt
= smopts_size
= 0;
600 * Start our lengthy setup, finalize by setting n_PSO_STARTED
603 n_progname
= argv
[0];
606 /* Command line parsing
607 * Variable settings need to be done twice, since the user surely wants the
608 * setting to take effect immediately, but also doesn't want it to be
609 * overwritten from within resource files */
610 while((i
= a_main_getopt(argc
, argv
, optstr
)) >= 0){
613 /* Execute an account command later on */
619 n_psonce
|= n_PSO_SENDMODE
;
620 nap
= salloc(sizeof(struct a_arg
));
624 a_curr
->aa_next
= nap
;
626 nap
->aa_file
= a_main_oarg
;
630 n_OBSOLETE(_("-B is obsolete, please use -# as necessary"));
633 /* Get Blind Carbon Copy Recipient list */
634 n_psonce
|= n_PSO_SENDMODE
;
635 bcc
= cat(bcc
, lextract(a_main_oarg
, GBCC
| GFULL
));
638 /* Get Carbon Copy Recipient list */
639 n_psonce
|= n_PSO_SENDMODE
;
640 cc
= cat(cc
, lextract(a_main_oarg
, GCC
| GFULL
));
647 n_OBSOLETE(_("-E will be removed, please use \"-Sskipemptybody\""));
648 ok_bset(skipemptybody
);
649 okey
= "skipemptybody";
652 n_poption
|= n_PO_EXISTONLY
;
655 n_poption
|= n_PO_F_FLAG
;
656 n_psonce
|= n_PSO_SENDMODE
;
659 /* User is specifying file to "edit" with Mail, as opposed to reading
660 * system mailbox. If no argument is given, we read his mbox file.
661 * Check for remaining arguments later */
665 n_poption
|= n_PO_HEADERSONLY
;
668 n_err(V_(usagestr
) _USAGE_ARGS
);
671 /* Ignore interrupts */
677 n_poption
|= n_PO_HEADERLIST
;
678 if(*Larg
== '"' || *Larg
== '\''){ /* TODO list.c:listspec_check() */
683 cp
= savestrbuf(Larg
, --j
);
689 if(qf
!= NULL
&& (!(n_poption
& n_PO_Mm_FLAG
) || qf
!= (char*)-1))
691 n_poption_arg_Mm
= a_main_oarg
;
696 if(qf
!= NULL
&& (!(n_poption
& n_PO_Mm_FLAG
) || qf
== (char*)-1))
700 n_poption
|= n_PO_Mm_FLAG
;
701 n_psonce
|= n_PSO_SENDMODE
;
704 /* Avoid initial header printing */
709 /* Don't source "unspecified system start-up file" */
710 if(resfiles
& a_RF_SET
){
711 emsg
= N_("-n cannot be used in conjunction with -:");
714 resfiles
= a_RF_USER
;
717 /* Additional options to pass-through to MTA TODO v15-compat legacy */
718 if(n_smopts_cnt
== smopts_size
)
719 smopts_size
= a_main_grow_cpp(&n_smopts
, smopts_size
+ 8,
721 n_smopts
[n_smopts_cnt
++] = a_main_oarg
;
724 if(qf
!= NULL
&& (n_poption
& n_PO_Mm_FLAG
)){
726 emsg
= N_("Only one of -M, -m or -q may be given");
729 /* Quote file TODO drop? -Q with real quote?? what ? */
730 n_psonce
|= n_PSO_SENDMODE
;
731 /* Allow for now, we have to special check validity of -q- later on! */
732 qf
= (a_main_oarg
[0] == '-' && a_main_oarg
[1] == '\0')
733 ? (char*)-1 : a_main_oarg
;
736 /* Open folders read-only */
737 n_poption
|= n_PO_R_FLAG
;
740 /* Set From address. */
741 n_poption
|= n_PO_r_FLAG
;
742 if(a_main_oarg
[0] != '\0'){
745 fa
= nalloc(a_main_oarg
, GSKIN
| GFULL
| GFULLEXTRA
);
746 if(is_addr_invalid(fa
, EACM_STRICT
| EACM_NOLOG
)){
747 emsg
= N_("Invalid address argument with -r");
750 n_poption_arg_r
= fa
;
751 /* TODO -r options is set in n_smopts, but may
752 * TODO be overwritten by setting from= in
753 * TODO an interactive session!
754 * TODO Maybe disable setting of from?
755 * TODO Warn user? Update manual!! */
756 okey
= savecat("from=", fa
->n_fullname
);
761 /* Set variable (TODO twice [only if successful]) */
765 okey
= a
[0] = a_main_oarg
;
767 n_pstate
|= n_PS_ROBOT
;
769 n_pstate
&= ~n_PS_ROBOT
;
774 if(oargs_cnt
== oargs_size
)
775 oargs_size
= a_main_grow_cpp(&oargs
, oargs_size
+ 8, oargs_cnt
);
776 oargs
[oargs_cnt
++] = okey
;
779 /* Subject:; take care for Debian #419840 and strip any \r and \n */
780 if(n_anyof_cp("\n\r", subject
= a_main_oarg
)){
781 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
782 for(subject
= cp
= savestr(a_main_oarg
); *cp
!= '\0'; ++cp
)
783 if(*cp
== '\n' || *cp
== '\r')
786 n_psonce
|= n_PSO_SENDMODE
;
789 /* Read defined set of headers from mail to be sent */
790 n_poption
|= n_PO_t_FLAG
;
791 n_psonce
|= n_PSO_SENDMODE
;
794 uarg
= savecat("%", a_main_oarg
);
797 fprintf(n_stdout
, _("%s version %s\n"), n_uagent
, ok_vlook(version
));
798 n_exit_status
= n_EXIT_OK
;
806 /* Add to list of commands to exec before entering normal operation */
807 if(Xargs_cnt
== Xargs_size
)
808 Xargs_size
= a_main_grow_cpp(&Xargs
, Xargs_size
+ 8, Xargs_cnt
);
809 Xargs
[Xargs_cnt
++] = a_main_oarg
;
812 /* Control which resource files shall be loaded */
813 if(!(resfiles
& (a_RF_SET
| a_RF_SYSTEM
))){
814 emsg
= N_("-n cannot be used in conjunction with -:");
818 while((i
= *a_main_oarg
++) != '\0')
820 case 'S': case 's': resfiles
|= a_RF_SYSTEM
; break;
821 case 'U': case 'u': resfiles
|= a_RF_USER
; break;
822 case ':': case '/': resfiles
&= ~a_RF_ALL
; break;
824 emsg
= N_("Invalid argument of -:");
829 /* Enable command escapes even in non-interactive mode */
830 n_poption
|= n_PO_TILDE_FLAG
;
833 /* Work in batch mode, even if non-interactive */
834 if(!(n_psonce
& n_PSO_INTERACTIVE
))
835 setvbuf(n_stdin
, NULL
, _IOLBF
, 0);
836 n_poption
|= n_PO_TILDE_FLAG
| n_PO_BATCH_FLAG
;
837 if (oargs_cnt
+ 8 >= oargs_size
)
838 oargs_size
= a_main_grow_cpp(&oargs
, oargs_size
+ 9, oargs_cnt
);
839 folder
= "/dev/null";
840 ok_vset(MAIL
, folder
);
841 ok_vset(MBOX
, folder
);
844 ok_vset(inbox
, folder
);
847 ok_bset(typescript_mode
);
848 oargs
[oargs_cnt
+ 0] = "MAIL=/dev/null";
849 oargs
[oargs_cnt
+ 1] = "MBOX=/dev/null";
850 oargs
[oargs_cnt
+ 2] = "emptystart";
851 oargs
[oargs_cnt
+ 3] = "noheader";
852 oargs
[oargs_cnt
+ 4] = "inbox=/dev/null";
853 oargs
[oargs_cnt
+ 5] = "quiet";
854 oargs
[oargs_cnt
+ 6] = "sendwait";
855 oargs
[oargs_cnt
+ 7] = "typescript-mode";
859 n_psonce
|= n_PSO_SENDMODE
;
864 n_err("%s\n", V_(emsg
));
865 n_err(V_(usagestr
) _USAGE_ARGS
);
867 n_exit_status
= n_EXIT_USE
;
874 /* The normal arguments may be followed by MTA arguments after a "--";
875 * however, -f may take off an argument, too, and before that.
876 * Since MTA arguments after "--" require *expandargv*, delay parsing off
877 * those options until after the resource files are loaded... */
878 if((cp
= argv
[i
= a_main_oind
]) == NULL
)
880 else if(cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0')
882 /* n_PO_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
883 else if(folder
!= NULL
&& /*folder[0] == '&' &&*/ folder
[1] == '\0'){
885 if((cp
= argv
[++i
]) != NULL
){
886 if(cp
[0] != '-' || cp
[1] != '-' || cp
[2] != '\0'){
887 emsg
= N_("More than one file given with -f");
893 n_psonce
|= n_PSO_SENDMODE
;
895 to
= cat(to
, lextract(cp
, GTO
| GFULL
));
896 if((cp
= argv
[++i
]) == NULL
)
898 if(cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0'){
906 /* ...BUT, since we use salloc() for the MTA n_smopts storage we need to
907 * allocate the necessary space for them before we fixate that storage! */
908 while(argv
[i
] != NULL
)
910 if(n_smopts_cnt
+ i
> smopts_size
)
912 a_main_grow_cpp(&n_smopts
, n_smopts_cnt
+ i
+ 1, n_smopts_cnt
);
914 /* Check for inconsistent arguments, fix some temporaries */
915 if(n_psonce
& n_PSO_SENDMODE
){
916 /* XXX This is only because BATCH_FLAG sets *folder*=/dev/null
917 * XXX in order to function. Ideally that would not be needed */
918 if(folder
!= NULL
&& !(n_poption
& n_PO_BATCH_FLAG
)){
919 emsg
= N_("Cannot give -f and people to send to.");
923 emsg
= N_("The -u option cannot be used in send mode");
926 if(!(n_poption
& n_PO_t_FLAG
) && to
== NULL
){
927 emsg
= N_("Send options without primary recipient specified.");
930 if((n_poption
& n_PO_t_FLAG
) && qf
!= NULL
){
931 emsg
= N_("The -M, -m, -q and -t options are mutual exclusive.");
934 if(n_poption
& (n_PO_EXISTONLY
| n_PO_HEADERSONLY
| n_PO_HEADERLIST
)){
935 emsg
= N_("The -e, -H and -L options cannot be used in send mode.");
938 if(n_poption
& n_PO_R_FLAG
){
939 emsg
= N_("The -R option is meaningless in send mode.");
943 if(n_psonce
& n_PSO_INTERACTIVE
){
945 if(!(n_poption
& n_PO_Mm_FLAG
))
946 emsg
= N_("-q can't use standard input when interactive.\n");
951 if(uarg
!= NULL
&& folder
!= NULL
){
952 emsg
= N_("The options -u and -f (and -#) are mutually exclusive");
955 if((n_poption
& (n_PO_EXISTONLY
| n_PO_HEADERSONLY
)) ==
956 (n_PO_EXISTONLY
| n_PO_HEADERSONLY
)){
957 emsg
= N_("The options -e and -H are mutual exclusive");
960 if((n_poption
& (n_PO_HEADERSONLY
| n_PO_HEADERLIST
) /* TODO OBSOLETE */
961 ) == (n_PO_HEADERSONLY
| n_PO_HEADERLIST
))
962 n_OBSOLETE(_("please use \"-e -L xy\" instead of \"-H -L xy\""));
969 * Likely to go, perform more setup
974 if(n_psonce
& n_PSO_INTERACTIVE
){
975 a_main_setscreensize(0);
977 # ifndef TTY_WANTS_SIGWINCH
978 if(safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
980 safe_signal(SIGWINCH
, &a_main_setscreensize
);
983 n_scrnheight
= n_realscreenheight
= 24, n_scrnwidth
= 80;
985 /* Fixate the current snapshot of our global auto-reclaimed storage instance.
986 * Memory is auto-reclaimed from now on */
987 n_memory_autorec_fixate();
989 /* load() any resource files */
990 if(resfiles
& a_RF_ALL
){
991 /* *expand() returns a savestr(), but load only uses the file name for
992 * fopen(), so it's safe to do this */
993 if(resfiles
& a_RF_SYSTEM
){
996 if((nload
= ok_blook(NAIL_NO_SYSTEM_RC
)))
997 n_OBSOLETE(_("Please use $MAILX_NO_SYSTEM_RC instead of "
998 "$NAIL_NO_SYSTEM_RC"));
999 if(!nload
&& !ok_blook(MAILX_NO_SYSTEM_RC
))
1000 n_load(VAL_SYSCONFDIR
"/" VAL_SYSCONFRC
);
1003 if(resfiles
& a_RF_USER
)
1004 n_load(fexpand(ok_vlook(MAILRC
), FEXP_LOCAL
| FEXP_NOPROTO
));
1006 if((cp
= ok_vlook(NAIL_EXTRA_RC
)) != NULL
)
1007 n_OBSOLETE(_("Please use *mailx-extra-rc*, not *NAIL_EXTRA_RC*"));
1008 if(cp
!= NULL
|| (cp
= ok_vlook(mailx_extra_rc
)) != NULL
)
1009 n_load(fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
));
1012 /* Ensure the -S and other command line options take precedence over
1013 * anything that may have been placed in resource files.
1014 * Our "ternary binary" option *verbose* needs special treament */
1015 if((n_poption
& (n_PO_VERB
| n_PO_VERBVERB
)) == n_PO_VERB
)
1016 n_poption
&= ~n_PO_VERB
;
1017 /* ..and be silent when unsetting an undefined variable */
1018 n_pstate
|= n_PS_ROBOT
;
1019 for(i
= 0; UICMP(z
, i
, <, oargs_cnt
); ++i
){
1026 n_pstate
&= ~n_PS_ROBOT
;
1028 /* Cause possible umask(2), now that any setting is established, and before
1029 * we change accounts, evaluate commands etc. */
1030 (void)ok_vlook(umask
);
1032 /* Additional options to pass-through to MTA, and allowed to do so? */
1033 if((cp
= ok_vlook(expandargv
)) != NULL
){
1034 bool_t isfail
, isrestrict
;
1036 isfail
= !asccasecmp(cp
, "fail");
1037 isrestrict
= (!isfail
&& !asccasecmp(cp
, "restrict"));
1039 if((n_poption
& n_PO_D_V
) && !isfail
&& !isrestrict
&& *cp
!= '\0')
1040 n_err(_("Unknown *expandargv* value: %s\n"), cp
);
1042 if((cp
= argv
[i
= a_main_oind
]) != NULL
){
1043 if(isfail
|| (isrestrict
&& (!(n_poption
& n_PO_TILDE_FLAG
) ||
1044 !(n_psonce
& n_PSO_INTERACTIVE
)))){
1045 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
1046 "using *sendmail-arguments*\n"));
1047 n_exit_status
= n_EXIT_USE
| n_EXIT_SEND_ERROR
;
1051 assert(n_smopts_cnt
+ 1 <= smopts_size
);
1052 n_smopts
[n_smopts_cnt
++] = cp
;
1053 }while((cp
= argv
[++i
]) != NULL
);
1057 /* We had to wait until the resource files are loaded and any command line
1058 * setting has been restored, but get the termcap up and going before we
1059 * switch account or running commands */
1061 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_QUICKRUN_MASK
))
1065 /* Now we can set the account */
1074 /* "load()" commands given on command line */
1076 n_load_Xargs(Xargs
, Xargs_cnt
);
1079 if(n_poption
& n_PO_Mm_FLAG
){
1080 if(qf
== (char*)-1){
1081 if(!mime_type_check_mtname(n_poption_arg_Mm
)){
1082 n_err(_("Could not find `mimetype' for -M argument: %s\n"),
1084 n_exit_status
= n_EXIT_ERR
;
1087 }else if((n_poption_arg_Mm
= mime_type_classify_filename(qf
)) == NULL
){
1088 n_err(_("Could not `mimetype'-classify -m argument: %s\n"),
1089 n_shexp_quote_cp(qf
, FAL0
));
1090 n_exit_status
= n_EXIT_ERR
;
1092 }else if(!asccasecmp(n_poption_arg_Mm
, "text/plain")) /* TODO no: magic */
1093 n_poption_arg_Mm
= NULL
;
1097 * We're finally completely setup and ready to go
1099 n_psonce
|= n_PSO_STARTED
;
1101 if(!(n_psonce
& n_PSO_SENDMODE
)){
1102 n_exit_status
= a_main_rcv_mode(folder
, Larg
);
1106 /* Now that full mailx(1)-style file expansion is possible handle the
1107 * attachments which we had delayed due to this.
1108 * This may use savestr(), but since we won't enter the command loop we
1109 * don't need to care about that */
1110 for(; a_head
!= NULL
; a_head
= a_head
->aa_next
){
1111 enum n_attach_error aerr
;
1113 attach
= n_attachment_append(attach
, a_head
->aa_file
, &aerr
, NULL
);
1114 if(aerr
!= n_ATTACH_ERR_NONE
){
1115 n_exit_status
= n_EXIT_ERR
;
1120 if(n_psonce
& n_PSO_INTERACTIVE
)
1122 mail(to
, cc
, bcc
, subject
, attach
, qf
, ((n_poption
& n_PO_F_FLAG
) != 0));
1123 if(n_psonce
& n_PSO_INTERACTIVE
)
1127 /* Be aware of identical code for `exit' command! */
1129 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_QUICKRUN_MASK
))
1130 n_termcap_destroy();
1134 #ifdef HAVE_MEMORY_DEBUG
1135 n_memory_autorec_pop(NULL
);
1137 #if (defined HAVE_DEBUG || defined HAVE_DEVEL)
1141 return n_exit_status
;
1144 /* Source the others in that case! */
1145 #ifdef HAVE_AMALGAMATION
1146 # include "config.h"