]> Shamusworld >> Repos - rmac/blob - error.c
Fix for bug #174 - when a macro contains an error, actually print the filename it...
[rmac] / error.c
1 //
2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // ERROR.C - Error Handling
4 // Copyright (C) 199x Landon Dyer, 2011-2021 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                 {
106                         // This is basically SetFilenameForErrorReporting() but we don't call it here
107                         // as it will clobber curfname. That function is used during fixups only so
108                         // it really doesn't matter at that point...
109                         char * filename;
110 #include <token.h>
111                         FILEREC * fr;
112                         uint16_t fnum = cur_inobj->inobj.imacro->im_macro->cfileno;
113                         // Check for absolute top filename (this should never happen)
114                         if (fnum == -1)
115                                 interror(8);
116                         else
117                         {
118                                 fr = filerec;
119
120                                 // Advance to the correct record...
121                                 while (fr != NULL && fnum != 0)
122                                 {
123                                         fr = fr->frec_next;
124                                         fnum--;
125                                 }
126                         }
127                         // Check for file # record not found (this should never happen either)
128                         if (fr == NULL)
129                                 interror(8);
130
131                         filename = fr->frec_name;
132
133                         sprintf(buf1, "%s %d: Error: %s\n", filename, cur_inobj->inobj.imacro->im_macro->lineList->lineno, buf);
134                 }
135                         break;
136                 case SRC_IREPT:
137                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.irept->lineno, buf);
138                         break;
139                 }
140         }
141         else
142                 // No current file so cur_inobj is NULL
143                 sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
144
145         if (err_flag)
146                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
147         else
148                 printf("%s", buf1);
149
150         taglist('E');
151         errcnt++;
152
153         return ERROR;
154 }
155
156
157 //
158 // Display warning message (uses printf() style variable arguments)
159 //
160 int warn(const char * text, ...)
161 {
162         char buf[EBUFSIZ];
163         char buf1[EBUFSIZ];
164
165         err_setup();
166         va_list arg;
167         va_start(arg, text);
168         vsprintf(buf, text, arg);
169         va_end(arg);
170
171         if (listing > 0)
172                 ship_ln(buf);
173
174         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
175
176         if (err_flag)
177                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
178         else
179                 printf("%s", buf1);
180
181         taglist('W');
182
183         return OK;
184 }
185
186
187 int fatal(const char * s)
188 {
189         char buf[EBUFSIZ];
190
191         err_setup();
192
193         if (listing > 0)
194                 ship_ln(s);
195
196         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
197
198         if (err_flag)
199                 unused = write(err_fd, buf, (LONG)strlen(buf));
200         else
201                 printf("%s", buf);
202
203         exit(1);
204 }
205
206
207 int interror(int n)
208 {
209         char buf[EBUFSIZ];
210
211         err_setup();
212         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
213
214         if (listing > 0)
215                 ship_ln(buf);
216
217         if (err_flag)
218                 unused = write(err_fd, buf, (LONG)strlen(buf));
219         else
220                 printf("%s", buf);
221
222         exit(1);
223 }
224