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