]> Shamusworld >> Repos - rmac/blob - error.c
Further fix for bug #135 (added RISC error reporting).
[rmac] / error.c
1 //
2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // ERROR.C - Error Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2020 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
7 //
8
9 #include "error.h"
10 #include <stdarg.h>
11 #include "listing.h"
12 #include "token.h"
13
14 // Exported variables
15 int errcnt;                                             // Error count
16 char * err_fname;                               // Name of error message file
17
18 // Internal variables
19 static long unused;                             // For supressing 'write' warnings
20
21
22 //
23 // Report error if not at EOL
24 //
25 // N.B.: Since this should *never* happen, we can feel free to add whatever
26 //       diagnostics that will help in tracking down a problem to this function.
27 //
28 int ErrorIfNotAtEOL(void)
29 {
30         if (*tok != EOL)
31         {
32                 error("syntax error. expected EOL, found $%X ('%c')", *tok, *tok);
33                 printf("Token = ");
34                 DumpToken(*tok);
35                 printf("\n");
36                 DumpTokenBuffer();
37         }
38
39         return 0;
40 }
41
42
43 //
44 // Cannot create a file
45 //
46 void CantCreateFile(const char * fn)
47 {
48         printf("Cannot create file: '%s'\n", fn);
49         exit(1);
50 }
51
52
53 //
54 // Setup for error message
55 //  o  Create error listing file (if necessary)
56 //  o  Set current filename
57 //
58 void err_setup(void)
59 {
60         char fnbuf[FNSIZ];
61
62         if (err_fname != NULL)
63         {
64                 strcpy(fnbuf, err_fname);
65
66                 if (*fnbuf == EOS)
67                         strcpy(fnbuf, firstfname);
68
69                 err_fname = NULL;
70
71                 if ((err_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
72                         CantCreateFile(fnbuf);
73
74                 err_flag = 1;
75         }
76 }
77
78
79 //
80 // Display error message (uses printf() style variable arguments)
81 //
82 int error(const char * text, ...)
83 {
84         char buf[EBUFSIZ];
85         char buf1[EBUFSIZ];
86
87         err_setup();
88
89         va_list arg;
90         va_start(arg, text);
91         vsprintf(buf, text, arg);
92         va_end(arg);
93
94         if (listing > 0)
95                 ship_ln(buf);
96
97         if (cur_inobj)
98         {
99                 switch (cur_inobj->in_type)
100                 {
101                 case SRC_IFILE:
102                         sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
103                         break;
104                 case SRC_IMACRO:
105                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.imacro->im_macro->lineList->lineno, buf);
106                         break;
107                 case SRC_IREPT:
108                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.irept->lineno, buf);
109                         break;
110                 }
111         }
112         else
113                 // No current file so cur_inobj is NULL
114                 sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
115
116         if (err_flag)
117                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
118         else
119                 printf("%s", buf1);
120
121         taglist('E');
122         errcnt++;
123
124         return ERROR;
125 }
126
127
128 //
129 // Display warning message (uses printf() style variable arguments)
130 //
131 int warn(const char * text, ...)
132 {
133         char buf[EBUFSIZ];
134         char buf1[EBUFSIZ];
135
136         err_setup();
137         va_list arg;
138         va_start(arg, text);
139         vsprintf(buf, text, arg);
140         va_end(arg);
141
142         if (listing > 0)
143                 ship_ln(buf);
144
145         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
146
147         if (err_flag)
148                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
149         else
150                 printf("%s", buf1);
151
152         taglist('W');
153
154         return OK;
155 }
156
157
158 int fatal(const char * s)
159 {
160         char buf[EBUFSIZ];
161
162         err_setup();
163
164         if (listing > 0)
165                 ship_ln(s);
166
167         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
168
169         if (err_flag)
170                 unused = write(err_fd, buf, (LONG)strlen(buf));
171         else
172                 printf("%s", buf);
173
174         exit(1);
175 }
176
177
178 int interror(int n)
179 {
180         char buf[EBUFSIZ];
181
182         err_setup();
183         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
184
185         if (listing > 0)
186                 ship_ln(buf);
187
188         if (err_flag)
189                 unused = write(err_fd, buf, (LONG)strlen(buf));
190         else
191                 printf("%s", buf);
192
193         exit(1);
194 }
195