extend_inputline(FILE *f, pcre_uint8 *start, const char *prompt)
{
pcre_uint8 *here = start;
for (;;)
{
size_t rlen = (size_t)(buffer_size - (here - buffer));
if (rlen > 1000)
{
int dlen;
/* Read the next line by normal means, prompting if the file is stdin. */
{
if (f == stdin) printf("%s", prompt);
if (fgets((char *)here, rlen, f) == NULL)
return (here == start)? NULL : start;
}
dlen = (int)strlen((char *)here);
if (dlen > 0 && here[dlen - 1] == '\n') return start;
here += dlen;
}
else
{
int new_buffer_size = 2*buffer_size;
pcre_uint8 *new_buffer = (pcre_uint8 *)malloc(new_buffer_size);
pcre_uint8 *new_dbuffer = (pcre_uint8 *)malloc(new_buffer_size);
pcre_uint8 *new_pbuffer = (pcre_uint8 *)malloc(new_buffer_size);
if (new_buffer == NULL || new_dbuffer == NULL || new_pbuffer == NULL)
{
fprintf(stderr, "pcretest: malloc(%d) failed\n", new_buffer_size);
exit(1);
}
memcpy(new_buffer, buffer, buffer_size);
memcpy(new_pbuffer, pbuffer, buffer_size);
buffer_size = new_buffer_size;
start = new_buffer + (start - buffer);
here = new_buffer + (here - buffer);
free(buffer);
free(dbuffer);
free(pbuffer);
buffer = new_buffer;
dbuffer = new_dbuffer;
pbuffer = new_pbuffer;
}
}
return NULL; /* Control never gets here */
}
{
pcre_uint8 *here = start;
for (;;)
{
size_t rlen = (size_t)(buffer_size - (here - buffer));
if (rlen > 1000)
{
int dlen;
/* Read the next line by normal means, prompting if the file is stdin. */
{
if (f == stdin) printf("%s", prompt);
if (fgets((char *)here, rlen, f) == NULL)
return (here == start)? NULL : start;
}
dlen = (int)strlen((char *)here);
if (dlen > 0 && here[dlen - 1] == '\n') return start;
here += dlen;
}
else
{
int new_buffer_size = 2*buffer_size;
pcre_uint8 *new_buffer = (pcre_uint8 *)malloc(new_buffer_size);
pcre_uint8 *new_dbuffer = (pcre_uint8 *)malloc(new_buffer_size);
pcre_uint8 *new_pbuffer = (pcre_uint8 *)malloc(new_buffer_size);
if (new_buffer == NULL || new_dbuffer == NULL || new_pbuffer == NULL)
{
fprintf(stderr, "pcretest: malloc(%d) failed\n", new_buffer_size);
exit(1);
}
memcpy(new_buffer, buffer, buffer_size);
memcpy(new_pbuffer, pbuffer, buffer_size);
buffer_size = new_buffer_size;
start = new_buffer + (start - buffer);
here = new_buffer + (here - buffer);
free(buffer);
free(dbuffer);
free(pbuffer);
buffer = new_buffer;
dbuffer = new_dbuffer;
pbuffer = new_pbuffer;
}
}
return NULL; /* Control never gets here */
}
The result in buffer, clearly does not contain the new-line, but I need to preserve the new-line. How could I do that?