scanopt.h revision 259065
1133819Stjr/* flex - tool to generate fast lexical analyzers */
2133819Stjr
3133819Stjr/*  Copyright (c) 1990 The Regents of the University of California. */
4133819Stjr/*  All rights reserved. */
5133819Stjr
6283371Sdchagin/*  This code is derived from software contributed to Berkeley by */
7133819Stjr/*  Vern Paxson. */
8133819Stjr
9133819Stjr/*  The United States Government has rights in this work pursuant */
10133819Stjr/*  to contract no. DE-AC03-76SF00098 between the United States */
11133819Stjr/*  Department of Energy and the University of California. */
12133819Stjr
13133819Stjr/*  This file is part of flex. */
14177999Skib
15227776Slstewart/*  Redistribution and use in source and binary forms, with or without */
16164199Sru/*  modification, are permitted provided that the following conditions */
17133819Stjr/*  are met: */
18255673Srdivacky
19133819Stjr/*  1. Redistributions of source code must retain the above copyright */
20161330Sjhb/*     notice, this list of conditions and the following disclaimer. */
21161330Sjhb/*  2. Redistributions in binary form must reproduce the above copyright */
22133819Stjr/*     notice, this list of conditions and the following disclaimer in the */
23133819Stjr/*     documentation and/or other materials provided with the distribution. */
24133819Stjr
25133819Stjr/*  Neither the name of the University nor the names of its contributors */
26133819Stjr/*  may be used to endorse or promote products derived from this software */
27133819Stjr/*  without specific prior written permission. */
28133819Stjr
29133819Stjr/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30133819Stjr/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31133819Stjr/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32133819Stjr/*  PURPOSE. */
33133819Stjr
34133819Stjr#ifndef SCANOPT_H
35133819Stjr#define SCANOPT_H
36133819Stjr
37143198Ssobomax#include "flexdef.h"
38283371Sdchagin
39283371Sdchagin
40283371Sdchagin#ifndef NO_SCANOPT_USAGE
41133819Stjr/* Used by scanopt_usage for pretty-printing. */
42133819Stjr#ifdef HAVE_NCURSES_H
43133819Stjr#include <ncurses.h>
44133819Stjr#endif
45133819Stjr#endif
46133819Stjr
47133819Stjr#ifdef __cplusplus
48133819Stjrextern  "C" {
49133819Stjr#endif
50133819Stjr#ifndef PROTO
51133819Stjr#define PROTO(args) args
52133819Stjr#endif
53133819Stjr/* Error codes. */ enum scanopt_err_t {
54133819Stjr		SCANOPT_ERR_OPT_UNRECOGNIZED = -1,	/* Unrecognized option. */
55133819Stjr		SCANOPT_ERR_OPT_AMBIGUOUS = -2,	/* It matched more than one option name. */
56133819Stjr		SCANOPT_ERR_ARG_NOT_FOUND = -3,	/* The required arg was not found. */
57133819Stjr		SCANOPT_ERR_ARG_NOT_ALLOWED = -4	/* Option does not take an argument. */
58133819Stjr	};
59133819Stjr
60133819Stjr
61133819Stjr/* flags passed to scanopt_init */
62133819Stjr	enum scanopt_flag_t {
63133819Stjr		SCANOPT_NO_ERR_MSG = 0x01	/* Suppress printing to stderr. */
64133819Stjr	};
65133819Stjr
66133819Stjr/* Specification for a single option. */
67236027Sed	struct optspec_t {
68236027Sed		const char *opt_fmt;	/* e.g., "--foo=FILE", "-f FILE", "-n [NUM]" */
69133819Stjr		int     r_val;	/* Value to be returned by scanopt_ex(). */
70133819Stjr		const char *desc;	/* Brief description of this option, or NULL. */
71133819Stjr	};
72133819Stjr	typedef struct optspec_t optspec_t;
73133819Stjr
74133819Stjr
75133819Stjr/* Used internally by scanopt() to maintain state. */
76133819Stjr/* Never modify these value directly. */
77133819Stjr	typedef void *scanopt_t;
78133819Stjr
79133819Stjr
80133819Stjr/* Initializes scanner and checks option list for errors.
81133819Stjr * Parameters:
82133819Stjr *   options - Array of options.
83133819Stjr *   argc    - Same as passed to main().
84133819Stjr *   argv    - Same as passed to main(). First element is skipped.
85133819Stjr *   flags   - Control behavior.
86133819Stjr * Return:  A malloc'd pointer .
87133819Stjr */
88133819Stjr	scanopt_t *scanopt_init PROTO ((const optspec_t * options,
89133819Stjr					int argc, char **argv, int flags));
90156919Snetchild
91156919Snetchild/* Frees memory used by scanner.
92156919Snetchild * Always returns 0. */
93156919Snetchild	int scanopt_destroy PROTO ((scanopt_t * scanner));
94133819Stjr
95133819Stjr#ifndef NO_SCANOPT_USAGE
96133819Stjr/* Prints a usage message based on contents of optlist.
97133819Stjr * Parameters:
98133819Stjr *   scanner  - The scanner, already initialized with scanopt_init().
99133819Stjr *   fp       - The file stream to write to.
100133819Stjr *   usage    - Text to be prepended to option list. May be NULL.
101133819Stjr * Return:  Always returns 0 (zero).
102133819Stjr */
103133819Stjr	int scanopt_usage
104133819Stjr		PROTO (
105133819Stjr		       (scanopt_t * scanner, FILE * fp,
106133819Stjr			const char *usage));
107133819Stjr#endif
108133819Stjr
109133819Stjr/* Scans command-line options in argv[].
110133819Stjr * Parameters:
111133819Stjr *   scanner  - The scanner, already initialized with scanopt_init().
112133819Stjr *   optarg   - Return argument, may be NULL.
113133819Stjr *              On success, it points to start of an argument.
114133819Stjr *   optindex - Return argument, may be NULL.
115133819Stjr *              On success or failure, it is the index of this option.
116133819Stjr *              If return is zero, then optindex is the NEXT valid option index.
117133819Stjr *
118133819Stjr * Return:  > 0 on success. Return value is from optspec_t->rval.
119133819Stjr *         == 0 if at end of options.
120133819Stjr *          < 0 on error (return value is an error code).
121133819Stjr *
122133819Stjr */
123133819Stjr	int scanopt
124133819Stjr		PROTO (
125133819Stjr		       (scanopt_t * scanner, char **optarg,
126133819Stjr			int *optindex));
127133819Stjr
128133819Stjr#ifdef __cplusplus
129133819Stjr}
130133819Stjr#endif
131133819Stjr#endif
132133819Stjr/* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */
133133819Stjr