n_shell_quote(): rewrite, tweak user-at-a-glance experience
[s-mailx.git] / main.c
blobd60e8d7f16fbb25945395e62087bb3efad21af4b
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 ui16_t const class_char[1 + 0x7F] = {
68 #define a_BC C_BLANK | C_CNTRL
69 #define a_SC C_SPACE | C_CNTRL
70 #define a_WC C_WHITE | C_CNTRL
71 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
72 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
73 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
74 C_CNTRL, a_BC, a_WC, a_SC, a_SC, a_SC, C_CNTRL, C_CNTRL,
75 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
76 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
77 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
78 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
79 #undef a_WC
80 #undef a_SC
81 #undef a_BC
82 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
83 C_BLANK, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
84 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
85 C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
86 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
87 C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL,
88 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
89 C_DIGIT, C_DIGIT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
90 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
91 C_PUNCT, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
92 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
93 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
94 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
95 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
96 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
97 C_UPPER, C_UPPER, C_UPPER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
98 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
99 C_PUNCT, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
100 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
101 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
102 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
103 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
104 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
105 C_LOWER, C_LOWER, C_LOWER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_CNTRL
108 /* Our own little getopt(3) */
109 static char *_oarg;
110 static int _oind, /*_oerr,*/ _oopt;
112 /* Our own little getopt(3); note --help is special-treated as 'h' */
113 static int _getopt(int argc, char * const argv[], char const *optstring);
115 /* Perform basic startup initialization */
116 static void _startup(void);
118 /* Grow a char** */
119 static size_t _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt);
121 /* Setup some variables which we require to be valid / verified */
122 static void _setup_vars(void);
124 /* We're in an interactive session - compute what the screen size for printing
125 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
126 * We use the following algorithm for the height:
127 * If baud rate < 1200, use 9
128 * If baud rate = 1200, use 14
129 * If baud rate > 1200, use 24 or ws_row
130 * Width is either 80 or ws_col */
131 static void _setscreensize(int is_sighdl);
133 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
134 * the system mailbox, and open up the right stuff */
135 static int _rcv_mode(char const *folder, char const *Larg);
137 /* Interrupt printing of the headers */
138 static void _hdrstop(int signo);
140 static int
141 _getopt(int argc, char * const argv[], char const *optstring)
143 static char const *lastp;
145 int rv = -1/*, colon*/;
146 char const *curp;
147 NYD_ENTER;
149 _oarg = NULL;
151 /*if ((colon = (optstring[0] == ':')))
152 ++optstring;*/
154 if (lastp != NULL) {
155 curp = lastp;
156 lastp = NULL;
157 } else {
158 if (_oind >= argc || argv[_oind] == NULL || argv[_oind][0] != '-' ||
159 argv[_oind][1] == '\0')
160 goto jleave;
161 if (argv[_oind][1] == '-' && argv[_oind][2] == '\0') {
162 /* We need this in for MTA arg detection (easier) ++_oind;*/
163 goto jleave;
165 curp = &argv[_oind][1];
168 for(_oopt = curp[0]; optstring[0] != '\0';){
169 if(optstring[0] != _oopt){
170 optstring += 1 + (optstring[1] == ':');
171 continue;
174 if (optstring[1] == ':') {
175 if (curp[1] != '\0') {
176 _oarg = UNCONST(curp + 1);
177 ++_oind;
178 } else {
179 if ((_oind += 2) > argc) {
180 /*if (!colon *//*&& _oerr*//*)*/ {
181 n_err(_("%s: option requires an argument -- %c\n"),
182 argv[0], (char)_oopt);
184 rv = (/*colon ? ':' :*/ '?');
185 goto jleave;
187 _oarg = argv[_oind - 1];
189 } else {
190 if (curp[1] != '\0')
191 lastp = curp + 1;
192 else
193 ++_oind;
195 rv = _oopt;
196 goto jleave;
199 /* Special support for --help, which is quite common */
200 if (_oopt == '-' && !strcmp(curp, "-help") && curp - 1 == argv[_oind]) {
201 ++_oind;
202 rv = 'h';
203 goto jleave;
206 /* Definitive error */
207 /*if (!colon *//*&& opterr*//*)*/
208 n_err(_("%s: invalid option -- %c\n"), argv[0], _oopt);
209 if (curp[1] != '\0')
210 lastp = curp + 1;
211 else
212 ++_oind;
213 _oarg = NULL;
214 rv = '?';
215 jleave:
216 NYD_LEAVE;
217 return rv;
220 static void
221 _startup(void)
223 char *cp;
224 NYD_ENTER;
226 image = -1;
227 dflpipe = SIG_DFL;
228 _oind = /*_oerr =*/ 1;
230 if ((cp = strrchr(progname, '/')) != NULL)
231 progname = ++cp;
233 /* Set up a reasonable environment */
235 #ifdef HAVE_NYD
236 safe_signal(SIGABRT, &_nyd_oncrash);
237 # ifdef SIGBUS
238 safe_signal(SIGBUS, &_nyd_oncrash);
239 # endif
240 safe_signal(SIGFPE, &_nyd_oncrash);
241 safe_signal(SIGILL, &_nyd_oncrash);
242 safe_signal(SIGSEGV, &_nyd_oncrash);
243 #endif
244 command_manager_start();
246 if (isatty(STDIN_FILENO)) /* TODO should be isatty(0) && isatty(2)?? */
247 options |= OPT_TTYIN;
248 if (isatty(STDOUT_FILENO))
249 options |= OPT_TTYOUT;
250 if ((options & (OPT_TTYIN | OPT_TTYOUT)) == (OPT_TTYIN | OPT_TTYOUT)) {
251 options |= OPT_INTERACTIVE;
252 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
255 /* -- >8 -- 8< -- */
257 #ifdef HAVE_SETLOCALE
258 setlocale(LC_ALL, "");
259 mb_cur_max = MB_CUR_MAX;
260 # ifdef HAVE_NL_LANGINFO
261 if (ok_vlook(ttycharset) == NULL && (cp = nl_langinfo(CODESET)) != NULL)
262 ok_vset(ttycharset, cp);
263 # endif
265 # ifdef HAVE_C90AMEND1
266 if (mb_cur_max > 1) {
267 wchar_t wc;
268 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
269 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
270 options |= OPT_UNICODE;
271 /* Reset possibly messed up state; luckily this also gives us an
272 * indication wether the encoding has locking shift state sequences */
273 /* TODO temporary - use option bits! */
274 if (mbtowc(&wc, NULL, mb_cur_max))
275 options |= OPT_ENC_MBSTATE;
277 # endif
278 #else
279 mb_cur_max = 1;
280 #endif
282 #ifdef HAVE_ICONV
283 iconvd = (iconv_t)-1;
284 #endif
285 NYD_LEAVE;
288 static size_t
289 _grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt)
291 /* Before spreserve(): use our string pool instead of LibC heap */
292 char const **newcpp;
293 NYD_ENTER;
295 newcpp = salloc(sizeof(char*) * newsize);
297 if (oldcnt > 0)
298 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
299 *cpp = newcpp;
300 NYD_LEAVE;
301 return newsize;
304 static void
305 _setup_vars(void){
306 /* XXX furtherly check paths? */
307 struct passwd *pwuid;
308 char const *cp;
309 NYD_ENTER;
311 group_id = getgid();
312 if((pwuid = getpwuid(user_id = getuid())) == NULL)
313 n_panic(_("Cannot associate a name with uid %lu"), (ul_i)user_id);
314 myname = savestr(pwuid->pw_name); /* XXX replace uses with vlook(LOGNAME)! */
316 /* C99 */{
317 char const *ep;
318 bool_t doenv;
320 if(!(doenv = (ep = ok_vlook(LOGNAME)) == NULL) &&
321 (doenv = strcmp(myname, ep)))
322 n_err(_("Warning: $LOGNAME=\"%s\" not identical to user (\"%s\")!\n"),
323 ep, myname);
324 if(doenv){
325 pstate |= PS_ROOT;
326 ok_vset(LOGNAME, myname);
327 pstate &= ~PS_ROOT;
330 if((ep = ok_vlook(USER)) != NULL && strcmp(myname, ep)){
331 n_err(_("Warning: $USER=\"%s\" not identical to user (\"%s\")!\n"),
332 ep, myname);
333 pstate |= PS_ROOT;
334 ok_vset(USER, myname);
335 pstate &= ~PS_ROOT;
339 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
341 /* */
342 if((cp = ok_vlook(HOME)) == NULL){
343 cp = pwuid->pw_dir;
344 pstate |= PS_ROOT;
345 ok_vset(HOME, cp);
346 pstate &= ~PS_ROOT;
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;
372 /* We manage those variables for our child processes, so ensure they
373 * are up to date, always */
374 if (options & OPT_INTERACTIVE)
375 pstate |= PS_SIGWINCH_PEND;
377 if ((cp = ok_vlook(LINES)) != NULL)
378 scrnheight = realscreenheight = (int)strtoul(cp, NULL, 0); /* TODO */
379 if ((cp = ok_vlook(COLUMNS)) != NULL)
380 scrnwidth = (int)strtoul(cp, NULL, 0); /* TODO posui32= not posnum=! */
382 if (scrnwidth != 0 && scrnheight != 0)
383 goto jleave;
386 #ifdef TIOCGWINSZ
387 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1)
388 ws.ws_col = ws.ws_row = 0;
389 #elif defined TIOCGSIZE
390 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) == -1)
391 ts.ts_lines = ts.ts_cols = 0;
392 #endif
394 if (scrnheight == 0) {
395 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) == -1)
396 ? B9600 : cfgetospeed(&tbuf));
398 if (ospeed < B1200)
399 scrnheight = 9;
400 else if (ospeed == B1200)
401 scrnheight = 14;
402 #ifdef TIOCGWINSZ
403 else if (ws.ws_row != 0)
404 scrnheight = ws.ws_row;
405 #elif defined TIOCGSIZE
406 else if (ts.ts_lines != 0)
407 scrnheight = ts.ts_lines;
408 #endif
409 else
410 scrnheight = 24;
412 #if defined TIOCGWINSZ || defined TIOCGSIZE
413 if (0 ==
414 # ifdef TIOCGWINSZ
415 (realscreenheight = ws.ws_row)
416 # else
417 (realscreenheight = ts.ts_lines)
418 # endif
420 realscreenheight = 24;
421 #endif
424 if (scrnwidth == 0 && 0 ==
425 #ifdef TIOCGWINSZ
426 (scrnwidth = ws.ws_col)
427 #elif defined TIOCGSIZE
428 (scrnwidth = ts.ts_cols)
429 #endif
431 scrnwidth = 80;
433 jleave:
434 #ifdef SIGWINCH
435 if (is_sighdl) {
436 pstate |= PS_SIGWINCH_PEND; /* XXX Not atomic */
437 if (options & OPT_INTERACTIVE)
438 n_tty_signal(SIGWINCH);
440 #endif
441 NYD_LEAVE;
444 static sigjmp_buf __hdrjmp; /* XXX */
446 static int
447 _rcv_mode(char const *folder, char const *Larg)
449 int i;
450 sighandler_type prevint;
451 NYD_ENTER;
453 if (folder == NULL)
454 folder = "%";
456 if (options & OPT_QUICKRUN_MASK)
457 i = FEDIT_RDONLY;
458 else
459 i = FEDIT_NONE;
460 i = setfile(folder, i);
461 if (i < 0) {
462 exit_status = EXIT_ERR; /* error already reported */
463 goto jquit;
465 if (options & OPT_QUICKRUN_MASK) {
466 exit_status = i;
467 if (i == EXIT_OK && (!(options & OPT_EXISTONLY) ||
468 (options & OPT_HEADERLIST)))
469 print_header_summary(Larg);
470 goto jquit;
472 check_folder_hook(FAL0);
474 if (i > 0 && !ok_blook(emptystart)) {
475 exit_status = EXIT_ERR;
476 goto jleave;
479 if (sigsetjmp(__hdrjmp, 1) == 0) {
480 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
481 safe_signal(SIGINT, _hdrstop);
482 if (!(options & OPT_N_FLAG)) {
483 if (!ok_blook(quiet))
484 printf(_("%s version %s. Type ? for help.\n"),
485 (ok_blook(bsdcompat) ? "Mail" : uagent), ok_vlook(version));
486 announce(1);
487 fflush(stdout);
489 safe_signal(SIGINT, prevint);
492 /* Enter the command loop */
493 if (options & OPT_INTERACTIVE)
494 n_tty_init();
495 n_commands();
496 if (options & OPT_INTERACTIVE)
497 n_tty_destroy();
499 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
500 safe_signal(SIGHUP, SIG_IGN);
501 safe_signal(SIGINT, SIG_IGN);
502 safe_signal(SIGQUIT, SIG_IGN);
504 jquit:
505 save_mbox_for_possible_quitstuff();
506 quit();
507 jleave:
508 NYD_LEAVE;
509 return exit_status;
512 static void
513 _hdrstop(int signo)
515 NYD_X; /* Signal handler */
516 UNUSED(signo);
518 fflush(stdout);
519 n_err_sighdl(_("\nInterrupt\n"));
520 siglongjmp(__hdrjmp, 1);
524 main(int argc, char *argv[]){
525 static char const optstr[] = "A:a:Bb:c:dEeFfHhiL:NnO:q:Rr:S:s:tu:VvX:::~#.",
526 usagestr[] = N_(
527 "Synopsis:\n"
528 " %s -h | --help\n"
529 " %s [-BdEFintv~] [-: spec] [-A account]\n"
530 "\t [-a attachment] [-b bcc-address] [-c cc-address]\n"
531 "\t [-q file] [-r from-address] [-S var[=value]..]\n"
532 "\t [-s subject] [-X cmd] [-.] to-address.. [-- mta-option..]\n"
533 " %s [-BdEeHiNnRv~] [-: spec] [-A account]\n"
534 "\t [-L spec-list] [-r from-address] [-S var[=value]..]\n"
535 "\t [-u user] [-X cmd] [-- mta-option..]\n"
536 " %s [-BdEeHiNnRv~#] [-: spec] [-A account] -f\n"
537 "\t [-L spec-list] [-r from-address] [-S var[=value]..]\n"
538 "\t [-X cmd] [file] [-- mta-option..]\n"
540 #define _USAGE_ARGS , progname, progname, progname, progname
542 struct a_arg *a_head, *a_curr;
543 struct name *to, *cc, *bcc;
544 struct attachment *attach;
545 char *cp, *subject, *qf, *Aarg, *Larg;
546 char const *okey, **oargs, **Xargs, *folder, *emsg;
547 size_t oargs_size, oargs_cnt, Xargs_size, Xargs_cnt, smopts_size;
548 enum{
549 a_RF_NONE = 0,
550 a_RF_SET = 1<<0,
551 a_RF_SYSTEM = 1<<1,
552 a_RF_USER = 1<<2,
553 a_RF_ALL = a_RF_SYSTEM | a_RF_USER
554 } resfiles;
555 int i;
556 NYD_ENTER;
558 a_head = NULL;
559 UNINIT(a_curr, NULL);
560 to = cc = bcc = NULL;
561 attach = NULL;
562 cp = subject = qf =
563 Aarg = Larg = NULL;
564 oargs = Xargs = NULL;
565 folder = emsg = NULL;
566 oargs_size = oargs_cnt = Xargs_size = Xargs_cnt = smopts_size = 0;
567 resfiles = a_RF_ALL;
570 * Start our lengthy setup, finalize by setting PS_STARTED
573 progname = argv[0];
574 _startup();
576 /* Command line parsing
577 * Variable settings need to be done twice, since the user surely wants the
578 * setting to take effect immediately, but also doesn't want it to be
579 * overwritten from within resource files */
580 while ((i = _getopt(argc, argv, optstr)) >= 0) {
581 switch (i) {
582 case 'A':
583 /* Execute an account command later on */
584 Aarg = _oarg;
585 break;
586 case 'a':
587 options |= OPT_SENDMODE;
588 { struct a_arg *nap = salloc(sizeof(struct a_arg));
589 if (a_head == NULL)
590 a_head = nap;
591 else
592 a_curr->aa_next = nap;
593 nap->aa_next = NULL;
594 nap->aa_file = _oarg;
595 a_curr = nap;
597 break;
598 case 'B':
599 /* Make 0/1 line buffered */
600 setvbuf(stdin, NULL, _IOLBF, 0);
601 setvbuf(stdout, NULL, _IOLBF, 0);
602 break;
603 case 'b':
604 /* Get Blind Carbon Copy Recipient list */
605 options |= OPT_SENDMODE;
606 bcc = cat(bcc, lextract(_oarg, GBCC | GFULL));
607 break;
608 case 'c':
609 /* Get Carbon Copy Recipient list */
610 options |= OPT_SENDMODE;
611 cc = cat(cc, lextract(_oarg, GCC | GFULL));
612 break;
613 case 'd':
614 ok_bset(debug, TRU1);
615 okey = "debug";
616 goto joarg;
617 case 'E':
618 ok_bset(skipemptybody, TRU1);
619 okey = "skipemptybody";
620 goto joarg;
621 case 'e':
622 options |= OPT_EXISTONLY;
623 break;
624 case 'F':
625 options |= OPT_F_FLAG | OPT_SENDMODE;
626 break;
627 case 'f':
628 /* User is specifying file to "edit" with Mail, as opposed to reading
629 * system mailbox. If no argument is given, we read his mbox file.
630 * Check for remaining arguments later */
631 folder = "&";
632 break;
633 case 'H':
634 options |= OPT_HEADERSONLY;
635 break;
636 case 'h':
637 n_err(V_(usagestr) _USAGE_ARGS);
638 goto j_leave;
639 case 'i':
640 /* Ignore interrupts */
641 ok_bset(ignore, TRU1);
642 okey = "ignore";
643 goto joarg;
644 case 'L':
645 Larg = _oarg;
646 options |= OPT_HEADERLIST;
647 if (*Larg == '"' || *Larg == '\'') { /* TODO list.c:listspec_check() */
648 size_t j = strlen(++Larg);
649 if (j > 0)
650 Larg[j - 1] = '\0';
652 break;
653 case 'N':
654 /* Avoid initial header printing */
655 ok_bset(header, FAL0);
656 okey = "noheader";
657 goto joarg;
658 case 'n':
659 /* Don't source "unspecified system start-up file" */
660 if(resfiles & a_RF_SET){
661 emsg = N_("-n cannot be used in conjunction with -:");
662 goto jusage;
664 resfiles = a_RF_USER;
665 break;
666 case 'O':
667 /* Additional options to pass-through to MTA TODO v15-compat legacy */
668 if (smopts_cnt == smopts_size)
669 smopts_size = _grow_cpp(&smopts, smopts_size + 8, smopts_cnt);
670 smopts[smopts_cnt++] = _oarg;
671 break;
672 case 'q':
673 /* Quote file TODO drop? -Q with real quote?? what ? */
674 options |= OPT_SENDMODE;
675 /* Allow for now, we have to special check validity of -q- later on! */
676 qf = (_oarg[0] == '-' && _oarg[1] == '\0') ? (char*)-1 : _oarg;
677 break;
678 case 'R':
679 /* Open folders read-only */
680 options |= OPT_R_FLAG;
681 break;
682 case 'r':
683 /* Set From address. */
684 options |= OPT_r_FLAG;
685 if (_oarg[0] != '\0') {
686 struct name *fa = nalloc(_oarg, GSKIN | GFULL | GFULLEXTRA);
688 if (is_addr_invalid(fa, EACM_STRICT | EACM_NOLOG)) {
689 emsg = N_("Invalid address argument with -r");
690 goto jusage;
692 option_r_arg = fa;
693 /* TODO -r options is set in smopts, but may
694 * TODO be overwritten by setting from= in
695 * TODO an interactive session!
696 * TODO Maybe disable setting of from?
697 * TODO Warn user? Update manual!! */
698 okey = savecat("from=", fa->n_fullname);
699 goto joarg;
701 break;
702 case 'S':
703 /* Set variable (twice) */
704 { char *a[2];
705 okey = a[0] = _oarg;
706 a[1] = NULL;
707 c_set(a);
709 joarg:
710 if (oargs_cnt == oargs_size)
711 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_cnt);
712 oargs[oargs_cnt++] = okey;
713 break;
714 case 's':
715 /* Subject: */
716 subject = _oarg;
717 options |= OPT_SENDMODE;
718 break;
719 case 't':
720 /* Read defined set of headers from mail to be sent */
721 options |= OPT_SENDMODE | OPT_t_FLAG;
722 break;
723 case 'u':
724 /* Temporarily set myname so that we can recognize the -u condition */
725 myname = savecat("%", _oarg);
726 break;
727 case 'V':
728 puts(ok_vlook(version));
729 exit(EXIT_OK);
730 /* NOTREACHED */
731 case 'v':
732 /* Be verbose */
733 ok_bset(verbose, TRU1);
734 okey = "verbose";
735 goto joarg;
736 case 'X':
737 /* Add to list of commands to exec before entering normal operation */
738 if (Xargs_cnt == Xargs_size)
739 Xargs_size = _grow_cpp(&Xargs, Xargs_size + 8, Xargs_cnt);
740 Xargs[Xargs_cnt++] = _oarg;
741 break;
742 case ':':
743 /* Control which resource files shall be loaded */
744 if(!(resfiles & (a_RF_SET | a_RF_SYSTEM))){
745 emsg = N_("-n cannot be used in conjunction with -:");
746 goto jusage;
748 resfiles = a_RF_SET;
749 while((i = *_oarg++) != '\0')
750 switch(i){
751 case 'S': case 's': resfiles |= a_RF_SYSTEM; break;
752 case 'U': case 'u': resfiles |= a_RF_USER; break;
753 case ':': case '/': resfiles &= ~a_RF_ALL; break;
754 default:
755 emsg = N_("Invalid argument of -:");
756 goto jusage;
758 break;
759 case '~':
760 /* Enable tilde escapes even in non-interactive mode */
761 options |= OPT_TILDE_FLAG;
762 break;
763 case '#':
764 /* Work in batch mode, even if non-interactive */
765 if (oargs_cnt + 5 >= oargs_size)
766 oargs_size = _grow_cpp(&oargs, oargs_size + 8, oargs_cnt);
767 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
768 folder = "/dev/null";
769 ok_bset(emptystart, TRU1);
770 ok_bset(header, FAL0);
771 ok_bset(quiet, TRU1);
772 ok_bset(sendwait, TRU1);
773 ok_vset(MBOX, folder);
774 oargs[oargs_cnt + 0] = "emptystart";
775 oargs[oargs_cnt + 1] = "noheader";
776 oargs[oargs_cnt + 2] = "quiet";
777 oargs[oargs_cnt + 3] = "sendwait";
778 oargs[oargs_cnt + 4] = "MBOX=/dev/null";
779 oargs_cnt += 5;
780 break;
781 case '.':
782 options |= OPT_SENDMODE;
783 goto jgetopt_done;
784 case '?':
785 jusage:
786 if (emsg != NULL)
787 n_err("%s\n", V_(emsg));
788 n_err(V_(usagestr) _USAGE_ARGS);
789 #undef _USAGE_ARGS
790 exit_status = EXIT_USE;
791 goto j_leave;
794 jgetopt_done:
797 /* The normal arguments may be followed by MTA arguments after a "--";
798 * however, -f may take off an argument, too, and before that.
799 * Since MTA arguments after "--" require *expandargv*, delay parsing off
800 * those options until after the resource files are loaded... */
801 if ((cp = argv[i = _oind]) == NULL)
803 else if (cp[0] == '-' && cp[1] == '-' && cp[2] == '\0')
804 ++i;
805 /* OPT_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
806 else if (folder != NULL && /*folder[0] == '&' &&*/ folder[1] == '\0') {
807 folder = cp;
808 if ((cp = argv[++i]) != NULL) {
809 if (cp[0] != '-' || cp[1] != '-' || cp[2] != '\0') {
810 emsg = N_("More than one file given with -f");
811 goto jusage;
813 ++i;
815 } else {
816 options |= OPT_SENDMODE;
817 for (;;) {
818 to = cat(to, lextract(cp, GTO | GFULL));
819 if ((cp = argv[++i]) == NULL)
820 break;
821 if (cp[0] == '-' && cp[1] == '-' && cp[2] == '\0') {
822 ++i;
823 break;
827 _oind = i;
829 /* ...BUT, since we use salloc() for the MTA smopts storage we need to
830 * allocate the necessary space for them before we call spreserve()! */
831 while (argv[i] != NULL)
832 ++i;
833 if (smopts_cnt + i > smopts_size)
834 DBG(smopts_size =) _grow_cpp(&smopts, smopts_cnt + i + 1, smopts_cnt);
836 /* Check for inconsistent arguments, fix some temporaries */
837 if (options & OPT_SENDMODE) {
838 /* XXX This is only because BATCH_FLAG sets *folder*=/dev/null
839 * XXX in order to function. Ideally that would not be needed */
840 if (folder != NULL && !(options & OPT_BATCH_FLAG)) {
841 emsg = N_("Cannot give -f and people to send to.");
842 goto jusage;
844 if (myname != NULL) {
845 emsg = N_("The -u option cannot be used in send mode");
846 goto jusage;
848 if (!(options & OPT_t_FLAG) && to == NULL) {
849 emsg = N_("Send options without primary recipient specified.");
850 goto jusage;
852 if ((options & OPT_t_FLAG) && qf != NULL) {
853 emsg = N_("-t and -q are mutual exclusive.");
854 goto jusage;
856 if (options & (OPT_EXISTONLY | OPT_HEADERSONLY | OPT_HEADERLIST)) {
857 emsg = N_("The -e, -H and -L options cannot be used in send mode.");
858 goto jusage;
860 if (options & OPT_R_FLAG) {
861 emsg = N_("The -R option is meaningless in send mode.");
862 goto jusage;
864 } else {
865 if (myname != NULL && folder != NULL) {
866 emsg = N_("The options -u and -f (and -#) are mutually exclusive");
867 goto jusage;
869 if ((options & (OPT_EXISTONLY | OPT_HEADERSONLY)) ==
870 (OPT_EXISTONLY | OPT_HEADERSONLY)) {
871 emsg = N_("The options -e and -H are mutual exclusive");
872 goto jusage;
874 if ((options & (OPT_HEADERSONLY | OPT_HEADERLIST)) == /* TODO OBSOLETE */
875 (OPT_HEADERSONLY | OPT_HEADERLIST))
876 OBSOLETE(_("please use \"-e -L xy\" instead of \"-H -L xy\""));
878 if(myname != NULL)
879 folder = myname;
883 * Likely to go, perform more setup
886 _setup_vars();
888 if (options & OPT_INTERACTIVE) {
889 /* Now we can finally check wether -q- was given */
890 if (qf == (char*)-1) {
891 emsg = N_("In interactive mode -q cannot use standard input \"-\"\n");
892 goto jusage;
895 _setscreensize(0);
896 #ifdef SIGWINCH
897 # ifndef TTY_WANTS_SIGWINCH
898 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
899 # endif
900 safe_signal(SIGWINCH, &_setscreensize);
901 #endif
902 } else
903 scrnheight = realscreenheight = 24, scrnwidth = 80;
905 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
906 spreserve();
908 /* load() any resource files */
909 if(resfiles & a_RF_ALL){
910 /* *expand() returns a savestr(), but load only uses the file name for
911 * fopen(), so it's safe to do this */
912 if((resfiles & a_RF_SYSTEM) && !ok_blook(NAIL_NO_SYSTEM_RC))
913 n_load(SYSCONFDIR "/" SYSCONFRC);
914 if(resfiles & a_RF_USER)
915 n_load(file_expand(ok_vlook(MAILRC)));
916 if((cp = ok_vlook(NAIL_EXTRA_RC)) != NULL)
917 n_load(file_expand(cp));
920 /* Ensure the -S and other command line options take precedence over
921 * anything that may have been placed in resource files.
922 * Our "ternary binary" option *verbose* needs special treament */
923 if ((options & (OPT_VERB | OPT_VERBVERB)) == OPT_VERB)
924 options &= ~OPT_VERB;
925 /* ..and be silent when unsetting an undefined variable */
926 pstate |= PS_ROBOT;
927 for (i = 0; UICMP(z, i, <, oargs_cnt); ++i) {
928 char const *a[2];
929 a[0] = oargs[i];
930 a[1] = NULL;
931 c_set(a);
933 pstate &= ~PS_ROBOT;
935 /* Additional options to pass-through to MTA, and allowed to do so? */
936 if ((cp = ok_vlook(expandargv)) != NULL) {
937 bool_t isfail = !asccasecmp(cp, "fail"),
938 isrestrict = (!isfail && !asccasecmp(cp, "restrict"));
940 if ((options & OPT_D_V) && !isfail && !isrestrict && *cp != '\0')
941 n_err(_("Unknown *expandargv* value: \"%s\"\n"), cp);
943 if ((cp = argv[i = _oind]) != NULL) {
944 if (isfail ||
945 (isrestrict && !(options & (OPT_INTERACTIVE |OPT_TILDE_FLAG)))) {
946 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
947 "using *sendmail-arguments*\n"));
948 exit_status = EXIT_USE | EXIT_SEND_ERROR;
949 goto jleave;
951 do {
952 assert(smopts_cnt + 1 <= smopts_size);
953 smopts[smopts_cnt++] = cp;
954 } while ((cp = argv[++i]) != NULL);
958 /* We had to wait until the resource files are loaded and any command line
959 * setting has been restored, but get the termcap up and going before we
960 * switch account or running commands */
961 #ifdef n_HAVE_TCAP
962 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE)
963 n_termcap_init();
964 #endif
966 /* Now we can set the account */
967 if (Aarg != NULL) {
968 char const *a[2];
969 a[0] = Aarg;
970 a[1] = NULL;
971 c_account(a);
974 /* "load()" commands given on command line */
975 if (Xargs_cnt > 0){
976 Xargs[Xargs_cnt] = NULL;
977 n_load_Xargs(Xargs);
981 * We're finally completely setup and ready to go
983 pstate |= PS_STARTED;
985 if (!(options & OPT_SENDMODE)) {
986 exit_status = _rcv_mode(folder, Larg);
987 goto jleave;
990 /* Now that full mailx(1)-style file expansion is possible handle the
991 * attachments which we had delayed due to this.
992 * This may use savestr(), but since we won't enter the command loop we
993 * don't need to care about that */
994 for (cp = NULL; a_head != NULL;) {
995 struct attachment *nahp, *nap;
997 if ((nahp = add_attachment(attach, a_head->aa_file, &nap)) != NULL) {
998 attach = nahp;
999 /* Did we split a charset set name for fixation purposes? */
1000 if (cp != NULL) {
1001 nap->a_conv = AC_FIX_INCS;
1002 nap->a_input_charset = cp;
1003 cp = NULL;
1005 a_head = a_head->aa_next;
1006 continue;
1008 i = errno;
1010 /* It may not have worked because of an appended character set name, so
1011 * try to split name and charset and retry once */
1012 if (cp == NULL && (cp = strrchr(a_head->aa_file, '=')) != NULL) {
1013 char *ncp, *nfp = savestrbuf(a_head->aa_file,
1014 PTR2SIZE(cp - a_head->aa_file));
1016 for (ncp = ++cp; *ncp != '\0'; ++ncp)
1017 if (!alnumchar(*ncp) && !punctchar(*ncp))
1018 break;
1019 if (*ncp == '\0') {
1020 a_head->aa_file = nfp;
1021 continue;
1025 n_perr(a_head->aa_file, i);
1026 exit_status = EXIT_ERR;
1027 goto jleave;
1030 if (options & OPT_INTERACTIVE)
1031 n_tty_init();
1032 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
1033 if (options & OPT_INTERACTIVE)
1034 n_tty_destroy();
1036 jleave:
1037 #ifdef n_HAVE_TCAP
1038 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE)
1039 n_termcap_destroy();
1040 #endif
1041 #ifdef HAVE_MEMORY_DEBUG
1042 sreset(FAL0);
1043 #endif
1044 j_leave:
1045 #ifdef HAVE_MEMORY_DEBUG
1046 n_memcheck();
1047 #endif
1048 NYD_LEAVE;
1049 return exit_status;
1052 FL int
1053 c_exit(void *v){/* TODO program state machine */
1054 NYD_ENTER;
1055 UNUSED(v);
1057 if(pstate & PS_STARTED){
1058 if(!(pstate & PS_SOURCING)){
1059 #ifdef n_HAVE_TCAP
1060 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) ==
1061 OPT_INTERACTIVE)
1062 n_termcap_destroy();
1063 #endif
1064 exit(EXIT_OK);
1067 pstate |= PS_EXIT;
1068 NYD_LEAVE;
1069 return 0;
1072 /* Source the others in that case! */
1073 #ifdef HAVE_AMALGAMATION
1074 # include "config.h"
1075 #endif
1077 /* s-it-mode */