home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / nroff-driver / table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  4.9 KB  |  164 lines

  1. /*
  2.     table (version 2.0) - a utility for creating nroff driver tables.
  3.  
  4.     table:  a program to compile nroff terminal driver tables.
  5.         Written by Bruce Townsend Oct, 1987.
  6.  
  7.     Copyright (c) 1987 by Bruce Townsend and Bell-Northern Research.
  8.     Permission is granted to use and distribute, except for profit,
  9.     providing this copyright notice and the author's name is included.
  10.     No warranty of any kind is expressed or implied, and no liability of
  11.     any kind is assumed by either the author or Bell-Northern Research.
  12.  
  13.     The contributions of Ian Darwin are gratefully acknowledged.
  14. */
  15.  
  16. #define C_SIZE    20000    /* The maximum amount of character data allowed
  17.                in the initialized structure t - increase if
  18.                necessary */
  19.  
  20. #include <stdio.h>
  21. #include "table.h"    /* This file contains the definition of the
  22.                the structures t and t_stor */
  23.  
  24. /*    The compiled tab file contains three primary elements:
  25.     -  An integer (c_size) which gives the size in bytes of the character
  26.        data (the third element of the file).
  27.     -  The structure t_stor, which contains integer data and integer
  28.        indices into the character data that follows.
  29.     -  c_size bytes of character data.
  30.     Note that the file size in bytes is therefore:
  31.     c_size + sizeof (int) + sizeof (t_stor)
  32. */
  33.  
  34. extern struct t t;    /* Defined in the tab file source */
  35. struct t_stor t_stor;    /* This structure is stored in the compiled tab file */
  36.  
  37. char    c_data[C_SIZE];    /* The character data to be stored in the tab file */
  38. int    c_size;        /* The amount of character data in bytes */
  39.  
  40. main (argc, argv)
  41. int    argc;
  42. char    *argv[];
  43. {
  44.     FILE    *table;
  45.     int    i;
  46.  
  47.     if (argc != 2) {    /* Need a file name argument */
  48.         fprintf (stderr, "Usage: table tabfilename\n");
  49.         exit (1);
  50.     }
  51.  
  52.     if ((table = fopen (argv[1], "w")) == NULL) {    /* Open the file */
  53.         fprintf (stderr, "Could not open file %s for writing\n", argv[1]);
  54.         exit (1);
  55.     }
  56.  
  57.     /* Copy the integer values from the initialized structure t
  58.        to the storage structure t_stor */
  59.     t_stor.bset = t.bset;
  60.     t_stor.breset = t.breset;
  61.     t_stor.Hor = t.Hor;
  62.     t_stor.Vert = t.Vert;
  63.     t_stor.Newline = t.Newline;
  64.     t_stor.Char = t.Char;
  65. #ifdef KANJI
  66.     t_stor.Kchar = t.Kchar;
  67. #endif KANJI
  68.     t_stor.Em = t.Em;
  69.     t_stor.Halfline = t.Halfline;
  70.     t_stor.Adj = t.Adj;
  71.  
  72.     /* Find each string in the character data table c_data, or add it to
  73.        the table if it is not there, and provide an index to it which
  74.        is stored in t_stor */
  75.     t_stor.twinit = addstring (t.twinit);
  76.     t_stor.twrest = addstring (t.twrest);
  77.     t_stor.twnl = addstring (t.twnl);
  78.     t_stor.hlr = addstring (t.hlr);
  79.     t_stor.hlf = addstring (t.hlf);
  80.     t_stor.flr = addstring (t.flr);
  81.     t_stor.bdon = addstring (t.bdon);
  82.     t_stor.bdoff = addstring (t.bdoff);
  83.     t_stor.iton = addstring (t.iton);
  84.     t_stor.itoff = addstring (t.itoff);
  85.     t_stor.ploton = addstring (t.ploton);
  86.     t_stor.plotoff = addstring (t.plotoff);
  87.     t_stor.up = addstring (t.up);
  88.     t_stor.down = addstring (t.down);
  89.     t_stor.right = addstring (t.right);
  90.     t_stor.left = addstring (t.left);
  91.     for (i = 0; i < 256 - 32; i++)
  92.         t_stor.codetab[i] = addchar (t.codetab[i]);
  93.     t_stor.zzz = 0;        /* The null terminator */
  94.  
  95.     /* Write to the tab file the amount of character data in bytes,
  96.        the structure t_stor, and the character data */
  97.     if (fwrite (&c_size, sizeof (c_size), 1, table) != 1 ||
  98.         fwrite (&t_stor, sizeof (t_stor), 1, table) != 1 ||
  99.         fwrite (c_data, sizeof (*c_data), c_size, table) != c_size) {
  100.         fprintf (stderr, "Write to file failed\n");
  101.         exit (1);
  102.     }
  103.  
  104.     /* Close the tab file */
  105.     if (fclose (table)) {
  106.         fprintf (stderr, "File %s not closed properly\n", argv[1]);
  107.         exit (1);
  108.     }
  109. }
  110.  
  111. addstring (string)
  112. char    *string;
  113. {
  114.     if (string)    /* If pointer is non-zero add string to table */
  115.         return (string_search (string, strlen (string) + 1));
  116.  
  117.     else        /* If pointer is zero return 0 index */
  118.         return (0);
  119. }
  120.  
  121. addchar (string)
  122. char    *string;
  123. {
  124.     if (string)    /* If pointer is non-zero add string to table */
  125.             /* Note that the string is at least 2 chars long
  126.                and that the first 2 bytes may be null */
  127.         return (string_search (string, strlen (string + 2) + 3));
  128.  
  129.     else        /* If pointer is zero return 0 index */
  130.         return (0);
  131. }
  132.  
  133. /*    string_search searches through the character array c_data to
  134.     find the string.  If found, the routine returns an integer
  135.     index that locates the string in c_data.  If it is not
  136.     found, the string is added to c_data, and c_size, the amount
  137.     of data in c_data, is updated.
  138. */
  139. string_search (string, length)
  140. char    *string;    /* The string to find in or add to the table */
  141. int    length;        /* The string length including the null terminator */
  142. {
  143.     int    s_index, c_index, match;
  144.     char    *pointer;
  145.  
  146.     for (s_index = 0; s_index <= c_size - length; s_index++) {
  147.         pointer = string;
  148.         match = 1;
  149.         for (c_index = s_index; c_index < s_index + length;) {
  150.         if (*pointer++ != c_data[c_index++]) {
  151.             match = 0;
  152.             break;
  153.         }
  154.         }
  155.         if (match) return (s_index);
  156.     }
  157.  
  158.     s_index = c_size;
  159.     while (length--)
  160.         c_data[c_size++] = *string++;
  161.  
  162.     return (s_index); 
  163. }
  164.