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 <string.h> 13 #include <unistd.h> 14 15 #include "xo.h" 16 17 int 18 main (int argc, char **argv) 19 { 20 static char base_grocery[] = "GRO"; 21 static char base_hardware[] = "HRD"; 22 struct item { 23 const char *i_title; 24 int i_sold; 25 int i_instock; 26 int i_onorder; 27 const char *i_sku_base; 28 int i_sku_num; 29 }; 30 struct item list[] = { 31 { "gum", 1412, 54, 10, base_grocery, 415 }, 32 { "rope", 85, 4, 2, base_hardware, 212 }, 33 { "ladder", 0, 2, 1, base_hardware, 517 }, 34 { "bolt", 4123, 144, 42, base_hardware, 632 }, 35 { "water", 17, 14, 2, base_grocery, 2331 }, 36 { NULL, 0, 0, 0, NULL, 0 } 37 }; 38 struct item list2[] = { 39 { "fish", 1321, 45, 1, base_grocery, 533 }, 40 { NULL, 0, 0, 0, NULL, 0 } 41 }; 42 struct item *ip; 43 xo_info_t info[] = { 44 { "in-stock", "number", "Number of items in stock" }, 45 { "name", "string", "Name of the item" }, 46 { "on-order", "number", "Number of items on order" }, 47 { "sku", "string", "Stock Keeping Unit" }, 48 { "sold", "number", "Number of items sold" }, 49 { XO_INFO_NULL }, 50 }; 51 52 argc = xo_parse_args(argc, argv); 53 if (argc < 0) 54 return 1; 55 56 for (argc = 1; argv[argc]; argc++) { 57 if (strcmp(argv[argc], "xml") == 0) 58 xo_set_style(NULL, XO_STYLE_XML); 59 else if (strcmp(argv[argc], "json") == 0) 60 xo_set_style(NULL, XO_STYLE_JSON); 61 else if (strcmp(argv[argc], "text") == 0) 62 xo_set_style(NULL, XO_STYLE_TEXT); 63 else if (strcmp(argv[argc], "html") == 0) 64 xo_set_style(NULL, XO_STYLE_HTML); 65 else if (strcmp(argv[argc], "pretty") == 0) 66 xo_set_flags(NULL, XOF_PRETTY); 67 else if (strcmp(argv[argc], "xpath") == 0) 68 xo_set_flags(NULL, XOF_XPATH); 69 else if (strcmp(argv[argc], "info") == 0) 70 xo_set_flags(NULL, XOF_INFO); 71 else if (strcmp(argv[argc], "error") == 0) { 72 close(-1); 73 xo_err(1, "error detected"); 74 } 75 } 76 77 xo_set_info(NULL, info, -1); 78 xo_set_flags(NULL, XOF_KEYS); 79 80 xo_open_container_h(NULL, "top"); 81 82 xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com"); 83 84 xo_attr("test", "value"); 85 xo_open_container("data"); 86 xo_open_list("item"); 87 xo_attr("test2", "value2"); 88 89 xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}" 90 "{T:On Order/%12s}{T:SKU/%5s}\n"); 91 92 for (ip = list; ip->i_title; ip++) { 93 xo_open_instance("item"); 94 xo_attr("test3", "value3"); 95 96 xo_emit("{keq:sku/%s-%u/%s-000-%u}" 97 "{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}" 98 "{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n", 99 ip->i_sku_base, ip->i_sku_num, 100 ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder, 101 ip->i_sku_base, ip->i_sku_num); 102 103 xo_close_instance("item"); 104 } 105 106 xo_close_list("item"); 107 xo_close_container("data"); 108 109 xo_emit("\n\n"); 110 111 xo_open_container("data2"); 112 xo_open_list("item"); 113 114 for (ip = list; ip->i_title; ip++) { 115 xo_open_instance("item"); 116 117 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num); 118 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title); 119 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n", 120 ip->i_sold, ip->i_sold ? ".0" : ""); 121 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock); 122 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder); 123 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n", 124 ip->i_sku_base, ip->i_sku_num); 125 126 xo_close_instance("item"); 127 } 128 129 xo_close_list("item"); 130 xo_close_container("data2"); 131 132 xo_open_container("data3"); 133 xo_open_list("item"); 134 135 for (ip = list2; ip->i_title; ip++) { 136 xo_open_instance("item"); 137 138 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num); 139 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title); 140 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n", 141 ip->i_sold, ip->i_sold ? ".0" : ""); 142 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock); 143 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder); 144 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n", 145 ip->i_sku_base, ip->i_sku_num); 146 147 xo_close_instance("item"); 148 } 149 150 xo_close_list("item"); 151 xo_close_container("data3"); 152 153 xo_open_container("data4"); 154 xo_open_list("item"); 155 156 for (ip = list; ip->i_title; ip++) { 157 xo_attr("test4", "value4"); 158 xo_emit("{Lwc:Item}{l:item}\n", ip->i_title); 159 } 160 161 xo_close_list("item"); 162 xo_close_container("data4"); 163 164 xo_emit("X{P:}X", "epic fail"); 165 xo_emit("X{T:}X", "epic fail"); 166 xo_emit("X{N:}X", "epic fail"); 167 xo_emit("X{L:}X\n", "epic fail"); 168 169 xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425); 170 xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455); 171 172 xo_close_container_h(NULL, "top"); 173 174 xo_finish(); 175 176 return 0; 177 } 178