From 0d2a1de66874d543cd0d9bec1a799da3faf33813 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 6 Aug 2004 20:23:41 +0000 Subject: [PATCH] New module 'getsubopt'. --- ChangeLog | 6 ++++ MODULES.html.sh | 1 + lib/ChangeLog | 6 ++++ lib/getsubopt.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ lib/getsubopt.h | 41 ++++++++++++++++++++++++ m4/ChangeLog | 4 +++ m4/getsubopt.m4 | 21 +++++++++++++ modules/getsubopt | 22 +++++++++++++ 8 files changed, 181 insertions(+) create mode 100644 lib/getsubopt.c create mode 100644 lib/getsubopt.h create mode 100644 m4/getsubopt.m4 create mode 100644 modules/getsubopt diff --git a/ChangeLog b/ChangeLog index fb60092eb4..f8c819726c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-08-02 Simon Josefsson + + * modules/getsubopt: New file. + * MODULES.html.sh (Support for systems lacking POSIX:2001): Add + getsubopt. + 2004-08-05 Paul Eggert Merge from coreutils. diff --git a/MODULES.html.sh b/MODULES.html.sh index c8e9dc2397..da64c51224 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -1702,6 +1702,7 @@ func_all_modules () func_module ftruncate func_module getgroups func_module gethostname + func_module getsubopt func_module gettimeofday func_module mkdir func_module mkstemp diff --git a/lib/ChangeLog b/lib/ChangeLog index 07616d5891..c7da52cb3b 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,9 @@ +2004-08-02 Simon Josefsson + + * getsubopt.h: New file, with comments from Bruno Haible. + * getsubopt.c: New file, from glibc, but slightly modified based on + suggestions from Paul Eggert . + 2004-08-05 Paul Eggert Merge from coreutils. diff --git a/lib/getsubopt.c b/lib/getsubopt.c new file mode 100644 index 0000000000..bd4034e7b0 --- /dev/null +++ b/lib/getsubopt.c @@ -0,0 +1,80 @@ +/* Parse comma separate list into words. + Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include +#include + +#if !_LIBC +/* This code is written for inclusion in gnu-libc, and uses names in + the namespace reserved for libc. If we're compiling in gnulib, + define those names to be the normal ones instead. */ +# include "strchrnul.h" +# undef __strchrnul +# define __strchrnul strchrnul +#endif + +/* Parse comma separated suboption from *OPTIONP and match against + strings in TOKENS. If found return index and set *VALUEP to + optional value introduced by an equal sign. If the suboption is + not part of TOKENS return in *VALUEP beginning of unknown + suboption. On exit *OPTIONP is set to the beginning of the next + token or at the terminating NUL character. */ +int +getsubopt (char **optionp, char *const *tokens, char **valuep) +{ + char *endp, *vstart; + int cnt; + + if (**optionp == '\0') + return -1; + + /* Find end of next token. */ + endp = __strchrnul (*optionp, ','); + + /* Find start of value. */ + vstart = memchr (*optionp, '=', endp - *optionp); + if (vstart == NULL) + vstart = endp; + + /* Try to match the characters between *OPTIONP and VSTART against + one of the TOKENS. */ + for (cnt = 0; tokens[cnt] != NULL; ++cnt) + if (strncmp (*optionp, tokens[cnt], vstart - *optionp) == 0 + && tokens[cnt][vstart - *optionp] == '\0') + { + /* We found the current option in TOKENS. */ + *valuep = vstart != endp ? vstart + 1 : NULL; + + if (*endp != '\0') + *endp++ = '\0'; + *optionp = endp; + + return cnt; + } + + /* The current suboption does not match any option. */ + *valuep = *optionp; + + if (*endp != '\0') + *endp++ = '\0'; + *optionp = endp; + + return -1; +} diff --git a/lib/getsubopt.h b/lib/getsubopt.h new file mode 100644 index 0000000000..24817bad3b --- /dev/null +++ b/lib/getsubopt.h @@ -0,0 +1,41 @@ +/* Parse comma separate list into words. + Copyright (C) 2004 Free Software Foundation, Inc. + Contributed by Simon Josefsson , 2004. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/* Assuming *OPTIONP is a comma separated list of elements of the form + "token" or "token=value", getsubopt parses the first of these elements. + If the first element refers to a "token" that is member of the given + NULL-terminated array of tokens: + - It replaces the comma with a NUL byte, updates *OPTIONP to point past + the first option and the comma, sets *VALUEP to the value of the + element (or NULL if it doesn't contain an "=" sign), + - It returns the index of the "token" in the given array of tokens. + Otherwise it returns -1, and *OPTIONP and *VALUEP are undefined. + For more details see the POSIX:2001 specification. + http://www.opengroup.org/onlinepubs/009695399/functions/getsubopt.html */ + +#if HAVE_GETSUBOPT + +/* Get getsubopt declaration. */ +#include + +#else + +extern int getsubopt (char **optionp, char *const *tokens, char **valuep); + +#endif diff --git a/m4/ChangeLog b/m4/ChangeLog index a493c1c983..098f169d47 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2004-08-02 Simon Josefsson + + * getsubopt.m4: New file. + 2004-08-05 Paul Eggert Merge from coreutils. diff --git a/m4/getsubopt.m4 b/m4/getsubopt.m4 new file mode 100644 index 0000000000..9b87e7c83a --- /dev/null +++ b/m4/getsubopt.m4 @@ -0,0 +1,21 @@ +# getsubopt.m4 serial 1 +dnl Copyright (C) 2004 Free Software Foundation, Inc. +dnl This file is free software, distributed under the terms of the GNU +dnl General Public License. As a special exception to the GNU General +dnl Public License, this file may be distributed as part of a program +dnl that contains a configuration script generated by Autoconf, under +dnl the same distribution terms as the rest of that program. + +AC_DEFUN([gl_FUNC_GETSUBOPT], +[ + dnl Persuade glibc to declare getsubopt(). + AC_REQUIRE([AC_GNU_SOURCE]) + + AC_REPLACE_FUNCS(getsubopt) + if test $ac_cv_func_getsubopt = no; then + gl_PREREQ_GETSUBOPT + fi +]) + +# Prerequisites of lib/getsubopt.c. +AC_DEFUN([gl_PREREQ_GETSUBOPT], [:]) diff --git a/modules/getsubopt b/modules/getsubopt new file mode 100644 index 0000000000..780c0ff0b7 --- /dev/null +++ b/modules/getsubopt @@ -0,0 +1,22 @@ +Description: +getsubopt: Parse comma separate list into words. + +Files: +lib/getsubopt.h +lib/getsubopt.c +m4/getsubopt.m4 + +Depends-on: +strchrnul + +configure.ac: +gl_FUNC_GETSUBOPT + +Makefile.am: +lib_SOURCES += getsubopt.h + +Include: +"getsubopt.h" + +Maintainer: +all, glibc -- 2.30.2