デバッガー機能の実装
[YACASL2.git] / src / cerr.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <stdbool.h>
6 #include "cerr.h"
7
8 void cerr_init()
9 {
10     cerr = malloc_chk(sizeof(CERR), "cerr");
11     cerr->num = 0;
12 }
13
14 CERR *cerr;
15
16 CERRLIST *cerrlist = NULL;
17
18 void addcerrlist(int cerrc, CERR cerrv[])
19 {
20     int i;
21     CERRLIST *p = NULL, *q = malloc_chk(sizeof(CERRLIST), "cerrlist");
22
23     assert(cerrc > 0 && cerrv != NULL);
24     for(i = 0; i < cerrc; i++) {
25         if(p == NULL) {
26             p = q;
27         } else {
28             p = p->next = malloc_chk(sizeof(CERRLIST), "cerrlist.next");
29         }
30         p->cerr = &cerrv[i];
31         p->next = NULL;
32     }
33     p->next = cerrlist;
34     cerrlist = q;
35 }
36
37 void printcerrlist()
38 {
39     CERRLIST *p;
40
41     if(cerrlist == NULL) {
42         puts("error list is null.");
43     } else {
44         for(p = cerrlist; p != NULL; p = p->next) {
45             printf("%d: %s\n", p->cerr->num, p->cerr->msg);
46         }
47     }
48 }
49
50 void setcerr(int num, const char *str)
51 {
52     /* 現在のエラー番号を設定  */
53     cerr->num = num;
54     /* 現在のエラーメッセージを設定 */
55     cerr->msg = malloc_chk(CERRMSGSIZE + 1, "cerr.msg");
56     if(0 < strlen(str) && strlen(str) <= CERRSTRSIZE) {
57         sprintf(cerr->msg, "%s: %s", str, getcerrmsg(cerr->num));
58     } else {
59         strcpy(cerr->msg, getcerrmsg(cerr->num));
60     }
61 }
62
63 char *getcerrmsg(int num)
64 {
65     CERRLIST *p;
66     char *msg = "unknown error";
67
68     for(p = cerrlist; p != NULL; p = p->next) {
69         if(num == p->cerr->num) {
70             msg = p->cerr->msg;
71             break;
72         }
73     }
74     return msg;
75 }
76
77 void freecerr()
78 {
79     CERRLIST *p = cerrlist, *q;
80
81     /* 現在のエラーメッセージを解放 */
82     FREE(cerr->msg);
83     /* 現在のエラーを解放 */
84     FREE(cerr);
85     /* エラーリストを解放 */
86     for(p = cerrlist; p != NULL; p = q) {
87         q = p->next;
88         FREE(p);
89     }
90 }