int main(int argc, char **argv) { struct name_table_entry *name_table = typeCalloc(struct name_table_entry, CAPTABSIZE); HashValue *hash_table = typeCalloc(HashValue, HASHTABSIZE); const char *root_name = ""; int column = 0; int bigstring = 0; int n; char buffer[BUFSIZ]; static const char *typenames[] = {"BOOLEAN", "NUMBER", "STRING"}; short BoolCount = 0; short NumCount = 0; short StrCount = 0; /* The first argument is the column-number (starting with 0). * The second is the root name of the tables to generate. */ if (argc <= 3 || (column = atoi(argv[1])) <= 0 || (column >= MAX_COLUMNS) || *(root_name = argv[2]) == 0 || (bigstring = atoi(argv[3])) < 0 || name_table == 0 || hash_table == 0) { fprintf(stderr, "usage: make_hash column root_name bigstring\n"); exit(EXIT_FAILURE); } /* * Read the table into our arrays. */ for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) { char **list, *nlp = strchr(buffer, '\n'); if (nlp) *nlp = '\0'; list = parse_columns(buffer); if (list == 0) /* blank or comment */ continue; if (column > count_columns(list)) { fprintf(stderr, "expected %d columns, have %d:\n%s\n", column, count_columns(list), buffer); exit(EXIT_FAILURE); } name_table[n].nte_link = -1; /* end-of-hash */ name_table[n].nte_name = strmalloc(list[column]); if (!strcmp(list[2], "bool")) { name_table[n].nte_type = BOOLEAN; name_table[n].nte_index = BoolCount++; } else if (!strcmp(list[2], "num")) { name_table[n].nte_type = NUMBER; name_table[n].nte_index = NumCount++; } else if (!strcmp(list[2], "str")) { name_table[n].nte_type = STRING; name_table[n].nte_index = StrCount++; } else { fprintf(stderr, "Unknown type: %s\n", list[2]); exit(EXIT_FAILURE); } n++; } _nc_make_hash_table(name_table, hash_table); /* * Write the compiled tables to standard output */ if (bigstring) { int len = 0; int nxt; printf("static const char %s_names_text[] = \\\n", root_name); for (n = 0; n < CAPTABSIZE; n++) { nxt = (int) strlen(name_table[n].nte_name) + 5; if (nxt + len > 72) { printf("\\\n"); len = 0; } printf("\"%s\\0\" ", name_table[n].nte_name); len += nxt; } printf(";\n\n"); len = 0; printf("static name_table_data const %s_names_data[] =\n", root_name); printf("{\n"); for (n = 0; n < CAPTABSIZE; n++) { printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n", len, typenames[name_table[n].nte_type], name_table[n].nte_index, name_table[n].nte_link, n < CAPTABSIZE - 1 ? ',' : ' '); len += (int) strlen(name_table[n].nte_name) + 1; } printf("};\n\n"); printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name); } else { printf("static struct name_table_entry const _nc_%s_table[] =\n", root_name); printf("{\n"); for (n = 0; n < CAPTABSIZE; n++) { _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "\"%s\"", name_table[n].nte_name); printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n", buffer, typenames[name_table[n].nte_type], name_table[n].nte_index, name_table[n].nte_link, n < CAPTABSIZE - 1 ? ',' : ' '); } printf("};\n\n"); } printf("static const HashValue _nc_%s_hash_table[%d] =\n", root_name, HASHTABSIZE + 1); printf("{\n"); for (n = 0; n < HASHTABSIZE; n++) { printf("\t%3d,\n", hash_table[n]); } printf("\t0\t/* base-of-table */\n"); printf("};\n\n"); printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n", BoolCount, NumCount, StrCount); printf("#error\t--> term.h and comp_captab.c disagree about the <--\n"); printf("#error\t--> numbers of booleans, numbers and/or strings <--\n"); printf("#endif\n\n"); free(hash_table); return EXIT_SUCCESS; }
int main(int argc, char **argv) { struct name_table_entry *name_table = typeCalloc(struct name_table_entry, CAPTABSIZE); struct name_table_entry **hash_table = typeCalloc(struct name_table_entry *, HASHTABSIZE); const char *root_name = ""; int column = 0; int n; char buffer[BUFSIZ]; static const char * typenames[] = { "BOOLEAN", "NUMBER", "STRING" }; short BoolCount = 0; short NumCount = 0; short StrCount = 0; /* The first argument is the column-number (starting with 0). * The second is the root name of the tables to generate. */ if (argc <= 2 || (column = atoi(argv[1])) <= 0 || (column >= MAX_COLUMNS) || *(root_name = argv[2]) == 0) { fprintf(stderr, "usage: make_hash column root_name\n"); exit(EXIT_FAILURE); } /* * Read the table into our arrays. */ for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin); ) { char **list, *nlp = strchr(buffer, '\n'); if (nlp) *nlp = '\0'; list = parse_columns(buffer); if (list == 0) /* blank or comment */ continue; name_table[n].nte_link = -1; /* end-of-hash */ name_table[n].nte_name = strdup(list[column]); if (!strcmp(list[2], "bool")) { name_table[n].nte_type = BOOLEAN; name_table[n].nte_index = BoolCount++; } else if (!strcmp(list[2], "num")) { name_table[n].nte_type = NUMBER; name_table[n].nte_index = NumCount++; } else if (!strcmp(list[2], "str")) { name_table[n].nte_type = STRING; name_table[n].nte_index = StrCount++; } else { fprintf(stderr, "Unknown type: %s\n", list[2]); exit(EXIT_FAILURE); } n++; } _nc_make_hash_table(name_table, hash_table); /* * Write the compiled tables to standard output */ printf("static struct name_table_entry const _nc_%s_table[] =\n", root_name); printf("{\n"); for (n = 0; n < CAPTABSIZE; n++) { sprintf(buffer, "\"%s\"", name_table[n].nte_name); printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n", buffer, typenames[name_table[n].nte_type], name_table[n].nte_index, name_table[n].nte_link, n < CAPTABSIZE - 1 ? ',' : ' '); } printf("};\n\n"); printf("const struct name_table_entry * const _nc_%s_hash_table[%d] =\n", root_name, HASHTABSIZE+1); printf("{\n"); for (n = 0; n < HASHTABSIZE; n++) { if (hash_table[n] != 0) { sprintf(buffer, "_nc_%s_table + %3ld", root_name, (long) (hash_table[n] - name_table)); } else { strcpy(buffer, "0"); } printf("\t%s,\n", buffer); } printf("\t_nc_%s_table\t/* base-of-table */\n", root_name); printf("};\n\n"); printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n", BoolCount, NumCount, StrCount); printf("#error\t--> term.h and comp_captab.c disagree about the <--\n"); printf("#error\t--> numbers of booleans, numbers and/or strings <--\n"); printf("#endif\n\n"); return EXIT_SUCCESS; }