From: Bruno Haible Date: Sat, 7 Feb 2004 18:03:46 +0000 (+0000) Subject: Use quotearg. X-Git-Tag: v0.14.2~294 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7280c1bcdb9efe1f4cd75a2f708535adf78caa15;p=thirdparty%2Fgettext.git Use quotearg. --- diff --git a/gettext-tools/ChangeLog b/gettext-tools/ChangeLog index 232ee7f00..63317205f 100644 --- a/gettext-tools/ChangeLog +++ b/gettext-tools/ChangeLog @@ -1,3 +1,7 @@ +2004-02-07 Bruno Haible + + * configure.ac: Invoke gl_QUOTEARG. + 2004-01-29 Bruno Haible * gettext-0.14.1 released. diff --git a/gettext-tools/configure.ac b/gettext-tools/configure.ac index 6bbebf436..cc57e1906 100644 --- a/gettext-tools/configure.ac +++ b/gettext-tools/configure.ac @@ -1,5 +1,5 @@ dnl Configuration for the gettext-tools directory of GNU gettext -dnl Copyright (C) 1995-1999, 2000-2003 Free Software Foundation, Inc. +dnl Copyright (C) 1995-1999, 2000-2004 Free Software Foundation, Inc. dnl dnl This program is free software; you can redistribute it and/or modify dnl it under the terms of the GNU General Public License as published by @@ -141,6 +141,7 @@ gt_FUNC_SETENV gl_FUNC_STRERROR jm_FUNC_GLIBC_UNLOCKED_IO gt_FUNC_ERROR_AT_LINE +gl_QUOTEARG gl_PATHMAX gl_FUNC_READLINK gl_XREADLINK diff --git a/gettext-tools/lib/Makefile.am b/gettext-tools/lib/Makefile.am index 5f31e39d4..128b97011 100644 --- a/gettext-tools/lib/Makefile.am +++ b/gettext-tools/lib/Makefile.am @@ -1,5 +1,5 @@ ## Makefile for the gettext-tools/lib subdirectory of GNU gettext -## Copyright (C) 1995-1998, 2000-2003 Free Software Foundation, Inc. +## Copyright (C) 1995-1998, 2000-2004 Free Software Foundation, Inc. ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -64,6 +64,7 @@ libgettextlib_la_SOURCES = \ pathname.h concatpath.c \ pipe.h pipe.c w32spawn.h \ progname.h progname.c progreloc.c \ + quotearg.h quotearg.c \ safe-read.h safe-read.c \ safe-write.h safe-write.c \ sh-quote.h sh-quote.c \ diff --git a/gettext-tools/lib/Makefile.msvc b/gettext-tools/lib/Makefile.msvc index 88387f777..17af0dd37 100644 --- a/gettext-tools/lib/Makefile.msvc +++ b/gettext-tools/lib/Makefile.msvc @@ -109,6 +109,7 @@ OBJECTS = \ concatpath.obj \ pipe.obj \ progname.obj progreloc.obj \ + quotearg.obj \ safe-read.obj \ safe-write.obj \ sh-quote.obj \ @@ -231,6 +232,9 @@ progname.obj : progname.c progreloc.obj : progreloc.c $(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c progreloc.c +quotearg.obj : quotearg.c + $(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c quotearg.c + safe-read.obj : safe-read.c $(CC) $(INCLUDES) $(CFLAGS) $(PICFLAGS) -c safe-read.c diff --git a/gettext-tools/lib/Makefile.vms b/gettext-tools/lib/Makefile.vms index 4390f6915..04b789122 100644 --- a/gettext-tools/lib/Makefile.vms +++ b/gettext-tools/lib/Makefile.vms @@ -67,6 +67,7 @@ OBJECTS = \ concatpath.obj, \ pipe.obj, \ progname.obj, progreloc.obj, \ + quotearg.obj, \ safe-read.obj, \ safe-write.obj, \ sh-quote.obj, \ @@ -192,6 +193,9 @@ progname.obj : progname.c progreloc.obj : progreloc.c $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) progreloc.c +quotearg.obj : quotearg.c + $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) quotearg.c + safe-read.obj : safe-read.c $(CC) $(INCLUDES) $(CFLAGS) /define=($(DEFS)) safe-read.c diff --git a/gettext-tools/lib/sh-quote.c b/gettext-tools/lib/sh-quote.c index b723c30bf..a549c46ef 100644 --- a/gettext-tools/lib/sh-quote.c +++ b/gettext-tools/lib/sh-quote.c @@ -1,5 +1,5 @@ /* Shell quoting. - Copyright (C) 2001-2003 Free Software Foundation, Inc. + Copyright (C) 2001-2004 Free Software Foundation, Inc. Written by Bruno Haible , 2001. This program is free software; you can redistribute it and/or modify @@ -25,99 +25,48 @@ #include -#include "strpbrk.h" +#include "quotearg.h" #include "xalloc.h" +/* Describes quoting for sh compatible shells. */ +static struct quoting_options *sh_quoting_options; -/* Must quote the program name and arguments since Unix shells interpret - characters like " ", "'", "<", ">", "$" etc. in a special way. This - kind of quoting should work unless the string contains "\n" and we call - csh. But we are lucky: only /bin/sh will be used. */ - -#define SHELL_SPECIAL_CHARS "\t\n !\"#$&'()*;<=>?[\\]`{|}~" +/* Initializes the sh_quoting_options variable. */ +static void +init_sh_quoting_options () +{ + sh_quoting_options = clone_quoting_options (NULL); + set_quoting_style (sh_quoting_options, shell_quoting_style); +} /* Returns the number of bytes needed for the quoted string. */ size_t shell_quote_length (const char *string) { - if (string[0] == '\0') - return 2; - else if (strpbrk (string, SHELL_SPECIAL_CHARS) == NULL) - return strlen (string); - else - { - char qchar = '\0'; /* last quote character: none or ' or " */ - size_t length = 0; - - for (; *string != '\0'; string++) - { - char c = *string; - char q = (c == '\'' ? '"' : '\''); - - if (qchar != q) - { - if (qchar) - length++; - qchar = q; - length++; - } - length++; - } - if (qchar) - length++; - - return length; - } + if (sh_quoting_options == NULL) + init_sh_quoting_options (); + return quotearg_buffer (NULL, 0, string, strlen (string), + sh_quoting_options); } -/* Copies the quoted string to p and returns the incremented p. */ +/* Copies the quoted string to p and returns the incremented p. + There must be room for shell_quote_length (string) + 1 bytes at p. */ char * shell_quote_copy (char *p, const char *string) { - if (string[0] == '\0') - { - memcpy (p, "''", 2); - return p + 2; - } - else if (strpbrk (string, SHELL_SPECIAL_CHARS) == NULL) - { - memcpy (p, string, strlen (string)); - return p + strlen (string); - } - else - { - char qchar = '\0'; /* last quote character: none or ' or " */ - - for (; *string != '\0'; string++) - { - char c = *string; - char q = (c == '\'' ? '"' : '\''); - - if (qchar != q) - { - if (qchar) - *p++ = qchar; - qchar = q; - *p++ = qchar; - } - *p++ = c; - } - if (qchar) - *p++ = qchar; - - return p; - } + if (sh_quoting_options == NULL) + init_sh_quoting_options (); + return p + quotearg_buffer (p, (size_t)(-1), string, strlen (string), + sh_quoting_options); } /* Returns the freshly allocated quoted string. */ char * shell_quote (const char *string) { - size_t length = shell_quote_length (string); - char *quoted = (char *) xmalloc (length + 1); - char *p = shell_quote_copy (quoted, string); - *p = '\0'; - return quoted; + if (sh_quoting_options == NULL) + init_sh_quoting_options (); + return quotearg_alloc (string, strlen (string), sh_quoting_options); } /* Returns a freshly allocated string containing all argument strings, quoted, diff --git a/gettext-tools/lib/sh-quote.h b/gettext-tools/lib/sh-quote.h index 76a583103..06895e619 100644 --- a/gettext-tools/lib/sh-quote.h +++ b/gettext-tools/lib/sh-quote.h @@ -1,5 +1,5 @@ /* Shell quoting. - Copyright (C) 2001-2002 Free Software Foundation, Inc. + Copyright (C) 2001-2002, 2004 Free Software Foundation, Inc. Written by Bruno Haible , 2001. This program is free software; you can redistribute it and/or modify @@ -25,7 +25,8 @@ /* Returns the number of bytes needed for the quoted string. */ extern size_t shell_quote_length (const char *string); -/* Copies the quoted string to p and returns the incremented p. */ +/* Copies the quoted string to p and returns the incremented p. + There must be room for shell_quote_length (string) + 1 bytes at p. */ extern char * shell_quote_copy (char *p, const char *string); /* Returns the freshly allocated quoted string. */ diff --git a/gettext-tools/lib/xalloc.h b/gettext-tools/lib/xalloc.h index 0e7ea8487..5a00f364a 100644 --- a/gettext-tools/lib/xalloc.h +++ b/gettext-tools/lib/xalloc.h @@ -1,5 +1,5 @@ /* malloc with out of memory checking. - Copyright (C) 2001-2003 Free Software Foundation, Inc. + Copyright (C) 2001-2004 Free Software Foundation, Inc. Written by Bruno Haible , 2001. This program is free software; you can redistribute it and/or modify @@ -56,6 +56,22 @@ extern void xalloc_die (void) extern char *xstrdup (const char *string); +/* Return 1 if an array of N objects, each of size S, cannot exist due + to size arithmetic overflow. S must be positive and N must be + nonnegative. This is a macro, not an inline function, so that it + works correctly even when SIZE_MAX < N. + + By gnulib convention, SIZE_MAX represents overflow in size + calculations, so the conservative dividend to use here is + SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. + However, malloc (SIZE_MAX) fails on all known hosts where + sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for + exactly-SIZE_MAX allocations on such hosts; this avoids a test and + branch when S is known to be 1. */ +# define xalloc_oversized(n, s) \ + ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) + + #ifdef __cplusplus } #endif