From 84e8e5b42798227d1105cd6dc0dda25edbee5418 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sun, 6 May 2001 17:21:11 +0000 Subject: [PATCH] Make prompt customization work with changeable Unix socket location. --- doc/src/sgml/ref/psql-ref.sgml | 23 ++++++++--- src/bin/psql/prompt.c | 70 +++++++--------------------------- 2 files changed, 31 insertions(+), 62 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 85efbf5642..9fbafcc881 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ @@ -1970,14 +1970,27 @@ testdb=> \set content `sed -e "s/'/\\\\\\'/g" < my_file.txt` %M - The full hostname (with domain name) of the database server (or - localhost if hostname information is not available). + + + The full hostname (with domain name) of the database server, + or [local] if the connection is over a + Unix domain socket, or + [local:/dir/name], + if the Unix domain socket is not at the compiled in default + location. + + %m - The hostname of the database server, truncated after the - first dot. + + + The hostname of the database server, truncated after the + first dot, or [local] if the connection + is over a Unix domain socket. + + diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 08db190267..974e97022d 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.18 2001/03/22 04:00:22 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.19 2001/05/06 17:21:11 petere Exp $ */ #include "postgres_fe.h" #include "prompt.h" @@ -32,9 +32,9 @@ * (might not be completely multibyte safe) * * Defined interpolations are: - * %M - database server "hostname.domainname" (or "localhost" if this - * information is not available) - * %m - like %M, but hostname only (before first dot) + * %M - database server "hostname.domainname", "[local]" for AF_UNIX + * sockets, "[local:/dir/name]" if not default + * %m - like %M, but hostname only (before first dot), or always "[local]" * %> - database server port number * %n - database user name * %/ - current database @@ -61,52 +61,6 @@ *-------------------------- */ -/* - * We need hostname information, only if connection is via UNIX socket - */ -#ifdef HAVE_UNIX_SOCKETS - -#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 /* HAVE_UNIX_SOCKETS */ - - char * get_prompt(promptStatus_t status) { @@ -166,23 +120,25 @@ get_prompt(promptStatus_t status) case 'm': if (pset.db) { + const char * host = PQhost(pset.db); + /* INET socket */ - if (PQhost(pset.db)) + if (host && host[0] && host[0] != '/') { - strncpy(buf, PQhost(pset.db), MAX_PROMPT_SIZE); + strncpy(buf, host, MAX_PROMPT_SIZE); if (*p == 'm') buf[strcspn(buf, ".")] = '\0'; } /* UNIX socket */ -#ifdef HAVE_UNIX_SOCKETS else { - if (*p == 'm') - localhost(HOSTNAME, buf, MAX_PROMPT_SIZE); + if (!host + || strcmp(host, DEFAULT_PGSOCKET_DIR)==0 + || *p == 'm') + strncpy(buf, "[local]", MAX_PROMPT_SIZE); else - localhost(DOMAINNAME, buf, MAX_PROMPT_SIZE); + snprintf(buf, MAX_PROMPT_SIZE, "[local:%s]", host); } -#endif /* HAVE_UNIX_SOCKETS */ } break; /* DB server port number */ -- 2.40.0