]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/dest.c
2 * "$Id: dest.c 177 2006-06-21 00:20:03Z jlovell $"
4 * User-defined destination (and option) support for the Common UNIX
5 * Printing System (CUPS).
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Easy Software Products and are protected by Federal
11 * copyright law. Distribution and use rights are outlined in the file
12 * "LICENSE.txt" which should have been included with this file. If this
13 * file is missing or damaged please contact Easy Software Products
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 * cupsAddDest() - Add a destination to the list of destinations.
30 * cupsFreeDests() - Free the memory used by the list of destinations.
31 * cupsGetDest() - Get the named destination from the list.
32 * cupsGetDests() - Get the list of destinations from the default server.
33 * cupsGetDests2() - Get the list of destinations from the specified server.
34 * cupsSetDests() - Set the list of destinations for the default server.
35 * cupsSetDests2() - Set the list of destinations for the specified server.
36 * cups_get_dests() - Get destinations from a file.
37 * cups_get_sdests() - Get destinations from a server.
41 * Include necessary headers...
51 #endif /* HAVE_NOTIFY_H */
58 static int cups_get_dests(const char *filename
, int num_dests
,
60 static int cups_get_sdests(http_t
*http
, ipp_op_t op
, int num_dests
,
65 * 'cupsAddDest()' - Add a destination to the list of destinations.
67 * Use the cupsSaveDests() function to save the updated list of destinations
68 * to the user's lpoptions file.
71 int /* O - New number of destinations */
72 cupsAddDest(const char *name
, /* I - Name of destination */
73 const char *instance
, /* I - Instance of destination or NULL for none/primary */
74 int num_dests
, /* I - Number of destinations */
75 cups_dest_t
**dests
) /* IO - Destinations */
77 int i
; /* Looping var */
78 cups_dest_t
*dest
; /* Destination pointer */
81 if (name
== NULL
|| dests
== NULL
)
84 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
88 * Add new destination...
92 dest
= malloc(sizeof(cups_dest_t
));
94 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (num_dests
+ 1));
101 for (i
= num_dests
; i
> 0; i
--, dest
++)
102 if (strcasecmp(name
, dest
->name
) < 0)
104 else if (!strcasecmp(name
, dest
->name
) &&
105 instance
!= NULL
&& dest
->instance
!= NULL
&&
106 strcasecmp(instance
, dest
->instance
) < 0)
110 memmove(dest
+ 1, dest
, i
* sizeof(cups_dest_t
));
112 dest
->name
= strdup(name
);
113 dest
->is_default
= 0;
114 dest
->num_options
= 0;
115 dest
->options
= (cups_option_t
*)0;
117 if (instance
== NULL
)
118 dest
->instance
= NULL
;
120 dest
->instance
= strdup(instance
);
122 return (num_dests
+ 1);
127 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
131 cupsFreeDests(int num_dests
, /* I - Number of destinations */
132 cups_dest_t
*dests
) /* I - Destinations */
134 int i
; /* Looping var */
135 cups_dest_t
*dest
; /* Current destination */
138 if (num_dests
== 0 || dests
== NULL
)
141 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
146 free(dest
->instance
);
148 cupsFreeOptions(dest
->num_options
, dest
->options
);
156 * 'cupsGetDest()' - Get the named destination from the list.
158 * Use the cupsGetDests() or cupsGetDests2() functions to get a
159 * list of supported destinations for the current user.
162 cups_dest_t
* /* O - Destination pointer or NULL */
163 cupsGetDest(const char *name
, /* I - Name of destination */
164 const char *instance
, /* I - Instance of destination */
165 int num_dests
, /* I - Number of destinations */
166 cups_dest_t
*dests
) /* I - Destinations */
168 int comp
; /* Result of comparison */
171 if (num_dests
== 0 || dests
== NULL
)
177 * NULL name for default printer.
180 while (num_dests
> 0)
182 if (dests
->is_default
)
192 * Lookup name and optionally the instance...
195 while (num_dests
> 0)
197 if ((comp
= strcasecmp(name
, dests
->name
)) < 0)
201 if ((instance
== NULL
&& dests
->instance
== NULL
) ||
202 (instance
!= NULL
&& dests
->instance
!= NULL
&&
203 strcasecmp(instance
, dests
->instance
) == 0))
217 * 'cupsGetDests()' - Get the list of destinations from the default server.
219 * Starting with CUPS 1.2, the returned list of destinations include the
220 * printer-info, printer-is-accepting-jobs, printer-is-shared,
221 * printer-make-and-model, printer-state, printer-state-change-time,
222 * printer-state-reasons, and printer-type attributes as options.
225 int /* O - Number of destinations */
226 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
228 int num_dests
; /* Number of destinations */
229 http_t
*http
; /* HTTP connection */
233 * Connect to the CUPS server and get the destination list and options...
236 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
238 num_dests
= cupsGetDests2(http
, dests
);
248 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
250 * Starting with CUPS 1.2, the returned list of destinations include the
251 * printer-info, printer-is-accepting-jobs, printer-is-shared,
252 * printer-make-and-model, printer-state, printer-state-change-time,
253 * printer-state-reasons, and printer-type attributes as options.
255 * @since CUPS 1.1.21@
258 int /* O - Number of destinations */
259 cupsGetDests2(http_t
*http
, /* I - HTTP connection */
260 cups_dest_t
**dests
) /* O - Destinations */
262 int i
; /* Looping var */
263 int num_dests
; /* Number of destinations */
264 cups_dest_t
*dest
; /* Destination pointer */
265 const char *home
; /* HOME environment variable */
266 char filename
[1024]; /* Local ~/.cups/lpoptions file */
267 const char *defprinter
; /* Default printer */
268 char name
[1024], /* Copy of printer name */
269 *instance
; /* Pointer to instance name */
270 int num_reals
; /* Number of real queues */
271 cups_dest_t
*reals
; /* Real queues */
272 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
276 * Range check the input...
283 * Initialize destination array...
287 *dests
= (cups_dest_t
*)0;
290 * Grab the printers and classes...
293 num_dests
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, num_dests
, dests
);
294 num_dests
= cups_get_sdests(http
, CUPS_GET_CLASSES
, num_dests
, dests
);
297 * Make a copy of the "real" queues for a later sanity check...
302 num_reals
= num_dests
;
303 reals
= calloc(num_reals
, sizeof(cups_dest_t
));
306 memcpy(reals
, *dests
, num_reals
* sizeof(cups_dest_t
));
317 * Grab the default destination...
320 if ((defprinter
= cupsGetDefault2(http
)) != NULL
)
323 * Grab printer and instance name...
326 strlcpy(name
, defprinter
, sizeof(name
));
328 if ((instance
= strchr(name
, '/')) != NULL
)
332 * Lookup the printer and instance and make it the default...
335 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
336 dest
->is_default
= 1;
341 * This initialization of "instance" is unnecessary, but avoids a
342 * compiler warning...
349 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
352 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
353 num_dests
= cups_get_dests(filename
, num_dests
, dests
);
355 if ((home
= getenv("HOME")) != NULL
)
357 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
358 if (access(filename
, 0))
359 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
361 num_dests
= cups_get_dests(filename
, num_dests
, dests
);
365 * Validate the current default destination - this prevents old
366 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
367 * pointing to a non-existent printer or class...
373 * See if we have a default printer...
376 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
379 * Have a default; see if it is real...
382 dest
= cupsGetDest(dest
->name
, NULL
, num_reals
, reals
);
386 * If dest is NULL, then no default (that exists) is set, so we
387 * need to set a default if one exists...
390 if (dest
== NULL
&& defprinter
!= NULL
)
392 for (i
= 0; i
< num_dests
; i
++)
393 (*dests
)[i
].is_default
= 0;
395 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
396 dest
->is_default
= 1;
407 * Return the number of destinations...
415 * 'cupsSetDests()' - Save the list of destinations for the default server.
417 * This function saves the destinations to /etc/cups/lpoptions when run
418 * as root and ~/.cups/lpoptions when run as a normal user.
422 cupsSetDests(int num_dests
, /* I - Number of destinations */
423 cups_dest_t
*dests
) /* I - Destinations */
425 http_t
*http
; /* HTTP connection */
429 * Connect to the CUPS server and save the destination list and options...
432 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
434 cupsSetDests2(http
, num_dests
, dests
);
442 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
444 * This function saves the destinations to /etc/cups/lpoptions when run
445 * as root and ~/.cups/lpoptions when run as a normal user.
447 * @since CUPS 1.1.21@
450 int /* O - 0 on success, -1 on error */
451 cupsSetDests2(http_t
*http
, /* I - HTTP connection */
452 int num_dests
, /* I - Number of destinations */
453 cups_dest_t
*dests
) /* I - Destinations */
455 int i
, j
; /* Looping vars */
456 int wrote
; /* Wrote definition? */
457 cups_dest_t
*dest
; /* Current destination */
458 cups_option_t
*option
; /* Current option */
459 FILE *fp
; /* File pointer */
460 const char *home
; /* HOME environment variable */
461 char filename
[1024]; /* lpoptions file */
462 int num_temps
; /* Number of temporary destinations */
463 cups_dest_t
*temps
, /* Temporary destinations */
464 *temp
; /* Current temporary dest */
465 const char *val
; /* Value of temporary option */
466 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
470 * Range check the input...
473 if (!http
|| !num_dests
|| !dests
)
477 * Get the server destinations...
480 num_temps
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, 0, &temps
);
481 num_temps
= cups_get_sdests(http
, CUPS_GET_CLASSES
, num_temps
, &temps
);
484 * Figure out which file to write to...
487 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
493 * Merge in server defaults...
496 num_temps
= cups_get_dests(filename
, num_temps
, &temps
);
499 * Point to user defaults...
502 if ((home
= getenv("HOME")) != NULL
)
505 * Remove the old ~/.lpoptions file...
508 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
512 * Create ~/.cups subdirectory...
515 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
516 if (access(filename
, 0))
517 mkdir(filename
, 0700);
519 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
525 * Try to open the file...
528 if ((fp
= fopen(filename
, "w")) == NULL
)
530 cupsFreeDests(num_temps
, temps
);
536 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
541 fchmod(fileno(fp
), 0644);
545 * Write each printer; each line looks like:
547 * Dest name[/instance] options
548 * Default name[/instance] options
551 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
552 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
554 if (dest
->is_default
)
556 fprintf(fp
, "Default %s", dest
->name
);
558 fprintf(fp
, "/%s", dest
->instance
);
565 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
566 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
568 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
571 * See if the server/global options match these; if so, don't
575 if (temp
&& (val
= cupsGetOption(option
->name
, temp
->num_options
,
576 temp
->options
)) != NULL
)
578 if (!strcasecmp(val
, option
->value
))
583 * Options don't match, write to the file...
588 fprintf(fp
, "Dest %s", dest
->name
);
590 fprintf(fp
, "/%s", dest
->instance
);
594 if (option
->value
[0])
596 if (strchr(option
->value
, ' ') != NULL
)
597 fprintf(fp
, " %s=\"%s\"", option
->name
, option
->value
);
599 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
602 fprintf(fp
, " %s", option
->name
);
610 * Free the temporary destinations and close the file...
613 cupsFreeDests(num_temps
, temps
);
617 #ifdef HAVE_NOTIFY_POST
619 * Send a notification so that MacOS X applications can know about the
623 notify_post("com.apple.printerListChange");
624 #endif /* HAVE_NOTIFY_POST */
631 * 'cups_get_dests()' - Get destinations from a file.
634 static int /* O - Number of destinations */
635 cups_get_dests(const char *filename
, /* I - File to read from */
636 int num_dests
, /* I - Number of destinations */
637 cups_dest_t
**dests
) /* IO - Destinations */
639 int i
; /* Looping var */
640 cups_dest_t
*dest
; /* Current destination */
641 FILE *fp
; /* File pointer */
642 char line
[8192], /* Line from file */
643 *lineptr
, /* Pointer into line */
644 *name
, /* Name of destination/option */
645 *instance
; /* Instance of destination */
646 const char *printer
; /* PRINTER or LPDEST */
650 * Check environment variables...
653 if ((printer
= getenv("LPDEST")) == NULL
)
654 if ((printer
= getenv("PRINTER")) != NULL
)
655 if (strcmp(printer
, "lp") == 0)
659 * Try to open the file...
662 if ((fp
= fopen(filename
, "r")) == NULL
)
666 * Read each printer; each line looks like:
668 * Dest name[/instance] options
669 * Default name[/instance] options
672 while (fgets(line
, sizeof(line
), fp
) != NULL
)
675 * See what type of line it is...
678 if (strncasecmp(line
, "dest", 4) == 0 && isspace(line
[4] & 255))
680 else if (strncasecmp(line
, "default", 7) == 0 && isspace(line
[7] & 255))
686 * Skip leading whitespace...
689 while (isspace(*lineptr
& 255))
698 * Search for an instance...
701 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
710 * Found an instance...
717 * Search for an instance...
720 while (!isspace(*lineptr
& 255) && *lineptr
)
729 * See if the primary instance of the destination exists; if not,
730 * ignore this entry and move on...
733 if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
737 * Add the destination...
740 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
742 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
753 * Add options until we hit the end of the line...
756 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
760 * Set this as default if needed...
763 if (strncasecmp(line
, "default", 7) == 0 && printer
== NULL
)
765 for (i
= 0; i
< num_dests
; i
++)
766 (*dests
)[i
].is_default
= 0;
768 dest
->is_default
= 1;
773 * Close the file and return...
783 * 'cups_get_sdests()' - Get destinations from a server.
786 static int /* O - Number of destinations */
787 cups_get_sdests(http_t
*http
, /* I - HTTP connection */
788 ipp_op_t op
, /* I - get-printers or get-classes */
789 int num_dests
, /* I - Number of destinations */
790 cups_dest_t
**dests
) /* IO - Destinations */
792 int i
; /* Looping var */
793 cups_dest_t
*dest
; /* Current destination */
794 ipp_t
*request
, /* IPP Request */
795 *response
; /* IPP Response */
796 ipp_attribute_t
*attr
; /* Current attribute */
797 int accepting
, /* printer-is-accepting-jobs attribute */
798 shared
, /* printer-is-shared attribute */
799 state
, /* printer-state attribute */
800 change_time
, /* printer-state-change-time attribute */
801 type
; /* printer-type attribute */
802 const char *info
, /* printer-info attribute */
803 *make_model
, /* printer-make-and-model attribute */
804 *name
; /* printer-name attribute */
805 char job_sheets
[1024], /* job-sheets option */
806 reasons
[1024], /* printer-state-reasons attribute */
807 *rptr
, /* Pointer into reasons string */
808 temp
[255]; /* Temporary string for numbers */
809 static const char * const pattrs
[] = /* Attributes we're interested in */
811 "job-sheets-default",
813 "printer-is-accepting-jobs",
815 "printer-make-and-model",
818 "printer-state-change-time",
819 "printer-state-reasons",
825 * Build a CUPS_GET_PRINTERS or CUPS_GET_CLASSES request, which require
826 * the following attributes:
829 * attributes-natural-language
830 * requesting-user-name
833 request
= ippNewRequest(op
);
835 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
836 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
839 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
840 "requesting-user-name", NULL
, cupsUser());
843 * Do the request and get back a response...
846 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
848 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
851 * Skip leading attributes until we hit a printer...
854 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
861 * Pull the needed attributes from this job...
870 state
= IPP_PRINTER_IDLE
;
871 type
= CUPS_PRINTER_LOCAL
;
873 strcpy(job_sheets
, "");
876 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
878 if (!strcmp(attr
->name
, "job-sheets-default") &&
879 (attr
->value_tag
== IPP_TAG_KEYWORD
||
880 attr
->value_tag
== IPP_TAG_NAME
))
882 if (attr
->num_values
== 2)
883 snprintf(job_sheets
, sizeof(job_sheets
), "%s,%s",
884 attr
->values
[0].string
.text
, attr
->values
[1].string
.text
);
886 strlcpy(job_sheets
, attr
->values
[0].string
.text
,
889 else if (!strcmp(attr
->name
, "printer-info") &&
890 attr
->value_tag
== IPP_TAG_TEXT
)
891 info
= attr
->values
[0].string
.text
;
892 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
893 attr
->value_tag
== IPP_TAG_BOOLEAN
)
894 accepting
= attr
->values
[0].boolean
;
895 else if (!strcmp(attr
->name
, "printer-is-shared") &&
896 attr
->value_tag
== IPP_TAG_BOOLEAN
)
897 shared
= attr
->values
[0].boolean
;
898 else if (!strcmp(attr
->name
, "printer-make-and-model") &&
899 attr
->value_tag
== IPP_TAG_TEXT
)
900 make_model
= attr
->values
[0].string
.text
;
901 else if (!strcmp(attr
->name
, "printer-name") &&
902 attr
->value_tag
== IPP_TAG_NAME
)
903 name
= attr
->values
[0].string
.text
;
904 else if (!strcmp(attr
->name
, "printer-state") &&
905 attr
->value_tag
== IPP_TAG_ENUM
)
906 state
= attr
->values
[0].integer
;
907 else if (!strcmp(attr
->name
, "printer-state-change-time") &&
908 attr
->value_tag
== IPP_TAG_INTEGER
)
909 change_time
= attr
->values
[0].integer
;
910 else if (!strcmp(attr
->name
, "printer-state-reasons") &&
911 attr
->value_tag
== IPP_TAG_KEYWORD
)
913 strlcpy(reasons
, attr
->values
[0].string
.text
, sizeof(reasons
));
914 for (i
= 1, rptr
= reasons
+ strlen(reasons
);
915 i
< attr
->num_values
;
918 snprintf(rptr
, sizeof(reasons
) - (rptr
- reasons
), ",%s",
919 attr
->values
[i
].string
.text
);
920 rptr
+= strlen(rptr
);
923 else if (!strcmp(attr
->name
, "printer-type") &&
924 attr
->value_tag
== IPP_TAG_ENUM
)
925 type
= attr
->values
[0].integer
;
931 * See if we have everything needed...
942 num_dests
= cupsAddDest(name
, NULL
, num_dests
, dests
);
944 if ((dest
= cupsGetDest(name
, NULL
, num_dests
, *dests
)) != NULL
)
947 dest
->num_options
= cupsAddOption("job-sheets", job_sheets
,
952 dest
->num_options
= cupsAddOption("printer-info", info
,
956 sprintf(temp
, "%d", accepting
);
957 dest
->num_options
= cupsAddOption("printer-is-accepting-jobs", temp
,
961 sprintf(temp
, "%d", shared
);
962 dest
->num_options
= cupsAddOption("printer-is-shared", temp
,
967 dest
->num_options
= cupsAddOption("printer-make-and-model",
968 make_model
, dest
->num_options
,
971 sprintf(temp
, "%d", state
);
972 dest
->num_options
= cupsAddOption("printer-state", temp
,
978 sprintf(temp
, "%d", change_time
);
979 dest
->num_options
= cupsAddOption("printer-state-change-time", temp
,
985 dest
->num_options
= cupsAddOption("printer-state-reasons", reasons
,
989 sprintf(temp
, "%d", type
);
990 dest
->num_options
= cupsAddOption("printer-type", temp
,
1003 * Return the count...
1011 * End of "$Id: dest.c 177 2006-06-21 00:20:03Z jlovell $".