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

  1. /*
  2.     table (version 2.0) - a utility for creating nroff driver tables.
  3.  
  4.     elbat:  a program to de-compile nroff terminal driver tables.
  5.         Written by Bruce Townsend Oct, 1987.
  6.         Based on a program by:
  7.         Matt Crawford, University of Chicago, 10 May 1984
  8.  
  9.     Copyright (c) 1987 by Bruce Townsend and Bell-Northern Research.
  10.     Permission is granted to use and distribute, except for profit,
  11.     providing this copyright notice and the author's name is included.
  12.     No warranty of any kind is expressed or implied, and no liability of
  13.     any kind is assumed by either the author or Bell-Northern Research.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include "table.h"
  22.  
  23. extern char    *structure;
  24.  
  25. char    *codelabel[] = {
  26.     "space",    "!",    "\"",    "#",    "$",    "%",    "&",
  27.     "' close",    "(",    ")",    "*",    "+",    ",",    "- hyphen",
  28.     ".",    "/",    "0",    "1",    "2",    "3",    "4",    "5",
  29.     "6",    "7",    "8",    "9",    ":",    ";",    "<",    "=",
  30.     ">",    "?",    "@",    "A",    "B",    "C",    "D",    "E",
  31.     "F",    "G",    "H",    "I",    "J",    "K",    "L",    "M",
  32.     "N",    "O",    "P",    "Q",    "R",    "S",    "T",    "U",
  33.     "V",    "W",    "X",    "Y",    "Z",    "[",    "\\",    "]",
  34.     "^",    "_ dash",    "` open",    "a",    "b",    "c",
  35.     "d",    "e",    "f",    "g",    "h",    "i",    "j",    "k",
  36.     "l",    "m",    "n",    "o",    "p",    "q",    "r",    "s",
  37.     "t",    "u",    "v",    "w",    "x",    "y",    "z",    "{",
  38.     "|",    "}",    "~",    "narrow sp",    "hyphen",    "bullet",
  39.     "square",    "3/4 em",    "rule",    "1/4",    "1/2",    "3/4",
  40.     "minus",    "fi",    "fl",    "ff",    "ffi",    "ffl",    "degree",
  41.     "dagger",    "section",    "foot mark",    "acute accent",
  42.     "grave accent",    "underrule",    "slash (longer)",
  43.     "half narrow space",    "unpaddable space",    "alpha",
  44.     "beta",    "gamma",    "delta",    "epsilon",    "zeta",
  45.     "eta",    "theta",    "iota",    "kappa",    "lambda",
  46.     "mu",    "nu",    "xi",    "omicron",    "pi",    "rho",    "sigma",
  47.     "tau",    "upsilon",    "phi",    "chi",    "psi",    "omega",
  48.     "Gamma",    "Delta",    "Theta",    "Lambda",
  49.     "Xi",    "Pi",    "Sigma",    "Tau",    "Upsilon",    "Phi",
  50.     "Psi",    "Omega",    "square root",    "terminal sigma",
  51.     "root en",    ">=",    "<=",    "identically equal",
  52.     "equation minus",    "approx =",    "approximates",
  53.     "not equal",    "right arrow",    "left arrow",    "up arrow",
  54.     "down arrow",    "eqn equals",    "multiply",    "divide",
  55.     "plus-minus",    "cup (union)",    "cap (intersection)",    "subset of",
  56.     "superset of",    "improper subset",    " improper superset",
  57.     "infinity",    "pt deriv",    "gradient",    "not",    "integral",
  58.     "proportional to",    "empty set",    "member of",
  59.     "equation plus",    "registration mk",    "copyright mk",
  60.     "box rule",    "cent sign",    "dbl dagger",    "right hand",
  61.     "left hand",    "math * ",    "bell system sign",
  62.     "or (was star)",    "circle",    "left top of big curly",
  63.     "left bottom of big curly",    "right top of big curly",
  64.     "right bottom of big curly",    "left center of big curly",
  65.     "right center of big curly",    "bold vertical rule",
  66.     "left bottom of big bracket",    "right bottom of big bracket",
  67.     "left top of big bracket",    "right top of big bracket",
  68.     "???",    "???",    "???",    "???",    "???",    "???",    "???",    "???",
  69.     "???",    "???",    "???",    "???",    "???",    /* The last 13 are unused */
  70. };
  71.  
  72. struct t t;
  73. struct t_stor t_stor;
  74. char    *mptr;        /* Pointer to the character data */
  75. int    c_size;        /* The amount of character data in bytes */
  76. int    debug;
  77.  
  78. #define intshow(memb)    printf ("/*%-8s*/\t\t%d,\n", "memb", t.memb)
  79. #define show(memb)    printf ("/*%-8s*/\t\t\"", "memb");\
  80.             vprint (t.memb);\
  81.             if (debug) printf("\",\t\t/*\t%d\t*/\n", t_stor.memb);\
  82.             else printf ("\",\n")
  83. #define getptr(memb)    t.memb = mptr + t_stor.memb
  84.  
  85. main(argc, argv)
  86. char    **argv;
  87. {
  88.     int    c, i;
  89.     char    labelbuf[64];
  90.  
  91.     if (argc == 3 && argv[1][0] == '-' && argv[1][1] == 'd') debug = 1;
  92.     if (argc != 2 && !debug) {
  93.         fprintf (stderr, "Usage: elbat [-d] tabfile > tabfile.c\n");
  94.         exit(1);
  95.     }
  96.  
  97.     loadtab(argv[argc - 1]);
  98.     
  99.     printf ("%s} t =\t{\n", structure);
  100.     printf ("/*bset    */\t\t0%o,\n", t.bset);
  101.     printf ("/*breset  */\t\t0%o,\n", t.breset);
  102.     intshow (Hor);
  103.     intshow (Vert);
  104.     intshow (Newline);
  105.     intshow (Char);
  106. #ifdef KANJI
  107.     intshow (Kchar);
  108. #endif KANJI
  109.     intshow (Em);
  110.     intshow (Halfline);
  111.     intshow (Adj);
  112.     if (debug) printf ("/* name\t\t\tstring\t\t\tindex into c_data */\n");
  113.     show (twinit);
  114.     show (twrest);
  115.     show (twnl);
  116.     show (hlr);
  117.     show (hlf);
  118.     show (flr);
  119.     show (bdon);
  120.     show (bdoff);
  121.     show (iton);
  122.     show (itoff);
  123.     show (ploton);
  124.     show (plotoff);
  125.     show (up);
  126.     show (down);
  127.     show (right);
  128.     show (left);
  129.  
  130.     for (c = 0; c < 256 - 32 - 13; c++) {
  131.         sprintf (labelbuf, "/* %s */", codelabel[c]);
  132.         printf ("%-20s\t\"", labelbuf);
  133.         if (t.codetab[c][0])
  134.         vprint (t.codetab[c]);
  135.         else if (t.codetab[c][1]) {
  136.         printf ("\\000");
  137.         vprint (t.codetab[c]+1);
  138.         }
  139.         else
  140.         printf ("\\000\\0");
  141.  
  142.         if (debug) printf("\",\t\t/*\t%d\t*/\n", t_stor.codetab[c]);
  143.         else printf ("\",\n");
  144.     }
  145.     printf ("};\n");
  146.  
  147.     if (debug) {
  148.         printf ("\n/* Character data dump: Size = %d bytes\n", c_size);
  149.         for (i = 0; i < c_size; i++) {
  150.         if (! (i % 10)) printf ("\n%6d  ", i);
  151.         chprint ((unsigned char) mptr[i]);
  152.         }
  153.         printf ("\n*/\n");
  154.     }
  155.  
  156.     exit (0);
  157. }
  158.  
  159. vprint (str)
  160. char    *str;
  161. {
  162.     if (!str) return;
  163.     while (*str) {
  164.         char    c[5];
  165.  
  166.         if (isascii (*str) && isprint (*str)
  167.                    && *str != '\\' && *str != '"' ) {
  168.         c[0] = *str;
  169.         c[1] = '\0';
  170.         }
  171.         else {
  172.         switch (*str) {
  173.         case '\\':
  174.             strcpy (c, "\\\\");
  175.             break;
  176.         case '"':
  177.             strcpy (c, "\\\"");
  178.             break;
  179.         case '\b':
  180.             strcpy (c, "\\b");
  181.             break;
  182.         case '\t':
  183.             strcpy (c, "\\t");
  184.             break;
  185.         case '\n':
  186.             strcpy (c, "\\n");
  187.             break;
  188.         case '\r':
  189.             strcpy (c, "\\r");
  190.             break;
  191.         default:
  192.             sprintf (c, "\\%3.3o", (int)*str & 0377);
  193.             break;
  194.         }
  195.         }
  196.     printf ("%s", c);
  197.     str++;
  198.     }
  199. }
  200.  
  201. chprint (c)
  202. unsigned char    c;
  203. {
  204.     if (isascii (c) && isgraph (c)) printf ("    %c", c);
  205.     else if (c) printf (" \\%.3o", c);
  206.     else printf (" ----");
  207. }
  208.  
  209. loadtab (tname)
  210. char    *tname;
  211. {
  212.     int    tfd, c;
  213.     char    *malloc();
  214.     struct stat    statbuf;
  215.  
  216.     if ((tfd=open (tname, O_RDONLY)) < 0 ) {
  217.         perror (tname);
  218.         exit (1);
  219.     }
  220.     fstat (tfd, &statbuf);
  221.     read (tfd, &c_size, sizeof (int));
  222.     read (tfd, &t_stor, sizeof (t_stor));
  223.     mptr = malloc (c_size);
  224.     read (tfd, mptr, c_size);
  225.     c = sizeof (int) + sizeof (t_stor) + c_size;
  226.     if (c != statbuf.st_size) fprintf (stderr,
  227.     "WARNING: %s size: %d bytes (should be %d), output likely corrupt\n",
  228.         tname, statbuf.st_size, c);
  229.  
  230.     t.bset = t_stor.bset;
  231.     t.breset = t_stor.breset;
  232.     t.Hor = t_stor.Hor;
  233.     t.Vert = t_stor.Vert;
  234.     t.Newline = t_stor.Newline;
  235.     t.Char = t_stor.Char;
  236. #ifdef KANJI
  237.     t.Kchar = t_stor.Kchar;
  238. #endif KANJI
  239.     t.Em = t_stor.Em;
  240.     t.Halfline = t_stor.Halfline;
  241.     t.Adj = t_stor.Adj;
  242.     getptr (twinit);
  243.     getptr (twrest);
  244.     getptr (twnl);
  245.     getptr (hlr);
  246.     getptr (hlf);
  247.     getptr (flr);
  248.     getptr (bdon);
  249.     getptr (bdoff);
  250.     getptr (iton);
  251.     getptr (itoff);
  252.     getptr (ploton);
  253.     getptr (plotoff);
  254.     getptr (up);
  255.     getptr (down);
  256.     getptr (right);
  257.     getptr (left);
  258.  
  259.     for (c = 0; c < 256 - 32; c++)
  260.         getptr (codetab[c]);
  261. }
  262.