📄 opt.c
字号:
/*opt.c version 3.17 ... modified for Cygwin windows compatibility (varargs.h replaced by stdarg.h)OPT is a subroutine library which facilitates the convenient input of parameters to a C or C++ program. Parameters are parsed from a command line, with further facilities for reading options from files, from environment strings, or from an interactive environment. The aim of the opt package is to permit programs to be both user- and programmer- friendly. The package attempts to on the one hand provide a direct and relatively full-featured input interface to the ultimate user of the program, and at the same time impose a minimal amount of work on the programmer to "attach" the package to his or her software. A texinfo file is part of the distribution; or you can view the html documentation at:http://nis-www.lanl.gov/~jt/Software/opt/opt_toc.htmlDownload the lastest version of the source code from:http://nis-www.lanl.gov/~jt/Software/opt is available to the public under the GNU General Public License:http://www.gnu.org/copyleft/gpl.htmlThis SOFTWARE has been authored by an employee of the University ofCalifornia, operator of the Los Alamos National Laboratory underContract No. W-7405-ENG-36 with the U.S. Department of Energy. TheU.S. Government has rights to use, reproduce, and distribute thisSOFTWARE. The public may copy, distribute, prepare derivative worksand publicly display this SOFTWARE without charge, provided that thisNotice and any statement of authorship are reproduced on allcopies. Neither the Government nor the University makes any warranty,express or implied, or assumes any liability or responsibility for theuse of this SOFTWARE. If SOFTWARE is modified to produce derivativeworks, such modified SOFTWARE should be clearly marked, so as not toconfuse it with the version available from LANL.*/#define SINGLEFILE 1#ifndef DISABLE_VARARGS#define DISABLE_VARARGS 0#endif#ifndef DISABLE_LONGJUMP#define DISABLE_LONGJUMP 0#endif#ifndef DISABLE_SIGNAL#define DISABLE_SIGNAL 0#endif#include <stdio.h>#ifdef convex#include <strings.h>#else#include <string.h>#endif#ifdef __TURBOC__#include <stdlib.h>#include <alloc.h>#endif#include <ctype.h>#if !DISABLE_LONGJUMP#include <setjmp.h>#endif#if !DISABLE_SIGNAL#include <signal.h>#endif#if !DISABLE_VARARG#ifdef __TURBOC__#include <stdarg.h>#else#include <stdarg.h>#endif#endifstatic char gstr[160]; /* generally useful global string *//* opt.h *//* User Include File for options package*/#ifndef _OPT_H#define _OPT_H /* Signal that this header file has been included *//* * These are delimiter characters */#define DELIM '-' /* option delimiter character */#define ALTDELIM '/' /* alternate delimiter character */#define OPTFROMFILE '@' /* denotes options are in a file */#define OPTTOFILE '%' /* says to put options in a file */#define DOCUMENT '-' /* write document to file */#define INTERACT '$' /* Flags interactive menu */#define HELPCH '?' /* Help character *//* * These are not available on command line * But may be invoked from a file */#define IGNOREEOL ';' /* Ignore until the end of line */#define RUN '=' /* Says to just run to completion */#define QUITCH '.' /* Quit character *//* * These are not available on command line or from a file * But may be invoked from the menu */#define BANG '!' /* Shell escape character */#define ADDITIONAL_OPTS '+' /* Additional options */typedef enum { INT, UNSINT, LONG, CHAR, INTLEVEL, FLOAT, DOUBLE, FLAG, NEGFLAG, ABSFLAG, ABSNEGFLAG, VSTRING, CSTRING, UNDELIMV, UNDELIMC } opt_TYPE;typedef int (*PFI)();#define OPT_EXT ".opt" /* standard options file extension */#ifndef TRUE#define TRUE 1#define FALSE 0#endif#ifndef ARGCHECK#ifdef __TURBOC__ #define ARGCHECK#endif /* __TURBOC__ */#ifdef __cplusplus #define ARGCHECK#endif /* __cplusplus */#endif /* ARGCHECK */#ifdef ARGCHECK extern int opt_register(char *,opt_TYPE,char,char *); extern void opt_usage_set(char *); extern void opt_title_set(char *); extern void opt_verify_set(PFI); extern void opt_doc_set(PFI); extern void opt_quit_set(PFI); extern void opt_run_set(PFI); extern void opt_help_setf(PFI); extern void opt_help_set(char,char *); extern void opt_ufilter_set(PFI); extern void opt_dfilter_set(PFI); extern void opt_env_set(char *); extern void opt_default_set(char *);#if DISABLE_VARARGS extern void opt_errmess(char *); extern void opt_message(char *); extern void opt_warning(char *); extern void opt_fatal(char *);#else extern void opt_errmess(); extern void opt_message(); extern void opt_warning(); extern void opt_fatal();#endif /* DISABLE_VARARGS */ extern void opt_abort_run(void); extern int opt_begin_run(PFI); extern int getopts(int,char **);#else extern int opt_register(); extern void opt_usage_set(); extern void opt_title_set(); extern void opt_verify_set(); extern void opt_doc_set(); extern void opt_quit_set(); extern void opt_run_set(); extern void opt_help_setf(); extern void opt_help_set(); extern void opt_help_set(); extern void opt_ufilter_set(); extern void opt_dfilter_set(); extern void opt_env_set(); extern void opt_default_set(); extern void opt_errmess(); extern void opt_message(); extern void opt_warning(); extern void opt_fatal(); extern void opt_abort_run(); extern int opt_begin_run(); extern int getopts();#endif /* ARGCHECK *//********************************* * Macro's for registering options */#define optregister(val,typ,c,str) opt_register((char *)&val,typ,c,str)#define optrunset(r) {int r(); opt_run_set( r );}#endif /* _OPT_H *//* ag.h */#ifndef _AG_H#define _AG_H#ifndef TRUE#define TRUE 1#define FALSE 0#endif/************************************* * ARGVECTOR structure * Basically, an (argc,argv) construct * with indices to which character of * which word is the current position */typedef struct { char **v; /* argument vector */ int c; /* argument count */ int iw; /* current word */ int ic; /* current character */ } ARGVECTOR;/* * Function prototypes */#ifdef ARGCHECKextern ARGVECTOR *ag_new(int,char **,ARGVECTOR *);extern int ag_enstring(char *,ARGVECTOR *,int);extern void ag_reset(ARGVECTOR *);extern int ag_w_number(ARGVECTOR *);extern void ag_w_advance(ARGVECTOR *);extern int ag_eow(ARGVECTOR *);extern int ag_end(ARGVECTOR *);extern char ag_c(ARGVECTOR *);extern char ag_c_next(ARGVECTOR *);extern char ag_cnn_next(ARGVECTOR *);extern char ag_c_advance(ARGVECTOR *);extern char ag_backspace(ARGVECTOR *);extern char *ag_s(ARGVECTOR *);extern char *ag_s_next(ARGVECTOR *);extern char *ag_s_advance(ARGVECTOR *);extern char *argnext(ARGVECTOR *);extern double argnextnum(ARGVECTOR *);#elseextern ARGVECTOR *ag_new();extern int ag_enstring();extern void ag_reset();extern int ag_w_number();extern void ag_w_advance();extern int ag_eow();extern int ag_end();extern char ag_c();extern char ag_c_next();extern char ag_cnn_next();extern char ag_c_advance();extern char ag_backspace();extern char *ag_s();extern char *ag_s_next();extern char *ag_s_advance();extern char *argnext();extern double argnextnum();#endif /* ARGCHECK */#endif /* _AG_H *//* opt_p.h *//* * Private header file for OPT package. */#ifndef _OPT_P_H#define _OPT_P_H#define OPT_MAXSTRLEN 80#ifndef TRUE#define TRUE 1#define FALSE 0#endif#ifndef OKAY#define OKAY 0#define NOT_OKAY (-1)#endif#ifndef DEBUG#define DEBUG 0#endiftypedef enum { False=FALSE, True=TRUE } flag;/****************** * Global variables */extern int nregopts; /* Number of registered options */extern flag menuflag; /* Is menu ON? */extern flag fileflag; /* Are options being read from a file? *//********************* * Function Prototypes */#ifndef ARGCHECK#ifdef __TURBOC__#define ARGCHECK#endif /* __TURBOC__ */#endif /* ARGCHECK */#ifdef ARGCHECKextern int opt_register(char *,opt_TYPE,char,char *);extern int opt_number(char);extern int opt_fprint(FILE *,int);extern int opt_tofile(FILE *);extern char *optstrval(int);extern char *optstrtyp(int);extern int opt_undelim(ARGVECTOR *);extern int opt_delim(ARGVECTOR *);extern void opt_get_help(char);#elseextern int opt_register();extern int opt_number();extern int opt_fprint();extern int opt_tofile();extern char *optstrval();extern char *optstrtyp();extern int opt_undelim();extern int opt_delim();extern void opt_get_help();#endif /* --------------- */ /* Process Options */ /* --------------- */extern char *opt_program_name;extern PFI dfilter_fcn, ufilter_fcn, verify_fcn, doc_fcn, quit_fcn, help_fcn, run_fcn;#ifdef ARGCHECKextern void opt_wr_title(void);extern void opt_process(int,char **);extern int opt_fromfname(char *);extern int opt_tofname(char *);extern int opt_getline(char *,FILE *);extern char *opt_mstring(int);extern int opt_number(char);extern char *short_progname(char *);extern int opt_undelim(ARGVECTOR *);extern int opt_delim(ARGVECTOR *);extern void opt_menu(void);extern void usage(char *);extern void opt_help();extern void opt_quit();extern void opt_doc();extern void opt_usage(FILE *);#elseextern void opt_wr_title();extern void opt_process();extern int opt_fromfname();extern int opt_getline();extern int opt_tofname();extern int opt_delim();extern int opt_number();extern int opt_undelim();extern void opt_menu();extern char *opt_mstring();extern void opt_doc();extern void opt_help();extern void opt_quit();extern void usage();extern void opt_usage();extern char *short_progname();extern int ignore_after_char();#endif /* ARGCHECK */#endif /* _OPT_P_H *//* ag.c */#ifndef SINGLEFILE#include <stdio.h>#ifdef convex#include <strings.h>#else#include <string.h>#endif#include "ag.h"#endif/* primitive routines for manipulation argument vectors *//* make-new-argvector ag_new() reset-argvector ag_reset() flag-end-of-word ag_eow() what-is-character ag_c() what-is-string ag_s() read-character ag_c() read-next-character ag_c_next() read-next-non-null-character ag_cnn_next() read-character-advance ag_c_advance() read-string ag_s() read-next-string ag_s_next() read-string-advance ag_s_advance() word-number ag_w_number() word-advance ag_w_advance() backspace ag_backspace() clear ag_clear() get-next-argument argnext() get-next-argument-value argnextnum()*/ARGVECTOR *ag_new(argc,argv,ag)int argc;char **argv;ARGVECTOR *ag;{ ag->v = argv; ag->c = argc; ag->iw = 0; ag->ic = 0; return(ag);}ag_fprint(fp,ag)FILE *fp;ARGVECTOR *ag;{ int i; for(i=0; i<ag->c; ++i) fprintf(fp,"[%s]",ag->v[i]); fprintf(fp,"\n");}voidag_reset(ag)ARGVECTOR *ag;{ ag->iw = 0; ag->ic = 0;}intag_w_number(ag)ARGVECTOR *ag;{ return( ag->iw );}voidag_w_advance(ag)ARGVECTOR *ag;{ ++(ag->iw); /* advance to next word */ ag->ic=0; /* beginning of next word */}/* ag_eow: end of word flag whether current position is at end of word*/intag_eow(ag)ARGVECTOR *ag;{ if( ag->iw >= ag->c ) return(TRUE); if( ag->ic >= strlen(ag->v[ag->iw]) ) return(TRUE); return(FALSE);}/* ag_end: end of command line flag whether current position is at end of command line */intag_end(ag)ARGVECTOR *ag;{ if( ag->iw >= ag->c ) return(TRUE); if( ag_eow(ag) && ag->iw == (ag->c)-1 ) return(TRUE); return(FALSE);}/* ag_c: return current character do not advance*/charag_c(ag)ARGVECTOR *ag;{ return(ag->v[ag->iw][ag->ic]);}charag_c_next(ag)ARGVECTOR *ag;{ return(ag->v[ag->iw][ag->ic+1]);}charag_cnn_next(ag)ARGVECTOR *ag;{ if( ag_c_next(ag) == '\0' ) { if(ag->iw+1 >= ag->c) return('\0'); else return(ag->v[ag->iw+1][0]); } else return( ag_c_next(ag) );}/* ag_c_advance: read current character, and advance to next return '\0' if end of word do not advance to next word*/charag_c_advance(ag)ARGVECTOR *ag;{ char c; /* current character */ if( ag_eow(ag) ) return(c='\0'); /* return NULL to signal that current*/ /* character is past end of word */ c = ag->v[ag->iw][ag->ic]; ++(ag->ic); /* advance to next character */ return(c);}charag_backspace(ag)ARGVECTOR *ag;{ if( --(ag->ic) < 0 ) /* if back past beginning of word */ { ag->ic=0; if(--(ag->iw) < 0) /* goto beginning of previous word */ ag_reset(ag); /* if no previous, reset */ else { while( !ag_eow(ag) ) /* goto end of prevous word */ ag_c_advance(ag); ag_backspace(ag); /* back to just before end */ } } return(ag->v[ag->iw][ag->ic]);}/* ag_s: returns current string returns "" if current position is at end of word returns NULL if past end of argument vector*/char *ag_s(ag)ARGVECTOR *ag;{ if( ag->iw < ag->c ) return( ag->v[ag->iw]+ag->ic ); else return( NULL );}char *ag_s_next(ag)ARGVECTOR *ag;{ if( ag->v[ag->iw][ag->ic+1] == '\0' ) { if(ag->iw+1 >= ag->c) return(NULL); else return(ag->v[ag->iw+1]); } else return( ag->v[ag->iw]+ag->ic+1 );}/* ag_s_advance: read current string and advance to next returns NULL if current string is at end does not check advanced string*/char *ag_s_advance(ag)ARGVECTOR *ag;{ char *s; /* current string */ if( ag_eow(ag) ) /* if end of word, go to next word */ ag_w_advance(ag); if( ag_eow(ag) ) /* if still at end of word, */ s = NULL; /* signify by returning NULL */ else s = ag->v[ag->iw] + ag->ic; ag_w_advance(ag); /* advance to next word */ return(s);}intag_clear(ag)ARGVECTOR *ag;{ while( !ag_end(ag) ) argnext(ag); return 1;} /* ------------------------ */ /* return the next argument */ /* ------------------------ */char *argnext(ag)ARGVECTOR *ag;{ static char nullchar='\0'; char *s; s = ag_s_advance(ag); if( s==NULL ) s = &nullchar; return(s);} /* ------------------------------------------- */ /* return the numerical value of next argument */ /* ------------------------------------------- */double argnextnum(ag)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -