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