2 * profile.c -- A simple configuration file parsing "library in a file"
4 * The profile library was originally written by Theodore Ts'o in 1995
5 * for use in the MIT Kerberos v5 library. It has been
6 * modified/enhanced/bug-fixed over time by other members of the MIT
7 * Kerberos team. This version was originally taken from the Kerberos
8 * v5 distribution, version 1.4.2, and radically simplified for use in
9 * e2fsprogs. (Support for locking for multi-threaded operations,
10 * being able to modify and update the configuration file
11 * programmatically, and Mac/Windows portability have been removed.
12 * It has been folded into a single C source file to make it easier to
13 * fold into an application program.)
15 * Copyright (C) 2005, 2006 by Theodore Ts'o.
18 * This file may be redistributed under the terms of the GNU Public
22 * Copyright (C) 1985-2005 by the Massachusetts Institute of Technology.
24 * All rights reserved.
26 * Export of this software from the United States of America may require
27 * a specific license from the United States Government. It is the
28 * responsibility of any person or organization contemplating export to
29 * obtain such a license before exporting.
31 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
32 * distribute this software and its documentation for any purpose and
33 * without fee is hereby granted, provided that the above copyright
34 * notice appear in all copies and that both that copyright notice and
35 * this permission notice appear in supporting documentation, and that
36 * the name of M.I.T. not be used in advertising or publicity pertaining
37 * to distribution of the software without specific, written prior
38 * permission. Furthermore if you modify this software you must label
39 * your software as modified software and not distribute it in such a
40 * fashion that it might be confused with the original MIT software.
41 * M.I.T. makes no representations about the suitability of this software
42 * for any purpose. It is provided "as is" without express or implied
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
47 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
66 #include <sys/types.h>
73 #include <et/com_err.h>
77 #undef STAT_ONCE_PER_SECOND
84 typedef long prf_magic_t;
87 * This is the structure which stores the profile information for a
88 * particular configuration file.
93 #ifdef STAT_ONCE_PER_SECOND
96 time_t timestamp; /* time tree was last updated from file */
97 int flags; /* r/w, dirty */
98 int upd_serial; /* incremented when data changes */
99 struct profile_node *root;
100 struct _prf_file_t *next;
103 typedef struct _prf_file_t *prf_file_t;
108 #define PROFILE_FILE_RW 0x0001
109 #define PROFILE_FILE_DIRTY 0x0002
110 #define PROFILE_FILE_NO_RELOAD 0x0004
113 * This structure defines the high-level, user visible profile_t
114 * object, which is used as a handle by users who need to query some
115 * configuration file(s)
119 prf_file_t first_file;
123 * Used by the profile iterator in prof_get.c
125 #define PROFILE_ITER_LIST_SECTION 0x0001
126 #define PROFILE_ITER_SECTIONS_ONLY 0x0002
127 #define PROFILE_ITER_RELATIONS_ONLY 0x0004
129 #define PROFILE_ITER_FINAL_SEEN 0x0100
132 * Check if a filespec is last in a list (NULL on UNIX, invalid FSSpec on MacOS
135 #define PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
137 struct profile_node {
142 unsigned int final:1; /* Indicate don't search next file */
143 unsigned int deleted:1;
144 struct profile_node *first_child;
145 struct profile_node *parent;
146 struct profile_node *next, *prev;
149 #define CHECK_MAGIC(node) \
150 if ((node)->magic != PROF_MAGIC_NODE) \
151 return PROF_MAGIC_NODE;
153 /* profile parser declarations */
158 struct profile_node *root_section;
159 struct profile_node *current_section;
162 static const char *default_filename = "<default>";
164 static profile_syntax_err_cb_t syntax_err_cb;
166 static errcode_t parse_line(char *line, struct parse_state *state);
169 static errcode_t profile_write_tree_file
170 (struct profile_node *root, FILE *dstfile);
172 static errcode_t profile_write_tree_to_buffer
173 (struct profile_node *root, char **buf);
177 static void profile_free_node
178 (struct profile_node *relation);
180 static errcode_t profile_create_node
181 (const char *name, const char *value,
182 struct profile_node **ret_node);
185 static errcode_t profile_verify_node
186 (struct profile_node *node);
189 static errcode_t profile_add_node
190 (struct profile_node *section,
191 const char *name, const char *value,
192 struct profile_node **ret_node);
194 static errcode_t profile_find_node
195 (struct profile_node *section,
196 const char *name, const char *value,
197 int section_flag, void **state,
198 struct profile_node **node);
200 static errcode_t profile_node_iterator
201 (void **iter_p, struct profile_node **ret_node,
202 char **ret_name, char **ret_value);
204 static errcode_t profile_open_file
205 (const char * file, prf_file_t *ret_prof);
207 static errcode_t profile_update_file
210 static void profile_free_file
211 (prf_file_t profile);
213 static errcode_t profile_get_value(profile_t profile, const char *name,
214 const char *subname, const char *subsubname,
215 const char **ret_value);
219 * prof_init.c --- routines that manipulate the user-visible profile_t
223 static int compstr(const void *m1, const void *m2)
225 const char *s1 = *((const char * const *) m1);
226 const char *s2 = *((const char * const *) m2);
228 return strcmp(s1, s2);
231 static void free_list(char **list)
238 for (cp = list; *cp; cp++)
243 static errcode_t get_dirlist(const char *dirname, char***ret_array)
250 char **array = 0, **new_array;
251 int max = 0, num = 0;
253 dir = opendir(dirname);
257 while ((de = readdir(dir)) != NULL) {
258 for (cp = de->d_name; *cp; cp++) {
266 fn = malloc(strlen(dirname) + strlen(de->d_name) + 2);
271 sprintf(fn, "%s/%s", dirname, de->d_name);
272 if ((stat(fn, &st) < 0) || !S_ISREG(st.st_mode)) {
278 new_array = realloc(array, sizeof(char *) * (max+1));
289 qsort(array, num, sizeof(char *), compstr);
304 profile_init(const char * const *files, profile_t *ret_profile)
306 const char * const *fs;
308 prf_file_t new_file, *last;
309 errcode_t retval = 0;
310 char **cpp, *cp, **array = 0;
312 profile = malloc(sizeof(struct _profile_t));
315 memset(profile, 0, sizeof(struct _profile_t));
316 profile->magic = PROF_MAGIC_PROFILE;
317 last = &profile->first_file;
319 /* if the filenames list is not specified return an empty profile */
321 for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) {
325 retval = get_dirlist(*fs, &array);
329 for (cpp = array; (cp = *cpp); cpp++) {
330 retval = profile_open_file(cp, &new_file);
331 if (retval == EACCES)
336 last = &new_file->next;
338 } else if ((retval != ENOTDIR) &&
339 strcmp(*fs, default_filename))
342 retval = profile_open_file(*fs, &new_file);
343 /* if this file is missing, skip to the next */
344 if (retval == ENOENT || retval == EACCES) {
350 last = &new_file->next;
353 * If all the files were not found, return the appropriate error.
355 if (!profile->first_file) {
362 *ret_profile = profile;
366 profile_release(profile);
371 profile_release(profile_t profile)
375 if (!profile || profile->magic != PROF_MAGIC_PROFILE)
378 for (p = profile->first_file; p; p = next) {
380 profile_free_file(p);
387 * This function sets the value of the pseudo file "<default>". If
388 * the file "<default>" had previously been passed to profile_init(),
389 * then def_string parameter will be parsed and used as the profile
390 * information for the "<default>" file.
392 errcode_t profile_set_default(profile_t profile, const char *def_string)
394 struct parse_state state;
398 char *line, *p, *end;
401 if (!def_string || !profile || profile->magic != PROF_MAGIC_PROFILE)
402 return PROF_MAGIC_PROFILE;
404 for (prf = profile->first_file; prf; prf = prf->next) {
405 if (strcmp(prf->filespec, default_filename) == 0)
412 profile_free_node(prf->root);
416 memset(&state, 0, sizeof(struct parse_state));
417 retval = profile_create_node("(root)", 0, &state.root_section);
425 end = strchr(in, '\n');
426 len = end ? (end - in) : (int) strlen(in);
427 if (len >= line_size) {
429 p = realloc(line, line_size);
436 memcpy(line, in, len);
438 retval = parse_line(line, &state);
442 (syntax_err_cb)(prf->filespec, retval,
446 profile_free_node(prf->root);
453 prf->root = state.root_section;
460 * prof_file.c ---- routines that manipulate an individual profile file.
463 errcode_t profile_open_file(const char * filespec,
464 prf_file_t *ret_prof)
470 char *expanded_filename;
472 prf = malloc(sizeof(struct _prf_file_t));
475 memset(prf, 0, sizeof(struct _prf_file_t));
476 prf->magic = PROF_MAGIC_FILE;
478 len = strlen(filespec)+1;
479 if (filespec[0] == '~' && filespec[1] == '/') {
480 home_env = getenv("HOME");
482 if (home_env == NULL) {
483 #ifdef HAVE_GETWUID_R
484 struct passwd *pw, pwx;
489 if (!getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
490 && pw != NULL && pw->pw_dir[0] != 0)
491 home_env = pw->pw_dir;
495 pw = getpwuid(getuid());
496 home_env = pw->pw_dir;
501 len += strlen(home_env);
503 expanded_filename = malloc(len);
504 if (expanded_filename == 0) {
505 profile_free_file(prf);
509 strcpy(expanded_filename, home_env);
510 strcat(expanded_filename, filespec+1);
512 memcpy(expanded_filename, filespec, len);
514 prf->filespec = expanded_filename;
516 if (strcmp(prf->filespec, default_filename) != 0) {
517 retval = profile_update_file(prf);
519 profile_free_file(prf);
528 errcode_t profile_update_file(prf_file_t prf)
533 #ifdef STAT_ONCE_PER_SECOND
539 struct parse_state state;
541 if (prf->flags & PROFILE_FILE_NO_RELOAD)
545 #ifdef STAT_ONCE_PER_SECOND
547 if (now == prf->last_stat && prf->root != NULL) {
551 if (stat(prf->filespec, &st)) {
555 #ifdef STAT_ONCE_PER_SECOND
556 prf->last_stat = now;
558 if (st.st_mtime == prf->timestamp && prf->root != NULL) {
562 profile_free_node(prf->root);
567 * If we don't have the stat() call, assume that our in-core
568 * memory image is correct. That is, we won't reread the
569 * profile file if it changes.
575 memset(&state, 0, sizeof(struct parse_state));
576 retval = profile_create_node("(root)", 0, &state.root_section);
580 f = fopen(prf->filespec, "r");
589 if (fgets(buf, sizeof(buf), f) == NULL)
591 retval = parse_line(buf, &state);
594 (syntax_err_cb)(prf->filespec, retval,
600 prf->root = state.root_section;
605 prf->timestamp = st.st_mtime;
610 void profile_free_file(prf_file_t prf)
613 profile_free_node(prf->root);
618 /* Begin the profile parser */
620 profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
622 profile_syntax_err_cb_t old;
625 syntax_err_cb = hook;
629 #define STATE_INIT_COMMENT 0
630 #define STATE_STD_LINE 1
631 #define STATE_GET_OBRACE 2
633 static char *skip_over_blanks(char *cp)
635 while (*cp && isspace((int) (*cp)))
640 static int end_or_comment(char ch)
642 return (ch == 0 || ch == '#' || ch == ';');
645 static char *skip_over_nonblanks(char *cp)
647 while (!end_or_comment(*cp) && !isspace(*cp))
652 static void strip_line(char *line)
654 char *p = line + strlen(line);
655 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
659 static void parse_quoted_string(char *str)
665 for (to = from = str; *from && *from != '"'; to++, from++) {
688 static errcode_t parse_line(char *line, struct parse_state *state)
690 char *cp, ch, *tag, *value;
693 struct profile_node *node;
694 int do_subsection = 0;
698 if (state->state == STATE_GET_OBRACE) {
699 cp = skip_over_blanks(line);
701 return PROF_MISSING_OBRACE;
702 state->state = STATE_STD_LINE;
705 if (state->state == STATE_INIT_COMMENT) {
708 state->state = STATE_STD_LINE;
714 cp = skip_over_blanks(line);
716 if (end_or_comment(ch))
719 if (state->group_level > 0)
720 return PROF_SECTION_NOTOP;
722 cp = skip_over_blanks(cp);
725 return PROF_SECTION_SYNTAX;
728 parse_quoted_string(cp);
731 while (isspace(*p) && (p > cp))
734 return PROF_SECTION_SYNTAX;
736 retval = profile_find_node(state->root_section, cp, 0, 1,
737 &iter, &state->current_section);
738 if (retval == PROF_NO_SECTION) {
739 retval = profile_add_node(state->root_section,
741 &state->current_section);
748 * Finish off the rest of the line.
752 state->current_section->final = 1;
756 * Spaces or comments after ']' should not be fatal
758 cp = skip_over_blanks(cp);
759 if (!end_or_comment(*cp))
760 return PROF_SECTION_SYNTAX;
764 if (state->group_level == 0)
765 return PROF_EXTRA_CBRACE;
767 state->current_section->final = 1;
768 state->current_section = state->current_section->parent;
769 state->group_level--;
773 * Parse the relations
776 cp = strchr(cp, '=');
778 return PROF_RELATION_SYNTAX;
780 return PROF_RELATION_SYNTAX;
784 parse_quoted_string(tag);
786 /* Look for whitespace on left-hand side. */
787 p = skip_over_nonblanks(tag);
790 p = skip_over_blanks(p);
791 /* If we have more non-whitespace, it's an error. */
793 return PROF_RELATION_SYNTAX;
796 cp = skip_over_blanks(cp+1);
801 parse_quoted_string(value);
802 } else if (end_or_comment(ch)) {
804 state->state = STATE_GET_OBRACE;
805 } else if (value[0] == '{') {
806 cp = skip_over_blanks(value+1);
808 if (end_or_comment(ch))
811 return PROF_RELATION_SYNTAX;
813 cp = skip_over_nonblanks(value);
814 p = skip_over_blanks(cp);
817 if (!end_or_comment(ch))
818 return PROF_RELATION_SYNTAX;
821 p = strchr(tag, '*');
824 retval = profile_add_node(state->current_section,
825 tag, 0, &state->current_section);
829 state->current_section->final = 1;
830 state->group_level++;
833 p = strchr(tag, '*');
836 profile_add_node(state->current_section, tag, value, &node);
844 * Return TRUE if the string begins or ends with whitespace
846 static int need_double_quotes(char *str)
850 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
852 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
853 strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
859 * Output a string with double quotes, doing appropriate backquoting
860 * of characters as necessary.
862 static void output_quoted_string(char *str, void (*cb)(const char *,void *),
874 while ((ch = *str++)) {
889 /* This would be a lot faster if we scanned
890 forward for the next "interesting"
904 /* Errors should be returned, not ignored! */
905 static void dump_profile(struct profile_node *root, int level,
906 void (*cb)(const char *, void *), void *data)
909 struct profile_node *p;
915 retval = profile_find_node(root, 0, 0, 0, &iter, &p);
918 for (i=0; i < level; i++)
920 if (need_double_quotes(p->name))
921 output_quoted_string(p->name, cb, data);
925 if (need_double_quotes(p->value))
926 output_quoted_string(p->value, cb, data);
934 retval = profile_find_node(root, 0, 0, 1, &iter, &p);
937 if (level == 0) { /* [xxx] */
939 if (need_double_quotes(p->name))
940 output_quoted_string(p->name, cb, data);
944 cb(p->final ? "*" : "", data);
946 dump_profile(p, level+1, cb, data);
948 } else { /* xxx = { ... } */
949 for (i=0; i < level; i++)
951 if (need_double_quotes(p->name))
952 output_quoted_string(p->name, cb, data);
957 dump_profile(p, level+1, cb, data);
958 for (i=0; i < level; i++)
961 cb(p->final ? "*" : "", data);
967 static void dump_profile_to_file_cb(const char *str, void *data)
972 errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
974 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
984 static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
988 if (b->max - b->cur < len) {
992 newsize = b->max + (b->max >> 1) + len + 1024;
993 newptr = realloc(b->base, newsize);
994 if (newptr == NULL) {
1001 memcpy(b->base + b->cur, d, len);
1002 b->cur += len; /* ignore overflow */
1005 static void dump_profile_to_buffer_cb(const char *str, void *data)
1007 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
1010 errcode_t profile_write_tree_to_buffer(struct profile_node *root,
1013 struct prof_buf prof_buf = { 0, 0, 0, 0 };
1015 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
1020 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
1021 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
1022 char *newptr = realloc(prof_buf.base, prof_buf.cur);
1024 prof_buf.base = newptr;
1026 *buf = prof_buf.base;
1032 * prof_tree.c --- these routines maintain the parse tree of the
1035 * All of the details of how the tree is stored is abstracted away in
1036 * this file; all of the other profile routines build, access, and
1037 * modify the tree via the accessor functions found in this file.
1039 * Each node may represent either a relation or a section header.
1041 * A section header must have its value field set to 0, and may a one
1042 * or more child nodes, pointed to by first_child.
1044 * A relation has as its value a pointer to allocated memory
1045 * containing a string. Its first_child pointer must be null.
1050 * Free a node, and any children
1052 void profile_free_node(struct profile_node *node)
1054 struct profile_node *child, *next;
1056 if (node->magic != PROF_MAGIC_NODE)
1062 for (child=node->first_child; child; child = next) {
1064 profile_free_node(child);
1073 #define strdup MYstrdup
1074 static char *MYstrdup (const char *s)
1076 size_t sz = strlen(s) + 1;
1077 char *p = malloc(sz);
1087 errcode_t profile_create_node(const char *name, const char *value,
1088 struct profile_node **ret_node)
1090 struct profile_node *new;
1092 new = malloc(sizeof(struct profile_node));
1095 memset(new, 0, sizeof(struct profile_node));
1096 new->magic = PROF_MAGIC_NODE;
1097 new->name = strdup(name);
1098 if (new->name == 0) {
1099 profile_free_node(new);
1103 new->value = strdup(value);
1104 if (new->value == 0) {
1105 profile_free_node(new);
1115 * This function verifies that all of the representation invariants of
1116 * the profile are true. If not, we have a programming bug somewhere,
1117 * probably in this file.
1119 #ifdef DEBUG_PROGRAM
1120 errcode_t profile_verify_node(struct profile_node *node)
1122 struct profile_node *p, *last;
1127 if (node->value && node->first_child)
1128 return PROF_SECTION_WITH_VALUE;
1131 for (p = node->first_child; p; last = p, p = p->next) {
1132 if (p->prev != last)
1133 return PROF_BAD_LINK_LIST;
1134 if (last && (last->next != p))
1135 return PROF_BAD_LINK_LIST;
1136 if (node->group_level+1 != p->group_level)
1137 return PROF_BAD_GROUP_LVL;
1138 if (p->parent != node)
1139 return PROF_BAD_PARENT_PTR;
1140 retval = profile_verify_node(p);
1149 * Add a node to a particular section
1151 errcode_t profile_add_node(struct profile_node *section, const char *name,
1152 const char *value, struct profile_node **ret_node)
1155 struct profile_node *p, *last, *new;
1157 CHECK_MAGIC(section);
1160 return PROF_ADD_NOT_SECTION;
1163 * Find the place to insert the new node. We look for the
1164 * place *after* the last match of the node name, since
1167 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1169 cmp = strcmp(p->name, name);
1173 retval = profile_create_node(name, value, &new);
1176 new->group_level = section->group_level+1;
1178 new->parent = section;
1186 section->first_child = new;
1193 * Iterate through the section, returning the nodes which match
1194 * the given name. If name is NULL, then interate through all the
1195 * nodes in the section. If section_flag is non-zero, only return the
1196 * section which matches the name; don't return relations. If value
1197 * is non-NULL, then only return relations which match the requested
1198 * value. (The value argument is ignored if section_flag is non-zero.)
1200 * The first time this routine is called, the state pointer must be
1201 * null. When this profile_find_node_relation() returns, if the state
1202 * pointer is non-NULL, then this routine should be called again.
1203 * (This won't happen if section_flag is non-zero, obviously.)
1206 errcode_t profile_find_node(struct profile_node *section, const char *name,
1207 const char *value, int section_flag, void **state,
1208 struct profile_node **node)
1210 struct profile_node *p;
1212 CHECK_MAGIC(section);
1217 p = section->first_child;
1219 for (; p; p = p->next) {
1220 if (name && (strcmp(p->name, name)))
1228 if (value && (strcmp(p->value, value)))
1240 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1243 * OK, we've found one match; now let's try to find another
1244 * one. This way, if we return a non-zero state pointer,
1245 * there's guaranteed to be another match that's returned.
1247 for (p = p->next; p; p = p->next) {
1248 if (name && (strcmp(p->name, name)))
1256 if (value && (strcmp(p->value, value)))
1267 * This is a general-purpose iterator for returning all nodes that
1268 * match the specified name array.
1270 struct profile_iterator {
1274 const char *const *names;
1279 struct profile_node *node;
1284 profile_iterator_create(profile_t profile, const char *const *names, int flags,
1287 struct profile_iterator *iter;
1291 return PROF_NO_PROFILE;
1292 if (profile->magic != PROF_MAGIC_PROFILE)
1293 return PROF_MAGIC_PROFILE;
1295 return PROF_BAD_NAMESET;
1296 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1298 return PROF_BAD_NAMESET;
1302 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1305 iter->magic = PROF_MAGIC_ITERATOR;
1306 iter->profile = profile;
1307 iter->names = names;
1308 iter->flags = flags;
1309 iter->file = profile->first_file;
1310 iter->done_idx = done_idx;
1317 void profile_iterator_free(void **iter_p)
1319 struct profile_iterator *iter;
1324 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1331 * Note: the returned character strings in ret_name and ret_value
1332 * points to the stored character string in the parse string. Before
1333 * this string value is returned to a calling application
1334 * (profile_node_iterator is not an exported interface), it should be
1337 errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1338 char **ret_name, char **ret_value)
1340 struct profile_iterator *iter = *iter_p;
1341 struct profile_node *section, *p;
1342 const char *const *cpp;
1346 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1347 return PROF_MAGIC_ITERATOR;
1348 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1349 return PROF_MAGIC_FILE;
1351 * If the file has changed, then the node pointer is invalid,
1352 * so we'll have search the file again looking for it.
1354 if (iter->node && (iter->file &&
1355 iter->file->upd_serial != iter->file_serial)) {
1356 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1357 skip_num = iter->num;
1360 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1361 return PROF_MAGIC_NODE;
1364 if (iter->node == 0) {
1365 if (iter->file == NULL ||
1366 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
1367 profile_iterator_free(iter_p);
1376 if ((retval = profile_update_file(iter->file))) {
1377 if (retval == ENOENT || retval == EACCES) {
1378 /* XXX memory leak? */
1380 iter->file = iter->file->next;
1385 profile_iterator_free(iter_p);
1389 iter->file_serial = iter->file->upd_serial;
1391 * Find the section to list if we are a LIST_SECTION,
1392 * or find the containing section if not.
1394 section = iter->file->root;
1395 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1396 for (p=section->first_child; p; p = p->next) {
1397 if (!strcmp(p->name, *cpp) && !p->value)
1406 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1410 iter->file = iter->file->next;
1415 iter->node = section->first_child;
1418 * OK, now we know iter->node is set up correctly. Let's do
1421 for (p = iter->node; p; p = p->next) {
1422 if (iter->name && strcmp(p->name, iter->name))
1424 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1427 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1441 iter->file = iter->file->next;
1446 if ((iter->node = p->next) == NULL)
1448 iter->file = iter->file->next;
1452 *ret_name = p->name;
1454 *ret_value = p->value;
1460 * prof_get.c --- routines that expose the public interfaces for
1461 * querying items from the profile.
1466 * This function only gets the first value from the file; it is a
1467 * helper function for profile_get_string, profile_get_integer, etc.
1469 errcode_t profile_get_value(profile_t profile, const char *name,
1470 const char *subname, const char *subsubname,
1471 const char **ret_value)
1476 const char *names[4];
1480 names[2] = subsubname;
1483 if ((retval = profile_iterator_create(profile, names,
1484 PROFILE_ITER_RELATIONS_ONLY,
1488 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1494 retval = PROF_NO_RELATION;
1497 profile_iterator_free(&state);
1502 profile_get_string(profile_t profile, const char *name, const char *subname,
1503 const char *subsubname, const char *def_val,
1510 retval = profile_get_value(profile, name, subname,
1511 subsubname, &value);
1512 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1520 *ret_string = malloc(strlen(value)+1);
1521 if (*ret_string == 0)
1523 strcpy(*ret_string, value);
1530 profile_get_integer(profile_t profile, const char *name, const char *subname,
1531 const char *subsubname, int def_val, int *ret_int)
1542 retval = profile_get_value(profile, name, subname, subsubname, &value);
1543 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1550 /* Empty string is no good. */
1551 return PROF_BAD_INTEGER;
1553 ret_long = strtol(value, &end_value, 0);
1555 /* Overflow or underflow. */
1556 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1557 return PROF_BAD_INTEGER;
1558 /* Value outside "int" range. */
1559 if ((long) (int) ret_long != ret_long)
1560 return PROF_BAD_INTEGER;
1561 /* Garbage in string. */
1562 if (end_value != value + strlen (value))
1563 return PROF_BAD_INTEGER;
1566 *ret_int = ret_long;
1571 profile_get_uint(profile_t profile, const char *name, const char *subname,
1572 const char *subsubname, unsigned int def_val,
1573 unsigned int *ret_int)
1578 unsigned long ret_long;
1584 retval = profile_get_value(profile, name, subname, subsubname, &value);
1585 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1592 /* Empty string is no good. */
1593 return PROF_BAD_INTEGER;
1595 ret_long = strtoul(value, &end_value, 0);
1597 /* Overflow or underflow. */
1598 if ((ret_long == ULONG_MAX) && errno != 0)
1599 return PROF_BAD_INTEGER;
1600 /* Value outside "int" range. */
1601 if ((unsigned long) (unsigned int) ret_long != ret_long)
1602 return PROF_BAD_INTEGER;
1603 /* Garbage in string. */
1604 if (end_value != value + strlen (value))
1605 return PROF_BAD_INTEGER;
1607 *ret_int = ret_long;
1612 profile_get_double(profile_t profile, const char *name, const char *subname,
1613 const char *subsubname, double def_val, double *ret_double)
1620 *ret_double = def_val;
1624 retval = profile_get_value(profile, name, subname, subsubname, &value);
1625 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1626 *ret_double = def_val;
1632 /* Empty string is no good. */
1633 return PROF_BAD_INTEGER;
1635 double_val = strtod(value, &end_value);
1637 /* Overflow or underflow. */
1639 return PROF_BAD_INTEGER;
1640 /* Garbage in string. */
1641 if (end_value != value + strlen(value))
1642 return PROF_BAD_INTEGER;
1644 *ret_double = double_val;
1648 static const char *const conf_yes[] = {
1649 "y", "yes", "true", "t", "1", "on",
1653 static const char *const conf_no[] = {
1654 "n", "no", "false", "nil", "0", "off",
1659 profile_parse_boolean(const char *s, int *ret_boolean)
1661 const char *const *p;
1663 if (ret_boolean == NULL)
1666 for(p=conf_yes; *p; p++) {
1667 if (!strcasecmp(*p,s)) {
1673 for(p=conf_no; *p; p++) {
1674 if (!strcasecmp(*p,s)) {
1680 return PROF_BAD_BOOLEAN;
1684 profile_get_boolean(profile_t profile, const char *name, const char *subname,
1685 const char *subsubname, int def_val, int *ret_boolean)
1691 *ret_boolean = def_val;
1695 retval = profile_get_value(profile, name, subname, subsubname, &value);
1696 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1697 *ret_boolean = def_val;
1702 return profile_parse_boolean (value, ret_boolean);
1706 profile_iterator(void **iter_p, char **ret_name, char **ret_value)
1711 retval = profile_node_iterator(iter_p, 0, &name, &value);
1717 *ret_name = malloc(strlen(name)+1);
1720 strcpy(*ret_name, name);
1726 *ret_value = malloc(strlen(value)+1);
1734 strcpy(*ret_value, value);
1741 #ifdef DEBUG_PROGRAM
1744 * test_profile.c --- testing program for the profile routine
1747 #include "argv_parse.h"
1748 #include "profile_helpers.h"
1750 const char *program_name = "test_profile";
1752 #define PRINT_VALUE 1
1753 #define PRINT_VALUES 2
1755 static void do_cmd(profile_t profile, char **argv)
1758 const char **names, *value;
1759 char **values, **cpp;
1764 names = (const char **) argv + 1;
1769 if (!strcmp(cmd, "query")) {
1770 retval = profile_get_values(profile, names, &values);
1771 print_status = PRINT_VALUES;
1772 } else if (!strcmp(cmd, "query1")) {
1773 const char *name = 0;
1774 const char *subname = 0;
1775 const char *subsubname = 0;
1781 subsubname = names[2];
1782 if (subsubname && names[3]) {
1784 "Only 3 levels are allowed with query1\n");
1787 retval = profile_get_value(profile, name, subname,
1788 subsubname, &value);
1789 print_status = PRINT_VALUE;
1790 } else if (!strcmp(cmd, "list_sections")) {
1791 retval = profile_get_subsection_names(profile, names,
1793 print_status = PRINT_VALUES;
1794 } else if (!strcmp(cmd, "list_relations")) {
1795 retval = profile_get_relation_names(profile, names,
1797 print_status = PRINT_VALUES;
1798 } else if (!strcmp(cmd, "dump")) {
1799 retval = profile_write_tree_file
1800 (profile->first_file->root, stdout);
1802 } else if (!strcmp(cmd, "clear")) {
1803 retval = profile_clear_relation(profile, names);
1804 } else if (!strcmp(cmd, "update")) {
1805 retval = profile_update_relation(profile, names+2,
1806 *names, *(names+1));
1808 } else if (!strcmp(cmd, "verify")) {
1809 retval = profile_verify_node
1810 (profile->first_file->root);
1812 } else if (!strcmp(cmd, "rename_section")) {
1813 retval = profile_rename_section(profile, names+1, *names);
1814 } else if (!strcmp(cmd, "add")) {
1816 if (strcmp(value, "NULL") == 0)
1818 retval = profile_add_relation(profile, names+1, value);
1819 } else if (!strcmp(cmd, "flush")) {
1820 retval = profile_flush(profile);
1823 printf("Invalid command.\n");
1826 com_err(cmd, retval, "");
1829 switch (print_status) {
1831 printf("%s\n", value);
1834 for (cpp = values; *cpp; cpp++)
1835 printf("%s\n", *cpp);
1836 profile_free_list(values);
1841 static void do_batchmode(profile_t profile)
1847 while (!feof(stdin)) {
1848 if (fgets(buf, sizeof(buf), stdin) == NULL)
1851 ret = argv_parse(buf, &argc, &argv);
1853 printf("Argv_parse returned %d!\n", ret);
1856 do_cmd(profile, argv);
1860 profile_release(profile);
1865 void syntax_err_report(const char *filename, long err, int line_num)
1867 fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
1868 filename, line_num, error_message(err));
1872 const char *default_str = "[foo]\n\tbar=quux\n\tsub = {\n\t\twin = true\n}\n";
1874 int main(int argc, char **argv)
1881 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
1885 initialize_prof_error_table();
1887 profile_set_syntax_err_cb(syntax_err_report);
1889 retval = profile_init_path(argv[1], &profile);
1891 com_err(program_name, retval, "while initializing profile");
1894 retval = profile_set_default(profile, default_str);
1896 com_err(program_name, retval, "while setting default");
1901 if (!cmd || !strcmp(cmd, "batch"))
1902 do_batchmode(profile);
1904 do_cmd(profile, argv+2);
1905 profile_release(profile);