-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
1371 lines (1232 loc) · 41.7 KB
/
input.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
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* 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: processing input line data and line queueing for macros
*
****************************************************************************/
#include <ctype.h>
#include <stdarg.h>
#include <sys/stat.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "reswords.h"
#include "condasm.h"
#include "equate.h"
#include "macro.h"
#include "labels.h"
#include "input.h"
#include "tokenize.h"
#include "proc.h"
#include "fastpass.h"
#include "listing.h"
#include "myassert.h"
extern struct ReservedWord ResWordTable[];
extern ret_code (* const directive[])( int, struct asm_tok[] );
extern char inside_comment;
#define REMOVECOMENT 0 /* 1=remove comments from source */
#define DETECTCTRLZ 1
/* FILESEQ: if 1, stores a linked list of source files, ordered
* by usage. Masm stores such a list in the COFF symbol table
* when -Zd/-Zi is set. It isn't necessary, however, and JWasm's
* COFF code currently will ignore the list.
*/
#define FILESEQ 0
char *commentbuffer;
struct asym *FileCur = NULL; /* @FileCur symbol */
struct asym LineCur = { NULL,"@Line", 0 };
static int queue_level; /* number of line queues onto the file stack */
/* SrcAlloc() and SrcFree() are used to store/release
* "file" items onto the file stack and "line queue" items.
*
* MemAlloc() uses the normal C heap functions.
* LclAlloc() uses the "fast" replacement if FASTMEM=1.
* it's probably best to use the first, since "file"
* and "line queue" items are "short-lived".
*/
#define SrcAlloc(x) MemAlloc(x)
#define SrcFree(x) MemFree(x)
/* item of a line queue */
struct line_list {
struct line_list *next;
#ifdef DEBUG_OUT
char lineno;
#endif
char line[1];
};
struct input_queue {
struct line_list *head;
struct line_list *tail;
};
struct file_list {
struct file_list *next;
union {
FILE *file; /* if item is a file */
struct macro_instance *mi; /* if item is a macro */
struct input_queue *lines; /* if item is a line queue */
};
uint_32 line_num; /* current line */
struct asym *macro; /* the symbol if it is a macro */
uint_16 srcfile; /* index of file in FNamesTab */
unsigned char islinesrc:1;
};
/* NOTE: the line queue is a simple list of lines.
if it must be nested, it is converted to a file_list item
and pushed onto the file stack.
*/
//static struct input_queue *line_queue; /* line queue */
//static struct file_list *file_stack; /* source item (file/macro) stack */
//static char *IncludePath;
#define line_queue ModuleInfo.g.line_queue
#define file_stack ModuleInfo.g.file_stack
#if FILESEQ
struct qdesc FileSeq;
#endif
#ifdef DEBUG_OUT
struct asm_tok *end_tokenarray;
char *end_stringbuf;
static char currlqline;
static long cntppl0;
static long cntppl1;
static long cntppl2;
static long cntflines;
static long cntlines;
int lq_line;
long cnttok0;
long cnttok1;
#endif
/* buffer for source lines
* since the lines are sometimes concatenated
* the buffer must be a multiple of MAX_LINE_LEN
*/
static char *srclinebuffer;
char *token_stringbuf; /* start token string buffer */
/* fixme: add '|| defined(__CYGWIN__)' ? */
#if defined(__UNIX__)
#define INC_PATH_DELIM ':'
#define INC_PATH_DELIM_STR ":"
#define DIR_SEPARATOR '/'
#define filecmp strcmp
#define _stat stat
#else
#define INC_PATH_DELIM ';'
#define INC_PATH_DELIM_STR ";"
#define DIR_SEPARATOR '\\'
#define filecmp _stricmp
#if defined(__CYGWIN__)
#define _stat stat
#endif
#endif
static char *GetFullPath( const char *name, char *buff, size_t max )
/******************************************************************/
{
char *p;
p = _fullpath( buff, name, max );
if( p == NULL )
p = (char *)name;
#if defined(__UNIX__)
if( (p[0] == '/' && p[1] == '/') && (name[0] != '/' || name[1] != '/') ) {
/*
* if the _fullpath result has a node number and
* the user didn't specify one, strip the node number
* off before returning
*/
p += 2;
while( *(p++) != '/' ) ;
}
#endif
return( p );
}
static time_t GetFileTimeStamp( const char *filename )
/****************************************************/
{
struct _stat statbuf;
if( _stat( filename, &statbuf ) != 0 ) {
return( 0 );
}
return( statbuf.st_mtime );
}
/* check if a file is in the array of known files.
* if no, store the file at the array's end.
* returns array index.
* the array is stored in the standard C heap!
* the filenames are stored in the "local" heap.
*/
static uint AddFile( char const *fname )
/**************************************/
{
struct fname_list *newfn;
uint index;
char name[_MAX_FNAME];
char ext[_MAX_EXT];
DebugMsg(("AddFile(%s) enter\n", fname ));
for( index = 0; index < ModuleInfo.g.cnt_fnames; index++ ) {
if( filecmp( fname, ( FNamesTab + index )->fullname ) == 0 )
return( index );
}
if ( ( index % 64 ) == 0 ) {
newfn = (struct fname_list *)MemAlloc( ( index + 64) * sizeof( struct fname_list ) );
if ( FNamesTab ) {
memcpy( newfn, FNamesTab, index * sizeof( struct fname_list ) );
MemFree( FNamesTab );
}
FNamesTab = newfn;
}
ModuleInfo.g.cnt_fnames = index + 1;
_splitpath( fname, NULL, NULL, name, ext );
#if 0
(FNamesTab+index)->mtime = GetFileTimeStamp( fname );
(FNamesTab+index)->name = (char *)LclAlloc( strlen( name ) + strlen( ext ) + 1 );
strcpy( (FNamesTab+index)->name, name );
strcat( (FNamesTab+index)->name, ext );
(FNamesTab+index)->fullname = (char *)LclAlloc( strlen( fname ) + 1 );
strcpy( (FNamesTab+index)->fullname, fname );
#else
newfn = FNamesTab + index;
/* timestamp needed for autodependancy records only */
if( Options.line_numbers )
newfn->mtime = GetFileTimeStamp( fname );
newfn->name = (char *)LclAlloc( strlen( name ) + strlen( ext ) + 1 );
strcpy( newfn->name, name );
strcat( newfn->name, ext );
newfn->fullname = (char *)LclAlloc( strlen( fname ) + 1 );
strcpy( newfn->fullname, fname );
#endif
return( index );
}
const struct fname_list *GetFName( uint index )
/*********************************************/
{
return( FNamesTab+index );
}
/* free the file array */
static void FreeFiles( void )
/***************************/
{
#if FASTMEM==0
int i;
for ( i = 0; i < ModuleInfo.g.cnt_fnames; i++ ) {
LclFree( (FNamesTab + i)->name );
LclFree( (FNamesTab+i)->fullname );
}
#endif
MemFree( FNamesTab );
FNamesTab = NULL;
return;
}
/* free a line queue */
static void FreeLineQueue( struct input_queue *queue )
/****************************************************/
{
struct line_list *curr;
struct line_list *next;
for( curr = queue->head; curr; curr = next ) {
next = curr->next;
SrcFree( curr );
}
SrcFree( queue );
}
/* clear input source stack (include files and open macros).
Usually the stack is empty when the END directive occurs,
but it isn't required that the END directive is located in
the main source file. Also, an END directive might be
simulated if a "too many errors" condition occurs.
*/
void ClearFileStack( void )
/*************************/
{
struct file_list *nextfile;
DeleteLineQueue();
/* dont close the last item (which is the main src file) */
for( ; file_stack->next ; file_stack = nextfile ) {
nextfile = file_stack->next;
if ( file_stack->islinesrc && ( file_stack->macro == NULL ) ) {
FreeLineQueue( file_stack->lines );
} else {
fclose( file_stack->file );
}
SrcFree( file_stack );
}
return;
}
/* returns value of predefined symbol @Line */
void UpdateLineNumber( struct asym *sym )
/***************************************/
{
struct file_list *fl;
for ( fl = file_stack; fl ; fl = fl->next )
if ( fl->islinesrc == FALSE ) {
sym->value = fl->line_num;
break;
}
return;
}
uint_32 GetLineNumber( void )
/***************************/
{
UpdateLineNumber( &LineCur );
return( LineCur.uvalue );
}
#ifdef DEBUG_OUT
char *GetTopLine( char *buffer )
/******************************/
{
*buffer = NULLC;
if ( lq_line )
sprintf( buffer, "(%u)", lq_line );
else if( file_stack->islinesrc == TRUE )
sprintf( buffer, "[%s.%lu]", file_stack->macro ? file_stack->macro->name : "", file_stack->line_num );
return( buffer );
}
#endif
/* read one line from current source file.
* returns NULL if EOF has been detected and no char stored in buffer
* v2.08: 00 in the stream no longer causes an exit. Hence if the
* char occurs in the comment part, everything is ok.
*/
static char *my_fgets( char *buffer, int max, FILE *fp )
/******************************************************/
{
char *ptr = buffer;
char *last = buffer + max;
int c;
c = getc( fp );
while( ptr < last ) {
switch ( c ) {
case '\r':
break; /* don't store CR */
case '\n':
/* fall through */
//case '\0': /* v2.08: */
#ifdef DEBUG_OUT
if ( Parse_Pass == PASS_1 )
cntflines++;
#endif
*ptr = NULLC;
return( buffer );
#if DETECTCTRLZ
case 0x1a:
/* since source files are opened in binary mode, ctrl-z
* handling must be done here.
*/
/* no break */
#endif
case EOF:
*ptr = NULLC;
return( ptr > buffer ? buffer : NULL );
default:
*ptr++ = c;
}
c = getc( fp );
}
EmitErr( LINE_TOO_LONG );
*(ptr-1) = NULLC;
return( buffer );
}
#if FILESEQ
void AddFileSeq( uint file )
/**************************/
{
struct file_seq *node;
node = LclAlloc( sizeof( struct file_seq ) );
node->next = NULL;
node->file = file;
if ( FileSeq.head == NULL )
FileSeq.head = FileSeq.tail = node;
else {
((struct file_seq *)FileSeq.tail)->next = node;
FileSeq.tail = node;
}
}
#endif
/* add a new item to the top of the file stack.
* is_linesrc: TRUE=item is a macro or a line queue.
* sym = macro symbol or NULL (for a real file or the line queue)
*/
static struct file_list *PushLineSource( bool is_linesrc, struct asym *sym )
/**************************************************************************/
{
struct file_list *fl;
fl = SrcAlloc( sizeof( struct file_list ) );
fl->next = file_stack;
fl->islinesrc = is_linesrc;
fl->line_num = 0;
fl->macro = sym;
file_stack = fl;
#ifdef DEBUG_OUT
currlqline = 0;
#endif
return( fl );
}
/*
* If there's a current line queue, push it onto the file stack.
*/
void NewLineQueue( void )
/***********************/
{
DebugMsg1(( "NewLineQueue() enter [line_queue=%X]\n", line_queue ));
if ( line_queue ) {
struct file_list *fl;
fl = PushLineSource( TRUE, NULL );
queue_level++;
fl->srcfile = get_curr_srcfile();
fl->lines = line_queue;
line_queue = NULL;
}
#ifdef DEBUG_OUT
currlqline = 0;
#endif
}
void DeleteLineQueue( void )
/**************************/
{
if ( line_queue ) {
FreeLineQueue( line_queue );
line_queue = NULL;
}
}
bool is_linequeue_populated( void )
/*********************************/
{
return( line_queue != NULL );
}
/* Add a line to the current line queue. */
void AddLineQueue( const char *line )
/***********************************/
{
unsigned i = strlen( line );
struct line_list *new;
DebugMsg1(( "AddLineQueue(%u): >%s<\n", ++currlqline, line ));
if ( line_queue == NULL ) {
line_queue = SrcAlloc( sizeof( struct input_queue ) );
line_queue->tail = NULL;
}
new = SrcAlloc( sizeof( struct line_list ) + i );
new->next = NULL;
#ifdef DEBUG_OUT
new->lineno = currlqline;
#endif
memcpy( new->line, line, i + 1 );
if( line_queue->tail == NULL ) {
line_queue->head = new;
} else {
/* insert at the tail */
line_queue->tail->next = new;
}
line_queue->tail = new;
return;
}
/* Add a line to the current line queue, "printf" format. */
void AddLineQueueX( const char *fmt, ... )
/****************************************/
{
va_list args;
char *d;
int i;
long l;
const char *s;
const char *p;
char buffer[MAX_LINE_LEN];
//DebugMsg(("AddlineQueueX(%s) enter\n", fmt ));
va_start( args, fmt );
for ( s = fmt, d = buffer; *s; s++ ) {
if ( *s == '%' ) {
s++;
switch ( *s ) {
case 'r':
i = va_arg( args, int );
GetResWName( i , d );
/* v2.06: the name is already copied */
//memcpy( d, ResWordTable[i].name, ResWordTable[i].len );
d += ResWordTable[i].len;
break;
case 's':
p = va_arg( args, char * );
i = strlen( p );
memcpy( d, p, i );
d += i;
*d = NULLC;
break;
case 'd':
case 'u':
case 'x':
#ifdef __I86__ /* v2.08: use long only if size(int) is 16-bit */
l = va_arg( args, long );
#else
l = va_arg( args, int );
#endif
if ( *s == 'x' ) {
myltoa( l, d, 16, FALSE, FALSE );
d += strlen( d );
} else {
myltoa( l, d, 10, l < 0, FALSE );
d += strlen( d );
/* v2.07: add a 't' suffix if radix is != 10 */
if ( ModuleInfo.radix != 10 )
*d++ = 't';
}
break;
default:
*d++ = *s;
}
} else
*d++ = *s;
}
*d = NULLC;
va_end( args );
//DebugMsg(("AddlineQueueX() done\n" ));
AddLineQueue( buffer );
return;
}
/* push the current line queue onto the file stack and
* associate a macro name to it so it can be displayed
* in case of errors. Param <line> is != 0 when GOTO
* is handled.
*/
void PushMacro( struct asym *macro, struct macro_instance *mi, unsigned line )
/****************************************************************************/
{
struct file_list *fl;
DebugMsg1(( "PushMacro(%s), queue level=%u\n", macro->name, queue_level ));
fl = PushLineSource( TRUE, macro );
fl->mi = mi;
//line_queue = NULL;
queue_level++;
fl->line_num = line;
return;
}
#if FASTMEM==0
bool MacroInUse( struct dsym *macro )
/***********************************/
{
struct file_list *fl;
for ( fl = file_stack; fl ; fl = fl->next )
if ( fl->macro == ¯o->sym )
return( TRUE );
return( FALSE );
}
#endif
uint get_curr_srcfile( void )
/***************************/
{
#if 1
struct file_list *fl;
for ( fl = file_stack; fl ; fl = fl->next )
if ( fl->islinesrc == FALSE )
return( fl->srcfile );
return( ModuleInfo.srcfile );
#else
return( file_stack ? file_stack->srcfile : ModuleInfo.srcfile );
#endif
}
void set_curr_srcfile( uint file, uint_32 line_num )
/**************************************************/
{
if ( file != 0xFFF ) /* 0xFFF is the special value for macro lines */
file_stack->srcfile = file;
file_stack->line_num = line_num;
return;
}
/* for error listing, render the current source file and line */
/* this function is also called if pass is > 1,
* which is a problem for FASTPASS because the file stack is empty.
*/
int GetCurrSrcPos( char *buffer )
/*******************************/
{
struct file_list *fl;
uint_32 line;
line = LineNumber;
for( fl = file_stack; fl && fl->islinesrc; fl = fl->next );
if ( fl ) {
if ( line )
return( sprintf( buffer, "%s(%lu) : ", GetFName( fl->srcfile )->name , line ) );
else
return( sprintf( buffer, "%s : ", GetFName( fl->srcfile )->name ) );
}
*buffer = NULLC;
return( 0 );
}
/* for error listing, render the source nesting structure.
* the structure consists of include files and macros.
*/
void print_source_nesting_structure( void )
/*****************************************/
{
struct file_list *fl;
unsigned tab = 1;
/* in main source file? */
if ( file_stack == NULL || file_stack->next == NULL )
return;
for( fl = file_stack; fl->next ; fl = fl->next ) {
if( fl->islinesrc == FALSE ) {
PrintNote( NOTE_INCLUDED_BY, tab, "", GetFName( fl->srcfile)->name, fl->line_num );
tab++;
} else if ( fl->macro != NULL ) {
//char fname[_MAX_FNAME+_MAX_EXT];
#ifndef __I86__ /* this function may be called on low stack condition */
char fname[_MAX_FNAME];
char fext[_MAX_EXT];
#endif
if (*(fl->macro->name) == NULLC ) {
PrintNote( NOTE_ITERATION_MACRO_CALLED_FROM, tab, "", "MacroLoop", fl->line_num, fl->macro->value + 1 );
} else {
#ifdef __I86__
PrintNote( NOTE_MACRO_CALLED_FROM, tab, "", fl->macro->name, fl->line_num, GetFName(((struct dsym *)fl->macro)->e.macroinfo->srcfile)->name, "" ) ;
#else
_splitpath( GetFName(((struct dsym *)fl->macro)->e.macroinfo->srcfile)->name, NULL, NULL, fname, fext );
PrintNote( NOTE_MACRO_CALLED_FROM, tab, "", fl->macro->name, fl->line_num, fname, fext );
#endif
}
tab++;
}
}
PrintNote( NOTE_MAIN_LINE_CODE, tab, "", GetFName(fl->srcfile)->name, fl->line_num );
}
/* Scan the include path for a file!
* variable IncludePath also contains directories set with -I cmdline option
*/
static FILE *open_file_in_include_path( const char *name, char fullpath[] )
/*************************************************************************/
{
char *curr;
char *next;
int i;
int namelen;
FILE *file = NULL;
while( isspace( *name ) )
name++;
curr = ModuleInfo.g.IncludePath;
namelen = strlen( name );
DebugMsg(("open_file_in_include_path(%s) enter\n", name ));
for ( ; curr; curr = next ) {
next = strchr( curr, INC_PATH_DELIM );
if ( next ) {
i = next - curr;
next++; /* skip path delimiter char (; or :) */
} else {
i = strlen( curr );
}
/* v2.06: ignore
* - "empty" entries in PATH
* - entries which would cause a buffer overflow
*/
if ( i == 0 || ( ( i + namelen ) >= _MAX_PATH ) )
continue;
memcpy( fullpath, curr, i );
if( fullpath[i-1] != '/'
#if !defined(__UNIX__)
&& fullpath[i-1] != '\\' && fullpath[i-1] != ':'
#endif
) {
fullpath[i] = DIR_SEPARATOR;
i++;
}
strcpy( fullpath+i, name );
DebugMsg(("open_file_in_include_path: >%s<\n", fullpath ));
file = fopen( fullpath, "rb" );
if( file ) {
break;
}
}
DebugMsg(("open_file_in_include_path()=%p\n", file ));
return( file );
}
/* the worker behind the INCLUDE directive. Also used
* by INCBIN and to include the main source file.
*/
ret_code InputQueueFile( const char *path, FILE * *pfile )
/********************************************************/
{
FILE *file = NULL;
struct file_list *fl;
char fullpath[ _MAX_PATH ];
char buffer[ _MAX_PATH ];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char drive2[_MAX_DRIVE];
char dir2[_MAX_DIR];
DebugMsg(("InputQueueFile(%s) enter\n", path ));
_splitpath( path, drive, dir, fname, ext );
DebugMsg(("InputQueueFile(): drive=%s, dir=%s, fname=%s, ext=%s\n", drive, dir, fname, ext ));
/* if no absolute path is given, then search in the directory
of the current source file first! */
if ( dir[0] != '\\' && dir[0] != '/' ) {
for ( fl = file_stack; fl ; fl = fl->next ) {
if ( fl->islinesrc == FALSE ) {
_splitpath( GetFName( fl->srcfile )->fullname, drive2, dir2, NULL, NULL );
DebugMsg(("InputQueueFile(): curr src=%s, split into drive=%s, dir=%s\n", GetFName( fl->srcfile)->fullname, drive2, dir2 ));
if ( dir2[0] == '\\' || dir2[0] == '/' ) {
_makepath( fullpath, drive2, dir2, fname, ext );
file = fopen( fullpath, "rb" );
DebugMsg(("InputQueueFile(): makepath()=%s, file=%X\n", fullpath, file ));
}
break;
}
}
}
if ( file == NULL ) {
fullpath[0] = NULLC;
file = fopen( path, "rb" );
DebugMsg(("InputQueueFile(): file=%X\n", file ));
/* if the file wasn't found, and include paths have been set,
* and NO absolute path is given, then search include dirs */
if( file == NULL && ModuleInfo.g.IncludePath != NULL && dir[0] != '\\' && dir[0] != '/' ) {
file = open_file_in_include_path( path, fullpath );
DebugMsg(("InputQueueFile(): open_file_in_include_path(%s, %s) returned file=%X\n", path, fullpath, file ));
}
if( file == NULL ) {
EmitErr( CANNOT_OPEN_FILE, path, ErrnoStr() );
return( ERROR );
}
}
if ( pfile )
*pfile = file;
else {
fl = PushLineSource( FALSE, NULL );
fl->srcfile = AddFile( GetFullPath( fullpath[0] ? fullpath : path, buffer, sizeof( buffer ) ) );
FileCur->string_ptr = GetFName( fl->srcfile )->name;
#if FILESEQ
if ( Options.line_numbers && Parse_Pass == PASS_1 )
AddFileSeq( fl->srcfile );
#endif
fl->file = file;
}
return( NOT_ERROR );
}
/* get the next source line. */
char *GetTextLine( char *buffer )
/*******************************/
{
struct line_list *inputline;
struct file_list *fl;
*buffer = NULLC;
/* Check the line_queue first!
* The line_queue is global and there is ONE only.
* If it must be nested, it's pushed onto the file stack.
*/
if ( line_queue != NULL ) {
if ( inputline = line_queue->head ) {
strcpy( buffer, inputline->line );
line_queue->head = inputline->next;
SrcFree( inputline );
#ifdef DEBUG_OUT
lq_line++;
if ( Parse_Pass == PASS_1 ) cntlines++;
#endif
return( buffer );
}
#ifdef DEBUG_OUT
lq_line = 0;
#endif
SrcFree( line_queue );
line_queue = NULL;
DebugMsg1(("GetTextLine: end of line queue\n" ));
return( NULL );
}
/* Now check the file stack!
* items on the file stack may be
* - pushed line queues ( islinesrc == TRUE && macro == NULL )
* - macro line queues ( islinesrc == TRUE && macro != NULL )
* - assembly files. ( islinesrc == FALSE )
*/
while( 1 ) {
fl = file_stack;
if( fl->islinesrc == FALSE ) {
if( my_fgets( buffer, MAX_LINE_LEN, fl->file ) ) {
fl->line_num++;
#ifdef DEBUG_OUT
if ( Parse_Pass == PASS_1 ) cntlines++;
#endif
return( buffer );
}
/* EOF of main module reached? */
if ( fl->next == NULL )
break;
file_stack = fl->next;
fclose( fl->file );
DebugMsg1(("GetTextLine: ***** EOF file %s *****\n", GetFName( fl->srcfile )->name ));
SrcFree( fl );
for( fl = file_stack; fl->islinesrc; fl = fl->next );
FileCur->string_ptr = GetFName( fl->srcfile)->name;
#if FILESEQ
if ( Options.line_numbers && Parse_Pass == PASS_1 )
AddFileSeq( fl->srcfile );
#endif
} else if ( fl->macro ) {
/* item is a macro */
fl->mi->currline = ( fl->mi->currline ? fl->mi->currline->next : fl->mi->startline );
if ( fl->mi->currline ) {
/* if line contains placeholders, replace them by current values */
if ( fl->mi->currline->ph_count ) {
fill_placeholders( buffer,
fl->mi->currline->line,
fl->mi->parmcnt,
fl->mi->localstart, fl->mi->parm_array );
} else {
strcpy( buffer, fl->mi->currline->line );
}
fl->line_num++;
#ifdef DEBUG_OUT
if ( Parse_Pass == PASS_1 ) cntlines++;
#endif
return( buffer );
}
queue_level--;
file_stack = fl->next;
SrcFree( fl );
DebugMsg1(("GetTextLine: qlvl=%u, end of macro, file stack=%p\n", queue_level, file_stack ));
break;
} else {
/* item is a line queue */
inputline = fl->lines->head;
if( inputline != NULL ) {
fl->line_num++;
strcpy( buffer, inputline->line );
fl->lines->head = inputline->next;
SrcFree( inputline );
#ifdef DEBUG_OUT
if ( Parse_Pass == PASS_1 ) cntlines++;
#endif
return( buffer );
}
queue_level--;
file_stack = fl->next;
SrcFree( fl->lines );
SrcFree( fl );
DebugMsg1(("GetTextLine: qlvl=%u, end of line queue, file stack=%p\n", queue_level, file_stack ));
break;
}
}
return( NULL ); /* end of main source file or macro reached */
}
/* add a string to the include path.
* called for -I cmdline options.
* the include path is rebuilt for each assembled module.
* it is stored in the standard C heap.
*/
void AddStringToIncludePath( const char *string )
/***********************************************/
{
char *tmp;
int len;
DebugMsg(("AddStringToIncludePath(%s) enter\n", string ));
while( isspace( *string ) )
string++;
len = strlen( string );
if ( len == 0 )
return;
if( ModuleInfo.g.IncludePath == NULL ) {
ModuleInfo.g.IncludePath = MemAlloc( len + 1 );
strcpy( ModuleInfo.g.IncludePath, string );
} else {
tmp = ModuleInfo.g.IncludePath;
ModuleInfo.g.IncludePath = MemAlloc( strlen( tmp ) + sizeof( INC_PATH_DELIM_STR ) +
len + 1 );
strcpy( ModuleInfo.g.IncludePath, tmp );
strcat( ModuleInfo.g.IncludePath, INC_PATH_DELIM_STR );
strcat( ModuleInfo.g.IncludePath, string );
MemFree( tmp );
}
}
#if 0
/* function to get value of @FileCur.
* won't work, because text macros don't use asym.sfunc_ptr
*/
static void GetFileCur( struct asym *sym )
/****************************************/
{
struct file_list *fl;
for( fl = file_stack; fl && fl->islinesrc; fl = fl->next );
sym->string_ptr = GetFName( fl->srcfile )->name;
DebugMsg1(("GetFileCur: curr value=%s\n", sym->string_ptr ));
}
#endif
#ifdef __I86__
#define SIZE_SRCLINES ( MAX_LINE_LEN * 2 )
#define SIZE_TOKENARRAY ( sizeof( struct asm_tok ) * MAX_TOKEN )
#define SIZE_STRINGBUFFER ( MAX_LINE_LEN * 2 )
#else
#define SIZE_SRCLINES ( MAX_LINE_LEN * ( MAX_MACRO_NESTING + 1 ) )
#define SIZE_TOKENARRAY ( sizeof( struct asm_tok ) * MAX_TOKEN * MAX_MACRO_NESTING )
#define SIZE_STRINGBUFFER ( MAX_LINE_LEN * MAX_MACRO_NESTING )
#endif
/* Initializer, called once for each module. */
void InputInit( void )
/********************/
{
struct file_list *fl;
char path[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
DebugMsg(( "InputInit() enter\n" ));
//cnt_fnames = 0;
//FNamesTab = NULL;
//IncludePath = NULL;
//file_stack = NULL;
#if FILESEQ
FileSeq.head = NULL;
#endif
fl = PushLineSource( FALSE, NULL );