[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-gnulib] new module: getsubopt
From: |
Simon Josefsson |
Subject: |
[Bug-gnulib] new module: getsubopt |
Date: |
Sun, 01 Aug 2004 20:36:52 +0200 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux) |
How about this?
I haven't really tested it, but it looks good to me.
Comments in general is appreciated, of course, but perhaps on the
following issues in particular:
* I didn't really understand the "restrict" stuff, so perhaps the
prototype in getsubopt.c should be changed. Perhaps it should be
changed anyway, it is K&R now.
* The C file is glibc stdlib/getsubopt.c, but slightly modified. I'll
send a proposed glibc update to the glibc folks, and if it is
accepted, gnulib could sync against that.
* MODULES.html.sh? It mention getsubopt, but I dunno where to add
getsubopt. IIUC, getsubopt is X/Open, not POSIX.
Thanks.
2004-08-01 Simon Josefsson <address@hidden>
* getsubopt.c: New file, from glibc but slightly modified.
* getsubopt.h: New file.
2004-08-01 Simon Josefsson <address@hidden>
* getsubopt.m4: New file.
2004-08-01 Simon Josefsson <address@hidden>
* modules/getsubopt: New file.
Index: lib/getsubopt.h
===================================================================
RCS file: lib/getsubopt.h
diff -N lib/getsubopt.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/getsubopt.h 1 Aug 2004 18:30:00 -0000
@@ -0,0 +1,32 @@
+/* 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 <address@hidden>, 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. */
+
+#if HAVE_GETSUBOPT
+
+/* Get getsubopt declaration. */
+#include <stdlib.h>
+
+#else
+
+extern int getsubopt (char **restrict optionp,
+ char *const *restrict tokens,
+ char **restrict valuep);
+
+#endif
Index: lib/getsubopt.c
===================================================================
RCS file: lib/getsubopt.c
diff -N lib/getsubopt.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/getsubopt.c 1 Aug 2004 18:30:00 -0000
@@ -0,0 +1,77 @@
+/* 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 <address@hidden>, 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>
+
+/* Get strchrnul. */
+#include <strchrnul.h>
+
+/* 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 (optionp, tokens, valuep)
+ 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 (memcmp (*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;
+}
Index: m4/getsubopt.m4
===================================================================
RCS file: m4/getsubopt.m4
diff -N m4/getsubopt.m4
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ m4/getsubopt.m4 1 Aug 2004 18:30:00 -0000
@@ -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], [:])
Index: modules/getsubopt
===================================================================
RCS file: modules/getsubopt
diff -N modules/getsubopt
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ modules/getsubopt 1 Aug 2004 18:30:00 -0000
@@ -0,0 +1,23 @@
+Description:
+getsubopt: Parse comma separate list into words.
+
+Files:
+lib/getsubopt.h
+lib/getsubopt.c
+m4/getsubopt.m4
+
+Depends-on:
+strchrnul
+restrict
+
+configure.ac:
+gl_FUNC_GETSUBOPT
+
+Makefile.am:
+lib_SOURCES += getsubopt.h
+
+Include:
+"getsubopt.h"
+
+Maintainer:
+all, glibc
- [Bug-gnulib] new module: getsubopt,
Simon Josefsson <=