New module 'getsubopt'.
authorBruno Haible <bruno@clisp.org>
Fri, 6 Aug 2004 20:23:41 +0000 (20:23 +0000)
committerBruno Haible <bruno@clisp.org>
Fri, 6 Aug 2004 20:23:41 +0000 (20:23 +0000)
ChangeLog
MODULES.html.sh
lib/ChangeLog
lib/getsubopt.c [new file with mode: 0644]
lib/getsubopt.h [new file with mode: 0644]
m4/ChangeLog
m4/getsubopt.m4 [new file with mode: 0644]
modules/getsubopt [new file with mode: 0644]

index fb60092..f8c8197 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2004-08-02  Simon Josefsson  <jas@extundo.com>
+
+       * modules/getsubopt: New file.
+       * MODULES.html.sh (Support for systems lacking POSIX:2001): Add
+       getsubopt.
+
 2004-08-05  Paul Eggert  <eggert@cs.ucla.edu>
 
        Merge from coreutils.
index c8e9dc2..da64c51 100755 (executable)
@@ -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
index 07616d5..c7da52c 100644 (file)
@@ -1,3 +1,9 @@
+2004-08-02  Simon Josefsson  <jas@extundo.com>
+
+       * getsubopt.h: New file, with comments from Bruno Haible.
+       * getsubopt.c: New file, from glibc, but slightly modified based on
+       suggestions from Paul Eggert <eggert@cs.ucla.edu>.
+
 2004-08-05  Paul Eggert  <eggert@cs.ucla.edu>
 
        Merge from coreutils.
diff --git a/lib/getsubopt.c b/lib/getsubopt.c
new file mode 100644 (file)
index 0000000..bd4034e
--- /dev/null
@@ -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 <drepper@cygnus.com>, 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 <stdlib.h>
+#include <string.h>
+
+#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 (file)
index 0000000..24817ba
--- /dev/null
@@ -0,0 +1,41 @@
+/* Parse comma separate list into words.
+   Copyright (C) 2004 Free Software Foundation, Inc.
+   Contributed by Simon Josefsson <jas@extundo.com>, 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 <stdlib.h>
+
+#else
+
+extern int getsubopt (char **optionp, char *const *tokens, char **valuep);
+
+#endif
index a493c1c..098f169 100644 (file)
@@ -1,3 +1,7 @@
+2004-08-02  Simon Josefsson  <jas@extundo.com>
+
+       * getsubopt.m4: New file.
+
 2004-08-05  Paul Eggert  <eggert@cs.ucla.edu>
 
        Merge from coreutils.
diff --git a/m4/getsubopt.m4 b/m4/getsubopt.m4
new file mode 100644 (file)
index 0000000..9b87e7c
--- /dev/null
@@ -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 <stdlib.h> 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 (file)
index 0000000..780c0ff
--- /dev/null
@@ -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