1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* psql - the PostgreSQL interactive terminal
*
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.40 2005/10/15 02:49:40 momjian Exp $
*/
#include "postgres_fe.h"
#include "prompt.h"
#include "libpq-fe.h"
#include "settings.h"
#include "common.h"
#include "input.h"
#include "variables.h"
#ifdef WIN32
#include <io.h>
#include <win32.h>
#endif
#ifdef HAVE_UNIX_SOCKETS
#include <unistd.h>
#include <netdb.h>
#endif
/*--------------------------
* get_prompt
*
* Returns a statically allocated prompt made by interpolating certain
* tcsh style escape sequences into pset.vars "PROMPT1|2|3".
* (might not be completely multibyte safe)
*
* Defined interpolations are:
* %M - database server "hostname.domainname", "[local]" for AF_UNIX
* sockets, "[local:/dir/name]" if not default
* %m - like %M, but hostname only (before first dot), or always "[local]"
* %> - database server port number
* %n - database user name
* %/ - current database
* %~ - like %/ but "~" when database name equals user name
* %# - "#" if superuser, ">" otherwise
* %R - in prompt1 normally =, or ^ if single line mode,
* or a ! if session is not connected to a database;
* in prompt2 -, *, ', or ";
* in prompt3 nothing
* %x - transaction status: empty, *, !, ? (unknown or no connection)
* %? - the error code of the last query (not yet implemented)
* %% - a percent sign
*
* %[0-9] - the character with the given decimal code
* %0[0-7] - the character with the given octal code
* %0x[0-9A-Fa-f] - the character with the given hexadecimal code
*
* %`command` - The result of executing command in /bin/sh with trailing
* newline stripped.
* %:name: - The value of the psql variable 'name'
* (those will not be rescanned for more escape sequences!)
*
* %[ ... %] - tell readline that the contained text is invisible
*
* If the application-wide prompts become NULL somehow, the returned string
* will be empty (not NULL!).
*--------------------------
*/
char *
get_prompt(promptStatus_t status)
{
#define MAX_PROMPT_SIZE 256
static char destination[MAX_PROMPT_SIZE + 1];
char buf[MAX_PROMPT_SIZE + 1];
bool esc = false;
const char *p;
const char *prompt_string = "? ";
const char *prompt_name = NULL;
switch (status)
{
case PROMPT_READY:
prompt_name = "PROMPT1";
break;
case PROMPT_CONTINUE:
case PROMPT_SINGLEQUOTE:
case PROMPT_DOUBLEQUOTE:
case PROMPT_DOLLARQUOTE:
case PROMPT_COMMENT:
case PROMPT_PAREN:
prompt_name = "PROMPT2";
break;
case PROMPT_COPY:
prompt_name = "PROMPT3";
break;
}
if (prompt_name)
prompt_string = GetVariable(pset.vars, prompt_name);
destination[0] = '\0';
for (p = prompt_string;
p && *p && strlen(destination) < MAX_PROMPT_SIZE;
p++)
{
memset(buf, 0, MAX_PROMPT_SIZE + 1);
if (esc)
{
switch (*p)
{
/* Current database */
case '/':
if (pset.db)
strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE);
break;
case '~':
if (pset.db)
{
const char *var;
if (strcmp(PQdb(pset.db), PQuser(pset.db)) == 0 ||
((var = getenv("PGDATABASE")) && strcmp(var, PQdb(pset.db)) == 0))
strcpy(buf, "~");
else
strncpy(buf, PQdb(pset.db), MAX_PROMPT_SIZE);
}
break;
/* DB server hostname (long/short) */
case 'M':
case 'm':
if (pset.db)
{
const char *host = PQhost(pset.db);
/* INET socket */
if (host && host[0] && !is_absolute_path(host))
{
strncpy(buf, host, MAX_PROMPT_SIZE);
if (*p == 'm')
buf[strcspn(buf, ".")] = '\0';
}
#ifdef HAVE_UNIX_SOCKETS
/* UNIX socket */
else
{
if (!host
|| strcmp(host, DEFAULT_PGSOCKET_DIR) == 0
|| *p == 'm')
strncpy(buf, "[local]", MAX_PROMPT_SIZE);
else
snprintf(buf, MAX_PROMPT_SIZE, "[local:%s]", host);
}
#endif
}
break;
/* DB server port number */
case '>':
if (pset.db && PQport(pset.db))
strncpy(buf, PQport(pset.db), MAX_PROMPT_SIZE);
break;
/* DB server user name */
case 'n':
if (pset.db)
strncpy(buf, session_username(), MAX_PROMPT_SIZE);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
*buf = (char) strtol(p, (char **) &p, 8);
--p;
break;
case 'R':
switch (status)
{
case PROMPT_READY:
if (!pset.db)
buf[0] = '!';
else if (!GetVariableBool(pset.vars, "SINGLELINE"))
buf[0] = '=';
else
buf[0] = '^';
break;
case PROMPT_CONTINUE:
buf[0] = '-';
break;
case PROMPT_SINGLEQUOTE:
buf[0] = '\'';
break;
case PROMPT_DOUBLEQUOTE:
buf[0] = '"';
break;
case PROMPT_DOLLARQUOTE:
buf[0] = '$';
break;
case PROMPT_COMMENT:
buf[0] = '*';
break;
case PROMPT_PAREN:
buf[0] = '(';
break;
default:
buf[0] = '\0';
break;
}
break;
case 'x':
if (!pset.db)
buf[0] = '?';
else
switch (PQtransactionStatus(pset.db))
{
case PQTRANS_IDLE:
buf[0] = '\0';
break;
case PQTRANS_ACTIVE:
case PQTRANS_INTRANS:
buf[0] = '*';
break;
case PQTRANS_INERROR:
buf[0] = '!';
break;
default:
buf[0] = '?';
break;
}
break;
case '?':
/* not here yet */
break;
case '#':
if (is_superuser())
buf[0] = '#';
else
buf[0] = '>';
break;
/* execute command */
case '`':
{
FILE *fd = NULL;
char *file = pg_strdup(p + 1);
int cmdend;
cmdend = strcspn(file, "`");
file[cmdend] = '\0';
if (file)
fd = popen(file, "r");
if (fd)
{
fgets(buf, MAX_PROMPT_SIZE - 1, fd);
pclose(fd);
}
if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
free(file);
p += cmdend + 1;
break;
}
/* interpolate variable */
case ':':
{
char *name;
const char *val;
int nameend;
name = pg_strdup(p + 1);
nameend = strcspn(name, ":");
name[nameend] = '\0';
val = GetVariable(pset.vars, name);
if (val)
strncpy(buf, val, MAX_PROMPT_SIZE);
free(name);
p += nameend + 1;
break;
}
case '[':
case ']':
#if defined(USE_READLINE) && defined(RL_PROMPT_START_IGNORE)
/*
* readline >=4.0 undocumented feature: non-printing
* characters in prompt strings must be marked as such, in
* order to properly display the line during editing.
*/
buf[0] = '\001';
buf[1] = (*p == '[') ? RL_PROMPT_START_IGNORE : RL_PROMPT_END_IGNORE;
#endif /* USE_READLINE */
break;
default:
buf[0] = *p;
buf[1] = '\0';
break;
}
esc = false;
}
else if (*p == '%')
esc = true;
else
{
buf[0] = *p;
buf[1] = '\0';
esc = false;
}
if (!esc)
strncat(destination, buf, MAX_PROMPT_SIZE - strlen(destination));
}
destination[MAX_PROMPT_SIZE] = '\0';
return destination;
}
|