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