mk-conf.sh, mk-mk.in: use variables for *all* utilities
[s-mailx.git] / main.c
bloba0f5f06fe8168a5719b4200dd342a28c68b99aec
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 */
85 VL uc_it const class_char[] = {
86 /* 000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel */
87 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
88 /* 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si */
89 C_CNTRL, C_BLANK, C_WHITE, C_SPACE, C_SPACE, C_SPACE, C_CNTRL, C_CNTRL,
90 /* 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb */
91 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
92 /* 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us */
93 C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL, C_CNTRL,
94 /* 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' */
95 C_BLANK, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
96 /* 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / */
97 C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
98 /* 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 */
99 C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL, C_OCTAL,
100 /* 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? */
101 C_DIGIT, C_DIGIT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
102 /* 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G */
103 C_PUNCT, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
104 /* 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O */
105 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
106 /* 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W */
107 C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER, C_UPPER,
108 /* 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ */
109 C_UPPER, C_UPPER, C_UPPER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT,
110 /* 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g */
111 C_PUNCT, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
112 /* 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o */
113 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
114 /* 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w */
115 C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER, C_LOWER,
116 /* 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del */
117 C_LOWER, C_LOWER, C_LOWER, C_PUNCT, C_PUNCT, C_PUNCT, C_PUNCT, C_CNTRL
120 /* getopt(3) fallback implementation */
121 #ifdef HAVE_GETOPT
122 # define _oarg optarg
123 # define _oind optind
124 /*# define _oerr opterr*/
125 # define _oopt optopt
126 #else
127 static char *_oarg;
128 static int _oind, /*_oerr,*/ _oopt;
129 #endif
131 /* getopt(3) fallback implementation */
132 #ifdef HAVE_GETOPT
133 # define _getopt getopt
134 #else
135 static int _getopt(int argc, char *const argv[], const char *optstring);
136 #endif
138 /* Perform basic startup initialization */
139 static void _startup(void);
141 /* Grow a char** */
142 static int _grow_cpp(char const ***cpp, int size, int cnt);
144 /* Initialize *tempdir*, *myname*, *homedir* */
145 static void _setup_vars(void);
147 /* We're in an interactive session - compute what the screen size for printing
148 * headers etc. should be; notify tty upon resize if *is_sighdl* is not 0.
149 * We use the following algorithm for the height:
150 * If baud rate < 1200, use 9
151 * If baud rate = 1200, use 14
152 * If baud rate > 1200, use 24 or ws_row
153 * Width is either 80 or ws_col */
154 static void _setscreensize(int is_sighdl);
156 /* Ok, we are reading mail. Decide whether we are editing a mailbox or reading
157 * the system mailbox, and open up the right stuff */
158 static int _rcv_mode(char const *folder);
160 /* Interrupt printing of the headers */
161 static void _hdrstop(int signo);
163 #ifndef HAVE_GETOPT
164 static int
165 _getopt(int argc, char * const argv[], char const *optstring)
167 static char const *lastp;
168 int rv = -1, colon;
169 char const *curp;
171 if ((colon = (optstring[0] == ':')))
172 ++optstring;
173 if (lastp != NULL) {
174 curp = lastp;
175 lastp = 0;
176 } else {
177 if (_oind >= argc || argv[_oind] == 0 || argv[_oind][0] != '-' ||
178 argv[_oind][1] == '\0')
179 goto jleave;
180 if (argv[_oind][1] == '-' && argv[_oind][2] == '\0') {
181 ++_oind;
182 goto jleave;
184 curp = &argv[_oind][1];
187 _oopt = curp[0];
188 while (optstring[0] != '\0') {
189 if (optstring[0] == ':' || optstring[0] != _oopt) {
190 ++optstring;
191 continue;
193 if (optstring[1] == ':') {
194 if (curp[1] != '\0') {
195 _oarg = UNCONST(&curp[1]);
196 ++_oind;
197 } else {
198 if ((_oind += 2) > argc) {
199 if (!colon /*&& _oerr*/) {
200 fprintf(stderr,
201 tr(79, "%s: option requires an argument -- %c\n"),
202 argv[0], (char)_oopt);
204 rv = (colon ? ':' : '?');
205 goto jleave;
207 _oarg = argv[_oind - 1];
209 } else {
210 if (curp[1] != '\0')
211 lastp = &curp[1];
212 else
213 ++_oind;
214 _oarg = NULL;
216 rv = _oopt;
217 goto jleave;
220 if (!colon /*&& opterr*/)
221 fprintf(stderr, tr(78, "%s: illegal option -- %c\n"), argv[0], _oopt);
222 if (curp[1] != '\0')
223 lastp = &curp[1];
224 else
225 ++_oind;
226 _oarg = 0;
227 rv = '?';
228 jleave:
229 return rv;
231 #endif /* !HAVE_GETOPT */
233 static void
234 _startup(void)
236 char *cp;
238 /* Absolutely the first thing we do is save our egid
239 * and set it to the rgid, so that we can safely run
240 * setgid. We use the sgid (saved set-gid) to allow ourselves
241 * to revert to the egid if we want (temporarily) to become
242 * privileged XXX (s-nail-)*dotlock(-program)* */
243 effectivegid = getegid();
244 realgid = getgid();
245 if (setgid(realgid) < 0) {
246 perror("setgid");
247 exit(1);
250 image = -1;
251 dflpipe = SIG_DFL;
252 #ifndef HAVE_GETOPT
253 _oind = /*_oerr =*/ 1;
254 #endif
256 if ((cp = strrchr(progname, '/')) != NULL)
257 progname = ++cp;
259 /* Set up a reasonable environment.
260 * Figure out whether we are being run interactively,
261 * start the SIGCHLD catcher, and so forth */
262 safe_signal(SIGCHLD, sigchild);
264 if (isatty(STDIN_FILENO)) /* TODO should be isatty(0) && isatty(2)?? */
265 options |= OPT_TTYIN | OPT_INTERACTIVE;
266 if (isatty(STDOUT_FILENO))
267 options |= OPT_TTYOUT;
268 if (IS_TTY_SESSION())
269 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
271 /* Define defaults for internal variables, based on POSIX 2008/Cor 1-2013 */
272 /* noallnet */
273 /* noappend */
274 assign("asksub", "");
275 /* noaskbcc */
276 /* noaskcc */
277 /* noautoprint */
278 /* nobang */
279 /* nocmd */
280 /* nocrt */
281 /* nodebug */
282 /* nodot */
283 /* assign("escape", "~"); TODO non-compliant */
284 /* noflipr */
285 /* nofolder */
286 assign("header", "");
287 /* nohold */
288 /* noignore */
289 /* noignoreeof */
290 /* nokeep */
291 /* nokeepsave */
292 /* nometoo */
293 /* noonehop -- Note: we ignore this one */
294 /* nooutfolder */
295 /* nopage */
296 assign("prompt", "\\& "); /* POSIX "? " unless *bsdcompat*, then "& " */
297 /* noquiet */
298 /* norecord */
299 assign("save", "");
300 /* nosendwait */
301 /* noshowto */
302 /* nosign */
303 /* noSign */
304 /* assign("toplines", "5"); XXX somewhat hmm */
306 #ifdef HAVE_SETLOCALE
307 setlocale(LC_ALL, "");
308 mb_cur_max = MB_CUR_MAX;
309 # ifdef HAVE_NL_LANGINFO
310 if (voption("ttycharset") == NULL && (cp = nl_langinfo(CODESET)) != NULL)
311 assign("ttycharset", cp);
312 # endif
314 # ifdef HAVE_C90AMEND1
315 if (mb_cur_max > 1) {
316 wchar_t wc;
317 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
318 mbtowc(&wc, "\342\202\254", 3) == 3 && wc == 0x20AC)
319 utf8 = 1;
320 /* Reset state - it may have been messed up; luckily this also
321 * gives us an indication wether the encoding has locking shift
322 * state sequences */
323 /* TODO temporary - use option bits! */
324 enc_has_state = mbtowc(&wc, NULL, mb_cur_max);
326 # endif
327 #else
328 mb_cur_max = 1;
329 #endif
331 #ifdef HAVE_CATGETS
332 # ifdef NL_CAT_LOCALE
333 catd = catopen(CATNAME, NL_CAT_LOCALE);
334 # else
335 catd = catopen(CATNAME, 0);
336 # endif
337 #endif
339 #ifdef HAVE_ICONV
340 iconvd = (iconv_t)-1;
341 #endif
344 static int
345 _grow_cpp(char const ***cpp, int size, int cnt)
347 /* Before spreserve(): use our string pool instead of LibC heap;
348 * Increment *size* by at least 5! */
349 char const **newcpp = salloc(sizeof(char*) * (size += 8));
351 if (cnt > 0)
352 memcpy(newcpp, *cpp, (size_t)cnt * sizeof(char*));
353 *cpp = newcpp;
354 return size;
357 static void
358 _setup_vars(void)
360 /* Before spreserve(): use our string pool instead of LibC heap */
361 /* XXX further check paths? */
362 char const *cp;
363 uid_t uid;
364 struct passwd *pwuid, *pw;
366 tempdir = ((cp = getenv("TMPDIR")) != NULL) ? savestr(cp) : TMPDIR_FALLBACK;
368 cp = (myname == NULL) ? getenv("USER") : myname;
369 uid = getuid();
370 if ((pwuid = getpwuid(uid)) == NULL)
371 panic(tr(201, "Cannot associate a name with uid %lu"), (ul_it)uid);
372 if (cp == NULL)
373 myname = pwuid->pw_name;
374 else if ((pw = getpwnam(cp)) == NULL)
375 panic(tr(236, "`%s' is not a user of this system"), cp);
376 else {
377 myname = pw->pw_name;
378 if (pw->pw_uid != uid)
379 options |= OPT_u_FLAG;
381 myname = savestr(myname);
382 /* XXX myfullname = pw->pw_gecos[OPTIONAL!] -> GUT THAT; TODO pw_shell */
384 if ((cp = getenv("HOME")) == NULL)
385 cp = "."; /* XXX User and Login objects; Login: pw->pw_dir */
386 homedir = savestr(cp);
389 static void
390 _setscreensize(int is_sighdl)
392 struct termios tbuf;
393 #ifdef TIOCGWINSZ
394 struct winsize ws;
395 #elif defined TIOCGSIZE
396 struct ttysize ts;
397 #endif
399 scrnheight = realscreenheight = scrnwidth = 0;
401 /* (Also) POSIX: LINES and COLUMNS always override. Adjust this
402 * a little bit to be able to honour resizes during our lifetime and
403 * only honour it upon first run; abuse *is_sighdl* as an indicator */
404 if (!is_sighdl) {
405 char *cp;
406 long i;
408 if ((cp = getenv("LINES")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
409 scrnheight = realscreenheight = (int)i;
410 if ((cp = getenv("COLUMNS")) != NULL && (i = strtol(cp, NULL, 10)) > 0)
411 scrnwidth = (int)i;
413 if (scrnwidth != 0 && scrnheight != 0)
414 goto jleave;
417 #ifdef TIOCGWINSZ
418 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
419 ws.ws_col = ws.ws_row = 0;
420 #elif defined TIOCGSIZE
421 if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ws) < 0)
422 ts.ts_lines = ts.ts_cols = 0;
423 #endif
425 if (scrnheight == 0) {
426 speed_t ospeed = ((tcgetattr(STDOUT_FILENO, &tbuf) < 0)
427 ? B9600 : cfgetospeed(&tbuf));
429 if (ospeed < B1200)
430 scrnheight = 9;
431 else if (ospeed == B1200)
432 scrnheight = 14;
433 #ifdef TIOCGWINSZ
434 else if (ws.ws_row != 0)
435 scrnheight = ws.ws_row;
436 #elif defined TIOCGSIZE
437 else if (ts.ts_lines != 0)
438 scrnheight = ts.ts_lines;
439 #endif
440 else
441 scrnheight = 24;
443 #if defined TIOCGWINSZ || defined TIOCGSIZE
444 if (0 ==
445 # ifdef TIOCGWINSZ
446 (realscreenheight = ws.ws_row)
447 # else
448 (realscreenheight = ts.ts_lines)
449 # endif
451 realscreenheight = 24;
452 #endif
455 if (scrnwidth == 0 && 0 ==
456 #ifdef TIOCGWINSZ
457 (scrnwidth = ws.ws_col)
458 #elif defined TIOCGSIZE
459 (scrnwidth = ts.ts_cols)
460 #endif
462 scrnwidth = 80;
464 jleave:
465 #ifdef SIGWINCH
466 if (is_sighdl && IS_TTY_SESSION())
467 tty_signal(SIGWINCH);
468 #endif
471 static sigjmp_buf __hdrjmp; /* XXX */
473 static int
474 _rcv_mode(char const *folder)
476 char *cp;
477 int i;
478 sighandler_type prevint;
480 if (folder == NULL)
481 folder = "%";
482 else if (*folder == '@') {
483 /* This must be treated specially to make invocation like
484 * -A imap -f @mailbox work */
485 if ((cp = value("folder")) != NULL && which_protocol(cp) == PROTO_IMAP)
486 (void)n_strlcpy(mailname, cp, MAXPATHLEN);
489 i = setfile(folder, 0);
490 if (i < 0)
491 exit(1); /* error already reported */
492 if (options & OPT_EXISTONLY)
493 exit(i);
495 if (options & OPT_HEADERSONLY) {
496 print_headers(1, msgCount);
497 exit(exit_status);
500 callhook(mailname, 0);
501 if (i > 0 && !boption("emptystart"))
502 exit(1);
504 if (sigsetjmp(__hdrjmp, 1) == 0) {
505 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
506 safe_signal(SIGINT, _hdrstop);
507 if (!(options & OPT_N_FLAG)) {
508 if (!value("quiet"))
509 printf(tr(140, "%s version %s. Type ? for help.\n"),
510 (boption("bsdcompat") ? "Mail" : uagent), version);
511 announce(1);
512 fflush(stdout);
514 safe_signal(SIGINT, prevint);
517 /* Enter the command loop */
518 if (options & OPT_INTERACTIVE)
519 tty_init();
520 commands();
521 if (options & OPT_INTERACTIVE)
522 tty_destroy();
524 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
525 safe_signal(SIGHUP, SIG_IGN);
526 safe_signal(SIGINT, SIG_IGN);
527 safe_signal(SIGQUIT, SIG_IGN);
529 save_mbox_for_possible_quitstuff();
530 quit();
531 return exit_status;
534 static void
535 _hdrstop(int signo)
537 UNUSED(signo);
539 fflush(stdout);
540 fprintf(stderr, tr(141, "\nInterrupt\n"));
541 siglongjmp(__hdrjmp, 1);
545 main(int argc, char *argv[])
547 static char const optstr[] = "A:a:Bb:c:DdEeFfHiNnO:q:Rr:S:s:tu:Vv~#",
548 usagestr[] =
549 "Synopsis:\n"
550 " %s [-BDdEFintv~] [-A acc] [-a attachment] "
551 "[-b bcc-addr] [-c cc-addr]\n"
552 "\t [-O mtaopt [-O mtaopt-arg]] [-q file] [-r from-addr] "
553 "[-S var[=value]]\n"
554 "\t [-s subject] to-addr...\n"
555 " %s [-BDdEeHiNnRv~#] [-A acct] "
556 "[-S var[=value]] -f [file]\n"
557 " %s [-BDdEeiNnRv~#] [-A acc] [-S var[=value]] [-u user]\n";
559 struct a_arg *a_head = NULL, *a_curr = /* silence CC */ NULL;
560 struct name *to = NULL, *cc = NULL, *bcc = NULL;
561 struct attachment *attach = NULL;
562 char *cp = NULL, *subject = NULL, *qf = NULL, *Aflag = NULL;
563 char const *okey, **oargs = NULL, *folder = NULL;
564 int oargs_size = 0, oargs_count = 0, smopts_size = 0, i;
567 * Start our lengthy setup
570 starting =
571 unset_allow_undefined = TRU1;
573 progname = argv[0];
574 _startup();
576 /* Command line parsing */
577 while ((i = _getopt(argc, argv, optstr)) >= 0) {
578 switch (i) {
579 case 'A':
580 /* Execute an account command later on */
581 Aflag = _oarg;
582 break;
583 case 'a':
584 { struct a_arg *nap = ac_alloc(sizeof(struct a_arg));
585 if (a_head == NULL)
586 a_head = nap;
587 else
588 a_curr->aa_next = nap;
589 nap->aa_next = NULL;
590 nap->aa_file = _oarg;
591 a_curr = nap;
593 options |= OPT_SENDMODE;
594 break;
595 case 'B':
596 /* Make 0/1 line buffered */
597 setvbuf(stdin, NULL, _IOLBF, 0);
598 setvbuf(stdout, NULL, _IOLBF, 0);
599 break;
600 case 'b':
601 /* Get Blind Carbon Copy Recipient list */
602 bcc = cat(bcc, checkaddrs(lextract(_oarg, GBCC | GFULL)));
603 options |= OPT_SENDMODE;
604 break;
605 case 'c':
606 /* Get Carbon Copy Recipient list */
607 cc = cat(cc, checkaddrs(lextract(_oarg, GCC | GFULL)));
608 options |= OPT_SENDMODE;
609 break;
610 case 'D':
611 #ifdef HAVE_IMAP
612 okey = "disconnected";
613 goto joarg;
614 #else
615 break;
616 #endif
617 case 'd':
618 okey = "debug";
619 #ifdef HAVE_DEBUG
620 assign(okey, "");
621 #endif
622 goto joarg;
623 case 'E':
624 okey = "skipemptybody";
625 goto joarg;
626 case 'e':
627 options |= OPT_EXISTONLY;
628 break;
629 case 'F':
630 options |= OPT_F_FLAG | OPT_SENDMODE;
631 break;
632 case 'f':
633 /* User is specifying file to "edit" with Mail, as opposed to reading
634 * system mailbox. If no argument is given, we read his mbox file.
635 * Check for remaining arguments later */
636 folder = "&";
637 break;
638 case 'H':
639 options |= OPT_HEADERSONLY;
640 break;
641 case 'i':
642 /* Ignore interrupts */
643 okey = "ignore";
644 goto joarg;
645 case 'N':
646 /* Avoid initial header printing */
647 okey = "noheader";
648 goto joarg;
649 case 'n':
650 /* Don't source "unspecified system start-up file" */
651 options |= OPT_NOSRC;
652 break;
653 case 'O':
654 /* Additional options to pass-through to MTA */
655 if (smopts_count == (size_t)smopts_size)
656 smopts_size = _grow_cpp(&smopts, smopts_size, (int)smopts_count);
657 smopts[smopts_count++] = skin(_oarg);
658 break;
659 case 'q':
660 /* Quote file TODO drop? -Q with real quote?? what ? */
661 if (*_oarg != '-')
662 qf = _oarg;
663 options |= OPT_SENDMODE;
664 break;
665 case 'R':
666 /* Open folders read-only */
667 options |= OPT_R_FLAG;
668 break;
669 case 'r':
670 /* Set From address. */
671 options |= OPT_r_FLAG;
672 if ((option_r_arg = _oarg)[0] != '\0') {
673 struct name *fa = nalloc(_oarg, GFULL);
675 if (is_addr_invalid(fa, 1) || is_fileorpipe_addr(fa)) {
676 fprintf(stderr, tr(271, "Invalid address argument with -r\n"));
677 goto jusage;
679 option_r_arg = fa->n_name;
680 /* TODO -r options is set in smopts, but may
681 * TODO be overwritten by setting from= in
682 * TODO an interactive session!
683 * TODO Maybe disable setting of from?
684 * TODO Warn user? Update manual!! */
685 okey = savecat("from=", fa->n_fullname);
686 goto joarg;
687 /* ..and fa goes even though it is ready :/ */
689 break;
690 case 'S':
691 /* Set variable. We need to do this twice, since the
692 * user surely wants the setting to take effect
693 * immediately, but also doesn't want it to be
694 * overwritten from within resource files */
695 { char *a[2];
696 okey = a[0] = _oarg;
697 a[1] = NULL;
698 set(a);
700 joarg:
701 if (oargs_count == oargs_size)
702 oargs_size = _grow_cpp(&oargs, oargs_size, oargs_count);
703 oargs[oargs_count++] = okey;
704 break;
705 case 's':
706 /* Subject: */
707 subject = _oarg;
708 options |= OPT_SENDMODE;
709 break;
710 case 't':
711 /* Read defined set of headers from mail to be send */
712 options |= OPT_SENDMODE | OPT_t_FLAG;
713 break;
714 case 'u':
715 /* Set user name to pretend to be; don't set OPT_u_FLAG yet, this is
716 * done as necessary in _setup_vars() above */
717 myname = _oarg;
718 break;
719 case 'V':
720 puts(version);
721 exit(0);
722 /* NOTREACHED */
723 case 'v':
724 /* Be verbose */
725 okey = "verbose";
726 #ifdef HAVE_DEBUG
727 assign(okey, "");
728 #endif
729 goto joarg;
730 case '~':
731 /* Enable tilde escapes even in non-interactive mode */
732 options |= OPT_TILDE_FLAG;
733 break;
734 case '#':
735 /* Work in batch mode, even if non-interactive */
736 if (oargs_count + 5 >= oargs_size)
737 oargs_size = _grow_cpp(&oargs, oargs_size, oargs_count);
738 oargs[oargs_count++] = "dot";
739 oargs[oargs_count++] = "emptystart";
740 oargs[oargs_count++] = "noheader";
741 oargs[oargs_count++] = "quiet";
742 oargs[oargs_count++] = "sendwait";
743 options |= OPT_TILDE_FLAG | OPT_BATCH_FLAG;
744 break;
745 case '?':
746 jusage:
747 fprintf(stderr, tr(135, usagestr), progname, progname, progname);
748 exit(2);
752 if (folder != NULL) {
753 if (_oind < argc) {
754 if (_oind + 1 < argc) {
755 fprintf(stderr, tr(205, "More than one file given with -f\n"));
756 goto jusage;
758 folder = argv[_oind];
760 } else {
761 for (i = _oind; argv[i]; ++i)
762 to = cat(to, checkaddrs(lextract(argv[i], GTO | GFULL)));
763 if (to != NULL)
764 options |= OPT_SENDMODE;
767 /* Check for inconsistent arguments */
768 if (folder != NULL && to != NULL) {
769 fprintf(stderr, tr(137, "Cannot give -f and people to send to.\n"));
770 goto jusage;
772 if ((options & (OPT_SENDMODE | OPT_t_FLAG)) == OPT_SENDMODE && to == NULL) {
773 fprintf(stderr, tr(138,
774 "Send options without primary recipient specified.\n"));
775 goto jusage;
777 if ((options & OPT_R_FLAG) && to != NULL) {
778 fprintf(stderr, tr(235, "The -R option is meaningless in send mode.\n"));
779 goto jusage;
783 * Likely to go, perform more setup
786 _setup_vars();
788 if (options & OPT_INTERACTIVE) {
789 _setscreensize(0);
790 #ifdef SIGWINCH
791 # ifndef TTY_WANTS_SIGWINCH
792 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
793 # endif
794 safe_signal(SIGWINCH, _setscreensize);
795 #endif
796 } else
797 scrnheight = realscreenheight = 24, scrnwidth = 80;
799 /* Snapshot our string pools. Memory is auto-reclaimed from now on */
800 spreserve();
802 if (!(options & OPT_NOSRC) && getenv("NAIL_NO_SYSTEM_RC") == NULL)
803 load(SYSCONFRC);
804 /* *expand() returns a savestr(), but load only uses the file name for
805 * fopen(), so it's safe to do this */
806 if ((cp = getenv("MAILRC")) != NULL)
807 load(file_expand(cp));
808 else if ((cp = getenv("NAILRC")) != NULL)
809 load(file_expand(cp));
810 else
811 load(file_expand(MAILRC));
812 if (getenv("NAIL_EXTRA_RC") == NULL && (cp = value("NAIL_EXTRA_RC")) != NULL)
813 load(file_expand(cp));
815 /* Now we can set the account */
816 if (Aflag != NULL) {
817 char *a[2];
818 a[0] = Aflag;
819 a[1] = NULL;
820 c_account(a);
823 /* Ensure the -S and other command line options take precedence over
824 * anything that may have been placed in resource files */
825 for (i = 0; i < oargs_count; ++i) {
826 char const *a[2];
827 a[0] = oargs[i];
828 a[1] = NULL;
829 set(a);
833 * We're finally completely setup and ready to go
836 starting =
837 unset_allow_undefined = FAL0;
839 if (options & OPT_DEBUG)
840 fprintf(stderr, tr(199, "user = %s, homedir = %s\n"), myname, homedir);
842 if (!(options & OPT_SENDMODE)) {
843 exit_status = _rcv_mode(folder);
844 goto jleave;
847 /* Now that full mailx(1)-style file expansion is possible handle the
848 * attachments which we had delayed due to this.
849 * This may use savestr(), but since we won't enter the command loop we
850 * don't need to care about that */
851 while (a_head != NULL) {
852 attach = add_attachment(attach, a_head->aa_file, NULL);
853 if (attach != NULL) {
854 a_curr = a_head;
855 a_head = a_head->aa_next;
856 ac_free(a_curr);
857 } else {
858 perror(a_head->aa_file);
859 exit(1);
863 /* xxx exit_status = EXIT_OK; */
864 if (options & OPT_INTERACTIVE)
865 tty_init();
866 mail(to, cc, bcc, subject, attach, qf, ((options & OPT_F_FLAG) != 0));
867 if (options & OPT_INTERACTIVE)
868 tty_destroy();
869 jleave:
870 return exit_status;
873 /* vim:set fenc=utf-8:s-it-mode */