2 * "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $"
4 * User-defined destination (and option) support for CUPS.
6 * Copyright 2007-2011 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
21 * _cupsAppleCopyDefaultPaperID() - Get the default paper ID.
22 * _cupsAppleGetUseLastPrinter() - Get whether to use the last used printer.
23 * _cupsAppleSetDefaultPaperID() - Set the default paper id.
24 * _cupsAppleSetUseLastPrinter() - Set whether to use the last used printer.
25 * cupsFreeDests() - Free the memory used by the list of
27 * cupsGetDest() - Get the named destination from the list.
28 * _cupsGetDests() - Get destinations from a server.
29 * cupsGetDests() - Get the list of destinations from the
31 * cupsGetDests2() - Get the list of destinations from the
33 * cupsGetNamedDest() - Get options for the named destination.
34 * cupsRemoveDest() - Remove a destination from the destination
36 * cupsSetDefaultDest() - Set the default destination.
37 * cupsSetDests() - Save the list of destinations for the
39 * cupsSetDests2() - Save the list of destinations for the
41 * _cupsUserDefault() - Get the user default printer from
42 * environment variables and location
44 * appleCopyLocations() - Copy the location history array.
45 * appleCopyNetwork() - Get the network ID for the current
47 * appleGetPaperSize() - Get the default paper size.
48 * appleGetPrinter() - Get a printer from the history array.
49 * appleSetDefault() - Set the default printer for this location.
50 * cups_add_dest() - Add a destination to the array.
51 * cups_compare_dests() - Compare two destinations.
52 * cups_find_dest() - Find a destination using a binary search.
53 * cups_get_default() - Get the default destination from an
55 * cups_get_dests() - Get destinations from a file.
56 * cups_make_string() - Make a comma-separated string of values
57 * from an IPP attribute.
61 * Include necessary headers...
64 #include "cups-private.h"
69 #endif /* HAVE_NOTIFY_H */
72 # include <SystemConfiguration/SystemConfiguration.h>
73 # define kDefaultPaperIDKey CFSTR("DefaultPaperID")
74 # define kLocationHistoryArrayKey CFSTR("kLocationHistoryArrayKeyTMP")
75 # define kLocationNetworkKey CFSTR("kLocationNetworkKey")
76 # define kLocationPrinterIDKey CFSTR("kLocationPrinterIDKey")
77 # define kPMPrintingPreferences CFSTR("com.apple.print.PrintingPrefs")
78 # define kCUPSPrintingPreferences CFSTR("org.cups.PrintingPrefs")
79 # define kUseLastPrinterAsCurrentPrinterKey CFSTR("UseLastPrinterAsCurrentPrinter")
80 #endif /* __APPLE__ */
88 static CFArrayRef
appleCopyLocations(void);
89 static CFStringRef
appleCopyNetwork(void);
90 static char *appleGetPaperSize(char *name
, int namesize
);
91 static CFStringRef
appleGetPrinter(CFArrayRef locations
, CFStringRef network
,
93 static void appleSetDefault(const char *name
);
94 #endif /* __APPLE__ */
95 static cups_dest_t
*cups_add_dest(const char *name
, const char *instance
,
96 int *num_dests
, cups_dest_t
**dests
);
97 static int cups_compare_dests(cups_dest_t
*a
, cups_dest_t
*b
);
98 static int cups_find_dest(const char *name
, const char *instance
,
99 int num_dests
, cups_dest_t
*dests
, int prev
,
101 static char *cups_get_default(const char *filename
, char *namebuf
,
102 size_t namesize
, const char **instance
);
103 static int cups_get_dests(const char *filename
, const char *match_name
,
104 const char *match_inst
, int user_default_set
,
105 int num_dests
, cups_dest_t
**dests
);
106 static char *cups_make_string(ipp_attribute_t
*attr
, char *buffer
,
111 * 'cupsAddDest()' - Add a destination to the list of destinations.
113 * This function cannot be used to add a new class or printer queue,
114 * it only adds a new container of saved options for the named
115 * destination or instance.
117 * If the named destination already exists, the destination list is
118 * returned unchanged. Adding a new instance of a destination creates
119 * a copy of that destination's options.
121 * Use the @link cupsSaveDests@ function to save the updated list of
122 * destinations to the user's lpoptions file.
125 int /* O - New number of destinations */
126 cupsAddDest(const char *name
, /* I - Destination name */
127 const char *instance
, /* I - Instance name or @code NULL@ for none/primary */
128 int num_dests
, /* I - Number of destinations */
129 cups_dest_t
**dests
) /* IO - Destinations */
131 int i
; /* Looping var */
132 cups_dest_t
*dest
; /* Destination pointer */
133 cups_dest_t
*parent
= NULL
; /* Parent destination */
134 cups_option_t
*doption
, /* Current destination option */
135 *poption
; /* Current parent option */
141 if (!cupsGetDest(name
, instance
, num_dests
, *dests
))
143 if (instance
&& !cupsGetDest(name
, NULL
, num_dests
, *dests
))
146 dest
= cups_add_dest(name
, instance
, &num_dests
, dests
);
149 * Find the base dest again now the array has been realloc'd.
152 parent
= cupsGetDest(name
, NULL
, num_dests
, *dests
);
154 if (instance
&& parent
&& parent
->num_options
> 0)
157 * Copy options from parent...
160 dest
->options
= calloc(sizeof(cups_option_t
), parent
->num_options
);
164 dest
->num_options
= parent
->num_options
;
166 for (i
= dest
->num_options
, doption
= dest
->options
,
167 poption
= parent
->options
;
169 i
--, doption
++, poption
++)
171 doption
->name
= _cupsStrRetain(poption
->name
);
172 doption
->value
= _cupsStrRetain(poption
->value
);
184 * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
187 CFStringRef
/* O - Default paper ID */
188 _cupsAppleCopyDefaultPaperID(void)
190 CFStringRef paper
; /* Default paper ID */
193 paper
= CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
194 kCUPSPrintingPreferences
);
197 paper
= CFPreferencesCopyAppValue(kDefaultPaperIDKey
,
198 kPMPrintingPreferences
);
205 * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
208 int /* O - 1 to use last printer, 0 otherwise */
209 _cupsAppleGetUseLastPrinter(void)
211 Boolean uselast
, /* Use last printer preference value */
212 uselast_set
; /* Valid is set? */
215 if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
218 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinterAsCurrentPrinterKey
,
219 kCUPSPrintingPreferences
,
222 uselast
= CFPreferencesGetAppBooleanValue(kUseLastPrinterAsCurrentPrinterKey
,
223 kPMPrintingPreferences
,
233 * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
237 _cupsAppleSetDefaultPaperID(
238 CFStringRef name
) /* I - New paper ID */
240 CFPreferencesSetAppValue(kDefaultPaperIDKey
, name
, kCUPSPrintingPreferences
);
245 * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
249 _cupsAppleSetUseLastPrinter(
250 int uselast
) /* O - 1 to use last printer, 0 otherwise */
252 CFPreferencesSetAppValue(kUseLastPrinterAsCurrentPrinterKey
,
253 uselast
? kCFBooleanTrue
: kCFBooleanFalse
,
254 kCUPSPrintingPreferences
);
256 #endif /* __APPLE__ */
260 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
264 cupsFreeDests(int num_dests
, /* I - Number of destinations */
265 cups_dest_t
*dests
) /* I - Destinations */
267 int i
; /* Looping var */
268 cups_dest_t
*dest
; /* Current destination */
271 if (num_dests
== 0 || dests
== NULL
)
274 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
276 _cupsStrFree(dest
->name
);
277 _cupsStrFree(dest
->instance
);
279 cupsFreeOptions(dest
->num_options
, dest
->options
);
287 * 'cupsGetDest()' - Get the named destination from the list.
289 * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
290 * list of supported destinations for the current user.
293 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
294 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
295 const char *instance
, /* I - Instance name or @code NULL@ */
296 int num_dests
, /* I - Number of destinations */
297 cups_dest_t
*dests
) /* I - Destinations */
299 int diff
, /* Result of comparison */
300 match
; /* Matching index */
303 if (num_dests
<= 0 || !dests
)
309 * NULL name for default printer.
312 while (num_dests
> 0)
314 if (dests
->is_default
)
324 * Lookup name and optionally the instance...
327 match
= cups_find_dest(name
, instance
, num_dests
, dests
, -1, &diff
);
330 return (dests
+ match
);
338 * '_cupsGetDests()' - Get destinations from a server.
340 * "op" is CUPS_GET_PRINTERS to get a full list, CUPS_GET_DEFAULT to get the
341 * system-wide default printer, or IPP_GET_PRINTER_ATTRIBUTES for a known
344 * "name" is the name of an existing printer and is only used when "op" is
345 * IPP_GET_PRINTER_ATTRIBUTES.
347 * "dest" is initialized to point to the array of destinations.
349 * 0 is returned if there are no printers, no default printer, or the named
350 * printer does not exist, respectively.
352 * Free the memory used by the destination array using the @link cupsFreeDests@
355 * Note: On Mac OS X this function also gets the default paper from the system
356 * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
357 * options array for each destination that supports it.
360 int /* O - Number of destinations */
361 _cupsGetDests(http_t
*http
, /* I - Connection to server or CUPS_HTTP_DEFAULT */
362 ipp_op_t op
, /* I - IPP operation */
363 const char *name
, /* I - Name of destination */
364 cups_dest_t
**dests
) /* IO - Destinations */
366 int num_dests
= 0; /* Number of destinations */
367 cups_dest_t
*dest
; /* Current destination */
368 ipp_t
*request
, /* IPP Request */
369 *response
; /* IPP Response */
370 ipp_attribute_t
*attr
; /* Current attribute */
371 const char *printer_name
; /* printer-name attribute */
372 char uri
[1024]; /* printer-uri value */
373 int num_options
; /* Number of options */
374 cups_option_t
*options
; /* Options */
376 char media_default
[41]; /* Default paper size */
377 #endif /* __APPLE__ */
378 char optname
[1024], /* Option name */
379 value
[2048], /* Option value */
380 *ptr
; /* Pointer into name/value */
381 static const char * const pattrs
[] = /* Attributes we're interested in */
383 "auth-info-required",
385 "job-sheets-default",
386 "marker-change-time",
388 "marker-high-levels",
396 #endif /* __APPLE__ */
400 "printer-is-accepting-jobs",
403 "printer-make-and-model",
406 "printer-state-change-time",
407 "printer-state-reasons",
409 "printer-uri-supported"
415 * Get the default paper size...
418 appleGetPaperSize(media_default
, sizeof(media_default
));
419 #endif /* __APPLE__ */
422 * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which
423 * require the following attributes:
426 * attributes-natural-language
427 * requesting-user-name
428 * printer-uri [for IPP_GET_PRINTER_ATTRIBUTES]
431 request
= ippNewRequest(op
);
433 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
434 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
437 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
438 "requesting-user-name", NULL
, cupsUser());
440 if (name
&& op
!= CUPS_GET_DEFAULT
)
442 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
443 "localhost", ippPort(), "/printers/%s", name
);
444 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
449 * Do the request and get back a response...
452 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
454 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
457 * Skip leading attributes until we hit a printer...
460 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
467 * Pull the needed attributes from this printer...
474 for (; attr
&& attr
->group_tag
== IPP_TAG_PRINTER
; attr
= attr
->next
)
476 if (attr
->value_tag
!= IPP_TAG_INTEGER
&&
477 attr
->value_tag
!= IPP_TAG_ENUM
&&
478 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
479 attr
->value_tag
!= IPP_TAG_TEXT
&&
480 attr
->value_tag
!= IPP_TAG_TEXTLANG
&&
481 attr
->value_tag
!= IPP_TAG_NAME
&&
482 attr
->value_tag
!= IPP_TAG_NAMELANG
&&
483 attr
->value_tag
!= IPP_TAG_KEYWORD
&&
484 attr
->value_tag
!= IPP_TAG_RANGE
&&
485 attr
->value_tag
!= IPP_TAG_URI
)
488 if (!strcmp(attr
->name
, "auth-info-required") ||
489 !strcmp(attr
->name
, "device-uri") ||
490 !strcmp(attr
->name
, "marker-change-time") ||
491 !strcmp(attr
->name
, "marker-colors") ||
492 !strcmp(attr
->name
, "marker-high-levels") ||
493 !strcmp(attr
->name
, "marker-levels") ||
494 !strcmp(attr
->name
, "marker-low-levels") ||
495 !strcmp(attr
->name
, "marker-message") ||
496 !strcmp(attr
->name
, "marker-names") ||
497 !strcmp(attr
->name
, "marker-types") ||
498 !strcmp(attr
->name
, "printer-commands") ||
499 !strcmp(attr
->name
, "printer-info") ||
500 !strcmp(attr
->name
, "printer-is-shared") ||
501 !strcmp(attr
->name
, "printer-make-and-model") ||
502 !strcmp(attr
->name
, "printer-state") ||
503 !strcmp(attr
->name
, "printer-state-change-time") ||
504 !strcmp(attr
->name
, "printer-type") ||
505 !strcmp(attr
->name
, "printer-is-accepting-jobs") ||
506 !strcmp(attr
->name
, "printer-location") ||
507 !strcmp(attr
->name
, "printer-state-reasons") ||
508 !strcmp(attr
->name
, "printer-uri-supported"))
511 * Add a printer description attribute...
514 num_options
= cupsAddOption(attr
->name
,
515 cups_make_string(attr
, value
,
517 num_options
, &options
);
520 else if (!strcmp(attr
->name
, "media-supported"))
523 * See if we can set a default media size...
526 int i
; /* Looping var */
528 for (i
= 0; i
< attr
->num_values
; i
++)
529 if (!strcasecmp(media_default
, attr
->values
[i
].string
.text
))
531 num_options
= cupsAddOption("media", media_default
, num_options
,
536 #endif /* __APPLE__ */
537 else if (!strcmp(attr
->name
, "printer-name") &&
538 attr
->value_tag
== IPP_TAG_NAME
)
539 printer_name
= attr
->values
[0].string
.text
;
540 else if (strncmp(attr
->name
, "notify-", 7) &&
541 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
542 attr
->value_tag
== IPP_TAG_ENUM
||
543 attr
->value_tag
== IPP_TAG_INTEGER
||
544 attr
->value_tag
== IPP_TAG_KEYWORD
||
545 attr
->value_tag
== IPP_TAG_NAME
||
546 attr
->value_tag
== IPP_TAG_RANGE
) &&
547 (ptr
= strstr(attr
->name
, "-default")) != NULL
)
550 * Add a default option...
553 strlcpy(optname
, attr
->name
, sizeof(optname
));
554 optname
[ptr
- attr
->name
] = '\0';
556 if (strcasecmp(optname
, "media") ||
557 !cupsGetOption("media", num_options
, options
))
558 num_options
= cupsAddOption(optname
,
559 cups_make_string(attr
, value
,
561 num_options
, &options
);
566 * See if we have everything needed...
571 cupsFreeOptions(num_options
, options
);
579 if ((dest
= cups_add_dest(printer_name
, NULL
, &num_dests
, dests
)) != NULL
)
581 dest
->num_options
= num_options
;
582 dest
->options
= options
;
585 cupsFreeOptions(num_options
, options
);
595 * Return the count...
603 * 'cupsGetDests()' - Get the list of destinations from the default server.
605 * Starting with CUPS 1.2, the returned list of destinations include the
606 * printer-info, printer-is-accepting-jobs, printer-is-shared,
607 * printer-make-and-model, printer-state, printer-state-change-time,
608 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
609 * adds the marker-change-time, marker-colors, marker-high-levels,
610 * marker-levels, marker-low-levels, marker-message, marker-names,
611 * marker-types, and printer-commands attributes as well.
613 * Use the @link cupsFreeDests@ function to free the destination list and
614 * the @link cupsGetDest@ function to find a particular destination.
617 int /* O - Number of destinations */
618 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
620 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
625 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
627 * Starting with CUPS 1.2, the returned list of destinations include the
628 * printer-info, printer-is-accepting-jobs, printer-is-shared,
629 * printer-make-and-model, printer-state, printer-state-change-time,
630 * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
631 * adds the marker-change-time, marker-colors, marker-high-levels,
632 * marker-levels, marker-low-levels, marker-message, marker-names,
633 * marker-types, and printer-commands attributes as well.
635 * Use the @link cupsFreeDests@ function to free the destination list and
636 * the @link cupsGetDest@ function to find a particular destination.
638 * @since CUPS 1.1.21/Mac OS X 10.4@
641 int /* O - Number of destinations */
642 cupsGetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
643 cups_dest_t
**dests
) /* O - Destinations */
645 int i
; /* Looping var */
646 int num_dests
; /* Number of destinations */
647 cups_dest_t
*dest
; /* Destination pointer */
648 const char *home
; /* HOME environment variable */
649 char filename
[1024]; /* Local ~/.cups/lpoptions file */
650 const char *defprinter
; /* Default printer */
651 char name
[1024], /* Copy of printer name */
652 *instance
, /* Pointer to instance name */
653 *user_default
; /* User default printer */
654 int num_reals
; /* Number of real queues */
655 cups_dest_t
*reals
; /* Real queues */
656 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
660 * Range check the input...
665 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad NULL dests pointer"), 1);
670 * Grab the printers and classes...
673 *dests
= (cups_dest_t
*)0;
674 num_dests
= _cupsGetDests(http
, CUPS_GET_PRINTERS
, NULL
, dests
);
676 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
678 cupsFreeDests(num_dests
, *dests
);
679 *dests
= (cups_dest_t
*)0;
684 * Make a copy of the "real" queues for a later sanity check...
689 num_reals
= num_dests
;
690 reals
= calloc(num_reals
, sizeof(cups_dest_t
));
693 memcpy(reals
, *dests
, num_reals
* sizeof(cups_dest_t
));
704 * Grab the default destination...
707 if ((user_default
= _cupsUserDefault(name
, sizeof(name
))) != NULL
)
709 else if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
711 strlcpy(name
, defprinter
, sizeof(name
));
718 * Separate printer and instance name...
721 if ((instance
= strchr(name
, '/')) != NULL
)
725 * Lookup the printer and instance and make it the default...
728 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
729 dest
->is_default
= 1;
735 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
738 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
739 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
742 if ((home
= getenv("HOME")) != NULL
)
744 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
746 num_dests
= cups_get_dests(filename
, NULL
, NULL
, user_default
!= NULL
,
751 * Validate the current default destination - this prevents old
752 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
753 * pointing to a non-existent printer or class...
759 * See if we have a default printer...
762 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
765 * Have a default; see if it is real...
768 dest
= cupsGetDest(dest
->name
, NULL
, num_reals
, reals
);
772 * If dest is NULL, then no default (that exists) is set, so we
773 * need to set a default if one exists...
776 if (dest
== NULL
&& defprinter
!= NULL
)
778 for (i
= 0; i
< num_dests
; i
++)
779 (*dests
)[i
].is_default
= 0;
781 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
782 dest
->is_default
= 1;
793 * Return the number of destinations...
797 _cupsSetError(IPP_OK
, NULL
, 0);
804 * 'cupsGetNamedDest()' - Get options for the named destination.
806 * This function is optimized for retrieving a single destination and should
807 * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either
808 * know the name of the destination or want to print to the default destination.
809 * If @code NULL@ is returned, the destination does not exist or there is no
810 * default destination.
812 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
813 * server will be used.
815 * If "name" is @code NULL@, the default printer for the current user will be
818 * The returned destination must be freed using @link cupsFreeDests@ with a
819 * "num_dests" value of 1.
821 * @since CUPS 1.4/Mac OS X 10.6@
824 cups_dest_t
* /* O - Destination or @code NULL@ */
825 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
826 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
827 const char *instance
) /* I - Instance name or @code NULL@ */
829 cups_dest_t
*dest
; /* Destination */
830 char filename
[1024], /* Path to lpoptions */
831 defname
[256]; /* Default printer name */
832 const char *home
= getenv("HOME"); /* Home directory */
833 int set_as_default
= 0; /* Set returned destination as default */
834 ipp_op_t op
= IPP_GET_PRINTER_ATTRIBUTES
;
835 /* IPP operation to get server ops */
836 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
840 * If "name" is NULL, find the default destination...
846 name
= _cupsUserDefault(defname
, sizeof(defname
));
850 char *ptr
; /* Temporary pointer... */
852 if ((ptr
= strchr(defname
, '/')) != NULL
)
863 * No default in the environment, try the user's lpoptions files...
866 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
868 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
874 * Still not there? Try the system lpoptions file...
877 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
878 cg
->cups_serverroot
);
879 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
885 * No locally-set default destination, ask the server...
888 op
= CUPS_GET_DEFAULT
;
893 * Get the printer's attributes...
896 if (!_cupsGetDests(http
, op
, name
, &dest
))
898 if (op
== CUPS_GET_DEFAULT
|| (name
&& !set_as_default
))
902 * The default printer from environment variables or from a
903 * configuration file does not exist. Find out the real default.
906 if (!_cupsGetDests(http
, CUPS_GET_DEFAULT
, NULL
, &dest
))
911 dest
->instance
= _cupsStrAlloc(instance
);
914 dest
->is_default
= 1;
917 * Then add local options...
920 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
921 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
925 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
927 cups_get_dests(filename
, name
, instance
, 1, 1, &dest
);
931 * Return the result...
939 * 'cupsRemoveDest()' - Remove a destination from the destination list.
941 * Removing a destination/instance does not delete the class or printer
942 * queue, merely the lpoptions for that destination/instance. Use the
943 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
944 * options for the user.
946 * @since CUPS 1.3/Mac OS X 10.5@
949 int /* O - New number of destinations */
950 cupsRemoveDest(const char *name
, /* I - Destination name */
951 const char *instance
, /* I - Instance name or @code NULL@ */
952 int num_dests
, /* I - Number of destinations */
953 cups_dest_t
**dests
) /* IO - Destinations */
955 int i
; /* Index into destinations */
956 cups_dest_t
*dest
; /* Pointer to destination */
960 * Find the destination...
963 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
970 _cupsStrFree(dest
->name
);
971 _cupsStrFree(dest
->instance
);
972 cupsFreeOptions(dest
->num_options
, dest
->options
);
975 * Remove the destination from the array...
983 memmove(dest
, dest
+ 1, (num_dests
- i
) * sizeof(cups_dest_t
));
990 * 'cupsSetDefaultDest()' - Set the default destination.
992 * @since CUPS 1.3/Mac OS X 10.5@
997 const char *name
, /* I - Destination name */
998 const char *instance
, /* I - Instance name or @code NULL@ */
999 int num_dests
, /* I - Number of destinations */
1000 cups_dest_t
*dests
) /* I - Destinations */
1002 int i
; /* Looping var */
1003 cups_dest_t
*dest
; /* Current destination */
1007 * Range check input...
1010 if (!name
|| num_dests
<= 0 || !dests
)
1014 * Loop through the array and set the "is_default" flag for the matching
1018 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1019 dest
->is_default
= !strcasecmp(name
, dest
->name
) &&
1020 ((!instance
&& !dest
->instance
) ||
1021 (instance
&& dest
->instance
&&
1022 !strcasecmp(instance
, dest
->instance
)));
1027 * 'cupsSetDests()' - Save the list of destinations for the default server.
1029 * This function saves the destinations to /etc/cups/lpoptions when run
1030 * as root and ~/.cups/lpoptions when run as a normal user.
1034 cupsSetDests(int num_dests
, /* I - Number of destinations */
1035 cups_dest_t
*dests
) /* I - Destinations */
1037 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
1042 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
1044 * This function saves the destinations to /etc/cups/lpoptions when run
1045 * as root and ~/.cups/lpoptions when run as a normal user.
1047 * @since CUPS 1.1.21/Mac OS X 10.4@
1050 int /* O - 0 on success, -1 on error */
1051 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1052 int num_dests
, /* I - Number of destinations */
1053 cups_dest_t
*dests
) /* I - Destinations */
1055 int i
, j
; /* Looping vars */
1056 int wrote
; /* Wrote definition? */
1057 cups_dest_t
*dest
; /* Current destination */
1058 cups_option_t
*option
; /* Current option */
1059 _ipp_option_t
*match
; /* Matching attribute for option */
1060 FILE *fp
; /* File pointer */
1062 const char *home
; /* HOME environment variable */
1064 char filename
[1024]; /* lpoptions file */
1065 int num_temps
; /* Number of temporary destinations */
1066 cups_dest_t
*temps
, /* Temporary destinations */
1067 *temp
; /* Current temporary dest */
1068 const char *val
; /* Value of temporary option */
1069 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1073 * Range check the input...
1076 if (!num_dests
|| !dests
)
1080 * Get the server destinations...
1083 num_temps
= _cupsGetDests(http
, CUPS_GET_PRINTERS
, NULL
, &temps
);
1085 if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE
)
1087 cupsFreeDests(num_temps
, temps
);
1092 * Figure out which file to write to...
1095 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
1101 * Merge in server defaults...
1104 num_temps
= cups_get_dests(filename
, NULL
, NULL
, 0, num_temps
, &temps
);
1107 * Point to user defaults...
1110 if ((home
= getenv("HOME")) != NULL
)
1113 * Create ~/.cups subdirectory...
1116 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
1117 if (access(filename
, 0))
1118 mkdir(filename
, 0700);
1120 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
1126 * Try to open the file...
1129 if ((fp
= fopen(filename
, "w")) == NULL
)
1131 cupsFreeDests(num_temps
, temps
);
1137 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
1142 fchmod(fileno(fp
), 0644);
1146 * Write each printer; each line looks like:
1148 * Dest name[/instance] options
1149 * Default name[/instance] options
1152 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
1153 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
1155 if (dest
->is_default
)
1157 fprintf(fp
, "Default %s", dest
->name
);
1159 fprintf(fp
, "/%s", dest
->instance
);
1166 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
1167 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
1169 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
1172 * See if this option is a printer attribute; if so, skip it...
1175 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
1176 match
->group_tag
== IPP_TAG_PRINTER
)
1180 * See if the server/global options match these; if so, don't
1185 (val
= cupsGetOption(option
->name
, temp
->num_options
,
1186 temp
->options
)) != NULL
&&
1187 !strcasecmp(val
, option
->value
))
1191 * Options don't match, write to the file...
1196 fprintf(fp
, "Dest %s", dest
->name
);
1198 fprintf(fp
, "/%s", dest
->instance
);
1202 if (option
->value
[0])
1204 if (strchr(option
->value
, ' ') ||
1205 strchr(option
->value
, '\\') ||
1206 strchr(option
->value
, '\"') ||
1207 strchr(option
->value
, '\''))
1210 * Quote the value...
1213 fprintf(fp
, " %s=\"", option
->name
);
1215 for (val
= option
->value
; *val
; val
++)
1217 if (strchr("\"\'\\", *val
))
1228 * Store the literal value...
1231 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
1235 fprintf(fp
, " %s", option
->name
);
1243 * Free the temporary destinations and close the file...
1246 cupsFreeDests(num_temps
, temps
);
1252 * Set the default printer for this location - this allows command-line
1253 * and GUI applications to share the same default destination...
1256 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
1257 appleSetDefault(dest
->name
);
1258 #endif /* __APPLE__ */
1260 #ifdef HAVE_NOTIFY_POST
1262 * Send a notification so that MacOS X applications can know about the
1266 notify_post("com.apple.printerListChange");
1267 #endif /* HAVE_NOTIFY_POST */
1274 * '_cupsUserDefault()' - Get the user default printer from environment
1275 * variables and location information.
1278 char * /* O - Default printer or NULL */
1279 _cupsUserDefault(char *name
, /* I - Name buffer */
1280 size_t namesize
) /* I - Size of name buffer */
1282 const char *env
; /* LPDEST or PRINTER env variable */
1284 CFStringRef network
; /* Network location */
1285 CFArrayRef locations
; /* Location array */
1286 CFStringRef locprinter
; /* Current printer */
1287 #endif /* __APPLE__ */
1290 if ((env
= getenv("LPDEST")) == NULL
)
1291 if ((env
= getenv("PRINTER")) != NULL
&& !strcmp(env
, "lp"))
1296 strlcpy(name
, env
, namesize
);
1302 * Use location-based defaults if "use last printer" is selected in the
1303 * system preferences...
1306 if (!_cupsAppleGetUseLastPrinter())
1308 DEBUG_puts("1_cupsUserDefault: Not using last printer as default...");
1314 * Get the current location...
1317 if ((network
= appleCopyNetwork()) == NULL
)
1319 DEBUG_puts("1_cupsUserDefault: Unable to get current network...");
1325 CFStringGetCString(network
, name
, namesize
, kCFStringEncodingUTF8
);
1326 DEBUG_printf(("2_cupsUserDefault: network=\"%s\"", name
));
1330 * Lookup the network in the preferences...
1333 if ((locations
= appleCopyLocations()) == NULL
)
1336 * Missing or bad location array, so no location-based default...
1339 DEBUG_puts("1_cupsUserDefault: Missing or bad location history array...");
1347 DEBUG_printf(("2_cupsUserDefault: Got location, %d entries...",
1348 (int)CFArrayGetCount(locations
)));
1350 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
1351 CFStringGetCString(locprinter
, name
, namesize
, kCFStringEncodingUTF8
);
1356 CFRelease(locations
);
1358 DEBUG_printf(("1_cupsUserDefault: Returning \"%s\"...", name
));
1360 return (*name
? name
: NULL
);
1364 * No location-based defaults on this platform...
1369 #endif /* __APPLE__ */
1375 * 'appleCopyLocations()' - Copy the location history array.
1378 static CFArrayRef
/* O - Location array or NULL */
1379 appleCopyLocations(void)
1381 CFArrayRef locations
; /* Location array */
1385 * Look up the location array in the preferences...
1388 if ((locations
= CFPreferencesCopyAppValue(kLocationHistoryArrayKey
,
1389 kPMPrintingPreferences
)) == NULL
)
1392 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
1394 CFRelease(locations
);
1403 * 'appleCopyNetwork()' - Get the network ID for the current location.
1406 static CFStringRef
/* O - Network ID */
1407 appleCopyNetwork(void)
1409 SCDynamicStoreRef dynamicStore
; /* System configuration data */
1410 CFStringRef key
; /* Current network configuration key */
1411 CFDictionaryRef ip_dict
; /* Network configuration data */
1412 CFStringRef network
= NULL
; /* Current network ID */
1415 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("Printing"), NULL
,
1418 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
1419 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
1421 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
1423 if ((network
= CFDictionaryGetValue(ip_dict
,
1424 kSCPropNetIPv4Router
)) != NULL
)
1433 CFRelease(dynamicStore
);
1441 * 'appleGetPaperSize()' - Get the default paper size.
1444 char * /* O - Default paper size */
1445 appleGetPaperSize(char *name
, /* I - Paper size name buffer */
1446 int namesize
) /* I - Size of buffer */
1448 CFStringRef defaultPaperID
; /* Default paper ID */
1449 _pwg_media_t
*pwgmedia
; /* PWG media size */
1452 defaultPaperID
= _cupsAppleCopyDefaultPaperID();
1453 if (!defaultPaperID
||
1454 CFGetTypeID(defaultPaperID
) != CFStringGetTypeID() ||
1455 !CFStringGetCString(defaultPaperID
, name
, namesize
,
1456 kCFStringEncodingUTF8
))
1458 else if ((pwgmedia
= _pwgMediaForLegacy(name
)) != NULL
)
1459 strlcpy(name
, pwgmedia
->pwg
, namesize
);
1462 CFRelease(defaultPaperID
);
1469 * 'appleGetPrinter()' - Get a printer from the history array.
1472 static CFStringRef
/* O - Printer name or NULL */
1473 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
1474 CFStringRef network
, /* I - Network name */
1475 CFIndex
*locindex
) /* O - Index in array */
1477 CFIndex i
, /* Looping var */
1478 count
; /* Number of locations */
1479 CFDictionaryRef location
; /* Current location */
1480 CFStringRef locnetwork
, /* Current network */
1481 locprinter
; /* Current printer */
1484 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
1485 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
1486 CFGetTypeID(location
) == CFDictionaryGetTypeID())
1488 if ((locnetwork
= CFDictionaryGetValue(location
,
1489 kLocationNetworkKey
)) != NULL
&&
1490 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
1491 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
1492 (locprinter
= CFDictionaryGetValue(location
,
1493 kLocationPrinterIDKey
)) != NULL
&&
1494 CFGetTypeID(locprinter
) == CFStringGetTypeID())
1499 return (locprinter
);
1508 * 'appleSetDefault()' - Set the default printer for this location.
1512 appleSetDefault(const char *name
) /* I - Default printer/class name */
1514 CFStringRef network
; /* Current network */
1515 CFArrayRef locations
; /* Old locations array */
1516 CFIndex locindex
; /* Index in locations array */
1517 CFStringRef locprinter
; /* Current printer */
1518 CFMutableArrayRef newlocations
; /* New locations array */
1519 CFMutableDictionaryRef newlocation
; /* New location */
1520 CFStringRef newprinter
; /* New printer */
1524 * Get the current location...
1527 if ((network
= appleCopyNetwork()) == NULL
)
1529 DEBUG_puts("1appleSetDefault: Unable to get current network...");
1533 if ((newprinter
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
1534 kCFStringEncodingUTF8
)) == NULL
)
1541 * Lookup the network in the preferences...
1544 if ((locations
= appleCopyLocations()) != NULL
)
1545 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
1553 CFStringCompare(locprinter
, newprinter
, 0) != kCFCompareEqualTo
)
1556 * Need to change the locations array...
1561 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
1565 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
1568 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
1569 &kCFTypeArrayCallBacks
);
1571 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
1572 &kCFTypeDictionaryKeyCallBacks
,
1573 &kCFTypeDictionaryValueCallBacks
);
1575 if (newlocation
&& newlocations
)
1578 * Put the new location at the front of the array...
1581 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
1582 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, newprinter
);
1583 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
1586 * Limit the number of locations to 10...
1589 while (CFArrayGetCount(newlocations
) > 10)
1590 CFArrayRemoveValueAtIndex(newlocations
, 10);
1593 * Push the changes out...
1596 CFPreferencesSetAppValue(kLocationHistoryArrayKey
, newlocations
,
1597 kPMPrintingPreferences
);
1598 CFPreferencesAppSynchronize(kPMPrintingPreferences
);
1599 notify_post("com.apple.printerPrefsChange");
1603 CFRelease(newlocations
);
1606 CFRelease(newlocation
);
1610 CFRelease(locations
);
1613 CFRelease(newprinter
);
1617 #endif /* __APPLE__ */
1621 * 'cups_add_dest()' - Add a destination to the array.
1623 * Unlike cupsAddDest(), this function does not check for duplicates.
1626 static cups_dest_t
* /* O - New destination */
1627 cups_add_dest(const char *name
, /* I - Name of destination */
1628 const char *instance
, /* I - Instance or NULL */
1629 int *num_dests
, /* IO - Number of destinations */
1630 cups_dest_t
**dests
) /* IO - Destinations */
1632 int insert
, /* Insertion point */
1633 diff
; /* Result of comparison */
1634 cups_dest_t
*dest
; /* Destination pointer */
1638 * Add new destination...
1641 if (*num_dests
== 0)
1642 dest
= malloc(sizeof(cups_dest_t
));
1644 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (*num_dests
+ 1));
1652 * Find where to insert the destination...
1655 if (*num_dests
== 0)
1659 insert
= cups_find_dest(name
, instance
, *num_dests
, *dests
, *num_dests
- 1,
1667 * Move the array elements as needed...
1670 if (insert
< *num_dests
)
1671 memmove(*dests
+ insert
+ 1, *dests
+ insert
,
1672 (*num_dests
- insert
) * sizeof(cups_dest_t
));
1677 * Initialize the destination...
1680 dest
= *dests
+ insert
;
1681 dest
->name
= _cupsStrAlloc(name
);
1682 dest
->instance
= _cupsStrAlloc(instance
);
1683 dest
->is_default
= 0;
1684 dest
->num_options
= 0;
1685 dest
->options
= (cups_option_t
*)0;
1692 * 'cups_compare_dests()' - Compare two destinations.
1695 static int /* O - Result of comparison */
1696 cups_compare_dests(cups_dest_t
*a
, /* I - First destination */
1697 cups_dest_t
*b
) /* I - Second destination */
1699 int diff
; /* Difference */
1702 if ((diff
= strcasecmp(a
->name
, b
->name
)) != 0)
1704 else if (a
->instance
&& b
->instance
)
1705 return (strcasecmp(a
->instance
, b
->instance
));
1707 return ((a
->instance
&& !b
->instance
) - (!a
->instance
&& b
->instance
));
1712 * 'cups_find_dest()' - Find a destination using a binary search.
1715 static int /* O - Index of match */
1716 cups_find_dest(const char *name
, /* I - Destination name */
1717 const char *instance
, /* I - Instance or NULL */
1718 int num_dests
, /* I - Number of destinations */
1719 cups_dest_t
*dests
, /* I - Destinations */
1720 int prev
, /* I - Previous index */
1721 int *rdiff
) /* O - Difference of match */
1723 int left
, /* Low mark for binary search */
1724 right
, /* High mark for binary search */
1725 current
, /* Current index */
1726 diff
; /* Result of comparison */
1727 cups_dest_t key
; /* Search key */
1730 key
.name
= (char *)name
;
1731 key
.instance
= (char *)instance
;
1736 * Start search on either side of previous...
1739 if ((diff
= cups_compare_dests(&key
, dests
+ prev
)) == 0 ||
1740 (diff
< 0 && prev
== 0) ||
1741 (diff
> 0 && prev
== (num_dests
- 1)))
1749 * Start with previous on right side...
1758 * Start wih previous on left side...
1762 right
= num_dests
- 1;
1768 * Start search in the middle...
1772 right
= num_dests
- 1;
1777 current
= (left
+ right
) / 2;
1778 diff
= cups_compare_dests(&key
, dests
+ current
);
1787 while ((right
- left
) > 1);
1792 * Check the last 1 or 2 elements...
1795 if ((diff
= cups_compare_dests(&key
, dests
+ left
)) <= 0)
1799 diff
= cups_compare_dests(&key
, dests
+ right
);
1805 * Return the closest destination and the difference...
1815 * 'cups_get_default()' - Get the default destination from an lpoptions file.
1818 static char * /* O - Default destination or NULL */
1819 cups_get_default(const char *filename
, /* I - File to read */
1820 char *namebuf
, /* I - Name buffer */
1821 size_t namesize
, /* I - Size of name buffer */
1822 const char **instance
) /* I - Instance */
1824 cups_file_t
*fp
; /* lpoptions file */
1825 char line
[8192], /* Line from file */
1826 *value
, /* Value for line */
1827 *nameptr
; /* Pointer into name */
1828 int linenum
; /* Current line */
1833 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1837 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1839 if (!strcasecmp(line
, "default") && value
)
1841 strlcpy(namebuf
, value
, namesize
);
1843 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
1845 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
1848 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
1851 *instance
= nameptr
;
1859 return (*namebuf
? namebuf
: NULL
);
1864 * 'cups_get_dests()' - Get destinations from a file.
1867 static int /* O - Number of destinations */
1869 const char *filename
, /* I - File to read from */
1870 const char *match_name
, /* I - Destination name we want */
1871 const char *match_inst
, /* I - Instance name we want */
1872 int user_default_set
, /* I - User default printer set? */
1873 int num_dests
, /* I - Number of destinations */
1874 cups_dest_t
**dests
) /* IO - Destinations */
1876 int i
; /* Looping var */
1877 cups_dest_t
*dest
; /* Current destination */
1878 cups_file_t
*fp
; /* File pointer */
1879 char line
[8192], /* Line from file */
1880 *lineptr
, /* Pointer into line */
1881 *name
, /* Name of destination/option */
1882 *instance
; /* Instance of destination */
1883 int linenum
; /* Current line number */
1886 DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", "
1887 "match_inst=\"%s\", user_default_set=%d, num_dests=%d, "
1888 "dests=%p)", filename
, match_name
, match_inst
,
1889 user_default_set
, num_dests
, dests
));
1892 * Try to open the file...
1895 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1899 * Read each printer; each line looks like:
1901 * Dest name[/instance] options
1902 * Default name[/instance] options
1907 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
1910 * See what type of line it is...
1913 DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
1914 linenum
, line
, lineptr
));
1916 if ((strcasecmp(line
, "dest") && strcasecmp(line
, "default")) || !lineptr
)
1918 DEBUG_puts("9cups_get_dests: Not a dest or default line...");
1925 * Search for an instance...
1928 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
1931 if (*lineptr
== '/')
1934 * Found an instance...
1941 * Search for an instance...
1944 while (!isspace(*lineptr
& 255) && *lineptr
)
1953 DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name
,
1957 * See if the primary instance of the destination exists; if not,
1958 * ignore this entry and move on...
1963 if (strcasecmp(name
, match_name
) ||
1964 (!instance
&& match_inst
) ||
1965 (instance
&& !match_inst
) ||
1966 (instance
&& strcasecmp(instance
, match_inst
)))
1971 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
1973 DEBUG_puts("9cups_get_dests: Not found!");
1979 * Add the destination...
1982 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
1984 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1990 DEBUG_puts("9cups_get_dests: Out of memory!");
1996 * Add options until we hit the end of the line...
1999 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
2003 * If we found what we were looking for, stop now...
2010 * Set this as default if needed...
2013 if (!user_default_set
&& !strcasecmp(line
, "default"))
2015 DEBUG_puts("9cups_get_dests: Setting as default...");
2017 for (i
= 0; i
< num_dests
; i
++)
2018 (*dests
)[i
].is_default
= 0;
2020 dest
->is_default
= 1;
2025 * Close the file and return...
2035 * 'cups_make_string()' - Make a comma-separated string of values from an IPP
2039 static char * /* O - New string */
2041 ipp_attribute_t
*attr
, /* I - Attribute to convert */
2042 char *buffer
, /* I - Buffer */
2043 size_t bufsize
) /* I - Size of buffer */
2045 int i
; /* Looping var */
2046 char *ptr
, /* Pointer into buffer */
2047 *end
, /* Pointer to end of buffer */
2048 *valptr
; /* Pointer into string attribute */
2052 * Return quickly if we have a single string value...
2055 if (attr
->num_values
== 1 &&
2056 attr
->value_tag
!= IPP_TAG_INTEGER
&&
2057 attr
->value_tag
!= IPP_TAG_ENUM
&&
2058 attr
->value_tag
!= IPP_TAG_BOOLEAN
&&
2059 attr
->value_tag
!= IPP_TAG_RANGE
)
2060 return (attr
->values
[0].string
.text
);
2063 * Copy the values to the string, separating with commas and escaping strings
2067 end
= buffer
+ bufsize
- 1;
2069 for (i
= 0, ptr
= buffer
; i
< attr
->num_values
&& ptr
< end
; i
++)
2074 switch (attr
->value_tag
)
2076 case IPP_TAG_INTEGER
:
2078 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].integer
);
2081 case IPP_TAG_BOOLEAN
:
2082 if (attr
->values
[i
].boolean
)
2083 strlcpy(ptr
, "true", end
- ptr
+ 1);
2085 strlcpy(ptr
, "false", end
- ptr
+ 1);
2088 case IPP_TAG_RANGE
:
2089 if (attr
->values
[i
].range
.lower
== attr
->values
[i
].range
.upper
)
2090 snprintf(ptr
, end
- ptr
+ 1, "%d", attr
->values
[i
].range
.lower
);
2092 snprintf(ptr
, end
- ptr
+ 1, "%d-%d", attr
->values
[i
].range
.lower
,
2093 attr
->values
[i
].range
.upper
);
2097 for (valptr
= attr
->values
[i
].string
.text
;
2098 *valptr
&& ptr
< end
;)
2100 if (strchr(" \t\n\\\'\"", *valptr
))
2102 if (ptr
>= (end
- 1))
2125 * End of "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $".