Commit 0e6652e6 authored by Bruce Momjian's avatar Bruce Momjian

psql cleanup

parent 2323b636
This diff is collapsed.
......@@ -11,39 +11,36 @@
typedef enum _backslashResult {
CMD_UNKNOWN = 0, /* not done parsing yet (internal only) */
CMD_SEND, /* query complete; send off */
CMD_SKIP_LINE, /* keep building query */
CMD_TERMINATE, /* quit program */
CMD_NEWEDIT, /* query buffer was changed (e.g., via \e) */
CMD_ERROR /* the execution of the backslash command resulted
in an error */
} backslashResult;
backslashResult
HandleSlashCmds(PsqlSettings *pset,
const char *line,
PQExpBuffer query_buf,
const char ** end_of_cmd);
bool
do_connect(const char *new_dbname,
const char *new_user,
PsqlSettings *pset);
bool
process_file(const char *filename,
PsqlSettings *pset);
bool
do_pset(const char * param,
const char * value,
printQueryOpt * popt,
bool quiet);
typedef enum _backslashResult
{
CMD_UNKNOWN = 0, /* not done parsing yet (internal only) */
CMD_SEND, /* query complete; send off */
CMD_SKIP_LINE, /* keep building query */
CMD_TERMINATE, /* quit program */
CMD_NEWEDIT, /* query buffer was changed (e.g., via \e) */
CMD_ERROR /* the execution of the backslash command
* resulted in an error */
} backslashResult;
backslashResult HandleSlashCmds(PsqlSettings *pset,
const char *line,
PQExpBuffer query_buf,
const char **end_of_cmd);
bool do_connect(const char *new_dbname,
const char *new_user,
PsqlSettings *pset);
bool process_file(const char *filename,
PsqlSettings *pset);
bool do_pset(const char *param,
const char *value,
printQueryOpt * popt,
bool quiet);
#endif
This diff is collapsed.
......@@ -5,21 +5,21 @@
#include "settings.h"
char *
xstrdup(const char * string);
xstrdup(const char *string);
bool
setQFout(const char *fname, PsqlSettings *pset);
setQFout(const char *fname, PsqlSettings *pset);
char *
simple_prompt(const char *prompt, int maxlen, bool echo);
simple_prompt(const char *prompt, int maxlen, bool echo);
const char *
interpolate_var(const char * name, PsqlSettings * pset);
interpolate_var(const char *name, PsqlSettings *pset);
PGresult *
PSQLexec(PsqlSettings *pset, const char *query);
PGresult *
PSQLexec(PsqlSettings *pset, const char *query);
bool
SendQuery(PsqlSettings *pset, const char *query);
SendQuery(PsqlSettings *pset, const char *query);
#endif /* COMMON_H */
#endif /* COMMON_H */
This diff is collapsed.
......@@ -8,15 +8,15 @@
/* handler for \copy */
bool
do_copy(const char *args, PsqlSettings *pset);
do_copy(const char *args, PsqlSettings *pset);
/* lower level processors for copy in/out streams */
bool
handleCopyOut(PGconn *conn, FILE *copystream);
handleCopyOut(PGconn *conn, FILE *copystream);
bool
handleCopyIn(PGconn *conn, FILE *copystream, const char * prompt);
handleCopyIn(PGconn *conn, FILE *copystream, const char *prompt);
#endif
This diff is collapsed.
......@@ -5,38 +5,38 @@
/* \da */
bool
describeAggregates(const char * name, PsqlSettings * pset);
describeAggregates(const char *name, PsqlSettings *pset);
/* \df */
bool
describeFunctions(const char * name, PsqlSettings * pset);
describeFunctions(const char *name, PsqlSettings *pset);
/* \dT */
bool
describeTypes(const char * name, PsqlSettings * pset);
describeTypes(const char *name, PsqlSettings *pset);
/* \do */
bool
describeOperators(const char * name, PsqlSettings * pset);
describeOperators(const char *name, PsqlSettings *pset);
/* \dp (formerly \z) */
bool
permissionsList(const char * name, PsqlSettings *pset);
permissionsList(const char *name, PsqlSettings *pset);
/* \dd */
bool
objectDescription(const char * object, PsqlSettings *pset);
objectDescription(const char *object, PsqlSettings *pset);
/* \d foo */
bool
describeTableDetails(const char * name, PsqlSettings * pset);
describeTableDetails(const char *name, PsqlSettings *pset);
/* \l */
bool
listAllDbs(PsqlSettings *pset);
listAllDbs(PsqlSettings *pset);
/* \dt, \di, \dS, etc. */
bool
listTables(const char * infotype, const char * name, PsqlSettings * pset);
listTables(const char *infotype, const char *name, PsqlSettings *pset);
#endif /* DESCRIBE_H */
#endif /* DESCRIBE_H */
This diff is collapsed.
......@@ -3,14 +3,13 @@
#include "settings.h"
void usage(void);
void usage(void);
void slashUsage(PsqlSettings *pset);
void slashUsage(PsqlSettings *pset);
void helpSQL(const char *topic);
void helpSQL(const char *topic);
void print_copyright(void);
void print_copyright(void);
#endif
......@@ -10,9 +10,11 @@
/* (of course there is no runtime command for doing that :) */
#ifdef USE_READLINE
static bool useReadline;
#endif
#ifdef USE_HISTORY
static bool useHistory;
#endif
......@@ -25,29 +27,31 @@ static bool useHistory;
char *
gets_interactive(const char *prompt)
{
char * s;
char *s;
#ifdef USE_READLINE
if (useReadline) {
s = readline(prompt);
fputc('\r', stdout);
fflush(stdout);
}
else {
if (useReadline)
{
s = readline(prompt);
fputc('\r', stdout);
fflush(stdout);
}
else
{
#endif
fputs(prompt, stdout);
fflush(stdout);
s = gets_fromFile(stdin);
fputs(prompt, stdout);
fflush(stdout);
s = gets_fromFile(stdin);
#ifdef USE_READLINE
}
}
#endif
#ifdef USE_HISTORY
if (useHistory && s && s[0] != '\0')
add_history(s);
if (useHistory && s && s[0] != '\0')
add_history(s);
#endif
return s;
return s;
}
......@@ -60,25 +64,27 @@ gets_interactive(const char *prompt)
char *
gets_fromFile(FILE *source)
{
PQExpBufferData buffer;
char line[1024];
initPQExpBuffer(&buffer);
while (fgets(line, 1024, source) != NULL) {
appendPQExpBufferStr(&buffer, line);
if (buffer.data[buffer.len-1] == '\n') {
buffer.data[buffer.len-1] = '\0';
return buffer.data;
PQExpBufferData buffer;
char line[1024];
initPQExpBuffer(&buffer);
while (fgets(line, 1024, source) != NULL)
{
appendPQExpBufferStr(&buffer, line);
if (buffer.data[buffer.len - 1] == '\n')
{
buffer.data[buffer.len - 1] = '\0';
return buffer.data;
}
}
}
if (buffer.len > 0)
return buffer.data; /* EOF after reading some bufferload(s) */
if (buffer.len > 0)
return buffer.data; /* EOF after reading some bufferload(s) */
/* EOF, so return null */
termPQExpBuffer(&buffer);
return NULL;
/* EOF, so return null */
termPQExpBuffer(&buffer);
return NULL;
}
......@@ -93,28 +99,33 @@ void
initializeInput(int flags)
{
#ifdef USE_READLINE
if (flags == 1) {
useReadline = true;
rl_readline_name = "psql";
}
if (flags == 1)
{
useReadline = true;
rl_readline_name = "psql";
}
#endif
#ifdef USE_HISTORY
if (flags == 1) {
const char * home;
useHistory = true;
using_history();
home = getenv("HOME");
if (home) {
char * psql_history = (char *) malloc(strlen(home) + 20);
if (psql_history) {
sprintf(psql_history, "%s/.psql_history", home);
read_history(psql_history);
free(psql_history);
}
if (flags == 1)
{
const char *home;
useHistory = true;
using_history();
home = getenv("HOME");
if (home)
{
char *psql_history = (char *) malloc(strlen(home) + 20);
if (psql_history)
{
sprintf(psql_history, "%s/.psql_history", home);
read_history(psql_history);
free(psql_history);
}
}
}
}
#endif
}
......@@ -124,17 +135,19 @@ bool
saveHistory(const char *fname)
{
#ifdef USE_HISTORY
if (useHistory) {
if (write_history(fname) != 0) {
perror(fname);
return false;
if (useHistory)
{
if (write_history(fname) != 0)
{
perror(fname);
return false;
}
return true;
}
return true;
}
else
return false;
else
return false;
#else
return false;
return false;
#endif
}
......@@ -144,19 +157,22 @@ void
finishInput(void)
{
#ifdef USE_HISTORY
if (useHistory) {
char * home;
char * psql_history;
home = getenv("HOME");
if (home) {
psql_history = (char *) malloc(strlen(home) + 20);
if (psql_history) {
sprintf(psql_history, "%s/.psql_history", home);
write_history(psql_history);
free(psql_history);
}
if (useHistory)
{
char *home;
char *psql_history;
home = getenv("HOME");
if (home)
{
psql_history = (char *) malloc(strlen(home) + 20);
if (psql_history)
{
sprintf(psql_history, "%s/.psql_history", home);
write_history(psql_history);
free(psql_history);
}
}
}
}
#endif
}
......@@ -38,19 +38,19 @@
char *
gets_interactive(const char *prompt);
gets_interactive(const char *prompt);
char *
gets_fromFile(FILE *source);
gets_fromFile(FILE *source);
void
initializeInput(int flags);
initializeInput(int flags);
bool
saveHistory(const char *fname);
saveHistory(const char *fname);
void
finishInput(void);
finishInput(void);
#endif
This diff is collapsed.
......@@ -3,9 +3,9 @@
#include "settings.h"
bool do_lo_export(PsqlSettings * pset, const char * loid_arg, const char * filename_arg);
bool do_lo_import(PsqlSettings * pset, const char * filename_arg, const char * comment_arg);
bool do_lo_unlink(PsqlSettings * pset, const char * loid_arg);
bool do_lo_list(PsqlSettings * pset);
bool do_lo_export(PsqlSettings *pset, const char *loid_arg, const char *filename_arg);
bool do_lo_import(PsqlSettings *pset, const char *filename_arg, const char *comment_arg);
bool do_lo_unlink(PsqlSettings *pset, const char *loid_arg);
bool do_lo_list(PsqlSettings *pset);
#endif /* LARGE_OBJ_H */
#endif /* LARGE_OBJ_H */
This diff is collapsed.
......@@ -5,6 +5,6 @@
#include "settings.h"
int
MainLoop(PsqlSettings *pset, FILE *source);
MainLoop(PsqlSettings *pset, FILE *source);
#endif MAINLOOP_H
#endif /* MAINLOOP_H */
This diff is collapsed.
......@@ -7,52 +7,58 @@
#include <stdio.h>
#include <libpq-fe.h>
enum printFormat {
PRINT_NOTHING = 0, /* to make sure someone initializes this */
PRINT_UNALIGNED,
PRINT_ALIGNED,
PRINT_HTML,
PRINT_LATEX
/* add your favourite output format here ... */
enum printFormat
{
PRINT_NOTHING = 0, /* to make sure someone initializes this */
PRINT_UNALIGNED,
PRINT_ALIGNED,
PRINT_HTML,
PRINT_LATEX
/* add your favourite output format here ... */
};
typedef struct _printTableOpt {
enum printFormat format; /* one of the above */
bool expanded; /* expanded/vertical output (if supported by output format) */
bool pager; /* use pager for output (if to stdout and stdout is a tty) */
bool tuples_only; /* don't output headers, row counts, etc. */
unsigned short int border; /* Print a border around the table. 0=none, 1=dividing lines, 2=full */
char *fieldSep; /* field separator for unaligned text mode */
char *tableAttr; /* attributes for HTML <table ...> */
} printTableOpt;
typedef struct _printTableOpt
{
enum printFormat format; /* one of the above */
bool expanded; /* expanded/vertical output (if supported
* by output format) */
bool pager; /* use pager for output (if to stdout and
* stdout is a tty) */
bool tuples_only; /* don't output headers, row counts, etc. */
unsigned short int border; /* Print a border around the table.
* 0=none, 1=dividing lines, 2=full */
char *fieldSep; /* field separator for unaligned text mode */
char *tableAttr; /* attributes for HTML <table ...> */
} printTableOpt;
/*
* Use this to print just any table in the supported formats.
* - title is just any string (NULL is fine)
* - headers is the column headings (NULL ptr terminated). It must be given and
* complete since the column count is generated from this.
* complete since the column count is generated from this.
* - cells are the data cells to be printed. Now you know why the correct
* column count is important
* column count is important
* - footers are lines to be printed below the table
* - align is an 'l' or an 'r' for every column, if the output format needs it.
* (You must specify this long enough. Otherwise anything could happen.)
* (You must specify this long enough. Otherwise anything could happen.)
*/
void
printTable(const char * title, char ** headers, char ** cells, char ** footers,
const char * align,
const printTableOpt * opt, FILE * fout);
void printTable(const char *title, char **headers, char **cells, char **footers,
const char *align,
const printTableOpt * opt, FILE *fout);
typedef struct _printQueryOpt {
printTableOpt topt; /* the options above */
char * nullPrint; /* how to print null entities */
bool quote; /* quote all values as much as possible */
char * title; /* override title */
char ** footers; /* override footer (default is "(xx rows)") */
} printQueryOpt;
typedef struct _printQueryOpt
{
printTableOpt topt; /* the options above */
char *nullPrint; /* how to print null entities */
bool quote; /* quote all values as much as possible */
char *title; /* override title */
char **footers; /* override footer (default is "(xx
* rows)") */
} printQueryOpt;
/*
* Use this to print query results
......@@ -60,7 +66,7 @@ typedef struct _printQueryOpt {
* It calls the printTable above with all the things set straight.
*/
void
printQuery(PGresult * result, const printQueryOpt * opt, FILE * fout);
printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout);
#endif /* PRINT_H */
#endif /* PRINT_H */
This diff is collapsed.
......@@ -3,17 +3,18 @@
#include "settings.h"
typedef enum _promptStatus {
PROMPT_READY,
PROMPT_CONTINUE,
PROMPT_COMMENT,
PROMPT_SINGLEQUOTE,
PROMPT_DOUBLEQUOTE,
PROMPT_COPY
} promptStatus_t;
typedef enum _promptStatus
{
PROMPT_READY,
PROMPT_CONTINUE,
PROMPT_COMMENT,
PROMPT_SINGLEQUOTE,
PROMPT_DOUBLEQUOTE,
PROMPT_COPY
} promptStatus_t;
const char *
get_prompt(PsqlSettings *pset, promptStatus_t status);
get_prompt(PsqlSettings *pset, promptStatus_t status);
#endif /* PROMPT_H */
#endif /* PROMPT_H */
This diff is collapsed.
......@@ -5,7 +5,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: psqlHelp.h,v 1.80 1999/10/29 23:52:22 momjian Exp $
* $Id: psqlHelp.h,v 1.81 1999/11/04 23:14:29 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -384,5 +384,6 @@ TIMEZONE|XACTISOLEVEL|CLIENT_ENCODING|SERVER_ENCODING"},
\tVACUUM [VERBOSE] [ANALYZE] [table]\n\
\tor\n\
\tVACUUM [VERBOSE] ANALYZE [table [(column_name1, ...column_nameN)]];"},
{NULL, NULL, NULL} /* important to keep a NULL terminator here!*/
{NULL, NULL, NULL} /* important to keep a NULL terminator
* here! */
};
......@@ -20,24 +20,27 @@
typedef struct _psqlSettings
{
PGconn *db; /* connection to backend */
FILE *queryFout; /* where to send the query results */
bool queryFoutPipe; /* queryFout is from a popen() */
printQueryOpt popt;
VariableSpace vars; /* "shell variable" repository */
char *gfname; /* one-shot file output argument for \g */
bool notty; /* stdin or stdout is not a tty (as determined on startup) */
bool useReadline; /* use libreadline routines */
bool useHistory;
bool getPassword; /* prompt the user for a username and
password */
FILE * cur_cmd_source; /* describe the status of the current main loop */
bool cur_cmd_interactive;
bool has_client_encoding; /* was PGCLIENTENCODING set on startup? */
PGconn *db; /* connection to backend */
FILE *queryFout; /* where to send the query results */
bool queryFoutPipe; /* queryFout is from a popen() */
printQueryOpt popt;
VariableSpace vars; /* "shell variable" repository */
char *gfname; /* one-shot file output argument for \g */
bool notty; /* stdin or stdout is not a tty (as
* determined on startup) */
bool useReadline; /* use libreadline routines */
bool useHistory;
bool getPassword; /* prompt the user for a username and
* password */
FILE *cur_cmd_source; /* describe the status of the current main
* loop */
bool cur_cmd_interactive;
bool has_client_encoding; /* was PGCLIENTENCODING set on
* startup? */
} PsqlSettings;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -3,12 +3,11 @@
/* The cooler version of strtok() which knows about quotes and doesn't
* overwrite your input */
extern char *
strtokx(const char *s,
const char *delim,
const char *quote,
char escape,
char * was_quoted,
unsigned int * token_pos);
extern char *strtokx(const char *s,
const char *delim,
const char *quote,
char escape,
char *was_quoted,
unsigned int *token_pos);
#endif /* STRINGUTILS_H */
......@@ -6,127 +6,145 @@
#include <assert.h>
VariableSpace CreateVariableSpace(void)
VariableSpace
CreateVariableSpace(void)
{
struct _variable *ptr;
ptr = calloc(1, sizeof *ptr);
if (!ptr) return NULL;
struct _variable *ptr;
ptr = calloc(1, sizeof *ptr);
if (!ptr)
return NULL;
ptr->name = strdup("@");
ptr->value = strdup("");
if (!ptr->name || !ptr->value)
{
free(ptr->name);
free(ptr->value);
free(ptr);
return NULL;
}
ptr->name = strdup("@");
ptr->value = strdup("");
if (!ptr->name || !ptr->value) {
free(ptr->name);
free(ptr->value);
free(ptr);
return NULL;
}
return ptr;
return ptr;
}
const char * GetVariable(VariableSpace space, const char * name)
const char *
GetVariable(VariableSpace space, const char *name)
{
struct _variable *current;
struct _variable *current;
if (!space)
return NULL;
if (!space)
return NULL;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return NULL;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
return NULL;
for (current = space; current; current = current->next) {
for (current = space; current; current = current->next)
{
#ifdef USE_ASSERT_CHECKING
assert(current->name);
assert(current->value);
assert(current->name);
assert(current->value);
#endif
if (strcmp(current->name, name)==0)
return current->value;
}
if (strcmp(current->name, name) == 0)
return current->value;
}
return NULL;
return NULL;
}
bool GetVariableBool(VariableSpace space, const char * name)
bool
GetVariableBool(VariableSpace space, const char *name)
{
return GetVariable(space, name)!=NULL ? true : false;
return GetVariable(space, name) != NULL ? true : false;
}
bool SetVariable(VariableSpace space, const char * name, const char * value)
bool
SetVariable(VariableSpace space, const char *name, const char *value)
{
struct _variable *current, *previous;
struct _variable *current,
*previous;
if (!space)
return false;
if (!space)
return false;
if (!value)
return DeleteVariable(space, name);
if (!value)
return DeleteVariable(space, name);
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return false;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
return false;
for (current = space; current; previous = current, current = current->next) {
for (current = space; current; previous = current, current = current->next)
{
#ifdef USE_ASSERT_CHECKING
assert(current->name);
assert(current->value);
assert(current->name);
assert(current->value);
#endif
if (strcmp(current->name, name)==0) {
free (current->value);
current->value = strdup(value);
return current->value ? true : false;
if (strcmp(current->name, name) == 0)
{
free(current->value);
current->value = strdup(value);
return current->value ? true : false;
}
}
}
previous->next = calloc(1, sizeof *(previous->next));
if (!previous->next)
return false;
previous->next->name = strdup(name);
if (!previous->next->name)
return false;
previous->next->value = strdup(value);
return previous->next->value ? true : false;
previous->next = calloc(1, sizeof *(previous->next));
if (!previous->next)
return false;
previous->next->name = strdup(name);
if (!previous->next->name)
return false;
previous->next->value = strdup(value);
return previous->next->value ? true : false;
}
bool DeleteVariable(VariableSpace space, const char * name)
bool
DeleteVariable(VariableSpace space, const char *name)
{
struct _variable *current, *previous;
struct _variable *current,
*previous;
if (!space)
return false;
if (!space)
return false;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return false;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
return false;
for (current = space, previous = NULL; current; previous = current, current = current->next) {
for (current = space, previous = NULL; current; previous = current, current = current->next)
{
#ifdef USE_ASSERT_CHECKING
assert(current->name);
assert(current->value);
assert(current->name);
assert(current->value);
#endif
if (strcmp(current->name, name)==0) {
free (current->name);
free (current->value);
if (previous)
previous->next = current->next;
free(current);
return true;
if (strcmp(current->name, name) == 0)
{
free(current->name);
free(current->value);
if (previous)
previous->next = current->next;
free(current);
return true;
}
}
}
return true;
return true;
}
void DestroyVariableSpace(VariableSpace space)
void
DestroyVariableSpace(VariableSpace space)
{
if (!space)
return;
if (!space)
return;
DestroyVariableSpace(space->next);
free(space);
DestroyVariableSpace(space->next);
free(space);
}
......@@ -13,21 +13,22 @@
#define VALID_VARIABLE_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
struct _variable {
char * name;
char * value;
struct _variable * next;
struct _variable
{
char *name;
char *value;
struct _variable *next;
};
typedef struct _variable * VariableSpace;
typedef struct _variable *VariableSpace;
VariableSpace CreateVariableSpace(void);
const char * GetVariable(VariableSpace space, const char * name);
bool GetVariableBool(VariableSpace space, const char * name);
bool SetVariable(VariableSpace space, const char * name, const char * value);
bool DeleteVariable(VariableSpace space, const char * name);
void DestroyVariableSpace(VariableSpace space);
const char *GetVariable(VariableSpace space, const char *name);
bool GetVariableBool(VariableSpace space, const char *name);
bool SetVariable(VariableSpace space, const char *name, const char *value);
bool DeleteVariable(VariableSpace space, const char *name);
void DestroyVariableSpace(VariableSpace space);
#endif /* VARIABLES_H */
#endif /* VARIABLES_H */
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