blob: 1775dffb8341a051756b93cfc16ce8f97c223e1e (
plain)
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
|
#include <config.h>
#include <c.h>
#include "input.h"
#include <pqexpbuffer.h>
#include "settings.h"
#include "tab-complete.h"
/* Runtime options for turning off readline and history */
/* (of course there is no runtime command for doing that :) */
#ifdef USE_READLINE
static bool useReadline;
#endif
#ifdef USE_HISTORY
static bool useHistory;
#endif
/*
* gets_interactive()
*
* Gets a line of interactive input, using readline of desired.
* The result is malloced.
*/
char *
gets_interactive(const char *prompt)
{
char *s;
#ifdef USE_READLINE
if (useReadline)
s = readline((char *) prompt);
else
{
#endif
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);
#endif
return s;
}
/*
* gets_fromFile
*
* Gets a line of noninteractive input from a file (which could be stdin).
*/
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;
}
}
if (buffer.len > 0)
return buffer.data; /* EOF after reading some bufferload(s) */
/* EOF, so return null */
termPQExpBuffer(&buffer);
return NULL;
}
/*
* Put any startup stuff related to input in here. It's good to maintain
* abstraction this way.
*
* The only "flag" right now is 1 for use readline & history.
*/
void
initializeInput(int flags, PsqlSettings *pset)
{
#ifdef USE_READLINE
if (flags == 1)
{
useReadline = true;
rl_readline_name = "psql";
initialize_readline(&(pset->db));
}
#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);
}
}
}
#endif
}
bool
saveHistory(const char *fname)
{
#ifdef USE_HISTORY
if (useHistory)
{
if (write_history((char *) fname) != 0)
{
perror(fname);
return false;
}
return true;
}
else
return false;
#else
return false;
#endif
}
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);
}
}
}
#endif
}
|