1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright (c) 2000-2017, PostgreSQL Global Development Group
5  *
6  * src/bin/psql/settings.h
7  */
8 #ifndef SETTINGS_H
9 #define SETTINGS_H
10 
11 
12 #include "variables.h"
13 #include "fe_utils/print.h"
14 
15 #define DEFAULT_FIELD_SEP "|"
16 #define DEFAULT_RECORD_SEP "\n"
17 
18 #if defined(WIN32) || defined(__CYGWIN__)
19 #define DEFAULT_EDITOR	"notepad.exe"
20 /* no DEFAULT_EDITOR_LINENUMBER_ARG for Notepad */
21 #else
22 #define DEFAULT_EDITOR	"vi"
23 #define DEFAULT_EDITOR_LINENUMBER_ARG "+"
24 #endif
25 
26 #define DEFAULT_PROMPT1 "%/%R%# "
27 #define DEFAULT_PROMPT2 "%/%R%# "
28 #define DEFAULT_PROMPT3 ">> "
29 
30 /*
31  * Note: these enums should generally be chosen so that zero corresponds
32  * to the default behavior.
33  */
34 
35 typedef enum
36 {
37 	PSQL_ECHO_NONE,
38 	PSQL_ECHO_QUERIES,
39 	PSQL_ECHO_ERRORS,
40 	PSQL_ECHO_ALL
41 } PSQL_ECHO;
42 
43 typedef enum
44 {
45 	PSQL_ECHO_HIDDEN_OFF,
46 	PSQL_ECHO_HIDDEN_ON,
47 	PSQL_ECHO_HIDDEN_NOEXEC
48 } PSQL_ECHO_HIDDEN;
49 
50 typedef enum
51 {
52 	PSQL_ERROR_ROLLBACK_OFF,
53 	PSQL_ERROR_ROLLBACK_INTERACTIVE,
54 	PSQL_ERROR_ROLLBACK_ON
55 } PSQL_ERROR_ROLLBACK;
56 
57 typedef enum
58 {
59 	PSQL_COMP_CASE_PRESERVE_UPPER,
60 	PSQL_COMP_CASE_PRESERVE_LOWER,
61 	PSQL_COMP_CASE_UPPER,
62 	PSQL_COMP_CASE_LOWER
63 } PSQL_COMP_CASE;
64 
65 typedef enum
66 {
67 	hctl_none = 0,
68 	hctl_ignorespace = 1,
69 	hctl_ignoredups = 2,
70 	hctl_ignoreboth = hctl_ignorespace | hctl_ignoredups
71 } HistControl;
72 
73 enum trivalue
74 {
75 	TRI_DEFAULT,
76 	TRI_NO,
77 	TRI_YES
78 };
79 
80 typedef struct _psqlSettings
81 {
82 	PGconn	   *db;				/* connection to backend */
83 	int			encoding;		/* client_encoding */
84 	FILE	   *queryFout;		/* where to send the query results */
85 	bool		queryFoutPipe;	/* queryFout is from a popen() */
86 
87 	FILE	   *copyStream;		/* Stream to read/write for \copy command */
88 
89 	PGresult   *last_error_result;	/* most recent error result, if any */
90 
91 	printQueryOpt popt;
92 
93 	char	   *gfname;			/* one-shot file output argument for \g */
94 	bool		g_expanded;		/* one-shot expanded output requested via \gx */
95 	char	   *gset_prefix;	/* one-shot prefix argument for \gset */
96 	bool		gexec_flag;		/* one-shot flag to execute query's results */
97 	bool		crosstab_flag;	/* one-shot request to crosstab results */
98 	char	   *ctv_args[4];	/* \crosstabview arguments */
99 
100 	bool		notty;			/* stdin or stdout is not a tty (as determined
101 								 * on startup) */
102 	enum trivalue getPassword;	/* prompt the user for a username and password */
103 	FILE	   *cur_cmd_source; /* describe the status of the current main
104 								 * loop */
105 	bool		cur_cmd_interactive;
106 	int			sversion;		/* backend server version */
107 	const char *progname;		/* in case you renamed psql */
108 	char	   *inputfile;		/* file being currently processed, if any */
109 	uint64		lineno;			/* also for error reporting */
110 	uint64		stmt_lineno;	/* line number inside the current statement */
111 
112 	bool		timing;			/* enable timing of all queries */
113 
114 	FILE	   *logfile;		/* session log file handle */
115 
116 	VariableSpace vars;			/* "shell variable" repository */
117 
118 	/*
119 	 * The remaining fields are set by assign hooks associated with entries in
120 	 * "vars".  They should not be set directly except by those hook
121 	 * functions.
122 	 */
123 	bool		autocommit;
124 	bool		on_error_stop;
125 	bool		quiet;
126 	bool		singleline;
127 	bool		singlestep;
128 	int			fetch_count;
129 	int			histsize;
130 	int			ignoreeof;
131 	PSQL_ECHO	echo;
132 	PSQL_ECHO_HIDDEN echo_hidden;
133 	PSQL_ERROR_ROLLBACK on_error_rollback;
134 	PSQL_COMP_CASE comp_case;
135 	HistControl histcontrol;
136 	const char *prompt1;
137 	const char *prompt2;
138 	const char *prompt3;
139 	PGVerbosity verbosity;		/* current error verbosity level */
140 	PGContextVisibility show_context;	/* current context display level */
141 } PsqlSettings;
142 
143 extern PsqlSettings pset;
144 
145 
146 #ifndef EXIT_SUCCESS
147 #define EXIT_SUCCESS 0
148 #endif
149 
150 #ifndef EXIT_FAILURE
151 #define EXIT_FAILURE 1
152 #endif
153 
154 #define EXIT_BADCONN 2
155 
156 #define EXIT_USER 3
157 
158 #endif
159