File: bits.c

package info (click to toggle)
util-linux 2.41-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 95,208 kB
  • sloc: ansic: 179,016; sh: 22,689; yacc: 1,284; makefile: 528; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (330 lines) | stat: -rw-r--r-- 7,729 bytes parent folder | download | duplicates (2)
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
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Copyright (c) 2024 Robin Jarry
 *
 * bits - convert bit masks from/to various formats
 */

#include <errno.h>
#include <getopt.h>
#include <sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "c.h"
#include "closestream.h"
#include "cpuset.h"
#include "nls.h"
#include "strutils.h"
#include "strv.h"
#include "xalloc.h"

static void parse_mask_or_list(const char *cmdline_arg,
		cpu_set_t *all_bits, size_t width)
{
	cpu_set_t *bits, *copy;
	char bitwise_op = '|';
	const char *arg;
	size_t size, n;

	arg = cmdline_arg;

	/* strip optional operator first */
	if (startswith(arg, "&")) {
		bitwise_op = '&';
		arg++;
	} else if (startswith(arg, "^")) {
		bitwise_op = '^';
		arg++;
	} else if (startswith(arg, "~")) {
		bitwise_op = '~';
		arg++;
	} else if (startswith(arg, "|")) {
		arg++;
	}

	bits = cpuset_alloc(width, &size, NULL);
	if (bits == NULL)
		errx(EXIT_FAILURE, _("error: cannot allocate bit mask"));

	if (startswith(arg, ",") || startswith(arg, "0x")) {
		if (startswith(arg, ","))
			arg++;
		if (cpumask_parse(arg, bits, size) < 0)
			errx(EXIT_FAILURE, _("error: invalid bit mask: %s"), cmdline_arg);
	} else {
		if (cpulist_parse(arg, bits, size, 1) < 0)
			errx(EXIT_FAILURE, _("error: invalid bit list: %s"), cmdline_arg);
	}

	/* truncate all bits beyond the requested mask size */
	for (n = cpuset_nbits(size) - 1; n >= width; n--)
		CPU_CLR_S(n, size, bits);

	copy = cpuset_alloc(width, &size, NULL);
	if (copy == NULL)
		errx(EXIT_FAILURE, _("error: cannot allocate bit mask"));
	memcpy(copy, all_bits, size);

	switch (bitwise_op) {
	case '&':
		CPU_AND_S(size, all_bits, copy, bits);
		break;
	case '|':
		CPU_OR_S(size, all_bits, copy, bits);
		break;
	case '^':
		CPU_XOR_S(size, all_bits, copy, bits);
		break;
	case '~':
		for (n = 0; n < width; n++) {
			if (CPU_ISSET_S(n, size, bits))
				CPU_CLR_S(n, size, all_bits);
		}
		break;
	}

	cpuset_free(bits);
	cpuset_free(copy);
}

static size_t num_digits(size_t value)
{
	size_t digits = 1;
	while (value > 9) {
		digits++;
		value /= 10;
	}
	return digits;
}

enum output_mode {
	MODE_BINARY,
	MODE_GROUPED_MASK,
	MODE_LIST,
	MODE_MASK,
};

static void print_bits(cpu_set_t *bits, size_t width, enum output_mode mode)
{
	const size_t size = CPU_ALLOC_SIZE(width);
	bool started = false;
	char *buf = NULL;
	size_t buf_size;
	ssize_t n = 0;

	if (CPU_COUNT_S(size, bits) == 0) {
		switch (mode) {
		case MODE_MASK:
			printf("0x0\n");
			break;
		case MODE_GROUPED_MASK:
			printf("0\n");
			break;
		case MODE_BINARY:
			printf("0b0\n");
			break;
		case MODE_LIST:
			break;
		}
		return;
	}

	switch (mode) {
	case MODE_MASK:
		/* fit 4 bits per character plus terminating nul byte */
		buf_size = ((cpuset_nbits(size) + 3) / 4) + 1;
		buf = xmalloc(buf_size);
		cpumask_create(buf, buf_size, bits, size);

		/* strip leading zeroes */
		while (buf[n] == '0')
			n++;
		printf("0x%s\n", buf + n);
		break;

	case MODE_GROUPED_MASK:
		/* fit 4 bits per character plus terminating nul byte */
		buf_size = ((cpuset_nbits(size) + 3) / 4) + 1;
		buf = xmalloc(buf_size);
		cpumask_create(buf, buf_size, bits, size);

		/* strip leading zeroes */
		while (buf[n] == '0')
			n++;

		while (buf[n] != '\0') {
			if (started && (n % 8) == 0)
				printf(",");
			if (buf[n] != '0')
				started = true;
			printf("%c", buf[n]);
			n++;
		}
		printf("\n");
		break;

	case MODE_BINARY:
		printf("0b");
		for (n = width - 1; n >= 0; n--) {
			if (started && ((n + 1) % 4) == 0)
				printf("_");
			if (CPU_ISSET_S(n, size, bits)) {
				started = true;
				printf("1");
			} else if (started) {
				printf("0");
			}
		}
		printf("\n");
		break;

	case MODE_LIST:
		/* Maximum number of digits (larger bit number) plus 1
		 * to account for a separating comma, times the number of bits
		 * set to 1. */
		buf_size = (num_digits(width - 1) + 1) * CPU_COUNT_S(size, bits);
		buf = xmalloc(buf_size);
		cpulist_create(buf, buf_size, bits, size);
		printf("%s\n", buf);
		break;
	}

	free(buf);
}

static void __attribute__((__noreturn__)) usage(void)
{
	fputs(USAGE_HEADER, stdout);
	fprintf(stdout, _(" %s [options] [<mask_or_list>...]\n"),
		program_invocation_short_name);

	fputs(USAGE_SEPARATOR, stdout);
	fputsln(_("Convert bit masks from/to various formats."), stdout);

	fputs(USAGE_ARGUMENTS, stdout);
	fputsln(_(" <mask_or_list>      bits specified as a hex mask (e.g. 0xeec2)\n"
		  "                       or as a comma-separated list of bit IDs"),
		stdout);
	fputs(USAGE_SEPARATOR, stdout);
	fputsln(_(" If not specified, arguments will be read from stdin."),
		stdout);

	fputs(USAGE_OPTIONS, stdout);
	fprintf(stdout, USAGE_HELP_OPTIONS(21));
	fputsln(_(" -w <num>, --width <num>\n"
	          "                     maximum width of bit masks (default 8192)"),
		stdout);

	fputs(_("\nOutput modes:\n"), stdout);
	fputsln(_(" -m, --mask          display bits as a hex mask value (default)"),
		stdout);
	fputsln(_(" -g, --grouped-mask  display bits as a hex mask value in 32bit\n"
		  "                       comma separated groups"), stdout);
	fputsln(_(" -b, --binary        display bits as a binary mask value"),
		stdout);
	fputsln(_(" -l, --list          display bits as a compressed list of bit IDs"),
		stdout);

	fprintf(stdout, USAGE_MAN_TAIL("bits(1)"));
	exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
	enum output_mode mode = MODE_MASK;
	char **stdin_lines = NULL;
	cpu_set_t *bits = NULL;
	size_t width = 8192;
	size_t alloc_size;
	int c;

#define FLAGS "Vhw:mgbl"
	static const struct option longopts[] = {
		{ "version",      no_argument,       NULL, 'V' },
		{ "help",         no_argument,       NULL, 'h' },
		{ "width",        required_argument, NULL, 'w' },
		{ "mask",         no_argument,       NULL, 'm' },
		{ "grouped-mask", no_argument,       NULL, 'g' },
		{ "binary",       no_argument,       NULL, 'b' },
		{ "list",         no_argument,       NULL, 'l' },
		{ NULL,           0,                 NULL,  0  }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	close_stdout_atexit();

	while ((c = getopt_long(argc, argv, FLAGS, longopts, NULL)) != -1) {
		switch (c) {
		case 'm':
			mode = MODE_MASK;
			break;
		case 'g':
			mode = MODE_GROUPED_MASK;
			break;
		case 'b':
			mode = MODE_BINARY;
			break;
		case 'l':
			mode = MODE_LIST;
			break;
		case 'w':
			/* allow up to 128k masks */
			width = str2unum_or_err(optarg,
				10, _("invalid --width"), 128 * 1024);
			break;
		case 'V':
			print_version(EXIT_SUCCESS);
		case 'h':
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}

	argc -= optind;
	argv += optind;
	if (argc == 0) {
		/* no arguments provided, read lines from stdin */
		char buf[LINE_MAX];

		while (fgets(buf, sizeof(buf), stdin)) {
			/* strip LF, CR, CRLF, LFCR */
			rtrim_whitespace((unsigned char *)buf);
			if (strv_push(&stdin_lines, xstrdup(buf)) < 0)
				errx(EXIT_FAILURE, _("cannot allocate memory"));
		}

		argc = strv_length(stdin_lines);
		argv = stdin_lines;
	}

	bits = cpuset_alloc(width, &alloc_size, NULL);
	if (bits == NULL)
		errx(EXIT_FAILURE, _("cannot allocate memory"));

	/* start off with all bits set to 0 */
	memset(bits, 0, alloc_size);

	for (; argc > 0; argc--, argv++)
		parse_mask_or_list(*argv, bits, width);

	strv_free(stdin_lines);

	print_bits(bits, width, mode);

	cpuset_free(bits);

	return EXIT_SUCCESS;
}