xref: /freebsd/contrib/libxo/tests/core/test_01.c (revision 10ff414c)
1 /*
2  * Copyright (c) 2014-2019, Juniper Networks, Inc.
3  * All rights reserved.
4  * This SOFTWARE is licensed under the LICENSE provided in the
5  * ../Copyright file. By downloading, installing, copying, or otherwise
6  * using the SOFTWARE, you agree to be bound by the terms of that
7  * LICENSE.
8  * Phil Shafer, July 2014
9  */
10 
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <unistd.h>
15 
16 #include "xo.h"
17 #include "xo_encoder.h"
18 
19 int
20 main (int argc, char **argv)
21 {
22     static char base_grocery[] = "GRO";
23     static char base_hardware[] = "HRD";
24     struct item {
25 	const char *i_title;
26 	int i_sold;
27 	int i_instock;
28 	int i_onorder;
29 	const char *i_sku_base;
30 	int i_sku_num;
31     };
32     struct item list[] = {
33 	{ "gum", 1412, 54, 10, base_grocery, 415 },
34 	{ "rope", 85, 4, 2, base_hardware, 212 },
35 	{ "ladder", 0, 2, 1, base_hardware, 517 },
36 	{ "bolt", 4123, 144, 42, base_hardware, 632 },
37 	{ "water", 17, 14, 2, base_grocery, 2331 },
38 	{ NULL, 0, 0, 0, NULL, 0 }
39     };
40     struct item list2[] = {
41 	{ "fish", 1321, 45, 1, base_grocery, 533 },
42 	{ NULL, 0, 0, 0, NULL, 0 }
43     };
44     struct item *ip;
45     xo_info_t info[] = {
46 	{ "in-stock", "number", "Number of items in stock" },
47 	{ "name", "string", "Name of the item" },
48 	{ "on-order", "number", "Number of items on order" },
49 	{ "sku", "string", "Stock Keeping Unit" },
50 	{ "sold", "number", "Number of items sold" },
51 	{ XO_INFO_NULL },
52     };
53 
54     char name[] = "test_01.test";  /* test trimming of xo_program */
55     argv[0] = name;
56 
57     argc = xo_parse_args(argc, argv);
58     if (argc < 0)
59 	return 1;
60 
61     for (argc = 1; argv[argc]; argc++) {
62 	if (xo_streq(argv[argc], "xml"))
63 	    xo_set_style(NULL, XO_STYLE_XML);
64 	else if (xo_streq(argv[argc], "json"))
65 	    xo_set_style(NULL, XO_STYLE_JSON);
66 	else if (xo_streq(argv[argc], "text"))
67 	    xo_set_style(NULL, XO_STYLE_TEXT);
68 	else if (xo_streq(argv[argc], "html"))
69 	    xo_set_style(NULL, XO_STYLE_HTML);
70 	else if (xo_streq(argv[argc], "pretty"))
71 	    xo_set_flags(NULL, XOF_PRETTY);
72 	else if (xo_streq(argv[argc], "xpath"))
73 	    xo_set_flags(NULL, XOF_XPATH);
74 	else if (xo_streq(argv[argc], "info"))
75 	    xo_set_flags(NULL, XOF_INFO);
76         else if (xo_streq(argv[argc], "error")) {
77             close(-1);
78             xo_err(1, "error detected");
79         }
80     }
81 
82     xo_set_info(NULL, info, -1);
83     xo_set_flags(NULL, XOF_KEYS);
84 
85     xo_open_container_h(NULL, "top");
86 
87     xo_emit("static {:type/ethernet} {:type/bridge} {:type/%4du} {:type/%3d}",
88 	    18, 24);
89 
90     xo_emit("anchor {[:/%d}{:address/%p}..{:port/%u}{]:}\n", 18, NULL, 1);
91     xo_emit("anchor {[:18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);
92     xo_emit("anchor {[:/18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);
93 
94     xo_emit("df {:used-percent/%5.0f}{U:%%}\n", (double) 12);
95 
96     xo_emit("{e:kve_start/%#jx}", (uintmax_t) 0xdeadbeef);
97     xo_emit("{e:kve_end/%#jx}", (uintmax_t) 0xcabb1e);
98 
99     xo_emit("testing argument modifier {a:}.{a:}...\n",
100 	    "host", "my-box", "domain", "example.com");
101 
102     xo_emit("testing argument modifier with encoding to {ea:}.{a:}...\n",
103 	    "host", "my-box", "domain", "example.com");
104 
105     xo_emit("{La:} {a:}\n", "Label text", "label", "value");
106 
107     xo_emit_field("Vt", "max-chaos", NULL, NULL, "  very  ");
108     xo_emit_field("V", "min-chaos", "%d", NULL, 42);
109     xo_emit_field("V", "some-chaos", "%d\n", "[%d]", 42);
110 
111     xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");
112 
113     xo_attr("test", "value");
114     xo_open_container("data");
115     xo_open_list("item");
116     xo_attr("test2", "value2");
117 
118     xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
119 	    "{T:On Order/%12s}{T:SKU/%5s}\n");
120 
121     for (ip = list; ip->i_title; ip++) {
122 	xo_open_instance("item");
123 	xo_attr("test3", "value3");
124 
125 	xo_emit("{keq:sku/%s-%u/%s-000-%u}"
126 		"{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"
127 		"{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",
128 		ip->i_sku_base, ip->i_sku_num,
129 		ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,
130 		ip->i_sku_base, ip->i_sku_num);
131 
132 	xo_close_instance("item");
133     }
134 
135     xo_close_list("item");
136     xo_close_container("data");
137 
138     xo_emit("\n\n");
139 
140     xo_open_container("data2");
141     xo_open_list("item");
142 
143     for (ip = list; ip->i_title; ip++) {
144 	xo_open_instance("item");
145 
146 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
147 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
148 	xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
149 		ip->i_sold, ip->i_sold ? ".0" : "");
150 	xo_emit("{P:   }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
151 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
152 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
153 		ip->i_sku_base, ip->i_sku_num);
154 
155 	xo_close_instance("item");
156     }
157 
158     xo_close_list("item");
159     xo_close_container("data2");
160 
161     xo_open_container("data3");
162     xo_open_list("item");
163 
164     for (ip = list2; ip->i_title; ip++) {
165 	xo_open_instance("item");
166 
167 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
168 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
169 	xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
170 		ip->i_sold, ip->i_sold ? ".0" : "");
171 	xo_emit("{P:   }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
172 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
173 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
174 		ip->i_sku_base, ip->i_sku_num);
175 
176 	xo_close_instance("item");
177     }
178 
179     xo_close_list("item");
180     xo_close_container("data3");
181 
182     xo_open_container("data4");
183     xo_open_list("item");
184 
185     for (ip = list; ip->i_title; ip++) {
186 	xo_attr("test4", "value4");
187 	xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
188     }
189 
190     xo_close_list("item");
191     xo_close_container("data4");
192 
193     xo_attr("test", "value");
194     xo_open_container("data");
195     xo_open_list("item");
196     xo_attr("test2", "value2");
197 
198     xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
199 	    "{T:On Order/%12s}{T:SKU/%5s}\n");
200 
201     for (ip = list; ip->i_title; ip++) {
202 	xo_open_instance("item");
203 	xo_attr("test3", "value3");
204 
205 	xo_emit("{keq:sku/%s-%u/%s-000-%u}"
206 		"{k:name/%-10s/%s}{n:sold/%12u/%u}",
207 		ip->i_sku_base, ip->i_sku_num,
208 		ip->i_title, ip->i_sold);
209 
210 	if (ip->i_onorder < 5)
211 	    xo_emit("Extra: {:extra}", "special");
212 
213 	if (ip->i_instock & 1)
214 	    xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
215 	xo_emit("{:on-order/%12u/%u}", ip->i_onorder);
216 	if (!(ip->i_instock & 1))
217 	    xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
218 
219 	xo_emit("{qkd:sku/%5s-000-%u/%s-000-%u}\n",
220 		ip->i_sku_base, ip->i_sku_num);
221 
222 	xo_close_instance("item");
223     }
224 
225     xo_close_list("item");
226     xo_close_container("data");
227 
228     xo_emit("\n\n");
229 
230 
231     xo_emit("X{P:}X", "epic fail");
232     xo_emit("X{T:}X", "epic fail");
233     xo_emit("X{N:}X", "epic fail");
234     xo_emit("X{L:}X\n", "epic fail");
235 
236     xo_emit("X{P:        }X{Lwc:Cost}{:cost/%u}\n", 425);
237     xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
238 
239     xo_emit("{e:mode/%s}{e:mode_octal/%s} {t:links/%s} "
240 	    "{t:user/%s}  {t:group/%s}  \n",
241 	    "mode", "octal", "links",
242 	    "user", "group", "extra1", "extra2", "extra3");
243 
244     xo_emit("{e:pre/%s}{t:links/%-*u}{t:post/%-*s}\n", "that", 8, 3, 8, "this");
245 
246     xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} "
247 	    "{t:user/%-*s}  {t:group/%-*s}  \n",
248 	    "/some/file", (int) 0640, 8, 1,
249 	    10, "user", 12, "group");
250 
251     xo_close_container_h(NULL, "top");
252 
253     xo_finish();
254 
255     return 0;
256 }
257