xref: /freebsd/contrib/libxo/tests/core/test_02.c (revision a4e5e0106ac7145f56eb39a691e302cabb4635be)
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     xo_emit("{Lc:Low\\/warn granularity}{P:\t}{:granularity-lw/%d}{Uw:/%sh}\n",
89 	    155, "mA");
90 
91     xo_emit("{:mbuf-current/%u}/{:mbuf-cache/%u}/{:mbuf-total/%u} "
92 	    "{N:mbufs <&> in use (current\\/cache\\/total)}\n",
93 	    10, 20, 30);
94 
95     xo_emit("{:distance/%u}{Uw:miles} from {:location}\n", 50, "Boston");
96     xo_emit("{:memory/%u}{U:k} left out of {:total/%u}{U:kb}\n", 64, 640);
97     xo_emit("{:memory/%u}{U:/%s} left out of {:total/%u}{U:/%s}\n",
98 	    64, "k", 640, "kilobytes");
99 
100     xo_emit("{,title:/before%safter:}\n", "working");
101 
102     xo_emit("{,display,white,colon:some/%s}"
103 	    "{,value:ten/%ju}{,value:eleven/%ju}\n",
104 	    "string", (uintmax_t) 10, (uintmax_t) 11);
105 
106     xo_emit("{:unknown/%u} "
107 	    "{N:/packet%s here\\/there\\/everywhere}\n",
108 	    1010, "s");
109 
110     xo_emit("{:unknown/%u} "
111 	    "{,note:/packet%s here\\/there\\/everywhere}\n",
112 	    1010, "s");
113 
114     xo_emit("({[:/%d}{n:min/15}/{n:cur/20}/{:max/%d}{]:})\n", 30, 125);
115     xo_emit("({[:30}{:min/%u}/{:cur/%u}/{:max/%u}{]:})\n", 15, 20, 125);
116     xo_emit("({[:-30}{n:min/15}/{n:cur/20}/{n:max/125}{]:})\n");
117     xo_emit("({[:}{:min/%u}/{:cur/%u}/{:max/%u}{]:/%d})\n", 15, 20, 125, -30);
118 
119     xo_emit("Humanize: {h:val1/%u}, {h,hn-space:val2/%u}, "
120 	    "{h,hn-decimal:val3/%u}, {h,hn-1000:val4/%u}, "
121 	    "{h,hn-decimal:val5/%u}\n",
122             21,
123 	    57 * 1024,
124 	    96 * 1024 * 1024,
125 	    (42 * 1024 + 420) * 1024,
126 	    1342172800);
127 
128     xo_open_list("flag");
129     xo_emit("{lq:flag/one} {lq:flag/two} {lq:flag/three}\n");
130     xo_close_list("flag");
131 
132     xo_emit("{n:works/%s}\n", NULL);
133 
134     xo_emit("{e:empty-tag/}");
135     xo_emit("1:{qt:t1/%*d} 2:{qt:t2/test%-*u} "
136 	    "3:{qt:t3/%10sx} 4:{qt:t4/x%-*.*s}\n",
137 	    6, 1000, 8, 5000, "ten-long", 10, 10, "test");
138     xo_emit("{E:this is an error}\n");
139     xo_emit("{E:/%s more error%s}\n", "two", "s" );
140     xo_emit("{W:this is an warning}\n");
141     xo_emit("{W:/%s more warning%s}\n", "two", "s" );
142     xo_emit("{L:/V1\\/V2 packet%s}: {:count/%u}\n", "s", 10);
143 
144     int test = 4;
145     xo_emit("{:test/%04d} {L:/tr%s}\n", test, (test == 1) ? "y" : "ies");
146 
147     xo_message("improper use of profanity; %s; %s",
148 	       "ten yard penalty", "first down");
149 
150     xo_error("Shut 'er down, Clancey!  She's a-pumpin' mud!  <>!,\"!<>\n");
151     xo_error("err message (%d)", 1);
152     xo_error("err message (%d)\n", 2);
153     xo_errorn("err message (%d)", 1);
154     xo_errorn("err message (%d)\n", 2);
155 
156     xo_close_container("data");
157 
158     xo_close_container_h(NULL, "top");
159 
160     xo_finish();
161 
162     return 0;
163 }
164