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
|
/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
/* From input file "multfile.pas" */
#include "p2c.h"
#define MULTFILE_G
#include "multfile.h"
Char nextData[256] = "";
typedef struct filenode {
Char name[256];
FILE *actualfile;
struct filenode *prev;
short lineno;
Char actualfile_NAME[_FNSIZE];
} filenode;
Static filenode *current = NULL;
Static short last_valid_line_no = 0;
Static boolean inputerror = false;
Static short reportitem = reportnewfile + reportoldfile + reporterror +
reportrecursive;
void report(short items)
{
reportitem = items;
}
Static boolean recursive(Char *filename)
{
boolean Result;
filenode *previous;
if (current == NULL)
return false;
previous = current;
/* p2c: multfile.pas: Note: Eliminated unused assignment statement [338] */
while (previous != NULL) {
if (!strcmp(filename, previous->name))
return true;
previous = previous->prev;
}
return false;
}
void pushFile(Char *filename)
{
filenode *newnode;
Char STR3[256];
if (recursive(filename)) {
printf("===! Ignoring recursive include of file %s\n", filename);
return;
}
newnode = Malloc(sizeof(filenode));
newnode->actualfile = NULL;
strcpy(newnode->name, filename);
newnode->prev = current;
newnode->lineno = 0;
strcpy(newnode->actualfile_NAME, filename);
if (newnode->actualfile != NULL)
newnode->actualfile = freopen(newnode->actualfile_NAME, "r",
newnode->actualfile);
else
newnode->actualfile = fopen(newnode->actualfile_NAME, "r");
_SETIO(newnode->actualfile != NULL, FileNotFound);
inputerror = (P_ioresult != 0);
if (inputerror)
Free(newnode);
else
current = newnode;
if (!inputerror && (reportitem & reportnewfile) > 0)
printf("==>> Input from file %s\n", currentFilename(STR3));
if (inputerror && (reportitem & reporterror) > 0)
printf("==!! Could not open file %s\n", filename);
}
void popFile(void)
{
filenode *previous;
Char STR2[256];
Char STR3[256];
if (current == NULL)
return;
if ((reportitem & reportclose) > 0)
printf("==>> Closing file %s at line number %d\n",
currentFilename(STR2), currentLineNo());
if (current->actualfile != NULL)
fclose(current->actualfile);
current->actualfile = NULL;
previous = current->prev;
Free(current);
current = previous;
if (current != NULL && (reportitem & reportoldfile) > 0)
printf("==>> Resuming input from file %s at line number %d\n",
currentFilename(STR3), currentLineNo());
}
void closeAll(void)
{
do {
popFile();
} while (current != NULL);
}
boolean eofCurrent(void)
{
return P_eof(current->actualfile);
}
Char *readLine(Char *Result)
{
Char s[256];
Char *TEMP;
Char STR2[256];
if (*nextData != '\0') {
strcpy(Result, nextData);
*nextData = '\0';
return Result;
}
if (eofAll())
return strcpy(Result, "");
_SETIO(fgets(s, 256, current->actualfile) != NULL, EndOfFile);
TEMP = strchr(s, '\n');
if (TEMP != NULL)
*TEMP = 0;
strcpy(Result, s);
inputerror = (P_ioresult != 0);
if (!inputerror) {
current->lineno++;
last_valid_line_no = current->lineno;
}
if (inputerror && (reportitem & reporterror) > 0)
printf("==!! Could not read from file %s\n", currentFilename(STR2));
return Result;
}
boolean isEmpty(Char *s)
{
short i, FORLIM;
if (*s == '\0')
return true;
FORLIM = strlen(s);
for (i = 0; i <= FORLIM - 1; i++) {
if (s[i] != ' ')
return false;
}
return true;
}
Char *readData(Char *Result)
{
Char s[256];
if (!isEmpty(nextData)) {
strcpy(Result, nextData);
*nextData = '\0';
return Result;
}
while (!eofAll()) {
readLine(s);
if (!isEmpty(s))
return strcpy(Result, s);
}
return strcpy(Result, "");
}
void skipBlanks(void)
{
while (*nextData == '\0') {
readData(nextData);
if (eofAll())
return;
}
}
boolean eofAll(void)
{
boolean Result;
/* p2c: multfile.pas: Note: Eliminated unused assignment statement [338] */
if (current == NULL)
return true;
if (eofCurrent()) {
popFile();
return (eofAll());
}
return false;
}
short currentLineNo(void)
{
return last_valid_line_no;
}
Char *currentFilename(Char *Result)
{
if (current == NULL)
return strcpy(Result, "No file open yet");
else
return strcpy(Result, current->name);
}
boolean fileError(void)
{
return inputerror;
}
/* End. */
|