Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: MIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* utility to create the register check tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * this includes inlined list.h safe for userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2009 Jerome Glisse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright 2009 Red Hat Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * 	Jerome Glisse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * 	Dave Airlie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <regex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <libgen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * container_of - cast a member of a structure out to the containing structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * @ptr:    the pointer to the member.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @type:   the type of the container struct this is embedded in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * @member: the name of the member within the struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define container_of(ptr, type, member) ({          \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	const typeof(((type *)0)->member)*__mptr = (ptr);    \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		     (type *)((char *)__mptr - offsetof(type, member)); })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * Simple doubly linked list implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * Some of the internal functions ("__xxx") are useful when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * manipulating whole lists rather than single entries, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * sometimes we already know the next/prev entries and we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * generate better code by using them directly rather than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * using the generic single-entry routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) struct list_head {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct list_head *next, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static inline void INIT_LIST_HEAD(struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	list->next = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	list->prev = list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * Insert a new entry between two known consecutive entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * This is only for internal list manipulation where we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * the prev/next entries already!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #ifndef CONFIG_DEBUG_LIST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static inline void __list_add(struct list_head *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			      struct list_head *prev, struct list_head *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	next->prev = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	new->next = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	new->prev = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	prev->next = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) extern void __list_add(struct list_head *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		       struct list_head *prev, struct list_head *next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * list_add_tail - add a new entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * @new: new entry to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * @head: list head to add it before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * Insert a new entry before the specified head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * This is useful for implementing queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static inline void list_add_tail(struct list_head *new, struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	__list_add(new, head->prev, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * list_entry - get the struct for this entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * @ptr:	the &struct list_head pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * @type:	the type of the struct this is embedded in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * @member:	the name of the list_head within the struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #define list_entry(ptr, type, member) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	container_of(ptr, type, member)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * list_for_each_entry	-	iterate over list of given type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * @pos:	the type * to use as a loop cursor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @head:	the head for your list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @member:	the name of the list_head within the struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define list_for_each_entry(pos, head, member)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	     &pos->member != (head); 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	     pos = list_entry(pos->member.next, typeof(*pos), member))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct offset {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	unsigned offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct table {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct list_head offsets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	unsigned offset_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned nentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	unsigned *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	char *gpu_prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static struct offset *offset_new(unsigned o)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct offset *offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	offset = (struct offset *)malloc(sizeof(struct offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		INIT_LIST_HEAD(&offset->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		offset->offset = o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void table_offset_add(struct table *t, struct offset *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	list_add_tail(&offset->list, &t->offsets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void table_init(struct table *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	INIT_LIST_HEAD(&t->offsets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	t->offset_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	t->nentry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	t->table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void table_print(struct table *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	unsigned nlloop, i, j, n, c, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	nlloop = (t->nentry + 3) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	c = t->nentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t->gpu_prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	       t->nentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	for (i = 0, id = 0; i < nlloop; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		n = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (n > c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			n = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		c -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		for (j = 0; j < n; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			if (j == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				printf("\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				printf(" ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			printf("0x%08X,", t->table[id++]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	printf("};\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int table_build(struct table *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct offset *offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	unsigned i, m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	t->nentry = ((t->offset_max >> 2) + 31) / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (t->table == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	memset(t->table, 0xff, sizeof(unsigned) * t->nentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	list_for_each_entry(offset, &t->offsets, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		i = (offset->offset >> 2) / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		m = (offset->offset >> 2) & 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		m = 1 << m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		t->table[i] ^= m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static char gpu_name[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int parser_auth(struct table *t, const char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	FILE *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	regex_t mask_rex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	regmatch_t match[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	char buf[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	size_t end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	int done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	unsigned o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct offset *offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	char last_reg_s[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int last_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (regcomp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	    (&mask_rex, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		fprintf(stderr, "Failed to compile regular expression\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	file = fopen(filename, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (file == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		fprintf(stderr, "Failed to open: %s\n", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	fseek(file, 0, SEEK_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	end = ftell(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	fseek(file, 0, SEEK_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	/* get header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (fgets(buf, 1024, file) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* first line will contain the last register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	 * and gpu name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	sscanf(buf, "%9s %9s", gpu_name, last_reg_s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	t->gpu_prefix = gpu_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	last_reg = strtol(last_reg_s, NULL, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (fgets(buf, 1024, file) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		len = strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		if (ftell(file) == end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			r = regexec(&mask_rex, buf, 4, match, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			if (r == REG_NOMATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			} else if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 					"Error matching regular expression %d in %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 					r, filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				buf[match[0].rm_eo] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				buf[match[1].rm_eo] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				buf[match[2].rm_eo] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				o = strtol(&buf[match[1].rm_so], NULL, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				offset = offset_new(o);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				table_offset_add(t, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				if (o > t->offset_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 					t->offset_max = o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	} while (!done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	fclose(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (t->offset_max < last_reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		t->offset_max = last_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	return table_build(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	struct table t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (argc != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		fprintf(stderr, "Usage: %s <authfile>\n", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	table_init(&t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (parser_auth(&t, argv[1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		fprintf(stderr, "Failed to parse file %s\n", argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	table_print(&t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }