home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / ps / psnup / psc.c < prev    next >
C/C++ Source or Header  |  1991-02-26  |  3KB  |  144 lines

  1. /***
  2.  ***    psc.c --    $Revision: 1.6 $ $Date: 86/04/04 10:35:54 $
  3.  ***
  4.  ***    Compress a PostScript file.
  5.  ***
  6.  ***    Ned Batchelder, University of Pennsylvania
  7.  ***    ned@UPenn.CSnet
  8.  ***/
  9.  
  10. # include <stdio.h>
  11.  
  12. # define RightMargin    70
  13.  
  14. char    *selfd = "[]{}/";    /* Self delimiting: Need no space around */
  15. char    *ws = " \t\n";        /* White space characters */
  16. char    lp = '(';        /* Opens a balanced text string */
  17. char    rp = ')';        /* Closes a balanced text string */
  18. char    lh = '<';        /* Opens a hex string */
  19. char    rh = '>';        /* Closes a hex string*/
  20. char    lit = '\\';        /* Quotes characters in strings */
  21. char    com = '%';        /* Introduces comments (to end of line) */
  22.  
  23. /*
  24.  * Psc runs as a pure filter. The input is compressed to the output.
  25.  */
  26.  
  27. main()
  28. {
  29.     int    plevel = 0;    /* The level of parens in a string */
  30.     int    needspace = 0;    /* Found some space, must delimit */
  31.     int    eatspace = 1;    /* We don't need any space now */
  32.     int    incomment = 0;    /* Are we skipping a comment? */
  33.     int    inhex = 0;    /* Are we in a hex string? */
  34.     int    column = 0;    /* Counts output columns to keep lines short */
  35.     int    keepch = 0;    /* For breaking strings */
  36.     char    c;        /* The current character */
  37.  
  38. # define put(c)        {putchar(c); column++;}
  39.  
  40.     /*
  41.      * First we copy the first line verbatim. This is to copy the comment
  42.      * for the file, the name of the file, and the initial '%!' if
  43.      * necessary.
  44.      */
  45.  
  46.     while ((c = getchar()) != '\n') {
  47.         putchar(c);
  48.     }
  49.     putchar('\n');
  50.  
  51.     /*
  52.      * Now we start compressing.
  53.      */
  54.  
  55.     while ((c = getchar()) != EOF) {
  56.         if (incomment) {
  57.             if (c == '\n') {
  58.                 incomment = 0;
  59.             }
  60.             continue;
  61.         } else if (plevel) {
  62.             if (column > RightMargin && keepch <= 0) {
  63.                 putchar('\\');
  64.                 putchar('\n');
  65.                 column = 0;
  66.             }
  67.             if (c == lit) {
  68.                 put(lit);
  69.                 put(getchar());
  70.                 keepch = 2;        /* Protect \ddd */
  71.             } else if (c == lp) {
  72.                 put(lp);
  73.                 plevel++;
  74.                 keepch = 0;
  75.             } else if (c == rp) {
  76.                 put(rp);
  77.                 plevel--;
  78.                 if (plevel == 0) {
  79.                     eatspace = 1;
  80.                     needspace = 0;
  81.                 }
  82.             } else {
  83.                 put(c);
  84.                 keepch--;
  85.             }
  86.         } else if (inhex) {
  87.             if (column > RightMargin) {
  88.                 putchar('\n');
  89.                 column = 0;
  90.             }
  91.             if (!index(ws, c)) {
  92.                 put(c);
  93.             }
  94.             if (c == rh) {
  95.                 eatspace = 1;
  96.                 needspace = 0;
  97.                 inhex = 0;
  98.             }
  99.         } else if (c == lh) {
  100.             put(lh);
  101.             inhex++;
  102.         } else if (c == com) {
  103.             if (column > RightMargin) {
  104.                 putchar('\n');
  105.                 column = 0;
  106.             }
  107.             incomment = 1;
  108.             if (!eatspace) {
  109.                 needspace = 1;
  110.             }
  111.         } else if (index(ws, c)) {
  112.             if (!eatspace) {
  113.                 needspace = 1;
  114.             }
  115.         } else if (index(selfd, c)) {
  116.             if (column > RightMargin) {
  117.                 putchar('\n');
  118.                 column = 0;
  119.             }
  120.             put(c);
  121.             eatspace = 1;
  122.             needspace = 0;
  123.         } else if (c == lp) {
  124.             put(lp);
  125.             plevel = 1;
  126.             keepch = 0;
  127.         } else {
  128.             if (needspace) {
  129.                 putchar('\n');
  130.                 column = 0;
  131.                 needspace = 0;
  132.             }
  133.             put(c);
  134.             eatspace = 0;
  135.         }
  136.     }
  137.  
  138.     putchar('\n');
  139.  
  140.     return 0;
  141. }
  142.  
  143. /* end of psc.c */
  144.