1 /*
2 * PPD constraint test program for CUPS.
3 *
4 * Copyright 2008-2012 by Apple Inc.
5 *
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
7 */
8
9 /*
10 * Include necessary headers...
11 */
12
13 #include "cups.h"
14 #include "ppd.h"
15 #include "string-private.h"
16
17
18 /*
19 * 'main()' - Main entry.
20 */
21
22 int /* O - Exit status */
main(int argc,char * argv[])23 main(int argc, /* I - Number of command-line arguments */
24 char *argv[]) /* I - Command-line arguments */
25 {
26 int i; /* Looping var */
27 ppd_file_t *ppd; /* PPD file loaded from disk */
28 char line[256], /* Input buffer */
29 *ptr, /* Pointer into buffer */
30 *optr, /* Pointer to first option name */
31 *cptr; /* Pointer to first choice */
32 int num_options; /* Number of options */
33 cups_option_t *options; /* Options */
34 char *option, /* Current option */
35 *choice; /* Current choice */
36
37
38 if (argc != 2)
39 {
40 puts("Usage: testconflicts filename.ppd");
41 return (1);
42 }
43
44 if ((ppd = ppdOpenFile(argv[1])) == NULL)
45 {
46 ppd_status_t err; /* Last error in file */
47 int linenum; /* Line number in file */
48
49 err = ppdLastError(&linenum);
50
51 printf("Unable to open PPD file \"%s\": %s on line %d\n", argv[1],
52 ppdErrorString(err), linenum);
53 return (1);
54 }
55
56 ppdMarkDefaults(ppd);
57
58 option = NULL;
59 choice = NULL;
60
61 for (;;)
62 {
63 num_options = 0;
64 options = NULL;
65
66 if (!cupsResolveConflicts(ppd, option, choice, &num_options, &options))
67 puts("Unable to resolve conflicts!");
68 else if ((!option && num_options > 0) || (option && num_options > 1))
69 {
70 fputs("Resolved conflicts with the following options:\n ", stdout);
71 for (i = 0; i < num_options; i ++)
72 if (!option || _cups_strcasecmp(option, options[i].name))
73 printf(" %s=%s", options[i].name, options[i].value);
74 putchar('\n');
75
76 cupsFreeOptions(num_options, options);
77 }
78
79 if (option)
80 {
81 free(option);
82 option = NULL;
83 }
84
85 if (choice)
86 {
87 free(choice);
88 choice = NULL;
89 }
90
91 printf("\nNew Option(s): ");
92 fflush(stdout);
93 if (!fgets(line, sizeof(line), stdin) || line[0] == '\n')
94 break;
95
96 for (ptr = line; isspace(*ptr & 255); ptr ++);
97 for (optr = ptr; *ptr && *ptr != '='; ptr ++);
98 if (!*ptr)
99 break;
100 for (*ptr++ = '\0', cptr = ptr; *ptr && !isspace(*ptr & 255); ptr ++);
101 if (!*ptr)
102 break;
103 *ptr++ = '\0';
104
105 option = strdup(optr);
106 choice = strdup(cptr);
107 num_options = cupsParseOptions(ptr, 0, &options);
108
109 ppdMarkOption(ppd, option, choice);
110 if (cupsMarkOptions(ppd, num_options, options))
111 puts("Options Conflict!");
112 cupsFreeOptions(num_options, options);
113 }
114
115 if (option)
116 free(option);
117 if (choice)
118 free(choice);
119
120 return (0);
121 }
122