]> Shamusworld >> Repos - rmac/blob - error.c
Added optimisation switches -o8 and -o9
[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         switch (cur_inobj->in_type)
95         {
96         case SRC_IFILE:
97                 sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
98                break;
99         case SRC_IMACRO:
100                 sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.imacro->im_macro->lineList->lineno, buf);
101                 break;
102         case SRC_IREPT:
103                 sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.irept->lineno, buf);
104                 break;
105         }
106
107         if (err_flag)
108                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
109         else
110                 printf("%s", buf1);
111
112         taglist('E');
113         errcnt++;
114
115         return ERROR;
116 }
117
118
119 //
120 // Display warning message (uses printf() style variable arguments)
121 //
122 int warn(const char * text, ...)
123 {
124         char buf[EBUFSIZ];
125         char buf1[EBUFSIZ];
126
127         err_setup();
128         va_list arg;
129         va_start(arg, text);
130         vsprintf(buf, text, arg);
131         va_end(arg);
132
133         if (listing > 0)
134                 ship_ln(buf);
135
136         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
137
138         if (err_flag)
139                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
140         else
141                 printf("%s", buf1);
142
143         taglist('W');
144
145         return OK;
146 }
147
148
149 int fatal(const char * s)
150 {
151         char buf[EBUFSIZ];
152
153         err_setup();
154
155         if (listing > 0)
156                 ship_ln(s);
157
158         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
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
168
169 int interror(int n)
170 {
171         char buf[EBUFSIZ];
172
173         err_setup();
174         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
175
176         if (listing > 0)
177                 ship_ln(buf);
178
179         if (err_flag)
180                 unused = write(err_fd, buf, (LONG)strlen(buf));
181         else
182                 printf("%s", buf);
183
184         exit(1);
185 }
186