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