]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libxo/tests/core/test_01.c
Import libxo-1.3.0:
[FreeBSD/FreeBSD.git] / contrib / libxo / tests / core / test_01.c
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     argc = xo_parse_args(argc, argv);
55     if (argc < 0)
56         return 1;
57
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")) {
74             close(-1);
75             xo_err(1, "error detected");
76         }
77     }
78
79     xo_set_info(NULL, info, -1);
80     xo_set_flags(NULL, XOF_KEYS);
81
82     xo_open_container_h(NULL, "top");
83
84     xo_emit("static {:type/ethernet} {:type/bridge} {:type/%4du} {:type/%3d}",
85             18, 24);
86
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);
90
91     xo_emit("df {:used-percent/%5.0f}{U:%%}\n", (double) 12);
92
93     xo_emit("{e:kve_start/%#jx}", (uintmax_t) 0xdeadbeef);
94     xo_emit("{e:kve_end/%#jx}", (uintmax_t) 0xcabb1e);
95
96     xo_emit("testing argument modifier {a:}.{a:}...\n",
97             "host", "my-box", "domain", "example.com");
98
99     xo_emit("testing argument modifier with encoding to {ea:}.{a:}...\n",
100             "host", "my-box", "domain", "example.com");
101
102     xo_emit("{La:} {a:}\n", "Label text", "label", "value");
103
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);
107
108     xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");
109
110     xo_attr("test", "value");
111     xo_open_container("data");
112     xo_open_list("item");
113     xo_attr("test2", "value2");
114
115     xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
116             "{T:On Order/%12s}{T:SKU/%5s}\n");
117
118     for (ip = list; ip->i_title; ip++) {
119         xo_open_instance("item");
120         xo_attr("test3", "value3");
121
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);
128
129         xo_close_instance("item");
130     }
131
132     xo_close_list("item");
133     xo_close_container("data");
134
135     xo_emit("\n\n");
136
137     xo_open_container("data2");
138     xo_open_list("item");
139
140     for (ip = list; ip->i_title; ip++) {
141         xo_open_instance("item");
142
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);
151
152         xo_close_instance("item");
153     }
154
155     xo_close_list("item");
156     xo_close_container("data2");
157
158     xo_open_container("data3");
159     xo_open_list("item");
160
161     for (ip = list2; ip->i_title; ip++) {
162         xo_open_instance("item");
163
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);
172
173         xo_close_instance("item");
174     }
175
176     xo_close_list("item");
177     xo_close_container("data3");
178
179     xo_open_container("data4");
180     xo_open_list("item");
181
182     for (ip = list; ip->i_title; ip++) {
183         xo_attr("test4", "value4");
184         xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
185     }
186
187     xo_close_list("item");
188     xo_close_container("data4");
189
190     xo_attr("test", "value");
191     xo_open_container("data");
192     xo_open_list("item");
193     xo_attr("test2", "value2");
194
195     xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
196             "{T:On Order/%12s}{T:SKU/%5s}\n");
197
198     for (ip = list; ip->i_title; ip++) {
199         xo_open_instance("item");
200         xo_attr("test3", "value3");
201
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);
206
207         if (ip->i_onorder < 5)
208             xo_emit("Extra: {:extra}", "special");
209
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);
215
216         xo_emit("{qkd:sku/%5s-000-%u/%s-000-%u}\n",
217                 ip->i_sku_base, ip->i_sku_num);
218
219         xo_close_instance("item");
220     }
221
222     xo_close_list("item");
223     xo_close_container("data");
224
225     xo_emit("\n\n");
226
227
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");
232
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);
235
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");
240
241     xo_emit("{e:pre/%s}{t:links/%-*u}{t:post/%-*s}\n", "that", 8, 3, 8, "this");
242
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");
247
248     xo_close_container_h(NULL, "top");
249
250     xo_finish();
251
252     return 0;
253 }