131337658SMarcel Moolenaar /*
25e203a9dSPhil Shafer * Copyright (c) 2014-2019, Juniper Networks, Inc.
331337658SMarcel Moolenaar * All rights reserved.
431337658SMarcel Moolenaar * This SOFTWARE is licensed under the LICENSE provided in the
531337658SMarcel Moolenaar * ../Copyright file. By downloading, installing, copying, or otherwise
631337658SMarcel Moolenaar * using the SOFTWARE, you agree to be bound by the terms of that
731337658SMarcel Moolenaar * LICENSE.
831337658SMarcel Moolenaar * Phil Shafer, July 2014
931337658SMarcel Moolenaar */
1031337658SMarcel Moolenaar
1131337658SMarcel Moolenaar #include <stdlib.h>
128a6eceffSPhil Shafer #include <stdint.h>
1331337658SMarcel Moolenaar #include <string.h>
14545ddfbeSMarcel Moolenaar #include <unistd.h>
1531337658SMarcel Moolenaar
1631337658SMarcel Moolenaar #include "xo.h"
1776afb20cSPhil Shafer #include "xo_encoder.h"
1831337658SMarcel Moolenaar
1931337658SMarcel Moolenaar int
main(int argc,char ** argv)2031337658SMarcel Moolenaar main (int argc, char **argv)
2131337658SMarcel Moolenaar {
2231337658SMarcel Moolenaar static char base_grocery[] = "GRO";
2331337658SMarcel Moolenaar static char base_hardware[] = "HRD";
2431337658SMarcel Moolenaar struct item {
2531337658SMarcel Moolenaar const char *i_title;
2631337658SMarcel Moolenaar int i_sold;
2731337658SMarcel Moolenaar int i_instock;
2831337658SMarcel Moolenaar int i_onorder;
2931337658SMarcel Moolenaar const char *i_sku_base;
3031337658SMarcel Moolenaar int i_sku_num;
3131337658SMarcel Moolenaar };
3231337658SMarcel Moolenaar struct item list[] = {
3331337658SMarcel Moolenaar { "gum", 1412, 54, 10, base_grocery, 415 },
3431337658SMarcel Moolenaar { "rope", 85, 4, 2, base_hardware, 212 },
3531337658SMarcel Moolenaar { "ladder", 0, 2, 1, base_hardware, 517 },
3631337658SMarcel Moolenaar { "bolt", 4123, 144, 42, base_hardware, 632 },
3731337658SMarcel Moolenaar { "water", 17, 14, 2, base_grocery, 2331 },
3831337658SMarcel Moolenaar { NULL, 0, 0, 0, NULL, 0 }
3931337658SMarcel Moolenaar };
4031337658SMarcel Moolenaar struct item list2[] = {
4131337658SMarcel Moolenaar { "fish", 1321, 45, 1, base_grocery, 533 },
4231337658SMarcel Moolenaar { NULL, 0, 0, 0, NULL, 0 }
4331337658SMarcel Moolenaar };
4431337658SMarcel Moolenaar struct item *ip;
4531337658SMarcel Moolenaar xo_info_t info[] = {
4631337658SMarcel Moolenaar { "in-stock", "number", "Number of items in stock" },
4731337658SMarcel Moolenaar { "name", "string", "Name of the item" },
4831337658SMarcel Moolenaar { "on-order", "number", "Number of items on order" },
4931337658SMarcel Moolenaar { "sku", "string", "Stock Keeping Unit" },
5031337658SMarcel Moolenaar { "sold", "number", "Number of items sold" },
51d1a0d267SMarcel Moolenaar { XO_INFO_NULL },
5231337658SMarcel Moolenaar };
5367322d16SPhil Shafer
5467322d16SPhil Shafer char name[] = "test_01.test"; /* test trimming of xo_program */
5567322d16SPhil Shafer argv[0] = name;
5631337658SMarcel Moolenaar
5731337658SMarcel Moolenaar argc = xo_parse_args(argc, argv);
5831337658SMarcel Moolenaar if (argc < 0)
5931337658SMarcel Moolenaar return 1;
6031337658SMarcel Moolenaar
6131337658SMarcel Moolenaar for (argc = 1; argv[argc]; argc++) {
6276afb20cSPhil Shafer if (xo_streq(argv[argc], "xml"))
6331337658SMarcel Moolenaar xo_set_style(NULL, XO_STYLE_XML);
6476afb20cSPhil Shafer else if (xo_streq(argv[argc], "json"))
6531337658SMarcel Moolenaar xo_set_style(NULL, XO_STYLE_JSON);
6676afb20cSPhil Shafer else if (xo_streq(argv[argc], "text"))
6731337658SMarcel Moolenaar xo_set_style(NULL, XO_STYLE_TEXT);
6876afb20cSPhil Shafer else if (xo_streq(argv[argc], "html"))
6931337658SMarcel Moolenaar xo_set_style(NULL, XO_STYLE_HTML);
7076afb20cSPhil Shafer else if (xo_streq(argv[argc], "pretty"))
7131337658SMarcel Moolenaar xo_set_flags(NULL, XOF_PRETTY);
7276afb20cSPhil Shafer else if (xo_streq(argv[argc], "xpath"))
7331337658SMarcel Moolenaar xo_set_flags(NULL, XOF_XPATH);
7476afb20cSPhil Shafer else if (xo_streq(argv[argc], "info"))
7531337658SMarcel Moolenaar xo_set_flags(NULL, XOF_INFO);
7676afb20cSPhil Shafer else if (xo_streq(argv[argc], "error")) {
77545ddfbeSMarcel Moolenaar close(-1);
78545ddfbeSMarcel Moolenaar xo_err(1, "error detected");
79545ddfbeSMarcel Moolenaar }
8031337658SMarcel Moolenaar }
8131337658SMarcel Moolenaar
82d1a0d267SMarcel Moolenaar xo_set_info(NULL, info, -1);
8331337658SMarcel Moolenaar xo_set_flags(NULL, XOF_KEYS);
8431337658SMarcel Moolenaar
85*34b867caSPhil Shafer xo_open_container_h(NULL, "top-level");
8631337658SMarcel Moolenaar
87983afe33SPhil Shafer xo_emit("static {:type/ethernet} {:type/bridge} {:type/%4du} {:type/%3d}",
88983afe33SPhil Shafer 18, 24);
89983afe33SPhil Shafer
90fd5e3f3eSPhil Shafer xo_emit("anchor {[:/%d}{:address/%p}..{:port/%u}{]:}\n", 18, NULL, 1);
91fd5e3f3eSPhil Shafer xo_emit("anchor {[:18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);
92fd5e3f3eSPhil Shafer xo_emit("anchor {[:/18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);
93fd5e3f3eSPhil Shafer
94264104f2SPhil Shafer xo_emit("df {:used-percent/%5.0f}{U:%%}\n", (double) 12);
95264104f2SPhil Shafer
968a6eceffSPhil Shafer xo_emit("{e:kve_start/%#jx}", (uintmax_t) 0xdeadbeef);
978a6eceffSPhil Shafer xo_emit("{e:kve_end/%#jx}", (uintmax_t) 0xcabb1e);
988a6eceffSPhil Shafer
9942ff34c3SPhil Shafer xo_emit("testing argument modifier {a:}.{a:}...\n",
10042ff34c3SPhil Shafer "host", "my-box", "domain", "example.com");
10142ff34c3SPhil Shafer
10242ff34c3SPhil Shafer xo_emit("testing argument modifier with encoding to {ea:}.{a:}...\n",
10342ff34c3SPhil Shafer "host", "my-box", "domain", "example.com");
10442ff34c3SPhil Shafer
10542ff34c3SPhil Shafer xo_emit("{La:} {a:}\n", "Label text", "label", "value");
10642ff34c3SPhil Shafer
107*34b867caSPhil Shafer const char *title = "My Title";
108*34b867caSPhil Shafer xo_emit_field("T", title, "%s\n", NULL, NULL);
109*34b867caSPhil Shafer
11042ff34c3SPhil Shafer xo_emit_field("Vt", "max-chaos", NULL, NULL, " very ");
11142ff34c3SPhil Shafer xo_emit_field("V", "min-chaos", "%d", NULL, 42);
11242ff34c3SPhil Shafer xo_emit_field("V", "some-chaos", "%d\n", "[%d]", 42);
11342ff34c3SPhil Shafer
114*34b867caSPhil Shafer xo_attr("test-attr", "attr-value");
115*34b867caSPhil Shafer xo_emit_field_h(NULL, ",leaf-list,quotes", "sku", "%s-%u", "%s-000-%u",
116*34b867caSPhil Shafer "gum", 1412);
117*34b867caSPhil Shafer
118d1a0d267SMarcel Moolenaar xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");
119d1a0d267SMarcel Moolenaar
120545ddfbeSMarcel Moolenaar xo_attr("test", "value");
12131337658SMarcel Moolenaar xo_open_container("data");
12231337658SMarcel Moolenaar xo_open_list("item");
123545ddfbeSMarcel Moolenaar xo_attr("test2", "value2");
12431337658SMarcel Moolenaar
12531337658SMarcel Moolenaar xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
12631337658SMarcel Moolenaar "{T:On Order/%12s}{T:SKU/%5s}\n");
12731337658SMarcel Moolenaar
12831337658SMarcel Moolenaar for (ip = list; ip->i_title; ip++) {
12931337658SMarcel Moolenaar xo_open_instance("item");
130545ddfbeSMarcel Moolenaar xo_attr("test3", "value3");
13131337658SMarcel Moolenaar
13231337658SMarcel Moolenaar xo_emit("{keq:sku/%s-%u/%s-000-%u}"
13331337658SMarcel Moolenaar "{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"
13431337658SMarcel Moolenaar "{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",
13531337658SMarcel Moolenaar ip->i_sku_base, ip->i_sku_num,
13631337658SMarcel Moolenaar ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,
13731337658SMarcel Moolenaar ip->i_sku_base, ip->i_sku_num);
13831337658SMarcel Moolenaar
13931337658SMarcel Moolenaar xo_close_instance("item");
14031337658SMarcel Moolenaar }
14131337658SMarcel Moolenaar
14231337658SMarcel Moolenaar xo_close_list("item");
14331337658SMarcel Moolenaar xo_close_container("data");
14431337658SMarcel Moolenaar
14531337658SMarcel Moolenaar xo_emit("\n\n");
14631337658SMarcel Moolenaar
147d1a0d267SMarcel Moolenaar xo_open_container("data2");
14831337658SMarcel Moolenaar xo_open_list("item");
14931337658SMarcel Moolenaar
15031337658SMarcel Moolenaar for (ip = list; ip->i_title; ip++) {
15131337658SMarcel Moolenaar xo_open_instance("item");
15231337658SMarcel Moolenaar
15331337658SMarcel Moolenaar xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
15431337658SMarcel Moolenaar xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
15531337658SMarcel Moolenaar xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
15631337658SMarcel Moolenaar ip->i_sold, ip->i_sold ? ".0" : "");
15731337658SMarcel Moolenaar xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
15831337658SMarcel Moolenaar xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
15931337658SMarcel Moolenaar xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
16031337658SMarcel Moolenaar ip->i_sku_base, ip->i_sku_num);
16131337658SMarcel Moolenaar
16231337658SMarcel Moolenaar xo_close_instance("item");
16331337658SMarcel Moolenaar }
16431337658SMarcel Moolenaar
16531337658SMarcel Moolenaar xo_close_list("item");
166d1a0d267SMarcel Moolenaar xo_close_container("data2");
16731337658SMarcel Moolenaar
168d1a0d267SMarcel Moolenaar xo_open_container("data3");
16931337658SMarcel Moolenaar xo_open_list("item");
17031337658SMarcel Moolenaar
17131337658SMarcel Moolenaar for (ip = list2; ip->i_title; ip++) {
17231337658SMarcel Moolenaar xo_open_instance("item");
17331337658SMarcel Moolenaar
17431337658SMarcel Moolenaar xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
17531337658SMarcel Moolenaar xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
17631337658SMarcel Moolenaar xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",
17731337658SMarcel Moolenaar ip->i_sold, ip->i_sold ? ".0" : "");
17831337658SMarcel Moolenaar xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
17931337658SMarcel Moolenaar xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
18031337658SMarcel Moolenaar xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",
18131337658SMarcel Moolenaar ip->i_sku_base, ip->i_sku_num);
18231337658SMarcel Moolenaar
18331337658SMarcel Moolenaar xo_close_instance("item");
18431337658SMarcel Moolenaar }
18531337658SMarcel Moolenaar
18631337658SMarcel Moolenaar xo_close_list("item");
187d1a0d267SMarcel Moolenaar xo_close_container("data3");
18831337658SMarcel Moolenaar
189d1a0d267SMarcel Moolenaar xo_open_container("data4");
190545ddfbeSMarcel Moolenaar xo_open_list("item");
191545ddfbeSMarcel Moolenaar
192545ddfbeSMarcel Moolenaar for (ip = list; ip->i_title; ip++) {
193545ddfbeSMarcel Moolenaar xo_attr("test4", "value4");
194545ddfbeSMarcel Moolenaar xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
195545ddfbeSMarcel Moolenaar }
196545ddfbeSMarcel Moolenaar
197545ddfbeSMarcel Moolenaar xo_close_list("item");
198d1a0d267SMarcel Moolenaar xo_close_container("data4");
199545ddfbeSMarcel Moolenaar
20076afb20cSPhil Shafer xo_attr("test", "value");
20176afb20cSPhil Shafer xo_open_container("data");
20276afb20cSPhil Shafer xo_open_list("item");
20376afb20cSPhil Shafer xo_attr("test2", "value2");
20476afb20cSPhil Shafer
20576afb20cSPhil Shafer xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
20676afb20cSPhil Shafer "{T:On Order/%12s}{T:SKU/%5s}\n");
20776afb20cSPhil Shafer
20876afb20cSPhil Shafer for (ip = list; ip->i_title; ip++) {
20976afb20cSPhil Shafer xo_open_instance("item");
21076afb20cSPhil Shafer xo_attr("test3", "value3");
21176afb20cSPhil Shafer
21276afb20cSPhil Shafer xo_emit("{keq:sku/%s-%u/%s-000-%u}"
21376afb20cSPhil Shafer "{k:name/%-10s/%s}{n:sold/%12u/%u}",
21476afb20cSPhil Shafer ip->i_sku_base, ip->i_sku_num,
21576afb20cSPhil Shafer ip->i_title, ip->i_sold);
21676afb20cSPhil Shafer
21776afb20cSPhil Shafer if (ip->i_onorder < 5)
21876afb20cSPhil Shafer xo_emit("Extra: {:extra}", "special");
21976afb20cSPhil Shafer
22076afb20cSPhil Shafer if (ip->i_instock & 1)
22176afb20cSPhil Shafer xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
22276afb20cSPhil Shafer xo_emit("{:on-order/%12u/%u}", ip->i_onorder);
22376afb20cSPhil Shafer if (!(ip->i_instock & 1))
22476afb20cSPhil Shafer xo_emit("{:in-stock/%12u/%u}", ip->i_instock);
22576afb20cSPhil Shafer
22676afb20cSPhil Shafer xo_emit("{qkd:sku/%5s-000-%u/%s-000-%u}\n",
22776afb20cSPhil Shafer ip->i_sku_base, ip->i_sku_num);
22876afb20cSPhil Shafer
22976afb20cSPhil Shafer xo_close_instance("item");
23076afb20cSPhil Shafer }
23176afb20cSPhil Shafer
23276afb20cSPhil Shafer xo_close_list("item");
23376afb20cSPhil Shafer xo_close_container("data");
23476afb20cSPhil Shafer
23576afb20cSPhil Shafer xo_emit("\n\n");
23676afb20cSPhil Shafer
23776afb20cSPhil Shafer
238545ddfbeSMarcel Moolenaar xo_emit("X{P:}X", "epic fail");
239545ddfbeSMarcel Moolenaar xo_emit("X{T:}X", "epic fail");
240545ddfbeSMarcel Moolenaar xo_emit("X{N:}X", "epic fail");
241545ddfbeSMarcel Moolenaar xo_emit("X{L:}X\n", "epic fail");
242545ddfbeSMarcel Moolenaar
243545ddfbeSMarcel Moolenaar xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425);
244545ddfbeSMarcel Moolenaar xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
245545ddfbeSMarcel Moolenaar
246a321cc5dSPhil Shafer xo_emit("{e:mode/%s}{e:mode_octal/%s} {t:links/%s} "
247a321cc5dSPhil Shafer "{t:user/%s} {t:group/%s} \n",
248a321cc5dSPhil Shafer "mode", "octal", "links",
249a321cc5dSPhil Shafer "user", "group", "extra1", "extra2", "extra3");
250a321cc5dSPhil Shafer
251264104f2SPhil Shafer xo_emit("{e:pre/%s}{t:links/%-*u}{t:post/%-*s}\n", "that", 8, 3, 8, "this");
252264104f2SPhil Shafer
253a321cc5dSPhil Shafer xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} "
254a321cc5dSPhil Shafer "{t:user/%-*s} {t:group/%-*s} \n",
255a321cc5dSPhil Shafer "/some/file", (int) 0640, 8, 1,
256a321cc5dSPhil Shafer 10, "user", 12, "group");
257a321cc5dSPhil Shafer
258*34b867caSPhil Shafer xo_close_container_h(NULL, "top-level");
25931337658SMarcel Moolenaar
26031337658SMarcel Moolenaar xo_finish();
26131337658SMarcel Moolenaar
26231337658SMarcel Moolenaar return 0;
26331337658SMarcel Moolenaar }
264