xref: /freebsd/contrib/libxo/tests/core/test_10.c (revision d6b92ffa)
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 <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <ctype.h>
17 
18 #include "xo.h"
19 
20 int
21 main (int argc, char **argv)
22 {
23     static char base_grocery[] = "GRO";
24     static char base_hardware[] = "HRD";
25     struct item {
26 	const char *i_title;
27 	int i_sold;
28 	int i_instock;
29 	int i_onorder;
30 	const char *i_sku_base;
31 	int i_sku_num;
32     };
33     struct item list[] = {
34 	{ "gum", 1412, 54, 10, base_grocery, 415 },
35 	{ "rope", 85, 4, 2, base_hardware, 212 },
36 	{ "ladder", 0, 2, 1, base_hardware, 517 },
37 	{ "bolt", 4123, 144, 42, base_hardware, 632 },
38 	{ "water", 17, 14, 2, base_grocery, 2331 },
39 	{ NULL, 0, 0, 0, NULL, 0 }
40     };
41     struct item list2[] = {
42 	{ "fish", 1321, 45, 1, base_grocery, 533 },
43 	{ NULL, 0, 0, 0, NULL, 0 }
44     };
45     struct item *ip;
46     xo_info_t info[] = {
47 	{ "in-stock", "number", "Number of items in stock" },
48 	{ "name", "string", "Name of the item" },
49 	{ "on-order", "number", "Number of items on order" },
50 	{ "sku", "string", "Stock Keeping Unit" },
51 	{ "sold", "number", "Number of items sold" },
52 	{ NULL, NULL, NULL },
53     };
54     int info_count = (sizeof(info) / sizeof(info[0])) - 1;
55 
56     argc = xo_parse_args(argc, argv);
57     if (argc < 0)
58 	return 1;
59 
60     for (argc = 1; argv[argc]; argc++) {
61 	if (strcmp(argv[argc], "xml") == 0)
62 	    xo_set_style(NULL, XO_STYLE_XML);
63 	else if (strcmp(argv[argc], "json") == 0)
64 	    xo_set_style(NULL, XO_STYLE_JSON);
65 	else if (strcmp(argv[argc], "text") == 0)
66 	    xo_set_style(NULL, XO_STYLE_TEXT);
67 	else if (strcmp(argv[argc], "html") == 0)
68 	    xo_set_style(NULL, XO_STYLE_HTML);
69 	else if (strcmp(argv[argc], "pretty") == 0)
70 	    xo_set_flags(NULL, XOF_PRETTY);
71 	else if (strcmp(argv[argc], "xpath") == 0)
72 	    xo_set_flags(NULL, XOF_XPATH);
73 	else if (strcmp(argv[argc], "info") == 0)
74 	    xo_set_flags(NULL, XOF_INFO);
75         else if (strcmp(argv[argc], "error") == 0) {
76             close(-1);
77             xo_err(1, "error detected");
78         }
79     }
80 
81     xo_set_info(NULL, info, info_count);
82     xo_set_flags(NULL, XOF_KEYS);
83 
84     /*  Normally one would use "XOF_COLOR_ALLOWED", but we want to force it */
85     xo_set_flags(NULL, XOF_COLOR);
86 
87     xo_set_version("3.1.4");
88 
89     xo_open_container_h(NULL, "top");
90 
91     xo_attr("test", "value");
92     xo_open_container("data");
93     xo_open_list("item");
94     xo_attr("test2", "value2");
95 
96     static const char *colors[] =
97 	{ "blue", "green", "red", "yellow", "default", NULL };
98 
99     int i;
100     for (i = 0; colors[i]; i++) {
101 	if (i > 0)
102 	    xo_emit("{C:/bg-%s}", colors[i-1]);
103 	xo_emit("{C:/fg-%s}{T:/%s}", colors[i], colors[i]);
104     }
105     xo_emit("{C:reset}\n");
106 
107     xo_emit("{C:bold}{:data} {C:underline}{:data} {C:inverse}{:data} "
108 	    "{C:no-bold}{:data} {C:no-inverse}{:data} "
109 	    "{C:no-underline}{:data}\n",
110 	    "bold", "bold-ul", "triple", "inv-ul", "underline", "plain");
111 
112     xo_emit("{T:Item/%-10s}{C:bold,underline}{T:Total Sold/%12s}{C:no-bold}"
113 	    "{T:In Stock/%12s}{C:/%s}"
114 	    "{T:On Order/%12s}{C:normal}{T:SKU/%5s}\n", "inverse");
115 
116 #if 0
117     xo_finish();
118     return 0;
119 #endif
120 
121     for (ip = list; ip->i_title; ip++) {
122 	xo_open_instance("item");
123 	xo_attr("test3", "value3");
124 
125 	xo_emit("{keq:sku/%s-%u/%s-000-%u}"
126 		"{k:name/%-10s/%s}{n:sold/%12u/%u}"
127 		"{C:/%s}{:in-stock/%12u/%u}{C:normal}"
128 		"{C:/fg-%s}{:on-order/%12u/%u}{C:/fg-default}"
129 		"{qkd:sku/%5s-000-%u/%s-000-%u}\n",
130 		ip->i_sku_base, ip->i_sku_num,
131 		ip->i_title, ip->i_sold,
132 		(ip->i_instock < 5) ? "inverse" : "normal", ip->i_instock,
133 		(ip->i_onorder > 5) ? "yellow" : "default", ip->i_onorder,
134 		ip->i_sku_base, ip->i_sku_num);
135 
136 	xo_close_instance("item");
137     }
138 
139     xo_close_list("item");
140     xo_close_container("data");
141 
142     xo_emit("\n\n");
143 
144     xo_open_container("data");
145     xo_open_list("item");
146 
147     for (ip = list; ip->i_title; ip++) {
148 	xo_open_instance("item");
149 
150 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
151 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
152 	xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
153 		ip->i_sold, ip->i_sold ? ".0" : "");
154 	xo_emit("{P:   }{Lcw:In stock}{C:inverse}{:in-stock/%u}{C:}\n",
155 		ip->i_instock);
156 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
157 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
158 		ip->i_sku_base, ip->i_sku_num);
159 
160 	xo_close_instance("item");
161     }
162 
163     xo_close_list("item");
164     xo_close_container("data");
165 
166     xo_open_container("data");
167     xo_open_list("item");
168 
169     for (ip = list2; ip->i_title; ip++) {
170 	xo_open_instance("item");
171 
172 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
173 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
174 	xo_emit("{P:   }{C:bg-blue   , fg-white, bold   }{L:Total sold}: "
175 		"{n:sold/%u%s}{C:}\n",
176 		ip->i_sold, ip->i_sold ? ".0" : "");
177 	xo_emit("{P:   }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
178 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
179 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
180 		ip->i_sku_base, ip->i_sku_num);
181 
182 	xo_close_instance("item");
183     }
184 
185     xo_close_list("item");
186     xo_close_container("data");
187 
188     xo_open_container("data");
189     xo_open_list("item");
190 
191     for (ip = list; ip->i_title; ip++) {
192 	xo_attr("test4", "value4");
193 	xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
194     }
195 
196     xo_close_list("item");
197     xo_close_container("data");
198 
199     xo_emit("X{P:}X", "epic fail");
200     xo_emit("X{T:}X", "epic fail");
201     xo_emit("X{N:}X", "epic fail");
202     xo_emit("X{L:}X\n", "epic fail");
203 
204     xo_emit("X{P:        }X{Lwc:Cost}{:cost/%u}\n", 425);
205     xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
206 
207     xo_close_container_h(NULL, "top");
208 
209     xo_finish();
210 
211     return 0;
212 }
213