diff options
Diffstat (limited to 'usr.bin/colldef/parse.y')
-rw-r--r-- | usr.bin/colldef/parse.y | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/usr.bin/colldef/parse.y b/usr.bin/colldef/parse.y index 95e4994..c180f82 100644 --- a/usr.bin/colldef/parse.y +++ b/usr.bin/colldef/parse.y @@ -29,6 +29,7 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include <arpa/inet.h> #include <err.h> #include <stdarg.h> #include <stdio.h> @@ -58,7 +59,6 @@ u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN]; struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1]; struct __collate_st_chain_pri *__collate_chain_pri_table; -int nchains = TABLE_SIZE; int chain_index; int prim_pri = 1, sec_pri = 1; #ifdef COLLATE_DEBUG @@ -105,6 +105,7 @@ substitute : SUBSTITUTE CHAR WITH STRING { order : ORDER order_list { FILE *fp; int ch, substed, ordered; + uint32_t u32; for (ch = 0; ch < UCHAR_MAX + 1; ch++) { substed = (__collate_substitute_table[ch][0] != ch); @@ -115,15 +116,29 @@ order : ORDER order_list { yyerror("Char 0x%02x can't be ordered since substituted", ch); } + if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table, + sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL) + yyerror("can't grow chain table"); + (void)memset(__collate_chain_pri_table[chain_index].str, 0, + sizeof(__collate_chain_pri_table[0].str)); + __collate_chain_pri_table[chain_index].prim = 0; + __collate_chain_pri_table[chain_index].sec = 0; + chain_index++; + if ((fp = fopen(out_file, "w")) == NULL) err(EX_UNAVAILABLE, "can't open destination file %s", out_file); - strcpy(__collate_version, COLLATE_VERSION); + strcpy(__collate_version, COLLATE_VERSION1_1); if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1) err(EX_IOERR, "IO error writting collate version to destination file %s", out_file); + u32 = htonl(chain_index); + if (fwrite(&u32, sizeof(u32), 1, fp) != 1) + err(EX_IOERR, + "IO error writting chains number to destination file %s", + out_file); if (fwrite(__collate_substitute_table, sizeof(__collate_substitute_table), 1, fp) != 1) err(EX_IOERR, @@ -135,7 +150,8 @@ order : ORDER order_list { "IO error writting char table to destination file %s", out_file); if (fwrite(__collate_chain_pri_table, - sizeof(*__collate_chain_pri_table), nchains, fp) != nchains) + sizeof(*__collate_chain_pri_table), chain_index, fp) != + chain_index) err(EX_IOERR, "IO error writting chain table to destination file %s", out_file); @@ -176,10 +192,15 @@ item : CHAR { __collate_char_pri_table[$1].prim = prim_pri++; } | chain { - if (chain_index >= TABLE_SIZE - 1) - yyerror("__collate_chain_pri_table overflow"); + if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table, + sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL) + yyerror("can't grow chain table"); + (void)memset(__collate_chain_pri_table[chain_index].str, 0, + sizeof(__collate_chain_pri_table[0].str)); (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain); - __collate_chain_pri_table[chain_index++].prim = prim_pri++; + __collate_chain_pri_table[chain_index].prim = prim_pri++; + __collate_chain_pri_table[chain_index].sec = 0; + chain_index++; } | CHAR RANGE CHAR { u_int i; @@ -226,10 +247,15 @@ prim_sub_item : CHAR { } } | chain { - if (chain_index >= TABLE_SIZE - 1) - yyerror("__collate_chain_pri_table overflow"); + if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table, + sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL) + yyerror("can't grow chain table"); + (void)memset(__collate_chain_pri_table[chain_index].str, 0, + sizeof(__collate_chain_pri_table[0].str)); (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain); - __collate_chain_pri_table[chain_index++].prim = prim_pri; + __collate_chain_pri_table[chain_index].prim = prim_pri; + __collate_chain_pri_table[chain_index].sec = 0; + chain_index++; } ; sec_sub_item : CHAR { @@ -253,11 +279,15 @@ sec_sub_item : CHAR { } } | chain { - if (chain_index >= TABLE_SIZE - 1) - yyerror("__collate_chain_pri_table overflow"); + if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table, + sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL) + yyerror("can't grow chain table"); + (void)memset(__collate_chain_pri_table[chain_index].str, 0, + sizeof(__collate_chain_pri_table[0].str)); (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain); __collate_chain_pri_table[chain_index].prim = prim_pri; - __collate_chain_pri_table[chain_index++].sec = sec_pri++; + __collate_chain_pri_table[chain_index].sec = sec_pri++; + chain_index++; } ; %% @@ -296,9 +326,6 @@ main(int ac, char **av) if ((yyin = fopen(*av, "r")) == NULL) err(EX_UNAVAILABLE, "can't open source file %s", *av); } - if ((__collate_chain_pri_table = - calloc(nchains, sizeof(*__collate_chain_pri_table))) == NULL) - err(EX_OSERR, "can't allocate chain table"); for (ch = 0; ch <= UCHAR_MAX; ch++) __collate_substitute_table[ch][0] = ch; yyparse(); @@ -337,7 +364,7 @@ collate_print_tables(void) printf("\t'%c' --> \"%s\"\n", i, __collate_substitute_table[i]); printf("Chain priority table:\n"); - for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) + for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++) printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec); printf("Char priority table:\n"); for (i = 0; i < UCHAR_MAX + 1; i++) |