nail.1: drop `(un)?setenv' and *bsdset*; add `environ'
[s-mailx.git] / main.c
blob93c1d334c36daf0101f71bbb38dfa7a4960e992f
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>.
7 */
8 /*
9 * Copyright (c) 1980, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. 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
34 * SUCH DAMAGE.
36 #undef n_FILE
37 #define n_FILE main
38 #define n_MAIN_SOURCE
40 #include "nail.h"
42 #include <sys/ioctl.h>
44 #include <pwd.h>
46 #ifdef HAVE_NL_LANGINFO
47 # include <langinfo.h>
48 #endif
49 #ifdef HAVE_SETLOCALE
50 # include <locale.h>
51 #endif
53 struct a_arg {
54 struct a_arg *aa_next;
55 char *aa_file;
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) */
103 static char *_oarg;
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);
112 /* Grow a char** */
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);
134 static int
135 _getopt(int argc, char * const argv[], char const *optstring)
137 static char const *lastp;
139 int rv = -1/*, colon*/;
140 char const *curp;
141 NYD_ENTER;
143 _oarg = NULL;
145 /*if ((colon = (optstring[0] == ':')))
146 ++optstring;*/
148 if (lastp != NULL) {
149 curp = lastp;
150 lastp = NULL;
151 } else {
152 if (_oind >= argc || argv[_oind] == NULL || argv[_oind][0] != '-' ||
153 argv[_oind][1] == '\0')
154 goto jleave;
155 if (argv[_oind][1] == '-' && argv[_oind][2] == '\0') {
156 /* We need this in for MTA arg detection (easier) ++_oind;*/
157 goto jleave;
159 curp = &argv[_oind][1];
162 for(_oopt = curp[0]; optstring[0] != '\0';){
163 if(optstring[0] != _oopt){
164 optstring += 1 + (optstring[1] == ':');
165 continue;
168 if (optstring[1] == ':') {
169 if (curp[1] != '\0') {
170 _oarg = UNCONST(curp + 1);
171 ++_oind;
172 } else {
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 ? ':' :*/ '?');
179 goto jleave;
181 _oarg = argv[_oind - 1];
183 } else {
184 if (curp[1] != '\0')
185 lastp = curp + 1;
186 else
187 ++_oind;
189 rv = _oopt;
190 goto jleave;
193 /* Special support for --help, which is quite common */
194 if (_oopt == '-' && !strcmp(curp, "-help") && curp - 1 == argv[_oind]) {
195 ++_oind;
196 rv = 'h';
197 goto jleave;
200 /* Definitive error */
201 /*if (!colon *//*&& opterr*//*)*/
202 n_err(_("%s: invalid option -- %c\n"), argv[0], _oopt);
203 if (curp[1] != '\0')
204 lastp = curp + 1;
205 else
206 ++_oind;
207 _oarg = NULL;
208 rv = '?';
209 jleave:
210 NYD_LEAVE;
211 return rv;
214 static void
215 _startup(void)
217 char *cp;
218 NYD_ENTER;
220 image = -1;
221 dflpipe = SIG_DFL;
222 _oind = /*_oerr =*/ 1;
224 if ((cp = strrchr(progname, '/')) != NULL)
225 progname = ++cp;
227 /* Set up a reasonable environment */
229 #ifdef HAVE_NYD
230 safe_signal(SIGABRT, &_nyd_oncrash);
231 # ifdef SIGBUS
232 safe_signal(SIGBUS, &_nyd_oncrash);
233 # endif
234 safe_signal(SIGFPE, &_nyd_oncrash);
235 safe_signal(SIGILL, &_nyd_oncrash);
236 safe_signal(SIGSEGV, &_nyd_oncrash);
237 #endif
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);
249 /* -- >8 -- 8< -- */
251 #ifdef HAVE_SETLOCALE
252 setlocale(LC_ALL, "");
253 mb_cur_max = MB_CUR_MAX;
254 # ifdef HAVE_NL_LANGINFO
255 if (ok_vlook(ttycharset) == NULL && (cp = nl_langinfo(CODESET)) != NULL)
256 ok_vset(ttycharset, cp);
257 # endif
259 # ifdef HAVE_C90AMEND1
260 if (mb_cur_max > 1) {
261 wchar_t wc;
262 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
263 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
264 options |= OPT_UNICODE;
265 /* Reset possibly messed up state; luckily this also gives us an
266 * indication wether the encoding has locking shift state sequences */
267 /* TODO temporary - use option bits! */
268 if (mbtowc(&wc, NULL, mb_cur_max))
269 options |= OPT_ENC_MBSTATE;
271 # endif
272 #else
273 mb_cur_max = 1;
274 #endif
276 #ifdef HAVE_ICONV
277 iconvd = (iconv_t)-1;
278 #endif
279 NYD_LEAVE;
282 static size_t
283 _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt)
285 /* Before spreserve(): use our string pool instead of LibC heap */
286 char const **newcpp;
287 NYD_ENTER;
289 newcpp = salloc(sizeof(char*) * newsize);
291 if (oldcnt > 0)
292 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
293 *cpp = newcpp;
294 NYD_LEAVE;
295 return newsize;
298 static void
299 _setup_vars(void){
300 /* XXX furtherly check paths? */
301 struct passwd *pwuid, *pw;
302 uid_t uid;
303 char const *cp;
304 bool_t doset;
305 NYD_ENTER;
307 /* Verify and fixate user identification */
308 if(myname != NULL){
309 cp = myname;
310 doset = TRU1;
311 }else if((doset = ((cp = ok_vlook(LOGNAME)) == NULL)))
312 cp = ok_vlook(USER);
314 group_id = getgid();
315 user_id = uid = getuid();
316 if((pwuid = getpwuid(uid)) == NULL)
317 n_panic(_("Cannot associate a name with uid %u"), user_id);
319 if(cp == NULL || *cp == '\0'){
320 cp = pwuid->pw_name;
321 pw = NULL;
322 doset = TRU1;
323 }else if((pw = getpwnam(cp)) == NULL){
324 n_alert(_("\"%s\" is not a user of this system"), cp);
325 exit(EXIT_NOUSER);
326 }else{
327 cp = pw->pw_name;
328 if(pw->pw_uid != uid)
329 options |= OPT_u_FLAG;
330 doset = TRU1;
333 if(doset){
334 pstate |= PS_STARTED;
335 ok_vset(LOGNAME, cp);
336 pstate &= ~PS_STARTED;
339 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
341 /* */
342 if((cp = ok_vlook(HOME)) == NULL){
343 cp = (pw != NULL) ? pw->pw_dir : pwuid->pw_dir;
344 pstate |= PS_STARTED;
345 ok_vset(HOME, cp);
346 pstate &= ~PS_STARTED;
349 (void)ok_vlook(TMPDIR);
350 NYD_LEAVE;
353 static void
354 _setscreensize(int is_sighdl) /* TODO global policy; int wraps; minvals! */
356 struct termios tbuf;
357 #ifdef TIOCGWINSZ
358 struct winsize ws;
359 #elif defined TIOCGSIZE
360 struct ttysize ts;
361 #endif
362 NYD_ENTER;
364 scrnheight = realscreenheight = scrnwidth = 0;
366 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
367 * a little bit to be able to honour resizes during our lifetime and
368 * only honour it upon first run; abuse *is_sighdl* as an indicator */
369 if (!is_sighdl) {
370 char *cp;
371 long i;
373 /* We manage those variables for our child processes, so ensure they
374 * are up to date, always */
375 if (options & OPT_INTERACTIVE)
376 pstate |= PS_SIGWINCH_PEND;
378 if ((cp = ok_vlook(LINES)) != NULL &&
379 (i = strtol(cp, NULL, 10)) > 0 && i < INT_MAX)
380 scrnheight = realscreenheight = (int)i;
381 if ((cp = ok_vlook(COLUMNS)) != NULL &&
382 (i = strtol(cp, NULL, 10)) > 0 && i < INT_MAX)
383 scrnwidth = (int)i;
385 if (scrnwidth != 0 && scrnheight != 0)
386 goto jleave;
389 #ifdef TIOCGWINSZ
390 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
391 ws.ws_col = ws.ws_row = 0;
392 #elif defined TIOCGSIZE
393 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) == -1)
394 ts.ts_lines = ts.ts_cols = 0;
395 #endif
397 if (scrnheight == 0) {
398 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) == -1)
399 ? B9600 : cfgetospeed(&tbuf));
401 if (ospeed < B1200)
402 scrnheight = 9;
403 else if (ospeed == B1200)
404 scrnheight = 14;
405 #ifdef TIOCGWINSZ
406 else if (ws.ws_row != 0)
407 scrnheight = ws.ws_row;
408 #elif defined TIOCGSIZE
409 else if (ts.ts_lines != 0)
410 scrnheight = ts.ts_lines;
411 #endif
412 else
413 scrnheight = 24;
415 #if defined TIOCGWINSZ || defined TIOCGSIZE
416 if (0 ==
417 # ifdef TIOCGWINSZ
418 (realscreenheight = ws.ws_row)
419 # else
420 (realscreenheight = ts.ts_lines)
421 # endif
423 realscreenheight = 24;
424 #endif
427 if (scrnwidth == 0 && 0 ==
428 #ifdef TIOCGWINSZ
429 (scrnwidth = ws.ws_col)
430 #elif defined TIOCGSIZE
431 (scrnwidth = ts.ts_cols)
432 #endif
434 scrnwidth = 80;
436 jleave:
437 #ifdef SIGWINCH
438 if (is_sighdl) {
439 pstate |= PS_SIGWINCH_PEND; /* XXX Not atomic */
440 if (options & OPT_INTERACTIVE)
441 n_tty_signal(SIGWINCH);
443 #endif
444 NYD_LEAVE;
447 static sigjmp_buf __hdrjmp; /* XXX */
449 static int
450 _rcv_mode(char const *folder, char const *Larg)
452 int i;
453 sighandler_type prevint;
454 NYD_ENTER;
456 if (folder == NULL)
457 folder = "%";
459 if (options & OPT_QUICKRUN_MASK)
460 i = FEDIT_RDONLY;
461 else
462 i = FEDIT_NONE;
463 i = setfile(folder, i);
464 if (i < 0) {
465 exit_status = EXIT_ERR; /* error already reported */
466 goto jquit;
468 if (options & OPT_QUICKRUN_MASK) {
469 exit_status = i;
470 if (i == EXIT_OK && (!(options & OPT_EXISTONLY) ||
471 (options & OPT_HEADERLIST)))
472 print_header_summary(Larg);
473 goto jquit;
475 check_folder_hook(FAL0);
477 if (i > 0 && !ok_blook(emptystart)) {
478 exit_status = EXIT_ERR;
479 goto jleave;
482 if (sigsetjmp(__hdrjmp, 1) == 0) {
483 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
484 safe_signal(SIGINT, _hdrstop);
485 if (!(options & OPT_N_FLAG)) {
486 if (!ok_blook(quiet))
487 printf(_("%s version %s. Type ? for help.\n"),
488 (ok_blook(bsdcompat) ? "Mail" : uagent), ok_vlook(version));
489 announce(1);
490 fflush(stdout);
492 safe_signal(SIGINT, prevint);
495 /* Enter the command loop */
496 if (options & OPT_INTERACTIVE)
497 n_tty_init();
498 n_commands();
499 if (options & OPT_INTERACTIVE)
500 n_tty_destroy();
502 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
503 safe_signal(SIGHUP, SIG_IGN);
504 safe_signal(SIGINT, SIG_IGN);
505 safe_signal(SIGQUIT, SIG_IGN);
507 jquit:
508 save_mbox_for_possible_quitstuff();
509 quit();
510 jleave:
511 NYD_LEAVE;
512 return exit_status;
515 static void
516 _hdrstop(int signo)
518 NYD_X; /* Signal handler */
519 UNUSED(signo);
521 fflush(stdout);
522 n_err_sighdl(_("\nInterrupt\n"));
523 siglongjmp(__hdrjmp, 1);
527 main(int argc, char *argv[]){
528 static char const optstr[] = "A:a:Bb:c:dEeFfHhiL:NnO:q:Rr:S:s:tu:VvX:::~#.",
529 usagestr[] = N_(
530 "Synopsis:\n"
531 " %s -h | --help\n"
532 " %s [-BdEFintv~] [-: spec] [-A account]\n"
533 "\t [-a attachment] [-b bcc-address] [-c cc-address]\n"
534 "\t [-q file] [-r from-address] [-S var[=value]..]\n"
535 "\t [-s subject] [-X cmd] [-.] to-address.. [-- mta-option..]\n"
536 " %s [-BdEeHiNnRv~#] [-: spec] [-A account]\n"
537 "\t [-L spec-list] [-r from-address] [-S var[=value]..]\n"
538 "\t [-X cmd] -f [file] [-- mta-option..]\n"
539 " %s [-BdEeHiNnRv~#] [-: spec] [-A account]\n"
540 "\t [-L spec-list] [-r from-address] [-S var[=value]..]\n"
541 "\t [-u user] [-X cmd] [-- mta-option..]\n"
543 #define _USAGE_ARGS , progname, progname, progname, progname
545 struct a_arg *a_head, *a_curr;
546 struct name *to, *cc, *bcc;
547 struct attachment *attach;
548 char *cp, *subject, *qf, *Aarg, *Larg;
549 char const *okey, **oargs, **Xargs, *folder, *emsg;
550 size_t oargs_size, oargs_cnt, Xargs_size, Xargs_cnt, smopts_size;
551 enum{
552 a_RF_NONE = 0,
553 a_RF_SET = 1<<0,
554 a_RF_SYSTEM = 1<<1,
555 a_RF_USER = 1<<2,
556 a_RF_ALL = a_RF_SYSTEM | a_RF_USER
557 } resfiles;
558 int i;
559 NYD_ENTER;
561 a_head = NULL;
562 UNINIT(a_curr, NULL);
563 to = cc = bcc = NULL;
564 attach = NULL;
565 cp = subject = qf =
566 Aarg = Larg = NULL;
567 oargs = Xargs = NULL;
568 folder = emsg = NULL;
569 oargs_size = oargs_cnt = Xargs_size = Xargs_cnt = smopts_size = 0;
570 resfiles = a_RF_ALL;
573 * Start our lengthy setup, finalize by setting PS_STARTED
576 progname = argv[0];
577 _startup();
579 /* Command line parsing
580 * Variable settings need to be done twice, since the user surely wants the
581 * setting to take effect immediately, but also doesn't want it to be
582 * overwritten from within resource files */
583 while ((i = _getopt(argc, argv, optstr)) >= 0) {
584 switch (i) {
585 case 'A':
586 /* Execute an account command later on */
587 Aarg = _oarg;
588 break;
589 case 'a':
590 options |= OPT_SENDMODE;
591 { struct a_arg *nap = salloc(sizeof(struct a_arg));
592 if (a_head == NULL)
593 a_head = nap;
594 else
595 a_curr->aa_next = nap;
596 nap->aa_next = NULL;
597 nap->aa_file = _oarg;
598 a_curr = nap;
600 break;
601 case 'B':
602 /* Make 0/1 line buffered */
603 setvbuf(stdin, NULL, _IOLBF, 0);
604 setvbuf(stdout, NULL, _IOLBF, 0);
605 break;
606 case 'b':
607 /* Get Blind Carbon Copy Recipient list */
608 options |= OPT_SENDMODE;
609 bcc = cat(bcc, lextract(_oarg, GBCC | GFULL));
610 break;
611 case 'c':
612 /* Get Carbon Copy Recipient list */
613 options |= OPT_SENDMODE;
614 cc = cat(cc, lextract(_oarg, GCC | GFULL));
615 break;
616 case 'd':
617 ok_bset(debug, TRU1);
618 okey = "debug";
619 goto joarg;
620 case 'E':
621 ok_bset(skipemptybody, TRU1);
622 okey = "skipemptybody";
623 goto joarg;
624 case 'e':
625 options |= OPT_EXISTONLY;
626 break;
627 case 'F':
628 options |= OPT_F_FLAG | OPT_SENDMODE;
629 break;
630 case 'f':
631 /* User is specifying file to "edit" with Mail, as opposed to reading
632 * system mailbox. If no argument is given, we read his mbox file.
633 * Check for remaining arguments later */
634 folder = "&";
635 break;
636 case 'H':
637 options |= OPT_HEADERSONLY;
638 break;
639 case 'h':
640 n_err(V_(usagestr) _USAGE_ARGS);
641 goto j_leave;
642 case 'i':
643 /* Ignore interrupts */
644 ok_bset(ignore, TRU1);
645 okey = "ignore";
646 goto joarg;
647 case 'L':
648 Larg = _oarg;
649 options |= OPT_HEADERLIST;
650 if (*Larg == '"' || *Larg == '\'') { /* TODO list.c:listspec_check() */
651 size_t j = strlen(++Larg);
652 if (j > 0)
653 Larg[j - 1] = '\0';
655 break;
656 case 'N':
657 /* Avoid initial header printing */
658 ok_bset(header, FAL0);
659 okey = "noheader";
660 goto joarg;
661 case 'n':
662 /* Don't source "unspecified system start-up file" */
663 if(resfiles & a_RF_SET){
664 emsg = N_("-n cannot be used in conjunction with -:");
665 goto jusage;
667 resfiles = a_RF_USER;
668 break;
669 case 'O':
670 /* Additional options to pass-through to MTA TODO v15-compat legacy */
671 if (smopts_cnt == smopts_size)
672 smopts_size = _grow_cpp(&smopts, smopts_size + 8, smopts_cnt);
673 smopts[smopts_cnt++] = _oarg;
674 break;
675 case 'q':
676 /* Quote file TODO drop? -Q with real quote?? what ? */
677 options |= OPT_SENDMODE;
678 /* Allow for now, we have to special check validity of -q- later on! */
679 qf = (_oarg[0] == '-' && _oarg[1] == '\0') ? (char*)-1 : _oarg;
680 break;
681 case 'R':
682 /* Open folders read-only */
683 options |= OPT_R_FLAG;
684 break;
685 case 'r':
686 /* Set From address. */
687 options |= OPT_r_FLAG;
688 if (_oarg[0] != '\0') {
689 struct name *fa = nalloc(_oarg, GSKIN | GFULL | GFULLEXTRA);
691 if (is_addr_invalid(fa, EACM_STRICT | EACM_NOLOG)) {
692 emsg = N_("Invalid address argument with -r");
693 goto jusage;
695 option_r_arg = fa;
696 /* TODO -r options is set in smopts, but may
697 * TODO be overwritten by setting from= in
698 * TODO an interactive session!
699 * TODO Maybe disable setting of from?
700 * TODO Warn user? Update manual!! */
701 okey = savecat("from=", fa->n_fullname);
702 goto joarg;
704 break;
705 case 'S':
706 /* Set variable (twice) */
707 { char *a[2];
708 okey = a[0] = _oarg;
709 a[1] = NULL;
710 c_set(a);
712 joarg:
713 if (oargs_cnt == oargs_size)
714 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_cnt);
715 oargs[oargs_cnt++] = okey;
716 break;
717 case 's':
718 /* Subject: */
719 subject = _oarg;
720 options |= OPT_SENDMODE;
721 break;
722 case 't':
723 /* Read defined set of headers from mail to be sent */
724 options |= OPT_SENDMODE | OPT_t_FLAG;
725 break;
726 case 'u':
727 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
728 * done as necessary in _setup_vars() above */
729 myname = _oarg;
730 break;
731 case 'V':
732 puts(ok_vlook(version));
733 exit(EXIT_OK);
734 /* NOTREACHED */
735 case 'v':
736 /* Be verbose */
737 ok_bset(verbose, TRU1);
738 okey = "verbose";
739 goto joarg;
740 case 'X':
741 /* Add to list of commands to exec before entering normal operation */
742 if (Xargs_cnt == Xargs_size)
743 Xargs_size = _grow_cpp(&Xargs, Xargs_size + 8, Xargs_cnt);
744 Xargs[Xargs_cnt++] = _oarg;
745 break;
746 case ':':
747 /* Control which resource files shall be loaded */
748 if(!(resfiles & (a_RF_SET | a_RF_SYSTEM))){
749 emsg = N_("-n cannot be used in conjunction with -:");
750 goto jusage;
752 resfiles = a_RF_SET;
753 while((i = *_oarg++) != '\0')
754 switch(i){
755 case 'S': case 's': resfiles |= a_RF_SYSTEM; break;
756 case 'U': case 'u': resfiles |= a_RF_USER; break;
757 case ':': case '/': resfiles &= ~a_RF_ALL; break;
758 default:
759 emsg = N_("Invalid argument of -:");
760 goto jusage;
762 break;
763 case '~':
764 /* Enable tilde escapes even in non-interactive mode */
765 options |= OPT_TILDE_FLAG;
766 break;
767 case '#':
768 /* Work in batch mode, even if non-interactive */
769 if (oargs_cnt + 5 >= oargs_size)
770 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_cnt);
771 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
772 folder = "/dev/null";
773 ok_bset(emptystart, TRU1);
774 ok_bset(header, FAL0);
775 ok_bset(quiet, TRU1);
776 ok_bset(sendwait, TRU1);
777 ok_vset(MBOX, folder);
778 oargs[oargs_cnt + 0] = "emptystart";
779 oargs[oargs_cnt + 1] = "noheader";
780 oargs[oargs_cnt + 2] = "quiet";
781 oargs[oargs_cnt + 3] = "sendwait";
782 oargs[oargs_cnt + 4] = "MBOX=/dev/null";
783 oargs_cnt += 5;
784 break;
785 case '.':
786 options |= OPT_SENDMODE;
787 goto jgetopt_done;
788 case '?':
789 jusage:
790 if (emsg != NULL)
791 n_err("%s\n", V_(emsg));
792 n_err(V_(usagestr) _USAGE_ARGS);
793 #undef _USAGE_ARGS
794 exit_status = EXIT_USE;
795 goto j_leave;
798 jgetopt_done:
801 /* The normal arguments may be followed by MTA arguments after a "--";
802 * however, -f may take off an argument, too, and before that.
803 * Since MTA arguments after "--" require *expandargv*, delay parsing off
804 * those options until after the resource files are loaded... */
805 if ((cp = argv[i = _oind]) == NULL)
807 else if (cp[0] == '-' && cp[1] == '-' && cp[2] == '\0')
808 ++i;
809 /* OPT_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
810 else if (folder != NULL && folder[1] == '\0') {
811 folder = cp;
812 if ((cp = argv[++i]) != NULL) {
813 if (cp[0] != '-' || cp[1] != '-' || cp[2] != '\0') {
814 emsg = N_("More than one file given with -f");
815 goto jusage;
817 ++i;
819 } else {
820 options |= OPT_SENDMODE;
821 for (;;) {
822 to = cat(to, lextract(cp, GTO | GFULL));
823 if ((cp = argv[++i]) == NULL)
824 break;
825 if (cp[0] == '-' && cp[1] == '-' && cp[2] == '\0') {
826 ++i;
827 break;
831 _oind = i;
833 /* ...BUT, since we use salloc() for the MTA smopts storage we need to
834 * allocate the necessary space for them before we call spreserve()! */
835 while (argv[i] != NULL)
836 ++i;
837 if (smopts_cnt + i > smopts_size)
838 DBG(smopts_size =) _grow_cpp(&smopts, smopts_cnt + i + 1, smopts_cnt);
840 /* Check for inconsistent arguments */
841 if (options & OPT_SENDMODE) {
842 /* XXX This is only because BATCH_FLAG sets *folder*=/dev/null
843 * XXX in order to function. Ideally that would not be needed */
844 if (folder != NULL && !(options & OPT_BATCH_FLAG)) {
845 emsg = N_("Cannot give -f and people to send to.");
846 goto jusage;
848 if (myname != NULL) {
849 emsg = N_("The -u option cannot be used in send mode");
850 goto jusage;
852 if (!(options & OPT_t_FLAG) && to == NULL) {
853 emsg = N_("Send options without primary recipient specified.");
854 goto jusage;
856 if ((options & OPT_t_FLAG) && qf != NULL) {
857 emsg = N_("-t and -q are mutual exclusive.");
858 goto jusage;
860 if (options & (OPT_EXISTONLY | OPT_HEADERSONLY | OPT_HEADERLIST)) {
861 emsg = N_("The -e, -H and -L options cannot be used in send mode.");
862 goto jusage;
864 if (options & OPT_R_FLAG) {
865 emsg = N_("The -R option is meaningless in send mode.");
866 goto jusage;
868 } else {
869 if (folder != NULL && myname != NULL) {
870 emsg = N_("The options -f and -u are mutually exclusive");
871 goto jusage;
873 if ((options & (OPT_EXISTONLY | OPT_HEADERSONLY)) ==
874 (OPT_EXISTONLY | OPT_HEADERSONLY)) {
875 emsg = N_("The options -e and -H are mutual exclusive");
876 goto jusage;
878 if ((options & (OPT_HEADERSONLY | OPT_HEADERLIST)) == /* TODO OBSOLETE */
879 (OPT_HEADERSONLY | OPT_HEADERLIST))
880 OBSOLETE(_("please use \"-e -L xy\" instead of \"-H -L xy\""));
884 * Likely to go, perform more setup
887 _setup_vars();
889 if (options & OPT_INTERACTIVE) {
890 /* Now we can finally check wether -q- was given */
891 if (qf == (char*)-1) {
892 emsg = N_("In interactive mode -q cannot use standard input \"-\"\n");
893 goto jusage;
896 _setscreensize(0);
897 #ifdef SIGWINCH
898 # ifndef TTY_WANTS_SIGWINCH
899 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
900 # endif
901 safe_signal(SIGWINCH, _setscreensize);
902 #endif
903 } else
904 scrnheight = realscreenheight = 24, scrnwidth = 80;
906 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
907 spreserve();
909 /* load() any resource files */
910 if(resfiles & a_RF_ALL){
911 /* *expand() returns a savestr(), but load only uses the file name for
912 * fopen(), so it's safe to do this */
913 if((resfiles & a_RF_SYSTEM) && !ok_blook(NAIL_NO_SYSTEM_RC))
914 n_load(SYSCONFDIR "/" SYSCONFRC);
915 if(resfiles & a_RF_USER)
916 n_load(file_expand(ok_vlook(MAILRC)));
917 if((cp = ok_vlook(NAIL_EXTRA_RC)) != NULL)
918 n_load(file_expand(cp));
921 /* Ensure the -S and other command line options take precedence over
922 * anything that may have been placed in resource files.
923 * Our "ternary binary" option *verbose* needs special treament */
924 if ((options & (OPT_VERB | OPT_VERBVERB)) == OPT_VERB)
925 options &= ~OPT_VERB;
926 /* ..and be silent when unsetting an undefined variable */
927 pstate |= PS_ROBOT;
928 for (i = 0; UICMP(z, i, <, oargs_cnt); ++i) {
929 char const *a[2];
930 a[0] = oargs[i];
931 a[1] = NULL;
932 c_set(a);
934 pstate &= ~PS_ROBOT;
936 /* Additional options to pass-through to MTA, and allowed to do so? */
937 if ((cp = ok_vlook(expandargv)) != NULL) {
938 bool_t isfail = !asccasecmp(cp, "fail"),
939 isrestrict = (!isfail && !asccasecmp(cp, "restrict"));
941 if ((options & OPT_D_V) && !isfail && !isrestrict && *cp != '\0')
942 n_err(_("Unknown *expandargv* value: \"%s\"\n"), cp);
944 if ((cp = argv[i = _oind]) != NULL) {
945 if (isfail ||
946 (isrestrict && !(options & (OPT_INTERACTIVE |OPT_TILDE_FLAG)))) {
947 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
948 "using *sendmail-arguments*\n"));
949 exit_status = EXIT_USE | EXIT_SEND_ERROR;
950 goto jleave;
952 do {
953 assert(smopts_cnt + 1 <= smopts_size);
954 smopts[smopts_cnt++] = cp;
955 } while ((cp = argv[++i]) != NULL);
959 /* We had to wait until the resource files are loaded and any command line
960 * setting has been restored, but get the termcap up and going before we
961 * switch account or running commands */
962 #ifdef n_HAVE_TCAP
963 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE)
964 n_termcap_init();
965 #endif
967 /* Now we can set the account */
968 if (Aarg != NULL) {
969 char const *a[2];
970 a[0] = Aarg;
971 a[1] = NULL;
972 c_account(a);
975 /* "load()" commands given on command line */
976 if (Xargs_cnt > 0){
977 Xargs[Xargs_cnt] = NULL;
978 n_load_Xargs(Xargs);
982 * We're finally completely setup and ready to go
984 pstate |= PS_STARTED;
986 if (options & OPT_DEBUG)
987 n_err(_("user = %s, homedir = %s\n"), myname, homedir);
989 if (!(options & OPT_SENDMODE)) {
990 exit_status = _rcv_mode(folder, Larg);
991 goto jleave;
994 /* Now that full mailx(1)-style file expansion is possible handle the
995 * attachments which we had delayed due to this.
996 * This may use savestr(), but since we won't enter the command loop we
997 * don't need to care about that */
998 for (cp = NULL; a_head != NULL;) {
999 struct attachment *nahp, *nap;
1001 if ((nahp = add_attachment(attach, a_head->aa_file, &nap)) != NULL) {
1002 attach = nahp;
1003 /* Did we split a charset set name for fixation purposes? */
1004 if (cp != NULL) {
1005 nap->a_conv = AC_FIX_INCS;
1006 nap->a_input_charset = cp;
1007 cp = NULL;
1009 a_head = a_head->aa_next;
1010 continue;
1012 i = errno;
1014 /* It may not have worked because of an appended character set name, so
1015 * try to split name and charset and retry once */
1016 if (cp == NULL && (cp = strrchr(a_head->aa_file, '=')) != NULL) {
1017 char *ncp, *nfp = savestrbuf(a_head->aa_file,
1018 PTR2SIZE(cp - a_head->aa_file));
1020 for (ncp = ++cp; *ncp != '\0'; ++ncp)
1021 if (!alnumchar(*ncp) && !punctchar(*ncp))
1022 break;
1023 if (*ncp == '\0') {
1024 a_head->aa_file = nfp;
1025 continue;
1029 n_perr(a_head->aa_file, i);
1030 exit_status = EXIT_ERR;
1031 goto jleave;
1034 if (options & OPT_INTERACTIVE)
1035 n_tty_init();
1036 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
1037 if (options & OPT_INTERACTIVE)
1038 n_tty_destroy();
1040 jleave:
1041 #ifdef n_HAVE_TCAP
1042 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE)
1043 n_termcap_destroy();
1044 #endif
1045 #ifdef HAVE_MEMORY_DEBUG
1046 sreset(FAL0);
1047 #endif
1048 j_leave:
1049 #ifdef HAVE_MEMORY_DEBUG
1050 n_memcheck();
1051 #endif
1052 NYD_LEAVE;
1053 return exit_status;
1056 FL int
1057 c_exit(void *v){/* TODO program state machine */
1058 NYD_ENTER;
1059 UNUSED(v);
1061 if(pstate & PS_STARTED){
1062 if(!(pstate & PS_SOURCING)){
1063 #ifdef n_HAVE_TCAP
1064 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) ==
1065 OPT_INTERACTIVE)
1066 n_termcap_destroy();
1067 #endif
1068 exit(EXIT_OK);
1071 pstate |= PS_EXIT;
1072 NYD_LEAVE;
1073 return 0;
1076 /* Source the others in that case! */
1077 #ifdef HAVE_AMALGAMATION
1078 # include "config.h"
1079 #endif
1081 /* s-it-mode */