2012-05-04 03:21:51 +00:00
|
|
|
/*
|
|
|
|
* fileio.c
|
|
|
|
* File reading utilities
|
|
|
|
*
|
2012-07-26 02:09:31 +00:00
|
|
|
* Copyright (c) 2012 pkgconf authors (see AUTHORS).
|
2012-05-04 03:21:51 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2012-07-20 19:29:58 +00:00
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
2012-05-04 03:21:51 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-18 04:38:25 +00:00
|
|
|
#include <libpkgconf/stdinc.h>
|
2015-09-06 14:35:08 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2012-05-04 03:21:51 +00:00
|
|
|
|
|
|
|
char *
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_fgetline(char *line, size_t size, FILE *stream)
|
2012-05-04 03:21:51 +00:00
|
|
|
{
|
|
|
|
char *s = line;
|
|
|
|
char *end = line + size - 1;
|
2012-07-30 06:53:32 +00:00
|
|
|
bool quoted = false;
|
2012-05-04 03:21:51 +00:00
|
|
|
int c = '\0', c2;
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (s < end && (c = getc(stream)) != EOF)
|
|
|
|
{
|
2012-07-30 06:53:32 +00:00
|
|
|
if (c == '\\')
|
|
|
|
{
|
|
|
|
quoted = true;
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-20 14:24:07 +00:00
|
|
|
else if (c == '#')
|
|
|
|
{
|
2015-04-23 19:59:02 +00:00
|
|
|
if (!quoted) {
|
|
|
|
/* Skip the rest of the line */
|
|
|
|
do {
|
|
|
|
c = getc(stream);
|
|
|
|
} while (c != '\n' && c != EOF);
|
|
|
|
*s++ = c;
|
2015-04-20 14:24:07 +00:00
|
|
|
break;
|
2015-04-23 19:59:02 +00:00
|
|
|
}
|
2015-04-20 14:24:07 +00:00
|
|
|
quoted = false;
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-30 06:53:32 +00:00
|
|
|
else if (c == '\n')
|
2012-05-04 03:21:51 +00:00
|
|
|
{
|
2012-07-30 06:53:32 +00:00
|
|
|
if (quoted)
|
|
|
|
{
|
2015-02-02 16:35:32 +00:00
|
|
|
/* Trim spaces */
|
|
|
|
do {
|
|
|
|
c2 = getc(stream);
|
|
|
|
} while (c2 == '\t' || c2 == ' ');
|
|
|
|
|
|
|
|
ungetc(c2, stream);
|
|
|
|
|
2012-07-30 06:53:32 +00:00
|
|
|
quoted = false;
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-02 16:35:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*s++ = c;
|
|
|
|
}
|
2012-07-30 06:53:32 +00:00
|
|
|
|
2012-05-04 03:21:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (c == '\r')
|
|
|
|
{
|
|
|
|
*s++ = '\n';
|
|
|
|
|
|
|
|
if ((c2 = getc(stream)) == '\n')
|
2012-07-30 06:53:32 +00:00
|
|
|
{
|
|
|
|
if (quoted)
|
|
|
|
{
|
|
|
|
quoted = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-04 03:21:51 +00:00
|
|
|
break;
|
2012-07-30 06:53:32 +00:00
|
|
|
}
|
2012-05-04 03:21:51 +00:00
|
|
|
|
|
|
|
ungetc(c2, stream);
|
2012-07-30 06:53:32 +00:00
|
|
|
|
|
|
|
if (quoted)
|
|
|
|
{
|
|
|
|
quoted = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-04 03:21:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2012-08-14 09:00:53 +00:00
|
|
|
{
|
2012-08-14 09:06:04 +00:00
|
|
|
if (quoted) {
|
2012-08-14 09:00:53 +00:00
|
|
|
*s++ = '\\';
|
2012-08-14 09:06:04 +00:00
|
|
|
quoted = false;
|
|
|
|
}
|
2012-05-04 03:21:51 +00:00
|
|
|
*s++ = c;
|
2012-08-14 09:00:53 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 03:21:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == EOF && (s == line || ferror(stream)))
|
|
|
|
return NULL;
|
|
|
|
|
2014-07-31 14:15:31 +00:00
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
/* Remove newline character. */
|
|
|
|
if (s > line && *(--s) == '\n') {
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
if (s > line && *(--s) == '\r')
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
|
2012-05-04 03:21:51 +00:00
|
|
|
return line;
|
|
|
|
}
|