Thread subjects: improve visibility situation..
[s-mailx.git] / main.c
blobb5baa32eaf0381f9d0f2a18afbaad1fef8726594
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 print_headers(1, msgCount);
465 exit(exit_status);
468 callhook(mailname, 0);
469 if (i > 0 && !boption("emptystart"))
470 exit(1);
472 if (sigsetjmp(__hdrjmp, 1) == 0) {
473 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
474 safe_signal(SIGINT, _hdrstop);
475 if (!(options & OPT_N_FLAG)) {
476 if (!value("quiet"))
477 printf(tr(140, "%s version %s. Type ? for help.\n"),
478 (boption("bsdcompat") ? "Mail" : uagent), version);
479 announce(1);
480 fflush(stdout);
482 safe_signal(SIGINT, prevint);
485 /* Enter the command loop */
486 if (options & OPT_INTERACTIVE)
487 tty_init();
488 commands();
489 if (options & OPT_INTERACTIVE)
490 tty_destroy();
492 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
493 safe_signal(SIGHUP, SIG_IGN);
494 safe_signal(SIGINT, SIG_IGN);
495 safe_signal(SIGQUIT, SIG_IGN);
497 save_mbox_for_possible_quitstuff();
498 quit();
499 return exit_status;
502 static void
503 _hdrstop(int signo)
505 UNUSED(signo);
507 fflush(stdout);
508 fprintf(stderr, tr(141, "\nInterrupt\n"));
509 siglongjmp(__hdrjmp, 1);
513 main(int argc, char *argv[])
515 static char const optstr[] = "A:a:Bb:c:DdEeFfHiNnO:q:Rr:S:s:tu:Vv~#",
516 usagestr[] =
517 "Synopsis:\n"
518 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
519 "[-b bcc-addr] [-c cc-addr]\n"
520 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
521 "[-S var[=value]]\n"
522 "\t [-s subject] to-addr...\n"
523 " %s [-BDdEeHiNnRv~#] [-A acct] "
524 "[-S var[=value]] -f [file]\n"
525 " %s [-BDdEeiNnRv~#] [-A acc] [-S var[=value]] [-u user]\n";
527 struct a_arg *a_head = NULL, *a_curr = /* silence CC */ NULL;
528 struct name *to = NULL, *cc = NULL, *bcc = NULL;
529 struct attachment *attach = NULL;
530 char *cp = NULL, *subject = NULL, *qf = NULL, *Aflag = NULL;
531 char const *okey, **oargs = NULL, *folder = NULL;
532 int oargs_size = 0, oargs_count = 0, smopts_size = 0, i;
535 * Start our lengthy setup
538 starting =
539 unset_allow_undefined = TRU1;
541 progname = argv[0];
542 _startup();
544 /* Command line parsing */
545 while ((i = _getopt(argc, argv, optstr)) >= 0) {
546 switch (i) {
547 case 'A':
548 /* Execute an account command later on */
549 Aflag = _oarg;
550 break;
551 case 'a':
552 { struct a_arg *nap = ac_alloc(sizeof(struct a_arg));
553 if (a_head == NULL)
554 a_head = nap;
555 else
556 a_curr->aa_next = nap;
557 nap->aa_next = NULL;
558 nap->aa_file = _oarg;
559 a_curr = nap;
561 options |= OPT_SENDMODE;
562 break;
563 case 'B':
564 /* Make 0/1 line buffered */
565 setvbuf(stdin, NULL, _IOLBF, 0);
566 setvbuf(stdout, NULL, _IOLBF, 0);
567 break;
568 case 'b':
569 /* Get Blind Carbon Copy Recipient list */
570 bcc = cat(bcc, checkaddrs(lextract(_oarg, GBCC | GFULL)));
571 options |= OPT_SENDMODE;
572 break;
573 case 'c':
574 /* Get Carbon Copy Recipient list */
575 cc = cat(cc, checkaddrs(lextract(_oarg, GCC | GFULL)));
576 options |= OPT_SENDMODE;
577 break;
578 case 'D':
579 #ifdef HAVE_IMAP
580 okey = "disconnected";
581 goto joarg;
582 #else
583 break;
584 #endif
585 case 'd':
586 okey = "debug";
587 #ifdef HAVE_DEBUG
588 assign(okey, "");
589 #endif
590 goto joarg;
591 case 'E':
592 okey = "skipemptybody";
593 goto joarg;
594 case 'e':
595 options |= OPT_EXISTONLY;
596 break;
597 case 'F':
598 options |= OPT_F_FLAG | OPT_SENDMODE;
599 break;
600 case 'f':
601 /* User is specifying file to "edit" with Mail, as opposed to reading
602 * system mailbox. If no argument is given, we read his mbox file.
603 * Check for remaining arguments later */
604 folder = "&";
605 break;
606 case 'H':
607 options |= OPT_HEADERSONLY;
608 break;
609 case 'i':
610 /* Ignore interrupts */
611 okey = "ignore";
612 goto joarg;
613 case 'N':
614 /* Avoid initial header printing */
615 okey = "noheader";
616 goto joarg;
617 case 'n':
618 /* Don't source "unspecified system start-up file" */
619 options |= OPT_NOSRC;
620 break;
621 case 'O':
622 /* Additional options to pass-through to MTA */
623 if (smopts_count == (size_t)smopts_size)
624 smopts_size = _grow_cpp(&smopts, smopts_size, (int)smopts_count);
625 smopts[smopts_count++] = skin(_oarg);
626 break;
627 case 'q':
628 /* Quote file TODO drop? -Q with real quote?? what ? */
629 if (*_oarg != '-')
630 qf = _oarg;
631 options |= OPT_SENDMODE;
632 break;
633 case 'R':
634 /* Open folders read-only */
635 options |= OPT_R_FLAG;
636 break;
637 case 'r':
638 /* Set From address. */
639 options |= OPT_r_FLAG;
640 if ((option_r_arg = _oarg)[0] != '\0') {
641 struct name *fa = nalloc(_oarg, GFULL);
643 if (is_addr_invalid(fa, 1) || is_fileorpipe_addr(fa)) {
644 fprintf(stderr, tr(271, "Invalid address argument with -r\n"));
645 goto jusage;
647 option_r_arg = fa->n_name;
648 /* TODO -r options is set in smopts, but may
649 * TODO be overwritten by setting from= in
650 * TODO an interactive session!
651 * TODO Maybe disable setting of from?
652 * TODO Warn user? Update manual!! */
653 okey = savecat("from=", fa->n_fullname);
654 goto joarg;
655 /* ..and fa goes even though it is ready :/ */
657 break;
658 case 'S':
659 /* Set variable. We need to do this twice, since the
660 * user surely wants the setting to take effect
661 * immediately, but also doesn't want it to be
662 * overwritten from within resource files */
663 { char *a[2];
664 okey = a[0] = _oarg;
665 a[1] = NULL;
666 set(a);
668 joarg:
669 if (oargs_count == oargs_size)
670 oargs_size = _grow_cpp(&oargs, oargs_size, oargs_count);
671 oargs[oargs_count++] = okey;
672 break;
673 case 's':
674 /* Subject: */
675 subject = _oarg;
676 options |= OPT_SENDMODE;
677 break;
678 case 't':
679 /* Read defined set of headers from mail to be send */
680 options |= OPT_SENDMODE | OPT_t_FLAG;
681 break;
682 case 'u':
683 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
684 * done as necessary in _setup_vars() above */
685 myname = _oarg;
686 break;
687 case 'V':
688 puts(version);
689 exit(0);
690 /* NOTREACHED */
691 case 'v':
692 /* Be verbose */
693 okey = "verbose";
694 #ifdef HAVE_DEBUG
695 assign(okey, "");
696 #endif
697 goto joarg;
698 case '~':
699 /* Enable tilde escapes even in non-interactive mode */
700 options |= OPT_TILDE_FLAG;
701 break;
702 case '#':
703 /* Work in batch mode, even if non-interactive */
704 if (oargs_count + 5 >= oargs_size)
705 oargs_size = _grow_cpp(&oargs, oargs_size, oargs_count);
706 oargs[oargs_count++] = "dot";
707 oargs[oargs_count++] = "emptystart";
708 oargs[oargs_count++] = "noheader";
709 oargs[oargs_count++] = "quiet";
710 oargs[oargs_count++] = "sendwait";
711 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
712 break;
713 case '?':
714 jusage:
715 fprintf(stderr, tr(135, usagestr), progname, progname, progname);
716 exit(2);
720 if (folder != NULL) {
721 if (_oind < argc) {
722 if (_oind + 1 < argc) {
723 fprintf(stderr, tr(205, "More than one file given with -f\n"));
724 goto jusage;
726 folder = argv[_oind];
728 } else {
729 for (i = _oind; argv[i]; ++i)
730 to = cat(to, checkaddrs(lextract(argv[i], GTO | GFULL)));
731 if (to != NULL)
732 options |= OPT_SENDMODE;
735 /* Check for inconsistent arguments */
736 if (folder != NULL && to != NULL) {
737 fprintf(stderr, tr(137, "Cannot give -f and people to send to.\n"));
738 goto jusage;
740 if ((options & (OPT_SENDMODE | OPT_t_FLAG)) == OPT_SENDMODE && to == NULL) {
741 fprintf(stderr, tr(138,
742 "Send options without primary recipient specified.\n"));
743 goto jusage;
745 if ((options & OPT_R_FLAG) && to != NULL) {
746 fprintf(stderr, tr(235, "The -R option is meaningless in send mode.\n"));
747 goto jusage;
751 * Likely to go, perform more setup
754 _setup_vars();
756 if (options & OPT_INTERACTIVE) {
757 _setscreensize(0);
758 #ifdef SIGWINCH
759 # ifndef TTY_WANTS_SIGWINCH
760 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
761 # endif
762 safe_signal(SIGWINCH, _setscreensize);
763 #endif
764 } else
765 scrnheight = realscreenheight = 24, scrnwidth = 80;
767 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
768 spreserve();
770 if (!(options & OPT_NOSRC) && getenv("NAIL_NO_SYSTEM_RC") == NULL)
771 load(SYSCONFRC);
772 /* *expand() returns a savestr(), but load only uses the file name for
773 * fopen(), so it's safe to do this */
774 if ((cp = getenv("MAILRC")) != NULL)
775 load(file_expand(cp));
776 else if ((cp = getenv("NAILRC")) != NULL)
777 load(file_expand(cp));
778 else
779 load(file_expand(MAILRC));
780 if (getenv("NAIL_EXTRA_RC") == NULL && (cp = value("NAIL_EXTRA_RC")) != NULL)
781 load(file_expand(cp));
783 /* Now we can set the account */
784 if (Aflag != NULL) {
785 char *a[2];
786 a[0] = Aflag;
787 a[1] = NULL;
788 c_account(a);
791 /* Ensure the -S and other command line options take precedence over
792 * anything that may have been placed in resource files */
793 for (i = 0; i < oargs_count; ++i) {
794 char const *a[2];
795 a[0] = oargs[i];
796 a[1] = NULL;
797 set(a);
801 * We're finally completely setup and ready to go
804 starting =
805 unset_allow_undefined = FAL0;
807 if (options & OPT_DEBUG)
808 fprintf(stderr, tr(199, "user = %s, homedir = %s\n"), myname, homedir);
810 if (!(options & OPT_SENDMODE)) {
811 exit_status = _rcv_mode(folder);
812 goto jleave;
815 /* Now that full mailx(1)-style file expansion is possible handle the
816 * attachments which we had delayed due to this.
817 * This may use savestr(), but since we won't enter the command loop we
818 * don't need to care about that */
819 while (a_head != NULL) {
820 attach = add_attachment(attach, a_head->aa_file, NULL);
821 if (attach != NULL) {
822 a_curr = a_head;
823 a_head = a_head->aa_next;
824 ac_free(a_curr);
825 } else {
826 perror(a_head->aa_file);
827 exit(1);
831 /* xxx exit_status = EXIT_OK; */
832 if (options & OPT_INTERACTIVE)
833 tty_init();
834 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
835 if (options & OPT_INTERACTIVE)
836 tty_destroy();
837 jleave:
838 return exit_status;
841 /* vim:set fenc=utf-8:s-it-mode */