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

  1. /*
  2.  * convtab.c -- convert old nroff driver tables to new form and vice-versa
  3.  *
  4.  * This main calls four functions dit{read,write} and ot{read,write} which seal
  5.  * away the operations of reading and writing new (dit*) and old (ot*) style
  6.  * nroff driver tables. All set up or read from the common nrtab_t structure.
  7.  *
  8.  * Common accesses to the structure are implemented in termtab.c, which
  9.  * contains code for a flavor of string storage pool that both use. The
  10.  * escapes.c module provides common character-escape and escape-expansion
  11.  * handling.
  12.  *
  13.  * This code brought to you as a public service by Eric S. Raymond, Feb 1988
  14.  * and is copyrighted (c)1988 by the author. Use, distribute, and mangle
  15.  * freely, but don't try to make money selling it unless you're going to send
  16.  * me a cut. Send bug reports, love letters and death threats to eric@snark
  17.  * aka ...!rutgers!vu-vlsi!snark!eric.
  18.  */
  19. /*LINTLIBRARY*/
  20. #include <stdio.h>
  21. #include "termtab.h"
  22.  
  23. extern char *mktemp(), *strcpy();
  24. extern void perror();
  25.  
  26. extern int ignorebad;    /* ditread.o supplies this */
  27.  
  28. main(argc, argv)
  29. int argc;
  30. char *argv[];
  31. {
  32.     int verbose = 0, debug = 0, filecount = 0;
  33.     FILE *ifp, *ofp;
  34.     nrtab_t *ttab;
  35.     extern errno;
  36.  
  37.     for (++argv, --argc; argc; argv++, argc--)
  38.     {
  39.     char sw = argv[0][0];
  40.     char opt = argv[0][1];
  41.  
  42.     if (sw == '-' || sw == '+')    /* a switch argument */
  43.     {
  44.         switch(opt)
  45.         {
  46.         case 'x':
  47.         ignorebad = (sw == '-');
  48.         if (verbose)
  49.             (void) printf("convtab: ignorebad %s\n",
  50.                   ignorebad ? "set" : "cleared");
  51.         break;
  52.  
  53.         case 'v':
  54.         verbose = (sw == '-');
  55.         if (verbose)
  56.             (void) printf("convtab: verbose %s\n",
  57.                   verbose ? "set" : "cleared");
  58.         break;
  59.  
  60.         case 'd':
  61.         if (debug = (sw == '-'))
  62.             verbose = 1;
  63.         if (verbose)
  64.             (void) printf("convtab: debug %s\n",
  65.                   debug ? "set" : "cleared");
  66.         break;
  67.         }
  68.     }
  69.     else                /* a filename argument */
  70.     {
  71.         char target[BUFSIZ];
  72.         char tempf[20];
  73.  
  74.         filecount++;
  75.  
  76.         (void) strcpy(tempf, "/tmp/convtabXXXXX");
  77.         (void) mktemp(tempf);
  78.  
  79.         if (strncmp(argv[0], "tab.", 4) == 0)
  80.         {
  81.         (void) sprintf(target, "tab%s", argv[0] + 4);
  82.         if (verbose)
  83.             (void) fprintf(stderr,
  84.             "convtab: %s (ditroff format) to %s (old format)\n",
  85.             argv[0], target);
  86.         if (!debug)
  87.         {
  88.             ifp = fopen(argv[0], "r");
  89.             ofp = fopen(tempf, "w");
  90.  
  91.             ttab = ditread(ifp);
  92.             otwrite(ttab, ofp);
  93.  
  94.             if (unlink(target) == -1)
  95.             perror("convtab (unlink failed)");
  96.             if (link(tempf, target) == -1)
  97.             perror("convtab (link failed)");
  98.             if (unlink(tempf) == -1)
  99.             perror("convtab (unlink failed)");
  100.         }
  101.         }
  102.         else if (strncmp(argv[0], "tab", 3) == 0)
  103.         {
  104.         (void) sprintf(target, "tab.%s", argv[0] + 3);
  105.         if (verbose)
  106.             (void) fprintf(stderr,
  107.             "convtab: %s (old format) to %s (ditroff format)\n",
  108.             argv[0], target);
  109.         if (!debug)
  110.         {
  111.             ifp = fopen(argv[0], "r");
  112.             ofp = fopen(tempf, "w");
  113.  
  114.             ttab = otread(ifp);
  115.             (void) fprintf(ofp, "%s\n", target + 4);
  116.             ditwrite(ttab, ofp);
  117.  
  118.             if (unlink(target) == -1)
  119.             perror("convtab (unlink failed)");
  120.             if (link(tempf, target) == -1)
  121.             perror("convtab (link failed)");
  122.             if (unlink(tempf) == -1)
  123.             perror("convtab (unlink failed)");
  124.         }
  125.         }
  126.         else
  127.         (void) fprintf(stderr,
  128.                "convtab: skipping %s, (name prefix no good)\n",
  129.                argv[0]);
  130.     }
  131.     }
  132.  
  133.     /* no filenames given? that's OK, just convert new to old as a filter */
  134.     if (filecount == 0)
  135.     {
  136.     ttab = ditread(ifp);
  137.     otwrite(ttab, ofp);
  138.     }
  139.  
  140.     return(0);
  141. }
  142.  
  143. /* convtab.c ends here */
  144.