THANKS: Russell Bell
[s-mailx.git] / main.c
blobf00ade53a249951478d2053f015c9ef0113bff21
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 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
7 * SPDX-License-Identifier: BSD-3-Clause TODO ISC
8 */
9 /*
10 * Copyright (c) 1980, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 #undef n_FILE
38 #define n_FILE main
39 #define n_MAIN_SOURCE
41 #include "nail.h"
43 #include <sys/ioctl.h>
45 #include <pwd.h>
47 struct a_arg{
48 struct a_arg *aa_next;
49 char const *aa_file;
52 /* (extern, but not with amalgamation, so define here) */
53 VL char const n_weekday_names[7 + 1][4] = {
54 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
56 VL char const n_month_names[12 + 1][4] = {
57 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
58 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
60 VL char const n_uagent[sizeof VAL_UAGENT] = VAL_UAGENT;
61 #ifdef HAVE_UISTRINGS
62 VL char const n_error[sizeof n_ERROR] = N_(n_ERROR);
63 #endif
64 VL char const n_path_devnull[sizeof n_PATH_DEVNULL] = n_PATH_DEVNULL;
65 VL char const n_reproducible_name[sizeof "reproducible_build"] =
66 "reproducible_build";
67 VL char const n_unirepl[sizeof n_UNIREPL] = n_UNIREPL;
68 VL char const n_empty[1] = "";
69 VL char const n_0[2] = "0";
70 VL char const n_1[2] = "1";
71 VL char const n_m1[3] = "-1";
72 VL char const n_em[2] = "!";
73 VL char const n_ns[2] = "#";
74 VL char const n_star[2] = "*";
75 VL char const n_hy[2] = "-";
76 VL char const n_qm[2] = "?";
77 VL char const n_at[2] = "@";
78 VL ui16_t const n_class_char[1 + 0x7F] = {
79 #define a_BC C_BLANK | C_CNTRL
80 #define a_SC C_SPACE | C_CNTRL
81 #define a_WC C_WHITE | C_CNTRL
82 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
83 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
84 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
85 C_CNTRL, a_BC, a_WC, a_SC, a_SC, a_SC, C_CNTRL, C_CNTRL,
86 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
87 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
88 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
89 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
90 #undef a_WC
91 #undef a_SC
92 #undef a_BC
93 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
94 C_BLANK, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
95 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
96 C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
97 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
98 C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL,
99 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
100 C_DIGIT, C_DIGIT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
101 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
102 C_PUNCT, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
103 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
104 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
105 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
106 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
107 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
108 C_UPPER, C_UPPER, C_UPPER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
109 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
110 C_PUNCT, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
111 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
112 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
113 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
114 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
115 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
116 C_LOWER, C_LOWER, C_LOWER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_CNTRL
119 /* Our own little getopt(3) */
120 static char const *a_main_oarg;
121 static int a_main_oind, /*_oerr,*/ a_main_oopt;
123 /* A little getopt(3). Note: --help/--version == -h/-v */
124 static int a_main_getopt(int argc, char * const argv[], char const *optstring);
126 /* */
127 static void a_main_usage(FILE *fp);
129 /* Perform basic startup initialization */
130 static void a_main_startup(void);
132 /* Grow a char** */
133 static size_t a_main_grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt);
135 /* Setup some variables which we require to be valid / verified */
136 static void a_main_setup_vars(void);
138 /* We're in an interactive session - compute what the screen size for printing
139 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
140 * We use the following algorithm for the height:
141 * If baud rate < 1200, use 9
142 * If baud rate = 1200, use 14
143 * If baud rate > 1200, use 24 or ws_row
144 * Width is either 80 or ws_col */
145 static void a_main_setscreensize(int is_sighdl);
147 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
148 * the system mailbox, and open up the right stuff */
149 static int a_main_rcv_mode(bool_t had_A_arg, char const *folder,
150 char const *Larg);
152 /* Interrupt printing of the headers */
153 static void a_main_hdrstop(int signo);
155 static int
156 a_main_getopt(int argc, char * const argv[], char const *optstring){
157 static char const *lastp;
158 char const *curp;
159 int rv/*, colon*/;
160 NYD2_ENTER;
162 a_main_oarg = NULL;
163 rv = -1;
165 /*if((colon = (optstring[0] == ':')))
166 ++optstring;*/
168 if(lastp != NULL){
169 curp = lastp;
170 lastp = NULL;
171 }else{
172 if(a_main_oind >= argc || argv[a_main_oind] == NULL ||
173 argv[a_main_oind][0] != '-' || argv[a_main_oind][1] == '\0')
174 goto jleave;
175 if(argv[a_main_oind][1] == '-' && argv[a_main_oind][2] == '\0'){
176 /* We need this in for MTA arg detection (easier) ++a_main_oind;*/
177 goto jleave;
179 curp = &argv[a_main_oind][1];
182 for(a_main_oopt = curp[0]; optstring[0] != '\0';){
183 if(optstring[0] != a_main_oopt){
184 optstring += 1 + (optstring[1] == ':');
185 continue;
188 if(optstring[1] == ':'){
189 if(curp[1] != '\0'){
190 a_main_oarg = n_UNCONST(curp + 1);
191 ++a_main_oind;
192 }else{
193 if((a_main_oind += 2) > argc){
194 /*if(!colon *//*&& _oerr*//*)*/{
195 n_err(_("%s: option requires an argument -- %c\n"),
196 argv[0], (char)a_main_oopt);
198 rv = (/*colon ? ':' :*/ '?');
199 goto jleave;
201 a_main_oarg = argv[a_main_oind - 1];
203 }else{
204 if(curp[1] != '\0')
205 lastp = curp + 1;
206 else
207 ++a_main_oind;
209 rv = a_main_oopt;
210 goto jleave;
213 /* Special support for --help and --version, which are quite common */
214 if(a_main_oopt == '-' && &curp[-1] == argv[a_main_oind]){
215 ++a_main_oind;
216 rv = 'h';
217 if(!strcmp(curp, "-help"))
218 goto jleave;
219 rv = 'V';
220 if(!strcmp(curp, "-version"))
221 goto jleave;
222 --a_main_oind;
225 /* Definitive error */
226 /*if(!colon *//*&& opterr*//*)*/
227 n_err(_("%s: invalid option -- %c\n"), argv[0], a_main_oopt);
228 if(curp[1] != '\0')
229 lastp = curp + 1;
230 else
231 ++a_main_oind;
232 a_main_oarg = NULL;
233 rv = '?';
234 jleave:
235 NYD2_LEAVE;
236 return rv;
239 static void
240 a_main_usage(FILE *fp){
241 /* Stay in 24 lines */
242 char buf[64];
243 size_t i;
244 NYD2_ENTER;
246 i = strlen(n_progname);
247 i = n_MIN(i, sizeof(buf) -1);
248 if(i > 0)
249 memset(buf, ' ', i);
250 buf[i] = '\0';
252 fprintf(fp, _("%s (%s %s): send and receive Internet mail\n"),
253 n_progname, n_uagent, ok_vlook(version));
254 if(fp != n_stderr)
255 putc('\n', fp);
257 fprintf(fp, _(
258 "Send-only mode: send mail \"to-address\" receiver(s):\n"
259 " %s [-DdEFinv~#] [-: spec] [-A account] [:-C \"custom: header\":]\n"
260 " %s [:-a attachment:] [:-b bcc-address:] [:-c cc-address:]\n"
261 " %s [-M type | -m file | -q file | -t] [-r from-address]\n"
262 " %s [:-S var[=value]:] [-s subject] [:-X cmd:] [-.] :to-address:\n"),
263 n_progname, buf, buf, buf);
264 if(fp != n_stderr)
265 putc('\n', fp);
267 fprintf(fp, _(
268 "\"Receive\" mode, starting on [-u user], primary *inbox* or [$MAIL]:\n"
269 " %s [-DdEeHiNnRv~#] [-: spec] [-A account] "
270 "[:-C \"custom: header\":]\n"
271 " %s [-L spec] [-r from-address] [:-S var[=value]:] [-u user] "
272 "[:-X cmd:]\n"),
273 n_progname, buf);
274 if(fp != n_stderr)
275 putc('\n', fp);
277 fprintf(fp, _(
278 "\"Receive\" mode, starting on -f (secondary $MBOX or [file]):\n"
279 " %s [-DdEeHiNnRv~#] [-: spec] [-A account] "
280 "[:-C \"custom: header\":] -f\n"
281 " %s [-L spec] [-r from-address] [:-S var[=value]:] [:-X cmd:] "
282 "[file]\n"),
283 n_progname, buf);
284 if(fp != n_stderr)
285 putc('\n', fp);
287 fprintf(fp, _(
288 ". -d sandbox, -:/ no .rc files, -. end options and force send-mode\n"
289 ". -a attachment[=input-charset[#output-charset]]\n"
290 ". -b, -c, to-address, (-r): ex@am.ple or '(Lovely) Ex <am@p.le>'\n"
291 ". -[Mmqt]: special input data (-t: template message on stdin)\n"
292 ". -e only mail check, -H header summary; "
293 "both: message specification via -L\n"
294 ". -S (un)sets variable, -X executes command(s), "
295 "-# enters batch mode\n"
296 ". Features via \"$ %s -Xversion -Xx\"\n"
297 ". Bugs/Contact via "
298 "\"$ %s -Sexpandaddr=shquote '\\$contact-mail'\"\n"),
299 n_progname, n_progname);
300 NYD2_LEAVE;
303 static void
304 a_main_startup(void){
305 char *cp;
306 NYD2_ENTER;
308 n_stdin = stdin;
309 n_stdout = stdout;
310 n_stderr = stderr;
311 dflpipe = SIG_DFL;
313 a_main_oind = /*_oerr =*/ 1;
315 if((cp = strrchr(n_progname, '/')) != NULL)
316 n_progname = ++cp;
318 #ifdef HAVE_NYD
319 safe_signal(SIGABRT, &_nyd_oncrash);
320 # ifdef SIGBUS
321 safe_signal(SIGBUS, &_nyd_oncrash);
322 # endif
323 safe_signal(SIGFPE, &_nyd_oncrash);
324 safe_signal(SIGILL, &_nyd_oncrash);
325 safe_signal(SIGSEGV, &_nyd_oncrash);
326 #endif
328 /* Initialize our input, loop and command machinery */
329 n_go_init();
331 /* Set up a reasonable environment */
333 /* TODO This is wrong: interactive is STDIN/STDERR for a POSIX sh(1).
334 * TODO For now we get this wrong, all over the place, as this software
335 * TODO has always been developed with stdout as an output channel.
336 * TODO Start doing it right for at least explicit terminal-related things,
337 * TODO but v15 should use ONLY this, also for terminal input! */
338 if(isatty(STDIN_FILENO)){
339 n_psonce |= n_PSO_TTYIN;
340 #if defined HAVE_MLE || defined HAVE_TERMCAP
341 if((n_tty_fp = fdopen(fileno(n_stdin), "w")) != NULL)
342 setvbuf(n_tty_fp, NULL, _IOLBF, 0);
343 #endif
345 if(isatty(STDOUT_FILENO))
346 n_psonce |= n_PSO_TTYOUT;
347 if((n_psonce & (n_PSO_TTYIN | n_PSO_TTYOUT)) ==
348 (n_PSO_TTYIN | n_PSO_TTYOUT)){
349 n_psonce |= n_PSO_INTERACTIVE;
350 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
353 /* STDOUT is always line buffered from our point of view */
354 setvbuf(n_stdout, NULL, _IOLBF, 0);
355 if(n_tty_fp == NULL)
356 n_tty_fp = n_stdout;
358 /* -- >8 -- 8< -- */
360 /* We need the endianess, runtime detected due to OPT_CROSS_BUILD */
361 /* C99 */{
362 union {ui16_t bom; ui8_t buf[2];} volatile u;
364 u.bom = 0xFEFFu;
365 if(u.buf[1] != 0xFEu)
366 n_psonce |= n_PSO_BIG_ENDIAN;
369 n_locale_init();
371 #ifdef HAVE_ICONV
372 iconvd = (iconv_t)-1;
373 #endif
375 /* Ensure some variables get loaded and/or verified */
377 (void)ok_blook(POSIXLY_CORRECT);
378 NYD2_LEAVE;
381 static size_t
382 a_main_grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt){
383 /* Just use auto-reclaimed storage, it will be preserved */
384 char const **newcpp;
385 NYD2_ENTER;
387 newcpp = n_autorec_alloc(sizeof(char*) * (newsize + 1));
389 if(oldcnt > 0)
390 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
391 *cpp = newcpp;
392 NYD2_LEAVE;
393 return newsize;
396 static void
397 a_main_setup_vars(void){
398 struct passwd *pwuid;
399 char const *cp;
400 NYD2_ENTER;
402 /* Detect, verify and fixate our invoking user (environment) */
403 n_group_id = getgid();
404 if((pwuid = getpwuid(n_user_id = getuid())) == NULL)
405 n_panic(_("Cannot associate a name with uid %lu"), (ul_i)n_user_id);
406 else{
407 char const *ep;
408 bool_t doenv;
410 if(!(doenv = (ep = ok_vlook(LOGNAME)) == NULL) &&
411 (doenv = (strcmp(pwuid->pw_name, ep) != 0)))
412 n_err(_("Warning: $LOGNAME (%s) not identical to user (%s)!\n"),
413 ep, pwuid->pw_name);
414 if(doenv){
415 n_pstate |= n_PS_ROOT;
416 ok_vset(LOGNAME, pwuid->pw_name);
417 n_pstate &= ~n_PS_ROOT;
420 /* BSD compat */
421 if((ep = ok_vlook(USER)) != NULL && strcmp(pwuid->pw_name, ep)){
422 n_err(_("Warning: $USER (%s) not identical to user (%s)!\n"),
423 ep, pwuid->pw_name);
424 n_pstate |= n_PS_ROOT;
425 ok_vset(USER, pwuid->pw_name);
426 n_pstate &= ~n_PS_ROOT;
429 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
432 /* Ensure some variables get loaded and/or verified.
433 * While doing so, take special care for invocations as root */
435 /* This is not automated just as $TMPDIR is for the initial setting, since
436 * we have the pwuid at hand and can simply use it! See accmacvar.c! */
437 if(n_user_id == 0 || (cp = ok_vlook(HOME)) == NULL){
438 cp = pwuid->pw_dir;
439 n_pstate |= n_PS_ROOT;
440 ok_vset(HOME, cp);
441 n_pstate &= ~n_PS_ROOT;
444 /* Do not honour TMPDIR if root */
445 if(n_user_id == 0)
446 ok_vset(TMPDIR, NULL);
447 else
448 (void)ok_vlook(TMPDIR);
450 /* Are we in a reproducible-builds.org environment?
451 * That special mode bends some settings (again) */
452 if(ok_vlook(SOURCE_DATE_EPOCH) != NULL){
453 n_psonce |= n_PSO_REPRODUCIBLE;
454 n_pstate |= n_PS_ROOT;
455 n_progname = n_reproducible_name;
456 ok_vset(LOGNAME, n_reproducible_name);
457 /* Do not care about USER at all in this special mode! */
458 n_pstate &= ~n_PS_ROOT;
459 cp = savecat(n_reproducible_name, ": ");
460 ok_vset(log_prefix, cp);
463 if((n_psonce & n_PSO_INTERACTIVE) ||
464 ((n_psonce & (n_PSO_TTYIN | n_PSO_TTYOUT)) &&
465 (n_poption & n_PO_BATCH_FLAG))){
466 a_main_setscreensize(FAL0);
467 if(n_psonce & n_PSO_INTERACTIVE){
468 /* XXX Yet WINCH after SIGWINCH/SIGCONT, but see POSIX TOSTOP flag */
469 #ifdef SIGWINCH
470 # ifndef TTY_WANTS_SIGWINCH
471 if(safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
472 # endif
473 safe_signal(SIGWINCH, &a_main_setscreensize);
474 #endif
475 #ifdef SIGCONT
476 safe_signal(SIGCONT, &a_main_setscreensize);
477 #endif
479 }else
480 /* $COLUMNS and $LINES defaults as documented in the manual */
481 n_scrnheight = n_realscreenheight = 24, n_scrnwidth = 80;
482 NYD2_LEAVE;
485 static void
486 a_main_setscreensize(int is_sighdl){/* TODO globl policy; int wraps; minvals! */
487 struct termios tbuf;
488 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
489 struct winsize ws;
490 #elif defined TIOCGSIZE
491 struct ttysize ts;
492 #endif
493 NYD2_ENTER;
494 assert((n_psonce & n_PSO_INTERACTIVE) || (n_poption & n_PO_BATCH_FLAG));
496 n_scrnheight = n_realscreenheight = n_scrnwidth = 0;
498 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
499 * a little bit to be able to honour resizes during our lifetime and
500 * only honour it upon first run; abuse *is_sighdl* as an indicator */
501 if(!is_sighdl){
502 char const *cp;
503 bool_t hadl, hadc;
505 if((hadl = ((cp = ok_vlook(LINES)) != NULL))){
506 n_idec_ui32_cp(&n_scrnheight, cp, 0, NULL);
507 n_realscreenheight = n_scrnheight;
509 if((hadc = ((cp = ok_vlook(COLUMNS)) != NULL)))
510 n_idec_ui32_cp(&n_scrnwidth, cp, 0, NULL);
512 if(n_scrnwidth != 0 && n_scrnheight != 0)
513 goto jleave;
515 /* In non-interactive mode, stop now, except for the documented case that
516 * both are set but not both have been usable */
517 if(!(n_psonce & n_PSO_INTERACTIVE) && (!hadl || !hadc)){
518 n_scrnheight = n_realscreenheight = 24;
519 n_scrnwidth = 80;
520 goto jleave;
524 #ifdef HAVE_TCGETWINSIZE
525 if(tcgetwinsize(fileno(n_tty_fp), &ws) == -1)
526 ws.ws_col = ws.ws_row = 0;
527 #elif defined TIOCGWINSZ
528 if(ioctl(fileno(n_tty_fp), TIOCGWINSZ, &ws) == -1)
529 ws.ws_col = ws.ws_row = 0;
530 #elif defined TIOCGSIZE
531 if(ioctl(fileno(n_tty_fp), TIOCGSIZE, &ws) == -1)
532 ts.ts_lines = ts.ts_cols = 0;
533 #endif
535 if(n_scrnheight == 0){
536 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
537 if(ws.ws_row != 0)
538 n_scrnheight = ws.ws_row;
539 #elif defined TIOCGSIZE
540 if(ts.ts_lines != 0)
541 n_scrnheight = ts.ts_lines;
542 #endif
543 else{
544 speed_t ospeed;
546 ospeed = ((tcgetattr(fileno(n_tty_fp), &tbuf) == -1)
547 ? B9600 : cfgetospeed(&tbuf));
549 if(ospeed < B1200)
550 n_scrnheight = 9;
551 else if(ospeed == B1200)
552 n_scrnheight = 14;
553 else
554 n_scrnheight = 24;
557 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ || defined TIOCGSIZE
558 if(0 ==
559 # if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
560 (n_realscreenheight = ws.ws_row)
561 # else
562 (n_realscreenheight = ts.ts_lines)
563 # endif
565 n_realscreenheight = 24;
566 #endif
569 if(n_scrnwidth == 0 && 0 ==
570 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
571 (n_scrnwidth = ws.ws_col)
572 #elif defined TIOCGSIZE
573 (n_scrnwidth = ts.ts_cols)
574 #endif
576 n_scrnwidth = 80;
578 /**/
579 n_pstate |= n_PS_SIGWINCH_PEND;
580 jleave:
581 NYD2_LEAVE;
584 static sigjmp_buf a_main__hdrjmp; /* XXX */
586 static int
587 a_main_rcv_mode(bool_t had_A_arg, char const *folder, char const *Larg){
588 sighandler_type prevint;
589 int i;
590 NYD_ENTER;
592 i = had_A_arg ? FEDIT_ACCOUNT : FEDIT_NONE;
593 if(n_poption & n_PO_QUICKRUN_MASK)
594 i |= FEDIT_RDONLY;
596 if(folder == NULL){
597 folder = "%";
598 if(had_A_arg)
599 i |= FEDIT_SYSBOX;
601 #ifdef HAVE_IMAP
602 else if(*folder == '@'){
603 /* This must be treated specially to make possible invocation like
604 * -A imap -f @mailbox */
605 char const *cp;
607 cp = n_folder_query();
608 if(which_protocol(cp, FAL0, FAL0, NULL) == PROTO_IMAP)
609 n_strscpy(mailname, cp, sizeof mailname);
611 #endif
613 i = setfile(folder, i);
614 if(i < 0){
615 n_exit_status = n_EXIT_ERR; /* error already reported */
616 goto jquit;
618 temporary_folder_hook_check(FAL0);
619 if(n_poption & n_PO_QUICKRUN_MASK){
620 n_exit_status = i;
621 if(i == n_EXIT_OK && (!(n_poption & n_PO_EXISTONLY) ||
622 (n_poption & n_PO_HEADERLIST)))
623 print_header_summary(Larg);
624 goto jquit;
627 if(i > 0 && !ok_blook(emptystart)){
628 n_exit_status = n_EXIT_ERR;
629 goto jleave;
632 if(sigsetjmp(a_main__hdrjmp, 1) == 0){
633 if((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
634 safe_signal(SIGINT, &a_main_hdrstop);
635 if(!ok_blook(quiet))
636 fprintf(n_stdout, _("%s version %s. Type `?' for help\n"),
637 n_uagent,
638 (n_psonce & n_PSO_REPRODUCIBLE
639 ? n_reproducible_name : ok_vlook(version)));
640 n_folder_announce(n_ANNOUNCE_MAIN_CALL | n_ANNOUNCE_CHANGE);
641 safe_signal(SIGINT, prevint);
644 /* Enter the command loop */
645 if(n_psonce & n_PSO_INTERACTIVE)
646 n_tty_init();
647 n_go_main_loop();
648 if(n_psonce & n_PSO_INTERACTIVE)
649 n_tty_destroy((n_psonce & n_PSO_XIT) != 0);
651 if(!(n_psonce & n_PSO_XIT)){
652 if(mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR){
653 safe_signal(SIGHUP, SIG_IGN);
654 safe_signal(SIGINT, SIG_IGN);
655 safe_signal(SIGQUIT, SIG_IGN);
657 jquit:
658 save_mbox_for_possible_quitstuff();
659 quit(FAL0);
661 jleave:
662 NYD_LEAVE;
663 return n_exit_status;
666 static void
667 a_main_hdrstop(int signo){
668 NYD_X; /* Signal handler */
669 n_UNUSED(signo);
671 fflush(n_stdout);
672 n_err_sighdl(_("\nInterrupt\n"));
673 siglongjmp(a_main__hdrjmp, 1);
677 main(int argc, char *argv[]){
678 /* TODO Once v15 control flow/carrier rewrite took place main() should
679 * TODO be rewritten and option parsing++ should be outsourced.
680 * TODO Like so we can get rid of some stack locals etc. */
681 /* Keep in SYNC: ./nail.1:"SYNOPSIS, main() */
682 static char const optstr[] =
683 "A:a:Bb:C:c:DdEeFfHhiL:M:m:NnO:q:Rr:S:s:tu:VvX:::~#.";
684 int i;
685 char *cp;
686 enum{
687 a_RF_NONE = 0,
688 a_RF_SET = 1<<0,
689 a_RF_SYSTEM = 1<<1,
690 a_RF_USER = 1<<2,
691 a_RF_ALL = a_RF_SYSTEM | a_RF_USER
692 } resfiles;
693 size_t Xargs_size, Xargs_cnt, smopts_size;
694 char const *Aarg, *emsg, *folder, *Larg, *okey, *qf,
695 *subject, *uarg, **Xargs;
696 struct attachment *attach;
697 struct name *to, *cc, *bcc;
698 struct a_arg *a_head, *a_curr;
699 NYD_ENTER;
701 a_head = NULL;
702 n_UNINIT(a_curr, NULL);
703 to = cc = bcc = NULL;
704 attach = NULL;
705 Aarg = emsg = folder = Larg = okey = qf = subject = uarg = NULL;
706 Xargs = NULL;
707 Xargs_size = Xargs_cnt = smopts_size = 0;
708 resfiles = a_RF_ALL;
711 * Start our lengthy setup, finalize by setting n_PSO_STARTED
714 n_progname = argv[0];
715 a_main_startup();
717 /* Command line parsing
718 * -S variable settings need to be done twice, since the user surely wants
719 * the setting to take effect immediately, but also doesn't want it to be
720 * overwritten from within resource files */
721 while((i = a_main_getopt(argc, argv, optstr)) >= 0){
722 switch(i){
723 case 'A':
724 /* Execute an account command later on */
725 Aarg = a_main_oarg;
726 break;
727 case 'a':{
728 /* Add an attachment */
729 struct a_arg *nap;
731 n_psonce |= n_PSO_SENDMODE;
732 nap = n_autorec_alloc(sizeof(struct a_arg));
733 if(a_head == NULL)
734 a_head = nap;
735 else
736 a_curr->aa_next = nap;
737 nap->aa_next = NULL;
738 nap->aa_file = a_main_oarg;
739 a_curr = nap;
740 }break;
741 case 'B':
742 n_OBSOLETE(_("-B is obsolete, please use -# as necessary"));
743 break;
744 case 'b':
745 /* Add (a) blind carbon copy recipient (list) */
746 n_psonce |= n_PSO_SENDMODE;
747 bcc = cat(bcc, lextract(a_main_oarg,
748 GBCC | GFULL | GSHEXP_PARSE_HACK));
749 break;
750 case 'C':{
751 /* Create custom header (at list tail) */
752 struct n_header_field **hflpp;
754 if(*(hflpp = &n_poption_arg_C) != NULL){
755 while((*hflpp)->hf_next != NULL)
756 *hflpp = (*hflpp)->hf_next;
757 hflpp = &(*hflpp)->hf_next;
759 if(!n_header_add_custom(hflpp, a_main_oarg, FAL0)){
760 emsg = N_("Invalid custom header data with -C");
761 goto jusage;
763 }break;
764 case 'c':
765 /* Add (a) carbon copy recipient (list) */
766 n_psonce |= n_PSO_SENDMODE;
767 cc = cat(cc, lextract(a_main_oarg, GCC | GFULL | GSHEXP_PARSE_HACK));
768 break;
769 case 'D':
770 #ifdef HAVE_IMAP
771 ok_bset(disconnected);
772 #endif
773 break;
774 case 'd':
775 ok_bset(debug);
776 break;
777 case 'E':
778 ok_bset(skipemptybody);
779 break;
780 case 'e':
781 /* Check if mail (matching -L) exists in given box, exit status */
782 n_poption |= n_PO_EXISTONLY;
783 break;
784 case 'F':
785 /* Save msg in file named after local part of first recipient */
786 n_poption |= n_PO_F_FLAG;
787 n_psonce |= n_PSO_SENDMODE;
788 break;
789 case 'f':
790 /* User is specifying file to "edit" with Mail, as opposed to reading
791 * system mailbox. If no argument is given, we read his mbox file.
792 * Check for remaining arguments later */
793 folder = "&";
794 break;
795 case 'H':
796 /* Display summary of headers, exit */
797 n_poption |= n_PO_HEADERSONLY;
798 break;
799 case 'h':
800 a_main_usage(n_stdout);
801 goto j_leave;
802 case 'i':
803 /* Ignore interrupts */
804 ok_bset(ignore);
805 break;
806 case 'L':
807 /* Display summary of headers which match given spec, exit.
808 * In conjunction with -e, only test the given spec for existence */
809 Larg = a_main_oarg;
810 n_poption |= n_PO_HEADERLIST;
811 if(*Larg == '"' || *Larg == '\''){ /* TODO list.c:listspec_check() */
812 size_t j;
814 j = strlen(++Larg);
815 if(j > 0){
816 cp = savestrbuf(Larg, --j);
817 Larg = cp;
820 break;
821 case 'M':
822 /* Flag message body (standard input) with given MIME type */
823 if(qf != NULL && (!(n_poption & n_PO_Mm_FLAG) || qf != (char*)-1))
824 goto jeMmq;
825 n_poption_arg_Mm = a_main_oarg;
826 qf = (char*)-1;
827 if(0){
828 /* FALLTHRU*/
829 case 'm':
830 /* Flag the given file with MIME type and use as message body */
831 if(qf != NULL && (!(n_poption & n_PO_Mm_FLAG) || qf == (char*)-1))
832 goto jeMmq;
833 qf = a_main_oarg;
835 n_poption |= n_PO_Mm_FLAG;
836 n_psonce |= n_PSO_SENDMODE;
837 break;
838 case 'N':
839 /* Avoid initial header printing */
840 ok_bclear(header);
841 break;
842 case 'n':
843 /* Don't source "unspecified system start-up file" */
844 if(resfiles & a_RF_SET){
845 emsg = N_("-n cannot be used in conjunction with -:");
846 goto jusage;
848 resfiles = a_RF_USER;
849 break;
850 case 'O':
851 /* Additional options to pass-through to MTA TODO v15-compat legacy */
852 if(n_smopts_cnt == smopts_size)
853 smopts_size = a_main_grow_cpp(&n_smopts, smopts_size + 8,
854 n_smopts_cnt);
855 n_smopts[n_smopts_cnt++] = a_main_oarg;
856 break;
857 case 'q':
858 /* "Quote" file: use as message body (-t without headers etc.) */
859 /* XXX Traditional. Add -Q to initialize as *quote*d content? */
860 if(qf != NULL && (n_poption & n_PO_Mm_FLAG)){
861 jeMmq:
862 emsg = N_("Only one of -M, -m or -q may be given");
863 goto jusage;
865 n_psonce |= n_PSO_SENDMODE;
866 /* Allow for now, we have to special check validity of -q- later on! */
867 qf = (a_main_oarg[0] == '-' && a_main_oarg[1] == '\0')
868 ? (char*)-1 : a_main_oarg;
869 break;
870 case 'R':
871 /* Open folders read-only */
872 n_poption |= n_PO_R_FLAG;
873 break;
874 case 'r':
875 /* Set From address. */
876 n_poption |= n_PO_r_FLAG;
877 if(a_main_oarg[0] == '\0')
878 break;
879 else{
880 struct name *fa;
882 fa = nalloc(a_main_oarg, GSKIN | GFULL | GFULLEXTRA);
883 if(is_addr_invalid(fa, EACM_STRICT | EACM_NOLOG)){
884 emsg = N_("Invalid address argument with -r");
885 goto jusage;
887 n_poption_arg_r = fa;
888 /* TODO -r options is set in n_smopts, but may
889 * TODO be overwritten by setting from= in
890 * TODO an interactive session!
891 * TODO Maybe disable setting of from?
892 * TODO Warn user? Update manual!! */
893 a_main_oarg = savecat("from=", fa->n_fullname);
895 /* FALLTHRU */
896 case 'S':
897 { struct str sin;
898 struct n_string s, *sp;
899 char const *a[2];
900 bool_t b;
902 if(!ok_blook(v15_compat)){
903 okey = a[0] = a_main_oarg;
904 sp = NULL;
905 }else{
906 enum n_shexp_state shs;
908 n_autorec_relax_create();
909 sp = n_string_creat_auto(&s);
910 sin.s = n_UNCONST(a_main_oarg);
911 sin.l = UIZ_MAX;
912 shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
913 n_SHEXP_PARSE_IGNORE_EMPTY |
914 n_SHEXP_PARSE_QUOTE_AUTO_FIXED |
915 n_SHEXP_PARSE_QUOTE_AUTO_DSQ), sp, &sin, NULL);
916 if((shs & n_SHEXP_STATE_ERR_MASK) ||
917 !(shs & n_SHEXP_STATE_STOP)){
918 n_autorec_relax_gut();
919 goto je_S;
921 okey = a[0] = n_string_cp_const(sp);
924 a[1] = NULL;
925 n_poption |= n_PO_S_FLAG_TEMPORARY;
926 n_pstate |= n_PS_ROBOT;
927 b = (c_set(a) == 0);
928 n_pstate &= ~n_PS_ROBOT;
929 n_poption &= ~n_PO_S_FLAG_TEMPORARY;
931 if(sp != NULL)
932 n_autorec_relax_gut();
933 if(!b && (ok_blook(errexit) || ok_blook(posix))){
934 je_S:
935 emsg = N_("-S failed to set variable");
936 goto jusage;
939 break;
940 case 's':
941 /* Subject:; take care for Debian #419840 and strip any \r and \n */
942 if(n_anyof_cp("\n\r", subject = a_main_oarg)){
943 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
944 for(subject = cp = savestr(a_main_oarg); *cp != '\0'; ++cp)
945 if(*cp == '\n' || *cp == '\r')
946 *cp = ' ';
948 n_psonce |= n_PSO_SENDMODE;
949 break;
950 case 't':
951 /* Use the given message as send template */
952 n_poption |= n_PO_t_FLAG;
953 n_psonce |= n_PSO_SENDMODE;
954 break;
955 case 'u':
956 /* Open primary mailbox of the given user */
957 uarg = savecat("%", a_main_oarg);
958 break;
959 case 'V':
960 fprintf(n_stdout, _("%s version %s\n"), n_uagent, ok_vlook(version));
961 n_exit_status = n_EXIT_OK;
962 goto j_leave;
963 case 'v':
964 /* Be verbose */
965 ok_bset(verbose);
966 break;
967 case 'X':
968 /* Add to list of commands to exec before entering normal operation */
969 if(Xargs_cnt == Xargs_size)
970 Xargs_size = a_main_grow_cpp(&Xargs, Xargs_size + 8, Xargs_cnt);
971 Xargs[Xargs_cnt++] = a_main_oarg;
972 break;
973 case ':':
974 /* Control which resource files shall be loaded */
975 if(!(resfiles & (a_RF_SET | a_RF_SYSTEM))){
976 emsg = N_("-n cannot be used in conjunction with -:");
977 goto jusage;
979 resfiles = a_RF_SET;
980 while((i = *a_main_oarg++) != '\0')
981 switch(i){
982 case 'S': case 's': resfiles |= a_RF_SYSTEM; break;
983 case 'U': case 'u': resfiles |= a_RF_USER; break;
984 case ':': case '/': resfiles &= ~a_RF_ALL; break;
985 default:
986 emsg = N_("Invalid argument of -:");
987 goto jusage;
989 break;
990 case '~':
991 /* Enable command escapes even in non-interactive mode */
992 n_poption |= n_PO_TILDE_FLAG;
993 break;
994 case '#':
995 /* Work in batch mode, even if non-interactive */
996 if(!(n_psonce & n_PSO_INTERACTIVE))
997 setvbuf(n_stdin, NULL, _IOLBF, 0);
998 n_poption |= n_PO_TILDE_FLAG | n_PO_BATCH_FLAG;
999 folder = n_path_devnull;
1000 n_pstate |= n_PS_ROBOT; /* (be silent unsetting undefined variables) */
1001 ok_vset(MAIL, folder);
1002 ok_vset(MBOX, folder);
1003 ok_bset(emptystart);
1004 ok_bclear(errexit);
1005 ok_bclear(header);
1006 ok_vset(inbox, folder);
1007 ok_bclear(posix);
1008 ok_bset(quiet);
1009 ok_bset(sendwait);
1010 ok_bset(typescript_mode);
1011 n_pstate &= ~n_PS_ROBOT;
1012 break;
1013 case '.':
1014 /* Enforce send mode */
1015 n_psonce |= n_PSO_SENDMODE;
1016 goto jgetopt_done;
1017 case '?':
1018 jusage:
1019 if(emsg != NULL)
1020 n_err("%s\n", V_(emsg));
1021 a_main_usage(n_stderr);
1022 n_exit_status = n_EXIT_USE;
1023 goto j_leave;
1026 jgetopt_done:
1029 /* The normal arguments may be followed by MTA arguments after a "--";
1030 * however, -f may take off an argument, too, and before that.
1031 * Since MTA arguments after "--" require *expandargv*, delay parsing off
1032 * those options until after the resource files are loaded... */
1033 if((cp = argv[i = a_main_oind]) == NULL)
1035 else if(cp[0] == '-' && cp[1] == '-' && cp[2] == '\0')
1036 ++i;
1037 /* n_PO_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
1038 else if(folder != NULL && /*folder[0] == '&' &&*/ folder[1] == '\0'){
1039 folder = cp;
1040 if((cp = argv[++i]) != NULL){
1041 if(cp[0] != '-' || cp[1] != '-' || cp[2] != '\0'){
1042 emsg = N_("More than one file given with -f");
1043 goto jusage;
1045 ++i;
1047 }else{
1048 n_psonce |= n_PSO_SENDMODE;
1049 for(;;){
1050 to = cat(to, lextract(cp, GTO | GFULL | GSHEXP_PARSE_HACK));
1051 if((cp = argv[++i]) == NULL)
1052 break;
1053 if(cp[0] == '-' && cp[1] == '-' && cp[2] == '\0'){
1054 ++i;
1055 break;
1059 a_main_oind = i;
1061 /* ...BUT, since we use n_autorec_alloc() for the MTA n_smopts storage we
1062 * need to allocate the space for them before we fixate that storage! */
1063 while(argv[i] != NULL)
1064 ++i;
1065 if(n_smopts_cnt + i > smopts_size)
1066 DBG(smopts_size =)
1067 a_main_grow_cpp(&n_smopts, n_smopts_cnt + i + 1, n_smopts_cnt);
1069 /* Check for inconsistent arguments, fix some temporaries */
1070 if(n_psonce & n_PSO_SENDMODE){
1071 /* XXX This is only because BATCH_FLAG sets *folder*=/dev/null
1072 * XXX in order to function. Ideally that would not be needed */
1073 if(folder != NULL && !(n_poption & n_PO_BATCH_FLAG)){
1074 emsg = N_("Cannot give -f and people to send to.");
1075 goto jusage;
1077 if(uarg != NULL){
1078 emsg = N_("The -u option cannot be used in send mode");
1079 goto jusage;
1081 if(!(n_poption & n_PO_t_FLAG) && to == NULL){
1082 emsg = N_("Send options without primary recipient specified.");
1083 goto jusage;
1085 if((n_poption & n_PO_t_FLAG) && qf != NULL){
1086 emsg = N_("The -M, -m, -q and -t options are mutual exclusive.");
1087 goto jusage;
1089 if(n_poption & (n_PO_EXISTONLY | n_PO_HEADERSONLY | n_PO_HEADERLIST)){
1090 emsg = N_("The -e, -H and -L options cannot be used in send mode.");
1091 goto jusage;
1093 if(n_poption & n_PO_R_FLAG){
1094 emsg = N_("The -R option is meaningless in send mode.");
1095 goto jusage;
1098 if(n_psonce & n_PSO_INTERACTIVE){
1099 if(qf == (char*)-1){
1100 if(!(n_poption & n_PO_Mm_FLAG))
1101 emsg = N_("-q can't use standard input when interactive.\n");
1102 goto jusage;
1105 }else{
1106 if(uarg != NULL && folder != NULL){
1107 emsg = N_("The options -u and -f (and -#) are mutually exclusive");
1108 goto jusage;
1110 if((n_poption & (n_PO_EXISTONLY | n_PO_HEADERSONLY)) ==
1111 (n_PO_EXISTONLY | n_PO_HEADERSONLY)){
1112 emsg = N_("The options -e and -H are mutual exclusive");
1113 goto jusage;
1115 if((n_poption & (n_PO_HEADERSONLY | n_PO_HEADERLIST) /* TODO OBSOLETE */
1116 ) == (n_PO_HEADERSONLY | n_PO_HEADERLIST))
1117 n_OBSOLETE(_("please use \"-e -L xy\" instead of \"-H -L xy\""));
1119 if(uarg != NULL)
1120 folder = uarg;
1124 * We have reached our second program state, the command line options have
1125 * been worked and verified a bit, we are likely to go, perform more setup
1127 n_psonce |= n_PSO_STARTED_GETOPT;
1129 a_main_setup_vars();
1131 /* Create memory pool snapshot; Memory is auto-reclaimed from now on */
1132 n_memory_pool_fixate();
1134 /* load() any resource files */
1135 if(resfiles & a_RF_ALL){
1136 /* *expand() returns a savestr(), but load() only uses the file name
1137 * for fopen(), so it is safe to do this */
1138 if(resfiles & a_RF_SYSTEM){
1139 bool_t nload;
1141 if((nload = ok_blook(NAIL_NO_SYSTEM_RC)))
1142 n_OBSOLETE(_("Please use $MAILX_NO_SYSTEM_RC instead of "
1143 "$NAIL_NO_SYSTEM_RC"));
1144 if(!nload && !ok_blook(MAILX_NO_SYSTEM_RC) &&
1145 !n_go_load(ok_vlook(system_mailrc)))
1146 goto j_leave;
1149 if((resfiles & a_RF_USER) &&
1150 !n_go_load(fexpand(ok_vlook(MAILRC), FEXP_LOCAL | FEXP_NOPROTO)))
1151 goto j_leave;
1153 if((cp = ok_vlook(NAIL_EXTRA_RC)) != NULL)
1154 n_OBSOLETE(_("Please use *mailx-extra-rc*, not *NAIL_EXTRA_RC*"));
1155 if((cp != NULL || (cp = ok_vlook(mailx_extra_rc)) != NULL) &&
1156 !n_go_load(fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)))
1157 goto j_leave;
1160 /* Cause possible umask(2) to be applied, now that any setting is
1161 * established, and before we change accounts, evaluate commands etc. */
1162 (void)ok_vlook(umask);
1164 /* Additional options to pass-through to MTA, and allowed to do so? */
1165 i = a_main_oind;
1166 if((cp = ok_vlook(expandargv)) != NULL){
1167 bool_t isfail, isrestrict;
1169 isfail = !asccasecmp(cp, "fail");
1170 isrestrict = (!isfail && !asccasecmp(cp, "restrict"));
1172 if((n_poption & n_PO_D_V) && !isfail && !isrestrict && *cp != '\0')
1173 n_err(_("Unknown *expandargv* value: %s\n"), cp);
1175 if((cp = argv[i]) != NULL){
1176 if(isfail || (isrestrict && (!(n_poption & n_PO_TILDE_FLAG) ||
1177 !(n_psonce & n_PSO_INTERACTIVE)))){
1178 je_expandargv:
1179 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
1180 "using *mta-arguments*\n"));
1181 n_exit_status = n_EXIT_USE | n_EXIT_SEND_ERROR;
1182 goto jleave;
1185 assert(n_smopts_cnt + 1 <= smopts_size);
1186 n_smopts[n_smopts_cnt++] = cp;
1187 }while((cp = argv[++i]) != NULL);
1189 }else if(argv[i] != NULL)
1190 goto je_expandargv;
1192 /* We had to wait until the resource files are loaded and any command line
1193 * setting has been restored, but get the termcap up and going before we
1194 * switch account or running commands */
1195 #ifdef n_HAVE_TCAP
1196 if((n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_QUICKRUN_MASK))
1197 n_termcap_init();
1198 #endif
1200 /* Now we can set the account */
1201 if(Aarg != NULL){
1202 char const *a[2];
1204 a[0] = Aarg;
1205 a[1] = NULL;
1206 if(c_account(a) && (!(n_psonce & n_PSO_INTERACTIVE) ||
1207 ok_blook(errexit) || ok_blook(posix))){
1208 n_exit_status = n_EXIT_USE | n_EXIT_SEND_ERROR;
1209 goto jleave;
1214 * Almost setup, only -X commands are missing!
1216 n_psonce |= n_PSO_STARTED_CONFIG;
1218 /* "load()" commands given on command line */
1219 if(Xargs_cnt > 0){
1220 if(!n_go_Xargs(Xargs, Xargs_cnt))
1221 goto jleave;
1224 /* Final tests */
1225 if(n_poption & n_PO_Mm_FLAG){
1226 if(qf == (char*)-1){
1227 if(!n_mimetype_check_mtname(n_poption_arg_Mm)){
1228 n_err(_("Could not find `mimetype' for -M argument: %s\n"),
1229 n_poption_arg_Mm);
1230 n_exit_status = n_EXIT_ERR;
1231 goto jleave;
1233 }else if(/* XXX only to satisfy Coverity! */qf != NULL &&
1234 (n_poption_arg_Mm = n_mimetype_classify_filename(qf)) == NULL){
1235 n_err(_("Could not `mimetype'-classify -m argument: %s\n"),
1236 n_shexp_quote_cp(qf, FAL0));
1237 n_exit_status = n_EXIT_ERR;
1238 goto jleave;
1239 }else if(!asccasecmp(n_poption_arg_Mm, "text/plain")) /* TODO no: magic */
1240 n_poption_arg_Mm = NULL;
1244 * We're finally completely setup and ready to go!
1246 n_psonce |= n_PSO_STARTED;
1248 if(!(n_psonce & n_PSO_SENDMODE))
1249 n_exit_status = a_main_rcv_mode((Aarg != NULL), folder, Larg);
1250 else{
1251 /* Now that full mailx(1)-style file expansion is possible handle the
1252 * attachments which we had delayed due to this.
1253 * This may use savestr(), but since we won't enter the command loop we
1254 * don't need to care about that */
1255 for(; a_head != NULL; a_head = a_head->aa_next){
1256 enum n_attach_error aerr;
1258 attach = n_attachment_append(attach, a_head->aa_file, &aerr, NULL);
1259 if(aerr != n_ATTACH_ERR_NONE){
1260 n_exit_status = n_EXIT_ERR;
1261 goto jleave;
1265 if(n_psonce & n_PSO_INTERACTIVE)
1266 n_tty_init();
1267 n_mail((n_poption & n_PO_F_FLAG ? n_MAILSEND_RECORD_RECIPIENT : 0),
1268 to, cc, bcc, subject, attach, qf);
1269 if(n_psonce & n_PSO_INTERACTIVE)
1270 n_tty_destroy((n_psonce & n_PSO_XIT) != 0);
1273 jleave:
1274 /* Be aware of identical code for `exit' command! */
1275 #ifdef n_HAVE_TCAP
1276 if((n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_QUICKRUN_MASK))
1277 n_termcap_destroy();
1278 #endif
1280 j_leave:
1281 #if defined HAVE_MEMORY_DEBUG || defined HAVE_NOMEMDBG
1282 n_memory_pool_pop(NULL, TRU1);
1283 #endif
1284 #if defined HAVE_DEBUG || defined HAVE_DEVEL || defined HAVE_NOMEMDBG
1285 n_memory_reset();
1286 #endif
1287 NYD_LEAVE;
1288 return n_exit_status;
1291 /* Source the others in that case! */
1292 #ifdef HAVE_AMALGAMATION
1293 # include <mk-config.h>
1294 #endif
1296 /* s-it-mode */