]> Shamusworld >> Repos - rmac/blob - error.c
Version bump for last commit. Now at version 1.13.2.
[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-2018 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         if (cur_inobj)
95         {
96                 switch (cur_inobj->in_type)
97                 {
98                 case SRC_IFILE:
99                         sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
100                         break;
101                 case SRC_IMACRO:
102                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.imacro->im_macro->lineList->lineno, buf);
103                         break;
104                 case SRC_IREPT:
105                         sprintf(buf1, "%s %d: Error: %s\n", curfname, cur_inobj->inobj.irept->lineno, buf);
106                         break;
107                 }
108         }
109         else
110                 // No current file so cur_inobj is NULL
111                 sprintf(buf1, "%s %d: Error: %s\n", curfname, curlineno, buf);
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 //
126 // Display warning message (uses printf() style variable arguments)
127 //
128 int warn(const char * text, ...)
129 {
130         char buf[EBUFSIZ];
131         char buf1[EBUFSIZ];
132
133         err_setup();
134         va_list arg;
135         va_start(arg, text);
136         vsprintf(buf, text, arg);
137         va_end(arg);
138
139         if (listing > 0)
140                 ship_ln(buf);
141
142         sprintf(buf1, "%s %d: Warning: %s\n", curfname, curlineno, buf);
143
144         if (err_flag)
145                 unused = write(err_fd, buf1, (LONG)strlen(buf1));
146         else
147                 printf("%s", buf1);
148
149         taglist('W');
150
151         return OK;
152 }
153
154
155 int fatal(const char * s)
156 {
157         char buf[EBUFSIZ];
158
159         err_setup();
160
161         if (listing > 0)
162                 ship_ln(s);
163
164         sprintf(buf, "%s %d: Fatal: %s\n", curfname, curlineno, s);
165
166         if (err_flag)
167                 unused = write(err_fd, buf, (LONG)strlen(buf));
168         else
169                 printf("%s", buf);
170
171         exit(1);
172 }
173
174
175 int interror(int n)
176 {
177         char buf[EBUFSIZ];
178
179         err_setup();
180         sprintf(buf, "%s %d: Internal error #%d\n", curfname, curlineno, n);
181
182         if (listing > 0)
183                 ship_ln(buf);
184
185         if (err_flag)
186                 unused = write(err_fd, buf, (LONG)strlen(buf));
187         else
188                 printf("%s", buf);
189
190         exit(1);
191 }
192