Commit be1c6e75 authored by Bruce Momjian's avatar Bruce Momjian

Here's some changes I made last night to psql's common.c (as found in

7.3.2).  It removes some code duplication and #ifdeffing, and some
unstructured ugliness such as tacky breaks and an unneeded continue.
Breaks up a large function into smaller functions and reduces required
nesting levels, and kills a variable or two.

Jeroen T. Vermeulen
parent 94701fb2
This diff is collapsed.
......@@ -3,7 +3,7 @@
*
* Copyright 2000 by PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.22 2003/03/18 22:15:44 petere Exp $
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.23 2003/03/20 06:00:12 momjian Exp $
*/
#ifndef COMMON_H
#define COMMON_H
......@@ -29,6 +29,8 @@ extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
extern volatile bool cancel_pressed;
extern PGconn *cancelConn;
extern void ResetCancelConn(void);
#ifndef WIN32
extern void handle_sigint(SIGNAL_ARGS);
#endif /* not WIN32 */
......
......@@ -3,7 +3,7 @@
*
* Copyright 2000 by PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.28 2002/10/19 00:22:14 tgl Exp $
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.29 2003/03/20 06:00:12 momjian Exp $
*/
#include "postgres_fe.h"
#include "copy.h"
......@@ -446,8 +446,6 @@ handleCopyOut(PGconn *conn, FILE *copystream)
char copybuf[COPYBUFSIZ];
int ret;
assert(cancelConn);
while (!copydone)
{
ret = PQgetline(conn, copybuf, COPYBUFSIZ);
......@@ -476,7 +474,7 @@ handleCopyOut(PGconn *conn, FILE *copystream)
}
fflush(copystream);
ret = !PQendcopy(conn);
cancelConn = NULL;
ResetCancelConn();
return ret;
}
......
......@@ -3,7 +3,7 @@
*
* Copyright 2000 by PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.21 2002/09/06 02:33:46 momjian Exp $
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.22 2003/03/20 06:00:12 momjian Exp $
*/
#include "postgres_fe.h"
#include "input.h"
......@@ -20,6 +20,15 @@
#ifdef USE_READLINE
static bool useReadline;
static bool useHistory;
enum histcontrol
{
hctl_none = 0,
hctl_ignorespace = 1,
hctl_ignoredups = 2,
hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups
};
#endif
#ifdef HAVE_ATEXIT
......@@ -33,6 +42,35 @@ static void finishInput(int, void *);
#define PSQLHISTORY ".psql_history"
#ifdef USE_READLINE
static enum histcontrol
GetHistControlConfig(void)
{
enum histcontrol HC;
const char *var;
var = GetVariable(pset.vars, "HISTCONTROL");
if (!var) HC = hctl_none;
else if (strcmp(var, "ignorespace") == 0) HC = hctl_ignorespace;
else if (strcmp(var, "ignoredups") == 0) HC = hctl_ignoredups;
else if (strcmp(var, "ignoreboth") == 0) HC = hctl_ignoreboth;
else HC = hctl_none;
return HC;
}
#endif
static char *
gets_basic(const char prompt[])
{
fputs(prompt, stdout);
fflush(stdout);
return gets_fromFile(stdin);
}
/*
* gets_interactive()
*
......@@ -40,45 +78,41 @@ static void finishInput(int, void *);
* The result is malloced.
*/
char *
gets_interactive(char *prompt)
gets_interactive(const char *prompt)
{
#ifdef USE_READLINE
char *s;
#ifdef USE_READLINE
const char *var;
static char *prev_hist = NULL;
#endif
#ifdef USE_READLINE
if (useReadline)
s = readline(prompt);
else
s = gets_basic(prompt);
if (useHistory && s && s[0])
{
#endif
fputs(prompt, stdout);
fflush(stdout);
s = gets_fromFile(stdin);
#ifdef USE_READLINE
}
#endif
enum histcontrol HC;
#ifdef USE_READLINE
if (useHistory && s && s[0] != '\0')
HC = GetHistControlConfig();
if (((HC & hctl_ignorespace) && s[0] == ' ') ||
((HC & hctl_ignoredups) && prev_hist && strcmp(s, prev_hist) == 0))
{
var = GetVariable(pset.vars, "HISTCONTROL");
if (!var || (var
&& !((strcmp(var, "ignorespace") == 0 || strcmp(var, "ignoreboth") == 0) && s[0] == ' ')
&& !((strcmp(var, "ignoredups") == 0 || strcmp(var, "ignoreboth") == 0) && prev_hist && strcmp(s, prev_hist) == 0)
))
/* Ignore this line as far as history is concerned */
}
else
{
free(prev_hist);
prev_hist = strdup(s);
add_history(s);
}
}
#endif
return s;
#else
return gets_basic(prompt);
#endif
}
......@@ -126,17 +160,12 @@ void
initializeInput(int flags)
{
#ifdef USE_READLINE
if (flags == 1)
if (flags & 1)
{
const char *home;
useReadline = true;
initialize_readline();
}
#endif
#ifdef USE_READLINE
if (flags == 1)
{
const char *home;
useHistory = true;
SetVariable(pset.vars, "HISTSIZE", "500");
......@@ -172,18 +201,14 @@ saveHistory(char *fname)
#ifdef USE_READLINE
if (useHistory && fname)
{
if (write_history(fname) != 0)
{
psql_error("could not save history to %s: %s\n", fname, strerror(errno));
return false;
}
if (write_history(fname) == 0)
return true;
psql_error("could not save history to %s: %s\n", fname, strerror(errno));
}
else
return false;
#else
return false;
#endif
return false;
}
......
......@@ -3,7 +3,7 @@
*
* Copyright 2000 by PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/bin/psql/input.h,v 1.18 2003/02/19 04:04:04 momjian Exp $
* $Header: /cvsroot/pgsql/src/bin/psql/input.h,v 1.19 2003/03/20 06:00:12 momjian Exp $
*/
#ifndef INPUT_H
#define INPUT_H
......@@ -32,7 +32,8 @@
#endif
#endif
char *gets_interactive(char *prompt);
char *gets_interactive(const char *prompt);
char *gets_fromFile(FILE *source);
void initializeInput(int flags);
......
......@@ -3,7 +3,7 @@
*
* Copyright 2000 by PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.51 2002/10/12 23:09:34 tgl Exp $
* $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.52 2003/03/20 06:00:12 momjian Exp $
*/
#include "postgres_fe.h"
#include "mainloop.h"
......@@ -92,8 +92,6 @@ MainLoop(FILE *source)
/* main loop to get queries and execute them */
while (1)
{
#ifndef WIN32
/*
* Welcome code for Control-C
*/
......@@ -113,6 +111,7 @@ MainLoop(FILE *source)
cancel_pressed = false;
}
#ifndef WIN32
if (sigsetjmp(main_loop_jmp, 1) != 0)
{
/* got here with longjmp */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment