1 /*
2  * Copyright (C) 1997-2004, Michael Jennings
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * @file libast.h
26  * Global LibAST header file.
27  *
28  * This file contains all general-purpose macros, function
29  * declarations, etc. for LibAST.  It is also responsible for
30  * including all required system headers and LibAST Object headers.
31  *
32  * @author Michael Jennings <mej@eterm.org>
33  * @version $Revision: 1.61 $
34  * @date $Date: 2005/07/16 01:39:24 $
35  */
36 
37 #ifndef _LIBAST_H_
38 #define _LIBAST_H_
39 
40 #include <libast/sysdefs.h>
41 
42 #include <limits.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <time.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48 #include <unistd.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <fcntl.h>
52 #include <dirent.h>
53 #include <errno.h>
54 #include <signal.h>
55 #include <limits.h>
56 #include <math.h>
57 #if TIME_WITH_SYS_TIME
58 # include <sys/time.h>
59 #endif
60 #if WITH_DMALLOC
61 # include <dmalloc.h>
62 #elif HAVE_MALLOC_H
63 # include <malloc.h>
64 #endif
65 
66 #include <netdb.h>
67 #include <sys/types.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <sys/un.h>
71 #include <arpa/inet.h>
72 #include <netinet/tcp.h>
73 #include <netinet/udp.h>
74 
75 #if LIBAST_X11_SUPPORT
76 # include <X11/Xatom.h>
77 # include <X11/X.h>
78 # include <X11/Intrinsic.h>
79 # if LIBAST_IMLIB2_SUPPORT
80 #  include <Imlib2.h>
81 # endif
82 #endif
83 
84 #ifdef __GNUC__
85 #  if __GNUC__ >= 4
86 #    undef  STRICT_ISO_C99
87 #    define STRICT_ISO_C99 1
88 #  endif
89 #else
90 #  define __attribute__(x)
91 #  define __extension__(x)
92 #  define __volatile__(x)
93 #endif
94 
95 #if LIBAST_REGEXP_SUPPORT_PCRE
96 #  if HAVE_PCRE_H
97 #    include <pcre.h>
98 #  elif HAVE_PCRE_PCRE_H
99 #    include <pcre/pcre.h>
100 #  endif
101 #elif LIBAST_REGEXP_SUPPORT_POSIX || LIBAST_REGEXP_SUPPORT_BSD
102 #  if HAVE_REGEX_H
103 #    include <regex.h>
104 #  endif
105 #  if LIBAST_REGEXP_SUPPORT_BSD
106 extern char *re_comp();
107 extern int re_exec();
108 #  endif
109 #endif
110 
111 #include <libast/types.h>
112 #include <libast/obj.h>
113 
114 #include <libast/mbuff.h>
115 #include <libast/objpair.h>
116 #include <libast/regexp.h>
117 #include <libast/socket.h>
118 #include <libast/str.h>
119 #include <libast/tok.h>
120 #include <libast/url.h>
121 
122 #include <libast/iterator_if.h>
123 #include <libast/list_if.h>
124 #include <libast/map_if.h>
125 #include <libast/vector_if.h>
126 
127 #include <libast/array.h>
128 #include <libast/linked_list.h>
129 #include <libast/dlinked_list.h>
130 
131 #include <libast/avl_tree.h>
132 
133 /******************************* GENERIC GOOP *********************************/
134 /**
135  * Mark a variable as used.
136  *
137  * This macro is used to explicitly mark a variable as "used."  It
138  * intentionally generates no real code, but suppresses gcc warnings
139  * about unused variables and/or parameters.  That way, the programmer
140  * can explicitly acknowledge that certain variables/parameters are
141  * intentionally unused, making the warnings more effective by
142  * eliminating false positives.
143  *
144  * @param x Any variable or parameter name.
145  */
146 #define USE_VAR(x)   (void) x
147 
148 /**
149  * @def MIN(a, b)
150  * Return the lesser of @a a or @a b.
151  *
152  * This macro compares its two parameters, @a a and @a b, and returns
153  * the lesser of the two (the minimum).  When building under gcc, a
154  * GNU-specific extension is used which prevents expressions used as
155  * parameters from being evaluated multiple times.
156  *
157  * @param a Any expression that evaluates to a value.
158  * @param b Any expression that evaluates to a value.
159  * @return The lesser of the two values.
160  */
161 /**
162  * @def MAX(a, b)
163  * Return the greater of @a a or @a b.
164  *
165  * This macro compares its two parameters, @a a and @a b, and returns
166  * the greater of the two (the maximum).  When building under gcc, a
167  * GNU-specific extension is used which prevents expressions used as
168  * parameters from being evaluated multiple times.
169  *
170  * @param a Any expression that evaluates to a value.
171  * @param b Any expression that evaluates to a value.
172  * @return The greater of the two values.
173  */
174 /**
175  * @def LOWER_BOUND(current, other)
176  * Force a lower bound on a variable.
177  *
178  * This macro checks the value of its first parameter, @a current, and
179  * makes sure it is greater than or equal to the value of @a other.
180  * If @a current is less than @a other, @a current is assigned the
181  * value of @a other.  In essence, this establishes a "lower bound" on
182  * @a current equal to the value of @a other.
183  *
184  * @param current The variable to check.
185  * @param other   The value by which @a current will be bound.
186  * @return The new value of @a current.
187  */
188 /**
189  * @def UPPER_BOUND(current, other)
190  * Force an upper bound on a variable.
191  *
192  * This macro checks the value of its first parameter, @a current, and
193  * makes sure it is less than or equal to the value of @a other.  If
194  * @a current is greater than @a other, @a current is assigned the
195  * value of @a other.  In essence, this establishes an "upper bound"
196  * on @a current equal to the value of @a other.
197  *
198  * @param current The variable to check.
199  * @param other   The value by which @a current will be bound.
200  * @return The new value of @a current.
201  */
202 /**
203  * @def BOUND(val, min, max)
204  * Force a variable to be within a given range.
205  *
206  * This macro checks the value of its first parameter, @a val, and
207  * makes sure it is between @a min and @a max, inclusive.  If @a val
208  * is above this range, it is assigned the value of @a max.  Likewise,
209  * if @a val is below this range, it is assigned the value of @a min.
210  * In essence, this establishes both an "upper bound" and a "lower
211  * bound" on @a val.
212  *
213  * @param val The variable to check.
214  * @param min The lowest value @a val may have.
215  * @param max The highest value @a val may have.
216  * @return The new value of @a val.
217  */
218 #ifdef MIN
219 # undef MIN
220 #endif
221 #ifdef MAX
222 # undef MAX
223 #endif
224 #ifdef __GNUC__
225 # define MIN(a,b)                       __extension__ ({__typeof__(a) aa = (a); __typeof__(b) bb = (b); (aa < bb) ? (aa) : (bb);})
226 # define MAX(a,b)                       __extension__ ({__typeof__(a) aa = (a); __typeof__(b) bb = (b); (aa > bb) ? (aa) : (bb);})
227 # define LOWER_BOUND(current, other)    __extension__ ({__typeof__(other) o = (other); ((current) < o) ? ((current) = o) : (current);})
228 # define UPPER_BOUND(current, other)    __extension__ ({__typeof__(other) o = (other); ((current) > o) ? ((current) = o) : (current);})
229 # define BOUND(val, min, max)           __extension__ ({__typeof__(min) m1 = (min); __typeof__(max) m2 = (max); ((val) < m1) ? ((val) = m1) : (((val) > m2) ? ((val) = m2) : (val));})
230 #else
231 # define MIN(a,b)                       (((a) < (b)) ? (a) : (b))
232 # define MAX(a,b)                       (((a) > (b)) ? (a) : (b))
233 # define LOWER_BOUND(current, other)    (((current) < (other)) ? ((current) = (other)) : (current))
234 # define UPPER_BOUND(current, other)    (((current) > (other)) ? ((current) = (other)) : (current))
235 # define BOUND(val, min, max)           (((val) < (min)) ? ((val) = (min)) : (((val) > (max)) ? ((val) = (max)) : (val)))
236 #endif
237 /** @def AT_LEAST(current, other) Alias for LOWER_BOUND().  This macro is an alias for LOWER_BOUND(). */
238 #define AT_LEAST(current, other)        LOWER_BOUND(current, other)
239 /** @def MAX_IT(current, other) Alias for LOWER_BOUND().  This macro is an alias for LOWER_BOUND(). */
240 #define MAX_IT(current, other)          LOWER_BOUND(current, other)
241 /** @def AT_MOST(current, other) Alias for UPPER_BOUND().  This macro is an alias for UPPER_BOUND(). */
242 #define AT_MOST(current, other)         UPPER_BOUND(current, other)
243 /** @def MIN_IT(current, other) Alias for UPPER_BOUND().  This macro is an alias for UPPER_BOUND(). */
244 #define MIN_IT(current, other)          UPPER_BOUND(current, other)
245 /** @def CONTAIN(val, min, max) Alias for BOUND().  This macro is an alias for BOUND(). */
246 #define CONTAIN(val, min, max)          BOUND(val, min, max)
247 /**
248  * Swaps two values.
249  *
250  * This macro swaps the values of its first two parameters using the
251  * third as temporary storage.
252  *
253  * @param one The first variable.
254  * @param two The second variable.
255  * @param tmp A temporary holding spot used during swapping.
256  */
257 #define SWAP_IT(one, two, tmp)          do {(tmp) = (one); (one) = (two); (two) = (tmp);} while (0)
258 
259 /**
260  * @def SWAP(a, b)
261  * Swaps two values.
262  *
263  * This macro performs the same task as the SWAP_IT() macro, except
264  * that no temporary variable is required.  Instead, a temporary
265  * variable is created by the macro itself.  Under gcc, the
266  * __typeof__() extension is used to create a temporary variable of
267  * the same type as @a a.  Under other compilers, a void pointer is
268  * used.
269  *
270  * @param a The first variable.
271  * @param b The second variable.
272  */
273 #ifdef __GNUC__
274 # define SWAP(a, b)  (void) __extension__ ({__typeof__(a) __tmp = (a); (a) = (b); (b) = __tmp;})
275 #else
276 # define SWAP(a, b)  do {void *__tmp = ((void *)(a)); (a) = (b); (b) = __tmp;} while (0)
277 #endif
278 /**
279  * Swaps two values.
280  *
281  * This macro swaps the values of @a a and @a b using the now-infamous
282  * chained XOR trick.
283  *
284  * @attention ONLY use this with like variables, and only those which
285  * can safely be cast to and from a long.  If you're unsure of whether
286  * or not it would be safe, use SWAP() or SWAP_IT() instead!
287  *
288  * @param a The first variable.
289  * @param b The second variable.
290  */
291 #if STRICT_ISO_C99
292 #  define BINSWAP(a, b)  SWAP(a, b)
293 #else
294 #  define BINSWAP(a, b)  (((long) (a)) ^= ((long) (b)) ^= ((long) (a)) ^= ((long) (b)))
295 #endif
296 
297 /**
298  * Make sure a char pointer is non-NULL before printing it.
299  *
300  * This is a convenience macro primarily targetted at systems like
301  * Solaris where doing a printf() on a NULL char pointer using %s
302  * results in a segmentation fault rather than helpful message.  This
303  * macro should be used in any place where a string is printed which
304  * could potentially be NULL.
305  *
306  * @param x A string (char *).
307  * @return @a x, or if @a x is NULL, the string "<@a x null>"
308  */
309 #define NONULL(x) (((char *) (x)) ? ((char *) (x)) : ((char *) ("<" #x " null>")))
310 
311 /**
312  * Not-A-Number
313  *
314  * This makes sure NAN is defined.
315  */
316 #ifndef NAN
317 #  ifdef MAX_FLOAT
318 #    define NAN MAX_FLOAT
319 #  elsif defined(MAXFLOAT)
320 #    define NAN MAX_FLOAT
321 #  elsif defined(HUGE)
322 #    define NAN HUGE
323 #  else
324 /* FIXME:  This could be dangerous...anyone have a better idea? */
325 #    define NAN 3.40282347e+38F
326 #  endif
327 #endif
328 
329 /****************************** DEBUGGING GOOP ********************************/
330 #ifndef LIBAST_DEBUG_FD
331 /**
332  * Where to send debugging output.
333  *
334  * This defines where debugging output should be sent.  Should be
335  * either stdout or stderr.
336  *
337  * @ingroup DOXGRP_DEBUG
338  */
339 # define LIBAST_DEBUG_FD  (stderr)
340 #endif
341 #ifndef DEBUG
342 /**
343  * Maximum compile-time debugging level.
344  *
345  * LibAST supports debugging levels, allowing for progressively more
346  * verbosity of debugging output as the level gets higher.  This
347  * defines the compile-time maximum; support for higher debugging
348  * levels than this will not even be compiled in, so use care when
349  * setting this.
350  *
351  * @ingroup DOXGRP_DEBUG
352  */
353 # define DEBUG 0
354 #endif
355 
356 /** UNDOCUMENTED. */
357 #define DEBUG_LEVEL       (libast_debug_level)
358 /** UNDOCUMENTED. */
359 #define DEBUG_FLAGS       (libast_debug_flags)
360 
361 /** Does nothing.  This macro is a nop (no operation).  It does nothing. */
362 #define NOP ((void)0)
363 
364 /**
365  * A fix-me NOP.
366  *
367  * This is the same as NOP(), but is used to mark something needing to
368  * be fixed.
369  */
370 #define FIXME_NOP(x)
371 /**
372  * Mark a block of code needing fixing.
373  *
374  * This marks a block of code needing fixing and removes it.
375  */
376 #define FIXME_BLOCK 0
377 
378 /**
379  * Mark unused blocks of code.
380  *
381  * This marks a block of code as unused and removes it.
382  */
383 #define UNUSED_BLOCK 0
384 
385 /**
386  * @def __DEBUG()
387  * Format and print debugging output.
388  *
389  * This macro formats and prints debugging output by prepending a
390  * timestamp, the filename, the line number, and (if available) the
391  * function name.
392  *
393  * This is an internal macro and should not be used directly.
394  * @ingroup DOXGRP_DEBUG
395  */
396 #if defined(__FILE__) && defined(__LINE__)
397 # ifdef __GNUC__
398 #  define __DEBUG()  fprintf(LIBAST_DEBUG_FD, "[%lu] %12s | %4d: %s(): ", (unsigned long) time(NULL), __FILE__, __LINE__, __FUNCTION__)
399 # else
400 #  define __DEBUG()  fprintf(LIBAST_DEBUG_FD, "[%lu] %12s | %4d: ", (unsigned long) time(NULL), __FILE__, __LINE__)
401 # endif
402 #else
403 # define __DEBUG()   NOP
404 #endif
405 
406 /**
407  * Assert reaching a line of code.
408  *
409  * This macro is simply a quick-and-dirty way of printing out a unique
410  * message which proves that a particular portion of code was reached
411  * and executed properly.
412  *
413  * @ingroup DOXGRP_DEBUG
414  */
415 #define MOO()  do {__DEBUG(); libast_dprintf("Moo.\n");} while (0)
416 
417 /**
418  * @def ASSERT(x)
419  * Asserts that a condition is true.
420  *
421  * This macro evaluates an expression, @a x, and takes action if the
422  * expression evaluates to false (0).  It works similarly to the libc
423  * function assert(), with the exception that it will not call abort()
424  * if the assertion fails.  Instead, it will either issue a fatal
425  * error (generally resulting in a backtrace) if debugging is active,
426  * or print a warning if it is not.  In either event, the warning/error
427  * message will contain the filename, line number, and (if available)
428  * function name where the error occured.
429  *
430  * If only a warning is generated, the function will return
431  * immediately.
432  *
433  * @param x Any valid boolean expression.
434  * @ingroup DOXGRP_DEBUG
435  */
436 /**
437  * @def ASSERT_RVAL(x, val)
438  * Asserts that a condition is true, and provides a return value in
439  * case it isn't.
440  *
441  * This macro is identical to ASSERT(), except that it returns a
442  * value, @a val, instead of returning void.
443  *
444  * @param x   Any valid boolean expression.
445  * @param val The return value to use if @a x evaluates to false.
446  * @ingroup DOXGRP_DEBUG
447  */
448 /**
449  * @def ASSERT_NOTREACHED()
450  * Asserts that a particular piece of code is not reached.
451  *
452  * This macro is used in sections of code that should never be
453  * reached.  Its actions are similar to those of ASSERT(), but instead
454  * of evaluating an expression, it always evaluates to false.
455  *
456  * @ingroup DOXGRP_DEBUG
457  */
458 /**
459  * @def ASSERT_NOTREACHED_RVAL(val)
460  * Asserts that a particular piece of code is not reached, and
461  * provides a return value in case it is.
462  *
463  * This macro is identical to ASSERT_NOTREACHED(), except that it
464  * returns a value, @a val, instead of returning void.
465  *
466  * @ingroup DOXGRP_DEBUG
467  */
468 /**
469  * @def ABORT()
470  * Throw a fatal exception.
471  *
472  * This macro is a replacement for the libc abort() function.  This
473  * version provides file/line/function information in the fatal error
474  * message.
475  *
476  * @ingroup DOXGRP_DEBUG
477  */
478 /**
479  * @def REQUIRE(x)
480  * Return if an expression is false.
481  *
482  * This macro is similar to ASSERT(), except that @a x evaluating to
483  * false is not necessarily an error.  Normally, this macro simply
484  * causes the function to return.  However, if debugging is active, a
485  * message is printed noting the expression @a x and the location of
486  * the failure.  This macro is often used to test preconditions, such
487  * as making sure pointers are non-NULL before using them.
488  *
489  * @param x Any valid boolean expression.
490  * @ingroup DOXGRP_DEBUG
491  */
492 /**
493  * @def REQUIRE_RVAL(x, v)
494  * Return @a v if an expression is false.
495  *
496  * This macro is identical to REQUIRE(), except that a return value
497  * for the function is supplied.
498  *
499  * @param x Any valid boolean expression
500  * @param v The function return value to use if @a x evaluates to
501  * false.
502  * @ingroup DOXGRP_DEBUG
503  */
504 #if DEBUG >= 1
505 # if defined(__FILE__) && defined(__LINE__)
506 #  ifdef __GNUC__
507 #   define ASSERT(x)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x);} \
508                                                     else {libast_print_warning("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x); return;}}} while (0)
509 #   define ASSERT_RVAL(x, val)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x);} \
510                                                               else {libast_print_warning("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x);} \
511                                                return (val);}} while (0)
512 #   define ASSERT_NOTREACHED()  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
513                                                    else {libast_print_warning("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
514                                     } while (0)
515 #   define ASSERT_NOTREACHED_RVAL(val)  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
516                                                            else {libast_print_warning("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
517                                             return (val);} while (0)
518 #   define ABORT() libast_fatal_error("Aborting in %s() at %s:%d.\n", __FUNCTION__, __FILE__, __LINE__)
519 #  else
520 #   define ASSERT(x)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x);} \
521                                                     else {libast_print_warning("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x); return;}}} while (0)
522 #   define ASSERT_RVAL(x, val)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x);} \
523                                                               else {libast_print_warning("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x);} \
524                                                return (val);}} while (0)
525 #   define ASSERT_NOTREACHED()  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
526                                                    else {libast_print_warning("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
527                                     } while (0)
528 #   define ASSERT_NOTREACHED_RVAL(val)  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
529                                                            else {libast_print_warning("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
530                                             return (val);} while (0)
531 #   define ABORT() libast_fatal_error("Aborting at %s:%d.\n", __FILE__, __LINE__)
532 #  endif
533 # else
534 #  define ASSERT(x)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed:  %s\n", #x);} \
535                                                    else {libast_print_warning("ASSERT failed:  %s\n", #x); return;}}} while (0)
536 #  define ASSERT_RVAL(x, val)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed:  %s\n", #x);} \
537                                                              else {libast_print_warning("ASSERT failed:  %s\n", #x);} return (val);}} while (0)
538 #  define ASSERT_NOTREACHED()           return
539 #  define ASSERT_NOTREACHED_RVAL(x)     return (x)
540 #  define ABORT()                       libast_fatal_error("Aborting.\n")
541 # endif
542 # define REQUIRE(x)                     do {if (!(x)) {if (DEBUG_LEVEL>=1) {__DEBUG(); libast_dprintf("REQUIRE failed:  %s\n", #x);} return;}} while (0)
543 # define REQUIRE_RVAL(x, v)             do {if (!(x)) {if (DEBUG_LEVEL>=1) {__DEBUG(); libast_dprintf("REQUIRE failed:  %s\n", #x);} return (v);}} while (0)
544 #else
545 # define ASSERT(x)                      NOP
546 # define ASSERT_RVAL(x, val)            NOP
547 # define ASSERT_NOTREACHED()            return
548 # define ASSERT_NOTREACHED_RVAL(val)    return (val)
549 # define ABORT()                        libast_fatal_error("Aborting.\n")
550 # define REQUIRE(x)                     do {if (!(x)) return;} while (0)
551 # define REQUIRE_RVAL(x, v)             do {if (!(x)) return (v);} while (0)
552 #endif
553 
554 /**
555  * @def DPRINTF(x)
556  * Print debugging output.
557  *
558  * This macro can be used for unconditional debugging output.  If any
559  * level of debugging support has been compiled in, this macro will
560  * print a debugging message.
561  *
562  * This macro will almost never be used directly; instead, use the
563  * D_*() macros.
564  *
565  * @attention Calls to this and other debugging output macros
566  * MUST be double-parenthesized, like so:  DPRINTF((...));
567  *
568  * @param x A parenthesized argument list suitable for a printf-style
569  *          function.
570  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
571  * @ingroup DOXGRP_DEBUG
572  */
573 /**
574  * @def DPRINTF1(x)
575  * Print level 1 debugging output.
576  *
577  * This macro is identical to DPRINTF(), except that the message will
578  * only be printed if the debug level is 1 or higher.
579  *
580  * @param x A parenthesized argument list suitable for a printf-style
581  *          function.
582  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
583  * @ingroup DOXGRP_DEBUG
584  */
585 /**
586  * @def DPRINTF2(x)
587  * Print level 2 debugging output.
588  *
589  * This macro is identical to DPRINTF(), except that the message will
590  * only be printed if the debug level is 2 or higher.
591  *
592  * @param x A parenthesized argument list suitable for a printf-style
593  *          function.
594  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
595  * @ingroup DOXGRP_DEBUG
596  */
597 /**
598  * @def DPRINTF3(x)
599  * Print level 3 debugging output.
600  *
601  * This macro is identical to DPRINTF(), except that the message will
602  * only be printed if the debug level is 3 or higher.
603  *
604  * @param x A parenthesized argument list suitable for a printf-style
605  *          function.
606  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
607  * @ingroup DOXGRP_DEBUG
608  */
609 /**
610  * @def DPRINTF4(x)
611  * Print level 4 debugging output.
612  *
613  * This macro is identical to DPRINTF(), except that the message will
614  * only be printed if the debug level is 4 or higher.
615  *
616  * @param x A parenthesized argument list suitable for a printf-style
617  *          function.
618  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
619  * @ingroup DOXGRP_DEBUG
620  */
621 /**
622  * @def DPRINTF5(x)
623  * Print level 5 debugging output.
624  *
625  * This macro is identical to DPRINTF(), except that the message will
626  * only be printed if the debug level is 5 or higher.
627  *
628  * @param x A parenthesized argument list suitable for a printf-style
629  *          function.
630  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
631  * @ingroup DOXGRP_DEBUG
632  */
633 /**
634  * @def DPRINTF6(x)
635  * Print level 6 debugging output.
636  *
637  * This macro is identical to DPRINTF(), except that the message will
638  * only be printed if the debug level is 6 or higher.
639  *
640  * @param x A parenthesized argument list suitable for a printf-style
641  *          function.
642  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
643  * @ingroup DOXGRP_DEBUG
644  */
645 /**
646  * @def DPRINTF7(x)
647  * Print level 7 debugging output.
648  *
649  * This macro is identical to DPRINTF(), except that the message will
650  * only be printed if the debug level is 7 or higher.
651  *
652  * @param x A parenthesized argument list suitable for a printf-style
653  *          function.
654  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
655  * @ingroup DOXGRP_DEBUG
656  */
657 /**
658  * @def DPRINTF8(x)
659  * Print level 8 debugging output.
660  *
661  * This macro is identical to DPRINTF(), except that the message will
662  * only be printed if the debug level is 8 or higher.
663  *
664  * @param x A parenthesized argument list suitable for a printf-style
665  *          function.
666  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
667  * @ingroup DOXGRP_DEBUG
668  */
669 /**
670  * @def DPRINTF9(x)
671  * Print level 9 debugging output.
672  *
673  * This macro is identical to DPRINTF(), except that the message will
674  * only be printed if the debug level is 9 or higher.
675  *
676  * @param x A parenthesized argument list suitable for a printf-style
677  *          function.
678  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
679  * @ingroup DOXGRP_DEBUG
680  */
681 #if DEBUG >= 1
682 # ifndef DPRINTF
683 #  define DPRINTF(x)           do { __DEBUG(); libast_dprintf x; } while (0)
684 # endif
685 # define DPRINTF1(x)           do { if (DEBUG_LEVEL >= 1) {__DEBUG(); libast_dprintf x;} } while (0)
686 # define DPRINTF2(x)           do { if (DEBUG_LEVEL >= 2) {__DEBUG(); libast_dprintf x;} } while (0)
687 # define DPRINTF3(x)           do { if (DEBUG_LEVEL >= 3) {__DEBUG(); libast_dprintf x;} } while (0)
688 # define DPRINTF4(x)           do { if (DEBUG_LEVEL >= 4) {__DEBUG(); libast_dprintf x;} } while (0)
689 # define DPRINTF5(x)           do { if (DEBUG_LEVEL >= 5) {__DEBUG(); libast_dprintf x;} } while (0)
690 # define DPRINTF6(x)           do { if (DEBUG_LEVEL >= 6) {__DEBUG(); libast_dprintf x;} } while (0)
691 # define DPRINTF7(x)           do { if (DEBUG_LEVEL >= 7) {__DEBUG(); libast_dprintf x;} } while (0)
692 # define DPRINTF8(x)           do { if (DEBUG_LEVEL >= 8) {__DEBUG(); libast_dprintf x;} } while (0)
693 # define DPRINTF9(x)           do { if (DEBUG_LEVEL >= 9) {__DEBUG(); libast_dprintf x;} } while (0)
694 #else
695 # ifndef DPRINTF
696 #  define DPRINTF(x)           NOP
697 # endif
698 # define DPRINTF1(x)           NOP
699 # define DPRINTF2(x)           NOP
700 # define DPRINTF3(x)           NOP
701 # define DPRINTF4(x)           NOP
702 # define DPRINTF5(x)           NOP
703 # define DPRINTF6(x)           NOP
704 # define DPRINTF7(x)           NOP
705 # define DPRINTF8(x)           NOP
706 # define DPRINTF9(x)           NOP
707 #endif
708 
709 /**
710  * Debugging output you (almost) never want.
711  *
712  * This macro is used for mapping debugging output you almost never
713  * want to see.  Map D_*() macros to this for overly verbose or
714  * problematic debugging information, then manually redefine this as
715  * needed.
716  *
717  * @param x A parenthesized argument list suitable for a printf-style
718  *          function.
719  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
720  * @ingroup DOXGRP_DEBUG
721  */
722 #define D_NEVER(x)             NOP
723 
724 /** Set options debugging to level 1.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
725 #define DEBUG_OPTIONS          1
726 /**
727  * Option debugging macro.
728  *
729  * This macro is used for debugging output related to the options
730  * subsystem.  It maps to DPRINTF1() so that options-related debugging
731  * output will occur at debug level 1 and higher.
732  *
733  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
734  * @ingroup DOXGRP_DEBUG
735  */
736 #if DEBUG >= DEBUG_OPTIONS
737 #  define D_OPTIONS_IF           if (DEBUG_LEVEL >= DEBUG_OPTIONS)
738 #  define D_OPTIONS(x)           do { D_OPTIONS_IF {DPRINTF(x);} } while (0)
739 #else
740 #  define D_OPTIONS_IF           if (0)
741 #  define D_OPTIONS(x)           NOP
742 #endif
743 
744 /** Set object system debugging to level 2.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
745 #define DEBUG_OBJ                2
746 /**
747  * Object debugging macro.
748  *
749  * This macro is used for debugging output related to the object
750  * subsystem.  It maps to DPRINTF2() so that object-related debugging
751  * output will occur at debug level 2 and higher.
752  *
753  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
754  * @ingroup DOXGRP_DEBUG
755  */
756 #if DEBUG >= DEBUG_OBJ
757 #  define D_OBJ_IF               if (DEBUG_LEVEL >= DEBUG_OBJ)
758 #  define D_OBJ(x)               do { D_OBJ_IF {DPRINTF(x);} } while (0)
759 #else
760 #  define D_OBJ_IF               if (0)
761 #  define D_OBJ(x)               NOP
762 #endif
763 
764 /** Set config file parser debugging to level 3.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
765 #define DEBUG_CONF               3
766 /**
767  * Config file parser debugging macro.
768  *
769  * This macro is used for debugging output related to the config file
770  * parser.  It maps to DPRINTF3() so that config-related debugging
771  * output will occur at debug level 3 and higher.
772  *
773  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
774  * @ingroup DOXGRP_DEBUG
775  */
776 #if DEBUG >= DEBUG_CONF
777 #  define D_CONF_IF              if (DEBUG_LEVEL >= DEBUG_CONF)
778 #  define D_CONF(x)              do { D_CONF_IF {DPRINTF(x);} } while (0)
779 #else
780 #  define D_CONF_IF              if (0)
781 #  define D_CONF(x)              NOP
782 #endif
783 
784 /** Set memory allocation debugging to level 5.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
785 #define DEBUG_MEM                5
786 /**
787  * Memory allocation debugging macro.
788  *
789  * This macro is used for debugging output related to the memory
790  * allocation subsystem.  It maps to DPRINTF1() so that mem-related
791  * debugging output will occur at debug level 5 and higher.
792  *
793  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
794  * @ingroup DOXGRP_DEBUG
795  */
796 #if DEBUG >= DEBUG_MEM
797 #  define D_MEM_IF               if (DEBUG_LEVEL >= DEBUG_MEM)
798 #  define D_MEM(x)               do { D_MEM_IF {DPRINTF(x);} } while (0)
799 #else
800 #  define D_MEM_IF               if (0)
801 #  define D_MEM(x)               NOP
802 #endif
803 
804 /** Set strings module debugging to level 9999.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
805 #define DEBUG_STRINGS            9999
806 /**
807  * String routine debugging macro.
808  *
809  * This macro is used for debugging output related to the string
810  * manipulation subsystem.  It maps to D_NEVER() so that
811  * string-related debugging output can only be activated manually.
812  *
813  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
814  * @ingroup DOXGRP_DEBUG
815  */
816 #if DEBUG >= DEBUG_STRINGS
817 #  define D_STRINGS_IF           if (DEBUG_LEVEL >= DEBUG_STRINGS)
818 #  define D_STRINGS(x)           do { D_STRINGS_IF {DPRINTF(x);} } while (0)
819 #else
820 #  define D_STRINGS_IF           if (0)
821 #  define D_STRINGS(x)           NOP
822 #endif
823 /** Set lexer/parser debugging to level 9999.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
824 #define DEBUG_PARSE              9999
825 /**
826  * Lexer/parser debugging macro.
827  *
828  * This macro is used for debugging output related to the lexer/parser
829  * portion of the config parser.  It maps to D_NEVER() so that
830  * parser-related debugging output can only be activated manually.
831  *
832  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
833  * @ingroup DOXGRP_DEBUG
834  */
835 #if DEBUG >= DEBUG_PARSE
836 #  define D_PARSE_IF             if (DEBUG_LEVEL >= DEBUG_PARSE)
837 #  define D_PARSE(x)             do { D_PARSE_IF {DPRINTF(x);} } while (0)
838 #else
839 #  define D_PARSE_IF             if (0)
840 #  define D_PARSE(x)             NOP
841 #endif
842 
843 
844 
845 /********************************* MEM GOOP ***********************************/
846 /**
847  * @def MALLOC(sz)
848  * Allocate @a sz bytes of memory.
849  *
850  * This macro is a replacement for the libc function malloc().  It
851  * allocates the specified number of bytes of memory on the heap and
852  * returns a pointer to that memory location.  This macro calls libc's
853  * malloc() if memory debugging is off, and spifmem_malloc() if it's
854  * on.
855  *
856  * @param sz The size in bytes of the block of memory to allocate.
857  * @return A pointer to the allocated memory.
858  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
859  * @ingroup DOXGRP_MEM
860  */
861 /**
862  * @def CALLOC(type, n)
863  * Allocate enough memory for @a n objects of type @a type.
864  *
865  * This macro is a replacement for the libc function calloc().  It
866  * allocates a block of memory on the heap large enough to hold @a n
867  * objects of type @a type (e.g., a @a type array of size @a n).  The
868  * memory area is zeroed out prior to the pointer to it being
869  * returned.  This macro calls libc's calloc() if memory debugging is
870  * off and spifmem_calloc() if it's on.
871  *
872  * @param type The type of object to be allocated (e.g., int).
873  * @param n    The number of objects to be allocated.
874  * @return A pointer to the allocated memory.
875  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
876  * @ingroup DOXGRP_MEM
877  */
878 /**
879  * @def REALLOC(mem, sz)
880  * Resize the memory block pointed to by @a mem to @a sz bytes.
881  *
882  * This macro is a replacement for the libc function realloc().  It
883  * changes the size of a chunk of memory previously allocated by
884  * malloc() or calloc() (or, by extension, the MALLOC()/CALLOC()
885  * macros) and returns a pointer to the (possibly moved) memory area.
886  * This macro calls libc's realloc() if memory debugging is off and
887  * spifmem_realloc() if it's on.
888  *
889  * @param mem The old pointer whose size will be changed.
890  * @param sz  The new size, in bytes, to be allocated.
891  * @return The new pointer value, which may or may not differ from the
892  *         old value.
893  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
894  * @ingroup DOXGRP_MEM
895  */
896 /**
897  * @def FREE(ptr)
898  * Free a previously-allocated memory block.
899  *
900  * This macro is a replacement for the libc function free().  It
901  * returns the previously-allocated memory block pointed to by @a ptr
902  * to the heap.  This macro calls libc's free() if memory debugging is
903  * off and spifmem_free() if it's on.  The @a ptr parameter is assigned
904  * the value of NULL after it has been freed.
905  *
906  * @param ptr The pointer to be freed.
907  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
908  * @ingroup DOXGRP_MEM
909  */
910 /**
911  * @def STRDUP(s)
912  * Duplicate a string pointer and return a pointer to the new copy.
913  *
914  * This macro is a replacement for the libc function strdup().  It
915  * allocates a section of memory large enough to hold the string @a s
916  * (including the trailing NUL character), copies the contents of @a s
917  * into the new buffer, and returns a pointer to the new copy.  This
918  * macro calls libc's strdup() of memory debugging is off and
919  * spifmem_strdup() if it's on.
920  *
921  * @param s The string to duplicate.
922  * @return A pointer to the newly-created copy.
923  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
924  * @ingroup DOXGRP_MEM
925  */
926 /**
927  * @def MALLOC_DUMP()
928  * Dumps a listing of all allocated pointers along with their sizes
929  * and contents in both hex and ASCII.
930  *
931  * This macro is used to view the status of memory allocated via the
932  * LibAST memory management system.  First the pointers used to track
933  * allocated memory are dumped (that's what pointer #0 is); then, each
934  * allocated pointer is dumped along with its size and contents, the
935  * latter being displayed both in hexadecimal form and ASCII form.
936  * Non-printable characters are replaced by dots ('.').  You can see
937  * a sample of the output in the
938  * @link mem_example.c memory management system example @endlink.
939  *
940  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
941  * @ingroup DOXGRP_MEM
942  */
943 /**
944  * @def X_CREATE_PIXMAP(d, win, w, h, depth)
945  * Create an X pixmap.
946  *
947  * This macro is a replacement for the Xlib function XCreatePixmap().
948  * It creates a pixmap of the specified size and returns an X resource
949  * ID for it.  This macro calls Xlib's XCreatePixmap() if memory
950  * debugging is off and spifmem_x_create_pixmap() if it's on.
951  *
952  * @param d     The X display connection.
953  * @param win   The X drawable on whose display the pixmap will be
954  *              created.
955  * @param w     The width in pixels of the pixmap.
956  * @param h     The height in pixels of the pixmap.
957  * @param depth The color depth for the pixmap.
958  * @return The Pixmap ID for the new pixmap.
959  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
960  * @ingroup DOXGRP_MEM
961  */
962 /**
963  * @def X_FREE_PIXMAP(d, p)
964  * Free the specified X pixmap.
965  *
966  * This macro is a replacement for the Xlib function XFreePixmap().
967  * It frees the specified pixmap.  This macro calls Xlib's
968  * XFreePixmap() if memory debugging is off and spifmem_x_free_pixmap()
969  * if it's on.
970  *
971  * @param d The X display connection.
972  * @param p The Pixmap to be freed.
973  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
974  * @ingroup DOXGRP_MEM
975  */
976 /**
977  * @def IMLIB_REGISTER_PIXMAP(p)
978  * Register a pixmap generated by Imlib2 so LibAST can track it.
979  *
980  * Unfortunately, there is no easy way to wrap all the different ways
981  * Imlib2 could conceivably create an image.  So instead, simply use
982  * this macro to register the pixmaps Imlib2 creates.  Then LibAST
983  * will be able to track them.  This macro calls
984  * spifmem_imlib_register_pixmap() if memory debugging is on and if
985  * Imlib2 support has been enabled.  Otherwise, it's a NOP().
986  *
987  * @param p The Pixmap Imlib2 created.
988  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
989  * @ingroup DOXGRP_MEM
990  */
991 /**
992  * @def IMLIB_FREE_PIXMAP(p)
993  * Free a pixmap (and its mask) generated by Imlib2.
994  *
995  * Once an Imlib2-generated pixmap has been registered, you should
996  * use this macro to free it.  It calls spifmem_imlib_free_pixmap() if
997  * Imlib2 support has been enabled.  Otherwise, it's a NOP().
998  *
999  * @param p The Imlib2-generated Pixmap to be freed.
1000  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1001  * @ingroup DOXGRP_MEM
1002  */
1003 /**
1004  * @def PIXMAP_DUMP()
1005  * Dump a listing of allocated pixmaps.
1006  *
1007  * This macro is analogous to the MALLOC_DUMP() macro; rather than
1008  * dumping a list of pointers, however, it dumps a list of allocated
1009  * pixmaps.  Like MALLOC_DUMP(), this macro is a NOP() if memory
1010  * debugging support has not been compiled into LibAST.
1011  *
1012  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1013  * @ingroup DOXGRP_MEM
1014  */
1015 /**
1016  * @def X_CREATE_GC(d, win, f, gcv)
1017  * Create an X graphics context.
1018  *
1019  * This macro is a replacement for the Xlib function XCreateGC().  It
1020  * creates a graphics context (GC) object and returns its X resource
1021  * ID.  This macro calls Xlib's XCreateGC() if memory debugging is
1022  * off and spifmem_x_create_gc() if it's on.
1023  *
1024  * @param d   The X display connection.
1025  * @param win The X drawable on whose screen the GC will be created.
1026  * @param f   The GC flags noting which members of @a gcv have set
1027  *            values.
1028  * @param gcv The GCValues structure defining properties of the GC.
1029  * @return The ID of the new GC.
1030  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1031  * @ingroup DOXGRP_MEM
1032  */
1033 /**
1034  * @def X_FREE_GC(d, gc)
1035  * Free an X graphics context.
1036  *
1037  * This macro is a replacement for the Xlib function XFreeGC().  It
1038  * frees a previously allocated graphics context (GC) object.  This
1039  * macro calls Xlib's XFreeGC() if memory debugging is off and
1040  * spifmem_x_free_gc() if it's on.
1041  *
1042  * @param d  The X display connection.
1043  * @param gc The graphics context object to free.
1044  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1045  * @ingroup DOXGRP_MEM
1046  */
1047 /**
1048  * @def GC_DUMP()
1049  * Dump a list of allocated graphics context objects.
1050  *
1051  * This macro is analogous to the MALLOC_DUMP() macro; rather than
1052  * dumping a list of pointers, however, it dumps a list of allocated
1053  * GC's.  Like MALLOC_DUMP(), this macro is a NOP() if memory
1054  * debugging support has not been compiled into LibAST.
1055  *
1056  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1057  * @ingroup DOXGRP_MEM
1058  */
1059 /**
1060  * @def MALLOC_MOD
1061  * MALLOC() call count interval.
1062  *
1063  * LibAST has the ability to count calls to MALLOC(); this defines the
1064  * interval for reporting the call count.  The default is 25, meaning
1065  * that LibAST will print the current count every 25 calls.  Note that
1066  * MALLOC_CALL_DEBUG must be defined when compiling LibAST, in
1067  * addition to memory debugging, for this feature to work.
1068  *
1069  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1070  * @ingroup DOXGRP_MEM
1071  */
1072 /**
1073  * @def REALLOC_MOD
1074  * REALLOC() call count interval.
1075  *
1076  * LibAST has the ability to count calls to REALLOC(); this defines
1077  * the interval for reporting the call count.  The default is 25,
1078  * meaning that LibAST will print the current count every 25 calls.
1079  * Note that MALLOC_CALL_DEBUG must be defined when compiling LibAST,
1080  * in addition to memory debugging, for this feature to work.
1081  *
1082  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1083  * @ingroup DOXGRP_MEM
1084  */
1085 /**
1086  * @def CALLOC_MOD
1087  * CALLOC() call count interval.
1088  *
1089  * LibAST has the ability to count calls to CALLOC(); this defines the
1090  * interval for reporting the call count.  The default is 25, meaning
1091  * that LibAST will print the current count every 25 calls.  Note that
1092  * MALLOC_CALL_DEBUG must be defined when compiling LibAST, in
1093  * addition to memory debugging, for this feature to work.
1094  *
1095  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1096  * @ingroup DOXGRP_MEM
1097  */
1098 /**
1099  * @def FREE_MOD
1100  * FREE() call count interval.
1101  *
1102  * LibAST has the ability to count calls to FREE(); this defines the
1103  * interval for reporting the call count.  The default is 25, meaning
1104  * that LibAST will print the current count every 25 calls.  Note that
1105  * MALLOC_CALL_DEBUG must be defined when compiling LibAST, in
1106  * addition to memory debugging, for this feature to work.
1107  *
1108  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1109  * @ingroup DOXGRP_MEM
1110  */
1111 #if (DEBUG >= DEBUG_MEM)
1112 # define MALLOC(sz)                             spifmem_malloc(SPIF_CAST(charptr) __FILE__, __LINE__, (sz))
1113 # define CALLOC(type,n)                         spifmem_calloc(SPIF_CAST(charptr) __FILE__, __LINE__, (n), (sizeof(type)))
1114 # define REALLOC(mem,sz)                        spifmem_realloc(SPIF_CAST(charptr) #mem, SPIF_CAST(charptr) __FILE__, __LINE__, (mem), (sz))
1115 # define FREE(ptr)                              do { spifmem_free(SPIF_CAST(charptr) #ptr, SPIF_CAST(charptr) __FILE__, __LINE__, (ptr)); (ptr) = NULL; } while (0)
1116 # define STRDUP(s)                              spifmem_strdup(SPIF_CAST(charptr) #s, SPIF_CAST(charptr) __FILE__, __LINE__, SPIF_CAST(charptr) (s))
1117 # define MALLOC_DUMP()                          spifmem_dump_mem_tables()
1118 # define X_CREATE_PIXMAP(d, win, w, h, depth)   spifmem_x_create_pixmap(SPIF_CAST(charptr) __FILE__, __LINE__, (d), (win), (w), (h), (depth))
1119 # define X_FREE_PIXMAP(d, p)                    spifmem_x_free_pixmap(SPIF_CAST(charptr) #p, SPIF_CAST(charptr) __FILE__, __LINE__, (d), (p))
1120 # if LIBAST_IMLIB2_SUPPORT
1121 #  define IMLIB_REGISTER_PIXMAP(p)              spifmem_imlib_register_pixmap(SPIF_CAST(charptr) #p, SPIF_CAST(charptr) __FILE__, __LINE__, (p))
1122 #  define IMLIB_FREE_PIXMAP(p)                  spifmem_imlib_free_pixmap(SPIF_CAST(charptr) #p, SPIF_CAST(charptr) __FILE__, __LINE__, (p))
1123 # else
1124 #  define IMLIB_REGISTER_PIXMAP(p)              NOP
1125 #  define IMLIB_FREE_PIXMAP(p)                  NOP
1126 # endif
1127 # define PIXMAP_DUMP()                          spifmem_dump_pixmap_tables()
1128 # define X_CREATE_GC(d, win, f, gcv)            spifmem_x_create_gc(__FILE__, __LINE__, (d), (win), (f), (gcv))
1129 # define X_FREE_GC(d, gc)                       spifmem_x_free_gc(#gc, __FILE__, __LINE__, (d), (gc))
1130 # define GC_DUMP()                              spifmem_dump_gc_tables()
1131 # define MALLOC_MOD 25
1132 # define REALLOC_MOD 25
1133 # define CALLOC_MOD 25
1134 # define FREE_MOD 25
1135 #else
1136 # define MALLOC(sz)                             malloc(sz)
1137 # define CALLOC(type,n)                         calloc((n),(sizeof(type)))
1138 # define REALLOC(mem,sz)                        ((sz) ? ((mem) ? (realloc((mem), (sz))) : (malloc(sz))) : ((mem) ? (free(mem), NULL) : (NULL)))
1139 # define FREE(ptr)                              do { free(ptr); (ptr) = NULL; } while (0)
1140 # define STRDUP(s)                              strdup((char *) s)
1141 # define MALLOC_DUMP()                          NOP
1142 # define X_CREATE_PIXMAP(d, win, w, h, depth)   XCreatePixmap((d), (win), (w), (h), (depth))
1143 # define X_FREE_PIXMAP(d, p)                    XFreePixmap((d), (p))
1144 # ifdef LIBAST_IMLIB2_SUPPORT
1145 #  define IMLIB_REGISTER_PIXMAP(p)              NOP
1146 #  define IMLIB_FREE_PIXMAP(p)                  imlib_free_pixmap_and_mask(p)
1147 # else
1148 #  define IMLIB_REGISTER_PIXMAP(p)              NOP
1149 #  define IMLIB_FREE_PIXMAP(p)                  NOP
1150 # endif
1151 # define PIXMAP_DUMP()                          NOP
1152 # define X_CREATE_GC(d, win, f, gcv)            XCreateGC((d), (win), (f), (gcv))
1153 # define X_FREE_GC(d, gc)                       XFreeGC((d), (gc))
1154 # define GC_DUMP()                              NOP
1155 #endif
1156 
1157 /* Fast memset() macro contributed by vendu */
1158 #if !defined(SIZEOF_LONG) || (SIZEOF_LONG == 8)
1159 /** UNDOCUMENTED */
1160 # define MEMSET_LONG() (l |= l<<32)
1161 #else
1162 /** UNDOCUMENTED */
1163 # define MEMSET_LONG() NOP
1164 #endif
1165 
1166 /**
1167  * @def MEMSET(s, c, count)
1168  * Initialize a memory region to a particular value.
1169  *
1170  * This macro is a replacement for the libc function memset().  It
1171  * initializes the memory region pointed to by @a s to the value
1172  * specified by @a c.  The size of the memory region is specified by
1173  * @a count.  Note that @a c must be a byte (char) value.
1174  *
1175  * This macro has been optimized to set as many bytes simultaneously as
1176  * the architecture can handle, so it should offer superior
1177  * performance to libc's memset() function.
1178  *
1179  * @param s     A pointer to the memory region to initialize.
1180  * @param c     The value to which all bytes in the block will be
1181  *              set.
1182  * @param count The size, in bytes, of the memory region.
1183  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
1184  * @ingroup DOXGRP_MEM
1185  */
1186 #define MEMSET(s, c, count) do { \
1187     char *end = (char *)(s) + (count); \
1188     long l; \
1189     long *l_dest = (long *)(s); \
1190     char *c_dest; \
1191  \
1192     if (!(s)) { \
1193         break; \
1194     } \
1195     /* areas of less than 4 * sizeof(long) are set in 1-byte chunks. */ \
1196     if (((unsigned long) count) >= 4 * sizeof(long)) { \
1197         /* fill l with c. */ \
1198         l = (c) | (c)<<8; \
1199         l |= l<<16; \
1200         MEMSET_LONG(); \
1201  \
1202         /* fill in 1-byte chunks until boundary of long is reached. */ \
1203         if ((unsigned long)l_dest & (unsigned long)(sizeof(long) -1)) { \
1204             c_dest = (char *)l_dest; \
1205             while ((unsigned long)c_dest & (unsigned long)(sizeof(long) -1)) { \
1206                 *(c_dest++) = (c); \
1207             } \
1208             l_dest = (long *)c_dest; \
1209         } \
1210  \
1211         /* fill in long-size chunks as long as possible. */ \
1212         while (((unsigned long) (end - (char *)l_dest)) >= sizeof(long)) { \
1213             *(l_dest++) = l; \
1214         } \
1215     } \
1216  \
1217     /* fill the tail in 1-byte chunks. */ \
1218     if ((char *)l_dest < end) { \
1219         c_dest = (char *)l_dest; \
1220         *(c_dest++) = (c); \
1221         while (c_dest < end) { \
1222             *(c_dest++) = (c); \
1223         } \
1224     } \
1225   } while (0)
1226 
1227 
1228 
1229 /******************************* STRINGS GOOP *********************************/
1230 /**
1231  * Returns the length of a literal string.
1232  *
1233  * This macro is like libc's strlen() function, except that it
1234  * requires the string parameter be a literal rather than a variable.
1235  * This makes calculating the string length for a literal easy without
1236  * incurring the speed penalty of a call to strlen().
1237  *
1238  * @param x The literal string (i.e., a fixed string in quotes, like
1239  *          "this.").
1240  * @return The length of the string.
1241  * @see @link DOXGRP_STRINGS String Utility Routines @endlink
1242  * @ingroup DOXGRP_STRINGS
1243  */
1244 #define CONST_STRLEN(x)            (sizeof(x) - 1)
1245 /**
1246  * Compares the beginning of a string with a literal.
1247  *
1248  * This macro, like the libc str*cmp() functions, returns an integer
1249  * less than, equal to, or greater than zero depending on if the
1250  * initial part of string @a s is found to be less than, to match, or
1251  * to be greater than the literal string.  Generally, this is used as
1252  * a boolean value (as !BEG_STRCASECMP()) to determine whether or not
1253  * @a s starts with @a constr or not.  Note that case is ignored, as
1254  * the name implies.
1255  *
1256  * @param s      The string variable to compare to.
1257  * @param constr A literal string representing what should be the
1258  *               beginning of @a s.
1259  * @return See above.
1260  * @see @link DOXGRP_STRINGS String Utility Routines @endlink
1261  * @ingroup DOXGRP_STRINGS
1262  */
1263 #define BEG_STRCASECMP(s, constr)  (strncasecmp(SPIF_CAST_C(char *) (s), constr, CONST_STRLEN(constr)))
1264 
1265 
1266 
1267 /******************************** CONF GOOP ***********************************/
1268 /**
1269  * @def PATH_MAX
1270  * The maximum length of a path specifier.
1271  *
1272  * LibAST requires PATH_MAX to be properly defined.  Unfortunately,
1273  * some UNIX versions (namely HP-UX) define it incorrectly.  Most UNIX
1274  * versions support a PATH_MAX of 1024, but all must support at least
1275  * 255.  So if PATH_MAX is defined to be less than 255 (like HP-UX and
1276  * its absolutely ludicrous value of 14), LibAST forceably redefines
1277  * it to be 255.
1278  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1279  * @ingroup DOXGRP_CONF
1280  */
1281 #if defined(PATH_MAX) && (PATH_MAX < 255)
1282 #  undef PATH_MAX
1283 #endif
1284 #ifndef PATH_MAX
1285 #  define PATH_MAX 255
1286 #endif
1287 
1288 /**
1289  * Maximum length of a line in a config file.
1290  *
1291  * At no time during parsing can any line in a config file exceed this
1292  * length (20 kB by default).
1293  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1294  * @ingroup DOXGRP_CONF
1295  */
1296 #define CONFIG_BUFF                     20480
1297 
1298 /**
1299  * Special flag character.
1300  *
1301  * This is the special character value passed to a config context
1302  * parser when the @c begin statement for that context is
1303  * encountered.
1304  *
1305  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1306  * @ingroup DOXGRP_CONF
1307  */
1308 #define SPIFCONF_BEGIN_CHAR                 '\001'
1309 /**
1310  * Special flag character string.
1311  *
1312  * This is the string representation of SPIFCONF_BEGIN_CHAR.
1313  *
1314  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1315  * @ingroup DOXGRP_CONF
1316  */
1317 #define SPIFCONF_BEGIN_STRING               "\001"
1318 /**
1319  * Special flag character.
1320  *
1321  * This is the special character value passed to a config context
1322  * parser when the @c end statement for that context is encountered.
1323  *
1324  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1325  * @ingroup DOXGRP_CONF
1326  */
1327 #define SPIFCONF_END_CHAR                   '\002'
1328 /**
1329  * Special flag character string.
1330  *
1331  * This is the string representation of SPIFCONF_END_CHAR.
1332  *
1333  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1334  * @ingroup DOXGRP_CONF
1335  */
1336 #define SPIFCONF_END_STRING                 "\002"
1337 
1338 /**
1339  * Compares boolean option value to allowed true values.
1340  *
1341  * This macro compares the value of a boolean option against the
1342  * acceptable boolean "true" values ("1", "on", "yes", and "true").
1343  *
1344  * @param s String value of a boolean option.
1345  * @return Non-zero if a match is found, zero if not.
1346  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1347  * @ingroup DOXGRP_CONF
1348  */
1349 #define BOOL_OPT_ISTRUE(s)  (!strcasecmp(SPIF_CHARPTR_C(s), true_vals[0]) \
1350                              || !strcasecmp(SPIF_CHARPTR_C(s), true_vals[1]) \
1351                              || !strcasecmp(SPIF_CHARPTR_C(s), true_vals[2]) \
1352                              || !strcasecmp(SPIF_CHARPTR_C(s), true_vals[3]))
1353 /**
1354  * Compares boolean option value to allowed false values.
1355  *
1356  * This macro compares the value of a boolean option against the
1357  * acceptable boolean "false" values ("0", "off", "no", and "false").
1358  *
1359  * @param s String value of a boolean option.
1360  * @return Non-zero if a match is found, zero if not.
1361  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1362  * @ingroup DOXGRP_CONF
1363  */
1364 #define BOOL_OPT_ISFALSE(s) (!strcasecmp(SPIF_CHARPTR_C(s), false_vals[0]) \
1365                              || !strcasecmp(SPIF_CHARPTR_C(s), false_vals[1]) \
1366                              || !strcasecmp(SPIF_CHARPTR_C(s), false_vals[2]) \
1367                              || !strcasecmp(SPIF_CHARPTR_C(s), false_vals[3]))
1368 
1369 /**
1370  * Skip-to-end flag.
1371  *
1372  * This symbol represents the bit in the FSS flags which specifies
1373  * that the parser should skip the rest of the file.
1374  *
1375  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1376  * @ingroup DOXGRP_CONF_FSS
1377  */
1378 #define FILE_SKIP_TO_END           (0x01)
1379 /**
1380  * Preprocessing flag.
1381  *
1382  * This symbol represents the bit in the FSS flags which specifies
1383  * that this file should be preprocessed.
1384  *
1385  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1386  * @ingroup DOXGRP_CONF_FSS
1387  */
1388 #define FILE_PREPROC               (0x02)
1389 /**
1390  * Push info for a new file onto the state stack.
1391  *
1392  * This macro adds a new file state structure to the top of the stack
1393  * and populates it with the information contained in the macro
1394  * parameters.  When a new file is opened for parsing, a call is made
1395  * to this macro to "push" the new file onto the top of the stack.
1396  *
1397  * @param f  The file pointer (FILE *) representing the newly-opened
1398  *           file.
1399  * @param p  The path to the newly-opened file.
1400  * @param o  The output file name (for preprocessing).
1401  * @param l  The current line number for the file.
1402  * @param fl The flag set for the file.
1403  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1404  * @ingroup DOXGRP_CONF_FSS
1405  */
1406 #define file_push(f, p, o, l, fl)  spifconf_register_fstate(f, p, o, l, fl)
1407 /**
1408  * Pop a state structure off the stack.
1409  *
1410  * This macro pops a file state structure off the top of the stack.  A
1411  * call to this macro occurs once the parsing of the current file is
1412  * completed.
1413  *
1414  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1415  * @ingroup DOXGRP_CONF_FSS
1416  */
1417 #define file_pop()                 (fstate_idx--)
1418 /**
1419  * Return the top file state structure on the stack.
1420  *
1421  * This macro is used to access the file state structure currently on
1422  * top of the stack.
1423  *
1424  * @return The file state structure atop the stack.
1425  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1426  * @ingroup DOXGRP_CONF_FSS
1427  */
1428 #define file_peek()                (fstate[fstate_idx])
1429 /**
1430  * Examine the file pointer on top of the stack.
1431  *
1432  * This macro returns the file pointer (FILE *) corresponding to the
1433  * file currently being parsed.
1434  *
1435  * @return The current file pointer.
1436  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1437  * @ingroup DOXGRP_CONF_FSS
1438  */
1439 #define file_peek_fp()             (fstate[fstate_idx].fp)
1440 /**
1441  * Examine the path of the current file.
1442  *
1443  * This macro returns the path for the file currently being parsed.
1444  *
1445  * @return The path of the current file.
1446  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1447  * @ingroup DOXGRP_CONF_FSS
1448  */
1449 #define file_peek_path()           (fstate[fstate_idx].path)
1450 /**
1451  * Examine the path of the current pre-processing output file.
1452  *
1453  * This macro returns the path for the preprocessing output file
1454  * currently being parsed.
1455  *
1456  * @return The path of the current preproc output file.
1457  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1458  * @ingroup DOXGRP_CONF_FSS
1459  */
1460 #define file_peek_outfile()        (fstate[fstate_idx].outfile)
1461 /**
1462  * Examine the line number of the current file.
1463  *
1464  * This macro returns the current line number within the current
1465  * config file.
1466  *
1467  * @return The line number of the current file.
1468  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1469  * @ingroup DOXGRP_CONF_FSS
1470  */
1471 #define file_peek_line()           (fstate[fstate_idx].line)
1472 /**
1473  * Check whether or not we're skipping to the end of the current
1474  * file.
1475  *
1476  * This macro returns zero if the current file is being parsed and
1477  * non-zero if the parser is skipping to its end.
1478  *
1479  * @return The skip-to-end flag for the current file.
1480  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1481  * @ingroup DOXGRP_CONF_FSS
1482  */
1483 #define file_peek_skip()           (fstate[fstate_idx].flags & FILE_SKIP_TO_END)
1484 /**
1485  * Check whether or not the current file was preprocessed.
1486  *
1487  * This macro returns zero if the current file was not preprocessed
1488  * and non-zero if it was.
1489  *
1490  * @return The preprocessing flag for the current file.
1491  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1492  * @ingroup DOXGRP_CONF_FSS
1493  */
1494 #define file_peek_preproc()        (fstate[fstate_idx].flags & FILE_PREPROC)
1495 
1496 /**
1497  * Set the file pointer for the current file.
1498  *
1499  * @internal
1500  * @param f The file pointer (FILE *).
1501  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1502  * @ingroup DOXGRP_CONF_FSS
1503  */
1504 #define file_poke_fp(f)            ((fstate[fstate_idx].fp) = (f))
1505 /**
1506  * Set the path for the current file.
1507  *
1508  * @internal
1509  * @param p The path.
1510  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1511  * @ingroup DOXGRP_CONF_FSS
1512  */
1513 #define file_poke_path(p)          ((fstate[fstate_idx].path) = (p))
1514 /**
1515  * Set the outfile for the current file.
1516  *
1517  * @internal
1518  * @param o The outfile.
1519  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1520  * @ingroup DOXGRP_CONF_FSS
1521  */
1522 #define file_poke_outfile(o)       ((fstate[fstate_idx].outfile) = (o))
1523 /**
1524  * Set the current line number for the current file.
1525  *
1526  * @internal
1527  * @param l The line number.
1528  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1529  * @ingroup DOXGRP_CONF_FSS
1530  */
1531 #define file_poke_line(l)          ((fstate[fstate_idx].line) = (l))
1532 /**
1533  * Set the skip-to-end flag for the current file.
1534  *
1535  * @internal
1536  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1537  * @ingroup DOXGRP_CONF_FSS
1538  */
1539 #define file_skip_to_end()         ((fstate[fstate_idx].flags) |= (FILE_SKIP_TO_END))
1540 /**
1541  * Set/clear the skip-to-end flag for the current file.
1542  *
1543  * @internal
1544  * @param s 0 to clear, non-zero to set.
1545  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1546  * @ingroup DOXGRP_CONF_FSS
1547  */
1548 #define file_poke_skip(s)          do {if (s) {fstate[fstate_idx].flags |= FILE_SKIP_TO_END;} else {fstate[fstate_idx].flags &= ~(FILE_SKIP_TO_END);} } while (0)
1549 /**
1550  * Set/clear the preprocessing flag for the current file.
1551  *
1552  * @internal
1553  * @param s 0 to clear, non-zero to set.
1554  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1555  * @ingroup DOXGRP_CONF_FSS
1556  */
1557 #define file_poke_preproc(s)       do {if (s) {fstate[fstate_idx].flags |= FILE_PREPROC;} else {fstate[fstate_idx].flags &= ~(FILE_PREPROC);} } while (0)
1558 /**
1559  * Set all state info for the current file.
1560  *
1561  * @internal
1562  * @param f  The file pointer (FILE *).
1563  * @param p  The file path.
1564  * @param o  The outfile.
1565  * @param l  The line number.
1566  * @param fl The flags.
1567  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1568  * @ingroup DOXGRP_CONF_FSS
1569  */
1570 #define file_poke(f, p, o, l, fl)  do {file_poke_fp(f); file_poke_path(p); file_poke_outfile(o); file_poke_line(l); fstate[fstate_idx].flags = (fl);} while (0)
1571 
1572 /**
1573  * Increment the line number for the current file.
1574  *
1575  * @internal
1576  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1577  * @ingroup DOXGRP_CONF_FSS
1578  */
1579 #define file_inc_line()            (fstate[fstate_idx].line++)
1580 
1581 /**
1582  * File state stack structure.
1583  *
1584  * This structure comprises the individual stack elements on the file
1585  * state stack.  One of these structures is present on the stack for
1586  * each file being parsed.
1587  *
1588  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
1589  * @ingroup DOXGRP_CONF_FSS
1590  */
1591 typedef struct file_state_struct {
1592     /**
1593      * File pointer.
1594      *
1595      * Contains an open file pointer used to read data from the
1596      * file.
1597      */
1598     FILE *fp;
1599     /**
1600      * File path.
1601      *
1602      * Contains the path to the file.
1603      */
1604     spif_charptr_t path;
1605     /**
1606      * Preprocessing output file.
1607      *
1608      * Contains the path to the file used for preprocessing
1609      * output.
1610      */
1611     spif_charptr_t outfile;
1612     /**
1613      * Line number.
1614      *
1615      * Contains the current line number for the file.
1616      */
1617     spif_uint32_t line;
1618     /**
1619      * File state flags.
1620      *
1621      * Contains the skip-to-end (FILE_SKIP_TO_END) and preprocessing
1622      * (FILE_PREPROC) flags for the file.
1623      */
1624     spif_uint8_t flags;
1625 } fstate_t;
1626 
1627 /**
1628  * Typedef for pointers to context handler functions.
1629  *
1630  * This function pointer type is used for variables, typecasts,
1631  * etc. involving context handler functions.  Context handlers must
1632  * accept two parameters, a char * containing either the config file
1633  * line or a begin/end magic string, and a void * containing state
1634  * information; they must return a void * which will be passed to the
1635  * next invocation of the handler as the aforementioned state
1636  * information parameter.
1637  *
1638  * @see @link DOXGRP_CONF_CTX Context Handling @endlink
1639  * @ingroup DOXGRP_CONF_CTX
1640  */
1641 typedef spif_ptr_t (*ctx_handler_t)(spif_charptr_t, spif_ptr_t);
1642 /**
1643  * Typedef for pointers to built-in functions.
1644  *
1645  * This function pointer type is used for config file built-in
1646  * function handlers.  LibAST supplies several built-in functions
1647  * which can be used in config files (%get(), %appname(), etc.);
1648  * client programs can add their own as well.  Built-in functions take
1649  * a single char * parameter, the parameter list passed to the
1650  * built-in function in the config file.  They return a char *, the
1651  * result string to substitute for the function call.
1652  *
1653  * @see @link DOXGRP_CONF Configuration File Parser @endlink
1654  * @ingroup DOXGRP_CONF
1655  */
1656 typedef spif_charptr_t (*spifconf_func_ptr_t) (spif_charptr_t);
1657 
1658 extern fstate_t *fstate;
1659 extern unsigned char fstate_idx;
1660 extern const char *true_vals[], *false_vals[];
1661 
1662 
1663 /******************************* OPTIONS GOOP **********************************/
1664 
1665 /*@{*/
1666 /**
1667  * @name Option Flags
1668  * Flags for individual options.
1669  *
1670  * Each option structure (spifopt_t_struct) has a 16-bit value called
1671  * "flags" associated with it.  The lowest 5 bits (0-4) are for option
1672  * types which do not require a specific value (such as boolean).  The
1673  * next 5 bits (5-9) are for option types which always require a
1674  * value.  Bit 10 is for abstract options, which could by definition
1675  * go either way.  And the final 5 bits (11-15) are for
1676  * non-type-related information, such as flagging preparsed or
1677  * deprecated options.
1678  *
1679  * @ingroup DOXGRP_OPT
1680  */
1681 
1682 /** No flags.  No flags. */
1683 #define SPIFOPT_FLAG_NONE                 (0)
1684 /** Boolean option.  This flag marks a boolean option. */
1685 #define SPIFOPT_FLAG_BOOLEAN              (1UL << 0)
1686 /** Counter option.  This flag marks a counter option. */
1687 #define SPIFOPT_FLAG_COUNTER              (1UL << 1)
1688 /** No-value type mask.  This is a bitmask to select the lower 5 bits
1689  *  (those which represent options where no value is required).
1690  */
1691 #define SPIFOPT_FLAG_TYPEMASK_NOVALUE     (0x001f)
1692 /** Integer option.  This flag marks an integer (numeric) option. */
1693 #define SPIFOPT_FLAG_INTEGER              (1UL << 5)
1694 /** String option.  This flag marks a string option. */
1695 #define SPIFOPT_FLAG_STRING               (1UL << 6)
1696 /** Argument list option.  This flag marks an argument list option
1697  *  (such as -e/--exec).  There can be one of these at most.
1698  */
1699 #define SPIFOPT_FLAG_ARGLIST              (1UL << 7)
1700 /** Value type mask.  This is a bitmask to select bits 5-9
1701  *  (those which represent options where a value is required).
1702  */
1703 #define SPIFOPT_FLAG_TYPEMASK_VALUE       (0x03e0)
1704 /** Abstract option.  This flag marks an abstract (client-handled)
1705  *  option.
1706  */
1707 #define SPIFOPT_FLAG_ABSTRACT             (1UL << 10)
1708 /** Type mask.  This is a bitmask to select all type-identifying
1709  *  option flag bits (0-10, inclusive).
1710  */
1711 #define SPIFOPT_FLAG_TYPEMASK             (0x07ff)
1712 /**
1713  * Preparsed option.  This flag marks an option which is preparsed
1714  * (i.e., parsed only on the pre-parse pass, which is generally done
1715  * before config files are read).
1716  */
1717 #define SPIFOPT_FLAG_PREPARSE             (1UL << 11)
1718 /**
1719  * Deprecated option.  This flag marks an option which has been
1720  * deprecated by the author(s) of the program.  A warning message is
1721  * printed whenever a deprecated option is encountered.
1722  */
1723 #define SPIFOPT_FLAG_DEPRECATED           (1UL << 12)
1724 /**
1725  * Array option.  This flag marks an option which, rather than taking
1726  * a single value of its type, takes multiple values of its type.
1727  * LibAST will allocate and return a NULL-terminated C-style array of
1728  * the given type.  The absence of this flag, except for abstract
1729  * options, means that multiple instances of a given flag will
1730  * overwrite previous values, if any.  Abstract options are entirely
1731  * client-handled, so this flag has no meaning there.
1732  */
1733 #define SPIFOPT_FLAG_ARRAY                (1UL << 13)
1734 /** Modifier mask.  This is a bitmask to select all the non-type
1735  *  flags; i.e., those which modify option behavior.
1736  */
1737 #define SPIFOPT_FLAG_MODMASK              (0xf800)
1738 /*@}*/
1739 
1740 /*@{*/
1741 /**
1742  * @name Parser Settings
1743  * Flags which alter the behavior of the parser itself.
1744  *
1745  * The option parser settings structure (spifopt_settings_t_struct)
1746  * has an 8-bit flag field which contains toggles affecting the
1747  * parser's internal behavior.  As a general rule, these will not be
1748  * flags that client programs will want to manipulate.  In the event
1749  * that you do wish to manipulate these flags, use the
1750  * SPIFOPT_FLAGS_*() macros.
1751  *
1752  * @ingroup DOXGRP_OPT
1753  */
1754 
1755 /**
1756  * Preparse flag.  This flag denotes whether or not the next call to
1757  * the spifopt_parse() function will parse only those options which
1758  * have the SPIFOPT_FLAG_PREPARSE flag set, after which it will clear
1759  * this flag.  Callers wishing to have certain options pre-parsed
1760  * should set this flag prior to invoking spifopt_parse().  Use of
1761  * this flag is not required.
1762  */
1763 #define SPIFOPT_SETTING_PREPARSE         (1UL << 0)
1764 /*@}*/
1765 
1766 /*@{*/
1767 /**
1768  * @name Option Declaration Convenience Macros
1769  * Macros which simplify the building of the options list.
1770  *
1771  * Each client program which intends to use the LibAST option parser
1772  * (i.e., calls spifopt_parse() one or more times) is responsible for
1773  * building its own option structure (spifopt_t) array which is then
1774  * registered with LibAST using the SPIFOPT_OPTLIST_SET() macro.
1775  *
1776  * To simplify the declaration of this structure, a set of convenience
1777  * macros is provided that will make the structure more
1778  * human-readable.  Although their use is not required, it is
1779  * recommended in order to enhance readability and reduce the
1780  * probability of human error.
1781  *
1782  * @ingroup DOXGRP_OPT
1783  */
1784 
1785 /**
1786  * Primary option declaration macro.
1787  *
1788  * This is the primary macro used for declaring an option.  All other
1789  * option declaration convenience macros are merely simpler forms of
1790  * this macro.  If you prefer, you can use this macro for all option
1791  * declarations; just be sure to get the parameters right.
1792  *
1793  * @param s The short form as a char, or 0 for none.
1794  * @param l The long form as a char *. (required)
1795  * @param d The description as a char *. (required)
1796  * @param f Bitwise OR of zero or more flags.
1797  * @param p The pointer to where the data should be stored.  Except in
1798  *          the case of function pointers (for abstract options), this
1799  *          parameter will need the & operator.  See the other
1800  *          convenience macros for type requirements.
1801  * @param m The bitmask for a boolean option, or 0 for other types.
1802  */
1803 #define SPIFOPT_OPTION(s, l, d, f, p, m)  { s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), f, p, m }
1804 /**
1805  * Declare a boolean option.
1806  *
1807  * This macro is used to declare a simple boolean option with both a
1808  * short and a long form.
1809  *
1810  * @param s The short form as a char, or 0 for none.
1811  * @param l The long form as a char *. (required)
1812  * @param d The description as a char *. (required)
1813  * @param v A variable of type "unsigned long" (or one that can be
1814  *          safely typecast to/from it) to be used as a bitfield.
1815  * @param m The bitmask to be set/unset within the bitfield.
1816  * @see SPIFOPT_OPTION()
1817  */
1818 #define SPIFOPT_BOOL(s, l, d, v, m) \
1819     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN), &(v), m)
1820 /**
1821  * Declare a pre-parsed boolean option.
1822  *
1823  * This macro is used to declare a boolean option with both a short
1824  * and a long form which will be pre-parsed.
1825  *
1826  * @param s The short form as a char, or 0 for none.
1827  * @param l The long form as a char *. (required)
1828  * @param d The description as a char *. (required)
1829  * @param v A variable of type "unsigned long" (or one that can be
1830  *          safely typecast to/from it) to be used as a bitfield.
1831  * @param m The bitmask to be set/unset within the bitfield.
1832  * @see SPIFOPT_OPTION()
1833  */
1834 #define SPIFOPT_BOOL_PP(s, l, d, v, m) \
1835     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN | SPIFOPT_FLAG_PREPARSE), &(v), m)
1836 /**
1837  * Declare a long-only boolean option.
1838  *
1839  * This macro is used to declare a boolean option with only a long
1840  * form.
1841  *
1842  * @param l The long form as a char *. (required)
1843  * @param d The description as a char *. (required)
1844  * @param v A variable of type "unsigned long" (or one that can be
1845  *          safely typecast to/from it) to be used as a bitfield.
1846  * @param m The bitmask to be set/unset within the bitfield.
1847  * @see SPIFOPT_OPTION()
1848  */
1849 #define SPIFOPT_BOOL_LONG(l, d, v, m) \
1850     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN), &(v), m)
1851 /**
1852  * Declare a long-only, pre-parsed boolean option.
1853  *
1854  * This macro is used to declare a boolean option with only a long
1855  * form which will be pre-parsed.
1856  *
1857  * @param l The long form as a char *. (required)
1858  * @param d The description as a char *. (required)
1859  * @param v A variable of type "unsigned long" (or one that can be
1860  *          safely typecast to/from it) to be used as a bitfield.
1861  * @param m The bitmask to be set/unset within the bitfield.
1862  * @see SPIFOPT_OPTION()
1863  */
1864 #define SPIFOPT_BOOL_LONG_PP(l, d, v, m) \
1865     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN | SPIFOPT_FLAG_PREPARSE), &(v), m)
1866 /**
1867  * Declare an integer option.
1868  *
1869  * This macro is used to declare a simple integer option with both a
1870  * short and a long form.
1871  *
1872  * @param s The short form as a char, or 0 for none.
1873  * @param l The long form as a char *. (required)
1874  * @param d The description as a char *. (required)
1875  * @param v A variable of type "int" (or one that can be
1876  *          safely typecast to/from it) to store the option value.
1877  * @see SPIFOPT_OPTION()
1878  */
1879 #define SPIFOPT_INT(s, l, d, v) \
1880     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER), &(v), 0)
1881 /**
1882  * Declare a pre-parsed integer option.
1883  *
1884  * This macro is used to declare an integer option with both a short
1885  * and a long form which will be pre-parsed.
1886  *
1887  * @param s The short form as a char, or 0 for none.
1888  * @param l The long form as a char *. (required)
1889  * @param d The description as a char *. (required)
1890  * @param v A variable of type "int" (or one that can be
1891  *          safely typecast to/from it) to store the option value.
1892  * @see SPIFOPT_OPTION()
1893  */
1894 #define SPIFOPT_INT_PP(s, l, d, v) \
1895     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER | SPIFOPT_FLAG_PREPARSE), &(v), 0)
1896 /**
1897  * Declare a long-only integer option.
1898  *
1899  * This macro is used to declare an integer option with only a long
1900  * form.
1901  *
1902  * @param l The long form as a char *. (required)
1903  * @param d The description as a char *. (required)
1904  * @param v A variable of type "int" (or one that can be
1905  *          safely typecast to/from it) to store the option value.
1906  * @see SPIFOPT_OPTION()
1907  */
1908 #define SPIFOPT_INT_LONG(l, d, v) \
1909     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER), &(v), 0)
1910 /**
1911  * Declare a long-only, pre-parsed integer option.
1912  *
1913  * This macro is used to declare an integer option with only a long
1914  * form which will be pre-parsed.
1915  *
1916  * @param l The long form as a char *. (required)
1917  * @param d The description as a char *. (required)
1918  * @param v A variable of type "int" (or one that can be
1919  *          safely typecast to/from it) to store the option value.
1920  * @see SPIFOPT_OPTION()
1921  */
1922 #define SPIFOPT_INT_LONG_PP(l, d, v) \
1923     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER | SPIFOPT_FLAG_PREPARSE), &(v), 0)
1924 /**
1925  * Declare a string option.
1926  *
1927  * This macro is used to declare a simple string option with both a
1928  * short and a long form.
1929  *
1930  * @param s The short form as a char, or 0 for none.
1931  * @param l The long form as a char *. (required)
1932  * @param d The description as a char *. (required)
1933  * @param v A variable of type "const char *" (or one that can be
1934  *          safely typecast to/from it) to store the option value.
1935  * @see SPIFOPT_OPTION()
1936  */
1937 #define SPIFOPT_STR(s, l, d, v) \
1938     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING), &(v), 0)
1939 /**
1940  * Declare a pre-parsed string option.
1941  *
1942  * This macro is used to declare a string option with both a short and
1943  * a long form which will be pre-parsed.
1944  *
1945  * @param s The short form as a char, or 0 for none.
1946  * @param l The long form as a char *. (required)
1947  * @param d The description as a char *. (required)
1948  * @param v A variable of type "const char *" (or one that can be
1949  *          safely typecast to/from it) to store the option value.
1950  * @see SPIFOPT_OPTION()
1951  */
1952 #define SPIFOPT_STR_PP(s, l, d, v) \
1953     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING | SPIFOPT_FLAG_PREPARSE), &(v), 0)
1954 /**
1955  * Declare a long-only string option.
1956  *
1957  * This macro is used to declare a string option with only a long
1958  * form.
1959  *
1960  * @param l The long form as a char *. (required)
1961  * @param d The description as a char *. (required)
1962  * @param v A variable of type "const char *" (or one that can be
1963  *          safely typecast to/from it) to store the option value.
1964  * @see SPIFOPT_OPTION()
1965  */
1966 #define SPIFOPT_STR_LONG(l, d, v) \
1967     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING), &(v), 0)
1968 /**
1969  * Declare a long-only, pre-parsed string option.
1970  *
1971  * This macro is used to declare a string option with only a long form
1972  * which will be pre-parsed.
1973  *
1974  * @param l The long form as a char *. (required)
1975  * @param d The description as a char *. (required)
1976  * @param v A variable of type "const char *" (or one that can be
1977  *          safely typecast to/from it) to store the option value.
1978  * @see SPIFOPT_OPTION()
1979  */
1980 #define SPIFOPT_STR_LONG_PP(l, d, v) \
1981     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING | SPIFOPT_FLAG_PREPARSE), &(v), 0)
1982 /**
1983  * Declare an argument list option.
1984  *
1985  * This macro is used to declare a simple argument list option with
1986  * both a short and a long form.
1987  *
1988  * @note Due to the nature of this option type and the fact that it
1989  *       can consume values to the end of the command line, only one
1990  *       option of this type can be declared.
1991  *
1992  * @param s The short form as a char, or 0 for none.
1993  * @param l The long form as a char *. (required)
1994  * @param d The description as a char *. (required)
1995  * @param p A pointer of type "char **" (or one that can be
1996  *          safely typecast to/from it) to store the NULL-terminated
1997  *          argument list.
1998  * @see SPIFOPT_OPTION()
1999  */
2000 #define SPIFOPT_ARGS(s, l, d, p) \
2001     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST), &(p), 0)
2002 /**
2003  * Declare a pre-parsed argument list option.
2004  *
2005  * This macro is used to declare an argument list option with both a
2006  * short and a long form which will be pre-parsed.
2007  *
2008  * @note Due to the nature of this option type and the fact that it
2009  *       can consume values to the end of the command line, only one
2010  *       option of this type can be declared.
2011  *
2012  * @param s The short form as a char, or 0 for none.
2013  * @param l The long form as a char *. (required)
2014  * @param d The description as a char *. (required)
2015  * @param p A pointer of type "char **" (or one that can be
2016  *          safely typecast to/from it) to store the NULL-terminated
2017  *          argument list.
2018  * @see SPIFOPT_OPTION()
2019  */
2020 #define SPIFOPT_ARGS_PP(s, l, d, p) \
2021     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST | SPIFOPT_FLAG_PREPARSE), &(p), 0)
2022 /**
2023  * Declare a long-only argument list option.
2024  *
2025  * This macro is used to declare an argument list option with only a
2026  * long form.
2027  *
2028  * @note Due to the nature of this option type and the fact that it
2029  *       can consume values to the end of the command line, only one
2030  *       option of this type can be declared.
2031  *
2032  * @param l The long form as a char *. (required)
2033  * @param d The description as a char *. (required)
2034  * @param p A pointer of type "char **" (or one that can be
2035  *          safely typecast to/from it) to store the NULL-terminated
2036  *          argument list.
2037  * @see SPIFOPT_OPTION()
2038  */
2039 #define SPIFOPT_ARGS_LONG(l, d, p) \
2040     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST), &(p), 0)
2041 /**
2042  * Declare a long-only, pre-parsed argument list option.
2043  *
2044  * This macro is used to declare an argument list option with only a
2045  * long form which will be pre-parsed.
2046  *
2047  * @note Due to the nature of this option type and the fact that it
2048  *       can consume values to the end of the command line, only one
2049  *       option of this type can be declared.
2050  *
2051  * @param l The long form as a char *. (required)
2052  * @param d The description as a char *. (required)
2053  * @param p A pointer of type "char **" (or one that can be
2054  *          safely typecast to/from it) to store the NULL-terminated
2055  *          argument list.
2056  * @see SPIFOPT_OPTION()
2057  */
2058 #define SPIFOPT_ARGS_LONG_PP(l, d, p) \
2059     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST | SPIFOPT_FLAG_PREPARSE), &(p), 0)
2060 /**
2061  * Declare an abstract option.
2062  *
2063  * This macro is used to declare a simple abstract option with both a
2064  * short and a long form.
2065  *
2066  * @param s The short form as a char, or 0 for none.
2067  * @param l The long form as a char *. (required)
2068  * @param d The description as a char *. (required)
2069  * @param f A function pointer of type "spifopt_abstract_handler_t"
2070  *          (or one that can be safely typecast to/from it) to handle
2071  *          the parsing of the option.
2072  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
2073  */
2074 #define SPIFOPT_ABST(s, l, d, f) \
2075     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT), SPIF_CAST(ptr) f, 0)
2076 /**
2077  * Declare a pre-parsed abstract option.
2078  *
2079  * This macro is used to declare an abstract option with both a short
2080  * and a long form which will be pre-parsed.
2081  *
2082  * @param s The short form as a char, or 0 for none.
2083  * @param l The long form as a char *. (required)
2084  * @param d The description as a char *. (required)
2085  * @param f A function pointer of type "spifopt_abstract_handler_t"
2086  *          (or one that can be safely typecast to/from it) to handle
2087  *          the parsing of the option.
2088  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
2089  */
2090 #define SPIFOPT_ABST_PP(s, l, d, f) \
2091     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT | SPIFOPT_FLAG_PREPARSE), SPIF_CAST(ptr) f, 0)
2092 /**
2093  * Declare a long-only abstract option.
2094  *
2095  * This macro is used to declare an abstract option with only a long
2096  * form.
2097  *
2098  * @param l The long form as a char *. (required)
2099  * @param d The description as a char *. (required)
2100  * @param f A function pointer of type "spifopt_abstract_handler_t"
2101  *          (or one that can be safely typecast to/from it) to handle
2102  *          the parsing of the option.
2103  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
2104  */
2105 #define SPIFOPT_ABST_LONG(l, d, f) \
2106     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT), SPIF_CAST(ptr) f, 0)
2107 /**
2108  * Declare a long-only, pre-parsed abstract option.
2109  *
2110  * This macro is used to declare an abstract option with only a long
2111  * form which will be pre-parsed.
2112  *
2113  * @param l The long form as a char *. (required)
2114  * @param d The description as a char *. (required)
2115  * @param f A function pointer of type "spifopt_abstract_handler_t"
2116  *          (or one that can be safely typecast to/from it) to handle
2117  *          the parsing of the option.
2118  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
2119  */
2120 #define SPIFOPT_ABST_LONG_PP(l, d, f) \
2121     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT | SPIFOPT_FLAG_PREPARSE), SPIF_CAST(ptr) f, 0)
2122 /*@}*/
2123 
2124 /*@{*/
2125 /**
2126  * @name Option Flag Macros
2127  * Macros which provide access to option flag information.
2128  *
2129  * These macros provide access to option type and flag information
2130  * while abstracting the actual method of storage and retrieval.  Each
2131  * macro takes exactly one parameter, the offset within the options
2132  * list.
2133  *
2134  * @ingroup DOXGRP_OPT
2135  */
2136 
2137 /**
2138  * Type macro.
2139  *
2140  * Retrieves the type flag.  Use of this macro is discouraged; use one
2141  * of the other macros instead.
2142  *
2143  * @param n The index of the desired option in the options list.
2144  * @return  The type flag (SPIFOPT_FLAG_*)
2145  */
2146 #define SPIFOPT_OPT_TYPE(n)               (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_TYPEMASK)
2147 /**
2148  * Boolean option test.  Tests whether or not the given option is boolean.
2149  *
2150  * @param n The index of the desired option in the options list.
2151  * @return  0 if option is not boolean, non-zero if it is.
2152  */
2153 #define SPIFOPT_OPT_IS_BOOLEAN(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_BOOLEAN)
2154 /**
2155  * Counter option test.  Tests whether or not the given option is a counter.
2156  *
2157  * @param n The index of the desired option in the options list.
2158  * @return  0 if option is not a counter, non-zero if it is.
2159  */
2160 #define SPIFOPT_OPT_IS_COUNTER(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_COUNTER)
2161 /**
2162  * Integer option test.  Tests whether or not the given option is an integer.
2163  *
2164  * @param n The index of the desired option in the options list.
2165  * @return  0 if option is not an integer, non-zero if it is.
2166  */
2167 #define SPIFOPT_OPT_IS_INTEGER(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_INTEGER)
2168 /**
2169  * String option test.  Tests whether or not the given option is a string.
2170  *
2171  * @param n The index of the desired option in the options list.
2172  * @return  0 if option is not a string, non-zero if it is.
2173  */
2174 #define SPIFOPT_OPT_IS_STRING(n)          (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_STRING)
2175 /**
2176  * Argument list option test.  Tests whether or not the given option
2177  * is an argument list.
2178  *
2179  * @param n The index of the desired option in the options list.
2180  * @return  0 if option is not an argument list, non-zero if it is.
2181  */
2182 #define SPIFOPT_OPT_IS_ARGLIST(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_ARGLIST)
2183 /**
2184  * Abstract option test.  Tests whether or not the given option is abstract.
2185  *
2186  * @param n The index of the desired option in the options list.
2187  * @return  0 if option is not abstract, non-zero if it is.
2188  */
2189 #define SPIFOPT_OPT_IS_ABSTRACT(n)        (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_ABSTRACT)
2190 /**
2191  * Preparse option test.  Tests whether or not the given option is to
2192  * be preparsed.
2193  *
2194  * @param n The index of the desired option in the options list.
2195  * @return  0 if option is not to be pre-parsed, non-zero if it is.
2196  */
2197 #define SPIFOPT_OPT_IS_PREPARSE(n)        (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_PREPARSE)
2198 /**
2199  * Deprecated option test.  Tests whether or not the given option is deprecated.
2200  *
2201  * @param n The index of the desired option in the options list.
2202  * @return  0 if option is not deprecated, non-zero if it is.
2203  */
2204 #define SPIFOPT_OPT_IS_DEPRECATED(n)      (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_DEPRECATED)
2205 /**
2206  * Array option test.  Tests whether or not the given option is an array.
2207  *
2208  * @param n The index of the desired option in the options list.
2209  * @return  0 if option is not an array, non-zero if it is.
2210  */
2211 #define SPIFOPT_OPT_IS_ARRAY(n)           (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_ARRAY)
2212 /**
2213  * Value required option test.  Tests whether or not the given option
2214  * requires a value.
2215  *
2216  * @param n The index of the desired option in the options list.
2217  * @return  0 if option does not require a value, non-zero if it does.
2218  */
2219 #define SPIFOPT_OPT_NEEDS_VALUE(n)        (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_TYPEMASK_VALUE)
2220 /*@}*/
2221 
2222 /*@{*/
2223 /**
2224  * @name Option Structure Access Macros
2225  * Macros which provide access to individual structure components.
2226  *
2227  * These macros provide an easy method for accessing the various
2228  * members of a single option structure (spifopt_t_struct) within the
2229  * options list without needing to know how the options list, or the
2230  * options within it, are stored.
2231  *
2232  * @ingroup DOXGRP_OPT
2233  */
2234 
2235 /**
2236  * Short form.  Returns the short form of the option.
2237  *
2238  * @param n The index of the desired option in the options list.
2239  * @return  The short (char) form of the option.
2240  */
2241 #define SPIFOPT_OPT_SHORT(n)              (SPIFOPT_OPTLIST_GET_OPT(n).short_opt)
2242 /**
2243  * Long form.  Returns the long form of the option.
2244  *
2245  * @param n The index of the desired option in the options list.
2246  * @return  The long (const char *) form of the option.
2247  */
2248 #define SPIFOPT_OPT_LONG(n)               (SPIFOPT_OPTLIST_GET_OPT(n).long_opt)
2249 /**
2250  * Description.  Returns the description of the option.
2251  *
2252  * @param n The index of the desired option in the options list.
2253  * @return  The description (const char *) of the option.
2254  */
2255 #define SPIFOPT_OPT_DESC(n)               (SPIFOPT_OPTLIST_GET_OPT(n).desc)
2256 /**
2257  * Flags.  Returns the flags for the option.
2258  *
2259  * @param n The index of the desired option in the options list.
2260  * @return  The flags (spif_uint16_t) for the option.
2261  */
2262 #define SPIFOPT_OPT_FLAGS(n)              (SPIFOPT_OPTLIST_GET_OPT(n).flags)
2263 /**
2264  * Value pointer.  Returns the value pointer for the option.
2265  *
2266  * @param n The index of the desired option in the options list.
2267  * @return  The value pointer (void *) for the option.
2268  */
2269 #define SPIFOPT_OPT_VALUE(n)              (SPIFOPT_OPTLIST_GET_OPT(n).value)
2270 /**
2271  * Bitmask.  Returns the bitmask for the option.
2272  *
2273  * @param n The index of the desired option in the options list.
2274  * @return  The bitmask (spif_uint32_t) for the option.
2275  */
2276 #define SPIFOPT_OPT_MASK(n)               (SPIFOPT_OPTLIST_GET_OPT(n).mask)
2277 /*@}*/
2278 
2279 /*@{*/
2280 /**
2281  * @name Option Parser Settings Macros
2282  * Macros which provide access to the option parser settings.
2283  *
2284  * These macros provide abstracted access to various settings used by
2285  * the option parser.
2286  *
2287  * @ingroup DOXGRP_OPT
2288  */
2289 
2290 /**
2291  * Retrieves a single option structure.
2292  *
2293  * This macro returns a single option structure (spifopt_t_struct)
2294  * from the options list.  This is mostly for internal use.
2295  *
2296  * @param n The index of the desired option in the options list.
2297  * @return  The option structure (spifopt_t_struct).
2298  */
2299 #define SPIFOPT_OPTLIST_GET_OPT(n)        (spifopt_settings.opt_list[((n) < (spifopt_settings.num_opts) ? (n) : (0))])
2300 /**
2301  * Sets the option list.
2302  *
2303  * This macro should be called by client programs to set the option
2304  * list.  It must be a spifopt_t * of some type (probably a
2305  * spifopt_t []).  Once set, it must not be manipulated directly; only
2306  * via the LibAST-supplied macros.
2307  *
2308  * @param l The options list variable (a spifopt_t pointer or array).
2309  */
2310 #define SPIFOPT_OPTLIST_SET(l)            (spifopt_settings.opt_list = ((spifopt_t *) (l)))
2311 /**
2312  * Obtains the number of options.
2313  *
2314  * This macro returns the total number of options in the options list.
2315  *
2316  * @return  The number of options in the list.
2317  */
2318 #define SPIFOPT_NUMOPTS_GET()             (spifopt_settings.num_opts)
2319 /**
2320  * Sets the number of options.
2321  *
2322  * This macro sets the number of options in the options list.  It
2323  * should be called immediately after SPIFOPT_OPTLIST_SET() and
2324  * @em before any functions or macros which use the options list.  The
2325  * most common way to handle this is to use a fixed array for the
2326  * options list and call
2327  *
2328  * @code
2329  * SPIFOPT_NUMOPTS_SET(sizeof(option_list) / sizeof(spifopt_t))
2330  * @endcode
2331  *
2332  * to set the option count.
2333  *
2334  * @param n The number of elements in the options list.
2335  */
2336 #define SPIFOPT_NUMOPTS_SET(n)            (spifopt_settings.num_opts = (n))
2337 /**
2338  * Obtains the option parser flag settings.
2339  *
2340  * This macro returns the value of the parser settings flags.  In most
2341  * cases, you should use SPIFOPT_FLAGS_IS_SET() instead.
2342  *
2343  * @return  The value of the parser settings flags.
2344  */
2345 #define SPIFOPT_FLAGS_GET()               (spifopt_settings.flags)
2346 /**
2347  * Sets an option parser settings flag.
2348  *
2349  * This macro sets one or more parser settings flags.  There are
2350  * currently no client-managed parser flags, so you should avoid this
2351  * macro.
2352  *
2353  * @param m The flag (or set of bitwise-or'd flags) to turn on.
2354  */
2355 #define SPIFOPT_FLAGS_SET(m)              (spifopt_settings.flags |= (m))
2356 /**
2357  * Checks whether or not an option parser settings flag is set.
2358  *
2359  * This macro tests one or more parser settings flags.  There are
2360  * currently no client-managed parser flags, so you should avoid this
2361  * macro.
2362  *
2363  * @param m The flag (or set of bitwise-or'd flags) to check.
2364  * @return  0 if none of the selected flags are set, non-zero if at
2365  *          least one is.
2366  */
2367 #define SPIFOPT_FLAGS_IS_SET(m)           (spifopt_settings.flags & (m))
2368 /**
2369  * Clears an option parser settings flag.
2370  *
2371  * This macro clears one or more parser settings flags.  There are
2372  * currently no client-managed parser flags, so you should avoid this
2373  * macro.
2374  *
2375  * @param m The flag (or set of bitwise-or'd flags) to turn off.
2376  */
2377 #define SPIFOPT_FLAGS_CLEAR(m)            (spifopt_settings.flags &= ~(m))
2378 /**
2379  * Gets the bad option count.
2380  *
2381  * This macro retrieves the count of bad options encountered during
2382  * option parsing.
2383  *
2384  * @return The number of bad options encountered.
2385  */
2386 #define SPIFOPT_BADOPTS_GET()             (spifopt_settings.bad_opts)
2387 /**
2388  * Sets the bad option count.
2389  *
2390  * This macro sets the count of bad options encountered during option
2391  * parsing.  This macro should not be used by client programs.
2392  *
2393  * @param n The number of bad options encountered.
2394  */
2395 #define SPIFOPT_BADOPTS_SET(n)            (spifopt_settings.bad_opts = (n))
2396 /**
2397  * Gets the bad option setting.
2398  *
2399  * This macro retrieves the setting for the number of bad options
2400  * allowed before parsing is aborted and the help screen is displayed.
2401  *
2402  * @return The number of bad options allowed.
2403  */
2404 #define SPIFOPT_ALLOWBAD_GET()            (spifopt_settings.allow_bad)
2405 /**
2406  * Sets the bad option setting.
2407  *
2408  * This macro sets the number of bad options allowed before parsing is
2409  * aborted and the help screen is displayed.  This macro should be
2410  * called prior to option parsing.
2411  *
2412  * @param n The number of bad options allowed.
2413  */
2414 #define SPIFOPT_ALLOWBAD_SET(n)           (spifopt_settings.allow_bad = (n))
2415 /**
2416  * Gets the help handler function pointer.
2417  *
2418  * This macro retrieves the pointer to the function which will be
2419  * called to display program help, due either to a help option or
2420  * an excess number of option parsing errors.  If this value is not
2421  * set by the client (via the SPIFOPT_HELPHANDLER_SET() macro), the
2422  * default handler is the built-in spifopt_usage() function.
2423  *
2424  * @return The function pointer for the help/usage screen function.
2425  */
2426 #define SPIFOPT_HELPHANDLER               ((spifopt_settings.help_handler) ? (spifopt_settings.help_handler) : (spifopt_usage))
2427 /**
2428  * Sets the help handler function pointer.
2429  *
2430  * This macro sets the pointer to the function which will be called to
2431  * display program help, due either to a help option or an excess
2432  * number of option parsing errors.  If the client fails to call this
2433  * macro prior to option parsing, the built-in spifopt_usage()
2434  * function is used instead.
2435  *
2436  * @param f A function pointer of type spifopt_helphandler_t.
2437  */
2438 #define SPIFOPT_HELPHANDLER_SET(f)        (spifopt_settings.help_handler = (f))
2439 /*@}*/
2440 
2441 /**
2442  * @name Type Definitions
2443  *
2444  */
2445 /*@{*/
2446 /**
2447  * Typedef for help handler function.
2448  *
2449  * This type is used for declaring/typecasting function pointers which
2450  * will be used for help handlers.  Functions used for this should be
2451  * declared as returning void (and in reality does not return at all)
2452  * and should take either no parameters, or a single char * parameter.
2453  *
2454  * @see @link DOXGRP_OPT Command Line Option Parser @endlink, SPIFOPT_HELPHANDLER_SET()
2455  * @ingroup DOXGRP_OPT
2456  */
2457 typedef void (*spifopt_helphandler_t)();
2458 /**
2459  * Typedef for abstract option handler function.
2460  *
2461  * This type is used for declaring/typecasting function pointers which
2462  * will be used for abstract option handlers.  Abstract options are
2463  * those which require special handling; LibAST implements this by
2464  * allowing for an arbitrary user-specified function be invoked when
2465  * such an option is encountered.  Functions used for this should be
2466  * declared as returning void and should take a single char *
2467  * parameter (the value of the option, or NULL if it had no value).
2468  *
2469  * @see @link DOXGRP_OPT Command Line Option Parser @endlink
2470  * @ingroup DOXGRP_OPT
2471  */
2472 typedef void (*spifopt_abstract_handler_t)(spif_charptr_t);
2473 
2474 /**
2475  * Option structure.
2476  *
2477  * This is the structure that holds the data for each of the command
2478  * line options for which the parser will be looking.  Client programs
2479  * must create an array of these structures (a spifopt_t []) and use
2480  * the SPIFOPT_OPTLIST_SET() macro to tell LibAST which variable it
2481  * is.
2482  *
2483  * @note This structure and its members should NEVER be accessed
2484  * directly; they are documented solely for informational purposes.
2485  * The SPIFOPT_* convenience macros provide a streamlined, easy-to-use
2486  * abstraction layer for declaring the option list, setting option
2487  * parser parameters, and so forth.  Even the internal code uses these
2488  * macros!  Consult the macro documentation and the example code for
2489  * further assistance.
2490  *
2491  * @see @link DOXGRP_OPT Command Line Option Parser @endlink, @link opt_example.c example code @endlink
2492  * @ingroup DOXGRP_OPT
2493  */
2494 typedef struct spifopt_t_struct {
2495     /**
2496      * Short form.
2497      *
2498      * The short (one char) form of the option.
2499      */
2500     spif_char_t short_opt;
2501     /**
2502      * Long form.
2503      *
2504      * The long (string) form of the option.
2505      */
2506     spif_charptr_t long_opt;
2507     /**
2508      * Description.
2509      *
2510      * The (brief) description of the option for the help screen.
2511      */
2512     spif_charptr_t desc;
2513     /**
2514      * Option type/attribute flags.
2515      *
2516      * The type and attribute flags for this option.
2517      */
2518     spif_uint16_t flags;
2519     /**
2520      * Value pointer.
2521      *
2522      * A pointer to where the value for this option should be stored.
2523      * Its exact type, and how it is interpreted, depends on the type
2524      * of option being defined.
2525      */
2526     void *value;
2527     /**
2528      * Boolean bitmask.
2529      *
2530      * For boolean options, this is the bitmask for the option.  For
2531      * other option types, it has no meaning.
2532      */
2533     spif_uint32_t mask;
2534 } spifopt_t;
2535 
2536 /**
2537  * Option parser settings structure.
2538  *
2539  * This is the structure that holds the settings and other internal
2540  * variables which control how the options parser functions.
2541  *
2542  * @note This structure and its members should NEVER be accessed
2543  * directly; they are documented solely for informational purposes.
2544  * The SPIFOPT_* convenience macros provide a streamlined, easy-to-use
2545  * abstraction layer for declaring the option list, setting option
2546  * parser parameters, and so forth.  Even the internal code uses these
2547  * macros!  Consult the macro documentation and the example code for
2548  * further assistance.
2549  *
2550  * @see @link DOXGRP_OPT Command Line Option Parser @endlink, @link opt_example.c example code @endlink
2551  * @ingroup DOXGRP_OPT
2552  */
2553 typedef struct spifopt_settings_t_struct {
2554     /**
2555      * Options list.
2556      *
2557      * The array of option structures defining the options to look
2558      * for.
2559      */
2560     spifopt_t *opt_list;
2561     /**
2562      * Option count.
2563      *
2564      * The total number of options in the options list.
2565      */
2566     spif_uint16_t num_opts;
2567     /**
2568      * Parser flags.
2569      *
2570      * Flags which control the behavior of the parser.
2571      */
2572     spif_uint8_t flags;
2573     /**
2574      * Bad option count.
2575      *
2576      * Keeps track of the number of bad options (i.e., option syntax
2577      * errors, such as missing values or unknown options)
2578      * encountered.
2579      */
2580     spif_uint8_t bad_opts;
2581     /**
2582      * Bad option limit.
2583      *
2584      * The maximum number of bad options allowed before giving up and
2585      * displaying the help text.
2586      */
2587     spif_uint8_t allow_bad;
2588     spif_uint8_t indent; /**< Unused. */
2589     /**
2590      * Help handler.
2591      *
2592      * Pointer to the function which is responsible for displaying the
2593      * help text.  If undefined, spifopt_usage() is used.
2594      */
2595     spifopt_helphandler_t help_handler;
2596 } spifopt_settings_t;
2597 /*@}*/
2598 
2599 extern spifopt_settings_t spifopt_settings;
2600 
2601 
2602 /******************************* HASHING GOOP *********************************/
2603 
2604 /**
2605  * @name Jenkins Hash
2606  *
2607  * Bob Jenkins' hash algorithm as published in December 1996.  Public
2608  * domain.  See http://burtleburtle.net/bob/hash/
2609  */
2610 /*@{*/
2611 /**
2612  * Calculate number of hash buckets needed for an n-bit hash key.
2613  *
2614  * This macro returns the number of hash buckets needed for a hash key
2615  * of n distinct bits.  Choose n based on your tolerable level of
2616  * collisions, on average, for 2^n key values.
2617  *
2618  * @param n The number of bits.
2619  * @return  Number of hash buckets required.
2620  *
2621  */
2622 #define SPIFHASH_SIZE(n)       (SPIF_CAST(uint32) (1UL << (n)))
2623 
2624 /**
2625  * Calculate mask to apply to hash key to get lowest n bits.
2626  *
2627  * This macro returns the bitmask needed for a hash key of n distinct
2628  * bits.  Choose n based on your tolerable level of collisions, on
2629  * average, for 2^n key values.
2630  *
2631  * @param n The number of bits.
2632  * @return  Bitmask to zero out all but the n lowest bits.
2633  *
2634  */
2635 #define SPIFHASH_MASK(n)       (SPIF_CAST(uint32) (SPIFHASH_SIZE(n) - 1))
2636 
2637 /**
2638  * Mix 3 32-bit integer values in a reproducible, reversible manner.
2639  *
2640  * This macro is used by the Jenkins hash algorithm to shuffle bits of
2641  * three integers in such a way as to make sure that changes are
2642  * propogated throughout the values.
2643  *
2644  * Instructions are arranged in such a way as to be parallizable;
2645  * i.e., excluding the first two instructions, each subsequent pair of
2646  * instructions may be evaluated simultaneously for pipelining
2647  * purposes.
2648  *
2649  * @param a A 32-bit integer.
2650  * @param b A 32-bit integer.
2651  * @param c A 32-bit integer.
2652  */
2653 #define SPIFHASH_JENKINS_MIX(a,b,c) \
2654 { \
2655     a -= b; a -= c; a ^= (c>>13); \
2656     b -= c; b -= a; b ^= (a<<8);  \
2657     c -= a; c -= b; c ^= (b>>13); \
2658     a -= b; a -= c; a ^= (c>>12); \
2659     b -= c; b -= a; b ^= (a<<16); \
2660     c -= a; c -= b; c ^= (b>>5);  \
2661     a -= b; a -= c; a ^= (c>>3);  \
2662     b -= c; b -= a; b ^= (a<<10); \
2663     c -= a; c -= b; c ^= (b>>15); \
2664 }
2665 
2666 /**
2667  * A pointer to a hash function.
2668  *
2669  * This type is used to refer to any of the spifhash_* functions.  A
2670  * variable of this type can point to any of the built-in hash
2671  * functions in LibAST interchangeably.
2672  *
2673  */
2674 typedef spif_uint32_t (*spifhash_func_t)(spif_uint8_t *, spif_uint32_t, spif_uint32_t);
2675 /*@}*/
2676 
2677 
2678 
2679 /******************************** PROTOTYPES **********************************/
2680 
2681 /* msgs.c */
2682 extern void libast_set_program_name(const char *);
2683 extern void libast_set_program_version(const char *);
2684 extern int libast_dprintf(const char *, ...);
2685 extern void libast_print_error(const char *fmt, ...);
2686 extern void libast_print_warning(const char *fmt, ...);
2687 extern void libast_fatal_error(const char *fmt, ...);
2688 
2689 /* debug.c */
2690 extern unsigned int DEBUG_LEVEL;
2691 
2692 /* mem.c */
2693 extern void spifmem_init(void);
2694 extern void *spifmem_malloc(const spif_charptr_t, unsigned long, size_t);
2695 extern void *spifmem_realloc(const spif_charptr_t, const spif_charptr_t, unsigned long, void *, size_t);
2696 extern void *spifmem_calloc(const spif_charptr_t, unsigned long, size_t, size_t);
2697 extern void spifmem_free(const spif_charptr_t, const spif_charptr_t, unsigned long, void *);
2698 extern spif_charptr_t spifmem_strdup(const spif_charptr_t, const spif_charptr_t,
2699                                      unsigned long, const spif_charptr_t);
2700 extern void spifmem_dump_mem_tables(void);
2701 #if LIBAST_X11_SUPPORT
2702 extern Pixmap spifmem_x_create_pixmap(const spif_charptr_t, unsigned long, Display *,
2703                                       Drawable, unsigned int, unsigned int, unsigned int);
2704 extern void spifmem_x_free_pixmap(const spif_charptr_t, const spif_charptr_t,
2705                                   unsigned long, Display *, Pixmap);
2706 # if LIBAST_IMLIB2_SUPPORT
2707 extern void spifmem_imlib_register_pixmap(const spif_charptr_t var, const spif_charptr_t filename,
2708                                           unsigned long line, Pixmap p);
2709 extern void spifmem_imlib_free_pixmap(const spif_charptr_t var, const spif_charptr_t filename,
2710                                       unsigned long line, Pixmap p);
2711 # endif
2712 extern void spifmem_dump_pixmap_tables(void);
2713 extern GC spifmem_x_create_gc(const spif_charptr_t, unsigned long, Display *, Drawable,
2714                               unsigned long, XGCValues *);
2715 extern void spifmem_x_free_gc(const spif_charptr_t, const spif_charptr_t, unsigned long, Display *, GC);
2716 extern void spifmem_dump_gc_tables(void);
2717 #endif
2718 extern void spiftool_free_array(void *, size_t);
2719 
2720 /* file.c */
2721 extern int spiftool_temp_file(spif_charptr_t, size_t);
2722 
2723 /* strings.c */
2724 extern spif_bool_t spiftool_safe_strncpy(spif_charptr_t dest, const spif_charptr_t src, spif_int32_t size);
2725 extern spif_bool_t spiftool_safe_strncat(spif_charptr_t dest, const spif_charptr_t src, spif_int32_t size);
2726 extern spif_charptr_t spiftool_substr(const spif_charptr_t, spif_int32_t, spif_int32_t);
2727 #if LIBAST_REGEXP_SUPPORT_POSIX && HAVE_REGEX_H
2728 extern spif_bool_t spiftool_regexp_match(const spif_charptr_t, const spif_charptr_t);
2729 extern spif_bool_t spiftool_regexp_match_r(const spif_charptr_t str, const spif_charptr_t pattern, regex_t **rexp);
2730 #endif
2731 extern spif_charptr_t *spiftool_split(const spif_charptr_t, const spif_charptr_t);
2732 extern spif_charptr_t *spiftool_split_regexp(const spif_charptr_t, const spif_charptr_t);
2733 extern spif_charptr_t spiftool_join(spif_charptr_t, spif_charptr_t *);
2734 extern spif_charptr_t spiftool_get_word(unsigned long, const spif_charptr_t);
2735 extern spif_charptr_t spiftool_get_pword(unsigned long, const spif_charptr_t);
2736 extern unsigned long spiftool_num_words(const spif_charptr_t);
2737 extern spif_charptr_t spiftool_chomp(spif_charptr_t);
2738 extern spif_charptr_t spiftool_downcase_str(spif_charptr_t);
2739 extern spif_charptr_t spiftool_upcase_str(spif_charptr_t);
2740 extern spif_charptr_t spiftool_safe_str(spif_charptr_t, unsigned short);
2741 extern spif_charptr_t spiftool_condense_whitespace(spif_charptr_t);
2742 extern void spiftool_hex_dump(void *, size_t);
2743 extern spif_cmp_t spiftool_version_compare(spif_charptr_t, spif_charptr_t);
2744 #if !(HAVE_MEMMEM)
2745 extern void *memmem(const void *, size_t, const void *, size_t);
2746 #endif
2747 #if !(HAVE_STRNLEN)
2748 extern size_t strnlen(const char *, size_t);
2749 #endif
2750 #if !(HAVE_USLEEP)
2751 extern void usleep(unsigned long);
2752 #endif
2753 #if !(HAVE_SNPRINTF)
2754 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
2755 extern int snprintf(char *str, size_t count, const char *fmt, ...);
2756 #endif
2757 #if !(HAVE_STRCASESTR)
2758 extern char *strcasestr(const char *, const char *);
2759 #endif
2760 #if !(HAVE_STRCASECHR)
2761 extern char *strcasechr(const char *, const char);
2762 #endif
2763 #if !(HAVE_STRCASEPBRK)
2764 extern char *strcasepbrk(const char *, const char *);
2765 #endif
2766 #if !(HAVE_STRREV)
2767 extern char *strrev(char *);
2768 #endif
2769 #if !(HAVE_STRSEP)
2770 extern char *strsep(char **, char *);
2771 #endif
2772 
2773 /* builtin_hashes.c */
2774 extern spif_uint32_t spifhash_jenkins(register spif_uint8_t *key, register spif_uint32_t length, register spif_uint32_t seed);
2775 extern spif_uint32_t spifhash_jenkins32(spif_uint8_t *key, register spif_uint32_t length, register spif_uint32_t seed);
2776 #if WORDS_BIGENDIAN
2777 #  define spifhash_jenkinsLE(k, l, s)  spifhash_jenkins((k), (l), (s))
2778 #else
2779 extern spif_uint32_t spifhash_jenkinsLE(register spif_uint8_t *key, register spif_uint32_t length, register spif_uint32_t seed);
2780 #endif
2781 extern spif_uint32_t spifhash_rotating(spif_uint8_t *key, spif_uint32_t len, spif_uint32_t seed);
2782 extern spif_uint32_t spifhash_one_at_a_time(spif_uint8_t *key, spif_uint32_t len, spif_uint32_t seed);
2783 extern spif_uint32_t spifhash_fnv(spif_uint8_t *key, spif_uint32_t len, spif_uint32_t seed);
2784 
2785 /* conf.c */
2786 extern void spifconf_init_subsystem(void);
2787 extern unsigned char spifconf_register_context(spif_charptr_t name, ctx_handler_t handler);
2788 extern unsigned char spifconf_register_fstate(FILE *fp, spif_charptr_t path, spif_charptr_t outfile, unsigned long line, unsigned char flags);
2789 extern unsigned char spifconf_register_builtin(char *name, spifconf_func_ptr_t ptr);
2790 extern unsigned char spifconf_register_context_state(unsigned char ctx_id);
2791 extern void spifconf_free_subsystem(void);
2792 extern spif_charptr_t spifconf_shell_expand(spif_charptr_t);
2793 extern spif_charptr_t spifconf_find_file(const spif_charptr_t file, const spif_charptr_t dir, const spif_charptr_t pathlist);
2794 extern FILE *spifconf_open_file(spif_charptr_t name);
2795 extern void spifconf_parse_line(FILE *fp, spif_charptr_t buff);
2796 extern spif_charptr_t spifconf_parse(spif_charptr_t conf_name, const spif_charptr_t dir, const spif_charptr_t path);
2797 
2798 /* options.c */
2799 extern void spifopt_parse(int, char **);
2800 extern void spifopt_usage(void);
2801 
2802 
2803 /* Do we, or do we not, pollute the namespace like we used to? */
2804 #if LIBAST_COMPAT_05_API
2805 /* The application must have defined this. */
2806 /* conf.c */
2807 # define CONF_BEGIN_CHAR                                         SPIFCONF_BEGIN_CHAR
2808 # define CONF_END_CHAR                                           SPIFCONF_END_CHAR
2809 # define CONF_BEGIN_STRING                                       SPIFCONF_BEGIN_STRING
2810 # define CONF_END_STRING                                         SPIFCONF_END_STRING
2811 typedef spifconf_func_ptr_t conf_func_ptr_t;
2812 # define conf_init_subsystem()                                   spifconf_init_subsystem()
2813 # define conf_register_context(a, b)                             spifconf_register_context((a), (b))
2814 # define conf_register_fstate(a, b, c, d, e)                     spifconf_register_fstate((a), (b), (c), (d), (e))
2815 # define conf_register_builtin(a, b)                             spifconf_register_builtin((a), (b))
2816 # define conf_register_context_state(a)                          spifconf_register_context_state(a)
2817 # define conf_free_subsystem()                                   spifconf_free_subsystem()
2818 # define shell_expand(a)                                         spifconf_shell_expand(a)
2819 # define conf_find_file(a, b, c)                                 spifconf_find_file((a), (b), (c))
2820 # define open_config_file(a)                                     spifconf_open_file(a)
2821 # define conf_parse_line(a, b)                                   spifconf_parse_line((a), (b))
2822 # define conf_parse(a, b, c)                                     spifconf_parse((a), (b), (c))
2823 
2824 /* mem.c */
2825 # define memrec_init()                                           spifmem_init()
2826 # define libast_malloc(a, b, c)                                  spifmem_malloc((a), (b), (c))
2827 # define libast_realloc(a, b, c, d, e)                           spifmem_realloc((a), (b), (c), (d), (e))
2828 # define libast_calloc(a, b, c, d)                               spifmem_calloc((a), (b), (c), (d))
2829 # define libast_free(a, b, c, d)                                 spifmem_free((a), (b), (c), (d))
2830 # define libast_strdup(a, b, c, d)                               spifmem_strdup((a), (b), (c), (d))
2831 # define libast_dump_mem_tables()                                spifmem_dump_mem_tables()
2832 # if LIBAST_X11_SUPPORT
2833 #  define libast_x_create_pixmap(a, b, c, d, e, f, g)            spifmem_x_create_pixmap((a), (b), (c), (d), (e), (f), (g))
2834 #  define libast_x_free_pixmap(a, b, c, d, e)                    spifmem_x_free_pixmap((a), (b), (c), (d), (e))
2835 #  if LIBAST_IMLIB2_SUPPORT
2836 #   define libast_imlib_register_pixmap(a, b, c, d)              spifmem_imlib_register_pixmap((a), (b), (c), (d))
2837 #   define libast_imlib_free_pixmap(a, b, c, d)                  spifmem_imlib_free_pixmap((a), (b), (c), (d))
2838 #  endif
2839 #  define libast_dump_pixmap_tables()                            spifmem_dump_pixmap_tables()
2840 #  define libast_x_create_gc(a, b, c, d, e, f)                   spifmem_x_create_gc((a), (b), (c), (d), (e), (f))
2841 #  define libast_x_free_gc(a, b, c, d, e)                        spifmem_x_free_gc((a), (b), (c), (d), (e))
2842 #  define libast_dump_gc_tables()                                spifmem_dump_gc_tables()
2843 # endif
2844 # define free_array(a, b)                                        spiftool_free_array((a), (b))
2845 
2846 /* file.c */
2847 # define libast_temp_file(a, b)                                  spiftool_temp_file((a), (b))
2848 
2849 /* msgs.c */
2850 static void (*print_error)(const char *, ...) = libast_print_error;
2851 static void (*print_warning)(const char *, ...) = libast_print_warning;
2852 static void (*fatal_error)(const char *, ...) = libast_fatal_error;
2853 
2854 /* strings.c */
2855 # define regexp_match(a, b)                                      spiftool_regexp_match((a), (b))
2856 # define regexp_match_r(a, b, c)                                 spiftool_regexp_match_r((a), (b), (c))
2857 # define split(a, b)                                             spiftool_split((a), (b))
2858 # define join(a, b)                                              spiftool_join((a), (b))
2859 # define get_word(a, b)                                          spiftool_get_word((a), (b))
2860 # define get_pword(a, b)                                         spiftool_get_pword((a), (b))
2861 # define num_words(a)                                            spiftool_num_words(a)
2862 # define chomp(a)                                                spiftool_chomp(a)
2863 # define strip_whitespace(a)                                     spiftool_strip_whitespace(a)
2864 # define downcase_str(a)                                         spiftool_downcase_str(a)
2865 # define upcase_str(a)                                           spiftool_upcase_str(a)
2866 # define safe_str(a, b)                                          spiftool_safe_str((a), (b))
2867 # define condense_whitespace(a)                                  spiftool_condense_whitespace(a)
2868 # define hex_dump(a, b)                                          spiftool_hex_dump((a), (b))
2869 # define version_compare(a, b)                                   spiftool_version_compare((a), (b))
2870 
2871 #endif /* LIBAST_COMPAT_05_API */
2872 
2873 #endif /* _LIBAST_H_ */
2874