summaryrefslogtreecommitdiff
path: root/dist/api_flags.c
blob: c0d3317d3a2ffd393a10fab4770285c949ea9762 (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996-2009 Oracle.  All rights reserved.
 */

#include <sys/types.h>

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct {
	char	*name;			/* API name */

	u_int	used_mask;		/* Bits used. */
} API;
API	**api_list, **api_end;

typedef struct {
	char	 *name;			/* Flag name */

	int	api_cnt;		/* APIs that use this flag. */
	API	**api, **api_end;

	u_int	value;			/* Bit value */
} FLAG;
FLAG	**flag_list, **flag_end;

int	verbose;
char	*progname;

int	add_entry(char *, char *);
void	define_print(char *, u_int);
void	dump_api(void);
void	dump_flags(void);
int	flag_cmp_alpha(const void *, const void *);
int	flag_cmp_api_cnt(const void *, const void *);
int	generate_flags(void);
int	parse(void);
void	print_api_mask(void);
void	print_api_remainder(void);
void	print_flag_value(void);
int	syserr(void);
int	usage(void);

int
main(int argc, char *argv[])
{
	enum { API_MASK, API_REMAINDER, FLAG_VALUE } output;
	int ch;

	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		++progname;

	output = FLAG_VALUE;
	while ((ch = getopt(argc, argv, "mrv")) != EOF)
		switch (ch) {
		case 'm':
			output = API_MASK;
			break;
		case 'r':
			output = API_REMAINDER;
			break;
		case 'v':
			verbose = 1;
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;

	if (parse() || generate_flags())
		return (EXIT_FAILURE);

	switch (output) {
	case API_MASK:
		print_api_mask();
		break;
	case API_REMAINDER:
		print_api_remainder();
		break;
	case FLAG_VALUE:
		print_flag_value();
		break;
	}

	if (verbose) {
		dump_api();
		dump_flags();
	}

	return (EXIT_SUCCESS);
}

int
parse()
{
	int lc;
	char *p, *api, buf[256];

	api = NULL;

	/*
	 * Read the method name/flag pairs.
	 */
	for (lc = 1; fgets(buf, sizeof(buf), stdin) != NULL; ++lc) {
		if ((p = strchr(buf, '\n')) != NULL)
			*p = '\0';
		else {
			fprintf(
			    stderr, "%s: %d: line too long\n", progname, lc);
			return (1);
		}

		/* Ignore any empty line or hash mark. */
		if (buf[0] == '\0' || buf[0] == '#')
			continue;

		/*
		 * A line without leading whitespace is an API name, a line
		 * with leading whitespace is a flag name.
		 */
		if (isspace(buf[0])) {
			if ((p = strtok(buf, " \t")) == NULL || *p == '#')
				continue;

			/* A flag without an API makes no sense. */
			if (api == NULL)
				goto format;

			/* Enter the pair into the array. */
			if (add_entry(api, p))
				return (1);
		} else {
			if ((p = strtok(buf, " \t")) == NULL)
				continue;
			if (api != NULL)
				free(api);
			if ((api = strdup(p)) == NULL)
				return (syserr());
		}
		if ((p = strtok(NULL, " \t")) != NULL && *p != '#')
			goto format;
	}

	return (0);

format:	fprintf(stderr, "%s: format error: line %d\n", progname, lc);
	return (1);
}

int
add_entry(char *api_name, char *flag_name)
{
	FLAG **fpp, *fp;
	API **app, *ap, **p;
	u_int cnt;

	/* Search for this api's API structure. */
	for (app = api_list;
	    app != NULL && *app != NULL && app < api_end; ++app)
		if (strcmp(api_name, (*app)->name) == 0)
			break;

	/* Allocate new space in the API array if necessary. */
	if (app == NULL || app == api_end) {
		cnt = app == NULL ? 100 : (u_int)(api_end - api_list) + 100;
		if ((api_list = realloc(api_list, sizeof(API *) * cnt)) == NULL)
			return (syserr());
		api_end = api_list + cnt;
		app = api_list + (cnt - 100);
		memset(app, 0, (u_int)(api_end - app) * sizeof(API *));
	}

	/* Allocate a new API structure and fill in the name if necessary. */
	if (*app == NULL &&
	    ((*app = calloc(sizeof(API), 1)) == NULL ||
	    ((*app)->name = strdup(api_name)) == NULL))
		return (syserr());

	ap = *app;

	/*
	 * There's a special keyword, "__MASK=<value>" that sets the initial
	 * flags value for an API, and so prevents those flag bits from being
	 * chosen for that API's flags.
	 */
	if (strncmp(flag_name, "__MASK=", sizeof("__MASK=") - 1) == 0) {
		ap->used_mask |=
		    strtoul(flag_name + sizeof("__MASK=") - 1, NULL, 0);
		return (0);
	}

	/* Search for this flag's FLAG structure. */
	for (fpp = flag_list;
	    fpp != NULL && *fpp != NULL && fpp < flag_end; ++fpp)
		if (strcmp(flag_name, (*fpp)->name) == 0)
			break;

	/* Realloc space in the FLAG array if necessary. */
	if (fpp == NULL || fpp == flag_end) {
		cnt = fpp == NULL ? 100 : (u_int)(flag_end - flag_list) + 100;
		if ((flag_list =
		    realloc(flag_list, sizeof(FLAG *) * cnt)) == NULL)
			return (syserr());
		flag_end = flag_list + cnt;
		fpp = flag_list + (cnt - 100);
		memset(fpp, 0, (u_int)(flag_end - fpp) * sizeof(FLAG *));
	}

	/* Allocate a new FLAG structure and fill in the name if necessary. */
	if (*fpp == NULL &&
	    ((*fpp = calloc(sizeof(FLAG), 1)) == NULL ||
	    ((*fpp)->name = strdup(flag_name)) == NULL))
		return (syserr());

	fp = *fpp;
	++fp->api_cnt;

	/* Check to see if this API is already listed for this flag. */
	for (p = fp->api; p != NULL && *p != NULL && p < fp->api_end; ++p)
		if (strcmp(api_name, (*p)->name) == 0) {
			fprintf(stderr,
			    "duplicate entry: %s / %s\n", api_name, flag_name);
			return (1);
		}

	/* Realloc space in the FLAG's API array if necessary. */
	if (p == NULL || p == fp->api_end) {
		cnt = p == NULL ? 20 : (u_int)(fp->api_end - fp->api) + 20;
		if ((fp->api = realloc(fp->api, sizeof(API *) * cnt)) == NULL)
			return (syserr());
		fp->api_end = fp->api + cnt;
		p = fp->api + (cnt - 20);
		memset(p, 0, (u_int)(fp->api_end - fp->api) * sizeof(API *));
	}
	*p = ap;

	return (0);
}

void
dump_api()
{
	API **app;

	printf("=============================\nAPI:\n");
	for (app = api_list; *app != NULL; ++app)
		printf("%s (%#x)\n", (*app)->name, (*app)->used_mask);
}

void
dump_flags()
{
	FLAG **fpp;
	API **api;
	char *sep;

	printf("=============================\nFLAGS:\n");
	for (fpp = flag_list; *fpp != NULL; ++fpp) {
		printf("%s (%#x, %d): ",
		    (*fpp)->name, (*fpp)->value, (*fpp)->api_cnt);
		sep = "";
		for (api = (*fpp)->api; *api != NULL; ++api) {
			printf("%s%s", sep, (*api)->name);
			sep = ", ";
		}
		printf("\n");
	}
}

int
flag_cmp_api_cnt(const void *a, const void *b)
{
	FLAG *af, *bf;

	af = *(FLAG **)a;
	bf = *(FLAG **)b;

	if (af == NULL) {
		if (bf == NULL)
			return (0);
		return (1);
	}
	if (bf == NULL) {
		if (af == NULL)
			return (0);
		return (-1);
	}
	if (af->api_cnt > bf->api_cnt)
		return (-1);
	if (af->api_cnt < bf->api_cnt)
		return (1);
	return (strcmp(af->name, bf->name));
}

int
generate_flags()
{
	FLAG **fpp;
	API **api;
	u_int mask;

	/* Sort the FLAGS array by reference count, in reverse order. */
	qsort(flag_list,
	    (u_int)(flag_end - flag_list), sizeof(FLAG *), flag_cmp_api_cnt);

	/*
	 * Here's the plan: walk the list of flags, allocating bits.  For
	 * each flag, we walk the list of APIs that use it and find a bit
	 * none of them are using.  That bit becomes the flag's value.
	 */
	for (fpp = flag_list; *fpp != NULL; ++fpp) {
		mask = 0xffffffff;			/* Set to all 1's */
		for (api = (*fpp)->api; *api != NULL; ++api)
			mask &= ~(*api)->used_mask;	/* Clear API's bits */
		if (mask == 0) {
			fprintf(stderr, "%s: ran out of bits at flag %s\n",
			   progname, (*fpp)->name);
			return (1);
		}
		(*fpp)->value = mask = 1 << (ffs(mask) - 1);
		for (api = (*fpp)->api; *api != NULL; ++api)
			(*api)->used_mask |= mask;	/* Set bit for API */
	}

	return (0);
}

int
flag_cmp_alpha(const void *a, const void *b)
{
	FLAG *af, *bf;

	af = *(FLAG **)a;
	bf = *(FLAG **)b;

	if (af == NULL) {
		if (bf == NULL)
			return (0);
		return (1);
	}
	if (bf == NULL) {
		if (af == NULL)
			return (0);
		return (-1);
	}
	return (strcmp(af->name, bf->name));
}

void
print_api_mask()
{
	API **app;
	char *p, buf[256];

	/* Output a mask for the API. */
	for (app = api_list; *app != NULL; ++app) {
		(void)snprintf(
		    buf, sizeof(buf), "_%s_API_MASK", (*app)->name);
		for (p = buf; *p != '\0'; ++p)
			if (islower(*p))
				*p = toupper(*p);
			else if (!isalpha(*p))
				*p = '_';
		define_print(buf, (*app)->used_mask);
	}
}

void
print_api_remainder()
{
	API **app;
	int unused, i;

	/* Output the bits remaining for the API. */
	for (app = api_list; *app != NULL; ++app) {
		for (i = unused = 0; i < 32; ++i)
			if (!((*app)->used_mask & (1 << i)))
				++unused;
		printf("%s: %d bits unused\n", (*app)->name, unused);
	}
}

void
print_flag_value()
{
	FLAG **fpp;

	/* Sort the FLAGS array in alphabetical order. */
	qsort(flag_list,
	    (u_int)(flag_end - flag_list), sizeof(FLAG *), flag_cmp_alpha);

	/* Output each flag's value. */
	for (fpp = flag_list; *fpp != NULL; ++fpp)
		define_print((*fpp)->name, (*fpp)->value);
}

void
define_print(char *name, u_int value)
{
	char *sep;

	switch (strlen(name) / 8) {
	case 0:
		sep = "\t\t\t\t\t";
		break;
	case 1:
		sep = "\t\t\t\t";
		break;
	case 2:
		sep = "\t\t\t";
		break;
	case 3:
		sep = "\t\t";
		break;
	default:
		sep = "\t";
		break;
	}
	printf("#define\t%s%s%#010x\n", name, sep, value);
}

int
syserr(void)
{
	fprintf(stderr, "%s: %s\n", progname, strerror(errno));
	return (1);
}

int
usage()
{
	(void)fprintf(stderr, "usage: %s [-mrv]\n", progname);
	return (EXIT_FAILURE);
}