2 * "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $"
4 * User-defined destination (and option) support for the Common UNIX
5 * Printing System (CUPS).
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1997-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * cupsAddDest() - Add a destination to the list of destinations.
21 * cupsFreeDests() - Free the memory used by the list of destinations.
22 * cupsGetDest() - Get the named destination from the list.
23 * cupsGetDests() - Get the list of destinations from the default
25 * cupsGetDests2() - Get the list of destinations from the specified
27 * cupsGetNamedDest() - Get options for the named destination.
28 * cupsRemoveDest() - Remove a destination from the destination list.
29 * cupsSetDefaultDest() - Set the default destination.
30 * cupsSetDests() - Save the list of destinations for the default
32 * cupsSetDests2() - Save the list of destinations for the specified
34 * appleCopyLocations() - Copy the location history array.
35 * appleCopyNetwork() - Get the network ID for the current location.
36 * appleGetPaperSize() - Get the default paper size.
37 * appleGetPrinter() - Get a printer from the history array.
38 * appleSetDefault() - Set the default printer for this location.
39 * appleUseLastPrinter() - Get the default printer preference value.
40 * cups_add_dest() - Add a destination to the array.
41 * cups_compare_dests() - Compare two destinations.
42 * cups_find_dest() - Find a destination using a binary search.
43 * cups_get_default() - Get the default destination from an lpoptions file.
44 * cups_get_dests() - Get destinations from a file.
45 * cups_get_sdests() - Get destinations from a server.
46 * cups_make_string() - Make a comma-separated string of values from an IPP
51 * Include necessary headers...
63 #endif /* HAVE_NOTIFY_H */
66 # include <sys/cdefs.h>
67 # include <CoreFoundation/CoreFoundation.h>
68 # include <SystemConfiguration/SystemConfiguration.h>
69 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
70 # define kLocationHistoryArrayKey CFSTR("kLocationHistoryArrayKeyTMP")
71 # define kLocationNetworkKey CFSTR("kLocationNetworkKey")
72 # define kLocationPrinterIDKey CFSTR("kLocationPrinterIDKey")
73 # define kPMPrintingPreferences CFSTR("com.apple.print.PrintingPrefs")
74 # define kUseLastPrinterAsCurrentPrinterKey CFSTR("UseLastPrinterAsCurrentPrinter")
75 #endif /* __APPLE__ */
83 static CFArrayRef
appleCopyLocations(void);
84 static CFStringRef
appleCopyNetwork(void);
85 static char *appleGetPaperSize(char *name
, int namesize
);
86 static CFStringRef
appleGetPrinter(CFArrayRef locations
, CFStringRef network
,
88 static void appleSetDefault(const char *name
);
89 static int appleUseLastPrinter(void);
90 #endif /* __APPLE__ */
91 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
92 int *num_dests
, cups_dest_t
**dests
);
93 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
94 static int cups_find_dest(const char *name
, const char *instance
,
95 int num_dests
, cups_dest_t
*dests
, int prev
,
97 static char *cups_get_default(const char *filename
, char *namebuf
,
98 size_t namesize
, const char **instance
);
99 static int cups_get_dests(const char *filename
, const char *match_name
,
100 const char *match_inst
, int user_default_set
,
101 int num_dests
, cups_dest_t
**dests
);
102 static int cups_get_sdests(http_t
*http
, ipp_op_t op
, const char *name
,
103 int num_dests
, cups_dest_t
**dests
);
104 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
109 * 'cupsAddDest()' - Add a destination to the list of destinations.
111 * This function cannot be used to add a new class or printer queue,
112 * it only adds a new container of saved options for the named
113 * destination or instance.
115 * If the named destination already exists, the destination list is
116 * returned unchanged. Adding a new instance of a destination creates
117 * a copy of that destination's options.
119 * Use the @link cupsSaveDests@ function to save the updated list of
120 * destinations to the user's lpoptions file.
123 int /* O - New number of destinations */
124 cupsAddDest(const char *name
, /* I - Destination name */
125 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
126 int num_dests
, /* I - Number of destinations */
127 cups_dest_t
**dests
) /* IO - Destinations */
129 int i
; /* Looping var */
130 cups_dest_t
*dest
; /* Destination pointer */
131 cups_dest_t
*parent
= NULL
; /* Parent destination */
132 cups_option_t
*doption
, /* Current destination option */
133 *poption
; /* Current parent option */
139 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
142 (parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
)) == NULL
)
145 dest
= cups_add_dest(name
, instance
, &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
);
402 if (access(filename
, 0))
403 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
405 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
410 * Validate the current default destination - this prevents old
411 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
412 * pointing to a non-existent printer or class...
418 * See if we have a default printer...
421 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
424 * Have a default; see if it is real...
427 dest
= cupsGetDest(dest
->name
, NULL
, num_reals
, reals
);
431 * If dest is NULL, then no default (that exists) is set, so we
432 * need to set a default if one exists...
435 if (dest
== NULL
&& defprinter
!= NULL
)
437 for (i
= 0; i
< num_dests
; i
++)
438 (*dests
)[i
].is_default
= 0;
440 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
441 dest
->is_default
= 1;
452 * Return the number of destinations...
456 _cupsSetError(IPP_OK
, NULL
, 0);
463 * 'cupsGetNamedDest()' - Get options for the named destination.
465 * This function is optimized for retrieving a single destination and should
466 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
467 * know the name of the destination or want to print to the default destination.
468 * If @code NULL@ is returned, the destination does not exist or there is no
469 * default destination.
471 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
472 * server will be used.
474 * If "name" is @code NULL@, the default printer for the current user will be
477 * The returned destination must be freed using @link cupsFreeDests@ with a
478 * "num_dests" value of 1.
480 * @since CUPS 1.4/Mac OS X 10.6@
483 cups_dest_t
* /* O - Destination or @code NULL@ */
484 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
485 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
486 const char *instance
) /* I - Instance name or @code NULL@ */
488 cups_dest_t
*dest
; /* Destination */
489 char filename
[1024], /* Path to lpoptions */
490 defname
[256]; /* Default printer name */
491 const char *home
= getenv("HOME"); /* Home directory */
492 int set_as_default
= 0; /* Set returned destination as default */
493 ipp_op_t op
= IPP_GET_PRINTER_ATTRIBUTES
;
494 /* IPP operation to get server ops */
495 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
499 * If "name" is NULL, find the default destination...
505 name
= _cupsUserDefault(defname
, sizeof(defname
));
510 * No default in the environment, try the user's lpoptions files...
513 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
515 if ((name
= cups_get_default(filename
, defname
, sizeof(defname
),
518 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
519 name
= cups_get_default(filename
, defname
, sizeof(defname
),
527 * Still not there? Try the system lpoptions file...
530 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
531 cg
->cups_serverroot
);
532 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
538 * No locally-set default destination, ask the server...
541 op
= CUPS_GET_DEFAULT
;
546 * Get the printer's attributes...
549 if (!cups_get_sdests(http
, op
, name
, 0, &dest
))
551 if (op
== CUPS_GET_DEFAULT
|| name
)
555 * The default printer from environment variables or from a
556 * configuration file does not exist. Find out the real default.
559 if (!cups_get_sdests(http
, CUPS_GET_DEFAULT
, NULL
, 0, &dest
))
564 dest
->instance
= _cupsStrAlloc(instance
);
567 dest
->is_default
= 1;
570 * Then add local options...
573 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
574 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
578 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
580 if (access(filename
, 0))
581 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
583 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
587 * Return the result...
595 * 'cupsRemoveDest()' - Remove a destination from the destination list.
597 * Removing a destination/instance does not delete the class or printer
598 * queue, merely the lpoptions for that destination/instance. Use the
599 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
600 * options for the user.
602 * @since CUPS 1.3/Mac OS X 10.5@
605 int /* O - New number of destinations */
606 cupsRemoveDest(const char *name
, /* I - Destination name */
607 const char *instance
, /* I - Instance name or @code NULL@ */
608 int num_dests
, /* I - Number of destinations */
609 cups_dest_t
**dests
) /* IO - Destinations */
611 int i
; /* Index into destinations */
612 cups_dest_t
*dest
; /* Pointer to destination */
616 * Find the destination...
619 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
626 _cupsStrFree(dest
->name
);
627 _cupsStrFree(dest
->instance
);
628 cupsFreeOptions(dest
->num_options
, dest
->options
);
631 * Remove the destination from the array...
639 memmove(dest
, dest
+ 1, (num_dests
- i
) * sizeof(cups_dest_t
));
646 * 'cupsSetDefaultDest()' - Set the default destination.
648 * @since CUPS 1.3/Mac OS X 10.5@
653 const char *name
, /* I - Destination name */
654 const char *instance
, /* I - Instance name or @code NULL@ */
655 int num_dests
, /* I - Number of destinations */
656 cups_dest_t
*dests
) /* I - Destinations */
658 int i
; /* Looping var */
659 cups_dest_t
*dest
; /* Current destination */
663 * Range check input...
666 if (!name
|| num_dests
<= 0 || !dests
)
670 * Loop through the array and set the "is_default" flag for the matching
674 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
675 dest
->is_default
= !strcasecmp(name
, dest
->name
) &&
676 ((!instance
&& !dest
->instance
) ||
677 (instance
&& dest
->instance
&&
678 !strcasecmp(instance
, dest
->instance
)));
683 * 'cupsSetDests()' - Save the list of destinations for the default server.
685 * This function saves the destinations to /etc/cups/lpoptions when run
686 * as root and ~/.cups/lpoptions when run as a normal user.
690 cupsSetDests(int num_dests
, /* I - Number of destinations */
691 cups_dest_t
*dests
) /* I - Destinations */
693 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
698 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
700 * This function saves the destinations to /etc/cups/lpoptions when run
701 * as root and ~/.cups/lpoptions when run as a normal user.
703 * @since CUPS 1.1.21/Mac OS X 10.4@
706 int /* O - 0 on success, -1 on error */
707 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
708 int num_dests
, /* I - Number of destinations */
709 cups_dest_t
*dests
) /* I - Destinations */
711 int i
, j
; /* Looping vars */
712 int wrote
; /* Wrote definition? */
713 cups_dest_t
*dest
; /* Current destination */
714 cups_option_t
*option
; /* Current option */
715 _ipp_option_t
*match
; /* Matching attribute for option */
716 FILE *fp
; /* File pointer */
718 const char *home
; /* HOME environment variable */
720 char filename
[1024]; /* lpoptions file */
721 int num_temps
; /* Number of temporary destinations */
722 cups_dest_t
*temps
, /* Temporary destinations */
723 *temp
; /* Current temporary dest */
724 const char *val
; /* Value of temporary option */
725 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
729 * Range check the input...
732 if (!num_dests
|| !dests
)
736 * Get the server destinations...
739 num_temps
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, NULL
, 0, &temps
);
741 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
743 cupsFreeDests(num_temps
, temps
);
748 * Figure out which file to write to...
751 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
757 * Merge in server defaults...
760 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
763 * Point to user defaults...
766 if ((home
= getenv("HOME")) != NULL
)
769 * Remove the old ~/.lpoptions file...
772 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
776 * Create ~/.cups subdirectory...
779 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
780 if (access(filename
, 0))
781 mkdir(filename
, 0700);
783 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
789 * Try to open the file...
792 if ((fp
= fopen(filename
, "w")) == NULL
)
794 cupsFreeDests(num_temps
, temps
);
800 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
805 fchmod(fileno(fp
), 0644);
809 * Write each printer; each line looks like:
811 * Dest name[/instance] options
812 * Default name[/instance] options
815 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
816 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
818 if (dest
->is_default
)
820 fprintf(fp
, "Default %s", dest
->name
);
822 fprintf(fp
, "/%s", dest
->instance
);
829 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
830 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
832 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
835 * See if this option is a printer attribute; if so, skip it...
838 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
839 match
->group_tag
== IPP_TAG_PRINTER
)
843 * See if the server/global options match these; if so, don't
848 (val
= cupsGetOption(option
->name
, temp
->num_options
,
849 temp
->options
)) != NULL
&&
850 !strcasecmp(val
, option
->value
))
854 * Options don't match, write to the file...
859 fprintf(fp
, "Dest %s", dest
->name
);
861 fprintf(fp
, "/%s", dest
->instance
);
865 if (option
->value
[0])
867 if (strchr(option
->value
, ' ') ||
868 strchr(option
->value
, '\\') ||
869 strchr(option
->value
, '\"') ||
870 strchr(option
->value
, '\''))
876 fprintf(fp
, " %s=\"", option
->name
);
878 for (val
= option
->value
; *val
; val
++)
880 if (strchr("\"\'\\", *val
))
891 * Store the literal value...
894 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
898 fprintf(fp
, " %s", option
->name
);
906 * Free the temporary destinations and close the file...
909 cupsFreeDests(num_temps
, temps
);
915 * Set the default printer for this location - this allows command-line
916 * and GUI applications to share the same default destination...
919 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
920 appleSetDefault(dest
->name
);
921 #endif /* __APPLE__ */
923 #ifdef HAVE_NOTIFY_POST
925 * Send a notification so that MacOS X applications can know about the
929 notify_post("com.apple.printerListChange");
930 #endif /* HAVE_NOTIFY_POST */
937 * '_cupsUserDefault()' - Get the user default printer from environment
938 * variables and location information.
941 char * /* O - Default printer or NULL */
942 _cupsUserDefault(char *name
, /* I - Name buffer */
943 size_t namesize
) /* I - Size of name buffer */
945 const char *env
; /* LPDEST or PRINTER env variable */
947 CFStringRef network
; /* Network location */
948 CFArrayRef locations
; /* Location array */
949 CFStringRef locprinter
; /* Current printer */
950 #endif /* __APPLE__ */
953 if ((env
= getenv("LPDEST")) == NULL
)
954 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
959 strlcpy(name
, env
, namesize
);
965 * Use location-based defaults if "use last printer" is selected in the
966 * system preferences...
969 if (!appleUseLastPrinter())
971 DEBUG_puts("1_cupsUserDefault: Not using last printer as default...");
977 * Get the current location...
980 if ((network
= appleCopyNetwork()) == NULL
)
982 DEBUG_puts("1_cupsUserDefault: Unable to get current network...");
988 CFStringGetCString(network
, name
, namesize
, kCFStringEncodingUTF8
);
989 DEBUG_printf(("2_cupsUserDefault: network=\"%s\"", name
));
993 * Lookup the network in the preferences...
996 if ((locations
= appleCopyLocations()) == NULL
)
999 * Missing or bad location array, so no location-based default...
1002 DEBUG_puts("1_cupsUserDefault: Missing or bad location history array...");
1010 DEBUG_printf(("2_cupsUserDefault: Got location, %d entries...",
1011 (int)CFArrayGetCount(locations
)));
1013 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
1014 CFStringGetCString(locprinter
, name
, namesize
, kCFStringEncodingUTF8
);
1019 CFRelease(locations
);
1021 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\"...", name
));
1023 return (*name
? name
: NULL
);
1027 * No location-based defaults on this platform...
1032 #endif /* __APPLE__ */
1038 * 'appleCopyLocations()' - Copy the location history array.
1041 static CFArrayRef
/* O - Location array or NULL */
1042 appleCopyLocations(void)
1044 CFArrayRef locations
; /* Location array */
1048 * Look up the location array in the preferences...
1051 if ((locations
= CFPreferencesCopyAppValue(kLocationHistoryArrayKey
,
1052 kPMPrintingPreferences
)) == NULL
)
1055 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
1057 CFRelease(locations
);
1066 * 'appleCopyNetwork()' - Get the network ID for the current location.
1069 static CFStringRef
/* O - Network ID */
1070 appleCopyNetwork(void)
1072 SCDynamicStoreRef dynamicStore
; /* System configuration data */
1073 CFStringRef key
; /* Current network configuration key */
1074 CFDictionaryRef ip_dict
; /* Network configuration data */
1075 CFStringRef network
= NULL
; /* Current network ID */
1078 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("Printing"), NULL
,
1081 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
1082 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
1084 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
1086 if ((network
= CFDictionaryGetValue(ip_dict
,
1087 kSCPropNetIPv4Router
)) != NULL
)
1096 CFRelease(dynamicStore
);
1104 * 'appleGetPaperSize()' - Get the default paper size.
1107 static char * /* O - Default paper size */
1108 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
1109 int namesize
) /* I - Size of buffer */
1111 CFStringRef defaultPaperID
; /* Default paper ID */
1112 _cups_pwg_media_t
*pwgmedia
; /* PWG media size */
1115 defaultPaperID
= CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
1116 kPMPrintingPreferences
);
1117 if (!defaultPaperID
||
1118 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
1119 !CFStringGetCString(defaultPaperID
, name
, namesize
,
1120 kCFStringEncodingUTF8
))
1122 else if ((pwgmedia
= _cupsPWGMediaByLegacy(name
)) != NULL
)
1123 strlcpy(name
, pwgmedia
->pwg
, namesize
);
1126 CFRelease(defaultPaperID
);
1133 * 'appleGetPrinter()' - Get a printer from the history array.
1136 static CFStringRef
/* O - Printer name or NULL */
1137 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
1138 CFStringRef network
, /* I - Network name */
1139 CFIndex
*locindex
) /* O - Index in array */
1141 CFIndex i
, /* Looping var */
1142 count
; /* Number of locations */
1143 CFDictionaryRef location
; /* Current location */
1144 CFStringRef locnetwork
, /* Current network */
1145 locprinter
; /* Current printer */
1148 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
1149 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
1150 CFGetTypeID(location
) == CFDictionaryGetTypeID())
1152 if ((locnetwork
= CFDictionaryGetValue(location
,
1153 kLocationNetworkKey
)) != NULL
&&
1154 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
1155 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
1156 (locprinter
= CFDictionaryGetValue(location
,
1157 kLocationPrinterIDKey
)) != NULL
&&
1158 CFGetTypeID(locprinter
) == CFStringGetTypeID())
1163 return (locprinter
);
1172 * 'appleSetDefault()' - Set the default printer for this location.
1176 appleSetDefault(const char *name
) /* I - Default printer/class name */
1178 CFStringRef network
; /* Current network */
1179 CFArrayRef locations
; /* Old locations array */
1180 CFIndex locindex
; /* Index in locations array */
1181 CFStringRef locprinter
; /* Current printer */
1182 CFMutableArrayRef newlocations
; /* New locations array */
1183 CFMutableDictionaryRef newlocation
; /* New location */
1184 CFStringRef newprinter
; /* New printer */
1188 * Get the current location...
1191 if ((network
= appleCopyNetwork()) == NULL
)
1193 DEBUG_puts("1appleSetDefault: Unable to get current network...");
1197 if ((newprinter
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
1198 kCFStringEncodingUTF8
)) == NULL
)
1205 * Lookup the network in the preferences...
1208 if ((locations
= appleCopyLocations()) != NULL
)
1209 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
1217 CFStringCompare(locprinter
, newprinter
, 0) != kCFCompareEqualTo
)
1220 * Need to change the locations array...
1225 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
1229 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
1232 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, NULL
);
1234 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
1235 &kCFTypeDictionaryKeyCallBacks
,
1236 &kCFTypeDictionaryValueCallBacks
);
1238 if (newlocation
&& newlocations
)
1241 * Put the new location at the front of the array...
1244 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
1245 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, newprinter
);
1246 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
1249 * Limit the number of locations to 10...
1252 while (CFArrayGetCount(newlocations
) > 10)
1253 CFArrayRemoveValueAtIndex(newlocations
, 10);
1256 * Push the changes out...
1259 CFPreferencesSetAppValue(kLocationHistoryArrayKey
, newlocations
,
1260 kPMPrintingPreferences
);
1261 CFPreferencesAppSynchronize(kPMPrintingPreferences
);
1262 notify_post("com.apple.printerPrefsChange");
1266 CFRelease(newlocations
);
1269 CFRelease(newlocation
);
1273 CFRelease(locations
);
1276 CFRelease(newprinter
);
1281 * 'appleUseLastPrinter()' - Get the default printer preference value.
1284 static int /* O - 1 to use last printer, 0 otherwise */
1285 appleUseLastPrinter(void)
1287 CFPropertyListRef uselast
; /* Use last printer preference value */
1290 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
1293 if ((uselast
= CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey
,
1294 kPMPrintingPreferences
)) != NULL
)
1298 if (uselast
== kCFBooleanFalse
)
1304 #endif /* __APPLE__ */
1308 * 'cups_add_dest()' - Add a destination to the array.
1310 * Unlike cupsAddDest(), this function does not check for duplicates.
1313 static cups_dest_t
* /* O - New destination */
1314 cups_add_dest(const char *name
, /* I - Name of destination */
1315 const char *instance
, /* I - Instance or NULL */
1316 int *num_dests
, /* IO - Number of destinations */
1317 cups_dest_t
**dests
) /* IO - Destinations */
1319 int insert
, /* Insertion point */
1320 diff
; /* Result of comparison */
1321 cups_dest_t
*dest
; /* Destination pointer */
1325 * Add new destination...
1328 if (*num_dests
== 0)
1329 dest
= malloc(sizeof(cups_dest_t
));
1331 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (*num_dests
+ 1));
1339 * Find where to insert the destination...
1342 if (*num_dests
== 0)
1346 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
1354 * Move the array elements as needed...
1357 if (insert
< *num_dests
)
1358 memmove(*dests
+ insert
+ 1, *dests
+ insert
,
1359 (*num_dests
- insert
) * sizeof(cups_dest_t
));
1364 * Initialize the destination...
1367 dest
= *dests
+ insert
;
1368 dest
->name
= _cupsStrAlloc(name
);
1369 dest
->instance
= _cupsStrAlloc(instance
);
1370 dest
->is_default
= 0;
1371 dest
->num_options
= 0;
1372 dest
->options
= (cups_option_t
*)0;
1379 * 'cups_compare_dests()' - Compare two destinations.
1382 static int /* O - Result of comparison */
1383 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
1384 cups_dest_t
*b
) /* I - Second destination */
1386 int diff
; /* Difference */
1389 if ((diff
= strcasecmp(a
->name
, b
->name
)) != 0)
1391 else if (a
->instance
&& b
->instance
)
1392 return (strcasecmp(a
->instance
, b
->instance
));
1394 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
1399 * 'cups_find_dest()' - Find a destination using a binary search.
1402 static int /* O - Index of match */
1403 cups_find_dest(const char *name
, /* I - Destination name */
1404 const char *instance
, /* I - Instance or NULL */
1405 int num_dests
, /* I - Number of destinations */
1406 cups_dest_t
*dests
, /* I - Destinations */
1407 int prev
, /* I - Previous index */
1408 int *rdiff
) /* O - Difference of match */
1410 int left
, /* Low mark for binary search */
1411 right
, /* High mark for binary search */
1412 current
, /* Current index */
1413 diff
; /* Result of comparison */
1414 cups_dest_t key
; /* Search key */
1417 key
.name
= (char *)name
;
1418 key
.instance
= (char *)instance
;
1423 * Start search on either side of previous...
1426 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
1427 (diff
< 0 && prev
== 0) ||
1428 (diff
> 0 && prev
== (num_dests
- 1)))
1436 * Start with previous on right side...
1445 * Start wih previous on left side...
1449 right
= num_dests
- 1;
1455 * Start search in the middle...
1459 right
= num_dests
- 1;
1464 current
= (left
+ right
) / 2;
1465 diff
= cups_compare_dests(&key
, dests
+ current
);
1474 while ((right
- left
) > 1);
1479 * Check the last 1 or 2 elements...
1482 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
1486 diff
= cups_compare_dests(&key
, dests
+ right
);
1492 * Return the closest destination and the difference...
1502 * 'cups_get_default()' - Get the default destination from an lpoptions file.
1505 static char * /* O - Default destination or NULL */
1506 cups_get_default(const char *filename
, /* I - File to read */
1507 char *namebuf
, /* I - Name buffer */
1508 size_t namesize
, /* I - Size of name buffer */
1509 const char **instance
) /* I - Instance */
1511 cups_file_t
*fp
; /* lpoptions file */
1512 char line
[8192], /* Line from file */
1513 *value
, /* Value for line */
1514 *nameptr
; /* Pointer into name */
1515 int linenum
; /* Current line */
1520 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1524 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1526 if (!strcasecmp(line
, "default") && value
)
1528 strlcpy(namebuf
, value
, namesize
);
1530 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
1532 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
1535 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
1538 *instance
= nameptr
;
1546 return (*namebuf
? namebuf
: NULL
);
1551 * 'cups_get_dests()' - Get destinations from a file.
1554 static int /* O - Number of destinations */
1556 const char *filename
, /* I - File to read from */
1557 const char *match_name
, /* I - Destination name we want */
1558 const char *match_inst
, /* I - Instance name we want */
1559 int user_default_set
, /* I - User default printer set? */
1560 int num_dests
, /* I - Number of destinations */
1561 cups_dest_t
**dests
) /* IO - Destinations */
1563 int i
; /* Looping var */
1564 cups_dest_t
*dest
; /* Current destination */
1565 cups_file_t
*fp
; /* File pointer */
1566 char line
[8192], /* Line from file */
1567 *lineptr
, /* Pointer into line */
1568 *name
, /* Name of destination/option */
1569 *instance
; /* Instance of destination */
1570 int linenum
; /* Current line number */
1573 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", "
1574 "match_inst=\"%s\", user_default_set=%d, num_dests=%d, "
1575 "dests=%p)", filename
, match_name
, match_inst
,
1576 user_default_set
, num_dests
, dests
));
1579 * Try to open the file...
1582 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1586 * Read each printer; each line looks like:
1588 * Dest name[/instance] options
1589 * Default name[/instance] options
1594 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
1597 * See what type of line it is...
1600 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
1601 linenum
, line
, lineptr
));
1603 if ((strcasecmp(line
, "dest") && strcasecmp(line
, "default")) || !lineptr
)
1605 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
1612 * Search for an instance...
1615 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
1618 if (*lineptr
== '/')
1621 * Found an instance...
1628 * Search for an instance...
1631 while (!isspace(*lineptr
& 255) && *lineptr
)
1640 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
1644 * See if the primary instance of the destination exists; if not,
1645 * ignore this entry and move on...
1650 if (strcasecmp(name
, match_name
) ||
1651 (!instance
&& match_inst
) ||
1652 (instance
&& !match_inst
) ||
1653 (instance
&& strcasecmp(instance
, match_inst
)))
1658 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
1660 DEBUG_puts("9cups_get_dests: Not found!");
1666 * Add the destination...
1669 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
1671 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1677 DEBUG_puts("9cups_get_dests: Out of memory!");
1683 * Add options until we hit the end of the line...
1686 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
1690 * If we found what we were looking for, stop now...
1697 * Set this as default if needed...
1700 if (!user_default_set
&& !strcasecmp(line
, "default"))
1702 DEBUG_puts("9cups_get_dests: Setting as default...");
1704 for (i
= 0; i
< num_dests
; i
++)
1705 (*dests
)[i
].is_default
= 0;
1707 dest
->is_default
= 1;
1712 * Close the file and return...
1722 * 'cups_get_sdests()' - Get destinations from a server.
1725 static int /* O - Number of destinations */
1726 cups_get_sdests(http_t
*http
, /* I - Connection to server or CUPS_HTTP_DEFAULT */
1727 ipp_op_t op
, /* I - IPP operation */
1728 const char *name
, /* I - Name of destination */
1729 int num_dests
, /* I - Number of destinations */
1730 cups_dest_t
**dests
) /* IO - Destinations */
1732 cups_dest_t
*dest
; /* Current destination */
1733 ipp_t
*request
, /* IPP Request */
1734 *response
; /* IPP Response */
1735 ipp_attribute_t
*attr
; /* Current attribute */
1736 const char *printer_name
; /* printer-name attribute */
1737 char uri
[1024]; /* printer-uri value */
1738 int num_options
; /* Number of options */
1739 cups_option_t
*options
; /* Options */
1741 char media_default
[41]; /* Default paper size */
1742 #endif /* __APPLE__ */
1743 char optname
[1024], /* Option name */
1744 value
[2048], /* Option value */
1745 *ptr
; /* Pointer into name/value */
1746 static const char * const pattrs
[] = /* Attributes we're interested in */
1748 "auth-info-required",
1750 "job-sheets-default",
1751 "marker-change-time",
1753 "marker-high-levels",
1755 "marker-low-levels",
1761 #endif /* __APPLE__ */
1765 "printer-is-accepting-jobs",
1766 "printer-is-shared",
1768 "printer-make-and-model",
1771 "printer-state-change-time",
1772 "printer-state-reasons",
1774 "printer-uri-supported"
1780 * Get the default paper size...
1783 appleGetPaperSize(media_default
, sizeof(media_default
));
1784 #endif /* __APPLE__ */
1787 * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which
1788 * require the following attributes:
1790 * attributes-charset
1791 * attributes-natural-language
1792 * requesting-user-name
1793 * printer-uri [for IPP_GET_PRINTER_ATTRIBUTES]
1796 request
= ippNewRequest(op
);
1798 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1799 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1802 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1803 "requesting-user-name", NULL
, cupsUser());
1805 if (name
&& op
!= CUPS_GET_DEFAULT
)
1807 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1808 "localhost", ippPort(), "/printers/%s", name
);
1809 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1814 * Do the request and get back a response...
1817 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1819 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1822 * Skip leading attributes until we hit a printer...
1825 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1832 * Pull the needed attributes from this printer...
1835 printer_name
= NULL
;
1839 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
1841 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
1842 attr
->value_tag
!= IPP_TAG_ENUM
&&
1843 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1844 attr
->value_tag
!= IPP_TAG_TEXT
&&
1845 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
1846 attr
->value_tag
!= IPP_TAG_NAME
&&
1847 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
1848 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
1849 attr
->value_tag
!= IPP_TAG_RANGE
&&
1850 attr
->value_tag
!= IPP_TAG_URI
)
1853 if (!strcmp(attr
->name
, "auth-info-required") ||
1854 !strcmp(attr
->name
, "device-uri") ||
1855 !strcmp(attr
->name
, "marker-change-time") ||
1856 !strcmp(attr
->name
, "marker-colors") ||
1857 !strcmp(attr
->name
, "marker-high-levels") ||
1858 !strcmp(attr
->name
, "marker-levels") ||
1859 !strcmp(attr
->name
, "marker-low-levels") ||
1860 !strcmp(attr
->name
, "marker-message") ||
1861 !strcmp(attr
->name
, "marker-names") ||
1862 !strcmp(attr
->name
, "marker-types") ||
1863 !strcmp(attr
->name
, "printer-commands") ||
1864 !strcmp(attr
->name
, "printer-info") ||
1865 !strcmp(attr
->name
, "printer-is-shared") ||
1866 !strcmp(attr
->name
, "printer-make-and-model") ||
1867 !strcmp(attr
->name
, "printer-state") ||
1868 !strcmp(attr
->name
, "printer-state-change-time") ||
1869 !strcmp(attr
->name
, "printer-type") ||
1870 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
1871 !strcmp(attr
->name
, "printer-location") ||
1872 !strcmp(attr
->name
, "printer-state-reasons") ||
1873 !strcmp(attr
->name
, "printer-uri-supported"))
1876 * Add a printer description attribute...
1879 num_options
= cupsAddOption(attr
->name
,
1880 cups_make_string(attr
, value
,
1882 num_options
, &options
);
1885 else if (!strcmp(attr
->name
, "media-supported"))
1888 * See if we can set a default media size...
1891 int i
; /* Looping var */
1893 for (i
= 0; i
< attr
->num_values
; i
++)
1894 if (!strcasecmp(media_default
, attr
->values
[i
].string
.text
))
1896 num_options
= cupsAddOption("media", media_default
, num_options
,
1901 #endif /* __APPLE__ */
1902 else if (!strcmp(attr
->name
, "printer-name") &&
1903 attr
->value_tag
== IPP_TAG_NAME
)
1904 printer_name
= attr
->values
[0].string
.text
;
1905 else if (strncmp(attr
->name
, "notify-", 7) &&
1906 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1907 attr
->value_tag
== IPP_TAG_ENUM
||
1908 attr
->value_tag
== IPP_TAG_INTEGER
||
1909 attr
->value_tag
== IPP_TAG_KEYWORD
||
1910 attr
->value_tag
== IPP_TAG_NAME
||
1911 attr
->value_tag
== IPP_TAG_RANGE
) &&
1912 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
1915 * Add a default option...
1918 strlcpy(optname
, attr
->name
, sizeof(optname
));
1919 optname
[ptr
- attr
->name
] = '\0';
1921 if (strcasecmp(optname
, "media") ||
1922 !cupsGetOption("media", num_options
, options
))
1923 num_options
= cupsAddOption(optname
,
1924 cups_make_string(attr
, value
,
1926 num_options
, &options
);
1931 * See if we have everything needed...
1936 cupsFreeOptions(num_options
, options
);
1944 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
1946 dest
->num_options
= num_options
;
1947 dest
->options
= options
;
1950 cupsFreeOptions(num_options
, options
);
1956 ippDelete(response
);
1960 * Return the count...
1968 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
1972 static char * /* O - New string */
1974 ipp_attribute_t
*attr
, /* I - Attribute to convert */
1975 char *buffer
, /* I - Buffer */
1976 size_t bufsize
) /* I - Size of buffer */
1978 int i
; /* Looping var */
1979 char *ptr
, /* Pointer into buffer */
1980 *end
, /* Pointer to end of buffer */
1981 *valptr
; /* Pointer into string attribute */
1985 * Return quickly if we have a single string value...
1988 if (attr
->num_values
== 1 &&
1989 attr
->value_tag
!= IPP_TAG_INTEGER
&&
1990 attr
->value_tag
!= IPP_TAG_ENUM
&&
1991 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
1992 attr
->value_tag
!= IPP_TAG_RANGE
)
1993 return (attr
->values
[0].string
.text
);
1996 * Copy the values to the string, separating with commas and escaping strings
2000 end
= buffer
+ bufsize
- 1;
2002 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
2007 switch (attr
->value_tag
)
2009 case IPP_TAG_INTEGER
:
2011 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].integer
);
2014 case IPP_TAG_BOOLEAN
:
2015 if (attr
->values
[i
].boolean
)
2016 strlcpy(ptr
, "true", end
- ptr
+ 1);
2018 strlcpy(ptr
, "false", end
- ptr
+ 1);
2021 case IPP_TAG_RANGE
:
2022 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2023 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].range
.lower
);
2025 snprintf(ptr
, end
- ptr
+ 1, "%d-%d", attr
->values
[i
].range
.lower
,
2026 attr
->values
[i
].range
.upper
);
2030 for (valptr
= attr
->values
[i
].string
.text
;
2031 *valptr
&& ptr
< end
;)
2033 if (strchr(" \t\n\\\'\"", *valptr
))
2035 if (ptr
>= (end
- 1))
2058 * End of "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $".