]>
Commit | Line | Data |
---|---|---|
80ca4592 | 1 | /* |
2 | * "$Id: cupstestdsc.c 5320 2006-03-21 19:03:25Z mike $" | |
3 | * | |
4 | * DSC test program for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2006 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * PostScript is a trademark of Adobe Systems, Inc. | |
25 | * | |
26 | * This file is subject to the Apple OS-Developed Software exception. | |
27 | * | |
28 | * Contents: | |
29 | * | |
30 | */ | |
31 | ||
32 | /* | |
33 | * Include necessary headers... | |
34 | */ | |
35 | ||
36 | #include <cups/string.h> | |
37 | #include <cups/cups.h> | |
38 | #include <cups/file.h> | |
39 | #include <cups/i18n.h> | |
40 | #include <errno.h> | |
41 | #include <stdlib.h> | |
42 | ||
43 | ||
44 | /* | |
45 | * Local functions... | |
46 | */ | |
47 | ||
48 | static int check_file(const char *filename); | |
49 | static void usage(void); | |
50 | ||
51 | ||
52 | /* | |
53 | * 'main()' - Main entry for test program. | |
54 | */ | |
55 | ||
56 | int /* O - Exit status */ | |
57 | main(int argc, /* I - Number of command-line args */ | |
58 | char *argv[]) /* I - Command-line arguments */ | |
59 | { | |
60 | int i; /* Looping var */ | |
61 | int status; /* Status of tests */ | |
62 | int num_files; /* Number of files tested */ | |
63 | ||
64 | ||
65 | /* | |
66 | * Collect command-line arguments... | |
67 | */ | |
68 | ||
69 | for (i = 1, num_files = 0, status = 0; i < argc; i ++) | |
70 | if (argv[i][0] == '-') | |
71 | { | |
72 | if (argv[i][1]) | |
73 | { | |
74 | /* | |
75 | * Currently the only supported option is "-h" (help)... | |
76 | */ | |
77 | ||
78 | usage(); | |
79 | } | |
80 | else | |
81 | { | |
82 | num_files ++; | |
83 | status += check_file("(stdin)"); | |
84 | } | |
85 | } | |
86 | else | |
87 | { | |
88 | num_files ++; | |
89 | status += check_file(argv[i]); | |
90 | } | |
91 | ||
92 | if (!num_files) | |
93 | usage(); | |
94 | ||
95 | return (status); | |
96 | } | |
97 | ||
98 | ||
99 | /* | |
100 | * 'check()' - Main entry for test program. | |
101 | */ | |
102 | ||
103 | static int /* O - 0 on success, 1 on failure */ | |
104 | check_file(const char *filename) /* I - File to read from */ | |
105 | { | |
106 | int i; /* Looping var */ | |
107 | cups_file_t *fp; /* File */ | |
108 | char line[1024]; /* Line from file */ | |
109 | int ch; /* Current character */ | |
110 | size_t bytes; /* Length of line */ | |
111 | int status; /* Status of test */ | |
112 | int linenum; /* Line number */ | |
113 | int binary; /* File contains binary data? */ | |
114 | float version; /* DSC version */ | |
115 | int lbrt[4]; /* Bounding box */ | |
116 | char page_label[256]; /* Page label string */ | |
117 | int page_number; /* Page number */ | |
118 | int level; /* Embedded document level */ | |
119 | int saw_bounding_box, /* %%BoundingBox seen? */ | |
120 | saw_pages, /* %%Pages seen? */ | |
121 | saw_end_comments, /* %%EndComments seen? */ | |
122 | saw_begin_prolog, /* %%BeginProlog seen? */ | |
123 | saw_end_prolog, /* %%EndProlog seen? */ | |
124 | saw_begin_setup, /* %%BeginSetup seen? */ | |
125 | saw_end_setup, /* %%EndSetup seen? */ | |
126 | saw_page, /* %%Page seen? */ | |
127 | saw_trailer, /* %%Trailer seen? */ | |
128 | saw_eof, /* %%EOF seen? */ | |
129 | saw_long_line; /* Saw long lines? */ | |
130 | ||
131 | ||
132 | /* | |
133 | * Open the file... | |
134 | */ | |
135 | ||
136 | if (!strcmp(filename, "(stdin)")) | |
137 | fp = cupsFileStdin(); | |
138 | else | |
139 | fp = cupsFileOpen(filename, "r"); | |
140 | ||
141 | if (!fp) | |
142 | { | |
143 | perror(filename); | |
144 | return (1); | |
145 | } | |
146 | ||
147 | /* | |
148 | * Scan the file... | |
149 | */ | |
150 | ||
151 | binary = 0; | |
152 | level = 0; | |
153 | linenum = 0; | |
154 | saw_begin_prolog = 0; | |
155 | saw_begin_setup = 0; | |
156 | saw_bounding_box = 0; | |
157 | saw_end_comments = 0; | |
158 | saw_end_prolog = 0; | |
159 | saw_end_setup = 0; | |
160 | saw_eof = 0; | |
161 | saw_long_line = 0; | |
162 | saw_page = 0; | |
163 | saw_pages = 0; | |
164 | saw_trailer = 0; | |
165 | status = 0; | |
166 | version = 0.0f; | |
167 | ||
168 | _cupsLangPrintf(stdout, "%s: ", filename); | |
169 | fflush(stdout); | |
170 | ||
171 | while ((bytes = cupsFileGetLine(fp, line, sizeof(line))) > 0) | |
172 | { | |
173 | linenum ++; | |
174 | ||
175 | if (bytes > 255) | |
176 | { | |
177 | if (!saw_long_line) | |
178 | { | |
179 | if (!status) | |
180 | _cupsLangPuts(stdout, _("FAIL\n")); | |
181 | ||
182 | status ++; | |
183 | _cupsLangPrintf(stdout, | |
184 | _(" Line %d is longer than 255 characters (%d)!\n" | |
185 | " REF: Page 25, Line Length\n"), | |
186 | linenum, (int)bytes); | |
187 | } | |
188 | ||
189 | saw_long_line ++; | |
190 | } | |
191 | ||
192 | if (linenum == 1) | |
193 | { | |
194 | if (strncmp(line, "%!PS-Adobe-", 11)) | |
195 | { | |
196 | if (!status) | |
197 | _cupsLangPuts(stdout, _("FAIL\n")); | |
198 | ||
199 | status ++; | |
200 | _cupsLangPuts(stdout, | |
201 | _(" Missing %!PS-Adobe-3.0 on first line!\n" | |
202 | " REF: Page 17, 3.1 Conforming Documents\n")); | |
203 | cupsFileClose(fp); | |
204 | return (1); | |
205 | } | |
206 | else | |
207 | version = atof(line + 11); | |
208 | } | |
209 | else if (level > 0) | |
210 | { | |
211 | if (!strncmp(line, "%%BeginDocument:", 16)) | |
212 | level ++; | |
213 | else if (!strncmp(line, "%%EndDocument", 13)) | |
214 | level --; | |
215 | } | |
216 | else if (saw_trailer) | |
217 | { | |
218 | if (!strncmp(line, "%%Pages:", 8)) | |
219 | { | |
220 | if (atoi(line + 8) <= 0) | |
221 | { | |
222 | if (!status) | |
223 | _cupsLangPuts(stdout, _("FAIL\n")); | |
224 | ||
225 | status ++; | |
226 | _cupsLangPrintf(stdout, | |
227 | _(" Bad %%%%Pages: on line %d!\n" | |
228 | " REF: Page 43, %%%%Pages:\n"), | |
229 | linenum); | |
230 | } | |
231 | else | |
232 | saw_pages = 1; | |
233 | } | |
234 | else if (!strncmp(line, "%%BoundingBox:", 14)) | |
235 | { | |
236 | if (sscanf(line + 14, "%d%d%d%d", lbrt + 0, lbrt + 1, lbrt + 2, | |
237 | lbrt + 3) != 4) | |
238 | { | |
239 | if (!status) | |
240 | _cupsLangPuts(stdout, _("FAIL\n")); | |
241 | ||
242 | status ++; | |
243 | _cupsLangPrintf(stdout, _(" Bad %%%%BoundingBox: on line %d!\n" | |
244 | " REF: Page 39, %%%%BoundingBox:\n"), | |
245 | linenum); | |
246 | } | |
247 | else | |
248 | saw_bounding_box = 1; | |
249 | } | |
250 | } | |
251 | else if (!saw_end_comments) | |
252 | { | |
253 | if (!strncmp(line, "%%EndComments", 13)) | |
254 | saw_end_comments = 1; | |
255 | else if (line[0] != '%') | |
256 | saw_end_comments = -1; | |
257 | else if (!strncmp(line, "%%Pages:", 8)) | |
258 | { | |
259 | if (strstr(line + 8, "(atend)")) | |
260 | saw_pages = -1; | |
261 | else if (atoi(line + 8) <= 0) | |
262 | { | |
263 | if (!status) | |
264 | _cupsLangPuts(stdout, _("FAIL\n")); | |
265 | ||
266 | status ++; | |
267 | _cupsLangPrintf(stdout, _(" Bad %%%%Pages: on line %d!\n" | |
268 | " REF: Page 43, %%%%Pages:\n"), | |
269 | linenum); | |
270 | } | |
271 | else | |
272 | saw_pages = 1; | |
273 | } | |
274 | else if (!strncmp(line, "%%BoundingBox:", 14)) | |
275 | { | |
276 | if (strstr(line, "(atend)")) | |
277 | saw_bounding_box = -1; | |
278 | else if (sscanf(line + 14, "%d%d%d%d", lbrt + 0, lbrt + 1, lbrt + 2, | |
279 | lbrt + 3) != 4) | |
280 | { | |
281 | if (!status) | |
282 | _cupsLangPuts(stdout, _("FAIL\n")); | |
283 | ||
284 | status ++; | |
285 | _cupsLangPrintf(stdout, _(" Bad %%%%BoundingBox: on line %d!\n" | |
286 | " REF: Page 39, %%%%BoundingBox:\n"), | |
287 | linenum); | |
288 | } | |
289 | else | |
290 | saw_bounding_box = 1; | |
291 | } | |
292 | } | |
293 | else if (saw_begin_prolog && !saw_end_prolog) | |
294 | { | |
295 | if (!strncmp(line, "%%EndProlog", 11)) | |
296 | saw_end_prolog = 1; | |
297 | } | |
298 | else if (saw_begin_setup && !saw_end_setup) | |
299 | { | |
300 | if (!strncmp(line, "%%EndSetup", 10)) | |
301 | saw_end_setup = 1; | |
302 | } | |
303 | else if (saw_end_comments) | |
304 | { | |
305 | if (!strncmp(line, "%%Page:", 7)) | |
306 | { | |
307 | if (sscanf(line + 7, "%255s%d", page_label, &page_number) != 2) | |
308 | { | |
309 | if (!status) | |
310 | _cupsLangPuts(stdout, _("FAIL\n")); | |
311 | ||
312 | status ++; | |
313 | _cupsLangPrintf(stdout, _(" Bad %%%%Page: on line %d!\n" | |
314 | " REF: Page 53, %%%%Page:\n"), | |
315 | linenum); | |
316 | } | |
317 | else | |
318 | saw_page = 1; | |
319 | } | |
320 | else if (!strncmp(line, "%%BeginProlog", 13)) | |
321 | saw_begin_prolog = 1; | |
322 | else if (!strncmp(line, "%%BeginSetup", 12)) | |
323 | saw_begin_setup = 1; | |
324 | else if (!strncmp(line, "%%BeginDocument:", 16)) | |
325 | level ++; | |
326 | else if (!strncmp(line, "%%EndDocument", 13)) | |
327 | level --; | |
328 | else if (!strncmp(line, "%%Trailer", 9)) | |
329 | saw_trailer = 1; | |
330 | } | |
331 | ||
332 | for (i = 0; !binary && i < bytes; i ++) | |
333 | { | |
334 | ch = line[i]; | |
335 | ||
336 | if ((ch < ' ' || (ch & 0x80)) && ch != '\n' && ch != '\r' && ch != '\t') | |
337 | binary = 1; | |
338 | } | |
339 | } | |
340 | ||
341 | if (saw_bounding_box <= 0) | |
342 | { | |
343 | if (!status) | |
344 | _cupsLangPuts(stdout, _("FAIL\n")); | |
345 | ||
346 | status ++; | |
347 | _cupsLangPuts(stdout, _(" Missing or bad %%BoundingBox: comment!\n" | |
348 | " REF: Page 39, %%BoundingBox:\n")); | |
349 | } | |
350 | ||
351 | if (saw_pages <= 0) | |
352 | { | |
353 | if (!status) | |
354 | _cupsLangPuts(stdout, _("FAIL\n")); | |
355 | ||
356 | status ++; | |
357 | _cupsLangPuts(stdout, _(" Missing or bad %%Pages: comment!\n" | |
358 | " REF: Page 43, %%Pages:\n")); | |
359 | } | |
360 | ||
361 | if (!saw_end_comments) | |
362 | { | |
363 | if (!status) | |
364 | _cupsLangPuts(stdout, _("FAIL\n")); | |
365 | ||
366 | status ++; | |
367 | _cupsLangPuts(stdout, _(" Missing %%EndComments comment!\n" | |
368 | " REF: Page 41, %%EndComments\n")); | |
369 | } | |
370 | ||
371 | if (!saw_page) | |
372 | { | |
373 | if (!status) | |
374 | _cupsLangPuts(stdout, _("FAIL\n")); | |
375 | ||
376 | status ++; | |
377 | _cupsLangPuts(stdout, _(" Missing or bad %%Page: comments!\n" | |
378 | " REF: Page 53, %%Page:\n")); | |
379 | } | |
380 | ||
381 | if (level < 0) | |
382 | { | |
383 | if (!status) | |
384 | _cupsLangPuts(stdout, _("FAIL\n")); | |
385 | ||
386 | status ++; | |
387 | _cupsLangPuts(stdout, _(" Too many %%EndDocument comments!\n")); | |
388 | } | |
389 | else if (level > 0) | |
390 | { | |
391 | if (!status) | |
392 | _cupsLangPuts(stdout, _("FAIL\n")); | |
393 | ||
394 | status ++; | |
395 | _cupsLangPuts(stdout, _(" Too many %%BeginDocument comments!\n")); | |
396 | } | |
397 | ||
398 | if (saw_long_line > 1) | |
399 | _cupsLangPrintf(stderr, | |
400 | _(" Saw %d lines that exceeded 255 characters!\n"), | |
401 | saw_long_line); | |
402 | ||
403 | if (!status) | |
404 | _cupsLangPuts(stdout, _("PASS\n")); | |
405 | ||
406 | if (binary) | |
407 | _cupsLangPuts(stdout, _(" Warning: file contains binary data!\n")); | |
408 | ||
409 | if (version < 3.0f) | |
410 | _cupsLangPrintf(stdout, | |
411 | _(" Warning: obsolete DSC version %.1f in file!\n"), | |
412 | version); | |
413 | ||
414 | if (saw_end_comments < 0) | |
415 | _cupsLangPuts(stdout, _(" Warning: no %%EndComments comment in file!\n")); | |
416 | ||
417 | cupsFileClose(fp); | |
418 | ||
419 | return (status); | |
420 | } | |
421 | ||
422 | ||
423 | /* | |
424 | * 'usage()' - Show program usage. | |
425 | */ | |
426 | ||
427 | static void | |
428 | usage(void) | |
429 | { | |
430 | _cupsLangPuts(stdout, | |
431 | _("Usage: cupstestdsc [options] filename.ps [... filename.ps]\n" | |
432 | " cupstestdsc [options] -\n" | |
433 | "\n" | |
434 | "Options:\n" | |
435 | "\n" | |
436 | " -h Show program usage\n" | |
437 | "\n" | |
438 | " Note: this program only validates the DSC comments, " | |
439 | "not the PostScript itself.\n")); | |
440 | ||
441 | exit(1); | |
442 | } | |
443 | ||
444 | ||
445 | /* | |
446 | * End of "$Id: cupstestdsc.c 5320 2006-03-21 19:03:25Z mike $". | |
447 | */ |