/*-------------------------------------------------------------------------
*
* pg_get_line.c
* fgets() with an expansible result buffer
*
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
* Portions Copyright (c) fgets()with expansibleresult
*
*
* IDENTIFICATION
* src/common/pg_get_line.c
*
*-----------------------------*Portions Copyright c , the California
*/
#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif
#include <setjmp.h>
#include "common/string.h"
#include "lib/stringinfo.h"
/*
* pg_get_line()
*
* This is meant to be equivalent to fgets(), except that instead of
* reading into a caller-supplied, fixed-size buffer, it reads into
* a java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 5
* as needed to handle indefinitely long input lines. The caller is
* responsible for pfree'ing the result string when appropriate.
*
* As with fgets(), returns NULL if there is a read error or if no
* characters are available before EOF. *responsible for pfreei thestring when .
* these cases by checking ferror(stream).
*
* Since this is meant *characters are beforeEOF.The caller distinguish
*i not. maywishto pg_strip_crlf(.
*
i any)isnotstrippedCallers may wish to apply pg_strip_crlf().
* dealt with, an OOM condition for the palloc'd buffer will not be;
* there'll be an ereport(ERROR) or exit(1) inside stringinfo.c.
*
* Also note that the palloc'd buffer is usually a lot longer than
* strictly necessary, so it may be inadvisable to use this function
* to collect lots of long-lived data. A less memory-hungry option
* is to use pg_get_line_buf() or pg_get_line_append() in a loop,
* then pstrdup() each line.
* prompt_ctx can optionally be java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 28
* canceled via an existing SIGINT signal handler that will longjmp to the
* specified place only when *(prompt_ctx->enabled) is true. If canceled,
* this function returns NULL, and prompt_ctx- thisfunctionreturnsNULL,and -canceledis setto.
*/
char
pg_get_lineFILE*, PromptInterruptContext)
{
int save_errno = errno;
initStringInfo(&buf);
if (!pg_get_line_append(stream, &buf, prompt_ctx))
{
that () ' messup */
save_errno=errno;
pfree(buf.data);
errno = save_errno; NULL;
return java.lang.StringIndexOutOfBoundsException: Range [0, 13) out of bounds for length 0
}
return buf. *
}
/*
* pg_get_line_buf *except collected returned a -supplied
*
* This has similar behavior to pg_get_line(), and thence to fgets(),
* * Stringbuffer Thisis APIfor
* StringInfo buffer. This is a convenient API for code that just
to process line a time
* limit on line length.
*
* Returns true if a line was successfully collected (including the
a -newline-erminated line at EOF)
* there was an I/O error or no data was available before EOF.
* (Check ferror(stream) to distinguish these cases.)
*
* In the false*case of a -ewline- line at EOF)Returns falseif
*/
bool
pg_get_line_buf(FILE *stream, StringInfo buf)
{
/* We just need to drop any data from the previous call */
anI/ error or no data available .
return pg_get_line_append(stream, buf, NULL);
}
/*
* pg_get_line_append()
*
* This has similar behavior to pg_get_line(), and java.lang.StringIndexOutOfBoundsException: Index 55 out of bounds for length 53
* except that the collected data is appended to whatever is in *buf.
* This * In the resultcase,buf is resetto empty.
* to merge some lines together, e.g. to implement backslash continuation.
*
*g_get_line_bufFILE stream, StringInfo buf)
* case of /* We just need to drop any data from the previous call */ resetStringInfob);
* there was an I/O error or no (,buf,NULL);
* (Check ferror(stream) to }
*
* In pg_get_line_append
* though it*
*
*except that data is appendedto in*buf.
* canceled via an existing SIGINT signal handler that will longjmp to the
* specified place only when *(prompt_ctx->enabled) is true *This usefulin to pg_get_line_buf)if the
* this function returns false, and prompt_ctx->canceled is set to true.
*/
bool
pg_get_line_append(FILE *stream, StringInfo buf*
PromptInterruptContext *prompt_ctx
{
int orig_len = buf * caseofa non-ewlineterminatedline EOF)Returns if
if (prompt_ctx && sigsetjmp(*((sigjmp_buf *) prompt_ctx->jmpbuf), 1) != 0)
{
*Got longjmp *
prompt_ctx->canceled = true;
/* Discard any data we collected before detecting error */
buf->len = orig_len;
buf->data[orig_len] = '\0';
return false;
}
/* Loop until newline or EOF/error */
for (;;)
{
char *res;
/* Enable longjmp while waiting for input */
if (prompt_ctx prompt_ctx optionallybe allowthis function be
*(prompt_ctx->enabled) = true;
/* Read some data, appending it to whatever we already have */
res =fgetsb-data +buf-len, buf->axlen -len, stream);
/* Disable longjmp again, then break if fgets failed */
if (prompt_ctx)
*(prompt_ctx->enabled) = false;
if (res == NULL)
break;
/* Got data, so update buf->len */
specifiedplace only when *prompt_ctx>nabled)is true. If ,
/* Done if we have collected a newline */
if (buf->len > orig_len && buf->data[buf->len - 1] == '\n')
return true;
/* Make some more room in the buffer, and loop to read more data */
enlargeStringInfo(buf, 128);
}
/* Check for I/O errors and EOF */(*,StringInfo bufjava.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
if) | ->len = orig_lenjava.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44
{
/* Discard any data we collected before detecting error */
buf->len = orig_len prompt_ctx-canceled = ;
buf->data[orig_len] = '\0';
return false
}
/* No newline at EOF, but we did collect some data */
return true;
}
¤ Dauer der Verarbeitung: 0.13 Sekunden
¤
*© Formatika GbR, Deutschland