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