Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/libxo/tests/core/test_01.c
39537 views
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-level");
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
const char *title = "My Title";
108
xo_emit_field("T", title, "%s\n", NULL, NULL);
109
110
xo_emit_field("Vt", "max-chaos", NULL, NULL, " very ");
111
xo_emit_field("V", "min-chaos", "%d", NULL, 42);
112
xo_emit_field("V", "some-chaos", "%d\n", "[%d]", 42);
113
114
xo_attr("test-attr", "attr-value");
115
xo_emit_field_h(NULL, ",leaf-list,quotes", "sku", "%s-%u", "%s-000-%u",
116
"gum", 1412);
117
118
xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");
119
120
xo_attr("test", "value");
121
xo_open_container("data");
122
xo_open_list("item");
123
xo_attr("test2", "value2");
124
125
xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
126
"{T:On Order/%12s}{T:SKU/%5s}\n");
127
128
for (ip = list; ip->i_title; ip++) {
129
xo_open_instance("item");
130
xo_attr("test3", "value3");
131
132
xo_emit("{keq:sku/%s-%u/%s-000-%u}"
133
"{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"
134
"{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",
135
ip->i_sku_base, ip->i_sku_num,
136
ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,
137
ip->i_sku_base, ip->i_sku_num);
138
139
xo_close_instance("item");
140
}
141
142
xo_close_list("item");
143
xo_close_container("data");
144
145
xo_emit("\n\n");
146
147
xo_open_container("data2");
148
xo_open_list("item");
149
150
for (ip = list; ip->i_title; ip++) {
151
xo_open_instance("item");
152
153
xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
154
xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
155
xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
156
ip->i_sold, ip->i_sold ? ".0" : "");
157
xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
158
xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
159
xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
160
ip->i_sku_base, ip->i_sku_num);
161
162
xo_close_instance("item");
163
}
164
165
xo_close_list("item");
166
xo_close_container("data2");
167
168
xo_open_container("data3");
169
xo_open_list("item");
170
171
for (ip = list2; ip->i_title; ip++) {
172
xo_open_instance("item");
173
174
xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
175
xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
176
xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
177
ip->i_sold, ip->i_sold ? ".0" : "");
178
xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
179
xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
180
xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
181
ip->i_sku_base, ip->i_sku_num);
182
183
xo_close_instance("item");
184
}
185
186
xo_close_list("item");
187
xo_close_container("data3");
188
189
xo_open_container("data4");
190
xo_open_list("item");
191
192
for (ip = list; ip->i_title; ip++) {
193
xo_attr("test4", "value4");
194
xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
195
}
196
197
xo_close_list("item");
198
xo_close_container("data4");
199
200
xo_attr("test", "value");
201
xo_open_container("data");
202
xo_open_list("item");
203
xo_attr("test2", "value2");
204
205
xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
206
"{T:On Order/%12s}{T:SKU/%5s}\n");
207
208
for (ip = list; ip->i_title; ip++) {
209
xo_open_instance("item");
210
xo_attr("test3", "value3");
211
212
xo_emit("{keq:sku/%s-%u/%s-000-%u}"
213
"{k:name/%-10s/%s}{n:sold/%12u/%u}",
214
ip->i_sku_base, ip->i_sku_num,
215
ip->i_title, ip->i_sold);
216
217
if (ip->i_onorder < 5)
218
xo_emit("Extra: {:extra}", "special");
219
220
if (ip->i_instock & 1)
221
xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
222
xo_emit("{:on-order/%12u/%u}", ip->i_onorder);
223
if (!(ip->i_instock & 1))
224
xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
225
226
xo_emit("{qkd:sku/%5s-000-%u/%s-000-%u}\n",
227
ip->i_sku_base, ip->i_sku_num);
228
229
xo_close_instance("item");
230
}
231
232
xo_close_list("item");
233
xo_close_container("data");
234
235
xo_emit("\n\n");
236
237
238
xo_emit("X{P:}X", "epic fail");
239
xo_emit("X{T:}X", "epic fail");
240
xo_emit("X{N:}X", "epic fail");
241
xo_emit("X{L:}X\n", "epic fail");
242
243
xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425);
244
xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
245
246
xo_emit("{e:mode/%s}{e:mode_octal/%s} {t:links/%s} "
247
"{t:user/%s} {t:group/%s} \n",
248
"mode", "octal", "links",
249
"user", "group", "extra1", "extra2", "extra3");
250
251
xo_emit("{e:pre/%s}{t:links/%-*u}{t:post/%-*s}\n", "that", 8, 3, 8, "this");
252
253
xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} "
254
"{t:user/%-*s} {t:group/%-*s} \n",
255
"/some/file", (int) 0640, 8, 1,
256
10, "user", 12, "group");
257
258
xo_close_container_h(NULL, "top-level");
259
260
xo_finish();
261
262
return 0;
263
}
264
265