Gavin Troy reported SEGV: partially revert [ac694bb]..
[s-mailx.git] / nail.h
blobc2717cab6ebb884771c6427085a1e4bc1bcc48fb
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Header inclusion, macros, constants, types and the global var declarations.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 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 * Mail -- a mail program
43 * Author: Kurt Shoens (UCB) March 25, 1978
46 #include "config.h"
48 #include <sys/stat.h>
49 #include <sys/types.h>
51 #include <errno.h>
52 #include <limits.h>
53 #include <setjmp.h>
54 #include <signal.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <termios.h>
60 #include <time.h>
61 #include <unistd.h>
63 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
64 # include <stdint.h>
65 #else
66 # include <inttypes.h>
67 #endif
69 #ifdef HAVE_C90AMEND1
70 # include <wchar.h>
71 # include <wctype.h>
72 #endif
73 #ifdef HAVE_DEBUG
74 # include <assert.h>
75 #endif
76 #ifdef HAVE_ICONV
77 # include <iconv.h>
78 #endif
79 #ifdef HAVE_REGEX
80 # include <regex.h>
81 #endif
83 #ifdef HAVE_OPENSSL_MD5
84 # include <openssl/md5.h>
85 #endif
88 * Constants, some nail-specific macros
91 #if !defined NI_MAXHOST || NI_MAXHOST < 1025
92 # undef NI_MAXHOST
93 # define NI_MAXHOST 1025
94 #endif
96 #ifndef PATH_MAX
97 # ifdef MAXPATHLEN
98 # define PATH_MAX MAXPATHLEN
99 # else
100 # define PATH_MAX 1024 /* _XOPEN_PATH_MAX POSIX 2008/Cor 1-2013 */
101 # endif
102 #endif
104 #ifndef STDIN_FILENO
105 # define STDIN_FILENO 0
106 #endif
107 #ifndef STDOUT_FILENO
108 # define STDOUT_FILENO 1
109 #endif
110 #ifndef STDERR_FILENO
111 # define STDERR_FILENO 2
112 #endif
114 #ifdef NSIG_MAX
115 # undef NSIG
116 # define NSIG NSIG_MAX
117 #elif !defined NSIG
118 # define NSIG ((sizeof(sigset_t) * 8) - 1)
119 #endif
121 /* */
123 #if BUFSIZ > 2560 /* TODO simply use BUFSIZ? */
124 # define LINESIZE BUFSIZ /* max readable line width */
125 #else
126 # define LINESIZE 2560
127 #endif
128 #define BUFFER_SIZE (BUFSIZ >= (1u << 13) ? BUFSIZ : (1u << 14))
130 #define CBAD (-15555)
131 #define APPEND /* New mail goes to end of mailbox */
132 #define ESCAPE '~' /* Default escape for sending */
133 #define FIO_STACK_SIZE 20 /* Maximum recursion for sourcing */
134 #define HIST_SIZE 242 /* tty.c: history list default size */
135 #define HSHSIZE 23 /* Hash prime (aliases, vars, macros) */
136 #define MAXARGC 1024 /* Maximum list of raw strings */
137 #define MAXEXP 25 /* Maximum expansion of aliases */
138 #define NYD_CALLS_MAX 1000 /* Number of NYD calls that are remembered */
139 #define PROMPT_BUFFER_SIZE 80 /* getprompt() bufsize (> 3!) */
141 #define ACCOUNT_NULL "null" /* Name of "null" account */
142 #define MAILRC "~/.mailrc"
143 #define TMPDIR_FALLBACK "/tmp"
145 #define COLOUR_MSGINFO "fg=green"
146 #define COLOUR_PARTINFO "fg=brown"
147 #define COLOUR_FROM_ "fg=brown"
148 #define COLOUR_HEADER "fg=red"
149 #define COLOUR_UHEADER "ft=bold,fg=red"
150 #define COLOUR_PAGERS "less"
151 #define COLOUR_TERMS \
152 "cons25,linux,rxvt,rxvt-unicode,screen,sun,"\
153 "vt100,vt220,wsvt25,xterm,xterm-color"
154 #define COLOUR_USER_HEADERS "from,subject"
156 #define FROM_DATEBUF 64 /* Size of RFC 4155 From_ line date */
157 #define DATE_DAYSYEAR 365L
158 #define DATE_SECSMIN 60L
159 #define DATE_MINSHOUR 60L
160 #define DATE_HOURSDAY 24L
161 #define DATE_SECSDAY (DATE_SECSMIN * DATE_MINSHOUR * DATE_HOURSDAY)
163 /* Default *encoding* as enum conversion below */
164 #define MIME_DEFAULT_ENCODING CONV_TOQP
166 /* Maximum allowed line length in a mail before QP folding is necessary), and
167 * the real limit we go for */
168 #define MIME_LINELEN_MAX 1000
169 #define MIME_LINELEN_LIMIT (MIME_LINELEN_MAX - 50)
171 /* Locations of mime.types(5) */
172 #define MIME_TYPES_USR "~/.mime.types"
173 #define MIME_TYPES_SYS "/etc/mime.types"
175 /* Fallback MIME charsets, if *charset-7bit* and *charset-8bit* or not set */
176 #define CHARSET_7BIT "US-ASCII"
177 #ifdef HAVE_ICONV
178 # define CHARSET_8BIT "UTF-8"
179 # define CHARSET_8BIT_OKEY charset_8bit
180 #else
181 # define CHARSET_8BIT "ISO-8859-1"
182 # define CHARSET_8BIT_OKEY ttycharset
183 #endif
185 /* Is *W* a quoting (ASCII only) character? */
186 #define ISQUOTE(W) \
187 ((W) == L'>' || (W) == L'|' || (W) == L'}' || (W) == L':')
189 /* Maximum number of quote characters (not bytes!) that'll be used on
190 * follow lines when compressing leading quote characters */
191 #define QUOTE_MAX 42
193 /* How much spaces should a <tab> count when *quote-fold*ing? (power-of-two!) */
194 #define QUOTE_TAB_SPACES 8
196 /* Maximum size of a message that is passed through to the spam system */
197 #define SPAM_MAXSIZE 420000
199 /* String dope: dynamic buffer size, and size of the single builtin one that's
200 * used first */
201 #define SBUFFER_SIZE 0x18000u
202 #define SBUFFER_BUILTIN 0x2000u
204 /* These come from the configuration (named Xxy to not clash with sh(1)..) */
205 #ifndef XSHELL
206 # define XSHELL "/bin/sh"
207 #endif
208 #ifndef XLISTER
209 # define XLISTER "ls"
210 #endif
211 #ifndef XPAGER
212 # define XPAGER "more"
213 #endif
216 * CC support, generic macros etc.
219 #undef __PREREQ
220 #if defined __GNUC__ || defined __clang__
221 # define __EXTEN __extension__
222 # ifdef __GNUC__
223 # define __PREREQ(X,Y) \
224 (__GNUC__ > (X) || (__GNUC__ == (X) && __GNUC_MINOR__ >= (Y)))
225 # else
226 # define __PREREQ(X,Y) 1
227 # endif
228 #else
229 # define __EXTEN
230 # define __PREREQ(X,Y) 0
231 #endif
233 #define EMPTY_FILE(F) typedef int CONCAT(avoid_empty_file__, F);
235 /* Pointer to size_t */
236 #define PTR2SIZE(X) ((size_t)(uintptr_t)(X))
238 /* Pointer comparison (types from below) */
239 #define PTRCMP(A,C,B) ((uintptr_t)(A) C (uintptr_t)(B))
241 /* Ditto, compare (maybe mixed-signed) integers cases to T bits, unsigned;
242 * Note: doesn't sign-extend correctly, that's still up to the caller */
243 #define UICMP(T,A,C,B) ((ui ## T ## _t)(A) C (ui ## T ## _t)(B))
245 /* Members in constant array */
246 #ifndef NELEM
247 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
248 #endif
250 /* sizeof() for member fields */
251 #define SIZEOF_FIELD(T,F) sizeof(((T *)NULL)->F)
253 /* Casts-away (*NOT* cast-away) */
254 #define UNUSED(X) ((void)(X))
255 #define UNCONST(P) ((void*)(uintptr_t)(void const*)(P))
256 #define UNVOLATILE(P) ((void*)(uintptr_t)(void volatile*)(P))
257 #define UNXXX(T,C,P) ((T)(uintptr_t)(C)(P))
259 /* __STDC_VERSION__ is ISO C99, so also use __STDC__, which should work */
260 #if defined __STDC__ || defined __STDC_VERSION__ /*|| defined __cplusplus*/
261 # define STRING(X) #X
262 # define XSTRING(X) STRING(X)
263 # define CONCAT(S1,S2) _CONCAT(S1, S2)
264 # define _CONCAT(S1,S2) S1 ## S2
265 #else
266 # define STRING(X) "X"
267 # define XSTRING STRING
268 # define CONCAT(S1,S2) S1/**/S2
269 #endif
271 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
272 /* Variable size arrays and structure fields */
273 # define VFIELD_SIZE(X)
274 # define VFIELD_SIZEOF(T,F) (0)
275 /* Inline functions */
276 # define HAVE_INLINE
277 # define INLINE inline
278 # define SINLINE static inline
279 #else
280 # define VFIELD_SIZE(X) (X)
281 # define VFIELD_SIZEOF(T,F) SIZEOF_FIELD(T, F)
282 # if __PREREQ(2, 9)
283 # define INLINE static __inline
284 # define SINLINE static __inline
285 # else
286 # define INLINE
287 # define SINLINE static
288 # endif
289 #endif
291 #undef __FUN__
292 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
293 # define __FUN__ __func__
294 #elif __PREREQ(3, 4)
295 # define __FUN__ __FUNCTION__
296 #else
297 # define __FUN__ uagent /* Something that is not a literal */
298 #endif
300 #if defined __predict_true && defined __predict_false
301 # define LIKELY(X) __predict_true(X)
302 # define UNLIKELY(X) __predict_false(X)
303 #elif __PREREQ(2, 96)
304 # define LIKELY(X) __builtin_expect(X, 1)
305 # define UNLIKELY(X) __builtin_expect(X, 0)
306 #else
307 # define LIKELY(X) (X)
308 # define UNLIKELY(X) (X)
309 #endif
311 /* Compile-Time-Assert */
312 #define CTA(TEST) _CTA_1(TEST, __LINE__)
313 #define _CTA_1(TEST,L) _CTA_2(TEST, L)
314 #define _CTA_2(TEST,L) \
315 typedef char COMPILE_TIME_ASSERT_failed_at_line_ ## L[(TEST) ? 1 : -1]
317 #undef ISPOW2
318 #define ISPOW2(X) ((((X) - 1) & (X)) == 0)
319 #undef MIN
320 #define MIN(A, B) ((A) < (B) ? (A) : (B))
321 #undef MAX
322 #define MAX(A, B) ((A) < (B) ? (B) : (A))
323 #undef ABS
324 #define ABS(A) ((A) < 0 ? -(A) : (A))
326 #undef DBG
327 #undef NDBG
328 #ifndef HAVE_DEBUG
329 # undef assert
330 # define assert(X) UNUSED(0)
331 # define DBG(X)
332 # define NDBG(X) X
333 #else
334 # define DBG(X) X
335 # define NDBG(X)
336 #endif
338 /* Translation (init in main.c) */
339 #undef tr
340 #ifdef HAVE_CATGETS
341 # define CATSET 1
342 # define tr(c,d) catgets(catd, CATSET, c, d)
343 #else
344 # define tr(c,d) (d)
345 #endif
348 * Types
351 /* TODO convert all integer types to the new [su]i(8|16|32|64)_t */
352 typedef unsigned long ul_it;
353 typedef unsigned int ui_it;
354 typedef unsigned short us_it;
355 typedef unsigned char uc_it;
357 typedef signed long sl_it;
358 typedef signed int si_it;
359 typedef signed short ss_it;
360 typedef signed char sc_it;
362 #ifdef UINT8_MAX
363 # define UI8_MAX UINT8_MAX
364 # define SI8_MIN INT8_MIN
365 # define SI8_MAX INT8_MAX
366 typedef uint8_t ui8_t;
367 typedef int8_t si8_t;
368 #elif UCHAR_MAX != 255
369 # error UCHAR_MAX must be 255
370 #else
371 # define UI8_MAX UCHAR_MAX
372 # define SI8_MIN CHAR_MIN
373 # define SI8_MAX CHAR_MAX
374 typedef unsigned char ui8_t;
375 typedef signed char si8_t;
376 #endif
378 #ifdef UINT16_MAX
379 # define UI16_MAX UINT16_MAX
380 # define SI16_MIN INT16_MIN
381 # define SI16_MAX INT16_MAX
382 typedef uint16_t ui16_t;
383 typedef int16_t si16_t;
384 #elif USHRT_MAX != 0xFFFFu
385 # error USHRT_MAX must be 0xFFFF
386 #else
387 # define UI16_MAX USHRT_MAX
388 # define SI16_MIN SHRT_MIN
389 # define SI16_MAX SHRT_MAX
390 typedef unsigned short ui16_t;
391 typedef signed short si16_t;
392 #endif
394 #ifdef UINT32_MAX
395 # define UI32_MAX UINT32_MAX
396 # define SI32_MIN INT32_MIN
397 # define SI32_MAX INT32_MAX
398 typedef uint32_t ui32_t;
399 typedef int32_t si32_t;
400 #elif ULONG_MAX == 0xFFFFFFFFu
401 # define UI32_MAX ULONG_MAX
402 # define SI32_MIN LONG_MIN
403 # define SI32_MAX LONG_MAX
404 typedef unsigned long int ui32_t;
405 typedef signed long int si32_t;
406 #elif UINT_MAX != 0xFFFFFFFFu
407 # error UINT_MAX must be 0xFFFFFFFF
408 #else
409 # define UI32_MAX UINT_MAX
410 # define SI32_MIN INT_MIN
411 # define SI32_MAX INT_MAX
412 typedef unsigned int ui32_t;
413 typedef signed int si32_t;
414 #endif
416 #ifdef UINT64_MAX
417 # define UI64_MAX UINT64_MAX
418 # define SI64_MIN INT64_MIN
419 # define SI64_MAX INT64_MAX
420 typedef uint64_t ui64_t;
421 #elif ULONG_MAX <= 0xFFFFFFFFu
422 # if !defined ULLONG_MAX || ULLONG_MAX != 0xFFFFFFFFFFFFFFFFu
423 # error We need a 64 bit integer
424 # else
425 # define UI64_MAX ULLONG_MAX
426 # define SI64_MIN LLONG_MIN
427 # define SI64_MAX LLONG_MAX
428 __EXTEN typedef unsigned long long ui64_t;
429 __EXTEN typedef signed long long si64_t;
430 # endif
431 #else
432 # define UI64_MAX ULONG_MAX
433 # define SI64_MIN LONG_MIN
434 # define SI64_MAX LONG_MAX
435 typedef unsigned long ui64_t;
436 typedef signed long si64_t;
437 #endif
439 /* (So that we can use UICMP() for size_t comparison, too) */
440 typedef size_t uiz_t;
441 typedef ssize_t siz_t;
443 #ifndef UINTPTR_MAX
444 # ifdef SIZE_MAX
445 # define uintptr_t size_t
446 # define UINTPTR_MAX SIZE_MAX
447 # else
448 # define uintptr_t unsigned long
449 # define UINTPTR_MAX ULONG_MAX
450 # endif
451 #endif
453 /* XXX Note we don't really deal with that the right way in that we pass size_t
454 * XXX arguments without casting; should do, because above we assert UINT_MAX
455 * XXX is indeed ui32_t */
456 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
457 # define ZFMT "zu"
458 #elif defined SIZE_MAX && SIZE_MAX == 0xFFFFFFFFu && ULONG_MAX != UINT_MAX
459 # define ZFMT "u"
460 #endif
461 #ifndef ZFMT
462 # define ZFMT "lu"
463 #endif
465 enum {FAL0, TRU1};
466 typedef si8_t bool_t;
468 typedef void ( *sighandler_type)(int);
470 enum user_options {
471 OPT_NONE,
472 OPT_DEBUG = 1u<< 0, /* -d / *debug* */
473 OPT_VERBOSE = 1u<< 1, /* -v / *verbose* */
474 OPT_EXISTONLY = 1u<< 2, /* -e */
475 OPT_HEADERSONLY = 1u<< 3, /* -H */
476 OPT_HEADERLIST = 1u<< 4, /* -L */
477 OPT_NOSRC = 1u<< 5, /* -n */
478 OPT_E_FLAG = 1u<< 6, /* -E / *skipemptybody* */
479 OPT_F_FLAG = 1u<< 7, /* -F */
480 OPT_N_FLAG = 1u<< 8, /* -N / *header* */
481 OPT_R_FLAG = 1u<< 9, /* -R */
482 OPT_r_FLAG = 1u<<10, /* -r (plus option_r_arg) */
483 OPT_t_FLAG = 1u<<11, /* -t */
484 OPT_u_FLAG = 1u<<12, /* -u given, or USER != getpwnam(3) */
485 OPT_TILDE_FLAG = 1u<<13, /* -~ */
486 OPT_BATCH_FLAG = 1u<<14, /* -# */
488 OPT_SENDMODE = 1u<<15, /* Usage case forces send mode */
489 OPT_INTERACTIVE = 1u<<16, /* isatty(0) */
490 OPT_TTYIN = OPT_INTERACTIVE,
491 OPT_TTYOUT = 1u<<17
493 #define IS_TTY_SESSION() \
494 ((options & (OPT_TTYIN | OPT_TTYOUT)) == (OPT_TTYIN | OPT_TTYOUT))
496 enum exit_status {
497 EXIT_OK = EXIT_SUCCESS,
498 EXIT_ERR = EXIT_FAILURE,
499 EXIT_USE = 2,
500 EXIT_COLL_ABORT = 1<<1, /* Message collection was aborted */
501 EXIT_SEND_ERROR = 1<<2 /* Unspecified send error occurred */
504 enum fexp_mode {
505 FEXP_FULL, /* Full expansion */
506 FEXP_LOCAL = 1<<0, /* Result must be local file/maildir */
507 FEXP_SHELL = 1<<1, /* No folder %,#,&,+ stuff, yet sh(1) */
508 FEXP_NSHORTCUT = 1<<2, /* Don't expand shortcuts */
509 FEXP_SILENT = 1<<3, /* Don't print but only return errors */
510 FEXP_MULTIOK = 1<<4 /* Expansion to many entries is ok */
513 enum flock_type {
514 FLOCK_READ,
515 FLOCK_WRITE,
516 FLOCK_UNLOCK
519 /* <0 means "stop" unless *prompt* extensions are enabled. */
520 enum prompt_exp {
521 PROMPT_STOP = -1, /* \c */
522 /* *prompt* extensions: \$, \@ etc. */
523 PROMPT_DOLLAR = -2,
524 PROMPT_AT = -3
527 enum colourspec {
528 COLOURSPEC_MSGINFO,
529 COLOURSPEC_PARTINFO,
530 COLOURSPEC_FROM_,
531 COLOURSPEC_HEADER,
532 COLOURSPEC_UHEADER,
533 COLOURSPEC_RESET
536 enum okay {
537 STOP = 0,
538 OKAY = 1
541 enum mimeenc {
542 MIME_NONE, /* message is not in MIME format */
543 MIME_BIN, /* message is in binary encoding */
544 MIME_8B, /* message is in 8bit encoding */
545 MIME_7B, /* message is in 7bit encoding */
546 MIME_QP, /* message is quoted-printable */
547 MIME_B64 /* message is in base64 encoding */
550 enum conversion {
551 CONV_NONE, /* no conversion */
552 CONV_7BIT, /* no conversion, is 7bit */
553 CONV_FROMQP, /* convert from quoted-printable */
554 CONV_TOQP, /* convert to quoted-printable */
555 CONV_8BIT, /* convert to 8bit (iconv) */
556 CONV_FROMB64, /* convert from base64 */
557 CONV_FROMB64_T, /* convert from base64/text */
558 CONV_TOB64, /* convert to base64 */
559 CONV_FROMHDR, /* convert from RFC1522 format */
560 CONV_TOHDR, /* convert to RFC1522 format */
561 CONV_TOHDR_A /* convert addresses for header */
564 enum sendaction {
565 SEND_MBOX, /* no conversion to perform */
566 SEND_RFC822, /* no conversion, no From_ line */
567 SEND_TODISP, /* convert to displayable form */
568 SEND_TODISP_ALL, /* same, include all MIME parts */
569 SEND_SHOW, /* convert to 'show' command form */
570 SEND_TOSRCH, /* convert for IMAP SEARCH */
571 SEND_TOFLTR, /* convert for spam mail filtering */
572 SEND_TOFILE, /* convert for saving body to a file */
573 SEND_TOPIPE, /* convert for pipe-content/subc. */
574 SEND_QUOTE, /* convert for quoting */
575 SEND_QUOTE_ALL, /* same, include all MIME parts */
576 SEND_DECRYPT /* decrypt */
579 enum mimecontent {
580 MIME_UNKNOWN, /* unknown content */
581 MIME_SUBHDR, /* inside a multipart subheader */
582 MIME_822, /* message/rfc822 content */
583 MIME_MESSAGE, /* other message/ content */
584 MIME_TEXT_PLAIN, /* text/plain content */
585 MIME_TEXT_HTML, /* text/html content */
586 MIME_TEXT, /* other text/ content */
587 MIME_ALTERNATIVE, /* multipart/alternative content */
588 MIME_DIGEST, /* multipart/digest content */
589 MIME_MULTI, /* other multipart/ content */
590 MIME_PKCS7, /* PKCS7 content */
591 MIME_DISCARD /* content is discarded */
594 enum oflags {
595 OF_RDONLY = 1<<0,
596 OF_WRONLY = 1<<1,
597 OF_RDWR = 1<<2,
598 OF_APPEND = 1<<3,
599 OF_CREATE = 1<<4,
600 OF_TRUNC = 1<<5,
601 OF_EXCL = 1<<6,
602 OF_CLOEXEC = 1<<7,
603 OF_UNLINK = 1<<8, /* Only for Ftmp(): unlink(2) after creation */
604 OF_HOLDSIGS = 1<<9, /* Mutual with OF_UNLINK - await Ftmp_free() */
605 OF_REGISTER = 1<<10 /* Register file in our file table */
608 enum tdflags {
609 TD_NONE, /* no display conversion */
610 TD_ISPR = 1<<0, /* use isprint() checks */
611 TD_ICONV = 1<<1, /* use iconv() */
612 TD_DELCTRL = 1<<2, /* delete control characters */
615 * NOTE: _TD_EOF and _TD_BUFCOPY may be ORd with enum conversion and
616 * enum sendaction, and may thus NOT clash with their bit range!
618 _TD_EOF = 1<<14, /* EOF seen, last round! */
619 _TD_BUFCOPY = 1<<15 /* Buffer may be constant, copy it */
622 enum protocol {
623 PROTO_FILE, /* refers to a local file */
624 PROTO_POP3, /* is a pop3 server string */
625 PROTO_IMAP, /* is an imap server string */
626 PROTO_MAILDIR, /* refers to a maildir folder */
627 PROTO_UNKNOWN /* unknown protocol */
630 #ifdef HAVE_SSL
631 enum ssl_verify_level {
632 SSL_VERIFY_IGNORE,
633 SSL_VERIFY_WARN,
634 SSL_VERIFY_ASK,
635 SSL_VERIFY_STRICT
637 #endif
639 /* A large enum with all the binary and value options a.k.a their keys.
640 * Only the constant keys are in here, to be looked up via ok_[bv]look(),
641 * ok_[bv]set() and ok_[bv]clear().
642 * Note: see the comments in acmava.c before changing *anything* in here! */
643 enum okeys {
644 /* Option keys for binary options */
645 ok_b_add_file_recipients,
646 ok_b_allnet,
647 ok_b_append,
648 ok_b_ask,
649 ok_b_askatend,
650 ok_b_askattach,
651 ok_b_askbcc,
652 ok_b_askcc,
653 ok_b_asksign,
654 ok_b_asksub,
655 ok_b_attachment_ask_content_description,
656 ok_b_attachment_ask_content_disposition,
657 ok_b_attachment_ask_content_id,
658 ok_b_attachment_ask_content_type,
659 ok_b_autocollapse,
660 ok_b_autoprint,
661 ok_b_autothread,
662 ok_b_bang,
663 ok_b_batch_exit_on_error,
664 ok_b_bsdannounce,
665 ok_b_bsdcompat,
666 ok_b_bsdflags,
667 ok_b_bsdheadline,
668 ok_b_bsdmsgs,
669 ok_b_bsdorder,
670 ok_b_bsdset,
671 ok_b_colour_disable,
672 ok_b_debug, /* {special=1} */
673 ok_b_disconnected,
674 ok_b_dot,
675 ok_b_editalong,
676 ok_b_editheaders,
677 ok_b_emptybox,
678 ok_b_emptystart,
679 ok_b_flipr,
680 ok_b_forward_as_attachment,
681 ok_b_fullnames,
682 ok_b_header, /* {special=1} */
683 ok_b_hold,
684 ok_b_idna_disable,
685 ok_b_ignore,
686 ok_b_ignoreeof,
687 ok_b_imap_use_starttls,
688 ok_b_keep,
689 ok_b_keep_content_length,
690 ok_b_keepsave,
691 ok_b_line_editor_disable,
692 ok_b_markanswered,
693 ok_b_message_id_disable,
694 ok_b_metoo,
695 ok_b_mime_allow_text_controls,
696 ok_b_mime_counter_evidence,
697 ok_b_outfolder,
698 ok_b_page,
699 ok_b_piperaw,
700 ok_b_pop3_bulk_load,
701 ok_b_pop3_no_apop,
702 ok_b_pop3_use_starttls,
703 ok_b_print_all_chars,
704 ok_b_print_alternatives,
705 ok_b_quiet,
706 ok_b_quote_as_attachment,
707 ok_b_recipients_in_cc,
708 ok_b_record_resent,
709 ok_b_reply_in_same_charset,
710 ok_b_Replyall,
711 ok_b_rfc822_body_from_, /* {name=rfc822-body-from_} */
712 ok_b_save,
713 ok_b_searchheaders,
714 ok_b_sendcharsets_else_ttycharset,
715 ok_b_sendwait,
716 ok_b_showlast,
717 ok_b_showname,
718 ok_b_showto,
719 ok_b_skipemptybody, /* {special=1} */
720 ok_b_smime_force_encryption,
721 ok_b_smime_no_default_ca,
722 ok_b_smime_sign,
723 ok_b_smtp_use_starttls,
724 ok_b_ssl_no_default_ca,
725 ok_b_ssl_v2_allow,
726 ok_b_writebackedited,
727 ok_b_verbose, /* {special=1} */
729 /* Option keys for values options */
730 ok_v_attrlist,
731 ok_v_autobcc,
732 ok_v_autocc,
733 ok_v_autosort,
734 ok_v_charset_7bit,
735 ok_v_charset_8bit,
736 ok_v_cmd,
737 ok_v_colour_pagers,
738 ok_v_colour_from_, /* {name=colour-from_} */
739 ok_v_colour_header,
740 ok_v_colour_msginfo,
741 ok_v_colour_partinfo,
742 ok_v_colour_terms,
743 ok_v_colour_uheader,
744 ok_v_colour_user_headers,
745 ok_v_crt,
746 ok_v_datefield,
747 ok_v_datefield_markout_older,
748 ok_v_DEAD,
749 ok_v_EDITOR,
750 ok_v_encoding,
751 ok_v_escape,
752 ok_v_folder, /* {special=1} */
753 ok_v_folder_hook,
754 ok_v_from,
755 ok_v_fwdheading,
756 ok_v_headline,
757 ok_v_hostname,
758 ok_v_imap_auth,
759 ok_v_imap_cache,
760 ok_v_imap_keepalive,
761 ok_v_imap_list_depth,
762 ok_v_indentprefix,
763 ok_v_line_editor_cursor_right, /* {special=1} */
764 ok_v_LISTER,
765 ok_v_MAIL,
766 ok_v_MBOX,
767 ok_v_mimetypes_load_control,
768 ok_v_NAIL_EXTRA_RC, /* {name=NAIL_EXTRA_RC} */
769 ok_v_NAIL_HEAD, /* {name=NAIL_HEAD} */
770 ok_v_NAIL_HISTFILE, /* {name=NAIL_HISTFILE} */
771 ok_v_NAIL_HISTSIZE, /* {name=NAIL_HISTSIZE} */
772 ok_v_NAIL_TAIL, /* {name=NAIL_TAIL} */
773 ok_v_newfolders,
774 ok_v_newmail,
775 ok_v_ORGANIZATION,
776 ok_v_PAGER,
777 ok_v_pop3_keepalive,
778 ok_v_prompt,
779 ok_v_quote,
780 ok_v_quote_fold,
781 ok_v_record,
782 ok_v_replyto,
783 ok_v_screen,
784 ok_v_sendcharsets,
785 ok_v_sender,
786 ok_v_sendmail,
787 ok_v_sendmail_progname,
788 ok_v_SHELL,
789 ok_v_Sign,
790 ok_v_sign,
791 ok_v_signature,
792 ok_v_smime_ca_dir,
793 ok_v_smime_ca_file,
794 ok_v_smime_crl_dir,
795 ok_v_smime_crl_file,
796 ok_v_smime_sign_cert,
797 ok_v_smime_sign_include_certs,
798 ok_v_smtp,
799 ok_v_smtp_auth,
800 ok_v_smtp_auth_password,
801 ok_v_smtp_auth_user,
802 ok_v_spam_command,
803 ok_v_spam_host,
804 ok_v_spam_maxsize,
805 ok_v_spam_port,
806 ok_v_spam_socket,
807 ok_v_spam_user,
808 ok_v_ssl_ca_dir,
809 ok_v_ssl_ca_file,
810 ok_v_ssl_cert,
811 ok_v_ssl_cipher_list,
812 ok_v_ssl_crl_dir,
813 ok_v_ssl_crl_file,
814 ok_v_ssl_key,
815 ok_v_ssl_method,
816 ok_v_ssl_rand_egd,
817 ok_v_ssl_rand_file,
818 ok_v_ssl_verify,
819 ok_v_stealthmua,
820 ok_v_toplines,
821 ok_v_ttycharset,
822 ok_v_VISUAL
825 struct str {
826 char *s; /* the string's content */
827 size_t l; /* the stings's length */
830 struct colour_table {
831 /* Plus a copy of *colour-user-headers* */
832 struct str ct_csinfo[COLOURSPEC_RESET+1 + 1];
835 struct time_current {
836 time_t tc_time;
837 struct tm tc_gm;
838 struct tm tc_local;
839 char tc_ctime[32];
842 struct quoteflt {
843 FILE *qf_os; /* Output stream */
844 char const *qf_pfix;
845 ui32_t qf_pfix_len; /* Length of prefix: 0: bypass */
846 ui32_t qf_qfold_min; /* Simple way: wrote prefix? */
847 #ifdef HAVE_QUOTE_FOLD
848 ui32_t qf_qfold_max; /* Otherwise: line lengths */
849 ui8_t qf_state; /* *quote-fold* state machine */
850 bool_t qf_brk_isws; /* Breakpoint is at WS */
851 ui8_t __dummy[2];
852 ui32_t qf_wscnt; /* Whitespace count */
853 ui32_t qf_brkl; /* Breakpoint */
854 ui32_t qf_brkw; /* Visual width, breakpoint */
855 ui32_t qf_datw; /* Current visual output line width */
856 struct str qf_dat; /* Current visual output line */
857 struct str qf_currq; /* Current quote, compressed */
858 mbstate_t qf_mbps[2];
859 #endif
862 struct search_expr {
863 char const *ss_where; /* to search for the expression (not always used) */
864 char const *ss_sexpr; /* String search expression; NULL: use .ss_reexpr */
865 #ifdef HAVE_REGEX
866 regex_t ss_reexpr;
867 #endif
870 struct eval_ctx {
871 struct str ev_line;
872 bool_t ev_is_recursive; /* Evaluation in evaluation? (collect ~:) */
873 ui8_t __dummy[6];
874 bool_t ev_add_history; /* Enter (final) command in history? */
875 char const *ev_new_content; /* History: reenter line, start with this */
878 struct termios_state {
879 struct termios ts_tios;
880 char *ts_linebuf;
881 size_t ts_linesize;
882 bool_t ts_needs_reset;
885 #define termios_state_reset() \
886 do {\
887 if (termios_state.ts_needs_reset) {\
888 tcsetattr(0, TCSADRAIN, &termios_state.ts_tios);\
889 termios_state.ts_needs_reset = FAL0;\
891 } while (0)
893 struct sock { /* data associated with a socket */
894 int s_fd; /* file descriptor */
895 #ifdef HAVE_SSL
896 int s_use_ssl; /* SSL is used */
897 # ifdef HAVE_OPENSSL
898 void *s_ssl; /* SSL object */
899 void *s_ctx; /* SSL context object */
900 # endif
901 #endif
902 char *s_wbuf; /* for buffered writes */
903 int s_wbufsize; /* allocated size of s_buf */
904 int s_wbufpos; /* position of first empty data byte */
905 char *s_rbufptr; /* read pointer to s_rbuf */
906 int s_rsz; /* size of last read in s_rbuf */
907 char const *s_desc; /* description of error messages */
908 void (*s_onclose)(void); /* execute on close */
909 char s_rbuf[LINESIZE + 1]; /* for buffered reads */
912 struct mailbox {
913 enum {
914 MB_NONE = 000, /* no reply expected */
915 MB_COMD = 001, /* command reply expected */
916 MB_MULT = 002, /* multiline reply expected */
917 MB_PREAUTH = 004, /* not in authenticated state */
918 MB_BYE = 010 /* may accept a BYE state */
919 } mb_active;
920 FILE *mb_itf; /* temp file with messages, read open */
921 FILE *mb_otf; /* same, write open */
922 char *mb_sorted; /* sort method */
923 enum {
924 MB_VOID, /* no type (e. g. connection failed) */
925 MB_FILE, /* local file */
926 MB_POP3, /* POP3 mailbox */
927 MB_IMAP, /* IMAP mailbox */
928 MB_MAILDIR, /* maildir folder */
929 MB_CACHE /* cached mailbox */
930 } mb_type; /* type of mailbox */
931 enum {
932 MB_DELE = 01, /* may delete messages in mailbox */
933 MB_EDIT = 02 /* may edit messages in mailbox */
934 } mb_perm;
935 int mb_compressed; /* is a compressed mbox file */
936 int mb_threaded; /* mailbox has been threaded */
937 #ifdef HAVE_IMAP
938 enum mbflags {
939 MB_NOFLAGS = 000,
940 MB_UIDPLUS = 001 /* supports IMAP UIDPLUS */
941 } mb_flags;
942 unsigned long mb_uidvalidity; /* IMAP unique identifier validity */
943 char *mb_imap_account; /* name of current IMAP account */
944 char *mb_imap_mailbox; /* name of current IMAP mailbox */
945 char *mb_cache_directory; /* name of cache directory */
946 #endif
947 struct sock mb_sock; /* socket structure */
950 enum needspec {
951 NEED_UNSPEC, /* unspecified need, don't fetch */
952 NEED_HEADER, /* need the header of a message */
953 NEED_BODY /* need header and body of a message */
956 enum havespec {
957 HAVE_NOTHING, /* nothing downloaded yet */
958 HAVE_HEADER = 01, /* header is downloaded */
959 HAVE_BODY = 02 /* entire message is downloaded */
962 /* flag bits. Attention: Flags that are used in cache.c may not change */
963 enum mflag {
964 MUSED = (1<< 0), /* entry is used, but this bit isn't */
965 MDELETED = (1<< 1), /* entry has been deleted */
966 MSAVED = (1<< 2), /* entry has been saved */
967 MTOUCH = (1<< 3), /* entry has been noticed */
968 MPRESERVE = (1<< 4), /* keep entry in sys mailbox */
969 MMARK = (1<< 5), /* message is marked! */
970 MODIFY = (1<< 6), /* message has been modified */
971 MNEW = (1<< 7), /* message has never been seen */
972 MREAD = (1<< 8), /* message has been read sometime. */
973 MSTATUS = (1<< 9), /* message status has changed */
974 MBOX = (1<<10), /* Send this to mbox, regardless */
975 MNOFROM = (1<<11), /* no From line */
976 MHIDDEN = (1<<12), /* message is hidden to user */
977 MFULLYCACHED = (1<<13), /* message is completely cached */
978 MBOXED = (1<<14), /* message has been sent to mbox */
979 MUNLINKED = (1<<15), /* message was unlinked from cache */
980 MNEWEST = (1<<16), /* message is very new (newmail) */
981 MFLAG = (1<<17), /* message has been flagged recently */
982 MUNFLAG = (1<<18), /* message has been unflagged */
983 MFLAGGED = (1<<19), /* message is `flagged' */
984 MANSWER = (1<<20), /* message has been answered recently */
985 MUNANSWER = (1<<21), /* message has been unanswered */
986 MANSWERED = (1<<22), /* message is `answered' */
987 MDRAFT = (1<<23), /* message has been drafted recently */
988 MUNDRAFT = (1<<24), /* message has been undrafted */
989 MDRAFTED = (1<<25), /* message is marked as `draft' */
990 MOLDMARK = (1<<26), /* messages was marked previously */
991 MSPAM = (1<<27) /* message is classified as spam */
993 #define MMNORM (MDELETED | MSAVED | MHIDDEN)
994 #define MMNDEL (MDELETED | MHIDDEN)
996 #define visible(mp) (((mp)->m_flag & MMNDEL) == 0)
998 struct mimepart {
999 enum mflag m_flag; /* flags */
1000 enum havespec m_have; /* downloaded parts of the part */
1001 #ifdef HAVE_SPAM
1002 ui32_t m_spamscore; /* Spam score as int, 24:8 bits */
1003 #endif
1004 int m_block; /* block number of this part */
1005 size_t m_offset; /* offset in block of part */
1006 size_t m_size; /* Bytes in the part */
1007 size_t m_xsize; /* Bytes in the full part */
1008 long m_lines; /* Lines in the message */
1009 long m_xlines; /* Lines in the full message */
1010 time_t m_time; /* time the message was sent */
1011 char const *m_from; /* message sender */
1012 struct mimepart *m_nextpart; /* next part at same level */
1013 struct mimepart *m_multipart; /* parts of multipart */
1014 struct mimepart *m_parent; /* enclosing multipart part */
1015 char *m_ct_type; /* content-type */
1016 char *m_ct_type_plain; /* content-type without specs */
1017 enum mimecontent m_mimecontent; /* same in enum */
1018 char const *m_charset; /* charset */
1019 char *m_ct_transfer_enc; /* content-transfer-encoding */
1020 enum mimeenc m_mimeenc; /* same in enum */
1021 char *m_partstring; /* part level string */
1022 char *m_filename; /* attachment filename */
1025 struct message {
1026 enum mflag m_flag; /* flags */
1027 enum havespec m_have; /* downloaded parts of the message */
1028 #ifdef HAVE_SPAM
1029 ui32_t m_spamscore; /* Spam score as int, 24:8 bits */
1030 #endif
1031 int m_block; /* block number of this message */
1032 size_t m_offset; /* offset in block of message */
1033 size_t m_size; /* Bytes in the message */
1034 size_t m_xsize; /* Bytes in the full message */
1035 long m_lines; /* Lines in the message */
1036 long m_xlines; /* Lines in the full message */
1037 time_t m_time; /* time the message was sent */
1038 time_t m_date; /* time in the 'Date' field */
1039 unsigned m_idhash; /* hash on Message-ID for threads */
1040 struct message *m_child; /* first child of this message */
1041 struct message *m_younger; /* younger brother of this message */
1042 struct message *m_elder; /* elder brother of this message */
1043 struct message *m_parent; /* parent of this message */
1044 unsigned m_level; /* thread level of message */
1045 long m_threadpos; /* position in threaded display */
1046 #ifdef HAVE_IMAP
1047 unsigned long m_uid; /* IMAP unique identifier */
1048 #endif
1049 char *m_maildir_file; /* original maildir file of msg */
1050 ui32_t m_maildir_hash; /* hash of file name in maildir sub */
1051 int m_collapsed; /* collapsed thread information */
1054 /* Given a file address, determine the block number it represents */
1055 #define mailx_blockof(off) ((int) ((off) / 4096))
1056 #define mailx_offsetof(off) ((int) ((off) % 4096))
1057 #define mailx_positionof(block, offset) ((off_t)(block) * 4096 + (offset))
1059 /* Argument types */
1060 enum argtype {
1061 ARG_MSGLIST = 0, /* Message list type */
1062 ARG_STRLIST = 1, /* A pure string */
1063 ARG_RAWLIST = 2, /* Shell string list */
1064 ARG_NOLIST = 3, /* Just plain 0 */
1065 ARG_NDMLIST = 4, /* Message list, no defaults */
1066 ARG_ECHOLIST = 5, /* Like raw list, but keep quote chars */
1067 ARG_ARGMASK = 7, /* Mask of the above */
1069 ARG_A = 1u<< 4, /* Needs an active mailbox */
1070 ARG_F = 1u<< 5, /* Is a conditional command */
1071 ARG_H = 1u<< 6, /* Never place in history */
1072 ARG_I = 1u<< 7, /* Interactive command bit */
1073 ARG_M = 1u<< 8, /* Legal from send mode bit */
1074 ARG_P = 1u<< 9, /* Autoprint dot after command */
1075 ARG_R = 1u<<10, /* Cannot be called from collect / recursion */
1076 ARG_T = 1u<<11, /* Is a transparent command */
1077 ARG_V = 1u<<12, /* Places data in temporary_arg_v_store */
1078 ARG_W = 1u<<13 /* Illegal when read only bit */
1081 enum gfield {
1082 GTO = 1, /* Grab To: line */
1083 GSUBJECT = 2, /* Likewise, Subject: line */
1084 GCC = 4, /* And the Cc: line */
1085 GBCC = 8, /* And also the Bcc: line */
1087 GNL = 16, /* Print blank line after */
1088 GDEL = 32, /* Entity removed from list */
1089 GCOMMA = 64, /* detract puts in commas */
1090 GUA = 128, /* User-Agent field */
1091 GMIME = 256, /* MIME 1.0 fields */
1092 GMSGID = 512, /* a Message-ID */
1093 /* 1024 */
1094 GIDENT = 2048, /* From:, Reply-To: and Organization: field */
1095 GREF = 4096, /* References: field */
1096 GDATE = 8192, /* Date: field */
1097 GFULL = 16384, /* include full names */
1098 GSKIN = 32768, /* skin names */
1099 GEXTRA = 65536, /* extra fields */
1100 GFILES = 131072 /* include filename addresses */
1102 #define GMASK (GTO | GSUBJECT | GCC | GBCC)
1104 /* Structure used to pass about the current state of a message (header) */
1105 struct header {
1106 struct name *h_to; /* Dynamic "To:" string */
1107 char *h_subject; /* Subject string */
1108 struct name *h_cc; /* Carbon copies string */
1109 struct name *h_bcc; /* Blind carbon copies */
1110 struct name *h_ref; /* References */
1111 struct attachment *h_attach; /* MIME attachments */
1112 char *h_charset; /* preferred charset */
1113 struct name *h_from; /* overridden "From:" field */
1114 struct name *h_replyto; /* overridden "Reply-To:" field */
1115 struct name *h_sender; /* overridden "Sender:" field */
1116 char *h_organization; /* overridden "Organization:" field */
1119 /* Handling of namelist nodes used in processing the recipients of mail and
1120 * aliases, inspection of mail-addresses and all that kind of stuff */
1121 enum nameflags {
1122 NAME_NAME_SALLOC = 1<< 0, /* .n_name is doped */
1123 NAME_FULLNAME_SALLOC = 1<< 1, /* .n_fullname is doped */
1124 NAME_SKINNED = 1<< 2, /* Is actually skin()ned */
1125 NAME_IDNA = 1<< 3, /* IDNA was applied */
1126 NAME_ADDRSPEC_CHECKED = 1<< 4, /* Address has been .. and */
1127 NAME_ADDRSPEC_ISFILE = 1<< 5, /* ..is a file path */
1128 NAME_ADDRSPEC_ISPIPE = 1<< 6, /* ..is a command for piping */
1129 NAME_ADDRSPEC_ISFILEORPIPE = NAME_ADDRSPEC_ISFILE | NAME_ADDRSPEC_ISPIPE,
1130 NAME_ADDRSPEC_ERR_EMPTY = 1<< 7, /* An empty string (or NULL) */
1131 NAME_ADDRSPEC_ERR_ATSEQ = 1<< 8, /* Weird @ sequence */
1132 NAME_ADDRSPEC_ERR_CHAR = 1<< 9, /* Invalid character */
1133 NAME_ADDRSPEC_ERR_IDNA = 1<<10, /* IDNA convertion failed */
1134 NAME_ADDRSPEC_INVALID = NAME_ADDRSPEC_ERR_EMPTY |
1135 NAME_ADDRSPEC_ERR_ATSEQ | NAME_ADDRSPEC_ERR_CHAR |
1136 NAME_ADDRSPEC_ERR_IDNA,
1138 _NAME_SHIFTWC = 11,
1139 _NAME_MAXWC = 0xFFFFF,
1140 _NAME_MASKWC = _NAME_MAXWC << _NAME_SHIFTWC
1143 /* In the !_ERR_EMPTY case, the failing character can be queried */
1144 #define NAME_ADDRSPEC_ERR_GETWC(F) \
1145 ((((unsigned int)(F) & _NAME_MASKWC) >> _NAME_SHIFTWC) & _NAME_MAXWC)
1146 #define NAME_ADDRSPEC_ERR_SET(F, E, WC) \
1147 do {\
1148 (F) = ((F) & ~(NAME_ADDRSPEC_INVALID | _NAME_MASKWC)) |\
1149 (E) | (((unsigned int)(WC) & _NAME_MAXWC) << _NAME_SHIFTWC);\
1150 } while (0)
1152 struct name {
1153 struct name *n_flink; /* Forward link in list. */
1154 struct name *n_blink; /* Backward list link */
1155 enum gfield n_type; /* From which list it came */
1156 enum nameflags n_flags; /* enum nameflags */
1157 char *n_name; /* This fella's name */
1158 char *n_fullname; /* Sometimes, name including comment */
1161 struct addrguts {
1162 char const *ag_input; /* Input string as given */
1163 size_t ag_ilen; /* strlen() of input */
1164 size_t ag_iaddr_start; /* Start of *addr-spec* in .ag_input */
1165 size_t ag_iaddr_aend; /* ..and one past its end */
1166 char *ag_skinned; /* Output (alloced if !=.ag_input) */
1167 size_t ag_slen; /* strlen() of .ag_skinned */
1168 size_t ag_sdom_start; /* Start of domain in .ag_skinned, */
1169 enum nameflags ag_n_flags; /* enum nameflags of .ag_skinned */
1172 /* MIME attachments */
1173 enum attach_conv {
1174 AC_DEFAULT, /* _get_lc() -> _iter_*() */
1175 AC_FIX_OUTCS, /* _get_lc() -> "charset=" .a_charset */
1176 AC_FIX_INCS, /* "charset=".a_input_charset (nocnv) */
1177 AC_TMPFILE /* attachment.a_tmpf is converted */
1180 struct attachment {
1181 struct attachment *a_flink; /* Forward link in list. */
1182 struct attachment *a_blink; /* Backward list link */
1183 char const *a_name; /* file name */
1184 char const *a_content_type; /* content type */
1185 char const *a_content_disposition; /* content disposition */
1186 char const *a_content_id; /* content id */
1187 char const *a_content_description; /* content description */
1188 char const *a_input_charset; /* Interpretation depends on .a_conv */
1189 char const *a_charset; /* ... */
1190 FILE *a_tmpf; /* If AC_TMPFILE */
1191 enum attach_conv a_conv; /* User chosen conversion */
1192 int a_msgno; /* message number */
1195 struct group {
1196 struct group *ge_link; /* Next person in this group */
1197 char *ge_name; /* This person's user name */
1200 struct grouphead {
1201 struct grouphead *g_link; /* Next grouphead in list */
1202 char *g_name; /* Name of this group */
1203 struct group *g_list; /* Users in group. */
1206 /* Structure of the hash table of ignored header fields */
1207 struct ignoretab {
1208 int i_count; /* Number of entries */
1209 struct ignore {
1210 struct ignore *i_link; /* Next ignored field in bucket */
1211 char *i_field; /* This ignored field */
1212 } *i_head[HSHSIZE];
1215 /* Token values returned by the scanner used for argument lists.
1216 * Also, sizes of scanner-related things */
1217 enum ltoken {
1218 TEOL = 0, /* End of the command line */
1219 TNUMBER = 1, /* A message number */
1220 TDASH = 2, /* A simple dash */
1221 TSTRING = 3, /* A string (possibly containing -) */
1222 TDOT = 4, /* A "." */
1223 TUP = 5, /* An "^" */
1224 TDOLLAR = 6, /* A "$" */
1225 TSTAR = 7, /* A "*" */
1226 TOPEN = 8, /* An '(' */
1227 TCLOSE = 9, /* A ')' */
1228 TPLUS = 10, /* A '+' */
1229 TERROR = 11, /* A lexical error */
1230 TCOMMA = 12, /* A ',' */
1231 TSEMI = 13, /* A ';' */
1232 TBACK = 14 /* A '`' */
1235 #define REGDEP 2 /* Maximum regret depth. */
1237 /* For the 'shortcut' and 'unshortcut' functionality */
1238 struct shortcut {
1239 struct shortcut *sh_next; /* next shortcut in list */
1240 char *sh_short; /* shortcut string */
1241 char *sh_long; /* expanded form */
1244 /* Kludges to handle the change from setexit / reset to setjmp / longjmp */
1245 #define setexit() (void)sigsetjmp(srbuf, 1)
1246 #define reset(x) siglongjmp(srbuf, x)
1248 /* Content-Transfer-Encodings as defined in RFC 2045:
1249 * - Quoted-Printable, section 6.7
1250 * - Base64, section 6.8 */
1252 #define QP_LINESIZE (4 * 19) /* Max. compliant QP linesize */
1254 #define B64_LINESIZE (4 * 19) /* Max. compliant Base64 linesize */
1255 #define B64_ENCODE_INPUT_PER_LINE 57 /* Max. input for Base64 encode/line */
1257 /* xxx QP came later, maybe rewrite all to use mimecte_flags directly? */
1258 enum __mimecte_flags {
1259 MIMECTE_NONE,
1260 MIMECTE_SALLOC = 1<<0, /* Use salloc(), not srealloc().. */
1261 /* ..result .s,.l point to user buffer of *_LINESIZE+[+[+]] bytes instead */
1262 MIMECTE_BUF = 1<<1,
1263 MIMECTE_CRLF = 1<<2, /* (encode) Append "\r\n" to lines */
1264 MIMECTE_LF = 1<<3, /* (encode) Append "\n" to lines */
1265 /* (encode) If one of _CRLF/_LF is set, honour *_LINESIZE+[+[+]] and
1266 * inject the desired line-ending whenever a linewrap is desired */
1267 MIMECTE_MULTILINE = 1<<4,
1268 /* (encode) Quote with header rules, do not generate soft NL breaks? */
1269 MIMECTE_ISHEAD = 1<<5
1272 enum qpflags {
1273 QP_NONE = MIMECTE_NONE,
1274 QP_SALLOC = MIMECTE_SALLOC,
1275 QP_BUF = MIMECTE_BUF,
1276 QP_ISHEAD = MIMECTE_ISHEAD
1279 enum b64flags {
1280 B64_NONE = MIMECTE_NONE,
1281 B64_SALLOC = MIMECTE_SALLOC,
1282 B64_BUF = MIMECTE_BUF,
1283 B64_CRLF = MIMECTE_CRLF,
1284 B64_LF = MIMECTE_LF,
1285 B64_MULTILINE = MIMECTE_MULTILINE
1288 /* Locale-independent character classes */
1289 enum {
1290 C_CNTRL = 0000,
1291 C_BLANK = 0001,
1292 C_WHITE = 0002,
1293 C_SPACE = 0004,
1294 C_PUNCT = 0010,
1295 C_OCTAL = 0020,
1296 C_DIGIT = 0040,
1297 C_UPPER = 0100,
1298 C_LOWER = 0200
1301 #define __ischarof(C, FLAGS) \
1302 (asciichar(C) && (class_char[(uc_it)(C)] & (FLAGS)) != 0)
1304 #define asciichar(c) ((uc_it)(c) <= 0177)
1305 #define alnumchar(c) __ischarof(c, C_DIGIT | C_OCTAL | C_UPPER | C_LOWER)
1306 #define alphachar(c) __ischarof(c, C_UPPER | C_LOWER)
1307 #define blankchar(c) __ischarof(c, C_BLANK)
1308 #define blankspacechar(c) __ischarof(c, C_BLANK | C_SPACE)
1309 #define cntrlchar(c) __ischarof(c, C_CNTRL)
1310 #define digitchar(c) __ischarof(c, C_DIGIT | C_OCTAL)
1311 #define lowerchar(c) __ischarof(c, C_LOWER)
1312 #define punctchar(c) __ischarof(c, C_PUNCT)
1313 #define spacechar(c) __ischarof(c, C_BLANK | C_SPACE | C_WHITE)
1314 #define upperchar(c) __ischarof(c, C_UPPER)
1315 #define whitechar(c) __ischarof(c, C_BLANK | C_WHITE)
1316 #define octalchar(c) __ischarof(c, C_OCTAL)
1318 #define upperconv(c) (lowerchar(c) ? (char)((uc_it)(c) - 'a' + 'A') : (c))
1319 #define lowerconv(c) (upperchar(c) ? (char)((uc_it)(c) - 'A' + 'a') : (c))
1320 /* RFC 822, 3.2. */
1321 #define fieldnamechar(c) \
1322 (asciichar(c) && (c) > 040 && (c) != 0177 && (c) != ':')
1324 /* Try to use alloca() for some function-local buffers and data, fall back to
1325 * smalloc()/free() if not available */
1326 #ifdef HAVE_ALLOCA
1327 # define ac_alloc(n) HAVE_ALLOCA(n)
1328 # define ac_free(n) do {UNUSED(n);} while (0)
1329 #else
1330 # define ac_alloc(n) smalloc(n)
1331 # define ac_free(n) free(n)
1332 #endif
1334 /* Single-threaded, use unlocked I/O */
1335 #ifdef HAVE_PUTC_UNLOCKED
1336 # undef getc
1337 # define getc(c) getc_unlocked(c)
1338 # undef putc
1339 # define putc(c, f) putc_unlocked(c, f)
1340 # undef putchar
1341 # define putchar(c) putc_unlocked((c), stdout)
1342 #endif
1344 /* Truncate a file to the last character written. This is useful just before
1345 * closing an old file that was opened for read/write */
1346 #define ftrunc(stream) \
1347 do {\
1348 off_t off;\
1349 fflush(stream);\
1350 off = ftell(stream);\
1351 if (off >= 0)\
1352 ftruncate(fileno(stream), off);\
1353 } while (0)
1355 /* fflush() and rewind() */
1356 #define fflush_rewind(stream) \
1357 do {\
1358 fflush(stream);\
1359 rewind(stream);\
1360 } while (0)
1362 /* There are problems with dup()ing of file-descriptors for child processes.
1363 * As long as those are not fixed in equal spirit to (outof(): FIX and
1364 * recode.., 2012-10-04), and to avoid reviving of bugs like (If *record* is
1365 * set, avoid writing dead content twice.., 2012-09-14), we have to somehow
1366 * accomplish that the FILE* fp makes itself comfortable with the *real* offset
1367 * of the underlaying file descriptor. Unfortunately Standard I/O and POSIX
1368 * don't describe a way for that -- fflush();rewind(); won't do it. This
1369 * fseek(END),rewind() pair works around the problem on *BSD and Linux.
1370 * Update as of 2014-03-03: with Issue 7 POSIX has overloaded fflush(3): if
1371 * used on a readable stream, then
1373 * if the file is not already at EOF, and the file is one capable of
1374 * seeking, the file offset of the underlying open file description shall
1375 * be set to the file position of the stream.
1377 * We need our own, simplified and reliable I/O */
1378 #if defined _POSIX_VERSION && _POSIX_VERSION + 0 >= 200809L
1379 # define really_rewind(stream) \
1380 do {\
1381 rewind(stream);\
1382 fflush(stream);\
1383 } while (0)
1384 #else
1385 # define really_rewind(stream) \
1386 do {\
1387 fseek(stream, 0, SEEK_END);\
1388 rewind(stream);\
1389 } while (0)
1390 #endif
1392 /* For saving the current directory and later returning */
1393 struct cw {
1394 #ifdef HAVE_FCHDIR
1395 int cw_fd;
1396 #else
1397 char cw_wd[PATH_MAX];
1398 #endif
1402 * Global variable declarations
1404 * These become instantiated in main.c.
1407 #undef VL
1408 #ifdef _MAIN_SOURCE
1409 # ifndef HAVE_AMALGAMATION
1410 # define VL
1411 # else
1412 # define VL static
1413 # endif
1414 #else
1415 # define VL extern
1416 #endif
1418 VL gid_t effectivegid; /* Saved from when we started up */
1419 VL gid_t realgid; /* Saved from when we started up */
1421 VL int mb_cur_max; /* Value of MB_CUR_MAX */
1422 VL int realscreenheight; /* The real screen height */
1423 VL int scrnwidth; /* Screen width, or best guess */
1424 VL int scrnheight; /* Screen height/guess (4 header) */
1425 VL int utf8; /* Locale uses UTF-8 encoding */
1426 VL int enc_has_state; /* Encoding has shift states */
1428 VL char **altnames; /* List of alternate names of user */
1429 VL char const *homedir; /* Path name of home directory */
1430 VL char const *myname; /* My login name */
1431 VL char const *progname; /* Our name */
1432 VL char const *tempdir; /* The temporary directory */
1434 VL int exit_status; /* Exit status */
1435 VL int options; /* Bits of enum user_options */
1436 VL char *option_r_arg; /* Argument to -r option */
1437 VL char const **smopts; /* sendmail(1) opts from commline */
1438 VL size_t smopts_count; /* Entries in smopts */
1440 /* TODO Join as many of these state machine bits into a single carrier! */
1441 VL int inhook; /* Currently executing a hook */
1442 VL bool_t exec_last_comm_error; /* Last execute() command failed */
1443 VL bool_t edit; /* Indicates editing a file */
1444 VL bool_t did_print_dot; /* Current message has been printed */
1445 VL bool_t list_saw_numbers; /* Last *LIST saw numerics */
1446 VL bool_t msglist_is_single; /* Last NDMLIST/MSGLIST chose 1 msg */
1447 VL bool_t loading; /* Loading user definitions */
1448 VL bool_t sourcing; /* Currently reading variant file */
1449 VL bool_t sawcom; /* Set after first command */
1450 VL bool_t starting; /* Still in startup code */
1451 VL bool_t var_clear_allow_undefined; /* v?ok_[bv]clear(): no complain */
1452 VL int noreset; /* String resets suspended */
1454 /* XXX stylish sorting */
1455 VL int msgCount; /* Count of messages read in */
1456 VL struct mailbox mb; /* Current mailbox */
1457 VL int image; /* File descriptor for msg image */
1458 VL char mailname[PATH_MAX]; /* Name of current file */
1459 VL char displayname[80 - 40]; /* Prettyfied for display */
1460 VL char prevfile[PATH_MAX]; /* Name of previous file */
1461 VL char const *account_name; /* Current account name or NULL */
1462 VL off_t mailsize; /* Size of system mailbox */
1463 VL struct message *dot; /* Pointer to current message */
1464 VL struct message *prevdot; /* Previous current message */
1465 VL struct message *message; /* The actual message structure */
1466 VL struct message *threadroot; /* first threaded message */
1467 VL int imap_created_mailbox; /* hack to get feedback from imap */
1469 VL struct grouphead *groups[HSHSIZE]; /* Pointer to active groups */
1470 VL struct ignoretab ignore[2]; /* ignored and retained fields
1471 * 0 is ignore, 1 is retain */
1472 VL struct ignoretab saveignore[2]; /* ignored and retained fields
1473 * on save to folder */
1474 VL struct ignoretab allignore[2]; /* special, ignore all headers */
1475 VL struct ignoretab fwdignore[2]; /* fields to ignore for forwarding */
1476 VL struct shortcut *shortcuts; /* list of shortcuts */
1478 VL struct time_current time_current; /* time(3); send: mail1() XXXcarrier */
1479 VL struct termios_state termios_state; /* getpassword(); see commands().. */
1481 #ifdef HAVE_COLOUR
1482 VL struct colour_table *colour_table;
1483 #endif
1485 #ifdef HAVE_SSL
1486 VL enum ssl_verify_level ssl_verify_level; /* SSL verification level */
1487 #endif
1489 #ifdef HAVE_ICONV
1490 VL iconv_t iconvd;
1491 #endif
1493 #ifdef HAVE_CATGETS
1494 VL nl_catd catd;
1495 #endif
1497 VL sigjmp_buf srbuf;
1498 VL int interrupts;
1499 VL sighandler_type dflpipe;
1500 VL sighandler_type handlerstacktop;
1501 #define handlerpush(f) (savedtop = handlerstacktop, handlerstacktop = (f))
1502 #define handlerpop() (handlerstacktop = savedtop)
1504 /* TODO Temporary hacks unless the codebase doesn't jump and uses pass-by-value
1505 * TODO carrier structs instead of locals */
1506 VL char *temporary_arg_v_store;
1507 VL void *temporary_localopts_store;
1509 /* The remaining variables need initialization */
1511 #ifndef HAVE_AMALGAMATION
1512 VL char const month_names[12 + 1][4];
1513 VL char const weekday_names[7 + 1][4];
1515 VL char const uagent[]; /* User agent */
1516 VL char const version[]; /* The version string */
1517 VL char const features[]; /* The "feature string" */
1519 VL uc_it const class_char[];
1520 #endif
1523 * Finally, let's include the function prototypes XXX embed
1526 #include "extern.h"
1528 /* vim:set fenc=utf-8:s-it-mode */