xref: /freebsd/contrib/libucl/utils/objdump.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /* Copyright (c) 2013, Dmitriy V. Reshetnikov
2  * Copyright (c) 2013, Vsevolod Stakhov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *       * Redistributions of source code must retain the above copyright
8  *         notice, this list of conditions and the following disclaimer.
9  *       * Redistributions in binary form must reproduce the above copyright
10  *         notice, this list of conditions and the following disclaimer in the
11  *         documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <stdio.h>
26 #include <errno.h>
27 
28 #include "ucl.h"
29 
30 void
31 ucl_obj_dump (const ucl_object_t *obj, unsigned int shift)
32 {
33 	int num = shift * 4 + 5;
34 	char *pre = (char *) malloc (num * sizeof(char));
35 	const ucl_object_t *cur, *tmp;
36 	ucl_object_iter_t it = NULL, it_obj = NULL;
37 
38 	pre[--num] = 0x00;
39 	while (num--)
40 		pre[num] = 0x20;
41 
42 	tmp = obj;
43 
44 	while ((obj = ucl_object_iterate (tmp, &it, false))) {
45 		printf ("%sucl object address: %p\n", pre + 4, obj);
46 		if (obj->key != NULL) {
47 			printf ("%skey: \"%s\"\n", pre, ucl_object_key (obj));
48 		}
49 		printf ("%sref: %u\n", pre, obj->ref);
50 		printf ("%slen: %u\n", pre, obj->len);
51 		printf ("%sprev: %p\n", pre, obj->prev);
52 		printf ("%snext: %p\n", pre, obj->next);
53 		if (obj->type == UCL_OBJECT) {
54 			printf ("%stype: UCL_OBJECT\n", pre);
55 			printf ("%svalue: %p\n", pre, obj->value.ov);
56 			it_obj = NULL;
57 			while ((cur = ucl_object_iterate (obj, &it_obj, true))) {
58 				ucl_obj_dump (cur, shift + 2);
59 			}
60 		}
61 		else if (obj->type == UCL_ARRAY) {
62 			printf ("%stype: UCL_ARRAY\n", pre);
63 			printf ("%svalue: %p\n", pre, obj->value.av);
64 			it_obj = NULL;
65 			while ((cur = ucl_object_iterate (obj, &it_obj, true))) {
66 				ucl_obj_dump (cur, shift + 2);
67 			}
68 		}
69 		else if (obj->type == UCL_INT) {
70 			printf ("%stype: UCL_INT\n", pre);
71 			printf ("%svalue: %jd\n", pre, (intmax_t)ucl_object_toint (obj));
72 		}
73 		else if (obj->type == UCL_FLOAT) {
74 			printf ("%stype: UCL_FLOAT\n", pre);
75 			printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
76 		}
77 		else if (obj->type == UCL_STRING) {
78 			printf ("%stype: UCL_STRING\n", pre);
79 			printf ("%svalue: \"%s\"\n", pre, ucl_object_tostring (obj));
80 		}
81 		else if (obj->type == UCL_BOOLEAN) {
82 			printf ("%stype: UCL_BOOLEAN\n", pre);
83 			printf ("%svalue: %s\n", pre, ucl_object_tostring_forced (obj));
84 		}
85 		else if (obj->type == UCL_TIME) {
86 			printf ("%stype: UCL_TIME\n", pre);
87 			printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
88 		}
89 		else if (obj->type == UCL_USERDATA) {
90 			printf ("%stype: UCL_USERDATA\n", pre);
91 			printf ("%svalue: %p\n", pre, obj->value.ud);
92 		}
93 	}
94 
95 	free (pre);
96 }
97 
98 int
99 main(int argc, char **argv)
100 {
101 	const char *fn = NULL;
102 	unsigned char *inbuf;
103 	struct ucl_parser *parser;
104 	int k, ret = 0, r = 0;
105 	ssize_t bufsize;
106 	ucl_object_t *obj = NULL;
107 	const ucl_object_t *par;
108 	FILE *in;
109 
110 	if (argc > 1) {
111 		fn = argv[1];
112 	}
113 
114 	if (fn != NULL) {
115 		in = fopen (fn, "r");
116 		if (in == NULL) {
117 			exit (-errno);
118 		}
119 	}
120 	else {
121 		in = stdin;
122 	}
123 
124 	parser = ucl_parser_new (0);
125 	inbuf = malloc (BUFSIZ);
126 	bufsize = BUFSIZ;
127 	r = 0;
128 
129 	while (!feof (in) && !ferror (in)) {
130 		if (r == bufsize) {
131 			inbuf = realloc (inbuf, bufsize * 2);
132 			bufsize *= 2;
133 			if (inbuf == NULL) {
134 				perror ("realloc");
135 				exit (EXIT_FAILURE);
136 			}
137 		}
138 		r += fread (inbuf + r, 1, bufsize - r, in);
139 	}
140 
141 	if (ferror (in)) {
142 		fprintf (stderr, "Failed to read the input file.\n");
143 		exit (EXIT_FAILURE);
144 	}
145 
146 	ucl_parser_add_chunk (parser, inbuf, r);
147 	fclose (in);
148 	if (ucl_parser_get_error(parser)) {
149 		printf ("Error occured: %s\n", ucl_parser_get_error(parser));
150 		ret = 1;
151 		goto end;
152 	}
153 
154 	obj = ucl_parser_get_object (parser);
155 	if (ucl_parser_get_error (parser)) {
156 		printf ("Error occured: %s\n", ucl_parser_get_error(parser));
157 		ret = 1;
158 		goto end;
159 	}
160 
161 	if (argc > 2) {
162 		for (k = 2; k < argc; k++) {
163 			printf ("search for \"%s\"... ", argv[k]);
164 			par = ucl_object_lookup (obj, argv[k]);
165 			printf ("%sfound\n", (par == NULL )?"not ":"");
166 			ucl_obj_dump (par, 0);
167 		}
168 	}
169 	else {
170 		ucl_obj_dump (obj, 0);
171 	}
172 
173 end:
174 	if (parser != NULL) {
175 		ucl_parser_free (parser);
176 	}
177 	if (obj != NULL) {
178 		ucl_object_unref (obj);
179 	}
180 
181 	return ret;
182 }
183