This commit is contained in:
hyung-hwan 2008-03-18 04:44:28 +00:00
parent 465a42db1c
commit fa1837d905
2 changed files with 50 additions and 8 deletions

View File

@ -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} * {License}
*/ */
#include <ase/utl/getopt.h>
/* /*
* Copyright (c) 1987-2002 The Regents of the University of California. * Copyright (c) 1987-2002 The Regents of the University of California.
* All rights reserved. * All rights reserved.
@ -37,6 +39,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if 0
/* declarations to provide consistent linkage */ /* declarations to provide consistent linkage */
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
@ -47,13 +50,18 @@ int opterr = 1, /* if error message should be printed */
optopt, /* character checked for validity */ optopt, /* character checked for validity */
optreset; /* reset getopt */ optreset; /* reset getopt */
char *optarg; /* argument associated with option */ char *optarg; /* argument associated with option */
#endif
#define BADCH ASE_T('?') #define BADCH ASE_T('?')
#define BADARG ASE_T(':') #define BADARG ASE_T(':')
#define EMSG ASE_T("") #define EMSG ASE_T("")
ase_cint_t ase_getopt ( #define optstr opt->str
int nargc, ase_char_t* const *nargv, const ase_char_t* ostr) #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 */ static char *place = EMSG; /* option letter processing */
ase_char_t* oli; /* option letter list index */ ase_char_t* oli; /* option letter list index */
@ -62,7 +70,7 @@ ase_cint_t ase_getopt (
{ {
/* update scanning pointer */ /* update scanning pointer */
optreset = 0; optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') if (optind >= argc || *(place = nargv[optind]) != '-')
{ {
place = EMSG; place = EMSG;
return ASE_CHAR_EOF; return ASE_CHAR_EOF;
@ -77,7 +85,7 @@ ase_cint_t ase_getopt (
} /* option letter okay? */ } /* option letter okay? */
if ((optopt = (int)*place++) == ASE_T(':') || if ((optopt = (int)*place++) == ASE_T(':') ||
!(oli = strchr(ostr, optopt))) !(oli = strchr(optstr, optopt)))
{ {
/* /*
* if the user didn't specify '-' as an option, * 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 (optopt == (int)'-') return ASE_CHAR_EOF;
if (!*place) ++optind; if (!*place) ++optind;
if (opterr && *ostr != ASE_T(':')) if (opterr && *optstr != ASE_T(':'))
(void)fprintf(stderr, (void)fprintf(stderr,
"%s: illegal option -- %c\n", __FILE__, optopt); "%s: illegal option -- %c\n", __FILE__, optopt);
return ASE_CHAR_BADCH; return ASE_CHAR_BADCH;
@ -102,11 +110,11 @@ ase_cint_t ase_getopt (
/* need an argument */ /* need an argument */
if (*place) optarg = place; /* no white space */ if (*place) optarg = place; /* no white space */
else if (nargc <= ++optind) else if (argc <= ++optind)
{ {
/* no arg */ /* no arg */
place = EMSG; place = EMSG;
if (*ostr == ASE_T(':')) return BADARG; if (*optstr == ASE_T(':')) return BADARG;
if (opterr) if (opterr)
(void)fprintf(stderr, (void)fprintf(stderr,
"%s: option requires an argument -- %c\n", "%s: option requires an argument -- %c\n",

34
ase/utl/getopt.h Normal file
View 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