allow parsing directly from strings with

krb5_config_parse_string_multi


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@13798 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
2004-04-26 07:14:28 +00:00
parent 4ac10a4c46
commit 5e02dc0ec1

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997 - 2002 Kungliga Tekniska H<>gskolan * Copyright (c) 1997 - 2004 Kungliga Tekniska H<>gskolan
* (Royal Institute of Technology, Stockholm, Sweden). * (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved. * All rights reserved.
* *
@@ -36,14 +36,46 @@ RCSID("$Id$");
#ifndef HAVE_NETINFO #ifndef HAVE_NETINFO
/* Gaah! I want a portable funopen */
struct fileptr {
const char *s;
FILE *f;
};
static char *
config_fgets(char *str, size_t len, struct fileptr *ptr)
{
/* XXX this is not correct, in that they don't do the same if the
line is longer than len */
if(ptr->f != NULL)
return fgets(str, len, ptr->f);
else {
/* this is almost strsep_copy */
const char *p;
ssize_t l;
if(*ptr->s == '\0')
return NULL;
p = ptr->s + strcspn(ptr->s, "\n");
if(*p == '\n')
p++;
l = min(len, p - ptr->s);
if(len > 0) {
memcpy(str, ptr->s, l);
str[l] = '\0';
}
ptr->s = p;
return str;
}
}
static krb5_error_code parse_section(char *p, krb5_config_section **s, static krb5_error_code parse_section(char *p, krb5_config_section **s,
krb5_config_section **res, krb5_config_section **res,
const char **error_message); const char **error_message);
static krb5_error_code parse_binding(FILE *f, unsigned *lineno, char *p, static krb5_error_code parse_binding(struct fileptr *f, unsigned *lineno, char *p,
krb5_config_binding **b, krb5_config_binding **b,
krb5_config_binding **parent, krb5_config_binding **parent,
const char **error_message); const char **error_message);
static krb5_error_code parse_list(FILE *f, unsigned *lineno, static krb5_error_code parse_list(struct fileptr *f, unsigned *lineno,
krb5_config_binding **parent, krb5_config_binding **parent,
const char **error_message); const char **error_message);
@@ -114,7 +146,7 @@ parse_section(char *p, krb5_config_section **s, krb5_config_section **parent,
*/ */
static krb5_error_code static krb5_error_code
parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent, parse_list(struct fileptr *f, unsigned *lineno, krb5_config_binding **parent,
const char **error_message) const char **error_message)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
@@ -122,7 +154,7 @@ parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent,
krb5_config_binding *b = NULL; krb5_config_binding *b = NULL;
unsigned beg_lineno = *lineno; unsigned beg_lineno = *lineno;
while(fgets(buf, sizeof(buf), f) != NULL) { while(config_fgets(buf, sizeof(buf), f) != NULL) {
char *p; char *p;
++*lineno; ++*lineno;
@@ -153,7 +185,7 @@ parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent,
*/ */
static krb5_error_code static krb5_error_code
parse_binding(FILE *f, unsigned *lineno, char *p, parse_binding(struct fileptr *f, unsigned *lineno, char *p,
krb5_config_binding **b, krb5_config_binding **parent, krb5_config_binding **b, krb5_config_binding **parent,
const char **error_message) const char **error_message)
{ {
@@ -209,26 +241,17 @@ parse_binding(FILE *f, unsigned *lineno, char *p,
*/ */
static krb5_error_code static krb5_error_code
krb5_config_parse_file_debug (const char *fname, krb5_config_parse_debug (struct fileptr *f,
krb5_config_section **res, krb5_config_section **res,
unsigned *lineno, unsigned *lineno,
const char **error_message) const char **error_message)
{ {
FILE *f; krb5_config_section *s = NULL;
krb5_config_section *s; krb5_config_binding *b = NULL;
krb5_config_binding *b;
char buf[BUFSIZ]; char buf[BUFSIZ];
krb5_error_code ret = 0; krb5_error_code ret;
s = NULL; while (config_fgets(buf, sizeof(buf), f) != NULL) {
b = NULL;
*lineno = 0;
f = fopen (fname, "r");
if (f == NULL) {
*error_message = "cannot open file";
return ENOENT;
}
while (fgets(buf, sizeof(buf), f) != NULL) {
char *p; char *p;
++*lineno; ++*lineno;
@@ -241,28 +264,43 @@ krb5_config_parse_file_debug (const char *fname,
continue; continue;
if (*p == '[') { if (*p == '[') {
ret = parse_section(p, &s, res, error_message); ret = parse_section(p, &s, res, error_message);
if (ret) { if (ret)
goto out; return ret;
}
b = NULL; b = NULL;
} else if (*p == '}') { } else if (*p == '}') {
*error_message = "unmatched }"; *error_message = "unmatched }";
ret = EINVAL; /* XXX */ return EINVAL; /* XXX */
goto out;
} else if(*p != '\0') { } else if(*p != '\0') {
if (s == NULL) { if (s == NULL) {
*error_message = "binding before section"; *error_message = "binding before section";
ret = EINVAL; return EINVAL;
goto out;
} }
ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message); ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message);
if (ret) if (ret)
goto out; return ret;
} }
} }
out: return 0;
fclose (f); }
return ret;
krb5_error_code
krb5_config_parse_string_multi(krb5_context context,
const char *string,
krb5_config_section **res)
{
const char *str;
unsigned lineno = 0;
krb5_error_code ret;
struct fileptr f;
f.f = NULL;
f.s = string;
ret = krb5_config_parse_debug (&f, res, &lineno, &str);
if (ret) {
krb5_set_error_string (context, "%s:%u: %s", "<constant>", lineno, str);
return ret;
}
return 0;
} }
krb5_error_code krb5_error_code
@@ -271,10 +309,19 @@ krb5_config_parse_file_multi (krb5_context context,
krb5_config_section **res) krb5_config_section **res)
{ {
const char *str; const char *str;
unsigned lineno; unsigned lineno = 0;
krb5_error_code ret; krb5_error_code ret;
struct fileptr f;
f.f = fopen(fname, "r");
f.s = NULL;
if(f.f == NULL) {
ret = errno;
krb5_set_error_string (context, "open %s: %s", fname, strerror(ret));
return ret;
}
ret = krb5_config_parse_file_debug (fname, res, &lineno, &str); ret = krb5_config_parse_debug (&f, res, &lineno, &str);
fclose(f.f);
if (ret) { if (ret) {
krb5_set_error_string (context, "%s:%u: %s", fname, lineno, str); krb5_set_error_string (context, "%s:%u: %s", fname, lineno, str);
return ret; return ret;