view tools/options.h @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
line wrap: on
line source
/*
The option type field controls the interpretation and syntax of individual args.
Option types are specified by OR'ing bit patterns which define the syntax for
the argument and the operation by which the argument is combined with the
default value. Additional bits refer to side-effects which are effects a
specific option may have which are not related to its syntax or its essential
operation.

Option syntax:
    FLAG_SYNTAX          -<name>
    ARG_SYNTAX           -<name>[=]<value>
    EQ_SYNTAX           [-]<name>=<value>
    ONOFF_SYNTAX         -<name>[[=]<value>]
    VAL_SYNTAX           -<name>[=]<value>    |  [-]<name>=<value>
    TOGGLE_SYNTAX        -<name>              |  [-]<name>=<value>
    ALL_SYNTAX           -<name>[[=]<value>]  |  [-]<name>=<value>

Option operations:
    LATCH       Copy the argument value.
    AND         Turn off, (arg effects default only when default is "on")
    OR          Turn on,  (arg effects default only when default is "off")
    TOGGLE      Invert the current default value (on/off), (unless forced by its value)

Side effects:
    SILENT      Don't print help line.
    EXCLUDE     Invert all other EXCLUDE arguments with respect to the current one.

Options of EXCLUSIVE type are a set in which one is "on" and the rest "off".
The value is allowed to be empty or "on", so that an option <name> by itself
can count as "on".

Options of SILENT type are not printed by standard help, unless specifically
named (eg help=<name>) or all options are asked for (eg help=all).


Some combined option types:
    FLAG        Command-line flag: -flag (toggle current default).
    SETFLAG     Command-line flag: -flag (toggle current default) or flag=on/off.
    ARG         Command-line argument: -argval (latch arg to val).
    EQ          Command-line argument: arg=val (latch arg to val).
    VAL         Command-line argument, combined style (latch arg to val).
    EX_OR       Mutually exclusive option value. Set option value "on", and
		all other EXCLUDE options "off".
    EX_AND      Mutually exclusive option value. Set option value "off", and
		all other EXCLUDE options "on".
    EX_TOGGLE   Mutually exclusive option value. Set option to given value,
		("on"/"off") and toggle all other EXCLUDE options ("off"/"on")
    DEBUG       Silent toggle (synonymous with STOG).
    STOG        Silent toggle.
    SVAL        Silent argument (combined-style latch).


Summary of main option types:

------------------------------------------------------------
Name        Operation   Side-effect     Purpose
---------   ---------   -----------     --------------------

(1) With syntax "-<name>"

FLAG        toggle                      Toggle flag default value.
EX_AND      and         exclude         Set flag off and all others on.

(2) With syntax "-<name> | [-]<name>=on|off"

SETFLAG     toggle                      Set flag on|off.
EX_OR       or          exclude         Set flag on and all others off.
EX_TOGGLE   toggle      exclude         Set flag on|off and all others off|on.
DEBUG       toggle      silent          Set flag silently
STOG        toggle      silent           "    "     "

(3) With syntax "-<name>[=]<value> | [-]<name>=<value>"

VAL         latch                       Latch arg to value.
SVAL        latch       silent          Latch arg to value silently.

*/

/* option syntax (base types) */

#define FLAG_SYNTAX     (  1   )    /*    -<name>           */
#define ARG_SYNTAX      (  2   )    /*    -<name>[=]<value> */
#define EQ_SYNTAX       (  4   )    /*   [-]<name>=<value>  */

/* operations between vals */

#define LATCH           (  8   )
#define AND             (  16  )
#define OR              (  32  )
#define TOGGLE          (  64  )

/* side-effects */

#define SILENT          (  128 )
#define EXCLUDE         (  256 )

/* option syntax combinations */

#define ONOFF_SYNTAX    ( FLAG_SYNTAX | ARG_SYNTAX              )
#define TOGGLE_SYNTAX   ( FLAG_SYNTAX |              EQ_SYNTAX  )
#define VAL_SYNTAX      (               ARG_SYNTAX | EQ_SYNTAX  )
#define ALL_SYNTAX      ( FLAG_SYNTAX | ARG_SYNTAX | EQ_SYNTAX  )

/* combined option types */

#define FLAG            ( TOGGLE | FLAG_SYNTAX                  )
#define SETFLAG         ( TOGGLE | TOGGLE_SYNTAX                )
#define ARG             ( LATCH | ARG_SYNTAX                    )
#define EQ              ( LATCH | EQ_SYNTAX                     )
#define VAL             ( LATCH | VAL_SYNTAX                    )

#define EX_OR           ( EXCLUDE | OR     | TOGGLE_SYNTAX      )
#define EX_AND          ( EXCLUDE | AND    | FLAG_SYNTAX        )
#define EX_TOGGLE       ( EXCLUDE | TOGGLE | TOGGLE_SYNTAX      )
#define DEBUG           ( SILENT | SETFLAG                      )
#define STOG            ( SILENT | SETFLAG                      )
#define SVAL            ( SILENT | VAL                          )

/* bit-testing operations */

#define and(x1,x2)      ( (x1)&(x2) )
#define bitset(x,bits)  ( and(x,bits) ? 1 : 0 )
#define bitsset(x,bits) ( ( and(x,bits) == bits ) ? 1 : 0 )

/* error flags */

#define UNKNOWN         ( -1 )
#define AMBIGUOUS       ( -2 )
#define BADVAL          ( -3 )

/* options structure */

typedef struct {
  char  *name ;         /* option name                                     */
  char  *dflt ;         /* default value (string)                          */
  char **val  ;         /* pointer to current value (string)               */
  char  *help ;         /* help message (max 15 chars)                     */
  unsigned int type ;   /* type of option (syntax, operation, side-effect) */
} Options ;


/* datatype list */

static char *datatype[] = {
	"char"  ,
	"short" ,
	"int"   ,
	"float" ,
	"double",
	"ASCII" ,
	"ascii" ,
   ( char * ) 0 } ;




/*
  types of helpopts

helpopts   standard usage, exit when done
helpopts1  standard usage, supplied function for exit or additional help
helpopts2  supplied usage and function for exit or additional help
helpopts3  supplied usage, exit when done
*/

#define helpopts(helpstr,prog,applic,option)             gethelp( helpstr, prog, applic, (char *)0, option, exit )
#define helpopts1(helpstr,prog,applic,option,tail)       gethelp( helpstr, prog, applic, (char *)0, option, tail )
#define helpopts2(helpstr,prog,applic,usage,option,tail) gethelp( helpstr, prog, applic, usage,     option, tail )
#define helpopts3(helpstr,prog,applic,usage,option)      gethelp( helpstr, prog, applic, usage,     option, exit )


/* option syntax (for help messages) */

static char Flag_Syntax[]   = "-<name>"                                 ;
static char Arg_Syntax[]    = "-<name>[=]<value>"                       ;
static char Eq_Syntax[]     = "[-]<name>=<value>"                       ;
static char Onoff_Syntax[]  = "-<name>[[=]<value>]"                     ;
static char Val_Syntax[]    = "-<name>[=]<value> | [-]<name>=<value>"   ;
static char Toggle_Syntax[] = "-<name> | [-]<name>=on|off"              ;
static char All_Syntax[]    = "-<name>[[=]<value>] | [-]<name>=<value>" ;

static char onstr[]  = "on"  ;
static char offstr[] = "off" ;


/* External declarations for variables and functions defined in options.c */

int   LINE_LENGTH   ;
int   PRECISION     ;   /* for printf */
int   FIELDWIDTH    ;   /* for printf */

int   ison()        ;
int   isoff()       ;
int   ismin()       ;
int   ismax()       ;

int   isopt()       ;
int   optindex()    ;
int   whichopt()    ;
char *optdflt()     ;
char *checksyntax() ;

int   getopts()     ;
int   getvals()     ;
int   selector()    ;
int   range()       ;
int   seekstart()   ;
int   typeindex()   ;
int   databytes()   ;

float check_overflow() ;

FILE *openopts()    ;
FILE *fropen()      ;


int   exit()        ;