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 #include "sbr/vector.h"
273 * Primary structure of folder/message information
276 int lowmsg
; /* Lowest msg number */
277 int hghmsg
; /* Highest msg number */
278 int nummsg
; /* Actual Number of msgs */
280 int lowsel
; /* Lowest selected msg number */
281 int hghsel
; /* Highest selected msg number */
282 int numsel
; /* Number of msgs selected */
284 int curmsg
; /* Number of current msg if any */
286 int msgflags
; /* Folder attributes (READONLY, etc) */
287 char *foldpath
; /* Pathname of folder */
290 * Name of sequences in this folder.
295 * bit flags for whether sequence
296 * is public (0), or private (1)
301 * These represent the lowest and highest possible
302 * message numbers we can put in the message status
303 * area, without calling folder_realloc().
309 * This is an array of bvector_t which we allocate dynamically.
310 * Each bvector_t is a set of bits flags for a particular message.
311 * These bit flags represent general attributes such as
312 * EXISTS, SELECTED, etc. as well as track if message is
313 * in a particular sequence.
316 struct bvector
*msgstats
; /* msg status */
319 * A FILE handle containing an open filehandle for the sequence file
320 * for this folder. If non-NULL, use it when the sequence file is
326 * The name of the public sequence file; required by lkfclose()
332 * Amount of space to allocate for msgstats. Allocate
333 * the array to have space for messages numbered lo to hi.
334 * Use MSGSTATNUM to load mp->num_msgstats first.
336 #define MSGSTATNUM(lo, hi) ((size_t) ((hi) - (lo) + 1))
337 #define MSGSTATSIZE(mp) ((mp)->num_msgstats * sizeof *(mp)->msgstats)
340 * macros for message and sequence manipulation
342 #define msgstat(mp,n) ((mp)->msgstats + (n) - mp->lowoff)
343 #define clear_msg_flags(mp,msgnum) bvector_clear_all (msgstat(mp, msgnum))
344 #define copy_msg_flags(mp,i,j) bvector_copy (msgstat(mp,i), msgstat(mp,j))
345 #define get_msg_flags(mp,ptr,msgnum) bvector_copy (ptr, msgstat(mp, msgnum))
346 #define set_msg_flags(mp,ptr,msgnum) bvector_copy (msgstat(mp, msgnum), ptr)
348 #define does_exist(mp,msgnum) bvector_at (msgstat(mp, msgnum), EXISTS)
349 #define unset_exists(mp,msgnum) bvector_clear (msgstat(mp, msgnum), EXISTS)
350 #define set_exists(mp,msgnum) bvector_set (msgstat(mp, msgnum), EXISTS)
352 #define is_selected(mp,msgnum) bvector_at (msgstat(mp, msgnum), SELECTED)
353 #define unset_selected(mp,msgnum) bvector_clear (msgstat(mp, msgnum), SELECTED)
354 #define set_selected(mp,msgnum) bvector_set (msgstat(mp, msgnum), SELECTED)
356 #define is_select_empty(mp,msgnum) \
357 bvector_at (msgstat(mp, msgnum), SELECT_EMPTY)
358 #define set_select_empty(mp,msgnum) \
359 bvector_set (msgstat(mp, msgnum), SELECT_EMPTY)
361 #define is_unseen(mp,msgnum) \
362 bvector_at (msgstat(mp, msgnum), SELECT_UNSEEN)
363 #define unset_unseen(mp,msgnum) \
364 bvector_clear (msgstat(mp, msgnum), SELECT_UNSEEN)
365 #define set_unseen(mp,msgnum) \
366 bvector_set (msgstat(mp, msgnum), SELECT_UNSEEN)
368 #define in_sequence(mp,seqnum,msgnum) \
369 bvector_at (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
370 #define clear_sequence(mp,seqnum,msgnum) \
371 bvector_clear (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
372 #define add_sequence(mp,seqnum,msgnum) \
373 bvector_set (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
375 #define is_seq_private(mp,seqnum) \
376 bvector_at (mp->attrstats, FFATTRSLOT + seqnum)
377 #define make_seq_public(mp,seqnum) \
378 bvector_clear (mp->attrstats, FFATTRSLOT + seqnum)
379 #define make_seq_private(mp,seqnum) \
380 bvector_set (mp->attrstats, FFATTRSLOT + seqnum)
381 #define make_all_public(mp) \
382 mp->attrstats = bvector_create(); bvector_clear_all (mp->attrstats)
385 * macros for folder attributes
387 #define clear_folder_flags(mp) ((mp)->msgflags = 0)
389 #define is_readonly(mp) ((mp)->msgflags & READONLY)
390 #define set_readonly(mp) ((mp)->msgflags |= READONLY)
392 #define other_files(mp) ((mp)->msgflags & OTHERS)
393 #define set_other_files(mp) ((mp)->msgflags |= OTHERS)
396 * m_getfld() message parsing
399 #define NAMESZ 999 /* Limit on component name size.
400 RFC 2822 limits line lengths to
401 998 characters, so a header name
402 can be at most that long.
403 m_getfld limits header names to 2
404 less than NAMESZ, which is fine,
405 because header names must be
406 followed by a colon. Add one for
409 /* Token type or error returned from m_getfld(), and its internal state
410 * for the next call. */
411 /* FLD detects the header's name is too long to fit in the fixed size
414 /* FLD reaches EOF after the header's name, or the name is followed by
415 * a linefeed rather than a colon and the body buffer isn't large enough
416 * to pretend this header line starts the body. */
418 /* The initial state, looking for headers. Returned when the header's
421 /* Another chunk of the header's value has been returned, but there's
424 /* A chunk of the email's body has been returned. */
426 /* Either the end of the input file has been reached, or the delimiter
427 * between emails has been found and the caller should
428 * m_getfld_state_reset() to reset the state to FLD for continuing
429 * through the file. */
432 typedef struct m_getfld_state
*m_getfld_state_t
;
434 #define NOUSE 0 /* draft being re-used */
436 #define TFOLDER 0 /* path() given a +folder */
437 #define TFILE 1 /* path() given a file */
438 #define TSUBCWF 2 /* path() given a @folder */
440 #define OUTPUTLINELEN 72 /* default line length for headers */
442 #define LINK "@" /* Name of link to file to which you are */
446 * credentials management
448 typedef struct nmh_creds
*nmh_creds_t
;
451 * miscellaneous macros
453 #define pidXwait(pid,cp) pidstatus (pidwait (pid, NOTOK), stdout, cp)
456 # define max(a,b) ((a) > (b) ? (a) : (b))
460 # define min(a,b) ((a) < (b) ? (a) : (b))
464 # define abs(a) ((a) > 0 ? (a) : -(a))
470 #define CTXMOD 0x01 /* context information modified */
471 #define DBITS "\020\01CTXMOD"
472 extern char ctxflags
;
474 extern char *invo_name
; /* command invocation name */
475 extern char *mypath
; /* user's $HOME */
476 extern char *defpath
; /* pathname of user's profile */
477 extern char *ctxpath
; /* pathname of user's context */
478 extern struct node
*m_defs
; /* list of profile/context entries */
481 * These standard strings are defined in config.c. They are the
482 * only system-dependent parameters in nmh, and thus by redefining
483 * their values and reloading the various modules, nmh will run
486 extern char *buildmimeproc
;
487 extern char *catproc
;
488 extern char *components
;
489 extern char *context
;
490 extern char *current
;
491 extern char *credentials_file
;
492 extern int credentials_no_perm_check
;
493 extern char *defaultfolder
;
494 extern char *digestcomps
;
495 extern char *distcomps
;
497 extern char *fileproc
;
498 extern char *foldprot
;
499 extern char *formatproc
;
500 extern char *forwcomps
;
502 extern char *incproc
;
504 extern char *mailproc
;
505 extern char *mh_defaults
;
506 extern char *mh_profile
;
508 extern char *mhlformat
;
509 extern char *mhlforward
;
510 extern char *mhlproc
;
511 extern char *mhlreply
;
512 extern char *moreproc
;
513 extern char *msgprot
;
514 extern char *nmhaccessftp
;
515 extern char *nmhaccessurl
;
516 extern char *nmhstorage
;
517 extern char *nmhcache
;
518 extern char *nmhprivcache
;
519 extern char *nsequence
;
520 extern char *packproc
;
521 extern char *postproc
;
522 extern char *pfolder
;
523 extern char *psequence
;
524 extern char *rcvdistcomps
;
525 extern char *rcvstoreproc
;
526 extern char *replcomps
;
527 extern char *replgroupcomps
;
528 extern char *rmmproc
;
529 extern char *sendproc
;
530 extern char *showmimeproc
;
531 extern char *showproc
;
532 extern char *usequence
;
533 extern char *user_agent
;
534 extern char *version_num
;
535 extern char *version_str
;
536 extern char *whatnowproc
;
537 extern char *whomproc
;
539 #include "prototypes.h"