xref: /freebsd/contrib/libxo/tests/core/test_09.c (revision b00ab754)
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 
70     xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
71 
72     for (ip = list; ip->i_title; ip++) {
73 	xo_emit("Name: {l:name/%-10s/%s}\n", ip->i_title);
74     }
75 
76     xo_close_container("contents");
77 
78     xo_emit("\n\n");
79     xo_open_container("contents");
80 
81     xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
82 
83     for (ip = list; ip->i_title; ip++) {
84 	xo_emit("Name: {l:item/%-10s/%s}\n", ip->i_title);
85     }
86 
87     xo_close_container("contents");
88 
89     xo_emit("\n\n");
90 
91     xo_open_container("contents");
92     xo_emit("{T:Test/%-10s}{T:Three/%12s}\n");
93 
94     xo_open_list("item");
95     for (ip = list; ip->i_title; ip++) {
96 	xo_emit("Name: {l:item/%-10s/%s}\n", ip->i_title);
97     }
98     xo_emit("{Lwc:/Total:}{:total}\n", "six");
99 
100     xo_emit("{:one}", "one");
101     xo_emit("{l:two}", "two");
102     xo_emit("{:three}", "three");
103 
104 
105     xo_close_container("contents");
106 
107     xo_emit("\n\n");
108 
109     xo_close_container_h(NULL, "top");
110 
111     xo_finish();
112 
113     return 0;
114 }
115