1 /* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
2 /* From input file "multfile.pas" */
3 
4 
5 #include "p2c.h"
6 
7 
8 #define MULTFILE_G
9 #include "multfile.h"
10 
11 
12 Char nextData[256] = "";
13 
14 
15 typedef struct filenode {
16   Char name[256];
17   FILE *actualfile;
18   struct filenode *prev;
19   short lineno;
20   Char actualfile_NAME[_FNSIZE];
21 } filenode;
22 
23 
24 Static filenode *current = NULL;
25 Static boolean inputerror = false;
26 Static short reportitem = reportnewfile + reportoldfile + reporterror +
27 			  reportrecursive;
28 
29 
report(short items)30 void report(short items)
31 {
32   reportitem = items;
33 }
34 
35 
recursive(Char * filename)36 Static boolean recursive(Char *filename)
37 {
38   boolean Result;
39   filenode *previous;
40 
41   if (current == NULL)
42     return false;
43   previous = current;
44 /* p2c: multfile.pas: Note: Eliminated unused assignment statement [338] */
45   while (previous != NULL) {
46     if (!strcmp(filename, previous->name))
47       return true;
48     previous = previous->prev;
49   }
50   return false;
51 }
52 
53 
pushFile(Char * filename)54 void pushFile(Char *filename)
55 {
56   filenode *newnode;
57   Char STR3[256];
58 
59   if (recursive(filename)) {
60     printf("===! Ignoring recursive include of file %s\n", filename);
61     return;
62   }
63   newnode = Malloc(sizeof(filenode));
64   newnode->actualfile = NULL;
65   strcpy(newnode->name, filename);
66   newnode->prev = current;
67   newnode->lineno = 0;
68   strcpy(newnode->actualfile_NAME, filename);
69   if (newnode->actualfile != NULL)
70     newnode->actualfile = freopen(newnode->actualfile_NAME, "r",
71 				  newnode->actualfile);
72   else
73     newnode->actualfile = fopen(newnode->actualfile_NAME, "r");
74   _SETIO(newnode->actualfile != NULL, FileNotFound);
75   inputerror = (P_ioresult != 0);
76   if (inputerror)
77     Free(newnode);
78   else
79     current = newnode;
80   if (!inputerror && (reportitem & reportnewfile) > 0)
81     printf("==>> Input from file %s\n", currentFilename(STR3));
82   if (inputerror && (reportitem & reporterror) > 0)
83     printf("==!! Could not open file %s\n", filename);
84 }
85 
86 
popFile(void)87 void popFile(void)
88 {
89   filenode *previous;
90   Char STR2[256];
91   Char STR3[256];
92 
93   if (current == NULL)
94     return;
95   if ((reportitem & reportclose) > 0)
96     printf("==>> Closing file %s at line number %d\n",
97 	   currentFilename(STR2), currentLineNo());
98   if (current->actualfile != NULL)
99     fclose(current->actualfile);
100   current->actualfile = NULL;
101   previous = current->prev;
102   Free(current);
103   current = previous;
104   if (current != NULL && (reportitem & reportoldfile) > 0)
105     printf("==>> Resuming input from file %s at line number %d\n",
106 	   currentFilename(STR3), currentLineNo());
107 }
108 
109 
closeAll(void)110 void closeAll(void)
111 {
112   do {
113     popFile();
114   } while (current != NULL);
115 }
116 
117 
eofCurrent(void)118 boolean eofCurrent(void)
119 {
120   return P_eof(current->actualfile);
121 }
122 
123 
readLine(Char * Result)124 Char *readLine(Char *Result)
125 {
126   Char s[256];
127   Char *TEMP;
128   Char STR2[256];
129 
130   if (*nextData != '\0') {
131     strcpy(Result, nextData);
132     *nextData = '\0';
133     return Result;
134   }
135   if (eofAll())
136     return strcpy(Result, "");
137   _SETIO(fgets(s, 256, current->actualfile) != NULL, EndOfFile);
138   TEMP = strchr(s, '\n');
139   if (TEMP != NULL)
140     *TEMP = 0;
141   strcpy(Result, s);
142   inputerror = (P_ioresult != 0);
143   if (!inputerror)
144     current->lineno++;
145   if (inputerror && (reportitem & reporterror) > 0)
146     printf("==!! Could not read from file %s\n", currentFilename(STR2));
147   return Result;
148 }
149 
150 
isEmpty(Char * s)151 boolean isEmpty(Char *s)
152 {
153   short i, FORLIM;
154 
155   if (*s == '\0')
156     return true;
157   FORLIM = strlen(s);
158   for (i = 0; i <= FORLIM - 1; i++) {
159     if (s[i] != ' ')
160       return false;
161   }
162   return true;
163 }
164 
165 
readData(Char * Result)166 Char *readData(Char *Result)
167 {
168   Char s[256];
169 
170   if (!isEmpty(nextData)) {
171     strcpy(Result, nextData);
172     *nextData = '\0';
173     return Result;
174   }
175   while (!eofAll()) {
176     readLine(s);
177     if (!isEmpty(s))
178       return strcpy(Result, s);
179   }
180   return strcpy(Result, "");
181 }
182 
183 
skipBlanks(void)184 void skipBlanks(void)
185 {
186   while (*nextData == '\0') {
187     readData(nextData);
188     if (eofAll())
189       return;
190   }
191 }
192 
193 
eofAll(void)194 boolean eofAll(void)
195 {
196   boolean Result;
197 
198 /* p2c: multfile.pas: Note: Eliminated unused assignment statement [338] */
199   if (current == NULL)
200     return true;
201   if (eofCurrent()) {
202     popFile();
203     return (eofAll());
204   }
205   return false;
206 }
207 
208 
currentLineNo(void)209 short currentLineNo(void)
210 {
211   if (current == NULL)
212     return 0;
213   else
214     return (current->lineno);
215 }
216 
217 
currentFilename(Char * Result)218 Char *currentFilename(Char *Result)
219 {
220   if (current == NULL)
221     return strcpy(Result, "No file open yet");
222   else
223     return strcpy(Result, current->name);
224 }
225 
226 
fileError(void)227 boolean fileError(void)
228 {
229   return inputerror;
230 }
231 
232 
233 
234 
235 /* End. */
236