[MDEV-11175] \H option does not replace localhost with a host name Created: 2016-10-30  Updated: 2016-11-02  Resolved: 2016-11-02

Status: Closed
Project: MariaDB Server
Component/s: Scripts & Clients
Affects Version/s: 10.2
Fix Version/s: 10.2.3

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Sergey Vojtovich
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-505 feature request: add \H option for my... Closed

 Description   

In MDEV-505, a prompt option \H was added. As I understand from the description, the main difference with \h is supposed to be the behavior for localhost: with \h it should remain localhost, and with \H it should become the real host name.

I don't see it happen:

MariaDB [(none)]> prompt \h > 
PROMPT set to '\h > '
localhost > prompt \H > 
PROMPT set to '\H > '
localhost > exit
Bye
$ hostname
jessie

And from the code I don't see how it could ever work:

        if (strstr(prompt, "Localhost"))
        {
          if (*c == 'h')
            processed_prompt.append("localhost");
          else
          {
            static char hostname[FN_REFLEN];
            if (hostname[0])
              processed_prompt.append(hostname);
            else if (gethostname(hostname, sizeof(hostname)) == 0)
              processed_prompt.append(hostname);
            else
              processed_prompt.append("gethostname(2) failed");
          }
        }
        else
        {
          const char *end=strcend(prompt,' ');
          processed_prompt.append(prompt, (uint) (end-prompt));
        }

Note the capital L in the first line.

In the original blog post it's both:

 prompt= connected ? mysql_get_host_info(&mysql) : "not_connected";
-    if (strstr(prompt, "Localhost"))
-      processed_prompt.append("localhost");
+
+    if (strstr(prompt, "Localhost") || strstr(prompt, "localhost"))
+          if ( *c == 'h' ) {

I don't know what Localhost part takes care of, but it's been there before, I suppose it's for a reason. Anyway, changing it back seems to make it work:

diff --git a/client/mysql.cc b/client/mysql.cc
index 65b7c19..e1a6011 100644
--- a/client/mysql.cc
+++ b/client/mysql.cc
@@ -5129,7 +5129,7 @@ static const char *construct_prompt()
       {
         const char *prompt;
         prompt= connected ? mysql_get_host_info(&mysql) : "not_connected";
-        if (strstr(prompt, "Localhost"))
+        if (strstr(prompt, "Localhost") || strstr(prompt, "localhost"))
         {
           if (*c == 'h')
             processed_prompt.append("localhost");

MariaDB [(none)]> prompt \H > 
PROMPT set to '\H > '
jessie > exit



 Comments   
Comment by Sergey Vojtovich [ 2016-11-02 ]

See also my comment at PR#163:
https://github.com/MariaDB/server/pull/163/files

"Localhost" stands for UNIX socket, while "localhost" stands for TCP/IP.

I added check for "localhost", but still it won't work if e.g. "127.0.0.1" was specified (or any other localhost alias).

Generated at Thu Feb 08 07:47:53 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.