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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright 2005 Colin Percival
 * All rights reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted providing 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 ``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 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>
__FBSDID("$FreeBSD$");

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct port;

typedef union {
	char * name;
	struct port * p;
} DEP;

typedef struct port {
	char * pkgname;
	char * portdir;
	char * prefix;
	char * comment;
	char * pkgdescr;
	char * maintainer;
	char * categories;
	size_t n_edep;
	DEP * edep;
	size_t n_pdep;
	DEP * pdep;
	size_t n_fdep;
	DEP * fdep;
	size_t n_bdep;
	DEP * bdep;
	size_t n_rdep;
	DEP * rdep;
	char * www;
	int recursed;
} PORT;

static void usage(void);
static char * strdup2(const char *str);
static DEP * makelist(char * str, size_t * n);
static PORT * portify(char * line);
static int portcompare(char * a, char * b);
static void heapifyports(PORT **pp, size_t size, size_t pos);
static PORT * findport(PORT ** pp, size_t st, size_t en, char * name, char * from);
static void translateport(PORT ** pp, size_t pplen, PORT * p);
static DEP * recurse_one(DEP * d, size_t * nd);
static void recurse(PORT * p);
static void heapifypkgs(DEP * d, size_t size, size_t pos);
static void sortpkgs(DEP * d, size_t nd);
static void printport(PORT * p);

static void
usage(void)
{

	fprintf(stderr, "usage: make_index file\n");
	exit(1);
	/* NOTREACHED */
}

static char *
strdup2(const char *str)
{
	char * r;

	r = strdup(str);
	if (r == NULL)
		err(1, "strdup");
	return r;
}

/* Take a space-separated list and return an array of (char *) */
static DEP *
makelist(char * str, size_t * n)
{
	DEP * d;
	size_t i;

	/* No depends at all? */
	if (str[0] == 0) {
		*n = 0;
		return NULL;
	}

	/* Count the number of fields */
	*n = 1;
	for (i = 0; str[i] != 0; i++)
		if (str[i] == ' ')
			(*n)++;

	/* Allocate and fill an array */
	d = malloc(*n * sizeof(DEP));
	if (d == NULL)
		err(1, "malloc(DEP)");
	for (i = 0; i < *n; i++) {
		d[i].name = strdup2(strsep(&str, " "));

		/* Strip trailing slashes */
		if (d[i].name[strlen(d[i].name) - 1] == '/')
			d[i].name[strlen(d[i].name) - 1] = 0;
	}

	return d;
}

/* Take a port's describe line and split it into fields */
static PORT *
portify(char * line)
{
	PORT * p;
	size_t i, n;

	/* Verify that line has the right number of fields */
	for (n = i = 0; line[i] != 0; i++)
		if (line[i] == '|')
			n++;
	if (n != 12)
		errx(1, "Port describe line is corrupt:\n%s\n", line);

	p = malloc(sizeof(PORT));
	if (p == NULL)
		err(1, "malloc(PORT)");

	p->pkgname = strdup2(strsep(&line, "|"));
	p->portdir = strdup2(strsep(&line, "|"));
	p->prefix = strdup2(strsep(&line, "|"));
	p->comment = strdup2(strsep(&line, "|"));
	p->pkgdescr = strdup2(strsep(&line, "|"));
	p->maintainer = strdup2(strsep(&line, "|"));
	p->categories = strdup2(strsep(&line, "|"));
	p->edep = makelist(strsep(&line, "|"), &p->n_edep);
	p->pdep = makelist(strsep(&line, "|"), &p->n_pdep);
	p->fdep = makelist(strsep(&line, "|"), &p->n_fdep);
	p->bdep = makelist(strsep(&line, "|"), &p->n_bdep);
	p->rdep = makelist(strsep(&line, "|"), &p->n_rdep);
	p->www = strdup2(strsep(&line, "|"));

	p->recursed = 0;

	/*
	 * line will now be equal to NULL -- we counted the field
	 * separators at the top of the function.
	 */

	return p;
}

/* Returns -1, 0, or 1 based on a comparison of the portdir strings */
static int
portcompare(char * a, char * b)
{
	size_t i;

	/* Find first non-matching position */
	for (i = 0; ; i++) {
		if (a[i] != b[i])
			break;
		if (a[i] == 0)			/* End of strings */
			return 0;
	}

	/* One string is a prefix of the other */
	if (a[i] == 0)
		return -1;
	if (b[i] == 0)
		return 1;

	/* One string has a category which is a prefix of the other */
	if (a[i] == '/')
		return -1;
	if (b[i] == '/')
		return 1;

	/* The two strings are simply different */
	if (a[i] < b[i])
		return -1;
	else
		return 1;
}

/* Heapify (PORT *) number pos in a pseudo-heap pp[0]..pp[size - 1] */
static void
heapifyports(PORT **pp, size_t size, size_t pos)
{
	size_t i = pos;
	PORT * tmp;

top:
	/* Find the largest value out of {pos, 2*pos+1, 2*pos+2} */
	if ((2 * pos + 1 < size) &&
	    (portcompare(pp[i]->portdir, pp[2 * pos + 1]->portdir) < 0))
		i = 2 * pos + 1;
	if ((2 * pos + 2 < size) &&
	    (portcompare(pp[i]->portdir, pp[2 * pos + 2]->portdir) < 0))
		i = 2 * pos + 2;

	/* If necessary, swap elements and iterate down the tree. */
	if (i != pos) {
		tmp = pp[pos];
		pp[pos] = pp[i];
		pp[i] = tmp;
		pos = i;
		goto top;
	}
}

/* Translate a port directory name into a (PORT *), and free the name */
static PORT *
findport(PORT ** pp, size_t st, size_t en, char * name, char * from)
{
	size_t mid;
	int r;

	if (st == en)
		errx(1, "%s: no entry for %s", from, name);

	mid = (st + en) / 2;
	r = portcompare(pp[mid]->portdir, name);

	if (r == 0) {
		free(name);
		return pp[mid];
	} else if (r < 0)
		return findport(pp, mid + 1, en, name, from);
	else
		return findport(pp, st, mid, name, from);
}

/* Translate all depends from names into PORT *s */
static void
translateport(PORT ** pp, size_t pplen, PORT * p)
{
	size_t i;

	for (i = 0; i < p->n_edep; i++)
		p->edep[i].p = findport(pp, 0, pplen, p->edep[i].name, p->portdir);
	for (i = 0; i < p->n_pdep; i++)
		p->pdep[i].p = findport(pp, 0, pplen, p->pdep[i].name, p->portdir);
	for (i = 0; i < p->n_fdep; i++)
		p->fdep[i].p = findport(pp, 0, pplen, p->fdep[i].name, p->portdir);
	for (i = 0; i < p->n_bdep; i++)
		p->bdep[i].p = findport(pp, 0, pplen, p->bdep[i].name, p->portdir);
	for (i = 0; i < p->n_rdep; i++)
		p->rdep[i].p = findport(pp, 0, pplen, p->rdep[i].name, p->portdir);
}

/* Recurse on one specific depends list */
static DEP *
recurse_one(DEP * d, size_t * nd)
{
	size_t i, j, k, n, N;

	N = n = *nd;
	for (i = 0; i < n; i++) {
		recurse(d[i].p);
		for (j = 0; j < d[i].p->n_rdep; j++) {
			for (k = 0; k < N; k++) {
				if (d[i].p->rdep[j].p == d[k].p)
					break;
			}
			if (k == N) {
				N++;
				if (N >= *nd) {
					*nd += *nd;
					d = realloc(d, *nd * sizeof(DEP));
					if (d == NULL)
						err(1, "realloc(d)");
				}
				d[k].p = d[i].p->rdep[j].p;
			}
		}
	}
	*nd = N;

	return d;
}

/* Recurse on the depends lists */
static void
recurse(PORT * p)
{
	switch (p->recursed) {
	case 0:
		/* First time we've seen this port */
		p->recursed = 1;
		break;
	case 1:
		/* We're in the middle of recursing this port */
		errx(1, "Circular dependency loop found: %s"
		    " depends upon itself.\n", p->pkgname);
	case 2:
		/* This port has already been recursed */
		return;
	}

	p->edep = recurse_one(p->edep, &p->n_edep);
	p->pdep = recurse_one(p->pdep, &p->n_pdep);
	p->fdep = recurse_one(p->fdep, &p->n_fdep);
	p->bdep = recurse_one(p->bdep, &p->n_bdep);
	p->rdep = recurse_one(p->rdep, &p->n_rdep);

	/* Finished recursing on this port */
	p->recursed = 2;
}

/* Heapify an element in a package list */
static void
heapifypkgs(DEP * d, size_t size, size_t pos)
{
	size_t i = pos;
	PORT * tmp;

top:
	/* Find the largest value out of {pos, 2*pos+1, 2*pos+2} */
	if ((2 * pos + 1 < size) &&
	    (strcmp(d[i].p->pkgname, d[2 * pos + 1].p->pkgname) < 0))
		i = 2 * pos + 1;
	if ((2 * pos + 2 < size) &&
	    (strcmp(d[i].p->pkgname, d[2 * pos + 2].p->pkgname) < 0))
		i = 2 * pos + 2;

	/* If necessary, swap elements and iterate down the tree. */
	if (i != pos) {
		tmp = d[pos].p;
		d[pos].p = d[i].p;
		d[i].p = tmp;
		pos = i;
		goto top;
	}
}

/* Sort a list of dependent packages in alphabetical order */
static void
sortpkgs(DEP * d, size_t nd)
{
	size_t i;
	PORT * tmp;

	if (nd == 0)
		return;

	for (i = nd; i > 0; i--)
		heapifypkgs(d, nd, i - 1);	/* Build a heap */
	for (i = nd - 1; i > 0; i--) {
		tmp = d[0].p;			/* Extract elements */
		d[0].p = d[i].p;
		d[i].p = tmp;
		heapifypkgs(d, i, 0);		/* And re-heapify */
	}
}

/* Output an index line for the given port. */
static void
printport(PORT * p)
{
	size_t i;

	sortpkgs(p->edep, p->n_edep);
	sortpkgs(p->pdep, p->n_pdep);
	sortpkgs(p->fdep, p->n_fdep);
	sortpkgs(p->bdep, p->n_bdep);
	sortpkgs(p->rdep, p->n_rdep);

	printf("%s|%s|%s|%s|%s|%s|%s|",
	    p->pkgname, p->portdir, p->prefix, p->comment, p->pkgdescr, 
	    p->maintainer, p->categories);
	for (i = 0; i < p->n_bdep; i++)
		printf("%s%s", i ? " " : "", p->bdep[i].p->pkgname);
	printf("|");
	for (i = 0; i < p->n_rdep; i++)
		printf("%s%s", i ? " " : "", p->rdep[i].p->pkgname);
	printf("|");
	printf("%s|", p->www);
	for (i = 0; i < p->n_edep; i++)
		printf("%s%s", i ? " " : "", p->edep[i].p->pkgname);
	printf("|");
	for (i = 0; i < p->n_pdep; i++)
		printf("%s%s", i ? " " : "", p->pdep[i].p->pkgname);
	printf("|");
	for (i = 0; i < p->n_fdep; i++)
		printf("%s%s", i ? " " : "", p->fdep[i].p->pkgname);
	printf("\n");
}

/*
 * Algorithm:
 * 1. Suck in all the data, splitting into fields.
 * 1a. If there are no ports, there is no INDEX.
 * 2. Sort the ports according to port directory.
 * 3. Using a binary search, translate each dependency from a
 * port directory name into a pointer to a port.
 * 4. Recursively follow dependencies, expanding the lists of
 * pointers as needed (using realloc).
 * 5. Iterate through the ports, printing them out (remembering
 * to list the dependent ports in alphabetical order).
 */

int
main(int argc, char *argv[])
{
	FILE * f;
	char * line;
	size_t linelen;
	PORT ** pp;	/* Array of pointers to PORTs */
	PORT * tmp;
	size_t pplen;	/* Allocated size of array */
	size_t i;

	if (argc != 2)
		usage();
	if ((f = fopen(argv[1], "r")) == NULL)
		err(1, "fopen(%s)", argv[1]);

	pplen = 1024;
	if ((pp = malloc(pplen * sizeof(PORT *))) == NULL)
		err(1, "malloc(pp)");

	/*
	 * 1. Suck in all the data, splitting into fields.
	 */
	for(i = 0; (line = fgetln(f, &linelen)) != NULL; i++) {
		if (line[linelen - 1] != '\n')
			errx(1, "Unterminated line encountered");
		line[linelen - 1] = 0;

		/* Enlarge array if needed */
		if (i >= pplen) {
			pplen *= 2;
			if ((pp = realloc(pp, pplen * sizeof(PORT *))) == NULL)
				err(1, "realloc(pp)");
		}

		pp[i] = portify(line);
	}
	/* Reallocate to the correct size */
	pplen = i;
	if ((pp = realloc(pp, pplen * sizeof(PORT *))) == NULL)
		err(1, "realloc(pp)");

	/* Make sure we actually reached the EOF */
	if (!feof(f))
		err(1, "fgetln(%s)", argv[1]);
	/* Close the describes file */
	if (fclose(f) != 0)
		err(1, "fclose(%s)", argv[1]);

	/*
	 * 1a. If there are no ports, there is no INDEX.
	 */
	if (pplen == 0)
		return 0;

	/*
	 * 2. Sort the ports according to port directory.
	 */
	for (i = pplen; i > 0; i--)
		heapifyports(pp, pplen, i - 1);	/* Build a heap */
	for (i = pplen - 1; i > 0; i--) {
		tmp = pp[0];			/* Extract elements */
		pp[0] = pp[i];
		pp[i] = tmp;
		heapifyports(pp, i, 0);		/* And re-heapify */
	}

	/*
	 * 3. Using a binary search, translate each dependency from a
	 * port directory name into a pointer to a port.
	 */
	for (i = 0; i < pplen; i++)
		translateport(pp, pplen, pp[i]);

	/*
	 * 4. Recursively follow dependencies, expanding the lists of
	 * pointers as needed (using realloc).
	 */
	for (i = 0; i < pplen; i++)
		recurse(pp[i]);

	/*
	 * 5. Iterate through the ports, printing them out (remembering
	 * to list the dependent ports in alphabetical order).
	 */
	for (i = 0; i < pplen; i++)
		printport(pp[i]);

	return 0;
}