]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupstestdsc.c
2 * DSC test program for CUPS.
4 * Copyright 2007-2010 by Apple Inc.
5 * Copyright 2006 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
9 * PostScript is a trademark of Adobe Systems, Inc.
13 * Include necessary headers...
16 #include <cups/cups-private.h>
23 static int check_file(const char *filename
);
24 static void usage(void) __attribute__((noreturn
));
28 * 'main()' - Main entry for test program.
31 int /* O - Exit status */
32 main(int argc
, /* I - Number of command-line args */
33 char *argv
[]) /* I - Command-line arguments */
35 int i
; /* Looping var */
36 int status
; /* Status of tests */
37 int num_files
; /* Number of files tested */
43 * Collect command-line arguments...
46 for (i
= 1, num_files
= 0, status
= 0; i
< argc
; i
++)
47 if (argv
[i
][0] == '-')
52 * Currently the only supported option is "-h" (help)...
60 status
+= check_file("(stdin)");
66 status
+= check_file(argv
[i
]);
77 * 'check()' - Check a file for conformance.
80 static int /* O - 0 on success, 1 on failure */
81 check_file(const char *filename
) /* I - File to read from */
83 int i
; /* Looping var */
84 cups_file_t
*fp
; /* File */
85 char line
[1024]; /* Line from file */
86 int ch
; /* Current character */
87 size_t bytes
; /* Length of line */
88 int status
; /* Status of test */
89 int linenum
; /* Line number */
90 int binary
; /* File contains binary data? */
91 float version
; /* DSC version */
92 int lbrt
[4]; /* Bounding box */
93 char page_label
[256]; /* Page label string */
94 int page_number
; /* Page number */
95 int last_page_number
; /* Last page number seen */
96 int level
; /* Embedded document level */
97 int saw_bounding_box
, /* %%BoundingBox seen? */
98 saw_pages
, /* %%Pages seen? */
99 saw_end_comments
, /* %%EndComments seen? */
100 saw_begin_prolog
, /* %%BeginProlog seen? */
101 saw_end_prolog
, /* %%EndProlog seen? */
102 saw_begin_setup
, /* %%BeginSetup seen? */
103 saw_end_setup
, /* %%EndSetup seen? */
104 saw_page
, /* %%Page seen? */
105 saw_trailer
, /* %%Trailer seen? */
106 saw_long_line
; /* Saw long lines? */
113 if (!strcmp(filename
, "(stdin)"))
114 fp
= cupsFileStdin();
116 fp
= cupsFileOpen(filename
, "r");
129 last_page_number
= 0;
132 saw_begin_prolog
= 0;
134 saw_bounding_box
= 0;
135 saw_end_comments
= 0;
146 printf("%s: ", filename
);
149 while ((bytes
= cupsFileGetLine(fp
, line
, sizeof(line
))) > 0)
158 _cupsLangPuts(stdout
, _("FAIL"));
161 _cupsLangPrintf(stdout
,
162 _(" Line %d is longer than 255 characters (%d).\n"
163 " REF: Page 25, Line Length"),
164 linenum
, (int)bytes
);
172 if (strncmp(line
, "%!PS-Adobe-", 11))
175 _cupsLangPuts(stdout
, _("FAIL"));
177 _cupsLangPuts(stdout
,
178 _(" Missing %!PS-Adobe-3.0 on first line.\n"
179 " REF: Page 17, 3.1 Conforming Documents"));
184 version
= (float)atof(line
+ 11);
188 if (!strncmp(line
, "%%BeginDocument:", 16))
190 else if (!strncmp(line
, "%%EndDocument", 13))
193 else if (saw_trailer
)
195 if (!strncmp(line
, "%%Pages:", 8))
197 if (atoi(line
+ 8) <= 0)
200 _cupsLangPuts(stdout
, _("FAIL"));
203 _cupsLangPrintf(stdout
,
204 _(" Bad %%%%Pages: on line %d.\n"
205 " REF: Page 43, %%%%Pages:"),
211 else if (!strncmp(line
, "%%BoundingBox:", 14))
213 if (sscanf(line
+ 14, "%d%d%d%d", lbrt
+ 0, lbrt
+ 1, lbrt
+ 2,
217 _cupsLangPuts(stdout
, _("FAIL"));
220 _cupsLangPrintf(stdout
, _(" Bad %%%%BoundingBox: on line %d.\n"
221 " REF: Page 39, %%%%BoundingBox:"),
225 saw_bounding_box
= 1;
228 else if (!saw_end_comments
)
230 if (!strncmp(line
, "%%EndComments", 13))
231 saw_end_comments
= 1;
232 else if (line
[0] != '%')
233 saw_end_comments
= -1;
234 else if (!strncmp(line
, "%%Pages:", 8))
236 if (strstr(line
+ 8, "(atend)"))
238 else if (atoi(line
+ 8) <= 0)
241 _cupsLangPuts(stdout
, _("FAIL"));
244 _cupsLangPrintf(stdout
, _(" Bad %%%%Pages: on line %d.\n"
245 " REF: Page 43, %%%%Pages:"),
251 else if (!strncmp(line
, "%%BoundingBox:", 14))
253 if (strstr(line
, "(atend)"))
254 saw_bounding_box
= -1;
255 else if (sscanf(line
+ 14, "%d%d%d%d", lbrt
+ 0, lbrt
+ 1, lbrt
+ 2,
259 _cupsLangPuts(stdout
, _("FAIL"));
262 _cupsLangPrintf(stdout
, _(" Bad %%%%BoundingBox: on line %d.\n"
263 " REF: Page 39, %%%%BoundingBox:"),
267 saw_bounding_box
= 1;
270 else if (saw_begin_prolog
&& !saw_end_prolog
)
272 if (!strncmp(line
, "%%EndProlog", 11))
275 else if (saw_begin_setup
&& !saw_end_setup
)
277 if (!strncmp(line
, "%%EndSetup", 10))
280 else if (saw_end_comments
)
282 if (!strncmp(line
, "%%Page:", 7))
284 if (sscanf(line
+ 7, "%255s%d", page_label
, &page_number
) != 2 ||
285 page_number
!= (last_page_number
+ 1) || page_number
< 1)
288 _cupsLangPuts(stdout
, _("FAIL"));
291 _cupsLangPrintf(stdout
, _(" Bad %%%%Page: on line %d.\n"
292 " REF: Page 53, %%%%Page:"),
297 last_page_number
= page_number
;
301 else if (!strncmp(line
, "%%BeginProlog", 13))
302 saw_begin_prolog
= 1;
303 else if (!strncmp(line
, "%%BeginSetup", 12))
305 else if (!strncmp(line
, "%%BeginDocument:", 16))
307 else if (!strncmp(line
, "%%EndDocument", 13))
309 else if (!strncmp(line
, "%%Trailer", 9))
313 for (i
= 0; !binary
&& i
< (int)bytes
; i
++)
317 if ((ch
< ' ' || (ch
& 0x80)) && ch
!= '\n' && ch
!= '\r' && ch
!= '\t')
322 if (saw_bounding_box
<= 0)
325 _cupsLangPuts(stdout
, _("FAIL"));
328 _cupsLangPuts(stdout
, _(" Missing or bad %%BoundingBox: comment.\n"
329 " REF: Page 39, %%BoundingBox:"));
335 _cupsLangPuts(stdout
, _("FAIL"));
338 _cupsLangPuts(stdout
, _(" Missing or bad %%Pages: comment.\n"
339 " REF: Page 43, %%Pages:"));
342 if (!saw_end_comments
)
345 _cupsLangPuts(stdout
, _("FAIL"));
348 _cupsLangPuts(stdout
, _(" Missing %%EndComments comment."
349 " REF: Page 41, %%EndComments"));
355 _cupsLangPuts(stdout
, _("FAIL"));
358 _cupsLangPuts(stdout
, _(" Missing or bad %%Page: comments.\n"
359 " REF: Page 53, %%Page:"));
365 _cupsLangPuts(stdout
, _("FAIL"));
368 _cupsLangPuts(stdout
, _(" Too many %%EndDocument comments."));
373 _cupsLangPuts(stdout
, _("FAIL"));
376 _cupsLangPuts(stdout
, _(" Too many %%BeginDocument comments."));
379 if (saw_long_line
> 1)
380 _cupsLangPrintf(stderr
,
381 _(" Saw %d lines that exceeded 255 characters."),
385 _cupsLangPuts(stdout
, _("PASS"));
388 _cupsLangPuts(stdout
, _(" Warning: file contains binary data."));
391 _cupsLangPrintf(stdout
,
392 _(" Warning: obsolete DSC version %.1f in file."),
395 if (saw_end_comments
< 0)
396 _cupsLangPuts(stdout
, _(" Warning: no %%EndComments comment in file."));
405 * 'usage()' - Show program usage.
411 _cupsLangPuts(stdout
, _("Usage: cupstestdsc [options] filename.ps [... "
413 _cupsLangPuts(stdout
, _(" cupstestdsc [options] -"));
414 _cupsLangPuts(stdout
, "");
415 _cupsLangPuts(stdout
, _("Options:"));
416 _cupsLangPuts(stdout
, "");
417 _cupsLangPuts(stdout
, _(" -h Show program usage"));
418 _cupsLangPuts(stdout
, "");
419 _cupsLangPuts(stdout
, _("Note: this program only validates the DSC comments, "
420 "not the PostScript itself."));