2 * Copyright (C) 2013 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file error2.c Simple program to create error2.h. */
20 #define log(fmt, ...) fprintf(stderr, "%s: " fmt, __FUNCTION__, ## __VA_ARGS__)
22 #define log(...) do {;} while (0)
25 #define HASH_TABLE_BITS 8
26 #define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS)
28 /* number of executables seen so far */
31 struct hash_table_entry {
33 /* only used for objecs, not for executables */
37 static struct hash_table_entry exe_table[HASH_TABLE_SIZE];
38 static struct hash_table_entry obj_table[HASH_TABLE_SIZE];
40 /* no need for anything sophisticated here */
41 static int hash_token(const char *tok)
43 uint32_t tmp = 31415927;
44 const uint8_t *src = (typeof(src))tok;
50 return tmp % HASH_TABLE_SIZE;
53 static inline bool slot_empty(int idx, struct hash_table_entry *table)
55 return table[idx].key == NULL;
58 static char *safe_strdup(const char *str)
60 char *result = strdup(str);
68 static bool lookup(const char *tok, struct hash_table_entry *table, int *idx)
70 int i, h = hash_token(tok);
72 for (i = 0; i < HASH_TABLE_SIZE; i++) {
73 *idx = (h + i) % HASH_TABLE_SIZE;
74 if (slot_empty(*idx, table))
76 if (!strcmp(table[*idx].key, tok))
79 log ("hash table full !?\n");
83 static bool insert(const char *tok, struct hash_table_entry *table, int *idx)
85 if (lookup(tok, table, idx))
86 return false; /* not inserted */
87 table[*idx].key = safe_strdup(tok);
91 static void process_token(char *tok)
94 size_t len = strlen(tok);
97 if (tok[len - 1] == ':') {
99 if (insert(tok, exe_table, &idx)) { /* new exe */
100 log("exe #%d: '%s', idx: %d\n", num_exe, tok, idx);
105 log("invalid input\n");
108 insert(tok, obj_table, &idx);
109 obj_table[idx].exe_bitmask |= (1 << (num_exe - 1));
113 static void print_ss_enum(int idx)
115 char *s = obj_table[idx].key;
119 printf("%c", toupper(*s));
123 static void dump_bipolar(void)
127 for (i = 0; i < HASH_TABLE_SIZE; i++) {
128 if (slot_empty(i, obj_table))
130 printf("#ifdef MAIN_INPUT_FILE_IS_%s\n", obj_table[i].key);
131 for (j = 0; j < HASH_TABLE_SIZE; j++) {
133 if (slot_empty(j, obj_table))
135 mi = obj_table[i].exe_bitmask;
136 mj = obj_table[j].exe_bitmask;
145 * The main function of error2.c.
147 * The purpose of this program is to create the error2.h file which defines the
148 * enumerations of all error codes which may be used by any given .c file. This
149 * header is included by most .c files of the paraslash suite.
151 * Since this program is executed on the build system, it must be compiled with
154 * \return \p EXIT_SUCCESS or \p EXIT_FAILURE.
162 ret = scanf("%96s", tok);