2 * Copyright (C) 2013-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file error2.c Simple program to create error2.h. */
22 #define log(fmt, ...) fprintf(stderr, "%s: " fmt, __FUNCTION__, ## __VA_ARGS__)
24 #define log(...) do {;} while (0)
27 #define HASH_TABLE_BITS 8
28 #define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS)
30 /* number of executables seen so far */
33 struct hash_table_entry {
35 /* only used for objecs, not for executables */
39 static struct hash_table_entry exe_table[HASH_TABLE_SIZE];
40 static struct hash_table_entry obj_table[HASH_TABLE_SIZE];
42 /* no need for anything sophisticated here */
43 static int hash_token(const char *tok)
45 uint32_t tmp = 31415927;
46 const uint8_t *src = (typeof(src))tok;
52 return tmp % HASH_TABLE_SIZE;
55 static inline bool slot_empty(int idx, struct hash_table_entry *table)
57 return table[idx].key == NULL;
60 static char *safe_strdup(const char *str)
62 char *result = strdup(str);
70 static bool lookup(const char *tok, struct hash_table_entry *table, int *idx)
72 int i, h = hash_token(tok);
74 for (i = 0; i < HASH_TABLE_SIZE; i++) {
75 *idx = (h + i) % HASH_TABLE_SIZE;
76 if (slot_empty(*idx, table))
78 if (!strcmp(table[*idx].key, tok))
81 log ("hash table full !?\n");
85 static bool insert(const char *tok, struct hash_table_entry *table, int *idx)
87 if (lookup(tok, table, idx))
88 return false; /* not inserted */
89 table[*idx].key = safe_strdup(tok);
93 static void process_token(char *tok)
96 size_t len = strlen(tok);
99 if (tok[len - 1] == ':') {
101 if (insert(tok, exe_table, &idx)) { /* new exe */
102 log("exe #%d: '%s', idx: %d\n", num_exe, tok, idx);
107 log("invalid input\n");
110 insert(tok, obj_table, &idx);
111 obj_table[idx].exe_bitmask |= (1 << (num_exe - 1));
115 static void print_ss_enum(int idx)
117 char *s = obj_table[idx].key;
121 printf("%c", toupper(*s));
125 static void dump_bipolar(void)
129 for (i = 0; i < HASH_TABLE_SIZE; i++) {
130 if (slot_empty(i, obj_table))
132 printf("#ifdef MAIN_INPUT_FILE_IS_%s\n", obj_table[i].key);
133 for (j = 0; j < HASH_TABLE_SIZE; j++) {
135 if (slot_empty(j, obj_table))
137 mi = obj_table[i].exe_bitmask;
138 mj = obj_table[j].exe_bitmask;
147 * The main function of error2.c.
149 * The purpose of this program is to create the error2.h file which defines the
150 * enumerations of all error codes which may be used by any given .c file. This
151 * header is included by most .c files of the paraslash suite.
153 * Since this program is executed on the build system, it must be compiled with
156 * \return \p EXIT_SUCCESS or \p EXIT_FAILURE.
164 ret = scanf("%96s", tok);