diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index eef6e4b..ce1f688 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3020,7 +3020,10 @@ static bool do_watch(PQExpBuffer query_buf, long sleep) { printQueryOpt myopt = pset.popt; - char title[50]; + char *title; + char *head_title = NULL; + int title_len; + int res = 0; if (!query_buf || query_buf->len <= 0) { @@ -3034,9 +3037,25 @@ do_watch(PQExpBuffer query_buf, long sleep) */ myopt.topt.pager = 0; + /* + * Take into account any title present in the user setup as a part of + * what is printed for each iteration by using it as a header. + */ + if (myopt.title) + { + title_len = strlen(myopt.title); + title = pg_malloc(title_len + 50); + head_title = pg_strdup(myopt.title); + } + else + { + title_len = 0; + title = pg_malloc(50); + head_title = pg_strdup(""); + } + for (;;) { - int res; time_t timer; long i; @@ -3045,8 +3064,9 @@ do_watch(PQExpBuffer query_buf, long sleep) * of completion of the command? */ timer = time(NULL); - snprintf(title, sizeof(title), _("Watch every %lds\t%s"), - sleep, asctime(localtime(&timer))); + snprintf(title, title_len + 50, + _("Watch every %lds\t%s%s"), + sleep, asctime(localtime(&timer)), head_title); myopt.title = title; /* Run the query and print out the results */ @@ -3056,10 +3076,8 @@ do_watch(PQExpBuffer query_buf, long sleep) * PSQLexecWatch handles the case where we can no longer repeat the * query, and returns 0 or -1. */ - if (res == 0) + if (res == 0 || res == -1) break; - if (res == -1) - return false; /* * Set up cancellation of 'watch' via SIGINT. We redo this each time @@ -3084,7 +3102,9 @@ do_watch(PQExpBuffer query_buf, long sleep) sigint_interrupt_enabled = false; } - return true; + pg_free(title); + pg_free(head_title); + return res == 0; } /*