]> diplodocus.org Git - nmh/blob - h/mh.h
Hoist test of search string for NULL out of loop.
[nmh] / h / mh.h
1 /*
2 * mh.h -- main header file for all of nmh
3 */
4
5 #include <h/nmh.h>
6
7 /*
8 * Well-used constants
9 */
10 #define NOTOK (-1) /* syscall()s return this on error */
11 #define OK 0 /* ditto on success */
12 #define DONE 1 /* trinary logic */
13 #define ALL ""
14 #define Nbby 8 /* number of bits/byte */
15
16 #define MAXARGS 1000 /* max arguments to exec */
17 #define NFOLDERS 1000 /* max folder arguments on command line */
18 #define DMAXFOLDER 4 /* typical number of digits */
19 #define MAXFOLDER 1000 /* message increment */
20
21 /*
22 * This macro is for use by scan, for example, so that platforms with
23 * a small BUFSIZ can easily allocate larger buffers.
24 */
25 #define NMH_BUFSIZ (BUFSIZ>=8192 ? BUFSIZ : 8192)
26
27 #ifndef FALSE
28 #define FALSE false
29 #endif
30 #ifndef TRUE
31 #define TRUE true
32 #endif
33 typedef unsigned char boolean; /* not int so we can pack in a structure */
34
35 /* If we're using gcc then give it some information about
36 * functions that abort.
37 */
38 #if __GNUC__ > 2
39 #define NORETURN __attribute__((__noreturn__))
40 #define NMH_UNUSED(i) (void) i
41 #else
42 #define NORETURN
43 #define NMH_UNUSED(i) i
44 #endif
45
46 /* LEN gives the strlen() of string constant s, excluding the
47 * terminating NUL. */
48 #define LEN(s) (sizeof (s) - 1)
49
50 /*
51 * char array that keeps track of size in both bytes and characters
52 * Usage note:
53 * Don't store return value of charstring_buffer() and use later
54 * after intervening push_back's; use charstring_buffer_copy()
55 * instead.
56 */
57 typedef struct charstring *charstring_t;
58
59 charstring_t charstring_create (size_t);
60 charstring_t charstring_copy (const charstring_t);
61 void charstring_free (charstring_t);
62 /* Append a single-byte character: */
63 void charstring_push_back (charstring_t, const char);
64 /* Append possibly multi-byte character(s): */
65 void charstring_push_back_chars (charstring_t, const char [], size_t, size_t);
66 void charstring_append (charstring_t, const charstring_t);
67 void charstring_append_cstring (charstring_t, const char []);
68 void charstring_clear (charstring_t);
69 /* Don't store return value of charstring_buffer() and use later after
70 intervening push_back's; use charstring_buffer_copy() instead. */
71 const char *charstring_buffer (const charstring_t);
72 /* User is responsible for free'ing result of buffer copy. */
73 char *charstring_buffer_copy (const charstring_t);
74 size_t charstring_bytes (const charstring_t);
75 size_t charstring_chars (const charstring_t);
76 /* Length of the last character in the charstring. */
77 int charstring_last_char_len (const charstring_t);
78
79 /*
80 * user context/profile structure
81 */
82 struct node {
83 char *n_name; /* key */
84 char *n_field; /* value */
85 char n_context; /* context, not profile */
86 struct node *n_next; /* next entry */
87 };
88
89 /*
90 * switches structure
91 */
92 #define AMBIGSW (-2) /* from smatch() on ambiguous switch */
93 #define UNKWNSW (-1) /* from smatch() on unknown switch */
94
95 struct swit {
96
97 /*
98 * Switch name
99 */
100
101 char *sw;
102
103 /* The minchars field is apparently used like this:
104
105 -# : Switch can be abbreviated to # characters; switch hidden in -help.
106 0 : Switch can't be abbreviated; switch shown in -help.
107 # : Switch can be abbreviated to # characters; switch shown in -help. */
108 int minchars;
109
110 /*
111 * If we pick this switch, return this value from smatch
112 */
113
114 int swret;
115 };
116
117 /*
118 * Macros to use when declaring struct swit arrays.
119 *
120 * These macros use a technique known as X-Macros. In your source code you
121 * use them like this:
122 *
123 * #define FOO_SWITCHES \
124 * X("switch1", 0, SWITCHSW) \
125 * X("switch2", 0, SWITCH2SW) \
126 * X("thirdswitch", 2, SWITCH3SW) \
127 *
128 * The argument to each entry in FOO_SWITCHES are the switch name (sw),
129 * the minchars field (see above) and the return value for this switch.
130 * Note that the last entry in the above definition must either omit the
131 * continuation backslash, or be followed by a blank line. In the nmh
132 * code the style is to have every line include a backslash and follow
133 * the SWITCHES macro definition by a blank line.
134 *
135 * After you define FOO_SWITCHES, you instantiate it as follows:
136 *
137 * #define X(sw, minchars, id) id,
138 * DEFINE_SWITCH_ENUM(FOO);
139 * #undef X
140 *
141 * #define X(sw, minchars, id) { sw, minchars, id },
142 * DEFINE_SWITCH_ARRAY(FOO);
143 * #undef X
144 *
145 * DEFINE_SWITCH_ENUM defines an extra enum at the end of the list called
146 * LEN_FOO.
147 */
148
149 #define DEFINE_SWITCH_ENUM(name) \
150 enum { \
151 name ## _SWITCHES \
152 LEN_ ## name \
153 }
154
155 #define DEFINE_SWITCH_ARRAY(name, array) \
156 static struct swit array[] = { \
157 name ## _SWITCHES \
158 { NULL, 0, 0 } \
159 }
160
161 extern struct swit anoyes[]; /* standard yes/no switches */
162
163 /*
164 * general folder attributes
165 */
166 #define READONLY (1<<0) /* No write access to folder */
167 #define SEQMOD (1<<1) /* folder's sequences modified */
168 #define ALLOW_NEW (1<<2) /* allow the "new" sequence */
169 #define OTHERS (1<<3) /* folder has other files */
170 #define MODIFIED (1<<4) /* msh in-core folder modified */
171
172 #define FBITS "\020\01READONLY\02SEQMOD\03ALLOW_NEW\04OTHERS\05MODIFIED"
173
174 /*
175 * first free slot for user defined sequences
176 * and attributes
177 */
178 #define FFATTRSLOT 5
179
180 /*
181 * internal messages attributes (sequences)
182 */
183 #define EXISTS (0) /* exists */
184 #define DELETED (1) /* deleted */
185 #define SELECTED (2) /* selected for use */
186 #define SELECT_EMPTY (3) /* "new" message */
187 #define SELECT_UNSEEN (4) /* inc/show "unseen" */
188
189 #define MBITS "\020\01EXISTS\02DELETED\03SELECTED\04NEW\05UNSEEN"
190
191 /*
192 * type for holding the sequence set of a message
193 */
194 typedef struct bvector *bvector_t;
195
196 bvector_t bvector_create (size_t /* initial size in bits, can be 0 */);
197 void bvector_copy (bvector_t, bvector_t);
198 void bvector_free (bvector_t);
199 void bvector_clear (bvector_t, size_t);
200 void bvector_clear_all (bvector_t);
201 void bvector_set (bvector_t, size_t);
202 unsigned int bvector_at (bvector_t, size_t);
203 const unsigned long *bvector_bits (bvector_t);
204 size_t bvector_maxsize (bvector_t);
205
206 typedef struct svector *svector_t;
207
208 svector_t svector_create (size_t);
209 void svector_free (svector_t);
210 char *svector_push_back (svector_t, char *);
211 char *svector_at (svector_t, size_t);
212 char **svector_find(svector_t, const char *);
213 char **svector_strs (svector_t);
214 size_t svector_size (svector_t);
215
216 typedef struct ivector *ivector_t;
217
218 ivector_t ivector_create (size_t);
219 void ivector_free (ivector_t);
220 int ivector_push_back (ivector_t, int);
221 int ivector_at (ivector_t, size_t);
222 int *ivector_atp (ivector_t, size_t);
223 size_t ivector_size (ivector_t);
224
225 /*
226 * Primary structure of folder/message information
227 */
228 struct msgs {
229 int lowmsg; /* Lowest msg number */
230 int hghmsg; /* Highest msg number */
231 int nummsg; /* Actual Number of msgs */
232
233 int lowsel; /* Lowest selected msg number */
234 int hghsel; /* Highest selected msg number */
235 int numsel; /* Number of msgs selected */
236
237 int curmsg; /* Number of current msg if any */
238
239 int msgflags; /* Folder attributes (READONLY, etc) */
240 char *foldpath; /* Pathname of folder */
241
242 /*
243 * Name of sequences in this folder.
244 */
245 svector_t msgattrs;
246
247 /*
248 * bit flags for whether sequence
249 * is public (0), or private (1)
250 */
251 bvector_t attrstats;
252
253 /*
254 * These represent the lowest and highest possible
255 * message numbers we can put in the message status
256 * area, without calling folder_realloc().
257 */
258 int lowoff;
259 int hghoff;
260
261 /*
262 * This is an array of bvector_t which we allocate dynamically.
263 * Each bvector_t is a set of bits flags for a particular message.
264 * These bit flags represent general attributes such as
265 * EXISTS, SELECTED, etc. as well as track if message is
266 * in a particular sequence.
267 */
268 size_t num_msgstats;
269 bvector_t *msgstats; /* msg status */
270
271 /*
272 * A FILE handle containing an open filehandle for the sequence file
273 * for this folder. If non-NULL, use it when the sequence file is
274 * written.
275 */
276 FILE *seqhandle;
277
278 /*
279 * The name of the public sequence file; required by lkfclose()
280 */
281 char *seqname;
282 };
283
284 /*
285 * Amount of space to allocate for msgstats. Allocate
286 * the array to have space for messages numbered lo to hi.
287 * Use MSGSTATNUM to load mp->num_msgstats first.
288 */
289 #define MSGSTATNUM(lo, hi) ((size_t) ((hi) - (lo) + 1))
290 #define MSGSTATSIZE(mp) ((mp)->num_msgstats * sizeof *(mp)->msgstats)
291
292 /*
293 * macros for message and sequence manipulation
294 */
295 #define msgstat(mp,n) (mp)->msgstats[(n) - mp->lowoff]
296 #define clear_msg_flags(mp,msgnum) bvector_clear_all (msgstat(mp, msgnum))
297 #define copy_msg_flags(mp,i,j) bvector_copy (msgstat(mp,i), msgstat(mp,j))
298 #define get_msg_flags(mp,ptr,msgnum) bvector_copy (ptr, msgstat(mp, msgnum))
299 #define set_msg_flags(mp,ptr,msgnum) bvector_copy (msgstat(mp, msgnum), ptr)
300
301 #define does_exist(mp,msgnum) bvector_at (msgstat(mp, msgnum), EXISTS)
302 #define unset_exists(mp,msgnum) bvector_clear (msgstat(mp, msgnum), EXISTS)
303 #define set_exists(mp,msgnum) bvector_set (msgstat(mp, msgnum), EXISTS)
304
305 #define is_selected(mp,msgnum) bvector_at (msgstat(mp, msgnum), SELECTED)
306 #define unset_selected(mp,msgnum) bvector_clear (msgstat(mp, msgnum), SELECTED)
307 #define set_selected(mp,msgnum) bvector_set (msgstat(mp, msgnum), SELECTED)
308
309 #define is_select_empty(mp,msgnum) \
310 bvector_at (msgstat(mp, msgnum), SELECT_EMPTY)
311 #define set_select_empty(mp,msgnum) \
312 bvector_set (msgstat(mp, msgnum), SELECT_EMPTY)
313
314 #define is_unseen(mp,msgnum) \
315 bvector_at (msgstat(mp, msgnum), SELECT_UNSEEN)
316 #define unset_unseen(mp,msgnum) \
317 bvector_clear (msgstat(mp, msgnum), SELECT_UNSEEN)
318 #define set_unseen(mp,msgnum) \
319 bvector_set (msgstat(mp, msgnum), SELECT_UNSEEN)
320
321 /* for msh only */
322 #define set_deleted(mp,msgnum) bvector_set (msgstat(mp, msgnum), DELETED)
323
324 #define in_sequence(mp,seqnum,msgnum) \
325 bvector_at (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
326 #define clear_sequence(mp,seqnum,msgnum) \
327 bvector_clear (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
328 #define add_sequence(mp,seqnum,msgnum) \
329 bvector_set (msgstat(mp, msgnum), FFATTRSLOT + seqnum)
330
331 #define is_seq_private(mp,seqnum) \
332 bvector_at (mp->attrstats, FFATTRSLOT + seqnum)
333 #define make_seq_public(mp,seqnum) \
334 bvector_clear (mp->attrstats, FFATTRSLOT + seqnum)
335 #define make_seq_private(mp,seqnum) \
336 bvector_set (mp->attrstats, FFATTRSLOT + seqnum)
337 #define make_all_public(mp) \
338 mp->attrstats = bvector_create(0); bvector_clear_all (mp->attrstats)
339
340 /*
341 * macros for folder attributes
342 */
343 #define clear_folder_flags(mp) ((mp)->msgflags = 0)
344
345 #define is_readonly(mp) ((mp)->msgflags & READONLY)
346 #define set_readonly(mp) ((mp)->msgflags |= READONLY)
347
348 #define other_files(mp) ((mp)->msgflags & OTHERS)
349 #define set_other_files(mp) ((mp)->msgflags |= OTHERS)
350
351 #define NULLMP ((struct msgs *) 0)
352
353 /*
354 * m_getfld() message parsing
355 */
356
357 #define NAMESZ 999 /* Limit on component name size.
358 RFC 2822 limits line lengths to
359 998 characters, so a header name
360 can be at most that long.
361 m_getfld limits header names to 2
362 less than NAMESZ, which is fine,
363 because header names must be
364 followed by a colon. Add one for
365 terminating NULL. */
366
367 #define LENERR (-2) /* Name too long error from getfld */
368 #define FMTERR (-3) /* Message Format error */
369 #define FLD 0 /* Field returned */
370 #define FLDPLUS 1 /* Field returned with more to come */
371 #define BODY 3 /* Body returned with more to come */
372 #define FILEEOF 5 /* Reached end of input file */
373
374 struct m_getfld_state;
375 typedef struct m_getfld_state *m_getfld_state_t;
376
377 /*
378 * Maildrop styles
379 */
380 #define MS_DEFAULT 0 /* default (one msg per file) */
381 #define MS_UNKNOWN 1 /* type not known yet */
382 #define MS_MBOX 2 /* Unix-style "from" lines */
383 #define MS_MMDF 3 /* string mmdlm2 */
384
385 #define NOUSE 0 /* draft being re-used */
386
387 #define TFOLDER 0 /* path() given a +folder */
388 #define TFILE 1 /* path() given a file */
389 #define TSUBCWF 2 /* path() given a @folder */
390
391 #define OUTPUTLINELEN 72 /* default line length for headers */
392
393 #define LINK "@" /* Name of link to file to which you are */
394 /* replying. */
395
396 /*
397 * credentials management
398 */
399 struct nmh_creds;
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>
501