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>
54 struct a_arg
*aa_next
;
58 /* (extern, but not with amalgamation, so define here) */
59 VL
char const weekday_names
[7 + 1][4] = {
60 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
62 VL
char const month_names
[12 + 1][4] = {
63 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
64 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
66 VL
char const uagent
[sizeof UAGENT
] = UAGENT
;
67 VL uc_i
const class_char
[1 + 0x7F] = {
68 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
69 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
70 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
71 C_CNTRL
, C_BLANK
, C_WHITE
, C_SPACE
, C_SPACE
, C_SPACE
, C_CNTRL
, C_CNTRL
,
72 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
73 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
74 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
75 C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
, C_CNTRL
,
76 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
77 C_BLANK
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
78 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
79 C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
80 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
81 C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
, C_OCTAL
,
82 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
83 C_DIGIT
, C_DIGIT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
84 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
85 C_PUNCT
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
,
86 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
87 C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
,
88 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
89 C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
, C_UPPER
,
90 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
91 C_UPPER
, C_UPPER
, C_UPPER
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
,
92 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
93 C_PUNCT
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
,
94 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
95 C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
,
96 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
97 C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
, C_LOWER
,
98 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
99 C_LOWER
, C_LOWER
, C_LOWER
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_PUNCT
, C_CNTRL
102 /* Our own little getopt(3) */
104 static int _oind
, /*_oerr,*/ _oopt
;
106 /* Our own little getopt(3); note --help is special-treated as 'h' */
107 static int _getopt(int argc
, char * const argv
[], char const *optstring
);
109 /* Perform basic startup initialization */
110 static void _startup(void);
113 static size_t _grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
);
115 /* Initialize *tempdir*, *myname*, *homedir* */
116 static void _setup_vars(void);
118 /* We're in an interactive session - compute what the screen size for printing
119 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
120 * We use the following algorithm for the height:
121 * If baud rate < 1200, use 9
122 * If baud rate = 1200, use 14
123 * If baud rate > 1200, use 24 or ws_row
124 * Width is either 80 or ws_col */
125 static void _setscreensize(int is_sighdl
);
127 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
128 * the system mailbox, and open up the right stuff */
129 static int _rcv_mode(char const *folder
, char const *Larg
);
131 /* Interrupt printing of the headers */
132 static void _hdrstop(int signo
);
135 _getopt(int argc
, char * const argv
[], char const *optstring
)
137 static char const *lastp
;
139 int rv
= -1/*, colon*/;
145 /*if ((colon = (optstring[0] == ':')))
152 if (_oind
>= argc
|| argv
[_oind
] == NULL
|| argv
[_oind
][0] != '-' ||
153 argv
[_oind
][1] == '\0')
155 if (argv
[_oind
][1] == '-' && argv
[_oind
][2] == '\0') {
156 /* We need this in for MTA arg detection (easier) ++_oind;*/
159 curp
= &argv
[_oind
][1];
162 for(_oopt
= curp
[0]; optstring
[0] != '\0';){
163 if(optstring
[0] != _oopt
){
164 optstring
+= 1 + (optstring
[1] == ':');
168 if (optstring
[1] == ':') {
169 if (curp
[1] != '\0') {
170 _oarg
= UNCONST(curp
+ 1);
173 if ((_oind
+= 2) > argc
) {
174 /*if (!colon *//*&& _oerr*//*)*/ {
175 n_err(_("%s: option requires an argument -- %c\n"),
176 argv
[0], (char)_oopt
);
178 rv
= (/*colon ? ':' :*/ '?');
181 _oarg
= argv
[_oind
- 1];
193 /* Special support for --help, which is quite common */
194 if (_oopt
== '-' && !strcmp(curp
, "-help") && curp
- 1 == argv
[_oind
]) {
200 /* Definitive error */
201 /*if (!colon *//*&& opterr*//*)*/
202 n_err(_("%s: invalid option -- %c\n"), argv
[0], _oopt
);
222 _oind
= /*_oerr =*/ 1;
224 if ((cp
= strrchr(progname
, '/')) != NULL
)
227 /* Set up a reasonable environment */
230 safe_signal(SIGABRT
, &_nyd_oncrash
);
232 safe_signal(SIGBUS
, &_nyd_oncrash
);
234 safe_signal(SIGFPE
, &_nyd_oncrash
);
235 safe_signal(SIGILL
, &_nyd_oncrash
);
236 safe_signal(SIGSEGV
, &_nyd_oncrash
);
238 command_manager_start();
240 if (isatty(STDIN_FILENO
)) /* TODO should be isatty(0) && isatty(2)?? */
241 options
|= OPT_TTYIN
;
242 if (isatty(STDOUT_FILENO
))
243 options
|= OPT_TTYOUT
;
244 if ((options
& (OPT_TTYIN
| OPT_TTYOUT
)) == (OPT_TTYIN
| OPT_TTYOUT
)) {
245 options
|= OPT_INTERACTIVE
;
246 safe_signal(SIGPIPE
, dflpipe
= SIG_IGN
);
251 /* Define defaults for internal variables, based on POSIX 2008/Cor 1-2013 */
253 * ./main.c:_startup(), ./nail.rc, ./nail.1:"Initial settings") */
256 ok_bset(asksub
, TRU1
);
265 /* ok_vset(escape, ESCAPE *"~"*); TODO non-compliant */
268 ok_bset(header
, TRU1
);
275 /* noonehop -- Note: we ignore this one */
278 ok_vset(prompt
, "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
286 /* ok_vset(toplines, "5"); XXX somewhat hmm */
288 /* TODO until we have an automatic mechanism for that, set some more
289 * TODO variables so that users see the internal fallback settings
290 * TODO (something like "defval=X,notempty=1") */
294 vp
= env_vlook("SHELL", TRU1
);
295 ok_vset(SHELL
, (vp
!= NULL
? vp
: XSHELL
));
297 vp
= env_vlook("LISTER", TRU1
);
298 ok_vset(LISTER
, (vp
!= NULL
? vp
: XLISTER
));
300 vp
= env_vlook("PAGER", TRU1
);
301 ok_vset(PAGER
, (vp
!= NULL
? vp
: XPAGER
));
303 ok_vset(sendmail
, SENDMAIL
);
304 ok_vset(sendmail_progname
, SENDMAIL_PROGNAME
);
309 #ifdef HAVE_SETLOCALE
310 setlocale(LC_ALL
, "");
311 mb_cur_max
= MB_CUR_MAX
;
312 # ifdef HAVE_NL_LANGINFO
313 if (ok_vlook(ttycharset
) == NULL
&& (cp
= nl_langinfo(CODESET
)) != NULL
)
314 ok_vset(ttycharset
, cp
);
317 # ifdef HAVE_C90AMEND1
318 if (mb_cur_max
> 1) {
320 if (mbtowc(&wc
, "\303\266", 2) == 2 && wc
== 0xF6 &&
321 mbtowc(&wc
, "\342\202\254", 3) == 3 && wc
== 0x20AC)
322 options
|= OPT_UNICODE
;
323 /* Reset possibly messed up state; luckily this also gives us an
324 * indication wether the encoding has locking shift state sequences */
325 /* TODO temporary - use option bits! */
326 if (mbtowc(&wc
, NULL
, mb_cur_max
))
327 options
|= OPT_ENC_MBSTATE
;
335 iconvd
= (iconv_t
)-1;
341 _grow_cpp(char const ***cpp
, size_t newsize
, size_t oldcnt
)
343 /* Before spreserve(): use our string pool instead of LibC heap */
347 newcpp
= salloc(sizeof(char*) * newsize
);
350 memcpy(newcpp
, *cpp
, oldcnt
* sizeof(char*));
359 /* Before spreserve(): use our string pool instead of LibC heap */
360 /* XXX further check paths? */
363 struct passwd
*pwuid
, *pw
;
366 /* Verify and fixate user identification */
369 else if ((cp
= env_vlook("LOGNAME", TRU1
)) == NULL
)
370 cp
= env_vlook("USER", TRU1
);
373 user_id
= uid
= getuid();
374 if ((pwuid
= getpwuid(uid
)) == NULL
)
375 n_panic(_("Cannot associate a name with uid %u"), user_id
);
377 if (cp
== NULL
|| *cp
== '\0')
378 myname
= pwuid
->pw_name
;
379 else if ((pw
= getpwnam(cp
)) == NULL
) {
380 n_alert(_("\"%s\" is not a user of this system"), cp
);
383 myname
= pw
->pw_name
;
384 if (pw
->pw_uid
!= uid
)
385 options
|= OPT_u_FLAG
;
387 myname
= savestr(myname
);
388 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
391 if ((cp
= env_vlook("HOME", TRU1
)) == NULL
)
392 cp
= "."; /* XXX User and Login objects; Login: pw->pw_dir */
393 homedir
= savestr(cp
);
395 tempdir
= ((cp
= env_vlook("TMPDIR", TRU1
)) != NULL
)
396 ? savestr(cp
) : TMPDIR_FALLBACK
;
401 _setscreensize(int is_sighdl
) /* TODO global policy; int wraps; minvals! */
406 #elif defined TIOCGSIZE
411 scrnheight
= realscreenheight
= scrnwidth
= 0;
413 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
414 * a little bit to be able to honour resizes during our lifetime and
415 * only honour it upon first run; abuse *is_sighdl* as an indicator */
420 if ((cp
= env_vlook("LINES", FAL0
)) != NULL
&&
421 (i
= strtol(cp
, NULL
, 10)) > 0 && i
< INT_MAX
)
422 scrnheight
= realscreenheight
= (int)i
;
423 if ((cp
= env_vlook("COLUMNS", FAL0
)) != NULL
&&
424 (i
= strtol(cp
, NULL
, 10)) > 0 && i
< INT_MAX
)
427 if (scrnwidth
!= 0 && scrnheight
!= 0)
432 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) == -1)
433 ws
.ws_col
= ws
.ws_row
= 0;
434 #elif defined TIOCGSIZE
435 if (ioctl(STDOUT_FILENO
, TIOCGSIZE
, &ws
) == -1)
436 ts
.ts_lines
= ts
.ts_cols
= 0;
439 if (scrnheight
== 0) {
440 speed_t ospeed
= ((tcgetattr(STDOUT_FILENO
, &tbuf
) == -1)
441 ? B9600
: cfgetospeed(&tbuf
));
445 else if (ospeed
== B1200
)
448 else if (ws
.ws_row
!= 0)
449 scrnheight
= ws
.ws_row
;
450 #elif defined TIOCGSIZE
451 else if (ts
.ts_lines
!= 0)
452 scrnheight
= ts
.ts_lines
;
457 #if defined TIOCGWINSZ || defined TIOCGSIZE
460 (realscreenheight
= ws
.ws_row
)
462 (realscreenheight
= ts
.ts_lines
)
465 realscreenheight
= 24;
469 if (scrnwidth
== 0 && 0 ==
471 (scrnwidth
= ws
.ws_col
)
472 #elif defined TIOCGSIZE
473 (scrnwidth
= ts
.ts_cols
)
480 if (is_sighdl
&& (options
& OPT_INTERACTIVE
))
481 n_tty_signal(SIGWINCH
);
486 static sigjmp_buf __hdrjmp
; /* XXX */
489 _rcv_mode(char const *folder
, char const *Larg
)
492 sighandler_type prevint
;
498 if (options
& OPT_QUICKRUN_MASK
)
502 i
= setfile(folder
, i
);
504 exit_status
= EXIT_ERR
; /* error already reported */
507 if (options
& OPT_QUICKRUN_MASK
) {
509 if (i
== EXIT_OK
&& (!(options
& OPT_EXISTONLY
) ||
510 (options
& OPT_HEADERLIST
)))
511 print_header_summary(Larg
);
514 check_folder_hook(FAL0
);
516 if (i
> 0 && !ok_blook(emptystart
)) {
517 exit_status
= EXIT_ERR
;
521 if (sigsetjmp(__hdrjmp
, 1) == 0) {
522 if ((prevint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
523 safe_signal(SIGINT
, _hdrstop
);
524 if (!(options
& OPT_N_FLAG
)) {
525 if (!ok_blook(quiet
))
526 printf(_("%s version %s. Type ? for help.\n"),
527 (ok_blook(bsdcompat
) ? "Mail" : uagent
), ok_vlook(version
));
531 safe_signal(SIGINT
, prevint
);
534 /* Enter the command loop */
535 if (options
& OPT_INTERACTIVE
)
538 if (options
& OPT_INTERACTIVE
)
541 if (mb
.mb_type
== MB_FILE
|| mb
.mb_type
== MB_MAILDIR
) {
542 safe_signal(SIGHUP
, SIG_IGN
);
543 safe_signal(SIGINT
, SIG_IGN
);
544 safe_signal(SIGQUIT
, SIG_IGN
);
547 save_mbox_for_possible_quitstuff();
557 NYD_X
; /* Signal handler */
561 n_err_sighdl(_("\nInterrupt\n"));
562 siglongjmp(__hdrjmp
, 1);
566 main(int argc
, char *argv
[]){
567 static char const optstr
[] = "A:a:Bb:c:dEeFfHhiL:NnO:q:Rr:S:s:tu:VvX:::~#.",
571 " %s [-BdEFintv~] [-: spec] [-A account]\n"
572 "\t [-a attachment] [-b bcc-address] [-c cc-address]\n"
573 "\t [-q file] [-r from-address] [-S var[=value]..]\n"
574 "\t [-s subject] [-X cmd] [-.] to-address.. [-- mta-option..]\n"
575 " %s [-BdEeHiNnRv~#] [-: spec] [-A account]\n"
576 "\t [-L spec-list] [-r from-address] [-S var[=value]..]\n"
577 "\t [-X cmd] -f [file] [-- mta-option..]\n"
578 " %s [-BdEeHiNnRv~#] [-: spec] [-A account]\n"
579 "\t [-L spec-list] [-r from-address] [-S var[=value]..]\n"
580 "\t [-u user] [-X cmd] [-- mta-option..]\n"
582 #define _USAGE_ARGS , progname, progname, progname, progname
584 struct a_arg
*a_head
, *a_curr
;
585 struct name
*to
, *cc
, *bcc
;
586 struct attachment
*attach
;
587 char *cp
, *subject
, *qf
, *Aarg
, *Larg
;
588 char const *okey
, **oargs
, **Xargs
, *folder
, *emsg
;
589 size_t oargs_size
, oargs_cnt
, Xargs_size
, Xargs_cnt
, smopts_size
;
595 a_RF_ALL
= a_RF_SYSTEM
| a_RF_USER
601 UNINIT(a_curr
, NULL
);
602 to
= cc
= bcc
= NULL
;
606 oargs
= Xargs
= NULL
;
607 folder
= emsg
= NULL
;
608 oargs_size
= oargs_cnt
= Xargs_size
= Xargs_cnt
= smopts_size
= 0;
612 * Start our lengthy setup, finalize by setting PS_STARTED
618 /* Command line parsing
619 * Variable settings need to be done twice, since the user surely wants the
620 * setting to take effect immediately, but also doesn't want it to be
621 * overwritten from within resource files */
622 while ((i
= _getopt(argc
, argv
, optstr
)) >= 0) {
625 /* Execute an account command later on */
629 options
|= OPT_SENDMODE
;
630 { struct a_arg
*nap
= salloc(sizeof(struct a_arg
));
634 a_curr
->aa_next
= nap
;
636 nap
->aa_file
= _oarg
;
641 /* Make 0/1 line buffered */
642 setvbuf(stdin
, NULL
, _IOLBF
, 0);
643 setvbuf(stdout
, NULL
, _IOLBF
, 0);
646 /* Get Blind Carbon Copy Recipient list */
647 options
|= OPT_SENDMODE
;
648 bcc
= cat(bcc
, lextract(_oarg
, GBCC
| GFULL
));
651 /* Get Carbon Copy Recipient list */
652 options
|= OPT_SENDMODE
;
653 cc
= cat(cc
, lextract(_oarg
, GCC
| GFULL
));
656 ok_bset(debug
, TRU1
);
660 ok_bset(skipemptybody
, TRU1
);
661 okey
= "skipemptybody";
664 options
|= OPT_EXISTONLY
;
667 options
|= OPT_F_FLAG
| OPT_SENDMODE
;
670 /* User is specifying file to "edit" with Mail, as opposed to reading
671 * system mailbox. If no argument is given, we read his mbox file.
672 * Check for remaining arguments later */
676 options
|= OPT_HEADERSONLY
;
679 n_err(V_(usagestr
) _USAGE_ARGS
);
682 /* Ignore interrupts */
683 ok_bset(ignore
, TRU1
);
688 options
|= OPT_HEADERLIST
;
689 if (*Larg
== '"' || *Larg
== '\'') { /* TODO list.c:listspec_check() */
690 size_t j
= strlen(++Larg
);
696 /* Avoid initial header printing */
697 ok_bset(header
, FAL0
);
701 /* Don't source "unspecified system start-up file" */
702 if(resfiles
& a_RF_SET
){
703 emsg
= N_("-n cannot be used in conjunction with -:");
706 resfiles
= a_RF_USER
;
709 /* Additional options to pass-through to MTA TODO v15-compat legacy */
710 if (smopts_cnt
== smopts_size
)
711 smopts_size
= _grow_cpp(&smopts
, smopts_size
+ 8, smopts_cnt
);
712 smopts
[smopts_cnt
++] = _oarg
;
715 /* Quote file TODO drop? -Q with real quote?? what ? */
716 options
|= OPT_SENDMODE
;
717 /* Allow for now, we have to special check validity of -q- later on! */
718 qf
= (_oarg
[0] == '-' && _oarg
[1] == '\0') ? (char*)-1 : _oarg
;
721 /* Open folders read-only */
722 options
|= OPT_R_FLAG
;
725 /* Set From address. */
726 options
|= OPT_r_FLAG
;
727 if (_oarg
[0] != '\0') {
728 struct name
*fa
= nalloc(_oarg
, GSKIN
| GFULL
| GFULLEXTRA
);
730 if (is_addr_invalid(fa
, EACM_STRICT
| EACM_NOLOG
)) {
731 emsg
= N_("Invalid address argument with -r");
735 /* TODO -r options is set in smopts, but may
736 * TODO be overwritten by setting from= in
737 * TODO an interactive session!
738 * TODO Maybe disable setting of from?
739 * TODO Warn user? Update manual!! */
740 okey
= savecat("from=", fa
->n_fullname
);
745 /* Set variable (twice) */
752 if (oargs_cnt
== oargs_size
)
753 oargs_size
= _grow_cpp(&oargs
, oargs_size
+ 8, oargs_cnt
);
754 oargs
[oargs_cnt
++] = okey
;
759 options
|= OPT_SENDMODE
;
762 /* Read defined set of headers from mail to be sent */
763 options
|= OPT_SENDMODE
| OPT_t_FLAG
;
766 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
767 * done as necessary in _setup_vars() above */
771 puts(ok_vlook(version
));
776 ok_bset(verbose
, TRU1
);
780 /* Add to list of commands to exec before entering normal operation */
781 if (Xargs_cnt
== Xargs_size
)
782 Xargs_size
= _grow_cpp(&Xargs
, Xargs_size
+ 8, Xargs_cnt
);
783 Xargs
[Xargs_cnt
++] = _oarg
;
786 /* Control which resource files shall be loaded */
787 if(!(resfiles
& (a_RF_SET
| a_RF_SYSTEM
))){
788 emsg
= N_("-n cannot be used in conjunction with -:");
792 while((i
= *_oarg
++) != '\0')
794 case 'S': case 's': resfiles
|= a_RF_SYSTEM
; break;
795 case 'U': case 'u': resfiles
|= a_RF_USER
; break;
796 case ':': case '/': resfiles
&= ~a_RF_ALL
; break;
798 emsg
= N_("Invalid argument of -:");
803 /* Enable tilde escapes even in non-interactive mode */
804 options
|= OPT_TILDE_FLAG
;
807 /* Work in batch mode, even if non-interactive */
808 if (oargs_cnt
+ 5 >= oargs_size
)
809 oargs_size
= _grow_cpp(&oargs
, oargs_size
+ 8, oargs_cnt
);
810 options
|= OPT_TILDE_FLAG
| OPT_BATCH_FLAG
;
811 folder
= "/dev/null";
812 ok_bset(emptystart
, TRU1
);
813 ok_bset(header
, FAL0
);
814 ok_bset(quiet
, TRU1
);
815 ok_bset(sendwait
, TRU1
);
816 ok_vset(MBOX
, folder
);
817 oargs
[oargs_cnt
+ 0] = "emptystart";
818 oargs
[oargs_cnt
+ 1] = "noheader";
819 oargs
[oargs_cnt
+ 2] = "quiet";
820 oargs
[oargs_cnt
+ 3] = "sendwait";
821 oargs
[oargs_cnt
+ 4] = "MBOX=/dev/null";
825 options
|= OPT_SENDMODE
;
830 n_err("%s\n", V_(emsg
));
831 n_err(V_(usagestr
) _USAGE_ARGS
);
833 exit_status
= EXIT_USE
;
840 /* The normal arguments may be followed by MTA arguments after a "--";
841 * however, -f may take off an argument, too, and before that.
842 * Since MTA arguments after "--" require *expandargv*, delay parsing off
843 * those options until after the resource files are loaded... */
844 if ((cp
= argv
[i
= _oind
]) == NULL
)
846 else if (cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0')
848 /* OPT_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
849 else if (folder
!= NULL
&& folder
[1] == '\0') {
851 if ((cp
= argv
[++i
]) != NULL
) {
852 if (cp
[0] != '-' || cp
[1] != '-' || cp
[2] != '\0') {
853 emsg
= N_("More than one file given with -f");
859 options
|= OPT_SENDMODE
;
861 to
= cat(to
, lextract(cp
, GTO
| GFULL
));
862 if ((cp
= argv
[++i
]) == NULL
)
864 if (cp
[0] == '-' && cp
[1] == '-' && cp
[2] == '\0') {
872 /* ...BUT, since we use salloc() for the MTA smopts storage we need to
873 * allocate the necessary space for them before we call spreserve()! */
874 while (argv
[i
] != NULL
)
876 if (smopts_cnt
+ i
> smopts_size
)
877 DBG(smopts_size
=) _grow_cpp(&smopts
, smopts_cnt
+ i
+ 1, smopts_cnt
);
879 /* Check for inconsistent arguments */
880 if (options
& OPT_SENDMODE
) {
881 /* XXX This is only because BATCH_FLAG sets *folder*=/dev/null
882 * XXX in order to function. Ideally that would not be needed */
883 if (folder
!= NULL
&& !(options
& OPT_BATCH_FLAG
)) {
884 emsg
= N_("Cannot give -f and people to send to.");
887 if (myname
!= NULL
) {
888 emsg
= N_("The -u option cannot be used in send mode");
891 if (!(options
& OPT_t_FLAG
) && to
== NULL
) {
892 emsg
= N_("Send options without primary recipient specified.");
895 if ((options
& OPT_t_FLAG
) && qf
!= NULL
) {
896 emsg
= N_("-t and -q are mutual exclusive.");
899 if (options
& (OPT_EXISTONLY
| OPT_HEADERSONLY
| OPT_HEADERLIST
)) {
900 emsg
= N_("The -e, -H and -L options cannot be used in send mode.");
903 if (options
& OPT_R_FLAG
) {
904 emsg
= N_("The -R option is meaningless in send mode.");
908 if (folder
!= NULL
&& myname
!= NULL
) {
909 emsg
= N_("The options -f and -u are mutually exclusive");
912 if ((options
& (OPT_EXISTONLY
| OPT_HEADERSONLY
)) ==
913 (OPT_EXISTONLY
| OPT_HEADERSONLY
)) {
914 emsg
= N_("The options -e and -H are mutual exclusive");
917 if ((options
& (OPT_HEADERSONLY
| OPT_HEADERLIST
)) == /* TODO OBSOLETE */
918 (OPT_HEADERSONLY
| OPT_HEADERLIST
))
919 OBSOLETE(_("please use \"-e -L xy\" instead of \"-H -L xy\""));
923 * Likely to go, perform more setup
928 if (options
& OPT_INTERACTIVE
) {
929 /* Now we can finally check wether -q- was given */
930 if (qf
== (char*)-1) {
931 emsg
= N_("In interactive mode -q cannot use standard input \"-\"\n");
937 # ifndef TTY_WANTS_SIGWINCH
938 if (safe_signal(SIGWINCH
, SIG_IGN
) != SIG_IGN
)
940 safe_signal(SIGWINCH
, _setscreensize
);
943 scrnheight
= realscreenheight
= 24, scrnwidth
= 80;
945 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
948 /* load() any resource files */
949 if(resfiles
& a_RF_ALL
){
950 /* *expand() returns a savestr(), but load only uses the file name for
951 * fopen(), so it's safe to do this */
952 if((resfiles
& a_RF_SYSTEM
) && !env_blook("NAIL_NO_SYSTEM_RC", TRU1
))
953 n_load(SYSCONFDIR
"/" SYSCONFRC
);
954 if(resfiles
& a_RF_USER
){
955 if((cp
= env_vlook("MAILRC", TRU1
)) == NULL
)
956 cp
= UNCONST(MAILRC
);
957 n_load(file_expand(cp
));
959 if(env_vlook("NAIL_EXTRA_RC", TRU1
) == NULL
&&
960 (cp
= ok_vlook(NAIL_EXTRA_RC
)) != NULL
)
961 n_load(file_expand(cp
));
964 /* Ensure the -S and other command line options take precedence over
965 * anything that may have been placed in resource files.
966 * Our "ternary binary" option *verbose* needs special treament */
967 if ((options
& (OPT_VERB
| OPT_VERBVERB
)) == OPT_VERB
)
968 options
&= ~OPT_VERB
;
969 /* ..and be silent when unsetting an undefined variable */
971 for (i
= 0; UICMP(z
, i
, <, oargs_cnt
); ++i
) {
979 /* Additional options to pass-through to MTA, and allowed to do so? */
980 if ((cp
= ok_vlook(expandargv
)) != NULL
) {
981 bool_t isfail
= !asccasecmp(cp
, "fail"),
982 isrestrict
= (!isfail
&& !asccasecmp(cp
, "restrict"));
984 if ((options
& OPT_D_V
) && !isfail
&& !isrestrict
&& *cp
!= '\0')
985 n_err(_("Unknown *expandargv* value: \"%s\"\n"), cp
);
987 if ((cp
= argv
[i
= _oind
]) != NULL
) {
989 (isrestrict
&& !(options
& (OPT_INTERACTIVE
|OPT_TILDE_FLAG
)))) {
990 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
991 "using *sendmail-arguments*\n"));
992 exit_status
= EXIT_USE
| EXIT_SEND_ERROR
;
996 assert(smopts_cnt
+ 1 <= smopts_size
);
997 smopts
[smopts_cnt
++] = cp
;
998 } while ((cp
= argv
[++i
]) != NULL
);
1002 /* We had to wait until the resource files are loaded and any command line
1003 * setting has been restored, but get the termcap up and going before we
1004 * switch account or running commands */
1006 if((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
)
1010 /* Now we can set the account */
1018 /* "load()" commands given on command line */
1020 Xargs
[Xargs_cnt
] = NULL
;
1021 n_load_Xargs(Xargs
);
1025 * We're finally completely setup and ready to go
1027 pstate
|= PS_STARTED
;
1029 if (options
& OPT_DEBUG
)
1030 n_err(_("user = %s, homedir = %s\n"), myname
, homedir
);
1032 if (!(options
& OPT_SENDMODE
)) {
1033 exit_status
= _rcv_mode(folder
, Larg
);
1037 /* Now that full mailx(1)-style file expansion is possible handle the
1038 * attachments which we had delayed due to this.
1039 * This may use savestr(), but since we won't enter the command loop we
1040 * don't need to care about that */
1041 for (cp
= NULL
; a_head
!= NULL
;) {
1042 struct attachment
*nahp
, *nap
;
1044 if ((nahp
= add_attachment(attach
, a_head
->aa_file
, &nap
)) != NULL
) {
1046 /* Did we split a charset set name for fixation purposes? */
1048 nap
->a_conv
= AC_FIX_INCS
;
1049 nap
->a_input_charset
= cp
;
1052 a_head
= a_head
->aa_next
;
1057 /* It may not have worked because of an appended character set name, so
1058 * try to split name and charset and retry once */
1059 if (cp
== NULL
&& (cp
= strrchr(a_head
->aa_file
, '=')) != NULL
) {
1060 char *ncp
, *nfp
= savestrbuf(a_head
->aa_file
,
1061 PTR2SIZE(cp
- a_head
->aa_file
));
1063 for (ncp
= ++cp
; *ncp
!= '\0'; ++ncp
)
1064 if (!alnumchar(*ncp
) && !punctchar(*ncp
))
1067 a_head
->aa_file
= nfp
;
1072 n_perr(a_head
->aa_file
, i
);
1073 exit_status
= EXIT_ERR
;
1077 if (options
& OPT_INTERACTIVE
)
1079 mail(to
, cc
, bcc
, subject
, attach
, qf
, ((options
& OPT_F_FLAG
) != 0));
1080 if (options
& OPT_INTERACTIVE
)
1085 if((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
)
1086 n_termcap_destroy();
1100 c_exit(void *v
){/* TODO program state machine */
1104 if(pstate
& PS_STARTED
){
1105 if(!(pstate
& PS_SOURCING
)){
1107 if((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) ==
1109 n_termcap_destroy();
1119 /* Source the others in that case! */
1120 #ifdef HAVE_AMALGAMATION
1121 # include "config.h"