blob: 90cc997807f17d33f04168a3958e2709df93bc84 [file] [log] [blame]
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001/*
2 * profile.c -- A simple configuration file parsing "library in a file"
3 *
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.)
14 *
15 * Copyright (C) 2005 by Theodore Ts'o.
16 *
17 * %Begin-Header%
18 * This file may be redistributed under the terms of the GNU Public
19 * License.
20 * %End-Header%
21 *
22 * Copyright (C) 1985-2005 by the Massachusetts Institute of Technology.
23 *
24 * All rights reserved.
25 *
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.
30 *
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
43 * warranty.
44 *
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.
48 *
49 */
50
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
54#include <stdio.h>
55#ifdef HAVE_STDLIB_H
56#include <stdlib.h>
57#endif
58#include <time.h>
59#include <string.h>
60#include <errno.h>
61#include <ctype.h>
62#include <limits.h>
63#include <stddef.h>
64#include <sys/types.h>
65#include <sys/stat.h>
66#ifdef HAVE_PWD_H
67#include <pwd.h>
68#endif
69
Theodore Ts'ocec71032006-01-01 21:31:45 -050070#include <et/com_err.h>
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050071#include "profile.h"
Theodore Ts'ofd7ac1f2005-12-31 01:28:33 -050072#include "prof_err.h"
73
Theodore Ts'o55822752005-12-31 16:24:07 -050074#undef STAT_ONCE_PER_SECOND
75#undef HAVE_STAT
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050076
77/* Begin prof_int.h */
78
79/*
80 * prof_int.h
81 */
82
83typedef long prf_magic_t;
84
85/*
86 * This is the structure which stores the profile information for a
87 * particular configuration file.
88 */
89struct _prf_data_t {
90 prf_magic_t magic;
91 struct profile_node *root;
92#ifdef STAT_ONCE_PER_SECOND
93 time_t last_stat;
94#endif
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 char *comment;
99
100 size_t fslen;
101
102 struct _prf_data_t *next;
103 /* Was: "profile_filespec_t filespec". Now: flexible char
104 array ... except, we need to work in C89, so an array
105 length must be specified. */
106 const char filespec[sizeof("/etc/krb5.conf")];
107};
108
109typedef struct _prf_data_t *prf_data_t;
110prf_data_t profile_make_prf_data(const char *);
111
112struct _prf_file_t {
113 prf_magic_t magic;
114 struct _prf_data_t *data;
115 struct _prf_file_t *next;
116};
117
118typedef struct _prf_file_t *prf_file_t;
119
120/*
121 * The profile flags
122 */
123#define PROFILE_FILE_RW 0x0001
124#define PROFILE_FILE_DIRTY 0x0002
125
126/*
127 * This structure defines the high-level, user visible profile_t
128 * object, which is used as a handle by users who need to query some
129 * configuration file(s)
130 */
131struct _profile_t {
132 prf_magic_t magic;
133 prf_file_t first_file;
134};
135
136/*
137 * Used by the profile iterator in prof_get.c
138 */
139#define PROFILE_ITER_LIST_SECTION 0x0001
140#define PROFILE_ITER_SECTIONS_ONLY 0x0002
141#define PROFILE_ITER_RELATIONS_ONLY 0x0004
142
143#define PROFILE_ITER_FINAL_SEEN 0x0100
144
145/*
146 * Check if a filespec is last in a list (NULL on UNIX, invalid FSSpec on MacOS
147 */
148
149#define PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
150
151/* profile_parse.c */
152
153static errcode_t profile_parse_file
154 (FILE *f, struct profile_node **root);
155
156#ifdef DEBUG_PROGRAM
157static errcode_t profile_write_tree_file
158 (struct profile_node *root, FILE *dstfile);
159
160static errcode_t profile_write_tree_to_buffer
161 (struct profile_node *root, char **buf);
162#endif
163
164
165/* prof_tree.c */
166
167static void profile_free_node
168 (struct profile_node *relation);
169
170static errcode_t profile_create_node
171 (const char *name, const char *value,
172 struct profile_node **ret_node);
173
174#ifdef DEBUG_PROGRAM
175static errcode_t profile_verify_node
176 (struct profile_node *node);
177#endif
178
179static errcode_t profile_add_node
180 (struct profile_node *section,
181 const char *name, const char *value,
182 struct profile_node **ret_node);
183
184static errcode_t profile_make_node_final
185 (struct profile_node *node);
186
187static int profile_is_node_final
188 (struct profile_node *node);
189
190#ifdef DEBUG_PROGRAM
191static const char *profile_get_node_name
192 (struct profile_node *node);
193
194static const char *profile_get_node_value
195 (struct profile_node *node);
196#endif
197
198static errcode_t profile_find_node
199 (struct profile_node *section,
200 const char *name, const char *value,
201 int section_flag, void **state,
202 struct profile_node **node);
203
204static errcode_t profile_find_node_relation
205 (struct profile_node *section,
206 const char *name, void **state,
207 char **ret_name, char **value);
208
209static errcode_t profile_find_node_subsection
210 (struct profile_node *section,
211 const char *name, void **state,
212 char **ret_name, struct profile_node **subsection);
213
214static errcode_t profile_get_node_parent
215 (struct profile_node *section,
216 struct profile_node **parent);
217
218static errcode_t profile_node_iterator_create
219 (profile_t profile, const char *const *names,
220 int flags, void **ret_iter);
221
222static void profile_node_iterator_free
223 (void **iter_p);
224
225static errcode_t profile_node_iterator
226 (void **iter_p, struct profile_node **ret_node,
227 char **ret_name, char **ret_value);
228
229/* prof_file.c */
230
231static errcode_t profile_open_file
232 (const char * file, prf_file_t *ret_prof);
233
234#define profile_update_file(P) profile_update_file_data((P)->data)
235static errcode_t profile_update_file_data
236 (prf_data_t profile);
237
238static void profile_free_file
239 (prf_file_t profile);
240
241/* prof_init.c -- included from profile.h */
242
243/* prof_get.c */
244
245static errcode_t profile_get_value
246 (profile_t profile, const char **names,
247 const char **ret_value);
248/* Others included from profile.h */
249
250/* prof_set.c -- included from profile.h */
251
252/* End prof_int.h */
253
254/* Begin prof_init.c */
255/*
256 * prof_init.c --- routines that manipulate the user-visible profile_t
257 * object.
258 */
259
260errcode_t
261profile_init(const char **files, profile_t *ret_profile)
262{
263 const char **fs;
264 profile_t profile;
265 prf_file_t new_file, last = 0;
266 errcode_t retval = 0;
267
268 profile = malloc(sizeof(struct _profile_t));
269 if (!profile)
270 return ENOMEM;
271 memset(profile, 0, sizeof(struct _profile_t));
272 profile->magic = PROF_MAGIC_PROFILE;
273
274 /* if the filenames list is not specified return an empty profile */
275 if ( files ) {
276 for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) {
277 retval = profile_open_file(*fs, &new_file);
278 /* if this file is missing, skip to the next */
279 if (retval == ENOENT || retval == EACCES) {
280 continue;
281 }
282 if (retval) {
283 profile_release(profile);
284 return retval;
285 }
286 if (last)
287 last->next = new_file;
288 else
289 profile->first_file = new_file;
290 last = new_file;
291 }
292 /*
293 * If last is still null after the loop, then all the files were
294 * missing, so return the appropriate error.
295 */
296 if (!last) {
297 profile_release(profile);
298 return ENOENT;
299 }
300 }
301
302 *ret_profile = profile;
303 return 0;
304}
305
Theodore Ts'o55822752005-12-31 16:24:07 -0500306#ifdef DEBUG_PROGRAM
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500307errcode_t
308profile_init_path(const char * filepath,
309 profile_t *ret_profile)
310{
311 int n_entries, i;
312 unsigned int ent_len;
313 const char *s, *t;
314 char **filenames;
315 errcode_t retval;
316
317 /* count the distinct filename components */
318 for(s = filepath, n_entries = 1; *s; s++) {
319 if (*s == ':')
320 n_entries++;
321 }
322
323 /* the array is NULL terminated */
324 filenames = (char **) malloc((n_entries+1) * sizeof(char*));
325 if (filenames == 0)
326 return ENOMEM;
327
328 /* measure, copy, and skip each one */
329 for(s = filepath, i=0; (t = strchr(s, ':')) || (t=s+strlen(s)); s=t+1, i++) {
330 ent_len = t-s;
331 filenames[i] = (char*) malloc(ent_len + 1);
332 if (filenames[i] == 0) {
333 /* if malloc fails, free the ones that worked */
334 while(--i >= 0) free(filenames[i]);
335 free(filenames);
336 return ENOMEM;
337 }
338 strncpy(filenames[i], s, ent_len);
339 filenames[i][ent_len] = 0;
340 if (*t == 0) {
341 i++;
342 break;
343 }
344 }
345 /* cap the array */
346 filenames[i] = 0;
347
348 retval = profile_init((const char **) filenames,
349 ret_profile);
350
351 /* count back down and free the entries */
352 while(--i >= 0) free(filenames[i]);
353 free(filenames);
354
355 return retval;
356}
Theodore Ts'o55822752005-12-31 16:24:07 -0500357#endif
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500358
359void
360profile_release(profile_t profile)
361{
362 prf_file_t p, next;
363
364 if (!profile || profile->magic != PROF_MAGIC_PROFILE)
365 return;
366
367 for (p = profile->first_file; p; p = next) {
368 next = p->next;
369 profile_free_file(p);
370 }
371 profile->magic = 0;
372 free(profile);
373}
374
375
376/* End prof_init.c */
377
378/* Begin prof_file.c */
379/*
380 * prof_file.c ---- routines that manipulate an individual profile file.
381 */
382
383prf_data_t
384profile_make_prf_data(const char *filename)
385{
386 prf_data_t d;
387 size_t len, flen, slen;
388 char *fcopy;
389
390 flen = strlen(filename);
391 slen = offsetof(struct _prf_data_t, filespec);
392 len = slen + flen + 1;
393 if (len < sizeof(struct _prf_data_t))
394 len = sizeof(struct _prf_data_t);
395 d = malloc(len);
396 if (d == NULL)
397 return NULL;
398 memset(d, 0, len);
399 fcopy = (char *) d + slen;
400 strcpy(fcopy, filename);
401 d->comment = NULL;
402 d->magic = PROF_MAGIC_FILE_DATA;
403 d->root = NULL;
404 d->next = NULL;
405 d->fslen = flen;
406 return d;
407}
408
409errcode_t profile_open_file(const char * filespec,
410 prf_file_t *ret_prof)
411{
412 prf_file_t prf;
413 errcode_t retval;
414 char *home_env = 0;
415 unsigned int len;
416 prf_data_t data;
417 char *expanded_filename;
418
419 prf = malloc(sizeof(struct _prf_file_t));
420 if (!prf)
421 return ENOMEM;
422 memset(prf, 0, sizeof(struct _prf_file_t));
423 prf->magic = PROF_MAGIC_FILE;
424
425 len = strlen(filespec)+1;
426 if (filespec[0] == '~' && filespec[1] == '/') {
427 home_env = getenv("HOME");
428#ifdef HAVE_PWD_H
429 if (home_env == NULL) {
430 uid_t uid;
431 struct passwd *pw, pwx;
432 char pwbuf[BUFSIZ];
433
434 uid = getuid();
435 if (!k5_getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
436 && pw != NULL && pw->pw_dir[0] != 0)
437 home_env = pw->pw_dir;
438 }
439#endif
440 if (home_env)
441 len += strlen(home_env);
442 }
443 expanded_filename = malloc(len);
444 if (expanded_filename == 0)
445 return errno;
446 if (home_env) {
447 strcpy(expanded_filename, home_env);
448 strcat(expanded_filename, filespec+1);
449 } else
450 memcpy(expanded_filename, filespec, len);
451
452 data = profile_make_prf_data(expanded_filename);
453 if (data == NULL) {
454 free(prf);
455 free(expanded_filename);
456 return ENOMEM;
457 }
458 free(expanded_filename);
459 prf->data = data;
460
461 retval = profile_update_file(prf);
462 if (retval) {
463 profile_free_file(prf);
464 return retval;
465 }
466
467 *ret_prof = prf;
468 return 0;
469}
470
471errcode_t profile_update_file_data(prf_data_t data)
472{
473 errcode_t retval;
474#ifdef HAVE_STAT
475 struct stat st;
476#ifdef STAT_ONCE_PER_SECOND
477 time_t now;
478#endif
479#endif
480 FILE *f;
481
482#ifdef HAVE_STAT
483#ifdef STAT_ONCE_PER_SECOND
484 now = time(0);
485 if (now == data->last_stat && data->root != NULL) {
486 return 0;
487 }
488#endif
489 if (stat(data->filespec, &st)) {
490 retval = errno;
491 return retval;
492 }
493#ifdef STAT_ONCE_PER_SECOND
494 data->last_stat = now;
495#endif
496 if (st.st_mtime == data->timestamp && data->root != NULL) {
497 return 0;
498 }
499 if (data->root) {
500 profile_free_node(data->root);
501 data->root = 0;
502 }
503 if (data->comment) {
504 free(data->comment);
505 data->comment = 0;
506 }
507#else
508 /*
509 * If we don't have the stat() call, assume that our in-core
510 * memory image is correct. That is, we won't reread the
511 * profile file if it changes.
512 */
513 if (data->root) {
514 return 0;
515 }
516#endif
517 errno = 0;
518 f = fopen(data->filespec, "r");
519 if (f == NULL) {
520 retval = errno;
521 if (retval == 0)
522 retval = ENOENT;
523 return retval;
524 }
525 data->upd_serial++;
526 retval = profile_parse_file(f, &data->root);
527 fclose(f);
528 if (retval) {
529 return retval;
530 }
531#ifdef HAVE_STAT
532 data->timestamp = st.st_mtime;
533#endif
534 return 0;
535}
536
537void profile_free_file(prf_file_t prf)
538{
539 prf_data_t data = prf->data;
540
541 if (data->root)
542 profile_free_node(data->root);
543 if (data->comment)
544 free(data->comment);
545 data->magic = 0;
546 free(data);
547 free(prf);
548}
549
550/* End prof_file.c */
551
552/* Begin prof_parse.c */
553
554#define SECTION_SEP_CHAR '/'
555
556#define STATE_INIT_COMMENT 1
557#define STATE_STD_LINE 2
558#define STATE_GET_OBRACE 3
559
560struct parse_state {
561 int state;
562 int group_level;
563 struct profile_node *root_section;
564 struct profile_node *current_section;
565};
566
567static char *skip_over_blanks(char *cp)
568{
569 while (*cp && isspace((int) (*cp)))
570 cp++;
571 return cp;
572}
573
574static void strip_line(char *line)
575{
576 char *p = line + strlen(line);
577 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
578 *p-- = 0;
579}
580
581static void parse_quoted_string(char *str)
582{
583 char *to, *from;
584
585 to = from = str;
586
587 for (to = from = str; *from && *from != '"'; to++, from++) {
588 if (*from == '\\') {
589 from++;
590 switch (*from) {
591 case 'n':
592 *to = '\n';
593 break;
594 case 't':
595 *to = '\t';
596 break;
597 case 'b':
598 *to = '\b';
599 break;
600 default:
601 *to = *from;
602 }
603 continue;
604 }
605 *to = *from;
606 }
607 *to = '\0';
608}
609
610
611static errcode_t parse_init_state(struct parse_state *state)
612{
613 state->state = STATE_INIT_COMMENT;
614 state->group_level = 0;
615
616 return profile_create_node("(root)", 0, &state->root_section);
617}
618
619static errcode_t parse_std_line(char *line, struct parse_state *state)
620{
621 char *cp, ch, *tag, *value;
622 char *p;
623 errcode_t retval;
624 struct profile_node *node;
625 int do_subsection = 0;
626 void *iter = 0;
627
628 if (*line == 0)
629 return 0;
630 if (line[0] == ';' || line[0] == '#')
631 return 0;
632 strip_line(line);
633 cp = skip_over_blanks(line);
634 ch = *cp;
635 if (ch == 0)
636 return 0;
637 if (ch == '[') {
638 if (state->group_level > 0)
639 return PROF_SECTION_NOTOP;
640 cp++;
641 p = strchr(cp, ']');
642 if (p == NULL)
643 return PROF_SECTION_SYNTAX;
644 *p = '\0';
645 retval = profile_find_node_subsection(state->root_section,
646 cp, &iter, 0,
647 &state->current_section);
648 if (retval == PROF_NO_SECTION) {
649 retval = profile_add_node(state->root_section,
650 cp, 0,
651 &state->current_section);
652 if (retval)
653 return retval;
654 } else if (retval)
655 return retval;
656
657 /*
658 * Finish off the rest of the line.
659 */
660 cp = p+1;
661 if (*cp == '*') {
662 profile_make_node_final(state->current_section);
663 cp++;
664 }
665 /*
666 * A space after ']' should not be fatal
667 */
668 cp = skip_over_blanks(cp);
669 if (*cp)
670 return PROF_SECTION_SYNTAX;
671 return 0;
672 }
673 if (ch == '}') {
674 if (state->group_level == 0)
675 return PROF_EXTRA_CBRACE;
676 if (*(cp+1) == '*')
677 profile_make_node_final(state->current_section);
678 retval = profile_get_node_parent(state->current_section,
679 &state->current_section);
680 if (retval)
681 return retval;
682 state->group_level--;
683 return 0;
684 }
685 /*
686 * Parse the relations
687 */
688 tag = cp;
689 cp = strchr(cp, '=');
690 if (!cp)
691 return PROF_RELATION_SYNTAX;
692 if (cp == tag)
693 return PROF_RELATION_SYNTAX;
694 *cp = '\0';
695 p = tag;
696 /* Look for whitespace on left-hand side. */
697 while (p < cp && !isspace((int)*p))
698 p++;
699 if (p < cp) {
700 /* Found some sort of whitespace. */
701 *p++ = 0;
702 /* If we have more non-whitespace, it's an error. */
703 while (p < cp) {
704 if (!isspace((int)*p))
705 return PROF_RELATION_SYNTAX;
706 p++;
707 }
708 }
709 cp = skip_over_blanks(cp+1);
710 value = cp;
711 if (value[0] == '"') {
712 value++;
713 parse_quoted_string(value);
714 } else if (value[0] == 0) {
715 do_subsection++;
716 state->state = STATE_GET_OBRACE;
717 } else if (value[0] == '{' && *(skip_over_blanks(value+1)) == 0)
718 do_subsection++;
719 else {
720 cp = value + strlen(value) - 1;
721 while ((cp > value) && isspace((int) (*cp)))
722 *cp-- = 0;
723 }
724 if (do_subsection) {
725 p = strchr(tag, '*');
726 if (p)
727 *p = '\0';
728 retval = profile_add_node(state->current_section,
729 tag, 0, &state->current_section);
730 if (retval)
731 return retval;
732 if (p)
733 profile_make_node_final(state->current_section);
734 state->group_level++;
735 return 0;
736 }
737 p = strchr(tag, '*');
738 if (p)
739 *p = '\0';
740 profile_add_node(state->current_section, tag, value, &node);
741 if (p)
742 profile_make_node_final(node);
743 return 0;
744}
745
746static errcode_t parse_line(char *line, struct parse_state *state)
747{
748 char *cp;
749
750 switch (state->state) {
751 case STATE_INIT_COMMENT:
752 if (line[0] != '[')
753 return 0;
754 state->state = STATE_STD_LINE;
755 case STATE_STD_LINE:
756 return parse_std_line(line, state);
757 case STATE_GET_OBRACE:
758 cp = skip_over_blanks(line);
759 if (*cp != '{')
760 return PROF_MISSING_OBRACE;
761 state->state = STATE_STD_LINE;
762 }
763 return 0;
764}
765
766errcode_t profile_parse_file(FILE *f, struct profile_node **root)
767{
768#define BUF_SIZE 2048
769 char *bptr;
770 errcode_t retval;
771 struct parse_state state;
772
773 bptr = malloc (BUF_SIZE);
774 if (!bptr)
775 return ENOMEM;
776
777 retval = parse_init_state(&state);
778 if (retval) {
779 free (bptr);
780 return retval;
781 }
782 while (!feof(f)) {
783 if (fgets(bptr, BUF_SIZE, f) == NULL)
784 break;
785#ifndef PROFILE_SUPPORTS_FOREIGN_NEWLINES
786 retval = parse_line(bptr, &state);
787 if (retval) {
788 free (bptr);
789 return retval;
790 }
791#else
792 {
793 char *p, *end;
794
795 if (strlen(bptr) >= BUF_SIZE - 1) {
796 /* The string may have foreign newlines and
797 gotten chopped off on a non-newline
798 boundary. Seek backwards to the last known
799 newline. */
800 long offset;
801 char *c = bptr + strlen (bptr);
802 for (offset = 0; offset > -BUF_SIZE; offset--) {
803 if (*c == '\r' || *c == '\n') {
804 *c = '\0';
805 fseek (f, offset, SEEK_CUR);
806 break;
807 }
808 c--;
809 }
810 }
811
812 /* First change all newlines to \n */
813 for (p = bptr; *p != '\0'; p++) {
814 if (*p == '\r')
815 *p = '\n';
816 }
817 /* Then parse all lines */
818 p = bptr;
819 end = bptr + strlen (bptr);
820 while (p < end) {
821 char* newline;
822 char* newp;
823
824 newline = strchr (p, '\n');
825 if (newline != NULL)
826 *newline = '\0';
827
828 /* parse_line modifies contents of p */
829 newp = p + strlen (p) + 1;
830 retval = parse_line (p, &state);
831 if (retval) {
832 free (bptr);
833 return retval;
834 }
835
836 p = newp;
837 }
838 }
839#endif
840 }
841 *root = state.root_section;
842
843 free (bptr);
844 return 0;
845}
846
847/*
848 * Return TRUE if the string begins or ends with whitespace
849 */
850static int need_double_quotes(char *str)
851{
852 if (!str || !*str)
853 return 0;
854 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
855 return 1;
856 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b'))
857 return 1;
858 return 0;
859}
860
861/*
862 * Output a string with double quotes, doing appropriate backquoting
863 * of characters as necessary.
864 */
865static void output_quoted_string(char *str, void (*cb)(const char *,void *),
866 void *data)
867{
868 char ch;
869 char buf[2];
870
871 cb("\"", data);
872 if (!str) {
873 cb("\"", data);
874 return;
875 }
876 buf[1] = 0;
877 while ((ch = *str++)) {
878 switch (ch) {
879 case '\\':
880 cb("\\\\", data);
881 break;
882 case '\n':
883 cb("\\n", data);
884 break;
885 case '\t':
886 cb("\\t", data);
887 break;
888 case '\b':
889 cb("\\b", data);
890 break;
891 default:
892 /* This would be a lot faster if we scanned
893 forward for the next "interesting"
894 character. */
895 buf[0] = ch;
896 cb(buf, data);
897 break;
898 }
899 }
900 cb("\"", data);
901}
902
903#ifndef EOL
904#define EOL "\n"
905#endif
906
Theodore Ts'o55822752005-12-31 16:24:07 -0500907#ifdef DEBUG_PROGRAM
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500908/* Errors should be returned, not ignored! */
909static void dump_profile(struct profile_node *root, int level,
910 void (*cb)(const char *, void *), void *data)
911{
912 int i;
913 struct profile_node *p;
914 void *iter;
915 long retval;
916 char *name, *value;
917
918 iter = 0;
919 do {
920 retval = profile_find_node_relation(root, 0, &iter,
921 &name, &value);
922 if (retval)
923 break;
924 for (i=0; i < level; i++)
925 cb("\t", data);
926 if (need_double_quotes(value)) {
927 cb(name, data);
928 cb(" = ", data);
929 output_quoted_string(value, cb, data);
930 cb(EOL, data);
931 } else {
932 cb(name, data);
933 cb(" = ", data);
934 cb(value, data);
935 cb(EOL, data);
936 }
937 } while (iter != 0);
938
939 iter = 0;
940 do {
941 retval = profile_find_node_subsection(root, 0, &iter,
942 &name, &p);
943 if (retval)
944 break;
945 if (level == 0) { /* [xxx] */
946 cb("[", data);
947 cb(name, data);
948 cb("]", data);
949 cb(profile_is_node_final(p) ? "*" : "", data);
950 cb(EOL, data);
951 dump_profile(p, level+1, cb, data);
952 cb(EOL, data);
953 } else { /* xxx = { ... } */
954 for (i=0; i < level; i++)
955 cb("\t", data);
956 cb(name, data);
957 cb(" = {", data);
958 cb(EOL, data);
959 dump_profile(p, level+1, cb, data);
960 for (i=0; i < level; i++)
961 cb("\t", data);
962 cb("}", data);
963 cb(profile_is_node_final(p) ? "*" : "", data);
964 cb(EOL, data);
965 }
966 } while (iter != 0);
967}
968
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500969static void dump_profile_to_file_cb(const char *str, void *data)
970{
971 fputs(str, data);
972}
973
974errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
975{
976 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
977 return 0;
978}
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500979
980struct prof_buf {
981 char *base;
982 size_t cur, max;
983 int err;
984};
985
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500986static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
987{
988 if (b->err)
989 return;
990 if (b->max - b->cur < len) {
991 size_t newsize;
992 char *newptr;
993
994 newsize = b->max + (b->max >> 1) + len + 1024;
995 newptr = realloc(b->base, newsize);
996 if (newptr == NULL) {
997 b->err = 1;
998 return;
999 }
1000 b->base = newptr;
1001 b->max = newsize;
1002 }
1003 memcpy(b->base + b->cur, d, len);
1004 b->cur += len; /* ignore overflow */
1005}
1006
1007static void dump_profile_to_buffer_cb(const char *str, void *data)
1008{
1009 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
1010}
1011
1012errcode_t profile_write_tree_to_buffer(struct profile_node *root,
1013 char **buf)
1014{
1015 struct prof_buf prof_buf = { 0, 0, 0, 0 };
1016
1017 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
1018 if (prof_buf.err) {
1019 *buf = NULL;
1020 return ENOMEM;
1021 }
1022 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
1023 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
1024 char *newptr = realloc(prof_buf.base, prof_buf.cur);
1025 if (newptr)
1026 prof_buf.base = newptr;
1027 }
1028 *buf = prof_buf.base;
1029 return 0;
1030}
1031#endif
1032
1033/* End prof_parse.c */
1034
1035/* Begin prof_tree.c */
1036
1037/*
1038 * prof_tree.c --- these routines maintain the parse tree of the
1039 * config file.
1040 *
1041 * All of the details of how the tree is stored is abstracted away in
1042 * this file; all of the other profile routines build, access, and
1043 * modify the tree via the accessor functions found in this file.
1044 *
1045 * Each node may represent either a relation or a section header.
1046 *
1047 * A section header must have its value field set to 0, and may a one
1048 * or more child nodes, pointed to by first_child.
1049 *
1050 * A relation has as its value a pointer to allocated memory
1051 * containing a string. Its first_child pointer must be null.
1052 *
1053 */
1054
1055struct profile_node {
1056 errcode_t magic;
1057 char *name;
1058 char *value;
1059 int group_level;
1060 int final:1; /* Indicate don't search next file */
1061 int deleted:1;
1062 struct profile_node *first_child;
1063 struct profile_node *parent;
1064 struct profile_node *next, *prev;
1065};
1066
1067#define CHECK_MAGIC(node) \
1068 if ((node)->magic != PROF_MAGIC_NODE) \
1069 return PROF_MAGIC_NODE;
1070
1071/*
1072 * Free a node, and any children
1073 */
1074void profile_free_node(struct profile_node *node)
1075{
1076 struct profile_node *child, *next;
1077
1078 if (node->magic != PROF_MAGIC_NODE)
1079 return;
1080
1081 if (node->name)
1082 free(node->name);
1083 if (node->value)
1084 free(node->value);
1085
1086 for (child=node->first_child; child; child = next) {
1087 next = child->next;
1088 profile_free_node(child);
1089 }
1090 node->magic = 0;
1091
1092 free(node);
1093}
1094
1095#ifndef HAVE_STRDUP
1096#undef strdup
1097#define strdup MYstrdup
1098static char *MYstrdup (const char *s)
1099{
1100 size_t sz = strlen(s) + 1;
1101 char *p = malloc(sz);
1102 if (p != 0)
1103 memcpy(p, s, sz);
1104 return p;
1105}
1106#endif
1107
1108/*
1109 * Create a node
1110 */
1111errcode_t profile_create_node(const char *name, const char *value,
1112 struct profile_node **ret_node)
1113{
1114 struct profile_node *new;
1115
1116 new = malloc(sizeof(struct profile_node));
1117 if (!new)
1118 return ENOMEM;
1119 memset(new, 0, sizeof(struct profile_node));
1120 new->name = strdup(name);
1121 if (new->name == 0) {
1122 profile_free_node(new);
1123 return ENOMEM;
1124 }
1125 if (value) {
1126 new->value = strdup(value);
1127 if (new->value == 0) {
1128 profile_free_node(new);
1129 return ENOMEM;
1130 }
1131 }
1132 new->magic = PROF_MAGIC_NODE;
1133
1134 *ret_node = new;
1135 return 0;
1136}
1137
1138/*
1139 * This function verifies that all of the representation invarients of
1140 * the profile are true. If not, we have a programming bug somewhere,
1141 * probably in this file.
1142 */
1143#ifdef DEBUG_PROGRAM
1144errcode_t profile_verify_node(struct profile_node *node)
1145{
1146 struct profile_node *p, *last;
1147 errcode_t retval;
1148
1149 CHECK_MAGIC(node);
1150
1151 if (node->value && node->first_child)
1152 return PROF_SECTION_WITH_VALUE;
1153
1154 last = 0;
1155 for (p = node->first_child; p; last = p, p = p->next) {
1156 if (p->prev != last)
1157 return PROF_BAD_LINK_LIST;
1158 if (last && (last->next != p))
1159 return PROF_BAD_LINK_LIST;
1160 if (node->group_level+1 != p->group_level)
1161 return PROF_BAD_GROUP_LVL;
1162 if (p->parent != node)
1163 return PROF_BAD_PARENT_PTR;
1164 retval = profile_verify_node(p);
1165 if (retval)
1166 return retval;
1167 }
1168 return 0;
1169}
1170#endif
1171
1172/*
1173 * Add a node to a particular section
1174 */
1175errcode_t profile_add_node(struct profile_node *section, const char *name,
1176 const char *value, struct profile_node **ret_node)
1177{
1178 errcode_t retval;
1179 struct profile_node *p, *last, *new;
1180
1181 CHECK_MAGIC(section);
1182
1183 if (section->value)
1184 return PROF_ADD_NOT_SECTION;
1185
1186 /*
1187 * Find the place to insert the new node. We look for the
1188 * place *after* the last match of the node name, since
1189 * order matters.
1190 */
1191 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1192 int cmp;
1193 cmp = strcmp(p->name, name);
1194 if (cmp > 0)
1195 break;
1196 }
1197 retval = profile_create_node(name, value, &new);
1198 if (retval)
1199 return retval;
1200 new->group_level = section->group_level+1;
1201 new->deleted = 0;
1202 new->parent = section;
1203 new->prev = last;
1204 new->next = p;
1205 if (p)
1206 p->prev = new;
1207 if (last)
1208 last->next = new;
1209 else
1210 section->first_child = new;
1211 if (ret_node)
1212 *ret_node = new;
1213 return 0;
1214}
1215
1216/*
1217 * Set the final flag on a particular node.
1218 */
1219errcode_t profile_make_node_final(struct profile_node *node)
1220{
1221 CHECK_MAGIC(node);
1222
1223 node->final = 1;
1224 return 0;
1225}
1226
1227/*
1228 * Check the final flag on a node
1229 */
1230int profile_is_node_final(struct profile_node *node)
1231{
1232 return (node->final != 0);
1233}
1234
1235#ifdef DEBUG_PROGRAM
1236/*
1237 * Return the name of a node. (Note: this is for internal functions
1238 * only; if the name needs to be returned from an exported function,
1239 * strdup it first!)
1240 */
1241const char *profile_get_node_name(struct profile_node *node)
1242{
1243 return node->name;
1244}
1245
1246/*
1247 * Return the value of a node. (Note: this is for internal functions
1248 * only; if the name needs to be returned from an exported function,
1249 * strdup it first!)
1250 */
1251const char *profile_get_node_value(struct profile_node *node)
1252{
1253 return node->value;
1254}
1255#endif
1256
1257/*
1258 * Iterate through the section, returning the nodes which match
1259 * the given name. If name is NULL, then interate through all the
1260 * nodes in the section. If section_flag is non-zero, only return the
1261 * section which matches the name; don't return relations. If value
1262 * is non-NULL, then only return relations which match the requested
1263 * value. (The value argument is ignored if section_flag is non-zero.)
1264 *
1265 * The first time this routine is called, the state pointer must be
1266 * null. When this profile_find_node_relation() returns, if the state
1267 * pointer is non-NULL, then this routine should be called again.
1268 * (This won't happen if section_flag is non-zero, obviously.)
1269 *
1270 */
1271errcode_t profile_find_node(struct profile_node *section, const char *name,
1272 const char *value, int section_flag, void **state,
1273 struct profile_node **node)
1274{
1275 struct profile_node *p;
1276
1277 CHECK_MAGIC(section);
1278 p = *state;
1279 if (p) {
1280 CHECK_MAGIC(p);
1281 } else
1282 p = section->first_child;
1283
1284 for (; p; p = p->next) {
1285 if (name && (strcmp(p->name, name)))
1286 continue;
1287 if (section_flag) {
1288 if (p->value)
1289 continue;
1290 } else {
1291 if (!p->value)
1292 continue;
1293 if (value && (strcmp(p->value, value)))
1294 continue;
1295 }
1296 if (p->deleted)
1297 continue;
1298 /* A match! */
1299 if (node)
1300 *node = p;
1301 break;
1302 }
1303 if (p == 0) {
1304 *state = 0;
1305 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1306 }
1307 /*
1308 * OK, we've found one match; now let's try to find another
1309 * one. This way, if we return a non-zero state pointer,
1310 * there's guaranteed to be another match that's returned.
1311 */
1312 for (p = p->next; p; p = p->next) {
1313 if (name && (strcmp(p->name, name)))
1314 continue;
1315 if (section_flag) {
1316 if (p->value)
1317 continue;
1318 } else {
1319 if (!p->value)
1320 continue;
1321 if (value && (strcmp(p->value, value)))
1322 continue;
1323 }
1324 /* A match! */
1325 break;
1326 }
1327 *state = p;
1328 return 0;
1329}
1330
1331
1332/*
1333 * Iterate through the section, returning the relations which match
1334 * the given name. If name is NULL, then interate through all the
1335 * relations in the section. The first time this routine is called,
1336 * the state pointer must be null. When this profile_find_node_relation()
1337 * returns, if the state pointer is non-NULL, then this routine should
1338 * be called again.
1339 *
1340 * The returned character string in value points to the stored
1341 * character string in the parse string. Before this string value is
1342 * returned to a calling application (profile_find_node_relation is not an
1343 * exported interface), it should be strdup()'ed.
1344 */
1345errcode_t profile_find_node_relation(struct profile_node *section,
1346 const char *name, void **state,
1347 char **ret_name, char **value)
1348{
1349 struct profile_node *p;
1350 errcode_t retval;
1351
1352 retval = profile_find_node(section, name, 0, 0, state, &p);
1353 if (retval)
1354 return retval;
1355
1356 if (p) {
1357 if (value)
1358 *value = p->value;
1359 if (ret_name)
1360 *ret_name = p->name;
1361 }
1362 return 0;
1363}
1364
1365/*
1366 * Iterate through the section, returning the subsections which match
1367 * the given name. If name is NULL, then interate through all the
1368 * subsections in the section. The first time this routine is called,
1369 * the state pointer must be null. When this profile_find_node_subsection()
1370 * returns, if the state pointer is non-NULL, then this routine should
1371 * be called again.
1372 *
1373 * This is (plus accessor functions for the name and value given a
1374 * profile node) makes this function mostly syntactic sugar for
1375 * profile_find_node.
1376 */
1377errcode_t profile_find_node_subsection(struct profile_node *section,
1378 const char *name, void **state,
1379 char **ret_name,
1380 struct profile_node **subsection)
1381{
1382 struct profile_node *p;
1383 errcode_t retval;
1384
1385 retval = profile_find_node(section, name, 0, 1, state, &p);
1386 if (retval)
1387 return retval;
1388
1389 if (p) {
1390 if (subsection)
1391 *subsection = p;
1392 if (ret_name)
1393 *ret_name = p->name;
1394 }
1395 return 0;
1396}
1397
1398/*
1399 * This function returns the parent of a particular node.
1400 */
1401errcode_t profile_get_node_parent(struct profile_node *section,
1402 struct profile_node **parent)
1403{
1404 *parent = section->parent;
1405 return 0;
1406}
1407
1408/*
1409 * This is a general-purpose iterator for returning all nodes that
1410 * match the specified name array.
1411 */
1412struct profile_iterator {
1413 prf_magic_t magic;
1414 profile_t profile;
1415 int flags;
1416 const char *const *names;
1417 const char *name;
1418 prf_file_t file;
1419 int file_serial;
1420 int done_idx;
1421 struct profile_node *node;
1422 int num;
1423};
1424
1425errcode_t profile_node_iterator_create(profile_t profile,
1426 const char *const *names, int flags,
1427 void **ret_iter)
1428{
1429 struct profile_iterator *iter;
1430 int done_idx = 0;
1431
1432 if (profile == 0)
1433 return PROF_NO_PROFILE;
1434 if (profile->magic != PROF_MAGIC_PROFILE)
1435 return PROF_MAGIC_PROFILE;
1436 if (!names)
1437 return PROF_BAD_NAMESET;
1438 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1439 if (!names[0])
1440 return PROF_BAD_NAMESET;
1441 done_idx = 1;
1442 }
1443
1444 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1445 return ENOMEM;
1446
1447 iter->magic = PROF_MAGIC_ITERATOR;
1448 iter->profile = profile;
1449 iter->names = names;
1450 iter->flags = flags;
1451 iter->file = profile->first_file;
1452 iter->done_idx = done_idx;
1453 iter->node = 0;
1454 iter->num = 0;
1455 *ret_iter = iter;
1456 return 0;
1457}
1458
1459void profile_node_iterator_free(void **iter_p)
1460{
1461 struct profile_iterator *iter;
1462
1463 if (!iter_p)
1464 return;
1465 iter = *iter_p;
1466 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1467 return;
1468 free(iter);
1469 *iter_p = 0;
1470}
1471
1472/*
1473 * Note: the returned character strings in ret_name and ret_value
1474 * points to the stored character string in the parse string. Before
1475 * this string value is returned to a calling application
1476 * (profile_node_iterator is not an exported interface), it should be
1477 * strdup()'ed.
1478 */
1479errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1480 char **ret_name, char **ret_value)
1481{
1482 struct profile_iterator *iter = *iter_p;
1483 struct profile_node *section, *p;
1484 const char *const *cpp;
1485 errcode_t retval;
1486 int skip_num = 0;
1487
1488 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1489 return PROF_MAGIC_ITERATOR;
1490 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1491 return PROF_MAGIC_FILE;
1492 if (iter->file && iter->file->data->magic != PROF_MAGIC_FILE_DATA)
1493 return PROF_MAGIC_FILE_DATA;
1494 /*
1495 * If the file has changed, then the node pointer is invalid,
1496 * so we'll have search the file again looking for it.
1497 */
1498 if (iter->node && (iter->file->data->upd_serial != iter->file_serial)) {
1499 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1500 skip_num = iter->num;
1501 iter->node = 0;
1502 }
1503 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1504 return PROF_MAGIC_NODE;
1505 }
1506get_new_file:
1507 if (iter->node == 0) {
1508 if (iter->file == 0 ||
1509 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
1510 profile_node_iterator_free(iter_p);
1511 if (ret_node)
1512 *ret_node = 0;
1513 if (ret_name)
1514 *ret_name = 0;
1515 if (ret_value)
1516 *ret_value =0;
1517 return 0;
1518 }
1519 if ((retval = profile_update_file(iter->file))) {
1520 if (retval == ENOENT || retval == EACCES) {
1521 /* XXX memory leak? */
1522 iter->file = iter->file->next;
1523 skip_num = 0;
1524 retval = 0;
1525 goto get_new_file;
1526 } else {
1527 profile_node_iterator_free(iter_p);
1528 return retval;
1529 }
1530 }
1531 iter->file_serial = iter->file->data->upd_serial;
1532 /*
1533 * Find the section to list if we are a LIST_SECTION,
1534 * or find the containing section if not.
1535 */
1536 section = iter->file->data->root;
1537 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1538 for (p=section->first_child; p; p = p->next) {
1539 if (!strcmp(p->name, *cpp) && !p->value)
1540 break;
1541 }
1542 if (!p) {
1543 section = 0;
1544 break;
1545 }
1546 section = p;
1547 if (p->final)
1548 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1549 }
1550 if (!section) {
1551 iter->file = iter->file->next;
1552 skip_num = 0;
1553 goto get_new_file;
1554 }
1555 iter->name = *cpp;
1556 iter->node = section->first_child;
1557 }
1558 /*
1559 * OK, now we know iter->node is set up correctly. Let's do
1560 * the search.
1561 */
1562 for (p = iter->node; p; p = p->next) {
1563 if (iter->name && strcmp(p->name, iter->name))
1564 continue;
1565 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1566 p->value)
1567 continue;
1568 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1569 !p->value)
1570 continue;
1571 if (skip_num > 0) {
1572 skip_num--;
1573 continue;
1574 }
1575 if (p->deleted)
1576 continue;
1577 break;
1578 }
1579 iter->num++;
1580 if (!p) {
1581 iter->file = iter->file->next;
1582 if (iter->file) {
1583 }
1584 iter->node = 0;
1585 skip_num = 0;
1586 goto get_new_file;
1587 }
1588 if ((iter->node = p->next) == NULL)
1589 iter->file = iter->file->next;
1590 if (ret_node)
1591 *ret_node = p;
1592 if (ret_name)
1593 *ret_name = p->name;
1594 if (ret_value)
1595 *ret_value = p->value;
1596 return 0;
1597}
1598
1599
1600
1601/* End prof_tree.c */
1602
1603
1604/* Begin prof_get.c */
1605/*
1606 * prof_get.c --- routines that expose the public interfaces for
1607 * querying items from the profile.
1608 *
1609 */
1610
1611/*
1612 * These functions --- init_list(), end_list(), and add_to_list() are
1613 * internal functions used to build up a null-terminated char ** list
1614 * of strings to be returned by functions like profile_get_values.
1615 *
1616 * The profile_string_list structure is used for internal booking
1617 * purposes to build up the list, which is returned in *ret_list by
1618 * the end_list() function.
1619 *
1620 * The publicly exported interface for freeing char** list is
1621 * profile_free_list().
1622 */
1623
1624struct profile_string_list {
1625 char **list;
1626 int num;
1627 int max;
1628};
1629
1630/*
1631 * Initialize the string list abstraction.
1632 */
1633static errcode_t init_list(struct profile_string_list *list)
1634{
1635 list->num = 0;
1636 list->max = 10;
1637 list->list = malloc(list->max * sizeof(char *));
1638 if (list->list == 0)
1639 return ENOMEM;
1640 list->list[0] = 0;
1641 return 0;
1642}
1643
1644/*
1645 * Free any memory left over in the string abstraction, returning the
1646 * built up list in *ret_list if it is non-null.
1647 */
1648static void end_list(struct profile_string_list *list, char ***ret_list)
1649{
1650 char **cp;
1651
1652 if (list == 0)
1653 return;
1654
1655 if (ret_list) {
1656 *ret_list = list->list;
1657 return;
1658 } else {
1659 for (cp = list->list; *cp; cp++)
1660 free(*cp);
1661 free(list->list);
1662 }
1663 list->num = list->max = 0;
1664 list->list = 0;
1665}
1666
1667/*
1668 * Add a string to the list.
1669 */
1670static errcode_t add_to_list(struct profile_string_list *list, const char *str)
1671{
1672 char *newstr, **newlist;
1673 int newmax;
1674
1675 if (list->num+1 >= list->max) {
1676 newmax = list->max + 10;
1677 newlist = realloc(list->list, newmax * sizeof(char *));
1678 if (newlist == 0)
1679 return ENOMEM;
1680 list->max = newmax;
1681 list->list = newlist;
1682 }
1683 newstr = malloc(strlen(str)+1);
1684 if (newstr == 0)
1685 return ENOMEM;
1686 strcpy(newstr, str);
1687
1688 list->list[list->num++] = newstr;
1689 list->list[list->num] = 0;
1690 return 0;
1691}
1692
1693/*
1694 * Return TRUE if the string is already a member of the list.
1695 */
1696static int is_list_member(struct profile_string_list *list, const char *str)
1697{
1698 char **cpp;
1699
1700 if (!list->list)
1701 return 0;
1702
1703 for (cpp = list->list; *cpp; cpp++) {
1704 if (!strcmp(*cpp, str))
1705 return 1;
1706 }
1707 return 0;
1708}
1709
1710/*
1711 * This function frees a null-terminated list as returned by
1712 * profile_get_values.
1713 */
1714void profile_free_list(char **list)
1715{
1716 char **cp;
1717
1718 if (list == 0)
1719 return;
1720
1721 for (cp = list; *cp; cp++)
1722 free(*cp);
1723 free(list);
1724}
1725
1726errcode_t
1727profile_get_values(profile_t profile, const char *const *names,
1728 char ***ret_values)
1729{
1730 errcode_t retval;
1731 void *state;
1732 char *value;
1733 struct profile_string_list values;
1734
1735 if ((retval = profile_node_iterator_create(profile, names,
1736 PROFILE_ITER_RELATIONS_ONLY,
1737 &state)))
1738 return retval;
1739
1740 if ((retval = init_list(&values)))
1741 return retval;
1742
1743 do {
1744 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1745 goto cleanup;
1746 if (value)
1747 add_to_list(&values, value);
1748 } while (state);
1749
1750 if (values.num == 0) {
1751 retval = PROF_NO_RELATION;
1752 goto cleanup;
1753 }
1754
1755 end_list(&values, ret_values);
1756 return 0;
1757
1758cleanup:
1759 end_list(&values, 0);
1760 return retval;
1761}
1762
1763/*
1764 * This function only gets the first value from the file; it is a
1765 * helper function for profile_get_string, profile_get_integer, etc.
1766 */
1767errcode_t profile_get_value(profile_t profile, const char **names,
1768 const char **ret_value)
1769{
1770 errcode_t retval;
1771 void *state;
1772 char *value;
1773
1774 if ((retval = profile_node_iterator_create(profile, names,
1775 PROFILE_ITER_RELATIONS_ONLY,
1776 &state)))
1777 return retval;
1778
1779 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1780 goto cleanup;
1781
1782 if (value)
1783 *ret_value = value;
1784 else
1785 retval = PROF_NO_RELATION;
1786
1787cleanup:
1788 profile_node_iterator_free(&state);
1789 return retval;
1790}
1791
1792errcode_t
1793profile_get_string(profile_t profile, const char *name, const char *subname,
1794 const char *subsubname, const char *def_val,
1795 char **ret_string)
1796{
1797 const char *value;
1798 errcode_t retval;
1799 const char *names[4];
1800
1801 if (profile) {
1802 names[0] = name;
1803 names[1] = subname;
1804 names[2] = subsubname;
1805 names[3] = 0;
1806 retval = profile_get_value(profile, names, &value);
1807 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1808 value = def_val;
1809 else if (retval)
1810 return retval;
1811 } else
1812 value = def_val;
1813
1814 if (value) {
1815 *ret_string = malloc(strlen(value)+1);
1816 if (*ret_string == 0)
1817 return ENOMEM;
1818 strcpy(*ret_string, value);
1819 } else
1820 *ret_string = 0;
1821 return 0;
1822}
1823
1824errcode_t
1825profile_get_integer(profile_t profile, const char *name, const char *subname,
1826 const char *subsubname, int def_val, int *ret_int)
1827{
1828 const char *value;
1829 errcode_t retval;
1830 const char *names[4];
1831 char *end_value;
1832 long ret_long;
1833
1834 *ret_int = def_val;
1835 if (profile == 0)
1836 return 0;
1837
1838 names[0] = name;
1839 names[1] = subname;
1840 names[2] = subsubname;
1841 names[3] = 0;
1842 retval = profile_get_value(profile, names, &value);
1843 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1844 *ret_int = def_val;
1845 return 0;
1846 } else if (retval)
1847 return retval;
1848
1849 if (value[0] == 0)
1850 /* Empty string is no good. */
1851 return PROF_BAD_INTEGER;
1852 errno = 0;
1853 ret_long = strtol (value, &end_value, 10);
1854
1855 /* Overflow or underflow. */
1856 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1857 return PROF_BAD_INTEGER;
1858 /* Value outside "int" range. */
1859 if ((long) (int) ret_long != ret_long)
1860 return PROF_BAD_INTEGER;
1861 /* Garbage in string. */
1862 if (end_value != value + strlen (value))
1863 return PROF_BAD_INTEGER;
1864
1865
1866 *ret_int = ret_long;
1867 return 0;
1868}
1869
1870static const char *const conf_yes[] = {
1871 "y", "yes", "true", "t", "1", "on",
1872 0,
1873};
1874
1875static const char *const conf_no[] = {
1876 "n", "no", "false", "nil", "0", "off",
1877 0,
1878};
1879
1880static errcode_t
1881profile_parse_boolean(const char *s, int *ret_boolean)
1882{
1883 const char *const *p;
1884
1885 if (ret_boolean == NULL)
1886 return PROF_EINVAL;
1887
1888 for(p=conf_yes; *p; p++) {
1889 if (!strcasecmp(*p,s)) {
1890 *ret_boolean = 1;
1891 return 0;
1892 }
1893 }
1894
1895 for(p=conf_no; *p; p++) {
1896 if (!strcasecmp(*p,s)) {
1897 *ret_boolean = 0;
1898 return 0;
1899 }
1900 }
1901
1902 return PROF_BAD_BOOLEAN;
1903}
1904
1905errcode_t
1906profile_get_boolean(profile_t profile, const char *name, const char *subname,
1907 const char *subsubname, int def_val, int *ret_boolean)
1908{
1909 const char *value;
1910 errcode_t retval;
1911 const char *names[4];
1912
1913 if (profile == 0) {
1914 *ret_boolean = def_val;
1915 return 0;
1916 }
1917
1918 names[0] = name;
1919 names[1] = subname;
1920 names[2] = subsubname;
1921 names[3] = 0;
1922 retval = profile_get_value(profile, names, &value);
1923 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1924 *ret_boolean = def_val;
1925 return 0;
1926 } else if (retval)
1927 return retval;
1928
1929 return profile_parse_boolean (value, ret_boolean);
1930}
1931
1932/*
1933 * This function will return the list of the names of subections in the
1934 * under the specified section name.
1935 */
1936errcode_t
1937profile_get_subsection_names(profile_t profile, const char **names,
1938 char ***ret_names)
1939{
1940 errcode_t retval;
1941 void *state;
1942 char *name;
1943 struct profile_string_list values;
1944
1945 if ((retval = profile_node_iterator_create(profile, names,
1946 PROFILE_ITER_LIST_SECTION | PROFILE_ITER_SECTIONS_ONLY,
1947 &state)))
1948 return retval;
1949
1950 if ((retval = init_list(&values)))
1951 return retval;
1952
1953 do {
1954 if ((retval = profile_node_iterator(&state, 0, &name, 0)))
1955 goto cleanup;
1956 if (name)
1957 add_to_list(&values, name);
1958 } while (state);
1959
1960 end_list(&values, ret_names);
1961 return 0;
1962
1963cleanup:
1964 end_list(&values, 0);
1965 return retval;
1966}
1967
1968/*
1969 * This function will return the list of the names of relations in the
1970 * under the specified section name.
1971 */
1972errcode_t
1973profile_get_relation_names(profile_t profile, const char **names,
1974 char ***ret_names)
1975{
1976 errcode_t retval;
1977 void *state;
1978 char *name;
1979 struct profile_string_list values;
1980
1981 if ((retval = profile_node_iterator_create(profile, names,
1982 PROFILE_ITER_LIST_SECTION | PROFILE_ITER_RELATIONS_ONLY,
1983 &state)))
1984 return retval;
1985
1986 if ((retval = init_list(&values)))
1987 return retval;
1988
1989 do {
1990 if ((retval = profile_node_iterator(&state, 0, &name, 0)))
1991 goto cleanup;
1992 if (name && !is_list_member(&values, name))
1993 add_to_list(&values, name);
1994 } while (state);
1995
1996 end_list(&values, ret_names);
1997 return 0;
1998
1999cleanup:
2000 end_list(&values, 0);
2001 return retval;
2002}
2003
2004errcode_t
2005profile_iterator_create(profile_t profile, const char *const *names, int flags,
2006 void **ret_iter)
2007{
2008 return profile_node_iterator_create(profile, names, flags, ret_iter);
2009}
2010
2011void
2012profile_iterator_free(void **iter_p)
2013{
2014 profile_node_iterator_free(iter_p);
2015}
2016
2017errcode_t
2018profile_iterator(void **iter_p, char **ret_name, char **ret_value)
2019{
2020 char *name, *value;
2021 errcode_t retval;
2022
2023 retval = profile_node_iterator(iter_p, 0, &name, &value);
2024 if (retval)
2025 return retval;
2026
2027 if (ret_name) {
2028 if (name) {
2029 *ret_name = malloc(strlen(name)+1);
2030 if (!*ret_name)
2031 return ENOMEM;
2032 strcpy(*ret_name, name);
2033 } else
2034 *ret_name = 0;
2035 }
2036 if (ret_value) {
2037 if (value) {
2038 *ret_value = malloc(strlen(value)+1);
2039 if (!*ret_value) {
2040 if (ret_name) {
2041 free(*ret_name);
2042 *ret_name = 0;
2043 }
2044 return ENOMEM;
2045 }
2046 strcpy(*ret_value, value);
2047 } else
2048 *ret_value = 0;
2049 }
2050 return 0;
2051}
2052
2053void
2054profile_release_string(char *str)
2055{
2056 free(str);
2057}
2058
2059/* End prof_get.c */
2060
2061#ifdef DEBUG_PROGRAM
2062
2063/*
2064 * test_profile.c --- testing program for the profile routine
2065 */
2066
2067#include "argv_parse.h"
2068
2069const char *program_name = "test_profile";
2070
2071#define PRINT_VALUE 1
2072#define PRINT_VALUES 2
2073
2074static void do_batchmode(profile)
2075 profile_t profile;
2076{
2077 errcode_t retval;
2078 int argc, ret;
2079 char **argv, **values, **cpp;
2080 char buf[256];
2081 const char **names, *value;
2082 char *cmd;
2083 int print_status;
2084
2085 while (!feof(stdin)) {
2086 if (fgets(buf, sizeof(buf), stdin) == NULL)
2087 break;
2088 printf(">%s", buf);
2089 ret = argv_parse(buf, &argc, &argv);
2090 if (ret != 0) {
2091 printf("Argv_parse returned %d!\n", ret);
2092 continue;
2093 }
2094 cmd = *(argv);
2095 names = (const char **) argv + 1;
2096 print_status = 0;
2097 retval = 0;
2098 if (cmd == 0) {
2099 argv_free(argv);
2100 continue;
2101 }
2102 if (!strcmp(cmd, "query")) {
2103 retval = profile_get_values(profile, names, &values);
2104 print_status = PRINT_VALUES;
2105 } else if (!strcmp(cmd, "query1")) {
2106 retval = profile_get_value(profile, names, &value);
2107 print_status = PRINT_VALUE;
2108 } else if (!strcmp(cmd, "list_sections")) {
2109 retval = profile_get_subsection_names(profile, names,
2110 &values);
2111 print_status = PRINT_VALUES;
2112 } else if (!strcmp(cmd, "list_relations")) {
2113 retval = profile_get_relation_names(profile, names,
2114 &values);
2115 print_status = PRINT_VALUES;
2116 } else if (!strcmp(cmd, "dump")) {
2117 retval = profile_write_tree_file
2118 (profile->first_file->data->root, stdout);
2119#if 0
2120 } else if (!strcmp(cmd, "clear")) {
2121 retval = profile_clear_relation(profile, names);
2122 } else if (!strcmp(cmd, "update")) {
2123 retval = profile_update_relation(profile, names+2,
2124 *names, *(names+1));
2125#endif
2126 } else if (!strcmp(cmd, "verify")) {
2127 retval = profile_verify_node
2128 (profile->first_file->data->root);
2129#if 0
2130 } else if (!strcmp(cmd, "rename_section")) {
2131 retval = profile_rename_section(profile, names+1,
2132 *names);
2133 } else if (!strcmp(cmd, "add")) {
2134 value = *names;
2135 if (strcmp(value, "NULL") == 0)
2136 value = NULL;
2137 retval = profile_add_relation(profile, names+1,
2138 value);
2139 } else if (!strcmp(cmd, "flush")) {
2140 retval = profile_flush(profile);
2141#endif
2142 } else {
2143 printf("Invalid command.\n");
2144 }
2145 if (retval) {
2146 com_err(cmd, retval, "");
2147 print_status = 0;
2148 }
2149 switch (print_status) {
2150 case PRINT_VALUE:
2151 printf("%s\n", value);
2152 break;
2153 case PRINT_VALUES:
2154 for (cpp = values; *cpp; cpp++)
2155 printf("%s\n", *cpp);
2156 profile_free_list(values);
2157 break;
2158 }
2159 printf("\n");
2160 argv_free(argv);
2161 }
2162 profile_release(profile);
2163 exit(0);
2164
2165}
2166
2167
2168int main(argc, argv)
2169 int argc;
2170 char **argv;
2171{
2172 profile_t profile;
2173 long retval;
2174 char **values, **cpp;
2175 const char *value;
2176 const char **names;
2177 char *cmd;
2178 int print_value = 0;
2179
2180 if (argc < 2) {
2181 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
2182 exit(1);
2183 }
2184
2185 initialize_prof_error_table();
2186
2187 retval = profile_init_path(argv[1], &profile);
2188 if (retval) {
2189 com_err(program_name, retval, "while initializing profile");
2190 exit(1);
2191 }
2192 cmd = *(argv+2);
2193 names = (const char **) argv+3;
2194 if (!cmd || !strcmp(cmd, "batch"))
2195 do_batchmode(profile);
2196 if (!strcmp(cmd, "query")) {
2197 retval = profile_get_values(profile, names, &values);
2198 } else if (!strcmp(cmd, "query1")) {
2199 retval = profile_get_value(profile, names, &value);
2200 print_value++;
2201 } else if (!strcmp(cmd, "list_sections")) {
2202 retval = profile_get_subsection_names(profile, names, &values);
2203 } else if (!strcmp(cmd, "list_relations")) {
2204 retval = profile_get_relation_names(profile, names, &values);
2205 } else {
2206 fprintf(stderr, "Invalid command.\n");
2207 exit(1);
2208 }
2209 if (retval) {
2210 com_err(argv[0], retval, "while getting values");
2211 profile_release(profile);
2212 exit(1);
2213 }
2214 if (print_value) {
2215 printf("%s\n", value);
2216 } else {
2217 for (cpp = values; *cpp; cpp++)
2218 printf("%s\n", *cpp);
2219 profile_free_list(values);
2220 }
2221 profile_release(profile);
2222
2223 return 0;
2224}
2225
2226#endif