Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8ff374c

Browse files
author
Neil Conway
committed
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
1 parent aa0d472 commit 8ff374c

File tree

1 file changed

+42
-63
lines changed

1 file changed

+42
-63
lines changed

src/bin/pg_ctl/pg_ctl.c

+42-63
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
66
*
7-
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.35 2004/10/13 10:35:05 momjian Exp $
7+
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.36 2004/10/15 01:36:12 neilc Exp $
88
*
99
*-------------------------------------------------------------------------
1010
*/
@@ -66,7 +66,7 @@ typedef enum
6666
static bool do_wait = false;
6767
static bool wait_set = false;
6868
static int wait_seconds = 60;
69-
static bool silence_echo = false;
69+
static bool silent_mode = false;
7070
static ShutdownMode shutdown_mode = SMART_MODE;
7171
static int sig = SIGTERM; /* default */
7272
static CtlCommand ctl_command = NO_COMMAND;
@@ -92,25 +92,26 @@ static void do_advice(void);
9292
static void do_help(void);
9393
static void set_mode(char *modeopt);
9494
static void set_sig(char *signame);
95-
static void do_start();
95+
static void do_start(void);
9696
static void do_stop(void);
9797
static void do_restart(void);
9898
static void do_reload(void);
9999
static void do_status(void);
100100
static void do_kill(pgpid_t pid);
101+
static void print_msg(const char *msg);
101102

102103
#if defined(WIN32) || defined(__CYGWIN__)
103104
static bool pgwin32_IsInstalled(SC_HANDLE);
104105
static char *pgwin32_CommandLine(bool);
105-
static void pgwin32_doRegister();
106-
static void pgwin32_doUnregister();
106+
static void pgwin32_doRegister(void);
107+
static void pgwin32_doUnregister(void);
107108
static void pgwin32_SetServiceStatus(DWORD);
108109
static void WINAPI pgwin32_ServiceHandler(DWORD);
109110
static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *);
110-
static void pgwin32_doRunAsService();
111+
static void pgwin32_doRunAsService(void);
111112
#endif
112113
static pgpid_t get_pgpid(void);
113-
static char **readfile(char *path);
114+
static char **readfile(const char *path);
114115
static int start_postmaster(void);
115116
static bool test_postmaster_connection(void);
116117

@@ -201,7 +202,6 @@ xmalloc(size_t size)
201202
}
202203

203204

204-
205205
static char *
206206
xstrdup(const char *s)
207207
{
@@ -216,7 +216,19 @@ xstrdup(const char *s)
216216
return result;
217217
}
218218

219-
219+
/*
220+
* Given an already-localized string, print it to stdout unless the
221+
* user has specified that no messages should be printed.
222+
*/
223+
static void
224+
print_msg(const char *msg)
225+
{
226+
if (!silent_mode)
227+
{
228+
fputs(msg, stdout);
229+
fflush(stdout);
230+
}
231+
}
220232

221233
static pgpid_t
222234
get_pgpid(void)
@@ -247,7 +259,7 @@ get_pgpid(void)
247259
* get the lines from a text file - return NULL if file can't be opened
248260
*/
249261
static char **
250-
readfile(char *path)
262+
readfile(const char *path)
251263
{
252264
FILE *infile;
253265
int maxlength = 0,
@@ -281,7 +293,6 @@ readfile(char *path)
281293
maxlength = linelen;
282294

283295
/* set up the result and the line buffer */
284-
285296
result = (char **) xmalloc((nlines + 1) * sizeof(char *));
286297
buffer = (char *) xmalloc(maxlength + 1);
287298

@@ -429,11 +440,7 @@ test_postmaster_connection(void)
429440
}
430441
else
431442
{
432-
if (!silence_echo)
433-
{
434-
printf(".");
435-
fflush(stdout);
436-
}
443+
print_msg(".");
437444
pg_usleep(1000000); /* 1 sec */
438445
}
439446
}
@@ -563,23 +570,18 @@ do_start(void)
563570

564571
if (do_wait)
565572
{
566-
if (!silence_echo)
567-
{
568-
printf(_("waiting for postmaster to start..."));
569-
fflush(stdout);
570-
}
573+
print_msg(_("waiting for postmaster to start..."));
571574

572575
if (test_postmaster_connection() == false)
573576
printf(_("could not start postmaster\n"));
574-
else if (!silence_echo)
575-
printf(_("done\npostmaster started\n"));
577+
else
578+
print_msg(_(" done\npostmaster started\n"));
576579
}
577-
else if (!silence_echo)
578-
printf(_("postmaster starting\n"));
580+
else
581+
print_msg(_("postmaster starting\n"));
579582
}
580583

581584

582-
583585
static void
584586
do_stop(void)
585587
{
@@ -612,27 +614,18 @@ do_stop(void)
612614

613615
if (!do_wait)
614616
{
615-
if (!silence_echo)
616-
printf(_("postmaster shutting down\n"));
617+
print_msg(_("postmaster shutting down\n"));
617618
return;
618619
}
619620
else
620621
{
621-
if (!silence_echo)
622-
{
623-
printf(_("waiting for postmaster to shut down... "));
624-
fflush(stdout);
625-
}
622+
print_msg(_("waiting for postmaster to shut down..."));
626623

627624
for (cnt = 0; cnt < wait_seconds; cnt++)
628625
{
629626
if ((pid = get_pgpid()) != 0)
630627
{
631-
if (!silence_echo)
632-
{
633-
printf(".");
634-
fflush(stdout);
635-
}
628+
print_msg(".");
636629
pg_usleep(1000000); /* 1 sec */
637630
}
638631
else
@@ -641,14 +634,12 @@ do_stop(void)
641634

642635
if (pid != 0) /* pid file still exists */
643636
{
644-
if (!silence_echo)
645-
printf(_("failed\n"));
637+
print_msg(_(" failed\n"));
646638

647639
write_stderr(_("%s: postmaster does not shut down\n"), progname);
648640
exit(1);
649641
}
650-
if (!silence_echo)
651-
printf(_("done\n"));
642+
print_msg(_(" done\n"));
652643

653644
printf(_("postmaster stopped\n"));
654645
}
@@ -691,23 +682,15 @@ do_restart(void)
691682
exit(1);
692683
}
693684

694-
if (!silence_echo)
695-
{
696-
printf(_("waiting for postmaster to shut down..."));
697-
fflush(stdout);
698-
}
685+
print_msg(_("waiting for postmaster to shut down..."));
699686

700687
/* always wait for restart */
701688

702689
for (cnt = 0; cnt < wait_seconds; cnt++)
703690
{
704691
if ((pid = get_pgpid()) != 0)
705692
{
706-
if (!silence_echo)
707-
{
708-
printf(".");
709-
fflush(stdout);
710-
}
693+
print_msg(".");
711694
pg_usleep(1000000); /* 1 sec */
712695
}
713696
else
@@ -716,16 +699,13 @@ do_restart(void)
716699

717700
if (pid != 0) /* pid file still exists */
718701
{
719-
if (!silence_echo)
720-
printf(_(" failed\n"));
702+
print_msg(_(" failed\n"));
721703

722704
write_stderr(_("%s: postmaster does not shut down\n"), progname);
723705
exit(1);
724706
}
725707

726-
if (!silence_echo)
727-
printf(_("done\n"));
728-
708+
print_msg(_(" done\n"));
729709
printf(_("postmaster stopped\n"));
730710
do_start();
731711
}
@@ -760,8 +740,7 @@ do_reload(void)
760740
exit(1);
761741
}
762742

763-
if (!silence_echo)
764-
fprintf(stdout, _("postmaster signaled\n"));
743+
print_msg(_("postmaster signaled\n"));
765744
}
766745

767746
/*
@@ -876,7 +855,7 @@ pgwin32_CommandLine(bool registration)
876855
}
877856

878857
static void
879-
pgwin32_doRegister()
858+
pgwin32_doRegister(void)
880859
{
881860
SC_HANDLE hService;
882861
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
@@ -908,7 +887,7 @@ pgwin32_doRegister()
908887
}
909888

910889
static void
911-
pgwin32_doUnregister()
890+
pgwin32_doUnregister(void)
912891
{
913892
SC_HANDLE hService;
914893
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
@@ -1060,7 +1039,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR * argv)
10601039
}
10611040

10621041
static void
1063-
pgwin32_doRunAsService()
1042+
pgwin32_doRunAsService(void)
10641043
{
10651044
SERVICE_TABLE_ENTRY st[] = {{register_servicename, pgwin32_ServiceMain},
10661045
{NULL, NULL}};
@@ -1287,7 +1266,7 @@ main(int argc, char **argv)
12871266
register_password = xstrdup(optarg);
12881267
break;
12891268
case 's':
1290-
silence_echo = true;
1269+
silent_mode = true;
12911270
break;
12921271
case 'U':
12931272
if (strchr(optarg, '\\'))

0 commit comments

Comments
 (0)