]> Shamusworld >> Repos - rmac/blob - error.c
300a07e9daabcb3851381ea568e0fe5fa91701ba
[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, 2011 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         if (*tok != EOL)
26                 error("syntax error");
27
28         return 0;
29 }
30
31
32 //
33 // Cannot Create a File
34 //
35 void cantcreat(const char * fn)
36 {
37         printf("cannot create: '%s'\n", fn);
38         exit(1);
39 }
40
41
42 //
43 // Setup for Error Message
44 // o  Create error listing file (if necessary)
45 // o  Set current filename
46 //
47 void err_setup(void)
48 {
49         char fnbuf[FNSIZ];
50
51 // This seems like it's unnecessary, as token.c seems to take care of this all by itself.
52 // Can restore if it's really needed. If not, into the bit bucket it goes. :-)
53 //      setfnum(cfileno);
54
55         if (err_fname != NULL)
56         {
57                 strcpy(fnbuf, err_fname);
58
59                 if (*fnbuf == EOS)
60                         strcpy(fnbuf, firstfname);
61
62                 err_fname = NULL;
63
64                 if ((err_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
65                         cantcreat(fnbuf);
66
67                 err_flag = 1;
68         }
69 }
70
71
72 //
73 // Display Error Message
74 //
75 int error(const char * s)
76 {
77         char buf[EBUFSIZ];
78         unsigned int length;
79
80         err_setup();
81
82         if (listing > 0)
83                 ship_ln(s);
84
85         sprintf(buf, "%s %d: Error: %s%s", curfname, curlineno, s, nl);
86         length = strlen(buf);
87
88         if (err_flag)
89                 unused = write(err_fd, buf, length);
90         else
91                 printf("%s", buf);
92
93         taglist('E');
94         errcnt++;
95
96         return ERROR;
97 }
98
99
100 int errors(const char * s, char * s1)
101 {
102         char buf[EBUFSIZ];
103         char buf1[EBUFSIZ];
104
105         err_setup();
106         sprintf(buf, s, s1);
107
108         if (listing > 0)
109                 ship_ln(buf);
110
111         sprintf(buf1, "%s %d: Error: %s%s", curfname, curlineno, buf, nl);
112
113         if (err_flag)
114                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
115         else
116                 printf("%s", buf1);
117
118         taglist('E');
119         ++errcnt;
120
121         return ERROR;
122 }
123
124
125 int warn(const char * s)
126 {
127         char buf[EBUFSIZ];
128
129         err_setup();
130
131         if (listing > 0)
132                 ship_ln(s);
133
134         sprintf(buf, "%s %d: Warning: %s%s", curfname, curlineno, s, nl);
135
136         if (err_flag)
137                 unused = write(err_fd, buf, (LONG)strlen(buf));
138         else
139                 printf("%s", buf);
140
141         taglist('W');
142
143         return OK;
144 }
145
146
147 int warns(const char * s, char * s1)
148 {
149         char buf[EBUFSIZ];
150         char buf1[EBUFSIZ];
151
152         err_setup();
153         sprintf(buf, s, s1);
154
155         if (listing > 0)
156                 ship_ln(s);
157
158         sprintf(buf1, "%s %d: Warning: %s%s", curfname, curlineno, buf, nl);
159
160         if (err_flag)
161                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
162         else
163                 printf("%s", buf1);
164
165         taglist('W');
166
167         return OK;
168 }
169
170
171 int warni(const char * s, unsigned i)
172 {
173         char buf[EBUFSIZ];
174         char buf1[EBUFSIZ];
175
176         err_setup();
177         sprintf(buf, s, i);
178
179         if (listing > 0)
180                 ship_ln(buf);
181
182         sprintf(buf1, "%s %d: Warning: %s%s", curfname, curlineno, buf, nl);
183
184         if (err_flag)
185                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
186         else
187                 printf("%s", buf1);
188
189         taglist('W');
190
191         return OK;
192 }
193
194
195 int fatal(const char * s)
196 {
197         char buf[EBUFSIZ];
198
199         err_setup();
200
201         if (listing > 0)
202                 ship_ln(s);
203
204         sprintf(buf, "%s %d: Fatal: %s%s", curfname, curlineno, s, nl);
205
206         if (err_flag)
207                 unused = write(err_fd, buf, (LONG)strlen(buf));
208         else
209                 printf("%s", buf);
210
211         exit(1);
212 }
213
214
215 int interror(int n)
216 {
217         char buf[EBUFSIZ];
218
219         err_setup();
220         sprintf(buf, "%s %d: Internal Error Number %d%s", curfname, curlineno, n, nl);
221
222         if (listing > 0)
223                 ship_ln(buf);
224
225         if (err_flag)
226                 unused = write(err_fd, buf, (LONG)strlen(buf));
227         else
228                 printf("%s", buf);
229
230         exit(1);
231 }
232