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