From 9c84647015e86c808d7e0a8ed5517d7b67b2e3ec Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 19 Jul 2021 10:24:39 +0000 Subject: [PATCH] added mio_getbopt() and mio_getuopt() --- mio/lib/Makefile.am | 3 + mio/lib/Makefile.in | 59 +++++++----- mio/lib/mio-opt.h | 141 +++++++++++++++++++++++++++++ mio/lib/opt-imp.h | 215 ++++++++++++++++++++++++++++++++++++++++++++ mio/lib/opt.c | 82 +++++++++++++++++ 5 files changed, 476 insertions(+), 24 deletions(-) create mode 100644 mio/lib/mio-opt.h create mode 100644 mio/lib/opt-imp.h create mode 100644 mio/lib/opt.c diff --git a/mio/lib/Makefile.am b/mio/lib/Makefile.am index d83cee3..1b154b5 100644 --- a/mio/lib/Makefile.am +++ b/mio/lib/Makefile.am @@ -37,6 +37,7 @@ include_HEADERS = \ mio-http.h \ mio-json.h \ mio-nwif.h \ + mio-opt.h \ mio-pac1.h \ mio-path.h \ mio-pipe.h \ @@ -72,6 +73,8 @@ libmio_la_SOURCES = \ mio-prv.h \ mio.c \ nwif.c \ + opt.c \ + opt-imp.h \ path.c \ pipe.c \ pro.c \ diff --git a/mio/lib/Makefile.in b/mio/lib/Makefile.in index 5d11083..9ec8927 100644 --- a/mio/lib/Makefile.in +++ b/mio/lib/Makefile.in @@ -150,10 +150,10 @@ libmio_la_DEPENDENCIES = $(am__DEPENDENCIES_2) $(am__DEPENDENCIES_1) \ am__libmio_la_SOURCES_DIST = chr.c dns.c dns-cli.c ecs.c ecs-imp.h \ err.c fmt.c fmt-imp.h htb.c htrd.c htre.c http.c http-cgi.c \ http-fil.c http-prv.h http-svr.c http-thr.c http-txt.c json.c \ - mio-prv.h mio.c nwif.c path.c pipe.c pro.c sck.c skad.c sys.c \ - sys-ass.c sys-err.c sys-log.c sys-mux.c sys-prv.h sys-tim.c \ - thr.c uch-case.h uch-prop.h tmr.c utf8.c utl.c utl-siph.c \ - mar.c mar-cli.c + mio-prv.h mio.c nwif.c opt.c opt-imp.h path.c pipe.c pro.c \ + sck.c skad.c sys.c sys-ass.c sys-err.c sys-log.c sys-mux.c \ + sys-prv.h sys-tim.c thr.c uch-case.h uch-prop.h tmr.c utf8.c \ + utl.c utl-siph.c mar.c mar-cli.c @ENABLE_MARIADB_TRUE@am__objects_1 = libmio_la-mar.lo \ @ENABLE_MARIADB_TRUE@ libmio_la-mar-cli.lo am_libmio_la_OBJECTS = libmio_la-chr.lo libmio_la-dns.lo \ @@ -162,13 +162,13 @@ am_libmio_la_OBJECTS = libmio_la-chr.lo libmio_la-dns.lo \ libmio_la-htre.lo libmio_la-http.lo libmio_la-http-cgi.lo \ libmio_la-http-fil.lo libmio_la-http-svr.lo \ libmio_la-http-thr.lo libmio_la-http-txt.lo libmio_la-json.lo \ - libmio_la-mio.lo libmio_la-nwif.lo libmio_la-path.lo \ - libmio_la-pipe.lo libmio_la-pro.lo libmio_la-sck.lo \ - libmio_la-skad.lo libmio_la-sys.lo libmio_la-sys-ass.lo \ - libmio_la-sys-err.lo libmio_la-sys-log.lo libmio_la-sys-mux.lo \ - libmio_la-sys-tim.lo libmio_la-thr.lo libmio_la-tmr.lo \ - libmio_la-utf8.lo libmio_la-utl.lo libmio_la-utl-siph.lo \ - $(am__objects_1) + libmio_la-mio.lo libmio_la-nwif.lo libmio_la-opt.lo \ + libmio_la-path.lo libmio_la-pipe.lo libmio_la-pro.lo \ + libmio_la-sck.lo libmio_la-skad.lo libmio_la-sys.lo \ + libmio_la-sys-ass.lo libmio_la-sys-err.lo libmio_la-sys-log.lo \ + libmio_la-sys-mux.lo libmio_la-sys-tim.lo libmio_la-thr.lo \ + libmio_la-tmr.lo libmio_la-utf8.lo libmio_la-utl.lo \ + libmio_la-utl-siph.lo $(am__objects_1) libmio_la_OBJECTS = $(am_libmio_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -206,9 +206,10 @@ am__depfiles_remade = ./$(DEPDIR)/libmio_la-chr.Plo \ ./$(DEPDIR)/libmio_la-http.Plo ./$(DEPDIR)/libmio_la-json.Plo \ ./$(DEPDIR)/libmio_la-mar-cli.Plo \ ./$(DEPDIR)/libmio_la-mar.Plo ./$(DEPDIR)/libmio_la-mio.Plo \ - ./$(DEPDIR)/libmio_la-nwif.Plo ./$(DEPDIR)/libmio_la-path.Plo \ - ./$(DEPDIR)/libmio_la-pipe.Plo ./$(DEPDIR)/libmio_la-pro.Plo \ - ./$(DEPDIR)/libmio_la-sck.Plo ./$(DEPDIR)/libmio_la-skad.Plo \ + ./$(DEPDIR)/libmio_la-nwif.Plo ./$(DEPDIR)/libmio_la-opt.Plo \ + ./$(DEPDIR)/libmio_la-path.Plo ./$(DEPDIR)/libmio_la-pipe.Plo \ + ./$(DEPDIR)/libmio_la-pro.Plo ./$(DEPDIR)/libmio_la-sck.Plo \ + ./$(DEPDIR)/libmio_la-skad.Plo \ ./$(DEPDIR)/libmio_la-sys-ass.Plo \ ./$(DEPDIR)/libmio_la-sys-err.Plo \ ./$(DEPDIR)/libmio_la-sys-log.Plo \ @@ -246,9 +247,9 @@ am__can_run_installinfo = \ esac am__include_HEADERS_DIST = mio-cfg.h mio-chr.h mio-cmn.h mio-dns.h \ mio-ecs.h mio-fmt.h mio-htb.h mio-htrd.h mio-htre.h mio-http.h \ - mio-json.h mio-nwif.h mio-pac1.h mio-path.h mio-pipe.h \ - mio-pro.h mio-sck.h mio-skad.h mio-thr.h mio-upac.h mio-utl.h \ - mio.h mio-mar.h + mio-json.h mio-nwif.h mio-opt.h mio-pac1.h mio-path.h \ + mio-pipe.h mio-pro.h mio-sck.h mio-skad.h mio-thr.h mio-upac.h \ + mio-utl.h mio.h mio-mar.h HEADERS = $(include_HEADERS) am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \ mio-cfg.h.in @@ -436,17 +437,17 @@ LIBADD_LIB_COMMON = $(LIBM) #pkgbindir = $(bindir) include_HEADERS = mio-cfg.h mio-chr.h mio-cmn.h mio-dns.h mio-ecs.h \ mio-fmt.h mio-htb.h mio-htrd.h mio-htre.h mio-http.h \ - mio-json.h mio-nwif.h mio-pac1.h mio-path.h mio-pipe.h \ - mio-pro.h mio-sck.h mio-skad.h mio-thr.h mio-upac.h mio-utl.h \ - mio.h $(am__append_1) + mio-json.h mio-nwif.h mio-opt.h mio-pac1.h mio-path.h \ + mio-pipe.h mio-pro.h mio-sck.h mio-skad.h mio-thr.h mio-upac.h \ + mio-utl.h mio.h $(am__append_1) lib_LTLIBRARIES = libmio.la libmio_la_SOURCES = chr.c dns.c dns-cli.c ecs.c ecs-imp.h err.c fmt.c \ fmt-imp.h htb.c htrd.c htre.c http.c http-cgi.c http-fil.c \ http-prv.h http-svr.c http-thr.c http-txt.c json.c mio-prv.h \ - mio.c nwif.c path.c pipe.c pro.c sck.c skad.c sys.c sys-ass.c \ - sys-err.c sys-log.c sys-mux.c sys-prv.h sys-tim.c thr.c \ - uch-case.h uch-prop.h tmr.c utf8.c utl.c utl-siph.c \ - $(am__append_2) + mio.c nwif.c opt.c opt-imp.h path.c pipe.c pro.c sck.c skad.c \ + sys.c sys-ass.c sys-err.c sys-log.c sys-mux.c sys-prv.h \ + sys-tim.c thr.c uch-case.h uch-prop.h tmr.c utf8.c utl.c \ + utl-siph.c $(am__append_2) libmio_la_CPPFLAGS = $(CPPFLAGS_LIB_COMMON) libmio_la_CFLAGS = $(CFLAGS_LIB_COMMON) $(am__append_3) libmio_la_LDFLAGS = $(LDFLAGS_LIB_COMMON) $(am__append_4) @@ -566,6 +567,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-mar.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-mio.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-nwif.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-opt.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-path.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-pipe.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libmio_la-pro.Plo@am__quote@ # am--include-marker @@ -739,6 +741,13 @@ libmio_la-nwif.lo: nwif.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -c -o libmio_la-nwif.lo `test -f 'nwif.c' || echo '$(srcdir)/'`nwif.c +libmio_la-opt.lo: opt.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -MT libmio_la-opt.lo -MD -MP -MF $(DEPDIR)/libmio_la-opt.Tpo -c -o libmio_la-opt.lo `test -f 'opt.c' || echo '$(srcdir)/'`opt.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-opt.Tpo $(DEPDIR)/libmio_la-opt.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='opt.c' object='libmio_la-opt.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -c -o libmio_la-opt.lo `test -f 'opt.c' || echo '$(srcdir)/'`opt.c + libmio_la-path.lo: path.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libmio_la_CPPFLAGS) $(CPPFLAGS) $(libmio_la_CFLAGS) $(CFLAGS) -MT libmio_la-path.lo -MD -MP -MF $(DEPDIR)/libmio_la-path.Tpo -c -o libmio_la-path.lo `test -f 'path.c' || echo '$(srcdir)/'`path.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libmio_la-path.Tpo $(DEPDIR)/libmio_la-path.Plo @@ -1040,6 +1049,7 @@ distclean: distclean-am -rm -f ./$(DEPDIR)/libmio_la-mar.Plo -rm -f ./$(DEPDIR)/libmio_la-mio.Plo -rm -f ./$(DEPDIR)/libmio_la-nwif.Plo + -rm -f ./$(DEPDIR)/libmio_la-opt.Plo -rm -f ./$(DEPDIR)/libmio_la-path.Plo -rm -f ./$(DEPDIR)/libmio_la-pipe.Plo -rm -f ./$(DEPDIR)/libmio_la-pro.Plo @@ -1122,6 +1132,7 @@ maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libmio_la-mar.Plo -rm -f ./$(DEPDIR)/libmio_la-mio.Plo -rm -f ./$(DEPDIR)/libmio_la-nwif.Plo + -rm -f ./$(DEPDIR)/libmio_la-opt.Plo -rm -f ./$(DEPDIR)/libmio_la-path.Plo -rm -f ./$(DEPDIR)/libmio_la-pipe.Plo -rm -f ./$(DEPDIR)/libmio_la-pro.Plo diff --git a/mio/lib/mio-opt.h b/mio/lib/mio-opt.h new file mode 100644 index 0000000..7570eed --- /dev/null +++ b/mio/lib/mio-opt.h @@ -0,0 +1,141 @@ +/* + * $Id$ + * + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _MIO_OPT_H_ +#define _MIO_OPT_H_ + +#include "mio-cmn.h" + +/** \file + * This file defines functions and data structures to process + * command-line arguments. + */ + +typedef struct mio_uopt_t mio_uopt_t; +typedef struct mio_uopt_lng_t mio_uopt_lng_t; + +struct mio_uopt_lng_t +{ + const mio_uch_t* str; + mio_uci_t val; +}; + +struct mio_uopt_t +{ + /* input */ + const mio_uch_t* str; /* option string */ + mio_uopt_lng_t* lng; /* long options */ + + /* output */ + mio_uci_t opt; /* character checked for validity */ + mio_uch_t* arg; /* argument associated with an option */ + + /* output */ + const mio_uch_t* lngopt; + + /* input + output */ + int ind; /* index into parent argv vector */ + + /* input + output - internal*/ + mio_uch_t* cur; +}; + +typedef struct mio_bopt_t mio_bopt_t; +typedef struct mio_bopt_lng_t mio_bopt_lng_t; + +struct mio_bopt_lng_t +{ + const mio_bch_t* str; + mio_bci_t val; +}; + +struct mio_bopt_t +{ + /* input */ + const mio_bch_t* str; /* option string */ + mio_bopt_lng_t* lng; /* long options */ + + /* output */ + mio_bci_t opt; /* character checked for validity */ + mio_bch_t* arg; /* argument associated with an option */ + + /* output */ + const mio_bch_t* lngopt; + + /* input + output */ + int ind; /* index into parent argv vector */ + + /* input + output - internal*/ + mio_bch_t* cur; +}; + +#if defined(__cplusplus) +extern "C" { +#endif + +/** + * The mio_getopt() function processes the \a argc command-line arguments + * pointed to by \a argv as configured in \a opt. It can process two + * different option styles: a single character starting with '-', and a + * long name starting with '--'. + * + * A character in \a opt.str is treated as a single character option. Should + * it require a parameter, specify ':' after it. + * + * Two special returning option characters indicate special error conditions. + * - \b ? indicates a bad option stored in the \a opt->opt field. + * - \b : indicates a bad parameter for an option stored in the \a opt->opt field. + * + * @return an option character on success, MIO_CHAR_EOF on no more options. + */ +MIO_EXPORT mio_uci_t mio_getuopt ( + int argc, /* argument count */ + mio_uch_t* const* argv, /* argument array */ + mio_uopt_t* opt /* option configuration */ +); + +MIO_EXPORT mio_bci_t mio_getbopt ( + int argc, /* argument count */ + mio_bch_t* const* argv, /* argument array */ + mio_bopt_t* opt /* option configuration */ +); + + +#if defined(MIO_OOCH_IS_UCH) +# define mio_opt_t mio_uopt_t +# define mio_opt_lng_t mio_uopt_lng_t +# define mio_getopt(argc,argv,opt) mio_getuopt(argc,argv,opt) +#else +# define mio_opt_t mio_bopt_t +# define mio_opt_lng_t mio_bopt_lng_t +# define mio_getopt(argc,argv,opt) mio_getbopt(argc,argv,opt) +#endif + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/mio/lib/opt-imp.h b/mio/lib/opt-imp.h new file mode 100644 index 0000000..24cf92e --- /dev/null +++ b/mio/lib/opt-imp.h @@ -0,0 +1,215 @@ +/* + * $Id$ + * + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +/* this file is supposed to be included by opt.c multiple times */ + + +#include "mio-opt.h" +#include "mio-utl.h" + +/* + * mio_getopt is based on BSD getopt. + * -------------------------------------------------------------------------- + * + * Copyright (c) 1987-2002 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * A. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * B. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * C. Neither the names of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS + * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * -------------------------------------------------------------------------- + */ + + + +xci_t xgetopt (int argc, xch_t* const* argv, xopt_t* opt) +{ + xch_t* oli; /* option letter list index */ + int dbldash = 0; + + opt->arg = MIO_NULL; + opt->lngopt = MIO_NULL; + + if (opt->cur == MIO_NULL) + { + opt->cur = XEMSG; + opt->ind = 1; + } + + if (*opt->cur == '\0') + { + /* update scanning pointer */ + if (opt->ind >= argc || *(opt->cur = argv[opt->ind]) != '-') + { + /* All arguments have been processed or the current + * argument doesn't start with a dash */ + opt->cur = XEMSG; + return XCI_EOF; + } + + opt->cur++; + + #if 0 + if (*opt->cur == '\0') + { + /* - */ + opt->ind++; + opt->cur = XEMSG; + return XCI_EOF; + } + #endif + + if (*opt->cur == '-') + { + if (*++opt->cur == '\0') + { + /* -- */ + opt->ind++; + opt->cur = XEMSG; + return XCI_EOF; + } + else + { + dbldash = 1; + } + } + } + + if (dbldash && opt->lng != MIO_NULL) + { + const xopt_lng_t* o; + xch_t* end = opt->cur; + + while (*end != '\0' && *end != '=') end++; + + for (o = opt->lng; o->str; o++) + { + const xch_t* str = o->str; + + if (*str == ':') str++; + + if (xcompcharscstr(opt->cur, end - opt->cur, str, 0) != 0) continue; + + /* match */ + opt->cur = XEMSG; + opt->lngopt = o->str; + + /* for a long matching option, remove the leading colon */ + if (opt->lngopt[0] == ':') opt->lngopt++; + + if (*end == '=') opt->arg = end + 1; + + if (*o->str != ':') + { + /* should not have an option argument */ + if (opt->arg != MIO_NULL) return BADARG; + } + else if (opt->arg == MIO_NULL) + { + /* check if it has a remaining argument + * available */ + if (argc <= ++opt->ind) return BADARG; + /* If so, the next available argument is + * taken to be an option argument */ + opt->arg = argv[opt->ind]; + } + + opt->ind++; + return o->val; + } + + /*if (*end == MIO_T('=')) *end = MIO_T('\0');*/ + opt->lngopt = opt->cur; + return BADCH; + } + + if ((opt->opt = *opt->cur++) == ':' || + (oli = xfindcharincstr(opt->str, opt->opt)) == MIO_NULL) + { + /* + * if the user didn't specify '-' as an option, + * assume it means EOF. + */ + if (opt->opt == (int)'-') return XCI_EOF; + if (*opt->cur == '\0') ++opt->ind; + return BADCH; + } + + if (*++oli != ':') + { + /* don't need argument */ + if (*opt->cur == '\0') opt->ind++; + } + else + { + /* need an argument */ + + if (*opt->cur != '\0') + { + /* no white space */ + opt->arg = opt->cur; + } + else if (argc <= ++opt->ind) + { + /* no arg */ + opt->cur = XEMSG; + /*if (*opt->str == ':')*/ return BADARG; + /*return BADCH;*/ + } + else + { + /* white space */ + opt->arg = argv[opt->ind]; + } + + opt->cur = XEMSG; + opt->ind++; + } + + return opt->opt; /* dump back option letter */ +} diff --git a/mio/lib/opt.c b/mio/lib/opt.c new file mode 100644 index 0000000..51ed761 --- /dev/null +++ b/mio/lib/opt.c @@ -0,0 +1,82 @@ +/* + * $Id$ + * + Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "mio-opt.h" +#include "mio-utl.h" + +#define BADCH '?' +#define BADARG ':' + +static mio_uch_t EMSG_UCH[] = { '\0' }; +static mio_bch_t EMSG_BCH[] = { '\0' }; + +/* ------------------------------------------------------------ */ + +#undef XEMSG +#undef xch_t +#undef xci_t +#undef xopt_t +#undef xopt_lng_t +#undef xgetopt +#undef xcompcharscstr +#undef xfindcharincstr +#undef XCI_EOF + +#define XEMSG EMSG_UCH +#define xch_t mio_uch_t +#define xci_t mio_uci_t +#define xopt_t mio_uopt_t +#define xopt_lng_t mio_uopt_lng_t +#define xgetopt mio_getuopt +#define xcompcharscstr mio_comp_uchars_ucstr +#define xfindcharincstr mio_find_uchar_in_ucstr +#define XCI_EOF MIO_BCI_EOF +#include "opt-imp.h" + +/* ------------------------------------------------------------ */ + +#undef XEMSG +#undef xch_t +#undef xci_t +#undef xopt_t +#undef xopt_lng_t +#undef xgetopt +#undef xcompcharscstr +#undef xfindcharincstr +#undef XCI_EOF + +#define XEMSG EMSG_BCH +#define xch_t mio_bch_t +#define xci_t mio_bci_t +#define xopt_t mio_bopt_t +#define xopt_lng_t mio_bopt_lng_t +#define xgetopt mio_getbopt +#define xcompcharscstr mio_comp_bchars_bcstr +#define xfindcharincstr mio_find_bchar_in_bcstr +#define XCI_EOF MIO_UCI_EOF +#include "opt-imp.h" + +/* ------------------------------------------------------------ */