]> diplodocus.org Git - nmh/blob - h/mh.h
8823d91963bd343f695618c031aa059619f39957
[nmh] / h / mh.h
1 /* mh.h -- main header file for all of nmh
2 */
3
4 #include <h/nmh.h>
5
6 /*
7 * Well-used constants
8 */
9 #define NOTOK (-1) /* syscall()s return this on error */
10 #define OK 0 /* ditto on success */
11 #define DONE 1 /* ternary logic */
12 #define ALL ""
13
14 #define MAXARGS 1000 /* max arguments to exec */
15 #define NFOLDERS 1000 /* max folder arguments on command line */
16 #define DMAXFOLDER 4 /* typical number of digits */
17 #define MAXFOLDER 1000 /* message increment */
18
19 /*
20 * This macro is for use by scan, for example, so that platforms with
21 * a small BUFSIZ can easily allocate larger buffers.
22 */
23 #define NMH_BUFSIZ max(BUFSIZ, 8192)
24
25 #ifndef FALSE
26 #define FALSE false
27 #endif
28 #ifndef TRUE
29 #define TRUE true
30 #endif
31
32 /* If we're using gcc then tell it extra information so it can do more
33 * compile-time checks. */
34 #if __GNUC__ > 2
35 #define NORETURN __attribute__((__noreturn__))
36 #define CONST __attribute__((const))
37 #define MALLOC __attribute__((malloc))
38 #define NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
39 #define PURE __attribute__((pure))
40 #define ENDNULL __attribute__((sentinel))
41 #else
42 #define NORETURN
43 #define CHECK_PRINTF(fmt, arg)
44 #define ALLOC_SIZE(...)
45 #define CONST
46 #define MALLOC
47 #define NONNULL(...)
48 #define PURE
49 #define ENDNULL
50 #endif
51
52 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
53 #define ALLOC_SIZE(...) __attribute__((alloc_size(__VA_ARGS__)))
54 #define CHECK_PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg)))
55 #else
56 #define ALLOC_SIZE(...)
57 #define CHECK_PRINTF(fmt, arg)
58 #endif
59
60 /* Silence the compiler's "unused variable" warning. */
61 #define NMH_UNUSED(i) (void)i
62
63 /* DIM gives the number of elements in the one-dimensional array a. */
64 #define DIM(a) (sizeof (a) / sizeof (*(a)))
65
66 /* LEN gives the strlen() of string constant s, excluding the
67 * terminating NUL. */
68 #define LEN(s) (sizeof (s) - 1)
69
70 /* FENDNULL fends off NULL by giving an empty string instead. */
71 #define FENDNULL(s) ((s) ? (s) : "")
72
73 /* If not specified in a file and PAGER is NULL or empty. */
74 #define DEFAULT_PAGER "more"
75
76 /*
77 * char array that keeps track of size in both bytes and characters
78 * Usage note:
79 * Don't store return value of charstring_buffer() and use later
80 * after intervening push_back's; use charstring_buffer_copy()
81 * instead.
82 */
83 typedef struct charstring *charstring_t;
84
85 charstring_t charstring_create (size_t);
86 charstring_t charstring_copy (const charstring_t) NONNULL(1);
87 void charstring_free (charstring_t);
88 /* Append a single-byte character: */
89 void charstring_push_back (charstring_t, const char) NONNULL(1);
90 /* Append possibly multi-byte character(s): */
91 void charstring_push_back_chars (charstring_t, const char [], size_t, size_t) NONNULL(1);
92 void charstring_append (charstring_t, const charstring_t) NONNULL(2);
93 void charstring_append_cstring (charstring_t, const char []) NONNULL(2);
94 void charstring_clear (charstring_t) NONNULL(1);
95 /* Don't store return value of charstring_buffer() and use later after
96 intervening push_back's; use charstring_buffer_copy() instead. */
97 const char *charstring_buffer (const charstring_t) NONNULL(1);
98 /* User is responsible for free'ing result of buffer copy. */
99 char *charstring_buffer_copy (const charstring_t) NONNULL(1);
100 size_t charstring_bytes (const charstring_t) NONNULL(1) PURE;
101 size_t charstring_chars (const charstring_t) NONNULL(1) PURE;
102 /* Length of the last character in the charstring. */
103 int charstring_last_char_len (const charstring_t) NONNULL(1);
104
105 /*
106 * user context/profile structure
107 */
108 struct node {
109 char *n_name; /* key */
110 char *n_field; /* value */
111 char n_context; /* context, not profile */
112 struct node *n_next; /* next entry */
113 };
114
115 /*
116 * switches structure
117 */
118 #define AMBIGSW (-2) /* from smatch() on ambiguous switch */
119 #define UNKWNSW (-1) /* from smatch() on unknown switch */
120
121 struct swit {
122
123 /*
124 * Switch name
125 */
126
127 char *sw;
128
129 /* The minchars field is apparently used like this:
130
131 -# : Switch can be abbreviated to # characters; switch hidden in -help.
132 0 : Switch can't be abbreviated; switch shown in -help.
133 # : Switch can be abbreviated to # characters; switch shown in -help. */
134 int minchars;
135
136 /*
137 * If we pick this switch, return this value from smatch
138 */
139
140 int swret;
141 };
142
143 /*
144 * Macros to use when declaring struct swit arrays.
145 *
146 * These macros use a technique known as X-Macros. In your source code you
147 * use them like this:
148 *
149 * #define FOO_SWITCHES \
150 * X("switch1", 0, SWITCHSW) \
151 * X("switch2", 0, SWITCH2SW) \
152 * X("thirdswitch", 2, SWITCH3SW) \
153 *
154 * The argument to each entry in FOO_SWITCHES are the switch name (sw),
155 * the minchars field (see above) and the return value for this switch.
156 * Note that the last entry in the above definition must either omit the
157 * continuation backslash, or be followed by a blank line. In the nmh
158 * code the style is to have every line include a backslash and follow
159 * the SWITCHES macro definition by a blank line.
160 *
161 * After you define FOO_SWITCHES, you instantiate it as follows:
162 *
163 * #define X(sw, minchars, id) id,
164 * DEFINE_SWITCH_ENUM(FOO);
165 * #undef X
166 *
167 * #define X(sw, minchars, id) { sw, minchars, id },
168 * DEFINE_SWITCH_ARRAY(FOO);
169 * #undef X
170 *
171 * DEFINE_SWITCH_ENUM defines an extra enum at the end of the list called
172 * LEN_FOO.
173 */
174
175 #define DEFINE_SWITCH_ENUM(name) \
176 enum { \
177 name ## _SWITCHES \
178 LEN_ ## name \
179 }
180
181 #define DEFINE_SWITCH_ARRAY(name, array) \
182 static struct swit array[] = { \
183 name ## _SWITCHES \
184 { NULL, 0, 0 } \
185 }
186
187 extern struct swit anoyes[]; /* standard yes/no switches */
188
189 /*
190 * general folder attributes
191 */
192 #define READONLY (1<<0) /* No write access to folder */
193 #define SEQMOD (1<<1) /* folder's sequences modified */
194 #define ALLOW_NEW (1<<2) /* allow the "new" sequence */
195 #define OTHERS (1<<3) /* folder has other files */
196
197 #define FBITS "\020\01READONLY\02SEQMOD\03ALLOW_NEW\04OTHERS"
198
199 /*
200 * first free slot for user defined sequences
201 * and attributes
202 */
203 #define FFATTRSLOT 4
204
205 /*
206 * internal messages attributes (sequences)
207 */
208 #define EXISTS (0) /* exists */
209 #define SELECTED (1) /* selected for use */
210 #define SELECT_EMPTY (2) /* "new" message */
211 #define SELECT_UNSEEN (3) /* inc/show "unseen" */
212
213 #define MBITS "\020\01EXISTS\02SELECTED\03NEW\04UNSEEN"
214
215 /* A vector of bits for tracking the sequence membership of a single
216 * message. Do not access the struct members; use vector.c.
217 * Do not move or copy this struct as it may contain a pointer to
218 * itself; use bvector_copy(). */
219 struct bvector {
220 unsigned long *bits;
221 size_t maxsize;
222 unsigned long tiny[2]; /* Default fixed-size storage for bits. */
223 };
224 typedef struct bvector *bvector_t;
225
226 bvector_t bvector_create (void);
227 void bvector_init(struct bvector *bv) NONNULL(1);
228 void bvector_copy (bvector_t, bvector_t) NONNULL(1, 2);
229 void bvector_free (bvector_t) NONNULL(1);
230 void bvector_fini(struct bvector *bv) NONNULL(1);
231 void bvector_clear (bvector_t, size_t) NONNULL(1);
232 void bvector_clear_all (bvector_t) NONNULL(1);
233 void bvector_set (bvector_t, size_t) NONNULL(1);
234 unsigned int bvector_at (bvector_t, size_t) NONNULL(1) PURE;
235 unsigned long bvector_first_bits (bvector_t) NONNULL(1) PURE;
236
237 typedef struct svector *svector_t;
238
239 svector_t svector_create (size_t);
240 void svector_free (svector_t) NONNULL(1);
241 char *svector_push_back (svector_t, char *) NONNULL(1);
242 char *svector_at (svector_t, size_t) NONNULL(1);
243 char **svector_find(svector_t, const char *) NONNULL(1) PURE;
244 char **svector_strs (svector_t) NONNULL(1) PURE;
245 size_t svector_size (svector_t) NONNULL(1) PURE;
246
247 typedef struct ivector *ivector_t;
248
249 ivector_t ivector_create (size_t);
250 void ivector_free (ivector_t) NONNULL(1);
251 int ivector_push_back (ivector_t, int) NONNULL(1);
252 int ivector_at (ivector_t, size_t) NONNULL(1);
253 int *ivector_atp (ivector_t, size_t) NONNULL(1);
254
255 /*
256 * Primary structure of folder/message information
257 */
258 struct msgs {
259 int lowmsg; /* Lowest msg number */
260 int hghmsg; /* Highest msg number */
261 int nummsg; /* Actual Number of msgs */
262
263 int lowsel; /* Lowest selected msg number */
264 int hghsel; /* Highest selected msg number */
265 int numsel; /* Number of msgs selected */
266
267 int curmsg; /* Number of current msg if any */
268
269 int msgflags; /* Folder attributes (READONLY, etc) */
270 char *foldpath; /* Pathname of folder */
271
272 /*
273 * Name of sequences in this folder.
274 */
275 svector_t msgattrs;
276
277 /*
278 * bit flags for whether sequence
279 * is public (0), or private (1)
280 */
281 bvector_t attrstats;
282
283 /*
284 * These represent the lowest and highest possible
285 * message numbers we can put in the message status
286 * area, without calling folder_realloc().
287 */
288 int lowoff;
289 int hghoff;
290
291 /*
292 * This is an array of bvector_t which we allocate dynamically.
293 * Each bvector_t is a set of bits flags for a particular message.
294 * These bit flags represent general attributes such as
295 * EXISTS, SELECTED, etc. as well as track if message is
296 * in a particular sequence.
297 */
298 size_t num_msgstats;
299 struct bvector *msgstats; /* msg status */
300
301 /*
302 * A FILE handle containing an open filehandle for the sequence file
303 * for this folder. If non-NULL, use it when the sequence file is
304 * written.
305 */
306 FILE *seqhandle;
307
308 /*
309 * The name of the public sequence file; required by lkfclose()
310 */
311 char *seqname;
312 };
313
314 /*
315 * Amount of space to allocate for msgstats. Allocate
316 * the array to have space for messages numbered lo to hi.
317 * Use MSGSTATNUM to load mp->num_msgstats first.
318 */
319 #define MSGSTATNUM(lo, hi) ((size_t) ((hi) - (lo) + 1))
320 #define MSGSTATSIZE(mp) ((mp)->num_msgstats * sizeof *(mp)->msgstats)
321
322 /*
323 * macros for message and sequence manipulation
324 */
325 #define msgstat(mp,n) ((mp)->msgstats + (n) - mp->lowoff)
326 #define clear_msg_flags(mp,msgnum) bvector_clear_all (msgstat(mp, msgnum))
327 #define copy_msg_flags(mp,i,j) bvector_copy (msgstat(mp,i), msgstat(mp,j))
328 #define get_msg_flags(mp,ptr,msgnum) bvector_copy (ptr, msgstat(mp, msgnum))
329 #define set_msg_flags(mp,ptr,msgnum) bvector_copy (msgstat(mp, msgnum), ptr)
330
331 #define does_exist(mp,msgnum) bvector_at (msgstat(mp, msgnum), EXISTS)
332 #define unset_exists(mp,msgnum) bvector_clear (msgstat(mp, msgnum), EXISTS)
333 #define set_exists(mp,msgnum) bvector_set (msgstat(mp, msgnum), EXISTS)
334
335 #define is_selected(mp,msgnum) bvector_at (msgstat(mp, msgnum), SELECTED)
336 #define unset_selected(mp,msgnum) bvector_clear (msgstat(mp, msgnum), SELECTED)
337 #define set_selected(mp,msgnum) bvector_set (msgstat(mp, msgnum), SELECTED)
338
339 #define is_select_empty(mp,msgnum) \
340 bvector_at (msgstat(mp, msgnum), SELECT_EMPTY)
341 #define set_select_empty(mp,msgnum) \
342 bvector_set (msgstat(mp, msgnum), SELECT_EMPTY)
343
344 #define is_unseen(mp,msgnum) \
345 bvector_at (msgstat(mp, msgnum), SELECT_UNSEEN)
346 #define unset_unseen(mp,msgnum) \
347 bvector_clear (msgstat(mp, msgnum), SELECT_UNSEEN)
348 #define set_unseen(mp,msgnum) \
349 bvector_set (msgstat(mp, msgnum), SELECT_UNSEEN)
350
351 #define in_sequence(mp,seqnum,msgnum) \
352 bvector_at (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
353 #define clear_sequence(mp,seqnum,msgnum) \
354 bvector_clear (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
355 #define add_sequence(mp,seqnum,msgnum) \
356 bvector_set (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
357
358 #define is_seq_private(mp,seqnum) \
359 bvector_at (mp->attrstats, FFATTRSLOT + seqnum)
360 #define make_seq_public(mp,seqnum) \
361 bvector_clear (mp->attrstats, FFATTRSLOT + seqnum)
362 #define make_seq_private(mp,seqnum) \
363 bvector_set (mp->attrstats, FFATTRSLOT + seqnum)
364 #define make_all_public(mp) \
365 mp->attrstats = bvector_create(); bvector_clear_all (mp->attrstats)
366
367 /*
368 * macros for folder attributes
369 */
370 #define clear_folder_flags(mp) ((mp)->msgflags = 0)
371
372 #define is_readonly(mp) ((mp)->msgflags & READONLY)
373 #define set_readonly(mp) ((mp)->msgflags |= READONLY)
374
375 #define other_files(mp) ((mp)->msgflags & OTHERS)
376 #define set_other_files(mp) ((mp)->msgflags |= OTHERS)
377
378 /*
379 * m_getfld() message parsing
380 */
381
382 #define NAMESZ 999 /* Limit on component name size.
383 RFC 2822 limits line lengths to
384 998 characters, so a header name
385 can be at most that long.
386 m_getfld limits header names to 2
387 less than NAMESZ, which is fine,
388 because header names must be
389 followed by a colon. Add one for
390 terminating NULL. */
391
392 /* Token type or error returned from m_getfld(), and its internal state
393 * for the next call. */
394 /* FLD detects the header's name is too long to fit in the fixed size
395 * array. */
396 #define LENERR (-2)
397 /* FLD reaches EOF after the header's name, or the name is followed by
398 * a linefeed rather than a colon and the body buffer isn't large enough
399 * to pretend this header line starts the body. */
400 #define FMTERR (-3)
401 /* The initial state, looking for headers. Returned when the header's
402 * value finishes. */
403 #define FLD 0
404 /* Another chunk of the header's value has been returned, but there's
405 * more to come. */
406 #define FLDPLUS 1
407 /* A chunk of the email's body has been returned. */
408 #define BODY 3
409 /* Either the end of the input file has been reached, or the delimiter
410 * between emails has been found and the caller should
411 * m_getfld_state_reset() to reset the state to FLD for continuing
412 * through the file. */
413 #define FILEEOF 5
414
415 typedef struct m_getfld_state *m_getfld_state_t;
416
417 #define NOUSE 0 /* draft being re-used */
418
419 #define TFOLDER 0 /* path() given a +folder */
420 #define TFILE 1 /* path() given a file */
421 #define TSUBCWF 2 /* path() given a @folder */
422
423 #define OUTPUTLINELEN 72 /* default line length for headers */
424
425 #define LINK "@" /* Name of link to file to which you are */
426 /* replying. */
427
428 /*
429 * credentials management
430 */
431 typedef struct nmh_creds *nmh_creds_t;
432
433 /*
434 * miscellaneous macros
435 */
436 #define pidXwait(pid,cp) pidstatus (pidwait (pid, NOTOK), stdout, cp)
437
438 #ifndef max
439 # define max(a,b) ((a) > (b) ? (a) : (b))
440 #endif
441
442 #ifndef min
443 # define min(a,b) ((a) < (b) ? (a) : (b))
444 #endif
445
446 #ifndef abs
447 # define abs(a) ((a) > 0 ? (a) : -(a))
448 #endif
449
450 /*
451 * GLOBAL VARIABLES
452 */
453 #define CTXMOD 0x01 /* context information modified */
454 #define DBITS "\020\01CTXMOD"
455 extern char ctxflags;
456
457 extern char *invo_name; /* command invocation name */
458 extern char *mypath; /* user's $HOME */
459 extern char *defpath; /* pathname of user's profile */
460 extern char *ctxpath; /* pathname of user's context */
461 extern struct node *m_defs; /* list of profile/context entries */
462
463 /*
464 * These standard strings are defined in config.c. They are the
465 * only system-dependent parameters in nmh, and thus by redefining
466 * their values and reloading the various modules, nmh will run
467 * on any system.
468 */
469 extern char *buildmimeproc;
470 extern char *catproc;
471 extern char *components;
472 extern char *context;
473 extern char *current;
474 extern char *credentials_file;
475 extern int credentials_no_perm_check;
476 extern char *defaultfolder;
477 extern char *digestcomps;
478 extern char *distcomps;
479 extern char *draft;
480 extern char *fileproc;
481 extern char *foldprot;
482 extern char *formatproc;
483 extern char *forwcomps;
484 extern char *inbox;
485 extern char *incproc;
486 extern char *lproc;
487 extern char *mailproc;
488 extern char *mh_defaults;
489 extern char *mh_profile;
490 extern char *mh_seq;
491 extern char *mhlformat;
492 extern char *mhlforward;
493 extern char *mhlproc;
494 extern char *mhlreply;
495 extern char *moreproc;
496 extern char *msgprot;
497 extern char *nmhaccessftp;
498 extern char *nmhaccessurl;
499 extern char *nmhstorage;
500 extern char *nmhcache;
501 extern char *nmhprivcache;
502 extern char *nsequence;
503 extern char *packproc;
504 extern char *postproc;
505 extern char *pfolder;
506 extern char *psequence;
507 extern char *rcvdistcomps;
508 extern char *rcvstoreproc;
509 extern char *replcomps;
510 extern char *replgroupcomps;
511 extern char *rmmproc;
512 extern char *sendproc;
513 extern char *showmimeproc;
514 extern char *showproc;
515 extern char *usequence;
516 extern char *user_agent;
517 extern char *version_num;
518 extern char *version_str;
519 extern char *whatnowproc;
520 extern char *whomproc;
521
522 extern void (*done) (int) NORETURN;
523
524 #include <h/prototypes.h>