2 * "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $"
4 * User-defined destination (and option) support for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsAddDest() - Add a destination to the list of destinations.
20 * cupsFreeDests() - Free the memory used by the list of destinations.
21 * cupsGetDest() - Get the named destination from the list.
22 * cupsGetDests() - Get the list of destinations from the default
24 * cupsGetDests2() - Get the list of destinations from the specified
26 * cupsGetNamedDest() - Get options for the named destination.
27 * cupsRemoveDest() - Remove a destination from the destination list.
28 * cupsSetDefaultDest() - Set the default destination.
29 * cupsSetDests() - Save the list of destinations for the default
31 * cupsSetDests2() - Save the list of destinations for the specified
33 * appleCopyLocations() - Copy the location history array.
34 * appleCopyNetwork() - Get the network ID for the current location.
35 * appleGetPaperSize() - Get the default paper size.
36 * appleGetPrinter() - Get a printer from the history array.
37 * appleSetDefault() - Set the default printer for this location.
38 * appleUseLastPrinter() - Get the default printer preference value.
39 * cups_add_dest() - Add a destination to the array.
40 * cups_compare_dests() - Compare two destinations.
41 * cups_find_dest() - Find a destination using a binary search.
42 * cups_get_default() - Get the default destination from an lpoptions file.
43 * cups_get_dests() - Get destinations from a file.
44 * cups_get_sdests() - Get destinations from a server.
45 * cups_make_string() - Make a comma-separated string of values from an IPP
50 * Include necessary headers...
53 #include "cups-private.h"
58 #endif /* HAVE_NOTIFY_H */
61 # include <sys/cdefs.h>
62 # include <CoreFoundation/CoreFoundation.h>
63 # include <SystemConfiguration/SystemConfiguration.h>
64 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
65 # define kLocationHistoryArrayKey CFSTR("kLocationHistoryArrayKeyTMP")
66 # define kLocationNetworkKey CFSTR("kLocationNetworkKey")
67 # define kLocationPrinterIDKey CFSTR("kLocationPrinterIDKey")
68 # define kPMPrintingPreferences CFSTR("com.apple.print.PrintingPrefs")
69 # define kUseLastPrinterAsCurrentPrinterKey CFSTR("UseLastPrinterAsCurrentPrinter")
70 #endif /* __APPLE__ */
78 static CFArrayRef
appleCopyLocations(void);
79 static CFStringRef
appleCopyNetwork(void);
80 static char *appleGetPaperSize(char *name
, int namesize
);
81 static CFStringRef
appleGetPrinter(CFArrayRef locations
, CFStringRef network
,
83 static void appleSetDefault(const char *name
);
84 static int appleUseLastPrinter(void);
85 #endif /* __APPLE__ */
86 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
87 int *num_dests
, cups_dest_t
**dests
);
88 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
89 static int cups_find_dest(const char *name
, const char *instance
,
90 int num_dests
, cups_dest_t
*dests
, int prev
,
92 static char *cups_get_default(const char *filename
, char *namebuf
,
93 size_t namesize
, const char **instance
);
94 static int cups_get_dests(const char *filename
, const char *match_name
,
95 const char *match_inst
, int user_default_set
,
96 int num_dests
, cups_dest_t
**dests
);
97 static int cups_get_sdests(http_t
*http
, ipp_op_t op
, const char *name
,
98 int num_dests
, cups_dest_t
**dests
);
99 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
104 * 'cupsAddDest()' - Add a destination to the list of destinations.
106 * This function cannot be used to add a new class or printer queue,
107 * it only adds a new container of saved options for the named
108 * destination or instance.
110 * If the named destination already exists, the destination list is
111 * returned unchanged. Adding a new instance of a destination creates
112 * a copy of that destination's options.
114 * Use the @link cupsSaveDests@ function to save the updated list of
115 * destinations to the user's lpoptions file.
118 int /* O - New number of destinations */
119 cupsAddDest(const char *name
, /* I - Destination name */
120 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
121 int num_dests
, /* I - Number of destinations */
122 cups_dest_t
**dests
) /* IO - Destinations */
124 int i
; /* Looping var */
125 cups_dest_t
*dest
; /* Destination pointer */
126 cups_dest_t
*parent
= NULL
; /* Parent destination */
127 cups_option_t
*doption
, /* Current destination option */
128 *poption
; /* Current parent option */
134 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
136 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
139 dest
= cups_add_dest(name
, instance
, &num_dests
, dests
);
142 * Find the base dest again now the array has been realloc'd.
145 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
147 if (instance
&& parent
&& parent
->num_options
> 0)
150 * Copy options from parent...
153 dest
->options
= calloc(sizeof(cups_option_t
), parent
->num_options
);
157 dest
->num_options
= parent
->num_options
;
159 for (i
= dest
->num_options
, doption
= dest
->options
,
160 poption
= parent
->options
;
162 i
--, doption
++, poption
++)
164 doption
->name
= _cupsStrRetain(poption
->name
);
165 doption
->value
= _cupsStrRetain(poption
->value
);
176 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
180 cupsFreeDests(int num_dests
, /* I - Number of destinations */
181 cups_dest_t
*dests
) /* I - Destinations */
183 int i
; /* Looping var */
184 cups_dest_t
*dest
; /* Current destination */
187 if (num_dests
== 0 || dests
== NULL
)
190 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
192 _cupsStrFree(dest
->name
);
193 _cupsStrFree(dest
->instance
);
195 cupsFreeOptions(dest
->num_options
, dest
->options
);
203 * 'cupsGetDest()' - Get the named destination from the list.
205 * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
206 * list of supported destinations for the current user.
209 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
210 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
211 const char *instance
, /* I - Instance name or @code NULL@ */
212 int num_dests
, /* I - Number of destinations */
213 cups_dest_t
*dests
) /* I - Destinations */
215 int diff
, /* Result of comparison */
216 match
; /* Matching index */
219 if (num_dests
<= 0 || !dests
)
225 * NULL name for default printer.
228 while (num_dests
> 0)
230 if (dests
->is_default
)
240 * Lookup name and optionally the instance...
243 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
246 return (dests
+ match
);
254 * 'cupsGetDests()' - Get the list of destinations from the default server.
256 * Starting with CUPS 1.2, the returned list of destinations include the
257 * printer-info, printer-is-accepting-jobs, printer-is-shared,
258 * printer-make-and-model, printer-state, printer-state-change-time,
259 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
260 * adds the marker-change-time, marker-colors, marker-high-levels,
261 * marker-levels, marker-low-levels, marker-message, marker-names,
262 * marker-types, and printer-commands attributes as well.
264 * Use the @link cupsFreeDests@ function to free the destination list and
265 * the @link cupsGetDest@ function to find a particular destination.
268 int /* O - Number of destinations */
269 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
271 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
276 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
278 * Starting with CUPS 1.2, the returned list of destinations include the
279 * printer-info, printer-is-accepting-jobs, printer-is-shared,
280 * printer-make-and-model, printer-state, printer-state-change-time,
281 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
282 * adds the marker-change-time, marker-colors, marker-high-levels,
283 * marker-levels, marker-low-levels, marker-message, marker-names,
284 * marker-types, and printer-commands attributes as well.
286 * Use the @link cupsFreeDests@ function to free the destination list and
287 * the @link cupsGetDest@ function to find a particular destination.
289 * @since CUPS 1.1.21/Mac OS X 10.4@
292 int /* O - Number of destinations */
293 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
294 cups_dest_t
**dests
) /* O - Destinations */
296 int i
; /* Looping var */
297 int num_dests
; /* Number of destinations */
298 cups_dest_t
*dest
; /* Destination pointer */
299 const char *home
; /* HOME environment variable */
300 char filename
[1024]; /* Local ~/.cups/lpoptions file */
301 const char *defprinter
; /* Default printer */
302 char name
[1024], /* Copy of printer name */
303 *instance
, /* Pointer to instance name */
304 *user_default
; /* User default printer */
305 int num_reals
; /* Number of real queues */
306 cups_dest_t
*reals
; /* Real queues */
307 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
311 * Range check the input...
316 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad NULL dests pointer"), 1);
321 * Initialize destination array...
325 *dests
= (cups_dest_t
*)0;
328 * Grab the printers and classes...
331 num_dests
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, NULL
, num_dests
, dests
);
333 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
335 cupsFreeDests(num_dests
, *dests
);
336 *dests
= (cups_dest_t
*)0;
341 * Make a copy of the "real" queues for a later sanity check...
346 num_reals
= num_dests
;
347 reals
= calloc(num_reals
, sizeof(cups_dest_t
));
350 memcpy(reals
, *dests
, num_reals
* sizeof(cups_dest_t
));
361 * Grab the default destination...
364 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
366 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
368 strlcpy(name
, defprinter
, sizeof(name
));
375 * Separate printer and instance name...
378 if ((instance
= strchr(name
, '/')) != NULL
)
382 * Lookup the printer and instance and make it the default...
385 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
386 dest
->is_default
= 1;
392 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
395 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
396 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
399 if ((home
= getenv("HOME")) != NULL
)
401 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
403 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
408 * Validate the current default destination - this prevents old
409 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
410 * pointing to a non-existent printer or class...
416 * See if we have a default printer...
419 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
422 * Have a default; see if it is real...
425 dest
= cupsGetDest(dest
->name
, NULL
, num_reals
, reals
);
429 * If dest is NULL, then no default (that exists) is set, so we
430 * need to set a default if one exists...
433 if (dest
== NULL
&& defprinter
!= NULL
)
435 for (i
= 0; i
< num_dests
; i
++)
436 (*dests
)[i
].is_default
= 0;
438 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
439 dest
->is_default
= 1;
450 * Return the number of destinations...
454 _cupsSetError(IPP_OK
, NULL
, 0);
461 * 'cupsGetNamedDest()' - Get options for the named destination.
463 * This function is optimized for retrieving a single destination and should
464 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
465 * know the name of the destination or want to print to the default destination.
466 * If @code NULL@ is returned, the destination does not exist or there is no
467 * default destination.
469 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
470 * server will be used.
472 * If "name" is @code NULL@, the default printer for the current user will be
475 * The returned destination must be freed using @link cupsFreeDests@ with a
476 * "num_dests" value of 1.
478 * @since CUPS 1.4/Mac OS X 10.6@
481 cups_dest_t
* /* O - Destination or @code NULL@ */
482 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
483 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
484 const char *instance
) /* I - Instance name or @code NULL@ */
486 cups_dest_t
*dest
; /* Destination */
487 char filename
[1024], /* Path to lpoptions */
488 defname
[256]; /* Default printer name */
489 const char *home
= getenv("HOME"); /* Home directory */
490 int set_as_default
= 0; /* Set returned destination as default */
491 ipp_op_t op
= IPP_GET_PRINTER_ATTRIBUTES
;
492 /* IPP operation to get server ops */
493 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
497 * If "name" is NULL, find the default destination...
503 name
= _cupsUserDefault(defname
, sizeof(defname
));
507 char *ptr
; /* Temporary pointer... */
509 if ((ptr
= strchr(defname
, '/')) != NULL
)
520 * No default in the environment, try the user's lpoptions files...
523 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
525 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
531 * Still not there? Try the system lpoptions file...
534 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
535 cg
->cups_serverroot
);
536 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
542 * No locally-set default destination, ask the server...
545 op
= CUPS_GET_DEFAULT
;
550 * Get the printer's attributes...
553 if (!cups_get_sdests(http
, op
, name
, 0, &dest
))
555 if (op
== CUPS_GET_DEFAULT
|| (name
&& !set_as_default
))
559 * The default printer from environment variables or from a
560 * configuration file does not exist. Find out the real default.
563 if (!cups_get_sdests(http
, CUPS_GET_DEFAULT
, NULL
, 0, &dest
))
568 dest
->instance
= _cupsStrAlloc(instance
);
571 dest
->is_default
= 1;
574 * Then add local options...
577 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
578 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
582 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
584 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
588 * Return the result...
596 * 'cupsRemoveDest()' - Remove a destination from the destination list.
598 * Removing a destination/instance does not delete the class or printer
599 * queue, merely the lpoptions for that destination/instance. Use the
600 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
601 * options for the user.
603 * @since CUPS 1.3/Mac OS X 10.5@
606 int /* O - New number of destinations */
607 cupsRemoveDest(const char *name
, /* I - Destination name */
608 const char *instance
, /* I - Instance name or @code NULL@ */
609 int num_dests
, /* I - Number of destinations */
610 cups_dest_t
**dests
) /* IO - Destinations */
612 int i
; /* Index into destinations */
613 cups_dest_t
*dest
; /* Pointer to destination */
617 * Find the destination...
620 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
627 _cupsStrFree(dest
->name
);
628 _cupsStrFree(dest
->instance
);
629 cupsFreeOptions(dest
->num_options
, dest
->options
);
632 * Remove the destination from the array...
640 memmove(dest
, dest
+ 1, (num_dests
- i
) * sizeof(cups_dest_t
));
647 * 'cupsSetDefaultDest()' - Set the default destination.
649 * @since CUPS 1.3/Mac OS X 10.5@
654 const char *name
, /* I - Destination name */
655 const char *instance
, /* I - Instance name or @code NULL@ */
656 int num_dests
, /* I - Number of destinations */
657 cups_dest_t
*dests
) /* I - Destinations */
659 int i
; /* Looping var */
660 cups_dest_t
*dest
; /* Current destination */
664 * Range check input...
667 if (!name
|| num_dests
<= 0 || !dests
)
671 * Loop through the array and set the "is_default" flag for the matching
675 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
676 dest
->is_default
= !strcasecmp(name
, dest
->name
) &&
677 ((!instance
&& !dest
->instance
) ||
678 (instance
&& dest
->instance
&&
679 !strcasecmp(instance
, dest
->instance
)));
684 * 'cupsSetDests()' - Save the list of destinations for the default server.
686 * This function saves the destinations to /etc/cups/lpoptions when run
687 * as root and ~/.cups/lpoptions when run as a normal user.
691 cupsSetDests(int num_dests
, /* I - Number of destinations */
692 cups_dest_t
*dests
) /* I - Destinations */
694 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
699 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
701 * This function saves the destinations to /etc/cups/lpoptions when run
702 * as root and ~/.cups/lpoptions when run as a normal user.
704 * @since CUPS 1.1.21/Mac OS X 10.4@
707 int /* O - 0 on success, -1 on error */
708 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
709 int num_dests
, /* I - Number of destinations */
710 cups_dest_t
*dests
) /* I - Destinations */
712 int i
, j
; /* Looping vars */
713 int wrote
; /* Wrote definition? */
714 cups_dest_t
*dest
; /* Current destination */
715 cups_option_t
*option
; /* Current option */
716 _ipp_option_t
*match
; /* Matching attribute for option */
717 FILE *fp
; /* File pointer */
719 const char *home
; /* HOME environment variable */
721 char filename
[1024]; /* lpoptions file */
722 int num_temps
; /* Number of temporary destinations */
723 cups_dest_t
*temps
, /* Temporary destinations */
724 *temp
; /* Current temporary dest */
725 const char *val
; /* Value of temporary option */
726 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
730 * Range check the input...
733 if (!num_dests
|| !dests
)
737 * Get the server destinations...
740 num_temps
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, NULL
, 0, &temps
);
742 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
744 cupsFreeDests(num_temps
, temps
);
749 * Figure out which file to write to...
752 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
758 * Merge in server defaults...
761 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
764 * Point to user defaults...
767 if ((home
= getenv("HOME")) != NULL
)
770 * Create ~/.cups subdirectory...
773 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
774 if (access(filename
, 0))
775 mkdir(filename
, 0700);
777 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
783 * Try to open the file...
786 if ((fp
= fopen(filename
, "w")) == NULL
)
788 cupsFreeDests(num_temps
, temps
);
794 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
799 fchmod(fileno(fp
), 0644);
803 * Write each printer; each line looks like:
805 * Dest name[/instance] options
806 * Default name[/instance] options
809 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
810 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
812 if (dest
->is_default
)
814 fprintf(fp
, "Default %s", dest
->name
);
816 fprintf(fp
, "/%s", dest
->instance
);
823 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
824 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
826 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
829 * See if this option is a printer attribute; if so, skip it...
832 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
833 match
->group_tag
== IPP_TAG_PRINTER
)
837 * See if the server/global options match these; if so, don't
842 (val
= cupsGetOption(option
->name
, temp
->num_options
,
843 temp
->options
)) != NULL
&&
844 !strcasecmp(val
, option
->value
))
848 * Options don't match, write to the file...
853 fprintf(fp
, "Dest %s", dest
->name
);
855 fprintf(fp
, "/%s", dest
->instance
);
859 if (option
->value
[0])
861 if (strchr(option
->value
, ' ') ||
862 strchr(option
->value
, '\\') ||
863 strchr(option
->value
, '\"') ||
864 strchr(option
->value
, '\''))
870 fprintf(fp
, " %s=\"", option
->name
);
872 for (val
= option
->value
; *val
; val
++)
874 if (strchr("\"\'\\", *val
))
885 * Store the literal value...
888 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
892 fprintf(fp
, " %s", option
->name
);
900 * Free the temporary destinations and close the file...
903 cupsFreeDests(num_temps
, temps
);
909 * Set the default printer for this location - this allows command-line
910 * and GUI applications to share the same default destination...
913 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
914 appleSetDefault(dest
->name
);
915 #endif /* __APPLE__ */
917 #ifdef HAVE_NOTIFY_POST
919 * Send a notification so that MacOS X applications can know about the
923 notify_post("com.apple.printerListChange");
924 #endif /* HAVE_NOTIFY_POST */
931 * '_cupsUserDefault()' - Get the user default printer from environment
932 * variables and location information.
935 char * /* O - Default printer or NULL */
936 _cupsUserDefault(char *name
, /* I - Name buffer */
937 size_t namesize
) /* I - Size of name buffer */
939 const char *env
; /* LPDEST or PRINTER env variable */
941 CFStringRef network
; /* Network location */
942 CFArrayRef locations
; /* Location array */
943 CFStringRef locprinter
; /* Current printer */
944 #endif /* __APPLE__ */
947 if ((env
= getenv("LPDEST")) == NULL
)
948 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
953 strlcpy(name
, env
, namesize
);
959 * Use location-based defaults if "use last printer" is selected in the
960 * system preferences...
963 if (!appleUseLastPrinter())
965 DEBUG_puts("1_cupsUserDefault: Not using last printer as default...");
971 * Get the current location...
974 if ((network
= appleCopyNetwork()) == NULL
)
976 DEBUG_puts("1_cupsUserDefault: Unable to get current network...");
982 CFStringGetCString(network
, name
, namesize
, kCFStringEncodingUTF8
);
983 DEBUG_printf(("2_cupsUserDefault: network=\"%s\"", name
));
987 * Lookup the network in the preferences...
990 if ((locations
= appleCopyLocations()) == NULL
)
993 * Missing or bad location array, so no location-based default...
996 DEBUG_puts("1_cupsUserDefault: Missing or bad location history array...");
1004 DEBUG_printf(("2_cupsUserDefault: Got location, %d entries...",
1005 (int)CFArrayGetCount(locations
)));
1007 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
1008 CFStringGetCString(locprinter
, name
, namesize
, kCFStringEncodingUTF8
);
1013 CFRelease(locations
);
1015 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\"...", name
));
1017 return (*name
? name
: NULL
);
1021 * No location-based defaults on this platform...
1026 #endif /* __APPLE__ */
1032 * 'appleCopyLocations()' - Copy the location history array.
1035 static CFArrayRef
/* O - Location array or NULL */
1036 appleCopyLocations(void)
1038 CFArrayRef locations
; /* Location array */
1042 * Look up the location array in the preferences...
1045 if ((locations
= CFPreferencesCopyAppValue(kLocationHistoryArrayKey
,
1046 kPMPrintingPreferences
)) == NULL
)
1049 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
1051 CFRelease(locations
);
1060 * 'appleCopyNetwork()' - Get the network ID for the current location.
1063 static CFStringRef
/* O - Network ID */
1064 appleCopyNetwork(void)
1066 SCDynamicStoreRef dynamicStore
; /* System configuration data */
1067 CFStringRef key
; /* Current network configuration key */
1068 CFDictionaryRef ip_dict
; /* Network configuration data */
1069 CFStringRef network
= NULL
; /* Current network ID */
1072 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("Printing"), NULL
,
1075 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
1076 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
1078 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
1080 if ((network
= CFDictionaryGetValue(ip_dict
,
1081 kSCPropNetIPv4Router
)) != NULL
)
1090 CFRelease(dynamicStore
);
1098 * 'appleGetPaperSize()' - Get the default paper size.
1101 static char * /* O - Default paper size */
1102 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
1103 int namesize
) /* I - Size of buffer */
1105 CFStringRef defaultPaperID
; /* Default paper ID */
1106 _pwg_media_t
*pwgmedia
; /* PWG media size */
1109 defaultPaperID
= CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
1110 kPMPrintingPreferences
);
1111 if (!defaultPaperID
||
1112 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
1113 !CFStringGetCString(defaultPaperID
, name
, namesize
,
1114 kCFStringEncodingUTF8
))
1116 else if ((pwgmedia
= _pwgMediaForLegacy(name
)) != NULL
)
1117 strlcpy(name
, pwgmedia
->pwg
, namesize
);
1120 CFRelease(defaultPaperID
);
1127 * 'appleGetPrinter()' - Get a printer from the history array.
1130 static CFStringRef
/* O - Printer name or NULL */
1131 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
1132 CFStringRef network
, /* I - Network name */
1133 CFIndex
*locindex
) /* O - Index in array */
1135 CFIndex i
, /* Looping var */
1136 count
; /* Number of locations */
1137 CFDictionaryRef location
; /* Current location */
1138 CFStringRef locnetwork
, /* Current network */
1139 locprinter
; /* Current printer */
1142 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
1143 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
1144 CFGetTypeID(location
) == CFDictionaryGetTypeID())
1146 if ((locnetwork
= CFDictionaryGetValue(location
,
1147 kLocationNetworkKey
)) != NULL
&&
1148 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
1149 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
1150 (locprinter
= CFDictionaryGetValue(location
,
1151 kLocationPrinterIDKey
)) != NULL
&&
1152 CFGetTypeID(locprinter
) == CFStringGetTypeID())
1157 return (locprinter
);
1166 * 'appleSetDefault()' - Set the default printer for this location.
1170 appleSetDefault(const char *name
) /* I - Default printer/class name */
1172 CFStringRef network
; /* Current network */
1173 CFArrayRef locations
; /* Old locations array */
1174 CFIndex locindex
; /* Index in locations array */
1175 CFStringRef locprinter
; /* Current printer */
1176 CFMutableArrayRef newlocations
; /* New locations array */
1177 CFMutableDictionaryRef newlocation
; /* New location */
1178 CFStringRef newprinter
; /* New printer */
1182 * Get the current location...
1185 if ((network
= appleCopyNetwork()) == NULL
)
1187 DEBUG_puts("1appleSetDefault: Unable to get current network...");
1191 if ((newprinter
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
1192 kCFStringEncodingUTF8
)) == NULL
)
1199 * Lookup the network in the preferences...
1202 if ((locations
= appleCopyLocations()) != NULL
)
1203 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
1211 CFStringCompare(locprinter
, newprinter
, 0) != kCFCompareEqualTo
)
1214 * Need to change the locations array...
1219 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
1223 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
1226 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
1227 &kCFTypeArrayCallBacks
);
1229 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
1230 &kCFTypeDictionaryKeyCallBacks
,
1231 &kCFTypeDictionaryValueCallBacks
);
1233 if (newlocation
&& newlocations
)
1236 * Put the new location at the front of the array...
1239 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
1240 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, newprinter
);
1241 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
1244 * Limit the number of locations to 10...
1247 while (CFArrayGetCount(newlocations
) > 10)
1248 CFArrayRemoveValueAtIndex(newlocations
, 10);
1251 * Push the changes out...
1254 CFPreferencesSetAppValue(kLocationHistoryArrayKey
, newlocations
,
1255 kPMPrintingPreferences
);
1256 CFPreferencesAppSynchronize(kPMPrintingPreferences
);
1257 notify_post("com.apple.printerPrefsChange");
1261 CFRelease(newlocations
);
1264 CFRelease(newlocation
);
1268 CFRelease(locations
);
1271 CFRelease(newprinter
);
1276 * 'appleUseLastPrinter()' - Get the default printer preference value.
1279 static int /* O - 1 to use last printer, 0 otherwise */
1280 appleUseLastPrinter(void)
1282 CFPropertyListRef uselast
; /* Use last printer preference value */
1285 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
1288 if ((uselast
= CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey
,
1289 kPMPrintingPreferences
)) != NULL
)
1293 if (uselast
== kCFBooleanFalse
)
1299 #endif /* __APPLE__ */
1303 * 'cups_add_dest()' - Add a destination to the array.
1305 * Unlike cupsAddDest(), this function does not check for duplicates.
1308 static cups_dest_t
* /* O - New destination */
1309 cups_add_dest(const char *name
, /* I - Name of destination */
1310 const char *instance
, /* I - Instance or NULL */
1311 int *num_dests
, /* IO - Number of destinations */
1312 cups_dest_t
**dests
) /* IO - Destinations */
1314 int insert
, /* Insertion point */
1315 diff
; /* Result of comparison */
1316 cups_dest_t
*dest
; /* Destination pointer */
1320 * Add new destination...
1323 if (*num_dests
== 0)
1324 dest
= malloc(sizeof(cups_dest_t
));
1326 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (*num_dests
+ 1));
1334 * Find where to insert the destination...
1337 if (*num_dests
== 0)
1341 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
1349 * Move the array elements as needed...
1352 if (insert
< *num_dests
)
1353 memmove(*dests
+ insert
+ 1, *dests
+ insert
,
1354 (*num_dests
- insert
) * sizeof(cups_dest_t
));
1359 * Initialize the destination...
1362 dest
= *dests
+ insert
;
1363 dest
->name
= _cupsStrAlloc(name
);
1364 dest
->instance
= _cupsStrAlloc(instance
);
1365 dest
->is_default
= 0;
1366 dest
->num_options
= 0;
1367 dest
->options
= (cups_option_t
*)0;
1374 * 'cups_compare_dests()' - Compare two destinations.
1377 static int /* O - Result of comparison */
1378 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
1379 cups_dest_t
*b
) /* I - Second destination */
1381 int diff
; /* Difference */
1384 if ((diff
= strcasecmp(a
->name
, b
->name
)) != 0)
1386 else if (a
->instance
&& b
->instance
)
1387 return (strcasecmp(a
->instance
, b
->instance
));
1389 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
1394 * 'cups_find_dest()' - Find a destination using a binary search.
1397 static int /* O - Index of match */
1398 cups_find_dest(const char *name
, /* I - Destination name */
1399 const char *instance
, /* I - Instance or NULL */
1400 int num_dests
, /* I - Number of destinations */
1401 cups_dest_t
*dests
, /* I - Destinations */
1402 int prev
, /* I - Previous index */
1403 int *rdiff
) /* O - Difference of match */
1405 int left
, /* Low mark for binary search */
1406 right
, /* High mark for binary search */
1407 current
, /* Current index */
1408 diff
; /* Result of comparison */
1409 cups_dest_t key
; /* Search key */
1412 key
.name
= (char *)name
;
1413 key
.instance
= (char *)instance
;
1418 * Start search on either side of previous...
1421 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
1422 (diff
< 0 && prev
== 0) ||
1423 (diff
> 0 && prev
== (num_dests
- 1)))
1431 * Start with previous on right side...
1440 * Start wih previous on left side...
1444 right
= num_dests
- 1;
1450 * Start search in the middle...
1454 right
= num_dests
- 1;
1459 current
= (left
+ right
) / 2;
1460 diff
= cups_compare_dests(&key
, dests
+ current
);
1469 while ((right
- left
) > 1);
1474 * Check the last 1 or 2 elements...
1477 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
1481 diff
= cups_compare_dests(&key
, dests
+ right
);
1487 * Return the closest destination and the difference...
1497 * 'cups_get_default()' - Get the default destination from an lpoptions file.
1500 static char * /* O - Default destination or NULL */
1501 cups_get_default(const char *filename
, /* I - File to read */
1502 char *namebuf
, /* I - Name buffer */
1503 size_t namesize
, /* I - Size of name buffer */
1504 const char **instance
) /* I - Instance */
1506 cups_file_t
*fp
; /* lpoptions file */
1507 char line
[8192], /* Line from file */
1508 *value
, /* Value for line */
1509 *nameptr
; /* Pointer into name */
1510 int linenum
; /* Current line */
1515 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1519 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1521 if (!strcasecmp(line
, "default") && value
)
1523 strlcpy(namebuf
, value
, namesize
);
1525 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
1527 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
1530 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
1533 *instance
= nameptr
;
1541 return (*namebuf
? namebuf
: NULL
);
1546 * 'cups_get_dests()' - Get destinations from a file.
1549 static int /* O - Number of destinations */
1551 const char *filename
, /* I - File to read from */
1552 const char *match_name
, /* I - Destination name we want */
1553 const char *match_inst
, /* I - Instance name we want */
1554 int user_default_set
, /* I - User default printer set? */
1555 int num_dests
, /* I - Number of destinations */
1556 cups_dest_t
**dests
) /* IO - Destinations */
1558 int i
; /* Looping var */
1559 cups_dest_t
*dest
; /* Current destination */
1560 cups_file_t
*fp
; /* File pointer */
1561 char line
[8192], /* Line from file */
1562 *lineptr
, /* Pointer into line */
1563 *name
, /* Name of destination/option */
1564 *instance
; /* Instance of destination */
1565 int linenum
; /* Current line number */
1568 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", "
1569 "match_inst=\"%s\", user_default_set=%d, num_dests=%d, "
1570 "dests=%p)", filename
, match_name
, match_inst
,
1571 user_default_set
, num_dests
, dests
));
1574 * Try to open the file...
1577 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1581 * Read each printer; each line looks like:
1583 * Dest name[/instance] options
1584 * Default name[/instance] options
1589 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
1592 * See what type of line it is...
1595 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
1596 linenum
, line
, lineptr
));
1598 if ((strcasecmp(line
, "dest") && strcasecmp(line
, "default")) || !lineptr
)
1600 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
1607 * Search for an instance...
1610 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
1613 if (*lineptr
== '/')
1616 * Found an instance...
1623 * Search for an instance...
1626 while (!isspace(*lineptr
& 255) && *lineptr
)
1635 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
1639 * See if the primary instance of the destination exists; if not,
1640 * ignore this entry and move on...
1645 if (strcasecmp(name
, match_name
) ||
1646 (!instance
&& match_inst
) ||
1647 (instance
&& !match_inst
) ||
1648 (instance
&& strcasecmp(instance
, match_inst
)))
1653 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
1655 DEBUG_puts("9cups_get_dests: Not found!");
1661 * Add the destination...
1664 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
1666 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1672 DEBUG_puts("9cups_get_dests: Out of memory!");
1678 * Add options until we hit the end of the line...
1681 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
1685 * If we found what we were looking for, stop now...
1692 * Set this as default if needed...
1695 if (!user_default_set
&& !strcasecmp(line
, "default"))
1697 DEBUG_puts("9cups_get_dests: Setting as default...");
1699 for (i
= 0; i
< num_dests
; i
++)
1700 (*dests
)[i
].is_default
= 0;
1702 dest
->is_default
= 1;
1707 * Close the file and return...
1717 * 'cups_get_sdests()' - Get destinations from a server.
1720 static int /* O - Number of destinations */
1721 cups_get_sdests(http_t
*http
, /* I - Connection to server or CUPS_HTTP_DEFAULT */
1722 ipp_op_t op
, /* I - IPP operation */
1723 const char *name
, /* I - Name of destination */
1724 int num_dests
, /* I - Number of destinations */
1725 cups_dest_t
**dests
) /* IO - Destinations */
1727 cups_dest_t
*dest
; /* Current destination */
1728 ipp_t
*request
, /* IPP Request */
1729 *response
; /* IPP Response */
1730 ipp_attribute_t
*attr
; /* Current attribute */
1731 const char *printer_name
; /* printer-name attribute */
1732 char uri
[1024]; /* printer-uri value */
1733 int num_options
; /* Number of options */
1734 cups_option_t
*options
; /* Options */
1736 char media_default
[41]; /* Default paper size */
1737 #endif /* __APPLE__ */
1738 char optname
[1024], /* Option name */
1739 value
[2048], /* Option value */
1740 *ptr
; /* Pointer into name/value */
1741 static const char * const pattrs
[] = /* Attributes we're interested in */
1743 "auth-info-required",
1745 "job-sheets-default",
1746 "marker-change-time",
1748 "marker-high-levels",
1750 "marker-low-levels",
1756 #endif /* __APPLE__ */
1760 "printer-is-accepting-jobs",
1761 "printer-is-shared",
1763 "printer-make-and-model",
1766 "printer-state-change-time",
1767 "printer-state-reasons",
1769 "printer-uri-supported"
1775 * Get the default paper size...
1778 appleGetPaperSize(media_default
, sizeof(media_default
));
1779 #endif /* __APPLE__ */
1782 * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which
1783 * require the following attributes:
1785 * attributes-charset
1786 * attributes-natural-language
1787 * requesting-user-name
1788 * printer-uri [for IPP_GET_PRINTER_ATTRIBUTES]
1791 request
= ippNewRequest(op
);
1793 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1794 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1797 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1798 "requesting-user-name", NULL
, cupsUser());
1800 if (name
&& op
!= CUPS_GET_DEFAULT
)
1802 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1803 "localhost", ippPort(), "/printers/%s", name
);
1804 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1809 * Do the request and get back a response...
1812 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1814 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1817 * Skip leading attributes until we hit a printer...
1820 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1827 * Pull the needed attributes from this printer...
1830 printer_name
= NULL
;
1834 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1836 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1837 attr
->value_tag
!= IPP_TAG_ENUM
&&
1838 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1839 attr
->value_tag
!= IPP_TAG_TEXT
&&
1840 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1841 attr
->value_tag
!= IPP_TAG_NAME
&&
1842 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1843 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1844 attr
->value_tag
!= IPP_TAG_RANGE
&&
1845 attr
->value_tag
!= IPP_TAG_URI
)
1848 if (!strcmp(attr
->name
, "auth-info-required") ||
1849 !strcmp(attr
->name
, "device-uri") ||
1850 !strcmp(attr
->name
, "marker-change-time") ||
1851 !strcmp(attr
->name
, "marker-colors") ||
1852 !strcmp(attr
->name
, "marker-high-levels") ||
1853 !strcmp(attr
->name
, "marker-levels") ||
1854 !strcmp(attr
->name
, "marker-low-levels") ||
1855 !strcmp(attr
->name
, "marker-message") ||
1856 !strcmp(attr
->name
, "marker-names") ||
1857 !strcmp(attr
->name
, "marker-types") ||
1858 !strcmp(attr
->name
, "printer-commands") ||
1859 !strcmp(attr
->name
, "printer-info") ||
1860 !strcmp(attr
->name
, "printer-is-shared") ||
1861 !strcmp(attr
->name
, "printer-make-and-model") ||
1862 !strcmp(attr
->name
, "printer-state") ||
1863 !strcmp(attr
->name
, "printer-state-change-time") ||
1864 !strcmp(attr
->name
, "printer-type") ||
1865 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1866 !strcmp(attr
->name
, "printer-location") ||
1867 !strcmp(attr
->name
, "printer-state-reasons") ||
1868 !strcmp(attr
->name
, "printer-uri-supported"))
1871 * Add a printer description attribute...
1874 num_options
= cupsAddOption(attr
->name
,
1875 cups_make_string(attr
, value
,
1877 num_options
, &options
);
1880 else if (!strcmp(attr
->name
, "media-supported"))
1883 * See if we can set a default media size...
1886 int i
; /* Looping var */
1888 for (i
= 0; i
< attr
->num_values
; i
++)
1889 if (!strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1891 num_options
= cupsAddOption("media", media_default
, num_options
,
1896 #endif /* __APPLE__ */
1897 else if (!strcmp(attr
->name
, "printer-name") &&
1898 attr
->value_tag
== IPP_TAG_NAME
)
1899 printer_name
= attr
->values
[0].string
.text
;
1900 else if (strncmp(attr
->name
, "notify-", 7) &&
1901 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1902 attr
->value_tag
== IPP_TAG_ENUM
||
1903 attr
->value_tag
== IPP_TAG_INTEGER
||
1904 attr
->value_tag
== IPP_TAG_KEYWORD
||
1905 attr
->value_tag
== IPP_TAG_NAME
||
1906 attr
->value_tag
== IPP_TAG_RANGE
) &&
1907 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1910 * Add a default option...
1913 strlcpy(optname
, attr
->name
, sizeof(optname
));
1914 optname
[ptr
- attr
->name
] = '\0';
1916 if (strcasecmp(optname
, "media") ||
1917 !cupsGetOption("media", num_options
, options
))
1918 num_options
= cupsAddOption(optname
,
1919 cups_make_string(attr
, value
,
1921 num_options
, &options
);
1926 * See if we have everything needed...
1931 cupsFreeOptions(num_options
, options
);
1939 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1941 dest
->num_options
= num_options
;
1942 dest
->options
= options
;
1945 cupsFreeOptions(num_options
, options
);
1951 ippDelete(response
);
1955 * Return the count...
1963 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
1967 static char * /* O - New string */
1969 ipp_attribute_t
*attr
, /* I - Attribute to convert */
1970 char *buffer
, /* I - Buffer */
1971 size_t bufsize
) /* I - Size of buffer */
1973 int i
; /* Looping var */
1974 char *ptr
, /* Pointer into buffer */
1975 *end
, /* Pointer to end of buffer */
1976 *valptr
; /* Pointer into string attribute */
1980 * Return quickly if we have a single string value...
1983 if (attr
->num_values
== 1 &&
1984 attr
->value_tag
!= IPP_TAG_INTEGER
&&
1985 attr
->value_tag
!= IPP_TAG_ENUM
&&
1986 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1987 attr
->value_tag
!= IPP_TAG_RANGE
)
1988 return (attr
->values
[0].string
.text
);
1991 * Copy the values to the string, separating with commas and escaping strings
1995 end
= buffer
+ bufsize
- 1;
1997 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
2002 switch (attr
->value_tag
)
2004 case IPP_TAG_INTEGER
:
2006 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].integer
);
2009 case IPP_TAG_BOOLEAN
:
2010 if (attr
->values
[i
].boolean
)
2011 strlcpy(ptr
, "true", end
- ptr
+ 1);
2013 strlcpy(ptr
, "false", end
- ptr
+ 1);
2016 case IPP_TAG_RANGE
:
2017 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2018 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].range
.lower
);
2020 snprintf(ptr
, end
- ptr
+ 1, "%d-%d", attr
->values
[i
].range
.lower
,
2021 attr
->values
[i
].range
.upper
);
2025 for (valptr
= attr
->values
[i
].string
.text
;
2026 *valptr
&& ptr
< end
;)
2028 if (strchr(" \t\n\\\'\"", *valptr
))
2030 if (ptr
>= (end
- 1))
2053 * End of "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $".