psql code cleanup:

- refactor a bunch of code to call a separate function print_msg() which
checks whether "silent mode" is enabled before printing an error
message.

- rename "silence_mode" to "silent_mode", which IMHO makes more sense

- make the error messages we emit in "waiting" mode more consistent; I
believe this fixes a recent error message regression
This commit is contained in:
Neil Conway 2004-10-15 01:36:12 +00:00
parent aa0d4726c3
commit 8ff374ce91
1 changed files with 42 additions and 63 deletions

View File

@ -4,7 +4,7 @@
* *
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.35 2004/10/13 10:35:05 momjian Exp $ * $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.36 2004/10/15 01:36:12 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -66,7 +66,7 @@ typedef enum
static bool do_wait = false; static bool do_wait = false;
static bool wait_set = false; static bool wait_set = false;
static int wait_seconds = 60; static int wait_seconds = 60;
static bool silence_echo = false; static bool silent_mode = false;
static ShutdownMode shutdown_mode = SMART_MODE; static ShutdownMode shutdown_mode = SMART_MODE;
static int sig = SIGTERM; /* default */ static int sig = SIGTERM; /* default */
static CtlCommand ctl_command = NO_COMMAND; static CtlCommand ctl_command = NO_COMMAND;
@ -92,25 +92,26 @@ static void do_advice(void);
static void do_help(void); static void do_help(void);
static void set_mode(char *modeopt); static void set_mode(char *modeopt);
static void set_sig(char *signame); static void set_sig(char *signame);
static void do_start(); static void do_start(void);
static void do_stop(void); static void do_stop(void);
static void do_restart(void); static void do_restart(void);
static void do_reload(void); static void do_reload(void);
static void do_status(void); static void do_status(void);
static void do_kill(pgpid_t pid); static void do_kill(pgpid_t pid);
static void print_msg(const char *msg);
#if defined(WIN32) || defined(__CYGWIN__) #if defined(WIN32) || defined(__CYGWIN__)
static bool pgwin32_IsInstalled(SC_HANDLE); static bool pgwin32_IsInstalled(SC_HANDLE);
static char *pgwin32_CommandLine(bool); static char *pgwin32_CommandLine(bool);
static void pgwin32_doRegister(); static void pgwin32_doRegister(void);
static void pgwin32_doUnregister(); static void pgwin32_doUnregister(void);
static void pgwin32_SetServiceStatus(DWORD); static void pgwin32_SetServiceStatus(DWORD);
static void WINAPI pgwin32_ServiceHandler(DWORD); static void WINAPI pgwin32_ServiceHandler(DWORD);
static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *); static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *);
static void pgwin32_doRunAsService(); static void pgwin32_doRunAsService(void);
#endif #endif
static pgpid_t get_pgpid(void); static pgpid_t get_pgpid(void);
static char **readfile(char *path); static char **readfile(const char *path);
static int start_postmaster(void); static int start_postmaster(void);
static bool test_postmaster_connection(void); static bool test_postmaster_connection(void);
@ -201,7 +202,6 @@ xmalloc(size_t size)
} }
static char * static char *
xstrdup(const char *s) xstrdup(const char *s)
{ {
@ -216,7 +216,19 @@ xstrdup(const char *s)
return result; return result;
} }
/*
* Given an already-localized string, print it to stdout unless the
* user has specified that no messages should be printed.
*/
static void
print_msg(const char *msg)
{
if (!silent_mode)
{
fputs(msg, stdout);
fflush(stdout);
}
}
static pgpid_t static pgpid_t
get_pgpid(void) get_pgpid(void)
@ -247,7 +259,7 @@ get_pgpid(void)
* get the lines from a text file - return NULL if file can't be opened * get the lines from a text file - return NULL if file can't be opened
*/ */
static char ** static char **
readfile(char *path) readfile(const char *path)
{ {
FILE *infile; FILE *infile;
int maxlength = 0, int maxlength = 0,
@ -281,7 +293,6 @@ readfile(char *path)
maxlength = linelen; maxlength = linelen;
/* set up the result and the line buffer */ /* set up the result and the line buffer */
result = (char **) xmalloc((nlines + 1) * sizeof(char *)); result = (char **) xmalloc((nlines + 1) * sizeof(char *));
buffer = (char *) xmalloc(maxlength + 1); buffer = (char *) xmalloc(maxlength + 1);
@ -429,11 +440,7 @@ test_postmaster_connection(void)
} }
else else
{ {
if (!silence_echo) print_msg(".");
{
printf(".");
fflush(stdout);
}
pg_usleep(1000000); /* 1 sec */ pg_usleep(1000000); /* 1 sec */
} }
} }
@ -563,23 +570,18 @@ do_start(void)
if (do_wait) if (do_wait)
{ {
if (!silence_echo) print_msg(_("waiting for postmaster to start..."));
{
printf(_("waiting for postmaster to start..."));
fflush(stdout);
}
if (test_postmaster_connection() == false) if (test_postmaster_connection() == false)
printf(_("could not start postmaster\n")); printf(_("could not start postmaster\n"));
else if (!silence_echo) else
printf(_("done\npostmaster started\n")); print_msg(_(" done\npostmaster started\n"));
} }
else if (!silence_echo) else
printf(_("postmaster starting\n")); print_msg(_("postmaster starting\n"));
} }
static void static void
do_stop(void) do_stop(void)
{ {
@ -612,27 +614,18 @@ do_stop(void)
if (!do_wait) if (!do_wait)
{ {
if (!silence_echo) print_msg(_("postmaster shutting down\n"));
printf(_("postmaster shutting down\n"));
return; return;
} }
else else
{ {
if (!silence_echo) print_msg(_("waiting for postmaster to shut down..."));
{
printf(_("waiting for postmaster to shut down... "));
fflush(stdout);
}
for (cnt = 0; cnt < wait_seconds; cnt++) for (cnt = 0; cnt < wait_seconds; cnt++)
{ {
if ((pid = get_pgpid()) != 0) if ((pid = get_pgpid()) != 0)
{ {
if (!silence_echo) print_msg(".");
{
printf(".");
fflush(stdout);
}
pg_usleep(1000000); /* 1 sec */ pg_usleep(1000000); /* 1 sec */
} }
else else
@ -641,14 +634,12 @@ do_stop(void)
if (pid != 0) /* pid file still exists */ if (pid != 0) /* pid file still exists */
{ {
if (!silence_echo) print_msg(_(" failed\n"));
printf(_("failed\n"));
write_stderr(_("%s: postmaster does not shut down\n"), progname); write_stderr(_("%s: postmaster does not shut down\n"), progname);
exit(1); exit(1);
} }
if (!silence_echo) print_msg(_(" done\n"));
printf(_("done\n"));
printf(_("postmaster stopped\n")); printf(_("postmaster stopped\n"));
} }
@ -691,11 +682,7 @@ do_restart(void)
exit(1); exit(1);
} }
if (!silence_echo) print_msg(_("waiting for postmaster to shut down..."));
{
printf(_("waiting for postmaster to shut down..."));
fflush(stdout);
}
/* always wait for restart */ /* always wait for restart */
@ -703,11 +690,7 @@ do_restart(void)
{ {
if ((pid = get_pgpid()) != 0) if ((pid = get_pgpid()) != 0)
{ {
if (!silence_echo) print_msg(".");
{
printf(".");
fflush(stdout);
}
pg_usleep(1000000); /* 1 sec */ pg_usleep(1000000); /* 1 sec */
} }
else else
@ -716,16 +699,13 @@ do_restart(void)
if (pid != 0) /* pid file still exists */ if (pid != 0) /* pid file still exists */
{ {
if (!silence_echo) print_msg(_(" failed\n"));
printf(_(" failed\n"));
write_stderr(_("%s: postmaster does not shut down\n"), progname); write_stderr(_("%s: postmaster does not shut down\n"), progname);
exit(1); exit(1);
} }
if (!silence_echo) print_msg(_(" done\n"));
printf(_("done\n"));
printf(_("postmaster stopped\n")); printf(_("postmaster stopped\n"));
do_start(); do_start();
} }
@ -760,8 +740,7 @@ do_reload(void)
exit(1); exit(1);
} }
if (!silence_echo) print_msg(_("postmaster signaled\n"));
fprintf(stdout, _("postmaster signaled\n"));
} }
/* /*
@ -876,7 +855,7 @@ pgwin32_CommandLine(bool registration)
} }
static void static void
pgwin32_doRegister() pgwin32_doRegister(void)
{ {
SC_HANDLE hService; SC_HANDLE hService;
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
@ -908,7 +887,7 @@ pgwin32_doRegister()
} }
static void static void
pgwin32_doUnregister() pgwin32_doUnregister(void)
{ {
SC_HANDLE hService; SC_HANDLE hService;
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
@ -1060,7 +1039,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR * argv)
} }
static void static void
pgwin32_doRunAsService() pgwin32_doRunAsService(void)
{ {
SERVICE_TABLE_ENTRY st[] = {{register_servicename, pgwin32_ServiceMain}, SERVICE_TABLE_ENTRY st[] = {{register_servicename, pgwin32_ServiceMain},
{NULL, NULL}}; {NULL, NULL}};
@ -1287,7 +1266,7 @@ main(int argc, char **argv)
register_password = xstrdup(optarg); register_password = xstrdup(optarg);
break; break;
case 's': case 's':
silence_echo = true; silent_mode = true;
break; break;
case 'U': case 'U':
if (strchr(optarg, '\\')) if (strchr(optarg, '\\'))