2 * Copyright (c) 2014-2019, Juniper Networks, Inc.
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
8 * Phil Shafer, July 2014
17 #include "xo_encoder.h"
20 main (int argc, char **argv)
22 static char base_grocery[] = "GRO";
23 static char base_hardware[] = "HRD";
29 const char *i_sku_base;
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 }
40 struct item list2[] = {
41 { "fish", 1321, 45, 1, base_grocery, 533 },
42 { NULL, 0, 0, 0, NULL, 0 }
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" },
54 argc = xo_parse_args(argc, argv);
58 for (argc = 1; argv[argc]; argc++) {
59 if (xo_streq(argv[argc], "xml"))
60 xo_set_style(NULL, XO_STYLE_XML);
61 else if (xo_streq(argv[argc], "json"))
62 xo_set_style(NULL, XO_STYLE_JSON);
63 else if (xo_streq(argv[argc], "text"))
64 xo_set_style(NULL, XO_STYLE_TEXT);
65 else if (xo_streq(argv[argc], "html"))
66 xo_set_style(NULL, XO_STYLE_HTML);
67 else if (xo_streq(argv[argc], "pretty"))
68 xo_set_flags(NULL, XOF_PRETTY);
69 else if (xo_streq(argv[argc], "xpath"))
70 xo_set_flags(NULL, XOF_XPATH);
71 else if (xo_streq(argv[argc], "info"))
72 xo_set_flags(NULL, XOF_INFO);
73 else if (xo_streq(argv[argc], "error")) {
75 xo_err(1, "error detected");
79 xo_set_info(NULL, info, -1);
80 xo_set_flags(NULL, XOF_KEYS);
82 xo_open_container_h(NULL, "top");
84 xo_emit("static {:type/ethernet} {:type/bridge} {:type/%4du} {:type/%3d}",
87 xo_emit("anchor {[:/%d}{:address/%p}..{:port/%u}{]:}\n", 18, NULL, 1);
88 xo_emit("anchor {[:18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);
89 xo_emit("anchor {[:/18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);
91 xo_emit("df {:used-percent/%5.0f}{U:%%}\n", (double) 12);
93 xo_emit("{e:kve_start/%#jx}", (uintmax_t) 0xdeadbeef);
94 xo_emit("{e:kve_end/%#jx}", (uintmax_t) 0xcabb1e);
96 xo_emit("testing argument modifier {a:}.{a:}...\n",
97 "host", "my-box", "domain", "example.com");
99 xo_emit("testing argument modifier with encoding to {ea:}.{a:}...\n",
100 "host", "my-box", "domain", "example.com");
102 xo_emit("{La:} {a:}\n", "Label text", "label", "value");
104 xo_emit_field("Vt", "max-chaos", NULL, NULL, " very ");
105 xo_emit_field("V", "min-chaos", "%d", NULL, 42);
106 xo_emit_field("V", "some-chaos", "%d\n", "[%d]", 42);
108 xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");
110 xo_attr("test", "value");
111 xo_open_container("data");
112 xo_open_list("item");
113 xo_attr("test2", "value2");
115 xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
116 "{T:On Order/%12s}{T:SKU/%5s}\n");
118 for (ip = list; ip->i_title; ip++) {
119 xo_open_instance("item");
120 xo_attr("test3", "value3");
122 xo_emit("{keq:sku/%s-%u/%s-000-%u}"
123 "{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"
124 "{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",
125 ip->i_sku_base, ip->i_sku_num,
126 ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,
127 ip->i_sku_base, ip->i_sku_num);
129 xo_close_instance("item");
132 xo_close_list("item");
133 xo_close_container("data");
137 xo_open_container("data2");
138 xo_open_list("item");
140 for (ip = list; ip->i_title; ip++) {
141 xo_open_instance("item");
143 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
144 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
145 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
146 ip->i_sold, ip->i_sold ? ".0" : "");
147 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
148 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
149 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
150 ip->i_sku_base, ip->i_sku_num);
152 xo_close_instance("item");
155 xo_close_list("item");
156 xo_close_container("data2");
158 xo_open_container("data3");
159 xo_open_list("item");
161 for (ip = list2; ip->i_title; ip++) {
162 xo_open_instance("item");
164 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
165 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
166 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
167 ip->i_sold, ip->i_sold ? ".0" : "");
168 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
169 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
170 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
171 ip->i_sku_base, ip->i_sku_num);
173 xo_close_instance("item");
176 xo_close_list("item");
177 xo_close_container("data3");
179 xo_open_container("data4");
180 xo_open_list("item");
182 for (ip = list; ip->i_title; ip++) {
183 xo_attr("test4", "value4");
184 xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
187 xo_close_list("item");
188 xo_close_container("data4");
190 xo_attr("test", "value");
191 xo_open_container("data");
192 xo_open_list("item");
193 xo_attr("test2", "value2");
195 xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
196 "{T:On Order/%12s}{T:SKU/%5s}\n");
198 for (ip = list; ip->i_title; ip++) {
199 xo_open_instance("item");
200 xo_attr("test3", "value3");
202 xo_emit("{keq:sku/%s-%u/%s-000-%u}"
203 "{k:name/%-10s/%s}{n:sold/%12u/%u}",
204 ip->i_sku_base, ip->i_sku_num,
205 ip->i_title, ip->i_sold);
207 if (ip->i_onorder < 5)
208 xo_emit("Extra: {:extra}", "special");
210 if (ip->i_instock & 1)
211 xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
212 xo_emit("{:on-order/%12u/%u}", ip->i_onorder);
213 if (!(ip->i_instock & 1))
214 xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
216 xo_emit("{qkd:sku/%5s-000-%u/%s-000-%u}\n",
217 ip->i_sku_base, ip->i_sku_num);
219 xo_close_instance("item");
222 xo_close_list("item");
223 xo_close_container("data");
228 xo_emit("X{P:}X", "epic fail");
229 xo_emit("X{T:}X", "epic fail");
230 xo_emit("X{N:}X", "epic fail");
231 xo_emit("X{L:}X\n", "epic fail");
233 xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425);
234 xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
236 xo_emit("{e:mode/%s}{e:mode_octal/%s} {t:links/%s} "
237 "{t:user/%s} {t:group/%s} \n",
238 "mode", "octal", "links",
239 "user", "group", "extra1", "extra2", "extra3");
241 xo_emit("{e:pre/%s}{t:links/%-*u}{t:post/%-*s}\n", "that", 8, 3, 8, "this");
243 xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} "
244 "{t:user/%-*s} {t:group/%-*s} \n",
245 "/some/file", (int) 0640, 8, 1,
246 10, "user", 12, "group");
248 xo_close_container_h(NULL, "top");