scanopt.h revision 250156
1230557Sjimharris/* flex - tool to generate fast lexical analyzers */
2230557Sjimharris
3230557Sjimharris/*  Copyright (c) 1990 The Regents of the University of California. */
4230557Sjimharris/*  All rights reserved. */
5230557Sjimharris
6230557Sjimharris/*  This code is derived from software contributed to Berkeley by */
7230557Sjimharris/*  Vern Paxson. */
8230557Sjimharris
9230557Sjimharris/*  The United States Government has rights in this work pursuant */
10230557Sjimharris/*  to contract no. DE-AC03-76SF00098 between the United States */
11230557Sjimharris/*  Department of Energy and the University of California. */
12230557Sjimharris
13230557Sjimharris/*  This file is part of flex. */
14230557Sjimharris
15230557Sjimharris/*  Redistribution and use in source and binary forms, with or without */
16230557Sjimharris/*  modification, are permitted provided that the following conditions */
17230557Sjimharris/*  are met: */
18230557Sjimharris
19230557Sjimharris/*  1. Redistributions of source code must retain the above copyright */
20230557Sjimharris/*     notice, this list of conditions and the following disclaimer. */
21230557Sjimharris/*  2. Redistributions in binary form must reproduce the above copyright */
22230557Sjimharris/*     notice, this list of conditions and the following disclaimer in the */
23230557Sjimharris/*     documentation and/or other materials provided with the distribution. */
24230557Sjimharris
25230557Sjimharris/*  Neither the name of the University nor the names of its contributors */
26230557Sjimharris/*  may be used to endorse or promote products derived from this software */
27230557Sjimharris/*  without specific prior written permission. */
28230557Sjimharris
29230557Sjimharris/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30230557Sjimharris/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31230557Sjimharris/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32230557Sjimharris/*  PURPOSE. */
33230557Sjimharris
34230557Sjimharris#ifndef SCANOPT_H
35230557Sjimharris#define SCANOPT_H
36230557Sjimharris
37230557Sjimharris#include "flexdef.h"
38230557Sjimharris
39230557Sjimharris
40230557Sjimharris#ifndef NO_SCANOPT_USAGE
41230557Sjimharris/* Used by scanopt_usage for pretty-printing. */
42230557Sjimharris#ifdef HAVE_NCURSES_H
43230557Sjimharris#include <ncurses.h>
44230557Sjimharris#endif
45230557Sjimharris#endif
46230557Sjimharris
47230557Sjimharris#ifdef __cplusplus
48230557Sjimharrisextern  "C" {
49230557Sjimharris#endif
50230557Sjimharris#ifndef PROTO
51230557Sjimharris#define PROTO(args) args
52230557Sjimharris#endif
53230557Sjimharris/* Error codes. */ enum scanopt_err_t {
54230557Sjimharris		SCANOPT_ERR_OPT_UNRECOGNIZED = -1,	/* Unrecognized option. */
55230557Sjimharris		SCANOPT_ERR_OPT_AMBIGUOUS = -2,	/* It matched more than one option name. */
56230557Sjimharris		SCANOPT_ERR_ARG_NOT_FOUND = -3,	/* The required arg was not found. */
57230557Sjimharris		SCANOPT_ERR_ARG_NOT_ALLOWED = -4	/* Option does not take an argument. */
58230557Sjimharris	};
59230557Sjimharris
60230557Sjimharris
61230557Sjimharris/* flags passed to scanopt_init */
62230557Sjimharris	enum scanopt_flag_t {
63230557Sjimharris		SCANOPT_NO_ERR_MSG = 0x01	/* Suppress printing to stderr. */
64230557Sjimharris	};
65230557Sjimharris
66230557Sjimharris/* Specification for a single option. */
67230557Sjimharris	struct optspec_t {
68230557Sjimharris		const char *opt_fmt;	/* e.g., "--foo=FILE", "-f FILE", "-n [NUM]" */
69230557Sjimharris		int     r_val;	/* Value to be returned by scanopt_ex(). */
70230557Sjimharris		const char *desc;	/* Brief description of this option, or NULL. */
71230557Sjimharris	};
72230557Sjimharris	typedef struct optspec_t optspec_t;
73230557Sjimharris
74230557Sjimharris
75230557Sjimharris/* Used internally by scanopt() to maintain state. */
76230557Sjimharris/* Never modify these value directly. */
77230557Sjimharris	typedef void *scanopt_t;
78230557Sjimharris
79230557Sjimharris
80230557Sjimharris/* Initializes scanner and checks option list for errors.
81230557Sjimharris * Parameters:
82230557Sjimharris *   options - Array of options.
83230557Sjimharris *   argc    - Same as passed to main().
84230557Sjimharris *   argv    - Same as passed to main(). First element is skipped.
85230557Sjimharris *   flags   - Control behavior.
86230557Sjimharris * Return:  A malloc'd pointer .
87230557Sjimharris */
88230557Sjimharris	scanopt_t *scanopt_init PROTO ((const optspec_t * options,
89230557Sjimharris					int argc, char **argv, int flags));
90230557Sjimharris
91230557Sjimharris/* Frees memory used by scanner.
92230557Sjimharris * Always returns 0. */
93230557Sjimharris	int scanopt_destroy PROTO ((scanopt_t * scanner));
94230557Sjimharris
95230557Sjimharris#ifndef NO_SCANOPT_USAGE
96230557Sjimharris/* Prints a usage message based on contents of optlist.
97230557Sjimharris * Parameters:
98230557Sjimharris *   scanner  - The scanner, already initialized with scanopt_init().
99230557Sjimharris *   fp       - The file stream to write to.
100230557Sjimharris *   usage    - Text to be prepended to option list. May be NULL.
101230557Sjimharris * Return:  Always returns 0 (zero).
102230557Sjimharris */
103230557Sjimharris	int scanopt_usage
104230557Sjimharris		PROTO (
105230557Sjimharris		       (scanopt_t * scanner, FILE * fp,
106230557Sjimharris			const char *usage));
107230557Sjimharris#endif
108230557Sjimharris
109230557Sjimharris/* Scans command-line options in argv[].
110230557Sjimharris * Parameters:
111230557Sjimharris *   scanner  - The scanner, already initialized with scanopt_init().
112230557Sjimharris *   optarg   - Return argument, may be NULL.
113230557Sjimharris *              On success, it points to start of an argument.
114230557Sjimharris *   optindex - Return argument, may be NULL.
115230557Sjimharris *              On success or failure, it is the index of this option.
116230557Sjimharris *              If return is zero, then optindex is the NEXT valid option index.
117230557Sjimharris *
118230557Sjimharris * Return:  > 0 on success. Return value is from optspec_t->rval.
119230557Sjimharris *         == 0 if at end of options.
120230557Sjimharris *          < 0 on error (return value is an error code).
121230557Sjimharris *
122230557Sjimharris */
123230557Sjimharris	int scanopt
124230557Sjimharris		PROTO (
125230557Sjimharris		       (scanopt_t * scanner, char **optarg,
126230557Sjimharris			int *optindex));
127230557Sjimharris
128230557Sjimharris#ifdef __cplusplus
129230557Sjimharris}
130230557Sjimharris#endif
131230557Sjimharris#endif
132230557Sjimharris/* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */
133230557Sjimharris