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);
610 /* Begin the profile parser */
612 profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
614 profile_syntax_err_cb_t old;
617 syntax_err_cb = hook;
621 #define STATE_INIT_COMMENT 0
622 #define STATE_STD_LINE 1
623 #define STATE_GET_OBRACE 2
625 static char *skip_over_blanks(char *cp)
627 while (*cp && isspace((int) (*cp)))
632 static int end_or_comment(char ch)
634 return (ch == 0 || ch == '#' || ch == ';');
637 static char *skip_over_nonblanks(char *cp)
639 while (!end_or_comment(*cp) && !isspace(*cp))
644 static void strip_line(char *line)
646 char *p = line + strlen(line);
647 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
651 static void parse_quoted_string(char *str)
657 for (to = from = str; *from && *from != '"'; to++, from++) {
680 static errcode_t parse_line(char *line, struct parse_state *state)
682 char *cp, ch, *tag, *value;
685 struct profile_node *node;
686 int do_subsection = 0;
690 if (state->state == STATE_GET_OBRACE) {
691 cp = skip_over_blanks(line);
693 return PROF_MISSING_OBRACE;
694 state->state = STATE_STD_LINE;
697 if (state->state == STATE_INIT_COMMENT) {
700 state->state = STATE_STD_LINE;
706 cp = skip_over_blanks(line);
708 if (end_or_comment(ch))
711 if (state->group_level > 0)
712 return PROF_SECTION_NOTOP;
714 cp = skip_over_blanks(cp);
717 return PROF_SECTION_SYNTAX;
720 parse_quoted_string(cp);
723 while (isspace(*p) && (p > cp))
726 return PROF_SECTION_SYNTAX;
728 retval = profile_find_node(state->root_section, cp, 0, 1,
729 &iter, &state->current_section);
730 if (retval == PROF_NO_SECTION) {
731 retval = profile_add_node(state->root_section,
733 &state->current_section);
740 * Finish off the rest of the line.
744 state->current_section->final = 1;
748 * Spaces or comments after ']' should not be fatal
750 cp = skip_over_blanks(cp);
751 if (!end_or_comment(*cp))
752 return PROF_SECTION_SYNTAX;
756 if (state->group_level == 0)
757 return PROF_EXTRA_CBRACE;
759 state->current_section->final = 1;
760 state->current_section = state->current_section->parent;
761 state->group_level--;
765 * Parse the relations
768 cp = strchr(cp, '=');
770 return PROF_RELATION_SYNTAX;
772 return PROF_RELATION_SYNTAX;
776 parse_quoted_string(tag);
778 /* Look for whitespace on left-hand side. */
779 p = skip_over_nonblanks(tag);
782 p = skip_over_blanks(p);
783 /* If we have more non-whitespace, it's an error. */
785 return PROF_RELATION_SYNTAX;
788 cp = skip_over_blanks(cp+1);
793 parse_quoted_string(value);
794 } else if (end_or_comment(ch)) {
796 state->state = STATE_GET_OBRACE;
797 } else if (value[0] == '{') {
798 cp = skip_over_blanks(value+1);
800 if (end_or_comment(ch))
803 return PROF_RELATION_SYNTAX;
805 cp = skip_over_nonblanks(value);
806 p = skip_over_blanks(cp);
809 if (!end_or_comment(ch))
810 return PROF_RELATION_SYNTAX;
813 p = strchr(tag, '*');
816 retval = profile_add_node(state->current_section,
817 tag, 0, &state->current_section);
821 state->current_section->final = 1;
822 state->group_level++;
825 p = strchr(tag, '*');
828 profile_add_node(state->current_section, tag, value, &node);
836 * Return TRUE if the string begins or ends with whitespace
838 static int need_double_quotes(char *str)
842 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
844 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
845 strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
851 * Output a string with double quotes, doing appropriate backquoting
852 * of characters as necessary.
854 static void output_quoted_string(char *str, void (*cb)(const char *,void *),
866 while ((ch = *str++)) {
881 /* This would be a lot faster if we scanned
882 forward for the next "interesting"
896 /* Errors should be returned, not ignored! */
897 static void dump_profile(struct profile_node *root, int level,
898 void (*cb)(const char *, void *), void *data)
901 struct profile_node *p;
907 retval = profile_find_node(root, 0, 0, 0, &iter, &p);
910 for (i=0; i < level; i++)
912 if (need_double_quotes(p->name))
913 output_quoted_string(p->name, cb, data);
917 if (need_double_quotes(p->value))
918 output_quoted_string(p->value, cb, data);
926 retval = profile_find_node(root, 0, 0, 1, &iter, &p);
929 if (level == 0) { /* [xxx] */
931 if (need_double_quotes(p->name))
932 output_quoted_string(p->name, cb, data);
936 cb(p->final ? "*" : "", data);
938 dump_profile(p, level+1, cb, data);
940 } else { /* xxx = { ... } */
941 for (i=0; i < level; i++)
943 if (need_double_quotes(p->name))
944 output_quoted_string(p->name, cb, data);
949 dump_profile(p, level+1, cb, data);
950 for (i=0; i < level; i++)
953 cb(p->final ? "*" : "", data);
959 static void dump_profile_to_file_cb(const char *str, void *data)
964 errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
966 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
976 static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
980 if (b->max - b->cur < len) {
984 newsize = b->max + (b->max >> 1) + len + 1024;
985 newptr = realloc(b->base, newsize);
986 if (newptr == NULL) {
993 memcpy(b->base + b->cur, d, len);
994 b->cur += len; /* ignore overflow */
997 static void dump_profile_to_buffer_cb(const char *str, void *data)
999 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
1002 errcode_t profile_write_tree_to_buffer(struct profile_node *root,
1005 struct prof_buf prof_buf = { 0, 0, 0, 0 };
1007 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
1012 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
1013 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
1014 char *newptr = realloc(prof_buf.base, prof_buf.cur);
1016 prof_buf.base = newptr;
1018 *buf = prof_buf.base;
1024 * prof_tree.c --- these routines maintain the parse tree of the
1027 * All of the details of how the tree is stored is abstracted away in
1028 * this file; all of the other profile routines build, access, and
1029 * modify the tree via the accessor functions found in this file.
1031 * Each node may represent either a relation or a section header.
1033 * A section header must have its value field set to 0, and may a one
1034 * or more child nodes, pointed to by first_child.
1036 * A relation has as its value a pointer to allocated memory
1037 * containing a string. Its first_child pointer must be null.
1042 * Free a node, and any children
1044 void profile_free_node(struct profile_node *node)
1046 struct profile_node *child, *next;
1048 if (node->magic != PROF_MAGIC_NODE)
1054 for (child=node->first_child; child; child = next) {
1056 profile_free_node(child);
1065 #define strdup MYstrdup
1066 static char *MYstrdup (const char *s)
1068 size_t sz = strlen(s) + 1;
1069 char *p = malloc(sz);
1079 errcode_t profile_create_node(const char *name, const char *value,
1080 struct profile_node **ret_node)
1082 struct profile_node *new;
1084 new = malloc(sizeof(struct profile_node));
1087 memset(new, 0, sizeof(struct profile_node));
1088 new->name = strdup(name);
1089 if (new->name == 0) {
1090 profile_free_node(new);
1094 new->value = strdup(value);
1095 if (new->value == 0) {
1096 profile_free_node(new);
1100 new->magic = PROF_MAGIC_NODE;
1107 * This function verifies that all of the representation invarients of
1108 * the profile are true. If not, we have a programming bug somewhere,
1109 * probably in this file.
1111 #ifdef DEBUG_PROGRAM
1112 errcode_t profile_verify_node(struct profile_node *node)
1114 struct profile_node *p, *last;
1119 if (node->value && node->first_child)
1120 return PROF_SECTION_WITH_VALUE;
1123 for (p = node->first_child; p; last = p, p = p->next) {
1124 if (p->prev != last)
1125 return PROF_BAD_LINK_LIST;
1126 if (last && (last->next != p))
1127 return PROF_BAD_LINK_LIST;
1128 if (node->group_level+1 != p->group_level)
1129 return PROF_BAD_GROUP_LVL;
1130 if (p->parent != node)
1131 return PROF_BAD_PARENT_PTR;
1132 retval = profile_verify_node(p);
1141 * Add a node to a particular section
1143 errcode_t profile_add_node(struct profile_node *section, const char *name,
1144 const char *value, struct profile_node **ret_node)
1147 struct profile_node *p, *last, *new;
1149 CHECK_MAGIC(section);
1152 return PROF_ADD_NOT_SECTION;
1155 * Find the place to insert the new node. We look for the
1156 * place *after* the last match of the node name, since
1159 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1161 cmp = strcmp(p->name, name);
1165 retval = profile_create_node(name, value, &new);
1168 new->group_level = section->group_level+1;
1170 new->parent = section;
1178 section->first_child = new;
1185 * Iterate through the section, returning the nodes which match
1186 * the given name. If name is NULL, then interate through all the
1187 * nodes in the section. If section_flag is non-zero, only return the
1188 * section which matches the name; don't return relations. If value
1189 * is non-NULL, then only return relations which match the requested
1190 * value. (The value argument is ignored if section_flag is non-zero.)
1192 * The first time this routine is called, the state pointer must be
1193 * null. When this profile_find_node_relation() returns, if the state
1194 * pointer is non-NULL, then this routine should be called again.
1195 * (This won't happen if section_flag is non-zero, obviously.)
1198 errcode_t profile_find_node(struct profile_node *section, const char *name,
1199 const char *value, int section_flag, void **state,
1200 struct profile_node **node)
1202 struct profile_node *p;
1204 CHECK_MAGIC(section);
1209 p = section->first_child;
1211 for (; p; p = p->next) {
1212 if (name && (strcmp(p->name, name)))
1220 if (value && (strcmp(p->value, value)))
1232 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1235 * OK, we've found one match; now let's try to find another
1236 * one. This way, if we return a non-zero state pointer,
1237 * there's guaranteed to be another match that's returned.
1239 for (p = p->next; p; p = p->next) {
1240 if (name && (strcmp(p->name, name)))
1248 if (value && (strcmp(p->value, value)))
1259 * This is a general-purpose iterator for returning all nodes that
1260 * match the specified name array.
1262 struct profile_iterator {
1266 const char *const *names;
1271 struct profile_node *node;
1276 profile_iterator_create(profile_t profile, const char *const *names, int flags,
1279 struct profile_iterator *iter;
1283 return PROF_NO_PROFILE;
1284 if (profile->magic != PROF_MAGIC_PROFILE)
1285 return PROF_MAGIC_PROFILE;
1287 return PROF_BAD_NAMESET;
1288 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1290 return PROF_BAD_NAMESET;
1294 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1297 iter->magic = PROF_MAGIC_ITERATOR;
1298 iter->profile = profile;
1299 iter->names = names;
1300 iter->flags = flags;
1301 iter->file = profile->first_file;
1302 iter->done_idx = done_idx;
1309 void profile_iterator_free(void **iter_p)
1311 struct profile_iterator *iter;
1316 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1323 * Note: the returned character strings in ret_name and ret_value
1324 * points to the stored character string in the parse string. Before
1325 * this string value is returned to a calling application
1326 * (profile_node_iterator is not an exported interface), it should be
1329 errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1330 char **ret_name, char **ret_value)
1332 struct profile_iterator *iter = *iter_p;
1333 struct profile_node *section, *p;
1334 const char *const *cpp;
1338 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1339 return PROF_MAGIC_ITERATOR;
1340 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1341 return PROF_MAGIC_FILE;
1343 * If the file has changed, then the node pointer is invalid,
1344 * so we'll have search the file again looking for it.
1346 if (iter->node && (iter->file &&
1347 iter->file->upd_serial != iter->file_serial)) {
1348 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1349 skip_num = iter->num;
1352 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1353 return PROF_MAGIC_NODE;
1356 if (iter->node == 0) {
1357 if (iter->file == 0 ||
1358 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
1359 profile_iterator_free(iter_p);
1368 if ((retval = profile_update_file(iter->file))) {
1369 if (retval == ENOENT || retval == EACCES) {
1370 /* XXX memory leak? */
1371 iter->file = iter->file->next;
1376 profile_iterator_free(iter_p);
1380 iter->file_serial = iter->file->upd_serial;
1382 * Find the section to list if we are a LIST_SECTION,
1383 * or find the containing section if not.
1385 section = iter->file->root;
1386 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1387 for (p=section->first_child; p; p = p->next) {
1388 if (!strcmp(p->name, *cpp) && !p->value)
1397 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1400 iter->file = iter->file->next;
1405 iter->node = section->first_child;
1408 * OK, now we know iter->node is set up correctly. Let's do
1411 for (p = iter->node; p; p = p->next) {
1412 if (iter->name && strcmp(p->name, iter->name))
1414 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1417 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1430 iter->file = iter->file->next;
1435 if ((iter->node = p->next) == NULL)
1436 iter->file = iter->file->next;
1440 *ret_name = p->name;
1442 *ret_value = p->value;
1448 * prof_get.c --- routines that expose the public interfaces for
1449 * querying items from the profile.
1454 * This function only gets the first value from the file; it is a
1455 * helper function for profile_get_string, profile_get_integer, etc.
1457 errcode_t profile_get_value(profile_t profile, const char *name,
1458 const char *subname, const char *subsubname,
1459 const char **ret_value)
1464 const char *names[4];
1468 names[2] = subsubname;
1471 if ((retval = profile_iterator_create(profile, names,
1472 PROFILE_ITER_RELATIONS_ONLY,
1476 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1482 retval = PROF_NO_RELATION;
1485 profile_iterator_free(&state);
1490 profile_get_string(profile_t profile, const char *name, const char *subname,
1491 const char *subsubname, const char *def_val,
1498 retval = profile_get_value(profile, name, subname,
1499 subsubname, &value);
1500 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1508 *ret_string = malloc(strlen(value)+1);
1509 if (*ret_string == 0)
1511 strcpy(*ret_string, value);
1518 profile_get_integer(profile_t profile, const char *name, const char *subname,
1519 const char *subsubname, int def_val, int *ret_int)
1530 retval = profile_get_value(profile, name, subname, subsubname, &value);
1531 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1538 /* Empty string is no good. */
1539 return PROF_BAD_INTEGER;
1541 ret_long = strtol (value, &end_value, 10);
1543 /* Overflow or underflow. */
1544 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1545 return PROF_BAD_INTEGER;
1546 /* Value outside "int" range. */
1547 if ((long) (int) ret_long != ret_long)
1548 return PROF_BAD_INTEGER;
1549 /* Garbage in string. */
1550 if (end_value != value + strlen (value))
1551 return PROF_BAD_INTEGER;
1554 *ret_int = ret_long;
1559 profile_get_uint(profile_t profile, const char *name, const char *subname,
1560 const char *subsubname, unsigned int def_val,
1561 unsigned int *ret_int)
1566 unsigned long ret_long;
1572 retval = profile_get_value(profile, name, subname, subsubname, &value);
1573 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1580 /* Empty string is no good. */
1581 return PROF_BAD_INTEGER;
1583 ret_long = strtoul (value, &end_value, 10);
1585 /* Overflow or underflow. */
1586 if ((ret_long == ULONG_MAX) && errno != 0)
1587 return PROF_BAD_INTEGER;
1588 /* Value outside "int" range. */
1589 if ((unsigned long) (unsigned int) ret_long != ret_long)
1590 return PROF_BAD_INTEGER;
1591 /* Garbage in string. */
1592 if (end_value != value + strlen (value))
1593 return PROF_BAD_INTEGER;
1595 *ret_int = ret_long;
1599 static const char *const conf_yes[] = {
1600 "y", "yes", "true", "t", "1", "on",
1604 static const char *const conf_no[] = {
1605 "n", "no", "false", "nil", "0", "off",
1610 profile_parse_boolean(const char *s, int *ret_boolean)
1612 const char *const *p;
1614 if (ret_boolean == NULL)
1617 for(p=conf_yes; *p; p++) {
1618 if (!strcasecmp(*p,s)) {
1624 for(p=conf_no; *p; p++) {
1625 if (!strcasecmp(*p,s)) {
1631 return PROF_BAD_BOOLEAN;
1635 profile_get_boolean(profile_t profile, const char *name, const char *subname,
1636 const char *subsubname, int def_val, int *ret_boolean)
1642 *ret_boolean = def_val;
1646 retval = profile_get_value(profile, name, subname, subsubname, &value);
1647 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1648 *ret_boolean = def_val;
1653 return profile_parse_boolean (value, ret_boolean);
1657 profile_iterator(void **iter_p, char **ret_name, char **ret_value)
1662 retval = profile_node_iterator(iter_p, 0, &name, &value);
1668 *ret_name = malloc(strlen(name)+1);
1671 strcpy(*ret_name, name);
1677 *ret_value = malloc(strlen(value)+1);
1685 strcpy(*ret_value, value);
1692 #ifdef DEBUG_PROGRAM
1695 * test_profile.c --- testing program for the profile routine
1698 #include "argv_parse.h"
1699 #include "profile_helpers.h"
1701 const char *program_name = "test_profile";
1703 #define PRINT_VALUE 1
1704 #define PRINT_VALUES 2
1706 static void do_cmd(profile_t profile, char **argv)
1709 const char **names, *value;
1710 char **values, **cpp;
1715 names = (const char **) argv + 1;
1720 if (!strcmp(cmd, "query")) {
1721 retval = profile_get_values(profile, names, &values);
1722 print_status = PRINT_VALUES;
1723 } else if (!strcmp(cmd, "query1")) {
1724 const char *name = 0;
1725 const char *subname = 0;
1726 const char *subsubname = 0;
1732 subsubname = names[2];
1733 if (subsubname && names[3]) {
1735 "Only 3 levels are allowed with query1\n");
1738 retval = profile_get_value(profile, name, subname,
1739 subsubname, &value);
1740 print_status = PRINT_VALUE;
1741 } else if (!strcmp(cmd, "list_sections")) {
1742 retval = profile_get_subsection_names(profile, names,
1744 print_status = PRINT_VALUES;
1745 } else if (!strcmp(cmd, "list_relations")) {
1746 retval = profile_get_relation_names(profile, names,
1748 print_status = PRINT_VALUES;
1749 } else if (!strcmp(cmd, "dump")) {
1750 retval = profile_write_tree_file
1751 (profile->first_file->root, stdout);
1753 } else if (!strcmp(cmd, "clear")) {
1754 retval = profile_clear_relation(profile, names);
1755 } else if (!strcmp(cmd, "update")) {
1756 retval = profile_update_relation(profile, names+2,
1757 *names, *(names+1));
1759 } else if (!strcmp(cmd, "verify")) {
1760 retval = profile_verify_node
1761 (profile->first_file->root);
1763 } else if (!strcmp(cmd, "rename_section")) {
1764 retval = profile_rename_section(profile, names+1, *names);
1765 } else if (!strcmp(cmd, "add")) {
1767 if (strcmp(value, "NULL") == 0)
1769 retval = profile_add_relation(profile, names+1, value);
1770 } else if (!strcmp(cmd, "flush")) {
1771 retval = profile_flush(profile);
1774 printf("Invalid command.\n");
1777 com_err(cmd, retval, "");
1780 switch (print_status) {
1782 printf("%s\n", value);
1785 for (cpp = values; *cpp; cpp++)
1786 printf("%s\n", *cpp);
1787 profile_free_list(values);
1792 static void do_batchmode(profile_t profile)
1798 while (!feof(stdin)) {
1799 if (fgets(buf, sizeof(buf), stdin) == NULL)
1802 ret = argv_parse(buf, &argc, &argv);
1804 printf("Argv_parse returned %d!\n", ret);
1807 do_cmd(profile, argv);
1811 profile_release(profile);
1816 void syntax_err_report(const char *filename, long err, int line_num)
1818 fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
1819 filename, line_num, error_message(err));
1823 const char *default_str = "[foo]\n\tbar=quux\n\tsub = {\n\t\twin = true\n}\n";
1825 int main(int argc, char **argv)
1832 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
1836 initialize_prof_error_table();
1838 profile_set_syntax_err_cb(syntax_err_report);
1840 retval = profile_init_path(argv[1], &profile);
1842 com_err(program_name, retval, "while initializing profile");
1845 retval = profile_set_default(profile, default_str);
1847 com_err(program_name, retval, "while setting default");
1852 if (!cmd || !strcmp(cmd, "batch"))
1853 do_batchmode(profile);
1855 do_cmd(profile, argv+2);
1856 profile_release(profile);