Move shared file reading code to kore_read_line()

This commit is contained in:
Joris Vink 2016-02-01 22:10:10 +01:00
parent 17e311dba9
commit 40f69924bb
4 changed files with 32 additions and 24 deletions

View File

@ -516,6 +516,7 @@ int kore_base64_encode(u_int8_t *, u_int32_t, char **);
int kore_base64_decode(char *, u_int8_t **, u_int32_t *);
void *kore_mem_find(void *, size_t, void *, u_int32_t);
char *kore_text_trim(char *, size_t);
char *kore_read_line(FILE *, char *, size_t);
#if !defined(KORE_NO_HTTP)
void kore_websocket_handshake(struct http_request *,

View File

@ -1053,20 +1053,10 @@ cli_buildopt_parse(const char *path)
bopt = NULL;
while (fgets(buf, sizeof(buf), fp) != NULL) {
p = buf;
buf[strcspn(buf, "\n")] = '\0';
while (isspace(*p))
p++;
if (p[0] == '#' || p[0] == '\0')
while ((p = kore_read_line(fp, buf, sizeof(buf))) != NULL) {
if (strlen(p) == 0)
continue;
for (t = p; *t != '\0'; t++) {
if (*t == '\t')
*t = ' ';
}
if (bopt != NULL && !strcmp(p, "}")) {
bopt = NULL;
continue;

View File

@ -198,22 +198,12 @@ kore_parse_config_file(const char *fpath)
kore_debug("parsing configuration file '%s'", fpath);
lineno = 1;
while (fgets(buf, sizeof(buf), fp) != NULL) {
p = buf;
buf[strcspn(buf, "\n")] = '\0';
while (isspace(*p))
p++;
if (p[0] == '#' || p[0] == '\0') {
while ((p = kore_read_line(fp, buf, sizeof(buf))) != NULL) {
if (strlen(p) == 0) {
lineno++;
continue;
}
for (t = p; *t != '\0'; t++) {
if (*t == '\t')
*t = ' ';
}
#if !defined(KORE_NO_HTTP)
if (!strcmp(p, "}") && current_handler != NULL) {
lineno++;

View File

@ -513,6 +513,33 @@ kore_text_trim(char *string, size_t len)
return (string);
}
char *
kore_read_line(FILE *fp, char *in, size_t len)
{
char *p, *t;
if (fgets(in, len, fp) == NULL)
return (NULL);
p = in;
in[strcspn(in, "\n")] = '\0';
while (isspace(*p))
p++;
if (p[0] == '#' || p[0] == '\0') {
p[0] = '\0';
return (p);
}
for (t = p; *t != '\0'; t++) {
if (*t == '\t')
*t = ' ';
}
return (p);
}
void
fatal(const char *fmt, ...)
{