]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/mark.c
6577c0bfcba8d1d5769894a535d00f1edc9b4cf1
[thirdparty/cups.git] / cups / mark.c
1 /*
2 * "$Id: mark.c 5700 2006-06-26 19:20:39Z mike $"
3 *
4 * Option marking routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * PostScript is a trademark of Adobe Systems, Inc.
25 *
26 * This file is subject to the Apple OS-Developed Software exception.
27 *
28 * Contents:
29 *
30 * ppdConflicts() - Check to see if there are any conflicts.
31 * ppdFindChoice() - Return a pointer to an option choice.
32 * ppdFindMarkedChoice() - Return the marked choice for the specified option.
33 * ppdFindOption() - Return a pointer to the specified option.
34 * ppdFirstOption() - Return the first option in the PPD file.
35 * ppdNextOption() - Return the next option in the PPD file.
36 * ppdIsMarked() - Check to see if an option is marked...
37 * ppdMarkDefaults() - Mark all default options in the PPD file.
38 * ppdMarkOption() - Mark an option in a PPD file.
39 * ppd_defaults() - Set the defaults for this group and all sub-groups.
40 */
41
42 /*
43 * Include necessary headers...
44 */
45
46 #include "cups.h"
47 #include "string.h"
48 #include "debug.h"
49
50
51 /*
52 * Local functions...
53 */
54
55 static void ppd_defaults(ppd_file_t *ppd, ppd_group_t *g);
56
57
58 /*
59 * 'ppdConflicts()' - Check to see if there are any conflicts.
60 */
61
62 int /* O - Number of conflicts found */
63 ppdConflicts(ppd_file_t *ppd) /* I - PPD to check */
64 {
65 int i, j, k, /* Looping variables */
66 conflicts; /* Number of conflicts */
67 ppd_const_t *c; /* Current constraint */
68 ppd_group_t *g, *sg; /* Groups */
69 ppd_option_t *o1, *o2; /* Options */
70 ppd_choice_t *c1, *c2; /* Choices */
71
72
73 if (ppd == NULL)
74 return (0);
75
76 /*
77 * Clear all conflicts...
78 */
79
80 conflicts = 0;
81
82 for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++)
83 {
84 for (j = g->num_options, o1 = g->options; j > 0; j --, o1 ++)
85 o1->conflicted = 0;
86
87 for (j = g->num_subgroups, sg = g->subgroups; j > 0; j --, sg ++)
88 for (k = sg->num_options, o1 = sg->options; k > 0; k --, o1 ++)
89 o1->conflicted = 0;
90 }
91
92 /*
93 * Loop through all of the UI constraints and flag any options
94 * that conflict...
95 */
96
97 for (i = ppd->num_consts, c = ppd->consts; i > 0; i --, c ++)
98 {
99 /*
100 * Grab pointers to the first option...
101 */
102
103 o1 = ppdFindOption(ppd, c->option1);
104
105 if (o1 == NULL)
106 continue;
107 else if (c->choice1[0] != '\0')
108 {
109 /*
110 * This constraint maps to a specific choice.
111 */
112
113 c1 = ppdFindChoice(o1, c->choice1);
114 }
115 else
116 {
117 /*
118 * This constraint applies to any choice for this option.
119 */
120
121 for (j = o1->num_choices, c1 = o1->choices; j > 0; j --, c1 ++)
122 if (c1->marked)
123 break;
124
125 if (j == 0 ||
126 strcasecmp(c1->choice, "None") == 0 ||
127 strcasecmp(c1->choice, "Off") == 0 ||
128 strcasecmp(c1->choice, "False") == 0)
129 c1 = NULL;
130 }
131
132 /*
133 * Grab pointers to the second option...
134 */
135
136 o2 = ppdFindOption(ppd, c->option2);
137
138 if (o2 == NULL)
139 continue;
140 else if (c->choice2[0] != '\0')
141 {
142 /*
143 * This constraint maps to a specific choice.
144 */
145
146 c2 = ppdFindChoice(o2, c->choice2);
147 }
148 else
149 {
150 /*
151 * This constraint applies to any choice for this option.
152 */
153
154 for (j = o2->num_choices, c2 = o2->choices; j > 0; j --, c2 ++)
155 if (c2->marked)
156 break;
157
158 if (j == 0 ||
159 strcasecmp(c2->choice, "None") == 0 ||
160 strcasecmp(c2->choice, "Off") == 0 ||
161 strcasecmp(c2->choice, "False") == 0)
162 c2 = NULL;
163 }
164
165 /*
166 * If both options are marked then there is a conflict...
167 */
168
169 if (c1 != NULL && c1->marked &&
170 c2 != NULL && c2->marked)
171 {
172 DEBUG_printf(("%s->%s conflicts with %s->%s (%s %s %s %s)\n",
173 o1->keyword, c1->choice, o2->keyword, c2->choice,
174 c->option1, c->choice1, c->option2, c->choice2));
175 conflicts ++;
176 o1->conflicted = 1;
177 o2->conflicted = 1;
178 }
179 }
180
181 /*
182 * Return the number of conflicts found...
183 */
184
185 return (conflicts);
186 }
187
188
189 /*
190 * 'ppdFindChoice()' - Return a pointer to an option choice.
191 */
192
193 ppd_choice_t * /* O - Choice pointer or NULL */
194 ppdFindChoice(ppd_option_t *o, /* I - Pointer to option */
195 const char *choice) /* I - Name of choice */
196 {
197 int i; /* Looping var */
198 ppd_choice_t *c; /* Current choice */
199
200
201 if (o == NULL || choice == NULL)
202 return (NULL);
203
204 for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
205 if (strcasecmp(c->choice, choice) == 0)
206 return (c);
207
208 return (NULL);
209 }
210
211
212 /*
213 * 'ppdFindMarkedChoice()' - Return the marked choice for the specified option.
214 */
215
216 ppd_choice_t * /* O - Pointer to choice or NULL */
217 ppdFindMarkedChoice(ppd_file_t *ppd, /* I - PPD file */
218 const char *option) /* I - Keyword/option name */
219 {
220 int i; /* Looping var */
221 ppd_option_t *o; /* Pointer to option */
222 ppd_choice_t *c; /* Pointer to choice */
223
224
225 if ((o = ppdFindOption(ppd, option)) == NULL)
226 return (NULL);
227
228 for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
229 if (c->marked)
230 return (c);
231
232 return (NULL);
233 }
234
235
236 /*
237 * 'ppdFindOption()' - Return a pointer to the specified option.
238 */
239
240 ppd_option_t * /* O - Pointer to option or NULL */
241 ppdFindOption(ppd_file_t *ppd, /* I - PPD file data */
242 const char *option) /* I - Option/Keyword name */
243 {
244 /*
245 * Range check input...
246 */
247
248 if (!ppd || !option)
249 return (NULL);
250
251 if (ppd->options)
252 {
253 /*
254 * Search in the array...
255 */
256
257 ppd_option_t key; /* Option search key */
258
259
260 strlcpy(key.keyword, option, sizeof(key.keyword));
261
262 return ((ppd_option_t *)cupsArrayFind(ppd->options, &key));
263 }
264 else
265 {
266 /*
267 * Search in each group...
268 */
269
270 int i, j; /* Looping vars */
271 ppd_group_t *group; /* Current group */
272 ppd_option_t *optptr; /* Current option */
273
274
275 for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
276 for (j = group->num_options, optptr = group->options;
277 j > 0;
278 j --, optptr ++)
279 if (!strcasecmp(optptr->keyword, option))
280 return (optptr);
281
282 return (NULL);
283 }
284 }
285
286
287 /*
288 * 'ppdIsMarked()' - Check to see if an option is marked...
289 */
290
291 int /* O - Non-zero if option is marked */
292 ppdIsMarked(ppd_file_t *ppd, /* I - PPD file data */
293 const char *option, /* I - Option/Keyword name */
294 const char *choice) /* I - Choice name */
295 {
296 ppd_option_t *o; /* Option pointer */
297 ppd_choice_t *c; /* Choice pointer */
298
299
300 if (ppd == NULL)
301 return (0);
302
303 if ((o = ppdFindOption(ppd, option)) == NULL)
304 return (0);
305
306 if ((c = ppdFindChoice(o, choice)) == NULL)
307 return (0);
308
309 return (c->marked);
310 }
311
312
313 /*
314 * 'ppdMarkDefaults()' - Mark all default options in the PPD file.
315 */
316
317 void
318 ppdMarkDefaults(ppd_file_t *ppd)/* I - PPD file record */
319 {
320 int i; /* Looping variables */
321 ppd_group_t *g; /* Current group */
322
323
324 if (ppd == NULL)
325 return;
326
327 for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++)
328 ppd_defaults(ppd, g);
329 }
330
331
332 /*
333 * 'ppdMarkOption()' - Mark an option in a PPD file.
334 *
335 * Notes:
336 *
337 * -1 is returned if the given option would conflict with any currently
338 * selected option.
339 */
340
341 int /* O - Number of conflicts */
342 ppdMarkOption(ppd_file_t *ppd, /* I - PPD file record */
343 const char *option, /* I - Keyword */
344 const char *choice) /* I - Option name */
345 {
346 int i, j; /* Looping vars */
347 ppd_option_t *o; /* Option pointer */
348 ppd_choice_t *c; /* Choice pointer */
349 struct lconv *loc; /* Locale data */
350
351
352 DEBUG_printf(("ppdMarkOption(ppd=%p, option=\"%s\", choice=\"%s\")\n",
353 ppd, option, choice));
354
355 /*
356 * Range check input...
357 */
358
359 if (!ppd || !option || !choice)
360 return (0);
361
362 /*
363 * AP_D_InputSlot is the "default input slot" on MacOS X, and setting
364 * it clears the regular InputSlot choices...
365 */
366
367 if (!strcasecmp(option, "AP_D_InputSlot"))
368 {
369 if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
370 for (i = 0; i < o->num_choices; i ++)
371 o->choices[i].marked = 0;
372 }
373
374 /*
375 * Check for custom options...
376 */
377
378 if ((o = ppdFindOption(ppd, option)) == NULL)
379 return (0);
380
381 loc = localeconv();
382
383 if (!strncasecmp(choice, "Custom.", 7))
384 {
385 /*
386 * Handle a custom option...
387 */
388
389 if ((c = ppdFindChoice(o, "Custom")) == NULL)
390 return (0);
391
392 if (!strcasecmp(option, "PageSize"))
393 {
394 /*
395 * Handle custom page sizes...
396 */
397
398 ppdPageSize(ppd, choice);
399 }
400 else
401 {
402 /*
403 * Handle other custom options...
404 */
405
406 ppd_coption_t *coption; /* Custom option */
407 ppd_cparam_t *cparam; /* Custom parameter */
408 char *units; /* Custom points units */
409
410
411 if ((coption = ppdFindCustomOption(ppd, option)) != NULL)
412 {
413 if ((cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params)) == NULL)
414 return (0);
415
416 switch (cparam->type)
417 {
418 case PPD_CUSTOM_CURVE :
419 case PPD_CUSTOM_INVCURVE :
420 case PPD_CUSTOM_REAL :
421 cparam->current.custom_real = _cupsStrScand(choice + 7, NULL,
422 loc);
423 break;
424
425 case PPD_CUSTOM_POINTS :
426 cparam->current.custom_points = _cupsStrScand(choice + 7,
427 &units, loc);
428
429 if (units)
430 {
431 if (!strcasecmp(units, "cm"))
432 cparam->current.custom_points *= 72.0 / 2.54;
433 else if (!strcasecmp(units, "mm"))
434 cparam->current.custom_points *= 72.0 / 25.4;
435 else if (!strcasecmp(units, "m"))
436 cparam->current.custom_points *= 72.0 / 0.0254;
437 else if (!strcasecmp(units, "in"))
438 cparam->current.custom_points *= 72.0;
439 else if (!strcasecmp(units, "ft"))
440 cparam->current.custom_points *= 12 * 72.0;
441 }
442 break;
443
444 case PPD_CUSTOM_INT :
445 cparam->current.custom_int = atoi(choice + 7);
446 break;
447
448 case PPD_CUSTOM_PASSCODE :
449 case PPD_CUSTOM_PASSWORD :
450 case PPD_CUSTOM_STRING :
451 if (cparam->current.custom_string)
452 free(cparam->current.custom_string);
453
454 cparam->current.custom_string = strdup(choice + 7);
455 break;
456 }
457 }
458 }
459
460 /*
461 * Make sure that we keep the option marked below...
462 */
463
464 choice = "Custom";
465 }
466 else if (choice[0] == '{')
467 {
468 /*
469 * Handle multi-value custom options...
470 */
471
472 ppd_coption_t *coption; /* Custom option */
473 ppd_cparam_t *cparam; /* Custom parameter */
474 char *units; /* Custom points units */
475 int num_vals; /* Number of values */
476 cups_option_t *vals, /* Values */
477 *val; /* Value */
478
479
480 if ((c = ppdFindChoice(o, "Custom")) == NULL)
481 return (0);
482
483 if ((coption = ppdFindCustomOption(ppd, option)) != NULL)
484 {
485 num_vals = cupsParseOptions(choice + 1, 0, &vals);
486
487 for (i = 0, val = vals; i < num_vals; i ++, val ++)
488 {
489 if ((cparam = ppdFindCustomParam(coption, val->name)) == NULL)
490 continue;
491
492 switch (cparam->type)
493 {
494 case PPD_CUSTOM_CURVE :
495 case PPD_CUSTOM_INVCURVE :
496 case PPD_CUSTOM_REAL :
497 cparam->current.custom_real = _cupsStrScand(val->value, NULL,
498 loc);
499 break;
500
501 case PPD_CUSTOM_POINTS :
502 cparam->current.custom_points = _cupsStrScand(val->value, &units,
503 loc);
504
505 if (units)
506 {
507 if (!strcasecmp(units, "cm"))
508 cparam->current.custom_points *= 72.0 / 2.54;
509 else if (!strcasecmp(units, "mm"))
510 cparam->current.custom_points *= 72.0 / 25.4;
511 else if (!strcasecmp(units, "m"))
512 cparam->current.custom_points *= 72.0 / 0.0254;
513 else if (!strcasecmp(units, "in"))
514 cparam->current.custom_points *= 72.0;
515 else if (!strcasecmp(units, "ft"))
516 cparam->current.custom_points *= 12 * 72.0;
517 }
518 break;
519
520 case PPD_CUSTOM_INT :
521 cparam->current.custom_int = atoi(val->value);
522 break;
523
524 case PPD_CUSTOM_PASSCODE :
525 case PPD_CUSTOM_PASSWORD :
526 case PPD_CUSTOM_STRING :
527 if (cparam->current.custom_string)
528 free(cparam->current.custom_string);
529
530 cparam->current.custom_string = strdup(val->value);
531 break;
532 }
533 }
534
535 cupsFreeOptions(num_vals, vals);
536 }
537 }
538 else
539 {
540 for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
541 if (!strcasecmp(c->choice, choice))
542 break;
543
544 if (!i)
545 return (0);
546 }
547
548 /*
549 * Option found; mark it and then handle unmarking any other options.
550 */
551
552 c->marked = 1;
553
554 if (o->ui != PPD_UI_PICKMANY)
555 {
556 /*
557 * Unmark all other choices...
558 */
559
560 for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
561 if (strcasecmp(c->choice, choice))
562 {
563 c->marked = 0;
564
565 if (!strcasecmp(option, "PageSize") ||
566 !strcasecmp(option, "PageRegion"))
567 {
568 /*
569 * Mark current page size...
570 */
571
572 for (j = 0; j < ppd->num_sizes; j ++)
573 ppd->sizes[j].marked = !strcasecmp(ppd->sizes[j].name,
574 choice);
575
576 /*
577 * Unmark the current PageSize or PageRegion setting, as
578 * appropriate...
579 */
580
581 if (!strcasecmp(option, "PageSize"))
582 {
583 if ((o = ppdFindOption(ppd, "PageRegion")) != NULL)
584 for (j = 0; j < o->num_choices; j ++)
585 o->choices[j].marked = 0;
586 }
587 else
588 {
589 if ((o = ppdFindOption(ppd, "PageSize")) != NULL)
590 for (j = 0; j < o->num_choices; j ++)
591 o->choices[j].marked = 0;
592 }
593 }
594 else if (!strcasecmp(option, "InputSlot"))
595 {
596 /*
597 * Unmark ManualFeed True and possibly mark ManualFeed False
598 * option...
599 */
600
601 if ((o = ppdFindOption(ppd, "ManualFeed")) != NULL)
602 for (j = 0; j < o->num_choices; j ++)
603 o->choices[j].marked = !strcasecmp(o->choices[j].choice, "False");
604 }
605 else if (!strcasecmp(option, "ManualFeed") &&
606 !strcasecmp(choice, "True"))
607 {
608 /*
609 * Unmark InputSlot option...
610 */
611
612 if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
613 for (j = 0; j < o->num_choices; j ++)
614 o->choices[j].marked = 0;
615 }
616 }
617 }
618
619 /*
620 * Return the number of conflicts...
621 */
622
623 return (ppdConflicts(ppd));
624 }
625
626
627 /*
628 * 'ppdFirstOption()' - Return the first option in the PPD file.
629 *
630 * Options are returned from all groups in sorted order.
631 *
632 * @since CUPS 1.2@
633 */
634
635 ppd_option_t * /* O - First option or NULL */
636 ppdFirstOption(ppd_file_t *ppd) /* I - PPD file */
637 {
638 if (!ppd)
639 return (NULL);
640 else
641 return ((ppd_option_t *)cupsArrayFirst(ppd->options));
642 }
643
644
645 /*
646 * 'ppdNextOption()' - Return the next option in the PPD file.
647 *
648 * Options are returned from all groups in sorted order.
649 *
650 * @since CUPS 1.2@
651 */
652
653 ppd_option_t * /* O - Next option or NULL */
654 ppdNextOption(ppd_file_t *ppd) /* I - PPD file */
655 {
656 if (!ppd)
657 return (NULL);
658 else
659 return ((ppd_option_t *)cupsArrayNext(ppd->options));
660 }
661
662
663 /*
664 * 'ppd_defaults()' - Set the defaults for this group and all sub-groups.
665 */
666
667 static void
668 ppd_defaults(ppd_file_t *ppd, /* I - PPD file */
669 ppd_group_t *g) /* I - Group to default */
670 {
671 int i; /* Looping var */
672 ppd_option_t *o; /* Current option */
673 ppd_group_t *sg; /* Current sub-group */
674
675
676 if (g == NULL)
677 return;
678
679 for (i = g->num_options, o = g->options; i > 0; i --, o ++)
680 if (strcasecmp(o->keyword, "PageRegion") != 0)
681 ppdMarkOption(ppd, o->keyword, o->defchoice);
682
683 for (i = g->num_subgroups, sg = g->subgroups; i > 0; i --, sg ++)
684 ppd_defaults(ppd, sg);
685 }
686
687
688 /*
689 * End of "$Id: mark.c 5700 2006-06-26 19:20:39Z mike $".
690 */