diff options
author | jbj <devnull@localhost> | 1999-10-25 18:22:19 +0000 |
---|---|---|
committer | jbj <devnull@localhost> | 1999-10-25 18:22:19 +0000 |
commit | 1cdcd275a8b81a8e5258a0696ab198ccfd7408ca (patch) | |
tree | 4740d7eb8fb2ff8d643498ca212d2adf4ad05deb /popt | |
parent | e2d517c67ce9c4a100bad0e44f17b930e4b457b7 (diff) | |
download | librpm-tizen-1cdcd275a8b81a8e5258a0696ab198ccfd7408ca.tar.gz librpm-tizen-1cdcd275a8b81a8e5258a0696ab198ccfd7408ca.tar.bz2 librpm-tizen-1cdcd275a8b81a8e5258a0696ab198ccfd7408ca.zip |
Add !#:+ token parsing to retrieve (and delete) next argument from list.
CVS patchset: 3398
CVS date: 1999/10/25 18:22:19
Diffstat (limited to 'popt')
-rw-r--r-- | popt/Makefile.am | 8 | ||||
-rwxr-xr-x | popt/configure.in | 8 | ||||
-rw-r--r-- | popt/findme.c | 21 | ||||
-rw-r--r-- | popt/po/popt.pot | 6 | ||||
-rw-r--r-- | popt/po/ro.po | 6 | ||||
-rw-r--r-- | popt/po/sk.po | 6 | ||||
-rw-r--r-- | popt/popt.c | 168 | ||||
-rw-r--r-- | popt/popt.h | 4 | ||||
-rw-r--r-- | popt/poptconfig.c | 23 | ||||
-rw-r--r-- | popt/popthelp.c | 17 | ||||
-rw-r--r-- | popt/poptint.h | 17 | ||||
-rw-r--r-- | popt/poptparse.c | 101 | ||||
-rw-r--r-- | popt/system.h | 55 | ||||
-rw-r--r-- | popt/test-poptrc | 3 | ||||
-rw-r--r-- | popt/test1.c | 29 | ||||
-rw-r--r-- | popt/test2.c | 10 | ||||
-rwxr-xr-x | popt/testit.sh | 4 |
17 files changed, 310 insertions, 176 deletions
diff --git a/popt/Makefile.am b/popt/Makefile.am index c0aca777e..ce9d5e6e4 100644 --- a/popt/Makefile.am +++ b/popt/Makefile.am @@ -2,25 +2,27 @@ AUTOMAKE_OPTIONS = 1.4 foreign -EXTRA_DIST = CHANGES autogen.sh findme.h $(man_MANS) popt.spec poptint.h \ +EXTRA_DIST = CHANGES autogen.sh $(man_MANS) popt.spec \ testit.sh po/Makefile.in.in po/POTFILES.in po/*.po po/popt.pot popt.ps SUBDIRS = intl po INCLUDES = -I$(top_srcdir) -noinst_INCLUDES = findme.h poptint.h +noinst_HEADERS = findme.h poptint.h system.h noinst_PROGRAMS = test1 test2 test1_SOURCES = test1.c +test1_LDFLAGS = -all-static test1_LDADD = $(lib_LTLIBRARIES) test2_SOURCES = test2.c +test2_LDFLAGS = -all-static test2_LDADD = $(lib_LTLIBRARIES) noinst_SCRIPTS = testit.sh TESTS_ENVIRONMENT = \ -test1="`pwd`/.libs/lt-test1" +test1="./test1" TESTS = testit.sh diff --git a/popt/configure.in b/popt/configure.in index a04b086a5..665a277fd 100755 --- a/popt/configure.in +++ b/popt/configure.in @@ -45,7 +45,7 @@ else fi AC_SUBST(TARGET) -AC_CHECK_HEADERS(unistd.h alloca.h libintl.h) +AC_CHECK_HEADERS(alloca.h libintl.h mcheck.h unistd.h) AC_MSG_CHECKING(for /usr/ucblib in LIBS) if test -d /usr/ucblib ; then if test "$build" = "mips-sni-sysv4" ; then @@ -59,9 +59,9 @@ else AC_MSG_RESULT(no) fi -AC_CHECK_FUNCS(strerror) -AC_CHECK_FUNCS(gettext) -AC_CHECK_FUNCS(dgettext) +AC_CHECK_FUNCS(strerror mtrace) +dnl AC_CHECK_FUNCS(gettext) +dnl AC_CHECK_FUNCS(dgettext) AC_CHECK_FUNC(setreuid, [], [ AC_CHECK_LIB(ucb, setreuid, [if echo $LIBS | grep -- -lucb >/dev/null ;then :; else LIBS="$LIBS -lc -lucb" USEUCB=y;fi]) ]) diff --git a/popt/findme.c b/popt/findme.c index 4ba4950e5..6d1b41c18 100644 --- a/popt/findme.c +++ b/popt/findme.c @@ -2,24 +2,7 @@ file accompanying popt source distributions, available from ftp://ftp.redhat.com/pub/code/popt */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#ifdef __NeXT -/* access macros are not declared in non posix mode in unistd.h - - don't try to use posix on NeXTstep 3.3 ! */ -#include <libc.h> -#endif - -#if HAVE_ALLOCA_H -# include <alloca.h> -#endif - +#include "system.h" #include "findme.h" const char * findProgramPath(const char * argv0) { @@ -31,7 +14,7 @@ const char * findProgramPath(const char * argv0) { /* If there is a / in the argv[0], it has to be an absolute path */ if (strchr(argv0, '/')) - return strdup(argv0); + return xstrdup(argv0); if (!path) return NULL; diff --git a/popt/po/popt.pot b/popt/po/popt.pot index 92380710b..e0204d7eb 100644 --- a/popt/po/popt.pot +++ b/popt/po/popt.pot @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" -"POT-Creation-Date: 1999-10-22 14:22-0400\n" +"POT-Creation-Date: 1999-10-25 14:20-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <LL@li.org>\n" @@ -14,10 +14,10 @@ msgstr "" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: ENCODING\n" -#: popthelp.c:36 +#: popthelp.c:23 msgid "Show this help message" msgstr "" -#: popthelp.c:37 +#: popthelp.c:24 msgid "Display brief usage message" msgstr "" diff --git a/popt/po/ro.po b/popt/po/ro.po index ff83784e0..f01b2de64 100644 --- a/popt/po/ro.po +++ b/popt/po/ro.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: POPT\n" -"POT-Creation-Date: 1999-09-21 14:38-0400\n" +"POT-Creation-Date: 1999-10-22 17:29-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cristian Gafton <gafton@redhat.com>\n" "Language-Team: LANGUAGE <LL@li.org>\n" @@ -14,11 +14,11 @@ msgstr "" "Content-Type: text/plain; charset=ISO-8859-2\n" "Content-Transfer-Encoding: 8bit\n" -#: ../popthelp.c:35 +#: popthelp.c:23 msgid "Show this help message" msgstr "Afisare mesaj de help" -#: ../popthelp.c:36 +#: popthelp.c:24 msgid "Display brief usage message" msgstr "Afisare mesaj sintaxa sumar" diff --git a/popt/po/sk.po b/popt/po/sk.po index fcb9c1946..712b63e39 100644 --- a/popt/po/sk.po +++ b/popt/po/sk.po @@ -5,7 +5,7 @@ msgid "" msgstr "" "Project-Id-Version: popt\n" -"POT-Creation-Date: 1999-09-21 14:38-0400\n" +"POT-Creation-Date: 1999-10-22 17:29-0400\n" "PO-Revision-Date: 1999-08-04 21:40+0200\n" "Last-Translator: Stanislav Meduna <stano@eunet.sk>\n" "Language-Team: Slovak <sk-i18n@rak.isternet.sk>\n" @@ -13,10 +13,10 @@ msgstr "" "Content-Type: text/plain; charset=iso-8859-2\n" "Content-Transfer-Encoding: 8bit\n" -#: ../popthelp.c:35 +#: popthelp.c:23 msgid "Show this help message" msgstr "Vypísať túto správu" -#: ../popthelp.c:36 +#: popthelp.c:24 msgid "Display brief usage message" msgstr "Zobraziť stručný návod na použitie" diff --git a/popt/popt.c b/popt/popt.c index 27b014ed2..6b829887b 100644 --- a/popt/popt.c +++ b/popt/popt.c @@ -2,25 +2,8 @@ file accompanying popt source distributions, available from ftp://ftp.redhat.com/pub/code/popt */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include <errno.h> -#include <ctype.h> -#include <fcntl.h> -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#if HAVE_ALLOCA_H -# include <alloca.h> -#endif - +#include "system.h" #include "findme.h" -#include "popt.h" #include "poptint.h" #ifndef HAVE_STRERROR @@ -37,7 +20,7 @@ static char * strerror(int errno) { void poptSetExecPath(poptContext con, const char * path, int allowAbsolute) { if (con->execPath) xfree(con->execPath); - con->execPath = strdup(path); + con->execPath = xstrdup(path); con->execAbsolute = allowAbsolute; } @@ -69,6 +52,7 @@ poptContext poptGetContext(const char * name, int argc, char ** argv, con->os = con->optionStack; con->os->argc = argc; con->os->argv = (const char **)argv; /* XXX don't change the API */ + con->os->argb = PBM_ALLOC(argc); if (!(flags & POPT_CONTEXT_KEEP_FIRST)) con->os->next = 1; /* skip argv[0] */ @@ -91,10 +75,32 @@ poptContext poptGetContext(const char * name, int argc, char ** argv, return con; } +static void cleanOSE(struct optionStackEntry *os) +{ + if (os->nextArg) { + xfree(os->nextArg); + os->nextArg = NULL; + } + if (os->argv) { + xfree(os->argv); + os->argv = NULL; + } + if (os->argb) { + PBM_FREE(os->argb); + os->argb = NULL; + } +} + void poptResetContext(poptContext con) { int i; - con->os = con->optionStack; + while (con->os > con->optionStack) { + cleanOSE(con->os--); + } + if (con->os->argb) { + PBM_FREE(con->os->argb); + con->os->argb = PBM_ALLOC(con->os->argc); + } con->os->currAlias = NULL; con->os->nextCharArg = NULL; con->os->nextArg = NULL; @@ -177,8 +183,7 @@ static int handleAlias(poptContext con, const char * longName, char shortName, if (i < 0) return 0; - if ((con->os - con->optionStack + 1) - == POPT_OPTION_DEPTH) + if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH) return POPT_ERROR_OPTSTOODEEP; if (nextCharArg && *nextCharArg) @@ -190,8 +195,9 @@ static int handleAlias(poptContext con, const char * longName, char shortName, con->os->nextArg = NULL; con->os->nextCharArg = NULL; con->os->currAlias = con->aliases + i; - con->os->argc = con->os->currAlias->argc; - con->os->argv = con->os->currAlias->argv; + poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv, + &con->os->argc, &con->os->argv); + con->os->argb = PBM_ALLOC(con->os->argc); return 1; } @@ -297,6 +303,71 @@ findOption(const struct poptOption * table, const char * longName, return opt; } +static const char *findNextArg(poptContext con, unsigned argx, int delete) +{ + struct optionStackEntry * os = con->os; + const char * arg; + + do { + int i; + arg = NULL; + while (os->next == os->argc && os > con->optionStack) os--; + if (os->next == os->argc && os == con->optionStack) break; + for (i = os->next; i < os->argc; i++) { + if (os->argb && PBM_ISSET(i, os->argb)) continue; + if (*os->argv[i] == '-') continue; + if (--argx > 0) continue; + arg = os->argv[i]; + if (delete) { + if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc); + PBM_SET(i, os->argb); + } + break; + } + if (os > con->optionStack) os--; + } while (arg == NULL); + return arg; +} + +static const char * expandNextArg(poptContext con, const char * s) +{ + const char *a; + size_t alen; + char *t, *te; + size_t tn = strlen(s) + 1; + char c; + + te = t = malloc(tn);; + while ((c = *s++) != '\0') { + switch (c) { + case '\\': /* escape */ + c = *s++; + break; + case '!': + if (!(s[0] == '#' && s[1] == ':' && s[2] == '+')) + break; + if ((a = findNextArg(con, 1, 1)) == NULL) + break; + s += 3; + + alen = strlen(a); + tn += alen; + *te = '\0'; + t = realloc(t, tn); + te = t + strlen(t); + strncpy(te, a, alen); te += alen; + continue; + /*@notreached@*/ break; + default: + break; + } + *te++ = c; + } + *te = '\0'; + t = realloc(t, strlen(t)); + return t; +} + /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */ int poptGetNextOpt(poptContext con) { @@ -310,17 +381,23 @@ int poptGetNextOpt(poptContext con) const char * longArg = NULL; while (!con->os->nextCharArg && con->os->next == con->os->argc - && con->os > con->optionStack) - con->os--; + && con->os > con->optionStack) { + cleanOSE(con->os--); + } if (!con->os->nextCharArg && con->os->next == con->os->argc) { invokeCallbacks(con, con->options, 1); if (con->doExec) execCommand(con); return -1; } + /* Process next long option */ if (!con->os->nextCharArg) { char * localOptString, * optString; + if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) { + con->os->next++; + continue; + } origOptString = con->os->argv[con->os->next++]; if (con->restLeftover || *origOptString != '-') { @@ -351,6 +428,7 @@ int poptGetNextOpt(poptContext con) else singleDash = 1; + /* XXX aliases with arg substitution need "--alias=arg" */ if (handleAlias(con, optString, '\0', NULL)) continue; if (handleExec(con, optString, '\0')) @@ -375,6 +453,7 @@ int poptGetNextOpt(poptContext con) con->os->nextCharArg = origOptString + 1; } + /* Process next short option */ if (con->os->nextCharArg) { origOptString = con->os->nextCharArg; @@ -404,19 +483,24 @@ int poptGetNextOpt(poptContext con) if (opt->arg) *((int *) opt->arg) = opt->val; } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) { + if (con->os->nextArg) { + xfree(con->os->nextArg); + con->os->nextArg = NULL; + } if (longArg) { - con->os->nextArg = longArg; + con->os->nextArg = expandNextArg(con, longArg); } else if (con->os->nextCharArg) { - con->os->nextArg = con->os->nextCharArg; + con->os->nextArg = expandNextArg(con, con->os->nextCharArg); con->os->nextCharArg = NULL; } else { while (con->os->next == con->os->argc && - con->os > con->optionStack) - con->os--; + con->os > con->optionStack) { + cleanOSE(con->os--); + } if (con->os->next == con->os->argc) return POPT_ERROR_NOARG; - con->os->nextArg = con->os->argv[con->os->next++]; + con->os->nextArg = expandNextArg(con, con->os->argv[con->os->next++]); } if (opt->arg) { @@ -425,7 +509,7 @@ int poptGetNextOpt(poptContext con) switch (opt->argInfo & POPT_ARG_MASK) { case POPT_ARG_STRING: - *((const char **) opt->arg) = con->os->nextArg; + *((const char **) opt->arg) = xstrdup(con->os->nextArg); break; case POPT_ARG_INT: @@ -473,8 +557,9 @@ int poptGetNextOpt(poptContext con) } if (opt->arg && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE - && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL) - con->finalArgv[con->finalArgvCount++] = strdup(con->os->nextArg); + && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL) { + con->finalArgv[con->finalArgvCount++] = xstrdup(con->os->nextArg); + } } return opt->val; @@ -508,6 +593,9 @@ const char ** poptGetArgs(poptContext con) { void poptFreeContext(poptContext con) { int i; + poptResetContext(con); + if (con->os->argb) free(con->os->argb); + for (i = 0; i < con->numAliases; i++) { if (con->aliases[i].longName) xfree(con->aliases[i].longName); free(con->aliases[i].argv); @@ -517,9 +605,7 @@ void poptFreeContext(poptContext con) { if (con->execs[i].longName) xfree(con->execs[i].longName); xfree(con->execs[i].script); } - - for (i = 0; i < con->finalArgvCount; i++) - xfree(con->finalArgv[i]); + xfree(con->execs); free(con->leftovers); free(con->finalArgv); @@ -593,12 +679,12 @@ const char *const poptStrerror(const int error) { } int poptStuffArgs(poptContext con, const char ** argv) { - int i; + int argc; if ((con->os - con->optionStack) == POPT_OPTION_DEPTH) return POPT_ERROR_OPTSTOODEEP; - for (i = 0; argv[i]; i++) + for (argc = 0; argv[argc]; argc++) ; con->os++; @@ -606,8 +692,8 @@ int poptStuffArgs(poptContext con, const char ** argv) { con->os->nextArg = NULL; con->os->nextCharArg = NULL; con->os->currAlias = NULL; - con->os->argc = i; - con->os->argv = argv; + poptDupArgv(argc, argv, &con->os->argc, &con->os->argv); + con->os->argb = PBM_ALLOC(argc); con->os->stuffed = 1; return 0; diff --git a/popt/popt.h b/popt/popt.h index 459dea70e..b1e58faba 100644 --- a/popt/popt.h +++ b/popt/popt.h @@ -109,8 +109,10 @@ int poptReadConfigFile(poptContext con, const char * fn); int poptReadDefaultConfig(poptContext con, int useEnv); /* argv should be freed -- this allows ', ", and \ quoting, but ' is treated the same as " and both may include \ quotes */ +int poptDupArgv(int argc, const char **argv, + /*@out@*/ int * argcPtr, /*@out@*/ const char *** argvPtr); int poptParseArgvString(const char * s, - /*@out@*/ int * argcPtr, /*@out@*/ char *** argvPtr); + /*@out@*/ int * argcPtr, /*@out@*/ const char *** argvPtr); /*@observer@*/ const char *const poptStrerror(const int error); void poptSetExecPath(poptContext con, const char * path, int allowAbsolute); void poptPrintHelp(poptContext con, FILE * f, int flags); diff --git a/popt/poptconfig.c b/popt/poptconfig.c index 5f7ec8d23..9d238620c 100644 --- a/popt/poptconfig.c +++ b/popt/poptconfig.c @@ -2,22 +2,7 @@ file accompanying popt source distributions, available from ftp://ftp.redhat.com/pub/code/popt */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include <ctype.h> -#include <errno.h> -#include <fcntl.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#if HAVE_ALLOCA_H -# include <alloca.h> -#endif - -#include "popt.h" +#include "system.h" #include "poptint.h" static void configLine(poptContext con, char * line) { @@ -51,19 +36,19 @@ static void configLine(poptContext con, char * line) { shortName = opt[1]; if (!strcmp(entryType, "alias")) { - if (poptParseArgvString(line, &alias.argc, (char ***)&alias.argv)) return; + if (poptParseArgvString(line, &alias.argc, &alias.argv)) return; alias.longName = longName, alias.shortName = shortName; poptAddAlias(con, alias, 0); } else if (!strcmp(entryType, "exec")) { con->execs = realloc(con->execs, /* XXX memory leak */ sizeof(*con->execs) * (con->numExecs + 1)); if (longName) - con->execs[con->numExecs].longName = strdup(longName); + con->execs[con->numExecs].longName = xstrdup(longName); else con->execs[con->numExecs].longName = NULL; con->execs[con->numExecs].shortName = shortName; - con->execs[con->numExecs].script = strdup(line); + con->execs[con->numExecs].script = xstrdup(line); con->numExecs++; } diff --git a/popt/popthelp.c b/popt/popthelp.c index 4ebe621fd..c36eceadd 100644 --- a/popt/popthelp.c +++ b/popt/popthelp.c @@ -4,20 +4,7 @@ file accompanying popt source distributions, available from ftp://ftp.redhat.com/pub/code/popt */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include <ctype.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#ifdef HAVE_ALLOCA_H -#include <alloca.h> -#endif - -#include "popt.h" +#include "system.h" #include "poptint.h" static void displayArgs(poptContext con, @@ -310,5 +297,5 @@ void poptPrintUsage(poptContext con, FILE * f, /*@unused@*/ int flags) { void poptSetOtherOptionHelp(poptContext con, const char * text) { if (con->otherHelp) xfree(con->otherHelp); - con->otherHelp = strdup(text); + con->otherHelp = xstrdup(text); } diff --git a/popt/poptint.h b/popt/poptint.h index 64e8cec62..89ef4e9e8 100644 --- a/popt/poptint.h +++ b/popt/poptint.h @@ -5,9 +5,26 @@ #ifndef H_POPTINT #define H_POPTINT +/* Bit mask macros. */ +typedef unsigned int __pbm_bits; +#define __PBM_NBITS (8 * sizeof (__pbm_bits)) +#define __PBM_IX(d) ((d) / __PBM_NBITS) +#define __PBM_MASK(d) ((__pbm_bits) 1 << ((d) % __PBM_NBITS)) +typedef struct { + __pbm_bits bits[1]; +} pbm_set; +#define __PBM_BITS(set) ((set)->bits) + +#define PBM_ALLOC(d) calloc(__PBM_IX (d) + 1, sizeof(__pbm_bits)) +#define PBM_FREE(s) free(s); +#define PBM_SET(d, s) (__PBM_BITS (s)[__PBM_IX (d)] |= __PBM_MASK (d)) +#define PBM_CLR(d, s) (__PBM_BITS (s)[__PBM_IX (d)] &= ~__PBM_MASK (d)) +#define PBM_ISSET(d, s) ((__PBM_BITS (s)[__PBM_IX (d)] & __PBM_MASK (d)) != 0) + struct optionStackEntry { int argc; /*@keep@*/ const char ** argv; + /*@only@*/ pbm_set * argb; int next; /*@keep@*/ const char * nextArg; /*@keep@*/ const char * nextCharArg; diff --git a/popt/poptparse.c b/popt/poptparse.c index 0d318f461..ad455de57 100644 --- a/popt/poptparse.c +++ b/popt/poptparse.c @@ -2,53 +2,52 @@ file accompanying popt source distributions, available from ftp://ftp.redhat.com/pub/code/popt */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include "system.h" +#include "poptint.h" -#include <ctype.h> -#include <stdlib.h> -#include <string.h> - -/* AIX requires this to be the first thing in the file. */ -#ifndef __GNUC__ -# if HAVE_ALLOCA_H -# include <alloca.h> -# else -# ifdef _AIX -#pragma alloca -# else -# ifndef alloca /* predefined by HP cc +Olibcalls */ -char *alloca (); -# endif -# endif -# endif -#elif defined(__GNUC__) && defined(__STRICT_ANSI__) -#define alloca __builtin_alloca -#endif +#define POPT_ARGV_ARRAY_GROW_DELTA 5 -#include "popt.h" +int poptDupArgv(int argc, const char **argv, + int * argcPtr, const char *** argvPtr) +{ + size_t nb = (argc + 1) * sizeof(*argv); + const char ** argv2; + char * dst; + int i; -#define POPT_ARGV_ARRAY_GROW_DELTA 5 + for (i = 0; i < argc; i++) { + if (argv[i] == NULL) + return POPT_ERROR_NOARG; + nb += strlen(argv[i]) + 1; + } + + argv2 = (void *) dst = malloc(nb); + dst += (argc + 1) * sizeof(*argv); -int poptParseArgvString(const char * s, int * argcPtr, char *** argvPtr) { - char * buf, * bufStart, * dst; + for (i = 0; i < argc; i++) { + argv2[i] = dst; + dst += strlen(strcpy(dst, argv[i])) + 1; + } + argv2[argc] = NULL; + + *argvPtr = argv2; + *argcPtr = argc; + return 0; +} + +int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr) +{ const char * src; char quote = '\0'; int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA; - char ** argv = malloc(sizeof(*argv) * argvAlloced); - const char ** argv2; + const char ** argv = malloc(sizeof(*argv) * argvAlloced); int argc = 0; - int i, buflen; - - buflen = strlen(s) + 1; - bufStart = buf = alloca(buflen); - memset(buf, '\0', buflen); + int buflen = strlen(s) + 1; + char * buf = memset(alloca(buflen), 0, buflen); - src = s; argv[argc] = buf; - while (*src) { + for (src = s; *src; src++) { if (quote == *src) { quote = '\0'; } else if (quote) { @@ -86,29 +85,33 @@ int poptParseArgvString(const char * s, int * argcPtr, char *** argvPtr) { *buf++ = *src; break; } - - src++; } if (strlen(argv[argc])) { argc++, buf++; } - dst = malloc((argc + 1) * sizeof(*argv) + (buf - bufStart)); - argv2 = (void *) dst; - dst += (argc + 1) * sizeof(*argv); - memcpy(argv2, argv, argc * sizeof(*argv)); - argv2[argc] = NULL; - memcpy(dst, bufStart, buf - bufStart); +#if 0 + { char * dst = malloc((argc + 1) * sizeof(*argv) + (buf - argv[0])); + const char ** argv2 = (void *) dst; + int i; - for (i = 0; i < argc; i++) { - argv2[i] = dst + (argv[i] - bufStart); + dst += (argc + 1) * sizeof(*argv); + memcpy(argv2, argv, argc * sizeof(*argv)); + argv2[argc] = NULL; + memcpy(dst, argv[0], buf - argv[0]); + + for (i = 0; i < argc; i++) + argv2[i] = dst + (argv[i] - argv[0]); + + *argvPtr = argv2; + *argcPtr = argc; } +#else + (void) poptDupArgv(argc, argv, argcPtr, argvPtr); +#endif free(argv); - *argvPtr = (char **)argv2; /* XXX don't change the API */ - *argcPtr = argc; - return 0; } diff --git a/popt/system.h b/popt/system.h new file mode 100644 index 000000000..43ad70f9a --- /dev/null +++ b/popt/system.h @@ -0,0 +1,55 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> + +#if HAVE_MCHECK_H +#include <mcheck.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#if HAVE_UNISTD_H +#include <unistd.h> +#endif + +#ifdef __NeXT +/* access macros are not declared in non posix mode in unistd.h - + don't try to use posix on NeXTstep 3.3 ! */ +#include <libc.h> +#endif + +/* AIX requires this to be the first thing in the file. */ +#ifndef __GNUC__ +# if HAVE_ALLOCA_H +# include <alloca.h> +# else +# ifdef _AIX +#pragma alloca +# else +# ifndef alloca /* predefined by HP cc +Olibcalls */ +char *alloca (); +# endif +# endif +# endif +#elif defined(__GNUC__) && defined(__STRICT_ANSI__) +#define alloca __builtin_alloca +#endif + +/*@only@*/ char * xstrdup (const char *str); + +#if HAVE_MCHECK_H && defined(__GNUC__) +#define vmefail() (fprintf(stderr, "virtual memory exhausted.\n"), exit(EXIT_FAILURE), NULL) +#define xstrdup(_str) (strcpy((malloc(strlen(_str)+1) ? : vmefail()), (_str))) +#else +#define xstrdup(_str) strdup(_str) +#endif /* HAVE_MCHECK_H && defined(__GNUC__) */ + + +#include "popt.h" diff --git a/popt/test-poptrc b/popt/test-poptrc index 7046ec9ba..509e0135d 100644 --- a/popt/test-poptrc +++ b/popt/test-poptrc @@ -4,7 +4,8 @@ test1 alias --takerest -- test1 alias -T --arg2 test1 alias -O --arg1 -test1 alias --wc --arg2 @@1 --arg1 @@2 +test1 alias --grab --arg2 "'foo !#:+'" +test1 alias --grabbar --grab bar test1 exec --echo-args echo test1 alias -e --echo-args diff --git a/popt/test1.c b/popt/test1.c index cd8e8977a..68ec7fdeb 100644 --- a/popt/test1.c +++ b/popt/test1.c @@ -2,10 +2,7 @@ file accompanying popt source distributions, available from ftp://ftp.redhat.com/pub/code/popt */ -#include <stdio.h> -#include <stdlib.h> - -#include "popt.h" +#include "system.h" static int pass2 = 0; static void option_callback(poptContext con, enum poptCallbackReason reason, @@ -71,35 +68,44 @@ static void resetVars(void) int main(int argc, char ** argv) { int rc; + int ec = 0; poptContext optCon; const char ** rest; int help = 0; int usage = 0; +#if HAVE_MCHECK_H && HAVE_MTRACE + mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ +#endif + resetVars(); optCon = poptGetContext("test1", argc, argv, options, 0); poptReadConfigFile(optCon, "./test-poptrc"); +#if 1 while ((rc = poptGetNextOpt(optCon)) > 0) /* Read all the options ... */ ; poptResetContext(optCon); /* ... and then start over. */ resetVars(); +#endif pass2 = 1; if ((rc = poptGetNextOpt(optCon)) < -1) { fprintf(stderr, "test1: bad argument %s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(rc)); - return 2; + ec = 2; + goto exit; } if (help) { poptPrintHelp(optCon, stdout, 0); - return 0; - } if (usage) { + goto exit; + } + if (usage) { poptPrintUsage(optCon, stdout, 0); - return 0; + goto exit; } fprintf(stdout, "arg1: %d arg2: %s", arg1, arg2); @@ -124,5 +130,10 @@ int main(int argc, char ** argv) { fprintf(stdout, "\n"); - return 0; +exit: + poptFreeContext(optCon); +#if HAVE_MCHECK_H && HAVE_MTRACE + muntrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ +#endif + return ec; } diff --git a/popt/test2.c b/popt/test2.c index 4e9275d7a..5deb89d0d 100644 --- a/popt/test2.c +++ b/popt/test2.c @@ -9,11 +9,7 @@ whim as long as this Copyright remains attached. */ -#include <stdio.h> -#include <string.h> -#include <popt.h> - -#define TEST2 +#include "system.h" char *PathnameOfKeyFile = NULL; char *PathnameOfOfferFile = NULL; @@ -123,6 +119,10 @@ main(int argc, char**argv ) { { NULL, 0, 0, NULL, 0, NULL, NULL } }; +#if HAVE_MCHECK_H && HAVE_MTRACE + mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */ +#endif + optCon = poptGetContext("createuser", argc, argv, optionsTable, 0); poptReadConfigFile(optCon, rcfile ); diff --git a/popt/testit.sh b/popt/testit.sh index 8883510f2..fdb154130 100755 --- a/popt/testit.sh +++ b/popt/testit.sh @@ -1,6 +1,6 @@ #!/bin/sh -${test1:=`pwd`/.libs/lt-test1} +${test1:=./test1} run() { prog=$1; shift @@ -47,6 +47,8 @@ run test1 "test1 - 26" "callback: c arg for cb2 foo arg1: 0 arg2: (none)" --cb2 run test1 "test1 - 27" "arg1: 0 arg2: (none) -" - run test1 "test1 - 28" "arg1: 0 arg2: foo -" - -2 foo run test1 "test1 - 29" "arg1: 0 arg2: bbbb" --arg2=aaaa -2 bbbb +run test1 "test1 - 30" "arg1: 0 arg2: 'foo bingo' rest: boggle" --grab bingo boggle +run test1 "test1 - 31" "arg1: 0 arg2: 'foo bar' rest: boggle" --grabbar boggle echo "" echo "Passed." |