Path: blob/main/contrib/libxo/tests/core/test_01.c
39537 views
/*1* Copyright (c) 2014-2019, Juniper Networks, Inc.2* All rights reserved.3* This SOFTWARE is licensed under the LICENSE provided in the4* ../Copyright file. By downloading, installing, copying, or otherwise5* using the SOFTWARE, you agree to be bound by the terms of that6* LICENSE.7* Phil Shafer, July 20148*/910#include <stdlib.h>11#include <stdint.h>12#include <string.h>13#include <unistd.h>1415#include "xo.h"16#include "xo_encoder.h"1718int19main (int argc, char **argv)20{21static char base_grocery[] = "GRO";22static char base_hardware[] = "HRD";23struct item {24const char *i_title;25int i_sold;26int i_instock;27int i_onorder;28const char *i_sku_base;29int i_sku_num;30};31struct 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};39struct item list2[] = {40{ "fish", 1321, 45, 1, base_grocery, 533 },41{ NULL, 0, 0, 0, NULL, 0 }42};43struct item *ip;44xo_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};5253char name[] = "test_01.test"; /* test trimming of xo_program */54argv[0] = name;5556argc = xo_parse_args(argc, argv);57if (argc < 0)58return 1;5960for (argc = 1; argv[argc]; argc++) {61if (xo_streq(argv[argc], "xml"))62xo_set_style(NULL, XO_STYLE_XML);63else if (xo_streq(argv[argc], "json"))64xo_set_style(NULL, XO_STYLE_JSON);65else if (xo_streq(argv[argc], "text"))66xo_set_style(NULL, XO_STYLE_TEXT);67else if (xo_streq(argv[argc], "html"))68xo_set_style(NULL, XO_STYLE_HTML);69else if (xo_streq(argv[argc], "pretty"))70xo_set_flags(NULL, XOF_PRETTY);71else if (xo_streq(argv[argc], "xpath"))72xo_set_flags(NULL, XOF_XPATH);73else if (xo_streq(argv[argc], "info"))74xo_set_flags(NULL, XOF_INFO);75else if (xo_streq(argv[argc], "error")) {76close(-1);77xo_err(1, "error detected");78}79}8081xo_set_info(NULL, info, -1);82xo_set_flags(NULL, XOF_KEYS);8384xo_open_container_h(NULL, "top-level");8586xo_emit("static {:type/ethernet} {:type/bridge} {:type/%4du} {:type/%3d}",8718, 24);8889xo_emit("anchor {[:/%d}{:address/%p}..{:port/%u}{]:}\n", 18, NULL, 1);90xo_emit("anchor {[:18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);91xo_emit("anchor {[:/18}{:address/%p}..{:port/%u}{]:}\n", NULL, 1);9293xo_emit("df {:used-percent/%5.0f}{U:%%}\n", (double) 12);9495xo_emit("{e:kve_start/%#jx}", (uintmax_t) 0xdeadbeef);96xo_emit("{e:kve_end/%#jx}", (uintmax_t) 0xcabb1e);9798xo_emit("testing argument modifier {a:}.{a:}...\n",99"host", "my-box", "domain", "example.com");100101xo_emit("testing argument modifier with encoding to {ea:}.{a:}...\n",102"host", "my-box", "domain", "example.com");103104xo_emit("{La:} {a:}\n", "Label text", "label", "value");105106const char *title = "My Title";107xo_emit_field("T", title, "%s\n", NULL, NULL);108109xo_emit_field("Vt", "max-chaos", NULL, NULL, " very ");110xo_emit_field("V", "min-chaos", "%d", NULL, 42);111xo_emit_field("V", "some-chaos", "%d\n", "[%d]", 42);112113xo_attr("test-attr", "attr-value");114xo_emit_field_h(NULL, ",leaf-list,quotes", "sku", "%s-%u", "%s-000-%u",115"gum", 1412);116117xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com");118119xo_attr("test", "value");120xo_open_container("data");121xo_open_list("item");122xo_attr("test2", "value2");123124xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"125"{T:On Order/%12s}{T:SKU/%5s}\n");126127for (ip = list; ip->i_title; ip++) {128xo_open_instance("item");129xo_attr("test3", "value3");130131xo_emit("{keq:sku/%s-%u/%s-000-%u}"132"{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"133"{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",134ip->i_sku_base, ip->i_sku_num,135ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,136ip->i_sku_base, ip->i_sku_num);137138xo_close_instance("item");139}140141xo_close_list("item");142xo_close_container("data");143144xo_emit("\n\n");145146xo_open_container("data2");147xo_open_list("item");148149for (ip = list; ip->i_title; ip++) {150xo_open_instance("item");151152xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);153xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);154xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",155ip->i_sold, ip->i_sold ? ".0" : "");156xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);157xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);158xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",159ip->i_sku_base, ip->i_sku_num);160161xo_close_instance("item");162}163164xo_close_list("item");165xo_close_container("data2");166167xo_open_container("data3");168xo_open_list("item");169170for (ip = list2; ip->i_title; ip++) {171xo_open_instance("item");172173xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);174xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);175xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",176ip->i_sold, ip->i_sold ? ".0" : "");177xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);178xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);179xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",180ip->i_sku_base, ip->i_sku_num);181182xo_close_instance("item");183}184185xo_close_list("item");186xo_close_container("data3");187188xo_open_container("data4");189xo_open_list("item");190191for (ip = list; ip->i_title; ip++) {192xo_attr("test4", "value4");193xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);194}195196xo_close_list("item");197xo_close_container("data4");198199xo_attr("test", "value");200xo_open_container("data");201xo_open_list("item");202xo_attr("test2", "value2");203204xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"205"{T:On Order/%12s}{T:SKU/%5s}\n");206207for (ip = list; ip->i_title; ip++) {208xo_open_instance("item");209xo_attr("test3", "value3");210211xo_emit("{keq:sku/%s-%u/%s-000-%u}"212"{k:name/%-10s/%s}{n:sold/%12u/%u}",213ip->i_sku_base, ip->i_sku_num,214ip->i_title, ip->i_sold);215216if (ip->i_onorder < 5)217xo_emit("Extra: {:extra}", "special");218219if (ip->i_instock & 1)220xo_emit("{:in-stock/%12u/%u}", ip->i_instock);221xo_emit("{:on-order/%12u/%u}", ip->i_onorder);222if (!(ip->i_instock & 1))223xo_emit("{:in-stock/%12u/%u}", ip->i_instock);224225xo_emit("{qkd:sku/%5s-000-%u/%s-000-%u}\n",226ip->i_sku_base, ip->i_sku_num);227228xo_close_instance("item");229}230231xo_close_list("item");232xo_close_container("data");233234xo_emit("\n\n");235236237xo_emit("X{P:}X", "epic fail");238xo_emit("X{T:}X", "epic fail");239xo_emit("X{N:}X", "epic fail");240xo_emit("X{L:}X\n", "epic fail");241242xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425);243xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);244245xo_emit("{e:mode/%s}{e:mode_octal/%s} {t:links/%s} "246"{t:user/%s} {t:group/%s} \n",247"mode", "octal", "links",248"user", "group", "extra1", "extra2", "extra3");249250xo_emit("{e:pre/%s}{t:links/%-*u}{t:post/%-*s}\n", "that", 8, 3, 8, "this");251252xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} "253"{t:user/%-*s} {t:group/%-*s} \n",254"/some/file", (int) 0640, 8, 1,25510, "user", 12, "group");256257xo_close_container_h(NULL, "top-level");258259xo_finish();260261return 0;262}263264265