]> Shamusworld >> Repos - rmac/blob - error.c
Doc fixes/updates.
[rmac] / error.c
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // ERROR.C - Error Handling
4 // Copyright (C) 199x Landon Dyer, 2017 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 "token.h"
11 #include "listing.h"
12
13 int errcnt;                                             // Error count
14 char * err_fname;                               // Name of error message file
15
16 static const char nl[] = "\n";
17 static long unused;                             // For supressing 'write' warnings
18
19
20 //
21 // Report error if not at EOL
22 //
23 int at_eol(void)
24 {
25         char msg[256];
26
27         if (*tok != EOL)
28         {
29                 sprintf(msg, "syntax error. expected EOL, found $%X ('%c')", *tok, *tok);
30                 error(msg);
31         }
32
33         return 0;
34 }
35
36
37 //
38 // Cannot create a file
39 //
40 void cantcreat(const char * fn)
41 {
42         printf("cannot create: '%s'\n", fn);
43         exit(1);
44 }
45
46
47 //
48 // Setup for error message
49 // o  Create error listing file (if necessary)
50 // o  Set current filename
51 //
52 void err_setup(void)
53 {
54         char fnbuf[FNSIZ];
55
56 // This seems like it's unnecessary, as token.c seems to take care of this all by itself.
57 // Can restore if it's really needed. If not, into the bit bucket it goes. :-)
58 //      setfnum(cfileno);
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                         cantcreat(fnbuf);
71
72                 err_flag = 1;
73         }
74 }
75
76
77 //
78 // Display error message
79 //
80 int error(const char * s)
81 {
82         char buf[EBUFSIZ];
83         unsigned int length;
84
85         err_setup();
86
87         if (listing > 0)
88                 ship_ln(s);
89
90         sprintf(buf, "%s %d: Error: %s%s", curfname, curlineno, s, nl);
91         length = strlen(buf);
92
93         if (err_flag)
94                 unused = write(err_fd, buf, length);
95         else
96                 printf("%s", buf);
97
98         taglist('E');
99         errcnt++;
100
101         return ERROR;
102 }
103
104
105 int errors(const char * s, char * s1)
106 {
107         char buf[EBUFSIZ];
108         char buf1[EBUFSIZ];
109
110         err_setup();
111         sprintf(buf, s, s1);
112
113         if (listing > 0)
114                 ship_ln(buf);
115
116         sprintf(buf1, "%s %d: Error: %s%s", curfname, curlineno, buf, nl);
117
118         if (err_flag)
119                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
120         else
121                 printf("%s", buf1);
122
123         taglist('E');
124         ++errcnt;
125
126         return ERROR;
127 }
128
129
130 int warn(const char * s)
131 {
132         char buf[EBUFSIZ];
133
134         err_setup();
135
136         if (listing > 0)
137                 ship_ln(s);
138
139         sprintf(buf, "%s %d: Warning: %s%s", curfname, curlineno, s, nl);
140
141         if (err_flag)
142                 unused = write(err_fd, buf, (LONG)strlen(buf));
143         else
144                 printf("%s", buf);
145
146         taglist('W');
147
148         return OK;
149 }
150
151
152 int warns(const char * s, char * s1)
153 {
154         char buf[EBUFSIZ];
155         char buf1[EBUFSIZ];
156
157         err_setup();
158         sprintf(buf, s, s1);
159
160         if (listing > 0)
161                 ship_ln(s);
162
163         sprintf(buf1, "%s %d: Warning: %s%s", curfname, curlineno, buf, nl);
164
165         if (err_flag)
166                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
167         else
168                 printf("%s", buf1);
169
170         taglist('W');
171
172         return OK;
173 }
174
175
176 int warni(const char * s, unsigned i)
177 {
178         char buf[EBUFSIZ];
179         char buf1[EBUFSIZ];
180
181         err_setup();
182         sprintf(buf, s, i);
183
184         if (listing > 0)
185                 ship_ln(buf);
186
187         sprintf(buf1, "%s %d: Warning: %s%s", curfname, curlineno, buf, nl);
188
189         if (err_flag)
190                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
191         else
192                 printf("%s", buf1);
193
194         taglist('W');
195
196         return OK;
197 }
198
199
200 int fatal(const char * s)
201 {
202         char buf[EBUFSIZ];
203
204         err_setup();
205
206         if (listing > 0)
207                 ship_ln(s);
208
209         sprintf(buf, "%s %d: Fatal: %s%s", curfname, curlineno, s, nl);
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 }
218
219
220 int interror(int n)
221 {
222         char buf[EBUFSIZ];
223
224         err_setup();
225         sprintf(buf, "%s %d: Internal Error Number %d%s", curfname, curlineno, n, nl);
226
227         if (listing > 0)
228                 ship_ln(buf);
229
230         if (err_flag)
231                 unused = write(err_fd, buf, (LONG)strlen(buf));
232         else
233                 printf("%s", buf);
234
235         exit(1);
236 }
237