]> Shamusworld >> Repos - rmac/blob - error.c
Fix for incbin not including the exact requested bytes if offset is non zero
[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 char * interror_msg[] = {
14         "Unknown internal error",       // Error not referenced, should not be displayed
15         "Unknown internal error",       // Error not referenced, should not be displayed
16         "Bad MULTX entry in chrtab",                            // Error #2
17         "Unknown internal error",       // Error not referenced, should not be displayed
18         "Bad fixup type",                                                       // Error #4
19         "Bad operator in expression stream",            // Error #5
20         "Can't find generated code in section",         // Error #6
21         "Fixup (loc) out of range"                                      // Error #7
22         "Absolute top filename found"                           // Error #8
23 };
24
25 // Exported variables
26 int errcnt;                                             // Error count
27 char * err_fname;                               // Name of error message file
28
29 // Internal variables
30 static long unused;                             // For supressing 'write' warnings
31
32 //
33 // Report error if not at EOL
34 //
35 // N.B.: Since this should *never* happen, we can feel free to add whatever
36 //       diagnostics that will help in tracking down a problem to this function.
37 //
38 int ErrorIfNotAtEOL(void)
39 {
40         if (*tok != EOL)
41         {
42                 error("syntax error. expected EOL, found $%X ('%c')", *tok, *tok);
43                 printf("Token = ");
44                 DumpToken(*tok);
45                 printf("\n");
46                 DumpTokenBuffer();
47         }
48
49         return 0;
50 }
51
52 //
53 // Cannot create a file
54 //
55 void CantCreateFile(const char * fn)
56 {
57         printf("Cannot create file: '%s'\n", fn);
58         exit(1);
59 }
60
61 //
62 // Setup for error message
63 //  o  Create error listing file (if necessary)
64 //  o  Set current filename
65 //
66 void err_setup(void)
67 {
68         char fnbuf[FNSIZ];
69
70         if (err_fname != NULL)
71         {
72                 strcpy(fnbuf, err_fname);
73
74                 if (*fnbuf == EOS)
75                         strcpy(fnbuf, firstfname);
76
77                 err_fname = NULL;
78
79                 if ((err_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
80                         CantCreateFile(fnbuf);
81
82                 err_flag = 1;
83         }
84 }
85
86 //
87 // Display error message (uses printf() style variable arguments)
88 //
89 int error(const char * text, ...)
90 {
91         char buf[EBUFSIZ];
92         char buf1[EBUFSIZ];
93
94         err_setup();
95
96         va_list arg;
97         va_start(arg, text);
98         vsprintf(buf, text, arg);
99         va_end(arg);
100
101         if (listing > 0)
102                 ship_ln(buf);
103
104         if (cur_inobj)
105         {
106                 switch (cur_inobj->in_type)
107                 {
108                 case SRC_IFILE:
109                         sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
110                         break;
111                 case SRC_IMACRO:
112                 {
113                         // This is basically SetFilenameForErrorReporting() but we don't
114                         // call it here as it will clobber curfname. That function is used
115                         // during fixups only so it really doesn't matter at that point...
116                         char * filename;
117                         FILEREC * fr;
118                         uint16_t fnum = cur_inobj->inobj.imacro->im_macro->cfileno;
119
120                         // Check for absolute top filename (this should never happen)
121                         if (fnum == -1)
122                                 interror(8);
123                         else
124                         {
125                                 fr = filerec;
126
127                                 // Advance to the correct record...
128                                 while (fr != NULL && fnum != 0)
129                                 {
130                                         fr = fr->frec_next;
131                                         fnum--;
132                                 }
133                         }
134                         // Check for file # record not found (this should never happen either)
135                         if (fr == NULL)
136                                 interror(8);
137
138                         filename = fr->frec_name;
139
140                         sprintf(buf1, "%s %d: Error: %s\nCalled from: %s %d\n", filename, cur_inobj->inobj.imacro->im_macro->lineList->lineno, buf,
141                         curfname, curlineno);
142                 }
143                         break;
144                 case SRC_IREPT:
145                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.irept->lineno, buf);
146                         break;
147                 }
148         }
149         else
150                 // No current file so cur_inobj is NULL
151                 sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
152
153         if (err_flag)
154                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
155         else
156                 printf("%s", buf1);
157
158         taglist('E');
159         errcnt++;
160
161         return ERROR;
162 }
163
164 //
165 // Display warning message (uses printf() style variable arguments)
166 //
167 int warn(const char * text, ...)
168 {
169         char buf[EBUFSIZ];
170         char buf1[EBUFSIZ];
171
172         err_setup();
173         va_list arg;
174         va_start(arg, text);
175         vsprintf(buf, text, arg);
176         va_end(arg);
177
178         if (listing > 0)
179                 ship_ln(buf);
180
181         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
182
183         if (err_flag)
184                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
185         else
186                 printf("%s", buf1);
187
188         taglist('W');
189
190         return OK;
191 }
192
193 int fatal(const char * s)
194 {
195         char buf[EBUFSIZ];
196
197         err_setup();
198
199         if (listing > 0)
200                 ship_ln(s);
201
202         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
203
204         if (err_flag)
205                 unused = write(err_fd, buf, (LONG)strlen(buf));
206         else
207                 printf("%s", buf);
208
209         exit(1);
210 }
211
212 int interror(int n)
213 {
214         char buf[EBUFSIZ];
215
216         err_setup();
217         sprintf(buf, "%s %d: Internal error #%d: %s\n", curfname, curlineno, n, interror_msg[n]);
218
219         if (listing > 0)
220                 ship_ln(buf);
221
222         if (err_flag)
223                 unused = write(err_fd, buf, (LONG)strlen(buf));
224         else
225                 printf("%s", buf);
226
227         exit(1);
228 }