1 /* aleph.c: C routines to support the Aleph Pascal Web distribution
2 * based on omega.c from the Omega project
3
4 This file is part of the Aleph project
5
6 Copyright (C) 1994--2001 John Plaice and Yannis Haralambous
7 Copyright (C) 2004 the Aleph team
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23 #define EXTERN extern
24 #include "alephd.h"
25
26 void
btestin(void)27 btestin(void)
28 {
29 string fname =
30 kpse_find_file (nameoffile + 1, kpse_program_binary_format, true);
31
32 if (fname) {
33 libcfree(nameoffile);
34 nameoffile = xmalloc(2+strlen(fname));
35 namelength = strlen(fname);
36 strcpy(nameoffile+1, fname);
37 }
38 else {
39 libcfree(nameoffile);
40 nameoffile = xmalloc(2);
41 namelength = 0;
42 nameoffile[0] = 0;
43 nameoffile[1] = 0;
44 }
45 }
46
47 int
getfilemode(FILE * f,int def)48 getfilemode (FILE *f, int def)
49 {
50 int c,m;
51 if ((def==0)||(feof(f))) m=0;
52 else {
53 c = getc(f);
54 if (c==EOF) m=0;
55 else if (c==0x5c) {
56 if (feof(f)) m=0;
57 else {
58 c = getc(f);
59 if (c==EOF) m=0;
60 else if (c==0) m=4;
61 else m=1;
62 ungetc(c,f);
63 c=0x5c;
64 }
65 }
66 else if (c==0x25) {
67 if (feof(f)) m=0;
68 else {
69 c = getc(f);
70 if (c==EOF) m=0;
71 else if (c==0) m=4;
72 else m=1;
73 ungetc(c,f);
74 c=0x25;
75 }
76 }
77 else if (c==0xe0) m=2;
78 else if (c==0x6c) m=2;
79 else if (c==0) {
80 if (feof(f)) m=0;
81 else {
82 c = getc(f);
83 if (c==EOF) m=0;
84 else if (c==0x5c) m=3;
85 else if (c==0x25) m=3;
86 else m=def;
87 ungetc(c,f);
88 c=0;
89 }
90 }
91 else m=def;
92 ungetc(c,f);
93 }
94 return m;
95 }
96
97 static int
getc_two_LE(FILE * f)98 getc_two_LE (FILE *f)
99 {
100 register int i,j;
101 i = getc(f);
102 if (i==EOF) { return i; }
103 j = getc(f);
104 if (j==EOF) { return j; }
105 return ((j<<8)|i);
106 }
107
108 static void
ungetc_two_LE(int c,FILE * f)109 ungetc_two_LE (int c, FILE *f)
110 {
111 ungetc((c>>8), f);
112 ungetc((c&0377), f);
113 }
114
115 int
getc_two(FILE * f)116 getc_two (FILE *f)
117 {
118 register int i,j;
119 i = getc(f);
120 if (i==EOF) { return i; }
121 j = getc(f);
122 if (j==EOF) { return j; }
123 return ((i<<8)|j);
124 }
125
126 void
ungetc_two(int c,FILE * f)127 ungetc_two (int c, FILE *f)
128 {
129 ungetc((c&0377), f);
130 ungetc((c>>8), f);
131 }
132
133 boolean
newinputln(FILE * f,halfword themode,halfword translation,boolean bypass)134 newinputln (FILE *f, halfword themode, halfword translation, boolean bypass)
135 {
136 return pnewinputln(f, themode, translation, bypass);
137 }
138
139 boolean
new_input_line(FILE * f,halfword themode)140 new_input_line (FILE *f, halfword themode)
141 {
142 register int i=EOF;
143
144 last = first;
145 otpinputend = 0;
146
147 if (themode==1) {
148 while ((otpinputend < ocpbufsize) && ((i = getc (f)) != EOF) &&
149 ((i != '\r') && (i != '\n')))
150 otpinputbuf[++otpinputend] = i;
151 if (i=='\r') {
152 i=getc(f); if (i != '\n') ungetc(i,f);
153 }
154 } else if (themode==2) {
155 while ((otpinputend < ocpbufsize) && ((i = getc (f)) != EOF) &&
156 (i != 0x25))
157 otpinputbuf[++otpinputend] = i;
158 } else if (themode==3) {
159 while ((otpinputend < ocpbufsize) && ((i = getc_two (f)) != EOF) &&
160 ((i != '\r') && (i != '\n')))
161 otpinputbuf[++otpinputend] = i;
162 if (i=='\r') {
163 i=getc_two(f); if (i != '\n') ungetc_two(i,f);
164 }
165 } else /* themode==4 */ {
166 while ((otpinputend < ocpbufsize) && ((i = getc_two_LE (f)) != EOF) &&
167 ((i != '\r') && (i != '\n')))
168 otpinputbuf[++otpinputend] = i;
169 if (i=='\r') {
170 i=getc_two_LE(f); if (i != '\n') ungetc_two_LE(i,f);
171 }
172 }
173
174 if (i == EOF && otpinputend == 0)
175 return false;
176
177 /* We didn't get the whole line because our buffer was too small.
178 */
179 if (i != EOF && (((themode!=2) && (i != '\n')) || ((themode==2) && (i != 0x25))))
180 {
181 (void) fprintf (stderr,
182 "! Unable to read an entire line---ocp_buf_size=%ld.\n",
183 (long) ocpbufsize);
184 (void) fprintf (stderr, "Please increase ocp_buf_size in texmf.cnf.\n");
185 uexit (1);
186 }
187
188 return true;
189 }
190
191 hashword hashtable[HASHTABLESIZE];
192
193 void
inithhashtable(void)194 inithhashtable (void)
195 {
196 int i;
197 for (i=0; i<HASHTABLESIZE; i++) {
198 hashtable[i].p = -1;
199 }
200
201 }
202
203 hashword *
createeqtbpos(int p)204 createeqtbpos (int p)
205 {
206 hashword *runner= &(hashtable[p%HASHTABLESIZE]);
207 if (runner->p==p) return runner;
208 while (runner->p != -1) {
209 runner = runner->ptr;
210 if (runner->p == p) return runner;
211 }
212 runner->p = p;
213 runner->mw = initeqtbentry(p);
214 runner->ptr = (hashword *) xmalloc(sizeof(hashword));
215 (runner->ptr)->p = -1;
216 return runner;
217 }
218
219 #if 0 /* unused */
220 memoryword *
221 createeqtbptr (int p)
222 {
223 hashword *runner= &(hashtable[p%HASHTABLESIZE]);
224 if (runner->p==p) return (&(runner->mw));
225 while (runner->p != -1) {
226 runner = runner->ptr;
227 if (runner->p == p) return (&(runner->mw));
228 }
229 runner->p = p;
230 runner->mw = ziniteqtbentry(p);
231 runner->ptr = (hashword *) xmalloc(sizeof(hashword));
232 (runner->ptr)->p = -1;
233 return (&(runner->mw));
234 }
235 #endif
236
237 hashword *
createxeqlevel(int p)238 createxeqlevel (int p)
239 {
240 hashword *runner= &(hashtable[p%HASHTABLESIZE]);
241 if (runner->p==p) return runner;
242 while (runner->p != -1) {
243 runner = runner->ptr;
244 if (runner->p == p) return runner;
245 }
246 runner->p = p;
247 runner->mw.cint = 1;
248 runner->mw.cint1 = 0;
249 runner->ptr = (hashword *) xmalloc(sizeof(hashword));
250 (runner->ptr)->p = -1;
251 return runner;
252 }
253
254 hashword *
createhashpos(int p)255 createhashpos (int p)
256 {
257 hashword *runner= &(hashtable[p%HASHTABLESIZE]);
258 if (runner->p==p) return runner;
259 while (runner->p != -1) {
260 runner = runner->ptr;
261 if (runner->p == p) return runner;
262 }
263 runner->p = p;
264 runner->mw.cint = 0;
265 runner->mw.cint1 = 0;
266 runner->ptr = (hashword *) xmalloc(sizeof(hashword));
267 (runner->ptr)->p = -1;
268 return runner;
269 }
270
271 void
dumphhashtable(void)272 dumphhashtable (void)
273 {
274 int i;
275 hashword *runner;
276 for (i=0; i<HASHTABLESIZE; i++) {
277 runner=&(hashtable[i]);
278 if (runner->p != -1) {
279 dumpint(-i);
280 while (runner->p != -1) {
281 dumpint(runner->p);
282 dumpwd(runner->mw);
283 runner=runner->ptr;
284 }
285 }
286 }
287 dumpint(-HASHTABLESIZE);
288 }
289
290 void
undumphhashtable(void)291 undumphhashtable (void)
292 {
293 int i,j;
294 hashword *runner;
295 j=0;
296 undumpint(i);
297 while (j<HASHTABLESIZE) {
298 i = (-i);
299 while (j<i) {
300 hashtable[j].p = -1;
301 j++;
302 }
303 if (i<HASHTABLESIZE) {
304 runner = &(hashtable[j]);
305 undumpint(i);
306 while (i>=0) {
307 runner->p = i;
308 undumpwd(runner->mw);
309 undumpint(i);
310 runner->ptr = (hashword *) xmalloc(sizeof(hashword));
311 runner = runner->ptr;
312 }
313 runner->p = -1;
314 j++;
315 }
316 }
317 }
318
319 void
odateandtime(int timecode,int daycode,int monthcode,int yearcode)320 odateandtime (int timecode, int daycode, int monthcode, int yearcode)
321 {
322 integer tc,dc,mc,yc;
323 dateandtime(tc,dc,mc,yc);
324 setneweqtbint(timecode,tc);
325 setneweqtbint(daycode,dc);
326 setneweqtbint(monthcode,mc);
327 setneweqtbint(yearcode,yc);
328 }
329
330 memoryword **fonttables;
331 static int font_entries = 0;
332
333 void
allocatefonttable(int font_number,int font_size)334 allocatefonttable (int font_number, int font_size)
335 {
336 int i;
337 if (font_entries==0) {
338 fonttables = xmalloc(256*sizeof(memoryword*));
339 font_entries=256;
340 } else if ((font_number==256)&&(font_entries==256)) {
341 fonttables = xrealloc(fonttables, 65536*sizeof(memoryword*));
342 font_entries=65536;
343 }
344 fonttables[font_number] = xmalloc((font_size+1)*sizeof(memoryword));
345 fonttables[font_number][0].cint = font_size;
346 fonttables[font_number][0].cint1 = 0;
347 for (i=1; i<=font_size; i++) {
348 fonttables[font_number][i].cint = 0;
349 fonttables[font_number][i].cint1 = 0;
350 }
351 }
352
353 void
dumpfonttable(int font_number,int words)354 dumpfonttable (int font_number, int words)
355 {
356 fonttables[font_number][0].cint=words;
357 dumpthings(fonttables[font_number][0], fonttables[font_number][0].cint+1);
358 }
359
360 void
undumpfonttable(int font_number)361 undumpfonttable(int font_number)
362 {
363 memoryword sizeword;
364 if (font_entries==0) {
365 fonttables = xmalloc(256*sizeof(memoryword*));
366 font_entries=256;
367 } else if ((font_number==256)&&(font_entries==256)) {
368 fonttables = xrealloc(fonttables, 65536*sizeof(memoryword*));
369 font_entries=65536;
370 }
371
372 undumpthings(sizeword,1);
373 fonttables[font_number] = xmalloc((sizeword.cint+1)*sizeof(memoryword));
374 fonttables[font_number][0].cint = sizeword.cint;
375 undumpthings(fonttables[font_number][1], sizeword.cint);
376 }
377
378 #if 0 /* unused */
379 memoryword **fontsorttables;
380 static int fontsort_entries = 0;
381
382 void
383 allocatefontsorttable (int fontsort_number, int fontsort_size)
384 {
385 int i;
386 if (fontsort_entries==0) {
387 fontsorttables = xmalloc(256*sizeof(memoryword*));
388 fontsort_entries=256;
389 } else if ((fontsort_number==256)&&(fontsort_entries==256)) {
390 fontsorttables = xrealloc(fontsorttables, 65536*sizeof(memoryword*));
391 fontsort_entries=65536;
392 }
393 fontsorttables[fontsort_number] = xmalloc((fontsort_size+1)*sizeof(memoryword));
394 fontsorttables[fontsort_number][0].cint = fontsort_size;
395 fontsorttables[fontsort_number][0].cint1 = 0;
396 for (i=1; i<=fontsort_size; i++) {
397 fontsorttables[fontsort_number][i].cint = 0;
398 fontsorttables[fontsort_number][i].cint1 = 0;
399 }
400 }
401
402 void
403 dumpfontsorttable (int fontsort_number, int words)
404 {
405 fontsorttables[fontsort_number][0].cint=words;
406 dumpthings(fontsorttables[fontsort_number][0],
407 fontsorttables[fontsort_number][0].cint+1);
408 }
409
410 void
411 undumpfontsorttable(int fontsort_number)
412 {
413 memoryword sizeword;
414 if (fontsort_entries==0) {
415 fontsorttables = xmalloc(256*sizeof(memoryword*));
416 fontsort_entries=256;
417 } else if ((fontsort_number==256)&&(fontsort_entries==256)) {
418 fontsorttables = xrealloc(fontsorttables, 65536*sizeof(memoryword*));
419 fontsort_entries=65536;
420 }
421
422 undumpthings(sizeword,1);
423 fontsorttables[fontsort_number] = xmalloc((sizeword.cint+1)*sizeof(memoryword));
424 fontsorttables[fontsort_number][0].cint = sizeword.cint;
425 undumpthings(fontsorttables[fontsort_number][1], sizeword.cint);
426 }
427 #endif
428
429 int **ocptables;
430 static int ocp_entries = 0;
431
432 void
allocateocptable(int ocp_number,int ocp_size)433 allocateocptable (int ocp_number, int ocp_size)
434 {
435 int i;
436 if (ocp_entries==0) {
437 ocptables = xmalloc(256*sizeof(int*));
438 ocp_entries=256;
439 } else if ((ocp_number==256)&&(ocp_entries==256)) {
440 ocptables = xrealloc(ocptables, 65536*sizeof(int*));
441 ocp_entries=65536;
442 }
443 ocptables[ocp_number] = xmalloc((1+ocp_size)*sizeof(int));
444 ocptables[ocp_number][0] = ocp_size;
445 for (i=1; i<=ocp_size; i++) {
446 ocptables[ocp_number][i] = 0;
447 }
448 }
449
450 void
dumpocptable(int ocp_number)451 dumpocptable (int ocp_number)
452 {
453 dumpthings(ocptables[ocp_number][0], ocptables[ocp_number][0]+1);
454 }
455
456 void
undumpocptable(int ocp_number)457 undumpocptable (int ocp_number)
458 {
459 int sizeword;
460 if (ocp_entries==0) {
461 ocptables = xmalloc(256*sizeof(int*));
462 ocp_entries=256;
463 } else if ((ocp_number==256)&&(ocp_entries==256)) {
464 ocptables = xrealloc(ocptables, 65536*sizeof(int*));
465 ocp_entries=65536;
466 }
467 undumpthings(sizeword,1);
468 ocptables[ocp_number] = xmalloc((1+sizeword)*sizeof(int));
469 ocptables[ocp_number][0] = sizeword;
470 undumpthings(ocptables[ocp_number][1], sizeword);
471 }
472