shhopt-1.1.7.orig/0042755000175000017500000000000007441742765013316 5ustar ooharaooharashhopt-1.1.7.orig/README0100644000175000017500000001132507440227061014155 0ustar ooharaooharashhopt - library for parsing command line options ================================================= This is a set of functions for parsing command line options. Both traditional one-character options, and GNU-style --long-options are supported. What separates this from traditional getopt? -------------------------------------------- This library does more of the parsing for you. You set up a special structure describing the names and types of the options you want your program to support. In the structure you also give addresses of variables to update or functions to call for the various options. By calling optParseOptions, all options in argv are parsed and removed from argv. What is left are the non-optional arguments to your program. The down-side is that you won't be able to make a program where the position of the options between the non-options are significant. Usage ----- To see how to use this library, take a look at the sample program example.c. A brief explanation: To parse your command line, you need to create and initialize an array of optStruct's. Each element in the array describes a long and short version of an option and specifies what type of option it is and how to handle it. The structure fields (see also shhopt.h): `shortName' is the short option name without the leading '-'. `longName' is the long option name without the leading "--". `type' specifies what type of option this is. (Does it expect an argument? Is it a flag? If it takes an argument, what type should it be?) `arg' is either a function to be called with the argument from the command line, or a pointer to a location in which to store the value. `flags' indicates whether `arg' points to a function or a storage location. The different argument types: `OPT_END' flags this as the last element in the options array. `OPT_FLAG' indicates an option that takes no arguments. If `arg' is not a function pointer, the value of `arg' will be set to 1 if this flag is found on the command line. `OPT_STRING' expects a string argument. `OPT_INT' expects an int argument. `OPT_UINT' expects an unsigned int argument. `OPT_LONG' expects a long argument. `OPT_ULONG' expects an unsigned long argument. The different flag types: `OPT_CALLFUNC' indicates that `arg' is a function pointer. If this is not given, `arg' is taken as a pointer to a variable. Notes ----- * A dash (`-') by itself is not taken as any kind of an option, as several programs use the dash to indicate the special files stdin and stdout. It is thus left as a normal argument to the program. * Two dashes (`--') as an argument is taken to mean that the rest of the arguments should not be scanned for options. This simplifies giving names of files that start with a dash. * Short (one-character) options accept parameters in two ways, either directly following the option in the same argv-entry, or in the next argv-entry: -sPARAMETER -s PARAMETER * Long options accept parameters in two ways: --long-option=PARAMETER --long-option PARAMETER To follow the GNU-tradition, your program documentation should use the first form. * Several one-character options may be combined after a single dash. If any of the options requires a parameter, the rest of the string is taken as this parameter. If there is no "rest of the string", the next argument is taken as the parameter. * There is no support for floating point (double) arguments to options. This is to avoid unnecessary linking with the math library. See example.c for how to get around it by writing a function converting a string argument to a double (functions strToDouble and doubleFunc). Portability ----------- If your libc lacks strtoul, you will need to link with GNU's -liberty, that may be found by anonymous ftp to ftp://ftp.gnu.org/pub/gnu/ The library has (more or less recently) been compiled and `tested' on the following systems: IRIX Release 5.3 IP22 GNU/Linux 2.4.17 with glibc 2.2.5 SunOS Release 4.1.3_U1 (-liberty needed) ULTRIX V4.4 (Rev. 69) All compilations were done using GNU's gcc, and GNU's make. Author ------ The program is written by Sverre H. Huseby shh@thathost.com Lofthusvn. 11 B http://shh.thathost.com/ N-0587 Oslo Norway License ------- This program is released under the Artistic License: http://www.opensource.org/licenses/artistic-license.html Comments (even as simple as "I use your program") are very welcome. If you insist on paying something, please donate some money to an organization that strives to make the world a better place for everyone. I don't like bugs, so please help me removing them by reporting whatever you find! shhopt-1.1.7.orig/INSTALL0100644000175000017500000000124507440225633014331 0ustar ooharaooharaInstalling shhopt ================= If you don't have gcc, you'll have to edit the Makefile. If your compiler is not ANSI, you'll have to change all files. 1. Check the Makefile. In particular, if you run Linux with ELF, you may want to make a shared library (search for SHARED). 2. Build the library make dep make 3. Install library and header file make install By default, the library will be installed in /usr/local/lib, and the header file in /usr/local/include. If you happen to know how to make one of those fancy configure files, and feel like making one for this library, please do so and send the files to me (contact info in the README file). shhopt-1.1.7.orig/CREDITS0100644000175000017500000000037507440225633014323 0ustar ooharaooharaThanks to (see ChangeLog for details): Andrew C. Feren Your name may appear here too, if you * fix bugs * write an even better documentation * extend the functionality of the library * do something nice * all of the above shhopt-1.1.7.orig/ChangeLog0100644000175000017500000000453207440227121015046 0ustar ooharaoohara2002-03-02 Sverre H. Huseby * Release: 1.1.7 * All: Updated contact information. 2000-03-10 Sverre H. Huseby * Release: 1.1.6 * README: Moved license info links from metalab.unc.edu to www.opensource.org. Updated my snail mail address to where I'll live from 2000-04-06 or so. * shhopt.lsm.in (Copying-policy): Pointed to opensource.org for licensing info. * shhopt.h: Removed the trailing comma from the enum, on request from . I always thought this was legal, but after examining the K&R book more thoroughly, it seems it is only legal for array initializers. 1999-09-10 Sverre H. Huseby * Release 1.1.5 * README: Added information on the Artistic license * shhopt.c (optMatch): Made the p variable const, as this compiles more cleanly on Borland's compiler, according to Vladislav Papayan * shhopt.c, example.c: Changed formatting of comments. 1998-07-05 Sverre H. Huseby * Release 1.1.4 * Updated author address. Tue May 27 23:13:19 1997 Sverre H. Huseby * Version: 1.1.3 Sat May 17 21:56:36 1997 Sverre H. Huseby * shhopt.h: Added `extern "C"'. Sun Mar 23 09:47:43 1997 Sverre H. Huseby * Release: 1.1.2 * Makefile: Creating lib*.so in addition to lib*.so.* Tue Nov 12 08:36:09 1996 Sverre H. Huseby * Version: 1.1.1 * shhopt.c: Minor restructuring, no code changes. * example.c: Restructured a bit, and added some comments. Thu Jun 6 10:55:00 1996 Sverre H. Huseby * Released version 1.1. * Makefile: Added example.o to the dependecy list of example. * README: Pointer to the double parsing in example.c * example.c: Added parsing of parameter taking a double value. Thu Jun 6 01:38:00 1996 Sverre H. Huseby * Released version 1.0. * Included patches from Andrew. * Did some minor cosmetic changes. Wed May 22 15:05:00 1996 Andrew C. Feren * Makefile: Changed from "ar rcs" to "ar rc" & ranlib. * README: Improved the documentation. * shhopt.c: Fixed bug for arguments of type unsigned int. Fri Mar 15 15:00:30 1996 Sverre H. Huseby * Released first BETA version: 0.1 shhopt-1.1.7.orig/TODO0100644000175000017500000000155507440225633013774 0ustar ooharaooharaFrom: bryanh@giraffe-data.com (Bryan Henderson) Subject: shhopt: extra comma Date: Wed, 8 Mar 2000 18:39:01 -0800 To: shh@thathost.com [...] Here's an idea for an enhancement: supply functions to allocate and fill the optStruct dynamically. Not because I see a need to change the command line syntax on the fly, but because this would allow one to include pointers to non-static storage in the optStruct. I did this manually, with a macro, but the time I spent working out the details defied the spirit of shhopt! And one more: Add an option to parse syntax like get_long_opt_only() (as opposed to get_long_opt()). I.e. allow long options with only one hyphen. That way we can have the two keystroke options that people enjoy so much without having to have two completely separate option syntaxes. ======================================================================= shhopt-1.1.7.orig/Makefile0100644000175000017500000000477707440227245014756 0ustar ooharaoohara# $Id: Makefile,v 1.12 2002/03/02 19:57:25 sverrehu Exp $ DIST = shhopt VERMAJ = 1 VERMIN = 1 VERPAT = 7 VERSION = $(VERMAJ).$(VERMIN).$(VERPAT) # Define SHARED as 1 for Linux shared ELF library #SHARED = 1 ifeq ($(SHARED),1) LIBTARGET = lib$(DIST).so.$(VERSION) LIBTARGETSO = lib$(DIST).so LIBTARGETSOMAJ = $(LIBTARGETSO).$(VERMAJ) CCSHRD = -fPIC else LIBTARGET = lib$(DIST).a endif LIBHEAD = $(DIST).h TARGETS = $(LIBTARGET) INSTBASEDIR = /usr/local INSTLIBDIR = $(INSTBASEDIR)/lib INSTINCDIR = $(INSTBASEDIR)/include INSTALL = install -m 644 INSTALLPROG = install -m 755 MKDIRP = install -d -m 755 CC = gcc OPTIM = -O2 INCDIR = -I. CCOPT = -s -Wall $(OPTIM) $(INCDIR) # Object files to store in the library LIBOBJS = shhopt.o all: $(TARGETS) # don't worry if you get ranlib not found errors. This probably means # that your ar does an implicit ranlib and you do not need to run ranlib # separately. This error is harmless. $(LIBTARGET): $(LIBOBJS) ifeq ($(SHARED),1) $(CC) -shared -Wl,-soname,$(LIBTARGETSOMAJ) -o $(LIBTARGET) $(LIBOBJS) else ar rc $(LIBTARGET) $(LIBOBJS) ranlib $(LIBTARGET) || true endif # Note that you may need GNU's -liberty if your libc lacks strtoul example: $(LIBTARGET) example.o $(CC) -o example example.c \ -L. -I. -L$(INSTLIBDIR) -I$(INSTINCDIR) -lshhopt .c.o: $(CC) $(CCSHRD) -o $@ -c $(CCOPT) $< depend dep: $(CC) $(INCDIR) -MM *.c >depend install: $(LIBTARGET) $(MKDIRP) $(INSTLIBDIR) $(INSTINCDIR) $(INSTALL) $(LIBTARGET) $(INSTLIBDIR) $(INSTALL) $(LIBHEAD) $(INSTINCDIR) ifeq ($(SHARED),1) ln -sf $(LIBTARGET) $(INSTLIBDIR)/$(LIBTARGETSOMAJ) ln -sf $(LIBTARGETSOMAJ) $(INSTLIBDIR)/$(LIBTARGETSO) echo "Now run ldconfig if necessary." endif clean: rm -f *.o core *~ depend chmod: chmod a+r * # To let the author make a distribution. The rest of the Makefile # should be used by the author only. LSMFILE = $(DIST)-$(VERSION).lsm DISTDIR = $(DIST)-$(VERSION) DISTFILE = $(DIST)-$(VERSION).tar.gz DISTFILES = README INSTALL CREDITS ChangeLog TODO \ Makefile $(LSMFILE) \ $(LIBHEAD) \ shhopt.c \ example.c $(LSMFILE): $(DIST).lsm.in VER=$(VERSION); \ DATE=`date "+%d%b%y"|tr '[a-z]' '[A-Z]'`; \ sed -e "s/VER/$$VER/g;s/DATE/$$DATE/g" $(DIST).lsm.in > $(LSMFILE) veryclean: clean rm -f $(TARGETS) $(DISTFILE) $(LSMFILE) example dist: $(LSMFILE) chmod mkdir $(DISTDIR) chmod a+rx $(DISTDIR) ln $(DISTFILES) $(DISTDIR) tar -cvzf $(DISTFILE) $(DISTDIR) chmod a+r $(DISTFILE) rm -rf $(DISTDIR) ifeq (depend,$(wildcard depend)) include depend endif shhopt-1.1.7.orig/shhopt-1.1.7.lsm0100644000175000017500000000104107440227247015761 0ustar ooharaooharaBegin3 Title: shhopt - library for parsing command line options Version: 1.1.7 Entered-date: 02MAR02 Description: C-functions for parsing command line options, both traditional one-character options, and GNU'ish --long-options. Keywords: programming, library, lib, commandline, options Author: shh@thathost.com (Sverre H. Huseby) Primary-site: http://shh.thathost.com/pub-unix/ Platforms: Requires ANSI C-compiler. Copying-policy: Artistic License http://www.opensource.org/licenses/artistic-license.html End shhopt-1.1.7.orig/shhopt.h0100644000175000017500000000250407440225633014755 0ustar ooharaoohara/* $Id: shhopt.h,v 1.5 2000/03/10 08:32:39 sverrehu Exp $ */ #ifndef SHHOPT_H #define SHHOPT_H #ifdef __cplusplus extern "C" { #endif /* constants for recognized option types. */ typedef enum { OPT_END, /* nothing. used as ending element. */ OPT_FLAG, /* no argument following. sets variable to 1. */ OPT_STRING, /* string argument. */ OPT_INT, /* signed integer argument. */ OPT_UINT, /* unsigned integer argument. */ OPT_LONG, /* signed long integer argument. */ OPT_ULONG /* unsigned long integer argument. */ } optArgType; /* flags modifying the default way options are handeled. */ #define OPT_CALLFUNC 1 /* pass argument to a function. */ typedef struct { char shortName; /* short option name. */ char *longName; /* long option name, no including '--'. */ optArgType type; /* option type. */ void *arg; /* pointer to variable to fill with argument, * or pointer to function if type == OPT_FUNC. */ int flags; /* modifier flags. */ } optStruct; void optSetFatalFunc(void (*f)(const char *, ...)); void optParseOptions(int *argc, char *argv[], optStruct opt[], int allowNegNum); #ifdef __cplusplus } #endif #endif shhopt-1.1.7.orig/shhopt.c0100644000175000017500000003005207440227142014744 0ustar ooharaoohara/* $Id: shhopt.c,v 1.6 2002/03/02 19:56:18 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE shhopt.c | | DESCRIPTION Functions for parsing command line arguments. Values | of miscellaneous types may be stored in variables, | or passed to functions as specified. | | REQUIREMENTS Some systems lack the ANSI C -function strtoul. If | your system is one of those, you'll ned to write | one yourself, or get the GNU liberty-library (from | ftp://ftp.gnu.org/pub/gnu/). | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include "shhopt.h" /*-----------------------------------------------------------------------+ | PRIVATE DATA | +-----------------------------------------------------------------------*/ static void optFatalFunc(const char *, ...); static void (*optFatal)(const char *format, ...) = optFatalFunc; /*-----------------------------------------------------------------------+ | PRIVATE FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | NAME optFatalFunc | | FUNCTION Show given message and abort the program. | | INPUT format, ... | Arguments used as with printf(). | | RETURNS Never returns. The program is aborted. */ void optFatalFunc(const char *format, ...) { va_list ap; fflush(stdout); va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); exit(99); } /*------------------------------------------------------------------------ | NAME optStructCount | | FUNCTION Get number of options in a optStruct. | | INPUT opt array of possible options. | | RETURNS Number of options in the given array. | | DESCRIPTION Count elements in an optStruct-array. The strcture must | be ended using an element of type OPT_END. */ static int optStructCount(optStruct opt[]) { int ret = 0; while (opt[ret].type != OPT_END) ++ret; return ret; } /*------------------------------------------------------------------------ | NAME optMatch | | FUNCTION Find a matching option. | | INPUT opt array of possible options. | s string to match, without `-' or `--'. | lng match long option, otherwise short. | | RETURNS Index to the option if found, -1 if not found. | | DESCRIPTION Short options are matched from the first character in | the given string. */ static int optMatch(optStruct opt[], const char *s, int lng) { int nopt, q, matchlen = 0; const char *p; nopt = optStructCount(opt); if (lng) { if ((p = strchr(s, '=')) != NULL) matchlen = p - s; else matchlen = strlen(s); } for (q = 0; q < nopt; q++) { if (lng) { if (!opt[q].longName) continue; if (strncmp(s, opt[q].longName, matchlen) == 0) return q; } else { if (!opt[q].shortName) continue; if (*s == opt[q].shortName) return q; } } return -1; } /*------------------------------------------------------------------------ | NAME optString | | FUNCTION Return a (static) string with the option name. | | INPUT opt the option to stringify. | lng is it a long option? | | RETURNS Pointer to static string. */ static char * optString(optStruct *opt, int lng) { static char ret[31]; if (lng) { strcpy(ret, "--"); strncpy(ret + 2, opt->longName, 28); } else { ret[0] = '-'; ret[1] = opt->shortName; ret[2] = '\0'; } return ret; } /*------------------------------------------------------------------------ | NAME optNeedsArgument | | FUNCTION Check if an option requires an argument. | | INPUT opt the option to check. | | RETURNS Boolean value. */ static int optNeedsArgument(optStruct *opt) { return opt->type == OPT_STRING || opt->type == OPT_INT || opt->type == OPT_UINT || opt->type == OPT_LONG || opt->type == OPT_ULONG; } /*------------------------------------------------------------------------ | NAME argvRemove | | FUNCTION Remove an entry from an argv-array. | | INPUT argc pointer to number of options. | argv array of option-/argument-strings. | i index of option to remove. | | OUTPUT argc new argument count. | argv array with given argument removed. */ static void argvRemove(int *argc, char *argv[], int i) { if (i >= *argc) return; while (i++ < *argc) argv[i - 1] = argv[i]; --*argc; } /*------------------------------------------------------------------------ | NAME optExecute | | FUNCTION Perform the action of an option. | | INPUT opt array of possible options. | arg argument to option, if it applies. | lng was the option given as a long option? | | RETURNS Nothing. Aborts in case of error. */ void optExecute(optStruct *opt, char *arg, int lng) { switch (opt->type) { case OPT_FLAG: if (opt->flags & OPT_CALLFUNC) ((void (*)(void)) opt->arg)(); else *((int *) opt->arg) = 1; break; case OPT_STRING: if (opt->flags & OPT_CALLFUNC) ((void (*)(char *)) opt->arg)(arg); else *((char **) opt->arg) = arg; break; case OPT_INT: case OPT_LONG: { long tmp; char *e; tmp = strtol(arg, &e, 10); if (*e) optFatal("invalid number `%s'\n", arg); if (errno == ERANGE || (opt->type == OPT_INT && (tmp > INT_MAX || tmp < INT_MIN))) optFatal("number `%s' to `%s' out of range\n", arg, optString(opt, lng)); if (opt->type == OPT_INT) { if (opt->flags & OPT_CALLFUNC) ((void (*)(int)) opt->arg)((int) tmp); else *((int *) opt->arg) = (int) tmp; } else /* OPT_LONG */ { if (opt->flags & OPT_CALLFUNC) ((void (*)(long)) opt->arg)(tmp); else *((long *) opt->arg) = tmp; } break; } case OPT_UINT: case OPT_ULONG: { unsigned long tmp; char *e; tmp = strtoul(arg, &e, 10); if (*e) optFatal("invalid number `%s'\n", arg); if (errno == ERANGE || (opt->type == OPT_UINT && tmp > UINT_MAX)) optFatal("number `%s' to `%s' out of range\n", arg, optString(opt, lng)); if (opt->type == OPT_UINT) { if (opt->flags & OPT_CALLFUNC) ((void (*)(unsigned)) opt->arg)((unsigned) tmp); else *((unsigned *) opt->arg) = (unsigned) tmp; } else /* OPT_ULONG */ { if (opt->flags & OPT_CALLFUNC) ((void (*)(unsigned long)) opt->arg)(tmp); else *((unsigned long *) opt->arg) = tmp; } break; } default: break; } } /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | NAME optSetFatalFunc | | FUNCTION Set function used to display error message and exit. | | SYNOPSIS #include "shhopt.h" | void optSetFatalFunc(void (*f)(const char *, ...)); | | INPUT f function accepting printf()'like parameters, | that _must_ abort the program. */ void optSetFatalFunc(void (*f)(const char *, ...)) { optFatal = f; } /*------------------------------------------------------------------------ | NAME optParseOptions | | FUNCTION Parse commandline options. | | SYNOPSIS #include "shhopt.h" | void optParseOptions(int *argc, char *argv[], | optStruct opt[], int allowNegNum); | | INPUT argc Pointer to number of options. | argv Array of option-/argument-strings. | opt Array of possible options. | allowNegNum | a negative number is not to be taken as | an option. | | OUTPUT argc new argument count. | argv array with arguments removed. | | RETURNS Nothing. Aborts in case of error. | | DESCRIPTION This function checks each option in the argv-array | against strings in the opt-array, and `executes' any | matching action. Any arguments to the options are | extracted and stored in the variables or passed to | functions pointed to by entries in opt. | | Options and arguments used are removed from the argv- | array, and argc is decreased accordingly. | | Any error leads to program abortion. */ void optParseOptions(int *argc, char *argv[], optStruct opt[], int allowNegNum) { int ai, /* argv index. */ optarg, /* argv index of option argument, or -1 if none. */ mi, /* Match index in opt. */ done; char *arg, /* Pointer to argument to an option. */ *o, /* pointer to an option character */ *p; /* * Loop through all arguments. */ for (ai = 0; ai < *argc; ) { /* * "--" indicates that the rest of the argv-array does not * contain options. */ if (strcmp(argv[ai], "--") == 0) { argvRemove(argc, argv, ai); break; } if (allowNegNum && argv[ai][0] == '-' && isdigit(argv[ai][1])) { ++ai; continue; } else if (strncmp(argv[ai], "--", 2) == 0) { /* long option */ /* find matching option */ if ((mi = optMatch(opt, argv[ai] + 2, 1)) < 0) optFatal("unrecognized option `%s'\n", argv[ai]); /* possibly locate the argument to this option. */ arg = NULL; if ((p = strchr(argv[ai], '=')) != NULL) arg = p + 1; /* does this option take an argument? */ optarg = -1; if (optNeedsArgument(&opt[mi])) { /* option needs an argument. find it. */ if (!arg) { if ((optarg = ai + 1) == *argc) optFatal("option `%s' requires an argument\n", optString(&opt[mi], 1)); arg = argv[optarg]; } } else { if (arg) optFatal("option `%s' doesn't allow an argument\n", optString(&opt[mi], 1)); } /* perform the action of this option. */ optExecute(&opt[mi], arg, 1); /* remove option and any argument from the argv-array. */ if (optarg >= 0) argvRemove(argc, argv, ai); argvRemove(argc, argv, ai); } else if (*argv[ai] == '-') { /* A dash by itself is not considered an option. */ if (argv[ai][1] == '\0') { ++ai; continue; } /* Short option(s) following */ o = argv[ai] + 1; done = 0; optarg = -1; while (*o && !done) { /* find matching option */ if ((mi = optMatch(opt, o, 0)) < 0) optFatal("unrecognized option `-%c'\n", *o); /* does this option take an argument? */ optarg = -1; arg = NULL; if (optNeedsArgument(&opt[mi])) { /* option needs an argument. find it. */ arg = o + 1; if (!*arg) { if ((optarg = ai + 1) == *argc) optFatal("option `%s' requires an argument\n", optString(&opt[mi], 0)); arg = argv[optarg]; } done = 1; } /* perform the action of this option. */ optExecute(&opt[mi], arg, 0); ++o; } /* remove option and any argument from the argv-array. */ if (optarg >= 0) argvRemove(argc, argv, ai); argvRemove(argc, argv, ai); } else { /* a non-option argument */ ++ai; } } } shhopt-1.1.7.orig/example.c0100644000175000017500000001046507440227142015100 0ustar ooharaoohara/* $Id: example.c,v 1.7 2002/03/02 19:56:18 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE example.c | | DESCRIPTION Sample source code using the shhopt-library to parse | command line options. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include #include /* needed in strToDouble only */ #include "shhopt.h" /*-----------------------------------------------------------------------+ | PRIVATE DATA | +-----------------------------------------------------------------------*/ /* called by -N or --number-func */ static void numberFunc(int i) { printf("integer function called, i=%d\n", i); } /* called by -S or --string-func */ static void stringFunc(char *s) { printf("string function called, s=\"%s\"\n", s); } /* called by -F or --flag-func */ static void flagFunc(void) { printf("flag function called\n"); } /* shhopt does not support double arguments, so we write our own * handler for this. The trick is to create a function taking a string * argument, and convert this string to a double. The following two * functions handles this. */ /* convert a string to a double value, with error checking */ static double strToDouble(char *s) { double d; char *e; d = strtod(s, &e); if (*e) { fprintf(stderr, "invalid number `%s'\n", s); exit(1); } if (errno == ERANGE) { fprintf(stderr, "number `%s' out of range\n", s); exit(1); } return d; } /* called by -D or --double-func */ static void doubleFunc(char *s) { double d; d = strToDouble(s); printf("double function called, d=\"%g\"\n", d); } static void usage(void) { printf( "usage: example [option ...] [other-arguments ...]\n" "\n" " -n, --number=NUMBER set value of variable `number'\n" " -N, --number-func=NUMBER call function with given integer argument\n" " -D, --double-func=NUMBER call function with given double argument\n" " -s, --string=STRING set value of variable `string'\n" " -S, --string-func=STRING call function with given string argument\n" " -f, --flag set value of variable `flag' to 1\n" " -F, --flag-func call a function with no argument\n" "\n" "A double-dash (`--') indicates that the rest of the line is not to\n" "be scanned for options.\n" ); exit(1); } /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ int main(int argc, char *argv[]) { int q; /* counter */ int number = 0; /* set to anything by -n or --number */ int flag = 0; /* set to 1 by -f or --flag */ char *string = "none"; /* set to anything by -s or --string */ optStruct opt[] = { /* short long type var/func special */ { 'h', "help", OPT_FLAG, usage, OPT_CALLFUNC }, { 'n', "number", OPT_INT, &number, 0 }, { 'N', "number-func", OPT_INT, numberFunc, OPT_CALLFUNC }, { 'D', "double-func", OPT_STRING, doubleFunc, OPT_CALLFUNC }, { 's', "string", OPT_STRING, &string, 0 }, { 'S', "string-func", OPT_STRING, stringFunc, OPT_CALLFUNC }, { 'f', "flag", OPT_FLAG, &flag, 0 }, { 'F', "flag-func", OPT_FLAG, flagFunc, OPT_CALLFUNC }, { 0, 0, OPT_END, 0, 0 } /* no more options */ }; /* if you link with the shhmsg-library, you may wish to set the * the error handling function by calling * optSetFatalFunc(msgFatal); */ /* parse all options */ optParseOptions(&argc, argv, opt, 0); /* display result */ printf("number=%d, flag=%d, string=\"%s\"\n", number, flag, string); /* what's left in argv, are any non-optinons. */ printf("remaining arguments: "); for (q = 1; q < argc; q++) printf("\"%s\" ", argv[q]); printf("\n"); return 0; }