xref: /freebsd/contrib/libxo/tests/core/test_01.c (revision bca9d05fdb058aa709621661c2feccae8940d94b)
1 /*
2  * Copyright (c) 2014, 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 
18 int
19 main (int argc, char **argv)
20 {
21     static char base_grocery[] = "GRO";
22     static char base_hardware[] = "HRD";
23     struct item {
24 	const char *i_title;
25 	int i_sold;
26 	int i_instock;
27 	int i_onorder;
28 	const char *i_sku_base;
29 	int i_sku_num;
30     };
31     struct item list[] = {
32 	{ "gum", 1412, 54, 10, base_grocery, 415 },
33 	{ "rope", 85, 4, 2, base_hardware, 212 },
34 	{ "ladder", 0, 2, 1, base_hardware, 517 },
35 	{ "bolt", 4123, 144, 42, base_hardware, 632 },
36 	{ "water", 17, 14, 2, base_grocery, 2331 },
37 	{ NULL, 0, 0, 0, NULL, 0 }
38     };
39     struct item list2[] = {
40 	{ "fish", 1321, 45, 1, base_grocery, 533 },
41 	{ NULL, 0, 0, 0, NULL, 0 }
42     };
43     struct item *ip;
44     xo_info_t info[] = {
45 	{ "in-stock", "number", "Number of items in stock" },
46 	{ "name", "string", "Name of the item" },
47 	{ "on-order", "number", "Number of items on order" },
48 	{ "sku", "string", "Stock Keeping Unit" },
49 	{ "sold", "number", "Number of items sold" },
50 	{ XO_INFO_NULL },
51     };
52 
53     argc = xo_parse_args(argc, argv);
54     if (argc < 0)
55 	return 1;
56 
57     for (argc = 1; argv[argc]; argc++) {
58 	if (strcmp(argv[argc], "xml") == 0)
59 	    xo_set_style(NULL, XO_STYLE_XML);
60 	else if (strcmp(argv[argc], "json") == 0)
61 	    xo_set_style(NULL, XO_STYLE_JSON);
62 	else if (strcmp(argv[argc], "text") == 0)
63 	    xo_set_style(NULL, XO_STYLE_TEXT);
64 	else if (strcmp(argv[argc], "html") == 0)
65 	    xo_set_style(NULL, XO_STYLE_HTML);
66 	else if (strcmp(argv[argc], "pretty") == 0)
67 	    xo_set_flags(NULL, XOF_PRETTY);
68 	else if (strcmp(argv[argc], "xpath") == 0)
69 	    xo_set_flags(NULL, XOF_XPATH);
70 	else if (strcmp(argv[argc], "info") == 0)
71 	    xo_set_flags(NULL, XOF_INFO);
72         else if (strcmp(argv[argc], "error") == 0) {
73             close(-1);
74             xo_err(1, "error detected");
75         }
76     }
77 
78     xo_set_info(NULL, info, -1);
79     xo_set_flags(NULL, XOF_KEYS);
80 
81     xo_open_container_h(NULL, "top");
82 
83     xo_emit("df {:used-percent/%5.0f}{U:%%}\n", (double) 12);
84 
85     xo_emit("{e:kve_start/%#jx}", (uintmax_t) 0xdeadbeef);
86     xo_emit("{e:kve_end/%#jx}", (uintmax_t) 0xcabb1e);
87 
88     xo_emit("testing argument modifier {a:}.{a:}...\n",
89 	    "host", "my-box", "domain", "example.com");
90 
91     xo_emit("testing argument modifier with encoding to {ea:}.{a:}...\n",
92 	    "host", "my-box", "domain", "example.com");
93 
94     xo_emit("{La:} {a:}\n", "Label text", "label", "value");
95 
96     xo_emit_field("Vt", "max-chaos", NULL, NULL, "  very  ");
97     xo_emit_field("V", "min-chaos", "%d", NULL, 42);
98     xo_emit_field("V", "some-chaos", "%d\n", "[%d]", 42);
99 
100     xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");
101 
102     xo_attr("test", "value");
103     xo_open_container("data");
104     xo_open_list("item");
105     xo_attr("test2", "value2");
106 
107     xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
108 	    "{T:On Order/%12s}{T:SKU/%5s}\n");
109 
110     for (ip = list; ip->i_title; ip++) {
111 	xo_open_instance("item");
112 	xo_attr("test3", "value3");
113 
114 	xo_emit("{keq:sku/%s-%u/%s-000-%u}"
115 		"{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"
116 		"{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",
117 		ip->i_sku_base, ip->i_sku_num,
118 		ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,
119 		ip->i_sku_base, ip->i_sku_num);
120 
121 	xo_close_instance("item");
122     }
123 
124     xo_close_list("item");
125     xo_close_container("data");
126 
127     xo_emit("\n\n");
128 
129     xo_open_container("data2");
130     xo_open_list("item");
131 
132     for (ip = list; ip->i_title; ip++) {
133 	xo_open_instance("item");
134 
135 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
136 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
137 	xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
138 		ip->i_sold, ip->i_sold ? ".0" : "");
139 	xo_emit("{P:   }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
140 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
141 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
142 		ip->i_sku_base, ip->i_sku_num);
143 
144 	xo_close_instance("item");
145     }
146 
147     xo_close_list("item");
148     xo_close_container("data2");
149 
150     xo_open_container("data3");
151     xo_open_list("item");
152 
153     for (ip = list2; ip->i_title; ip++) {
154 	xo_open_instance("item");
155 
156 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
157 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
158 	xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
159 		ip->i_sold, ip->i_sold ? ".0" : "");
160 	xo_emit("{P:   }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
161 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
162 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
163 		ip->i_sku_base, ip->i_sku_num);
164 
165 	xo_close_instance("item");
166     }
167 
168     xo_close_list("item");
169     xo_close_container("data3");
170 
171     xo_open_container("data4");
172     xo_open_list("item");
173 
174     for (ip = list; ip->i_title; ip++) {
175 	xo_attr("test4", "value4");
176 	xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
177     }
178 
179     xo_close_list("item");
180     xo_close_container("data4");
181 
182     xo_emit("X{P:}X", "epic fail");
183     xo_emit("X{T:}X", "epic fail");
184     xo_emit("X{N:}X", "epic fail");
185     xo_emit("X{L:}X\n", "epic fail");
186 
187     xo_emit("X{P:        }X{Lwc:Cost}{:cost/%u}\n", 425);
188     xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
189 
190     xo_emit("{e:mode/%s}{e:mode_octal/%s} {t:links/%s} "
191 	    "{t:user/%s}  {t:group/%s}  \n",
192 	    "mode", "octal", "links",
193 	    "user", "group", "extra1", "extra2", "extra3");
194 
195     xo_emit("{e:pre/%s}{t:links/%-*u}{t:post/%-*s}\n", "that", 8, 3, 8, "this");
196 
197     xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} "
198 	    "{t:user/%-*s}  {t:group/%-*s}  \n",
199 	    "/some/file", (int) 0640, 8, 1,
200 	    10, "user", 12, "group");
201 
202     xo_close_container_h(NULL, "top");
203 
204     xo_finish();
205 
206     return 0;
207 }
208