summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/alephdir/aleph.c
blob: 88bd9ca85fe273b903ee2cce105e8192429d16f5 (plain)
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
/* aleph.c: C routines to support the Aleph Pascal Web distribution
 * based on omega.c from the Omega project

This file is part of the Aleph project

Copyright (C) 1994--2001 John Plaice and Yannis Haralambous
Copyright (C) 2004 the Aleph team

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#define EXTERN extern
#include "alephd.h"

void
btestin(void)
{
    string fname =
    kpse_find_file (nameoffile + 1, kpse_program_binary_format, true);

    if (fname) {
      libcfree(nameoffile);
      nameoffile = xmalloc(2+strlen(fname));
      namelength = strlen(fname);
      strcpy(nameoffile+1, fname);
    }
    else {
      libcfree(nameoffile);
      nameoffile = xmalloc(2);
      namelength = 0;
      nameoffile[0] = 0;
      nameoffile[1] = 0;
    }
}

int
getfilemode (FILE *f, int def)
{
    int c,m;
    if ((def==0)||(feof(f))) m=0;
    else {
       c = getc(f);
       if (c==EOF) m=0;
       else if (c==0x5c) {
          if (feof(f)) m=0;
          else {
             c = getc(f);
             if (c==EOF) m=0;
             else if (c==0) m=4;
             else m=1;
             ungetc(c,f);
             c=0x5c;
          }
       }
       else if (c==0x25) {
          if (feof(f)) m=0;
          else {
             c = getc(f);
             if (c==EOF) m=0;
             else if (c==0) m=4;
             else m=1;
             ungetc(c,f);
             c=0x25;
          }
       }
       else if (c==0xe0) m=2;
       else if (c==0x6c) m=2;
       else if (c==0) {
          if (feof(f)) m=0;
          else {
             c = getc(f);
             if (c==EOF) m=0;
             else if (c==0x5c) m=3;
             else if (c==0x25) m=3;
             else m=def;
             ungetc(c,f);
             c=0;
          }
       }
       else m=def;
       ungetc(c,f);
    }
    return m;
}

int
getc_two_LE (FILE *f)
{
    register int i,j;
    i = getc(f);
    if (i==EOF) { return i; }
    j = getc(f);
    if (j==EOF) { return j; }
    return ((j<<8)|i);
}

void
ungetc_two_LE (int c, FILE *f)
{
    ungetc((c>>8), f);
    ungetc((c&0377), f);
}

int
getc_two (FILE *f)
{      
    register int i,j;
    i = getc(f);
    if (i==EOF) { return i; }
    j = getc(f);
    if (j==EOF) { return j; }
    return ((i<<8)|j);
}
 
void
ungetc_two (int c, FILE *f)
{
    ungetc((c&0377), f);
    ungetc((c>>8), f);
}
 
extern boolean zpnewinputln ();

boolean
newinputln (FILE *f, halfword themode, halfword translation, boolean bypass)
{
    return zpnewinputln((alphafile)f,
                        (halfword)themode,
                        (halfword)translation,
                        (boolean) bypass);
}
 
boolean
new_input_line (FILE *f, halfword themode)
{
  register int i=EOF;
 
  last = first;
  otpinputend = 0;
 
  if (themode==1) {
     while ((otpinputend < ocpbufsize) && ((i = getc (f)) != EOF) &&
            ((i != '\r') && (i != '\n')))
        otpinputbuf[++otpinputend] = i;
     if (i=='\r') {
         i=getc(f); if (i != '\n') ungetc(i,f);
     }
  } else if (themode==2) {
     while ((otpinputend < ocpbufsize) && ((i = getc (f)) != EOF) &&
            (i != 0x25))
        otpinputbuf[++otpinputend] = i;
  } else if (themode==3) {
     while ((otpinputend < ocpbufsize) && ((i = getc_two (f)) != EOF) &&
            ((i != '\r') && (i != '\n')))
        otpinputbuf[++otpinputend] = i;
     if (i=='\r') {
         i=getc_two(f); if (i != '\n') ungetc_two(i,f);
     }
  } else /* themode==4 */ {
     while ((otpinputend < ocpbufsize) && ((i = getc_two_LE (f)) != EOF) &&
            ((i != '\r') && (i != '\n')))
        otpinputbuf[++otpinputend] = i;
     if (i=='\r') {
         i=getc_two_LE(f); if (i != '\n') ungetc_two_LE(i,f);
     }
  }
 
  if (i == EOF && otpinputend == 0)
      return false;
 
  /* We didn't get the whole line because our buffer was too small.
*/
 if (i != EOF && (((themode!=2) && (i != '\n')) || ((themode==2) && (i != 0x25))))
    {
      (void) fprintf (stderr,
                     "! Unable to read an entire line---ocp_buf_size=%ld.\n",ocpbufsize);
      (void) fprintf (stderr, "Please increase ocp_buf_size in texmf.cnf.\n");
      uexit (1);
    }
 
    return true;
}

extern memoryword ziniteqtbentry () ;

hashword hashtable[HASHTABLESIZE];

void
inithhashtable (void)
{
   int i;
   for (i=0; i<HASHTABLESIZE; i++) {
      hashtable[i].p = -1;
   }
      
}

hashword *
createeqtbpos (int p)
{
   hashword *runner= &(hashtable[p%HASHTABLESIZE]);
   if (runner->p==p) return runner;
   while (runner->p != -1) {
      runner = runner->ptr; 
      if (runner->p == p) return runner;
   } 
   runner->p = p;
   runner->mw = ziniteqtbentry(p);
   runner->ptr = (hashword *) xmalloc(sizeof(hashword));
   (runner->ptr)->p = -1;
   return runner;
}

memoryword *
createeqtbptr (int p)
{
   hashword *runner= &(hashtable[p%HASHTABLESIZE]);
   if (runner->p==p) return (&(runner->mw));
   while (runner->p != -1) {
      runner = runner->ptr; 
      if (runner->p == p) return (&(runner->mw));
   } 
   runner->p = p;
   runner->mw = ziniteqtbentry(p);
   runner->ptr = (hashword *) xmalloc(sizeof(hashword));
   (runner->ptr)->p = -1;
   return (&(runner->mw));
}

hashword *
createxeqlevel (int p)
{
   hashword *runner= &(hashtable[p%HASHTABLESIZE]);
   if (runner->p==p) return runner;
   while (runner->p != -1) {
      runner = runner->ptr;
      if (runner->p == p) return runner;
   }
   runner->p = p;
   runner->mw.cint = 1;
   runner->mw.cint1 = 0;
   runner->ptr = (hashword *) xmalloc(sizeof(hashword));
   (runner->ptr)->p = -1;
   return runner;
}

hashword *
createhashpos (int p)
{
   hashword *runner= &(hashtable[p%HASHTABLESIZE]);
   if (runner->p==p) return runner;
   while (runner->p != -1) {
      runner = runner->ptr; 
      if (runner->p == p) return runner;
   } 
   runner->p = p;
   runner->mw.cint = 0;
   runner->mw.cint1 = 0;
   runner->ptr = (hashword *) xmalloc(sizeof(hashword));
   (runner->ptr)->p = -1;
   return runner;
}

void
dumphhashtable (void)
{
   int i;
   hashword *runner;
   for (i=0; i<HASHTABLESIZE; i++) {
      runner=&(hashtable[i]);
      if (runner->p != -1) {
         dumpint(-i);
         while (runner->p != -1) {
            dumpint(runner->p);
            dumpwd(runner->mw);
            runner=runner->ptr;
         }
      }
   }
   dumpint(-HASHTABLESIZE);
}

void
undumphhashtable (void)
{
   int i,j;
   hashword *runner;
   j=0;
   undumpint(i);
   while (j<HASHTABLESIZE) {
      i = (-i);
      while (j<i) {
         hashtable[j].p = -1;
         j++;
      }
      if (i<HASHTABLESIZE) {
         runner = &(hashtable[j]);
         undumpint(i);
         while (i>=0) {
            runner->p = i;
            undumpwd(runner->mw);
            undumpint(i);
            runner->ptr = (hashword *) xmalloc(sizeof(hashword));
            runner = runner->ptr;
         }
         runner->p = -1;
         j++;
      }
   }
}

void
odateandtime (int timecode, int daycode, int monthcode, int yearcode)
{
    integer tc,dc,mc,yc;
    dateandtime(tc,dc,mc,yc);
    setneweqtbint(timecode,tc);
    setneweqtbint(daycode,dc);
    setneweqtbint(monthcode,mc);
    setneweqtbint(yearcode,yc);
}

memoryword **fonttables;
static int font_entries = 0;

void
allocatefonttable (int font_number, int font_size)
{
    int i;
    if (font_entries==0) {
      fonttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
      font_entries=256;
    } else if ((font_number==256)&&(font_entries==256)) {
      fonttables = xrealloc(fonttables, 65536);
      font_entries=65536;
    }
    fonttables[font_number] =
       (memoryword *) xmalloc((font_size+1)*sizeof(memoryword));
    fonttables[font_number][0].cint = font_size;
    fonttables[font_number][0].cint1 = 0;
    for (i=1; i<=font_size; i++) {
        fonttables[font_number][i].cint  = 0;
        fonttables[font_number][i].cint1 = 0;
    }
}

void
dumpfonttable (int  font_number, int  words)
{
    fonttables[font_number][0].cint=words;
    dumpthings(fonttables[font_number][0], fonttables[font_number][0].cint+1);
}

void
undumpfonttable(int font_number)
{
    memoryword sizeword;
    if (font_entries==0) {
      fonttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
      font_entries=256;
    } else if ((font_number==256)&&(font_entries==256)) {
      fonttables = xrealloc(fonttables, 65536);
      font_entries=65536;
    }

    undumpthings(sizeword,1);
    fonttables[font_number] =
        (memoryword *) xmalloc((sizeword.cint+1)*sizeof(memoryword));
    fonttables[font_number][0].cint = sizeword.cint;
    undumpthings(fonttables[font_number][1], sizeword.cint);
}

memoryword **fontsorttables;
static int fontsort_entries = 0;

void
allocatefontsorttable (int  fontsort_number, int  fontsort_size)
{
    int i;
    if (fontsort_entries==0) {
      fontsorttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
      fontsort_entries=256;
    } else if ((fontsort_number==256)&&(fontsort_entries==256)) {
      fontsorttables = xrealloc(fontsorttables, 65536);
      fontsort_entries=65536;
    }
    fontsorttables[fontsort_number] =
       (memoryword *) xmalloc((fontsort_size+1)*sizeof(memoryword));
    fontsorttables[fontsort_number][0].cint = fontsort_size;
    fontsorttables[fontsort_number][0].cint1 = 0;
    for (i=1; i<=fontsort_size; i++) {
        fontsorttables[fontsort_number][i].cint  = 0;
        fontsorttables[fontsort_number][i].cint1 = 0;
    }
}

void
dumpfontsorttable (int fontsort_number, int words)
{
    fontsorttables[fontsort_number][0].cint=words;
    dumpthings(fontsorttables[fontsort_number][0],
               fontsorttables[fontsort_number][0].cint+1);
}

void
undumpfontsorttable(int fontsort_number)
{
    memoryword sizeword;
    if (fontsort_entries==0) {
      fontsorttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
      fontsort_entries=256;
    } else if ((fontsort_number==256)&&(fontsort_entries==256)) {
      fontsorttables = xrealloc(fontsorttables, 65536);
      fontsort_entries=65536;
    }

    undumpthings(sizeword,1);
    fontsorttables[fontsort_number] =
        (memoryword *) xmalloc((sizeword.cint+1)*sizeof(memoryword));
    fontsorttables[fontsort_number][0].cint = sizeword.cint;
    undumpthings(fontsorttables[fontsort_number][1], sizeword.cint);
}

int **ocptables;
static int ocp_entries = 0;

void
allocateocptable (int ocp_number, int ocp_size)
{
    int i;
    if (ocp_entries==0) {
      ocptables = (int **) xmalloc(256*sizeof(int**));
      ocp_entries=256;
    } else if ((ocp_number==256)&&(ocp_entries==256)) {
      ocptables = xrealloc(ocptables, 65536);
      ocp_entries=65536;
    }
    ocptables[ocp_number] =
       (int *) xmalloc((1+ocp_size)*sizeof(int));
    ocptables[ocp_number][0] = ocp_size;
    for (i=1; i<=ocp_size; i++) {
        ocptables[ocp_number][i]  = 0;
    }
}

void
dumpocptable (int ocp_number)
{
    dumpthings(ocptables[ocp_number][0], ocptables[ocp_number][0]+1);
}

void
undumpocptable (int ocp_number)
{
    int sizeword;
    if (ocp_entries==0) {
      ocptables = (int **) xmalloc(256*sizeof(int**));
      ocp_entries=256;
    } else if ((ocp_number==256)&&(ocp_entries==256)) {
      ocptables = xrealloc(ocptables, 65536);
      ocp_entries=65536;
    }
    undumpthings(sizeword,1);
    ocptables[ocp_number] =
        (int *) xmalloc((1+sizeword)*sizeof(int));
    ocptables[ocp_number][0] = sizeword;
    undumpthings(ocptables[ocp_number][1], sizeword);
}