This commit is contained in:
parent
465a42db1c
commit
fa1837d905
@ -1,9 +1,11 @@
|
||||
/*
|
||||
* $Id: getopt.c 134 2008-03-17 10:30:05Z baconevi $
|
||||
* $Id: getopt.c 135 2008-03-17 10:44:28Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
#include <ase/utl/getopt.h>
|
||||
|
||||
/*
|
||||
* Copyright (c) 1987-2002 The Regents of the University of California.
|
||||
* All rights reserved.
|
||||
@ -37,6 +39,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if 0
|
||||
/* declarations to provide consistent linkage */
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
@ -47,13 +50,18 @@ int opterr = 1, /* if error message should be printed */
|
||||
optopt, /* character checked for validity */
|
||||
optreset; /* reset getopt */
|
||||
char *optarg; /* argument associated with option */
|
||||
#endif
|
||||
|
||||
#define BADCH ASE_T('?')
|
||||
#define BADARG ASE_T(':')
|
||||
#define EMSG ASE_T("")
|
||||
|
||||
ase_cint_t ase_getopt (
|
||||
int nargc, ase_char_t* const *nargv, const ase_char_t* ostr)
|
||||
#define optstr opt->str
|
||||
#define optarg opt->arg
|
||||
#define optind opt->ind
|
||||
#define optopt opt->opt
|
||||
|
||||
ase_cint_t ase_getopt (int argc, ase_char_t* const *nargv, ase_opt_t* opt)
|
||||
{
|
||||
static char *place = EMSG; /* option letter processing */
|
||||
ase_char_t* oli; /* option letter list index */
|
||||
@ -62,7 +70,7 @@ ase_cint_t ase_getopt (
|
||||
{
|
||||
/* update scanning pointer */
|
||||
optreset = 0;
|
||||
if (optind >= nargc || *(place = nargv[optind]) != '-')
|
||||
if (optind >= argc || *(place = nargv[optind]) != '-')
|
||||
{
|
||||
place = EMSG;
|
||||
return ASE_CHAR_EOF;
|
||||
@ -77,7 +85,7 @@ ase_cint_t ase_getopt (
|
||||
} /* option letter okay? */
|
||||
|
||||
if ((optopt = (int)*place++) == ASE_T(':') ||
|
||||
!(oli = strchr(ostr, optopt)))
|
||||
!(oli = strchr(optstr, optopt)))
|
||||
{
|
||||
/*
|
||||
* if the user didn't specify '-' as an option,
|
||||
@ -85,7 +93,7 @@ ase_cint_t ase_getopt (
|
||||
*/
|
||||
if (optopt == (int)'-') return ASE_CHAR_EOF;
|
||||
if (!*place) ++optind;
|
||||
if (opterr && *ostr != ASE_T(':'))
|
||||
if (opterr && *optstr != ASE_T(':'))
|
||||
(void)fprintf(stderr,
|
||||
"%s: illegal option -- %c\n", __FILE__, optopt);
|
||||
return ASE_CHAR_BADCH;
|
||||
@ -102,11 +110,11 @@ ase_cint_t ase_getopt (
|
||||
/* need an argument */
|
||||
|
||||
if (*place) optarg = place; /* no white space */
|
||||
else if (nargc <= ++optind)
|
||||
else if (argc <= ++optind)
|
||||
{
|
||||
/* no arg */
|
||||
place = EMSG;
|
||||
if (*ostr == ASE_T(':')) return BADARG;
|
||||
if (*optstr == ASE_T(':')) return BADARG;
|
||||
if (opterr)
|
||||
(void)fprintf(stderr,
|
||||
"%s: option requires an argument -- %c\n",
|
||||
|
34
ase/utl/getopt.h
Normal file
34
ase/utl/getopt.h
Normal file
@ -0,0 +1,34 @@
|
||||
|
||||
#ifndef _ASE_UTL_GETOPT_H_
|
||||
#define _ASE_UTL_GETOPT_H_
|
||||
|
||||
#include <ase/cmn/types.h>
|
||||
#include <ase/cmn/macros.h>
|
||||
|
||||
typedef struct ase_opt_t ase_opt_t;
|
||||
|
||||
struct ase_opt_t
|
||||
{
|
||||
/* input */
|
||||
const ase_char_t* str;
|
||||
|
||||
/* output */
|
||||
ase_cint_t opt; /* character checked for validity */
|
||||
ase_char_t* arg; /* argument associated with an option */
|
||||
int err;
|
||||
|
||||
/* input + output */
|
||||
int ind; /* index into parent argv vector */
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
ase_cint_t ase_getopt (int argc, ase_char_t* argv[], ase_opt_t* opt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user