]> Shamusworld >> Repos - rmac/blob - error.c
Version bump for last commit.
[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\n", filename, cur_inobj->inobj.imacro->im_macro->lineList->lineno, buf);
131                 }
132                         break;
133                 case SRC_IREPT:
134                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.irept->lineno, buf);
135                         break;
136                 }
137         }
138         else
139                 // No current file so cur_inobj is NULL
140                 sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
141
142         if (err_flag)
143                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
144         else
145                 printf("%s", buf1);
146
147         taglist('E');
148         errcnt++;
149
150         return ERROR;
151 }
152
153 //
154 // Display warning message (uses printf() style variable arguments)
155 //
156 int warn(const char * text, ...)
157 {
158         char buf[EBUFSIZ];
159         char buf1[EBUFSIZ];
160
161         err_setup();
162         va_list arg;
163         va_start(arg, text);
164         vsprintf(buf, text, arg);
165         va_end(arg);
166
167         if (listing > 0)
168                 ship_ln(buf);
169
170         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
171
172         if (err_flag)
173                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
174         else
175                 printf("%s", buf1);
176
177         taglist('W');
178
179         return OK;
180 }
181
182 int fatal(const char * s)
183 {
184         char buf[EBUFSIZ];
185
186         err_setup();
187
188         if (listing > 0)
189                 ship_ln(s);
190
191         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
192
193         if (err_flag)
194                 unused = write(err_fd, buf, (LONG)strlen(buf));
195         else
196                 printf("%s", buf);
197
198         exit(1);
199 }
200
201 int interror(int n)
202 {
203         char buf[EBUFSIZ];
204
205         err_setup();
206         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
207
208         if (listing > 0)
209                 ship_ln(buf);
210
211         if (err_flag)
212                 unused = write(err_fd, buf, (LONG)strlen(buf));
213         else
214                 printf("%s", buf);
215
216         exit(1);
217 }