home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / nrtable / table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  7.1 KB  |  250 lines

  1. /*
  2.  *
  3.  *     TABLE - A PROGRAM TO PREPARE NROFF DRIVER TABLES
  4.  *     copyright (c) 1985 by Bruce Townsend and Bell-Northern Research.
  5.  *     Permission hereby granted to use, distribute, modify, or copy
  6.  *     except for profit, providing this disclaimer is included.
  7.  *
  8.  *     The contributions of Ian Darwin (the Makefile, cleaning up the
  9.  *     code, etc) are gratefully acknowledged.
  10.  *
  11.  *     Version 1.2 -
  12.  *        * The program has been substantially modified from version 1.1
  13.  *          The major bug in version 1.1 was that zero-width characters
  14.  *          were not handled properly. This has been fixed.
  15.  *
  16.  *     Please send bug reports to Bruce Townsend (utcs!bnr-vpa!bruce)
  17.  *
  18.  *     Usage:
  19.  *           1) Build a file tabXXX.c, where XXX is some reasonable
  20.  *              acronym for the terminal or printer you want to drive
  21.  *              (peruse existing names to see what's reasonable!).
  22.  *              Make this file by hacking up one of the existing
  23.  *              table entries.
  24.  *           2) If the amount of char data that the structure
  25.  *              references is very large, you may want to redefine
  26.  *              C_SIZE
  27.  *           3) check to see whether <sgtty.h> or <termio.h> should
  28.  *              be #included in the tabXXX.c file. One of these include
  29.  *              files may be necessary to provide settings for t.bset,
  30.  *              t.breset. This is instead of the previous tendency to code
  31.  *              octal magic numbers in these fields.
  32.  *           4) Enter your new table into the Makefile; just add the
  33.  *              name "tabXXX" to the TABFILES definition.
  34.  *           5) Type "make tabXXX" where tabXXX is the same name
  35.  *              you chose before.
  36.  *           6) Locate the tabfile in the proper place (on our system
  37.  *              the directory is /usr/lib/term
  38.  *
  39.  *
  40.  */
  41.  
  42. #define C_SIZE    10000    /* The maximum amount of character data allowed
  43.                in the initialized structure t - increase if
  44.                necessary */
  45.  
  46. #include <stdio.h>
  47. #include "term.h"    /* This file contains the definition of the
  48.                the structures t and t_stor */
  49.  
  50. /************* DO NOT ALTER ANYTHING AFTER THIS POINT ***************/
  51.  
  52. extern struct t t;
  53. struct t_stor t_stor;
  54.  
  55. char    c_data[C_SIZE];
  56. char    *c_pointer[256];
  57. int    c_length[256];
  58. char    *c_end = c_data;
  59. int    n_strings, c_size;
  60.  
  61. main (argc, argv)
  62. int    argc;
  63. char    *argv[];
  64. {
  65.     FILE    *table;
  66.     int    i, j, i_len, j_len;
  67.     char    *tail, *start, *char_pointer, *strcpy();
  68.  
  69.     if (argc != 2) {    /* Need a file name argument */
  70.         fprintf (stderr, "Usage: table \"file\"\n");
  71.         exit (1);
  72.         }
  73.  
  74.     /* Open the file */
  75.     if ((table = fopen (argv[1], "w")) == NULL) {
  76.         fprintf (stderr, "File %s not opened for writing\n", argv[1]);
  77.         exit (1);
  78.         }
  79.  
  80.     /* Copy the integer values from the initialized structure
  81.        to the storage structure */
  82.     t_stor.bset = t.bset;
  83.     t_stor.breset = t.breset;
  84.     t_stor.Hor = t.Hor;
  85.     t_stor.Vert = t.Vert;
  86.     t_stor.Newline = t.Newline;
  87.     t_stor.Char = t.Char;
  88.     t_stor.Em = t.Em;
  89.     t_stor.Halfline = t.Halfline;
  90.     t_stor.Adj = t.Adj;
  91.  
  92.     /* Add the character strings into a character array */
  93.     addstring (t.twinit);
  94.     addstring (t.twrest);
  95.     addstring (t.twnl);
  96.     addstring (t.hlr);
  97.     addstring (t.hlf);
  98.     addstring (t.flr);
  99.     addstring (t.bdon);
  100.     addstring (t.bdoff);
  101.     addstring (t.iton);
  102.     addstring (t.itoff);
  103.     addstring (t.ploton);
  104.     addstring (t.plotoff);
  105.     addstring (t.up);
  106.     addstring (t.down);
  107.     addstring (t.right);
  108.     addstring (t.left);
  109.     for (i = 0; i < 256 - 32; i++) addchar (t.codetab[i]);
  110.  
  111.     /* eliminate strings which are tails of other strings */
  112.     for (i = 0; i < n_strings; i++) {
  113.         if (! c_pointer[i]) continue;    /* String cleared out */
  114.         i_len = c_length[i];
  115.         for (j = 0; j < n_strings; j++) {
  116.         if (i == j || ! c_pointer[j]) continue;
  117.         j_len = c_length[j];
  118.         if (i_len <= j_len) {    /* string i could be tail of string j */
  119.             tail = c_pointer[j] + j_len - i_len;
  120.             if (! char_comp (c_pointer[i], tail, i_len)) {
  121.             c_pointer[i] = 0;
  122.             break;
  123.             }
  124.             }
  125.         }
  126.         }
  127.  
  128.     /* Compress the c_data array */
  129.     char_pointer = c_data;
  130.     for (i = j = 0; i < n_strings; i++) {
  131.         if (! (start = c_pointer[i])) continue;
  132.         c_pointer[j] = char_pointer;
  133.         c_length[j++] = c_length[i];
  134.         for (i_len = c_length[i]; i_len--;) *char_pointer++ = *start++;
  135.         *char_pointer++ = 0;
  136.         }
  137.     n_strings = j;
  138.     c_size = char_pointer - c_data;
  139.  
  140.     /* Now find each string in this table and provide an index to it */
  141.     t_stor.twinit = findstring (t.twinit);
  142.     t_stor.twrest = findstring (t.twrest);
  143.     t_stor.twnl = findstring (t.twnl);
  144.     t_stor.hlr = findstring (t.hlr);
  145.     t_stor.hlf = findstring (t.hlf);
  146.     t_stor.flr = findstring (t.flr);
  147.     t_stor.bdon = findstring (t.bdon);
  148.     t_stor.bdoff = findstring (t.bdoff);
  149.     t_stor.iton = findstring (t.iton);
  150.     t_stor.itoff = findstring (t.itoff);
  151.     t_stor.ploton = findstring (t.ploton);
  152.     t_stor.plotoff = findstring (t.plotoff);
  153.     t_stor.up = findstring (t.up);
  154.     t_stor.down = findstring (t.down);
  155.     t_stor.right = findstring (t.right);
  156.     t_stor.left = findstring (t.left);
  157.     for (i = 0; i < 256 - 32; i++) {
  158.         t_stor.codetab[i] = findchar (t.codetab[i]);
  159.         }
  160.     t_stor.zzz = 0;
  161.  
  162.     /* Write the character storage block size */
  163.     if (fwrite (&c_size, sizeof (c_size), 1, table) != 1)
  164.         write_err ();
  165.  
  166.     if (fwrite (&t_stor, sizeof (t_stor), 1, table) != 1)
  167.         write_err ();
  168.  
  169.     if (fwrite (c_data, sizeof (*c_data), c_size, table) != c_size)
  170.         write_err ();
  171.  
  172.     if (fclose (table)) {
  173.         fprintf (stderr, "File %s not closed properly\n", argv[1]);
  174.         exit (1);
  175.         }
  176.     }
  177.  
  178. addstring (string)
  179. char    *string;
  180. {
  181.     c_pointer[n_strings] = c_end;
  182.     c_end += (c_length[n_strings] = strlen (string)) + 1;
  183.     if (c_end >= c_data + C_SIZE) {
  184.         fprintf (stderr, "Table size too small, increase it!\n");
  185.         exit(1);
  186.         }
  187.     strcpy (c_pointer[n_strings++], string);
  188. }
  189.  
  190. addchar (string)
  191. char    *string;
  192. {
  193.     c_pointer[n_strings] = c_end;
  194.     c_end += (c_length[n_strings] = strlen (string + 2) + 2) + 1;
  195.     if (c_end >= c_data + C_SIZE) {
  196.         fprintf (stderr, "Table size too small, increase it!\n");
  197.         exit(1);
  198.         }
  199.     *c_pointer[n_strings] = *string++;    /* Copy in first two bytes */
  200.     *(c_pointer[n_strings]+1) = *string++;
  201.     strcpy (c_pointer[n_strings++] + 2, string); /* Copy the rest */
  202. }
  203.  
  204. char_comp (str1, str2, len)
  205. char    *str1, *str2;
  206. int    len;
  207. {
  208.     while (len--) {
  209.         if (*str1++ != *str2++) return (1);
  210.         }
  211.     return (0);
  212.     }
  213.  
  214. findstring (string)
  215. char    *string;
  216. {
  217.     int    c_len, s_len, i;
  218.  
  219.     for (i = 0; i < n_strings; i++) {
  220.         if ((c_len = c_length[i]) >= (s_len = strlen (string))) {
  221.         if (!char_comp (string, c_pointer[i] + c_len - s_len, s_len))
  222.             return (c_pointer[i] + c_len - s_len - c_data);
  223.         }
  224.         }
  225.     fprintf (stderr, "Serious bug! string not found in table\n");
  226.     exit(1);
  227.     /* NOTREACHED */
  228.     }
  229.  
  230. findchar (string)
  231. char    *string;
  232. {
  233.     int    c_len, s_len, i;
  234.  
  235.     for (i = 0; i < n_strings; i++) {
  236.         if ((c_len = c_length[i]) >= (s_len = strlen (string+2) + 2)) {
  237.         if (!char_comp (string, c_pointer[i] + c_len - s_len, s_len))
  238.             return (c_pointer[i] + c_len - s_len - c_data);
  239.         }
  240.         }
  241.     fprintf (stderr, "Serious bug! character not found in table\n");
  242.     exit(1);
  243.     /* NOTREACHED */
  244.     }
  245.  
  246. write_err () {
  247.     fprintf (stderr, "Write to file failed\n");
  248.     exit (1);
  249.     }
  250.