xref: /freebsd/contrib/flex/src/scanopt.h (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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 #ifndef SCANOPT_H
35 #define SCANOPT_H
36 
37 #include "flexdef.h"
38 
39 
40 #ifndef NO_SCANOPT_USAGE
41 /* Used by scanopt_usage for pretty-printing. */
42 #ifdef HAVE_NCURSES_H
43 #include <ncurses.h>
44 #endif
45 #endif
46 
47 #ifdef __cplusplus
48 extern  "C" {
49 #endif
50 /* Error codes. */ enum scanopt_err_t {
51 		SCANOPT_ERR_OPT_UNRECOGNIZED = -1,	/* Unrecognized option. */
52 		SCANOPT_ERR_OPT_AMBIGUOUS = -2,	/* It matched more than one option name. */
53 		SCANOPT_ERR_ARG_NOT_FOUND = -3,	/* The required arg was not found. */
54 		SCANOPT_ERR_ARG_NOT_ALLOWED = -4	/* Option does not take an argument. */
55 	};
56 
57 
58 /* flags passed to scanopt_init */
59 	enum scanopt_flag_t {
60 		SCANOPT_NO_ERR_MSG = 0x01	/* Suppress printing to stderr. */
61 	};
62 
63 /* Specification for a single option. */
64 	struct optspec_t {
65 		const char *opt_fmt;	/* e.g., "--foo=FILE", "-f FILE", "-n [NUM]" */
66 		int     r_val;	/* Value to be returned by scanopt_ex(). */
67 		const char *desc;	/* Brief description of this option, or NULL. */
68 	};
69 	typedef struct optspec_t optspec_t;
70 
71 
72 /* Used internally by scanopt() to maintain state. */
73 /* Never modify these value directly. */
74 	typedef void *scanopt_t;
75 
76 
77 /* Initializes scanner and checks option list for errors.
78  * Parameters:
79  *   options - Array of options.
80  *   argc    - Same as passed to main().
81  *   argv    - Same as passed to main(). First element is skipped.
82  *   flags   - Control behavior.
83  * Return:  A malloc'd pointer .
84  */
85 	scanopt_t *scanopt_init (const optspec_t * options, int argc,
86 				 char **argv, int flags);
87 
88 /* Frees memory used by scanner.
89  * Always returns 0. */
90 	int scanopt_destroy (scanopt_t * scanner);
91 
92 #ifndef NO_SCANOPT_USAGE
93 /* Prints a usage message based on contents of optlist.
94  * Parameters:
95  *   scanner  - The scanner, already initialized with scanopt_init().
96  *   fp       - The file stream to write to.
97  *   usage    - Text to be prepended to option list. May be NULL.
98  * Return:  Always returns 0 (zero).
99  */
100 	int scanopt_usage (scanopt_t * scanner, FILE * fp, const char *usage);
101 #endif
102 
103 /* Scans command-line options in argv[].
104  * Parameters:
105  *   scanner  - The scanner, already initialized with scanopt_init().
106  *   optarg   - Return argument, may be NULL.
107  *              On success, it points to start of an argument.
108  *   optindex - Return argument, may be NULL.
109  *              On success or failure, it is the index of this option.
110  *              If return is zero, then optindex is the NEXT valid option index.
111  *
112  * Return:  > 0 on success. Return value is from optspec_t->rval.
113  *         == 0 if at end of options.
114  *          < 0 on error (return value is an error code).
115  *
116  */
117 	int scanopt (scanopt_t * scanner, char **optarg, int *optindex);
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 #endif
123 /* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */
124