* It has been folded into a single C source file to make it easier to
* fold into an application program.)
*
- * Copyright (C) 2005 by Theodore Ts'o.
+ * Copyright (C) 2005, 2006 by Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <dirent.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#undef STAT_ONCE_PER_SECOND
#undef HAVE_STAT
-/* Begin prof_int.h */
-
/*
* prof_int.h
*/
* This is the structure which stores the profile information for a
* particular configuration file.
*/
-struct _prf_data_t {
+struct _prf_file_t {
prf_magic_t magic;
- struct profile_node *root;
+ char *filespec;
#ifdef STAT_ONCE_PER_SECOND
time_t last_stat;
#endif
time_t timestamp; /* time tree was last updated from file */
int flags; /* r/w, dirty */
int upd_serial; /* incremented when data changes */
- char *comment;
-
- size_t fslen;
-
- struct _prf_data_t *next;
- /* Was: "profile_filespec_t filespec". Now: flexible char
- array ... except, we need to work in C89, so an array
- length must be specified. */
- const char filespec[sizeof("/etc/krb5.conf")];
-};
-
-typedef struct _prf_data_t *prf_data_t;
-prf_data_t profile_make_prf_data(const char *);
-
-struct _prf_file_t {
- prf_magic_t magic;
- struct _prf_data_t *data;
+ struct profile_node *root;
struct _prf_file_t *next;
};
*/
#define PROFILE_FILE_RW 0x0001
#define PROFILE_FILE_DIRTY 0x0002
+#define PROFILE_FILE_NO_RELOAD 0x0004
/*
* This structure defines the high-level, user visible profile_t
#define PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
-/* profile_parse.c */
+struct profile_node {
+ errcode_t magic;
+ char *name;
+ char *value;
+ int group_level;
+ unsigned int final:1; /* Indicate don't search next file */
+ unsigned int deleted:1;
+ struct profile_node *first_child;
+ struct profile_node *parent;
+ struct profile_node *next, *prev;
+};
+
+#define CHECK_MAGIC(node) \
+ if ((node)->magic != PROF_MAGIC_NODE) \
+ return PROF_MAGIC_NODE;
-static errcode_t profile_parse_file
- (FILE *f, struct profile_node **root);
+/* profile parser declarations */
+struct parse_state {
+ int state;
+ int group_level;
+ int line_num;
+ struct profile_node *root_section;
+ struct profile_node *current_section;
+};
+
+static const char *default_filename = "<default>";
+
+static profile_syntax_err_cb_t syntax_err_cb;
+
+static errcode_t parse_line(char *line, struct parse_state *state);
#ifdef DEBUG_PROGRAM
static errcode_t profile_write_tree_file
#endif
-/* prof_tree.c */
-
static void profile_free_node
(struct profile_node *relation);
const char *name, const char *value,
struct profile_node **ret_node);
-static errcode_t profile_make_node_final
- (struct profile_node *node);
-
-#ifdef DEBUG_PROGRAM
-static int profile_is_node_final
- (struct profile_node *node);
-
-static const char *profile_get_node_name
- (struct profile_node *node);
-
-static const char *profile_get_node_value
- (struct profile_node *node);
-#endif
-
static errcode_t profile_find_node
(struct profile_node *section,
const char *name, const char *value,
int section_flag, void **state,
struct profile_node **node);
-#ifdef DEBUG_PROGRAM
-static errcode_t profile_find_node_relation
- (struct profile_node *section,
- const char *name, void **state,
- char **ret_name, char **value);
-#endif
-
-static errcode_t profile_find_node_subsection
- (struct profile_node *section,
- const char *name, void **state,
- char **ret_name, struct profile_node **subsection);
-
-static errcode_t profile_get_node_parent
- (struct profile_node *section,
- struct profile_node **parent);
-
-static errcode_t profile_node_iterator_create
- (profile_t profile, const char *const *names,
- int flags, void **ret_iter);
-
-static void profile_node_iterator_free
- (void **iter_p);
-
static errcode_t profile_node_iterator
(void **iter_p, struct profile_node **ret_node,
char **ret_name, char **ret_value);
-/* prof_file.c */
-
static errcode_t profile_open_file
(const char * file, prf_file_t *ret_prof);
-#define profile_update_file(P) profile_update_file_data((P)->data)
-static errcode_t profile_update_file_data
- (prf_data_t profile);
+static errcode_t profile_update_file
+ (prf_file_t prf);
static void profile_free_file
(prf_file_t profile);
-/* prof_init.c -- included from profile.h */
+static errcode_t profile_get_value(profile_t profile, const char *name,
+ const char *subname, const char *subsubname,
+ const char **ret_value);
-/* prof_get.c */
-static errcode_t profile_get_value
- (profile_t profile, const char **names,
- const char **ret_value);
-/* Others included from profile.h */
-
-/* prof_set.c -- included from profile.h */
-
-/* End prof_int.h */
-
-/* Begin prof_init.c */
/*
* prof_init.c --- routines that manipulate the user-visible profile_t
* object.
*/
+static int compstr(const void *m1, const void *m2)
+{
+ const char *s1 = *((const char * const *) m1);
+ const char *s2 = *((const char * const *) m2);
+
+ return strcmp(s1, s2);
+}
+
+static void free_list(char **list)
+{
+ char **cp;
+
+ if (list == 0)
+ return;
+
+ for (cp = list; *cp; cp++)
+ free(*cp);
+ free(list);
+}
+
+static errcode_t get_dirlist(const char *dirname, char***ret_array)
+{
+ DIR *dir;
+ struct dirent *de;
+ struct stat st;
+ errcode_t retval;
+ char *fn, *cp;
+ char **array = 0, **new_array;
+ int max = 0, num = 0;
+
+ dir = opendir(dirname);
+ if (!dir)
+ return errno;
+
+ while ((de = readdir(dir)) != NULL) {
+ for (cp = de->d_name; *cp; cp++) {
+ if (!isalnum(*cp) &&
+ (*cp != '-') &&
+ (*cp != '_'))
+ break;
+ }
+ if (*cp)
+ continue;
+ fn = malloc(strlen(dirname) + strlen(de->d_name) + 2);
+ if (!fn) {
+ retval = ENOMEM;
+ goto errout;
+ }
+ sprintf(fn, "%s/%s", dirname, de->d_name);
+ if ((stat(fn, &st) < 0) || !S_ISREG(st.st_mode)) {
+ free(fn);
+ continue;
+ }
+ if (num >= max) {
+ max += 10;
+ new_array = realloc(array, sizeof(char *) * (max+1));
+ if (!new_array) {
+ retval = ENOMEM;
+ goto errout;
+ }
+ array = new_array;
+ }
+ array[num++] = fn;
+ }
+ if (array) {
+ qsort(array, num, sizeof(char *), compstr);
+ array[num++] = 0;
+ }
+ *ret_array = array;
+ closedir(dir);
+ return 0;
+errout:
+ closedir(dir);
+ free_list(array);
+ return retval;
+}
+
errcode_t
profile_init(const char **files, profile_t *ret_profile)
{
const char **fs;
profile_t profile;
- prf_file_t new_file, last = 0;
+ prf_file_t new_file, *last;
errcode_t retval = 0;
+ char **cpp, *cp, **array = 0;
profile = malloc(sizeof(struct _profile_t));
if (!profile)
return ENOMEM;
memset(profile, 0, sizeof(struct _profile_t));
profile->magic = PROF_MAGIC_PROFILE;
+ last = &profile->first_file;
/* if the filenames list is not specified return an empty profile */
if ( files ) {
for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) {
+ retval = get_dirlist(*fs, &array);
+ if (retval == 0) {
+ if (!array)
+ continue;
+ for (cpp = array; (cp = *cpp); cpp++) {
+ retval = profile_open_file(cp, &new_file);
+ if (retval == EACCES)
+ continue;
+ if (retval)
+ goto errout;
+ *last = new_file;
+ last = &new_file->next;
+ }
+ } else if ((retval != ENOTDIR) &&
+ strcmp(*fs, default_filename))
+ goto errout;
+
retval = profile_open_file(*fs, &new_file);
/* if this file is missing, skip to the next */
if (retval == ENOENT || retval == EACCES) {
continue;
}
- if (retval) {
- profile_release(profile);
- return retval;
- }
- if (last)
- last->next = new_file;
- else
- profile->first_file = new_file;
- last = new_file;
+ if (retval)
+ goto errout;
+ *last = new_file;
+ last = &new_file->next;
}
/*
- * If last is still null after the loop, then all the files were
- * missing, so return the appropriate error.
+ * If all the files were not found, return the appropriate error.
*/
- if (!last) {
+ if (!profile->first_file) {
profile_release(profile);
return ENOENT;
}
}
+ free_list(array);
*ret_profile = profile;
return 0;
-}
-
-#ifdef DEBUG_PROGRAM
-errcode_t
-profile_init_path(const char * filepath,
- profile_t *ret_profile)
-{
- int n_entries, i;
- unsigned int ent_len;
- const char *s, *t;
- char **filenames;
- errcode_t retval;
-
- /* count the distinct filename components */
- for(s = filepath, n_entries = 1; *s; s++) {
- if (*s == ':')
- n_entries++;
- }
-
- /* the array is NULL terminated */
- filenames = (char **) malloc((n_entries+1) * sizeof(char*));
- if (filenames == 0)
- return ENOMEM;
-
- /* measure, copy, and skip each one */
- for(s = filepath, i=0; (t = strchr(s, ':')) || (t=s+strlen(s)); s=t+1, i++) {
- ent_len = t-s;
- filenames[i] = (char*) malloc(ent_len + 1);
- if (filenames[i] == 0) {
- /* if malloc fails, free the ones that worked */
- while(--i >= 0) free(filenames[i]);
- free(filenames);
- return ENOMEM;
- }
- strncpy(filenames[i], s, ent_len);
- filenames[i][ent_len] = 0;
- if (*t == 0) {
- i++;
- break;
- }
- }
- /* cap the array */
- filenames[i] = 0;
-
- retval = profile_init((const char **) filenames,
- ret_profile);
-
- /* count back down and free the entries */
- while(--i >= 0) free(filenames[i]);
- free(filenames);
-
+errout:
+ free_list(array);
+ profile_release(profile);
return retval;
}
-#endif
void
profile_release(profile_t profile)
free(profile);
}
+/*
+ * This function sets the value of the pseudo file "<default>". If
+ * the file "<default>" had previously been passed to profile_init(),
+ * then def_string parameter will be parsed and used as the profile
+ * information for the "<default>" file.
+ */
+errcode_t profile_set_default(profile_t profile, const char *def_string)
+{
+ struct parse_state state;
+ prf_file_t prf;
+ errcode_t retval;
+ const char *in;
+ char *line, *p, *end;
+ int line_size, len;
+
+ if (!def_string || !profile || profile->magic != PROF_MAGIC_PROFILE)
+ return PROF_MAGIC_PROFILE;
-/* End prof_init.c */
+ for (prf = profile->first_file; prf; prf = prf->next) {
+ if (strcmp(prf->filespec, default_filename) == 0)
+ break;
+ }
+ if (!prf)
+ return 0;
+
+ if (prf->root) {
+ profile_free_node(prf->root);
+ prf->root = 0;
+ }
+
+ memset(&state, 0, sizeof(struct parse_state));
+ retval = profile_create_node("(root)", 0, &state.root_section);
+ if (retval)
+ return retval;
+
+ line = 0;
+ line_size = 0;
+ in = def_string;
+ while (*in) {
+ end = strchr(in, '\n');
+ len = end ? (end - in) : (int) strlen(in);
+ if (len >= line_size) {
+ line_size = len+1;
+ p = realloc(line, line_size);
+ if (!p) {
+ retval = ENOMEM;
+ goto errout;
+ }
+ line = p;
+ }
+ memcpy(line, in, len);
+ line[len] = 0;
+ retval = parse_line(line, &state);
+ if (retval) {
+ errout:
+ if (syntax_err_cb)
+ (syntax_err_cb)(prf->filespec, retval,
+ state.line_num);
+ free(line);
+ if (prf->root)
+ profile_free_node(prf->root);
+ return retval;
+ }
+ if (!end)
+ break;
+ in = end+1;
+ }
+ prf->root = state.root_section;
+ free(line);
+
+ return 0;
+}
-/* Begin prof_file.c */
/*
* prof_file.c ---- routines that manipulate an individual profile file.
*/
-prf_data_t
-profile_make_prf_data(const char *filename)
-{
- prf_data_t d;
- size_t len, flen, slen;
- char *fcopy;
-
- flen = strlen(filename);
- slen = offsetof(struct _prf_data_t, filespec);
- len = slen + flen + 1;
- if (len < sizeof(struct _prf_data_t))
- len = sizeof(struct _prf_data_t);
- d = malloc(len);
- if (d == NULL)
- return NULL;
- memset(d, 0, len);
- fcopy = (char *) d + slen;
- strcpy(fcopy, filename);
- d->comment = NULL;
- d->magic = PROF_MAGIC_FILE_DATA;
- d->root = NULL;
- d->next = NULL;
- d->fslen = flen;
- return d;
-}
-
errcode_t profile_open_file(const char * filespec,
prf_file_t *ret_prof)
{
errcode_t retval;
char *home_env = 0;
unsigned int len;
- prf_data_t data;
char *expanded_filename;
prf = malloc(sizeof(struct _prf_file_t));
home_env = getenv("HOME");
#ifdef HAVE_PWD_H
if (home_env == NULL) {
- uid_t uid;
+#ifdef HAVE_GETWUID_R
struct passwd *pw, pwx;
+ uid_t uid;
char pwbuf[BUFSIZ];
uid = getuid();
- if (!k5_getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
+ if (!getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
&& pw != NULL && pw->pw_dir[0] != 0)
home_env = pw->pw_dir;
+#else
+ struct passwd *pw;
+
+ pw = getpwuid(getuid());
+ home_env = pw->pw_dir;
+#endif
}
#endif
if (home_env)
len += strlen(home_env);
}
expanded_filename = malloc(len);
- if (expanded_filename == 0)
+ if (expanded_filename == 0) {
+ profile_free_file(prf);
return errno;
+ }
if (home_env) {
strcpy(expanded_filename, home_env);
strcat(expanded_filename, filespec+1);
} else
memcpy(expanded_filename, filespec, len);
- data = profile_make_prf_data(expanded_filename);
- if (data == NULL) {
- free(prf);
- free(expanded_filename);
- return ENOMEM;
- }
- free(expanded_filename);
- prf->data = data;
+ prf->filespec = expanded_filename;
- retval = profile_update_file(prf);
- if (retval) {
- profile_free_file(prf);
- return retval;
+ if (strcmp(prf->filespec, default_filename) != 0) {
+ retval = profile_update_file(prf);
+ if (retval) {
+ profile_free_file(prf);
+ return retval;
+ }
}
*ret_prof = prf;
return 0;
}
-errcode_t profile_update_file_data(prf_data_t data)
+errcode_t profile_update_file(prf_file_t prf)
{
errcode_t retval;
#ifdef HAVE_STAT
#endif
#endif
FILE *f;
+ char buf[2048];
+ struct parse_state state;
+
+ if (prf->flags & PROFILE_FILE_NO_RELOAD)
+ return 0;
#ifdef HAVE_STAT
#ifdef STAT_ONCE_PER_SECOND
now = time(0);
- if (now == data->last_stat && data->root != NULL) {
+ if (now == prf->last_stat && prf->root != NULL) {
return 0;
}
#endif
- if (stat(data->filespec, &st)) {
+ if (stat(prf->filespec, &st)) {
retval = errno;
return retval;
}
#ifdef STAT_ONCE_PER_SECOND
- data->last_stat = now;
+ prf->last_stat = now;
#endif
- if (st.st_mtime == data->timestamp && data->root != NULL) {
+ if (st.st_mtime == prf->timestamp && prf->root != NULL) {
return 0;
}
- if (data->root) {
- profile_free_node(data->root);
- data->root = 0;
- }
- if (data->comment) {
- free(data->comment);
- data->comment = 0;
+ if (prf->root) {
+ profile_free_node(prf->root);
+ prf->root = 0;
}
#else
/*
* memory image is correct. That is, we won't reread the
* profile file if it changes.
*/
- if (data->root) {
+ if (prf->root) {
return 0;
}
#endif
+ memset(&state, 0, sizeof(struct parse_state));
+ retval = profile_create_node("(root)", 0, &state.root_section);
+ if (retval)
+ return retval;
errno = 0;
- f = fopen(data->filespec, "r");
+ f = fopen(prf->filespec, "r");
if (f == NULL) {
retval = errno;
if (retval == 0)
retval = ENOENT;
return retval;
}
- data->upd_serial++;
- retval = profile_parse_file(f, &data->root);
- fclose(f);
- if (retval) {
- return retval;
+ prf->upd_serial++;
+ while (!feof(f)) {
+ if (fgets(buf, sizeof(buf), f) == NULL)
+ break;
+ retval = parse_line(buf, &state);
+ if (retval) {
+ if (syntax_err_cb)
+ (syntax_err_cb)(prf->filespec, retval,
+ state.line_num);
+ fclose(f);
+ return retval;
+ }
}
+ prf->root = state.root_section;
+
+ fclose(f);
+
#ifdef HAVE_STAT
- data->timestamp = st.st_mtime;
+ prf->timestamp = st.st_mtime;
#endif
return 0;
}
void profile_free_file(prf_file_t prf)
{
- prf_data_t data = prf->data;
-
- if (data->root)
- profile_free_node(data->root);
- if (data->comment)
- free(data->comment);
- data->magic = 0;
- free(data);
+ if (prf->root)
+ profile_free_node(prf->root);
+ if (prf->filespec)
+ free(prf->filespec);
free(prf);
}
-/* End prof_file.c */
+/* Begin the profile parser */
-/* Begin prof_parse.c */
-
-#define SECTION_SEP_CHAR '/'
+profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
+{
+ profile_syntax_err_cb_t old;
-#define STATE_INIT_COMMENT 1
-#define STATE_STD_LINE 2
-#define STATE_GET_OBRACE 3
+ old = syntax_err_cb;
+ syntax_err_cb = hook;
+ return(old);
+}
-struct parse_state {
- int state;
- int group_level;
- struct profile_node *root_section;
- struct profile_node *current_section;
-};
+#define STATE_INIT_COMMENT 0
+#define STATE_STD_LINE 1
+#define STATE_GET_OBRACE 2
static char *skip_over_blanks(char *cp)
{
return cp;
}
+static int end_or_comment(char ch)
+{
+ return (ch == 0 || ch == '#' || ch == ';');
+}
+
+static char *skip_over_nonblanks(char *cp)
+{
+ while (!end_or_comment(*cp) && !isspace(*cp))
+ cp++;
+ return cp;
+}
+
static void strip_line(char *line)
{
char *p = line + strlen(line);
*to = '\0';
}
-
-static errcode_t parse_init_state(struct parse_state *state)
-{
- state->state = STATE_INIT_COMMENT;
- state->group_level = 0;
-
- return profile_create_node("(root)", 0, &state->root_section);
-}
-
-static errcode_t parse_std_line(char *line, struct parse_state *state)
+static errcode_t parse_line(char *line, struct parse_state *state)
{
char *cp, ch, *tag, *value;
char *p;
int do_subsection = 0;
void *iter = 0;
- if (*line == 0)
+ state->line_num++;
+ if (state->state == STATE_GET_OBRACE) {
+ cp = skip_over_blanks(line);
+ if (*cp != '{')
+ return PROF_MISSING_OBRACE;
+ state->state = STATE_STD_LINE;
return 0;
- if (line[0] == ';' || line[0] == '#')
+ }
+ if (state->state == STATE_INIT_COMMENT) {
+ if (line[0] != '[')
+ return 0;
+ state->state = STATE_STD_LINE;
+ }
+
+ if (*line == 0)
return 0;
strip_line(line);
cp = skip_over_blanks(line);
ch = *cp;
- if (ch == 0)
+ if (end_or_comment(ch))
return 0;
if (ch == '[') {
if (state->group_level > 0)
return PROF_SECTION_NOTOP;
cp++;
+ cp = skip_over_blanks(cp);
p = strchr(cp, ']');
if (p == NULL)
return PROF_SECTION_SYNTAX;
- *p = '\0';
- retval = profile_find_node_subsection(state->root_section,
- cp, &iter, 0,
- &state->current_section);
+ if (*cp == '"') {
+ cp++;
+ parse_quoted_string(cp);
+ } else {
+ *p-- = '\0';
+ while (isspace(*p) && (p > cp))
+ *p-- = '\0';
+ if (*cp == 0)
+ return PROF_SECTION_SYNTAX;
+ }
+ retval = profile_find_node(state->root_section, cp, 0, 1,
+ &iter, &state->current_section);
if (retval == PROF_NO_SECTION) {
retval = profile_add_node(state->root_section,
cp, 0,
*/
cp = p+1;
if (*cp == '*') {
- profile_make_node_final(state->current_section);
+ state->current_section->final = 1;
cp++;
}
/*
- * A space after ']' should not be fatal
+ * Spaces or comments after ']' should not be fatal
*/
cp = skip_over_blanks(cp);
- if (*cp)
+ if (!end_or_comment(*cp))
return PROF_SECTION_SYNTAX;
return 0;
}
if (state->group_level == 0)
return PROF_EXTRA_CBRACE;
if (*(cp+1) == '*')
- profile_make_node_final(state->current_section);
- retval = profile_get_node_parent(state->current_section,
- &state->current_section);
- if (retval)
- return retval;
+ state->current_section->final = 1;
+ state->current_section = state->current_section->parent;
state->group_level--;
return 0;
}
if (cp == tag)
return PROF_RELATION_SYNTAX;
*cp = '\0';
- p = tag;
- /* Look for whitespace on left-hand side. */
- while (p < cp && !isspace((int)*p))
- p++;
- if (p < cp) {
- /* Found some sort of whitespace. */
- *p++ = 0;
- /* If we have more non-whitespace, it's an error. */
- while (p < cp) {
- if (!isspace((int)*p))
- return PROF_RELATION_SYNTAX;
- p++;
- }
+ if (*tag == '"') {
+ tag++;
+ parse_quoted_string(tag);
+ } else {
+ /* Look for whitespace on left-hand side. */
+ p = skip_over_nonblanks(tag);
+ if (*p)
+ *p++ = 0;
+ p = skip_over_blanks(p);
+ /* If we have more non-whitespace, it's an error. */
+ if (*p)
+ return PROF_RELATION_SYNTAX;
}
+
cp = skip_over_blanks(cp+1);
value = cp;
- if (value[0] == '"') {
+ ch = value[0];
+ if (ch == '"') {
value++;
parse_quoted_string(value);
- } else if (value[0] == 0) {
+ } else if (end_or_comment(ch)) {
do_subsection++;
state->state = STATE_GET_OBRACE;
- } else if (value[0] == '{' && *(skip_over_blanks(value+1)) == 0)
- do_subsection++;
- else {
- cp = value + strlen(value) - 1;
- while ((cp > value) && isspace((int) (*cp)))
- *cp-- = 0;
+ } else if (value[0] == '{') {
+ cp = skip_over_blanks(value+1);
+ ch = *cp;
+ if (end_or_comment(ch))
+ do_subsection++;
+ else
+ return PROF_RELATION_SYNTAX;
+ } else {
+ cp = skip_over_nonblanks(value);
+ p = skip_over_blanks(cp);
+ ch = *p;
+ *cp = 0;
+ if (!end_or_comment(ch))
+ return PROF_RELATION_SYNTAX;
}
if (do_subsection) {
p = strchr(tag, '*');
if (retval)
return retval;
if (p)
- profile_make_node_final(state->current_section);
+ state->current_section->final = 1;
state->group_level++;
return 0;
}
*p = '\0';
profile_add_node(state->current_section, tag, value, &node);
if (p)
- profile_make_node_final(node);
- return 0;
-}
-
-static errcode_t parse_line(char *line, struct parse_state *state)
-{
- char *cp;
-
- switch (state->state) {
- case STATE_INIT_COMMENT:
- if (line[0] != '[')
- return 0;
- state->state = STATE_STD_LINE;
- case STATE_STD_LINE:
- return parse_std_line(line, state);
- case STATE_GET_OBRACE:
- cp = skip_over_blanks(line);
- if (*cp != '{')
- return PROF_MISSING_OBRACE;
- state->state = STATE_STD_LINE;
- }
- return 0;
-}
-
-errcode_t profile_parse_file(FILE *f, struct profile_node **root)
-{
-#define BUF_SIZE 2048
- char *bptr;
- errcode_t retval;
- struct parse_state state;
-
- bptr = malloc (BUF_SIZE);
- if (!bptr)
- return ENOMEM;
-
- retval = parse_init_state(&state);
- if (retval) {
- free (bptr);
- return retval;
- }
- while (!feof(f)) {
- if (fgets(bptr, BUF_SIZE, f) == NULL)
- break;
-#ifndef PROFILE_SUPPORTS_FOREIGN_NEWLINES
- retval = parse_line(bptr, &state);
- if (retval) {
- free (bptr);
- return retval;
- }
-#else
- {
- char *p, *end;
-
- if (strlen(bptr) >= BUF_SIZE - 1) {
- /* The string may have foreign newlines and
- gotten chopped off on a non-newline
- boundary. Seek backwards to the last known
- newline. */
- long offset;
- char *c = bptr + strlen (bptr);
- for (offset = 0; offset > -BUF_SIZE; offset--) {
- if (*c == '\r' || *c == '\n') {
- *c = '\0';
- fseek (f, offset, SEEK_CUR);
- break;
- }
- c--;
- }
- }
-
- /* First change all newlines to \n */
- for (p = bptr; *p != '\0'; p++) {
- if (*p == '\r')
- *p = '\n';
- }
- /* Then parse all lines */
- p = bptr;
- end = bptr + strlen (bptr);
- while (p < end) {
- char* newline;
- char* newp;
-
- newline = strchr (p, '\n');
- if (newline != NULL)
- *newline = '\0';
-
- /* parse_line modifies contents of p */
- newp = p + strlen (p) + 1;
- retval = parse_line (p, &state);
- if (retval) {
- free (bptr);
- return retval;
- }
-
- p = newp;
- }
- }
-#endif
- }
- *root = state.root_section;
-
- free (bptr);
+ node->final = 1;
return 0;
}
return 0;
if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
return 1;
- if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b'))
+ if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
+ strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
return 1;
return 0;
}
struct profile_node *p;
void *iter;
long retval;
- char *name, *value;
iter = 0;
do {
- retval = profile_find_node_relation(root, 0, &iter,
- &name, &value);
+ retval = profile_find_node(root, 0, 0, 0, &iter, &p);
if (retval)
break;
for (i=0; i < level; i++)
cb("\t", data);
- if (need_double_quotes(value)) {
- cb(name, data);
- cb(" = ", data);
- output_quoted_string(value, cb, data);
- cb(EOL, data);
- } else {
- cb(name, data);
- cb(" = ", data);
- cb(value, data);
- cb(EOL, data);
- }
+ if (need_double_quotes(p->name))
+ output_quoted_string(p->name, cb, data);
+ else
+ cb(p->name, data);
+ cb(" = ", data);
+ if (need_double_quotes(p->value))
+ output_quoted_string(p->value, cb, data);
+ else
+ cb(p->value, data);
+ cb(EOL, data);
} while (iter != 0);
iter = 0;
do {
- retval = profile_find_node_subsection(root, 0, &iter,
- &name, &p);
+ retval = profile_find_node(root, 0, 0, 1, &iter, &p);
if (retval)
break;
if (level == 0) { /* [xxx] */
cb("[", data);
- cb(name, data);
+ if (need_double_quotes(p->name))
+ output_quoted_string(p->name, cb, data);
+ else
+ cb(p->name, data);
cb("]", data);
- cb(profile_is_node_final(p) ? "*" : "", data);
+ cb(p->final ? "*" : "", data);
cb(EOL, data);
dump_profile(p, level+1, cb, data);
cb(EOL, data);
} else { /* xxx = { ... } */
for (i=0; i < level; i++)
cb("\t", data);
- cb(name, data);
+ if (need_double_quotes(p->name))
+ output_quoted_string(p->name, cb, data);
+ else
+ cb(p->name, data);
cb(" = {", data);
cb(EOL, data);
dump_profile(p, level+1, cb, data);
for (i=0; i < level; i++)
cb("\t", data);
cb("}", data);
- cb(profile_is_node_final(p) ? "*" : "", data);
+ cb(p->final ? "*" : "", data);
cb(EOL, data);
}
} while (iter != 0);
}
#endif
-/* End prof_parse.c */
-
-/* Begin prof_tree.c */
-
/*
* prof_tree.c --- these routines maintain the parse tree of the
* config file.
*
*/
-struct profile_node {
- errcode_t magic;
- char *name;
- char *value;
- int group_level;
- int final:1; /* Indicate don't search next file */
- int deleted:1;
- struct profile_node *first_child;
- struct profile_node *parent;
- struct profile_node *next, *prev;
-};
-
-#define CHECK_MAGIC(node) \
- if ((node)->magic != PROF_MAGIC_NODE) \
- return PROF_MAGIC_NODE;
-
/*
* Free a node, and any children
*/
}
/*
- * Set the final flag on a particular node.
- */
-errcode_t profile_make_node_final(struct profile_node *node)
-{
- CHECK_MAGIC(node);
-
- node->final = 1;
- return 0;
-}
-
-#ifdef DEBUG_PROGRAM
-/*
- * Check the final flag on a node
- */
-int profile_is_node_final(struct profile_node *node)
-{
- return (node->final != 0);
-}
-
-/*
- * Return the name of a node. (Note: this is for internal functions
- * only; if the name needs to be returned from an exported function,
- * strdup it first!)
- */
-const char *profile_get_node_name(struct profile_node *node)
-{
- return node->name;
-}
-
-/*
- * Return the value of a node. (Note: this is for internal functions
- * only; if the name needs to be returned from an exported function,
- * strdup it first!)
- */
-const char *profile_get_node_value(struct profile_node *node)
-{
- return node->value;
-}
-#endif
-
-/*
* Iterate through the section, returning the nodes which match
* the given name. If name is NULL, then interate through all the
* nodes in the section. If section_flag is non-zero, only return the
return 0;
}
-
-#ifdef DEBUG_PROGRAM
-/*
- * Iterate through the section, returning the relations which match
- * the given name. If name is NULL, then interate through all the
- * relations in the section. The first time this routine is called,
- * the state pointer must be null. When this profile_find_node_relation()
- * returns, if the state pointer is non-NULL, then this routine should
- * be called again.
- *
- * The returned character string in value points to the stored
- * character string in the parse string. Before this string value is
- * returned to a calling application (profile_find_node_relation is not an
- * exported interface), it should be strdup()'ed.
- */
-errcode_t profile_find_node_relation(struct profile_node *section,
- const char *name, void **state,
- char **ret_name, char **value)
-{
- struct profile_node *p;
- errcode_t retval;
-
- retval = profile_find_node(section, name, 0, 0, state, &p);
- if (retval)
- return retval;
-
- if (p) {
- if (value)
- *value = p->value;
- if (ret_name)
- *ret_name = p->name;
- }
- return 0;
-}
-#endif
-
-/*
- * Iterate through the section, returning the subsections which match
- * the given name. If name is NULL, then interate through all the
- * subsections in the section. The first time this routine is called,
- * the state pointer must be null. When this profile_find_node_subsection()
- * returns, if the state pointer is non-NULL, then this routine should
- * be called again.
- *
- * This is (plus accessor functions for the name and value given a
- * profile node) makes this function mostly syntactic sugar for
- * profile_find_node.
- */
-errcode_t profile_find_node_subsection(struct profile_node *section,
- const char *name, void **state,
- char **ret_name,
- struct profile_node **subsection)
-{
- struct profile_node *p;
- errcode_t retval;
-
- retval = profile_find_node(section, name, 0, 1, state, &p);
- if (retval)
- return retval;
-
- if (p) {
- if (subsection)
- *subsection = p;
- if (ret_name)
- *ret_name = p->name;
- }
- return 0;
-}
-
-/*
- * This function returns the parent of a particular node.
- */
-errcode_t profile_get_node_parent(struct profile_node *section,
- struct profile_node **parent)
-{
- *parent = section->parent;
- return 0;
-}
-
/*
* This is a general-purpose iterator for returning all nodes that
* match the specified name array.
int num;
};
-errcode_t profile_node_iterator_create(profile_t profile,
- const char *const *names, int flags,
- void **ret_iter)
+errcode_t
+profile_iterator_create(profile_t profile, const char *const *names, int flags,
+ void **ret_iter)
{
struct profile_iterator *iter;
int done_idx = 0;
return 0;
}
-void profile_node_iterator_free(void **iter_p)
+void profile_iterator_free(void **iter_p)
{
struct profile_iterator *iter;
return PROF_MAGIC_ITERATOR;
if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
return PROF_MAGIC_FILE;
- if (iter->file && iter->file->data->magic != PROF_MAGIC_FILE_DATA)
- return PROF_MAGIC_FILE_DATA;
/*
* If the file has changed, then the node pointer is invalid,
* so we'll have search the file again looking for it.
*/
- if (iter->node && (iter->file->data->upd_serial != iter->file_serial)) {
+ if (iter->node && (iter->file &&
+ iter->file->upd_serial != iter->file_serial)) {
iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
skip_num = iter->num;
iter->node = 0;
if (iter->node == 0) {
if (iter->file == 0 ||
(iter->flags & PROFILE_ITER_FINAL_SEEN)) {
- profile_node_iterator_free(iter_p);
+ profile_iterator_free(iter_p);
if (ret_node)
*ret_node = 0;
if (ret_name)
retval = 0;
goto get_new_file;
} else {
- profile_node_iterator_free(iter_p);
+ profile_iterator_free(iter_p);
return retval;
}
}
- iter->file_serial = iter->file->data->upd_serial;
+ iter->file_serial = iter->file->upd_serial;
/*
* Find the section to list if we are a LIST_SECTION,
* or find the containing section if not.
*/
- section = iter->file->data->root;
+ section = iter->file->root;
for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
for (p=section->first_child; p; p = p->next) {
if (!strcmp(p->name, *cpp) && !p->value)
iter->num++;
if (!p) {
iter->file = iter->file->next;
- if (iter->file) {
- }
iter->node = 0;
skip_num = 0;
goto get_new_file;
}
-
-/* End prof_tree.c */
-
-
-/* Begin prof_get.c */
/*
* prof_get.c --- routines that expose the public interfaces for
* querying items from the profile.
*/
/*
- * These functions --- init_list(), end_list(), and add_to_list() are
- * internal functions used to build up a null-terminated char ** list
- * of strings to be returned by functions like profile_get_values.
- *
- * The profile_string_list structure is used for internal booking
- * purposes to build up the list, which is returned in *ret_list by
- * the end_list() function.
- *
- * The publicly exported interface for freeing char** list is
- * profile_free_list().
- */
-
-struct profile_string_list {
- char **list;
- int num;
- int max;
-};
-
-/*
- * Initialize the string list abstraction.
- */
-static errcode_t init_list(struct profile_string_list *list)
-{
- list->num = 0;
- list->max = 10;
- list->list = malloc(list->max * sizeof(char *));
- if (list->list == 0)
- return ENOMEM;
- list->list[0] = 0;
- return 0;
-}
-
-/*
- * Free any memory left over in the string abstraction, returning the
- * built up list in *ret_list if it is non-null.
- */
-static void end_list(struct profile_string_list *list, char ***ret_list)
-{
- char **cp;
-
- if (list == 0)
- return;
-
- if (ret_list) {
- *ret_list = list->list;
- return;
- } else {
- for (cp = list->list; *cp; cp++)
- free(*cp);
- free(list->list);
- }
- list->num = list->max = 0;
- list->list = 0;
-}
-
-/*
- * Add a string to the list.
- */
-static errcode_t add_to_list(struct profile_string_list *list, const char *str)
-{
- char *newstr, **newlist;
- int newmax;
-
- if (list->num+1 >= list->max) {
- newmax = list->max + 10;
- newlist = realloc(list->list, newmax * sizeof(char *));
- if (newlist == 0)
- return ENOMEM;
- list->max = newmax;
- list->list = newlist;
- }
- newstr = malloc(strlen(str)+1);
- if (newstr == 0)
- return ENOMEM;
- strcpy(newstr, str);
-
- list->list[list->num++] = newstr;
- list->list[list->num] = 0;
- return 0;
-}
-
-/*
- * Return TRUE if the string is already a member of the list.
- */
-static int is_list_member(struct profile_string_list *list, const char *str)
-{
- char **cpp;
-
- if (!list->list)
- return 0;
-
- for (cpp = list->list; *cpp; cpp++) {
- if (!strcmp(*cpp, str))
- return 1;
- }
- return 0;
-}
-
-/*
- * This function frees a null-terminated list as returned by
- * profile_get_values.
- */
-void profile_free_list(char **list)
-{
- char **cp;
-
- if (list == 0)
- return;
-
- for (cp = list; *cp; cp++)
- free(*cp);
- free(list);
-}
-
-errcode_t
-profile_get_values(profile_t profile, const char *const *names,
- char ***ret_values)
-{
- errcode_t retval;
- void *state;
- char *value;
- struct profile_string_list values;
-
- if ((retval = profile_node_iterator_create(profile, names,
- PROFILE_ITER_RELATIONS_ONLY,
- &state)))
- return retval;
-
- if ((retval = init_list(&values)))
- return retval;
-
- do {
- if ((retval = profile_node_iterator(&state, 0, 0, &value)))
- goto cleanup;
- if (value)
- add_to_list(&values, value);
- } while (state);
-
- if (values.num == 0) {
- retval = PROF_NO_RELATION;
- goto cleanup;
- }
-
- end_list(&values, ret_values);
- return 0;
-
-cleanup:
- end_list(&values, 0);
- return retval;
-}
-
-/*
* This function only gets the first value from the file; it is a
* helper function for profile_get_string, profile_get_integer, etc.
*/
-errcode_t profile_get_value(profile_t profile, const char **names,
+errcode_t profile_get_value(profile_t profile, const char *name,
+ const char *subname, const char *subsubname,
const char **ret_value)
{
errcode_t retval;
void *state;
char *value;
+ const char *names[4];
- if ((retval = profile_node_iterator_create(profile, names,
- PROFILE_ITER_RELATIONS_ONLY,
- &state)))
+ names[0] = name;
+ names[1] = subname;
+ names[2] = subsubname;
+ names[3] = 0;
+
+ if ((retval = profile_iterator_create(profile, names,
+ PROFILE_ITER_RELATIONS_ONLY,
+ &state)))
return retval;
if ((retval = profile_node_iterator(&state, 0, 0, &value)))
retval = PROF_NO_RELATION;
cleanup:
- profile_node_iterator_free(&state);
+ profile_iterator_free(&state);
return retval;
}
{
const char *value;
errcode_t retval;
- const char *names[4];
if (profile) {
- names[0] = name;
- names[1] = subname;
- names[2] = subsubname;
- names[3] = 0;
- retval = profile_get_value(profile, names, &value);
+ retval = profile_get_value(profile, name, subname,
+ subsubname, &value);
if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
value = def_val;
else if (retval)
{
const char *value;
errcode_t retval;
- const char *names[4];
char *end_value;
long ret_long;
if (profile == 0)
return 0;
- names[0] = name;
- names[1] = subname;
- names[2] = subsubname;
- names[3] = 0;
- retval = profile_get_value(profile, names, &value);
+ retval = profile_get_value(profile, name, subname, subsubname, &value);
if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
*ret_int = def_val;
return 0;
return 0;
}
+errcode_t
+profile_get_uint(profile_t profile, const char *name, const char *subname,
+ const char *subsubname, unsigned int def_val,
+ unsigned int *ret_int)
+{
+ const char *value;
+ errcode_t retval;
+ char *end_value;
+ unsigned long ret_long;
+
+ *ret_int = def_val;
+ if (profile == 0)
+ return 0;
+
+ retval = profile_get_value(profile, name, subname, subsubname, &value);
+ if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
+ *ret_int = def_val;
+ return 0;
+ } else if (retval)
+ return retval;
+
+ if (value[0] == 0)
+ /* Empty string is no good. */
+ return PROF_BAD_INTEGER;
+ errno = 0;
+ ret_long = strtoul (value, &end_value, 10);
+
+ /* Overflow or underflow. */
+ if ((ret_long == ULONG_MAX) && errno != 0)
+ return PROF_BAD_INTEGER;
+ /* Value outside "int" range. */
+ if ((unsigned long) (unsigned int) ret_long != ret_long)
+ return PROF_BAD_INTEGER;
+ /* Garbage in string. */
+ if (end_value != value + strlen (value))
+ return PROF_BAD_INTEGER;
+
+ *ret_int = ret_long;
+ return 0;
+}
+
static const char *const conf_yes[] = {
"y", "yes", "true", "t", "1", "on",
0,
{
const char *value;
errcode_t retval;
- const char *names[4];
if (profile == 0) {
*ret_boolean = def_val;
return 0;
}
- names[0] = name;
- names[1] = subname;
- names[2] = subsubname;
- names[3] = 0;
- retval = profile_get_value(profile, names, &value);
+ retval = profile_get_value(profile, name, subname, subsubname, &value);
if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
*ret_boolean = def_val;
return 0;
return profile_parse_boolean (value, ret_boolean);
}
-/*
- * This function will return the list of the names of subections in the
- * under the specified section name.
- */
-errcode_t
-profile_get_subsection_names(profile_t profile, const char **names,
- char ***ret_names)
-{
- errcode_t retval;
- void *state;
- char *name;
- struct profile_string_list values;
-
- if ((retval = profile_node_iterator_create(profile, names,
- PROFILE_ITER_LIST_SECTION | PROFILE_ITER_SECTIONS_ONLY,
- &state)))
- return retval;
-
- if ((retval = init_list(&values)))
- return retval;
-
- do {
- if ((retval = profile_node_iterator(&state, 0, &name, 0)))
- goto cleanup;
- if (name)
- add_to_list(&values, name);
- } while (state);
-
- end_list(&values, ret_names);
- return 0;
-
-cleanup:
- end_list(&values, 0);
- return retval;
-}
-
-/*
- * This function will return the list of the names of relations in the
- * under the specified section name.
- */
-errcode_t
-profile_get_relation_names(profile_t profile, const char **names,
- char ***ret_names)
-{
- errcode_t retval;
- void *state;
- char *name;
- struct profile_string_list values;
-
- if ((retval = profile_node_iterator_create(profile, names,
- PROFILE_ITER_LIST_SECTION | PROFILE_ITER_RELATIONS_ONLY,
- &state)))
- return retval;
-
- if ((retval = init_list(&values)))
- return retval;
-
- do {
- if ((retval = profile_node_iterator(&state, 0, &name, 0)))
- goto cleanup;
- if (name && !is_list_member(&values, name))
- add_to_list(&values, name);
- } while (state);
-
- end_list(&values, ret_names);
- return 0;
-
-cleanup:
- end_list(&values, 0);
- return retval;
-}
-
-errcode_t
-profile_iterator_create(profile_t profile, const char *const *names, int flags,
- void **ret_iter)
-{
- return profile_node_iterator_create(profile, names, flags, ret_iter);
-}
-
-void
-profile_iterator_free(void **iter_p)
-{
- profile_node_iterator_free(iter_p);
-}
-
errcode_t
profile_iterator(void **iter_p, char **ret_name, char **ret_value)
{
return 0;
}
-void
-profile_release_string(char *str)
-{
- free(str);
-}
-
-/* End prof_get.c */
-
#ifdef DEBUG_PROGRAM
/*
*/
#include "argv_parse.h"
+#include "profile_helpers.h"
const char *program_name = "test_profile";
retval = profile_get_values(profile, names, &values);
print_status = PRINT_VALUES;
} else if (!strcmp(cmd, "query1")) {
- retval = profile_get_value(profile, names, &value);
+ const char *name = 0;
+ const char *subname = 0;
+ const char *subsubname = 0;
+
+ name = names[0];
+ if (name)
+ subname = names[1];
+ if (subname)
+ subsubname = names[2];
+ if (subsubname && names[3]) {
+ fprintf(stderr,
+ "Only 3 levels are allowed with query1\n");
+ retval = EINVAL;
+ } else
+ retval = profile_get_value(profile, name, subname,
+ subsubname, &value);
print_status = PRINT_VALUE;
} else if (!strcmp(cmd, "list_sections")) {
retval = profile_get_subsection_names(profile, names,
print_status = PRINT_VALUES;
} else if (!strcmp(cmd, "dump")) {
retval = profile_write_tree_file
- (profile->first_file->data->root, stdout);
+ (profile->first_file->root, stdout);
#if 0
} else if (!strcmp(cmd, "clear")) {
retval = profile_clear_relation(profile, names);
#endif
} else if (!strcmp(cmd, "verify")) {
retval = profile_verify_node
- (profile->first_file->data->root);
+ (profile->first_file->root);
#if 0
} else if (!strcmp(cmd, "rename_section")) {
retval = profile_rename_section(profile, names+1, *names);
}
+void syntax_err_report(const char *filename, long err, int line_num)
+{
+ fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
+ filename, line_num, error_message(err));
+ exit(1);
+}
+
+const char *default_str = "[foo]\n\tbar=quux\n\tsub = {\n\t\twin = true\n}\n";
int main(int argc, char **argv)
{
}
initialize_prof_error_table();
+
+ profile_set_syntax_err_cb(syntax_err_report);
retval = profile_init_path(argv[1], &profile);
if (retval) {
com_err(program_name, retval, "while initializing profile");
exit(1);
}
+ retval = profile_set_default(profile, default_str);
+ if (retval) {
+ com_err(program_name, retval, "while setting default");
+ exit(1);
+ }
+
cmd = *(argv+2);
if (!cmd || !strcmp(cmd, "batch"))
do_batchmode(profile);