Re: quotearg.c's shell_quoting_style and MinGW

Bruno Haible <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs,gmane.comp.gnu.utils.bugs
Message-ID <3655972.P3fFl5LxXA@linuix>
Eli Zaretskii wrote:
> use a different set of styles: a "shell command-line argument" quoting
> style and a "file-name" quoting style.  The former would be used for
> producing shell commands, and will use platform-dependent quoting,
> similar to the patch I sent.  The latter would be used for quoting
> file names which are not meant to be passed to a shell

Yes, this idea of thinking is good: Different purposes need different
ways of quoting.

But instead of putting it all into the 'quotearg' module - whose primary
purpose has historically been error messages and file names -, I would
assiociate the "file-name" quoting style with the 'quotearg' module and
the "command-line argument" style with the following new API, closely
modeled on sh-quote.h.

=============================== system-quote.h ===============================
/* Quoting for a system command.
   Copyright (C) 2001-2012 Free Software Foundation, Inc.
   Written by Bruno Haible <[email protected]>, 2012.

   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
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef _SYSTEM_QUOTE_H
#define _SYSTEM_QUOTE_H

/* When passing a command the system's command interpreter, we must quote the
   program name and arguments, since
     - Unix shells interpret characters like " ", "'", "<", ">", "$" etc. in a
       special way,
     - Windows CreateProcess() interprets characters like ' ', '\t', '\\', '"'
       etc. (but not '<' and '>') in a special way,
     - Windows cmd.exe also interprets characters like '<', '>', '&', '%', etc.
       in a special way.  */

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Identifier for the kind of interpreter of the command.  */
enum system_command_interpreter
{
  /* The interpreter used by the system() and popen() functions.
     This is equivalent to SCI_POSIX_SH on Unix platforms and
     SCI_WINDOWS_CMD on native Windows platforms.  */
  SCI_SYSTEM
  /* The POSIX /bin/sh.  */
  , SCI_POSIX_SH
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  /* The native Windows CreateProcess() function.  */
  , SCI_WINDOWS_CREATEPROCESS
  /* The native Windows cmd.exe interpreter.  */
  , SCI_WINDOWS_CMD
#endif
};

/* Returns the number of bytes needed for the quoted string.  */
extern size_t
       system_quote_length (enum system_command_interpreter interpreter,
                            const char *string);

/* 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 *
       system_quote_copy (enum system_command_interpreter interpreter,
                          char *p, const char *string);

/* Returns the freshly allocated quoted string.  */
extern char *
       system_quote (enum system_command_interpreter interpreter,
                     const char *string);

/* Returns a freshly allocated string containing all argument strings, quoted,
   separated through spaces.  */
extern char *
       system_quote_argv (enum system_command_interpreter interpreter,
                          const char * const *argv);

#ifdef __cplusplus
}
#endif

#endif /* _SYSTEM_QUOTE_H */
==============================================================================

The system_quote_argv function would be what I called create_system_command
earlier, with the added 'interpreter' argument.

For GNU diffutils, this means just replacing specific uses of
sh_quote with system_quote(SCI_SYSTEM, ...), right before the calls to
popen() and system().

How does that sound?

Bruno
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.