]> Shamusworld >> Repos - rmac/blob - error.c
75e91490d3d44df485fed4cd180befc545055443
[rmac] / error.c
1 //
2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // ERROR.C - Error Handling
4 // Copyright (C) 199x Landon Dyer, 2011-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 <stdarg.h>
11 #include "listing.h"
12 #include "token.h"
13
14 int errcnt;                                             // Error count
15 char * err_fname;                               // Name of error message file
16
17 static long unused;                             // For supressing 'write' warnings
18
19
20 //
21 // Report error if not at EOL
22 // N.B.: Since this should *never* happen, we can feel free to add whatever
23 //       diagnostics that will help in tracking down a problem to this function.
24 //
25 int at_eol(void)
26 {
27         if (*tok != EOL)
28         {
29                 error("syntax error. expected EOL, found $%X ('%c')", *tok, *tok);
30                 printf("Token = ");
31                 DumpToken(*tok);
32                 printf("\n");
33                 DumpTokenBuffer();
34         }
35
36         return 0;
37 }
38
39
40 //
41 // Cannot create a file
42 //
43 void cantcreat(const char * fn)
44 {
45         printf("cannot create: '%s'\n", fn);
46         exit(1);
47 }
48
49
50 //
51 // Setup for error message
52 //  o  Create error listing file (if necessary)
53 //  o  Set current filename
54 //
55 void err_setup(void)
56 {
57         char fnbuf[FNSIZ];
58
59         if (err_fname != NULL)
60         {
61                 strcpy(fnbuf, err_fname);
62
63                 if (*fnbuf == EOS)
64                         strcpy(fnbuf, firstfname);
65
66                 err_fname = NULL;
67
68                 if ((err_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
69                         cantcreat(fnbuf);
70
71                 err_flag = 1;
72         }
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         sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
95
96         if (err_flag)
97                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
98         else
99                 printf("%s", buf1);
100
101         taglist('E');
102         errcnt++;
103
104         return ERROR;
105 }
106
107
108 //
109 // Display warning message (uses printf() style variable arguments)
110 //
111 int warn(const char * text, ...)
112 {
113         char buf[EBUFSIZ];
114         char buf1[EBUFSIZ];
115
116         err_setup();
117         va_list arg;
118         va_start(arg, text);
119         vsprintf(buf, text, arg);
120         va_end(arg);
121
122         if (listing > 0)
123                 ship_ln(buf);
124
125         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
126
127         if (err_flag)
128                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
129         else
130                 printf("%s", buf1);
131
132         taglist('W');
133
134         return OK;
135 }
136
137
138 int fatal(const char * s)
139 {
140         char buf[EBUFSIZ];
141
142         err_setup();
143
144         if (listing > 0)
145                 ship_ln(s);
146
147         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
148
149         if (err_flag)
150                 unused = write(err_fd, buf, (LONG)strlen(buf));
151         else
152                 printf("%s", buf);
153
154         exit(1);
155 }
156
157
158 int interror(int n)
159 {
160         char buf[EBUFSIZ];
161
162         err_setup();
163         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
164
165         if (listing > 0)
166                 ship_ln(buf);
167
168         if (err_flag)
169                 unused = write(err_fd, buf, (LONG)strlen(buf));
170         else
171                 printf("%s", buf);
172
173         exit(1);
174 }
175