dca936535cffe9d05994283907945f9f2f77e68d
[YACASL2.git] / src / cerr.c
1 #include "cerr.h"
2
3 CERRARRAY cerr[] = {
4     { 101, "label already defined" },
5     { 102, "label table is full" },
6     { 103, "label not found" },
7     { 104, "label length is too long" },
8     { 105, "no command in the line" },
9     { 106, "operand count mismatch" },
10     { 107, "no label in START" },
11     { 108, "not command of operand \"r\"" },
12     { 109, "not command of operand \"r1,r2\"" },
13     { 110, "not command of operand \"r,adr[,x]\"" },
14     { 111, "not command of operand \"adr[,x]\"" },
15     { 112, "not command of no operand" },
16     { 113, "command not defined" },
17     { 114, "not integer" },
18     { 115, "not hex" },
19     { 116, "out of hex range" },
20     { 117, "operand is too many" },
21     { 118, "operand length is too long" },
22     { 119, "out of COMET II memory" },
23     { 120, "GR0 in operand x" },
24     { 121, "cannot get operand token" },
25     { 122, "cannot create hash table" },
26     { 201, "execute - out of COMET II memory" },
27     { 202, "SVC input - out of Input memory" },
28     { 203, "SVC output - out of COMET II memory" },
29     { 204, "Program Register (PR) - out of COMET II memory" },
30     { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
31     { 206, "Address - out of COMET II memory" },
32     { 207, "Stack Pointer (SP) - out of COMET II memory" },
33 };
34
35 /* エラー番号とエラーメッセージを設定する */
36 void setcerr(int num, const char *val)
37 {
38     cerrno = num;
39     cerrmsg = malloc(256);
40     if(val != NULL) {
41         strcpy(cerrmsg, val);
42         strcat(cerrmsg, ": ");
43         strcat(cerrmsg, getcerrmsg(cerrno));
44     } else {
45         strcpy(cerrmsg, getcerrmsg(cerrno));
46     }
47 }
48
49 /* エラー番号からメッセージを返す */
50 char *getcerrmsg(int num)
51 {
52     assert(num > 0);
53     int i;
54     for(i = 0; i < ARRAYSIZE(cerr); i++) {
55         if((&cerr[i])->num == num) {
56             return (&cerr[i])->msg;
57         }
58     }
59     return "unkown error";
60 }
61
62 /* エラーを解放する */
63 void freecerr()
64 {
65     assert(cerrno > 0);
66     cerrno = 0;
67     if(strlen(cerrmsg) > 0) {
68         free(cerrmsg);
69     }
70 }