2 * Copyright (c) 2014, 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
18 main (int argc, char **argv)
20 static char base_grocery[] = "GRO";
21 static char base_hardware[] = "HRD";
27 const char *i_sku_base;
30 struct item list[] = {
31 { "gum", 1412, 54, 10, base_grocery, 415 },
32 { "rope", 85, 4, 2, base_hardware, 212 },
33 { "ladder", 0, 2, 1, base_hardware, 517 },
34 { "bolt", 4123, 144, 42, base_hardware, 632 },
35 { "water", 17, 14, 2, base_grocery, 2331 },
36 { NULL, 0, 0, 0, NULL, 0 }
38 struct item list2[] = {
39 { "fish", 1321, 45, 1, base_grocery, 533 },
40 { NULL, 0, 0, 0, NULL, 0 }
44 { "in-stock", "number", "Number of items in stock" },
45 { "name", "string", "Name of the item" },
46 { "on-order", "number", "Number of items on order" },
47 { "sku", "string", "Stock Keeping Unit" },
48 { "sold", "number", "Number of items sold" },
52 argc = xo_parse_args(argc, argv);
56 for (argc = 1; argv[argc]; argc++) {
57 if (strcmp(argv[argc], "xml") == 0)
58 xo_set_style(NULL, XO_STYLE_XML);
59 else if (strcmp(argv[argc], "json") == 0)
60 xo_set_style(NULL, XO_STYLE_JSON);
61 else if (strcmp(argv[argc], "text") == 0)
62 xo_set_style(NULL, XO_STYLE_TEXT);
63 else if (strcmp(argv[argc], "html") == 0)
64 xo_set_style(NULL, XO_STYLE_HTML);
65 else if (strcmp(argv[argc], "pretty") == 0)
66 xo_set_flags(NULL, XOF_PRETTY);
67 else if (strcmp(argv[argc], "xpath") == 0)
68 xo_set_flags(NULL, XOF_XPATH);
69 else if (strcmp(argv[argc], "info") == 0)
70 xo_set_flags(NULL, XOF_INFO);
71 else if (strcmp(argv[argc], "error") == 0) {
73 xo_err(1, "error detected");
77 xo_set_info(NULL, info, -1);
78 xo_set_flags(NULL, XOF_KEYS);
80 xo_open_container_h(NULL, "top");
82 xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");
84 xo_attr("test", "value");
85 xo_open_container("data");
87 xo_attr("test2", "value2");
89 xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
90 "{T:On Order/%12s}{T:SKU/%5s}\n");
92 for (ip = list; ip->i_title; ip++) {
93 xo_open_instance("item");
94 xo_attr("test3", "value3");
96 xo_emit("{keq:sku/%s-%u/%s-000-%u}"
97 "{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"
98 "{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",
99 ip->i_sku_base, ip->i_sku_num,
100 ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,
101 ip->i_sku_base, ip->i_sku_num);
103 xo_close_instance("item");
106 xo_close_list("item");
107 xo_close_container("data");
111 xo_open_container("data2");
112 xo_open_list("item");
114 for (ip = list; ip->i_title; ip++) {
115 xo_open_instance("item");
117 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
118 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
119 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
120 ip->i_sold, ip->i_sold ? ".0" : "");
121 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
122 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
123 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
124 ip->i_sku_base, ip->i_sku_num);
126 xo_close_instance("item");
129 xo_close_list("item");
130 xo_close_container("data2");
132 xo_open_container("data3");
133 xo_open_list("item");
135 for (ip = list2; ip->i_title; ip++) {
136 xo_open_instance("item");
138 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
139 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
140 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
141 ip->i_sold, ip->i_sold ? ".0" : "");
142 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
143 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
144 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
145 ip->i_sku_base, ip->i_sku_num);
147 xo_close_instance("item");
150 xo_close_list("item");
151 xo_close_container("data3");
153 xo_open_container("data4");
154 xo_open_list("item");
156 for (ip = list; ip->i_title; ip++) {
157 xo_attr("test4", "value4");
158 xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
161 xo_close_list("item");
162 xo_close_container("data4");
164 xo_emit("X{P:}X", "epic fail");
165 xo_emit("X{T:}X", "epic fail");
166 xo_emit("X{N:}X", "epic fail");
167 xo_emit("X{L:}X\n", "epic fail");
169 xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425);
170 xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
172 xo_close_container_h(NULL, "top");