Many: unite many HAVE_ to HAVE_C90AMEND1, plus
[s-mailx.git] / main.c
blob4428d9ffde2631819c2780316b8bd179d4a81c7f
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Startup -- interface with user.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
41 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
42 * correct several buffer overruns (at least one ot them was exploitable).
43 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
44 * ---
45 * Note: We set egid to realgid ... and only if we need the egid we will
46 * switch back temporary. Nevertheless, I do not like seg faults.
47 * Werner Fink, <werner@suse.de>
50 #ifndef HAVE_AMALGAMATION
51 # define _MAIN_SOURCE
52 # include "nail.h"
53 #endif
55 #include <sys/ioctl.h>
57 #include <fcntl.h>
58 #include <pwd.h>
60 #ifdef HAVE_NL_LANGINFO
61 # include <langinfo.h>
62 #endif
63 #ifdef HAVE_SETLOCALE
64 # include <locale.h>
65 #endif
67 #include "version.h"
69 struct a_arg {
70 struct a_arg *aa_next;
71 char *aa_file;
74 /* (extern, but not with amalgamation, so define here) */
75 VL char const weekday_names[7 + 1][4] = {
76 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", ""
78 VL char const month_names[12 + 1][4] = {
79 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
80 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""
82 VL char const uagent[] = UAGENT;
83 VL char const version[] = VERSION;
84 /*VL char const features[]; The "feature string" comes from config.h */
86 VL sighandler_type dflpipe = SIG_DFL;
88 /* getopt(3) fallback implementation */
89 #ifdef HAVE_GETOPT
90 # define _oarg optarg
91 # define _oind optind
92 /*# define _oerr opterr*/
93 # define _oopt optopt
94 #else
95 static char *_oarg;
96 static int _oind, /*_oerr,*/ _oopt;
97 #endif
99 /* getopt(3) fallback implementation */
100 #ifdef HAVE_GETOPT
101 # define _getopt getopt
102 #else
103 static int _getopt(int argc, char *const argv[], const char *optstring);
104 #endif
106 /* Perform basic startup initialization */
107 static void _startup(void);
109 /* Grow a char** */
110 static int _grow_cpp(char const ***cpp, int size, int cnt);
112 /* Initialize *tempdir*, *myname*, *homedir* */
113 static void _setup_vars(void);
115 /* We're in an interactive session - compute what the screen size for printing
116 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
117 * We use the following algorithm for the height:
118 * If baud rate < 1200, use 9
119 * If baud rate = 1200, use 14
120 * If baud rate > 1200, use 24 or ws_row
121 * Width is either 80 or ws_col */
122 static void _setscreensize(int is_sighdl);
124 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
125 * the system mailbox, and open up the right stuff */
126 static int _rcv_mode(char const *folder);
128 /* Interrupt printing of the headers */
129 static void _hdrstop(int signo);
131 #ifndef HAVE_GETOPT
132 static int
133 _getopt(int argc, char * const argv[], char const *optstring)
135 static char const *lastp;
136 int rv = -1, colon;
137 char const *curp;
139 if ((colon = (optstring[0] == ':')))
140 ++optstring;
141 if (lastp != NULL) {
142 curp = lastp;
143 lastp = 0;
144 } else {
145 if (_oind >= argc || argv[_oind] == 0 || argv[_oind][0] != '-' ||
146 argv[_oind][1] == '\0')
147 goto jleave;
148 if (argv[_oind][1] == '-' && argv[_oind][2] == '\0') {
149 ++_oind;
150 goto jleave;
152 curp = &argv[_oind][1];
155 _oopt = curp[0];
156 while (optstring[0] != '\0') {
157 if (optstring[0] == ':' || optstring[0] != _oopt) {
158 ++optstring;
159 continue;
161 if (optstring[1] == ':') {
162 if (curp[1] != '\0') {
163 _oarg = UNCONST(&curp[1]);
164 ++_oind;
165 } else {
166 if ((_oind += 2) > argc) {
167 if (!colon /*&& _oerr*/) {
168 fprintf(stderr,
169 tr(79, "%s: option requires an argument -- %c\n"),
170 argv[0], (char)_oopt);
172 rv = (colon ? ':' : '?');
173 goto jleave;
175 _oarg = argv[_oind - 1];
177 } else {
178 if (curp[1] != '\0')
179 lastp = &curp[1];
180 else
181 ++_oind;
182 _oarg = NULL;
184 rv = _oopt;
185 goto jleave;
188 if (!colon /*&& opterr*/)
189 fprintf(stderr, tr(78, "%s: illegal option -- %c\n"), argv[0], _oopt);
190 if (curp[1] != '\0')
191 lastp = &curp[1];
192 else
193 ++_oind;
194 _oarg = 0;
195 rv = '?';
196 jleave:
197 return rv;
199 #endif /* !HAVE_GETOPT */
201 static void
202 _startup(void)
204 char *cp;
206 /* Absolutely the first thing we do is save our egid
207 * and set it to the rgid, so that we can safely run
208 * setgid. We use the sgid (saved set-gid) to allow ourselves
209 * to revert to the egid if we want (temporarily) to become
210 * privileged XXX (s-nail-)*dotlock(-program)* */
211 effectivegid = getegid();
212 realgid = getgid();
213 if (setgid(realgid) < 0) {
214 perror("setgid");
215 exit(1);
218 image = -1;
219 dflpipe = SIG_DFL;
220 #ifndef HAVE_GETOPT
221 _oind = /*_oerr =*/ 1;
222 #endif
224 if ((cp = strrchr(progname, '/')) != NULL)
225 progname = ++cp;
227 /* Set up a reasonable environment.
228 * Figure out whether we are being run interactively,
229 * start the SIGCHLD catcher, and so forth */
230 safe_signal(SIGCHLD, sigchild);
232 if (isatty(STDIN_FILENO)) /* TODO should be isatty(0) && isatty(2)?? */
233 options |= OPT_TTYIN | OPT_INTERACTIVE;
234 if (isatty(STDOUT_FILENO))
235 options |= OPT_TTYOUT;
236 if (IS_TTY_SESSION())
237 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
239 /* Define defaults for internal variables, based on POSIX 2008/Cor 1-2013 */
240 /* noallnet */
241 /* noappend */
242 assign("asksub", "");
243 /* noaskbcc */
244 /* noaskcc */
245 /* noautoprint */
246 /* nobang */
247 /* nocmd */
248 /* nocrt */
249 /* nodebug */
250 /* nodot */
251 /* assign("escape", "~"); TODO non-compliant */
252 /* noflipr */
253 /* nofolder */
254 assign("header", "");
255 /* nohold */
256 /* noignore */
257 /* noignoreeof */
258 /* nokeep */
259 /* nokeepsave */
260 /* nometoo */
261 /* noonehop -- Note: we ignore this one */
262 /* nooutfolder */
263 /* nopage */
264 assign("prompt", "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
265 /* noquiet */
266 /* norecord */
267 assign("save", "");
268 /* nosendwait */
269 /* noshowto */
270 /* nosign */
271 /* noSign */
272 /* assign("toplines", "5"); XXX somewhat hmm */
274 #ifdef HAVE_SETLOCALE
275 setlocale(LC_ALL, "");
276 mb_cur_max = MB_CUR_MAX;
277 # ifdef HAVE_NL_LANGINFO
278 if (voption("ttycharset") == NULL && (cp = nl_langinfo(CODESET)) != NULL)
279 assign("ttycharset", cp);
280 # endif
282 # ifdef HAVE_C90AMEND1
283 if (mb_cur_max > 1) {
284 wchar_t wc;
285 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
286 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
287 utf8 = 1;
288 /* Reset state - it may have been messed up; luckily this also
289 * gives us an indication wether the encoding has locking shift
290 * state sequences */
291 /* TODO temporary - use option bits! */
292 enc_has_state = mbtowc(&wc, NULL, mb_cur_max);
294 # endif
295 #else
296 mb_cur_max = 1;
297 #endif
299 #ifdef HAVE_CATGETS
300 # ifdef NL_CAT_LOCALE
301 catd = catopen(CATNAME, NL_CAT_LOCALE);
302 # else
303 catd = catopen(CATNAME, 0);
304 # endif
305 #endif
307 #ifdef HAVE_ICONV
308 iconvd = (iconv_t)-1;
309 #endif
312 static int
313 _grow_cpp(char const ***cpp, int size, int cnt)
315 /* Before spreserve(): use our string pool instead of LibC heap;
316 * Increment *size* by at least 5! */
317 char const **newcpp = salloc(sizeof(char*) * (size += 8));
319 if (cnt > 0)
320 memcpy(newcpp, *cpp, (size_t)cnt * sizeof(char*));
321 *cpp = newcpp;
322 return size;
325 static void
326 _setup_vars(void)
328 /* Before spreserve(): use our string pool instead of LibC heap */
329 /* XXX further check paths? */
330 char const *cp;
331 uid_t uid;
332 struct passwd *pwuid, *pw;
334 tempdir = ((cp = getenv("TMPDIR")) != NULL) ? savestr(cp) : TMPDIR_FALLBACK;
336 cp = (myname == NULL) ? getenv("USER") : myname;
337 uid = getuid();
338 if ((pwuid = getpwuid(uid)) == NULL)
339 panic(tr(201, "Cannot associate a name with uid %lu"), (ul_it)uid);
340 if (cp == NULL)
341 myname = pwuid->pw_name;
342 else if ((pw = getpwnam(cp)) == NULL)
343 panic(tr(236, "`%s' is not a user of this system"), cp);
344 else {
345 myname = pw->pw_name;
346 if (pw->pw_uid != uid)
347 options |= OPT_u_FLAG;
349 myname = savestr(myname);
350 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
352 if ((cp = getenv("HOME")) == NULL)
353 cp = "."; /* XXX User and Login objects; Login: pw->pw_dir */
354 homedir = savestr(cp);
357 static void
358 _setscreensize(int is_sighdl)
360 struct termios tbuf;
361 #ifdef TIOCGWINSZ
362 struct winsize ws;
363 #elif defined TIOCGSIZE
364 struct ttysize ts;
365 #endif
367 scrnheight = realscreenheight = scrnwidth = 0;
369 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
370 * a little bit to be able to honour resizes during our lifetime and
371 * only honour it upon first run; abuse *is_sighdl* as an indicator */
372 if (!is_sighdl) {
373 char *cp;
374 long i;
376 if ((cp = getenv("LINES")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
377 scrnheight = realscreenheight = (int)i;
378 if ((cp = getenv("COLUMNS")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
379 scrnwidth = (int)i;
381 if (scrnwidth != 0 && scrnheight != 0)
382 goto jleave;
385 #ifdef TIOCGWINSZ
386 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
387 ws.ws_col = ws.ws_row = 0;
388 #elif defined TIOCGSIZE
389 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) < 0)
390 ts.ts_lines = ts.ts_cols = 0;
391 #endif
393 if (scrnheight == 0) {
394 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) < 0)
395 ? B9600 : cfgetospeed(&tbuf));
397 if (ospeed < B1200)
398 scrnheight = 9;
399 else if (ospeed == B1200)
400 scrnheight = 14;
401 #ifdef TIOCGWINSZ
402 else if (ws.ws_row != 0)
403 scrnheight = ws.ws_row;
404 #elif defined TIOCGSIZE
405 else if (ts.ts_lines != 0)
406 scrnheight = ts.ts_lines;
407 #endif
408 else
409 scrnheight = 24;
411 #if defined TIOCGWINSZ || defined TIOCGSIZE
412 if (0 ==
413 # ifdef TIOCGWINSZ
414 (realscreenheight = ws.ws_row)
415 # else
416 (realscreenheight = ts.ts_lines)
417 # endif
419 realscreenheight = 24;
420 #endif
423 if (scrnwidth == 0 && 0 ==
424 #ifdef TIOCGWINSZ
425 (scrnwidth = ws.ws_col)
426 #elif defined TIOCGSIZE
427 (scrnwidth = ts.ts_cols)
428 #endif
430 scrnwidth = 80;
432 jleave:
433 #ifdef SIGWINCH
434 if (is_sighdl && IS_TTY_SESSION())
435 tty_signal(SIGWINCH);
436 #endif
439 static sigjmp_buf __hdrjmp; /* XXX */
441 static int
442 _rcv_mode(char const *folder)
444 char *cp;
445 int i;
446 sighandler_type prevint;
448 if (folder == NULL)
449 folder = "%";
450 else if (*folder == '@') {
451 /* This must be treated specially to make invocation like
452 * -A imap -f @mailbox work */
453 if ((cp = value("folder")) != NULL && which_protocol(cp) == PROTO_IMAP)
454 (void)n_strlcpy(mailname, cp, MAXPATHLEN);
457 i = setfile(folder, 0);
458 if (i < 0)
459 exit(1); /* error already reported */
460 if (options & OPT_EXISTONLY)
461 exit(i);
463 if (options & OPT_HEADERSONLY) {
464 #ifdef HAVE_IMAP
465 if (mb.mb_type == MB_IMAP)
466 imap_getheaders(1, msgCount);
467 #endif
468 time_current_update(&time_current, FAL0);
469 for (i = 1; i <= msgCount; ++i)
470 printhead(i, stdout, 0);
471 exit(exit_status);
474 callhook(mailname, 0);
475 if (i > 0 && !boption("emptystart"))
476 exit(1);
478 if (sigsetjmp(__hdrjmp, 1) == 0) {
479 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
480 safe_signal(SIGINT, _hdrstop);
481 if (!(options & OPT_N_FLAG)) {
482 if (!value("quiet"))
483 printf(tr(140, "%s version %s. Type ? for help.\n"),
484 (boption("bsdcompat") ? "Mail" : uagent), version);
485 announce(1);
486 fflush(stdout);
488 safe_signal(SIGINT, prevint);
491 /* Enter the command loop */
492 if (options & OPT_INTERACTIVE)
493 tty_init();
494 commands();
495 if (options & OPT_INTERACTIVE)
496 tty_destroy();
498 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
499 safe_signal(SIGHUP, SIG_IGN);
500 safe_signal(SIGINT, SIG_IGN);
501 safe_signal(SIGQUIT, SIG_IGN);
503 save_mbox_for_possible_quitstuff();
504 quit();
505 return exit_status;
508 static void
509 _hdrstop(int signo)
511 UNUSED(signo);
513 fflush(stdout);
514 fprintf(stderr, tr(141, "\nInterrupt\n"));
515 siglongjmp(__hdrjmp, 1);
519 main(int argc, char *argv[])
521 static char const optstr[] = "A:a:Bb:c:DdEeFfHiNnO:q:Rr:S:s:tu:Vv~#",
522 usagestr[] =
523 "Synopsis:\n"
524 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
525 "[-b bcc-addr] [-c cc-addr]\n"
526 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
527 "[-S var[=value]]\n"
528 "\t [-s subject] to-addr...\n"
529 " %s [-BDdEeHiNnRv~#] [-A acct] "
530 "[-S var[=value]] -f [file]\n"
531 " %s [-BDdEeiNnRv~#] [-A acc] [-S var[=value]] [-u user]\n";
533 struct a_arg *a_head = NULL, *a_curr = /* silence CC */ NULL;
534 struct name *to = NULL, *cc = NULL, *bcc = NULL;
535 struct attachment *attach = NULL;
536 char *cp = NULL, *subject = NULL, *qf = NULL, *Aflag = NULL;
537 char const *okey, **oargs = NULL, *folder = NULL;
538 int oargs_size = 0, oargs_count = 0, smopts_size = 0, i;
541 * Start our lengthy setup
544 starting =
545 unset_allow_undefined = TRU1;
547 progname = argv[0];
548 _startup();
550 /* Command line parsing */
551 while ((i = _getopt(argc, argv, optstr)) >= 0) {
552 switch (i) {
553 case 'A':
554 /* Execute an account command later on */
555 Aflag = _oarg;
556 break;
557 case 'a':
558 { struct a_arg *nap = ac_alloc(sizeof(struct a_arg));
559 if (a_head == NULL)
560 a_head = nap;
561 else
562 a_curr->aa_next = nap;
563 nap->aa_next = NULL;
564 nap->aa_file = _oarg;
565 a_curr = nap;
567 options |= OPT_SENDMODE;
568 break;
569 case 'B':
570 /* Make 0/1 line buffered */
571 setvbuf(stdin, NULL, _IOLBF, 0);
572 setvbuf(stdout, NULL, _IOLBF, 0);
573 break;
574 case 'b':
575 /* Get Blind Carbon Copy Recipient list */
576 bcc = cat(bcc, checkaddrs(lextract(_oarg, GBCC | GFULL)));
577 options |= OPT_SENDMODE;
578 break;
579 case 'c':
580 /* Get Carbon Copy Recipient list */
581 cc = cat(cc, checkaddrs(lextract(_oarg, GCC | GFULL)));
582 options |= OPT_SENDMODE;
583 break;
584 case 'D':
585 #ifdef HAVE_IMAP
586 okey = "disconnected";
587 goto joarg;
588 #else
589 break;
590 #endif
591 case 'd':
592 okey = "debug";
593 #ifdef HAVE_DEBUG
594 assign(okey, "");
595 #endif
596 goto joarg;
597 case 'E':
598 okey = "skipemptybody";
599 goto joarg;
600 case 'e':
601 options |= OPT_EXISTONLY;
602 break;
603 case 'F':
604 options |= OPT_F_FLAG | OPT_SENDMODE;
605 break;
606 case 'f':
607 /* User is specifying file to "edit" with Mail, as opposed to reading
608 * system mailbox. If no argument is given, we read his mbox file.
609 * Check for remaining arguments later */
610 folder = "&";
611 break;
612 case 'H':
613 options |= OPT_HEADERSONLY;
614 break;
615 case 'i':
616 /* Ignore interrupts */
617 okey = "ignore";
618 goto joarg;
619 case 'N':
620 /* Avoid initial header printing */
621 okey = "noheader";
622 goto joarg;
623 case 'n':
624 /* Don't source "unspecified system start-up file" */
625 options |= OPT_NOSRC;
626 break;
627 case 'O':
628 /* Additional options to pass-through to MTA */
629 if (smopts_count == (size_t)smopts_size)
630 smopts_size = _grow_cpp(&smopts, smopts_size, (int)smopts_count);
631 smopts[smopts_count++] = skin(_oarg);
632 break;
633 case 'q':
634 /* Quote file TODO drop? -Q with real quote?? what ? */
635 if (*_oarg != '-')
636 qf = _oarg;
637 options |= OPT_SENDMODE;
638 break;
639 case 'R':
640 /* Open folders read-only */
641 options |= OPT_R_FLAG;
642 break;
643 case 'r':
644 /* Set From address. */
645 options |= OPT_r_FLAG;
646 if ((option_r_arg = _oarg)[0] != '\0') {
647 struct name *fa = nalloc(_oarg, GFULL);
649 if (is_addr_invalid(fa, 1) || is_fileorpipe_addr(fa)) {
650 fprintf(stderr, tr(271, "Invalid address argument with -r\n"));
651 goto jusage;
653 option_r_arg = fa->n_name;
654 /* TODO -r options is set in smopts, but may
655 * TODO be overwritten by setting from= in
656 * TODO an interactive session!
657 * TODO Maybe disable setting of from?
658 * TODO Warn user? Update manual!! */
659 okey = savecat("from=", fa->n_fullname);
660 goto joarg;
661 /* ..and fa goes even though it is ready :/ */
663 break;
664 case 'S':
665 /* Set variable. We need to do this twice, since the
666 * user surely wants the setting to take effect
667 * immediately, but also doesn't want it to be
668 * overwritten from within resource files */
669 { char *a[2];
670 okey = a[0] = _oarg;
671 a[1] = NULL;
672 set(a);
674 joarg:
675 if (oargs_count == oargs_size)
676 oargs_size = _grow_cpp(&oargs, oargs_size, oargs_count);
677 oargs[oargs_count++] = okey;
678 break;
679 case 's':
680 /* Subject: */
681 subject = _oarg;
682 options |= OPT_SENDMODE;
683 break;
684 case 't':
685 /* Read defined set of headers from mail to be send */
686 options |= OPT_SENDMODE | OPT_t_FLAG;
687 break;
688 case 'u':
689 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
690 * done as necessary in _setup_vars() above */
691 myname = _oarg;
692 break;
693 case 'V':
694 puts(version);
695 exit(0);
696 /* NOTREACHED */
697 case 'v':
698 /* Be verbose */
699 okey = "verbose";
700 #ifdef HAVE_DEBUG
701 assign(okey, "");
702 #endif
703 goto joarg;
704 case '~':
705 /* Enable tilde escapes even in non-interactive mode */
706 options |= OPT_TILDE_FLAG;
707 break;
708 case '#':
709 /* Work in batch mode, even if non-interactive */
710 if (oargs_count + 5 >= oargs_size)
711 oargs_size = _grow_cpp(&oargs, oargs_size, oargs_count);
712 oargs[oargs_count++] = "dot";
713 oargs[oargs_count++] = "emptystart";
714 oargs[oargs_count++] = "noheader";
715 oargs[oargs_count++] = "quiet";
716 oargs[oargs_count++] = "sendwait";
717 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
718 break;
719 case '?':
720 jusage:
721 fprintf(stderr, tr(135, usagestr), progname, progname, progname);
722 exit(2);
726 if (folder != NULL) {
727 if (_oind < argc) {
728 if (_oind + 1 < argc) {
729 fprintf(stderr, tr(205, "More than one file given with -f\n"));
730 goto jusage;
732 folder = argv[_oind];
734 } else {
735 for (i = _oind; argv[i]; ++i)
736 to = cat(to, checkaddrs(lextract(argv[i], GTO | GFULL)));
737 if (to != NULL)
738 options |= OPT_SENDMODE;
741 /* Check for inconsistent arguments */
742 if (folder != NULL && to != NULL) {
743 fprintf(stderr, tr(137, "Cannot give -f and people to send to.\n"));
744 goto jusage;
746 if ((options & (OPT_SENDMODE | OPT_t_FLAG)) == OPT_SENDMODE && to == NULL) {
747 fprintf(stderr, tr(138,
748 "Send options without primary recipient specified.\n"));
749 goto jusage;
751 if ((options & OPT_R_FLAG) && to != NULL) {
752 fprintf(stderr, tr(235, "The -R option is meaningless in send mode.\n"));
753 goto jusage;
757 * Likely to go, perform more setup
760 _setup_vars();
762 if (options & OPT_INTERACTIVE) {
763 _setscreensize(0);
764 #ifdef SIGWINCH
765 # ifndef TTY_WANTS_SIGWINCH
766 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
767 # endif
768 safe_signal(SIGWINCH, _setscreensize);
769 #endif
770 } else
771 scrnheight = realscreenheight = 24, scrnwidth = 80;
773 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
774 spreserve();
776 if (!(options & OPT_NOSRC) && getenv("NAIL_NO_SYSTEM_RC") == NULL)
777 load(SYSCONFRC);
778 /* *expand() returns a savestr(), but load only uses the file name for
779 * fopen(), so it's safe to do this */
780 if ((cp = getenv("MAILRC")) != NULL)
781 load(file_expand(cp));
782 else if ((cp = getenv("NAILRC")) != NULL)
783 load(file_expand(cp));
784 else
785 load(file_expand(MAILRC));
786 if (getenv("NAIL_EXTRA_RC") == NULL && (cp = value("NAIL_EXTRA_RC")) != NULL)
787 load(file_expand(cp));
789 /* Now we can set the account */
790 if (Aflag != NULL) {
791 char *a[2];
792 a[0] = Aflag;
793 a[1] = NULL;
794 c_account(a);
797 /* Ensure the -S and other command line options take precedence over
798 * anything that may have been placed in resource files */
799 for (i = 0; i < oargs_count; ++i) {
800 char const *a[2];
801 a[0] = oargs[i];
802 a[1] = NULL;
803 set(a);
807 * We're finally completely setup and ready to go
810 starting =
811 unset_allow_undefined = FAL0;
813 if (options & OPT_DEBUG)
814 fprintf(stderr, tr(199, "user = %s, homedir = %s\n"), myname, homedir);
816 if (!(options & OPT_SENDMODE)) {
817 exit_status = _rcv_mode(folder);
818 goto jleave;
821 /* Now that full mailx(1)-style file expansion is possible handle the
822 * attachments which we had delayed due to this.
823 * This may use savestr(), but since we won't enter the command loop we
824 * don't need to care about that */
825 while (a_head != NULL) {
826 attach = add_attachment(attach, a_head->aa_file, NULL);
827 if (attach != NULL) {
828 a_curr = a_head;
829 a_head = a_head->aa_next;
830 ac_free(a_curr);
831 } else {
832 perror(a_head->aa_file);
833 exit(1);
837 /* xxx exit_status = EXIT_OK; */
838 if (options & OPT_INTERACTIVE)
839 tty_init();
840 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
841 if (options & OPT_INTERACTIVE)
842 tty_destroy();
843 jleave:
844 return exit_status;
847 /* vim:set fenc=utf-8:s-it-mode */