home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / gnu / groff_src.lha / groff-1.10src / pfbtops / pfbtops.c < prev    next >
C/C++ Source or Header  |  1993-04-19  |  2KB  |  113 lines

  1. /* This translates ps fonts in .pfb format to ASCII ps files. */
  2.  
  3. #include <stdio.h>
  4.  
  5. /* Binary bytes per output line. */
  6. #define BYTES_PER_LINE (64/2)
  7. #define HEX_DIGITS "0123456789abcdef"
  8.  
  9. static char *program_name;
  10.  
  11. static void error(s)
  12.      char *s;
  13. {
  14.   fprintf(stderr, "%s: %s\n", program_name, s);
  15.   exit(2);
  16. }
  17.  
  18. static void usage()
  19. {
  20.   fprintf(stderr, "usage: %s [-v] [pfb_file]\n", program_name);
  21.   exit(1);
  22. }
  23.  
  24. int main(argc, argv)
  25.      int argc;
  26.      char **argv;
  27. {
  28.   int opt;
  29.   extern int optind;
  30.  
  31.   program_name = argv[0];
  32.  
  33.   while ((opt = getopt(argc, argv, "v")) != EOF) {
  34.     switch (opt) {
  35.     case 'v':
  36.       {
  37.     extern char *version_string;
  38.     fprintf(stderr, "pfbtops groff version %s\n", version_string);
  39.     fflush(stderr);
  40.     break;
  41.       }
  42.     case '?':
  43.       usage();
  44.     }
  45.   }
  46.  
  47.   if (argc - optind > 1)
  48.     usage();
  49.   if (argc > optind && !freopen(argv[optind], "r", stdin))
  50.     {
  51.       perror(argv[optind]);
  52.       exit(1);
  53.     }
  54.   for (;;)
  55.     {
  56.       int type, c, i;
  57.       long n;
  58.  
  59.       c = getchar();
  60.       if (c != 0x80)
  61.     error("first byte of packet not 0x80");
  62.       type = getchar();
  63.       if (type == 3)
  64.     break;
  65.       if (type != 1 && type != 2)
  66.     error("bad packet type");
  67.       n = 0;
  68.       for (i = 0; i < 4; i++)
  69.     {
  70.       c = getchar();
  71.       if (c == EOF)
  72.         error("end of file in packet header");
  73.       n |= (long)c << (i << 3);
  74.     }
  75.       if (n < 0)
  76.     error("negative packet length");
  77.       if (type == 1)
  78.     {
  79.       while (--n >= 0)
  80.         {
  81.           c = getchar();
  82.           if (c == EOF)
  83.         error("end of file in text packet");
  84.           if (c == '\r')
  85.         c = '\n';
  86.           putchar(c);
  87.         }
  88.       if (c != '\n')
  89.         putchar('\n');
  90.     }
  91.       else
  92.     {
  93.       int count = 0;
  94.       while (--n >= 0)
  95.         {
  96.           c = getchar();
  97.           if (c == EOF)
  98.         error("end of file in binary packet");
  99.           if (count >= BYTES_PER_LINE)
  100.         {
  101.           putchar('\n');
  102.           count = 0;
  103.         }
  104.           count++;
  105.           putchar(HEX_DIGITS[(c >> 4) & 0xf]);
  106.           putchar(HEX_DIGITS[c & 0xf]);
  107.         }
  108.       putchar('\n');
  109.     }
  110.     }
  111.   exit(0);
  112. }
  113.