From patchwork Fri Mar 29 11:42:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 87814 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6736E3858C5E for ; Fri, 29 Mar 2024 11:43:51 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id B5E033858D34 for ; Fri, 29 Mar 2024 11:42:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B5E033858D34 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B5E033858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711712563; cv=none; b=x7lVptIevHQZOQprVvIk2YCSLqKmSUMzY4P/K/qVDX8UdcniiwWfDWVFhFMN9Xk//86ewAxuER2pCwthjW2OucrUur88x2ZUBtxDIlKrM6UAmC+VjYcXVWuFTdzbdHdBhn+FCradosVTFwP3CkbXdRs9A3ZZRwLTzcdA6pQPh4U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711712563; c=relaxed/simple; bh=JwaU5YMevt74UPj+f2vmxFi/ArEvEpd0oO9s3myO1Dk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ltx/WS9aY2FZwcxvjcX0bI0NvU7PF/HIYtJurUK2qrU8gouiNRUGYizKY3ewzLW8eNGSAnBuUrfUlv8VQO08yHDyb2YJfV//uKTxq0mthi5PEbb4konzUo0TQ3tY988VQUpbcX2vid7zmssyNQ+ER8+zif8/gQV43HBfizmCQnk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711712561; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0HE1hYNohM+n9QQ2EZmVfMv59/0dMXCrUDGCE1IygHU=; b=BMwO29oS5SkV71XHUdnH7Chk0+7qMQw67KE4YMul/XQKNyISnpACIrIsd5vTmxBTYujU4e iCrigcZ7HJycT/NVJTKhgJ20N8AY8IiClihzTG5g6trWaiHw4apwJ9p74jjhB/OKDbrX8G 5zC4jlI+AIQ6TtNtyogtIMD7YdzcU4k= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-454-wSvqatHnM-mZdtD7EUX5qw-1; Fri, 29 Mar 2024 07:42:40 -0400 X-MC-Unique: wSvqatHnM-mZdtD7EUX5qw-1 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-515c5796481so1306442e87.0 for ; Fri, 29 Mar 2024 04:42:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711712557; x=1712317357; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0HE1hYNohM+n9QQ2EZmVfMv59/0dMXCrUDGCE1IygHU=; b=B369SeCBi0CbAijX6+iJ4mwqN0zELUcqQFJ0Xwm9wKVVlIUuyvEwI3N4zC55DPXcnZ Ypxujd1sG0IUGVNWsZWqHj3HSEhTN5lErtY76o9SnVergbFblOU437qv8Fr1dqVYPO8o Dmn26optqRlN36s5YvneGtyKIGmEQFgLqjaEEKN68MYBVMyUmyAYa+mkprwqluCNwX8M e5y15bLanNmne85FlGpBOm47cvpcW/ppSyrsGKesa5KvsDAym+eY/X7jSWkAoX5W6TA2 a8tkAVGiOepgYAYZcw2lSaQTkLOPcS+oclHRUAq4QvPPG2UOfcKNZjQRLM4pu98R/lCA TIgw== X-Gm-Message-State: AOJu0YwO/AEnYVMaCnjoe17qpRe/KYaGuLC3bME0UsKhjs4Cb7WBz1II L8tt2Pk34okjwAH8v+fbf3/H9Gkoyhz/HA5SYhkq5HUoFoMPGHEeZu3kS9VSH4ndMmpwALvXtvo G9mVZEHXQlQdP867ZZ+J2XWXsxuQZGIbOsU4s/ubnJEls19yWY03FpnMqFyltE1+vGdnGKNgiap HpYa1h4wTUkfu31Z6Zkx8gu7FvclN+8vxcF9R7vDNBbC0= X-Received: by 2002:ac2:4181:0:b0:515:b630:c3b9 with SMTP id z1-20020ac24181000000b00515b630c3b9mr1532736lfh.16.1711712557299; Fri, 29 Mar 2024 04:42:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHn3Xt2BdCPXITxwj2MQGv5UMrC0Tx4SKOL92tPad6tQIhRhYz0CRsPwbstwVv4Xi5AlUzuVg== X-Received: by 2002:ac2:4181:0:b0:515:b630:c3b9 with SMTP id z1-20020ac24181000000b00515b630c3b9mr1532718lfh.16.1711712556581; Fri, 29 Mar 2024 04:42:36 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id g16-20020a17090613d000b00a45ff5a30cesm1842036ejc.183.2024.03.29.04.42.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 04:42:35 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/6] gdb: move display of completion results into completion_result class Date: Fri, 29 Mar 2024 11:42:28 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org When using the 'complete' command to complete file names we have some problems. The suggested completion should include any required escaping, so if there is a filename '/tmp/aa"bb' (without the single quotes), then this should be displayed in the completion output like: (gdb) complete file /tmp/aa file /tmp/aa\"bb This doesn't currently happen. We already have some code in filename_completer (completer.c) that handles adding the trailing '/' character if the completion result is a directory, so we might be tempted to add any needed escaping in that function, however if we do then we run into a problem. If there are multiple completion results from a 'complete' command, then in complete_command (cli/cli-cmds.c) we sort the completion results prior to printing them. If those results have already had the escaping added then the sort will be done on the text including the escape characters. This means that results from the 'complete' command will appear in a different order than readline would present them; readline sorts the results and then adds and required escaping. I think that the 'complete' command should behave the same as readline, sort the entries and then add the escaping. This means that we need to sort before adding the escaping. There is a second problem when using the 'complete' command with file names, that is trailing quote characters. The addition of a trailing quote character is a bit complex due (I think) to the structure of readline itself. Adding the quote character currently occurs in two places in GDB. In completion_tracker::build_completion_result (completer.c) we add the trailing quote in the case where we only have a single result, and in complete_command (cli/cli-cmds.c) we add the trailing quote character if we have more than one result. With these two cases we ensure that the 'complete' command always adds a trailing quote (when necessary) to the results it displays. However, I think that for filename completion we don't always want a trailing quote. Consider if we have a file '/tmp/xxx/foo.c' (without the quotes), and then we do: (gdb) complete file /tmp/xx What should the result of this be? Well, if we use TAB completion like this: (gdb) file /tmp/xx (gdb) file /tmp/xxx/ then readline completes the directory 'xxx/', but doesn't try to complete the filename within the xxx/ directory until we press again. So I think that the results of the 'complete' command should share this behaviour: (gdb) complete file /tmp/xx file /tmp/xxx/ And this is what we get right now, but what if the user adds some opening quotes? Right now we get this: (gdb) complete file "/tmp/xx file "/tmp/xxx/ Which I think is correct, there's no trailing quote added. This is because in completion_tracker::build_completion_result the completion_tracker object doesn't know that the double quote is the quote_char(). However, this causes a problem, if we do this: (gdb) complete file "/tmp/xxx/f file "/tmp/xxx/foo.c We're still missing the trailing quote, even though in this case, when we've expanded a complete filename, adding the trailing quote would be the correct thing to do. The cause is, of course, the same, the completion_tracker is unaware of what the quote character is, and so doesn't add the quote when needed. However, the problem gets worse, if we create an additional file '/tmp/xxa/bar.c', notice that this is in a different directory. So now when we do this: (gdb) complete file "/tmp/xx file "/tmp/xxa/" file "/tmp/xxx/" Now the trailing quote has been added even though we haven't completed a full filename. This is because in complete_command (cli/cli-cmds.c) we do know what the quote character is, and the trailing quote is always added. There are multiple problems here, but at least part of the problem will be solved by making the result printing in complete_command smarter. My plan is to allow different completion functions to modify how the 'complete' results are printed. This commit doesn't solve any of the problems listed above. Instead this commit is just a refactor. I've moved the result printing logic out of complete_command and created a new function completion_result::print_matches. And that's it. Nothing has functionally changed yet, that will come in later commits when the print_matches function is made smarter. There should be no user visible changes after this commit. --- gdb/cli/cli-cmds.c | 26 +------------------------- gdb/completer.c | 33 +++++++++++++++++++++++++++++++++ gdb/completer.h | 13 +++++++++++++ 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index df11f956245..0b621f65917 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -424,31 +424,7 @@ complete_command (const char *arg, int from_tty) { std::string arg_prefix (arg, word - arg); - if (result.number_matches == 1) - printf_unfiltered ("%s%s\n", arg_prefix.c_str (), result.match_list[0]); - else - { - result.sort_match_list (); - - for (size_t i = 0; i < result.number_matches; i++) - { - printf_unfiltered ("%s%s", - arg_prefix.c_str (), - result.match_list[i + 1]); - if (quote_char) - printf_unfiltered ("%c", quote_char); - printf_unfiltered ("\n"); - } - } - - if (result.number_matches == max_completions) - { - /* ARG_PREFIX and WORD are included in the output so that emacs - will include the message in the output. */ - printf_unfiltered (_("%s%s %s\n"), - arg_prefix.c_str (), word, - get_max_completions_reached_message ()); - } + result.print_matches (arg_prefix, word, quote_char); } } diff --git a/gdb/completer.c b/gdb/completer.c index 4cda5f3a383..9b4041da01a 100644 --- a/gdb/completer.c +++ b/gdb/completer.c @@ -2443,6 +2443,39 @@ completion_result::reset_match_list () } } +/* See completer.h */ + +void +completion_result::print_matches (const std::string &prefix, + const char *word, int quote_char) +{ + if (this->number_matches == 1) + printf_unfiltered ("%s%s\n", prefix.c_str (), this->match_list[0]); + else + { + this->sort_match_list (); + + for (size_t i = 0; i < this->number_matches; i++) + { + printf_unfiltered ("%s%s", prefix.c_str (), + this->match_list[i + 1]); + if (quote_char) + printf_unfiltered ("%c", quote_char); + printf_unfiltered ("\n"); + } + } + + if (this->number_matches == max_completions) + { + /* PREFIX and WORD are included in the output so that emacs will + include the message in the output. */ + printf_unfiltered (_("%s%s %s\n"), + prefix.c_str (), word, + get_max_completions_reached_message ()); + } + +} + /* Helper for gdb_rl_attempted_completion_function, which does most of the work. This is called by readline to build the match list array and to determine the lowest common denominator. The real matches diff --git a/gdb/completer.h b/gdb/completer.h index 98a12f3907c..4419c8f6d30 100644 --- a/gdb/completer.h +++ b/gdb/completer.h @@ -268,6 +268,19 @@ struct completion_result /* Sort the match list. */ void sort_match_list (); + /* Called to display all matches (used by the 'complete' command). + PREFIX is everything before the completion word. WORD is the word + being completed, this is only used if we reach the maximum number of + completions, otherwise, each line of output consists of PREFIX + followed by one of the possible completion words. + + The QUOTE_CHAR is appended after each possible completion word and + should be the quote character that appears before the completion word, + or the null-character if there is no quote before the completion + word. */ + void print_matches (const std::string &prefix, const char *word, + int quote_char); + private: /* Destroy the match list array and its contents. */ void reset_match_list ();