Back to index

tetex-bin  3.0
omega.c
Go to the documentation of this file.
00001 /* omega.c: C routines to support the Omega Pascal Web distribution
00002 
00003 This file is part of Omega,
00004 which is based on the web2c distribution of TeX,
00005 
00006 Copyright (c) 1994--2001 John Plaice and Yannis Haralambous
00007 
00008 Omega is free software; you can redistribute it and/or modify
00009 it under the terms of the GNU General Public License as published by
00010 the Free Software Foundation; either version 2 of the License, or
00011 (at your option) any later version.
00012 
00013 Omega is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Omega; if not, write to the Free Software Foundation, Inc.,
00020 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00021 
00022 */
00023 
00024 #define EXTERN extern
00025 #include "omegad.h"
00026 
00027 void
00028 btestin(void)
00029 {
00030     string fname =
00031     kpse_find_file (nameoffile + 1, kpse_program_binary_format, true);
00032 
00033     if (fname) {
00034       libcfree(nameoffile);
00035       nameoffile = (char *)xmalloc(2+strlen(fname));
00036       namelength = strlen(fname);
00037       strcpy(nameoffile+1, fname);
00038     }
00039     else {
00040       libcfree(nameoffile);
00041       nameoffile = (char *)xmalloc(2);
00042       namelength = 0;
00043       nameoffile[0] = 0;
00044       nameoffile[1] = 0;
00045     }
00046 }
00047 
00048 int
00049 getfilemode P2C(FILE *, f, int, def)
00050 {
00051     int c,m;
00052     if ((def==0)||(feof(f))) m=0;
00053     else {
00054        c = getc(f);
00055        if (c==EOF) m=0;
00056        else if (c==0x5c) {
00057           if (feof(f)) m=0;
00058           else {
00059              c = getc(f);
00060              if (c==EOF) m=0;
00061              else if (c==0) m=4;
00062              else m=1;
00063              ungetc(c,f);
00064              c=0x5c;
00065           }
00066        }
00067        else if (c==0x25) {
00068           if (feof(f)) m=0;
00069           else {
00070              c = getc(f);
00071              if (c==EOF) m=0;
00072              else if (c==0) m=4;
00073              else m=1;
00074              ungetc(c,f);
00075              c=0x25;
00076           }
00077        }
00078        else if (c==0xe0) m=2;
00079        else if (c==0x6c) m=2;
00080        else if (c==0) {
00081           if (feof(f)) m=0;
00082           else {
00083              c = getc(f);
00084              if (c==EOF) m=0;
00085              else if (c==0x5c) m=3;
00086              else if (c==0x25) m=3;
00087              else m=def;
00088              ungetc(c,f);
00089              c=0;
00090           }
00091        }
00092        else m=def;
00093        ungetc(c,f);
00094     }
00095     return m;
00096 }
00097 
00098 int
00099 getc_two_LE P1C(FILE *, f)
00100 {
00101     register int i,j;
00102     i = getc(f);
00103     if (i==EOF) { return i; }
00104     j = getc(f);
00105     if (j==EOF) { return j; }
00106     return ((j<<8)|i);
00107 }
00108 
00109 void
00110 ungetc_two_LE P2C(int, c, FILE *, f)
00111 {
00112     ungetc((c>>8), f);
00113     ungetc((c&0377), f);
00114 }
00115 
00116 int
00117 getc_two P1C(FILE *, f)
00118 {      
00119     register int i,j;
00120     i = getc(f);
00121     if (i==EOF) { return i; }
00122     j = getc(f);
00123     if (j==EOF) { return j; }
00124     return ((i<<8)|j);
00125 }
00126  
00127 void
00128 ungetc_two P2C(int, c, FILE *, f)
00129 {
00130     ungetc((c&0377), f);
00131     ungetc((c>>8), f);
00132 }
00133 
00134 #define advance_cin if ((c_in = fgetc(f)) == EOF) return EOF
00135 
00136 int
00137 getc_UTF8 P1C(FILE *, f)
00138 {
00139     register int c_in,c_out;
00140 
00141     advance_cin;
00142     if (c_in<0x80) {
00143         c_out = c_in & 0x7f;
00144     } else if (c_in<0xe0) {
00145         c_out = (c_in & 0x1f) << 6;
00146         advance_cin;
00147         c_out |= c_in & 0x3f;
00148     } else if (c_in<=0xf0) {
00149         c_out = (c_in & 0xf) << 12;
00150         advance_cin;
00151         c_out |= (c_in & 0x3f) << 6;
00152         advance_cin;
00153         c_out |= c_in & 0x3f;
00154     } else if (c_in<0xf8) {
00155         c_out = (c_in & 0x7) << 18;
00156         advance_cin;
00157         c_out |= (c_in & 0x3f) << 12;
00158         advance_cin;
00159         c_out |= (c_in & 0x3f) << 6;
00160         advance_cin;
00161         c_out |= c_in & 0x3f;
00162     } else if (c_in<0xfc) {
00163         c_out = (c_in & 0x3) << 24;
00164         advance_cin;
00165         c_out |= (c_in & 0x3f) << 18;
00166         advance_cin;
00167         c_out |= (c_in & 0x3f) << 12;
00168         advance_cin;
00169         c_out |= (c_in & 0x3f) << 6;
00170         advance_cin;
00171         c_out |= c_in & 0x3f;
00172     } else { /* c>=0xfc */
00173         c_out = (c_in & 0x1)   << 30;
00174         advance_cin;
00175         c_out |= (c_in & 0x3f) << 24;
00176         advance_cin;
00177         c_out |= (c_in & 0x3f) << 18;
00178         advance_cin;
00179         c_out |= (c_in & 0x3f) << 12;
00180         advance_cin;
00181         c_out |= (c_in & 0x3f) << 6;
00182         advance_cin;
00183         c_out |= c_in & 0x3f;
00184     }
00185     return c_out;
00186 }
00187 
00188 void
00189 ungetc_UTF8 P2C(int, c, FILE *, f)
00190 {
00191     /* Still to be done */
00192 }
00193 
00194 #define advance_two_cin advance_cin; \
00195     c_in_hi = c_in; \
00196     advance_cin; \
00197     c_in = (c_in_hi & 0xff) << 8
00198 
00199 int
00200 getc_UTF16 P1C(FILE *, f)
00201 {
00202     register int c_in,c_in_hi,c_out;
00203 
00204     advance_two_cin;
00205     if ((c_in>=0xd800) && (c_in<=0xdbff)) {
00206         c_out = (c_in - 0xd800) * 0x400;
00207         advance_two_cin;
00208         if ((c_in>=0xdc00) && (c_in<=0xdfff)) {
00209            c_out += (c_in - 0xdc00) + 0x10000;
00210         } else {
00211            return 0xfffd;
00212         }
00213     } else {
00214         c_out = c_in;
00215     }
00216     return c_out;
00217 }
00218 
00219 void
00220 ungetc_UTF16 P2C(int, c, FILE *, f)
00221 {
00222     /* Still to be done */
00223 }
00224 
00225 #define advance_twoLE_cin advance_cin; \
00226     c_in_lo = c_in; \
00227     advance_cin; \
00228     c_in = (c_in << 8) | (c_in_lo & 0xff)
00229 
00230 int
00231 getc_UTF16LE P1C(FILE *, f)
00232 {
00233     register int c_in,c_in_lo,c_out;
00234 
00235     advance_twoLE_cin;
00236     if ((c_in>=0xd800) && (c_in<=0xdbff)) {
00237         c_out = (c_in - 0xd800) * 0x400;
00238         advance_twoLE_cin;
00239         if ((c_in>=0xdc00) && (c_in<=0xdfff)) {
00240            c_out += (c_in - 0xdc00) + 0x10000;
00241         } else {
00242            return 0xfffd;
00243         }
00244     } else {
00245         c_out = c_in;
00246     }
00247     return c_out;
00248 }
00249 
00250 void
00251 ungetc_UTF16LE P2C(int, c, FILE *, f)
00252 {
00253     /* Still to be done */
00254 }
00255 
00256  
00257 extern boolean zpnewinputln ();
00258 
00259 boolean
00260 newinputln P4C(FILE *,f, halfword,themode, halfword,translation, boolean,bypass)
00261 {
00262     return zpnewinputln((alphafile)f,
00263                         (halfword)themode,
00264                         (halfword)translation,
00265                         (boolean) bypass);
00266 }
00267  
00268 boolean
00269 new_input_line P2C(FILE *, f, halfword, themode)
00270 {
00271   register int i=EOF;
00272  
00273   last = first;
00274   otpinputend = 0;
00275  
00276   if (themode==1) {
00277      while ((otpinputend < ocpbufsize) && ((i = getc (f)) != EOF) &&
00278             ((i != '\r') && (i != '\n')))
00279         otpinputbuf[++otpinputend] = i;
00280      if (i=='\r') {
00281          i=getc(f); if (i != '\n') ungetc(i,f);
00282      }
00283   } else if (themode==2) {
00284      while ((otpinputend < ocpbufsize) && ((i = getc (f)) != EOF) &&
00285             (i != 0x25))
00286         otpinputbuf[++otpinputend] = i;
00287   } else if (themode==3) {
00288      while ((otpinputend < ocpbufsize) && ((i = getc_two (f)) != EOF) &&
00289             ((i != '\r') && (i != '\n')))
00290         otpinputbuf[++otpinputend] = i;
00291      if (i=='\r') {
00292          i=getc_two(f); if (i != '\n') ungetc_two(i,f);
00293      }
00294   } else if (themode==4) {
00295      while ((otpinputend < ocpbufsize) && ((i = getc_two_LE (f)) != EOF) &&
00296             ((i != '\r') && (i != '\n')))
00297         otpinputbuf[++otpinputend] = i;
00298      if (i=='\r') {
00299          i=getc_two_LE(f); if (i != '\n') ungetc_two_LE(i,f);
00300      }
00301   } else if (themode==5) {
00302      while ((otpinputend < ocpbufsize) && ((i = getc_UTF8 (f)) != EOF) &&
00303             ((i != '\r') && (i != '\n')))
00304         otpinputbuf[++otpinputend] = i;
00305      if (i=='\r') {
00306          i=getc_UTF8(f); if (i != '\n') ungetc_UTF8(i,f);
00307      }
00308   } else if (themode==6) {
00309      while ((otpinputend < ocpbufsize) && ((i = getc_UTF16 (f)) != EOF) &&
00310             ((i != '\r') && (i != '\n')))
00311         otpinputbuf[++otpinputend] = i;
00312      if (i=='\r') {
00313          i=getc_UTF16(f); if (i != '\n') ungetc_UTF16(i,f);
00314      }
00315   } else /* themode==7 */ {
00316      while ((otpinputend < ocpbufsize) && ((i = getc_UTF16LE (f)) != EOF) &&
00317             ((i != '\r') && (i != '\n')))
00318         otpinputbuf[++otpinputend] = i;
00319      if (i=='\r') {
00320          i=getc_UTF16LE(f); if (i != '\n') ungetc_UTF16LE(i,f);
00321      }
00322   }
00323  
00324   if (i == EOF && otpinputend == 0)
00325       return false;
00326  
00327   /* We didn't get the whole line because our buffer was too small.
00328 */
00329  if (i != EOF && (((themode!=2) && (i != '\n')) || ((themode==2) && (i != 0x25))))
00330     {
00331       (void) fprintf (stderr,
00332                      "! Unable to read an entire line---ocp_buf_size=%ld.\n",ocpbufsize);
00333       (void) fprintf (stderr, "Please increase ocp_buf_size in texmf.cnf.\n");
00334       uexit (1);
00335     }
00336  
00337     return true;
00338 }
00339 
00340 extern memoryword ziniteqtbentry () ;
00341 
00342 hashword hashtable[HASHTABLESIZE];
00343 
00344 void
00345 inithhashtable P1H(void)
00346 {
00347    int i;
00348    for (i=0; i<HASHTABLESIZE; i++) {
00349       hashtable[i].p = -1;
00350    }
00351       
00352 }
00353 
00354 hashword *
00355 createeqtbpos P1C(int, p)
00356 {
00357    hashword *runner= &(hashtable[p%HASHTABLESIZE]);
00358    if (runner->p==p) return runner;
00359    while (runner->p != -1) {
00360       runner = runner->ptr; 
00361       if (runner->p == p) return runner;
00362    } 
00363    runner->p = p;
00364    runner->mw = ziniteqtbentry(p);
00365    runner->ptr = (hashword *) xmalloc(sizeof(hashword));
00366    (runner->ptr)->p = -1;
00367    return runner;
00368 }
00369 
00370 memoryword *
00371 createeqtbptr P1C(int, p)
00372 {
00373    hashword *runner= &(hashtable[p%HASHTABLESIZE]);
00374    if (runner->p==p) return (&(runner->mw));
00375    while (runner->p != -1) {
00376       runner = runner->ptr; 
00377       if (runner->p == p) return (&(runner->mw));
00378    } 
00379    runner->p = p;
00380    runner->mw = ziniteqtbentry(p);
00381    runner->ptr = (hashword *) xmalloc(sizeof(hashword));
00382    (runner->ptr)->p = -1;
00383    return (&(runner->mw));
00384 }
00385 
00386 hashword *
00387 createxeqlevel P1C(int, p)
00388 {
00389    hashword *runner= &(hashtable[p%HASHTABLESIZE]);
00390    if (runner->p==p) return runner;
00391    while (runner->p != -1) {
00392       runner = runner->ptr;
00393       if (runner->p == p) return runner;
00394    }
00395    runner->p = p;
00396    runner->mw.cint = 1;
00397    runner->mw.cint1 = 0;
00398    runner->ptr = (hashword *) xmalloc(sizeof(hashword));
00399    (runner->ptr)->p = -1;
00400    return runner;
00401 }
00402 
00403 hashword *
00404 createhashpos P1C(int, p)
00405 {
00406    hashword *runner= &(hashtable[p%HASHTABLESIZE]);
00407    if (runner->p==p) return runner;
00408    while (runner->p != -1) {
00409       runner = runner->ptr; 
00410       if (runner->p == p) return runner;
00411    } 
00412    runner->p = p;
00413    runner->mw.cint = 0;
00414    runner->mw.cint1 = 0;
00415    runner->ptr = (hashword *) xmalloc(sizeof(hashword));
00416    (runner->ptr)->p = -1;
00417    return runner;
00418 }
00419 
00420 void
00421 dumphhashtable P1H(void)
00422 {
00423    int i;
00424    hashword *runner;
00425    for (i=0; i<HASHTABLESIZE; i++) {
00426       runner=&(hashtable[i]);
00427       if (runner->p != -1) {
00428          dumpint(-i);
00429          while (runner->p != -1) {
00430             dumpint(runner->p);
00431             dumpwd(runner->mw);
00432             runner=runner->ptr;
00433          }
00434       }
00435    }
00436    dumpint(-HASHTABLESIZE);
00437 }
00438 
00439 void
00440 undumphhashtable P1H(void)
00441 {
00442    int i,j;
00443    hashword *runner;
00444    j=0;
00445    undumpint(i);
00446    while (j<HASHTABLESIZE) {
00447       i = (-i);
00448       while (j<i) {
00449          hashtable[j].p = -1;
00450          j++;
00451       }
00452       if (i<HASHTABLESIZE) {
00453          runner = &(hashtable[j]);
00454          undumpint(i);
00455          while (i>=0) {
00456             runner->p = i;
00457             undumpwd(runner->mw);
00458             undumpint(i);
00459             runner->ptr = (hashword *) xmalloc(sizeof(hashword));
00460             runner = runner->ptr;
00461          }
00462          runner->p = -1;
00463          j++;
00464       }
00465    }
00466 }
00467 
00468 void
00469 odateandtime P4C(int,timecode, int,daycode, int,monthcode, int,yearcode)
00470 {
00471     integer tc,dc,mc,yc;
00472     dateandtime(tc,dc,mc,yc);
00473     setneweqtbint(timecode,tc);
00474     setneweqtbint(daycode,dc);
00475     setneweqtbint(monthcode,mc);
00476     setneweqtbint(yearcode,yc);
00477 }
00478 
00479 memoryword **fonttables;
00480 static int font_entries = 0;
00481 
00482 void
00483 allocatefonttable P2C(int, font_number, int, font_size)
00484 {
00485     int i;
00486     if (font_entries==0) {
00487       fonttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
00488       font_entries=256;
00489     } else if ((font_number==256)&&(font_entries==256)) {
00490       fonttables = (memoryword **) xrealloc(fonttables, 65536);
00491       font_entries=65536;
00492     }
00493     fonttables[font_number] =
00494        (memoryword *) xmalloc((font_size+1)*sizeof(memoryword));
00495     fonttables[font_number][0].cint = font_size;
00496     fonttables[font_number][0].cint1 = 0;
00497     for (i=1; i<=font_size; i++) {
00498         fonttables[font_number][i].cint  = 0;
00499         fonttables[font_number][i].cint1 = 0;
00500     }
00501 }
00502 
00503 void
00504 dumpfonttable P2C(int, font_number, int, words)
00505 {
00506     fonttables[font_number][0].cint=words;
00507     dumpthings(fonttables[font_number][0], fonttables[font_number][0].cint+1);
00508 }
00509 
00510 void
00511 undumpfonttable P1C(int, font_number)
00512 {
00513     memoryword sizeword;
00514     if (font_entries==0) {
00515       fonttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
00516       font_entries=256;
00517     } else if ((font_number==256)&&(font_entries==256)) {
00518       fonttables = (memoryword **) xrealloc(fonttables, 65536);
00519       font_entries=65536;
00520     }
00521 
00522     undumpthings(sizeword,1);
00523     fonttables[font_number] =
00524         (memoryword *) xmalloc((sizeword.cint+1)*sizeof(memoryword));
00525     fonttables[font_number][0].cint = sizeword.cint;
00526     undumpthings(fonttables[font_number][1], sizeword.cint);
00527 }
00528 
00529 memoryword **fontsorttables;
00530 static int fontsort_entries = 0;
00531 
00532 void
00533 allocatefontsorttable P2C(int, fontsort_number, int, fontsort_size)
00534 {
00535     int i;
00536     if (fontsort_entries==0) {
00537       fontsorttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
00538       fontsort_entries=256;
00539     } else if ((fontsort_number==256)&&(fontsort_entries==256)) {
00540       fontsorttables = (memoryword **) xrealloc(fontsorttables, 65536);
00541       fontsort_entries=65536;
00542     }
00543     fontsorttables[fontsort_number] =
00544        (memoryword *) xmalloc((fontsort_size+1)*sizeof(memoryword));
00545     fontsorttables[fontsort_number][0].cint = fontsort_size;
00546     fontsorttables[fontsort_number][0].cint1 = 0;
00547     for (i=1; i<=fontsort_size; i++) {
00548         fontsorttables[fontsort_number][i].cint  = 0;
00549         fontsorttables[fontsort_number][i].cint1 = 0;
00550     }
00551 }
00552 
00553 void
00554 dumpfontsorttable P2C(int, fontsort_number, int, words)
00555 {
00556     fontsorttables[fontsort_number][0].cint=words;
00557     dumpthings(fontsorttables[fontsort_number][0],
00558                fontsorttables[fontsort_number][0].cint+1);
00559 }
00560 
00561 void
00562 undumpfontsorttable P1C(int, fontsort_number)
00563 {
00564     memoryword sizeword;
00565     if (fontsort_entries==0) {
00566       fontsorttables = (memoryword **) xmalloc(256*sizeof(memoryword**));
00567       fontsort_entries=256;
00568     } else if ((fontsort_number==256)&&(fontsort_entries==256)) {
00569       fontsorttables = (memoryword **) xrealloc(fontsorttables, 65536);
00570       fontsort_entries=65536;
00571     }
00572 
00573     undumpthings(sizeword,1);
00574     fontsorttables[fontsort_number] =
00575         (memoryword *) xmalloc((sizeword.cint+1)*sizeof(memoryword));
00576     fontsorttables[fontsort_number][0].cint = sizeword.cint;
00577     undumpthings(fontsorttables[fontsort_number][1], sizeword.cint);
00578 }
00579 
00580 int **ocptables;
00581 static int ocp_entries = 0;
00582 
00583 void
00584 allocateocptable P2C(int, ocp_number, int, ocp_size)
00585 {
00586     int i;
00587     if (ocp_entries==0) {
00588       ocptables = (int **) xmalloc(256*sizeof(int**));
00589       ocp_entries=256;
00590     } else if ((ocp_number==256)&&(ocp_entries==256)) {
00591       ocptables = (int **) xrealloc(ocptables, 65536);
00592       ocp_entries=65536;
00593     }
00594     ocptables[ocp_number] =
00595        (int *) xmalloc((1+ocp_size)*sizeof(int));
00596     ocptables[ocp_number][0] = ocp_size;
00597     for (i=1; i<=ocp_size; i++) {
00598         ocptables[ocp_number][i]  = 0;
00599     }
00600 }
00601 
00602 void
00603 dumpocptable P1C(int, ocp_number)
00604 {
00605     dumpthings(ocptables[ocp_number][0], ocptables[ocp_number][0]+1);
00606 }
00607 
00608 void
00609 undumpocptable P1C(int, ocp_number)
00610 {
00611     int sizeword;
00612     if (ocp_entries==0) {
00613       ocptables = (int **) xmalloc(256*sizeof(int**));
00614       ocp_entries=256;
00615     } else if ((ocp_number==256)&&(ocp_entries==256)) {
00616       ocptables = (int **) xrealloc(ocptables, 65536);
00617       ocp_entries=65536;
00618     }
00619     undumpthings(sizeword,1);
00620     ocptables[ocp_number] =
00621         (int *) xmalloc((1+sizeword)*sizeof(int));
00622     ocptables[ocp_number][0] = sizeword;
00623     undumpthings(ocptables[ocp_number][1], sizeword);
00624 }