04d311e55e096d85a2d3c6fb2f67cc958def0c91
[YACASL2.git] / test / unit / print_cmdtype_code / print_cmdtype_code.c
1 #include "casl2.h"
2 #include "cerr.h"
3 #include <string.h>
4
5 int compare_code(const void *a, const void *b)
6 {
7     const CMDTYPECODE ca = **(const CMDTYPECODE **)a;
8     const CMDTYPECODE cb = **(const CMDTYPECODE **)b;
9     int diff;
10     if((diff = strcmp(ca.cmd, cb.cmd)) == 0) {
11         return ca.type - cb.type;
12     } else {
13         return diff;
14     }
15 }
16
17 /* 命令と命令タイプがキーのハッシュ表を表示する */
18 void print_cmdtype_code()
19 {
20     int i, j = 0;
21     CMDCODETAB *np;
22     CMDTYPECODE **ar;
23     ar = malloc(sizeof(*ar) * cmdtypecodesize);
24     for(i = 0; i < cmdtabsize; i++) {
25         np = cmdtype_code[i];
26         while(np != NULL) {
27             ar[j++] = np->cmdtypecode;
28             np = np->next;
29         }
30     }
31     qsort(ar, cmdcodesize, sizeof(*ar), (int (*)(const void*, const void*))compare_code);
32     for(i = 0; i < cmdcodesize; i++) {
33         fprintf(stdout, "%s\t0%02o\t#%04X\n", ar[i]->cmd, ar[i]->type, ar[i]->code);
34     }
35 }
36
37 int main()
38 {
39     /* エラーの初期化 */
40     cerr = malloc_chk(sizeof(CERR), "cerr");
41     /* ハッシュ表作成 */
42     create_cmdtype_code();
43     /* 命令表の表示 */
44     print_cmdtype_code();
45     /* ハッシュ表解放 */
46     free_cmdtype_code();
47     if(cerr->num != 0) {
48         printf("\terror - %d: %s\n", cerr->num, cerr->msg);
49         freecerr();
50         exit(-1);
51     }
52     return 0;
53 }