1 /*
2 ** OSSP cfg - Configuration Parsing
3 ** Copyright (c) 2002-2006 Ralf S. Engelschall <rse@engelschall.com>
4 ** Copyright (c) 2002-2006 The OSSP Project (http://www.ossp.org/)
5 **
6 ** This file is part of OSSP cfg, a configuration parsing
7 ** library which can be found at http://www.ossp.org/pkg/lib/cfg/.
8 **
9 ** Permission to use, copy, modify, and distribute this software for
10 ** any purpose with or without fee is hereby granted, provided that
11 ** the above copyright notice and this permission notice appear in all
12 ** copies.
13 **
14 ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
15 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
18 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
21 ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 ** SUCH DAMAGE.
26 **
27 ** cfg_test.c: test suite
28 */
29
30 /* standard system headers */
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 /* OSSP cfg headers */
36 #include "cfg.h"
37 #include "cfg_util.h"
38 #include "cfg_global.h"
39
40 /* main test program procedure */
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 cfg_rc_t rc;
44 char *im_ptr;
45 size_t im_size;
46 size_t im_used;
47 char *ex_ptr;
48 char *error;
49 cfg_t *cfg;
50 cfg_node_t **vec;
51 int i;
52
53 /* command line processing */
54 if (argc < 2 || argc > 3) {
55 fprintf(stderr, "USAGE: %s <file> [<select>]\n", argv[0]);
56 exit(1);
57 }
58
59 /* read configuration file into memory */
60 if ((rc = cfg_util_readfile(argv[1], &im_ptr, &im_size, &im_used)) != CFG_OK) {
61 fprintf(stderr, "ERROR: reading file \"%s\"\n", argv[1]);
62 exit(1);
63 }
64
65 /* create configuration object */
66 if ((rc = cfg_create(&cfg)) != CFG_OK) {
67 cfg_error(cfg, rc, &error);
68 fprintf(stderr, "ERROR: cfg_create: %s\n", error);
69 free(im_ptr);
70 exit(1);
71 }
72
73 /* parse configuration */
74 if ((rc = cfg_import(cfg, NULL, CFG_FMT_CFG, im_ptr, im_used)) != CFG_OK) {
75 cfg_error(cfg, rc, &error);
76 fprintf(stderr, "ERROR: cfg_import: %s\n", error);
77 free(im_ptr);
78 cfg_destroy(cfg);
79 exit(1);
80 }
81
82 /* free configuration file in memory */
83 free(im_ptr);
84
85 /* export configuration again into memory */
86 if ((rc = cfg_export(cfg, NULL, CFG_FMT_CFG, &ex_ptr, 0)) != CFG_OK) {
87 cfg_error(cfg, rc, &error);
88 fprintf(stderr, "ERROR: cfg_export: %s\n", error);
89 cfg_destroy(cfg);
90 exit(1);
91 }
92
93 /* print results */
94 fprintf(stdout, "%s", ex_ptr);
95 fflush(stdout);
96
97 /* free configuration file in memory */
98 free(ex_ptr);
99
100 /* optional node selection */
101 if (argc == 3) {
102 fprintf(stdout, "==== selection process ====\n");
103 if ((rc = cfg_node_select(cfg, NULL, &vec, "%s", argv[2])) != CFG_OK) {
104 cfg_error(cfg, rc, &error);
105 fprintf(stderr, "ERROR: cfg_node_select: %s\n", error);
106 cfg_destroy(cfg);
107 exit(1);
108 }
109 fprintf(stdout, "==== selection results ====\n");
110 for (i = 0; vec[i] != NULL; i++) {
111 fprintf(stdout, "---- selection result #%d ----\n", i);
112 if ((rc = cfg_export(cfg, vec[i], CFG_FMT_CFG, &ex_ptr, 0)) != CFG_OK) {
113 cfg_error(cfg, rc, &error);
114 fprintf(stderr, "ERROR: cfg_export: %s\n", error);
115 cfg_destroy(cfg);
116 exit(1);
117 }
118 fprintf(stdout, "%s", ex_ptr);
119 fprintf(stdout, "---- selection #%d ----\n", i);
120 free(ex_ptr);
121 }
122 fprintf(stdout, "==== selection end ====\n");
123 }
124
125 /* destroy configuration object */
126 cfg_destroy(cfg);
127
128 return 0;
129 }
130
131