-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.c
More file actions
125 lines (110 loc) · 2.86 KB
/
source.c
File metadata and controls
125 lines (110 loc) · 2.86 KB
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
/**
** "i8086" An assembler for the 16-bit Intel x86 CPUs
**
** Copyright (C) 2024 Jeff Penfold (jeff.penfold@googlemail.com)
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
/*
* Source
* ======
*
* Simplified, nesting, source line input mechanism.
*/
#include "os.h"
#include "includes.h"
/*
* Provide a stream like system to access nested file
* includes.
*
* Define data structures used to track this.
*/
typedef struct {
char *fname;
int line;
FILE *fd;
} file_record;
/*
* The 'stack' of opened source files.
*/
static file_record file_io[ MAX_FILE_NESTING ];
static int nested_files = 0;
/*
* Declare a routine called to insert a new file into the stream.
* This file will provide the next line of text to be processed
* by the assembler. Return TRUE if it's all worked.
*/
boolean include_file( char *name ) {
file_record *fr;
if( nested_files == MAX_FILE_NESTING ) {
log_error_i( "Maximum file nesting reached", MAX_FILE_NESTING );
return( FALSE );
}
fr = &( file_io[ nested_files ]);
if(( fr->fd = fopen( name, "r" )) == NIL( FILE )) {
log_error_s( "Unable to read file", name );
return( FALSE );
}
fr->fname = save_string( name );
fr->line = 0;
nested_files++;
return( TRUE );
}
/*
* Pull off the next line for the input stream.
*/
boolean next_line( char *buffer, int len ) {
while( nested_files ) {
file_record *fr;
fr = &( file_io[ nested_files-1 ]);
if(( fgets( buffer, len, fr->fd )) != NIL( char )) {
buffer[ len-1 ] = EOS;
fr->line += 1;
return( TRUE );
}
fclose( fr->fd );
nested_files--;
}
return( FALSE );
}
/*
* Skip to end closes the current file. If this is the
* last file open (ie the file from the command line)
* then the next call to "next_line" to return FALSE
* indicating the end of the input data.
*/
boolean skip_to_end( void ) {
if( nested_files ) {
fclose( file_io[ --nested_files ].fd );
return( TRUE );
}
return( FALSE );
}
/*
* Send a description of where we are to a FILE.
*
* This works even if we are not in a file through
* the simple expedient of not outputting anything.
*/
void error_is_at( FILE *to ) {
int i;
for( i = nested_files; i; i-- ) {
file_record *fr;
fr = &( file_io[ i-1 ]);
fprintf( to, "%s:%d,", fr->fname, fr->line );
}
}
/*
* EOF
*/