Debian Codegraph @

texk/xdvik/squeeze/squeeze.c

raw
1 /*========================================================================*\
2
3 Copyright (c) 1994-2004 Paul Vojta
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to
7 deal in the Software without restriction, including without limitation the
8 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 sell copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
19 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
22
23 NOTE:
24 This routine is adapted from the squeeze.c that comes with dvips;
25 it bears the message:
26 This software is Copyright 1988 by Radical Eye Software.
27 Used with permission.
28
29 \*========================================================================*/
30
31 /*
32 * This routine squeezes a PostScript file down to its
33 * minimum. We parse and then output it.
34 * Adapted for xdvi 1/94. Writes a C program that contains the PS file
35 * as a constant string.
36 */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #ifndef EXIT_SUCCESS
42 #define EXIT_SUCCESS 0
43 #endif
44 #ifndef EXIT_FAILURE
45 #define EXIT_FAILURE 1
46 #endif
47
48
49 #define LINELENGTH (72)
50 #define BUFLENGTH (1000)
51 #undef putchar
52 #define putchar(a) (void)putc(a, out) ;
53 FILE *in, *out;
54 static int linepos = 0;
55 static int lastspecial = 1;
56 static int stringlen = 0;
57 /*
58 * This next routine writes out a `special' character. In this case,
59 * we simply put it out, since any special character terminates the
60 * preceding token.
61 */
62 static void
63 specialout(char c)
64 {
65 if (linepos + 1 > LINELENGTH) {
66 (void)fputs("\\n\\\n", out);
67 stringlen += linepos + 1;
68 linepos = 0;
69 }
70 putchar(c);
71 linepos++;
72 lastspecial = 1;
73 }
74
75 static void
76 strout(char *s)
77 {
78 if (linepos + strlen(s) > LINELENGTH) {
79 (void)fputs("\\n\\\n", out);
80 stringlen += linepos + 1;
81 linepos = 0;
82 }
83 linepos += strlen(s);
84 while (*s != 0)
85 putchar(*s++);
86 lastspecial = 1;
87 }
88
89 static void
90 cmdout(char *s)
91 {
92 int l;
93
94 l = strlen(s);
95 if (linepos + l + 1 > LINELENGTH) {
96 (void)fputs("\\n\\\n", out);
97 stringlen += linepos + 1;
98 linepos = 0;
99 lastspecial = 1;
100 }
101 if (!lastspecial) {
102 putchar(' ');
103 linepos++;
104 }
105 while (*s != 0) {
106 putchar(*s++);
107 }
108 linepos += l;
109 lastspecial = 0;
110 }
111
112 char buf[BUFLENGTH];
113 #ifndef VMS
114 int
115 #endif
116 main(int argc, char *argv[])
117 {
118 int c;
119 char *b;
120 char seeking;
121
122 if (argc > 3 || (in = (argc < 2 ? stdin : fopen(argv[1], "r"))) == NULL ||
123 (out = (argc < 3 ? stdout : fopen(argv[2], "w"))) == NULL) {
124 (void)fprintf(stderr, "Usage: squeeze [infile [outfile]]\n");
125 exit(EXIT_FAILURE);
126 }
127 (void)fputs("/*\n"
128 " * DO NOT EDIT THIS FILE!\n"
129 " * It was created by squeeze.c from another file (see the Makefile).\n"
130 " */\n\n"
131 "const char psheader[] = \"\\\n", out);
132 while (1) {
133 c = getc(in);
134 if (c == EOF)
135 break;
136 if (c == '%') {
137 while ((c = getc(in)) != '\n');
138 }
139 if (c <= ' ')
140 continue;
141 switch (c) {
142 case '{':
143 case '}':
144 case '[':
145 case ']':
146 specialout(c);
147 break;
148 case '<':
149 case '(':
150 if (c == '(')
151 seeking = ')';
152 else
153 seeking = '>';
154 b = buf;
155 *b++ = c;
156 do {
157 c = getc(in);
158 if (b > buf + BUFLENGTH - 2) {
159 (void)fprintf(stderr, "Overran buffer seeking %c", seeking);
160 exit(EXIT_FAILURE);
161 }
162 *b++ = c;
163 if (c == '\\')
164 *b++ = getc(in);
165 } while (c != seeking);
166 *b++ = 0;
167 strout(buf);
168 break;
169 default:
170 b = buf;
171 while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
172 (c >= '0' && c <= '9') || (c == '/') || (c == '@') ||
173 (c == '!') || (c == '"') || (c == '&') || (c == '*')
174 || (c == ':') || (c == ',') || (c == ';') || (c == '?')
175 || (c == '^') || (c == '~') || (c == '-') || (c == '.')
176 || (c == '#') || (c == '|') || (c == '_') || (c == '=')
177 || (c == '$') || (c == '+')) {
178 *b++ = c;
179 c = getc(in);
180 }
181 if (b == buf) {
182 (void)fprintf(stderr, "Oops! Missed a case: %c.\n", c);
183 exit(EXIT_FAILURE);
184 }
185 *b++ = 0;
186 (void)ungetc(c, in);
187 cmdout(buf);
188 }
189 }
190 (void)fprintf(out,
191 "\\n\";\n\n int\tpsheaderlen\t= %d;\n",
192 stringlen + linepos + 1);
193 return 0;
194 }