Suppress "variable may be used uninitialized" warning.
[pgsql.git] / src / common / pg_get_line.c
blobea37a359454cdc99ba18c42db5b93c35c9b61652
1 /*-------------------------------------------------------------------------
3 * pg_get_line.c
4 * fgets() with an expansible result buffer
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * src/common/pg_get_line.c
13 *-------------------------------------------------------------------------
15 #ifndef FRONTEND
16 #include "postgres.h"
17 #else
18 #include "postgres_fe.h"
19 #endif
21 #include <setjmp.h>
23 #include "common/string.h"
24 #include "lib/stringinfo.h"
28 * pg_get_line()
30 * This is meant to be equivalent to fgets(), except that instead of
31 * reading into a caller-supplied, fixed-size buffer, it reads into
32 * a palloc'd (in frontend, really malloc'd) string, which is resized
33 * as needed to handle indefinitely long input lines. The caller is
34 * responsible for pfree'ing the result string when appropriate.
36 * As with fgets(), returns NULL if there is a read error or if no
37 * characters are available before EOF. The caller can distinguish
38 * these cases by checking ferror(stream).
40 * Since this is meant to be equivalent to fgets(), the trailing newline
41 * (if any) is not stripped. Callers may wish to apply pg_strip_crlf().
43 * Note that while I/O errors are reflected back to the caller to be
44 * dealt with, an OOM condition for the palloc'd buffer will not be;
45 * there'll be an ereport(ERROR) or exit(1) inside stringinfo.c.
47 * Also note that the palloc'd buffer is usually a lot longer than
48 * strictly necessary, so it may be inadvisable to use this function
49 * to collect lots of long-lived data. A less memory-hungry option
50 * is to use pg_get_line_buf() or pg_get_line_append() in a loop,
51 * then pstrdup() each line.
53 * prompt_ctx can optionally be provided to allow this function to be
54 * canceled via an existing SIGINT signal handler that will longjmp to the
55 * specified place only when *(prompt_ctx->enabled) is true. If canceled,
56 * this function returns NULL, and prompt_ctx->canceled is set to true.
58 char *
59 pg_get_line(FILE *stream, PromptInterruptContext *prompt_ctx)
61 StringInfoData buf;
63 initStringInfo(&buf);
65 if (!pg_get_line_append(stream, &buf, prompt_ctx))
67 /* ensure that free() doesn't mess up errno */
68 int save_errno = errno;
70 pfree(buf.data);
71 errno = save_errno;
72 return NULL;
75 return buf.data;
79 * pg_get_line_buf()
81 * This has similar behavior to pg_get_line(), and thence to fgets(),
82 * except that the collected data is returned in a caller-supplied
83 * StringInfo buffer. This is a convenient API for code that just
84 * wants to read and process one line at a time, without any artificial
85 * limit on line length.
87 * Returns true if a line was successfully collected (including the
88 * case of a non-newline-terminated line at EOF). Returns false if
89 * there was an I/O error or no data was available before EOF.
90 * (Check ferror(stream) to distinguish these cases.)
92 * In the false-result case, buf is reset to empty.
94 bool
95 pg_get_line_buf(FILE *stream, StringInfo buf)
97 /* We just need to drop any data from the previous call */
98 resetStringInfo(buf);
99 return pg_get_line_append(stream, buf, NULL);
103 * pg_get_line_append()
105 * This has similar behavior to pg_get_line(), and thence to fgets(),
106 * except that the collected data is appended to whatever is in *buf.
107 * This is useful in preference to pg_get_line_buf() if the caller wants
108 * to merge some lines together, e.g. to implement backslash continuation.
110 * Returns true if a line was successfully collected (including the
111 * case of a non-newline-terminated line at EOF). Returns false if
112 * there was an I/O error or no data was available before EOF.
113 * (Check ferror(stream) to distinguish these cases.)
115 * In the false-result case, the contents of *buf are logically unmodified,
116 * though it's possible that the buffer has been resized.
118 * prompt_ctx can optionally be provided to allow this function to be
119 * canceled via an existing SIGINT signal handler that will longjmp to the
120 * specified place only when *(prompt_ctx->enabled) is true. If canceled,
121 * this function returns false, and prompt_ctx->canceled is set to true.
123 bool
124 pg_get_line_append(FILE *stream, StringInfo buf,
125 PromptInterruptContext *prompt_ctx)
127 int orig_len = buf->len;
129 if (prompt_ctx && sigsetjmp(*((sigjmp_buf *) prompt_ctx->jmpbuf), 1) != 0)
131 /* Got here with longjmp */
132 prompt_ctx->canceled = true;
133 /* Discard any data we collected before detecting error */
134 buf->len = orig_len;
135 buf->data[orig_len] = '\0';
136 return false;
139 /* Loop until newline or EOF/error */
140 for (;;)
142 char *res;
144 /* Enable longjmp while waiting for input */
145 if (prompt_ctx)
146 *(prompt_ctx->enabled) = true;
148 /* Read some data, appending it to whatever we already have */
149 res = fgets(buf->data + buf->len, buf->maxlen - buf->len, stream);
151 /* Disable longjmp again, then break if fgets failed */
152 if (prompt_ctx)
153 *(prompt_ctx->enabled) = false;
155 if (res == NULL)
156 break;
158 /* Got data, so update buf->len */
159 buf->len += strlen(buf->data + buf->len);
161 /* Done if we have collected a newline */
162 if (buf->len > orig_len && buf->data[buf->len - 1] == '\n')
163 return true;
165 /* Make some more room in the buffer, and loop to read more data */
166 enlargeStringInfo(buf, 128);
169 /* Check for I/O errors and EOF */
170 if (ferror(stream) || buf->len == orig_len)
172 /* Discard any data we collected before detecting error */
173 buf->len = orig_len;
174 buf->data[orig_len] = '\0';
175 return false;
178 /* No newline at EOF, but we did collect some data */
179 return true;