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