| 1 | /* flex - tool to generate fast lexical analyzers */ | 
|---|
| 2 |  | 
|---|
| 3 | /*  Copyright (c) 1990 The Regents of the University of California. */ | 
|---|
| 4 | /*  All rights reserved. */ | 
|---|
| 5 |  | 
|---|
| 6 | /*  This code is derived from software contributed to Berkeley by */ | 
|---|
| 7 | /*  Vern Paxson. */ | 
|---|
| 8 |  | 
|---|
| 9 | /*  The United States Government has rights in this work pursuant */ | 
|---|
| 10 | /*  to contract no. DE-AC03-76SF00098 between the United States */ | 
|---|
| 11 | /*  Department of Energy and the University of California. */ | 
|---|
| 12 |  | 
|---|
| 13 | /*  This file is part of flex. */ | 
|---|
| 14 |  | 
|---|
| 15 | /*  Redistribution and use in source and binary forms, with or without */ | 
|---|
| 16 | /*  modification, are permitted provided that the following conditions */ | 
|---|
| 17 | /*  are met: */ | 
|---|
| 18 |  | 
|---|
| 19 | /*  1. Redistributions of source code must retain the above copyright */ | 
|---|
| 20 | /*     notice, this list of conditions and the following disclaimer. */ | 
|---|
| 21 | /*  2. Redistributions in binary form must reproduce the above copyright */ | 
|---|
| 22 | /*     notice, this list of conditions and the following disclaimer in the */ | 
|---|
| 23 | /*     documentation and/or other materials provided with the distribution. */ | 
|---|
| 24 |  | 
|---|
| 25 | /*  Neither the name of the University nor the names of its contributors */ | 
|---|
| 26 | /*  may be used to endorse or promote products derived from this software */ | 
|---|
| 27 | /*  without specific prior written permission. */ | 
|---|
| 28 |  | 
|---|
| 29 | /*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */ | 
|---|
| 30 | /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ | 
|---|
| 31 | /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ | 
|---|
| 32 | /*  PURPOSE. */ | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | #ifndef SCANOPT_H | 
|---|
| 36 | #define SCANOPT_H | 
|---|
| 37 |  | 
|---|
| 38 | #include "flexdef.h" | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef NO_SCANOPT_USAGE | 
|---|
| 42 | /* Used by scanopt_usage for pretty-printing. */ | 
|---|
| 43 | #ifdef HAVE_NCURSES_H | 
|---|
| 44 | #include <ncurses.h> | 
|---|
| 45 | #endif | 
|---|
| 46 | #endif | 
|---|
| 47 |  | 
|---|
| 48 | #ifdef __cplusplus | 
|---|
| 49 | extern  "C" { | 
|---|
| 50 | #endif | 
|---|
| 51 | #ifndef PROTO | 
|---|
| 52 | #define PROTO(args) args | 
|---|
| 53 | #endif | 
|---|
| 54 | /* Error codes. */ enum scanopt_err_t { | 
|---|
| 55 | SCANOPT_ERR_OPT_UNRECOGNIZED = -1,      /* Unrecognized option. */ | 
|---|
| 56 | SCANOPT_ERR_OPT_AMBIGUOUS = -2, /* It matched more than one option name. */ | 
|---|
| 57 | SCANOPT_ERR_ARG_NOT_FOUND = -3, /* The required arg was not found. */ | 
|---|
| 58 | SCANOPT_ERR_ARG_NOT_ALLOWED = -4        /* Option does not take an argument. */ | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 | /* flags passed to scanopt_init */ | 
|---|
| 63 | enum scanopt_flag_t { | 
|---|
| 64 | SCANOPT_NO_ERR_MSG = 0x01       /* Suppress printing to stderr. */ | 
|---|
| 65 | }; | 
|---|
| 66 |  | 
|---|
| 67 | /* Specification for a single option. */ | 
|---|
| 68 | struct optspec_t { | 
|---|
| 69 | const char *opt_fmt;    /* e.g., "--foo=FILE", "-f FILE", "-n [NUM]" */ | 
|---|
| 70 | int     r_val;  /* Value to be returned by scanopt_ex(). */ | 
|---|
| 71 | const char *desc;       /* Brief description of this option, or NULL. */ | 
|---|
| 72 | }; | 
|---|
| 73 | typedef struct optspec_t optspec_t; | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | /* Used internally by scanopt() to maintain state. */ | 
|---|
| 77 | /* Never modify these value directly. */ | 
|---|
| 78 | typedef void *scanopt_t; | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /* Initializes scanner and checks option list for errors. | 
|---|
| 83 | * Parameters: | 
|---|
| 84 | *   options - Array of options. | 
|---|
| 85 | *   argc    - Same as passed to main(). | 
|---|
| 86 | *   argv    - Same as passed to main(). First element is skipped. | 
|---|
| 87 | *   flags   - Control behavior. | 
|---|
| 88 | * Return:  A malloc'd pointer . | 
|---|
| 89 | */ | 
|---|
| 90 | scanopt_t *scanopt_init PROTO ((const optspec_t * options, | 
|---|
| 91 | int argc, char **argv, int flags)); | 
|---|
| 92 |  | 
|---|
| 93 | /* Frees memory used by scanner. | 
|---|
| 94 | * Always returns 0. */ | 
|---|
| 95 | int scanopt_destroy PROTO ((scanopt_t * scanner)); | 
|---|
| 96 |  | 
|---|
| 97 | #ifndef NO_SCANOPT_USAGE | 
|---|
| 98 | /* Prints a usage message based on contents of optlist. | 
|---|
| 99 | * Parameters: | 
|---|
| 100 | *   scanner  - The scanner, already initialized with scanopt_init(). | 
|---|
| 101 | *   fp       - The file stream to write to. | 
|---|
| 102 | *   usage    - Text to be prepended to option list. May be NULL. | 
|---|
| 103 | * Return:  Always returns 0 (zero). | 
|---|
| 104 | */ | 
|---|
| 105 | int scanopt_usage | 
|---|
| 106 | PROTO ( | 
|---|
| 107 | (scanopt_t * scanner, FILE * fp, | 
|---|
| 108 | const char *usage)); | 
|---|
| 109 | #endif | 
|---|
| 110 |  | 
|---|
| 111 | /* Scans command-line options in argv[]. | 
|---|
| 112 | * Parameters: | 
|---|
| 113 | *   scanner  - The scanner, already initialized with scanopt_init(). | 
|---|
| 114 | *   optarg   - Return argument, may be NULL. | 
|---|
| 115 | *              On success, it points to start of an argument. | 
|---|
| 116 | *   optindex - Return argument, may be NULL. | 
|---|
| 117 | *              On success or failure, it is the index of this option. | 
|---|
| 118 | *              If return is zero, then optindex is the NEXT valid option index. | 
|---|
| 119 | * | 
|---|
| 120 | * Return:  > 0 on success. Return value is from optspec_t->rval. | 
|---|
| 121 | *         == 0 if at end of options. | 
|---|
| 122 | *          < 0 on error (return value is an error code). | 
|---|
| 123 | * | 
|---|
| 124 | */ | 
|---|
| 125 | int scanopt | 
|---|
| 126 | PROTO ( | 
|---|
| 127 | (scanopt_t * scanner, char **optarg, | 
|---|
| 128 | int *optindex)); | 
|---|
| 129 |  | 
|---|
| 130 | #ifdef __cplusplus | 
|---|
| 131 | } | 
|---|
| 132 | #endif | 
|---|
| 133 | #endif | 
|---|
| 134 | /* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */ | 
|---|