Re: psql prompt - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: psql prompt
Date
Msg-id 200006091612.MAA02892@candle.pha.pa.us
Whole thread Raw
Responses Re: psql prompt  (Karel Zak <zakkr@zf.jcu.cz>)
List pgsql-patches
Not sure what to do with this patch.  It appears most of it was already
applied.


>
> On Thu, 9 Mar 2000, Bruce Momjian wrote:
>
> > Karel, can you submit a new patch to clean up these new items, rather
> > than just an entire new patch.
>
>  Here is a new patch (this patch remove my old code from psql.)
> Bruce, will better if Peter check this patch first. He is really
> pedantic :-) But I merit punishment...
>
> > > 1) updates the documentation and the comment on top of the function
>
> Yes.
>
> > >
> > > 2) keeps the correct semantics of %m vs %M
>
>  Yes. My code in this patch is corrent for %m (hello) %M (hello.domain.xx).
> But your code is not correct. See TCP/IP connection:
>
> $ ./psql abil -h bootes -U zakkr
>
> abil=# \set PROMPT1 %M:%/%#%32                <-- without a domainname
> bootes:abil# \set PROMPT1 %m:%/%#%32
> bootes:abil#
>
> You use for prompt PQhost(), but in PGconn struct is (probably) save
> hostname from psql's argv[]. But it is not always real and full hostname
> (with domainname) - it can be a name from /etc/hosts. A solution is use
> gethostbyaddr( conn->raddr ) instead simple PQhost().
>
> But it is really cosmetic option. We have propably more interesting a work
> (pg_acl ?) :-)
>
> > > 3) is portable to Windows (you can ifdef it out, since there are no domain
> > > sockets on Windows)
>
> Sorry. It correct now.
>
> > > 4) Uses gethostname. That's more portable.
>
> I used gethostname() and gethostbyname() (for full domain name (%M)).
>
> Well, Peter is all in this patch correct?
>
>                         Karel
Content-Description:

[ application/x-gzip is not supported, skipping... ]


--
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
diff -r -B -C 2 pgsql.ORIG/doc/src/sgml/ref/psql-ref.sgml pgsql.NEW/doc/src/sgml/ref/psql-ref.sgml
*** pgsql.ORIG/doc/src/sgml/ref/psql-ref.sgml    Wed Mar  1 22:09:56 2000
--- pgsql.NEW/doc/src/sgml/ref/psql-ref.sgml    Fri Mar 10 15:55:12 2000
***************
*** 1967,1972 ****
        <varlistentry>
          <term><literal>%M</literal></term>
!     <listitem><para>The hostname of the database server (or <quote>.</quote>
!          if Unix domain socket).</para></listitem>
        </varlistentry>

--- 1967,1972 ----
        <varlistentry>
          <term><literal>%M</literal></term>
!     <listitem><para>The full hostname (with domainname) of the database server (or
!          <quote>localhost</quote> if hostname information is not available).</para></listitem>
        </varlistentry>

diff -r -B -C 2 pgsql.ORIG/src/bin/psql/prompt.c pgsql.NEW/src/bin/psql/prompt.c
*** pgsql.ORIG/src/bin/psql/prompt.c    Fri Mar 10 15:44:43 2000
--- pgsql.NEW/src/bin/psql/prompt.c    Fri Mar 10 15:40:02 2000
***************
*** 4,9 ****
   * Copyright 2000 by PostgreSQL Global Development Group
   *
!  * $Header: /usr/local/cvsroot/pgsql/src/bin/psql/prompt.c,v 1.10 2000/03/08 01:38:59 momjian Exp $
!  */
  #include "postgres.h"
  #include "prompt.h"
--- 4,9 ----
   * Copyright 2000 by PostgreSQL Global Development Group
   *
!  * $Header: /usr/local/cvsroot/pgsql/src/bin/psql/prompt.c,v 1.9 2000/02/16 13:15:26 momjian Exp $
!  */
  #include "postgres.h"
  #include "prompt.h"
***************
*** 20,24 ****
  #endif

! #include <sys/utsname.h>

  /*--------------------------
--- 20,27 ----
  #endif

! #if !defined(__CYGWIN32__) && !defined(__QNX__)
! #include <unistd.h>
! #include <netdb.h>
! #endif

  /*--------------------------
***************
*** 30,35 ****
   *
   * Defined interpolations are:
!  * %M - database server hostname (or "." if not TCP/IP)
!  * %m - like %M but hostname truncated after first dot
   * %> - database server port number (or "." if not TCP/IP)
   * %n - database user name
--- 33,39 ----
   *
   * Defined interpolations are:
!  * %M - database server "hostname.domainname" (or "localhost" if this
!  *    information is not available)
!  * %m - like %M, but hostname only (before firts dot)
   * %> - database server port number (or "." if not TCP/IP)
   * %n - database user name
***************
*** 57,60 ****
--- 61,106 ----
   *--------------------------
   */
+
+ /*
+  * We need hostname information, only if connection is via UNIX socket
+  */
+ #if !defined(__CYGWIN32__) && !defined(__QNX__)
+
+ #define DOMAINNAME    1
+ #define HOSTNAME    2
+
+ /*
+  * Return full hostname for localhost.
+  *    - informations are init only in firts time - not queries DNS or NIS
+  *      for every localhost() call
+  */
+ static char *
+ localhost(int type, char *buf, int siz)
+ {
+     static struct hostent    *hp = NULL;
+     static int err = 0;
+
+     if (hp==NULL && err==0) {
+         char hname[256];
+
+         if (gethostname(hname, 256) == 0)
+             if (!(hp = gethostbyname(hname)))
+                 err = 1;
+         else
+             err = 1;
+     }
+
+     if (hp==NULL)
+         return strncpy(buf, "localhost", siz);
+
+     strncpy(buf, hp->h_name, siz);        /* full aaa.bbb.ccc */
+
+     if (type==HOSTNAME)
+         buf[strcspn(buf, ".")] = '\0';
+
+     return buf;
+ }
+ #endif
+
  char *
  get_prompt(promptStatus_t status)
***************
*** 116,119 ****
--- 162,166 ----
                      if (pset.db)
                      {
+                         /* INET socket */
                          if (PQhost(pset.db))
                          {
***************
*** 122,136 ****
                                  buf[strcspn(buf, ".")] = '\0';
                          }
!                         else if (*p == 'M')
!                             buf[0] = '.';
!                         else
!                         {
!                             struct utsname ubuf;
!
!                             if (uname(&ubuf) < 0)
!                                 buf[0] = '.';
!                             else
!                                 strncpy(buf, ubuf.nodename, MAX_PROMPT_SIZE);
!                         }
                      }
                      break;
--- 169,181 ----
                                  buf[strcspn(buf, ".")] = '\0';
                          }
!                         /* UNIX socket */
! #if !defined(__CYGWIN32__) && !defined(__QNX__)
!                         else {
!                             if (*p == 'm')
!                                 localhost(HOSTNAME, buf, MAX_PROMPT_SIZE);
!                             else
!                                 localhost(DOMAINNAME, buf, MAX_PROMPT_SIZE);
!                         }
! #endif
                      }
                      break;

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: NO-CREATE-TABLE and NO-LOCK-TABLE
Next
From: Karel Zak
Date:
Subject: Re: psql prompt