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