a_go_evaluate(): fix un/signed comparison
[s-mailx.git] / main.c
blobadf7f62310aaf1eed86399711555e14e04b96a8b
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 */
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 struct a_arg{
47 struct a_arg *aa_next;
48 char const *aa_file;
51 /* (extern, but not with amalgamation, so define here) */
52 VL char const n_weekday_names[7 + 1][4] = {
53 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
55 VL char const n_month_names[12 + 1][4] = {
56 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
57 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
59 VL char const n_uagent[sizeof VAL_UAGENT] = VAL_UAGENT;
60 #ifdef HAVE_UISTRINGS
61 VL char const n_error[sizeof n_ERROR] = N_(n_ERROR);
62 #endif
63 VL char const n_path_devnull[sizeof n_PATH_DEVNULL] = n_PATH_DEVNULL;
64 VL char const n_reproducible_name[sizeof "reproducible_build"] =
65 "reproducible_build";
66 VL char const n_unirepl[sizeof n_UNIREPL] = n_UNIREPL;
67 VL char const n_empty[1] = "";
68 VL char const n_0[2] = "0";
69 VL char const n_1[2] = "1";
70 VL char const n_m1[3] = "-1";
71 VL char const n_em[2] = "!";
72 VL char const n_ns[2] = "#";
73 VL char const n_star[2] = "*";
74 VL char const n_hy[2] = "-";
75 VL char const n_qm[2] = "?";
76 VL char const n_at[2] = "@";
77 VL ui16_t const n_class_char[1 + 0x7F] = {
78 #define a_BC C_BLANK | C_CNTRL
79 #define a_SC C_SPACE | C_CNTRL
80 #define a_WC C_WHITE | C_CNTRL
81 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
82 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
83 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
84 C_CNTRL, a_BC, a_WC, a_SC, a_SC, a_SC, C_CNTRL, C_CNTRL,
85 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
86 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
87 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
88 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
89 #undef a_WC
90 #undef a_SC
91 #undef a_BC
92 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
93 C_BLANK, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
94 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
95 C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
96 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
97 C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL,
98 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
99 C_DIGIT, C_DIGIT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
100 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
101 C_PUNCT, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
102 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
103 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
104 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
105 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
106 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
107 C_UPPER, C_UPPER, C_UPPER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
108 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
109 C_PUNCT, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
110 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
111 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
112 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
113 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
114 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
115 C_LOWER, C_LOWER, C_LOWER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_CNTRL
118 /* Our own little getopt(3) */
119 static char const *a_main_oarg;
120 static int a_main_oind, /*_oerr,*/ a_main_oopt;
122 /* A little getopt(3). Note: --help/--version == -h/-v */
123 static int a_main_getopt(int argc, char * const argv[], char const *optstring);
125 /* */
126 static void a_main_usage(FILE *fp);
128 /* Perform basic startup initialization */
129 static void a_main_startup(void);
131 /* Grow a char** */
132 static size_t a_main_grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt);
134 /* Setup some variables which we require to be valid / verified */
135 static void a_main_setup_vars(void);
137 /* We're in an interactive session - compute what the screen size for printing
138 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
139 * We use the following algorithm for the height:
140 * If baud rate < 1200, use 9
141 * If baud rate = 1200, use 14
142 * If baud rate > 1200, use 24 or ws_row
143 * Width is either 80 or ws_col */
144 static void a_main_setscreensize(int is_sighdl);
146 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
147 * the system mailbox, and open up the right stuff */
148 static int a_main_rcv_mode(bool_t had_A_arg, char const *folder,
149 char const *Larg);
151 /* Interrupt printing of the headers */
152 static void a_main_hdrstop(int signo);
154 static int
155 a_main_getopt(int argc, char * const argv[], char const *optstring){
156 static char const *lastp;
157 char const *curp;
158 int rv/*, colon*/;
159 NYD2_ENTER;
161 a_main_oarg = NULL;
162 rv = -1;
164 /*if((colon = (optstring[0] == ':')))
165 ++optstring;*/
167 if(lastp != NULL){
168 curp = lastp;
169 lastp = NULL;
170 }else{
171 if(a_main_oind >= argc || argv[a_main_oind] == NULL ||
172 argv[a_main_oind][0] != '-' || argv[a_main_oind][1] == '\0')
173 goto jleave;
174 if(argv[a_main_oind][1] == '-' && argv[a_main_oind][2] == '\0'){
175 /* We need this in for MTA arg detection (easier) ++a_main_oind;*/
176 goto jleave;
178 curp = &argv[a_main_oind][1];
181 for(a_main_oopt = curp[0]; optstring[0] != '\0';){
182 if(optstring[0] != a_main_oopt){
183 optstring += 1 + (optstring[1] == ':');
184 continue;
187 if(optstring[1] == ':'){
188 if(curp[1] != '\0'){
189 a_main_oarg = n_UNCONST(curp + 1);
190 ++a_main_oind;
191 }else{
192 if((a_main_oind += 2) > argc){
193 /*if(!colon *//*&& _oerr*//*)*/{
194 n_err(_("%s: option requires an argument -- %c\n"),
195 argv[0], (char)a_main_oopt);
197 rv = (/*colon ? ':' :*/ '?');
198 goto jleave;
200 a_main_oarg = argv[a_main_oind - 1];
202 }else{
203 if(curp[1] != '\0')
204 lastp = curp + 1;
205 else
206 ++a_main_oind;
208 rv = a_main_oopt;
209 goto jleave;
212 /* Special support for --help and --version, which are quite common */
213 if(a_main_oopt == '-' && &curp[-1] == argv[a_main_oind]){
214 ++a_main_oind;
215 rv = 'h';
216 if(!strcmp(curp, "-help"))
217 goto jleave;
218 rv = 'V';
219 if(!strcmp(curp, "-version"))
220 goto jleave;
221 --a_main_oind;
224 /* Definitive error */
225 /*if(!colon *//*&& opterr*//*)*/
226 n_err(_("%s: invalid option -- %c\n"), argv[0], a_main_oopt);
227 if(curp[1] != '\0')
228 lastp = curp + 1;
229 else
230 ++a_main_oind;
231 a_main_oarg = NULL;
232 rv = '?';
233 jleave:
234 NYD2_LEAVE;
235 return rv;
238 static void
239 a_main_usage(FILE *fp){
240 /* Stay in 24 lines */
241 char buf[64];
242 size_t i;
243 NYD2_ENTER;
245 i = strlen(n_progname);
246 i = n_MIN(i, sizeof(buf) -1);
247 if(i > 0)
248 memset(buf, ' ', i);
249 buf[i] = '\0';
251 fprintf(fp, _("%s (%s %s): send and receive Internet mail\n"),
252 n_progname, n_uagent, ok_vlook(version));
253 if(fp != n_stderr)
254 putc('\n', fp);
256 fprintf(fp, _(
257 "Send-only mode: send mail \"to-address\" receiver(s):\n"
258 " %s [-DdEFinv~#] [-: spec] [-A account] [:-C \"custom: header\":]\n"
259 " %s [:-a attachment:] [:-b bcc-address:] [:-c cc-address:]\n"
260 " %s [-M type | -m file | -q file | -t] [-r from-address]\n"
261 " %s [:-S var[=value]:] [-s subject] [:-X cmd:] [-.] :to-address:\n"),
262 n_progname, buf, buf, buf);
263 if(fp != n_stderr)
264 putc('\n', fp);
266 fprintf(fp, _(
267 "\"Receive\" mode, starting on [-u user], primary *inbox* or [$MAIL]:\n"
268 " %s [-DdEeHiNnRv~#] [-: spec] [-A account] "
269 "[:-C \"custom: header\":]\n"
270 " %s [-L spec] [-r from-address] [:-S var[=value]:] [-u user] "
271 "[:-X cmd:]\n"),
272 n_progname, buf);
273 if(fp != n_stderr)
274 putc('\n', fp);
276 fprintf(fp, _(
277 "\"Receive\" mode, starting on -f (secondary $MBOX or [file]):\n"
278 " %s [-DdEeHiNnRv~#] [-: spec] [-A account] "
279 "[:-C \"custom: header\":] -f\n"
280 " %s [-L spec] [-r from-address] [:-S var[=value]:] [:-X cmd:] "
281 "[file]\n"),
282 n_progname, buf);
283 if(fp != n_stderr)
284 putc('\n', fp);
286 fprintf(fp, _(
287 ". -d sandbox, -:/ no .rc files, -. end options and force send-mode\n"
288 ". -a attachment[=input-charset[#output-charset]]\n"
289 ". -b, -c, to-address, (-r): ex@am.ple or '(Lovely) Ex <am@p.le>'\n"
290 ". -[Mmqt]: special input data (-t: template message on stdin)\n"
291 ". -e only mail check, -H header summary; "
292 "both: message specification via -L\n"
293 ". -S (un)sets variable, -X executes command(s), "
294 "-# enters batch mode\n"
295 ". Features via \"$ %s -Xversion -Xx\"\n"
296 ". Bugs/Contact via "
297 "\"$ %s -Sexpandaddr=shquote '\\$contact-mail'\"\n"),
298 n_progname, n_progname);
299 NYD2_LEAVE;
302 static void
303 a_main_startup(void){
304 char *cp;
305 NYD2_ENTER;
307 n_stdin = stdin;
308 n_stdout = stdout;
309 n_stderr = stderr;
310 dflpipe = SIG_DFL;
312 a_main_oind = /*_oerr =*/ 1;
314 if((cp = strrchr(n_progname, '/')) != NULL)
315 n_progname = ++cp;
317 #ifdef HAVE_NYD
318 safe_signal(SIGABRT, &_nyd_oncrash);
319 # ifdef SIGBUS
320 safe_signal(SIGBUS, &_nyd_oncrash);
321 # endif
322 safe_signal(SIGFPE, &_nyd_oncrash);
323 safe_signal(SIGILL, &_nyd_oncrash);
324 safe_signal(SIGSEGV, &_nyd_oncrash);
325 #endif
327 /* Initialize our input, loop and command machinery */
328 n_go_init();
330 /* Set up a reasonable environment */
332 /* TODO This is wrong: interactive is STDIN/STDERR for a POSIX sh(1).
333 * TODO For now we get this wrong, all over the place, as this software
334 * TODO has always been developed with stdout as an output channel.
335 * TODO Start doing it right for at least explicit terminal-related things,
336 * TODO but v15 should use ONLY this, also for terminal input! */
337 if(isatty(STDIN_FILENO)){
338 n_psonce |= n_PSO_TTYIN;
339 #if defined HAVE_MLE || defined HAVE_TERMCAP
340 if((n_tty_fp = fdopen(fileno(n_stdin), "w")) != NULL)
341 setvbuf(n_tty_fp, NULL, _IOLBF, 0);
342 #endif
344 if(isatty(STDOUT_FILENO))
345 n_psonce |= n_PSO_TTYOUT;
346 if((n_psonce & (n_PSO_TTYIN | n_PSO_TTYOUT)) ==
347 (n_PSO_TTYIN | n_PSO_TTYOUT)){
348 n_psonce |= n_PSO_INTERACTIVE;
349 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
352 /* STDOUT is always line buffered from our point of view */
353 setvbuf(n_stdout, NULL, _IOLBF, 0);
354 if(n_tty_fp == NULL)
355 n_tty_fp = n_stdout;
357 /* -- >8 -- 8< -- */
359 /* We need the endianess, runtime detected due to OPT_CROSS_BUILD */
360 /* C99 */{
361 union {ui16_t bom; ui8_t buf[2];} volatile u;
363 u.bom = 0xFEFFu;
364 if(u.buf[1] != 0xFEu)
365 n_psonce |= n_PSO_BIG_ENDIAN;
368 n_locale_init();
370 #ifdef HAVE_ICONV
371 iconvd = (iconv_t)-1;
372 #endif
374 /* Ensure some variables get loaded and/or verified */
376 (void)ok_blook(POSIXLY_CORRECT);
377 NYD2_LEAVE;
380 static size_t
381 a_main_grow_cpp(char const ***cpp, size_t newsize, size_t oldcnt){
382 /* Just use auto-reclaimed storage, it will be preserved */
383 char const **newcpp;
384 NYD2_ENTER;
386 newcpp = n_autorec_alloc(sizeof(char*) * (newsize + 1));
388 if(oldcnt > 0)
389 memcpy(newcpp, *cpp, oldcnt * sizeof(char*));
390 *cpp = newcpp;
391 NYD2_LEAVE;
392 return newsize;
395 static void
396 a_main_setup_vars(void){
397 struct passwd *pwuid;
398 char const *cp;
399 NYD2_ENTER;
401 /* Detect, verify and fixate our invoking user (environment) */
402 n_group_id = getgid();
403 if((pwuid = getpwuid(n_user_id = getuid())) == NULL)
404 n_panic(_("Cannot associate a name with uid %lu"), (ul_i)n_user_id);
405 else{
406 char const *ep;
407 bool_t doenv;
409 if(!(doenv = (ep = ok_vlook(LOGNAME)) == NULL) &&
410 (doenv = (strcmp(pwuid->pw_name, ep) != 0)))
411 n_err(_("Warning: $LOGNAME (%s) not identical to user (%s)!\n"),
412 ep, pwuid->pw_name);
413 if(doenv){
414 n_pstate |= n_PS_ROOT;
415 ok_vset(LOGNAME, pwuid->pw_name);
416 n_pstate &= ~n_PS_ROOT;
419 /* BSD compat */
420 if((ep = ok_vlook(USER)) != NULL && strcmp(pwuid->pw_name, ep)){
421 n_err(_("Warning: $USER (%s) not identical to user (%s)!\n"),
422 ep, pwuid->pw_name);
423 n_pstate |= n_PS_ROOT;
424 ok_vset(USER, pwuid->pw_name);
425 n_pstate &= ~n_PS_ROOT;
428 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
431 /* Ensure some variables get loaded and/or verified.
432 * While doing so, take special care for invocations as root */
434 /* This is not automatized just as $TMPDIR is for the initial setting, since
435 * we have the pwuid at hand and can simply use it! See accmacvar.c! */
436 if(n_user_id == 0 || (cp = ok_vlook(HOME)) == NULL){
437 cp = pwuid->pw_dir;
438 n_pstate |= n_PS_ROOT;
439 ok_vset(HOME, cp);
440 n_pstate &= ~n_PS_ROOT;
443 /* Do not honour TMPDIR if root */
444 if(n_user_id == 0)
445 ok_vset(TMPDIR, NULL);
446 else
447 (void)ok_vlook(TMPDIR);
449 /* Are we in a reproducible-builds.org environment?
450 * That special mode bends some settings (again) */
451 if(ok_vlook(SOURCE_DATE_EPOCH) != NULL){
452 n_psonce |= n_PSO_REPRODUCIBLE;
453 n_pstate |= n_PS_ROOT;
454 n_progname = n_reproducible_name;
455 ok_vset(LOGNAME, n_reproducible_name);
456 /* Do not care about USER at all in this special mode! */
457 n_pstate &= ~n_PS_ROOT;
458 cp = savecat(n_reproducible_name, ": ");
459 ok_vset(log_prefix, cp);
462 if((n_psonce & n_PSO_INTERACTIVE) ||
463 ((n_psonce & (n_PSO_TTYIN | n_PSO_TTYOUT)) &&
464 (n_poption & n_PO_BATCH_FLAG))){
465 a_main_setscreensize(FAL0);
466 if(n_psonce & n_PSO_INTERACTIVE){
467 /* XXX Yet WINCH after SIGWINCH/SIGCONT, but see POSIX TOSTOP flag */
468 #ifdef SIGWINCH
469 # ifndef TTY_WANTS_SIGWINCH
470 if(safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
471 # endif
472 safe_signal(SIGWINCH, &a_main_setscreensize);
473 #endif
474 #ifdef SIGCONT
475 safe_signal(SIGCONT, &a_main_setscreensize);
476 #endif
478 }else
479 n_scrnheight = n_realscreenheight = 24, n_scrnwidth = 80;
480 NYD2_LEAVE;
483 static void
484 a_main_setscreensize(int is_sighdl){/* TODO globl policy; int wraps; minvals! */
485 struct termios tbuf;
486 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
487 struct winsize ws;
488 #elif defined TIOCGSIZE
489 struct ttysize ts;
490 #endif
491 NYD2_ENTER;
493 n_scrnheight = n_realscreenheight = n_scrnwidth = 0;
495 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
496 * a little bit to be able to honour resizes during our lifetime and
497 * only honour it upon first run; abuse *is_sighdl* as an indicator */
498 if(!is_sighdl){
499 char const *cp;
501 if((cp = ok_vlook(LINES)) != NULL){
502 n_idec_ui32_cp(&n_scrnheight, cp, 0, NULL);
503 n_realscreenheight = n_scrnheight;
505 if((cp = ok_vlook(COLUMNS)) != NULL)
506 n_idec_ui32_cp(&n_scrnwidth, cp, 0, NULL);
508 if(n_scrnwidth != 0 && n_scrnheight != 0)
509 goto jleave;
511 /* For batch mode without explicit request, stop now */
512 if(!(n_psonce & n_PSO_INTERACTIVE)){
513 n_scrnheight = n_realscreenheight = 24;
514 n_scrnwidth = 80;
515 goto jleave;
519 #ifdef HAVE_TCGETWINSIZE
520 if(tcgetwinsize(fileno(n_tty_fp), &ws) == -1)
521 ws.ws_col = ws.ws_row = 0;
522 #elif defined TIOCGWINSZ
523 if(ioctl(fileno(n_tty_fp), TIOCGWINSZ, &ws) == -1)
524 ws.ws_col = ws.ws_row = 0;
525 #elif defined TIOCGSIZE
526 if(ioctl(fileno(n_tty_fp), TIOCGSIZE, &ws) == -1)
527 ts.ts_lines = ts.ts_cols = 0;
528 #endif
530 if(n_scrnheight == 0){
531 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
532 if(ws.ws_row != 0)
533 n_scrnheight = ws.ws_row;
534 #elif defined TIOCGSIZE
535 if(ts.ts_lines != 0)
536 n_scrnheight = ts.ts_lines;
537 #endif
538 else{
539 speed_t ospeed;
541 ospeed = ((tcgetattr(fileno(n_tty_fp), &tbuf) == -1)
542 ? B9600 : cfgetospeed(&tbuf));
544 if(ospeed < B1200)
545 n_scrnheight = 9;
546 else if(ospeed == B1200)
547 n_scrnheight = 14;
548 else
549 n_scrnheight = 24;
552 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ || defined TIOCGSIZE
553 if(0 ==
554 # if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
555 (n_realscreenheight = ws.ws_row)
556 # else
557 (n_realscreenheight = ts.ts_lines)
558 # endif
560 n_realscreenheight = 24;
561 #endif
564 if(n_scrnwidth == 0 && 0 ==
565 #if defined HAVE_TCGETWINSIZE || defined TIOCGWINSZ
566 (n_scrnwidth = ws.ws_col)
567 #elif defined TIOCGSIZE
568 (n_scrnwidth = ts.ts_cols)
569 #endif
571 n_scrnwidth = 80;
573 /**/
574 n_pstate |= n_PS_SIGWINCH_PEND;
575 jleave:
576 NYD2_LEAVE;
579 static sigjmp_buf a_main__hdrjmp; /* XXX */
581 static int
582 a_main_rcv_mode(bool_t had_A_arg, char const *folder, char const *Larg){
583 sighandler_type prevint;
584 int i;
585 NYD_ENTER;
587 i = had_A_arg ? FEDIT_ACCOUNT : FEDIT_NONE;
588 if(n_poption & n_PO_QUICKRUN_MASK)
589 i |= FEDIT_RDONLY;
591 if(folder == NULL){
592 folder = "%";
593 if(had_A_arg)
594 i |= FEDIT_SYSBOX;
596 #ifdef HAVE_IMAP
597 else if(*folder == '@'){
598 /* This must be treated specially to make possible invocation like
599 * -A imap -f @mailbox */
600 char const *cp;
602 cp = n_folder_query();
603 if(which_protocol(cp, FAL0, FAL0, NULL) == PROTO_IMAP)
604 n_strscpy(mailname, cp, sizeof mailname);
606 #endif
608 i = setfile(folder, i);
609 if(i < 0){
610 n_exit_status = n_EXIT_ERR; /* error already reported */
611 goto jquit;
613 temporary_folder_hook_check(FAL0);
614 if(n_poption & n_PO_QUICKRUN_MASK){
615 n_exit_status = i;
616 if(i == n_EXIT_OK && (!(n_poption & n_PO_EXISTONLY) ||
617 (n_poption & n_PO_HEADERLIST)))
618 print_header_summary(Larg);
619 goto jquit;
622 if(i > 0 && !ok_blook(emptystart)){
623 n_exit_status = n_EXIT_ERR;
624 goto jleave;
627 if(sigsetjmp(a_main__hdrjmp, 1) == 0){
628 if((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
629 safe_signal(SIGINT, &a_main_hdrstop);
630 if(!ok_blook(quiet))
631 fprintf(n_stdout, _("%s version %s. Type `?' for help\n"),
632 n_uagent,
633 (n_psonce & n_PSO_REPRODUCIBLE
634 ? n_reproducible_name : ok_vlook(version)));
635 n_folder_announce(n_ANNOUNCE_MAIN_CALL | n_ANNOUNCE_CHANGE);
636 safe_signal(SIGINT, prevint);
639 /* Enter the command loop */
640 if(n_psonce & n_PSO_INTERACTIVE)
641 n_tty_init();
642 n_go_main_loop();
643 if(n_psonce & n_PSO_INTERACTIVE)
644 n_tty_destroy((n_psonce & n_PSO_XIT) != 0);
646 if(!(n_psonce & n_PSO_XIT)){
647 if(mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR){
648 safe_signal(SIGHUP, SIG_IGN);
649 safe_signal(SIGINT, SIG_IGN);
650 safe_signal(SIGQUIT, SIG_IGN);
652 jquit:
653 save_mbox_for_possible_quitstuff();
654 quit(FAL0);
656 jleave:
657 NYD_LEAVE;
658 return n_exit_status;
661 static void
662 a_main_hdrstop(int signo){
663 NYD_X; /* Signal handler */
664 n_UNUSED(signo);
666 fflush(n_stdout);
667 n_err_sighdl(_("\nInterrupt\n"));
668 siglongjmp(a_main__hdrjmp, 1);
672 main(int argc, char *argv[]){
673 /* TODO Once v15 control flow/carrier rewrite took place main() should
674 * TODO be rewritten and option parsing++ should be outsourced.
675 * TODO Like so we can get rid of some stack locals etc. */
676 /* Keep in SYNC: ./nail.1:"SYNOPSIS, main() */
677 static char const optstr[] =
678 "A:a:Bb:C:c:DdEeFfHhiL:M:m:NnO:q:Rr:S:s:tu:VvX:::~#.";
679 int i;
680 char *cp;
681 enum{
682 a_RF_NONE = 0,
683 a_RF_SET = 1<<0,
684 a_RF_SYSTEM = 1<<1,
685 a_RF_USER = 1<<2,
686 a_RF_ALL = a_RF_SYSTEM | a_RF_USER
687 } resfiles;
688 size_t Xargs_size, Xargs_cnt, smopts_size;
689 char const *Aarg, *emsg, *folder, *Larg, *okey, *qf,
690 *subject, *uarg, **Xargs;
691 struct attachment *attach;
692 struct name *to, *cc, *bcc;
693 struct a_arg *a_head, *a_curr;
694 NYD_ENTER;
696 a_head = NULL;
697 n_UNINIT(a_curr, NULL);
698 to = cc = bcc = NULL;
699 attach = NULL;
700 Aarg = emsg = folder = Larg = okey = qf = subject = uarg = NULL;
701 Xargs = NULL;
702 Xargs_size = Xargs_cnt = smopts_size = 0;
703 resfiles = a_RF_ALL;
706 * Start our lengthy setup, finalize by setting n_PSO_STARTED
709 n_progname = argv[0];
710 a_main_startup();
712 /* Command line parsing
713 * -S variable settings need to be done twice, since the user surely wants
714 * the setting to take effect immediately, but also doesn't want it to be
715 * overwritten from within resource files */
716 while((i = a_main_getopt(argc, argv, optstr)) >= 0){
717 switch(i){
718 case 'A':
719 /* Execute an account command later on */
720 Aarg = a_main_oarg;
721 break;
722 case 'a':{
723 /* Add an attachment */
724 struct a_arg *nap;
726 n_psonce |= n_PSO_SENDMODE;
727 nap = n_autorec_alloc(sizeof(struct a_arg));
728 if(a_head == NULL)
729 a_head = nap;
730 else
731 a_curr->aa_next = nap;
732 nap->aa_next = NULL;
733 nap->aa_file = a_main_oarg;
734 a_curr = nap;
735 } break;
736 case 'B':
737 n_OBSOLETE(_("-B is obsolete, please use -# as necessary"));
738 break;
739 case 'b':
740 /* Add (a) blind carbon copy recipient (list) */
741 n_psonce |= n_PSO_SENDMODE;
742 bcc = cat(bcc, lextract(a_main_oarg,
743 GBCC | GFULL | GSHEXP_PARSE_HACK));
744 break;
745 case 'C':{
746 /* Create custom header (at list tail) */
747 struct n_header_field **hflpp;
749 if(*(hflpp = &n_poption_arg_C) != NULL){
750 while((*hflpp)->hf_next != NULL)
751 *hflpp = (*hflpp)->hf_next;
752 hflpp = &(*hflpp)->hf_next;
754 if(!n_header_add_custom(hflpp, a_main_oarg, FAL0)){
755 emsg = N_("Invalid custom header data with -C");
756 goto jusage;
758 } break;
759 case 'c':
760 /* Add (a) carbon copy recipient (list) */
761 n_psonce |= n_PSO_SENDMODE;
762 cc = cat(cc, lextract(a_main_oarg, GCC | GFULL | GSHEXP_PARSE_HACK));
763 break;
764 case 'D':
765 #ifdef HAVE_IMAP
766 ok_bset(disconnected);
767 #endif
768 break;
769 case 'd':
770 ok_bset(debug);
771 break;
772 case 'E':
773 ok_bset(skipemptybody);
774 break;
775 case 'e':
776 /* Check if mail (matching -L) exists in given box, exit status */
777 n_poption |= n_PO_EXISTONLY;
778 break;
779 case 'F':
780 /* Save msg in file named after local part of first recipient */
781 n_poption |= n_PO_F_FLAG;
782 n_psonce |= n_PSO_SENDMODE;
783 break;
784 case 'f':
785 /* User is specifying file to "edit" with Mail, as opposed to reading
786 * system mailbox. If no argument is given, we read his mbox file.
787 * Check for remaining arguments later */
788 folder = "&";
789 break;
790 case 'H':
791 /* Display summary of headers, exit */
792 n_poption |= n_PO_HEADERSONLY;
793 break;
794 case 'h':
795 a_main_usage(n_stdout);
796 goto j_leave;
797 case 'i':
798 /* Ignore interrupts */
799 ok_bset(ignore);
800 break;
801 case 'L':
802 /* Display summary of headers which match given spec, exit.
803 * In conjunction with -e, only test the given spec for existence */
804 Larg = a_main_oarg;
805 n_poption |= n_PO_HEADERLIST;
806 if(*Larg == '"' || *Larg == '\''){ /* TODO list.c:listspec_check() */
807 size_t j;
809 j = strlen(++Larg);
810 if(j > 0){
811 cp = savestrbuf(Larg, --j);
812 Larg = cp;
815 break;
816 case 'M':
817 /* Flag message body (standard input) with given MIME type */
818 if(qf != NULL && (!(n_poption & n_PO_Mm_FLAG) || qf != (char*)-1))
819 goto jeMmq;
820 n_poption_arg_Mm = a_main_oarg;
821 qf = (char*)-1;
822 if(0){
823 /* FALLTHRU*/
824 case 'm':
825 /* Flag the given file with MIME type and use as message body */
826 if(qf != NULL && (!(n_poption & n_PO_Mm_FLAG) || qf == (char*)-1))
827 goto jeMmq;
828 qf = a_main_oarg;
830 n_poption |= n_PO_Mm_FLAG;
831 n_psonce |= n_PSO_SENDMODE;
832 break;
833 case 'N':
834 /* Avoid initial header printing */
835 ok_bclear(header);
836 break;
837 case 'n':
838 /* Don't source "unspecified system start-up file" */
839 if(resfiles & a_RF_SET){
840 emsg = N_("-n cannot be used in conjunction with -:");
841 goto jusage;
843 resfiles = a_RF_USER;
844 break;
845 case 'O':
846 /* Additional options to pass-through to MTA TODO v15-compat legacy */
847 if(n_smopts_cnt == smopts_size)
848 smopts_size = a_main_grow_cpp(&n_smopts, smopts_size + 8,
849 n_smopts_cnt);
850 n_smopts[n_smopts_cnt++] = a_main_oarg;
851 break;
852 case 'q':
853 /* "Quote" file: use as message body (-t without headers etc.) */
854 /* XXX Traditional. Add -Q to initialize as *quote*d content? */
855 if(qf != NULL && (n_poption & n_PO_Mm_FLAG)){
856 jeMmq:
857 emsg = N_("Only one of -M, -m or -q may be given");
858 goto jusage;
860 n_psonce |= n_PSO_SENDMODE;
861 /* Allow for now, we have to special check validity of -q- later on! */
862 qf = (a_main_oarg[0] == '-' && a_main_oarg[1] == '\0')
863 ? (char*)-1 : a_main_oarg;
864 break;
865 case 'R':
866 /* Open folders read-only */
867 n_poption |= n_PO_R_FLAG;
868 break;
869 case 'r':
870 /* Set From address. */
871 n_poption |= n_PO_r_FLAG;
872 if(a_main_oarg[0] == '\0')
873 break;
874 else{
875 struct name *fa;
877 fa = nalloc(a_main_oarg, GSKIN | GFULL | GFULLEXTRA);
878 if(is_addr_invalid(fa, EACM_STRICT | EACM_NOLOG)){
879 emsg = N_("Invalid address argument with -r");
880 goto jusage;
882 n_poption_arg_r = fa;
883 /* TODO -r options is set in n_smopts, but may
884 * TODO be overwritten by setting from= in
885 * TODO an interactive session!
886 * TODO Maybe disable setting of from?
887 * TODO Warn user? Update manual!! */
888 a_main_oarg = savecat("from=", fa->n_fullname);
890 /* FALLTHRU */
891 case 'S':
892 { struct str sin;
893 struct n_string s, *sp;
894 char const *a[2];
895 bool_t b;
897 if(!ok_blook(v15_compat)){
898 okey = a[0] = a_main_oarg;
899 sp = NULL;
900 }else{
901 enum n_shexp_state shs;
903 n_autorec_relax_create();
904 sp = n_string_creat_auto(&s);
905 sin.s = n_UNCONST(a_main_oarg);
906 sin.l = UIZ_MAX;
907 shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
908 n_SHEXP_PARSE_IGNORE_EMPTY |
909 n_SHEXP_PARSE_QUOTE_AUTO_FIXED |
910 n_SHEXP_PARSE_QUOTE_AUTO_DSQ), sp, &sin, NULL);
911 if((shs & n_SHEXP_STATE_ERR_MASK) ||
912 !(shs & n_SHEXP_STATE_STOP)){
913 n_autorec_relax_gut();
914 goto je_S;
916 okey = a[0] = n_string_cp_const(sp);
919 a[1] = NULL;
920 n_poption |= n_PO_S_FLAG_TEMPORARY;
921 n_pstate |= n_PS_ROBOT;
922 b = (c_set(a) == 0);
923 n_pstate &= ~n_PS_ROBOT;
924 n_poption &= ~n_PO_S_FLAG_TEMPORARY;
926 if(sp != NULL)
927 n_autorec_relax_gut();
928 if(!b && (ok_blook(errexit) || ok_blook(posix))){
929 je_S:
930 emsg = N_("-S failed to set variable");
931 goto jusage;
934 break;
935 case 's':
936 /* Subject:; take care for Debian #419840 and strip any \r and \n */
937 if(n_anyof_cp("\n\r", subject = a_main_oarg)){
938 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
939 for(subject = cp = savestr(a_main_oarg); *cp != '\0'; ++cp)
940 if(*cp == '\n' || *cp == '\r')
941 *cp = ' ';
943 n_psonce |= n_PSO_SENDMODE;
944 break;
945 case 't':
946 /* Use the given message as send template */
947 n_poption |= n_PO_t_FLAG;
948 n_psonce |= n_PSO_SENDMODE;
949 break;
950 case 'u':
951 /* Open primary mailbox of the given user */
952 uarg = savecat("%", a_main_oarg);
953 break;
954 case 'V':
955 fprintf(n_stdout, _("%s version %s\n"), n_uagent, ok_vlook(version));
956 n_exit_status = n_EXIT_OK;
957 goto j_leave;
958 case 'v':
959 /* Be verbose */
960 ok_bset(verbose);
961 break;
962 case 'X':
963 /* Add to list of commands to exec before entering normal operation */
964 if(Xargs_cnt == Xargs_size)
965 Xargs_size = a_main_grow_cpp(&Xargs, Xargs_size + 8, Xargs_cnt);
966 Xargs[Xargs_cnt++] = a_main_oarg;
967 break;
968 case ':':
969 /* Control which resource files shall be loaded */
970 if(!(resfiles & (a_RF_SET | a_RF_SYSTEM))){
971 emsg = N_("-n cannot be used in conjunction with -:");
972 goto jusage;
974 resfiles = a_RF_SET;
975 while((i = *a_main_oarg++) != '\0')
976 switch(i){
977 case 'S': case 's': resfiles |= a_RF_SYSTEM; break;
978 case 'U': case 'u': resfiles |= a_RF_USER; break;
979 case ':': case '/': resfiles &= ~a_RF_ALL; break;
980 default:
981 emsg = N_("Invalid argument of -:");
982 goto jusage;
984 break;
985 case '~':
986 /* Enable command escapes even in non-interactive mode */
987 n_poption |= n_PO_TILDE_FLAG;
988 break;
989 case '#':
990 /* Work in batch mode, even if non-interactive */
991 if(!(n_psonce & n_PSO_INTERACTIVE))
992 setvbuf(n_stdin, NULL, _IOLBF, 0);
993 n_poption |= n_PO_TILDE_FLAG | n_PO_BATCH_FLAG;
994 folder = n_path_devnull;
995 n_pstate |= n_PS_ROBOT; /* (be silent unsetting undefined variables) */
996 ok_vset(MAIL, folder);
997 ok_vset(MBOX, folder);
998 ok_bset(emptystart);
999 ok_bclear(errexit);
1000 ok_bclear(header);
1001 ok_vset(inbox, folder);
1002 ok_bclear(posix);
1003 ok_bset(quiet);
1004 ok_bset(sendwait);
1005 ok_bset(typescript_mode);
1006 n_pstate &= ~n_PS_ROBOT;
1007 break;
1008 case '.':
1009 /* Enforce send mode */
1010 n_psonce |= n_PSO_SENDMODE;
1011 goto jgetopt_done;
1012 case '?':
1013 jusage:
1014 if(emsg != NULL)
1015 n_err("%s\n", V_(emsg));
1016 a_main_usage(n_stderr);
1017 n_exit_status = n_EXIT_USE;
1018 goto j_leave;
1021 jgetopt_done:
1024 /* The normal arguments may be followed by MTA arguments after a "--";
1025 * however, -f may take off an argument, too, and before that.
1026 * Since MTA arguments after "--" require *expandargv*, delay parsing off
1027 * those options until after the resource files are loaded... */
1028 if((cp = argv[i = a_main_oind]) == NULL)
1030 else if(cp[0] == '-' && cp[1] == '-' && cp[2] == '\0')
1031 ++i;
1032 /* n_PO_BATCH_FLAG sets to /dev/null, but -f can still be used and sets & */
1033 else if(folder != NULL && /*folder[0] == '&' &&*/ folder[1] == '\0'){
1034 folder = cp;
1035 if((cp = argv[++i]) != NULL){
1036 if(cp[0] != '-' || cp[1] != '-' || cp[2] != '\0'){
1037 emsg = N_("More than one file given with -f");
1038 goto jusage;
1040 ++i;
1042 }else{
1043 n_psonce |= n_PSO_SENDMODE;
1044 for(;;){
1045 to = cat(to, lextract(cp, GTO | GFULL | GSHEXP_PARSE_HACK));
1046 if((cp = argv[++i]) == NULL)
1047 break;
1048 if(cp[0] == '-' && cp[1] == '-' && cp[2] == '\0'){
1049 ++i;
1050 break;
1054 a_main_oind = i;
1056 /* ...BUT, since we use n_autorec_alloc() for the MTA n_smopts storage we
1057 * need to allocate the space for them before we fixate that storage! */
1058 while(argv[i] != NULL)
1059 ++i;
1060 if(n_smopts_cnt + i > smopts_size)
1061 DBG(smopts_size =)
1062 a_main_grow_cpp(&n_smopts, n_smopts_cnt + i + 1, n_smopts_cnt);
1064 /* Check for inconsistent arguments, fix some temporaries */
1065 if(n_psonce & n_PSO_SENDMODE){
1066 /* XXX This is only because BATCH_FLAG sets *folder*=/dev/null
1067 * XXX in order to function. Ideally that would not be needed */
1068 if(folder != NULL && !(n_poption & n_PO_BATCH_FLAG)){
1069 emsg = N_("Cannot give -f and people to send to.");
1070 goto jusage;
1072 if(uarg != NULL){
1073 emsg = N_("The -u option cannot be used in send mode");
1074 goto jusage;
1076 if(!(n_poption & n_PO_t_FLAG) && to == NULL){
1077 emsg = N_("Send options without primary recipient specified.");
1078 goto jusage;
1080 if((n_poption & n_PO_t_FLAG) && qf != NULL){
1081 emsg = N_("The -M, -m, -q and -t options are mutual exclusive.");
1082 goto jusage;
1084 if(n_poption & (n_PO_EXISTONLY | n_PO_HEADERSONLY | n_PO_HEADERLIST)){
1085 emsg = N_("The -e, -H and -L options cannot be used in send mode.");
1086 goto jusage;
1088 if(n_poption & n_PO_R_FLAG){
1089 emsg = N_("The -R option is meaningless in send mode.");
1090 goto jusage;
1093 if(n_psonce & n_PSO_INTERACTIVE){
1094 if(qf == (char*)-1){
1095 if(!(n_poption & n_PO_Mm_FLAG))
1096 emsg = N_("-q can't use standard input when interactive.\n");
1097 goto jusage;
1100 }else{
1101 if(uarg != NULL && folder != NULL){
1102 emsg = N_("The options -u and -f (and -#) are mutually exclusive");
1103 goto jusage;
1105 if((n_poption & (n_PO_EXISTONLY | n_PO_HEADERSONLY)) ==
1106 (n_PO_EXISTONLY | n_PO_HEADERSONLY)){
1107 emsg = N_("The options -e and -H are mutual exclusive");
1108 goto jusage;
1110 if((n_poption & (n_PO_HEADERSONLY | n_PO_HEADERLIST) /* TODO OBSOLETE */
1111 ) == (n_PO_HEADERSONLY | n_PO_HEADERLIST))
1112 n_OBSOLETE(_("please use \"-e -L xy\" instead of \"-H -L xy\""));
1114 if(uarg != NULL)
1115 folder = uarg;
1119 * We have reached our second program state, the command line options have
1120 * been worked and verified a bit, we are likely to go, perform more setup
1122 n_psonce |= n_PSO_STARTED_GETOPT;
1124 a_main_setup_vars();
1126 /* Create memory pool snapshot; Memory is auto-reclaimed from now on */
1127 n_memory_pool_fixate();
1129 /* load() any resource files */
1130 if(resfiles & a_RF_ALL){
1131 /* *expand() returns a savestr(), but load() only uses the file name
1132 * for fopen(), so it is safe to do this */
1133 if(resfiles & a_RF_SYSTEM){
1134 bool_t nload;
1136 if((nload = ok_blook(NAIL_NO_SYSTEM_RC)))
1137 n_OBSOLETE(_("Please use $MAILX_NO_SYSTEM_RC instead of "
1138 "$NAIL_NO_SYSTEM_RC"));
1139 if(!nload && !ok_blook(MAILX_NO_SYSTEM_RC) &&
1140 !n_go_load(ok_vlook(system_mailrc)))
1141 goto j_leave;
1144 if((resfiles & a_RF_USER) &&
1145 !n_go_load(fexpand(ok_vlook(MAILRC), FEXP_LOCAL | FEXP_NOPROTO)))
1146 goto j_leave;
1148 if((cp = ok_vlook(NAIL_EXTRA_RC)) != NULL)
1149 n_OBSOLETE(_("Please use *mailx-extra-rc*, not *NAIL_EXTRA_RC*"));
1150 if((cp != NULL || (cp = ok_vlook(mailx_extra_rc)) != NULL) &&
1151 !n_go_load(fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)))
1152 goto j_leave;
1155 /* Cause possible umask(2) to be applied, now that any setting is
1156 * established, and before we change accounts, evaluate commands etc. */
1157 (void)ok_vlook(umask);
1159 /* Additional options to pass-through to MTA, and allowed to do so? */
1160 i = a_main_oind;
1161 if((cp = ok_vlook(expandargv)) != NULL){
1162 bool_t isfail, isrestrict;
1164 isfail = !asccasecmp(cp, "fail");
1165 isrestrict = (!isfail && !asccasecmp(cp, "restrict"));
1167 if((n_poption & n_PO_D_V) && !isfail && !isrestrict && *cp != '\0')
1168 n_err(_("Unknown *expandargv* value: %s\n"), cp);
1170 if((cp = argv[i]) != NULL){
1171 if(isfail || (isrestrict && (!(n_poption & n_PO_TILDE_FLAG) ||
1172 !(n_psonce & n_PSO_INTERACTIVE)))){
1173 je_expandargv:
1174 n_err(_("*expandargv* doesn't allow MTA arguments; consider "
1175 "using *mta-arguments*\n"));
1176 n_exit_status = n_EXIT_USE | n_EXIT_SEND_ERROR;
1177 goto jleave;
1180 assert(n_smopts_cnt + 1 <= smopts_size);
1181 n_smopts[n_smopts_cnt++] = cp;
1182 }while((cp = argv[++i]) != NULL);
1184 }else if(argv[i] != NULL)
1185 goto je_expandargv;
1187 /* We had to wait until the resource files are loaded and any command line
1188 * setting has been restored, but get the termcap up and going before we
1189 * switch account or running commands */
1190 #ifdef n_HAVE_TCAP
1191 if((n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_QUICKRUN_MASK))
1192 n_termcap_init();
1193 #endif
1195 /* Now we can set the account */
1196 if(Aarg != NULL){
1197 char const *a[2];
1199 a[0] = Aarg;
1200 a[1] = NULL;
1201 if(c_account(a) && (!(n_psonce & n_PSO_INTERACTIVE) ||
1202 ok_blook(errexit) || ok_blook(posix))){
1203 n_exit_status = n_EXIT_USE | n_EXIT_SEND_ERROR;
1204 goto jleave;
1209 * Almost setup, only -X commands are missing!
1211 n_psonce |= n_PSO_STARTED_CONFIG;
1213 /* "load()" commands given on command line */
1214 if(Xargs_cnt > 0){
1215 if(!n_go_Xargs(Xargs, Xargs_cnt))
1216 goto jleave;
1219 /* Final tests */
1220 if(n_poption & n_PO_Mm_FLAG){
1221 if(qf == (char*)-1){
1222 if(!n_mimetype_check_mtname(n_poption_arg_Mm)){
1223 n_err(_("Could not find `mimetype' for -M argument: %s\n"),
1224 n_poption_arg_Mm);
1225 n_exit_status = n_EXIT_ERR;
1226 goto jleave;
1228 }else if((n_poption_arg_Mm = n_mimetype_classify_filename(qf)) == NULL){
1229 n_err(_("Could not `mimetype'-classify -m argument: %s\n"),
1230 n_shexp_quote_cp(qf, FAL0));
1231 n_exit_status = n_EXIT_ERR;
1232 goto jleave;
1233 }else if(!asccasecmp(n_poption_arg_Mm, "text/plain")) /* TODO no: magic */
1234 n_poption_arg_Mm = NULL;
1238 * We're finally completely setup and ready to go!
1240 n_psonce |= n_PSO_STARTED;
1242 if(!(n_psonce & n_PSO_SENDMODE))
1243 n_exit_status = a_main_rcv_mode((Aarg != NULL), folder, Larg);
1244 else{
1245 /* Now that full mailx(1)-style file expansion is possible handle the
1246 * attachments which we had delayed due to this.
1247 * This may use savestr(), but since we won't enter the command loop we
1248 * don't need to care about that */
1249 for(; a_head != NULL; a_head = a_head->aa_next){
1250 enum n_attach_error aerr;
1252 attach = n_attachment_append(attach, a_head->aa_file, &aerr, NULL);
1253 if(aerr != n_ATTACH_ERR_NONE){
1254 n_exit_status = n_EXIT_ERR;
1255 goto jleave;
1259 if(n_psonce & n_PSO_INTERACTIVE)
1260 n_tty_init();
1261 n_mail((n_poption & n_PO_F_FLAG ? n_MAILSEND_RECORD_RECIPIENT : 0),
1262 to, cc, bcc, subject, attach, qf);
1263 if(n_psonce & n_PSO_INTERACTIVE)
1264 n_tty_destroy((n_psonce & n_PSO_XIT) != 0);
1267 jleave:
1268 /* Be aware of identical code for `exit' command! */
1269 #ifdef n_HAVE_TCAP
1270 if((n_psonce & n_PSO_INTERACTIVE) && !(n_poption & n_PO_QUICKRUN_MASK))
1271 n_termcap_destroy();
1272 #endif
1274 j_leave:
1275 #if defined HAVE_MEMORY_DEBUG || defined HAVE_NOMEMDBG
1276 n_memory_pool_pop(NULL);
1277 #endif
1278 #if defined HAVE_DEBUG || defined HAVE_DEVEL || defined HAVE_NOMEMDBG
1279 n_memory_reset();
1280 #endif
1281 NYD_LEAVE;
1282 return n_exit_status;
1285 /* Source the others in that case! */
1286 #ifdef HAVE_AMALGAMATION
1287 # include <mk-config.h>
1288 #endif
1290 /* s-it-mode */