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
));
508 * No default in the environment, try the user's lpoptions files...
511 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
513 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
519 * Still not there? Try the system lpoptions file...
522 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
523 cg
->cups_serverroot
);
524 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
530 * No locally-set default destination, ask the server...
533 op
= CUPS_GET_DEFAULT
;
538 * Get the printer's attributes...
541 if (!cups_get_sdests(http
, op
, name
, 0, &dest
))
543 if (op
== CUPS_GET_DEFAULT
|| (name
&& !set_as_default
))
547 * The default printer from environment variables or from a
548 * configuration file does not exist. Find out the real default.
551 if (!cups_get_sdests(http
, CUPS_GET_DEFAULT
, NULL
, 0, &dest
))
556 dest
->instance
= _cupsStrAlloc(instance
);
559 dest
->is_default
= 1;
562 * Then add local options...
565 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
566 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
570 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
572 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
576 * Return the result...
584 * 'cupsRemoveDest()' - Remove a destination from the destination list.
586 * Removing a destination/instance does not delete the class or printer
587 * queue, merely the lpoptions for that destination/instance. Use the
588 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
589 * options for the user.
591 * @since CUPS 1.3/Mac OS X 10.5@
594 int /* O - New number of destinations */
595 cupsRemoveDest(const char *name
, /* I - Destination name */
596 const char *instance
, /* I - Instance name or @code NULL@ */
597 int num_dests
, /* I - Number of destinations */
598 cups_dest_t
**dests
) /* IO - Destinations */
600 int i
; /* Index into destinations */
601 cups_dest_t
*dest
; /* Pointer to destination */
605 * Find the destination...
608 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
615 _cupsStrFree(dest
->name
);
616 _cupsStrFree(dest
->instance
);
617 cupsFreeOptions(dest
->num_options
, dest
->options
);
620 * Remove the destination from the array...
628 memmove(dest
, dest
+ 1, (num_dests
- i
) * sizeof(cups_dest_t
));
635 * 'cupsSetDefaultDest()' - Set the default destination.
637 * @since CUPS 1.3/Mac OS X 10.5@
642 const char *name
, /* I - Destination name */
643 const char *instance
, /* I - Instance name or @code NULL@ */
644 int num_dests
, /* I - Number of destinations */
645 cups_dest_t
*dests
) /* I - Destinations */
647 int i
; /* Looping var */
648 cups_dest_t
*dest
; /* Current destination */
652 * Range check input...
655 if (!name
|| num_dests
<= 0 || !dests
)
659 * Loop through the array and set the "is_default" flag for the matching
663 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
664 dest
->is_default
= !strcasecmp(name
, dest
->name
) &&
665 ((!instance
&& !dest
->instance
) ||
666 (instance
&& dest
->instance
&&
667 !strcasecmp(instance
, dest
->instance
)));
672 * 'cupsSetDests()' - Save the list of destinations for the default server.
674 * This function saves the destinations to /etc/cups/lpoptions when run
675 * as root and ~/.cups/lpoptions when run as a normal user.
679 cupsSetDests(int num_dests
, /* I - Number of destinations */
680 cups_dest_t
*dests
) /* I - Destinations */
682 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
687 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
689 * This function saves the destinations to /etc/cups/lpoptions when run
690 * as root and ~/.cups/lpoptions when run as a normal user.
692 * @since CUPS 1.1.21/Mac OS X 10.4@
695 int /* O - 0 on success, -1 on error */
696 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
697 int num_dests
, /* I - Number of destinations */
698 cups_dest_t
*dests
) /* I - Destinations */
700 int i
, j
; /* Looping vars */
701 int wrote
; /* Wrote definition? */
702 cups_dest_t
*dest
; /* Current destination */
703 cups_option_t
*option
; /* Current option */
704 _ipp_option_t
*match
; /* Matching attribute for option */
705 FILE *fp
; /* File pointer */
707 const char *home
; /* HOME environment variable */
709 char filename
[1024]; /* lpoptions file */
710 int num_temps
; /* Number of temporary destinations */
711 cups_dest_t
*temps
, /* Temporary destinations */
712 *temp
; /* Current temporary dest */
713 const char *val
; /* Value of temporary option */
714 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
718 * Range check the input...
721 if (!num_dests
|| !dests
)
725 * Get the server destinations...
728 num_temps
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, NULL
, 0, &temps
);
730 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
732 cupsFreeDests(num_temps
, temps
);
737 * Figure out which file to write to...
740 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
746 * Merge in server defaults...
749 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
752 * Point to user defaults...
755 if ((home
= getenv("HOME")) != NULL
)
758 * Create ~/.cups subdirectory...
761 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
762 if (access(filename
, 0))
763 mkdir(filename
, 0700);
765 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
771 * Try to open the file...
774 if ((fp
= fopen(filename
, "w")) == NULL
)
776 cupsFreeDests(num_temps
, temps
);
782 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
787 fchmod(fileno(fp
), 0644);
791 * Write each printer; each line looks like:
793 * Dest name[/instance] options
794 * Default name[/instance] options
797 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
798 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
800 if (dest
->is_default
)
802 fprintf(fp
, "Default %s", dest
->name
);
804 fprintf(fp
, "/%s", dest
->instance
);
811 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
812 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
814 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
817 * See if this option is a printer attribute; if so, skip it...
820 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
821 match
->group_tag
== IPP_TAG_PRINTER
)
825 * See if the server/global options match these; if so, don't
830 (val
= cupsGetOption(option
->name
, temp
->num_options
,
831 temp
->options
)) != NULL
&&
832 !strcasecmp(val
, option
->value
))
836 * Options don't match, write to the file...
841 fprintf(fp
, "Dest %s", dest
->name
);
843 fprintf(fp
, "/%s", dest
->instance
);
847 if (option
->value
[0])
849 if (strchr(option
->value
, ' ') ||
850 strchr(option
->value
, '\\') ||
851 strchr(option
->value
, '\"') ||
852 strchr(option
->value
, '\''))
858 fprintf(fp
, " %s=\"", option
->name
);
860 for (val
= option
->value
; *val
; val
++)
862 if (strchr("\"\'\\", *val
))
873 * Store the literal value...
876 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
880 fprintf(fp
, " %s", option
->name
);
888 * Free the temporary destinations and close the file...
891 cupsFreeDests(num_temps
, temps
);
897 * Set the default printer for this location - this allows command-line
898 * and GUI applications to share the same default destination...
901 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
902 appleSetDefault(dest
->name
);
903 #endif /* __APPLE__ */
905 #ifdef HAVE_NOTIFY_POST
907 * Send a notification so that MacOS X applications can know about the
911 notify_post("com.apple.printerListChange");
912 #endif /* HAVE_NOTIFY_POST */
919 * '_cupsUserDefault()' - Get the user default printer from environment
920 * variables and location information.
923 char * /* O - Default printer or NULL */
924 _cupsUserDefault(char *name
, /* I - Name buffer */
925 size_t namesize
) /* I - Size of name buffer */
927 const char *env
; /* LPDEST or PRINTER env variable */
929 CFStringRef network
; /* Network location */
930 CFArrayRef locations
; /* Location array */
931 CFStringRef locprinter
; /* Current printer */
932 #endif /* __APPLE__ */
935 if ((env
= getenv("LPDEST")) == NULL
)
936 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
941 strlcpy(name
, env
, namesize
);
947 * Use location-based defaults if "use last printer" is selected in the
948 * system preferences...
951 if (!appleUseLastPrinter())
953 DEBUG_puts("1_cupsUserDefault: Not using last printer as default...");
959 * Get the current location...
962 if ((network
= appleCopyNetwork()) == NULL
)
964 DEBUG_puts("1_cupsUserDefault: Unable to get current network...");
970 CFStringGetCString(network
, name
, namesize
, kCFStringEncodingUTF8
);
971 DEBUG_printf(("2_cupsUserDefault: network=\"%s\"", name
));
975 * Lookup the network in the preferences...
978 if ((locations
= appleCopyLocations()) == NULL
)
981 * Missing or bad location array, so no location-based default...
984 DEBUG_puts("1_cupsUserDefault: Missing or bad location history array...");
992 DEBUG_printf(("2_cupsUserDefault: Got location, %d entries...",
993 (int)CFArrayGetCount(locations
)));
995 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
996 CFStringGetCString(locprinter
, name
, namesize
, kCFStringEncodingUTF8
);
1001 CFRelease(locations
);
1003 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\"...", name
));
1005 return (*name
? name
: NULL
);
1009 * No location-based defaults on this platform...
1014 #endif /* __APPLE__ */
1020 * 'appleCopyLocations()' - Copy the location history array.
1023 static CFArrayRef
/* O - Location array or NULL */
1024 appleCopyLocations(void)
1026 CFArrayRef locations
; /* Location array */
1030 * Look up the location array in the preferences...
1033 if ((locations
= CFPreferencesCopyAppValue(kLocationHistoryArrayKey
,
1034 kPMPrintingPreferences
)) == NULL
)
1037 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
1039 CFRelease(locations
);
1048 * 'appleCopyNetwork()' - Get the network ID for the current location.
1051 static CFStringRef
/* O - Network ID */
1052 appleCopyNetwork(void)
1054 SCDynamicStoreRef dynamicStore
; /* System configuration data */
1055 CFStringRef key
; /* Current network configuration key */
1056 CFDictionaryRef ip_dict
; /* Network configuration data */
1057 CFStringRef network
= NULL
; /* Current network ID */
1060 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("Printing"), NULL
,
1063 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
1064 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
1066 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
1068 if ((network
= CFDictionaryGetValue(ip_dict
,
1069 kSCPropNetIPv4Router
)) != NULL
)
1078 CFRelease(dynamicStore
);
1086 * 'appleGetPaperSize()' - Get the default paper size.
1089 static char * /* O - Default paper size */
1090 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
1091 int namesize
) /* I - Size of buffer */
1093 CFStringRef defaultPaperID
; /* Default paper ID */
1094 _pwg_media_t
*pwgmedia
; /* PWG media size */
1097 defaultPaperID
= CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
1098 kPMPrintingPreferences
);
1099 if (!defaultPaperID
||
1100 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
1101 !CFStringGetCString(defaultPaperID
, name
, namesize
,
1102 kCFStringEncodingUTF8
))
1104 else if ((pwgmedia
= _pwgMediaForLegacy(name
)) != NULL
)
1105 strlcpy(name
, pwgmedia
->pwg
, namesize
);
1108 CFRelease(defaultPaperID
);
1115 * 'appleGetPrinter()' - Get a printer from the history array.
1118 static CFStringRef
/* O - Printer name or NULL */
1119 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
1120 CFStringRef network
, /* I - Network name */
1121 CFIndex
*locindex
) /* O - Index in array */
1123 CFIndex i
, /* Looping var */
1124 count
; /* Number of locations */
1125 CFDictionaryRef location
; /* Current location */
1126 CFStringRef locnetwork
, /* Current network */
1127 locprinter
; /* Current printer */
1130 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
1131 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
1132 CFGetTypeID(location
) == CFDictionaryGetTypeID())
1134 if ((locnetwork
= CFDictionaryGetValue(location
,
1135 kLocationNetworkKey
)) != NULL
&&
1136 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
1137 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
1138 (locprinter
= CFDictionaryGetValue(location
,
1139 kLocationPrinterIDKey
)) != NULL
&&
1140 CFGetTypeID(locprinter
) == CFStringGetTypeID())
1145 return (locprinter
);
1154 * 'appleSetDefault()' - Set the default printer for this location.
1158 appleSetDefault(const char *name
) /* I - Default printer/class name */
1160 CFStringRef network
; /* Current network */
1161 CFArrayRef locations
; /* Old locations array */
1162 CFIndex locindex
; /* Index in locations array */
1163 CFStringRef locprinter
; /* Current printer */
1164 CFMutableArrayRef newlocations
; /* New locations array */
1165 CFMutableDictionaryRef newlocation
; /* New location */
1166 CFStringRef newprinter
; /* New printer */
1170 * Get the current location...
1173 if ((network
= appleCopyNetwork()) == NULL
)
1175 DEBUG_puts("1appleSetDefault: Unable to get current network...");
1179 if ((newprinter
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
1180 kCFStringEncodingUTF8
)) == NULL
)
1187 * Lookup the network in the preferences...
1190 if ((locations
= appleCopyLocations()) != NULL
)
1191 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
1199 CFStringCompare(locprinter
, newprinter
, 0) != kCFCompareEqualTo
)
1202 * Need to change the locations array...
1207 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
1211 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
1214 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, NULL
);
1216 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
1217 &kCFTypeDictionaryKeyCallBacks
,
1218 &kCFTypeDictionaryValueCallBacks
);
1220 if (newlocation
&& newlocations
)
1223 * Put the new location at the front of the array...
1226 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
1227 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, newprinter
);
1228 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
1231 * Limit the number of locations to 10...
1234 while (CFArrayGetCount(newlocations
) > 10)
1235 CFArrayRemoveValueAtIndex(newlocations
, 10);
1238 * Push the changes out...
1241 CFPreferencesSetAppValue(kLocationHistoryArrayKey
, newlocations
,
1242 kPMPrintingPreferences
);
1243 CFPreferencesAppSynchronize(kPMPrintingPreferences
);
1244 notify_post("com.apple.printerPrefsChange");
1248 CFRelease(newlocations
);
1251 CFRelease(newlocation
);
1255 CFRelease(locations
);
1258 CFRelease(newprinter
);
1263 * 'appleUseLastPrinter()' - Get the default printer preference value.
1266 static int /* O - 1 to use last printer, 0 otherwise */
1267 appleUseLastPrinter(void)
1269 CFPropertyListRef uselast
; /* Use last printer preference value */
1272 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
1275 if ((uselast
= CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey
,
1276 kPMPrintingPreferences
)) != NULL
)
1280 if (uselast
== kCFBooleanFalse
)
1286 #endif /* __APPLE__ */
1290 * 'cups_add_dest()' - Add a destination to the array.
1292 * Unlike cupsAddDest(), this function does not check for duplicates.
1295 static cups_dest_t
* /* O - New destination */
1296 cups_add_dest(const char *name
, /* I - Name of destination */
1297 const char *instance
, /* I - Instance or NULL */
1298 int *num_dests
, /* IO - Number of destinations */
1299 cups_dest_t
**dests
) /* IO - Destinations */
1301 int insert
, /* Insertion point */
1302 diff
; /* Result of comparison */
1303 cups_dest_t
*dest
; /* Destination pointer */
1307 * Add new destination...
1310 if (*num_dests
== 0)
1311 dest
= malloc(sizeof(cups_dest_t
));
1313 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (*num_dests
+ 1));
1321 * Find where to insert the destination...
1324 if (*num_dests
== 0)
1328 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
1336 * Move the array elements as needed...
1339 if (insert
< *num_dests
)
1340 memmove(*dests
+ insert
+ 1, *dests
+ insert
,
1341 (*num_dests
- insert
) * sizeof(cups_dest_t
));
1346 * Initialize the destination...
1349 dest
= *dests
+ insert
;
1350 dest
->name
= _cupsStrAlloc(name
);
1351 dest
->instance
= _cupsStrAlloc(instance
);
1352 dest
->is_default
= 0;
1353 dest
->num_options
= 0;
1354 dest
->options
= (cups_option_t
*)0;
1361 * 'cups_compare_dests()' - Compare two destinations.
1364 static int /* O - Result of comparison */
1365 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
1366 cups_dest_t
*b
) /* I - Second destination */
1368 int diff
; /* Difference */
1371 if ((diff
= strcasecmp(a
->name
, b
->name
)) != 0)
1373 else if (a
->instance
&& b
->instance
)
1374 return (strcasecmp(a
->instance
, b
->instance
));
1376 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
1381 * 'cups_find_dest()' - Find a destination using a binary search.
1384 static int /* O - Index of match */
1385 cups_find_dest(const char *name
, /* I - Destination name */
1386 const char *instance
, /* I - Instance or NULL */
1387 int num_dests
, /* I - Number of destinations */
1388 cups_dest_t
*dests
, /* I - Destinations */
1389 int prev
, /* I - Previous index */
1390 int *rdiff
) /* O - Difference of match */
1392 int left
, /* Low mark for binary search */
1393 right
, /* High mark for binary search */
1394 current
, /* Current index */
1395 diff
; /* Result of comparison */
1396 cups_dest_t key
; /* Search key */
1399 key
.name
= (char *)name
;
1400 key
.instance
= (char *)instance
;
1405 * Start search on either side of previous...
1408 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
1409 (diff
< 0 && prev
== 0) ||
1410 (diff
> 0 && prev
== (num_dests
- 1)))
1418 * Start with previous on right side...
1427 * Start wih previous on left side...
1431 right
= num_dests
- 1;
1437 * Start search in the middle...
1441 right
= num_dests
- 1;
1446 current
= (left
+ right
) / 2;
1447 diff
= cups_compare_dests(&key
, dests
+ current
);
1456 while ((right
- left
) > 1);
1461 * Check the last 1 or 2 elements...
1464 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
1468 diff
= cups_compare_dests(&key
, dests
+ right
);
1474 * Return the closest destination and the difference...
1484 * 'cups_get_default()' - Get the default destination from an lpoptions file.
1487 static char * /* O - Default destination or NULL */
1488 cups_get_default(const char *filename
, /* I - File to read */
1489 char *namebuf
, /* I - Name buffer */
1490 size_t namesize
, /* I - Size of name buffer */
1491 const char **instance
) /* I - Instance */
1493 cups_file_t
*fp
; /* lpoptions file */
1494 char line
[8192], /* Line from file */
1495 *value
, /* Value for line */
1496 *nameptr
; /* Pointer into name */
1497 int linenum
; /* Current line */
1502 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1506 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1508 if (!strcasecmp(line
, "default") && value
)
1510 strlcpy(namebuf
, value
, namesize
);
1512 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
1514 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
1517 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
1520 *instance
= nameptr
;
1528 return (*namebuf
? namebuf
: NULL
);
1533 * 'cups_get_dests()' - Get destinations from a file.
1536 static int /* O - Number of destinations */
1538 const char *filename
, /* I - File to read from */
1539 const char *match_name
, /* I - Destination name we want */
1540 const char *match_inst
, /* I - Instance name we want */
1541 int user_default_set
, /* I - User default printer set? */
1542 int num_dests
, /* I - Number of destinations */
1543 cups_dest_t
**dests
) /* IO - Destinations */
1545 int i
; /* Looping var */
1546 cups_dest_t
*dest
; /* Current destination */
1547 cups_file_t
*fp
; /* File pointer */
1548 char line
[8192], /* Line from file */
1549 *lineptr
, /* Pointer into line */
1550 *name
, /* Name of destination/option */
1551 *instance
; /* Instance of destination */
1552 int linenum
; /* Current line number */
1555 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", "
1556 "match_inst=\"%s\", user_default_set=%d, num_dests=%d, "
1557 "dests=%p)", filename
, match_name
, match_inst
,
1558 user_default_set
, num_dests
, dests
));
1561 * Try to open the file...
1564 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1568 * Read each printer; each line looks like:
1570 * Dest name[/instance] options
1571 * Default name[/instance] options
1576 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
1579 * See what type of line it is...
1582 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
1583 linenum
, line
, lineptr
));
1585 if ((strcasecmp(line
, "dest") && strcasecmp(line
, "default")) || !lineptr
)
1587 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
1594 * Search for an instance...
1597 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
1600 if (*lineptr
== '/')
1603 * Found an instance...
1610 * Search for an instance...
1613 while (!isspace(*lineptr
& 255) && *lineptr
)
1622 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
1626 * See if the primary instance of the destination exists; if not,
1627 * ignore this entry and move on...
1632 if (strcasecmp(name
, match_name
) ||
1633 (!instance
&& match_inst
) ||
1634 (instance
&& !match_inst
) ||
1635 (instance
&& strcasecmp(instance
, match_inst
)))
1640 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
1642 DEBUG_puts("9cups_get_dests: Not found!");
1648 * Add the destination...
1651 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
1653 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1659 DEBUG_puts("9cups_get_dests: Out of memory!");
1665 * Add options until we hit the end of the line...
1668 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
1672 * If we found what we were looking for, stop now...
1679 * Set this as default if needed...
1682 if (!user_default_set
&& !strcasecmp(line
, "default"))
1684 DEBUG_puts("9cups_get_dests: Setting as default...");
1686 for (i
= 0; i
< num_dests
; i
++)
1687 (*dests
)[i
].is_default
= 0;
1689 dest
->is_default
= 1;
1694 * Close the file and return...
1704 * 'cups_get_sdests()' - Get destinations from a server.
1707 static int /* O - Number of destinations */
1708 cups_get_sdests(http_t
*http
, /* I - Connection to server or CUPS_HTTP_DEFAULT */
1709 ipp_op_t op
, /* I - IPP operation */
1710 const char *name
, /* I - Name of destination */
1711 int num_dests
, /* I - Number of destinations */
1712 cups_dest_t
**dests
) /* IO - Destinations */
1714 cups_dest_t
*dest
; /* Current destination */
1715 ipp_t
*request
, /* IPP Request */
1716 *response
; /* IPP Response */
1717 ipp_attribute_t
*attr
; /* Current attribute */
1718 const char *printer_name
; /* printer-name attribute */
1719 char uri
[1024]; /* printer-uri value */
1720 int num_options
; /* Number of options */
1721 cups_option_t
*options
; /* Options */
1723 char media_default
[41]; /* Default paper size */
1724 #endif /* __APPLE__ */
1725 char optname
[1024], /* Option name */
1726 value
[2048], /* Option value */
1727 *ptr
; /* Pointer into name/value */
1728 static const char * const pattrs
[] = /* Attributes we're interested in */
1730 "auth-info-required",
1732 "job-sheets-default",
1733 "marker-change-time",
1735 "marker-high-levels",
1737 "marker-low-levels",
1743 #endif /* __APPLE__ */
1747 "printer-is-accepting-jobs",
1748 "printer-is-shared",
1750 "printer-make-and-model",
1753 "printer-state-change-time",
1754 "printer-state-reasons",
1756 "printer-uri-supported"
1762 * Get the default paper size...
1765 appleGetPaperSize(media_default
, sizeof(media_default
));
1766 #endif /* __APPLE__ */
1769 * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which
1770 * require the following attributes:
1772 * attributes-charset
1773 * attributes-natural-language
1774 * requesting-user-name
1775 * printer-uri [for IPP_GET_PRINTER_ATTRIBUTES]
1778 request
= ippNewRequest(op
);
1780 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1781 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1784 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1785 "requesting-user-name", NULL
, cupsUser());
1787 if (name
&& op
!= CUPS_GET_DEFAULT
)
1789 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1790 "localhost", ippPort(), "/printers/%s", name
);
1791 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1796 * Do the request and get back a response...
1799 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1801 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1804 * Skip leading attributes until we hit a printer...
1807 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1814 * Pull the needed attributes from this printer...
1817 printer_name
= NULL
;
1821 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1823 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1824 attr
->value_tag
!= IPP_TAG_ENUM
&&
1825 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1826 attr
->value_tag
!= IPP_TAG_TEXT
&&
1827 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1828 attr
->value_tag
!= IPP_TAG_NAME
&&
1829 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1830 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1831 attr
->value_tag
!= IPP_TAG_RANGE
&&
1832 attr
->value_tag
!= IPP_TAG_URI
)
1835 if (!strcmp(attr
->name
, "auth-info-required") ||
1836 !strcmp(attr
->name
, "device-uri") ||
1837 !strcmp(attr
->name
, "marker-change-time") ||
1838 !strcmp(attr
->name
, "marker-colors") ||
1839 !strcmp(attr
->name
, "marker-high-levels") ||
1840 !strcmp(attr
->name
, "marker-levels") ||
1841 !strcmp(attr
->name
, "marker-low-levels") ||
1842 !strcmp(attr
->name
, "marker-message") ||
1843 !strcmp(attr
->name
, "marker-names") ||
1844 !strcmp(attr
->name
, "marker-types") ||
1845 !strcmp(attr
->name
, "printer-commands") ||
1846 !strcmp(attr
->name
, "printer-info") ||
1847 !strcmp(attr
->name
, "printer-is-shared") ||
1848 !strcmp(attr
->name
, "printer-make-and-model") ||
1849 !strcmp(attr
->name
, "printer-state") ||
1850 !strcmp(attr
->name
, "printer-state-change-time") ||
1851 !strcmp(attr
->name
, "printer-type") ||
1852 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1853 !strcmp(attr
->name
, "printer-location") ||
1854 !strcmp(attr
->name
, "printer-state-reasons") ||
1855 !strcmp(attr
->name
, "printer-uri-supported"))
1858 * Add a printer description attribute...
1861 num_options
= cupsAddOption(attr
->name
,
1862 cups_make_string(attr
, value
,
1864 num_options
, &options
);
1867 else if (!strcmp(attr
->name
, "media-supported"))
1870 * See if we can set a default media size...
1873 int i
; /* Looping var */
1875 for (i
= 0; i
< attr
->num_values
; i
++)
1876 if (!strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1878 num_options
= cupsAddOption("media", media_default
, num_options
,
1883 #endif /* __APPLE__ */
1884 else if (!strcmp(attr
->name
, "printer-name") &&
1885 attr
->value_tag
== IPP_TAG_NAME
)
1886 printer_name
= attr
->values
[0].string
.text
;
1887 else if (strncmp(attr
->name
, "notify-", 7) &&
1888 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1889 attr
->value_tag
== IPP_TAG_ENUM
||
1890 attr
->value_tag
== IPP_TAG_INTEGER
||
1891 attr
->value_tag
== IPP_TAG_KEYWORD
||
1892 attr
->value_tag
== IPP_TAG_NAME
||
1893 attr
->value_tag
== IPP_TAG_RANGE
) &&
1894 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1897 * Add a default option...
1900 strlcpy(optname
, attr
->name
, sizeof(optname
));
1901 optname
[ptr
- attr
->name
] = '\0';
1903 if (strcasecmp(optname
, "media") ||
1904 !cupsGetOption("media", num_options
, options
))
1905 num_options
= cupsAddOption(optname
,
1906 cups_make_string(attr
, value
,
1908 num_options
, &options
);
1913 * See if we have everything needed...
1918 cupsFreeOptions(num_options
, options
);
1926 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1928 dest
->num_options
= num_options
;
1929 dest
->options
= options
;
1932 cupsFreeOptions(num_options
, options
);
1938 ippDelete(response
);
1942 * Return the count...
1950 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
1954 static char * /* O - New string */
1956 ipp_attribute_t
*attr
, /* I - Attribute to convert */
1957 char *buffer
, /* I - Buffer */
1958 size_t bufsize
) /* I - Size of buffer */
1960 int i
; /* Looping var */
1961 char *ptr
, /* Pointer into buffer */
1962 *end
, /* Pointer to end of buffer */
1963 *valptr
; /* Pointer into string attribute */
1967 * Return quickly if we have a single string value...
1970 if (attr
->num_values
== 1 &&
1971 attr
->value_tag
!= IPP_TAG_INTEGER
&&
1972 attr
->value_tag
!= IPP_TAG_ENUM
&&
1973 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1974 attr
->value_tag
!= IPP_TAG_RANGE
)
1975 return (attr
->values
[0].string
.text
);
1978 * Copy the values to the string, separating with commas and escaping strings
1982 end
= buffer
+ bufsize
- 1;
1984 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
1989 switch (attr
->value_tag
)
1991 case IPP_TAG_INTEGER
:
1993 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].integer
);
1996 case IPP_TAG_BOOLEAN
:
1997 if (attr
->values
[i
].boolean
)
1998 strlcpy(ptr
, "true", end
- ptr
+ 1);
2000 strlcpy(ptr
, "false", end
- ptr
+ 1);
2003 case IPP_TAG_RANGE
:
2004 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2005 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].range
.lower
);
2007 snprintf(ptr
, end
- ptr
+ 1, "%d-%d", attr
->values
[i
].range
.lower
,
2008 attr
->values
[i
].range
.upper
);
2012 for (valptr
= attr
->values
[i
].string
.text
;
2013 *valptr
&& ptr
< end
;)
2015 if (strchr(" \t\n\\\'\"", *valptr
))
2017 if (ptr
>= (end
- 1))
2040 * End of "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $".