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