MCPcopy Create free account
hub / github.com/apache/cloudberry / gets_fromFile

Function gets_fromFile

src/bin/psql/input.c:187–244  ·  view source on GitHub ↗

* gets_fromFile * * Gets a line of noninteractive input from a file (which could be stdin). * The result is a malloc'd string, or NULL on EOF or input error. * * Caller *must* have set up sigint_interrupt_jmp before calling. * * Note: we re-use a static PQExpBuffer for each call. This is to avoid * leaking memory if interrupted by SIGINT. */

Source from the content-addressed store, hash-verified

185 * leaking memory if interrupted by SIGINT.
186 */
187char *
188gets_fromFile(FILE *source)
189{
190 static PQExpBuffer buffer = NULL;
191
192 char line[1024];
193
194 if (buffer == NULL) /* first time through? */
195 buffer = createPQExpBuffer();
196 else
197 resetPQExpBuffer(buffer);
198
199 for (;;)
200 {
201 char *result;
202
203 /* Enable SIGINT to longjmp to sigint_interrupt_jmp */
204 sigint_interrupt_enabled = true;
205
206 /* Get some data */
207 result = fgets(line, sizeof(line), source);
208
209 /* Disable SIGINT again */
210 sigint_interrupt_enabled = false;
211
212 /* EOF or error? */
213 if (result == NULL)
214 {
215 if (ferror(source))
216 {
217 pg_log_error("could not read from input file: %m");
218 return NULL;
219 }
220 break;
221 }
222
223 appendPQExpBufferStr(buffer, line);
224
225 if (PQExpBufferBroken(buffer))
226 {
227 pg_log_error("out of memory");
228 return NULL;
229 }
230
231 /* EOL? */
232 if (buffer->len > 0 && buffer->data[buffer->len - 1] == '\n')
233 {
234 buffer->data[buffer->len - 1] = '\0';
235 return pg_strdup(buffer->data);
236 }
237 }
238
239 if (buffer->len > 0) /* EOF after reading some bufferload(s) */
240 return pg_strdup(buffer->data);
241
242 /* EOF, so return null */
243 return NULL;
244}

Callers 3

gets_interactiveFunction · 0.85
exec_command_promptFunction · 0.85
MainLoopFunction · 0.85

Calls 4

createPQExpBufferFunction · 0.85
resetPQExpBufferFunction · 0.85
appendPQExpBufferStrFunction · 0.85
pg_strdupFunction · 0.85

Tested by

no test coverage detected