xref: /freebsd/contrib/libxo/tests/core/test_02.c (revision d8ffc21c5ca6f7d4f2d9a65dc6308699af0b6a01)
1 /*
2  * Copyright (c) 2014-2019, 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 <stdint.h>
14 #include <string.h>
15 
16 #include "xo.h"
17 #include "xo_encoder.h"
18 
19 #include "xo_humanize.h"
20 
21 int
22 main (int argc, char **argv)
23 {
24     argc = xo_parse_args(argc, argv);
25     if (argc < 0)
26 	return 1;
27 
28     for (argc = 1; argv[argc]; argc++) {
29 	if (xo_streq(argv[argc], "xml"))
30 	    xo_set_style(NULL, XO_STYLE_XML);
31 	else if (xo_streq(argv[argc], "json"))
32 	    xo_set_style(NULL, XO_STYLE_JSON);
33 	else if (xo_streq(argv[argc], "text"))
34 	    xo_set_style(NULL, XO_STYLE_TEXT);
35 	else if (xo_streq(argv[argc], "html"))
36 	    xo_set_style(NULL, XO_STYLE_HTML);
37 	else if (xo_streq(argv[argc], "pretty"))
38 	    xo_set_flags(NULL, XOF_PRETTY);
39 	else if (xo_streq(argv[argc], "xpath"))
40 	    xo_set_flags(NULL, XOF_XPATH);
41 	else if (xo_streq(argv[argc], "info"))
42 	    xo_set_flags(NULL, XOF_INFO);
43     }
44 
45     xo_set_flags(NULL, XOF_UNITS); /* Always test w/ this */
46     xo_set_file(stdout);
47 
48     xo_open_container_h(NULL, "top");
49 
50     xo_open_container("data");
51 
52     xo_emit("{kt:name/%-*.*s}{eq:flags/0x%x}",
53 	    5, 5, "em0", 34883);
54 
55     xo_emit("{d:/%-*.*s}{etk:name}{eq:flags/0x%x}",
56 	    5, 5, "em0", "em0", 34883);
57 
58     xo_emit("We are {{emit}}{{ting}} some {:what}\n", "braces");
59 
60     xo_message("abcdef");
61     close(-1);
62     xo_message_e("abcdef");
63 
64     xo_message("improper use of profanity; %s; %s",
65 	       "ten yard penalty", "first down");
66 
67     xo_emit("length {:length/%6.6s}\n", "abcdefghijklmnopqrstuvwxyz");
68 
69     close(-1);
70     xo_emit("close {:fd/%d} returned {:error/%m} {:test}\n", -1, "good");
71     close(-1);
72     xo_emit("close {:fd/%d} returned {:error/%6.6m} {:test}\n", -1, "good");
73 
74 
75     xo_message("improper use of profanity; %s; %s",
76 	       "ten yard penalty", "first down");
77 
78     xo_emit(" {:lines/%7ju} {:words/%7ju} "
79             "{:characters/%7ju} {d:filename/%s}\n",
80             (uintmax_t) 20, (uintmax_t) 30, (uintmax_t) 40, "file");
81 
82     int i;
83     for (i = 0; i < 5; i++)
84 	xo_emit("{lw:bytes/%d}{Np:byte,bytes}\n", i);
85 
86 
87     xo_emit("{:mbuf-current/%u}/{:mbuf-cache/%u}/{:mbuf-total/%u} "
88 	    "{N:mbufs <&> in use (current\\/cache\\/total)}\n",
89 	    10, 20, 30);
90 
91     xo_emit("{:distance/%u}{Uw:miles} from {:location}\n", 50, "Boston");
92     xo_emit("{:memory/%u}{U:k} left out of {:total/%u}{U:kb}\n", 64, 640);
93     xo_emit("{:memory/%u}{U:/%s} left out of {:total/%u}{U:/%s}\n",
94 	    64, "k", 640, "kilobytes");
95 
96     xo_emit("{,title:/before%safter:}\n", "working");
97 
98     xo_emit("{,display,white,colon:some/%s}"
99 	    "{,value:ten/%ju}{,value:eleven/%ju}\n",
100 	    "string", (uintmax_t) 10, (uintmax_t) 11);
101 
102     xo_emit("{:unknown/%u} "
103 	    "{N:/packet%s here\\/there\\/everywhere}\n",
104 	    1010, "s");
105 
106     xo_emit("{:unknown/%u} "
107 	    "{,note:/packet%s here\\/there\\/everywhere}\n",
108 	    1010, "s");
109 
110     xo_emit("({[:/%d}{n:min/15}/{n:cur/20}/{:max/%d}{]:})\n", 30, 125);
111     xo_emit("({[:30}{:min/%u}/{:cur/%u}/{:max/%u}{]:})\n", 15, 20, 125);
112     xo_emit("({[:-30}{n:min/15}/{n:cur/20}/{n:max/125}{]:})\n");
113     xo_emit("({[:}{:min/%u}/{:cur/%u}/{:max/%u}{]:/%d})\n", 15, 20, 125, -30);
114 
115     xo_emit("Humanize: {h:val1/%u}, {h,hn-space:val2/%u}, "
116 	    "{h,hn-decimal:val3/%u}, {h,hn-1000:val4/%u}, "
117 	    "{h,hn-decimal:val5/%u}\n",
118             21,
119 	    57 * 1024,
120 	    96 * 1024 * 1024,
121 	    (42 * 1024 + 420) * 1024,
122 	    1342172800);
123 
124     xo_open_list("flag");
125     xo_emit("{lq:flag/one} {lq:flag/two} {lq:flag/three}\n");
126     xo_close_list("flag");
127 
128     xo_emit("{n:works/%s}\n", NULL);
129 
130     xo_emit("{e:empty-tag/}");
131     xo_emit("1:{qt:t1/%*d} 2:{qt:t2/test%-*u} "
132 	    "3:{qt:t3/%10sx} 4:{qt:t4/x%-*.*s}\n",
133 	    6, 1000, 8, 5000, "ten-long", 10, 10, "test");
134     xo_emit("{E:this is an error}\n");
135     xo_emit("{E:/%s more error%s}\n", "two", "s" );
136     xo_emit("{W:this is an warning}\n");
137     xo_emit("{W:/%s more warning%s}\n", "two", "s" );
138     xo_emit("{L:/V1\\/V2 packet%s}: {:count/%u}\n", "s", 10);
139 
140     int test = 4;
141     xo_emit("{:test/%04d} {L:/tr%s}\n", test, (test == 1) ? "y" : "ies");
142 
143     xo_message("improper use of profanity; %s; %s",
144 	       "ten yard penalty", "first down");
145 
146     xo_error("Shut 'er down, Clancey!  She's a-pumpin' mud!  <>!,\"!<>\n");
147 
148     xo_close_container("data");
149 
150     xo_close_container_h(NULL, "top");
151 
152     xo_finish();
153 
154     return 0;
155 }
156