xref: /freebsd/contrib/libucl/utils/objdump.c (revision a831d7d1c538b93ffec095657d9a6e6e778db195)
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_iterate_object (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: %hd\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_iterate_object (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 			ucl_obj_dump (obj->value.av, shift + 2);
65 		}
66 		else if (obj->type == UCL_INT) {
67 			printf ("%stype: UCL_INT\n", pre);
68 			printf ("%svalue: %jd\n", pre, (intmax_t)ucl_object_toint (obj));
69 		}
70 		else if (obj->type == UCL_FLOAT) {
71 			printf ("%stype: UCL_FLOAT\n", pre);
72 			printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
73 		}
74 		else if (obj->type == UCL_STRING) {
75 			printf ("%stype: UCL_STRING\n", pre);
76 			printf ("%svalue: \"%s\"\n", pre, ucl_object_tostring (obj));
77 		}
78 		else if (obj->type == UCL_BOOLEAN) {
79 			printf ("%stype: UCL_BOOLEAN\n", pre);
80 			printf ("%svalue: %s\n", pre, ucl_object_tostring_forced (obj));
81 		}
82 		else if (obj->type == UCL_TIME) {
83 			printf ("%stype: UCL_TIME\n", pre);
84 			printf ("%svalue: %f\n", pre, ucl_object_todouble (obj));
85 		}
86 		else if (obj->type == UCL_USERDATA) {
87 			printf ("%stype: UCL_USERDATA\n", pre);
88 			printf ("%svalue: %p\n", pre, obj->value.ud);
89 		}
90 	}
91 
92 	free (pre);
93 }
94 
95 int
96 main(int argc, char **argv)
97 {
98 	const char *fn = NULL;
99 	char inbuf[8192];
100 	struct ucl_parser *parser;
101 	int k, ret = 0, r = 0;
102 	ucl_object_t *obj = NULL;
103 	const ucl_object_t *par;
104 	FILE *in;
105 
106 	if (argc > 1) {
107 		fn = argv[1];
108 	}
109 
110 	if (fn != NULL) {
111 		in = fopen (fn, "r");
112 		if (in == NULL) {
113 			exit (-errno);
114 		}
115 	}
116 	else {
117 		in = stdin;
118 	}
119 
120 	parser = ucl_parser_new (0);
121 	while (!feof (in) && r < (int)sizeof (inbuf)) {
122 		r += fread (inbuf + r, 1, sizeof (inbuf) - r, in);
123 	}
124 	ucl_parser_add_chunk (parser, inbuf, r);
125 	fclose (in);
126 	if (ucl_parser_get_error(parser)) {
127 		printf ("Error occured: %s\n", ucl_parser_get_error(parser));
128 		ret = 1;
129 		goto end;
130 	}
131 
132 	obj = ucl_parser_get_object (parser);
133 	if (ucl_parser_get_error (parser)) {
134 		printf ("Error occured: %s\n", ucl_parser_get_error(parser));
135 		ret = 1;
136 		goto end;
137 	}
138 
139 	if (argc > 2) {
140 		for (k = 2; k < argc; k++) {
141 			printf ("search for \"%s\"... ", argv[k]);
142 			par = ucl_object_find_key (obj, argv[k]);
143 			printf ("%sfound\n", (par == NULL )?"not ":"");
144 			ucl_obj_dump (par, 0);
145 		}
146 	}
147 	else {
148 		ucl_obj_dump (obj, 0);
149 	}
150 
151 end:
152 	if (parser != NULL) {
153 		ucl_parser_free (parser);
154 	}
155 	if (obj != NULL) {
156 		ucl_object_unref (obj);
157 	}
158 
159 	return ret;
160 }
161