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
110 * This structure defines the high-level, user visible profile_t
111 * object, which is used as a handle by users who need to query some
112 * configuration file(s)
116 prf_file_t first_file;
120 * Used by the profile iterator in prof_get.c
122 #define PROFILE_ITER_LIST_SECTION 0x0001
123 #define PROFILE_ITER_SECTIONS_ONLY 0x0002
124 #define PROFILE_ITER_RELATIONS_ONLY 0x0004
126 #define PROFILE_ITER_FINAL_SEEN 0x0100
129 * Check if a filespec is last in a list (NULL on UNIX, invalid FSSpec on MacOS
132 #define PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
134 struct profile_node {
139 int final:1; /* Indicate don't search next file */
141 struct profile_node *first_child;
142 struct profile_node *parent;
143 struct profile_node *next, *prev;
146 #define CHECK_MAGIC(node) \
147 if ((node)->magic != PROF_MAGIC_NODE) \
148 return PROF_MAGIC_NODE;
150 static errcode_t profile_parse_file
151 (FILE *f, prf_file_t prf);
154 static errcode_t profile_write_tree_file
155 (struct profile_node *root, FILE *dstfile);
157 static errcode_t profile_write_tree_to_buffer
158 (struct profile_node *root, char **buf);
162 static void profile_free_node
163 (struct profile_node *relation);
165 static errcode_t profile_create_node
166 (const char *name, const char *value,
167 struct profile_node **ret_node);
170 static errcode_t profile_verify_node
171 (struct profile_node *node);
174 static errcode_t profile_add_node
175 (struct profile_node *section,
176 const char *name, const char *value,
177 struct profile_node **ret_node);
179 static errcode_t profile_find_node
180 (struct profile_node *section,
181 const char *name, const char *value,
182 int section_flag, void **state,
183 struct profile_node **node);
185 static errcode_t profile_node_iterator
186 (void **iter_p, struct profile_node **ret_node,
187 char **ret_name, char **ret_value);
189 static errcode_t profile_open_file
190 (const char * file, prf_file_t *ret_prof);
192 static errcode_t profile_update_file
195 static void profile_free_file
196 (prf_file_t profile);
198 static errcode_t profile_get_value(profile_t profile, const char *name,
199 const char *subname, const char *subsubname,
200 const char **ret_value);
204 * prof_init.c --- routines that manipulate the user-visible profile_t
208 static int compstr(const void *m1, const void *m2)
210 const char *s1 = *((const char **) m1);
211 const char *s2 = *((const char **) m2);
213 return strcmp(s1, s2);
216 static void free_list(char **list)
223 for (cp = list; *cp; cp++)
228 static errcode_t get_dirlist(const char *dirname, char***ret_array)
235 char **array = 0, **new_array;
236 int max = 0, num = 0;
238 dir = opendir(dirname);
242 while ((de = readdir(dir)) != NULL) {
243 for (cp = de->d_name; *cp; cp++) {
251 fn = malloc(strlen(dirname) + strlen(de->d_name) + 2);
256 sprintf(fn, "%s/%s", dirname, de->d_name);
257 if ((stat(fn, &st) < 0) || !S_ISREG(st.st_mode)) {
263 new_array = realloc(array, sizeof(char *) * (max+1));
272 qsort(array, num, sizeof(char *), compstr);
284 profile_init(const char **files, profile_t *ret_profile)
288 prf_file_t new_file, *last;
289 errcode_t retval = 0;
290 char **cpp, *cp, **array = 0;
292 profile = malloc(sizeof(struct _profile_t));
295 memset(profile, 0, sizeof(struct _profile_t));
296 profile->magic = PROF_MAGIC_PROFILE;
297 last = &profile->first_file;
299 /* if the filenames list is not specified return an empty profile */
301 for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) {
302 retval = get_dirlist(*fs, &array);
304 for (cpp = array; (cp = *cpp); cpp++) {
305 retval = profile_open_file(cp, &new_file);
306 if (retval == EACCES)
311 last = &new_file->next;
313 } else if (retval != ENOTDIR)
316 retval = profile_open_file(*fs, &new_file);
317 /* if this file is missing, skip to the next */
318 if (retval == ENOENT || retval == EACCES) {
324 last = &new_file->next;
327 * If all the files were not found, return the appropriate error.
329 if (!profile->first_file) {
330 profile_release(profile);
336 *ret_profile = profile;
340 profile_release(profile);
345 profile_release(profile_t profile)
349 if (!profile || profile->magic != PROF_MAGIC_PROFILE)
352 for (p = profile->first_file; p; p = next) {
354 profile_free_file(p);
362 * prof_file.c ---- routines that manipulate an individual profile file.
365 errcode_t profile_open_file(const char * filespec,
366 prf_file_t *ret_prof)
372 char *expanded_filename;
374 prf = malloc(sizeof(struct _prf_file_t));
377 memset(prf, 0, sizeof(struct _prf_file_t));
378 prf->magic = PROF_MAGIC_FILE;
380 len = strlen(filespec)+1;
381 if (filespec[0] == '~' && filespec[1] == '/') {
382 home_env = getenv("HOME");
384 if (home_env == NULL) {
385 #ifdef HAVE_GETWUID_R
386 struct passwd *pw, pwx;
391 if (!getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
392 && pw != NULL && pw->pw_dir[0] != 0)
393 home_env = pw->pw_dir;
397 pw = getpwuid(getuid());
398 home_env = pw->pw_dir;
403 len += strlen(home_env);
405 expanded_filename = malloc(len);
406 if (expanded_filename == 0)
409 strcpy(expanded_filename, home_env);
410 strcat(expanded_filename, filespec+1);
412 memcpy(expanded_filename, filespec, len);
414 prf->filespec = expanded_filename;
416 retval = profile_update_file(prf);
418 profile_free_file(prf);
426 errcode_t profile_update_file(prf_file_t prf)
431 #ifdef STAT_ONCE_PER_SECOND
438 #ifdef STAT_ONCE_PER_SECOND
440 if (now == prf->last_stat && prf->root != NULL) {
444 if (stat(prf->filespec, &st)) {
448 #ifdef STAT_ONCE_PER_SECOND
449 prf->last_stat = now;
451 if (st.st_mtime == prf->timestamp && prf->root != NULL) {
455 profile_free_node(prf->root);
460 * If we don't have the stat() call, assume that our in-core
461 * memory image is correct. That is, we won't reread the
462 * profile file if it changes.
469 f = fopen(prf->filespec, "r");
477 retval = profile_parse_file(f, prf);
483 prf->timestamp = st.st_mtime;
488 void profile_free_file(prf_file_t prf)
491 profile_free_node(prf->root);
497 /* Begin the profile parser */
499 static profile_syntax_err_cb_t syntax_err_cb;
501 profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
503 profile_syntax_err_cb_t old;
506 syntax_err_cb = hook;
510 #define STATE_INIT_COMMENT 0
511 #define STATE_STD_LINE 1
512 #define STATE_GET_OBRACE 2
518 struct profile_node *root_section;
519 struct profile_node *current_section;
522 static char *skip_over_blanks(char *cp)
524 while (*cp && isspace((int) (*cp)))
529 static int end_or_comment(char ch)
531 return (ch == 0 || ch == '#' || ch == ';');
534 static char *skip_over_nonblanks(char *cp)
536 while (!end_or_comment(*cp) && !isspace(*cp))
541 static void strip_line(char *line)
543 char *p = line + strlen(line);
544 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
548 static void parse_quoted_string(char *str)
554 for (to = from = str; *from && *from != '"'; to++, from++) {
578 static errcode_t parse_std_line(char *line, struct parse_state *state)
580 char *cp, ch, *tag, *value;
583 struct profile_node *node;
584 int do_subsection = 0;
590 cp = skip_over_blanks(line);
592 if (end_or_comment(ch))
595 if (state->group_level > 0)
596 return PROF_SECTION_NOTOP;
598 cp = skip_over_blanks(cp);
601 return PROF_SECTION_SYNTAX;
604 parse_quoted_string(cp);
607 while (isspace(*p) && (p > cp))
610 return PROF_SECTION_SYNTAX;
612 retval = profile_find_node(state->root_section, cp, 0, 1,
613 &iter, &state->current_section);
614 if (retval == PROF_NO_SECTION) {
615 retval = profile_add_node(state->root_section,
617 &state->current_section);
624 * Finish off the rest of the line.
628 state->current_section->final = 1;
632 * Spaces or comments after ']' should not be fatal
634 cp = skip_over_blanks(cp);
635 if (!end_or_comment(*cp))
636 return PROF_SECTION_SYNTAX;
640 if (state->group_level == 0)
641 return PROF_EXTRA_CBRACE;
643 state->current_section->final = 1;
644 state->current_section = state->current_section->parent;
645 state->group_level--;
649 * Parse the relations
652 cp = strchr(cp, '=');
654 return PROF_RELATION_SYNTAX;
656 return PROF_RELATION_SYNTAX;
660 parse_quoted_string(tag);
662 /* Look for whitespace on left-hand side. */
663 p = skip_over_nonblanks(tag);
666 p = skip_over_blanks(p);
667 /* If we have more non-whitespace, it's an error. */
669 return PROF_RELATION_SYNTAX;
672 cp = skip_over_blanks(cp+1);
677 parse_quoted_string(value);
678 } else if (end_or_comment(ch)) {
680 state->state = STATE_GET_OBRACE;
681 } else if (value[0] == '{') {
682 cp = skip_over_blanks(value+1);
684 if (end_or_comment(ch))
687 return PROF_RELATION_SYNTAX;
689 cp = skip_over_nonblanks(value);
690 p = skip_over_blanks(cp);
693 if (!end_or_comment(ch))
694 return PROF_RELATION_SYNTAX;
697 p = strchr(tag, '*');
700 retval = profile_add_node(state->current_section,
701 tag, 0, &state->current_section);
705 state->current_section->final = 1;
706 state->group_level++;
709 p = strchr(tag, '*');
712 profile_add_node(state->current_section, tag, value, &node);
718 static errcode_t parse_line(char *line, struct parse_state *state)
723 switch (state->state) {
724 case STATE_INIT_COMMENT:
727 state->state = STATE_STD_LINE;
729 return parse_std_line(line, state);
730 case STATE_GET_OBRACE:
731 cp = skip_over_blanks(line);
733 return PROF_MISSING_OBRACE;
734 state->state = STATE_STD_LINE;
739 errcode_t profile_parse_file(FILE *f, prf_file_t file)
743 struct parse_state state;
745 memset(&state, 0, sizeof(struct parse_state));
747 retval = profile_create_node("(root)", 0, &state.root_section);
752 if (fgets(buf, sizeof(buf), f) == NULL)
754 retval = parse_line(buf, &state);
757 (syntax_err_cb)(file->filespec, retval,
762 file->root = state.root_section;
770 * Return TRUE if the string begins or ends with whitespace
772 static int need_double_quotes(char *str)
776 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
778 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
779 strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
785 * Output a string with double quotes, doing appropriate backquoting
786 * of characters as necessary.
788 static void output_quoted_string(char *str, void (*cb)(const char *,void *),
800 while ((ch = *str++)) {
815 /* This would be a lot faster if we scanned
816 forward for the next "interesting"
830 /* Errors should be returned, not ignored! */
831 static void dump_profile(struct profile_node *root, int level,
832 void (*cb)(const char *, void *), void *data)
835 struct profile_node *p;
841 retval = profile_find_node(root, 0, 0, 0, &iter, &p);
844 for (i=0; i < level; i++)
846 if (need_double_quotes(p->name))
847 output_quoted_string(p->name, cb, data);
851 if (need_double_quotes(p->value))
852 output_quoted_string(p->value, cb, data);
860 retval = profile_find_node(root, 0, 0, 1, &iter, &p);
863 if (level == 0) { /* [xxx] */
865 if (need_double_quotes(p->name))
866 output_quoted_string(p->name, cb, data);
870 cb(p->final ? "*" : "", data);
872 dump_profile(p, level+1, cb, data);
874 } else { /* xxx = { ... } */
875 for (i=0; i < level; i++)
877 if (need_double_quotes(p->name))
878 output_quoted_string(p->name, cb, data);
883 dump_profile(p, level+1, cb, data);
884 for (i=0; i < level; i++)
887 cb(p->final ? "*" : "", data);
893 static void dump_profile_to_file_cb(const char *str, void *data)
898 errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
900 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
910 static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
914 if (b->max - b->cur < len) {
918 newsize = b->max + (b->max >> 1) + len + 1024;
919 newptr = realloc(b->base, newsize);
920 if (newptr == NULL) {
927 memcpy(b->base + b->cur, d, len);
928 b->cur += len; /* ignore overflow */
931 static void dump_profile_to_buffer_cb(const char *str, void *data)
933 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
936 errcode_t profile_write_tree_to_buffer(struct profile_node *root,
939 struct prof_buf prof_buf = { 0, 0, 0, 0 };
941 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
946 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
947 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
948 char *newptr = realloc(prof_buf.base, prof_buf.cur);
950 prof_buf.base = newptr;
952 *buf = prof_buf.base;
958 * prof_tree.c --- these routines maintain the parse tree of the
961 * All of the details of how the tree is stored is abstracted away in
962 * this file; all of the other profile routines build, access, and
963 * modify the tree via the accessor functions found in this file.
965 * Each node may represent either a relation or a section header.
967 * A section header must have its value field set to 0, and may a one
968 * or more child nodes, pointed to by first_child.
970 * A relation has as its value a pointer to allocated memory
971 * containing a string. Its first_child pointer must be null.
976 * Free a node, and any children
978 void profile_free_node(struct profile_node *node)
980 struct profile_node *child, *next;
982 if (node->magic != PROF_MAGIC_NODE)
990 for (child=node->first_child; child; child = next) {
992 profile_free_node(child);
1001 #define strdup MYstrdup
1002 static char *MYstrdup (const char *s)
1004 size_t sz = strlen(s) + 1;
1005 char *p = malloc(sz);
1015 errcode_t profile_create_node(const char *name, const char *value,
1016 struct profile_node **ret_node)
1018 struct profile_node *new;
1020 new = malloc(sizeof(struct profile_node));
1023 memset(new, 0, sizeof(struct profile_node));
1024 new->name = strdup(name);
1025 if (new->name == 0) {
1026 profile_free_node(new);
1030 new->value = strdup(value);
1031 if (new->value == 0) {
1032 profile_free_node(new);
1036 new->magic = PROF_MAGIC_NODE;
1043 * This function verifies that all of the representation invarients of
1044 * the profile are true. If not, we have a programming bug somewhere,
1045 * probably in this file.
1047 #ifdef DEBUG_PROGRAM
1048 errcode_t profile_verify_node(struct profile_node *node)
1050 struct profile_node *p, *last;
1055 if (node->value && node->first_child)
1056 return PROF_SECTION_WITH_VALUE;
1059 for (p = node->first_child; p; last = p, p = p->next) {
1060 if (p->prev != last)
1061 return PROF_BAD_LINK_LIST;
1062 if (last && (last->next != p))
1063 return PROF_BAD_LINK_LIST;
1064 if (node->group_level+1 != p->group_level)
1065 return PROF_BAD_GROUP_LVL;
1066 if (p->parent != node)
1067 return PROF_BAD_PARENT_PTR;
1068 retval = profile_verify_node(p);
1077 * Add a node to a particular section
1079 errcode_t profile_add_node(struct profile_node *section, const char *name,
1080 const char *value, struct profile_node **ret_node)
1083 struct profile_node *p, *last, *new;
1085 CHECK_MAGIC(section);
1088 return PROF_ADD_NOT_SECTION;
1091 * Find the place to insert the new node. We look for the
1092 * place *after* the last match of the node name, since
1095 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1097 cmp = strcmp(p->name, name);
1101 retval = profile_create_node(name, value, &new);
1104 new->group_level = section->group_level+1;
1106 new->parent = section;
1114 section->first_child = new;
1121 * Iterate through the section, returning the nodes which match
1122 * the given name. If name is NULL, then interate through all the
1123 * nodes in the section. If section_flag is non-zero, only return the
1124 * section which matches the name; don't return relations. If value
1125 * is non-NULL, then only return relations which match the requested
1126 * value. (The value argument is ignored if section_flag is non-zero.)
1128 * The first time this routine is called, the state pointer must be
1129 * null. When this profile_find_node_relation() returns, if the state
1130 * pointer is non-NULL, then this routine should be called again.
1131 * (This won't happen if section_flag is non-zero, obviously.)
1134 errcode_t profile_find_node(struct profile_node *section, const char *name,
1135 const char *value, int section_flag, void **state,
1136 struct profile_node **node)
1138 struct profile_node *p;
1140 CHECK_MAGIC(section);
1145 p = section->first_child;
1147 for (; p; p = p->next) {
1148 if (name && (strcmp(p->name, name)))
1156 if (value && (strcmp(p->value, value)))
1168 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1171 * OK, we've found one match; now let's try to find another
1172 * one. This way, if we return a non-zero state pointer,
1173 * there's guaranteed to be another match that's returned.
1175 for (p = p->next; p; p = p->next) {
1176 if (name && (strcmp(p->name, name)))
1184 if (value && (strcmp(p->value, value)))
1195 * This is a general-purpose iterator for returning all nodes that
1196 * match the specified name array.
1198 struct profile_iterator {
1202 const char *const *names;
1207 struct profile_node *node;
1212 profile_iterator_create(profile_t profile, const char *const *names, int flags,
1215 struct profile_iterator *iter;
1219 return PROF_NO_PROFILE;
1220 if (profile->magic != PROF_MAGIC_PROFILE)
1221 return PROF_MAGIC_PROFILE;
1223 return PROF_BAD_NAMESET;
1224 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1226 return PROF_BAD_NAMESET;
1230 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1233 iter->magic = PROF_MAGIC_ITERATOR;
1234 iter->profile = profile;
1235 iter->names = names;
1236 iter->flags = flags;
1237 iter->file = profile->first_file;
1238 iter->done_idx = done_idx;
1245 void profile_iterator_free(void **iter_p)
1247 struct profile_iterator *iter;
1252 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1259 * Note: the returned character strings in ret_name and ret_value
1260 * points to the stored character string in the parse string. Before
1261 * this string value is returned to a calling application
1262 * (profile_node_iterator is not an exported interface), it should be
1265 errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1266 char **ret_name, char **ret_value)
1268 struct profile_iterator *iter = *iter_p;
1269 struct profile_node *section, *p;
1270 const char *const *cpp;
1274 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1275 return PROF_MAGIC_ITERATOR;
1276 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1277 return PROF_MAGIC_FILE;
1279 * If the file has changed, then the node pointer is invalid,
1280 * so we'll have search the file again looking for it.
1282 if (iter->node && (iter->file->upd_serial != iter->file_serial)) {
1283 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1284 skip_num = iter->num;
1287 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1288 return PROF_MAGIC_NODE;
1291 if (iter->node == 0) {
1292 if (iter->file == 0 ||
1293 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
1294 profile_iterator_free(iter_p);
1303 if ((retval = profile_update_file(iter->file))) {
1304 if (retval == ENOENT || retval == EACCES) {
1305 /* XXX memory leak? */
1306 iter->file = iter->file->next;
1311 profile_iterator_free(iter_p);
1315 iter->file_serial = iter->file->upd_serial;
1317 * Find the section to list if we are a LIST_SECTION,
1318 * or find the containing section if not.
1320 section = iter->file->root;
1321 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1322 for (p=section->first_child; p; p = p->next) {
1323 if (!strcmp(p->name, *cpp) && !p->value)
1332 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1335 iter->file = iter->file->next;
1340 iter->node = section->first_child;
1343 * OK, now we know iter->node is set up correctly. Let's do
1346 for (p = iter->node; p; p = p->next) {
1347 if (iter->name && strcmp(p->name, iter->name))
1349 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1352 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1365 iter->file = iter->file->next;
1372 if ((iter->node = p->next) == NULL)
1373 iter->file = iter->file->next;
1377 *ret_name = p->name;
1379 *ret_value = p->value;
1385 * prof_get.c --- routines that expose the public interfaces for
1386 * querying items from the profile.
1391 * This function only gets the first value from the file; it is a
1392 * helper function for profile_get_string, profile_get_integer, etc.
1394 errcode_t profile_get_value(profile_t profile, const char *name,
1395 const char *subname, const char *subsubname,
1396 const char **ret_value)
1401 const char *names[4];
1405 names[2] = subsubname;
1408 if ((retval = profile_iterator_create(profile, names,
1409 PROFILE_ITER_RELATIONS_ONLY,
1413 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1419 retval = PROF_NO_RELATION;
1422 profile_iterator_free(&state);
1427 profile_get_string(profile_t profile, const char *name, const char *subname,
1428 const char *subsubname, const char *def_val,
1435 retval = profile_get_value(profile, name, subname,
1436 subsubname, &value);
1437 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1445 *ret_string = malloc(strlen(value)+1);
1446 if (*ret_string == 0)
1448 strcpy(*ret_string, value);
1455 profile_get_integer(profile_t profile, const char *name, const char *subname,
1456 const char *subsubname, int def_val, int *ret_int)
1467 retval = profile_get_value(profile, name, subname, subsubname, &value);
1468 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1475 /* Empty string is no good. */
1476 return PROF_BAD_INTEGER;
1478 ret_long = strtol (value, &end_value, 10);
1480 /* Overflow or underflow. */
1481 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1482 return PROF_BAD_INTEGER;
1483 /* Value outside "int" range. */
1484 if ((long) (int) ret_long != ret_long)
1485 return PROF_BAD_INTEGER;
1486 /* Garbage in string. */
1487 if (end_value != value + strlen (value))
1488 return PROF_BAD_INTEGER;
1491 *ret_int = ret_long;
1495 static const char *const conf_yes[] = {
1496 "y", "yes", "true", "t", "1", "on",
1500 static const char *const conf_no[] = {
1501 "n", "no", "false", "nil", "0", "off",
1506 profile_parse_boolean(const char *s, int *ret_boolean)
1508 const char *const *p;
1510 if (ret_boolean == NULL)
1513 for(p=conf_yes; *p; p++) {
1514 if (!strcasecmp(*p,s)) {
1520 for(p=conf_no; *p; p++) {
1521 if (!strcasecmp(*p,s)) {
1527 return PROF_BAD_BOOLEAN;
1531 profile_get_boolean(profile_t profile, const char *name, const char *subname,
1532 const char *subsubname, int def_val, int *ret_boolean)
1538 *ret_boolean = def_val;
1542 retval = profile_get_value(profile, name, subname, subsubname, &value);
1543 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1544 *ret_boolean = def_val;
1549 return profile_parse_boolean (value, ret_boolean);
1553 profile_iterator(void **iter_p, char **ret_name, char **ret_value)
1558 retval = profile_node_iterator(iter_p, 0, &name, &value);
1564 *ret_name = malloc(strlen(name)+1);
1567 strcpy(*ret_name, name);
1573 *ret_value = malloc(strlen(value)+1);
1581 strcpy(*ret_value, value);
1588 #ifdef DEBUG_PROGRAM
1591 * test_profile.c --- testing program for the profile routine
1594 #include "argv_parse.h"
1595 #include "profile_helpers.h"
1597 const char *program_name = "test_profile";
1599 #define PRINT_VALUE 1
1600 #define PRINT_VALUES 2
1602 static void do_cmd(profile_t profile, char **argv)
1605 const char **names, *value;
1606 char **values, **cpp;
1611 names = (const char **) argv + 1;
1616 if (!strcmp(cmd, "query")) {
1617 retval = profile_get_values(profile, names, &values);
1618 print_status = PRINT_VALUES;
1619 } else if (!strcmp(cmd, "query1")) {
1620 const char *name = 0;
1621 const char *subname = 0;
1622 const char *subsubname = 0;
1628 subsubname = names[2];
1629 if (subsubname && names[3]) {
1631 "Only 3 levels are allowed with query1\n");
1634 retval = profile_get_value(profile, name, subname,
1635 subsubname, &value);
1636 print_status = PRINT_VALUE;
1637 } else if (!strcmp(cmd, "list_sections")) {
1638 retval = profile_get_subsection_names(profile, names,
1640 print_status = PRINT_VALUES;
1641 } else if (!strcmp(cmd, "list_relations")) {
1642 retval = profile_get_relation_names(profile, names,
1644 print_status = PRINT_VALUES;
1645 } else if (!strcmp(cmd, "dump")) {
1646 retval = profile_write_tree_file
1647 (profile->first_file->root, stdout);
1649 } else if (!strcmp(cmd, "clear")) {
1650 retval = profile_clear_relation(profile, names);
1651 } else if (!strcmp(cmd, "update")) {
1652 retval = profile_update_relation(profile, names+2,
1653 *names, *(names+1));
1655 } else if (!strcmp(cmd, "verify")) {
1656 retval = profile_verify_node
1657 (profile->first_file->root);
1659 } else if (!strcmp(cmd, "rename_section")) {
1660 retval = profile_rename_section(profile, names+1, *names);
1661 } else if (!strcmp(cmd, "add")) {
1663 if (strcmp(value, "NULL") == 0)
1665 retval = profile_add_relation(profile, names+1, value);
1666 } else if (!strcmp(cmd, "flush")) {
1667 retval = profile_flush(profile);
1670 printf("Invalid command.\n");
1673 com_err(cmd, retval, "");
1676 switch (print_status) {
1678 printf("%s\n", value);
1681 for (cpp = values; *cpp; cpp++)
1682 printf("%s\n", *cpp);
1683 profile_free_list(values);
1688 static void do_batchmode(profile_t profile)
1694 while (!feof(stdin)) {
1695 if (fgets(buf, sizeof(buf), stdin) == NULL)
1698 ret = argv_parse(buf, &argc, &argv);
1700 printf("Argv_parse returned %d!\n", ret);
1703 do_cmd(profile, argv);
1707 profile_release(profile);
1712 void syntax_err_report(const char *filename, long err, int line_num)
1714 fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
1715 filename, line_num, error_message(err));
1719 int main(int argc, char **argv)
1726 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
1730 initialize_prof_error_table();
1732 profile_set_syntax_err_cb(syntax_err_report);
1734 retval = profile_init_path(argv[1], &profile);
1736 com_err(program_name, retval, "while initializing profile");
1740 if (!cmd || !strcmp(cmd, "batch"))
1741 do_batchmode(profile);
1743 do_cmd(profile, argv+2);
1744 profile_release(profile);