-
Notifications
You must be signed in to change notification settings - Fork 23
/
utils.cpp
979 lines (816 loc) · 21.4 KB
/
utils.cpp
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#include "stdafx.h"
//max depth of #defines
#define RECURSION_LIMIT 20
#include "storage.h"
#include "parser.h"
#include "hash.h"
#include "utils.h"
#include "expand_buf.h"
#include "console.h"
#include "spasm.h"
#include "errors.h"
static void destroy_char_value (label_t *label) {
if (label->name)
free (label->name);
if (label)
free (label);
}
/* Routine prototype */
char *mystrpbrk (const char * string, const char * control) {
const unsigned char *str = (const unsigned char *) string;
const unsigned char *ctrl = (const unsigned char *) control;
int map[256];
memset(map, 0, sizeof(map));
while (*ctrl)
{
map[*ctrl++] = true;
}
/* 1st char in control map stops search */
while (*str)
{
if (map[*str])
return((char *)str);
str++;
}
return NULL;
}
/*
* Returns true if ptr has reached what is the end of an assembly
* line of code.
* For example: comment, line break (\), or new line
*/
bool is_end_of_code_line (const char *ptr) {
if (ptr == NULL)
return false;
return (*ptr == '\0' || *ptr == '\n' || *ptr == '\r' || *ptr == ';' || *ptr == '\\');
}
/*
* Skips to the next physical line in the file
*/
char *skip_to_next_line (const char *ptr) {
if (ptr == NULL)
return NULL;
while (*ptr && *ptr != '\n' && *ptr != '\r')
ptr++;
if (*ptr == '\r')
ptr++;
if (*ptr == '\n')
ptr++;
return (char *) ptr;
}
bool is_name_char (char c) {
static const char ext_label_set[] = { '_', '[', ']', '!', '?', '.' };
return isalnum(c) || memchr(ext_label_set, c, sizeof(ext_label_set)) != NULL;
}
/*
* Skips to the end of a value spasm2 label name
*/
char *skip_to_name_end (const char *ptr) {
if (ptr == NULL)
return NULL;
while (is_name_char(*ptr))
ptr++;
return (char *) ptr;
}
char *skip_to_line_end (const char *ptr) {
bool in_escape = false, in_quotes = false;
if (ptr == NULL)
return NULL;
while (*ptr && *ptr != '\n' && *ptr != '\r' && (*ptr != ';' || in_quotes)) {
if (*ptr == '\\') in_escape = true;
else if (*ptr == '\"' && !in_escape) in_quotes = !in_quotes;
else in_escape = false;
ptr++;
}
return (char *) ptr;
}
const char *skip_to_code_line_end(const char *ptr)
{
const char *next = next_code_line((char *) ptr);
if (*next == '\0')
return next;
while (is_end_of_code_line (--next)) ;
return next + 1;
}
char *skip_whitespace (const char *ptr) {
if (ptr == NULL)
return NULL;
while (isspace ((unsigned char) *ptr) && *ptr != '\n' && *ptr != '\r')
ptr++;
return (char *) ptr;
}
bool is_arg (char test) {
return (test == '*' || test == '&' || test == '%' ||
test == '@' || test == '^' || test == '#');
}
char *next_expr (const char *ptr, const char *delims) {
bool in_string = false, // "xxx xxx xx"
in_escape = false; // \n
int in_quote = 0; // 'x'
/* Is there a word left to get? */
// Skip whitespace at the start
ptr = skip_whitespace (ptr);
if (is_end_of_code_line (ptr) || *ptr == ')')
return (char *) ptr;
if (strlen(delims) == 1) {
while (*ptr != '\0' && !((strchr (ptr, delims[0]) == ptr || is_end_of_code_line (ptr))
&& !in_escape && !in_string && in_quote == 0)) {
if (!in_escape) {
switch( *ptr ) {
case '"':
if (in_quote == 0) in_string = !in_string;
break;
case '\'':
if (!in_string && in_quote == 0) in_quote = 3;
break;
case '\\':
in_escape = true;
break;
default:
/* If this char wasn't ', redo the inside */
if (in_quote == 1) {
ptr -= 2;
}
}
if (in_quote) in_quote--;
} else {
in_escape = false;
}
ptr++;
}
} else {
while (*ptr != '\0' && !((mystrpbrk (ptr, delims) == ptr || is_end_of_code_line (ptr))
&& !in_escape && !in_string && in_quote == 0)) {
if (!in_escape) {
switch( *ptr ) {
case '"':
if (in_quote == 0) in_string = !in_string;
break;
case '\'':
if (!in_string && in_quote == 0) in_quote = 3;
break;
case '\\':
in_escape = true;
break;
default:
/* If this char wasn't ', redo the inside */
if (in_quote == 1) {
ptr -= 2;
}
}
if (in_quote) in_quote--;
} else {
in_escape = false;
}
ptr++;
}
}
return (char *) ptr;
}
char *next_code_line(char *ptr) {
ptr = next_expr(ptr, "\\;\r\n");
if (*ptr == ';')
return skip_to_next_line(ptr);
if (*ptr == '\\')
ptr++;
if (*ptr == '\r')
ptr++;
if (*ptr == '\n')
ptr++;
return ptr;
}
/*
* Parses an expression and returns
* the result as a string if possible,
* otherwise expands the expression
*/
char *eval (const char *expr)
{
char result[256], *expr_value;
int value;
int session = StartSPASMErrorSession();
bool fResult = parse_num (expr, &value);
EndSPASMErrorSession(session);
if (fResult)
{
const char *sign = "";
if (value < 0) {
sign = "-";
value = -value;
}
snprintf (result, sizeof (result), "%s$%0X", sign, value);
expr_value = strdup (result);
}
else
{
expr_value = expand_expr (expr);
}
return expr_value;
}
/*
* Parses the arguments for a macro,
* returns a pointer to the end of the
* arguments, or NULL on errors, sets
* curr_arg_set to the node
* holding the values of the args
*/
char *parse_args (const char *ptr, define_t *define, list_t **curr_arg_set) {
int num_args = 0;
if (curr_arg_set == NULL)
return NULL;
if (*ptr != '(' && define->num_args == 0)
return (char *) ptr;
*curr_arg_set = add_arg_set();
//then parse each argument and store its value
if (*ptr == '(') {
ptr = skip_whitespace(ptr + 1);
if (*ptr != ')' && !is_end_of_code_line(ptr))
{
char *word = NULL;
arg_context_t context = ARG_CONTEXT_INITIALIZER;
while ((word = extract_arg_string(&ptr, &context)) != NULL)
{
if (num_args >= define->num_args) {
SetLastSPASMWarning(SPASM_WARN_MACRO_TOO_MANY_ARGS, define->name);
break;
}
//int nSession = StartSPASMErrorSession();
if (*define->args[num_args] == '@')
{
add_arg(strdup(define->args[num_args++]), strdup(word), *curr_arg_set);
}
else
{
label_t *label = search_labels(define->name);
add_arg(strdup(define->args[num_args++]), eval(word), *curr_arg_set);
//free(evald_word);
}
//AddSPASMErrorSessionAnnotation(nSession, "Error during evaluation of macro '%s' argument '%s'", define->name, define->args[num_args - 1]);
//ReplaySPASMErrorSession(nSession);
//EndSPASMErrorSession(nSession);
}
}
}
//fill in the undefined arguments
while (num_args < define->num_args)
add_arg (strdup (define->args[num_args++]), NULL, *curr_arg_set);
if (*ptr == ')') ptr++;
return (char *) ptr;
}
/*
* Parses the arguments for a macro,
* and replaces @* arguments with their
* contents
*/
char *replace_literal_args (const char *ptr, define_t *define, list_t **curr_arg_set) {
char word[MAX_ARG_LEN], *new_ptr;
int totalLen = strlen(ptr);
if (curr_arg_set == NULL)
return NULL;
if (*ptr != '(' && define->num_args == 0)
return (char *) ptr;
//Buckeye: we don't need to check for a label because |ld blah will generate a
//syntax error no matter what we replace blah with (unless its \\ something
//which I'll let you use as a hack)
ptr = skip_whitespace(ptr + 1);
ptr = skip_to_name_end(ptr);
if (*ptr == '(')
{
ptr++;
}
ptr = skip_whitespace(ptr + 1);
while ((new_ptr = skip_to_name_end(ptr)) != NULL && ptr != new_ptr)
{
int old_len = new_ptr - ptr;
strncpy(word, ptr, old_len);
word[old_len] = '\0';
int i;
for (i = 0; i < define->num_args; i++) {
if (*define->args[i] == '@' && !strcasecmp(define->args[i] + 1, word)) {
define_t *arg_define = search_defines(define->args[i]);
int new_len = strlen(arg_define->contents);
if (old_len != new_len) {
memmove((char *) ptr + new_len, ptr + old_len, sizeof(char) * old_len + 3);
}
//don't copy the \0
strncpy((char *) ptr, arg_define->contents, new_len);
}
}
if (*new_ptr == ',')
new_ptr++;
ptr = skip_whitespace(new_ptr);
}
return (char *) ptr;
}
/*
* Returns TRUE if a
* line contains a word,
* ignoring whitespace at
* the beginning
*/
bool line_has_word (char *ptr, const char *word, int word_len) {
ptr = skip_whitespace(ptr);
//make sure the file doesn't end before the end of the word + required whitespace,
// then see if the word's found on the line
if (strnlen (ptr, word_len) == word_len && !strncasecmp (ptr, word, word_len)) {
//then make sure it isn't just the start of another word
ptr += word_len;
//if (isspace (*ptr) || is_end_of_code_line (ptr))
return true;
}
return false;
}
/*
* Slow function to simplify coding of macros and directives with argument lists
* .db blank, blank, macro(blank,blank), blank
* macro( macro( blank, blank), blank)
*
* Returns
* ptr to delimiter if terminated by reaching delimiter
* NULL if input was not delimiter terminated
*/
bool read_expr_impl(const char ** const ptr, char word[256], const char *delims) {
bool in_string = false, // "xxx xxx xx"
in_escape = false; // \n
int in_quote = 0; // 'x'
int level = 0;
char *word_ptr = word;
/* Is there a word left to get? */
// Skip whitespace at the start
*ptr = skip_whitespace (*ptr);
if (is_end_of_code_line (*ptr) || **ptr == ')') {
if (word)
*word = '\0';
return false;
}
while (**ptr != '\0' && !((mystrpbrk (*ptr, delims) == *ptr || is_end_of_code_line (*ptr))
&& !in_escape && !in_string && in_quote == 0 && level == 0)) {
if (!in_escape) {
switch( **ptr ) {
case '"':
if (in_quote == 0) in_string = !in_string;
break;
case '\'':
if (!in_string && in_quote == 0) in_quote = 3;
break;
case '\\':
in_escape = true;
break;
case '(':
if (!in_string && in_quote == 0) level++;
break;
case ')':
if (!in_string && in_quote == 0) level--;
if (level < 0) {
//(*ptr)--; //12/29/2010 stp not sure
goto finish_read_expr;
}
break;
default:
/* If this char wasn't ', redo the inside */
if (in_quote == 1) {
*ptr -= 2;
word_ptr -= 2;
}
}
if (in_quote) in_quote--;
} else {
in_escape = false;
}
if (word_ptr - word >= 254) {
show_fatal_error ("Expression is too long - must be 255 chars or less");
if (word)
strcpy (word, "0");
return true;
}
if (word)
*word_ptr++ = **ptr;
(*ptr)++;
}
finish_read_expr:
// Remove whitespace at the end
if (word) {
while (word_ptr > word && isspace((unsigned char) word_ptr[-1]))
*(--word_ptr) = '\0';
*word_ptr = '\0';
}
/* input is either the delimiter or null */
if (is_end_of_code_line (*ptr)) return true;
return true;
}
char *extract_arg_string(const char ** const ptr, arg_context_t *context)
{
bool fReadResult = read_expr((char **) ptr, context->arg, _T(","));
if (fReadResult == true)
{
if (**ptr == ',')
{
context->fExpectingMore = true;
(*ptr)++;
}
else
{
context->fExpectingMore = false;
}
}
else
{
if (context->fExpectingMore == true)
{
SetLastSPASMError(SPASM_ERR_VALUE_EXPECTED);
}
return NULL;
}
return context->arg;
}
char *escape_string(const char *input)
{
expand_buf_t *escape_string = eb_init(strlen(input) + strlen(input) / 2);
eb_append(escape_string, "\"", 1);
unsigned int i;
for (i = 0; i < strlen(input); i++)
{
switch (input[i])
{
case '"':
{
eb_append(escape_string, "\\\"", -1);
break;
}
case '\\':
{
eb_append(escape_string, "\\\\", -1);
break;
}
default:
{
eb_append(escape_string, &input[i], 1);
break;
}
}
}
eb_append(escape_string, "\"", 1);
char *result = eb_extract(escape_string);
eb_free(escape_string);
return result;
}
/*
* Removes surrounding quotation marks (if necessary)
* and reduces control characters
*/
char* reduce_string (char* input) {
char *output = input;
int quote_count = 0;
for (int i = 0; input[i] != '\0'; i++, output++)
{
if (input[i] == '\\')
{
int value;
char pbuf[8] = "'\\ '";
pbuf[2] = input[++i];
parse_num (pbuf, &value);
*output = value;
}
else if (input[i] == '"')
{
quote_count++;
*output = input[i];
}
else
{
*output = input[i];
}
}
*output = '\0';
// Count the quotes
if (quote_count >= 2)
{
if (input[0] == '"' && input[strlen(input) - 1] == '"')
{
input[strlen(input) - 1] = '\0';
memmove(input, input + 1, strlen(input));
}
}
return input;
}
/*
* Replaces backslashes in filename
* with forward slashes to work across
* different platforms
*/
char *fix_filename (char *filename) {
char *sl = strchr (filename, WRONG_PATH_SEPARATOR);
while (sl != NULL) {
sl[0] = PATH_SEPARATOR;
sl = strchr (sl + 1, WRONG_PATH_SEPARATOR);
}
return filename;
}
/*
* Checks to see if the given path is a full path or relative path
*/
bool is_abs_path(const char *filename) {
if (filename == NULL)
return false;
char *fn = skip_whitespace((char *) filename);
if (fn[0] == '/' || fn[0] == '\\')
return true;
if (strlen(fn) < 2)
return false;
if (fn[1] == ':' && isalpha(fn[0]))
return true;
return false;
}
/*
* Returns an allocated copy
* of the string converted to
* uppercase
*/
char *strup (const char *input) {
char *new_string = (char *) malloc (strlen (input) + 1);
/*int i;
for (i = 0; input[i] != '\0'; i++)
new_string[i] = toupper (input[i]);
new_string[i] = '\0';*/
//modp_toupper_copy(new_string, input, strlen(input));
modp_toupper_copy(new_string, input, strlen(input));
return new_string;
}
void release_file_contents(char *contents)
{
#ifdef USE_MEMORY_MAPPED_FILES
UnmapViewOfFile(contents);
#else
free(contents);
#endif
}
/*
* Gets file contents,
* sets size and
* returns address of
* allocated contents
* or NULL on error
*/
char *get_file_contents (const char *filename) {
#ifdef USE_MEMORY_MAPPED_FILES
HANDLE hFile = CreateFile(
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return NULL;
}
HANDLE hMapFile = CreateFileMapping(
hFile,
NULL,
PAGE_READONLY,
0L,
0L,
NULL);
LPBYTE lpData = (LPBYTE) MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
return (char *) lpData;
#else
FILE *file;
char *buffer;
size_t size, read_size;
// first try to open it
file = fopen (filename, "rb");
if (!file)
return NULL;
// find file size (Spencer says it's not a hack)
fseek (file, 0, SEEK_END);
size = ftell (file);
rewind (file);
// If there is a UTF-8 endian marker at the beginning of the file, skip it.
const unsigned char utf8_endian_mark[] = {0xEF, 0xBB, 0xBF};
bool matched = true;
for (unsigned i = 0; matched && i < sizeof(utf8_endian_mark); i++) {
int c = fgetc(file);
if (c == EOF) {
return NULL;
}
matched &= (unsigned char)c == utf8_endian_mark[i];
}
if (matched) {
size -= sizeof(utf8_endian_mark);
} else {
rewind(file);
}
// now allocate the memory and read in the contents
buffer = (char *) malloc (size + 1);
read_size = fread (buffer, 1, size, file);
fclose (file);
if (read_size != size) {
free (buffer);
return NULL;
}
buffer[size] = '\0';
return buffer;
#endif
}
/*
* check defined named "name" to see if
* it has the value "value". true
* if it does
*/
bool define_with_value (const char *name, const int value) {
define_t *define = search_defines (name);
if (define == NULL) return false;
return (parse_f (define->contents) == value);
}
/*
* Changes the filename extension of a file to the given ext. If
* the file does not have an extension, a new one is added on
*/
char *change_extension (const char *filename, const char *ext) {
char *dot;
char *new_fn = (char *) malloc(strlen(filename) + strlen(ext) + 2);
strcpy(new_fn, filename);
dot = strrchr(new_fn, '.');
if (dot != NULL) *dot = '\0';
strcat(new_fn, ".");
return strcat(new_fn, ext);
}
#if defined(WIN32) || defined(MACVER)
//char *strndup (const char *str, int len)
//{
// char *dupstr;
//
// dupstr = (char *) malloc(len + 1);
// if (dupstr) {
// strncpy (dupstr, str, len);
// dupstr[len] = '\0';
// }
// return dupstr;
//}
int strnlen (const char *str, int maxlen) {
int len = 0;
while (*(str + len) && len < maxlen)
len++;
return len;
}
#endif
/*
* Fully expands the text of an expression by
* replacing all macros and labels with their
* text replacements
*/
static void expand_expr_full (const char *expr, expand_buf *new_expr, int depth, bool search_local) {
const char *block_start;
char *name;
static char separators[] = "=+-*<>|&/%^()\\, \t\r";
define_t *define;
label_t *label;
int name_len;
if (depth > RECURSION_LIMIT) {
SetLastSPASMError (SPASM_ERR_RECURSION_DEPTH, RECURSION_LIMIT);
return;
}
while (*expr) {
//handle strings
if (*expr == '\"') {
block_start = expr++;
while (*expr && *expr != '\"') {
if (!(*expr == '\\' && *(++expr) == '\0'))
expr++;
}
if (*expr)
expr++;
eb_append (new_expr, block_start, expr - block_start);
}
//get the next name and look it up
name = skip_to_name_end (expr);
name_len = name - expr;
//skip if there's no name, or if it's a macro with arguments
if (name_len > 0) {
name = strndup (expr, name_len);
//if it's a define, recursively expand its contents
if ((define = search_defines (name, search_local)) && define->contents != NULL) {
list_t *args = NULL;
if (define->num_args > 0) {
expr = parse_args (expr + name_len, define, &args);
if (!expr) {
free (name);
return;
}
} else {
expr += name_len;
}
expand_expr_full (define->contents, new_expr, depth + 1, search_local);
remove_arg_set (args);
if (error_occurred) {
free(name);
return;
}
//if it's a label, write its value
} else if ((label = search_labels (name))) {
char buf[10];
snprintf (buf, sizeof(buf), "%d", label->value);
eb_append (new_expr, buf, -1);
expr += name_len;
//otherwise, it might be a forward ref, so just write the name
} else {
eb_append (new_expr, name, name_len);
expr += name_len;
}
free (name);
} else if (*expr) {
//if it can't be a name, just write the next char and move on
eb_append (new_expr, expr, 1);
expr++;
}
//skip past separators
block_start = expr;
while (*expr && strchr (separators, *expr))
expr++;
//write those
eb_append (new_expr, block_start, expr - block_start);
}
}
char *expand_expr (const char *expr, bool search_local) {
expand_buf *new_expr;
char *new_expr_text;
new_expr = eb_init(strlen(expr) * 2);
expand_expr_full (expr, new_expr, 0, search_local);
new_expr_text = eb_extract (new_expr);
eb_free (new_expr);
return new_expr_text;
}
/*
* Does a checked memory allocation,
* shows an error and exits if out
* of memory
*/
/*void *malloc (size_t size) {
void *ret = malloc (size);
if (ret != NULL)
return ret;
puts ("Out of memory, exiting");
exit (EXIT_FATAL_ERROR);
}*/
void show_error_prefix(const char *zcif, const int zln) {
//BuckeyeDude
//this is temporary till everything is rolled into errors.cpp
WORD attr = save_console_attributes();
#ifdef _WIN32
//TCHAR szPrefix[256];
//sprintf(szPrefix, "%s:%d: error: ", zcif, zln);
//OutputDebugString(szPrefix);
#endif
set_console_attributes (COLOR_RED);
printf ("%s:%d: error: ", zcif, zln);
restore_console_attributes(attr);
}
void show_error(const char *text, ...) {
WORD attr = save_console_attributes();
va_list args;
if (exit_code < EXIT_ERRORS) exit_code = EXIT_ERRORS;
show_error_prefix(curr_input_file, line_num);
set_console_attributes (COLOR_RED);
va_start(args, text);
vprintf (text, args);
putchar ('\n');
restore_console_attributes(attr);
}
void show_fatal_error(const char *text, ...) {
WORD attr = save_console_attributes();
va_list args;
if (exit_code < EXIT_FATAL_ERROR) exit_code = EXIT_FATAL_ERROR;
show_error_prefix(curr_input_file, line_num);
#ifdef WIN32
OutputDebugString(text);
OutputDebugString(TEXT("\n"));
#endif
set_console_attributes (COLOR_RED);
va_start(args, text);
vprintf (text, args);
putchar ('\n');
error_occurred = true;
restore_console_attributes(attr);
}
void show_warning_prefix(const char *zcif, int zln) {
WORD attr = save_console_attributes();
set_console_attributes (COLOR_YELLOW);
printf ("%s:%d: warning: ", zcif, zln);
restore_console_attributes(attr);
}
void show_warning(const char *text, ...) {
WORD attr = save_console_attributes();
va_list args;
if (exit_code < EXIT_WARNINGS) exit_code = EXIT_WARNINGS;
show_warning_prefix(curr_input_file, line_num);
#ifdef WIN32
OutputDebugString(text);
OutputDebugString(TEXT("\n"));
#endif
set_console_attributes (COLOR_YELLOW);
va_start(args, text);
vprintf (text, args);
putchar ('\n');
restore_console_attributes(attr);
}