1 /* $NetBSD: autoopts.h,v 1.2 2010/12/04 23:08:36 christos Exp $ */ 2 3 4 /* 5 * Time-stamp: "2008-11-01 20:08:06 bkorb" 6 * 7 * autoopts.h Id: d5e30331d37ca10ec88c592d24d8615dd6c1f0ee 8 * 9 * This file defines all the global structures and special values 10 * used in the automated option processing library. 11 * 12 * This file is part of AutoOpts, a companion to AutoGen. 13 * AutoOpts is free software. 14 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved 15 * 16 * AutoOpts is available under any one of two licenses. The license 17 * in use must be one of these two and the choice is under the control 18 * of the user of the license. 19 * 20 * The GNU Lesser General Public License, version 3 or later 21 * See the files "COPYING.lgplv3" and "COPYING.gplv3" 22 * 23 * The Modified Berkeley Software Distribution License 24 * See the file "COPYING.mbsd" 25 * 26 * These files have the following md5sums: 27 * 28 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3 29 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3 30 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd 31 */ 32 33 #ifndef AUTOGEN_AUTOOPTS_H 34 #define AUTOGEN_AUTOOPTS_H 35 36 #include "compat/compat.h" 37 #include "ag-char-map.h" 38 39 #define AO_NAME_LIMIT 127 40 #define AO_NAME_SIZE ((size_t)(AO_NAME_LIMIT + 1)) 41 42 #ifndef AG_PATH_MAX 43 # ifdef PATH_MAX 44 # define AG_PATH_MAX ((size_t)PATH_MAX) 45 # else 46 # define AG_PATH_MAX ((size_t)4096) 47 # endif 48 #else 49 # if defined(PATH_MAX) && (PATH_MAX > MAXPATHLEN) 50 # undef AG_PATH_MAX 51 # define AG_PATH_MAX ((size_t)PATH_MAX) 52 # endif 53 #endif 54 55 #undef EXPORT 56 #define EXPORT 57 58 #if defined(_WIN32) && !defined(__CYGWIN__) 59 # define DIRCH '\\' 60 #else 61 # define DIRCH '/' 62 #endif 63 64 #ifndef EX_NOINPUT 65 # define EX_NOINPUT 66 66 #endif 67 #ifndef EX_SOFTWARE 68 # define EX_SOFTWARE 70 69 #endif 70 #ifndef EX_CONFIG 71 # define EX_CONFIG 78 72 #endif 73 74 /* 75 * Convert the number to a list usable in a printf call 76 */ 77 #define NUM_TO_VER(n) ((n) >> 12), ((n) >> 7) & 0x001F, (n) & 0x007F 78 79 #define NAMED_OPTS(po) \ 80 (((po)->fOptSet & (OPTPROC_SHORTOPT | OPTPROC_LONGOPT)) == 0) 81 82 #define SKIP_OPT(p) (((p)->fOptState & (OPTST_DOCUMENT|OPTST_OMITTED)) != 0) 83 84 typedef int tDirection; 85 #define DIRECTION_PRESET -1 86 #define DIRECTION_PROCESS 1 87 #define DIRECTION_CALLED 0 88 89 #define PROCESSING(d) ((d)>0) 90 #define PRESETTING(d) ((d)<0) 91 92 /* 93 * Procedure success codes 94 * 95 * USAGE: define procedures to return "tSuccess". Test their results 96 * with the SUCCEEDED, FAILED and HADGLITCH macros. 97 * 98 * Microsoft sticks its nose into user space here, so for Windows' sake, 99 * make sure all of these are undefined. 100 */ 101 #undef SUCCESS 102 #undef FAILURE 103 #undef PROBLEM 104 #undef SUCCEEDED 105 #undef SUCCESSFUL 106 #undef FAILED 107 #undef HADGLITCH 108 109 #define SUCCESS ((tSuccess) 0) 110 #define FAILURE ((tSuccess)-1) 111 #define PROBLEM ((tSuccess) 1) 112 113 typedef int tSuccess; 114 115 #define SUCCEEDED( p ) ((p) == SUCCESS) 116 #define SUCCESSFUL( p ) SUCCEEDED( p ) 117 #define FAILED( p ) ((p) < SUCCESS) 118 #define HADGLITCH( p ) ((p) > SUCCESS) 119 120 /* 121 * When loading a line (or block) of text as an option, the value can 122 * be processed in any of several modes: 123 * 124 * @table @samp 125 * @item keep 126 * Every part of the value between the delimiters is saved. 127 * 128 * @item uncooked 129 * Even if the value begins with quote characters, do not do quote processing. 130 * 131 * @item cooked 132 * If the value looks like a quoted string, then process it. 133 * Double quoted strings are processed the way strings are in "C" programs, 134 * except they are treated as regular characters if the following character 135 * is not a well-established escape sequence. 136 * Single quoted strings (quoted with apostrophies) are handled the way 137 * strings are handled in shell scripts, *except* that backslash escapes 138 * are honored before backslash escapes and apostrophies. 139 * @end table 140 */ 141 typedef enum { 142 OPTION_LOAD_COOKED, 143 OPTION_LOAD_UNCOOKED, 144 OPTION_LOAD_KEEP 145 } tOptionLoadMode; 146 147 extern tOptionLoadMode option_load_mode; 148 149 /* 150 * The pager state is used by optionPagedUsage() procedure. 151 * When it runs, it sets itself up to be called again on exit. 152 * If, however, a routine needs a child process to do some work 153 * before it is done, then 'pagerState' must be set to 154 * 'PAGER_STATE_CHILD' so that optionPagedUsage() will not try 155 * to run the pager program before its time. 156 */ 157 typedef enum { 158 PAGER_STATE_INITIAL, 159 PAGER_STATE_READY, 160 PAGER_STATE_CHILD 161 } tePagerState; 162 163 extern tePagerState pagerState; 164 165 typedef enum { 166 ENV_ALL, 167 ENV_IMM, 168 ENV_NON_IMM 169 } teEnvPresetType; 170 171 typedef enum { 172 TOPT_UNDEFINED = 0, 173 TOPT_SHORT, 174 TOPT_LONG, 175 TOPT_DEFAULT 176 } teOptType; 177 178 typedef struct { 179 tOptDesc* pOD; 180 tCC* pzOptArg; 181 tAoUL flags; 182 teOptType optType; 183 } tOptState; 184 #define OPTSTATE_INITIALIZER(st) \ 185 { NULL, NULL, OPTST_ ## st, TOPT_UNDEFINED } 186 187 #define TEXTTO_TABLE \ 188 _TT_( LONGUSAGE ) \ 189 _TT_( USAGE ) \ 190 _TT_( VERSION ) 191 #define _TT_(n) \ 192 TT_ ## n , 193 194 typedef enum { TEXTTO_TABLE COUNT_TT } teTextTo; 195 196 #undef _TT_ 197 198 typedef struct { 199 tCC* pzStr; 200 tCC* pzReq; 201 tCC* pzNum; 202 tCC* pzFile; 203 tCC* pzKey; 204 tCC* pzKeyL; 205 tCC* pzBool; 206 tCC* pzNest; 207 tCC* pzOpt; 208 tCC* pzNo; 209 tCC* pzBrk; 210 tCC* pzNoF; 211 tCC* pzSpc; 212 tCC* pzOptFmt; 213 tCC* pzTime; 214 } arg_types_t; 215 216 #define AGALOC( c, w ) ao_malloc((size_t)c) 217 #define AGREALOC( p, c, w ) ao_realloc((void*)p, (size_t)c) 218 #define AGFREE(_p) do{ \ 219 void* X = (void*)(intptr_t)_p; \ 220 ao_free(X); \ 221 } while (/*CONSTCOND*/0) 222 #define AGDUPSTR( p, s, w ) (p = ao_strdup(s)) 223 224 static void * 225 ao_malloc( size_t sz ); 226 227 static void * 228 ao_realloc( void *p, size_t sz ); 229 230 static void 231 ao_free( void *p ); 232 233 static char * 234 ao_strdup( char const *str ); 235 236 #define TAGMEM( m, t ) 237 238 /* 239 * DO option handling? 240 * 241 * Options are examined at two times: at immediate handling time and at 242 * normal handling time. If an option is disabled, the timing may be 243 * different from the handling of the undisabled option. The OPTST_DIABLED 244 * bit indicates the state of the currently discovered option. 245 * So, here's how it works: 246 * 247 * A) handling at "immediate" time, either 1 or 2: 248 * 249 * 1. OPTST_DISABLED is not set: 250 * IMM must be set 251 * DISABLE_IMM don't care 252 * TWICE don't care 253 * DISABLE_TWICE don't care 254 * 0 -and- 1 x x x 255 * 256 * 2. OPTST_DISABLED is set: 257 * IMM don't care 258 * DISABLE_IMM must be set 259 * TWICE don't care 260 * DISABLE_TWICE don't care 261 * 1 -and- x 1 x x 262 */ 263 #define DO_IMMEDIATELY(_flg) \ 264 ( (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == OPTST_IMM) \ 265 || ( ((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) \ 266 == (OPTST_DISABLED|OPTST_DISABLE_IMM) )) 267 268 /* B) handling at "regular" time because it was not immediate 269 * 270 * 1. OPTST_DISABLED is not set: 271 * IMM must *NOT* be set 272 * DISABLE_IMM don't care 273 * TWICE don't care 274 * DISABLE_TWICE don't care 275 * 0 -and- 0 x x x 276 * 277 * 2. OPTST_DISABLED is set: 278 * IMM don't care 279 * DISABLE_IMM don't care 280 * TWICE must be set 281 * DISABLE_TWICE don't care 282 * 1 -and- x x 1 x 283 */ 284 #define DO_NORMALLY(_flg) ( \ 285 (((_flg) & (OPTST_DISABLED|OPTST_IMM)) == 0) \ 286 || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_IMM)) == \ 287 OPTST_DISABLED) ) 288 289 /* C) handling at "regular" time because it is to be handled twice. 290 * The immediate bit was already tested and found to be set: 291 * 292 * 3. OPTST_DISABLED is not set: 293 * IMM is set (but don't care) 294 * DISABLE_IMM don't care 295 * TWICE must be set 296 * DISABLE_TWICE don't care 297 * 0 -and- ? x 1 x 298 * 299 * 4. OPTST_DISABLED is set: 300 * IMM don't care 301 * DISABLE_IMM is set (but don't care) 302 * TWICE don't care 303 * DISABLE_TWICE must be set 304 * 1 -and- x ? x 1 305 */ 306 #define DO_SECOND_TIME(_flg) ( \ 307 (((_flg) & (OPTST_DISABLED|OPTST_TWICE)) == \ 308 OPTST_TWICE) \ 309 || (((_flg) & (OPTST_DISABLED|OPTST_DISABLE_TWICE)) == \ 310 (OPTST_DISABLED|OPTST_DISABLE_TWICE) )) 311 312 /* 313 * text_mmap structure. Only active on platforms with mmap(2). 314 */ 315 #ifdef HAVE_SYS_MMAN_H 316 # include <sys/mman.h> 317 #else 318 # ifndef PROT_READ 319 # define PROT_READ 0x01 320 # endif 321 # ifndef PROT_WRITE 322 # define PROT_WRITE 0x02 323 # endif 324 # ifndef MAP_SHARED 325 # define MAP_SHARED 0x01 326 # endif 327 # ifndef MAP_PRIVATE 328 # define MAP_PRIVATE 0x02 329 # endif 330 #endif 331 332 #ifndef MAP_FAILED 333 # define MAP_FAILED ((void*)-1) 334 #endif 335 336 #ifndef _SC_PAGESIZE 337 # ifdef _SC_PAGE_SIZE 338 # define _SC_PAGESIZE _SC_PAGE_SIZE 339 # endif 340 #endif 341 342 #ifndef HAVE_STRCHR 343 extern char* strchr( char const *s, int c); 344 extern char* strrchr( char const *s, int c); 345 #endif 346 347 /* 348 * Define and initialize all the user visible strings. 349 * We do not do translations. If translations are to be done, then 350 * the client will provide a callback for that purpose. 351 */ 352 #undef DO_TRANSLATIONS 353 #include "autoopts/usage-txt.h" 354 355 /* 356 * File pointer for usage output 357 */ 358 extern FILE* option_usage_fp; 359 360 extern tOptProc optionPrintVersion, optionPagedUsage, optionLoadOpt; 361 362 #endif /* AUTOGEN_AUTOOPTS_H */ 363 /* 364 * Local Variables: 365 * mode: C 366 * c-file-style: "stroustrup" 367 * indent-tabs-mode: nil 368 * End: 369 * end of autoopts/autoopts.h */ 370