#include "print.h"
#include "psqlscan.h"
#include "print.h"
Go to the source code of this file.
Typedefs | |
typedef enum _backslashResult | backslashResult |
Enumerations | |
enum | _backslashResult { PSQL_CMD_UNKNOWN = 0, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, PSQL_CMD_TERMINATE, PSQL_CMD_NEWEDIT, PSQL_CMD_ERROR } |
Functions | |
backslashResult | HandleSlashCmds (PsqlScanState scan_state, PQExpBuffer query_buf) |
int | process_file (char *filename, bool single_txn, bool use_relative_path) |
bool | do_pset (const char *param, const char *value, printQueryOpt *popt, bool quiet) |
void | connection_warnings (bool in_startup) |
void | SyncVariables (void) |
void | UnsyncVariables (void) |
typedef enum _backslashResult backslashResult |
enum _backslashResult |
PSQL_CMD_UNKNOWN | |
PSQL_CMD_SEND | |
PSQL_CMD_SKIP_LINE | |
PSQL_CMD_TERMINATE | |
PSQL_CMD_NEWEDIT | |
PSQL_CMD_ERROR |
Definition at line 15 of file command.h.
{ PSQL_CMD_UNKNOWN = 0, /* not done parsing yet (internal only) */ PSQL_CMD_SEND, /* query complete; send off */ PSQL_CMD_SKIP_LINE, /* keep building query */ PSQL_CMD_TERMINATE, /* quit program */ PSQL_CMD_NEWEDIT, /* query buffer was changed (e.g., via \e) */ PSQL_CMD_ERROR /* the execution of the backslash command * resulted in an error */ } backslashResult;
void connection_warnings | ( | bool | in_startup | ) |
Definition at line 1732 of file command.c.
References _, _psqlSettings::db, _psqlSettings::notty, PQparameterStatus(), printSSLInfo(), _psqlSettings::progname, pset, _psqlSettings::quiet, server_version, snprintf(), and _psqlSettings::sversion.
Referenced by do_connect(), and main().
{ if (!pset.quiet && !pset.notty) { int client_ver = PG_VERSION_NUM; if (pset.sversion != client_ver) { const char *server_version; char server_ver_str[16]; /* Try to get full text form, might include "devel" etc */ server_version = PQparameterStatus(pset.db, "server_version"); if (!server_version) { snprintf(server_ver_str, sizeof(server_ver_str), "%d.%d.%d", pset.sversion / 10000, (pset.sversion / 100) % 100, pset.sversion % 100); server_version = server_ver_str; } printf(_("%s (%s, server %s)\n"), pset.progname, PG_VERSION, server_version); } /* For version match, only print psql banner on startup. */ else if (in_startup) printf("%s (%s)\n", pset.progname, PG_VERSION); if (pset.sversion / 100 > client_ver / 100) printf(_("WARNING: %s major version %d.%d, server major version %d.%d.\n" " Some psql features might not work.\n"), pset.progname, client_ver / 10000, (client_ver / 100) % 100, pset.sversion / 10000, (pset.sversion / 100) % 100); #ifdef WIN32 checkWin32Codepage(); #endif printSSLInfo(); } }
bool do_pset | ( | const char * | param, | |
const char * | value, | |||
printQueryOpt * | popt, | |||
bool | quiet | |||
) |
Definition at line 2244 of file command.c.
References _, _align2string(), Assert, printTableOpt::border, printTableOpt::columns, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::fieldSep, printTableOpt::format, free, get_line_style(), printTableOpt::line_style, printTextFormat::name, NULL, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, ParseVariableBool(), pg_asciiformat, pg_asciiformat_old, pg_strcasecmp(), pg_strdup(), pg_strncasecmp(), pg_utf8format, psql_error(), printTableOpt::recordSep, separator::separator, separator::separator_zero, printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, and printTableOpt::tuples_only.
Referenced by exec_command(), and parse_psql_options().
{ size_t vallen = 0; Assert(param != NULL); if (value) vallen = strlen(value); /* set format */ if (strcmp(param, "format") == 0) { if (!value) ; else if (pg_strncasecmp("unaligned", value, vallen) == 0) popt->topt.format = PRINT_UNALIGNED; else if (pg_strncasecmp("aligned", value, vallen) == 0) popt->topt.format = PRINT_ALIGNED; else if (pg_strncasecmp("wrapped", value, vallen) == 0) popt->topt.format = PRINT_WRAPPED; else if (pg_strncasecmp("html", value, vallen) == 0) popt->topt.format = PRINT_HTML; else if (pg_strncasecmp("latex", value, vallen) == 0) popt->topt.format = PRINT_LATEX; else if (pg_strncasecmp("latex-longtable", value, vallen) == 0) popt->topt.format = PRINT_LATEX_LONGTABLE; else if (pg_strncasecmp("troff-ms", value, vallen) == 0) popt->topt.format = PRINT_TROFF_MS; else { psql_error("\\pset: allowed formats are unaligned, aligned, wrapped, html, latex, troff-ms\n"); return false; } if (!quiet) printf(_("Output format is %s.\n"), _align2string(popt->topt.format)); } /* set table line style */ else if (strcmp(param, "linestyle") == 0) { if (!value) ; else if (pg_strncasecmp("ascii", value, vallen) == 0) popt->topt.line_style = &pg_asciiformat; else if (pg_strncasecmp("old-ascii", value, vallen) == 0) popt->topt.line_style = &pg_asciiformat_old; else if (pg_strncasecmp("unicode", value, vallen) == 0) popt->topt.line_style = &pg_utf8format; else { psql_error("\\pset: allowed line styles are ascii, old-ascii, unicode\n"); return false; } if (!quiet) printf(_("Line style is %s.\n"), get_line_style(&popt->topt)->name); } /* set border style/width */ else if (strcmp(param, "border") == 0) { if (value) popt->topt.border = atoi(value); if (!quiet) printf(_("Border style is %d.\n"), popt->topt.border); } /* set expanded/vertical mode */ else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0) { if (value && pg_strcasecmp(value, "auto") == 0) popt->topt.expanded = 2; else if (value) popt->topt.expanded = ParseVariableBool(value); else popt->topt.expanded = !popt->topt.expanded; if (!quiet) { if (popt->topt.expanded == 1) printf(_("Expanded display is on.\n")); else if (popt->topt.expanded == 2) printf(_("Expanded display is used automatically.\n")); else printf(_("Expanded display is off.\n")); } } /* locale-aware numeric output */ else if (strcmp(param, "numericlocale") == 0) { if (value) popt->topt.numericLocale = ParseVariableBool(value); else popt->topt.numericLocale = !popt->topt.numericLocale; if (!quiet) { if (popt->topt.numericLocale) puts(_("Showing locale-adjusted numeric output.")); else puts(_("Locale-adjusted numeric output is off.")); } } /* null display */ else if (strcmp(param, "null") == 0) { if (value) { free(popt->nullPrint); popt->nullPrint = pg_strdup(value); } if (!quiet) printf(_("Null display is \"%s\".\n"), popt->nullPrint ? popt->nullPrint : ""); } /* field separator for unaligned text */ else if (strcmp(param, "fieldsep") == 0) { if (value) { free(popt->topt.fieldSep.separator); popt->topt.fieldSep.separator = pg_strdup(value); popt->topt.fieldSep.separator_zero = false; } if (!quiet) { if (popt->topt.fieldSep.separator_zero) printf(_("Field separator is zero byte.\n")); else printf(_("Field separator is \"%s\".\n"), popt->topt.fieldSep.separator); } } else if (strcmp(param, "fieldsep_zero") == 0) { free(popt->topt.fieldSep.separator); popt->topt.fieldSep.separator = NULL; popt->topt.fieldSep.separator_zero = true; if (!quiet) printf(_("Field separator is zero byte.\n")); } /* record separator for unaligned text */ else if (strcmp(param, "recordsep") == 0) { if (value) { free(popt->topt.recordSep.separator); popt->topt.recordSep.separator = pg_strdup(value); popt->topt.recordSep.separator_zero = false; } if (!quiet) { if (popt->topt.recordSep.separator_zero) printf(_("Record separator is zero byte.\n")); else if (strcmp(popt->topt.recordSep.separator, "\n") == 0) printf(_("Record separator is <newline>.")); else printf(_("Record separator is \"%s\".\n"), popt->topt.recordSep.separator); } } else if (strcmp(param, "recordsep_zero") == 0) { free(popt->topt.recordSep.separator); popt->topt.recordSep.separator = NULL; popt->topt.recordSep.separator_zero = true; if (!quiet) printf(_("Record separator is zero byte.\n")); } /* toggle between full and tuples-only format */ else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0) { if (value) popt->topt.tuples_only = ParseVariableBool(value); else popt->topt.tuples_only = !popt->topt.tuples_only; if (!quiet) { if (popt->topt.tuples_only) puts(_("Showing only tuples.")); else puts(_("Tuples only is off.")); } } /* set title override */ else if (strcmp(param, "title") == 0) { free(popt->title); if (!value) popt->title = NULL; else popt->title = pg_strdup(value); if (!quiet) { if (popt->title) printf(_("Title is \"%s\".\n"), popt->title); else printf(_("Title is unset.\n")); } } /* set HTML table tag options */ else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0) { free(popt->topt.tableAttr); if (!value) popt->topt.tableAttr = NULL; else popt->topt.tableAttr = pg_strdup(value); if (!quiet) { if (popt->topt.tableAttr) printf(_("Table attribute is \"%s\".\n"), popt->topt.tableAttr); else printf(_("Table attributes unset.\n")); } } /* toggle use of pager */ else if (strcmp(param, "pager") == 0) { if (value && pg_strcasecmp(value, "always") == 0) popt->topt.pager = 2; else if (value) if (ParseVariableBool(value)) popt->topt.pager = 1; else popt->topt.pager = 0; else if (popt->topt.pager == 1) popt->topt.pager = 0; else popt->topt.pager = 1; if (!quiet) { if (popt->topt.pager == 1) puts(_("Pager is used for long output.")); else if (popt->topt.pager == 2) puts(_("Pager is always used.")); else puts(_("Pager usage is off.")); } } /* disable "(x rows)" footer */ else if (strcmp(param, "footer") == 0) { if (value) popt->topt.default_footer = ParseVariableBool(value); else popt->topt.default_footer = !popt->topt.default_footer; if (!quiet) { if (popt->topt.default_footer) puts(_("Default footer is on.")); else puts(_("Default footer is off.")); } } /* set border style/width */ else if (strcmp(param, "columns") == 0) { if (value) popt->topt.columns = atoi(value); if (!quiet) printf(_("Target width is %d.\n"), popt->topt.columns); } else { psql_error("\\pset: unknown option: %s\n", param); return false; } return true; }
backslashResult HandleSlashCmds | ( | PsqlScanState | scan_state, | |
PQExpBuffer | query_buf | |||
) |
Definition at line 97 of file command.c.
References arg, Assert, _psqlSettings::cur_cmd_interactive, exec_command(), free, NULL, OT_NO_EVAL, OT_WHOLE_LINE, pset, PSQL_CMD_ERROR, PSQL_CMD_UNKNOWN, psql_error(), psql_scan_slash_command(), psql_scan_slash_command_end(), psql_scan_slash_option(), _psqlSettings::queryFout, and status().
Referenced by main(), and MainLoop().
{ backslashResult status = PSQL_CMD_SKIP_LINE; char *cmd; char *arg; Assert(scan_state != NULL); /* Parse off the command name */ cmd = psql_scan_slash_command(scan_state); /* And try to execute it */ status = exec_command(cmd, scan_state, query_buf); if (status == PSQL_CMD_UNKNOWN) { if (pset.cur_cmd_interactive) psql_error("Invalid command \\%s. Try \\? for help.\n", cmd); else psql_error("invalid command \\%s\n", cmd); status = PSQL_CMD_ERROR; } if (status != PSQL_CMD_ERROR) { /* eat any remaining arguments after a valid command */ /* note we suppress evaluation of backticks here */ while ((arg = psql_scan_slash_option(scan_state, OT_NO_EVAL, NULL, false))) { psql_error("\\%s: extra argument \"%s\" ignored\n", cmd, arg); free(arg); } } else { /* silently throw away rest of line after an erroneous command */ while ((arg = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, false))) free(arg); } /* if there is a trailing \\, swallow it */ psql_scan_slash_command_end(scan_state); free(cmd); /* some commands write to queryFout, so make sure output is sent */ fflush(pset.queryFout); return status; }
Definition at line 2114 of file command.c.
References canonicalize_path(), error(), get_parent_directory(), has_drive_prefix(), _psqlSettings::inputfile, is_absolute_path, join_path_components(), MainLoop(), NULL, _psqlSettings::on_error_stop, PG_BINARY_R, PQclear(), pset, psql_error(), PSQLexec(), relpath, strerror(), and strlcpy().
{ FILE *fd; int result; char *oldfilename; char relpath[MAXPGPATH]; PGresult *res; if (!filename) { fd = stdin; filename = NULL; } else if (strcmp(filename, "-") != 0) { canonicalize_path(filename); /* * If we were asked to resolve the pathname relative to the location * of the currently executing script, and there is one, and this is a * relative pathname, then prepend all but the last pathname component * of the current script to this pathname. */ if (use_relative_path && pset.inputfile && !is_absolute_path(filename) && !has_drive_prefix(filename)) { strlcpy(relpath, pset.inputfile, sizeof(relpath)); get_parent_directory(relpath); join_path_components(relpath, relpath, filename); canonicalize_path(relpath); filename = relpath; } fd = fopen(filename, PG_BINARY_R); if (!fd) { psql_error("%s: %s\n", filename, strerror(errno)); return EXIT_FAILURE; } } else { fd = stdin; filename = "<stdin>"; /* for future error messages */ } oldfilename = pset.inputfile; pset.inputfile = filename; if (single_txn) { if ((res = PSQLexec("BEGIN", false)) == NULL) { if (pset.on_error_stop) { result = EXIT_USER; goto error; } } else PQclear(res); } result = MainLoop(fd); if (single_txn) { if ((res = PSQLexec("COMMIT", false)) == NULL) { if (pset.on_error_stop) { result = EXIT_USER; goto error; } } else PQclear(res); } error: if (fd != stdin) fclose(fd); pset.inputfile = oldfilename; return result; }
void SyncVariables | ( | void | ) |
Definition at line 1840 of file command.c.
References _psqlSettings::db, printTableOpt::encoding, _psqlSettings::encoding, pg_encoding_to_char(), _psqlSettings::popt, PQclientEncoding(), PQdb(), PQhost(), PQport(), PQserverVersion(), PQsetErrorVerbosity(), PQuser(), pset, SetVariable(), _psqlSettings::sversion, printQueryOpt::topt, _psqlSettings::vars, and _psqlSettings::verbosity.
Referenced by do_connect(), and main().
{ /* get stuff from connection */ pset.encoding = PQclientEncoding(pset.db); pset.popt.topt.encoding = pset.encoding; pset.sversion = PQserverVersion(pset.db); SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); SetVariable(pset.vars, "USER", PQuser(pset.db)); SetVariable(pset.vars, "HOST", PQhost(pset.db)); SetVariable(pset.vars, "PORT", PQport(pset.db)); SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding)); /* send stuff to it, too */ PQsetErrorVerbosity(pset.db, pset.verbosity); }
void UnsyncVariables | ( | void | ) |
Definition at line 1863 of file command.c.
References NULL, pset, SetVariable(), and _psqlSettings::vars.
Referenced by CheckConnection().
{ SetVariable(pset.vars, "DBNAME", NULL); SetVariable(pset.vars, "USER", NULL); SetVariable(pset.vars, "HOST", NULL); SetVariable(pset.vars, "PORT", NULL); SetVariable(pset.vars, "ENCODING", NULL); }