Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Joel Brobecker <brobecker@gnat.com>
To: gdb-patches@sources.redhat.com
Subject: [RFA/RFC] Problem with '!' escaping with zsh/bash/ksh
Date: Fri, 02 May 2003 23:35:00 -0000	[thread overview]
Message-ID: <20030502233458.GP992@gnat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1361 bytes --]

Hello,

the following change introduced a problem when forking inferiors when
the path to the executable contains '!' characters. This only occurs
with sh-like shells, like zsh/bash/ksh. I have reproduced this on Linux
and HP/UX.

        * fork-inferior.c (fork_inferior): Add '!' to the list of
        characters that need to be quoted when building a string for the
        shell.  Quote '!' specifically with a backslash, since CSH chokes
        when trying to evaluate "str!str". 

Witness:

        (gdb) run
        Starting program: /home/brobecke/tmp/GEO_ENV!9.159/foo 
        zsh: no such file or directory: /home/brobecke/tmp/GEO_ENV\!9.159/foo
        
        Program exited with code 01.
        You can't do that without a process to debug.

As far as I can tell from the comments in fork_inferior and my own
experiments, the bang should be escaped only for C shells. To reflect
this, I have made the following change to fork-child.c.

2002-05-02  J. Brobecker  <brobecker@gnat.com>

        * fork-child.c (escape_bang_in_quoted_argument): New function.
        (fork_inferior): Escape '!' characters in quoted arguments
        only when needed.

Tested on HP/UX, with SHELL set to ZSH. No regression. Also tested by
verifying that it does fix the problem without breaking csh. Ok to apply?
Some better suggestions, maybe?

Thanks,
-- 
Joel

[-- Attachment #2: fork-child.c.diff --]
[-- Type: text/plain, Size: 1979 bytes --]

Index: fork-child.c
===================================================================
RCS file: /nile.c/cvs/Dev/gdb/gdb-5.3/gdb/fork-child.c,v
retrieving revision 1.2
diff -c -3 -p -r1.2 fork-child.c
*** fork-child.c	16 Jan 2003 10:40:02 -0000	1.2
--- fork-child.c	2 May 2003 23:31:34 -0000
*************** breakup_args (char *scratch, char **argv
*** 93,98 ****
--- 93,121 ----
  
  }
  
+ /* When executing a command under the given shell, return non-zero
+    if the '!' character should be escaped when embedded in a quoted
+    command-line argument.  */
+ 
+ static int
+ escape_bang_in_quoted_argument (const char *shell_file)
+ {
+   const int shell_file_len = strlen (shell_file);
+   
+   /* Bang should be escaped only in C Shells.  For now, simply check
+      that the shell name ends with 'csh', which covers at least csh
+      and tcsh.  This should be good enough for now.  */
+ 
+   if (shell_file_len < 3)
+     return 0;
+ 
+   if (shell_file[shell_file_len - 3] == 'c'
+       && shell_file[shell_file_len - 2] == 's'
+       && shell_file[shell_file_len - 1] == 'h')
+     return 1;
+ 
+   return 0;
+ }
  
  /* Start an inferior Unix child process and sets inferior_ptid to its pid.
     EXEC_FILE is the file to run.
*************** fork_inferior (char *exec_file_arg, char
*** 176,181 ****
--- 199,205 ----
  
        char *p;
        int need_to_quote;
+       const int escape_bang = escape_bang_in_quoted_argument (shell_file);
  
        strcat (shell_command, "exec ");
  
*************** fork_inferior (char *exec_file_arg, char
*** 220,226 ****
  	    {
  	      if (*p == '\'')
  		strcat (shell_command, "'\\''");
! 	      else if (*p == '!')
  		strcat (shell_command, "\\!");
  	      else
  		strncat (shell_command, p, 1);
--- 244,250 ----
  	    {
  	      if (*p == '\'')
  		strcat (shell_command, "'\\''");
! 	      else if (*p == '!' && escape_bang)
  		strcat (shell_command, "\\!");
  	      else
  		strncat (shell_command, p, 1);

             reply	other threads:[~2003-05-02 23:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-02 23:35 Joel Brobecker [this message]
2003-05-03  8:39 ` Eli Zaretskii
2003-05-03 14:34   ` Daniel Jacobowitz
2003-05-03 16:51     ` Joel Brobecker
2003-05-03 16:59       ` Daniel Jacobowitz
2003-05-05 19:29         ` Andrew Cagney
2003-05-08 18:11           ` Joel Brobecker
2003-05-21 23:40           ` Joel Brobecker
2003-05-22 17:50             ` Andrew Cagney
2003-05-24  7:40             ` Eli Zaretskii
2003-05-03 19:00       ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030502233458.GP992@gnat.com \
    --to=brobecker@gnat.com \
    --cc=gdb-patches@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox