home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / nroffgraphics / part01 / otread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-06  |  2.8 KB  |  80 lines

  1. /*
  2.  * otread.c -- read a driver table in old nroff format into core
  3.  *
  4.  * Use this function to read an old-nroff-style driver table into the
  5.  * internal form given in the term.h header file. Note that the terminal
  6.  * name slot is *not* filled in.
  7.  *
  8.  * This function will return NULL if the input file is too short (i.e its
  9.  * length is less than sizeof(int) + sizeof(t_stor) + the byte count
  10.  * in the file's first int). It will also return NULL if it finds an offset
  11.  * larger than the string table size.
  12.  *
  13.  * Warning: the driver structure pointer handed back uses static storage,
  14.  * a second otread() call will overwrite it.
  15.  *
  16.  * Also, the code performs mildly unnatural acts on some structure fields.
  17.  * See the last comment before the end of the routine and beware. If the
  18.  * method fails it's more than likely to result in garbaged pointers in the
  19.  * returned internal-representation structure and core dumps later on.
  20.  *
  21.  * This code brought to you as a public service by Eric S. Raymond, Feb 1988
  22.  * and is copyrighted (c)1988 by the author. Use, distribute, and mangle
  23.  * freely, but don't try to make money selling it unless you're going to send
  24.  * me a cut. Send bug reports, love letters and death threats to eric@snark
  25.  * aka ...!rutgers!vu-vlsi!snark!eric.
  26.  */
  27. /*LINTLIBRARY*/
  28. #include <stdio.h>
  29. #include "termtab.h"
  30.  
  31. extern char *malloc();
  32.  
  33. nrtab_t *otread(tfp)
  34. FILE *tfp;
  35. {
  36.     int        c_size, *ip;
  37.     register char    **pp, *mptr;
  38.     static nrext_t    external;
  39.     static nrtab_t    internal;
  40.  
  41.     /* string table size */
  42.     if (fread((char *)&c_size, sizeof(int), 1, tfp) != 1)
  43.     return((nrtab_t *)NULL);
  44.  
  45.     /* fixed-size header part */
  46.     if (fread((char *)&external, sizeof(external), 1, tfp) != 1)
  47.     return((nrtab_t *)NULL);
  48.  
  49.     /* now read the strings table into core */
  50.     if (fread(mptr = malloc((unsigned)c_size), c_size, 1, tfp) != 1)
  51.     return((nrtab_t *)NULL);
  52.  
  53.     internal.bset = external.bset;
  54.     internal.breset = external.breset;
  55.     internal.Hor = external.Hor;
  56.     internal.Vert = external.Vert;
  57.     internal.Newline = external.Newline;
  58.     internal.Char = external.Char;
  59.     internal.Em = external.Em;
  60.     internal.Halfline = external.Halfline;
  61.     internal.Adj = external.Adj;
  62.  
  63.     /*
  64.      * Wooo-ah! Tricky-shit alert! We step through the fields of the external
  65.      * and internal representation structures as though they were arrays of
  66.      * (int) and (char *) respectively. C's structure-padding rules *seem*
  67.      * to almost guarantee this will work, but I wouldn't bet the farm on it
  68.      * given some of the weird broken architectures and compilers out there.
  69.      */
  70.     for (ip = &external.twinit, pp = &internal.twinit; pp < &internal.zzz; )
  71.     if (*ip > c_size)
  72.         return((nrtab_t *)NULL);
  73.     else
  74.         *pp++ = mptr + *ip++;
  75.  
  76.     return(&internal);
  77. }
  78.  
  79. /* otread.c ends here */
  80.