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++) {
324 retval = get_dirlist(*fs, &array);
328 for (cpp = array; (cp = *cpp); cpp++) {
329 retval = profile_open_file(cp, &new_file);
330 if (retval == EACCES)
335 last = &new_file->next;
337 } else if ((retval != ENOTDIR) &&
338 strcmp(*fs, default_filename))
341 retval = profile_open_file(*fs, &new_file);
342 /* if this file is missing, skip to the next */
343 if (retval == ENOENT || retval == EACCES) {
349 last = &new_file->next;
352 * If all the files were not found, return the appropriate error.
354 if (!profile->first_file) {
361 *ret_profile = profile;
365 profile_release(profile);
370 profile_release(profile_t profile)
374 if (!profile || profile->magic != PROF_MAGIC_PROFILE)
377 for (p = profile->first_file; p; p = next) {
379 profile_free_file(p);
386 * This function sets the value of the pseudo file "<default>". If
387 * the file "<default>" had previously been passed to profile_init(),
388 * then def_string parameter will be parsed and used as the profile
389 * information for the "<default>" file.
391 errcode_t profile_set_default(profile_t profile, const char *def_string)
393 struct parse_state state;
397 char *line, *p, *end;
400 if (!def_string || !profile || profile->magic != PROF_MAGIC_PROFILE)
401 return PROF_MAGIC_PROFILE;
403 for (prf = profile->first_file; prf; prf = prf->next) {
404 if (strcmp(prf->filespec, default_filename) == 0)
411 profile_free_node(prf->root);
415 memset(&state, 0, sizeof(struct parse_state));
416 retval = profile_create_node("(root)", 0, &state.root_section);
424 end = strchr(in, '\n');
425 len = end ? (end - in) : (int) strlen(in);
426 if (len >= line_size) {
428 p = realloc(line, line_size);
435 memcpy(line, in, len);
437 retval = parse_line(line, &state);
441 (syntax_err_cb)(prf->filespec, retval,
445 profile_free_node(prf->root);
452 prf->root = state.root_section;
459 * prof_file.c ---- routines that manipulate an individual profile file.
462 errcode_t profile_open_file(const char * filespec,
463 prf_file_t *ret_prof)
469 char *expanded_filename;
471 prf = malloc(sizeof(struct _prf_file_t));
474 memset(prf, 0, sizeof(struct _prf_file_t));
475 prf->magic = PROF_MAGIC_FILE;
477 len = strlen(filespec)+1;
478 if (filespec[0] == '~' && filespec[1] == '/') {
479 home_env = getenv("HOME");
481 if (home_env == NULL) {
482 #ifdef HAVE_GETWUID_R
483 struct passwd *pw, pwx;
488 if (!getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
489 && pw != NULL && pw->pw_dir[0] != 0)
490 home_env = pw->pw_dir;
494 pw = getpwuid(getuid());
495 home_env = pw->pw_dir;
500 len += strlen(home_env);
502 expanded_filename = malloc(len);
503 if (expanded_filename == 0) {
504 profile_free_file(prf);
508 strcpy(expanded_filename, home_env);
509 strcat(expanded_filename, filespec+1);
511 memcpy(expanded_filename, filespec, len);
513 prf->filespec = expanded_filename;
515 if (strcmp(prf->filespec, default_filename) != 0) {
516 retval = profile_update_file(prf);
518 profile_free_file(prf);
527 errcode_t profile_update_file(prf_file_t prf)
532 #ifdef STAT_ONCE_PER_SECOND
538 struct parse_state state;
540 if (prf->flags & PROFILE_FILE_NO_RELOAD)
544 #ifdef STAT_ONCE_PER_SECOND
546 if (now == prf->last_stat && prf->root != NULL) {
550 if (stat(prf->filespec, &st)) {
554 #ifdef STAT_ONCE_PER_SECOND
555 prf->last_stat = now;
557 if (st.st_mtime == prf->timestamp && prf->root != NULL) {
561 profile_free_node(prf->root);
566 * If we don't have the stat() call, assume that our in-core
567 * memory image is correct. That is, we won't reread the
568 * profile file if it changes.
574 memset(&state, 0, sizeof(struct parse_state));
575 retval = profile_create_node("(root)", 0, &state.root_section);
579 f = fopen(prf->filespec, "r");
588 if (fgets(buf, sizeof(buf), f) == NULL)
590 retval = parse_line(buf, &state);
593 (syntax_err_cb)(prf->filespec, retval,
599 prf->root = state.root_section;
604 prf->timestamp = st.st_mtime;
609 void profile_free_file(prf_file_t prf)
612 profile_free_node(prf->root);
617 /* Begin the profile parser */
619 profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
621 profile_syntax_err_cb_t old;
624 syntax_err_cb = hook;
628 #define STATE_INIT_COMMENT 0
629 #define STATE_STD_LINE 1
630 #define STATE_GET_OBRACE 2
632 static char *skip_over_blanks(char *cp)
634 while (*cp && isspace((int) (*cp)))
639 static int end_or_comment(char ch)
641 return (ch == 0 || ch == '#' || ch == ';');
644 static char *skip_over_nonblanks(char *cp)
646 while (!end_or_comment(*cp) && !isspace(*cp))
651 static void strip_line(char *line)
653 char *p = line + strlen(line);
654 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
658 static void parse_quoted_string(char *str)
664 for (to = from = str; *from && *from != '"'; to++, from++) {
687 static errcode_t parse_line(char *line, struct parse_state *state)
689 char *cp, ch, *tag, *value;
692 struct profile_node *node;
693 int do_subsection = 0;
697 if (state->state == STATE_GET_OBRACE) {
698 cp = skip_over_blanks(line);
700 return PROF_MISSING_OBRACE;
701 state->state = STATE_STD_LINE;
704 if (state->state == STATE_INIT_COMMENT) {
707 state->state = STATE_STD_LINE;
713 cp = skip_over_blanks(line);
715 if (end_or_comment(ch))
718 if (state->group_level > 0)
719 return PROF_SECTION_NOTOP;
721 cp = skip_over_blanks(cp);
724 return PROF_SECTION_SYNTAX;
727 parse_quoted_string(cp);
730 while (isspace(*p) && (p > cp))
733 return PROF_SECTION_SYNTAX;
735 retval = profile_find_node(state->root_section, cp, 0, 1,
736 &iter, &state->current_section);
737 if (retval == PROF_NO_SECTION) {
738 retval = profile_add_node(state->root_section,
740 &state->current_section);
747 * Finish off the rest of the line.
751 state->current_section->final = 1;
755 * Spaces or comments after ']' should not be fatal
757 cp = skip_over_blanks(cp);
758 if (!end_or_comment(*cp))
759 return PROF_SECTION_SYNTAX;
763 if (state->group_level == 0)
764 return PROF_EXTRA_CBRACE;
766 state->current_section->final = 1;
767 state->current_section = state->current_section->parent;
768 state->group_level--;
772 * Parse the relations
775 cp = strchr(cp, '=');
777 return PROF_RELATION_SYNTAX;
779 return PROF_RELATION_SYNTAX;
783 parse_quoted_string(tag);
785 /* Look for whitespace on left-hand side. */
786 p = skip_over_nonblanks(tag);
789 p = skip_over_blanks(p);
790 /* If we have more non-whitespace, it's an error. */
792 return PROF_RELATION_SYNTAX;
795 cp = skip_over_blanks(cp+1);
800 parse_quoted_string(value);
801 } else if (end_or_comment(ch)) {
803 state->state = STATE_GET_OBRACE;
804 } else if (value[0] == '{') {
805 cp = skip_over_blanks(value+1);
807 if (end_or_comment(ch))
810 return PROF_RELATION_SYNTAX;
812 cp = skip_over_nonblanks(value);
813 p = skip_over_blanks(cp);
816 if (!end_or_comment(ch))
817 return PROF_RELATION_SYNTAX;
820 p = strchr(tag, '*');
823 retval = profile_add_node(state->current_section,
824 tag, 0, &state->current_section);
828 state->current_section->final = 1;
829 state->group_level++;
832 p = strchr(tag, '*');
835 profile_add_node(state->current_section, tag, value, &node);
843 * Return TRUE if the string begins or ends with whitespace
845 static int need_double_quotes(char *str)
849 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
851 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
852 strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
858 * Output a string with double quotes, doing appropriate backquoting
859 * of characters as necessary.
861 static void output_quoted_string(char *str, void (*cb)(const char *,void *),
873 while ((ch = *str++)) {
888 /* This would be a lot faster if we scanned
889 forward for the next "interesting"
903 /* Errors should be returned, not ignored! */
904 static void dump_profile(struct profile_node *root, int level,
905 void (*cb)(const char *, void *), void *data)
908 struct profile_node *p;
914 retval = profile_find_node(root, 0, 0, 0, &iter, &p);
917 for (i=0; i < level; i++)
919 if (need_double_quotes(p->name))
920 output_quoted_string(p->name, cb, data);
924 if (need_double_quotes(p->value))
925 output_quoted_string(p->value, cb, data);
933 retval = profile_find_node(root, 0, 0, 1, &iter, &p);
936 if (level == 0) { /* [xxx] */
938 if (need_double_quotes(p->name))
939 output_quoted_string(p->name, cb, data);
943 cb(p->final ? "*" : "", data);
945 dump_profile(p, level+1, cb, data);
947 } else { /* xxx = { ... } */
948 for (i=0; i < level; i++)
950 if (need_double_quotes(p->name))
951 output_quoted_string(p->name, cb, data);
956 dump_profile(p, level+1, cb, data);
957 for (i=0; i < level; i++)
960 cb(p->final ? "*" : "", data);
966 static void dump_profile_to_file_cb(const char *str, void *data)
971 errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
973 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
983 static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
987 if (b->max - b->cur < len) {
991 newsize = b->max + (b->max >> 1) + len + 1024;
992 newptr = realloc(b->base, newsize);
993 if (newptr == NULL) {
1000 memcpy(b->base + b->cur, d, len);
1001 b->cur += len; /* ignore overflow */
1004 static void dump_profile_to_buffer_cb(const char *str, void *data)
1006 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
1009 errcode_t profile_write_tree_to_buffer(struct profile_node *root,
1012 struct prof_buf prof_buf = { 0, 0, 0, 0 };
1014 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
1019 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
1020 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
1021 char *newptr = realloc(prof_buf.base, prof_buf.cur);
1023 prof_buf.base = newptr;
1025 *buf = prof_buf.base;
1031 * prof_tree.c --- these routines maintain the parse tree of the
1034 * All of the details of how the tree is stored is abstracted away in
1035 * this file; all of the other profile routines build, access, and
1036 * modify the tree via the accessor functions found in this file.
1038 * Each node may represent either a relation or a section header.
1040 * A section header must have its value field set to 0, and may a one
1041 * or more child nodes, pointed to by first_child.
1043 * A relation has as its value a pointer to allocated memory
1044 * containing a string. Its first_child pointer must be null.
1049 * Free a node, and any children
1051 void profile_free_node(struct profile_node *node)
1053 struct profile_node *child, *next;
1055 if (node->magic != PROF_MAGIC_NODE)
1061 for (child=node->first_child; child; child = next) {
1063 profile_free_node(child);
1072 #define strdup MYstrdup
1073 static char *MYstrdup (const char *s)
1075 size_t sz = strlen(s) + 1;
1076 char *p = malloc(sz);
1086 errcode_t profile_create_node(const char *name, const char *value,
1087 struct profile_node **ret_node)
1089 struct profile_node *new;
1091 new = malloc(sizeof(struct profile_node));
1094 memset(new, 0, sizeof(struct profile_node));
1095 new->name = strdup(name);
1096 if (new->name == 0) {
1097 profile_free_node(new);
1101 new->value = strdup(value);
1102 if (new->value == 0) {
1103 profile_free_node(new);
1107 new->magic = PROF_MAGIC_NODE;
1114 * This function verifies that all of the representation invarients of
1115 * the profile are true. If not, we have a programming bug somewhere,
1116 * probably in this file.
1118 #ifdef DEBUG_PROGRAM
1119 errcode_t profile_verify_node(struct profile_node *node)
1121 struct profile_node *p, *last;
1126 if (node->value && node->first_child)
1127 return PROF_SECTION_WITH_VALUE;
1130 for (p = node->first_child; p; last = p, p = p->next) {
1131 if (p->prev != last)
1132 return PROF_BAD_LINK_LIST;
1133 if (last && (last->next != p))
1134 return PROF_BAD_LINK_LIST;
1135 if (node->group_level+1 != p->group_level)
1136 return PROF_BAD_GROUP_LVL;
1137 if (p->parent != node)
1138 return PROF_BAD_PARENT_PTR;
1139 retval = profile_verify_node(p);
1148 * Add a node to a particular section
1150 errcode_t profile_add_node(struct profile_node *section, const char *name,
1151 const char *value, struct profile_node **ret_node)
1154 struct profile_node *p, *last, *new;
1156 CHECK_MAGIC(section);
1159 return PROF_ADD_NOT_SECTION;
1162 * Find the place to insert the new node. We look for the
1163 * place *after* the last match of the node name, since
1166 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1168 cmp = strcmp(p->name, name);
1172 retval = profile_create_node(name, value, &new);
1175 new->group_level = section->group_level+1;
1177 new->parent = section;
1185 section->first_child = new;
1192 * Iterate through the section, returning the nodes which match
1193 * the given name. If name is NULL, then interate through all the
1194 * nodes in the section. If section_flag is non-zero, only return the
1195 * section which matches the name; don't return relations. If value
1196 * is non-NULL, then only return relations which match the requested
1197 * value. (The value argument is ignored if section_flag is non-zero.)
1199 * The first time this routine is called, the state pointer must be
1200 * null. When this profile_find_node_relation() returns, if the state
1201 * pointer is non-NULL, then this routine should be called again.
1202 * (This won't happen if section_flag is non-zero, obviously.)
1205 errcode_t profile_find_node(struct profile_node *section, const char *name,
1206 const char *value, int section_flag, void **state,
1207 struct profile_node **node)
1209 struct profile_node *p;
1211 CHECK_MAGIC(section);
1216 p = section->first_child;
1218 for (; p; p = p->next) {
1219 if (name && (strcmp(p->name, name)))
1227 if (value && (strcmp(p->value, value)))
1239 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1242 * OK, we've found one match; now let's try to find another
1243 * one. This way, if we return a non-zero state pointer,
1244 * there's guaranteed to be another match that's returned.
1246 for (p = p->next; p; p = p->next) {
1247 if (name && (strcmp(p->name, name)))
1255 if (value && (strcmp(p->value, value)))
1266 * This is a general-purpose iterator for returning all nodes that
1267 * match the specified name array.
1269 struct profile_iterator {
1273 const char *const *names;
1278 struct profile_node *node;
1283 profile_iterator_create(profile_t profile, const char *const *names, int flags,
1286 struct profile_iterator *iter;
1290 return PROF_NO_PROFILE;
1291 if (profile->magic != PROF_MAGIC_PROFILE)
1292 return PROF_MAGIC_PROFILE;
1294 return PROF_BAD_NAMESET;
1295 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1297 return PROF_BAD_NAMESET;
1301 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1304 iter->magic = PROF_MAGIC_ITERATOR;
1305 iter->profile = profile;
1306 iter->names = names;
1307 iter->flags = flags;
1308 iter->file = profile->first_file;
1309 iter->done_idx = done_idx;
1316 void profile_iterator_free(void **iter_p)
1318 struct profile_iterator *iter;
1323 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1330 * Note: the returned character strings in ret_name and ret_value
1331 * points to the stored character string in the parse string. Before
1332 * this string value is returned to a calling application
1333 * (profile_node_iterator is not an exported interface), it should be
1336 errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1337 char **ret_name, char **ret_value)
1339 struct profile_iterator *iter = *iter_p;
1340 struct profile_node *section, *p;
1341 const char *const *cpp;
1345 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1346 return PROF_MAGIC_ITERATOR;
1347 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1348 return PROF_MAGIC_FILE;
1350 * If the file has changed, then the node pointer is invalid,
1351 * so we'll have search the file again looking for it.
1353 if (iter->node && (iter->file &&
1354 iter->file->upd_serial != iter->file_serial)) {
1355 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1356 skip_num = iter->num;
1359 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1360 return PROF_MAGIC_NODE;
1363 if (iter->node == 0) {
1364 if (iter->file == 0 ||
1365 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
1366 profile_iterator_free(iter_p);
1375 if ((retval = profile_update_file(iter->file))) {
1376 if (retval == ENOENT || retval == EACCES) {
1377 /* XXX memory leak? */
1378 iter->file = iter->file->next;
1383 profile_iterator_free(iter_p);
1387 iter->file_serial = iter->file->upd_serial;
1389 * Find the section to list if we are a LIST_SECTION,
1390 * or find the containing section if not.
1392 section = iter->file->root;
1393 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1394 for (p=section->first_child; p; p = p->next) {
1395 if (!strcmp(p->name, *cpp) && !p->value)
1404 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1407 iter->file = iter->file->next;
1412 iter->node = section->first_child;
1415 * OK, now we know iter->node is set up correctly. Let's do
1418 for (p = iter->node; p; p = p->next) {
1419 if (iter->name && strcmp(p->name, iter->name))
1421 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1424 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1437 iter->file = iter->file->next;
1442 if ((iter->node = p->next) == NULL)
1443 iter->file = iter->file->next;
1447 *ret_name = p->name;
1449 *ret_value = p->value;
1455 * prof_get.c --- routines that expose the public interfaces for
1456 * querying items from the profile.
1461 * This function only gets the first value from the file; it is a
1462 * helper function for profile_get_string, profile_get_integer, etc.
1464 errcode_t profile_get_value(profile_t profile, const char *name,
1465 const char *subname, const char *subsubname,
1466 const char **ret_value)
1471 const char *names[4];
1475 names[2] = subsubname;
1478 if ((retval = profile_iterator_create(profile, names,
1479 PROFILE_ITER_RELATIONS_ONLY,
1483 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1489 retval = PROF_NO_RELATION;
1492 profile_iterator_free(&state);
1497 profile_get_string(profile_t profile, const char *name, const char *subname,
1498 const char *subsubname, const char *def_val,
1505 retval = profile_get_value(profile, name, subname,
1506 subsubname, &value);
1507 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1515 *ret_string = malloc(strlen(value)+1);
1516 if (*ret_string == 0)
1518 strcpy(*ret_string, value);
1525 profile_get_integer(profile_t profile, const char *name, const char *subname,
1526 const char *subsubname, int def_val, int *ret_int)
1537 retval = profile_get_value(profile, name, subname, subsubname, &value);
1538 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1545 /* Empty string is no good. */
1546 return PROF_BAD_INTEGER;
1548 ret_long = strtol (value, &end_value, 10);
1550 /* Overflow or underflow. */
1551 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1552 return PROF_BAD_INTEGER;
1553 /* Value outside "int" range. */
1554 if ((long) (int) ret_long != ret_long)
1555 return PROF_BAD_INTEGER;
1556 /* Garbage in string. */
1557 if (end_value != value + strlen (value))
1558 return PROF_BAD_INTEGER;
1561 *ret_int = ret_long;
1566 profile_get_uint(profile_t profile, const char *name, const char *subname,
1567 const char *subsubname, unsigned int def_val,
1568 unsigned int *ret_int)
1573 unsigned long ret_long;
1579 retval = profile_get_value(profile, name, subname, subsubname, &value);
1580 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1587 /* Empty string is no good. */
1588 return PROF_BAD_INTEGER;
1590 ret_long = strtoul (value, &end_value, 10);
1592 /* Overflow or underflow. */
1593 if ((ret_long == ULONG_MAX) && errno != 0)
1594 return PROF_BAD_INTEGER;
1595 /* Value outside "int" range. */
1596 if ((unsigned long) (unsigned int) ret_long != ret_long)
1597 return PROF_BAD_INTEGER;
1598 /* Garbage in string. */
1599 if (end_value != value + strlen (value))
1600 return PROF_BAD_INTEGER;
1602 *ret_int = ret_long;
1607 profile_get_double(profile_t profile, const char *name, const char *subname,
1608 const char *subsubname, double def_val, double *ret_double)
1615 *ret_double = def_val;
1619 retval = profile_get_value(profile, name, subname, subsubname, &value);
1620 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1621 *ret_double = def_val;
1627 /* Empty string is no good. */
1628 return PROF_BAD_INTEGER;
1630 double_val = strtod(value, &end_value);
1632 /* Overflow or underflow. */
1634 return PROF_BAD_INTEGER;
1635 /* Garbage in string. */
1636 if (end_value != value + strlen(value))
1637 return PROF_BAD_INTEGER;
1639 *ret_double = double_val;
1643 static const char *const conf_yes[] = {
1644 "y", "yes", "true", "t", "1", "on",
1648 static const char *const conf_no[] = {
1649 "n", "no", "false", "nil", "0", "off",
1654 profile_parse_boolean(const char *s, int *ret_boolean)
1656 const char *const *p;
1658 if (ret_boolean == NULL)
1661 for(p=conf_yes; *p; p++) {
1662 if (!strcasecmp(*p,s)) {
1668 for(p=conf_no; *p; p++) {
1669 if (!strcasecmp(*p,s)) {
1675 return PROF_BAD_BOOLEAN;
1679 profile_get_boolean(profile_t profile, const char *name, const char *subname,
1680 const char *subsubname, int def_val, int *ret_boolean)
1686 *ret_boolean = def_val;
1690 retval = profile_get_value(profile, name, subname, subsubname, &value);
1691 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1692 *ret_boolean = def_val;
1697 return profile_parse_boolean (value, ret_boolean);
1701 profile_iterator(void **iter_p, char **ret_name, char **ret_value)
1706 retval = profile_node_iterator(iter_p, 0, &name, &value);
1712 *ret_name = malloc(strlen(name)+1);
1715 strcpy(*ret_name, name);
1721 *ret_value = malloc(strlen(value)+1);
1729 strcpy(*ret_value, value);
1736 #ifdef DEBUG_PROGRAM
1739 * test_profile.c --- testing program for the profile routine
1742 #include "argv_parse.h"
1743 #include "profile_helpers.h"
1745 const char *program_name = "test_profile";
1747 #define PRINT_VALUE 1
1748 #define PRINT_VALUES 2
1750 static void do_cmd(profile_t profile, char **argv)
1753 const char **names, *value;
1754 char **values, **cpp;
1759 names = (const char **) argv + 1;
1764 if (!strcmp(cmd, "query")) {
1765 retval = profile_get_values(profile, names, &values);
1766 print_status = PRINT_VALUES;
1767 } else if (!strcmp(cmd, "query1")) {
1768 const char *name = 0;
1769 const char *subname = 0;
1770 const char *subsubname = 0;
1776 subsubname = names[2];
1777 if (subsubname && names[3]) {
1779 "Only 3 levels are allowed with query1\n");
1782 retval = profile_get_value(profile, name, subname,
1783 subsubname, &value);
1784 print_status = PRINT_VALUE;
1785 } else if (!strcmp(cmd, "list_sections")) {
1786 retval = profile_get_subsection_names(profile, names,
1788 print_status = PRINT_VALUES;
1789 } else if (!strcmp(cmd, "list_relations")) {
1790 retval = profile_get_relation_names(profile, names,
1792 print_status = PRINT_VALUES;
1793 } else if (!strcmp(cmd, "dump")) {
1794 retval = profile_write_tree_file
1795 (profile->first_file->root, stdout);
1797 } else if (!strcmp(cmd, "clear")) {
1798 retval = profile_clear_relation(profile, names);
1799 } else if (!strcmp(cmd, "update")) {
1800 retval = profile_update_relation(profile, names+2,
1801 *names, *(names+1));
1803 } else if (!strcmp(cmd, "verify")) {
1804 retval = profile_verify_node
1805 (profile->first_file->root);
1807 } else if (!strcmp(cmd, "rename_section")) {
1808 retval = profile_rename_section(profile, names+1, *names);
1809 } else if (!strcmp(cmd, "add")) {
1811 if (strcmp(value, "NULL") == 0)
1813 retval = profile_add_relation(profile, names+1, value);
1814 } else if (!strcmp(cmd, "flush")) {
1815 retval = profile_flush(profile);
1818 printf("Invalid command.\n");
1821 com_err(cmd, retval, "");
1824 switch (print_status) {
1826 printf("%s\n", value);
1829 for (cpp = values; *cpp; cpp++)
1830 printf("%s\n", *cpp);
1831 profile_free_list(values);
1836 static void do_batchmode(profile_t profile)
1842 while (!feof(stdin)) {
1843 if (fgets(buf, sizeof(buf), stdin) == NULL)
1846 ret = argv_parse(buf, &argc, &argv);
1848 printf("Argv_parse returned %d!\n", ret);
1851 do_cmd(profile, argv);
1855 profile_release(profile);
1860 void syntax_err_report(const char *filename, long err, int line_num)
1862 fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
1863 filename, line_num, error_message(err));
1867 const char *default_str = "[foo]\n\tbar=quux\n\tsub = {\n\t\twin = true\n}\n";
1869 int main(int argc, char **argv)
1876 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
1880 initialize_prof_error_table();
1882 profile_set_syntax_err_cb(syntax_err_report);
1884 retval = profile_init_path(argv[1], &profile);
1886 com_err(program_name, retval, "while initializing profile");
1889 retval = profile_set_default(profile, default_str);
1891 com_err(program_name, retval, "while setting default");
1896 if (!cmd || !strcmp(cmd, "batch"))
1897 do_batchmode(profile);
1899 do_cmd(profile, argv+2);
1900 profile_release(profile);