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