]> Shamusworld >> Repos - rmac/blob - error.c
Small fix that should have gone into the last commit. :-P
[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 <token.h>
12 #include "listing.h"
13 #include "token.h"
14
15 // Exported variables
16 int errcnt;                                             // Error count
17 char * err_fname;                               // Name of error message file
18
19 // Internal variables
20 static long unused;                             // For supressing 'write' warnings
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 // Cannot create a file
44 //
45 void CantCreateFile(const char * fn)
46 {
47         printf("Cannot create file: '%s'\n", fn);
48         exit(1);
49 }
50
51 //
52 // Setup for error message
53 //  o  Create error listing file (if necessary)
54 //  o  Set current filename
55 //
56 void err_setup(void)
57 {
58         char fnbuf[FNSIZ];
59
60         if (err_fname != NULL)
61         {
62                 strcpy(fnbuf, err_fname);
63
64                 if (*fnbuf == EOS)
65                         strcpy(fnbuf, firstfname);
66
67                 err_fname = NULL;
68
69                 if ((err_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
70                         CantCreateFile(fnbuf);
71
72                 err_flag = 1;
73         }
74 }
75
76 //
77 // Display error message (uses printf() style variable arguments)
78 //
79 int error(const char * text, ...)
80 {
81         char buf[EBUFSIZ];
82         char buf1[EBUFSIZ];
83
84         err_setup();
85
86         va_list arg;
87         va_start(arg, text);
88         vsprintf(buf, text, arg);
89         va_end(arg);
90
91         if (listing > 0)
92                 ship_ln(buf);
93
94         if (cur_inobj)
95         {
96                 switch (cur_inobj->in_type)
97                 {
98                 case SRC_IFILE:
99                         sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
100                         break;
101                 case SRC_IMACRO:
102                 {
103                         // This is basically SetFilenameForErrorReporting() but we don't
104                         // call it here as it will clobber curfname. That function is used
105                         // during fixups only so it really doesn't matter at that point...
106                         char * filename;
107                         FILEREC * fr;
108                         uint16_t fnum = cur_inobj->inobj.imacro->im_macro->cfileno;
109
110                         // Check for absolute top filename (this should never happen)
111                         if (fnum == -1)
112                                 interror(8);
113                         else
114                         {
115                                 fr = filerec;
116
117                                 // Advance to the correct record...
118                                 while (fr != NULL && fnum != 0)
119                                 {
120                                         fr = fr->frec_next;
121                                         fnum--;
122                                 }
123                         }
124                         // Check for file # record not found (this should never happen either)
125                         if (fr == NULL)
126                                 interror(8);
127
128                         filename = fr->frec_name;
129
130                         sprintf(buf1, "%s %d: Error: %s\nCalled from: %s %d\n", filename, cur_inobj->inobj.imacro->im_macro->lineList->lineno, buf,
131                         curfname, curlineno);
132                 }
133                         break;
134                 case SRC_IREPT:
135                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.irept->lineno, buf);
136                         break;
137                 }
138         }
139         else
140                 // No current file so cur_inobj is NULL
141                 sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
142
143         if (err_flag)
144                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
145         else
146                 printf("%s", buf1);
147
148         taglist('E');
149         errcnt++;
150
151         return ERROR;
152 }
153
154 //
155 // Display warning message (uses printf() style variable arguments)
156 //
157 int warn(const char * text, ...)
158 {
159         char buf[EBUFSIZ];
160         char buf1[EBUFSIZ];
161
162         err_setup();
163         va_list arg;
164         va_start(arg, text);
165         vsprintf(buf, text, arg);
166         va_end(arg);
167
168         if (listing > 0)
169                 ship_ln(buf);
170
171         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
172
173         if (err_flag)
174                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
175         else
176                 printf("%s", buf1);
177
178         taglist('W');
179
180         return OK;
181 }
182
183 int fatal(const char * s)
184 {
185         char buf[EBUFSIZ];
186
187         err_setup();
188
189         if (listing > 0)
190                 ship_ln(s);
191
192         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
193
194         if (err_flag)
195                 unused = write(err_fd, buf, (LONG)strlen(buf));
196         else
197                 printf("%s", buf);
198
199         exit(1);
200 }
201
202 int interror(int n)
203 {
204         char buf[EBUFSIZ];
205
206         err_setup();
207         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
208
209         if (listing > 0)
210                 ship_ln(buf);
211
212         if (err_flag)
213                 unused = write(err_fd, buf, (LONG)strlen(buf));
214         else
215                 printf("%s", buf);
216
217         exit(1);
218 }