「make check」でのテストが、コマンド更新時だけに実行されるよう修正
[YACASL2.git] / src / cerr.c
1 #include "casl2.h"
2
3 /* エラーコードリスト */
4 typedef struct {
5     int num;
6     char *msg;
7 } CERRARRAY;
8 CERRARRAY cerr[] = {
9     { 101, "label already defined" },
10     { 102, "label table is full" },
11     { 103, "label not found" },
12     { 104, "label length is too long" },
13     { 105, "no command in the line" },
14     { 106, "operand count mismatch" },
15     { 107, "no label in START" },
16     { 108, "not command of operand \"r\"" },
17     { 109, "not command of operand \"r1,r2\"" },
18     { 110, "not command of operand \"r,adr[,x]\"" },
19     { 111, "not command of operand \"adr[,x]\"" },
20     { 112, "not command of no operand" },
21     { 113, "command not defined" },
22     { 114, "not integer" },
23     { 115, "not hex" },
24     { 116, "out of hex range" },
25     { 117, "operand is too many" },
26     { 118, "operand length is too long" },
27     { 119, "out of COMET II memory" },
28     { 120, "GR0 in operand x" },
29     { 121, "cannot get operand token" },
30     { 122, "cannot create hash table" },
31     { 201, "execute - out of COMET II memory" },
32     { 202, "SVC input - out of Input memory" },
33     { 203, "SVC output - out of COMET II memory" },
34     { 204, "Program Register (PR) - out of COMET II memory" },
35     { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
36     { 206, "Address - out of COMET II memory" },
37     { 207, "Stack Pointer (SP) - out of COMET II memory" },
38 };
39
40 /* WORD値を文字列に変換 */
41 char *wtoa(WORD word)
42 {
43     char *p = malloc(6), *q = malloc(6);
44     int i = 0, j;
45     do{
46         *(p + i++) = word % 10 + '0';
47     } while((word /= 10) > 0);
48     for(j = 0; j < i; j++) {
49         *(q + j) = *(p + (i - 1) - j);
50     }
51     *(q + j + 1) = '\0';
52     return q;
53 }
54
55 /* エラー番号とエラーメッセージを設定する */
56 void setcerr(int num, const char *val)
57 {
58     cerrno = num;
59     cerrmsg = malloc(256);
60     if(val != NULL) {
61         strcpy(cerrmsg, val);
62         strcat(cerrmsg, ": ");
63         strcat(cerrmsg, getcerrmsg(cerrno));
64     } else {
65         strcpy(cerrmsg, getcerrmsg(cerrno));
66     }
67 }
68
69 /* エラー番号からメッセージを返す */
70 char *getcerrmsg(int num)
71 {
72     assert(num > 0);
73     int i;
74     for(i = 0; i < ARRAYSIZE(cerr); i++) {
75         if((&cerr[i])->num == num) {
76             return (&cerr[i])->msg;
77         }
78     }
79     return "unkown error";
80 }
81
82 /* エラーを解放する */
83 void freecerr()
84 {
85     assert(cerrno > 0);
86     cerrno = 0;
87     if(strlen(cerrmsg) > 0) {
88         free(cerrmsg);
89     }
90 }