]> Shamusworld >> Repos - rmac/blob - error.c
6da93e2079a279a2c03f5b3a0e6a945c139cbea6
[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 //
23 int at_eol(void)
24 {
25         if (*tok != EOL)
26                 error("syntax error. expected EOL, found $%X ('%c')", *tok, *tok);
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         if (err_fname != NULL)
52         {
53                 strcpy(fnbuf, err_fname);
54
55                 if (*fnbuf == EOS)
56                         strcpy(fnbuf, firstfname);
57
58                 err_fname = NULL;
59
60                 if ((err_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
61                         cantcreat(fnbuf);
62
63                 err_flag = 1;
64         }
65 }
66
67
68 //
69 // Display error message (uses printf() style variable arguments)
70 //
71 int error(const char * text, ...)
72 {
73         char buf[EBUFSIZ];
74         char buf1[EBUFSIZ];
75
76         err_setup();
77
78         va_list arg;
79         va_start(arg, text);
80         vsprintf(buf, text, arg);
81         va_end(arg);
82
83         if (listing > 0)
84                 ship_ln(buf);
85
86         sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
87
88         if (err_flag)
89                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
90         else
91                 printf("%s", buf1);
92
93         taglist('E');
94         errcnt++;
95
96         return ERROR;
97 }
98
99
100 //
101 // Display warning message (uses printf() style variable arguments)
102 //
103 int warn(const char * text, ...)
104 {
105         char buf[EBUFSIZ];
106         char buf1[EBUFSIZ];
107
108         err_setup();
109         va_list arg;
110         va_start(arg, text);
111         vsprintf(buf, text, arg);
112         va_end(arg);
113
114         if (listing > 0)
115                 ship_ln(buf);
116
117         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
118
119         if (err_flag)
120                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
121         else
122                 printf("%s", buf1);
123
124         taglist('W');
125
126         return OK;
127 }
128
129
130 int fatal(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: Fatal: %s\n", curfname, curlineno, s);
140
141         if (err_flag)
142                 unused = write(err_fd, buf, (LONG)strlen(buf));
143         else
144                 printf("%s", buf);
145
146         exit(1);
147 }
148
149
150 int interror(int n)
151 {
152         char buf[EBUFSIZ];
153
154         err_setup();
155         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
156
157         if (listing > 0)
158                 ship_ln(buf);
159
160         if (err_flag)
161                 unused = write(err_fd, buf, (LONG)strlen(buf));
162         else
163                 printf("%s", buf);
164
165         exit(1);
166 }
167