]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testppd.c
Merge changes from CUPS 1.4svn-r7282.
[thirdparty/cups.git] / cups / testppd.c
1 /*
2 * "$Id: testppd.c 6936 2007-09-10 18:15:36Z mike $"
3 *
4 * PPD test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Main entry.
20 */
21
22 /*
23 * Include necessary headers...
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <cups/string.h>
29 #include <errno.h>
30 #include "ppd.h"
31 #ifdef WIN32
32 # include <io.h>
33 #else
34 # include <unistd.h>
35 # include <fcntl.h>
36 #endif /* WIN32 */
37
38
39 /*
40 * Test data...
41 */
42
43 static const char *default_code =
44 "[{\n"
45 "%%BeginFeature: *PageRegion Letter\n"
46 "PageRegion=Letter\n"
47 "%%EndFeature\n"
48 "} stopped cleartomark\n"
49 "[{\n"
50 "%%BeginFeature: *InputSlot Tray\n"
51 "InputSlot=Tray\n"
52 "%%EndFeature\n"
53 "} stopped cleartomark\n"
54 "[{\n"
55 "%%BeginFeature: *IntOption None\n"
56 "%%EndFeature\n"
57 "} stopped cleartomark\n"
58 "[{\n"
59 "%%BeginFeature: *StringOption None\n"
60 "%%EndFeature\n"
61 "} stopped cleartomark\n";
62
63 static const char *custom_code =
64 "[{\n"
65 "%%BeginFeature: *InputSlot Tray\n"
66 "InputSlot=Tray\n"
67 "%%EndFeature\n"
68 "} stopped cleartomark\n"
69 "[{\n"
70 "%%BeginFeature: *IntOption None\n"
71 "%%EndFeature\n"
72 "} stopped cleartomark\n"
73 "[{\n"
74 "%%BeginFeature: *StringOption None\n"
75 "%%EndFeature\n"
76 "} stopped cleartomark\n"
77 "[{\n"
78 "%%BeginFeature: *CustomPageSize True\n"
79 "400\n"
80 "500\n"
81 "0\n"
82 "0\n"
83 "0\n"
84 "PageSize=Custom\n"
85 "%%EndFeature\n"
86 "} stopped cleartomark\n";
87
88
89 /*
90 * 'main()' - Main entry.
91 */
92
93 int /* O - Exit status */
94 main(int argc, /* I - Number of command-line arguments */
95 char *argv[]) /* I - Command-line arguments */
96 {
97 ppd_file_t *ppd; /* PPD file loaded from disk */
98 int status; /* Status of tests (0 = success, 1 = fail) */
99 int conflicts; /* Number of conflicts */
100 char *s; /* String */
101 char buffer[8192]; /* String buffer */
102 const char *text; /* Localized text */
103
104
105 status = 0;
106
107 if (argc == 1)
108 {
109 fputs("ppdOpenFile: ", stdout);
110
111 if ((ppd = ppdOpenFile("test.ppd")) != NULL)
112 puts("PASS");
113 else
114 {
115 ppd_status_t err; /* Last error in file */
116 int line; /* Line number in file */
117
118
119 status ++;
120 err = ppdLastError(&line);
121
122 printf("FAIL (%s on line %d)\n", ppdErrorString(err), line);
123 }
124
125 fputs("ppdMarkDefaults: ", stdout);
126 ppdMarkDefaults(ppd);
127
128 if ((conflicts = ppdConflicts(ppd)) == 0)
129 puts("PASS");
130 else
131 {
132 status ++;
133 printf("FAIL (%d conflicts)\n", conflicts);
134 }
135
136 fputs("ppdEmitString (defaults): ", stdout);
137 if ((s = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL &&
138 !strcmp(s, default_code))
139 puts("PASS");
140 else
141 {
142 status ++;
143 printf("FAIL (%d bytes instead of %d)\n", s ? (int)strlen(s) : 0,
144 (int)strlen(default_code));
145
146 if (s)
147 puts(s);
148 }
149
150 if (s)
151 free(s);
152
153 fputs("ppdEmitString (custom size): ", stdout);
154 ppdMarkOption(ppd, "PageSize", "Custom.400x500");
155
156 if ((s = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL &&
157 !strcmp(s, custom_code))
158 puts("PASS");
159 else
160 {
161 status ++;
162 printf("FAIL (%d bytes instead of %d)\n", s ? (int)strlen(s) : 0,
163 (int)strlen(custom_code));
164
165 if (s)
166 puts(s);
167 }
168
169 if (s)
170 free(s);
171
172 /*
173 * Test localization...
174 */
175
176 fputs("ppdLocalizeIPPReason(text): ", stdout);
177 if (ppdLocalizeIPPReason(ppd, "foo", NULL, buffer, sizeof(buffer)) &&
178 !strcmp(buffer, "Foo Reason"))
179 puts("PASS");
180 else
181 {
182 status ++;
183 printf("FAIL (\"%s\" instead of \"Foo Reason\")\n", buffer);
184 }
185
186 fputs("ppdLocalizeIPPReason(http): ", stdout);
187 if (ppdLocalizeIPPReason(ppd, "foo", "http", buffer, sizeof(buffer)) &&
188 !strcmp(buffer, "http://foo/bar.html"))
189 puts("PASS");
190 else
191 {
192 status ++;
193 printf("FAIL (\"%s\" instead of \"http://foo/bar.html\")\n", buffer);
194 }
195
196 fputs("ppdLocalizeIPPReason(help): ", stdout);
197 if (ppdLocalizeIPPReason(ppd, "foo", "help", buffer, sizeof(buffer)) &&
198 !strcmp(buffer, "help:anchor='foo'%20bookID=Vendor%20Help"))
199 puts("PASS");
200 else
201 {
202 status ++;
203 printf("FAIL (\"%s\" instead of \"help:anchor='foo'%%20bookID=Vendor%%20Help\")\n", buffer);
204 }
205
206 fputs("ppdLocalizeIPPReason(file): ", stdout);
207 if (ppdLocalizeIPPReason(ppd, "foo", "file", buffer, sizeof(buffer)) &&
208 !strcmp(buffer, "/help/foo/bar.html"))
209 puts("PASS");
210 else
211 {
212 status ++;
213 printf("FAIL (\"%s\" instead of \"/help/foo/bar.html\")\n", buffer);
214 }
215
216 putenv("LANG=fr");
217
218 fputs("ppdLocalizeIPPReason(fr text): ", stdout);
219 if (ppdLocalizeIPPReason(ppd, "foo", NULL, buffer, sizeof(buffer)) &&
220 !strcmp(buffer, "La Long Foo Reason"))
221 puts("PASS");
222 else
223 {
224 status ++;
225 printf("FAIL (\"%s\" instead of \"La Long Foo Reason\")\n", buffer);
226 }
227
228 putenv("LANG=zh_TW");
229
230 fputs("ppdLocalizeIPPReason(zh_TW text): ", stdout);
231 if (ppdLocalizeIPPReason(ppd, "foo", NULL, buffer, sizeof(buffer)) &&
232 !strcmp(buffer, "Number 1 Foo Reason"))
233 puts("PASS");
234 else
235 {
236 status ++;
237 printf("FAIL (\"%s\" instead of \"Number 1 Foo Reason\")\n", buffer);
238 }
239
240 /*
241 * cupsMarkerName localization...
242 */
243
244 putenv("LANG=en");
245
246 fputs("ppdLocalizeMarkerName(bogus): ", stdout);
247
248 if ((text = ppdLocalizeMarkerName(ppd, "bogus")) != NULL)
249 {
250 status ++;
251 printf("FAIL (\"%s\" instead of NULL)\n", text);
252 }
253 else
254 puts("PASS");
255
256 fputs("ppdLocalizeMarkerName(cyan): ", stdout);
257
258 if ((text = ppdLocalizeMarkerName(ppd, "cyan")) != NULL &&
259 !strcmp(text, "Cyan Toner"))
260 puts("PASS");
261 else
262 {
263 status ++;
264 printf("FAIL (\"%s\" instead of \"Cyan Toner\")\n",
265 text ? text : "(null)");
266 }
267
268 putenv("LANG=fr");
269
270 fputs("ppdLocalizeMarkerName(fr cyan): ", stdout);
271 if ((text = ppdLocalizeMarkerName(ppd, "cyan")) != NULL &&
272 !strcmp(text, "La Toner Cyan"))
273 puts("PASS");
274 else
275 {
276 status ++;
277 printf("FAIL (\"%s\" instead of \"La Toner Cyan\")\n",
278 text ? text : "(null)");
279 }
280
281 putenv("LANG=zh_TW");
282
283 fputs("ppdLocalizeMarkerName(zh_TW cyan): ", stdout);
284 if ((text = ppdLocalizeMarkerName(ppd, "cyan")) != NULL &&
285 !strcmp(text, "Number 1 Cyan Toner"))
286 puts("PASS");
287 else
288 {
289 status ++;
290 printf("FAIL (\"%s\" instead of \"Number 1 Cyan Toner\")\n",
291 text ? text : "(null)");
292 }
293 }
294 else
295 {
296 if ((ppd = ppdOpenFile(argv[1])) == NULL)
297 {
298 ppd_status_t err; /* Last error in file */
299 int line; /* Line number in file */
300
301
302 status ++;
303 err = ppdLastError(&line);
304
305 printf("%s: %s on line %d\n", argv[1], ppdErrorString(err), line);
306 }
307 else
308 {
309 int i, j, k; /* Looping vars */
310 ppd_attr_t *attr; /* Current attribute */
311 ppd_group_t *group; /* Option group */
312 ppd_option_t *option; /* Option */
313 ppd_coption_t *coption; /* Custom option */
314 ppd_cparam_t *cparam; /* Custom parameter */
315 char lang[255]; /* LANG environment variable */
316
317
318 if (argc > 2)
319 {
320 snprintf(lang, sizeof(lang), "LANG=%s", argv[2]);
321 putenv(lang);
322 }
323
324 ppdLocalize(ppd);
325
326 for (i = ppd->num_groups, group = ppd->groups;
327 i > 0;
328 i --, group ++)
329 {
330 printf("%s (%s):\n", group->name, group->text);
331
332 for (j = group->num_options, option = group->options;
333 j > 0;
334 j --, option ++)
335 {
336 printf(" %s (%s):\n", option->keyword, option->text);
337
338 for (k = 0; k < option->num_choices; k ++)
339 printf(" - %s (%s)\n", option->choices[k].choice,
340 option->choices[k].text);
341
342 if ((coption = ppdFindCustomOption(ppd, option->keyword)) != NULL)
343 {
344 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
345 cparam;
346 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
347 {
348 switch (cparam->type)
349 {
350 case PPD_CUSTOM_CURVE :
351 printf(" %s(%s): PPD_CUSTOM_CURVE (%g to %g)\n",
352 cparam->name, cparam->text,
353 cparam->minimum.custom_curve,
354 cparam->maximum.custom_curve);
355 break;
356
357 case PPD_CUSTOM_INT :
358 printf(" %s(%s): PPD_CUSTOM_INT (%d to %d)\n",
359 cparam->name, cparam->text,
360 cparam->minimum.custom_int,
361 cparam->maximum.custom_int);
362 break;
363
364 case PPD_CUSTOM_INVCURVE :
365 printf(" %s(%s): PPD_CUSTOM_INVCURVE (%g to %g)\n",
366 cparam->name, cparam->text,
367 cparam->minimum.custom_invcurve,
368 cparam->maximum.custom_invcurve);
369 break;
370
371 case PPD_CUSTOM_PASSCODE :
372 printf(" %s(%s): PPD_CUSTOM_PASSCODE (%d to %d)\n",
373 cparam->name, cparam->text,
374 cparam->minimum.custom_passcode,
375 cparam->maximum.custom_passcode);
376 break;
377
378 case PPD_CUSTOM_PASSWORD :
379 printf(" %s(%s): PPD_CUSTOM_PASSWORD (%d to %d)\n",
380 cparam->name, cparam->text,
381 cparam->minimum.custom_password,
382 cparam->maximum.custom_password);
383 break;
384
385 case PPD_CUSTOM_POINTS :
386 printf(" %s(%s): PPD_CUSTOM_POINTS (%g to %g)\n",
387 cparam->name, cparam->text,
388 cparam->minimum.custom_points,
389 cparam->maximum.custom_points);
390 break;
391
392 case PPD_CUSTOM_REAL :
393 printf(" %s(%s): PPD_CUSTOM_REAL (%g to %g)\n",
394 cparam->name, cparam->text,
395 cparam->minimum.custom_real,
396 cparam->maximum.custom_real);
397 break;
398
399 case PPD_CUSTOM_STRING :
400 printf(" %s(%s): PPD_CUSTOM_STRING (%d to %d)\n",
401 cparam->name, cparam->text,
402 cparam->minimum.custom_string,
403 cparam->maximum.custom_string);
404 break;
405 }
406 }
407 }
408 }
409 }
410
411 puts("Attributes:");
412
413 for (attr = (ppd_attr_t *)cupsArrayFirst(ppd->sorted_attrs);
414 attr;
415 attr = (ppd_attr_t *)cupsArrayNext(ppd->sorted_attrs))
416 printf(" *%s %s/%s: \"%s\"\n", attr->name, attr->spec,
417 attr->text, attr->value ? attr->value : "");
418 }
419 }
420
421 #ifdef __APPLE__
422 if (getenv("MallocStackLogging") && getenv("MallocStackLoggingNoCompact"))
423 {
424 char command[1024]; /* malloc_history command */
425
426 snprintf(command, sizeof(command), "malloc_history %d -all_by_size",
427 getpid());
428 fflush(stdout);
429 system(command);
430 }
431 #endif /* __APPLE__ */
432
433 ppdClose(ppd);
434
435 return (status);
436 }
437
438
439 /*
440 * End of "$Id: testppd.c 6936 2007-09-10 18:15:36Z mike $".
441 */