]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/dest.c
2 * "$Id: dest.c 5753 2006-07-18 19:53:24Z mike $"
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 _ipp_option_t
*match
; /* Matching attribute for option */
460 FILE *fp
; /* File pointer */
461 const char *home
; /* HOME environment variable */
462 char filename
[1024]; /* lpoptions file */
463 int num_temps
; /* Number of temporary destinations */
464 cups_dest_t
*temps
, /* Temporary destinations */
465 *temp
; /* Current temporary dest */
466 const char *val
; /* Value of temporary option */
467 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
471 * Range check the input...
474 if (!http
|| !num_dests
|| !dests
)
478 * Get the server destinations...
481 num_temps
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, 0, &temps
);
482 num_temps
= cups_get_sdests(http
, CUPS_GET_CLASSES
, num_temps
, &temps
);
485 * Figure out which file to write to...
488 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
494 * Merge in server defaults...
497 num_temps
= cups_get_dests(filename
, num_temps
, &temps
);
500 * Point to user defaults...
503 if ((home
= getenv("HOME")) != NULL
)
506 * Remove the old ~/.lpoptions file...
509 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
513 * Create ~/.cups subdirectory...
516 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
517 if (access(filename
, 0))
518 mkdir(filename
, 0700);
520 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
526 * Try to open the file...
529 if ((fp
= fopen(filename
, "w")) == NULL
)
531 cupsFreeDests(num_temps
, temps
);
537 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
542 fchmod(fileno(fp
), 0644);
546 * Write each printer; each line looks like:
548 * Dest name[/instance] options
549 * Default name[/instance] options
552 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
553 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
555 if (dest
->is_default
)
557 fprintf(fp
, "Default %s", dest
->name
);
559 fprintf(fp
, "/%s", dest
->instance
);
566 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
567 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
569 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
572 * See if this option is a printer attribute; if so, skip it...
575 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
576 match
->group_tag
== IPP_TAG_PRINTER
)
580 * See if the server/global options match these; if so, don't
585 (val
= cupsGetOption(option
->name
, temp
->num_options
,
586 temp
->options
)) != NULL
&&
587 !strcasecmp(val
, option
->value
))
591 * Options don't match, write to the file...
596 fprintf(fp
, "Dest %s", dest
->name
);
598 fprintf(fp
, "/%s", dest
->instance
);
602 if (option
->value
[0])
604 if (strchr(option
->value
, ' ') ||
605 strchr(option
->value
, '\\') ||
606 strchr(option
->value
, '\"') ||
607 strchr(option
->value
, '\''))
613 fprintf(fp
, " %s=\"", option
->name
);
615 for (val
= option
->value
; *val
; val
++)
617 if (strchr("\"\'\\", *val
))
628 * Store the literal value...
631 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
635 fprintf(fp
, " %s", option
->name
);
643 * Free the temporary destinations and close the file...
646 cupsFreeDests(num_temps
, temps
);
650 #ifdef HAVE_NOTIFY_POST
652 * Send a notification so that MacOS X applications can know about the
656 notify_post("com.apple.printerListChange");
657 #endif /* HAVE_NOTIFY_POST */
664 * 'cups_get_dests()' - Get destinations from a file.
667 static int /* O - Number of destinations */
668 cups_get_dests(const char *filename
, /* I - File to read from */
669 int num_dests
, /* I - Number of destinations */
670 cups_dest_t
**dests
) /* IO - Destinations */
672 int i
; /* Looping var */
673 cups_dest_t
*dest
; /* Current destination */
674 FILE *fp
; /* File pointer */
675 char line
[8192], /* Line from file */
676 *lineptr
, /* Pointer into line */
677 *name
, /* Name of destination/option */
678 *instance
; /* Instance of destination */
679 const char *printer
; /* PRINTER or LPDEST */
683 * Check environment variables...
686 if ((printer
= getenv("LPDEST")) == NULL
)
687 if ((printer
= getenv("PRINTER")) != NULL
)
688 if (strcmp(printer
, "lp") == 0)
692 * Try to open the file...
695 if ((fp
= fopen(filename
, "r")) == NULL
)
699 * Read each printer; each line looks like:
701 * Dest name[/instance] options
702 * Default name[/instance] options
705 while (fgets(line
, sizeof(line
), fp
) != NULL
)
708 * See what type of line it is...
711 if (strncasecmp(line
, "dest", 4) == 0 && isspace(line
[4] & 255))
713 else if (strncasecmp(line
, "default", 7) == 0 && isspace(line
[7] & 255))
719 * Skip leading whitespace...
722 while (isspace(*lineptr
& 255))
731 * Search for an instance...
734 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
743 * Found an instance...
750 * Search for an instance...
753 while (!isspace(*lineptr
& 255) && *lineptr
)
762 * See if the primary instance of the destination exists; if not,
763 * ignore this entry and move on...
766 if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
770 * Add the destination...
773 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
775 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
786 * Add options until we hit the end of the line...
789 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
793 * Set this as default if needed...
796 if (strncasecmp(line
, "default", 7) == 0 && printer
== NULL
)
798 for (i
= 0; i
< num_dests
; i
++)
799 (*dests
)[i
].is_default
= 0;
801 dest
->is_default
= 1;
806 * Close the file and return...
816 * 'cups_get_sdests()' - Get destinations from a server.
819 static int /* O - Number of destinations */
820 cups_get_sdests(http_t
*http
, /* I - HTTP connection */
821 ipp_op_t op
, /* I - get-printers or get-classes */
822 int num_dests
, /* I - Number of destinations */
823 cups_dest_t
**dests
) /* IO - Destinations */
825 int i
; /* Looping var */
826 cups_dest_t
*dest
; /* Current destination */
827 ipp_t
*request
, /* IPP Request */
828 *response
; /* IPP Response */
829 ipp_attribute_t
*attr
; /* Current attribute */
830 int accepting
, /* printer-is-accepting-jobs attribute */
831 shared
, /* printer-is-shared attribute */
832 state
, /* printer-state attribute */
833 change_time
, /* printer-state-change-time attribute */
834 type
; /* printer-type attribute */
835 const char *info
, /* printer-info attribute */
836 *make_model
, /* printer-make-and-model attribute */
837 *name
; /* printer-name attribute */
838 char job_sheets
[1024], /* job-sheets option */
839 reasons
[1024], /* printer-state-reasons attribute */
840 *rptr
, /* Pointer into reasons string */
841 temp
[255]; /* Temporary string for numbers */
842 static const char * const pattrs
[] = /* Attributes we're interested in */
844 "job-sheets-default",
846 "printer-is-accepting-jobs",
848 "printer-make-and-model",
851 "printer-state-change-time",
852 "printer-state-reasons",
858 * Build a CUPS_GET_PRINTERS or CUPS_GET_CLASSES request, which require
859 * the following attributes:
862 * attributes-natural-language
863 * requesting-user-name
866 request
= ippNewRequest(op
);
868 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
869 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
872 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
873 "requesting-user-name", NULL
, cupsUser());
876 * Do the request and get back a response...
879 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
881 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
884 * Skip leading attributes until we hit a printer...
887 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
894 * Pull the needed attributes from this job...
903 state
= IPP_PRINTER_IDLE
;
904 type
= CUPS_PRINTER_LOCAL
;
906 strcpy(job_sheets
, "");
909 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
911 if (!strcmp(attr
->name
, "job-sheets-default") &&
912 (attr
->value_tag
== IPP_TAG_KEYWORD
||
913 attr
->value_tag
== IPP_TAG_NAME
))
915 if (attr
->num_values
== 2)
916 snprintf(job_sheets
, sizeof(job_sheets
), "%s,%s",
917 attr
->values
[0].string
.text
, attr
->values
[1].string
.text
);
919 strlcpy(job_sheets
, attr
->values
[0].string
.text
,
922 else if (!strcmp(attr
->name
, "printer-info") &&
923 attr
->value_tag
== IPP_TAG_TEXT
)
924 info
= attr
->values
[0].string
.text
;
925 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
926 attr
->value_tag
== IPP_TAG_BOOLEAN
)
927 accepting
= attr
->values
[0].boolean
;
928 else if (!strcmp(attr
->name
, "printer-is-shared") &&
929 attr
->value_tag
== IPP_TAG_BOOLEAN
)
930 shared
= attr
->values
[0].boolean
;
931 else if (!strcmp(attr
->name
, "printer-make-and-model") &&
932 attr
->value_tag
== IPP_TAG_TEXT
)
933 make_model
= attr
->values
[0].string
.text
;
934 else if (!strcmp(attr
->name
, "printer-name") &&
935 attr
->value_tag
== IPP_TAG_NAME
)
936 name
= attr
->values
[0].string
.text
;
937 else if (!strcmp(attr
->name
, "printer-state") &&
938 attr
->value_tag
== IPP_TAG_ENUM
)
939 state
= attr
->values
[0].integer
;
940 else if (!strcmp(attr
->name
, "printer-state-change-time") &&
941 attr
->value_tag
== IPP_TAG_INTEGER
)
942 change_time
= attr
->values
[0].integer
;
943 else if (!strcmp(attr
->name
, "printer-state-reasons") &&
944 attr
->value_tag
== IPP_TAG_KEYWORD
)
946 strlcpy(reasons
, attr
->values
[0].string
.text
, sizeof(reasons
));
947 for (i
= 1, rptr
= reasons
+ strlen(reasons
);
948 i
< attr
->num_values
;
951 snprintf(rptr
, sizeof(reasons
) - (rptr
- reasons
), ",%s",
952 attr
->values
[i
].string
.text
);
953 rptr
+= strlen(rptr
);
956 else if (!strcmp(attr
->name
, "printer-type") &&
957 attr
->value_tag
== IPP_TAG_ENUM
)
958 type
= attr
->values
[0].integer
;
964 * See if we have everything needed...
975 num_dests
= cupsAddDest(name
, NULL
, num_dests
, dests
);
977 if ((dest
= cupsGetDest(name
, NULL
, num_dests
, *dests
)) != NULL
)
980 dest
->num_options
= cupsAddOption("job-sheets", job_sheets
,
985 dest
->num_options
= cupsAddOption("printer-info", info
,
989 sprintf(temp
, "%d", accepting
);
990 dest
->num_options
= cupsAddOption("printer-is-accepting-jobs", temp
,
994 sprintf(temp
, "%d", shared
);
995 dest
->num_options
= cupsAddOption("printer-is-shared", temp
,
1000 dest
->num_options
= cupsAddOption("printer-make-and-model",
1001 make_model
, dest
->num_options
,
1004 sprintf(temp
, "%d", state
);
1005 dest
->num_options
= cupsAddOption("printer-state", temp
,
1011 sprintf(temp
, "%d", change_time
);
1012 dest
->num_options
= cupsAddOption("printer-state-change-time", temp
,
1018 dest
->num_options
= cupsAddOption("printer-state-reasons", reasons
,
1022 sprintf(temp
, "%d", type
);
1023 dest
->num_options
= cupsAddOption("printer-type", temp
,
1032 ippDelete(response
);
1036 * Return the count...
1044 * End of "$Id: dest.c 5753 2006-07-18 19:53:24Z mike $".