-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy patherrmsg.c
463 lines (405 loc) · 12.7 KB
/
errmsg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Diagnostics routines: (fatal) errors, warnings, notes
*
****************************************************************************/
#include <stdarg.h>
#include <ctype.h>
#include <setjmp.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "input.h"
#include "tokenize.h"
#include "macro.h"
#include "msgtext.h"
#include "listing.h"
#include "segment.h"
#include <stdio.h>
#include "Colors.h"
#ifdef _WIN32
#include "winconsole.h"
#endif
extern void print_source_nesting_structure( void );
extern jmp_buf jmpenv;
//static bool Errfile_Written;
//static void PrtMsg( int severity, int msgnum, va_list args1, va_list args2 );
//void PutMsg( FILE *fp, int severity, int msgnum, va_list args );
char banner_printed = FALSE;
static const char usage[] = {
#include "usage.h"
};
static const char usage2[] = {
#include "usage2.h"
};
#ifdef DEBUG_OUT
/* there are intransparent IDEs that don't want to tell you the real, current command line arguments
* and often those tools also swallow anything that is written to stdout or stderr.
* To make jwasm write a trace log to a file, enable the next line!
* Additionally, you'll probably have to enable line Set_dt() in cmdline.c, function ParseCmdline().
*/
//#define DBGLOGFILE "jwasm.log"
#ifdef DBGLOGFILE
FILE *fdbglog = NULL;
#endif
void DoDebugMsg( const char *format, ... )
/****************************************/
{
va_list args;
if( !Options.debug ) return;
if( ModuleInfo.cref == FALSE && CurrFName[ASM] != NULL )
return;
va_start( args, format );
#ifdef DBGLOGFILE
if ( fdbglog == NULL )
fdbglog = fopen( DBGLOGFILE, "w" );
vfprintf( fdbglog, format, args );
#else
vprintf( format, args );
#endif
va_end( args );
#if 0
#ifdef DBGLOGFILE
fflush( fdbglog );
#else
fflush( stdout );
#endif
#endif
}
void DoDebugMsg1( const char *format, ... )
/****************************************/
{
va_list args;
char buffer[MAX_LINE_LEN];
if( !Options.debug ) return;
if( ModuleInfo.cref == FALSE ) return;
#ifdef DBGLOGFILE
if ( fdbglog == NULL )
fdbglog = fopen( DBGLOGFILE, "w" );
#endif
//if ( CurrFName[ASM] )
if ( ModuleInfo.g.src_stack ) {
#ifdef DBGLOGFILE
fprintf( fdbglog, "%" I32_SPEC "u%s. ", GetLineNumber(), GetTopLine( buffer ) );
#else
printf( "%" I32_SPEC "u%s. ", GetLineNumber(), GetTopLine( buffer ) );
#endif
}
va_start( args, format );
#ifdef DBGLOGFILE
vfprintf( fdbglog, format, args );
#else
vprintf( format, args );
#endif
va_end( args );
#if 0
#ifdef DBGLOGFILE
fflush( fdbglog );
#else
fflush( stdout );
#endif
#endif
}
#endif
int write_logo( void )
/********************/
{
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
HANDLE hConsole;
memset(&screenBufferInfo, 0, sizeof(screenBufferInfo));
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &screenBufferInfo);
#endif
if( banner_printed == FALSE ) {
banner_printed = TRUE;
#ifdef _WIN32
SetConsoleTextAttribute(hConsole, WIN_LTGREEN | (screenBufferInfo.wAttributes & 0xfff0) );
printf("%s", MsgGetEx(MSG_UASM));
SetConsoleTextAttribute(hConsole, WIN_CYAN | (screenBufferInfo.wAttributes & 0xfff0) );
printf("%s\n", MsgGetEx(MSG_UASM2));
SetConsoleTextAttribute(hConsole, screenBufferInfo.wAttributes);
#else
printf(FGRN("%s"), MsgGetEx(MSG_UASM));
printf(FCYN("%s\n"), MsgGetEx(MSG_UASM2));
#endif
return( 4 ); /* return number of lines printed */
}
return( 0 );
}
void PrintUsage( void )
/*********************/
{
const char *p;
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
HANDLE hConsole;
memset(&screenBufferInfo, 0, sizeof(screenBufferInfo));
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &screenBufferInfo);
#endif
write_logo();
#ifdef _WIN32
SetConsoleTextAttribute(hConsole, WIN_LTYELLOW | (screenBufferInfo.wAttributes & 0xfff0));
for (p = usage2; *p != '\n'; ) {
const char *p2 = p + strlen(p) + 1;
printf("%-20s %s\n", p, p2);
p = p2 + strlen(p2) + 1;
}
#else
for (p = usage2; *p != '\n'; ) {
const char *p2 = p + strlen(p) + 1;
printf(FYEL("%-20s %s\n"), p, p2);
p = p2 + strlen(p2) + 1;
}
#endif
#ifdef _WIN32
SetConsoleTextAttribute(hConsole, WIN_WHITE | (screenBufferInfo.wAttributes & 0xfff0));
for (p = usage; *p != '\n'; ) {
const char *p2 = p + strlen(p) + 1;
printf("%-20s %s\n", p, p2);
p = p2 + strlen(p2) + 1;
}
SetConsoleTextAttribute(hConsole, screenBufferInfo.wAttributes);
#else
for (p = usage; *p != '\n'; ) {
const char *p2 = p + strlen(p) + 1;
printf(FWHT("%-20s %s\n"), p, p2);
p = p2 + strlen(p2) + 1;
}
#endif
}
static void PutMsg( FILE *fp, int severity, int msgnum, va_list args )
/********************************************************************/
{
int i,j;
const char *type;
const char *pMsg;
char buffer[MAX_LINE_LEN+128];
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
HANDLE hConsole;
memset(&screenBufferInfo, 0, sizeof(screenBufferInfo));
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &screenBufferInfo);
#endif
if( fp != NULL ) {
if ( severity && ( j = GetCurrSrcPos( buffer ) ) ) {
fwrite( buffer, 1, j, fp );
}
pMsg = MsgGetEx( msgnum );
switch ( severity ) {
case 1: type = MsgGetEx( MSG_FATAL_PREFIX ); break;
case 2: type = MsgGetEx( MSG_ERROR_PREFIX ); break;
case 4: type = MsgGetEx( MSG_WARNING_PREFIX ); break;
default: type = NULL; i = 0; break;
}
if ( type )
i = sprintf( buffer, "%s A%4u: ", type, severity * 1000 + msgnum );
i += vsprintf( buffer+i, pMsg, args );
#ifdef _WIN32
if (severity <= 2)
SetConsoleTextAttribute(hConsole, WIN_LTRED | (screenBufferInfo.wAttributes & 0xfff0));
else
SetConsoleTextAttribute(hConsole, WIN_LTYELLOW | (screenBufferInfo.wAttributes & 0xfff0));
fwrite(buffer, 1, i, fp);
fwrite("\n", 1, 1, fp);
SetConsoleTextAttribute(hConsole, screenBufferInfo.wAttributes);
#else
if (severity <= 2)
printf(KRED);
else
printf(KYEL);
fflush(NULL);
fwrite(buffer, 1, i, fp);
fwrite("\n", 1, 1, fp);
printf(RST);
fflush(NULL);
#endif
/* if in Pass 1, add the error msg to the listing */
if ( CurrFile[LST] &&
severity &&
Parse_Pass == PASS_1 &&
fp == CurrFile[ERR] ) {
LstWrite( LSTTYPE_DIRECTIVE, GetCurrOffset(), 0 );
/* size of "blank" prefix to be explained! */
LstPrintf( " %s", buffer );
LstNL();
}
}
}
static void PrtMsg( int severity, int msgnum, va_list args1, va_list args2 )
/**************************************************************************/
{
#ifndef __SW_BD
write_logo();
#endif
/* open .err file if not already open and a name is given */
if( CurrFile[ERR] == NULL && CurrFName[ERR] != NULL ) {
CurrFile[ERR] = fopen( CurrFName[ERR], "w" );
if( CurrFile[ERR] == NULL ) {
/* v2.06: no fatal error anymore if error file cannot be written */
char *p = CurrFName[ERR];
CurrFName[ERR] = NULL; /* set to NULL before EmitErr()! */
Options.no_error_disp = FALSE; /* disable -eq! */
EmitErr( CANNOT_OPEN_FILE, p, ErrnoStr() );
LclFree( p );
}
}
/* v2.05: new option -eq */
if ( Options.no_error_disp == FALSE ) {
PutMsg( errout, severity, msgnum, args1 );
fflush( errout ); /* 27-feb-90 */
}
if( CurrFile[ERR] ) {
//Errfile_Written = TRUE;
PutMsg( CurrFile[ERR], severity, msgnum, args2 );
}
}
/* notes: "included by", "macro called from", ... */
void PrintNote( int msgnum, ... )
/*******************************/
{
va_list args1, args2;
va_start( args1, msgnum );
va_start( args2, msgnum );
PrtMsg( 0, msgnum, args1, args2 );
va_end( args1 );
va_end( args2 );
}
int EmitErr( int msgnum, ... )
/****************************/
{
va_list args1, args2;
#ifdef DEBUG_OUT
printf( "%s\n", ModuleInfo.tokenarray ? ModuleInfo.tokenarray[0].tokpos : "" );
#endif
va_start( args1, msgnum );
va_start( args2, msgnum );
PrtMsg( 2, msgnum, args1, args2 );
va_end( args1 );
va_end( args2 );
ModuleInfo.g.error_count++;
write_to_file = FALSE;
print_source_nesting_structure();
if( Options.error_limit != -1 && ModuleInfo.g.error_count == Options.error_limit+1 )
Fatal( TOO_MANY_ERRORS );
return( ERROR );
}
int EmitError( int msgnum )
/*************************/
{
return( EmitErr( msgnum ) );
}
void EmitWarn( int level, int msgnum, ... )
/*****************************************/
{
va_list args1, args2;
if( level <= Options.warning_level ) {
#ifdef DEBUG_OUT
printf( "%s\n", ModuleInfo.tokenarray ? ModuleInfo.tokenarray[0].tokpos : "" );
#endif
va_start( args1, msgnum );
va_start( args2, msgnum );
if( !Options.warning_error ) {
PrtMsg( 4, msgnum, args1, args2 );
ModuleInfo.g.warning_count++;
} else {
PrtMsg( 2, msgnum, args1, args2 );
ModuleInfo.g.error_count++;
}
va_end( args1 );
va_end( args2 );
print_source_nesting_structure();
}
}
char *ErrnoStr( void )
/********************/
{
static char buffer[32];
return( ( errno == ENOENT ) ? "ENOENT" : myltoa( errno, buffer, 10, FALSE, FALSE ) );
}
/* fatal error (out of memory, unable to open files for write, ...)
* don't use functions which need to alloc memory here!
* v2.08: do not exit(), just a longjmp() into AssembleModule().
*/
void Fatal( int msgnum, ... )
/***************************/
{
va_list args1, args2;
/* v2.11: call PrtMsg() instead of PutMsg().
* Makes the fatal error appear in the .ERR and .LST files
*/
va_start( args1, msgnum );
va_start( args2, msgnum );
PrtMsg( 1, msgnum, args1, args2 );
va_end( args1 );
va_end( args2 );
ModuleInfo.g.error_count++;
//write_to_file = FALSE;
/* setjmp() has been called in AssembleModule().
* if a fatal error happens outside of this function, longjmp()
* is NOT to be used ( out of memory condition, @cmd file open error, ... )
*/
if ( CurrFName[ASM] )
longjmp( jmpenv, 2 );
exit(1);
}
#if 0
void SeekError( void )
/********************/
{
DebugMsg(("SeekError occured\n"));
Fatal( FILE_SEEK_ERROR, CurrFName[OBJ], errno );
};
#endif
void WriteError( void )
/*********************/
{
DebugMsg(("WriteError occured\n"));
Fatal( FILE_WRITE_ERROR, CurrFName[OBJ], errno );
};
#ifndef NDEBUG
int InternalError( const char *file, unsigned line )
/**************************************************/
/* it's used by myassert() function in debug version */
{
#if 1
char buffer[MAX_LINE_LEN];
DebugMsg(("InternalError enter\n"));
ModuleInfo.g.error_count++;
GetCurrSrcPos( buffer );
fprintf( errout, "%s", buffer );
fprintf( errout, MsgGetEx( INTERNAL_ERROR ), file, line );
close_files();
exit( EXIT_FAILURE );
#else
Fatal( INTERNAL_ERROR, file, line );
#endif
return( 0 );
}
#endif