aboutsummaryrefslogtreecommitdiffstats
path: root/acpiconf-8-Add-libxo-3-support.patch
blob: b65739c88653b4349202265cc4d208444bc5d55e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
From fbb0ec5e3cc2f5b6d9cd023fd5cf7ae38b97ad6a Mon Sep 17 00:00:00 2001
From: Cameron Katri <me@cameronkatri.com>
Date: Thu, 20 May 2021 15:03:17 -0400
Subject: [PATCH] acpiconf(8): Add libxo(3) support

---
 usr.sbin/acpi/acpiconf/Makefile   |   2 +
 usr.sbin/acpi/acpiconf/acpiconf.8 |  12 ++-
 usr.sbin/acpi/acpiconf/acpiconf.c | 122 ++++++++++++++++--------------
 3 files changed, 80 insertions(+), 56 deletions(-)

diff --git a/usr.sbin/acpi/acpiconf/Makefile b/usr.sbin/acpi/acpiconf/Makefile
index 0bbadc1e8fb..479cf9563b2 100644
--- a/usr.sbin/acpi/acpiconf/Makefile
+++ b/usr.sbin/acpi/acpiconf/Makefile
@@ -5,4 +5,6 @@ PROG=	acpiconf
 MAN=	acpiconf.8
 WARNS?=	3
 
+LIBADD=	xo
+
 .include <bsd.prog.mk>
diff --git a/usr.sbin/acpi/acpiconf/acpiconf.8 b/usr.sbin/acpi/acpiconf/acpiconf.8
index 53888d8489e..7e4e7c9564c 100644
--- a/usr.sbin/acpi/acpiconf/acpiconf.8
+++ b/usr.sbin/acpi/acpiconf/acpiconf.8
@@ -27,7 +27,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd December 27, 2018
+.Dd May 19, 2021
 .Dt ACPICONF 8
 .Os
 .Sh NAME
@@ -35,6 +35,7 @@
 .Nd control ACPI power management
 .Sh SYNOPSIS
 .Nm
+.Op Fl -libxo
 .Op Fl h
 .Op Fl i Ar batt
 .Op Fl k Ar ack
@@ -46,6 +47,13 @@ utility allows the user control of the ACPI power management
 functions.
 The following command-line options are recognized:
 .Bl -tag -width ".Fl s Ar type"
+.It Fl -libxo
+Generate output via
+.Xr libxo 3
+in a selection of different human and machine readable formats.
+See
+.Xr xo_parse_args 3
+for details on command line arguments.
 .It Fl h
 Displays a summary of available options.
 .It Fl i Ar batt
@@ -96,6 +104,8 @@ for more information about
 .Va $local_startup .
 .El
 .Sh SEE ALSO
+.Xr libxo 3 ,
+.Xr xo_parse_args 3 ,
 .Xr acpi 4 ,
 .Xr acpidump 8 ,
 .Xr apm 8 ,
diff --git a/usr.sbin/acpi/acpiconf/acpiconf.c b/usr.sbin/acpi/acpiconf/acpiconf.c
index 8a31657fb98..f4d5e06b0d3 100644
--- a/usr.sbin/acpi/acpiconf/acpiconf.c
+++ b/usr.sbin/acpi/acpiconf/acpiconf.c
@@ -37,6 +37,7 @@
 #include <sys/ioctl.h>
 #include <sysexits.h>
 #include <unistd.h>
+#include <libxo/xo.h>
 
 #include <dev/acpica/acpiio.h>
 
@@ -53,7 +54,7 @@ acpi_init(void)
 	if (acpifd == -1)
 		acpifd = open(ACPIDEV, O_RDONLY);
 	if (acpifd == -1)
-		err(EX_OSFILE, ACPIDEV);
+		xo_err(EX_OSFILE, ACPIDEV);
 }
 
 /* Prepare to sleep and then wait for the signal that sleeping can occur. */
@@ -65,7 +66,7 @@ acpi_sleep(int sleep_type)
 	/* Notify OS that we want to sleep.  devd(8) gets this notify. */
 	ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
 	if (ret != 0)
-		err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
+		xo_err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
 }
 
 /* Ack or abort a pending suspend request. */
@@ -76,7 +77,7 @@ acpi_sleep_ack(int err_val)
 
 	ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
 	if (ret != 0)
-		err(EX_IOERR, "ack sleep type failed");
+		xo_err(EX_IOERR, "ack sleep type failed");
 }
 
 /* should be a acpi define, but doesn't appear to be */
@@ -92,78 +93,82 @@ acpi_battinfo(int num)
 	uint32_t volt;
 
 	if (num < 0 || num > 64)
-		errx(EX_USAGE, "invalid battery %d", num);
+		xo_errx(EX_USAGE, "invalid battery %d", num);
 
 	/* Print battery design information. */
 	battio.unit = num;
 	if (ioctl(acpifd, ACPIIO_BATT_GET_BIX, &battio) == -1)
-		err(EX_IOERR, "get battery info (%d) failed", num);
+		xo_err(EX_IOERR, "get battery info (%d) failed", num);
 	amp = battio.bix.units;
 	pwr_units = amp ? "mA" : "mW";
 	if (battio.bix.dcap == UNKNOWN_CAP)
-		printf("Design capacity:\tunknown\n");
+		xo_emit("{Lc:Design capacity}{P:\t}{:design-capacity/unknown}\n");
 	else
-		printf("Design capacity:\t%d %sh\n", battio.bix.dcap,
-		    pwr_units);
+		xo_emit("{Lc:Design capacity}{P:\t}{:design-capacity/%d}{Uw:/%sh}\n",
+		    battio.bix.dcap, pwr_units);
 	if (battio.bix.lfcap == UNKNOWN_CAP)
-		printf("Last full capacity:\tunknown\n");
+		xo_emit("{Lc:Last full capacity}{P:\t}{:last-full-capacity/unknown}\n");
 	else
-		printf("Last full capacity:\t%d %sh\n", battio.bix.lfcap,
-		    pwr_units);
-	printf("Technology:\t\t%s\n", battio.bix.btech == 0 ?
+		xo_emit("{Lc:Last full capacity}{P:\t}{:last-full-capacity/%d}{Uw:/%sh}\n",
+		    battio.bix.lfcap, pwr_units);
+	xo_emit("{Lc:Technology}{P:\t\t}{:technology}\n", battio.bix.btech == 0 ?
 	    "primary (non-rechargeable)" : "secondary (rechargeable)");
 	if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_1)) {
-		printf("Battery Swappable Capability:\t");
+		xo_emit("{Lc:Battery Swappable Capability}{P:\t}");
 		if (battio.bix.scap == ACPI_BIX_SCAP_NO)
-			printf("Non-swappable\n");
+			xo_emit("{:swappable/Non-swappable/no}\n");
 		else if (battio.bix.scap == ACPI_BIX_SCAP_COLD)
-			printf("cold swap\n");
+			xo_emit("{:swappable/cold swap/cold}\n");
 		else if (battio.bix.scap == ACPI_BIX_SCAP_HOT)
-			printf("hot swap\n");
+			xo_emit("{:swappable/hot swap/hot}\n");
 		else
-			printf("unknown\n");
+			xo_emit("{:swappable/unknown}\n");
 	}
 	if (battio.bix.dvol == UNKNOWN_CAP)
-		printf("Design voltage:\t\tunknown\n");
+		xo_emit("{Lc:Design voltage}{P:\t\t}{:voltage/unknown}\n");
 	else
-		printf("Design voltage:\t\t%d mV\n", battio.bix.dvol);
-	printf("Capacity (warn):\t%d %sh\n", battio.bix.wcap, pwr_units);
-	printf("Capacity (low):\t\t%d %sh\n", battio.bix.lcap, pwr_units);
+		xo_emit("{Lc:Design voltage}{P:\t\t}{:voltage/%d}{Uw:mV}\n", battio.bix.dvol);
+	xo_emit("{Lc:Capacity (warn)}{P:\t}{:warn-capacity/%d}{Uw:/%sh}\n",
+	    battio.bix.wcap, pwr_units);
+	xo_emit("{Lc:Capacity (low)}{P:\t\t}{:low-capacity/%d}{Uw:/%sh}\n",
+	    battio.bix.lcap, pwr_units);
 	if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_0)) {
 		if (battio.bix.cycles != ACPI_BATT_UNKNOWN)
-			printf("Cycle Count:\t\t%d\n", battio.bix.cycles);
-		printf("Mesurement Accuracy:\t%d %%\n",
+			xo_emit("{Lc:Cycle Count}{P:\t\t}{:cycles/%d}\n", battio.bix.cycles);
+		xo_emit("{Lc:Mesurement Accuracy}{P:\t}{:accuracy/%d}{Uw:%%}\n",
 		    battio.bix.accuracy / 1000);
 		if (battio.bix.stmax != ACPI_BATT_UNKNOWN)
-			printf("Max Sampling Time:\t%d ms\n",
+			xo_emit("{Lc:Max Sampling Time}{P:\t}{:sampling-max/%d}{Uw:ms}\n",
 			    battio.bix.stmax);
 		if (battio.bix.stmin != ACPI_BATT_UNKNOWN)
-			printf("Min Sampling Time:\t%d ms\n",
+			xo_emit("{Lc:Min Sampling Time}{P:\t}{:sampling-min/%d}{Uw:ms}\n",
 			    battio.bix.stmin);
-		printf("Max Average Interval:\t%d ms\n",
+		xo_emit("{Lc:Max Average Interval}{P:\t}{:avg-interval-max/%d}{Uw:ms}\n",
 		    battio.bix.aimax);
-		printf("Min Average Interval:\t%d ms\n",
+		xo_emit("{Lc:Min Average Interval}{P:\t}{:avg-interval-min/%d}{Uw:ms}\n",
 		    battio.bix.aimin);
 	}
-	printf("Low/warn granularity:\t%d %sh\n", battio.bix.gra1, pwr_units);
-	printf("Warn/full granularity:\t%d %sh\n", battio.bix.gra2, pwr_units);
-	printf("Model number:\t\t%s\n", battio.bix.model);
-	printf("Serial number:\t\t%s\n", battio.bix.serial);
-	printf("Type:\t\t\t%s\n", battio.bix.type);
-	printf("OEM info:\t\t%s\n", battio.bix.oeminfo);
+	xo_emit("{Lc:Low\\/warn granularity}{P:\t}{:granularity-lw/%d}{Uw:/%sh}\n",
+	    battio.bix.gra1, pwr_units);
+	xo_emit("{Lc:Warn\\/full granularity}{P:\t}{:granularity-wf/%d}{Uw:/%sh}\n",
+	    battio.bix.gra2, pwr_units);
+	xo_emit("{Lc:Model number}{P:\t\t}{:model}\n", battio.bix.model);
+	xo_emit("{Lc:Serial number}{P:\t\t}{:serial}\n", battio.bix.serial);
+	xo_emit("{Lc:Type}{P:\t\t\t}{:type}\n", battio.bix.type);
+	xo_emit("{Lc:OEM info}{P:\t\t}{:oeminfo}\n", battio.bix.oeminfo);
 
 	/* Fetch battery voltage information. */
 	volt = UNKNOWN_VOLTAGE;
 	battio.unit = num;
 	if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
-		err(EX_IOERR, "get battery status (%d) failed", num);
+		xo_err(EX_IOERR, "get battery status (%d) failed", num);
 	if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
 		volt = battio.bst.volt;
 
 	/* Print current battery state information. */
 	battio.unit = num;
 	if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
-		err(EX_IOERR, "get battery user info (%d) failed", num);
+		xo_err(EX_IOERR, "get battery user info (%d) failed", num);
 	if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
 		const char *state;
 		switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
@@ -188,36 +193,36 @@ acpi_battinfo(int num)
 		default:
 			state = "invalid";
 		}
-		printf("State:\t\t\t%s\n", state);
+		xo_emit("{Lc:State}{P:\t\t\t}{:state}\n", state);
 		if (battio.battinfo.cap == -1)
-			printf("Remaining capacity:\tunknown\n");
+			xo_emit("{Lc:Remaining capacity}{P:\t}{:remaining-capacity/unknown}\n");
 		else
-			printf("Remaining capacity:\t%d%%\n",
+			xo_emit("{Lc:Remaining capacity}{P:\t}{:remaining-capacity/%d}{D:%%}\n",
 			    battio.battinfo.cap);
 		if (battio.battinfo.min == -1)
-			printf("Remaining time:\t\tunknown\n");
+			xo_emit("{Lc:Remaining time}{P:\t\t}{:remaining-time/unknown}\n");
 		else {
 			hours = battio.battinfo.min / 60;
 			min = battio.battinfo.min % 60;
-			printf("Remaining time:\t\t%d:%02d\n", hours, min);
+			xo_emit("{Lc:Remaining time}{P:\t\t}{:remaining-time/%d:%02d}\n", hours, min);
 		}
 		if (battio.battinfo.rate == -1)
-			printf("Present rate:\t\tunknown\n");
+			xo_emit("{Lc:Present rate}{P:\t\t}{:present-rate/unknown}\n");
 		else if (amp && volt != UNKNOWN_VOLTAGE) {
-			printf("Present rate:\t\t%d mA (%d mW)\n",
+			xo_emit("{Lc:Present rate}{P:\t\t}{:present-rate/%d}{Uw:mA} {D:(}{:present-rate-mw/%d}{Uw:mW}{D:)}\n",
 			    battio.battinfo.rate,
 			    battio.battinfo.rate * volt / 1000);
 		} else
-			printf("Present rate:\t\t%d %s\n",
+			xo_emit("{Lc:Present rate}{P:\t\t}{:present-rate/%d}{Uw:/%s}\n",
 			    battio.battinfo.rate, pwr_units);
 	} else
-		printf("State:\t\t\tnot present\n");
+		xo_emit("{Lc:State}{P:\t\t\t}{:state/not present}\n");
 
 	/* Print battery voltage information. */
 	if (volt == UNKNOWN_VOLTAGE)
-		printf("Present voltage:\tunknown\n");
+		xo_emit("{Lc:Present voltage}{P:\t}{q:present-voltage/unknown}\n");
 	else
-		printf("Present voltage:\t%d mV\n", volt);
+		xo_emit("{Lc:Present voltage}{P:\t}{q:present-voltage/%d}{Uw:mV}\n", volt);
 
 	return (0);
 }
@@ -225,7 +230,9 @@ acpi_battinfo(int num)
 static void
 usage(const char* prog)
 {
-	printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
+	xo_error("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
+	xo_close_container("acpiconf");
+	xo_finish();
 	exit(0);
 }
 
@@ -237,6 +244,9 @@ main(int argc, char *argv[])
 	int	iflag = 0, kflag = 0, sflag = 0;
 
 	prog = argv[0];
+
+	argc = xo_parse_args(argc, argv);
+	xo_open_container("acpiconf");
 	if (argc < 2)
 		usage(prog);
 		/* NOTREACHED */
@@ -249,13 +259,13 @@ main(int argc, char *argv[])
 			iflag = 1;
 			battery = strtol(optarg, &end, 10);
 			if ((size_t)(end - optarg) != strlen(optarg))
-			    errx(EX_USAGE, "invalid battery");
+			    xo_errx(EX_USAGE, "invalid battery");
 			break;
 		case 'k':
 			kflag = 1;
 			ack = strtol(optarg, &end, 10);
 			if ((size_t)(end - optarg) != strlen(optarg))
-			    errx(EX_USAGE, "invalid ack argument");
+			    xo_errx(EX_USAGE, "invalid ack argument");
 			break;
 		case 's':
 			sflag = 1;
@@ -263,9 +273,9 @@ main(int argc, char *argv[])
 				optarg++;
 			sleep_type = strtol(optarg, &end, 10);
 			if ((size_t)(end - optarg) != strlen(optarg))
-			    errx(EX_USAGE, "invalid sleep type");
+			    xo_errx(EX_USAGE, "invalid sleep type");
 			if (sleep_type < 1 || sleep_type > 4)
-				errx(EX_USAGE, "invalid sleep type (%d)",
+				xo_errx(EX_USAGE, "invalid sleep type (%d)",
 				     sleep_type);
 			break;
 		case 'h':
@@ -278,19 +288,19 @@ main(int argc, char *argv[])
 	argv += optind;
 
 	if (iflag != 0 && kflag != 0 && sflag != 0)
-			errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
+			xo_errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
 
 	if (iflag  != 0) {
 		if (kflag != 0)
-			errx(EX_USAGE, "-i and -k are mutually exclusive");
+			xo_errx(EX_USAGE, "-i and -k are mutually exclusive");
 		if (sflag != 0)
-			errx(EX_USAGE, "-i and -s are mutually exclusive");
+			xo_errx(EX_USAGE, "-i and -s are mutually exclusive");
 		acpi_battinfo(battery);
 	}
 
 	if (kflag != 0) {
 		if (sflag != 0)
-			errx(EX_USAGE, "-k and -s are mutually exclusive");
+			xo_errx(EX_USAGE, "-k and -s are mutually exclusive");
 		acpi_sleep_ack(ack);
 	}
 
@@ -299,5 +309,7 @@ main(int argc, char *argv[])
 		acpi_sleep(sleep_type);
 
 	close(acpifd);
+	xo_close_container("acpiconf");
+	xo_finish();
 	exit (0);
 }
-- 
2.31.1