Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* $NetBSD: citrus_prop.c,v 1.6 2022/04/19 20:32:14 rillig Exp $ */

/*-
 * Copyright (c)2006 Citrus Project,
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: citrus_prop.c,v 1.6 2022/04/19 20:32:14 rillig Exp $");
#endif /* LIBC_SCCS and not lint */

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "citrus_namespace.h"
#include "citrus_bcs.h"
#include "citrus_region.h"
#include "citrus_memstream.h"
#include "citrus_prop.h"

typedef struct {
	_citrus_prop_type_t type;
	union {
		const char *str;
		int chr;
		bool boolean;
		uint64_t num;
	} u;
} _citrus_prop_object_t;

static __inline void
_citrus_prop_object_init(_citrus_prop_object_t *obj, _citrus_prop_type_t type)
{
	_DIAGASSERT(obj != NULL);

	obj->type = type;
	memset(&obj->u, 0, sizeof(obj->u));
}

static __inline void
_citrus_prop_object_uninit(_citrus_prop_object_t *obj)
{
	_DIAGASSERT(obj != NULL);

	if (obj->type == _CITRUS_PROP_STR)
		free(__UNCONST(obj->u.str));
}

static const char *xdigit = "0123456789ABCDEF";

#define _CITRUS_PROP_READ_UINT_COMMON(_func_, _type_, _max_)		\
static int								\
_citrus_prop_read_##_func_##_common(struct _memstream * __restrict ms,	\
	_type_ * __restrict result, int base)				\
{									\
	_type_ acc, cutoff;						\
	int n, ch, cutlim;						\
	char *p;							\
									\
	_DIAGASSERT(ms != NULL);					\
	_DIAGASSERT(result != NULL);					\
									\
	acc = (_type_)0;						\
	cutoff = _max_ / base;						\
	cutlim = _max_ % base;						\
	for (;;) {							\
		ch = _memstream_getc(ms);				\
		p = strchr(xdigit, _bcs_toupper(ch));			\
		if (p == NULL || (n = (p - xdigit)) >= base)		\
			break;						\
		if (acc > cutoff || (acc == cutoff && n > cutlim))	\
			break;						\
		acc *= base;						\
		acc += n;						\
	}								\
	_memstream_ungetc(ms, ch);					\
	*result = acc;							\
	return 0;							\
}
_CITRUS_PROP_READ_UINT_COMMON(chr, int, UCHAR_MAX)
_CITRUS_PROP_READ_UINT_COMMON(num, uint64_t, UINT64_MAX)
#undef _CITRUS_PROP_READ_UINT_COMMON

#define _CITRUS_PROP_READ_INT(_func_, _type_)			\
static int							\
_citrus_prop_read_##_func_(struct _memstream * __restrict ms,	\
	_citrus_prop_object_t * __restrict obj)			\
{								\
	int ch, neg, base;					\
								\
	_DIAGASSERT(ms != NULL);				\
	_DIAGASSERT(obj != NULL);				\
								\
	_memstream_skip_ws(ms);					\
	ch = _memstream_getc(ms);				\
	neg = 0;						\
	switch (ch) {						\
	case '-':						\
		neg = 1;					\
	case '+':						\
		ch = _memstream_getc(ms);			\
	}							\
	base = 10;						\
	if (ch == '0') {					\
		base -= 2;					\
		ch = _memstream_getc(ms);			\
		if (ch == 'x' || ch == 'X') {			\
			ch = _memstream_getc(ms);		\
			if (_bcs_isxdigit(ch) == 0) {		\
				_memstream_ungetc(ms, ch);	\
				obj->u._func_ = 0;		\
				return 0;			\
			}					\
			base += 8;				\
		}						\
	} else if (_bcs_isdigit(ch) == 0)			\
		return EINVAL;					\
	_memstream_ungetc(ms, ch);				\
	return _citrus_prop_read_##_func_##_common		\
	    (ms, &obj->u._func_, base);				\
}
_CITRUS_PROP_READ_INT(chr, int)
_CITRUS_PROP_READ_INT(num, uint64_t)
#undef _CITRUS_PROP_READ_INT

static int
_citrus_prop_read_character_common(struct _memstream * __restrict ms,
	int * __restrict result)
{
	int ch, base;

	_DIAGASSERT(ms != NULL);
	_DIAGASSERT(result != NULL);

	ch = _memstream_getc(ms);
	if (ch != '\\') {
		*result = ch;
	} else {
		ch = _memstream_getc(ms);
		base = 16;
		switch (ch) {
		case 'a': *result = '\a'; break;
		case 'b': *result = '\b'; break;
		case 'f': *result = '\f'; break;
		case 'n': *result = '\n'; break;
		case 'r': *result = '\r'; break;
		case 't': *result = '\t'; break;
		case 'v': *result = '\v'; break;
		/*FALLTHROUGH*/
		case '0': case '1': case '2': case '3':
		case '4': case '5': case '6': case '7':
			_memstream_ungetc(ms, ch);
			base -= 8;
		case 'x':
			return _citrus_prop_read_chr_common(ms, result, base);
			
		default:
			/* unknown escape */
			*result = ch;
		}
	}
	return 0;
}

static int
_citrus_prop_read_character(struct _memstream * __restrict ms,
	_citrus_prop_object_t * __restrict obj)
{
	int ch, errnum;

	_DIAGASSERT(ms != NULL);
	_DIAGASSERT(obj != NULL);

	_memstream_skip_ws(ms);
	ch = _memstream_getc(ms);
	if (ch != '\'') {
		_memstream_ungetc(ms, ch);
		return _citrus_prop_read_chr(ms, obj);
	}
	errnum = _citrus_prop_read_character_common(ms, &ch);
	if (errnum != 0)
		return errnum;
	obj->u.chr = ch;
	ch = _memstream_getc(ms);
	if (ch != '\'')
		return EINVAL;
	return 0;
}

static int
_citrus_prop_read_bool(struct _memstream * __restrict ms,
	_citrus_prop_object_t * __restrict obj)
{
	_DIAGASSERT(ms != NULL);
	_DIAGASSERT(obj != NULL);

	_memstream_skip_ws(ms);
	switch (_bcs_tolower(_memstream_getc(ms))) {
	case 't':
		if (_bcs_tolower(_memstream_getc(ms)) == 'r' &&
		    _bcs_tolower(_memstream_getc(ms)) == 'u' &&
		    _bcs_tolower(_memstream_getc(ms)) == 'e') {
			obj->u.boolean = true;
			return 0;
		}
		break;
	case 'f':
		if (_bcs_tolower(_memstream_getc(ms)) == 'a' &&
		    _bcs_tolower(_memstream_getc(ms)) == 'l' &&
		    _bcs_tolower(_memstream_getc(ms)) == 's' &&
		    _bcs_tolower(_memstream_getc(ms)) == 'e') {
			obj->u.boolean = false;
			return 0;
		}
	}
	return EINVAL;
}

static int
_citrus_prop_read_str(struct _memstream * __restrict ms,
	_citrus_prop_object_t * __restrict obj)
{
	int errnum, quot, ch;
	char *s, *t;
#define _CITRUS_PROP_STR_BUFSIZ	512
	size_t n, m;

	_DIAGASSERT(ms != NULL);
	_DIAGASSERT(obj != NULL);

	m = _CITRUS_PROP_STR_BUFSIZ;
	s = malloc(m);
	if (s == NULL)
		return ENOMEM;
	n = 0;
	_memstream_skip_ws(ms);
	quot = _memstream_getc(ms);
	switch (quot) {
	case EOF:
		goto done;
	case '\\':
		_memstream_ungetc(ms, quot);
		quot = EOF;
	/*FALLTHROUGH*/
	case '\"': case '\'':
		break;
	default:
		s[n] = quot;
		++n, --m;
		quot = EOF;
	}
	for (;;) {
		if (m < 1) {
			m = _CITRUS_PROP_STR_BUFSIZ;
			t = realloc(s, n + m);
			if (t == NULL) {
				free(s);
				return ENOMEM;
			}
			s = t;
		}
		ch = _memstream_getc(ms);
		if (quot == ch || (quot == EOF &&
		    (ch == ';' || _bcs_isspace(ch)))) {
done:
			s[n] = '\0';
			obj->u.str = (const char *)s;
			return 0;
		}
		_memstream_ungetc(ms, ch);
		errnum = _citrus_prop_read_character_common(ms, &ch);
		if (errnum != 0)
			return errnum;
		s[n] = ch;
		++n, --m;
	}
	free(s);
	return EINVAL;
#undef _CITRUS_PROP_STR_BUFSIZ
}

typedef int (*_citrus_prop_read_type_t)(struct _memstream * __restrict,
	_citrus_prop_object_t * __restrict);

static const _citrus_prop_read_type_t readers[] = {
	_citrus_prop_read_bool,
	_citrus_prop_read_str,
	_citrus_prop_read_character,
	_citrus_prop_read_num,
};

static __inline int
_citrus_prop_read_symbol(struct _memstream * __restrict ms,
	char * __restrict s, size_t n)
{
	int ch;
	size_t m;

	_DIAGASSERT(ms != NULL);
	_DIAGASSERT(s != NULL);
	_DIAGASSERT(n > 0);

	for (m = 0; m < n; ++m) {
		ch = _memstream_getc(ms);
		if (ch != '_' && _bcs_isalnum(ch) == 0)
			goto name_found;
		s[m] = ch;
	}
	ch = _memstream_getc(ms);
	if (ch == '_' || _bcs_isalnum(ch) != 0)
		return EINVAL;

name_found:
	_memstream_ungetc(ms, ch);
	s[m] = '\0';

	return 0;
}

static int
_citrus_prop_parse_element(struct _memstream * __restrict ms,
	const _citrus_prop_hint_t * __restrict hints,
	void * __restrict context)
{
	int ch, errnum;
#define _CITRUS_PROP_HINT_NAME_LEN_MAX	255
	char name[_CITRUS_PROP_HINT_NAME_LEN_MAX + 1];
	const _citrus_prop_hint_t *hint;
	_citrus_prop_object_t ostart, oend;

	_DIAGASSERT(ms != NULL);
	_DIAGASSERT(hints != NULL);

	errnum = _citrus_prop_read_symbol(ms, name, sizeof(name));
	if (errnum != 0)
		return errnum;
	for (hint = hints; hint->name != NULL; ++hint) {
		if (_citrus_bcs_strcasecmp(name, hint->name) == 0)
			goto hint_found;
	}
	return EINVAL;

hint_found:
	_memstream_skip_ws(ms);
	ch = _memstream_getc(ms);
	if (ch != '=' && ch != ':')
		_memstream_ungetc(ms, ch);
	do {
		_citrus_prop_object_init(&ostart, hint->type);
		_citrus_prop_object_init(&oend, hint->type);
		errnum = (*readers[hint->type])(ms, &ostart);
		if (errnum != 0)
			return errnum;
		_memstream_skip_ws(ms);
		ch = _memstream_getc(ms);
		switch (hint->type) {
		case _CITRUS_PROP_BOOL:
		case _CITRUS_PROP_STR:
			break;
		default:
			if (ch != '-')
				break;
			errnum = (*readers[hint->type])(ms, &oend);
			if (errnum != 0)
				return errnum;
			_memstream_skip_ws(ms);
			ch = _memstream_getc(ms);
		}
#define CALL0(_func_)					\
do {							\
	_DIAGASSERT(hint->cb._func_.func != NULL);	\
	errnum = (*hint->cb._func_.func)(context,	\
	    hint->name,	ostart.u._func_);		\
} while (0)
#define CALL1(_func_)					\
do {							\
	_DIAGASSERT(hint->cb._func_.func != NULL);	\
	errnum = (*hint->cb._func_.func)(context,	\
	    hint->name,	ostart.u._func_, oend.u._func_);\
} while (0)

		switch (hint->type) {

		case _CITRUS_PROP_BOOL:
			CALL0(boolean);
			break;

		case _CITRUS_PROP_STR:
			CALL0(str);
			break;

		case _CITRUS_PROP_CHR:
			CALL1(chr);
			break;

		case _CITRUS_PROP_NUM:
			CALL1(num);
			break;

		default:
			abort();
			/*NOTREACHED*/
		}
#undef CALL0
#undef CALL1
		_citrus_prop_object_uninit(&ostart);
		_citrus_prop_object_uninit(&oend);
		if (errnum != 0)
			return errnum;
	} while (ch == ',');
	if (ch != ';')
		_memstream_ungetc(ms, ch);
	return 0;
}

int
_citrus_prop_parse_variable(const _citrus_prop_hint_t * __restrict hints,
	void * __restrict context, const void *var, size_t lenvar)
{
	struct _memstream ms;
	int errnum, ch;

	_DIAGASSERT(hints != NULL);

	_memstream_bind_ptr(&ms, __UNCONST(var), lenvar);
	for (;;) {
		_memstream_skip_ws(&ms);
		ch = _memstream_getc(&ms);
		if (ch == EOF || ch == '\0')
			break;
		_memstream_ungetc(&ms, ch);
		errnum = _citrus_prop_parse_element(&ms, hints, context);
		if (errnum != 0)
			return errnum;
	}
	return 0;
}