Commit 76bc8cb9 authored by Julian Assange's avatar Julian Assange

Large re-write/enhancement. In pg-101 Jolly only included a smaller part

of my (proff) patch. This is the rest of it, with a few, mainly aesthetic
changes. I've removed a lot of redundency from the original code,
added support for the new PQprint() routines in libpq, expanded tables,
and a few generally nifty ways of massaging data in and out of the
backend. Still needs some good stress testing.
parent 23c7ff0b
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.6 1996/07/23 03:03:43 scrappy Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.7 1996/07/25 06:46:35 julian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
#include <signal.h> #include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include "libpq-fe.h" #include "libpq-fe.h"
#include "stringutils.h" #include "stringutils.h"
...@@ -32,52 +32,57 @@ extern char *readline(char *); /* in rlstubs.c */ ...@@ -32,52 +32,57 @@ extern char *readline(char *); /* in rlstubs.c */
#include "history.h" #include "history.h"
#else #else
#include <readline/readline.h> #include <readline/readline.h>
#include <history.h> #include <readline/history.h>
#endif #endif
#endif #endif
#define MAX_QUERY_BUFFER 20000 #define MAX_QUERY_BUFFER 20000
#define MAX_FIELD_SEP_LENGTH 40
#define COPYBUFSIZ 8192 #define COPYBUFSIZ 8192
#define DEFAULT_FIELD_SEP " " #define DEFAULT_FIELD_SEP "|"
#define DEFAULT_EDITOR "vi" #define DEFAULT_EDITOR "vi"
#define DEFAULT_SHELL "/bin/sh" #define DEFAULT_SHELL "/bin/sh"
typedef struct _psqlSettings { typedef struct _psqlSettings {
int echoQuery; /* if 1, echo the query before sending it */ PGconn *db; /* connection to backend */
int quiet; /* run quietly, no messages, no promt */
int singleStep; /* if 1, prompt for each query */
int singleLineMode; /* if 1, query terminated by newline */
int useReadline; /* use the readline routines or not */
int printHeader; /* print output field headers or not */
int fillAlign; /* fill align the fields */
FILE *queryFout; /* where to send the query results */ FILE *queryFout; /* where to send the query results */
char fieldSep[MAX_FIELD_SEP_LENGTH]; /* field separator */ PQprintOpt opt; /* options to be passed to PQprint */
char *prompt; /* prompt to display */
char *gfname; /* one-shot file output argument for \g */
bool notty; /* input or output is not a tty */
bool pipe; /* queryFout is from a popen() */
bool echoQuery; /* echo the query before sending it */
bool quiet; /* run quietly, no messages, no promt */
bool singleStep; /* prompt before for each query */
bool singleLineMode; /* query terminated by newline */
bool useReadline; /* use libreadline routines */
} PsqlSettings; } PsqlSettings;
/* declarations for functions in this file */ /* declarations for functions in this file */
static void usage(char* progname); static void usage(char* progname);
static void slashUsage(); static void slashUsage();
static void handleCopyOut(PGresult *res, int quiet); static void handleCopyOut(PGresult *res, bool quiet);
static void handleCopyIn(PGresult *res, int quiet); static void handleCopyIn(PGresult *res, bool quiet);
static int tableList(PGconn* conn, int deep_tablelist); static int tableList(PsqlSettings *ps, bool deep_tablelist);
static int tableDesc(PGconn* conn, char* table); static int tableDesc(PsqlSettings *ps, char *table);
char* gets_noreadline(char* prompt, FILE* source); char* gets_noreadline(char* prompt, FILE* source);
char* gets_readline(char* prompt, FILE* source); char* gets_readline(char* prompt, FILE* source);
char* gets_fromFile(char* prompt, FILE* source); char* gets_fromFile(char* prompt, FILE* source);
int listAllDbs(PGconn *db, PsqlSettings *settings); int listAllDbs(PsqlSettings *settings);
int SendQuery(PGconn* db, char* query, PsqlSettings *settings); int SendQuery(PsqlSettings *settings, char* query);
int HandleSlashCmds(PGconn** db_ptr, int HandleSlashCmds(PsqlSettings *settings,
char *line, char *line,
char** prompt_ptr, char *query);
char *query, int MainLoop(PsqlSettings *settings, FILE *source);
PsqlSettings *settings); /* probably should move this into libpq */
int MainLoop(PGconn** db_ptr, FILE *source, PsqlSettings *settings); void PQprint(FILE *fp,
FILE* setFout(char *fname); PGresult *res,
PQprintOpt *po
);
FILE* setFout(PsqlSettings *ps, char *fname);
/* /*
* usage * usage
...@@ -89,22 +94,24 @@ usage(char* progname) ...@@ -89,22 +94,24 @@ usage(char* progname)
{ {
fprintf(stderr,"Usage: %s [options] [dbname]\n",progname); fprintf(stderr,"Usage: %s [options] [dbname]\n",progname);
fprintf(stderr,"\t -a authsvc set authentication service\n"); fprintf(stderr,"\t -a authsvc set authentication service\n");
fprintf(stderr,"\t -A turn off fill-justification when printing out attributes\n"); fprintf(stderr,"\t -A turn off alignment when printing out attributes\n");
fprintf(stderr,"\t -c query run single query (slash commands too)\n"); fprintf(stderr,"\t -c query run single query (slash commands too)\n");
fprintf(stderr,"\t -d dbName specify database name\n"); fprintf(stderr,"\t -d dbName specify database name\n");
fprintf(stderr,"\t -e echo the query sent to the backend\n"); fprintf(stderr,"\t -e echo the query sent to the backend\n");
fprintf(stderr,"\t -f filename use file as a source of queries\n"); fprintf(stderr,"\t -f filename use file as a source of queries\n");
fprintf(stderr,"\t -F sep set the field separator (default is " ")\n"); fprintf(stderr,"\t -F sep set the field separator (default is " ")\n");
fprintf(stderr,"\t -h help information\n"); fprintf(stderr,"\t -h host set database server host\n");
fprintf(stderr,"\t -H host set database server host\n"); fprintf(stderr,"\t -H turn on html3.0 table output\n");
fprintf(stderr,"\t -l list available databases\n"); fprintf(stderr,"\t -l list available databases\n");
fprintf(stderr,"\t -n don't use readline library\n"); fprintf(stderr,"\t -n don't use readline library\n");
fprintf(stderr,"\t -o filename send output to filename\n"); fprintf(stderr,"\t -o filename send output to filename or (|pipe)\n");
fprintf(stderr,"\t -p port set port number\n"); fprintf(stderr,"\t -p port set port number\n");
fprintf(stderr,"\t -q run quietly (no messages, no prompts)\n"); fprintf(stderr,"\t -q run quietly (no messages, no prompts)\n");
fprintf(stderr,"\t -s single step mode (prompts for each query)\n"); fprintf(stderr,"\t -s single step mode (prompts for each query)\n");
fprintf(stderr,"\t -S single line mode (i.e. query terminated by newline)\n"); fprintf(stderr,"\t -S single line mode (i.e. query terminated by newline)\n");
fprintf(stderr,"\t -T turn off printing of attribute names\n"); fprintf(stderr,"\t -t turn off printing of attribute headers\n");
fprintf(stderr,"\t -T html set html3.0 table command options (cf. -H)\n");
fprintf(stderr,"\t -x turn on expanded output (field names on left)");
exit(1); exit(1);
} }
...@@ -113,29 +120,60 @@ usage(char* progname) ...@@ -113,29 +120,60 @@ usage(char* progname)
* print out usage for the backslash commands * print out usage for the backslash commands
*/ */
char *on(bool f)
{
return f? "on": "off";
}
static void static void
slashUsage() slashUsage(PsqlSettings *ps)
{ {
fprintf(stderr,"\t \\a -- toggle fill-justification of display of attributes\n"); fprintf(stderr,"\t \\a -- toggle field-alignment (currenty %s)\n", on(ps->opt.align));
fprintf(stderr,"\t \\C [<captn>] -- set html3 caption (currently '%s')\n", ps->opt.caption? ps->opt.caption: "");
fprintf(stderr,"\t \\c <dbname> -- connect to new database (currently '%s')\n", PQdb(ps->db));
fprintf(stderr,"\t \\d [<table>] -- list tables in database or columns in <table>\n"); fprintf(stderr,"\t \\d [<table>] -- list tables in database or columns in <table>\n");
fprintf(stderr,"\t \\d * -- list tables in database and columns in all tables\n"); fprintf(stderr,"\t \\d * -- list tables in database and columns in all tables\n");
fprintf(stderr,"\t \\e [<fname>] -- edit the current query buffer or <fname>\n"); fprintf(stderr,"\t \\e [<fname>] -- edit the current query buffer or <fname>\n");
fprintf(stderr,"\t \\f <sep> -- change field separator\n"); fprintf(stderr,"\t \\f [<sep>] -- change field separater (currently '%s')\n", ps->opt.fieldSep);
fprintf(stderr,"\t \\g -- query to backend\n"); fprintf(stderr,"\t \\g [<fname>] -- send query to backend [and place results in <fname>]\n");
fprintf(stderr,"\t \\h <command> -- help on syntax of sql commands\n"); fprintf(stderr,"\t \\g |<cmd> -- send query to backend and pipe results into <cmd>\n");
fprintf(stderr,"\t \\h [<cmd>] -- help on syntax of sql commands\n");
fprintf(stderr,"\t \\h * -- complete description of all sql commands\n"); fprintf(stderr,"\t \\h * -- complete description of all sql commands\n");
fprintf(stderr,"\t \\g -- send query to backend\n"); fprintf(stderr,"\t \\H -- toggle html3 output (currently %s)\n", on(ps->opt.html3));
fprintf(stderr,"\t \\i <fname> -- read queries from filename\n"); fprintf(stderr,"\t \\i <fname> -- read and execute queries from filename\n");
fprintf(stderr,"\t \\l -- list all databases\n"); fprintf(stderr,"\t \\l -- list all databases\n");
fprintf(stderr,"\t \\o [<fname>] -- send query results file named <fname> or stdout\n"); fprintf(stderr,"\t \\m -- toggle monitor-like type-setting (currently %s)\n", on(ps->opt.standard));
fprintf(stderr,"\t \\o [<fname>] -- send all query results to <fname> or stdout\n");
fprintf(stderr,"\t \\o |<cmd> -- pipe all query results through <cmd>\n");
fprintf(stderr,"\t \\p -- print the current query buffer\n"); fprintf(stderr,"\t \\p -- print the current query buffer\n");
fprintf(stderr,"\t \\q -- quit\n"); fprintf(stderr,"\t \\q -- quit\n");
fprintf(stderr,"\t \\s [<fname>] -- save or print history\n"); fprintf(stderr,"\t \\r [<fname>] -- edit <fname> then execute on save\n");
fprintf(stderr,"\t \\t -- toggle output field headers (defaults to on)\n"); fprintf(stderr,"\t \\s [<fname>] -- print history or save it in <fname>\n");
fprintf(stderr,"\t \\! [<cmd>] -- shell escape\n"); fprintf(stderr,"\t \\t -- toggle table output header (currently %s)\n", on(ps->opt.header));
fprintf(stderr,"\t \\T [<html>] -- set html3.0 <table ...> options (currently '%s')\n", ps->opt.tableOpt? ps->opt.tableOpt: "");
fprintf(stderr,"\t \\x -- toggle expanded output (currently %s)\n", on(ps->opt.expanded));
fprintf(stderr,"\t \\z -- zorch current query buffer (i.e clear it)\n");
fprintf(stderr,"\t \\! [<cmd>] -- shell escape or command\n");
fprintf(stderr,"\t \\? -- help\n"); fprintf(stderr,"\t \\? -- help\n");
} }
PGresult *
PSQLexec(PsqlSettings *ps, char *query)
{
PGresult *res = PQexec(ps->db, query);
if (!res)
fputs(PQerrorMessage(ps->db), stderr);
else
{
if (PQresultStatus(res)==PGRES_COMMAND_OK ||
PQresultStatus(res)==PGRES_TUPLES_OK)
return res;
if (!ps->quiet)
fputs(PQerrorMessage(ps->db), stderr);
PQclear(res);
}
return NULL;
}
/* /*
* listAllDbs * listAllDbs
* *
...@@ -144,50 +182,37 @@ slashUsage() ...@@ -144,50 +182,37 @@ slashUsage()
* *
* *
*/ */
int int
listAllDbs(PGconn *db, PsqlSettings *settings) listAllDbs(PsqlSettings *ps)
{ {
PGresult *results; PGresult *results;
char* query = "select * from pg_database;"; char* query = "select * from pg_database;";
results = PQexec(db, query); if (!(results=PSQLexec(ps, query)))
if (results == NULL) {
fprintf(stderr,"%s", PQerrorMessage(db));
return 1; return 1;
}
if (PQresultStatus(results) != PGRES_TUPLES_OK)
{
fprintf(stderr,"Unexpected error from executing: %s\n", query);
return 2;
}
else else
{ {
PQdisplayTuples(results, PQprint(ps->queryFout,
settings->queryFout, results,
settings->fillAlign, &ps->opt);
settings->fieldSep,
settings->printHeader,
settings->quiet);
PQclear(results); PQclear(results);
return 0; return 0;
} }
} }
/* /*
* tableList (PGconn* conn)
* *
* List The Database Tables * List The Database Tables
* returns 0 if all went well * returns 0 if all went well
* *
*/ */
int int
tableList (PGconn* conn, int deep_tablelist) tableList (PsqlSettings *ps, bool deep_tablelist)
{ {
char listbuf[256]; char listbuf[256];
int nColumns; int nColumns;
int i; int i;
char* ru;
char* rk; char* rk;
char* rr; char* rr;
...@@ -203,17 +228,8 @@ tableList (PGconn* conn, int deep_tablelist) ...@@ -203,17 +228,8 @@ tableList (PGconn* conn, int deep_tablelist)
add in the int4oideq function */ add in the int4oideq function */
strcat(listbuf," and usesysid = relowner"); strcat(listbuf," and usesysid = relowner");
strcat(listbuf," ORDER BY relname "); strcat(listbuf," ORDER BY relname ");
res = PQexec(conn,listbuf); if (!(res=PSQLexec(ps, listbuf)))
if (res == NULL) { return -1;
fprintf(stderr,"%s", PQerrorMessage(conn));
return (-1);
}
if ((PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) <= 0)) {
fprintf(stderr,"No tables found in database %s.\n", PQdb(conn));
PQclear(res);
return (-1);
}
/* first, print out the attribute names */ /* first, print out the attribute names */
nColumns = PQntuples(res); nColumns = PQntuples(res);
...@@ -236,14 +252,14 @@ tableList (PGconn* conn, int deep_tablelist) ...@@ -236,14 +252,14 @@ tableList (PGconn* conn, int deep_tablelist)
PQclear(res); PQclear(res);
for (i=0; i < nColumns; i++) { for (i=0; i < nColumns; i++) {
tableDesc(conn,table[i]); tableDesc(ps, table[i]);
} }
free(table); free(table);
} }
else { else {
/* Display the information */ /* Display the information */
printf ("\nDatabase = %s\n", PQdb(conn)); printf ("\nDatabase = %s\n", PQdb(ps->db));
printf (" +------------------+----------------------------------+----------+\n"); printf (" +------------------+----------------------------------+----------+\n");
printf (" | Owner | Relation | Type |\n"); printf (" | Owner | Relation | Type |\n");
printf (" +------------------+----------------------------------+----------+\n"); printf (" +------------------+----------------------------------+----------+\n");
...@@ -272,7 +288,7 @@ tableList (PGconn* conn, int deep_tablelist) ...@@ -272,7 +288,7 @@ tableList (PGconn* conn, int deep_tablelist)
} }
/* /*
* Describe a table (PGconn* conn, char* table) * Describe a table
* *
* Describe the columns in a database table. * Describe the columns in a database table.
* returns 0 if all went well * returns 0 if all went well
...@@ -280,7 +296,7 @@ tableList (PGconn* conn, int deep_tablelist) ...@@ -280,7 +296,7 @@ tableList (PGconn* conn, int deep_tablelist)
* *
*/ */
int int
tableDesc (PGconn* conn, char* table) tableDesc (PsqlSettings *ps, char *table)
{ {
char descbuf[256]; char descbuf[256];
int nColumns; int nColumns;
...@@ -302,16 +318,8 @@ tableDesc (PGconn* conn, char* table) ...@@ -302,16 +318,8 @@ tableDesc (PGconn* conn, char* table)
strcat(descbuf," and a.attrelid = c.oid "); strcat(descbuf," and a.attrelid = c.oid ");
strcat(descbuf," and a.atttypid = t.oid "); strcat(descbuf," and a.atttypid = t.oid ");
strcat(descbuf," ORDER BY attnum "); strcat(descbuf," ORDER BY attnum ");
res = PQexec(conn,descbuf); if (!(res = PSQLexec(ps, descbuf)))
if (res == NULL) { return -1;
fprintf(stderr,"%s", PQerrorMessage(conn));
return (-1);
}
if ((PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) <= 0)) {
fprintf(stderr,"Couldn't find table %s!\n", table);
PQclear(res);
return (-1);
}
/* first, print out the attribute names */ /* first, print out the attribute names */
nColumns = PQntuples(res); nColumns = PQntuples(res);
if (nColumns > 0) if (nColumns > 0)
...@@ -332,15 +340,15 @@ tableDesc (PGconn* conn, char* table) ...@@ -332,15 +340,15 @@ tableDesc (PGconn* conn, char* table)
rsize = atoi(PQgetvalue(res,i,3)); rsize = atoi(PQgetvalue(res,i,3));
if (strcmp(rtype, "text") == 0) { if (strcmp(rtype, "text") == 0) {
printf ("%-32.32s |", rtype); printf ("%-32.32s |", rtype);
printf ("%-6s |", "var" ); printf ("%6s |", "var" );
} }
else if (strcmp(rtype, "bpchar") == 0) { else if (strcmp(rtype, "bpchar") == 0) {
printf ("%-32.32s |", "char"); printf ("%-32.32s |", "char");
printf ("%-6i |", rsize > 0 ? rsize - 4 : 0 ); printf ("%6i |", rsize > 0 ? rsize - 4 : 0 );
} }
else if (strcmp(rtype, "varchar") == 0) { else if (strcmp(rtype, "varchar") == 0) {
printf ("%-32.32s |", rtype); printf ("%-32.32s |", rtype);
printf ("%-6i |", rsize > 0 ? rsize - 4 : 0 ); printf ("%6i |", rsize > 0 ? rsize - 4 : 0 );
} }
else { else {
/* array types start with an underscore */ /* array types start with an underscore */
...@@ -355,9 +363,9 @@ tableDesc (PGconn* conn, char* table) ...@@ -355,9 +363,9 @@ tableDesc (PGconn* conn, char* table)
free(newname); free(newname);
} }
if (rsize > 0) if (rsize > 0)
printf ("%-6i |", rsize); printf ("%6i |", rsize);
else else
printf ("%-6s |", "var"); printf ("%6s |", "var");
} }
printf("\n"); printf("\n");
} }
...@@ -396,7 +404,6 @@ gets_readline(char* prompt, FILE* source) ...@@ -396,7 +404,6 @@ gets_readline(char* prompt, FILE* source)
return (readline(prompt)); return (readline(prompt));
} }
/* /*
* gets_fromFile prompt source * gets_fromFile prompt source
* *
...@@ -426,14 +433,12 @@ gets_fromFile(char* prompt, FILE* source) ...@@ -426,14 +433,12 @@ gets_fromFile(char* prompt, FILE* source)
} }
/* /*
* SendQuery: * SendQuery: send the query string to the backend
SendQuery: send the query string to the backend
*
* return 0 if the query executed successfully * return 0 if the query executed successfully
* returns 1 otherwise * returns 1 otherwise
*/ */
int int
SendQuery(PGconn* db, char* query, PsqlSettings *settings) SendQuery(PsqlSettings *settings, char *query)
{ {
PGresult* results; PGresult* results;
PGnotify* notify; PGnotify* notify;
...@@ -454,20 +459,41 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings) ...@@ -454,20 +459,41 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings)
gets_fromFile("",stdin); gets_fromFile("",stdin);
} }
results = PQexec(db, query); results = PQexec(settings->db, query);
if (results == NULL) { if (results == NULL) {
fprintf(stderr,"%s",PQerrorMessage(db)); fprintf(stderr,"%s",PQerrorMessage(settings->db));
return 1; return 1;
} }
switch (PQresultStatus(results)) { switch (PQresultStatus(results)) {
case PGRES_TUPLES_OK: case PGRES_TUPLES_OK:
PQdisplayTuples(results, if (settings->gfname)
settings->queryFout, {
settings->fillAlign, PsqlSettings ps=*settings;
settings->fieldSep, FILE *fp;
settings->printHeader, ps.queryFout=stdout;
settings->quiet); fp=setFout(&ps, settings->gfname);
if (!fp || fp==stdout)
{
status = 1;
break;
}
PQprint(fp,
results,
&(settings->opt));
if (ps.pipe)
pclose(fp);
else
fclose(fp);
settings->gfname=NULL;
break;
} else
{
PQprint(settings->queryFout,
results,
&(settings->opt));
fflush(settings->queryFout);
}
PQclear(results); PQclear(results);
break; break;
case PGRES_EMPTY_QUERY: case PGRES_EMPTY_QUERY:
...@@ -475,7 +501,7 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings) ...@@ -475,7 +501,7 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings)
break; break;
case PGRES_COMMAND_OK: case PGRES_COMMAND_OK:
if (!settings->quiet) if (!settings->quiet)
fprintf(stdout,"%s\n",PQcmdStatus(results)); fprintf(stdout,"%s\n", PQcmdStatus(results));
break; break;
case PGRES_COPY_OUT: case PGRES_COPY_OUT:
handleCopyOut(results, settings->quiet); handleCopyOut(results, settings->quiet);
...@@ -487,13 +513,12 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings) ...@@ -487,13 +513,12 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings)
case PGRES_FATAL_ERROR: case PGRES_FATAL_ERROR:
case PGRES_BAD_RESPONSE: case PGRES_BAD_RESPONSE:
status = 1; status = 1;
fprintf(stderr,"%s",PQerrorMessage(db)); fprintf(stderr,"%s",PQerrorMessage(settings->db));
break; break;
} }
/* check for asynchronous returns */ /* check for asynchronous returns */
notify = PQnotifies(db); notify = PQnotifies(settings->db);
if (notify) { if (notify) {
fprintf(stderr,"ASYNC NOTIFY of '%s' from backend pid '%d' received\n", fprintf(stderr,"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
notify->relname, notify->be_pid); notify->relname, notify->be_pid);
...@@ -504,6 +529,71 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings) ...@@ -504,6 +529,71 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings)
} }
void
editFile(char *fname)
{
char *editorName;
char *sys;
editorName = getenv("EDITOR");
if (!editorName)
editorName = DEFAULT_EDITOR;
sys=malloc(strlen(editorName)+strlen(fname)+32+1);
if (!sys)
{
perror("malloc");
exit(1);
}
sprintf(sys, "exec '%s' '%s'", editorName, fname);
system(sys);
free(sys);
}
bool
toggle(PsqlSettings *settings, bool *sw, char *msg)
{
*sw= !*sw;
if (!settings->quiet)
fprintf(stderr, "turned %s %s\n", on(*sw), msg);
return *sw;
}
char *
decode(char *s)
{
char *p, *d;
bool esc=0;
for (d=p=s; *p; p++)
{
char c=*p;
if (esc)
{
switch(*p)
{
case 'n':
c='\n';
break;
case 'r':
c='\r';
break;
case 't':
c='\t';
break;
case 'f':
c='\f';
break;
}
esc=0;
} else
if (c=='\\')
{
esc=1;
continue;
}
*d++=c;
}
*d='\0';
}
/* /*
HandleSlashCmds: HandleSlashCmds:
...@@ -519,189 +609,161 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings) ...@@ -519,189 +609,161 @@ SendQuery(PGconn* db, char* query, PsqlSettings *settings)
2 - terminate processing of this query entirely 2 - terminate processing of this query entirely
*/ */
int int
HandleSlashCmds(PGconn** db_ptr, HandleSlashCmds(PsqlSettings *settings,
char* line, char* line,
char** prompt_ptr, char *query)
char *query,
PsqlSettings *settings)
{ {
int status = 0; int status = 1;
PGconn* db = *db_ptr;
char* dbname = PQdb(db);
char *optarg = NULL; char *optarg = NULL;
int len; int len;
len = strlen(line); len = strlen(line);
if (len > 2) if (len > 2)
{
optarg = leftTrim(line+2); optarg = leftTrim(line+2);
decode(optarg);
}
switch (line[1]) switch (line[1])
{ {
case 'a': /* toggles to fill fields on output */ case 'a': /* toggles to align fields on output */
if (settings->fillAlign) toggle(settings, &settings->opt.align, "field alignment");
settings->fillAlign = 0; break;
case 'C': /* define new caption */
if (settings->opt.caption)
free(settings->opt.caption);
if (!optarg)
settings->opt.caption=NULL;
else else
settings->fillAlign = 1; if (!(settings->opt.caption=dupstr(optarg)))
if (!settings->quiet) {
fprintf(stderr,"turning %s fill-justification\n", perror("malloc");
(settings->fillAlign) ? "on" : "off" ); exit(1);
}
break; break;
case 'c': /* \c means connect to new database */ case 'c': /* \c means connect to new database */
{ {
char *dbname=PQdb(settings->db);
if (!optarg) { if (!optarg) {
fprintf(stderr,"\\c must be followed by a database name\n"); fprintf(stderr,"\\c must be followed by a database name\n");
status = 1;
break; break;
} }
if (strcmp(optarg, dbname) == 0) { {
fprintf(stderr,"already connected to %s\n", dbname); PGconn *olddb=settings->db;
status = 1;
break;
}
else {
PGconn *olddb;
printf("closing connection to database:%s\n", dbname); printf("closing connection to database: %s\n", dbname);
olddb = db; settings->db = PQsetdb(PQhost(olddb), PQport(olddb), NULL, NULL, optarg);
db = PQsetdb(PQhost(olddb), PQport(olddb), NULL, NULL, optarg);
*db_ptr = db;
printf("connecting to new database: %s\n", optarg); printf("connecting to new database: %s\n", optarg);
if (PQstatus(db) == CONNECTION_BAD) { if (PQstatus(settings->db) == CONNECTION_BAD) {
fprintf(stderr,"%s\n", PQerrorMessage(db)); fprintf(stderr,"%s\n", PQerrorMessage(settings->db));
printf("reconnecting to %s\n", dbname); printf("reconnecting to %s\n", dbname);
db = PQsetdb(PQhost(olddb), PQport(olddb), settings->db = PQsetdb(PQhost(olddb), PQport(olddb),
NULL, NULL, dbname); NULL, NULL, dbname);
*db_ptr = db; if (PQstatus(settings->db) == CONNECTION_BAD) {
if (PQstatus(db) == CONNECTION_BAD) {
fprintf(stderr, fprintf(stderr,
"could not reconnect to %s. exiting\n", dbname); "could not reconnect to %s. exiting\n", dbname);
exit(2); exit(2);
} }
status = 1;
break; break;
} }
PQfinish(olddb); PQfinish(olddb);
free(*prompt_ptr); free(settings->prompt);
*prompt_ptr = malloc(strlen(optarg) + 10); settings->prompt = malloc(strlen(PQdb(settings->db)) + 10);
sprintf(*prompt_ptr,"%s=> ", optarg); sprintf(settings->prompt,"%s=> ", PQdb(settings->db));
status = 1;
break; break;
} }
} }
break; break;
case 'd': /* \d describe tables or columns in a table */ case 'd': /* \d describe tables or columns in a table */
{
if (!optarg) { if (!optarg) {
tableList(db,0); tableList(settings, 0);
status = 1;
break; break;
} }
if ( strcmp(optarg,"*") == 0 ) { if (strcmp(optarg, "*") == 0 ) {
tableList(db, 0); tableList(settings, 0);
tableList(db, 1); tableList(settings, 1);
} }
else { else {
tableDesc(db,optarg); tableDesc(settings, optarg);
} }
status = 1;
break; break;
}
case 'e': case 'e':
{ {
char s[256];
int fd; int fd;
int ql = strlen(query); char tmp[64];
int f_arg = 0; char *fname;
int cc; int cc;
int ql = strlen(query);
if (optarg) if (optarg)
{ fname=optarg;
f_arg = 1;
strcpy(s, optarg);
}
else else
{ {
sprintf(s, "/tmp/psql.%d.%d", geteuid(), getpid()); sprintf(tmp, "/tmp/psql.%d.%d", geteuid(), getpid());
unlink(s); fname=tmp;
unlink(tmp);
if (ql) if (ql)
{ {
if ((fd=open(s, O_EXCL|O_CREAT|O_WRONLY, 0600))==-1) if ((fd=open(tmp, O_EXCL|O_CREAT|O_WRONLY, 0600))==-1)
{ {
perror(s); perror(tmp);
break; break;
} }
if (query[ql-1]!='\n') if (query[ql-1]!='\n')
strcat(query, "\n"); strcat(query, "\n");
if (write(fd, query, ql)!=ql) if (write(fd, query, ql)!=ql)
{ {
perror(s); perror(tmp);
close(fd); close(fd);
unlink(s); unlink(tmp);
break; break;
} }
close(fd); close(fd);
} }
} }
editFile(fname);
if ((fd=open(fname, O_RDONLY))==-1)
{ {
char sys[256]; perror(fname);
char *editorName; if (!optarg)
editorName = getenv("EDITOR"); unlink(fname);
if (editorName == NULL)
editorName = DEFAULT_EDITOR;
sprintf(sys, "exec %s %s", editorName, s);
system(sys);
}
if ((fd=open(s, O_RDONLY))==-1)
{
if (!f_arg)
unlink(s);
break; break;
} }
if ((cc=read(fd, query, MAX_QUERY_BUFFER))==-1) if ((cc=read(fd, query, MAX_QUERY_BUFFER))==-1)
{ {
perror(s); perror(fname);
close(fd); close(fd);
if (!f_arg) if (!optarg)
unlink(s); unlink(fname);
break; break;
} }
query[cc]='\0'; query[cc]='\0';
close(fd); close(fd);
if (!f_arg) if (!optarg)
unlink(s); unlink(fname);
rightTrim(query); rightTrim(query);
if (query[strlen(query)-1]==';') if (query[strlen(query)-1]==';')
return 0; return 0;
break; break;
} }
case 'f': case 'f':
{
char *fs=DEFAULT_FIELD_SEP;
if (optarg) if (optarg)
strcpy(settings->fieldSep,optarg); fs=optarg;
else if (settings->opt.fieldSep);
strcpy(settings->fieldSep,DEFAULT_FIELD_SEP); free(settings->opt.fieldSep);
break; if (!(settings->opt.fieldSep=dupstr(fs)))
case 'g': /* \g means send query */
status = 0;
break;
case 'i': /* \i is include file */
{ {
FILE* fd; perror("malloc");
exit(1);
if (!optarg) {
fprintf(stderr,"\\i must be followed by a file name\n");
status = 1;
break;
} }
if (!settings->quiet)
if ( (fd = fopen(optarg, "r")) == NULL) fprintf(stderr, "field separater changed to '%s'\n", settings->opt.fieldSep);
{
fprintf(stderr,"file named %s could not be opened\n",optarg);
status = 1;
break; break;
} }
MainLoop(&db, fd, settings); case 'g': /* \g means send query */
fclose(fd); settings->gfname = optarg;
status = 1; status = 0;
break; break;
}
case 'h': case 'h':
{ {
char* cmd; char* cmd;
...@@ -727,7 +789,7 @@ HandleSlashCmds(PGconn** db_ptr, ...@@ -727,7 +789,7 @@ HandleSlashCmds(PGconn** db_ptr,
numCmds = numCmds - 1; numCmds = numCmds - 1;
if ( strcmp(cmd,"*") == 0 ) { if (strcmp(cmd, "*") == 0 ) {
all_help=1; all_help=1;
} }
...@@ -748,74 +810,154 @@ HandleSlashCmds(PGconn** db_ptr, ...@@ -748,74 +810,154 @@ HandleSlashCmds(PGconn** db_ptr,
if (i == numCmds && ! all_help) if (i == numCmds && ! all_help)
printf("command not found, try \\h with no arguments to see available help\n"); printf("command not found, try \\h with no arguments to see available help\n");
} }
status = 1; break;
}
case 'i': /* \i is include file */
{
FILE* fd;
if (!optarg) {
fprintf(stderr,"\\i must be followed by a file name\n");
break;
}
if ((fd = fopen(optarg, "r")) == NULL)
{
fprintf(stderr,"file named %s could not be opened\n",optarg);
break;
}
MainLoop(settings, fd);
fclose(fd);
break; break;
} }
case 'l': /* \l is list database */ case 'l': /* \l is list database */
listAllDbs(db,settings); listAllDbs(settings);
status = 1; break;
case 'H':
if (toggle(settings, &settings->opt.html3, "HTML3.0 tablular output"))
settings->opt.standard = 0;
break; break;
case 'o': case 'o':
settings->queryFout = setFout(optarg); setFout(settings, optarg);
break; break;
case 'p': case 'p':
if (query) { if (query)
{
fputs(query, stdout); fputs(query, stdout);
fputc('\n', stdout); fputc('\n', stdout);
status = 1;
} }
break; break;
case 'q': /* \q is quit */ case 'q': /* \q is quit */
status = 2; status = 2;
break; break;
case 'r': case 'r':
query[0] = '\0'; {
status = 1; FILE* fd;
static char *lastfile;
struct stat st, st2;
if (optarg)
{
if (lastfile)
free(lastfile);
lastfile=malloc(strlen(optarg+1));
if (!lastfile)
{
perror("malloc");
exit(1);
}
strcpy(lastfile, optarg);
} else if (!lastfile)
{
fprintf(stderr,"\\r must be followed by a file name initially\n");
break; break;
case 's': /* \s is save history to a file */ }
stat(lastfile, &st);
editFile(lastfile);
if ((stat(lastfile, &st2) == -1) || ((fd = fopen(lastfile, "r")) == NULL))
{ {
char* fname; perror(lastfile);
if (!optarg) {
fprintf(stderr,"\\s must be followed by a file name\n");
status = 1;
break; break;
} }
if (st2.st_mtime==st.st_mtime)
fname = optarg;
if (write_history(fname) != 0)
{ {
fprintf(stderr,"cannot write history to %s\n",fname); if (!settings->quiet)
fprintf(stderr, "warning: %s not modified. query not executed\n", lastfile);
fclose(fd);
break;
} }
status = 1; MainLoop(settings, fd);
fclose(fd);
break; break;
} }
case 't': case 's': /* \s is save history to a file */
if ( settings->printHeader ) if (!optarg)
settings->printHeader = 0; optarg="/dev/tty";
if (write_history(optarg) != 0)
fprintf(stderr,"cannot write history to %s\n",optarg);
break;
case 'm': /* monitor like type-setting */
if (toggle(settings, &settings->opt.standard, "standard SQL separaters and padding"))
{
settings->opt.html3 = settings->opt.expanded = 0;
settings->opt.align = settings->opt.header = 1;
free(settings->opt.fieldSep);
settings->opt.fieldSep=dupstr("|");
if (!settings->quiet)
fprintf(stderr, "field separater changed to '%s'\n", settings->opt.fieldSep);
} else
{
free(settings->opt.fieldSep);
settings->opt.fieldSep=dupstr(DEFAULT_FIELD_SEP);
if (!settings->quiet)
fprintf(stderr, "field separater changed to '%s'\n", settings->opt.fieldSep);
}
break;
case 't': /* toggle headers */
toggle(settings, &settings->opt.header, "output headers");
break;
case 'T': /* define html <table ...> option */
if (settings->opt.tableOpt)
free(settings->opt.tableOpt);
if (!optarg)
settings->opt.tableOpt=NULL;
else else
settings->printHeader = 1; if (!(settings->opt.tableOpt=dupstr(optarg)))
{
perror("malloc");
exit(1);
}
break;
case 'x':
toggle(settings, &settings->opt.expanded, "expanded table representation");
break;
case 'z': /* zorch buffer */
query[0]='\0';
if (!settings->quiet) if (!settings->quiet)
fprintf(stderr,"turning %s printing of field headers\n", fprintf(stderr, "zorched current query buffer\n");
(settings->printHeader) ? "on" : "off" );
break; break;
case '!': case '!':
if (!optarg) { if (!optarg) {
char sys[256]; char *sys;
char *shellName; char *shellName;
shellName = getenv("SHELL"); shellName = getenv("SHELL");
if (shellName == NULL) if (shellName == NULL)
shellName = DEFAULT_SHELL; shellName = DEFAULT_SHELL;
sys = malloc(strlen(shellName)+16);
if (!sys)
{
perror("malloc");
exit(1);
}
sprintf(sys,"exec %s", shellName); sprintf(sys,"exec %s", shellName);
system(sys); system(sys);
free(sys);
} }
else else
system(optarg); system(optarg);
break; break;
default: default:
case '?': /* \? is help */ case '?': /* \? is help */
slashUsage(); slashUsage(settings);
status = 1;
break; break;
} }
return status; return status;
...@@ -830,19 +972,14 @@ HandleSlashCmds(PGconn** db_ptr, ...@@ -830,19 +972,14 @@ HandleSlashCmds(PGconn** db_ptr,
*db_ptr must be initialized and set *db_ptr must be initialized and set
*/ */
int int
MainLoop(PGconn** db_ptr, MainLoop(PsqlSettings *settings, FILE* source)
FILE* source,
PsqlSettings *settings)
{ {
char* prompt; /* readline prompt */
char* line; /* line of input*/ char* line; /* line of input*/
int len; /* length of the line */ int len; /* length of the line */
char query[MAX_QUERY_BUFFER]; /* multi-line query storage */ char query[MAX_QUERY_BUFFER]; /* multi-line query storage */
PGconn* db = *db_ptr;
char* dbname = PQdb(db);
int exitStatus = 0; int exitStatus = 0;
int slashCmdStatus = 0; int slashCmdStatus = 0;
/* slashCmdStatus can be: /* slashCmdStatus can be:
0 - send currently constructed query to backend (i.e. we got a \g) 0 - send currently constructed query to backend (i.e. we got a \g)
...@@ -850,18 +987,21 @@ MainLoop(PGconn** db_ptr, ...@@ -850,18 +987,21 @@ MainLoop(PGconn** db_ptr,
2 - terminate processing of this query entirely 2 - terminate processing of this query entirely
*/ */
int send_query = 0; bool sendQuery = 0;
int interactive; bool querySent = 0;
bool interactive;
READ_ROUTINE GetNextLine; READ_ROUTINE GetNextLine;
interactive = (source == stdin); interactive = ((source == stdin) && !settings->notty);
#define PROMPT "=> "
if (interactive) { if (interactive) {
prompt = malloc(strlen(dbname) + 10); if (settings->prompt)
free(settings->prompt);
settings->prompt = malloc(strlen(PQdb(settings->db)) + strlen(PROMPT) + 1);
if (settings->quiet) if (settings->quiet)
prompt[0] = '\0'; settings->prompt[0] = '\0';
else else
sprintf(prompt,"%s=> ", dbname); sprintf(settings->prompt,"%s%s", PQdb(settings->db), PROMPT);
if (settings->useReadline) { if (settings->useReadline) {
using_history(); using_history();
GetNextLine = gets_readline; GetNextLine = gets_readline;
...@@ -875,7 +1015,7 @@ MainLoop(PGconn** db_ptr, ...@@ -875,7 +1015,7 @@ MainLoop(PGconn** db_ptr,
query[0] = '\0'; query[0] = '\0';
/* main loop for getting queries and executing them */ /* main loop for getting queries and executing them */
while ((line = GetNextLine(prompt, source)) != NULL) while ((line = GetNextLine(settings->prompt, source)) != NULL)
{ {
exitStatus = 0; exitStatus = 0;
line = rightTrim(line); /* remove whitespaces on the right, incl. \n's */ line = rightTrim(line); /* remove whitespaces on the right, incl. \n's */
...@@ -895,6 +1035,11 @@ MainLoop(PGconn** db_ptr, ...@@ -895,6 +1035,11 @@ MainLoop(PGconn** db_ptr,
free(line); free(line);
continue; continue;
} }
if (line[0] != '\\' && querySent)
{
query[0]='\0';
querySent = 0;
}
len = strlen(line); len = strlen(line);
...@@ -903,34 +1048,31 @@ MainLoop(PGconn** db_ptr, ...@@ -903,34 +1048,31 @@ MainLoop(PGconn** db_ptr,
/* do the query immediately if we are doing single line queries /* do the query immediately if we are doing single line queries
or if the last character is a semicolon */ or if the last character is a semicolon */
send_query = settings->singleLineMode || (line[len-1] == ';') ; sendQuery = settings->singleLineMode || (line[len-1] == ';') ;
/* normally, \ commands have to be start the line, /* normally, \ commands have to be start the line,
but for backwards compatibility with monitor, but for backwards compatibility with monitor,
check for \g at the end of line */ check for \g at the end of line */
if (len > 2 && !send_query) if (len > 2 && !sendQuery)
{ {
if (line[len-1]=='g' && line[len-2]=='\\') if (line[len-1]=='g' && line[len-2]=='\\')
{ {
send_query = 1; sendQuery = 1;
line[len-2]='\0'; line[len-2]='\0';
} }
} }
/* slash commands have to be on their own line */ /* slash commands have to be on their own line */
if (line[0] == '\\') { if (line[0] == '\\') {
slashCmdStatus = HandleSlashCmds(db_ptr, slashCmdStatus = HandleSlashCmds(settings,
line, line,
&prompt, query);
query,
settings);
db = *db_ptr; /* in case \c changed the database */
if (slashCmdStatus == 1) if (slashCmdStatus == 1)
continue; continue;
if (slashCmdStatus == 2) if (slashCmdStatus == 2)
break; break;
if (slashCmdStatus == 0) if (slashCmdStatus == 0)
send_query = 1; sendQuery = 1;
} }
else else
if (strlen(query) + len > MAX_QUERY_BUFFER) if (strlen(query) + len > MAX_QUERY_BUFFER)
...@@ -946,16 +1088,16 @@ MainLoop(PGconn** db_ptr, ...@@ -946,16 +1088,16 @@ MainLoop(PGconn** db_ptr,
else else
strcpy(query,line); strcpy(query,line);
if (send_query && query[0] != '\0') if (sendQuery && query[0] != '\0')
{ {
/* echo the line read from the file, /* echo the line read from the file,
unless we are in single_step mode, because single_step mode unless we are in single_step mode, because single_step mode
will echo anyway */ will echo anyway */
if (!interactive && !settings->singleStep) if (!interactive && !settings->singleStep && !settings->quiet)
fprintf(stderr,"%s\n",query); fprintf(stderr,"%s\n", query);
exitStatus = SendQuery(db, query, settings); exitStatus = SendQuery(settings, query);
query[0] = '\0'; querySent = 1;
} }
free(line); /* free storage malloc'd by GetNextLine */ free(line); /* free storage malloc'd by GetNextLine */
...@@ -967,9 +1109,8 @@ int ...@@ -967,9 +1109,8 @@ int
main(int argc, char** argv) main(int argc, char** argv)
{ {
extern char* optarg; extern char* optarg;
extern int optind, opterr; extern int optind;
PGconn *db;
char* dbname = NULL; char* dbname = NULL;
char* host = NULL; char* host = NULL;
char* port = NULL; char* port = NULL;
...@@ -980,31 +1121,27 @@ main(int argc, char** argv) ...@@ -980,31 +1121,27 @@ main(int argc, char** argv)
char* singleQuery = NULL; char* singleQuery = NULL;
int listDatabases = 0 ; bool listDatabases = 0 ;
int exitStatus = 0; int exitStatus = 0;
int singleSlashCmd = 0; bool singleSlashCmd = 0;
int c; int c;
memset(&settings, 0, sizeof settings);
#ifdef NOREADLINE settings.opt.align = 1;
settings.useReadline = 0; settings.opt.header = 1;
#else settings.queryFout = stdout;
settings.opt.fieldSep=dupstr(DEFAULT_FIELD_SEP);
if (!isatty(0) || !isatty(1))
settings.quiet = settings.notty = 1;
else
#ifndef NOREADLINE
settings.useReadline = 1; settings.useReadline = 1;
#endif #endif
settings.quiet = 0; while ((c = getopt(argc, argv, "Aa:c:d:ef:F:lh:Hnso:p:qStT:x")) != EOF) {
settings.fillAlign = 1;
settings.printHeader = 1;
settings.echoQuery = 0;
settings.singleStep = 0;
settings.singleLineMode = 0;
settings.queryFout = stdout;
strcpy(settings.fieldSep, DEFAULT_FIELD_SEP);
while ((c = getopt(argc, argv, "Aa:c:d:ef:F:lhH:nso:p:qST")) != EOF) {
switch (c) { switch (c) {
case 'A': case 'A':
settings.fillAlign = 0; settings.opt.align = 0;
break; break;
case 'a': case 'a':
fe_setauthsvc(optarg, errbuf); fe_setauthsvc(optarg, errbuf);
...@@ -1025,19 +1162,22 @@ main(int argc, char** argv) ...@@ -1025,19 +1162,22 @@ main(int argc, char** argv)
qfilename = optarg; qfilename = optarg;
break; break;
case 'F': case 'F':
strncpy(settings.fieldSep,optarg,MAX_FIELD_SEP_LENGTH); settings.opt.fieldSep=dupstr(optarg);
break; break;
case 'l': case 'l':
listDatabases = 1; listDatabases = 1;
break; break;
case 'H': case 'h':
host = optarg; host = optarg;
break; break;
case 'H':
settings.opt.html3 = 1;
break;
case 'n': case 'n':
settings.useReadline = 0; settings.useReadline = 0;
break; break;
case 'o': case 'o':
settings.queryFout = setFout(optarg); setFout(&settings, optarg);
break; break;
case 'p': case 'p':
port = optarg; port = optarg;
...@@ -1051,10 +1191,15 @@ main(int argc, char** argv) ...@@ -1051,10 +1191,15 @@ main(int argc, char** argv)
case 'S': case 'S':
settings.singleLineMode = 1; settings.singleLineMode = 1;
break; break;
case 't':
settings.opt.header = 0;
break;
case 'T': case 'T':
settings.printHeader = 0; settings.opt.tableOpt = dupstr(optarg);
break;
case 'x':
settings.opt.expanded = 0;
break; break;
case 'h':
default: default:
usage(argv[0]); usage(argv[0]);
break; break;
...@@ -1067,16 +1212,16 @@ main(int argc, char** argv) ...@@ -1067,16 +1212,16 @@ main(int argc, char** argv)
if (listDatabases) if (listDatabases)
dbname = "template1"; dbname = "template1";
db = PQsetdb(host, port, NULL, NULL, dbname); settings.db = PQsetdb(host, port, NULL, NULL, dbname);
dbname = PQdb(db); dbname = PQdb(settings.db);
if (PQstatus(db) == CONNECTION_BAD) { if (PQstatus(settings.db) == CONNECTION_BAD) {
fprintf(stderr,"Connection to database '%s' failed.\n", dbname); fprintf(stderr,"Connection to database '%s' failed.\n", dbname);
fprintf(stderr,"%s",PQerrorMessage(db)); fprintf(stderr,"%s",PQerrorMessage(settings.db));
exit(1); exit(1);
} }
if (listDatabases) { if (listDatabases) {
exit(listAllDbs(db,&settings)); exit(listAllDbs(&settings));
} }
if (!settings.quiet && !singleQuery && !qfilename) { if (!settings.quiet && !singleQuery && !qfilename) {
...@@ -1092,7 +1237,6 @@ main(int argc, char** argv) ...@@ -1092,7 +1237,6 @@ main(int argc, char** argv)
/* read in a file full of queries instead of reading in queries /* read in a file full of queries instead of reading in queries
interactively */ interactively */
char *line; char *line;
char prompt[100];
if ( singleSlashCmd ) { if ( singleSlashCmd ) {
/* Not really a query, but "Do what I mean, not what I say." */ /* Not really a query, but "Do what I mean, not what I say." */
...@@ -1102,24 +1246,25 @@ main(int argc, char** argv) ...@@ -1102,24 +1246,25 @@ main(int argc, char** argv)
line = malloc(strlen(qfilename) + 5); line = malloc(strlen(qfilename) + 5);
sprintf(line,"\\i %s", qfilename); sprintf(line,"\\i %s", qfilename);
} }
HandleSlashCmds(&db, line, (char**)prompt, "", &settings); HandleSlashCmds(&settings, line, "");
} else { } else {
if (singleQuery) { if (singleQuery) {
exitStatus = SendQuery(db, singleQuery, &settings); exitStatus = SendQuery(&settings, singleQuery);
} }
else else
exitStatus = MainLoop(&db, stdin, &settings); exitStatus = MainLoop(&settings, stdin);
} }
PQfinish(db); PQfinish(settings.db);
return exitStatus; return exitStatus;
} }
#define COPYBUFSIZ 8192
static void static void
handleCopyOut(PGresult *res, int quiet) handleCopyOut(PGresult *res, bool quiet)
{ {
bool copydone = false; bool copydone = false;
char copybuf[COPYBUFSIZ]; char copybuf[COPYBUFSIZ];
...@@ -1153,7 +1298,7 @@ handleCopyOut(PGresult *res, int quiet) ...@@ -1153,7 +1298,7 @@ handleCopyOut(PGresult *res, int quiet)
static void static void
handleCopyIn(PGresult *res, int quiet) handleCopyIn(PGresult *res, bool quiet)
{ {
bool copydone = false; bool copydone = false;
bool firstload; bool firstload;
...@@ -1212,24 +1357,38 @@ handleCopyIn(PGresult *res, int quiet) ...@@ -1212,24 +1357,38 @@ handleCopyIn(PGresult *res, int quiet)
PQendcopy(res->conn); PQendcopy(res->conn);
} }
/* try to open fname and return a FILE*, /* try to open fname and return a FILE*,
if it fails, use stdout, instead */ if it fails, use stdout, instead */
FILE* FILE*
setFout(char *fname) setFout(PsqlSettings *ps, char *fname)
{ {
FILE *queryFout; if (ps->queryFout && ps->queryFout != stdout)
{
if (ps->pipe)
pclose(ps->queryFout);
else
fclose(ps->queryFout);
}
if (!fname) if (!fname)
queryFout = stdout; ps->queryFout = stdout;
else { else
queryFout = fopen(fname, "w"); {
if (!queryFout) { if (*fname == '|')
{
signal(SIGPIPE, SIG_IGN);
ps->queryFout = popen(fname+1, "w");
ps->pipe = 1;
}
else
{
ps->queryFout = fopen(fname, "w");
ps->pipe = 0;
}
if (!ps->queryFout) {
perror(fname); perror(fname);
queryFout = stdout; ps->queryFout = stdout;
} }
} }
return ps->queryFout;
return queryFout;
} }
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