public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Lancelot SIX <lsix@lancelotsix.com>
To: Andrew Burgess <aburgess@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 1/6] gdb: improve escaping when completing filenames
Date: Sat, 30 Mar 2024 23:48:07 +0000	[thread overview]
Message-ID: <20240330234807.y2qwpjwn2kaa6vtd@octopus> (raw)
In-Reply-To: <0543026b57b831d545d99e325d6e6c3ed9c4c968.1711712401.git.aburgess@redhat.com>

Hi Andrew,

Thanks for working on that!

I have a couple of really minor comments below.

On Fri, Mar 29, 2024 at 11:42:27AM +0000, Andrew Burgess wrote:
> --- a/gdb/completer.c
> +++ b/gdb/completer.c
> @@ -204,6 +204,153 @@ noop_completer (struct cmd_list_element *ignore,
>  {
>  }
>  
> +/* Return 1 if the character at EINDEX in STRING is quoted (there is an
> +   unclosed quoted string), or if the character at EINDEX is quoted by a
> +   backslash.  */
> +
> +static int
> +gdb_completer_file_name_char_is_quoted (char *string, int eindex)
> +{
> +  for (int i = 0; i <= eindex && string[i] != '\0'; )
> +    {
> +      char c = string[i];
> +
> +      if (c == '\\')
> +	{
> +	  /* The backslash itself is not quoted.  */
> +	  if (i >= eindex)
> +	    return 0;
> +	  ++i;
> +	  /* But the next character is.  */
> +	  if (i >= eindex)
> +	    return 1;
> +	  if (string[i] == '\0')
> +	    return 0;
> +	  ++i;
> +	  continue;
> +	}
> +      else if (strchr (rl_completer_quote_characters, c) != nullptr)
> +	{
> +	  /* This assumes that extract_string_maybe_quoted can handle a
> +	     string quoted with character C.  Currently this is true as the
> +	     only characters we put in rl_completer_quote_characters are
> +	     single and/or double quotes, both of which
> +	     extract_string_maybe_quoted can handle.  */

Maybe it is worth asserting that assumption

    gdb_assert (c == '"' || c == '\'');

> +	  const char *tmp = &string[i];
> +	  (void) extract_string_maybe_quoted (&tmp);
> +	  i = tmp - string;
> +
> +	  /* Consider any character within the string we just skipped over
> +	     as quoted, though this might not be completely correct; the
> +	     opening and closing quotes are not themselves quoted.  But so
> +	     far this doesn't seem to have caused any issues.  */
> +	  if (i >= eindex)
> +	    return 1;
> +	}
> +      else
> +	++i;
> +    }
> +
> +  return 0;
> +}
> +
> +/* Removing character escaping from FILENAME.  QUOTE_CHAR is the quote
> +   character around FILENAME or the null-character if there is no quoting
> +   around FILENAME.  */
> +
> +static char *
> +gdb_completer_file_name_dequote (char *filename, int quote_char)
> +{
> +  std::string tmp;
> +
> +  if (quote_char == '\'')
> +    {
> +      /* There is no backslash escaping within a single quoted string.  In
> +	 this case we can just return the input string.  */
> +      tmp = filename;
> +    }
> +  else if (quote_char == '"')
> +    {
> +      /* Remove escaping from a double quoted string.  */
> +      for (const char *input = filename;
> +	   *input != '\0';
> +	   ++input)
> +	{
> +	  if (input[0] == '\\'
> +	      && input[1] != '\0'
> +	      && strchr ("\"\\", input[1]) != nullptr)
> +	    ++input;
> +	  tmp += *input;
> +	}
> +    }
> +  else
> +    {

It might be overkill, but I would probably add "gdb_assert (quote_char ==
'\0')".

> +      /* Remove escaping from an unquoted string.  */
> +      for (const char *input = filename;
> +	   *input != '\0';
> +	   ++input)
> +	{
> +	  /* We allow anything to be escaped in an unquoted string.  */
> +	  if (*input == '\\')
> +	    {
> +	      ++input;
> +	      if (*input == '\0')
> +		break;
> +	    }
> +
> +	  tmp += *input;
> +	}
> +    }
> +
> +  return strdup (tmp.c_str ());
> +}
> +
> +/* Apply character escaping to the file name in TEXT.  QUOTE_PTR points to
> +   the quote character surrounding TEXT, or points to the null-character if
> +   there are no quotes around TEXT.  MATCH_TYPE will be one of the readline
> +   constants SINGLE_MATCH or MULTI_MATCH depending on if there is one or
> +   many completions.  */
> +
> +static char *
> +gdb_completer_file_name_quote (char *text, int match_type, char *quote_ptr)

The match_type argument is never used, maybe mark it with
[[maybe_unused]]?

> +{
> +  std::string str;
> +
> +  if (*quote_ptr == '\'')
> +    {
> +      /* There is no backslash escaping permitted within a single quoted
> +	 string, so in this case we can just return the input sting.  */
> +      str = text;
> +    }
> +  else if (*quote_ptr == '"')
> +    {
> +      /* Add escaping for a double quoted filename.  */
> +      for (const char *input = text;
> +	   *input != '\0';
> +	   ++input)
> +	{
> +	  if (strchr ("\"\\", *input) != nullptr)
> +	    str += '\\';
> +	  str += *input;
> +	}
> +    }
> +  else
> +    {

Similarly, I’d add a "gdb_assert (*quote_ptr == '\0')".

> +      /* Add escaping for an unquoted filename.  */
> +      for (const char *input = text;
> +	   *input != '\0';
> +	   ++input)
> +	{
> +	  if (strchr (" \t\n\\\"'", *input)
> +	      != nullptr)
> +	    str += '\\';
> +	  str += *input;
> +	}
> +    }
> +
> +  return strdup (str.c_str ());
> +}
> +

Best,
Lancelot.

  reply	other threads:[~2024-03-30 23:48 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-29 11:42 [PATCH 0/6] Further filename completion improvements Andrew Burgess
2024-03-29 11:42 ` [PATCH 1/6] gdb: improve escaping when completing filenames Andrew Burgess
2024-03-30 23:48   ` Lancelot SIX [this message]
2024-03-29 11:42 ` [PATCH 2/6] gdb: move display of completion results into completion_result class Andrew Burgess
2024-03-29 12:14   ` Eli Zaretskii
2024-03-30 23:30     ` Lancelot SIX
2024-03-31  5:49       ` Eli Zaretskii
2024-04-12 17:24         ` Andrew Burgess
2024-04-12 18:42           ` Eli Zaretskii
2024-04-12 22:20             ` Andrew Burgess
2024-04-13  6:36               ` Eli Zaretskii
2024-04-13  9:09                 ` Andrew Burgess
2024-04-13  9:46                   ` Eli Zaretskii
2024-04-12 17:31       ` Andrew Burgess
2024-03-29 11:42 ` [PATCH 3/6] gdb: simplify completion_result::print_matches Andrew Burgess
2024-03-30 23:48   ` Lancelot SIX
2024-03-29 11:42 ` [PATCH 4/6] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-03-30 23:49   ` Lancelot SIX
2024-03-31  5:55     ` Eli Zaretskii
2024-04-12 17:42       ` Andrew Burgess
2024-04-12 18:44         ` Eli Zaretskii
2024-04-12 22:29           ` Andrew Burgess
2024-04-13  6:39             ` Eli Zaretskii
2024-03-29 11:42 ` [PATCH 5/6] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-03-29 11:42 ` [PATCH 6/6] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess
2024-04-20  9:10 ` [PATCHv2 0/8] Further filename completion improvements Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 1/8] gdb/doc: document how filename arguments are formatted Andrew Burgess
2024-04-20  9:44     ` Eli Zaretskii
2024-04-27 10:01       ` Andrew Burgess
2024-04-27 10:06         ` Eli Zaretskii
2024-04-29  9:10           ` Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 2/8] gdb: split apart two different types of filename completion Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 3/8] gdb: improve escaping when completing filenames Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 4/8] gdb: move display of completion results into completion_result class Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 5/8] gdb: simplify completion_result::print_matches Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 6/8] gdb: add match formatter mechanism for 'complete' command output Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 7/8] gdb: apply escaping to filenames in 'complete' results Andrew Burgess
2024-04-20  9:10   ` [PATCHv2 8/8] gdb: improve gdb_rl_find_completion_word for quoted words Andrew Burgess

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=20240330234807.y2qwpjwn2kaa6vtd@octopus \
    --to=lsix@lancelotsix.com \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).