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.
64 #include <sys/types.h>
71 #include <et/com_err.h>
75 #undef STAT_ONCE_PER_SECOND
82 typedef long prf_magic_t;
85 * This is the structure which stores the profile information for a
86 * particular configuration file.
91 #ifdef STAT_ONCE_PER_SECOND
94 time_t timestamp; /* time tree was last updated from file */
95 int flags; /* r/w, dirty */
96 int upd_serial; /* incremented when data changes */
97 struct profile_node *root;
98 struct _prf_file_t *next;
101 typedef struct _prf_file_t *prf_file_t;
106 #define PROFILE_FILE_RW 0x0001
107 #define PROFILE_FILE_DIRTY 0x0002
108 #define PROFILE_FILE_NO_RELOAD 0x0004
111 * This structure defines the high-level, user visible profile_t
112 * object, which is used as a handle by users who need to query some
113 * configuration file(s)
117 prf_file_t first_file;
121 * Used by the profile iterator in prof_get.c
123 #define PROFILE_ITER_LIST_SECTION 0x0001
124 #define PROFILE_ITER_SECTIONS_ONLY 0x0002
125 #define PROFILE_ITER_RELATIONS_ONLY 0x0004
127 #define PROFILE_ITER_FINAL_SEEN 0x0100
130 * Check if a filespec is last in a list (NULL on UNIX, invalid FSSpec on MacOS
133 #define PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
135 struct profile_node {
140 unsigned int final:1; /* Indicate don't search next file */
141 unsigned int deleted:1;
142 struct profile_node *first_child;
143 struct profile_node *parent;
144 struct profile_node *next, *prev;
147 #define CHECK_MAGIC(node) \
148 if ((node)->magic != PROF_MAGIC_NODE) \
149 return PROF_MAGIC_NODE;
151 /* profile parser declarations */
156 struct profile_node *root_section;
157 struct profile_node *current_section;
160 static const char *default_filename = "<default>";
162 static profile_syntax_err_cb_t syntax_err_cb;
164 static errcode_t parse_line(char *line, struct parse_state *state);
167 static errcode_t profile_write_tree_file
168 (struct profile_node *root, FILE *dstfile);
170 static errcode_t profile_write_tree_to_buffer
171 (struct profile_node *root, char **buf);
175 static void profile_free_node
176 (struct profile_node *relation);
178 static errcode_t profile_create_node
179 (const char *name, const char *value,
180 struct profile_node **ret_node);
183 static errcode_t profile_verify_node
184 (struct profile_node *node);
187 static errcode_t profile_add_node
188 (struct profile_node *section,
189 const char *name, const char *value,
190 struct profile_node **ret_node);
192 static errcode_t profile_find_node
193 (struct profile_node *section,
194 const char *name, const char *value,
195 int section_flag, void **state,
196 struct profile_node **node);
198 static errcode_t profile_node_iterator
199 (void **iter_p, struct profile_node **ret_node,
200 char **ret_name, char **ret_value);
202 static errcode_t profile_open_file
203 (const char * file, prf_file_t *ret_prof);
205 static errcode_t profile_update_file
208 static void profile_free_file
209 (prf_file_t profile);
211 static errcode_t profile_get_value(profile_t profile, const char *name,
212 const char *subname, const char *subsubname,
213 const char **ret_value);
217 * prof_init.c --- routines that manipulate the user-visible profile_t
221 static int compstr(const void *m1, const void *m2)
223 const char *s1 = *((const char * const *) m1);
224 const char *s2 = *((const char * const *) m2);
226 return strcmp(s1, s2);
229 static void free_list(char **list)
236 for (cp = list; *cp; cp++)
241 static errcode_t get_dirlist(const char *dirname, char***ret_array)
248 char **array = 0, **new_array;
249 int max = 0, num = 0;
251 dir = opendir(dirname);
255 while ((de = readdir(dir)) != NULL) {
256 for (cp = de->d_name; *cp; cp++) {
264 fn = malloc(strlen(dirname) + strlen(de->d_name) + 2);
269 sprintf(fn, "%s/%s", dirname, de->d_name);
270 if ((stat(fn, &st) < 0) || !S_ISREG(st.st_mode)) {
276 new_array = realloc(array, sizeof(char *) * (max+1));
286 qsort(array, num, sizeof(char *), compstr);
299 profile_init(const char **files, profile_t *ret_profile)
303 prf_file_t new_file, *last;
304 errcode_t retval = 0;
305 char **cpp, *cp, **array = 0;
307 profile = malloc(sizeof(struct _profile_t));
310 memset(profile, 0, sizeof(struct _profile_t));
311 profile->magic = PROF_MAGIC_PROFILE;
312 last = &profile->first_file;
314 /* if the filenames list is not specified return an empty profile */
316 for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) {
317 retval = get_dirlist(*fs, &array);
321 for (cpp = array; (cp = *cpp); cpp++) {
322 retval = profile_open_file(cp, &new_file);
323 if (retval == EACCES)
328 last = &new_file->next;
330 } else if ((retval != ENOTDIR) &&
331 strcmp(*fs, default_filename))
334 retval = profile_open_file(*fs, &new_file);
335 /* if this file is missing, skip to the next */
336 if (retval == ENOENT || retval == EACCES) {
342 last = &new_file->next;
345 * If all the files were not found, return the appropriate error.
347 if (!profile->first_file) {
348 profile_release(profile);
354 *ret_profile = profile;
358 profile_release(profile);
363 profile_release(profile_t profile)
367 if (!profile || profile->magic != PROF_MAGIC_PROFILE)
370 for (p = profile->first_file; p; p = next) {
372 profile_free_file(p);
379 * This function sets the value of the pseudo file "<default>". If
380 * the file "<default>" had previously been passed to profile_init(),
381 * then def_string parameter will be parsed and used as the profile
382 * information for the "<default>" file.
384 errcode_t profile_set_default(profile_t profile, const char *def_string)
386 struct parse_state state;
390 char *line, *p, *end;
393 if (!def_string || !profile || profile->magic != PROF_MAGIC_PROFILE)
394 return PROF_MAGIC_PROFILE;
396 for (prf = profile->first_file; prf; prf = prf->next) {
397 if (strcmp(prf->filespec, default_filename) == 0)
404 profile_free_node(prf->root);
408 memset(&state, 0, sizeof(struct parse_state));
409 retval = profile_create_node("(root)", 0, &state.root_section);
417 end = strchr(in, '\n');
418 len = end ? (end - in) : (int) strlen(in);
419 if (len >= line_size) {
421 p = realloc(line, line_size);
428 memcpy(line, in, len);
430 retval = parse_line(line, &state);
434 (syntax_err_cb)(prf->filespec, retval,
438 profile_free_node(prf->root);
445 prf->root = state.root_section;
452 * prof_file.c ---- routines that manipulate an individual profile file.
455 errcode_t profile_open_file(const char * filespec,
456 prf_file_t *ret_prof)
462 char *expanded_filename;
464 prf = malloc(sizeof(struct _prf_file_t));
467 memset(prf, 0, sizeof(struct _prf_file_t));
468 prf->magic = PROF_MAGIC_FILE;
470 len = strlen(filespec)+1;
471 if (filespec[0] == '~' && filespec[1] == '/') {
472 home_env = getenv("HOME");
474 if (home_env == NULL) {
475 #ifdef HAVE_GETWUID_R
476 struct passwd *pw, pwx;
481 if (!getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
482 && pw != NULL && pw->pw_dir[0] != 0)
483 home_env = pw->pw_dir;
487 pw = getpwuid(getuid());
488 home_env = pw->pw_dir;
493 len += strlen(home_env);
495 expanded_filename = malloc(len);
496 if (expanded_filename == 0) {
497 profile_free_file(prf);
501 strcpy(expanded_filename, home_env);
502 strcat(expanded_filename, filespec+1);
504 memcpy(expanded_filename, filespec, len);
506 prf->filespec = expanded_filename;
508 if (strcmp(prf->filespec, default_filename) != 0) {
509 retval = profile_update_file(prf);
511 profile_free_file(prf);
520 errcode_t profile_update_file(prf_file_t prf)
525 #ifdef STAT_ONCE_PER_SECOND
531 struct parse_state state;
533 if (prf->flags & PROFILE_FILE_NO_RELOAD)
537 #ifdef STAT_ONCE_PER_SECOND
539 if (now == prf->last_stat && prf->root != NULL) {
543 if (stat(prf->filespec, &st)) {
547 #ifdef STAT_ONCE_PER_SECOND
548 prf->last_stat = now;
550 if (st.st_mtime == prf->timestamp && prf->root != NULL) {
554 profile_free_node(prf->root);
559 * If we don't have the stat() call, assume that our in-core
560 * memory image is correct. That is, we won't reread the
561 * profile file if it changes.
567 memset(&state, 0, sizeof(struct parse_state));
568 retval = profile_create_node("(root)", 0, &state.root_section);
572 f = fopen(prf->filespec, "r");
581 if (fgets(buf, sizeof(buf), f) == NULL)
583 retval = parse_line(buf, &state);
586 (syntax_err_cb)(prf->filespec, retval,
592 prf->root = state.root_section;
597 prf->timestamp = st.st_mtime;
602 void profile_free_file(prf_file_t prf)
605 profile_free_node(prf->root);
611 /* Begin the profile parser */
613 profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
615 profile_syntax_err_cb_t old;
618 syntax_err_cb = hook;
622 #define STATE_INIT_COMMENT 0
623 #define STATE_STD_LINE 1
624 #define STATE_GET_OBRACE 2
626 static char *skip_over_blanks(char *cp)
628 while (*cp && isspace((int) (*cp)))
633 static int end_or_comment(char ch)
635 return (ch == 0 || ch == '#' || ch == ';');
638 static char *skip_over_nonblanks(char *cp)
640 while (!end_or_comment(*cp) && !isspace(*cp))
645 static void strip_line(char *line)
647 char *p = line + strlen(line);
648 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
652 static void parse_quoted_string(char *str)
658 for (to = from = str; *from && *from != '"'; to++, from++) {
681 static errcode_t parse_line(char *line, struct parse_state *state)
683 char *cp, ch, *tag, *value;
686 struct profile_node *node;
687 int do_subsection = 0;
691 if (state->state == STATE_GET_OBRACE) {
692 cp = skip_over_blanks(line);
694 return PROF_MISSING_OBRACE;
695 state->state = STATE_STD_LINE;
698 if (state->state == STATE_INIT_COMMENT) {
701 state->state = STATE_STD_LINE;
707 cp = skip_over_blanks(line);
709 if (end_or_comment(ch))
712 if (state->group_level > 0)
713 return PROF_SECTION_NOTOP;
715 cp = skip_over_blanks(cp);
718 return PROF_SECTION_SYNTAX;
721 parse_quoted_string(cp);
724 while (isspace(*p) && (p > cp))
727 return PROF_SECTION_SYNTAX;
729 retval = profile_find_node(state->root_section, cp, 0, 1,
730 &iter, &state->current_section);
731 if (retval == PROF_NO_SECTION) {
732 retval = profile_add_node(state->root_section,
734 &state->current_section);
741 * Finish off the rest of the line.
745 state->current_section->final = 1;
749 * Spaces or comments after ']' should not be fatal
751 cp = skip_over_blanks(cp);
752 if (!end_or_comment(*cp))
753 return PROF_SECTION_SYNTAX;
757 if (state->group_level == 0)
758 return PROF_EXTRA_CBRACE;
760 state->current_section->final = 1;
761 state->current_section = state->current_section->parent;
762 state->group_level--;
766 * Parse the relations
769 cp = strchr(cp, '=');
771 return PROF_RELATION_SYNTAX;
773 return PROF_RELATION_SYNTAX;
777 parse_quoted_string(tag);
779 /* Look for whitespace on left-hand side. */
780 p = skip_over_nonblanks(tag);
783 p = skip_over_blanks(p);
784 /* If we have more non-whitespace, it's an error. */
786 return PROF_RELATION_SYNTAX;
789 cp = skip_over_blanks(cp+1);
794 parse_quoted_string(value);
795 } else if (end_or_comment(ch)) {
797 state->state = STATE_GET_OBRACE;
798 } else if (value[0] == '{') {
799 cp = skip_over_blanks(value+1);
801 if (end_or_comment(ch))
804 return PROF_RELATION_SYNTAX;
806 cp = skip_over_nonblanks(value);
807 p = skip_over_blanks(cp);
810 if (!end_or_comment(ch))
811 return PROF_RELATION_SYNTAX;
814 p = strchr(tag, '*');
817 retval = profile_add_node(state->current_section,
818 tag, 0, &state->current_section);
822 state->current_section->final = 1;
823 state->group_level++;
826 p = strchr(tag, '*');
829 profile_add_node(state->current_section, tag, value, &node);
837 * Return TRUE if the string begins or ends with whitespace
839 static int need_double_quotes(char *str)
843 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
845 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
846 strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
852 * Output a string with double quotes, doing appropriate backquoting
853 * of characters as necessary.
855 static void output_quoted_string(char *str, void (*cb)(const char *,void *),
867 while ((ch = *str++)) {
882 /* This would be a lot faster if we scanned
883 forward for the next "interesting"
897 /* Errors should be returned, not ignored! */
898 static void dump_profile(struct profile_node *root, int level,
899 void (*cb)(const char *, void *), void *data)
902 struct profile_node *p;
908 retval = profile_find_node(root, 0, 0, 0, &iter, &p);
911 for (i=0; i < level; i++)
913 if (need_double_quotes(p->name))
914 output_quoted_string(p->name, cb, data);
918 if (need_double_quotes(p->value))
919 output_quoted_string(p->value, cb, data);
927 retval = profile_find_node(root, 0, 0, 1, &iter, &p);
930 if (level == 0) { /* [xxx] */
932 if (need_double_quotes(p->name))
933 output_quoted_string(p->name, cb, data);
937 cb(p->final ? "*" : "", data);
939 dump_profile(p, level+1, cb, data);
941 } else { /* xxx = { ... } */
942 for (i=0; i < level; i++)
944 if (need_double_quotes(p->name))
945 output_quoted_string(p->name, cb, data);
950 dump_profile(p, level+1, cb, data);
951 for (i=0; i < level; i++)
954 cb(p->final ? "*" : "", data);
960 static void dump_profile_to_file_cb(const char *str, void *data)
965 errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
967 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
977 static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
981 if (b->max - b->cur < len) {
985 newsize = b->max + (b->max >> 1) + len + 1024;
986 newptr = realloc(b->base, newsize);
987 if (newptr == NULL) {
994 memcpy(b->base + b->cur, d, len);
995 b->cur += len; /* ignore overflow */
998 static void dump_profile_to_buffer_cb(const char *str, void *data)
1000 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
1003 errcode_t profile_write_tree_to_buffer(struct profile_node *root,
1006 struct prof_buf prof_buf = { 0, 0, 0, 0 };
1008 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
1013 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
1014 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
1015 char *newptr = realloc(prof_buf.base, prof_buf.cur);
1017 prof_buf.base = newptr;
1019 *buf = prof_buf.base;
1025 * prof_tree.c --- these routines maintain the parse tree of the
1028 * All of the details of how the tree is stored is abstracted away in
1029 * this file; all of the other profile routines build, access, and
1030 * modify the tree via the accessor functions found in this file.
1032 * Each node may represent either a relation or a section header.
1034 * A section header must have its value field set to 0, and may a one
1035 * or more child nodes, pointed to by first_child.
1037 * A relation has as its value a pointer to allocated memory
1038 * containing a string. Its first_child pointer must be null.
1043 * Free a node, and any children
1045 void profile_free_node(struct profile_node *node)
1047 struct profile_node *child, *next;
1049 if (node->magic != PROF_MAGIC_NODE)
1057 for (child=node->first_child; child; child = next) {
1059 profile_free_node(child);
1068 #define strdup MYstrdup
1069 static char *MYstrdup (const char *s)
1071 size_t sz = strlen(s) + 1;
1072 char *p = malloc(sz);
1082 errcode_t profile_create_node(const char *name, const char *value,
1083 struct profile_node **ret_node)
1085 struct profile_node *new;
1087 new = malloc(sizeof(struct profile_node));
1090 memset(new, 0, sizeof(struct profile_node));
1091 new->name = strdup(name);
1092 if (new->name == 0) {
1093 profile_free_node(new);
1097 new->value = strdup(value);
1098 if (new->value == 0) {
1099 profile_free_node(new);
1103 new->magic = PROF_MAGIC_NODE;
1110 * This function verifies that all of the representation invarients of
1111 * the profile are true. If not, we have a programming bug somewhere,
1112 * probably in this file.
1114 #ifdef DEBUG_PROGRAM
1115 errcode_t profile_verify_node(struct profile_node *node)
1117 struct profile_node *p, *last;
1122 if (node->value && node->first_child)
1123 return PROF_SECTION_WITH_VALUE;
1126 for (p = node->first_child; p; last = p, p = p->next) {
1127 if (p->prev != last)
1128 return PROF_BAD_LINK_LIST;
1129 if (last && (last->next != p))
1130 return PROF_BAD_LINK_LIST;
1131 if (node->group_level+1 != p->group_level)
1132 return PROF_BAD_GROUP_LVL;
1133 if (p->parent != node)
1134 return PROF_BAD_PARENT_PTR;
1135 retval = profile_verify_node(p);
1144 * Add a node to a particular section
1146 errcode_t profile_add_node(struct profile_node *section, const char *name,
1147 const char *value, struct profile_node **ret_node)
1150 struct profile_node *p, *last, *new;
1152 CHECK_MAGIC(section);
1155 return PROF_ADD_NOT_SECTION;
1158 * Find the place to insert the new node. We look for the
1159 * place *after* the last match of the node name, since
1162 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1164 cmp = strcmp(p->name, name);
1168 retval = profile_create_node(name, value, &new);
1171 new->group_level = section->group_level+1;
1173 new->parent = section;
1181 section->first_child = new;
1188 * Iterate through the section, returning the nodes which match
1189 * the given name. If name is NULL, then interate through all the
1190 * nodes in the section. If section_flag is non-zero, only return the
1191 * section which matches the name; don't return relations. If value
1192 * is non-NULL, then only return relations which match the requested
1193 * value. (The value argument is ignored if section_flag is non-zero.)
1195 * The first time this routine is called, the state pointer must be
1196 * null. When this profile_find_node_relation() returns, if the state
1197 * pointer is non-NULL, then this routine should be called again.
1198 * (This won't happen if section_flag is non-zero, obviously.)
1201 errcode_t profile_find_node(struct profile_node *section, const char *name,
1202 const char *value, int section_flag, void **state,
1203 struct profile_node **node)
1205 struct profile_node *p;
1207 CHECK_MAGIC(section);
1212 p = section->first_child;
1214 for (; p; p = p->next) {
1215 if (name && (strcmp(p->name, name)))
1223 if (value && (strcmp(p->value, value)))
1235 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1238 * OK, we've found one match; now let's try to find another
1239 * one. This way, if we return a non-zero state pointer,
1240 * there's guaranteed to be another match that's returned.
1242 for (p = p->next; p; p = p->next) {
1243 if (name && (strcmp(p->name, name)))
1251 if (value && (strcmp(p->value, value)))
1262 * This is a general-purpose iterator for returning all nodes that
1263 * match the specified name array.
1265 struct profile_iterator {
1269 const char *const *names;
1274 struct profile_node *node;
1279 profile_iterator_create(profile_t profile, const char *const *names, int flags,
1282 struct profile_iterator *iter;
1286 return PROF_NO_PROFILE;
1287 if (profile->magic != PROF_MAGIC_PROFILE)
1288 return PROF_MAGIC_PROFILE;
1290 return PROF_BAD_NAMESET;
1291 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1293 return PROF_BAD_NAMESET;
1297 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1300 iter->magic = PROF_MAGIC_ITERATOR;
1301 iter->profile = profile;
1302 iter->names = names;
1303 iter->flags = flags;
1304 iter->file = profile->first_file;
1305 iter->done_idx = done_idx;
1312 void profile_iterator_free(void **iter_p)
1314 struct profile_iterator *iter;
1319 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1326 * Note: the returned character strings in ret_name and ret_value
1327 * points to the stored character string in the parse string. Before
1328 * this string value is returned to a calling application
1329 * (profile_node_iterator is not an exported interface), it should be
1332 errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1333 char **ret_name, char **ret_value)
1335 struct profile_iterator *iter = *iter_p;
1336 struct profile_node *section, *p;
1337 const char *const *cpp;
1341 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1342 return PROF_MAGIC_ITERATOR;
1343 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1344 return PROF_MAGIC_FILE;
1346 * If the file has changed, then the node pointer is invalid,
1347 * so we'll have search the file again looking for it.
1349 if (iter->node && (iter->file &&
1350 iter->file->upd_serial != iter->file_serial)) {
1351 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1352 skip_num = iter->num;
1355 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1356 return PROF_MAGIC_NODE;
1359 if (iter->node == 0) {
1360 if (iter->file == 0 ||
1361 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
1362 profile_iterator_free(iter_p);
1371 if ((retval = profile_update_file(iter->file))) {
1372 if (retval == ENOENT || retval == EACCES) {
1373 /* XXX memory leak? */
1374 iter->file = iter->file->next;
1379 profile_iterator_free(iter_p);
1383 iter->file_serial = iter->file->upd_serial;
1385 * Find the section to list if we are a LIST_SECTION,
1386 * or find the containing section if not.
1388 section = iter->file->root;
1389 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1390 for (p=section->first_child; p; p = p->next) {
1391 if (!strcmp(p->name, *cpp) && !p->value)
1400 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1403 iter->file = iter->file->next;
1408 iter->node = section->first_child;
1411 * OK, now we know iter->node is set up correctly. Let's do
1414 for (p = iter->node; p; p = p->next) {
1415 if (iter->name && strcmp(p->name, iter->name))
1417 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1420 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1433 iter->file = iter->file->next;
1438 if ((iter->node = p->next) == NULL)
1439 iter->file = iter->file->next;
1443 *ret_name = p->name;
1445 *ret_value = p->value;
1451 * prof_get.c --- routines that expose the public interfaces for
1452 * querying items from the profile.
1457 * This function only gets the first value from the file; it is a
1458 * helper function for profile_get_string, profile_get_integer, etc.
1460 errcode_t profile_get_value(profile_t profile, const char *name,
1461 const char *subname, const char *subsubname,
1462 const char **ret_value)
1467 const char *names[4];
1471 names[2] = subsubname;
1474 if ((retval = profile_iterator_create(profile, names,
1475 PROFILE_ITER_RELATIONS_ONLY,
1479 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1485 retval = PROF_NO_RELATION;
1488 profile_iterator_free(&state);
1493 profile_get_string(profile_t profile, const char *name, const char *subname,
1494 const char *subsubname, const char *def_val,
1501 retval = profile_get_value(profile, name, subname,
1502 subsubname, &value);
1503 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1511 *ret_string = malloc(strlen(value)+1);
1512 if (*ret_string == 0)
1514 strcpy(*ret_string, value);
1521 profile_get_integer(profile_t profile, const char *name, const char *subname,
1522 const char *subsubname, int def_val, int *ret_int)
1533 retval = profile_get_value(profile, name, subname, subsubname, &value);
1534 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1541 /* Empty string is no good. */
1542 return PROF_BAD_INTEGER;
1544 ret_long = strtol (value, &end_value, 10);
1546 /* Overflow or underflow. */
1547 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1548 return PROF_BAD_INTEGER;
1549 /* Value outside "int" range. */
1550 if ((long) (int) ret_long != ret_long)
1551 return PROF_BAD_INTEGER;
1552 /* Garbage in string. */
1553 if (end_value != value + strlen (value))
1554 return PROF_BAD_INTEGER;
1557 *ret_int = ret_long;
1562 profile_get_uint(profile_t profile, const char *name, const char *subname,
1563 const char *subsubname, unsigned int def_val,
1564 unsigned int *ret_int)
1569 unsigned long ret_long;
1575 retval = profile_get_value(profile, name, subname, subsubname, &value);
1576 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1583 /* Empty string is no good. */
1584 return PROF_BAD_INTEGER;
1586 ret_long = strtoul (value, &end_value, 10);
1588 /* Overflow or underflow. */
1589 if ((ret_long == ULONG_MAX) && errno != 0)
1590 return PROF_BAD_INTEGER;
1591 /* Value outside "int" range. */
1592 if ((unsigned long) (unsigned int) ret_long != ret_long)
1593 return PROF_BAD_INTEGER;
1594 /* Garbage in string. */
1595 if (end_value != value + strlen (value))
1596 return PROF_BAD_INTEGER;
1598 *ret_int = ret_long;
1602 static const char *const conf_yes[] = {
1603 "y", "yes", "true", "t", "1", "on",
1607 static const char *const conf_no[] = {
1608 "n", "no", "false", "nil", "0", "off",
1613 profile_parse_boolean(const char *s, int *ret_boolean)
1615 const char *const *p;
1617 if (ret_boolean == NULL)
1620 for(p=conf_yes; *p; p++) {
1621 if (!strcasecmp(*p,s)) {
1627 for(p=conf_no; *p; p++) {
1628 if (!strcasecmp(*p,s)) {
1634 return PROF_BAD_BOOLEAN;
1638 profile_get_boolean(profile_t profile, const char *name, const char *subname,
1639 const char *subsubname, int def_val, int *ret_boolean)
1645 *ret_boolean = def_val;
1649 retval = profile_get_value(profile, name, subname, subsubname, &value);
1650 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1651 *ret_boolean = def_val;
1656 return profile_parse_boolean (value, ret_boolean);
1660 profile_iterator(void **iter_p, char **ret_name, char **ret_value)
1665 retval = profile_node_iterator(iter_p, 0, &name, &value);
1671 *ret_name = malloc(strlen(name)+1);
1674 strcpy(*ret_name, name);
1680 *ret_value = malloc(strlen(value)+1);
1688 strcpy(*ret_value, value);
1695 #ifdef DEBUG_PROGRAM
1698 * test_profile.c --- testing program for the profile routine
1701 #include "argv_parse.h"
1702 #include "profile_helpers.h"
1704 const char *program_name = "test_profile";
1706 #define PRINT_VALUE 1
1707 #define PRINT_VALUES 2
1709 static void do_cmd(profile_t profile, char **argv)
1712 const char **names, *value;
1713 char **values, **cpp;
1718 names = (const char **) argv + 1;
1723 if (!strcmp(cmd, "query")) {
1724 retval = profile_get_values(profile, names, &values);
1725 print_status = PRINT_VALUES;
1726 } else if (!strcmp(cmd, "query1")) {
1727 const char *name = 0;
1728 const char *subname = 0;
1729 const char *subsubname = 0;
1735 subsubname = names[2];
1736 if (subsubname && names[3]) {
1738 "Only 3 levels are allowed with query1\n");
1741 retval = profile_get_value(profile, name, subname,
1742 subsubname, &value);
1743 print_status = PRINT_VALUE;
1744 } else if (!strcmp(cmd, "list_sections")) {
1745 retval = profile_get_subsection_names(profile, names,
1747 print_status = PRINT_VALUES;
1748 } else if (!strcmp(cmd, "list_relations")) {
1749 retval = profile_get_relation_names(profile, names,
1751 print_status = PRINT_VALUES;
1752 } else if (!strcmp(cmd, "dump")) {
1753 retval = profile_write_tree_file
1754 (profile->first_file->root, stdout);
1756 } else if (!strcmp(cmd, "clear")) {
1757 retval = profile_clear_relation(profile, names);
1758 } else if (!strcmp(cmd, "update")) {
1759 retval = profile_update_relation(profile, names+2,
1760 *names, *(names+1));
1762 } else if (!strcmp(cmd, "verify")) {
1763 retval = profile_verify_node
1764 (profile->first_file->root);
1766 } else if (!strcmp(cmd, "rename_section")) {
1767 retval = profile_rename_section(profile, names+1, *names);
1768 } else if (!strcmp(cmd, "add")) {
1770 if (strcmp(value, "NULL") == 0)
1772 retval = profile_add_relation(profile, names+1, value);
1773 } else if (!strcmp(cmd, "flush")) {
1774 retval = profile_flush(profile);
1777 printf("Invalid command.\n");
1780 com_err(cmd, retval, "");
1783 switch (print_status) {
1785 printf("%s\n", value);
1788 for (cpp = values; *cpp; cpp++)
1789 printf("%s\n", *cpp);
1790 profile_free_list(values);
1795 static void do_batchmode(profile_t profile)
1801 while (!feof(stdin)) {
1802 if (fgets(buf, sizeof(buf), stdin) == NULL)
1805 ret = argv_parse(buf, &argc, &argv);
1807 printf("Argv_parse returned %d!\n", ret);
1810 do_cmd(profile, argv);
1814 profile_release(profile);
1819 void syntax_err_report(const char *filename, long err, int line_num)
1821 fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
1822 filename, line_num, error_message(err));
1826 const char *default_str = "[foo]\n\tbar=quux\n\tsub = {\n\t\twin = true\n}\n";
1828 int main(int argc, char **argv)
1835 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
1839 initialize_prof_error_table();
1841 profile_set_syntax_err_cb(syntax_err_report);
1843 retval = profile_init_path(argv[1], &profile);
1845 com_err(program_name, retval, "while initializing profile");
1848 retval = profile_set_default(profile, default_str);
1850 com_err(program_name, retval, "while setting default");
1855 if (!cmd || !strcmp(cmd, "batch"))
1856 do_batchmode(profile);
1858 do_cmd(profile, argv+2);
1859 profile_release(profile);