Commit b6db47aa authored by Boyuan Yang's avatar Boyuan Yang

Import Upstream version 1.1.7

parents
Thanks to (see ChangeLog for details):
Andrew C. Feren <feren@ctron.com>
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
2002-03-02 Sverre H. Huseby <shh@thathost.com>
* Release: 1.1.7
* All: Updated contact information.
2000-03-10 Sverre H. Huseby <shh@thathost.com>
* 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 <bryanh@giraffe-data.com>. 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 <shh@thathost.com>
* 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
<vlpa@daleen.com>
* shhopt.c, example.c: Changed formatting of comments.
1998-07-05 Sverre H. Huseby <shh@thathost.com>
* Release 1.1.4
* Updated author address.
Tue May 27 23:13:19 1997 Sverre H. Huseby <s.h.huseby@usit.uio.no>
* Version: 1.1.3
Sat May 17 21:56:36 1997 Sverre H. Huseby <s.h.huseby@usit.uio.no>
* shhopt.h: Added `extern "C"'.
Sun Mar 23 09:47:43 1997 Sverre H. Huseby <s.h.huseby@usit.uio.no>
* Release: 1.1.2
* Makefile: Creating lib*.so in addition to lib*.so.*
Tue Nov 12 08:36:09 1996 Sverre H. Huseby <sverrehu@ifi.uio.no>
* 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 <sverrehu@ifi.uio.no>
* 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 <sverrehu@ifi.uio.no>
* Released version 1.0.
* Included patches from Andrew.
* Did some minor cosmetic changes.
Wed May 22 15:05:00 1996 Andrew C. Feren <feren@ctron.com>
* 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 <sverrehu@ender>
* Released first BETA version: 0.1
Installing 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).
# $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 - 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!
From: 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.
=======================================================================
/* $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 <shh@thathost.com>
+----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h> /* 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;
}
Begin3
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
This diff is collapsed.
/* $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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment