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