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