1 /* mh.h -- main header file for all of nmh
6 /* It's undefined behaviour in C99 to convert from a function pointer to
7 * a data-object pointer, e.g. void pointer. gcc's -pedantic warns of
8 * this and can stop compilation. POSIX requires the operation however,
9 * e.g. for dlsym(3), and so we know it's safe on POSIX platforms, e.g.
10 * the pointers are of the same size. Thus use a union to subvert gcc's
11 * check. The function-pointer equivalent of a void pointer is any
12 * function-pointer type as all function pointers are defined to be
13 * convertible from one another; use the simplest available. */
22 #define NOTOK (-1) /* syscall()s return this on error */
23 #define OK 0 /* ditto on success */
24 #define DONE 1 /* ternary logic */
27 #define MAXARGS 1000 /* max arguments to exec */
28 #define NFOLDERS 1000 /* max folder arguments on command line */
29 #define DMAXFOLDER 4 /* typical number of digits */
30 #define MAXFOLDER 1000 /* message increment */
33 * This macro is for use by scan, for example, so that platforms with
34 * a small BUFSIZ can easily allocate larger buffers.
36 #define NMH_BUFSIZ max(BUFSIZ, 8192)
38 /* If we're using gcc then tell it extra information so it can do more
39 * compile-time checks. */
41 #define NORETURN __attribute__((__noreturn__))
42 #define CONST __attribute__((const))
43 #define MALLOC __attribute__((malloc))
44 #define NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
45 #define PURE __attribute__((pure))
46 #define ENDNULL __attribute__((sentinel))
49 #define CHECK_PRINTF(fmt, arg)
50 #define ALLOC_SIZE(...)
58 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
59 #define ALLOC_SIZE(...) __attribute__((alloc_size(__VA_ARGS__)))
60 #define CHECK_PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg)))
62 #define ALLOC_SIZE(...)
63 #define CHECK_PRINTF(fmt, arg)
66 /* Silence the compiler's "unused variable" warning. */
67 #define NMH_UNUSED(i) (void)i
69 /* DIM gives the number of elements in the one-dimensional array a. */
70 #define DIM(a) (sizeof (a) / sizeof (*(a)))
72 /* LEN gives the strlen() of string constant s, excluding the
74 #define LEN(s) (sizeof (s) - 1)
76 /* FENDNULL fends off NULL by giving an empty string instead. */
77 #define FENDNULL(s) ((s) ? (s) : "")
79 /* If not specified in a file and PAGER is NULL or empty. */
80 #define DEFAULT_PAGER "more"
83 * char array that keeps track of size in both bytes and characters
85 * Don't store return value of charstring_buffer() and use later
86 * after intervening push_back's; use charstring_buffer_copy()
89 typedef struct charstring
*charstring_t
;
91 charstring_t
charstring_create (size_t);
92 charstring_t
charstring_copy (const charstring_t
) NONNULL(1);
93 void charstring_free (charstring_t
);
94 /* Append a single-byte character: */
95 void charstring_push_back (charstring_t
, const char) NONNULL(1);
96 /* Append possibly multi-byte character(s): */
97 void charstring_push_back_chars (charstring_t
, const char [], size_t, size_t) NONNULL(1);
98 void charstring_append (charstring_t
, const charstring_t
) NONNULL(2);
99 void charstring_append_cstring (charstring_t
, const char []) NONNULL(2);
100 void charstring_clear (charstring_t
) NONNULL(1);
101 /* Don't store return value of charstring_buffer() and use later after
102 intervening push_back's; use charstring_buffer_copy() instead. */
103 const char *charstring_buffer (const charstring_t
) NONNULL(1);
104 /* User is responsible for free'ing result of buffer copy. */
105 char *charstring_buffer_copy (const charstring_t
) NONNULL(1);
106 size_t charstring_bytes (const charstring_t
) NONNULL(1) PURE
;
107 size_t charstring_chars (const charstring_t
) NONNULL(1) PURE
;
108 /* Length of the last character in the charstring. */
109 int charstring_last_char_len (const charstring_t
) NONNULL(1);
112 * user context/profile structure
115 char *n_name
; /* key */
116 char *n_field
; /* value */
117 char n_context
; /* context, not profile */
118 struct node
*n_next
; /* next entry */
124 #define AMBIGSW (-2) /* from smatch() on ambiguous switch */
125 #define UNKWNSW (-1) /* from smatch() on unknown switch */
136 * The previous comments here about minchars was incorrect; this is
137 * (AFAIK) the correct information.
139 * A minchars of "0" means this switch can be abbreviated to any number
140 * of characters (assuming the abbreviation does not match any other
143 * A positive value for minchars means that when the user specifies
144 * the switch on the command line, it MUST be at least that many
147 * A negative value for minchars means that the user-given switch must
148 * be that many characters, but will NOT be shown in -help output.
150 * So what should I use? Well, for nearly all switches you want to specify
151 * a minchars of 0. smatch will report an error if the switch given
152 * matches more than one entry. Let's say you have the following
153 * two switches: -append and -apply. -app will return AMBIGSW from
154 * smatch. -appe and -appl will work fine. So 0 is the correct choice
157 * The only time you want to specify a minimum length is if you have
158 * a switch who's name is a substring of a longer switch. The example
159 * you see sometimes in the code is -form and -format. If you gave a
160 * minchars of 0 for both, -form would match both -form AND -format,
161 * and you'd always get AMBIGSW. The solution is to specify a minchars
162 * of 5 for -format; that way just -form will just match -form. When
163 * a minchars is given, the -help output will specify the minimum
164 * switch length, like this:
168 * A negative value works the same way, except the switch isn't printed
169 * in -help. Why would you do that? Well, there are a few instances
170 * of internal switches and some switches which only appear if a particular
171 * feature is enabled (such as SASL or TLS). Lately I've been of the
172 * opinion that all switches should be specified, even if they are
173 * internal or use non-available features, but currently the smatch
174 * code still supports this.
176 * This isn't the appropriate place to make this note, but since I was
177 * here ... when creating switches, you should make a negation switch
178 * right after the enabling switch. E.g. you should have:
180 * X("sasl", 0, SASLSW) \
181 * X("nosasl", 0, NOSASLSW) \
183 * in the switch array, because when you run -help, print_sw will detect
192 * If we pick this switch, return this value from smatch
199 * Macros to use when declaring struct swit arrays.
201 * These macros use a technique known as X-Macros. In your source code you
202 * use them like this:
204 * #define FOO_SWITCHES \
205 * X("switch1", 0, SWITCHSW) \
206 * X("switch2", 0, SWITCH2SW) \
207 * X("thirdswitch", 2, SWITCH3SW) \
209 * The argument to each entry in FOO_SWITCHES are the switch name (sw),
210 * the minchars field (see above) and the return value for this switch.
211 * Note that the last entry in the above definition must either omit the
212 * continuation backslash, or be followed by a blank line. In the nmh
213 * code the style is to have every line include a backslash and follow
214 * the SWITCHES macro definition by a blank line.
216 * After you define FOO_SWITCHES, you instantiate it as follows:
218 * #define X(sw, minchars, id) id,
219 * DEFINE_SWITCH_ENUM(FOO);
222 * #define X(sw, minchars, id) { sw, minchars, id },
223 * DEFINE_SWITCH_ARRAY(FOO);
226 * DEFINE_SWITCH_ENUM defines an extra enum at the end of the list called
230 #define DEFINE_SWITCH_ENUM(name) \
236 #define DEFINE_SWITCH_ARRAY(name, array) \
237 static struct swit array[] = { \
242 extern struct swit anoyes
[]; /* standard yes/no switches */
245 * general folder attributes
247 #define READONLY (1<<0) /* No write access to folder */
248 #define SEQMOD (1<<1) /* folder's sequences modified */
249 #define ALLOW_NEW (1<<2) /* allow the "new" sequence */
250 #define OTHERS (1<<3) /* folder has other files */
252 #define FBITS "\020\01READONLY\02SEQMOD\03ALLOW_NEW\04OTHERS"
255 * first free slot for user defined sequences
261 * internal messages attributes (sequences)
263 #define EXISTS (0) /* exists */
264 #define SELECTED (1) /* selected for use */
265 #define SELECT_EMPTY (2) /* "new" message */
266 #define SELECT_UNSEEN (3) /* inc/show "unseen" */
268 #define MBITS "\020\01EXISTS\02SELECTED\03NEW\04UNSEEN"
270 /* A vector of bits for tracking the sequence membership of a single
271 * message. Do not access the struct members; use vector.c.
272 * Do not move or copy this struct as it may contain a pointer to
273 * itself; use bvector_copy(). */
277 unsigned long tiny
[2]; /* Default fixed-size storage for bits. */
279 typedef struct bvector
*bvector_t
;
281 bvector_t
bvector_create (void);
282 void bvector_init(struct bvector
*bv
) NONNULL(1);
283 void bvector_copy (bvector_t
, bvector_t
) NONNULL(1, 2);
284 void bvector_free (bvector_t
) NONNULL(1);
285 void bvector_fini(struct bvector
*bv
) NONNULL(1);
286 void bvector_clear (bvector_t
, size_t) NONNULL(1);
287 void bvector_clear_all (bvector_t
) NONNULL(1);
288 void bvector_set (bvector_t
, size_t) NONNULL(1);
289 unsigned int bvector_at (bvector_t
, size_t) NONNULL(1) PURE
;
290 unsigned long bvector_first_bits (bvector_t
) NONNULL(1) PURE
;
292 typedef struct svector
*svector_t
;
294 svector_t
svector_create (size_t);
295 void svector_free (svector_t
) NONNULL(1);
296 char *svector_push_back (svector_t
, char *) NONNULL(1);
297 char *svector_at (svector_t
, size_t) NONNULL(1);
298 char **svector_find(svector_t
, const char *) NONNULL(1) PURE
;
299 char **svector_strs (svector_t
) NONNULL(1) PURE
;
300 size_t svector_size (svector_t
) NONNULL(1) PURE
;
302 typedef struct ivector
*ivector_t
;
304 ivector_t
ivector_create (size_t);
305 void ivector_free (ivector_t
) NONNULL(1);
306 int ivector_push_back (ivector_t
, int) NONNULL(1);
307 int ivector_at (ivector_t
, size_t) NONNULL(1);
308 int *ivector_atp (ivector_t
, size_t) NONNULL(1);
311 * Primary structure of folder/message information
314 int lowmsg
; /* Lowest msg number */
315 int hghmsg
; /* Highest msg number */
316 int nummsg
; /* Actual Number of msgs */
318 int lowsel
; /* Lowest selected msg number */
319 int hghsel
; /* Highest selected msg number */
320 int numsel
; /* Number of msgs selected */
322 int curmsg
; /* Number of current msg if any */
324 int msgflags
; /* Folder attributes (READONLY, etc) */
325 char *foldpath
; /* Pathname of folder */
328 * Name of sequences in this folder.
333 * bit flags for whether sequence
334 * is public (0), or private (1)
339 * These represent the lowest and highest possible
340 * message numbers we can put in the message status
341 * area, without calling folder_realloc().
347 * This is an array of bvector_t which we allocate dynamically.
348 * Each bvector_t is a set of bits flags for a particular message.
349 * These bit flags represent general attributes such as
350 * EXISTS, SELECTED, etc. as well as track if message is
351 * in a particular sequence.
354 struct bvector
*msgstats
; /* msg status */
357 * A FILE handle containing an open filehandle for the sequence file
358 * for this folder. If non-NULL, use it when the sequence file is
364 * The name of the public sequence file; required by lkfclose()
370 * Amount of space to allocate for msgstats. Allocate
371 * the array to have space for messages numbered lo to hi.
372 * Use MSGSTATNUM to load mp->num_msgstats first.
374 #define MSGSTATNUM(lo, hi) ((size_t) ((hi) - (lo) + 1))
375 #define MSGSTATSIZE(mp) ((mp)->num_msgstats * sizeof *(mp)->msgstats)
378 * macros for message and sequence manipulation
380 #define msgstat(mp,n) ((mp)->msgstats + (n) - mp->lowoff)
381 #define clear_msg_flags(mp,msgnum) bvector_clear_all (msgstat(mp, msgnum))
382 #define copy_msg_flags(mp,i,j) bvector_copy (msgstat(mp,i), msgstat(mp,j))
383 #define get_msg_flags(mp,ptr,msgnum) bvector_copy (ptr, msgstat(mp, msgnum))
384 #define set_msg_flags(mp,ptr,msgnum) bvector_copy (msgstat(mp, msgnum), ptr)
386 #define does_exist(mp,msgnum) bvector_at (msgstat(mp, msgnum), EXISTS)
387 #define unset_exists(mp,msgnum) bvector_clear (msgstat(mp, msgnum), EXISTS)
388 #define set_exists(mp,msgnum) bvector_set (msgstat(mp, msgnum), EXISTS)
390 #define is_selected(mp,msgnum) bvector_at (msgstat(mp, msgnum), SELECTED)
391 #define unset_selected(mp,msgnum) bvector_clear (msgstat(mp, msgnum), SELECTED)
392 #define set_selected(mp,msgnum) bvector_set (msgstat(mp, msgnum), SELECTED)
394 #define is_select_empty(mp,msgnum) \
395 bvector_at (msgstat(mp, msgnum), SELECT_EMPTY)
396 #define set_select_empty(mp,msgnum) \
397 bvector_set (msgstat(mp, msgnum), SELECT_EMPTY)
399 #define is_unseen(mp,msgnum) \
400 bvector_at (msgstat(mp, msgnum), SELECT_UNSEEN)
401 #define unset_unseen(mp,msgnum) \
402 bvector_clear (msgstat(mp, msgnum), SELECT_UNSEEN)
403 #define set_unseen(mp,msgnum) \
404 bvector_set (msgstat(mp, msgnum), SELECT_UNSEEN)
406 #define in_sequence(mp,seqnum,msgnum) \
407 bvector_at (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
408 #define clear_sequence(mp,seqnum,msgnum) \
409 bvector_clear (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
410 #define add_sequence(mp,seqnum,msgnum) \
411 bvector_set (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
413 #define is_seq_private(mp,seqnum) \
414 bvector_at (mp->attrstats, FFATTRSLOT + seqnum)
415 #define make_seq_public(mp,seqnum) \
416 bvector_clear (mp->attrstats, FFATTRSLOT + seqnum)
417 #define make_seq_private(mp,seqnum) \
418 bvector_set (mp->attrstats, FFATTRSLOT + seqnum)
419 #define make_all_public(mp) \
420 mp->attrstats = bvector_create(); bvector_clear_all (mp->attrstats)
423 * macros for folder attributes
425 #define clear_folder_flags(mp) ((mp)->msgflags = 0)
427 #define is_readonly(mp) ((mp)->msgflags & READONLY)
428 #define set_readonly(mp) ((mp)->msgflags |= READONLY)
430 #define other_files(mp) ((mp)->msgflags & OTHERS)
431 #define set_other_files(mp) ((mp)->msgflags |= OTHERS)
434 * m_getfld() message parsing
437 #define NAMESZ 999 /* Limit on component name size.
438 RFC 2822 limits line lengths to
439 998 characters, so a header name
440 can be at most that long.
441 m_getfld limits header names to 2
442 less than NAMESZ, which is fine,
443 because header names must be
444 followed by a colon. Add one for
447 /* Token type or error returned from m_getfld(), and its internal state
448 * for the next call. */
449 /* FLD detects the header's name is too long to fit in the fixed size
452 /* FLD reaches EOF after the header's name, or the name is followed by
453 * a linefeed rather than a colon and the body buffer isn't large enough
454 * to pretend this header line starts the body. */
456 /* The initial state, looking for headers. Returned when the header's
459 /* Another chunk of the header's value has been returned, but there's
462 /* A chunk of the email's body has been returned. */
464 /* Either the end of the input file has been reached, or the delimiter
465 * between emails has been found and the caller should
466 * m_getfld_state_reset() to reset the state to FLD for continuing
467 * through the file. */
470 typedef struct m_getfld_state
*m_getfld_state_t
;
472 #define NOUSE 0 /* draft being re-used */
474 #define TFOLDER 0 /* path() given a +folder */
475 #define TFILE 1 /* path() given a file */
476 #define TSUBCWF 2 /* path() given a @folder */
478 #define OUTPUTLINELEN 72 /* default line length for headers */
480 #define LINK "@" /* Name of link to file to which you are */
484 * credentials management
486 typedef struct nmh_creds
*nmh_creds_t
;
489 * miscellaneous macros
491 #define pidXwait(pid,cp) pidstatus (pidwait (pid, NOTOK), stdout, cp)
494 # define max(a,b) ((a) > (b) ? (a) : (b))
498 # define min(a,b) ((a) < (b) ? (a) : (b))
502 # define abs(a) ((a) > 0 ? (a) : -(a))
508 #define CTXMOD 0x01 /* context information modified */
509 #define DBITS "\020\01CTXMOD"
510 extern char ctxflags
;
512 extern char *invo_name
; /* command invocation name */
513 extern char *mypath
; /* user's $HOME */
514 extern char *defpath
; /* pathname of user's profile */
515 extern char *ctxpath
; /* pathname of user's context */
516 extern struct node
*m_defs
; /* list of profile/context entries */
519 * These standard strings are defined in config.c. They are the
520 * only system-dependent parameters in nmh, and thus by redefining
521 * their values and reloading the various modules, nmh will run
524 extern char *buildmimeproc
;
525 extern char *catproc
;
526 extern char *components
;
527 extern char *context
;
528 extern char *current
;
529 extern char *credentials_file
;
530 extern int credentials_no_perm_check
;
531 extern char *defaultfolder
;
532 extern char *digestcomps
;
533 extern char *distcomps
;
535 extern char *fileproc
;
536 extern char *foldprot
;
537 extern char *formatproc
;
538 extern char *forwcomps
;
540 extern char *incproc
;
542 extern char *mailproc
;
543 extern char *mh_defaults
;
544 extern char *mh_profile
;
546 extern char *mhlformat
;
547 extern char *mhlforward
;
548 extern char *mhlproc
;
549 extern char *mhlreply
;
550 extern char *moreproc
;
551 extern char *msgprot
;
552 extern char *nmhaccessftp
;
553 extern char *nmhaccessurl
;
554 extern char *nmhstorage
;
555 extern char *nmhcache
;
556 extern char *nmhprivcache
;
557 extern char *nsequence
;
558 extern char *packproc
;
559 extern char *postproc
;
560 extern char *pfolder
;
561 extern char *psequence
;
562 extern char *rcvdistcomps
;
563 extern char *rcvstoreproc
;
564 extern char *replcomps
;
565 extern char *replgroupcomps
;
566 extern char *rmmproc
;
567 extern char *sendproc
;
568 extern char *showmimeproc
;
569 extern char *showproc
;
570 extern char *usequence
;
571 extern char *user_agent
;
572 extern char *version_num
;
573 extern char *version_str
;
574 extern char *whatnowproc
;
575 extern char *whomproc
;
577 #include <h/prototypes.h>