1 /* 2 * Copyright (c) 2015, 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 2015 9 */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <unistd.h> 15 #include <errno.h> 16 17 #include "xo.h" 18 19 int 20 main (int argc, char **argv) 21 { 22 struct item { 23 const char *i_title; 24 int i_count; 25 }; 26 struct item list[] = { 27 { "gum", 1412 }, 28 { "rope", 85 }, 29 { "ladder", 0 }, 30 { "bolt", 4123 }, 31 { "water", 17 }, 32 { NULL, 0 } 33 }; 34 struct item *ip; 35 int i; 36 37 argc = xo_parse_args(argc, argv); 38 if (argc < 0) 39 return 1; 40 41 for (argc = 1; argv[argc]; argc++) { 42 if (strcmp(argv[argc], "xml") == 0) 43 xo_set_style(NULL, XO_STYLE_XML); 44 else if (strcmp(argv[argc], "json") == 0) 45 xo_set_style(NULL, XO_STYLE_JSON); 46 else if (strcmp(argv[argc], "text") == 0) 47 xo_set_style(NULL, XO_STYLE_TEXT); 48 else if (strcmp(argv[argc], "html") == 0) 49 xo_set_style(NULL, XO_STYLE_HTML); 50 else if (strcmp(argv[argc], "pretty") == 0) 51 xo_set_flags(NULL, XOF_PRETTY); 52 else if (strcmp(argv[argc], "xpath") == 0) 53 xo_set_flags(NULL, XOF_XPATH); 54 else if (strcmp(argv[argc], "info") == 0) 55 xo_set_flags(NULL, XOF_INFO); 56 else if (strcmp(argv[argc], "error") == 0) { 57 close(-1); 58 xo_err(1, "error detected"); 59 } 60 } 61 62 xo_set_flags(NULL, XOF_KEYS); 63 xo_set_program("test"); 64 65 xo_open_container_h(NULL, "top"); 66 67 xo_open_container("data"); 68 xo_open_container("contents"); 69 xo_open_list("item"); 70 71 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n"); 72 73 for (ip = list; ip->i_title; ip++) { 74 xo_open_instance("item"); 75 76 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n", 77 ip->i_title, ip->i_count); 78 79 xo_close_instance("item"); 80 } 81 82 xo_close_list("item"); 83 xo_close_container("contents"); 84 xo_close_container("data"); 85 86 xo_emit("\n\n"); 87 88 xo_open_container("data2"); 89 xo_open_container("contents"); 90 91 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n"); 92 93 for (ip = list; ip->i_title; ip++) { 94 xo_open_instance("item"); 95 96 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n", 97 ip->i_title, ip->i_count); 98 } 99 100 xo_close_container("data2"); 101 102 xo_emit("\n\n"); 103 104 xo_open_container("data3"); 105 xo_open_marker("m1"); 106 xo_open_container("contents"); 107 108 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n"); 109 110 for (ip = list; ip->i_title; ip++) { 111 xo_open_instance("item"); 112 113 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n", 114 ip->i_title, ip->i_count); 115 } 116 117 xo_close_container("data3"); /* Should be a noop */ 118 xo_emit("{:test}", "one"); 119 120 xo_close_marker("m1"); 121 xo_close_container("data3"); /* Should be a noop */ 122 123 xo_emit("\n\n"); 124 125 xo_open_container("data4"); 126 xo_open_marker("m1"); 127 xo_open_container("contents"); 128 129 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n"); 130 131 for (ip = list; ip->i_title; ip++) { 132 xo_open_instance("item"); 133 134 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n", 135 ip->i_title, ip->i_count); 136 137 xo_open_marker("m2"); 138 for (i = 0; i < 3; i++) { 139 xo_open_instance("sub"); 140 xo_emit("{Lwc:/Name}{:name/%d} + 1 = {:next/%d}\n", i, i + 1); 141 xo_close_container("data4"); 142 } 143 xo_close_marker("m2"); 144 xo_emit("{Lwc:/Last}{:last/%d}\n", i); 145 } 146 147 xo_close_container("data4"); /* Should be a noop */ 148 xo_emit("{:test}", "one"); 149 150 xo_emit("\n\n"); 151 152 xo_close_container_h(NULL, "top"); 153 154 xo_finish(); 155 156 return 0; 157 } 158