scanopt.h revision 228072
11541Srgrimes/* flex - tool to generate fast lexical analyzers */
21541Srgrimes
31541Srgrimes/*  Copyright (c) 1990 The Regents of the University of California. */
41541Srgrimes/*  All rights reserved. */
51541Srgrimes
61541Srgrimes/*  This code is derived from software contributed to Berkeley by */
71541Srgrimes/*  Vern Paxson. */
81541Srgrimes
91541Srgrimes/*  The United States Government has rights in this work pursuant */
101541Srgrimes/*  to contract no. DE-AC03-76SF00098 between the United States */
111541Srgrimes/*  Department of Energy and the University of California. */
121541Srgrimes
131541Srgrimes/*  This file is part of flex. */
141541Srgrimes
151541Srgrimes/*  Redistribution and use in source and binary forms, with or without */
161541Srgrimes/*  modification, are permitted provided that the following conditions */
171541Srgrimes/*  are met: */
181541Srgrimes
191541Srgrimes/*  1. Redistributions of source code must retain the above copyright */
201541Srgrimes/*     notice, this list of conditions and the following disclaimer. */
211541Srgrimes/*  2. Redistributions in binary form must reproduce the above copyright */
221541Srgrimes/*     notice, this list of conditions and the following disclaimer in the */
231541Srgrimes/*     documentation and/or other materials provided with the distribution. */
241541Srgrimes
251541Srgrimes/*  Neither the name of the University nor the names of its contributors */
261541Srgrimes/*  may be used to endorse or promote products derived from this software */
271541Srgrimes/*  without specific prior written permission. */
281541Srgrimes
291541Srgrimes/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
301541Srgrimes/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
311541Srgrimes/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
321541Srgrimes/*  PURPOSE. */
331541Srgrimes
3450477Speter#ifndef SCANOPT_H
351541Srgrimes#define SCANOPT_H
361541Srgrimes
371541Srgrimes#include "flexdef.h"
381541Srgrimes
391541Srgrimes
401541Srgrimes#ifndef NO_SCANOPT_USAGE
411541Srgrimes/* Used by scanopt_usage for pretty-printing. */
421541Srgrimes#ifdef HAVE_NCURSES_H
4312577Sbde#include <ncurses.h>
441541Srgrimes#endif
451541Srgrimes#endif
4624206Sbde
4724206Sbde#ifdef __cplusplus
481541Srgrimesextern  "C" {
493308Sphk#endif
5012517Sjulian#ifndef PROTO
5129357Speter#define PROTO(args) args
5241086Struckman#endif
5312517Sjulian/* Error codes. */ enum scanopt_err_t {
541541Srgrimes		SCANOPT_ERR_OPT_UNRECOGNIZED = -1,	/* Unrecognized option. */
551541Srgrimes		SCANOPT_ERR_OPT_AMBIGUOUS = -2,	/* It matched more than one option name. */
561541Srgrimes		SCANOPT_ERR_ARG_NOT_FOUND = -3,	/* The required arg was not found. */
571541Srgrimes		SCANOPT_ERR_ARG_NOT_ALLOWED = -4	/* Option does not take an argument. */
581541Srgrimes	};
5912675Sjulian
6012675Sjulian
6112675Sjulian/* flags passed to scanopt_init */
6212675Sjulian	enum scanopt_flag_t {
6329357Speter		SCANOPT_NO_ERR_MSG = 0x01	/* Suppress printing to stderr. */
6412675Sjulian	};
6512675Sjulian
6647625Sphk/* Specification for a single option. */
6747625Sphk	struct optspec_t {
6847625Sphk		const char *opt_fmt;	/* e.g., "--foo=FILE", "-f FILE", "-n [NUM]" */
6947625Sphk		int     r_val;	/* Value to be returned by scanopt_ex(). */
7047625Sphk		const char *desc;	/* Brief description of this option, or NULL. */
7147625Sphk	};
7247625Sphk	typedef struct optspec_t optspec_t;
7347625Sphk
7447625Sphk
7547625Sphk/* Used internally by scanopt() to maintain state. */
7647625Sphk/* Never modify these value directly. */
7747625Sphk	typedef void *scanopt_t;
7847625Sphk
7947625Sphk
8047625Sphk/* Initializes scanner and checks option list for errors.
8147625Sphk * Parameters:
8247625Sphk *   options - Array of options.
8347625Sphk *   argc    - Same as passed to main().
8447625Sphk *   argv    - Same as passed to main(). First element is skipped.
8547625Sphk *   flags   - Control behavior.
8647625Sphk * Return:  A malloc'd pointer .
8712675Sjulian */
8812819Sphk	scanopt_t *scanopt_init PROTO ((const optspec_t * options,
891541Srgrimes					int argc, char **argv, int flags));
901541Srgrimes
9141087Struckman/* Frees memory used by scanner.
921541Srgrimes * Always returns 0. */
931541Srgrimes	int scanopt_destroy PROTO ((scanopt_t * scanner));
941541Srgrimes
951541Srgrimes#ifndef NO_SCANOPT_USAGE
961541Srgrimes/* Prints a usage message based on contents of optlist.
9712675Sjulian * Parameters:
981541Srgrimes *   scanner  - The scanner, already initialized with scanopt_init().
991541Srgrimes *   fp       - The file stream to write to.
1001541Srgrimes *   usage    - Text to be prepended to option list. May be NULL.
1011541Srgrimes * Return:  Always returns 0 (zero).
1021541Srgrimes */
1031541Srgrimes	int scanopt_usage
1041541Srgrimes		PROTO (
1051541Srgrimes		       (scanopt_t * scanner, FILE * fp,
10641086Struckman			const char *usage));
1071541Srgrimes#endif
1081541Srgrimes
1091541Srgrimes/* Scans command-line options in argv[].
1101541Srgrimes * Parameters:
11112675Sjulian *   scanner  - The scanner, already initialized with scanopt_init().
1121541Srgrimes *   optarg   - Return argument, may be NULL.
1131541Srgrimes *              On success, it points to start of an argument.
1141541Srgrimes *   optindex - Return argument, may be NULL.
1151541Srgrimes *              On success or failure, it is the index of this option.
1161541Srgrimes *              If return is zero, then optindex is the NEXT valid option index.
1171541Srgrimes *
1181541Srgrimes * Return:  > 0 on success. Return value is from optspec_t->rval.
1191541Srgrimes *         == 0 if at end of options.
12041086Struckman *          < 0 on error (return value is an error code).
1211541Srgrimes *
1221541Srgrimes */
1231541Srgrimes	int scanopt
1241541Srgrimes		PROTO (
12512675Sjulian		       (scanopt_t * scanner, char **optarg,
1261541Srgrimes			int *optindex));
1271541Srgrimes
1281541Srgrimes#ifdef __cplusplus
1291541Srgrimes}
1301541Srgrimes#endif
1311541Srgrimes#endif
1321541Srgrimes/* vim:set tabstop=8 softtabstop=4 shiftwidth=4: */
1331541Srgrimes