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.
65 #include <sys/types.h>
72 #include <et/com_err.h>
76 #undef STAT_ONCE_PER_SECOND
83 typedef long prf_magic_t;
86 * This is the structure which stores the profile information for a
87 * particular configuration file.
92 #ifdef STAT_ONCE_PER_SECOND
95 time_t timestamp; /* time tree was last updated from file */
96 int flags; /* r/w, dirty */
97 int upd_serial; /* incremented when data changes */
98 struct profile_node *root;
99 struct _prf_file_t *next;
102 typedef struct _prf_file_t *prf_file_t;
107 #define PROFILE_FILE_RW 0x0001
108 #define PROFILE_FILE_DIRTY 0x0002
109 #define PROFILE_FILE_NO_RELOAD 0x0004
112 * This structure defines the high-level, user visible profile_t
113 * object, which is used as a handle by users who need to query some
114 * configuration file(s)
118 prf_file_t first_file;
122 * Used by the profile iterator in prof_get.c
124 #define PROFILE_ITER_LIST_SECTION 0x0001
125 #define PROFILE_ITER_SECTIONS_ONLY 0x0002
126 #define PROFILE_ITER_RELATIONS_ONLY 0x0004
128 #define PROFILE_ITER_FINAL_SEEN 0x0100
131 * Check if a filespec is last in a list (NULL on UNIX, invalid FSSpec on MacOS
134 #define PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
136 struct profile_node {
141 unsigned int final:1; /* Indicate don't search next file */
142 unsigned int deleted:1;
143 struct profile_node *first_child;
144 struct profile_node *parent;
145 struct profile_node *next, *prev;
148 #define CHECK_MAGIC(node) \
149 if ((node)->magic != PROF_MAGIC_NODE) \
150 return PROF_MAGIC_NODE;
152 /* profile parser declarations */
157 struct profile_node *root_section;
158 struct profile_node *current_section;
161 static const char *default_filename = "<default>";
163 static profile_syntax_err_cb_t syntax_err_cb;
165 static errcode_t parse_line(char *line, struct parse_state *state);
168 static errcode_t profile_write_tree_file
169 (struct profile_node *root, FILE *dstfile);
171 static errcode_t profile_write_tree_to_buffer
172 (struct profile_node *root, char **buf);
176 static void profile_free_node
177 (struct profile_node *relation);
179 static errcode_t profile_create_node
180 (const char *name, const char *value,
181 struct profile_node **ret_node);
184 static errcode_t profile_verify_node
185 (struct profile_node *node);
188 static errcode_t profile_add_node
189 (struct profile_node *section,
190 const char *name, const char *value,
191 struct profile_node **ret_node);
193 static errcode_t profile_find_node
194 (struct profile_node *section,
195 const char *name, const char *value,
196 int section_flag, void **state,
197 struct profile_node **node);
199 static errcode_t profile_node_iterator
200 (void **iter_p, struct profile_node **ret_node,
201 char **ret_name, char **ret_value);
203 static errcode_t profile_open_file
204 (const char * file, prf_file_t *ret_prof);
206 static errcode_t profile_update_file
209 static void profile_free_file
210 (prf_file_t profile);
212 static errcode_t profile_get_value(profile_t profile, const char *name,
213 const char *subname, const char *subsubname,
214 const char **ret_value);
218 * prof_init.c --- routines that manipulate the user-visible profile_t
222 static int compstr(const void *m1, const void *m2)
224 const char *s1 = *((const char * const *) m1);
225 const char *s2 = *((const char * const *) m2);
227 return strcmp(s1, s2);
230 static void free_list(char **list)
237 for (cp = list; *cp; cp++)
242 static errcode_t get_dirlist(const char *dirname, char***ret_array)
249 char **array = 0, **new_array;
250 int max = 0, num = 0;
252 dir = opendir(dirname);
256 while ((de = readdir(dir)) != NULL) {
257 for (cp = de->d_name; *cp; cp++) {
265 fn = malloc(strlen(dirname) + strlen(de->d_name) + 2);
270 sprintf(fn, "%s/%s", dirname, de->d_name);
271 if ((stat(fn, &st) < 0) || !S_ISREG(st.st_mode)) {
277 new_array = realloc(array, sizeof(char *) * (max+1));
288 qsort(array, num, sizeof(char *), compstr);
303 profile_init(const char **files, profile_t *ret_profile)
307 prf_file_t new_file, *last;
308 errcode_t retval = 0;
309 char **cpp, *cp, **array = 0;
311 profile = malloc(sizeof(struct _profile_t));
314 memset(profile, 0, sizeof(struct _profile_t));
315 profile->magic = PROF_MAGIC_PROFILE;
316 last = &profile->first_file;
318 /* if the filenames list is not specified return an empty profile */
320 for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) {
321 retval = get_dirlist(*fs, &array);
325 for (cpp = array; (cp = *cpp); cpp++) {
326 retval = profile_open_file(cp, &new_file);
327 if (retval == EACCES)
332 last = &new_file->next;
334 } else if ((retval != ENOTDIR) &&
335 strcmp(*fs, default_filename))
338 retval = profile_open_file(*fs, &new_file);
339 /* if this file is missing, skip to the next */
340 if (retval == ENOENT || retval == EACCES) {
346 last = &new_file->next;
349 * If all the files were not found, return the appropriate error.
351 if (!profile->first_file) {
358 *ret_profile = profile;
362 profile_release(profile);
367 profile_release(profile_t profile)
371 if (!profile || profile->magic != PROF_MAGIC_PROFILE)
374 for (p = profile->first_file; p; p = next) {
376 profile_free_file(p);
383 * This function sets the value of the pseudo file "<default>". If
384 * the file "<default>" had previously been passed to profile_init(),
385 * then def_string parameter will be parsed and used as the profile
386 * information for the "<default>" file.
388 errcode_t profile_set_default(profile_t profile, const char *def_string)
390 struct parse_state state;
394 char *line, *p, *end;
397 if (!def_string || !profile || profile->magic != PROF_MAGIC_PROFILE)
398 return PROF_MAGIC_PROFILE;
400 for (prf = profile->first_file; prf; prf = prf->next) {
401 if (strcmp(prf->filespec, default_filename) == 0)
408 profile_free_node(prf->root);
412 memset(&state, 0, sizeof(struct parse_state));
413 retval = profile_create_node("(root)", 0, &state.root_section);
421 end = strchr(in, '\n');
422 len = end ? (end - in) : (int) strlen(in);
423 if (len >= line_size) {
425 p = realloc(line, line_size);
432 memcpy(line, in, len);
434 retval = parse_line(line, &state);
438 (syntax_err_cb)(prf->filespec, retval,
442 profile_free_node(prf->root);
449 prf->root = state.root_section;
456 * prof_file.c ---- routines that manipulate an individual profile file.
459 errcode_t profile_open_file(const char * filespec,
460 prf_file_t *ret_prof)
466 char *expanded_filename;
468 prf = malloc(sizeof(struct _prf_file_t));
471 memset(prf, 0, sizeof(struct _prf_file_t));
472 prf->magic = PROF_MAGIC_FILE;
474 len = strlen(filespec)+1;
475 if (filespec[0] == '~' && filespec[1] == '/') {
476 home_env = getenv("HOME");
478 if (home_env == NULL) {
479 #ifdef HAVE_GETWUID_R
480 struct passwd *pw, pwx;
485 if (!getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
486 && pw != NULL && pw->pw_dir[0] != 0)
487 home_env = pw->pw_dir;
491 pw = getpwuid(getuid());
492 home_env = pw->pw_dir;
497 len += strlen(home_env);
499 expanded_filename = malloc(len);
500 if (expanded_filename == 0) {
501 profile_free_file(prf);
505 strcpy(expanded_filename, home_env);
506 strcat(expanded_filename, filespec+1);
508 memcpy(expanded_filename, filespec, len);
510 prf->filespec = expanded_filename;
512 if (strcmp(prf->filespec, default_filename) != 0) {
513 retval = profile_update_file(prf);
515 profile_free_file(prf);
524 errcode_t profile_update_file(prf_file_t prf)
529 #ifdef STAT_ONCE_PER_SECOND
535 struct parse_state state;
537 if (prf->flags & PROFILE_FILE_NO_RELOAD)
541 #ifdef STAT_ONCE_PER_SECOND
543 if (now == prf->last_stat && prf->root != NULL) {
547 if (stat(prf->filespec, &st)) {
551 #ifdef STAT_ONCE_PER_SECOND
552 prf->last_stat = now;
554 if (st.st_mtime == prf->timestamp && prf->root != NULL) {
558 profile_free_node(prf->root);
563 * If we don't have the stat() call, assume that our in-core
564 * memory image is correct. That is, we won't reread the
565 * profile file if it changes.
571 memset(&state, 0, sizeof(struct parse_state));
572 retval = profile_create_node("(root)", 0, &state.root_section);
576 f = fopen(prf->filespec, "r");
585 if (fgets(buf, sizeof(buf), f) == NULL)
587 retval = parse_line(buf, &state);
590 (syntax_err_cb)(prf->filespec, retval,
596 prf->root = state.root_section;
601 prf->timestamp = st.st_mtime;
606 void profile_free_file(prf_file_t prf)
609 profile_free_node(prf->root);
614 /* Begin the profile parser */
616 profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
618 profile_syntax_err_cb_t old;
621 syntax_err_cb = hook;
625 #define STATE_INIT_COMMENT 0
626 #define STATE_STD_LINE 1
627 #define STATE_GET_OBRACE 2
629 static char *skip_over_blanks(char *cp)
631 while (*cp && isspace((int) (*cp)))
636 static int end_or_comment(char ch)
638 return (ch == 0 || ch == '#' || ch == ';');
641 static char *skip_over_nonblanks(char *cp)
643 while (!end_or_comment(*cp) && !isspace(*cp))
648 static void strip_line(char *line)
650 char *p = line + strlen(line);
651 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
655 static void parse_quoted_string(char *str)
661 for (to = from = str; *from && *from != '"'; to++, from++) {
684 static errcode_t parse_line(char *line, struct parse_state *state)
686 char *cp, ch, *tag, *value;
689 struct profile_node *node;
690 int do_subsection = 0;
694 if (state->state == STATE_GET_OBRACE) {
695 cp = skip_over_blanks(line);
697 return PROF_MISSING_OBRACE;
698 state->state = STATE_STD_LINE;
701 if (state->state == STATE_INIT_COMMENT) {
704 state->state = STATE_STD_LINE;
710 cp = skip_over_blanks(line);
712 if (end_or_comment(ch))
715 if (state->group_level > 0)
716 return PROF_SECTION_NOTOP;
718 cp = skip_over_blanks(cp);
721 return PROF_SECTION_SYNTAX;
724 parse_quoted_string(cp);
727 while (isspace(*p) && (p > cp))
730 return PROF_SECTION_SYNTAX;
732 retval = profile_find_node(state->root_section, cp, 0, 1,
733 &iter, &state->current_section);
734 if (retval == PROF_NO_SECTION) {
735 retval = profile_add_node(state->root_section,
737 &state->current_section);
744 * Finish off the rest of the line.
748 state->current_section->final = 1;
752 * Spaces or comments after ']' should not be fatal
754 cp = skip_over_blanks(cp);
755 if (!end_or_comment(*cp))
756 return PROF_SECTION_SYNTAX;
760 if (state->group_level == 0)
761 return PROF_EXTRA_CBRACE;
763 state->current_section->final = 1;
764 state->current_section = state->current_section->parent;
765 state->group_level--;
769 * Parse the relations
772 cp = strchr(cp, '=');
774 return PROF_RELATION_SYNTAX;
776 return PROF_RELATION_SYNTAX;
780 parse_quoted_string(tag);
782 /* Look for whitespace on left-hand side. */
783 p = skip_over_nonblanks(tag);
786 p = skip_over_blanks(p);
787 /* If we have more non-whitespace, it's an error. */
789 return PROF_RELATION_SYNTAX;
792 cp = skip_over_blanks(cp+1);
797 parse_quoted_string(value);
798 } else if (end_or_comment(ch)) {
800 state->state = STATE_GET_OBRACE;
801 } else if (value[0] == '{') {
802 cp = skip_over_blanks(value+1);
804 if (end_or_comment(ch))
807 return PROF_RELATION_SYNTAX;
809 cp = skip_over_nonblanks(value);
810 p = skip_over_blanks(cp);
813 if (!end_or_comment(ch))
814 return PROF_RELATION_SYNTAX;
817 p = strchr(tag, '*');
820 retval = profile_add_node(state->current_section,
821 tag, 0, &state->current_section);
825 state->current_section->final = 1;
826 state->group_level++;
829 p = strchr(tag, '*');
832 profile_add_node(state->current_section, tag, value, &node);
840 * Return TRUE if the string begins or ends with whitespace
842 static int need_double_quotes(char *str)
846 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
848 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
849 strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
855 * Output a string with double quotes, doing appropriate backquoting
856 * of characters as necessary.
858 static void output_quoted_string(char *str, void (*cb)(const char *,void *),
870 while ((ch = *str++)) {
885 /* This would be a lot faster if we scanned
886 forward for the next "interesting"
900 /* Errors should be returned, not ignored! */
901 static void dump_profile(struct profile_node *root, int level,
902 void (*cb)(const char *, void *), void *data)
905 struct profile_node *p;
911 retval = profile_find_node(root, 0, 0, 0, &iter, &p);
914 for (i=0; i < level; i++)
916 if (need_double_quotes(p->name))
917 output_quoted_string(p->name, cb, data);
921 if (need_double_quotes(p->value))
922 output_quoted_string(p->value, cb, data);
930 retval = profile_find_node(root, 0, 0, 1, &iter, &p);
933 if (level == 0) { /* [xxx] */
935 if (need_double_quotes(p->name))
936 output_quoted_string(p->name, cb, data);
940 cb(p->final ? "*" : "", data);
942 dump_profile(p, level+1, cb, data);
944 } else { /* xxx = { ... } */
945 for (i=0; i < level; i++)
947 if (need_double_quotes(p->name))
948 output_quoted_string(p->name, cb, data);
953 dump_profile(p, level+1, cb, data);
954 for (i=0; i < level; i++)
957 cb(p->final ? "*" : "", data);
963 static void dump_profile_to_file_cb(const char *str, void *data)
968 errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
970 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
980 static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
984 if (b->max - b->cur < len) {
988 newsize = b->max + (b->max >> 1) + len + 1024;
989 newptr = realloc(b->base, newsize);
990 if (newptr == NULL) {
997 memcpy(b->base + b->cur, d, len);
998 b->cur += len; /* ignore overflow */
1001 static void dump_profile_to_buffer_cb(const char *str, void *data)
1003 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
1006 errcode_t profile_write_tree_to_buffer(struct profile_node *root,
1009 struct prof_buf prof_buf = { 0, 0, 0, 0 };
1011 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
1016 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
1017 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
1018 char *newptr = realloc(prof_buf.base, prof_buf.cur);
1020 prof_buf.base = newptr;
1022 *buf = prof_buf.base;
1028 * prof_tree.c --- these routines maintain the parse tree of the
1031 * All of the details of how the tree is stored is abstracted away in
1032 * this file; all of the other profile routines build, access, and
1033 * modify the tree via the accessor functions found in this file.
1035 * Each node may represent either a relation or a section header.
1037 * A section header must have its value field set to 0, and may a one
1038 * or more child nodes, pointed to by first_child.
1040 * A relation has as its value a pointer to allocated memory
1041 * containing a string. Its first_child pointer must be null.
1046 * Free a node, and any children
1048 void profile_free_node(struct profile_node *node)
1050 struct profile_node *child, *next;
1052 if (node->magic != PROF_MAGIC_NODE)
1058 for (child=node->first_child; child; child = next) {
1060 profile_free_node(child);
1069 #define strdup MYstrdup
1070 static char *MYstrdup (const char *s)
1072 size_t sz = strlen(s) + 1;
1073 char *p = malloc(sz);
1083 errcode_t profile_create_node(const char *name, const char *value,
1084 struct profile_node **ret_node)
1086 struct profile_node *new;
1088 new = malloc(sizeof(struct profile_node));
1091 memset(new, 0, sizeof(struct profile_node));
1092 new->name = strdup(name);
1093 if (new->name == 0) {
1094 profile_free_node(new);
1098 new->value = strdup(value);
1099 if (new->value == 0) {
1100 profile_free_node(new);
1104 new->magic = PROF_MAGIC_NODE;
1111 * This function verifies that all of the representation invarients of
1112 * the profile are true. If not, we have a programming bug somewhere,
1113 * probably in this file.
1115 #ifdef DEBUG_PROGRAM
1116 errcode_t profile_verify_node(struct profile_node *node)
1118 struct profile_node *p, *last;
1123 if (node->value && node->first_child)
1124 return PROF_SECTION_WITH_VALUE;
1127 for (p = node->first_child; p; last = p, p = p->next) {
1128 if (p->prev != last)
1129 return PROF_BAD_LINK_LIST;
1130 if (last && (last->next != p))
1131 return PROF_BAD_LINK_LIST;
1132 if (node->group_level+1 != p->group_level)
1133 return PROF_BAD_GROUP_LVL;
1134 if (p->parent != node)
1135 return PROF_BAD_PARENT_PTR;
1136 retval = profile_verify_node(p);
1145 * Add a node to a particular section
1147 errcode_t profile_add_node(struct profile_node *section, const char *name,
1148 const char *value, struct profile_node **ret_node)
1151 struct profile_node *p, *last, *new;
1153 CHECK_MAGIC(section);
1156 return PROF_ADD_NOT_SECTION;
1159 * Find the place to insert the new node. We look for the
1160 * place *after* the last match of the node name, since
1163 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1165 cmp = strcmp(p->name, name);
1169 retval = profile_create_node(name, value, &new);
1172 new->group_level = section->group_level+1;
1174 new->parent = section;
1182 section->first_child = new;
1189 * Iterate through the section, returning the nodes which match
1190 * the given name. If name is NULL, then interate through all the
1191 * nodes in the section. If section_flag is non-zero, only return the
1192 * section which matches the name; don't return relations. If value
1193 * is non-NULL, then only return relations which match the requested
1194 * value. (The value argument is ignored if section_flag is non-zero.)
1196 * The first time this routine is called, the state pointer must be
1197 * null. When this profile_find_node_relation() returns, if the state
1198 * pointer is non-NULL, then this routine should be called again.
1199 * (This won't happen if section_flag is non-zero, obviously.)
1202 errcode_t profile_find_node(struct profile_node *section, const char *name,
1203 const char *value, int section_flag, void **state,
1204 struct profile_node **node)
1206 struct profile_node *p;
1208 CHECK_MAGIC(section);
1213 p = section->first_child;
1215 for (; p; p = p->next) {
1216 if (name && (strcmp(p->name, name)))
1224 if (value && (strcmp(p->value, value)))
1236 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1239 * OK, we've found one match; now let's try to find another
1240 * one. This way, if we return a non-zero state pointer,
1241 * there's guaranteed to be another match that's returned.
1243 for (p = p->next; p; p = p->next) {
1244 if (name && (strcmp(p->name, name)))
1252 if (value && (strcmp(p->value, value)))
1263 * This is a general-purpose iterator for returning all nodes that
1264 * match the specified name array.
1266 struct profile_iterator {
1270 const char *const *names;
1275 struct profile_node *node;
1280 profile_iterator_create(profile_t profile, const char *const *names, int flags,
1283 struct profile_iterator *iter;
1287 return PROF_NO_PROFILE;
1288 if (profile->magic != PROF_MAGIC_PROFILE)
1289 return PROF_MAGIC_PROFILE;
1291 return PROF_BAD_NAMESET;
1292 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1294 return PROF_BAD_NAMESET;
1298 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1301 iter->magic = PROF_MAGIC_ITERATOR;
1302 iter->profile = profile;
1303 iter->names = names;
1304 iter->flags = flags;
1305 iter->file = profile->first_file;
1306 iter->done_idx = done_idx;
1313 void profile_iterator_free(void **iter_p)
1315 struct profile_iterator *iter;
1320 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1327 * Note: the returned character strings in ret_name and ret_value
1328 * points to the stored character string in the parse string. Before
1329 * this string value is returned to a calling application
1330 * (profile_node_iterator is not an exported interface), it should be
1333 errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1334 char **ret_name, char **ret_value)
1336 struct profile_iterator *iter = *iter_p;
1337 struct profile_node *section, *p;
1338 const char *const *cpp;
1342 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1343 return PROF_MAGIC_ITERATOR;
1344 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1345 return PROF_MAGIC_FILE;
1347 * If the file has changed, then the node pointer is invalid,
1348 * so we'll have search the file again looking for it.
1350 if (iter->node && (iter->file &&
1351 iter->file->upd_serial != iter->file_serial)) {
1352 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1353 skip_num = iter->num;
1356 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1357 return PROF_MAGIC_NODE;
1360 if (iter->node == 0) {
1361 if (iter->file == 0 ||
1362 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
1363 profile_iterator_free(iter_p);
1372 if ((retval = profile_update_file(iter->file))) {
1373 if (retval == ENOENT || retval == EACCES) {
1374 /* XXX memory leak? */
1375 iter->file = iter->file->next;
1380 profile_iterator_free(iter_p);
1384 iter->file_serial = iter->file->upd_serial;
1386 * Find the section to list if we are a LIST_SECTION,
1387 * or find the containing section if not.
1389 section = iter->file->root;
1390 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1391 for (p=section->first_child; p; p = p->next) {
1392 if (!strcmp(p->name, *cpp) && !p->value)
1401 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1404 iter->file = iter->file->next;
1409 iter->node = section->first_child;
1412 * OK, now we know iter->node is set up correctly. Let's do
1415 for (p = iter->node; p; p = p->next) {
1416 if (iter->name && strcmp(p->name, iter->name))
1418 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1421 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1434 iter->file = iter->file->next;
1439 if ((iter->node = p->next) == NULL)
1440 iter->file = iter->file->next;
1444 *ret_name = p->name;
1446 *ret_value = p->value;
1452 * prof_get.c --- routines that expose the public interfaces for
1453 * querying items from the profile.
1458 * This function only gets the first value from the file; it is a
1459 * helper function for profile_get_string, profile_get_integer, etc.
1461 errcode_t profile_get_value(profile_t profile, const char *name,
1462 const char *subname, const char *subsubname,
1463 const char **ret_value)
1468 const char *names[4];
1472 names[2] = subsubname;
1475 if ((retval = profile_iterator_create(profile, names,
1476 PROFILE_ITER_RELATIONS_ONLY,
1480 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1486 retval = PROF_NO_RELATION;
1489 profile_iterator_free(&state);
1494 profile_get_string(profile_t profile, const char *name, const char *subname,
1495 const char *subsubname, const char *def_val,
1502 retval = profile_get_value(profile, name, subname,
1503 subsubname, &value);
1504 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1512 *ret_string = malloc(strlen(value)+1);
1513 if (*ret_string == 0)
1515 strcpy(*ret_string, value);
1522 profile_get_integer(profile_t profile, const char *name, const char *subname,
1523 const char *subsubname, int def_val, int *ret_int)
1534 retval = profile_get_value(profile, name, subname, subsubname, &value);
1535 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1542 /* Empty string is no good. */
1543 return PROF_BAD_INTEGER;
1545 ret_long = strtol (value, &end_value, 10);
1547 /* Overflow or underflow. */
1548 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1549 return PROF_BAD_INTEGER;
1550 /* Value outside "int" range. */
1551 if ((long) (int) ret_long != ret_long)
1552 return PROF_BAD_INTEGER;
1553 /* Garbage in string. */
1554 if (end_value != value + strlen (value))
1555 return PROF_BAD_INTEGER;
1558 *ret_int = ret_long;
1563 profile_get_uint(profile_t profile, const char *name, const char *subname,
1564 const char *subsubname, unsigned int def_val,
1565 unsigned int *ret_int)
1570 unsigned long ret_long;
1576 retval = profile_get_value(profile, name, subname, subsubname, &value);
1577 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1584 /* Empty string is no good. */
1585 return PROF_BAD_INTEGER;
1587 ret_long = strtoul (value, &end_value, 10);
1589 /* Overflow or underflow. */
1590 if ((ret_long == ULONG_MAX) && errno != 0)
1591 return PROF_BAD_INTEGER;
1592 /* Value outside "int" range. */
1593 if ((unsigned long) (unsigned int) ret_long != ret_long)
1594 return PROF_BAD_INTEGER;
1595 /* Garbage in string. */
1596 if (end_value != value + strlen (value))
1597 return PROF_BAD_INTEGER;
1599 *ret_int = ret_long;
1604 profile_get_double(profile_t profile, const char *name, const char *subname,
1605 const char *subsubname, double def_val, double *ret_double)
1612 *ret_double = def_val;
1616 retval = profile_get_value(profile, name, subname, subsubname, &value);
1617 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1618 *ret_double = def_val;
1624 /* Empty string is no good. */
1625 return PROF_BAD_INTEGER;
1627 double_val = strtod(value, &end_value);
1629 /* Overflow or underflow. */
1631 return PROF_BAD_INTEGER;
1632 /* Garbage in string. */
1633 if (end_value != value + strlen(value))
1634 return PROF_BAD_INTEGER;
1636 *ret_double = double_val;
1640 static const char *const conf_yes[] = {
1641 "y", "yes", "true", "t", "1", "on",
1645 static const char *const conf_no[] = {
1646 "n", "no", "false", "nil", "0", "off",
1651 profile_parse_boolean(const char *s, int *ret_boolean)
1653 const char *const *p;
1655 if (ret_boolean == NULL)
1658 for(p=conf_yes; *p; p++) {
1659 if (!strcasecmp(*p,s)) {
1665 for(p=conf_no; *p; p++) {
1666 if (!strcasecmp(*p,s)) {
1672 return PROF_BAD_BOOLEAN;
1676 profile_get_boolean(profile_t profile, const char *name, const char *subname,
1677 const char *subsubname, int def_val, int *ret_boolean)
1683 *ret_boolean = def_val;
1687 retval = profile_get_value(profile, name, subname, subsubname, &value);
1688 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1689 *ret_boolean = def_val;
1694 return profile_parse_boolean (value, ret_boolean);
1698 profile_iterator(void **iter_p, char **ret_name, char **ret_value)
1703 retval = profile_node_iterator(iter_p, 0, &name, &value);
1709 *ret_name = malloc(strlen(name)+1);
1712 strcpy(*ret_name, name);
1718 *ret_value = malloc(strlen(value)+1);
1726 strcpy(*ret_value, value);
1733 #ifdef DEBUG_PROGRAM
1736 * test_profile.c --- testing program for the profile routine
1739 #include "argv_parse.h"
1740 #include "profile_helpers.h"
1742 const char *program_name = "test_profile";
1744 #define PRINT_VALUE 1
1745 #define PRINT_VALUES 2
1747 static void do_cmd(profile_t profile, char **argv)
1750 const char **names, *value;
1751 char **values, **cpp;
1756 names = (const char **) argv + 1;
1761 if (!strcmp(cmd, "query")) {
1762 retval = profile_get_values(profile, names, &values);
1763 print_status = PRINT_VALUES;
1764 } else if (!strcmp(cmd, "query1")) {
1765 const char *name = 0;
1766 const char *subname = 0;
1767 const char *subsubname = 0;
1773 subsubname = names[2];
1774 if (subsubname && names[3]) {
1776 "Only 3 levels are allowed with query1\n");
1779 retval = profile_get_value(profile, name, subname,
1780 subsubname, &value);
1781 print_status = PRINT_VALUE;
1782 } else if (!strcmp(cmd, "list_sections")) {
1783 retval = profile_get_subsection_names(profile, names,
1785 print_status = PRINT_VALUES;
1786 } else if (!strcmp(cmd, "list_relations")) {
1787 retval = profile_get_relation_names(profile, names,
1789 print_status = PRINT_VALUES;
1790 } else if (!strcmp(cmd, "dump")) {
1791 retval = profile_write_tree_file
1792 (profile->first_file->root, stdout);
1794 } else if (!strcmp(cmd, "clear")) {
1795 retval = profile_clear_relation(profile, names);
1796 } else if (!strcmp(cmd, "update")) {
1797 retval = profile_update_relation(profile, names+2,
1798 *names, *(names+1));
1800 } else if (!strcmp(cmd, "verify")) {
1801 retval = profile_verify_node
1802 (profile->first_file->root);
1804 } else if (!strcmp(cmd, "rename_section")) {
1805 retval = profile_rename_section(profile, names+1, *names);
1806 } else if (!strcmp(cmd, "add")) {
1808 if (strcmp(value, "NULL") == 0)
1810 retval = profile_add_relation(profile, names+1, value);
1811 } else if (!strcmp(cmd, "flush")) {
1812 retval = profile_flush(profile);
1815 printf("Invalid command.\n");
1818 com_err(cmd, retval, "");
1821 switch (print_status) {
1823 printf("%s\n", value);
1826 for (cpp = values; *cpp; cpp++)
1827 printf("%s\n", *cpp);
1828 profile_free_list(values);
1833 static void do_batchmode(profile_t profile)
1839 while (!feof(stdin)) {
1840 if (fgets(buf, sizeof(buf), stdin) == NULL)
1843 ret = argv_parse(buf, &argc, &argv);
1845 printf("Argv_parse returned %d!\n", ret);
1848 do_cmd(profile, argv);
1852 profile_release(profile);
1857 void syntax_err_report(const char *filename, long err, int line_num)
1859 fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
1860 filename, line_num, error_message(err));
1864 const char *default_str = "[foo]\n\tbar=quux\n\tsub = {\n\t\twin = true\n}\n";
1866 int main(int argc, char **argv)
1873 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
1877 initialize_prof_error_table();
1879 profile_set_syntax_err_cb(syntax_err_report);
1881 retval = profile_init_path(argv[1], &profile);
1883 com_err(program_name, retval, "while initializing profile");
1886 retval = profile_set_default(profile, default_str);
1888 com_err(program_name, retval, "while setting default");
1893 if (!cmd || !strcmp(cmd, "batch"))
1894 do_batchmode(profile);
1896 do_cmd(profile, argv+2);
1897 profile_release(profile);