home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d797 / psutils.lha / PSUtils / pfbtops.c < prev    next >
C/C++ Source or Header  |  1993-01-10  |  2KB  |  112 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 (79/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.     fputs("pfbtops Amiga version 1.0\n", stderr);
  38.     fflush(stderr);
  39.     break;
  40.       }
  41.     case '?':
  42.       usage();
  43.     }
  44.   }
  45.  
  46.   if (argc - optind > 1)
  47.     usage();
  48.   if (argc > optind && !freopen(argv[optind], "r", stdin))
  49.     {
  50.       perror(argv[optind]);
  51.       exit(1);
  52.     }
  53.   for (;;)
  54.     {
  55.       int type, c, i;
  56.       long n;
  57.  
  58.       c = getchar();
  59.       if (c != 0x80)
  60.     error("first byte of packet not 0x80");
  61.       type = getchar();
  62.       if (type == 3)
  63.     break;
  64.       if (type != 1 && type != 2)
  65.     error("bad packet type");
  66.       n = 0;
  67.       for (i = 0; i < 4; i++)
  68.     {
  69.       c = getchar();
  70.       if (c == EOF)
  71.         error("end of file in packet header");
  72.       n |= (long)c << (i << 3);
  73.     }
  74.       if (n < 0)
  75.     error("negative packet length");
  76.       if (type == 1)
  77.     {
  78.       while (--n >= 0)
  79.         {
  80.           c = getchar();
  81.           if (c == EOF)
  82.         error("end of file in text packet");
  83.           if (c == '\r')
  84.         c = '\n';
  85.           putchar(c);
  86.         }
  87.       if (c != '\n')
  88.         putchar('\n');
  89.     }
  90.       else
  91.     {
  92.       int count = 0;
  93.       while (--n >= 0)
  94.         {
  95.           c = getchar();
  96.           if (c == EOF)
  97.         error("end of file in binary packet");
  98.           if (count >= BYTES_PER_LINE)
  99.         {
  100.           putchar('\n');
  101.           count = 0;
  102.         }
  103.           count++;
  104.           putchar(HEX_DIGITS[(c >> 4) & 0xf]);
  105.           putchar(HEX_DIGITS[c & 0xf]);
  106.         }
  107.       putchar('\n');
  108.     }
  109.     }
  110.   exit(0);
  111. }
  112.