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