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("testing argument modifier {a:}.{a:}...\n", 83 "host", "my-box", "domain", "example.com"); 84 85 xo_emit("testing argument modifier with encoding to {ea:}.{a:}...\n", 86 "host", "my-box", "domain", "example.com"); 87 88 xo_emit("{La:} {a:}\n", "Label text", "label", "value"); 89 90 xo_emit_field("Vt", "max-chaos", NULL, NULL, " very "); 91 xo_emit_field("V", "min-chaos", "%d", NULL, 42); 92 xo_emit_field("V", "some-chaos", "%d\n", "[%d]", 42); 93 94 xo_emit("Connecting to {:host}.{:domain}...\n", "my-box", "example.com"); 95 96 xo_attr("test", "value"); 97 xo_open_container("data"); 98 xo_open_list("item"); 99 xo_attr("test2", "value2"); 100 101 xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}" 102 "{T:On Order/%12s}{T:SKU/%5s}\n"); 103 104 for (ip = list; ip->i_title; ip++) { 105 xo_open_instance("item"); 106 xo_attr("test3", "value3"); 107 108 xo_emit("{keq:sku/%s-%u/%s-000-%u}" 109 "{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}" 110 "{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n", 111 ip->i_sku_base, ip->i_sku_num, 112 ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder, 113 ip->i_sku_base, ip->i_sku_num); 114 115 xo_close_instance("item"); 116 } 117 118 xo_close_list("item"); 119 xo_close_container("data"); 120 121 xo_emit("\n\n"); 122 123 xo_open_container("data2"); 124 xo_open_list("item"); 125 126 for (ip = list; ip->i_title; ip++) { 127 xo_open_instance("item"); 128 129 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num); 130 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title); 131 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n", 132 ip->i_sold, ip->i_sold ? ".0" : ""); 133 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock); 134 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder); 135 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n", 136 ip->i_sku_base, ip->i_sku_num); 137 138 xo_close_instance("item"); 139 } 140 141 xo_close_list("item"); 142 xo_close_container("data2"); 143 144 xo_open_container("data3"); 145 xo_open_list("item"); 146 147 for (ip = list2; 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}{:in-stock/%u}\n", ip->i_instock); 155 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder); 156 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n", 157 ip->i_sku_base, ip->i_sku_num); 158 159 xo_close_instance("item"); 160 } 161 162 xo_close_list("item"); 163 xo_close_container("data3"); 164 165 xo_open_container("data4"); 166 xo_open_list("item"); 167 168 for (ip = list; ip->i_title; ip++) { 169 xo_attr("test4", "value4"); 170 xo_emit("{Lwc:Item}{l:item}\n", ip->i_title); 171 } 172 173 xo_close_list("item"); 174 xo_close_container("data4"); 175 176 xo_emit("X{P:}X", "epic fail"); 177 xo_emit("X{T:}X", "epic fail"); 178 xo_emit("X{N:}X", "epic fail"); 179 xo_emit("X{L:}X\n", "epic fail"); 180 181 xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425); 182 xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455); 183 184 xo_emit("{e:mode/%s}{e:mode_octal/%s} {t:links/%s} " 185 "{t:user/%s} {t:group/%s} \n", 186 "mode", "octal", "links", 187 "user", "group", "extra1", "extra2", "extra3"); 188 189 xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} " 190 "{t:user/%-*s} {t:group/%-*s} \n", 191 "/some/file", (int) 0640, 8, 1, 192 10, "user", 12, "group"); 193 194 xo_close_container_h(NULL, "top"); 195 196 xo_finish(); 197 198 return 0; 199 } 200