| 1 |
/* $Id: squeeze.c 14277 2009-07-16 12:00:58Z peter $ |
| 2 |
* Copyright 1986-2008 Tomas Rokicki. |
| 3 |
* You may freely use, modify and/or distribute this program or any |
| 4 |
* portion thereof. |
| 5 |
* This routine squeezes a PostScript file down to its |
| 6 |
* minimum. We parse and then output it. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <fcntl.h> |
| 13 |
|
| 14 |
#ifndef O_BINARY |
| 15 |
# ifdef _O_BINARY |
| 16 |
# define O_BINARY _O_BINARY |
| 17 |
# else |
| 18 |
# define O_BINARY 0 |
| 19 |
# endif |
| 20 |
#endif /* !O_BINARY */ |
| 21 |
|
| 22 |
#if O_BINARY |
| 23 |
# ifdef __CYGWIN__ |
| 24 |
# include <unistd.h> |
| 25 |
# include <io.h> |
| 26 |
# define SET_BINARY(fd) setmode((fd), O_BINARY) |
| 27 |
# else |
| 28 |
# define SET_BINARY(fd) _setmode((fd), O_BINARY) |
| 29 |
# endif |
| 30 |
#endif |
| 31 |
|
| 32 |
#define LINELENGTH (72) |
| 33 |
#define BUFLENGTH (1000) |
| 34 |
|
| 35 |
static char buf[BUFLENGTH] ; |
| 36 |
static FILE *in, *out ; |
| 37 |
static int linepos = 0 ; |
| 38 |
static int lastspecial = 1 ; |
| 39 |
/* |
| 40 |
* This next routine writes out a `special' character. In this case, |
| 41 |
* we simply put it out, since any special character terminates the |
| 42 |
* preceding token. |
| 43 |
*/ |
| 44 |
static void |
| 45 |
specialout (char c) |
| 46 |
{ |
| 47 |
if (linepos + 1 > LINELENGTH) { |
| 48 |
putchar('\n') ; |
| 49 |
linepos = 0 ; |
| 50 |
} |
| 51 |
putchar(c) ; |
| 52 |
linepos++ ; |
| 53 |
lastspecial = 1 ; |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
strout (char *s) |
| 58 |
{ |
| 59 |
if (linepos + strlen(s) > LINELENGTH) { |
| 60 |
putchar('\n') ; |
| 61 |
linepos = 0 ; |
| 62 |
} |
| 63 |
linepos += strlen(s) ; |
| 64 |
while (*s != 0) |
| 65 |
putchar(*s++) ; |
| 66 |
lastspecial = 1 ; |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
cmdout (char *s) |
| 71 |
{ |
| 72 |
int l ; |
| 73 |
|
| 74 |
l = strlen(s) ; |
| 75 |
if (linepos + l + 1 > LINELENGTH) { |
| 76 |
putchar('\n') ; |
| 77 |
linepos = 0 ; |
| 78 |
lastspecial = 1 ; |
| 79 |
} |
| 80 |
if (! lastspecial && *s != '/') { |
| 81 |
putchar(' ') ; |
| 82 |
linepos++ ; |
| 83 |
} |
| 84 |
while (*s != 0) { |
| 85 |
putchar(*s++) ; |
| 86 |
} |
| 87 |
linepos += l ; |
| 88 |
lastspecial = 0 ; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
int |
| 93 |
main (int argc, char *argv[]) |
| 94 |
{ |
| 95 |
int c ; |
| 96 |
char *b ; |
| 97 |
char seeking ; |
| 98 |
|
| 99 |
if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL || |
| 100 |
(out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) { |
| 101 |
(void)fprintf(stderr, "Usage: squeeze [infile [outfile]]\n") ; |
| 102 |
exit(1) ; |
| 103 |
} |
| 104 |
/* Binary output is safer (for those systems which care to know the |
| 105 |
difference) since PostScript can include non-printable characters. */ |
| 106 |
#if O_BINARY |
| 107 |
if (!isatty(fileno(in))) |
| 108 |
SET_BINARY(fileno(in)) ; |
| 109 |
if (!isatty(fileno(out))) |
| 110 |
SET_BINARY(fileno(out)) ; |
| 111 |
#endif |
| 112 |
(void)fprintf(out, "%%!\n") ; |
| 113 |
while (1) { |
| 114 |
c = getc(in) ; |
| 115 |
if (c==EOF) |
| 116 |
break ; |
| 117 |
if (c=='%') { |
| 118 |
while ((c=getc(in))!='\n') ; |
| 119 |
} |
| 120 |
if (c <= ' ') |
| 121 |
continue ; |
| 122 |
switch (c) { |
| 123 |
case '{' : |
| 124 |
case '}' : |
| 125 |
case '[' : |
| 126 |
case ']' : |
| 127 |
specialout((char) c) ; |
| 128 |
break ; |
| 129 |
case '<' : |
| 130 |
case '(' : |
| 131 |
if (c=='(') |
| 132 |
seeking = ')' ; |
| 133 |
else |
| 134 |
seeking = '>' ; |
| 135 |
b = buf ; |
| 136 |
*b++ = c ; |
| 137 |
do { |
| 138 |
c = getc(in) ; |
| 139 |
if (b > buf + BUFLENGTH-2) { |
| 140 |
(void)fprintf(stderr, "Overran buffer seeking %c", seeking) ; |
| 141 |
exit(1) ; |
| 142 |
} |
| 143 |
*b++ = c ; |
| 144 |
if (c=='\\') |
| 145 |
*b++ = getc(in) ; |
| 146 |
} while (c != seeking) ; |
| 147 |
*b++ = 0 ; |
| 148 |
strout(buf) ; |
| 149 |
break ; |
| 150 |
default: |
| 151 |
b = buf ; |
| 152 |
while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')|| |
| 153 |
(c>='0'&&c<='9')||(c=='/')||(c=='@')|| |
| 154 |
(c=='!')||(c=='"')||(c=='&')||(c=='*')||(c==':')|| |
| 155 |
(c==',')||(c==';')||(c=='?')||(c=='^')||(c=='~')|| |
| 156 |
(c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')|| |
| 157 |
(c=='=')||(c=='$')||(c=='+')) { |
| 158 |
*b++ = c ; |
| 159 |
c = getc(in) ; |
| 160 |
} |
| 161 |
if (b == buf) { |
| 162 |
(void)fprintf(stderr, "Oops! Missed a case: %c.\n", c) ; |
| 163 |
exit(1) ; |
| 164 |
} |
| 165 |
*b++ = 0 ; |
| 166 |
(void)ungetc(c, in) ; |
| 167 |
cmdout(buf) ; |
| 168 |
} |
| 169 |
} |
| 170 |
if (linepos != 0) |
| 171 |
putchar('\n') ; |
| 172 |
exit(0) ; |
| 173 |
/*NOTREACHED*/ |
| 174 |
} |