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