2 * "$Id: printers.c 5231 2006-03-05 17:22:27Z mike $"
4 * Printer routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddPrinter() - Add a printer to the system.
27 * cupsdAddPrinterHistory() - Add the current printer state to the history.
28 * cupsdAddPrinterUser() - Add a user to the ACL.
29 * cupsdDeleteAllPrinters() - Delete all printers from the system.
30 * cupsdDeletePrinter() - Delete a printer from the system.
31 * cupsdDeletePrinterFilters() - Delete all MIME filters for a printer.
32 * cupsdFindPrinter() - Find a printer in the list.
33 * cupsdFreePrinterUsers() - Free allow/deny users.
34 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
35 * cupsdRenamePrinter() - Rename a printer.
36 * cupsdSaveAllPrinters() - Save all printer definitions to the
38 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
40 * cupsdSetPrinterReasons() - Set/update the reasons strings.
41 * cupsdSetPrinterState() - Update the current state of a printer.
42 * cupsdStopPrinter() - Stop a printer from printing any jobs...
43 * cupsdUpdatePrinters() - Update printers after a partial reload.
44 * cupsdValidateDest() - Validate a printer/class destination.
45 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
46 * applications that need it...
47 * cupsdSanitizeURI() - Sanitize a device URI...
48 * add_printer_defaults() - Add name-default attributes to the printer
50 * add_printer_filter() - Add a MIME filter for a printer.
51 * add_printer_formats() - Add document-format-supported values for
53 * compare_printers() - Compare two printers.
54 * transcode_nickname() - Convert the PPD NickName to UTF-8...
55 * write_irix_config() - Update the config files used by the IRIX
57 * write_irix_state() - Update the status files used by IRIX
58 * printing desktop tools.
62 * Include necessary headers...
66 #include <cups/transcode.h>
73 static void add_printer_defaults(cupsd_printer_t
*p
);
74 static void add_printer_filter(cupsd_printer_t
*p
, const char *filter
);
75 static void add_printer_formats(cupsd_printer_t
*p
);
76 static int compare_printers(void *first
, void *second
, void *data
);
77 static void transcode_nickname(cupsd_printer_t
*p
, ppd_file_t
*ppd
);
79 static void write_irix_config(cupsd_printer_t
*p
);
80 static void write_irix_state(cupsd_printer_t
*p
);
85 * 'cupsdAddPrinter()' - Add a printer to the system.
88 cupsd_printer_t
* /* O - New printer */
89 cupsdAddPrinter(const char *name
) /* I - Name of printer */
91 cupsd_printer_t
*p
; /* New printer */
95 * Range check input...
98 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddPrinter(\"%s\")", name
);
101 * Create a new printer entity...
104 if ((p
= calloc(1, sizeof(cupsd_printer_t
))) == NULL
)
106 cupsdLogMessage(CUPSD_LOG_CRIT
, "Unable to allocate memory for printer - %s",
111 cupsdSetString(&p
->name
, name
);
112 cupsdSetString(&p
->info
, name
);
113 cupsdSetString(&p
->hostname
, ServerName
);
115 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/printers/%s", ServerName
, LocalPort
, name
);
116 cupsdSetStringf(&p
->device_uri
, "file:/dev/null");
118 p
->state
= IPP_PRINTER_STOPPED
;
119 p
->state_time
= time(NULL
);
121 p
->shared
= DefaultShared
;
122 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
124 cupsdSetString(&p
->job_sheets
[0], "none");
125 cupsdSetString(&p
->job_sheets
[1], "none");
127 cupsdSetString(&p
->error_policy
, "stop-printer");
128 cupsdSetString(&p
->op_policy
, DefaultPolicy
);
130 p
->op_policy_ptr
= DefaultPolicyPtr
;
132 if (MaxPrinterHistory
)
133 p
->history
= calloc(MaxPrinterHistory
, sizeof(ipp_t
*));
136 * Insert the printer in the printer list alphabetically...
140 Printers
= cupsArrayNew(compare_printers
, NULL
);
142 cupsArrayAdd(Printers
, p
);
144 if (!ImplicitPrinters
)
145 ImplicitPrinters
= cupsArrayNew(compare_printers
, NULL
);
148 * Return the new printer...
156 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
160 cupsdAddPrinterHistory(
161 cupsd_printer_t
*p
) /* I - Printer */
163 ipp_t
*history
; /* History collection */
167 * Stop early if we aren't keeping history data...
170 if (MaxPrinterHistory
<= 0)
174 * Retire old history data as needed...
177 p
->sequence_number
++;
179 if (p
->num_history
>= MaxPrinterHistory
)
182 ippDelete(p
->history
[0]);
183 memmove(p
->history
, p
->history
+ 1, p
->num_history
* sizeof(ipp_t
*));
187 * Create a collection containing the current printer-state, printer-up-time,
188 * printer-state-message, and printer-state-reasons attributes.
192 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
194 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
196 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-shared", p
->shared
);
197 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message",
198 NULL
, p
->state_message
);
201 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
202 "com.apple.print.recoverable-message", NULL
, p
->recoverable
);
203 #endif /* __APPLE__ */
204 if (p
->num_reasons
== 0)
205 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
206 "printer-state-reasons", NULL
,
207 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
209 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
210 "printer-state-reasons", p
->num_reasons
, NULL
,
211 (const char * const *)p
->reasons
);
212 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
213 "printer-state-change-time", p
->state_time
);
214 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
215 "printer-state-sequence-number", p
->sequence_number
);
217 p
->history
[p
->num_history
] = history
;
223 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
228 cupsd_printer_t
*p
, /* I - Printer */
229 const char *username
) /* I - User */
231 const char **temp
; /* Temporary array pointer */
237 if (p
->num_users
== 0)
238 temp
= malloc(sizeof(char **));
240 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
246 temp
+= p
->num_users
;
248 if ((*temp
= strdup(username
)) != NULL
)
254 * 'cupsdCreateCommonData()' - Create the common printer data.
258 cupsdCreateCommonData(void)
260 int i
; /* Looping var */
261 ipp_attribute_t
*attr
; /* Attribute data */
262 static const int nups
[] = /* number-up-supported values */
263 { 1, 2, 4, 6, 9, 16 };
264 static const ipp_orient_t orients
[4] =/* orientation-requested-supported values */
268 IPP_REVERSE_LANDSCAPE
,
271 static const char * const holds
[] = /* job-hold-until-supported values */
282 static const char * const versions
[] =/* ipp-versions-supported values */
287 static const ipp_op_t ops
[] = /* operations-supported values */
294 IPP_GET_JOB_ATTRIBUTES
,
296 IPP_GET_PRINTER_ATTRIBUTES
,
302 IPP_SET_JOB_ATTRIBUTES
,
303 IPP_CREATE_PRINTER_SUBSCRIPTION
,
304 IPP_CREATE_JOB_SUBSCRIPTION
,
305 IPP_GET_SUBSCRIPTION_ATTRIBUTES
,
306 IPP_GET_SUBSCRIPTIONS
,
307 IPP_RENEW_SUBSCRIPTION
,
308 IPP_CANCEL_SUBSCRIPTION
,
309 IPP_GET_NOTIFICATIONS
,
325 CUPS_AUTHENTICATE_JOB
,
328 static const char * const charsets
[] =/* charset-supported values */
333 static const char * const compressions
[] =
334 { /* document-compression-supported values */
338 #endif /* HAVE_LIBZ */
340 static const char * const multiple_document_handling
[] =
341 { /* multiple-document-handling-supported values */
342 "separate-documents-uncollated-copies",
343 "separate-documents-collated-copies"
345 static const char * const errors
[] = /* printer-error-policy-supported values */
351 static const char * const notify_attrs
[] =
352 { /* notify-attributes-supported values */
353 "printer-state-change-time",
354 "notify-lease-expiration-time",
355 "notify-subscriber-user-name"
357 static const char * const notify_events
[] =
358 { /* notify-events-supported values */
360 "job-config-changed",
367 "printer-config-changed",
369 "printer-finishings-changed",
370 "printer-media-changed",
374 "printer-state-changed",
384 ippDelete(CommonData
);
386 CommonData
= ippNew();
389 * This list of attributes is sorted to improve performance when the
390 * client provides a requested-attributes attribute...
393 /* charset-configured */
394 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
,
395 "charset-configured", NULL
, DefaultCharset
);
397 /* charset-supported */
398 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
,
399 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
402 /* compression-supported */
403 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
404 "compression-supported",
405 sizeof(compressions
) / sizeof(compressions
[0]),
408 /* copies-supported */
409 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
411 /* document-format-default */
412 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
413 "document-format-default", NULL
, "application/octet-stream");
415 /* generated-natural-language-supported */
416 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
417 "generated-natural-language-supported", NULL
, DefaultLanguage
);
419 /* ipp-versions-supported */
420 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
421 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
424 /* job-hold-until-supported */
425 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
426 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
429 /* job-priority-supported */
430 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
431 "job-priority-supported", 100);
433 /* job-sheets-supported */
434 if (cupsArrayCount(Banners
) > 0)
437 * Setup the job-sheets-supported attribute...
440 if (Classification
&& !ClassifyOverride
)
441 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
442 "job-sheets-supported", NULL
, Classification
);
444 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
445 "job-sheets-supported", cupsArrayCount(Banners
) + 1,
449 cupsdLogMessage(CUPSD_LOG_EMERG
,
450 "Unable to allocate memory for "
451 "job-sheets-supported attribute: %s!", strerror(errno
));
452 else if (!Classification
|| ClassifyOverride
)
454 cupsd_banner_t
*banner
; /* Current banner */
457 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
459 for (i
= 1, banner
= (cupsd_banner_t
*)cupsArrayFirst(Banners
);
461 i
++, banner
= (cupsd_banner_t
*)cupsArrayNext(Banners
))
462 attr
->values
[i
].string
.text
= _cupsStrAlloc(banner
->name
);
466 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
467 "job-sheets-supported", NULL
, "none");
469 /* multiple-document-handling-supported */
470 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
471 "multiple-document-handling-supported",
472 sizeof(multiple_document_handling
) /
473 sizeof(multiple_document_handling
[0]), NULL
,
474 multiple_document_handling
);
476 /* multiple-document-jobs-supported */
477 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
478 "multiple-document-jobs-supported", 1);
480 /* multiple-operation-time-out */
481 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
482 "multiple-operation-time-out", 60);
484 /* natural-language-configured */
485 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
486 "natural-language-configured", NULL
, DefaultLanguage
);
488 /* notify-attributes-supported */
489 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
490 "notify-attributes-supported",
491 (int)(sizeof(notify_attrs
) / sizeof(notify_attrs
[0])),
494 /* notify-lease-duration-default */
495 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
496 "notify-lease-duration-default", DefaultLeaseDuration
);
498 /* notify-lease-duration-supported */
499 ippAddRange(CommonData
, IPP_TAG_PRINTER
,
500 "notify-lease-duration-supported", 0,
501 MaxLeaseDuration
? MaxLeaseDuration
: 2147483647);
503 /* notify-max-events-supported */
504 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
505 "notify-max-events-supported", MaxEvents
);
507 /* notify-notify-events-default */
508 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
509 "notify-events-default", NULL
, "job-completed");
511 /* notify-notify-events-supported */
512 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
513 "notify-events-supported",
514 (int)(sizeof(notify_events
) / sizeof(notify_events
[0])),
515 NULL
, notify_events
);
517 /* notify-pull-method-supported */
518 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
519 "notify-pull-method-supported", NULL
, "ippget");
521 /* TODO: scan notifier directory */
522 /* notify-schemes-supported */
523 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
524 "notify-schemes-supported", NULL
, "mailto");
526 /* number-up-supported */
527 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
528 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
530 /* operations-supported */
531 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
532 "operations-supported",
533 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, (int *)ops
);
535 /* orientation-requested-supported */
536 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
537 "orientation-requested-supported", 4, (int *)orients
);
539 /* page-ranges-supported */
540 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
542 /* pdf-override-supported */
543 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
544 "pdl-override-supported", NULL
, "not-attempted");
546 /* printer-error-policy-supported */
547 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
548 "printer-error-policy-supported",
549 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
551 /* printer-op-policy-supported */
552 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
553 "printer-op-policy-supported", NumPolicies
, NULL
, NULL
);
554 for (i
= 0; i
< NumPolicies
; i
++)
555 attr
->values
[i
].string
.text
= _cupsStrAlloc(Policies
[i
]->name
);
560 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
564 cupsdDeleteAllPrinters(void)
566 cupsd_printer_t
*p
; /* Pointer to current printer/class */
569 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
571 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
572 if (!(p
->type
& CUPS_PRINTER_CLASS
))
573 cupsdDeletePrinter(p
, 0);
578 * 'cupsdDeletePrinter()' - Delete a printer from the system.
583 cupsd_printer_t
*p
, /* I - Printer to delete */
584 int update
) /* I - Update printers.conf? */
586 int i
; /* Looping var */
588 char filename
[1024]; /* Interface script filename */
592 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
596 * Save the current position in the Printers array...
599 cupsArraySave(Printers
);
602 * Stop printing on this printer...
605 cupsdStopPrinter(p
, update
);
608 * If this printer is the next for browsing, point to the next one...
613 cupsArrayFind(Printers
, p
);
614 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
618 * Remove the printer from the list...
621 cupsArrayRemove(Printers
, p
);
624 * Remove the dummy interface/icon/option files under IRIX...
628 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
631 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
635 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
638 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
641 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
644 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
649 * If p is the default printer, assign a different one...
652 if (p
== DefaultPrinter
)
654 DefaultPrinter
= NULL
;
656 if (UseNetworkDefault
)
659 * Find the first network default printer and use it...
662 cupsd_printer_t
*dp
; /* New default printer */
665 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
667 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
668 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
677 * Remove this printer from any classes and send a browse delete message...
680 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
682 cupsdDeletePrinterFromClasses(p
);
683 cupsdSendBrowseDelete(p
);
687 * Free all memory used by the printer...
690 if (p
->printers
!= NULL
)
693 if (MaxPrinterHistory
)
695 for (i
= 0; i
< p
->num_history
; i
++)
696 ippDelete(p
->history
[i
]);
701 for (i
= 0; i
< p
->num_reasons
; i
++)
706 cupsdDeletePrinterFilters(p
);
708 mimeDeleteType(MimeDatabase
, p
->filetype
);
710 cupsdFreePrinterUsers(p
);
713 cupsdClearString(&p
->uri
);
714 cupsdClearString(&p
->hostname
);
715 cupsdClearString(&p
->name
);
716 cupsdClearString(&p
->location
);
717 cupsdClearString(&p
->make_model
);
718 cupsdClearString(&p
->info
);
719 cupsdClearString(&p
->job_sheets
[0]);
720 cupsdClearString(&p
->job_sheets
[1]);
721 cupsdClearString(&p
->device_uri
);
722 cupsdClearString(&p
->port_monitor
);
723 cupsdClearString(&p
->op_policy
);
724 cupsdClearString(&p
->error_policy
);
727 free(p
->browse_attrs
);
730 cupsdClearString(&p
->recoverable
);
731 #endif /* __APPLE__ */
733 cupsFreeOptions(p
->num_options
, p
->options
);
738 * Restore the previous position in the Printers array...
741 cupsArrayRestore(Printers
);
746 * 'cupsdDeletePrinterFilters()' - Delete all MIME filters for a printer.
750 cupsdDeletePrinterFilters(
751 cupsd_printer_t
*p
) /* I - Printer to remove from */
753 mime_filter_t
*filter
; /* MIME filter looping var */
757 * Range check input...
764 * Remove all filters from the MIME database that have a destination
768 for (filter
= mimeFirstFilter(MimeDatabase
);
770 filter
= mimeNextFilter(MimeDatabase
))
771 if (filter
->dst
== p
->filetype
)
774 * Delete the current filter...
777 mimeDeleteFilter(MimeDatabase
, filter
);
783 * 'cupsdFindDest()' - Find a destination in the list.
786 cupsd_printer_t
* /* O - Destination in list */
787 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
789 cupsd_printer_t key
; /* Search key */
792 key
.name
= (char *)name
;
793 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
798 * 'cupsdFindPrinter()' - Find a printer in the list.
801 cupsd_printer_t
* /* O - Printer in list */
802 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
804 cupsd_printer_t
*p
; /* Printer in list */
807 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
815 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
819 cupsdFreePrinterUsers(
820 cupsd_printer_t
*p
) /* I - Printer */
822 int i
; /* Looping var */
825 if (!p
|| !p
->num_users
)
828 for (i
= 0; i
< p
->num_users
; i
++)
829 free((void *)p
->users
[i
]);
839 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
843 cupsdLoadAllPrinters(void)
845 cups_file_t
*fp
; /* printers.conf file */
846 int linenum
; /* Current line number */
847 char line
[1024], /* Line from file */
848 *value
, /* Pointer to value */
849 *valueptr
; /* Pointer into value */
850 cupsd_printer_t
*p
; /* Current printer */
854 * Open the printers.conf file...
857 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
858 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
861 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
867 * Read printer configurations until we hit EOF...
873 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
876 * Decode the directive...
879 if (!strcasecmp(line
, "<Printer") ||
880 !strcasecmp(line
, "<DefaultPrinter"))
883 * <Printer name> or <DefaultPrinter name>
886 if (p
== NULL
&& value
)
889 * Add the printer and a base file type...
892 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
894 p
= cupsdAddPrinter(value
);
896 p
->state
= IPP_PRINTER_IDLE
;
899 * Set the default printer as needed...
902 if (!strcasecmp(line
, "<DefaultPrinter"))
907 cupsdLogMessage(CUPSD_LOG_ERROR
,
908 "Syntax error on line %d of printers.conf.", linenum
);
912 else if (!strcasecmp(line
, "</Printer>"))
917 * Close out the current printer...
920 cupsdSetPrinterAttrs(p
);
921 cupsdAddPrinterHistory(p
);
923 if (p
->device_uri
&& strncmp(p
->device_uri
, "file:", 5) &&
924 p
->state
!= IPP_PRINTER_STOPPED
)
927 * See if the backend exists...
930 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
933 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
934 *valueptr
= '\0'; /* Chop everything but URI scheme */
939 * Backend does not exist, stop printer...
942 p
->state
= IPP_PRINTER_STOPPED
;
943 snprintf(p
->state_message
, sizeof(p
->state_message
),
944 "Backend %s does not exist!", line
);
952 cupsdLogMessage(CUPSD_LOG_ERROR
,
953 "Syntax error on line %d of printers.conf.", linenum
);
959 cupsdLogMessage(CUPSD_LOG_ERROR
,
960 "Syntax error on line %d of printers.conf.", linenum
);
963 else if (!strcasecmp(line
, "Info"))
966 cupsdSetString(&p
->info
, value
);
968 else if (!strcasecmp(line
, "Location"))
971 cupsdSetString(&p
->location
, value
);
973 else if (!strcasecmp(line
, "DeviceURI"))
976 cupsdSetString(&p
->device_uri
, value
);
979 cupsdLogMessage(CUPSD_LOG_ERROR
,
980 "Syntax error on line %d of printers.conf.", linenum
);
984 else if (!strcasecmp(line
, "Option") && value
)
990 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
993 cupsdLogMessage(CUPSD_LOG_ERROR
,
994 "Syntax error on line %d of printers.conf.", linenum
);
997 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
999 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
1003 else if (!strcasecmp(line
, "PortMonitor"))
1005 if (value
&& strcmp(value
, "none"))
1006 cupsdSetString(&p
->port_monitor
, value
);
1008 cupsdClearString(&p
->port_monitor
);
1011 cupsdLogMessage(CUPSD_LOG_ERROR
,
1012 "Syntax error on line %d of printers.conf.", linenum
);
1016 else if (!strcasecmp(line
, "State"))
1019 * Set the initial queue state...
1022 if (value
&& !strcasecmp(value
, "idle"))
1023 p
->state
= IPP_PRINTER_IDLE
;
1024 else if (value
&& !strcasecmp(value
, "stopped"))
1025 p
->state
= IPP_PRINTER_STOPPED
;
1028 cupsdLogMessage(CUPSD_LOG_ERROR
,
1029 "Syntax error on line %d of printers.conf.", linenum
);
1033 else if (!strcasecmp(line
, "StateMessage"))
1036 * Set the initial queue state message...
1040 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1042 else if (!strcasecmp(line
, "StateTime"))
1045 * Set the state time...
1049 p
->state_time
= atoi(value
);
1051 else if (!strcasecmp(line
, "Accepting"))
1054 * Set the initial accepting state...
1058 (!strcasecmp(value
, "yes") ||
1059 !strcasecmp(value
, "on") ||
1060 !strcasecmp(value
, "true")))
1063 (!strcasecmp(value
, "no") ||
1064 !strcasecmp(value
, "off") ||
1065 !strcasecmp(value
, "false")))
1069 cupsdLogMessage(CUPSD_LOG_ERROR
,
1070 "Syntax error on line %d of printers.conf.", linenum
);
1074 else if (!strcasecmp(line
, "Shared"))
1077 * Set the initial shared state...
1081 (!strcasecmp(value
, "yes") ||
1082 !strcasecmp(value
, "on") ||
1083 !strcasecmp(value
, "true")))
1086 (!strcasecmp(value
, "no") ||
1087 !strcasecmp(value
, "off") ||
1088 !strcasecmp(value
, "false")))
1092 cupsdLogMessage(CUPSD_LOG_ERROR
,
1093 "Syntax error on line %d of printers.conf.", linenum
);
1097 else if (!strcasecmp(line
, "JobSheets"))
1100 * Set the initial job sheets...
1105 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1110 cupsdSetString(&p
->job_sheets
[0], value
);
1112 while (isspace(*valueptr
& 255))
1117 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1122 cupsdSetString(&p
->job_sheets
[1], value
);
1127 cupsdLogMessage(CUPSD_LOG_ERROR
,
1128 "Syntax error on line %d of printers.conf.", linenum
);
1132 else if (!strcasecmp(line
, "AllowUser"))
1137 cupsdAddPrinterUser(p
, value
);
1141 cupsdLogMessage(CUPSD_LOG_ERROR
,
1142 "Syntax error on line %d of printers.conf.", linenum
);
1146 else if (!strcasecmp(line
, "DenyUser"))
1151 cupsdAddPrinterUser(p
, value
);
1155 cupsdLogMessage(CUPSD_LOG_ERROR
,
1156 "Syntax error on line %d of printers.conf.", linenum
);
1160 else if (!strcasecmp(line
, "QuotaPeriod"))
1163 p
->quota_period
= atoi(value
);
1166 cupsdLogMessage(CUPSD_LOG_ERROR
,
1167 "Syntax error on line %d of printers.conf.", linenum
);
1171 else if (!strcasecmp(line
, "PageLimit"))
1174 p
->page_limit
= atoi(value
);
1177 cupsdLogMessage(CUPSD_LOG_ERROR
,
1178 "Syntax error on line %d of printers.conf.", linenum
);
1182 else if (!strcasecmp(line
, "KLimit"))
1185 p
->k_limit
= atoi(value
);
1188 cupsdLogMessage(CUPSD_LOG_ERROR
,
1189 "Syntax error on line %d of printers.conf.", linenum
);
1193 else if (!strcasecmp(line
, "OpPolicy"))
1196 cupsdSetString(&p
->op_policy
, value
);
1199 cupsdLogMessage(CUPSD_LOG_ERROR
,
1200 "Syntax error on line %d of printers.conf.", linenum
);
1204 else if (!strcasecmp(line
, "ErrorPolicy"))
1207 cupsdSetString(&p
->error_policy
, value
);
1210 cupsdLogMessage(CUPSD_LOG_ERROR
,
1211 "Syntax error on line %d of printers.conf.", linenum
);
1218 * Something else we don't understand...
1221 cupsdLogMessage(CUPSD_LOG_ERROR
,
1222 "Unknown configuration directive %s on line %d of printers.conf.",
1232 * 'cupsdRenamePrinter()' - Rename a printer.
1237 cupsd_printer_t
*p
, /* I - Printer */
1238 const char *name
) /* I - New name */
1241 * Remove the printer from the array(s) first...
1244 cupsArrayRemove(Printers
, p
);
1246 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1247 cupsArrayRemove(ImplicitPrinters
, p
);
1250 * Rename the printer type...
1253 mimeDeleteType(MimeDatabase
, p
->filetype
);
1254 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1257 * Rename the printer...
1260 cupsdSetStringf(&p
->name
, name
);
1263 * Reset printer attributes...
1266 cupsdSetPrinterAttrs(p
);
1269 * Add the printer back to the printer array(s)...
1272 cupsArrayAdd(Printers
, p
);
1273 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1274 cupsArrayAdd(ImplicitPrinters
, p
);
1279 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1284 cupsdSaveAllPrinters(void)
1286 int i
; /* Looping var */
1287 cups_file_t
*fp
; /* printers.conf file */
1288 char temp
[1024]; /* Temporary string */
1289 char backup
[1024]; /* printers.conf.O file */
1290 cupsd_printer_t
*printer
; /* Current printer class */
1291 time_t curtime
; /* Current time */
1292 struct tm
*curdate
; /* Current date */
1293 cups_option_t
*option
; /* Current option */
1297 * Create the printers.conf file...
1300 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1301 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1303 if (rename(temp
, backup
))
1305 if (errno
!= ENOENT
)
1306 cupsdLogMessage(CUPSD_LOG_ERROR
,
1307 "Unable to backup printers.conf - %s", strerror(errno
));
1310 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1312 cupsdLogMessage(CUPSD_LOG_ERROR
,
1313 "Unable to save printers.conf - %s", strerror(errno
));
1315 if (rename(backup
, temp
))
1316 cupsdLogMessage(CUPSD_LOG_ERROR
,
1317 "Unable to restore printers.conf - %s", strerror(errno
));
1321 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1324 * Restrict access to the file...
1327 fchown(cupsFileNumber(fp
), getuid(), Group
);
1328 fchmod(cupsFileNumber(fp
), 0600);
1331 * Write a small header to the file...
1334 curtime
= time(NULL
);
1335 curdate
= localtime(&curtime
);
1336 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1338 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1339 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1342 * Write each local printer known to the system...
1345 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1347 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1350 * Skip remote destinations and printer classes...
1353 if ((printer
->type
& CUPS_PRINTER_REMOTE
) ||
1354 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1355 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1359 * Write printers as needed...
1362 if (printer
== DefaultPrinter
)
1363 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1365 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1368 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
1370 if (printer
->location
)
1371 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
1373 if (printer
->device_uri
)
1374 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
1376 if (printer
->port_monitor
)
1377 cupsFilePrintf(fp
, "PortMonitor %s\n", printer
->port_monitor
);
1379 if (printer
->state
== IPP_PRINTER_STOPPED
)
1381 cupsFilePuts(fp
, "State Stopped\n");
1382 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
1385 cupsFilePuts(fp
, "State Idle\n");
1387 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1389 if (printer
->accepting
)
1390 cupsFilePuts(fp
, "Accepting Yes\n");
1392 cupsFilePuts(fp
, "Accepting No\n");
1394 if (printer
->shared
)
1395 cupsFilePuts(fp
, "Shared Yes\n");
1397 cupsFilePuts(fp
, "Shared No\n");
1399 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
1400 printer
->job_sheets
[1]);
1402 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1403 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1404 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1406 for (i
= 0; i
< printer
->num_users
; i
++)
1407 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
1410 if (printer
->op_policy
)
1411 cupsFilePrintf(fp
, "OpPolicy %s\n", printer
->op_policy
);
1412 if (printer
->error_policy
)
1413 cupsFilePrintf(fp
, "ErrorPolicy %s\n", printer
->error_policy
);
1415 for (i
= printer
->num_options
, option
= printer
->options
;
1418 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
1420 cupsFilePuts(fp
, "</Printer>\n");
1424 * Make IRIX desktop & printer status happy
1427 write_irix_state(printer
);
1436 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1440 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
1442 int i
, /* Looping var */
1443 length
; /* Length of browse attributes */
1444 char uri
[HTTP_MAX_URI
]; /* URI for printer */
1445 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1446 char filename
[1024]; /* Name of PPD file */
1447 int num_media
; /* Number of media options */
1448 cupsd_location_t
*auth
; /* Pointer to authentication element */
1449 const char *auth_supported
; /* Authentication supported */
1450 cups_ptype_t printer_type
; /* Printer type data */
1451 ppd_file_t
*ppd
; /* PPD file data */
1452 ppd_option_t
*input_slot
, /* InputSlot options */
1453 *media_type
, /* MediaType options */
1454 *page_size
, /* PageSize options */
1455 *output_bin
, /* OutputBin options */
1456 *media_quality
, /* EFMediaQualityMode options */
1457 *duplex
; /* Duplex options */
1458 ppd_attr_t
*ppdattr
; /* PPD attribute */
1459 ipp_attribute_t
*attr
; /* Attribute data */
1460 ipp_value_t
*val
; /* Attribute value */
1461 int num_finishings
; /* Number of finishings */
1462 ipp_finish_t finishings
[5]; /* finishings-supported values */
1463 cups_option_t
*option
; /* Current printer option */
1464 static const char * const sides
[3] = /* sides-supported values */
1467 "two-sided-long-edge",
1468 "two-sided-short-edge"
1472 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
1476 * Make sure that we have the common attributes defined...
1480 cupsdCreateCommonData();
1483 * Clear out old filters, if any...
1486 cupsdDeletePrinterFilters(p
);
1489 * Figure out the authentication that is required for the printer.
1492 auth_supported
= "requesting-user-name";
1493 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
1495 if (p
->type
& CUPS_PRINTER_CLASS
)
1496 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1498 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1500 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
)
1501 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1505 if (auth
->type
== AUTH_BASIC
|| auth
->type
== AUTH_BASICDIGEST
)
1506 auth_supported
= "basic";
1507 else if (auth
->type
== AUTH_DIGEST
)
1508 auth_supported
= "digest";
1510 if (auth
->type
!= AUTH_NONE
)
1511 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1513 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1516 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1520 * Create the required IPP attributes for a printer...
1524 ippDelete(p
->attrs
);
1526 p
->attrs
= ippNew();
1528 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1529 "uri-authentication-supported", NULL
, auth_supported
);
1530 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1531 "uri-security-supported", NULL
, "none");
1532 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
1534 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1535 NULL
, p
->location
? p
->location
: "");
1536 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1537 NULL
, p
->info
? p
->info
: "");
1538 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
1544 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1545 "requesting-user-name-denied", p
->num_users
, NULL
,
1548 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1549 "requesting-user-name-allowed", p
->num_users
, NULL
,
1553 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1554 "job-quota-period", p
->quota_period
);
1555 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1556 "job-k-limit", p
->k_limit
);
1557 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1558 "job-page-limit", p
->page_limit
);
1560 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_REMOTE
))
1563 * Setup the job-sheets-default attribute...
1566 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1567 "job-sheets-default", 2, NULL
, NULL
);
1571 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
1572 Classification
: p
->job_sheets
[0]);
1573 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
1574 Classification
: p
->job_sheets
[1]);
1578 printer_type
= p
->type
;
1582 if (p
->type
& CUPS_PRINTER_REMOTE
)
1585 * Tell the client this is a remote printer of some type...
1588 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1589 "printer-uri-supported", NULL
, p
->uri
);
1592 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1593 "printer-make-and-model", NULL
, p
->make_model
);
1595 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1603 * Assign additional attributes depending on whether this is a printer
1607 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
1609 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
1614 * Add class-specific attributes...
1617 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
1618 p
->printers
[0]->make_model
)
1619 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1620 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
1622 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1623 "printer-make-and-model", NULL
, "Local Printer Class");
1625 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1626 "file:///dev/null");
1628 if (p
->num_printers
> 0)
1631 * Add a list of member URIs and names...
1634 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1635 "member-uris", p
->num_printers
, NULL
, NULL
);
1636 p
->type
|= CUPS_PRINTER_OPTIONS
;
1638 for (i
= 0; i
< p
->num_printers
; i
++)
1641 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->uri
);
1643 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
1646 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1647 "member-names", p
->num_printers
, NULL
, NULL
);
1651 for (i
= 0; i
< p
->num_printers
; i
++)
1652 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->name
);
1659 * Add printer-specific attributes... Start by sanitizing the device
1660 * URI so it doesn't have a username or password in it...
1664 strcpy(uri
, "file:/dev/null");
1665 else if (strstr(p
->device_uri
, "://") != NULL
)
1668 * http://..., ipp://..., etc.
1671 cupsdSanitizeURI(p
->device_uri
, uri
, sizeof(uri
));
1676 * file:..., serial:..., etc.
1679 strlcpy(uri
, p
->device_uri
, sizeof(uri
));
1682 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1686 * Assign additional attributes from the PPD file (if any)...
1689 p
->type
|= CUPS_PRINTER_BW
;
1690 finishings
[0] = IPP_FINISHINGS_NONE
;
1693 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
1696 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1699 * Add make/model and other various attributes...
1702 if (ppd
->color_device
)
1703 p
->type
|= CUPS_PRINTER_COLOR
;
1704 if (ppd
->variable_sizes
)
1705 p
->type
|= CUPS_PRINTER_VARIABLE
;
1706 if (!ppd
->manual_copies
)
1707 p
->type
|= CUPS_PRINTER_COPIES
;
1708 if ((ppdattr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
1709 if (ppdattr
->value
&& !strcasecmp(ppdattr
->value
, "true"))
1710 p
->type
|= CUPS_PRINTER_FAX
;
1712 ippAddBoolean(p
->attrs
, IPP_TAG_PRINTER
, "color-supported",
1714 if (ppd
->throughput
)
1715 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1716 "pages-per-minute", ppd
->throughput
);
1721 * The NickName can be localized in the character set specified
1722 * by the LanugageEncoding attribute. Convert as needed to
1726 transcode_nickname(p
, ppd
);
1728 else if (ppd
->modelname
)
1729 cupsdSetString(&p
->make_model
, ppd
->modelname
);
1731 cupsdSetString(&p
->make_model
, "Bad PPD File");
1733 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1734 "printer-make-and-model", NULL
, p
->make_model
);
1737 * Add media options from the PPD file...
1740 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) != NULL
)
1741 num_media
= input_slot
->num_choices
;
1745 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
1746 num_media
+= media_type
->num_choices
;
1748 if ((page_size
= ppdFindOption(ppd
, "PageSize")) != NULL
)
1749 num_media
+= page_size
->num_choices
;
1751 if ((media_quality
= ppdFindOption(ppd
, "EFMediaQualityMode")) != NULL
)
1752 num_media
+= media_quality
->num_choices
;
1756 cupsdLogMessage(CUPSD_LOG_CRIT
,
1757 "The PPD file for printer %s contains no media "
1758 "options and is therefore invalid!", p
->name
);
1762 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1763 "media-supported", num_media
, NULL
, NULL
);
1768 if (input_slot
!= NULL
)
1769 for (i
= 0; i
< input_slot
->num_choices
; i
++, val
++)
1770 val
->string
.text
= _cupsStrAlloc(input_slot
->choices
[i
].choice
);
1772 if (media_type
!= NULL
)
1773 for (i
= 0; i
< media_type
->num_choices
; i
++, val
++)
1774 val
->string
.text
= _cupsStrAlloc(media_type
->choices
[i
].choice
);
1776 if (media_quality
!= NULL
)
1777 for (i
= 0; i
< media_quality
->num_choices
; i
++, val
++)
1778 val
->string
.text
= _cupsStrAlloc(media_quality
->choices
[i
].choice
);
1780 if (page_size
!= NULL
)
1782 for (i
= 0; i
< page_size
->num_choices
; i
++, val
++)
1783 val
->string
.text
= _cupsStrAlloc(page_size
->choices
[i
].choice
);
1785 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1786 "media-default", NULL
, page_size
->defchoice
);
1788 else if (input_slot
!= NULL
)
1789 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1790 "media-default", NULL
, input_slot
->defchoice
);
1791 else if (media_type
!= NULL
)
1792 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1793 "media-default", NULL
, media_type
->defchoice
);
1794 else if (media_quality
!= NULL
)
1795 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1796 "media-default", NULL
, media_quality
->defchoice
);
1798 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1799 "media-default", NULL
, "none");
1807 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
1809 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1810 "output-bin-supported", output_bin
->num_choices
,
1815 for (i
= 0, val
= attr
->values
;
1816 i
< output_bin
->num_choices
;
1818 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
1826 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1827 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1828 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1829 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
1830 duplex
= ppdFindOption(ppd
, "JCLDuplex");
1832 if (duplex
&& duplex
->num_choices
> 1)
1834 p
->type
|= CUPS_PRINTER_DUPLEX
;
1836 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1837 "sides-supported", 3, NULL
, sides
);
1839 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
1840 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1841 "sides-default", NULL
, "two-sided-short-edge");
1842 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
1843 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1844 "sides-default", NULL
, "two-sided-long-edge");
1846 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1847 "sides-default", NULL
, "one-sided");
1850 if (ppdFindOption(ppd
, "Collate") != NULL
)
1851 p
->type
|= CUPS_PRINTER_COLLATE
;
1853 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
1855 p
->type
|= CUPS_PRINTER_STAPLE
;
1856 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
1859 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
1861 p
->type
|= CUPS_PRINTER_BIND
;
1862 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
1865 for (i
= 0; i
< ppd
->num_sizes
; i
++)
1866 if (ppd
->sizes
[i
].length
> 1728)
1867 p
->type
|= CUPS_PRINTER_LARGE
;
1868 else if (ppd
->sizes
[i
].length
> 1008)
1869 p
->type
|= CUPS_PRINTER_MEDIUM
;
1871 p
->type
|= CUPS_PRINTER_SMALL
;
1874 * Add a filter from application/vnd.cups-raw to printer/name to
1875 * handle "raw" printing by users.
1878 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1881 * Add any filters in the PPD file...
1884 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
1885 for (i
= 0; i
< ppd
->num_filters
; i
++)
1887 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
1888 add_printer_filter(p
, ppd
->filters
[i
]);
1891 if (ppd
->num_filters
== 0)
1894 * If there are no filters, add a PostScript printing filter.
1897 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1901 * Show current and available port monitors for this printer...
1904 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "port-monitor",
1905 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
1908 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1910 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
1914 if (strstr(ppd
->protocols
, "TBCP"))
1916 else if (strstr(ppd
->protocols
, "BCP"))
1920 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1921 "port-monitor-supported", i
, NULL
, NULL
);
1923 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
1925 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1927 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
1928 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppdattr
->value
);
1932 if (strstr(ppd
->protocols
, "TBCP"))
1933 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
1934 else if (strstr(ppd
->protocols
, "BCP"))
1935 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
1939 * Close the PPD and set the type...
1944 printer_type
= p
->type
;
1946 else if (!access(filename
, 0))
1948 int pline
; /* PPD line number */
1949 ppd_status_t pstatus
; /* PPD load status */
1952 pstatus
= ppdLastError(&pline
);
1954 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
1957 if (pstatus
<= PPD_ALLOC_ERROR
)
1958 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
1960 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
1961 ppdErrorString(pstatus
), pline
);
1963 cupsdLogMessage(CUPSD_LOG_INFO
,
1964 "Hint: Run \"cupstestppd %s\" and fix any errors.",
1968 * Add a filter from application/vnd.cups-raw to printer/name to
1969 * handle "raw" printing by users.
1972 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1975 * Add a PostScript filter, since this is still possibly PS printer.
1978 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1983 * If we have an interface script, add a filter entry for it...
1986 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
1988 if (!access(filename
, X_OK
))
1991 * Yes, we have a System V style interface script; use it!
1994 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1995 "printer-make-and-model", NULL
, "Local System V Printer");
1997 snprintf(filename
, sizeof(filename
), "*/* 0 %s/interfaces/%s",
1998 ServerRoot
, p
->name
);
1999 add_printer_filter(p
, filename
);
2001 else if (p
->device_uri
&&
2002 !strncmp(p
->device_uri
, "ipp://", 6) &&
2003 (strstr(p
->device_uri
, "/printers/") != NULL
||
2004 strstr(p
->device_uri
, "/classes/") != NULL
))
2007 * Tell the client this is really a hard-wired remote printer.
2010 printer_type
|= CUPS_PRINTER_REMOTE
;
2013 * Point the printer-uri-supported attribute to the
2017 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2018 "printer-uri-supported", NULL
, p
->device_uri
);
2021 * Then set the make-and-model accordingly...
2024 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2025 "printer-make-and-model", NULL
, "Remote Printer");
2028 * Print all files directly...
2036 * Otherwise we have neither - treat this as a "dumb" printer
2037 * with no PPD file...
2040 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2041 "printer-make-and-model", NULL
, "Local Raw Printer");
2047 ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2048 "finishings-supported", num_finishings
, (int *)finishings
);
2049 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2050 "finishings-default", IPP_FINISHINGS_NONE
);
2055 * Copy the printer options into a browse attributes string we can re-use.
2058 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2060 const char *valptr
; /* Pointer into value */
2061 char *attrptr
; /* Pointer into attribute string */
2065 * Free the old browse attributes as needed...
2068 if (p
->browse_attrs
)
2069 free(p
->browse_attrs
);
2072 * Compute the length of all attributes + job-sheets, lease-duration,
2073 * and BrowseLocalOptions.
2076 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2080 length
+= strlen(option
->name
) + 2;
2084 for (valptr
= option
->value
; *valptr
; valptr
++)
2085 if (strchr(" \"\'\\", *valptr
))
2092 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2094 if (BrowseLocalOptions
)
2095 length
+= 12 + strlen(BrowseLocalOptions
);
2098 * Allocate the new string...
2101 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2102 cupsdLogMessage(CUPSD_LOG_ERROR
,
2103 "Unable to allocate %d bytes for browse data!",
2108 * Got the allocated string, now copy the options and attributes over...
2111 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2112 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2113 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2115 if (BrowseLocalOptions
)
2117 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2118 attrptr
+= strlen(attrptr
);
2121 for (i
= p
->num_options
, option
= p
->options
;
2126 strcpy(attrptr
, option
->name
);
2127 attrptr
+= strlen(attrptr
);
2133 for (valptr
= option
->value
; *valptr
; valptr
++)
2135 if (strchr(" \"\'\\", *valptr
))
2138 *attrptr
++ = *valptr
;
2148 * Populate the document-format-supported attribute...
2151 add_printer_formats(p
);
2153 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2157 * Add name-default attributes...
2160 add_printer_defaults(p
);
2164 * Write the IRIX printer config and status files...
2167 write_irix_config(p
);
2168 write_irix_state(p
);
2174 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2178 cupsdSetPrinterReasons(
2179 cupsd_printer_t
*p
, /* I - Printer */
2180 const char *s
) /* I - Reasons strings */
2182 int i
; /* Looping var */
2183 const char *sptr
; /* Pointer into reasons */
2184 char reason
[255], /* Reason string */
2185 *rptr
; /* Pointer into reason */
2188 if (s
[0] == '-' || s
[0] == '+')
2191 * Add/remove reasons...
2199 * Replace reasons...
2204 for (i
= 0; i
< p
->num_reasons
; i
++)
2205 free(p
->reasons
[i
]);
2211 * Loop through all of the reasons...
2217 * Skip leading whitespace and commas...
2220 while (isspace(*sptr
& 255) || *sptr
== ',')
2223 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2224 if (rptr
< (reason
+ sizeof(reason
) - 1))
2238 for (i
= 0; i
< p
->num_reasons
; i
++)
2239 if (!strcasecmp(reason
, p
->reasons
[i
]))
2242 * Found a match, so remove it...
2246 free(p
->reasons
[i
]);
2248 if (i
< p
->num_reasons
)
2249 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2250 (p
->num_reasons
- i
) * sizeof(char *));
2255 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2261 for (i
= 0; i
< p
->num_reasons
; i
++)
2262 if (!strcasecmp(reason
, p
->reasons
[i
]))
2265 if (i
>= p
->num_reasons
)
2267 p
->reasons
[i
] = strdup(reason
);
2276 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2280 cupsdSetPrinterState(
2281 cupsd_printer_t
*p
, /* I - Printer to change */
2282 ipp_pstate_t s
, /* I - New state */
2283 int update
) /* I - Update printers.conf? */
2285 ipp_pstate_t old_state
; /* Old printer state */
2289 * Can't set status of remote printers...
2292 if (p
->type
& CUPS_PRINTER_REMOTE
)
2296 * Set the new state...
2299 old_state
= p
->state
;
2305 * Let the browse code know this needs to be updated...
2309 p
->state_time
= time(NULL
);
2313 write_irix_state(p
);
2317 cupsdAddPrinterHistory(p
);
2320 * Save the printer configuration if a printer goes from idle or processing
2321 * to stopped (or visa-versa)...
2324 if ((old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
) &&
2327 if (p
->type
& CUPS_PRINTER_CLASS
)
2328 cupsdSaveAllClasses();
2330 cupsdSaveAllPrinters();
2336 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2340 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2341 int update
)/* I - Update printers.conf? */
2343 cupsd_job_t
*job
; /* Active print job */
2347 * Set the printer state...
2350 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2353 * See if we have a job printing on this printer...
2359 * Get pointer to job...
2362 job
= (cupsd_job_t
*)p
->job
;
2368 cupsdStopJob(job
, 0);
2371 * Reset the state to pending...
2374 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2375 job
->state_value
= IPP_JOB_PENDING
;
2383 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2387 cupsdUpdatePrinters(void)
2389 cupsd_printer_t
*p
; /* Current printer */
2393 * Loop through the printers and recreate the printer attributes
2394 * for any local printers since the policy and/or access control
2395 * stuff may have changed. Also, if browsing is disabled, remove
2396 * any remote printers...
2399 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2401 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2403 if (!Browsing
&& (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_REMOTE
)))
2405 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2406 cupsArrayRemove(ImplicitPrinters
, p
);
2408 cupsArraySave(Printers
);
2409 cupsdDeletePrinter(p
, 0);
2410 cupsArrayRestore(Printers
);
2413 else if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2414 cupsdSetPrinterAttrs(p
);
2417 * Update the operation policy pointer...
2420 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
2421 p
->op_policy_ptr
= DefaultPolicyPtr
;
2427 * 'cupsdValidateDest()' - Validate a printer/class destination.
2430 const char * /* O - Printer or class name */
2432 const char *hostname
, /* I - Host name */
2433 const char *resource
, /* I - Resource name */
2434 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
2435 cupsd_printer_t
**printer
) /* O - Printer pointer */
2437 cupsd_printer_t
*p
; /* Current printer */
2438 char localname
[1024],/* Localized hostname */
2439 *lptr
, /* Pointer into localized hostname */
2440 *sptr
; /* Pointer into server name */
2443 DEBUG_printf(("cupsdValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname
, resource
,
2447 * Initialize return values...
2453 *dtype
= (cups_ptype_t
)0;
2456 * See if the resource is a class or printer...
2459 if (!strncmp(resource
, "/classes/", 9))
2467 else if (!strncmp(resource
, "/printers/", 10))
2478 * Bad resource name...
2485 * See if the printer or class name exists...
2488 p
= cupsdFindDest(resource
);
2490 if (p
== NULL
&& strchr(resource
, '@') == NULL
)
2497 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2498 CUPS_PRINTER_REMOTE
);
2503 * Change localhost to the server name...
2506 if (!strcasecmp(hostname
, "localhost"))
2507 hostname
= ServerName
;
2509 strlcpy(localname
, hostname
, sizeof(localname
));
2511 if (!strcasecmp(hostname
, ServerName
))
2514 * Localize the hostname...
2517 lptr
= strchr(localname
, '.');
2518 sptr
= strchr(ServerName
, '.');
2520 if (sptr
!= NULL
&& lptr
!= NULL
)
2523 * Strip the common domain name components...
2526 while (lptr
!= NULL
)
2528 if (!strcasecmp(lptr
, sptr
))
2534 lptr
= strchr(lptr
+ 1, '.');
2539 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
2542 * Find a matching printer or class...
2545 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2547 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2548 if (!strcasecmp(p
->hostname
, localname
) &&
2549 !strcasecmp(p
->name
, resource
))
2554 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2555 CUPS_PRINTER_REMOTE
);
2564 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2569 cupsdWritePrintcap(void)
2571 cups_file_t
*fp
; /* printcap file */
2572 cupsd_printer_t
*p
; /* Current printer */
2577 * Update the IRIX printer state for the default printer; if
2578 * no printers remain, then the default printer file will be
2582 write_irix_state(DefaultPrinter
);
2586 * See if we have a printcap file; if not, don't bother writing it.
2589 if (!Printcap
|| !*Printcap
)
2593 * Open the printcap file...
2596 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
2600 * Put a comment header at the top so that users will know where the
2601 * data has come from...
2604 cupsFilePuts(fp
, "# This file was automatically generated by cupsd(8) from the\n");
2605 cupsFilePrintf(fp
, "# %s/printers.conf file. All changes to this file\n",
2607 cupsFilePuts(fp
, "# will be lost.\n");
2612 * Write a new printcap with the current list of printers.
2615 switch (PrintcapFormat
)
2619 * Each printer is put in the file as:
2629 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
2630 DefaultPrinter
->info
, ServerName
, DefaultPrinter
->name
);
2632 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2634 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2635 if (p
!= DefaultPrinter
)
2636 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
2637 ServerName
, p
->name
);
2640 case PRINTCAP_SOLARIS
:
2642 * Each printer is put in the file as:
2644 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2645 * _default:use=DefaultPrinter
2647 * :bsdaddr=ServerName,Printer1:\
2648 * :description=Description:
2650 * :bsdaddr=ServerName,Printer2:\
2651 * :description=Description:
2653 * :bsdaddr=ServerName,Printer3:\
2654 * :description=Description:
2657 * :bsdaddr=ServerName,PrinterN:\
2658 * :description=Description:
2661 cupsFilePuts(fp
, "_all:all=");
2662 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2664 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
2665 cupsFilePrintf(fp
, "%s%c", p
->name
,
2666 cupsArrayNext(Printers
) ? ',' : '\n');
2669 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
2671 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2673 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2674 cupsFilePrintf(fp
, "%s:\\\n"
2675 "\t:bsdaddr=%s,%s:\\\n"
2676 "\t:description=%s:\n",
2677 p
->name
, ServerName
, p
->name
, p
->info
? p
->info
: "");
2691 * 'cupsdSanitizeURI()' - Sanitize a device URI...
2694 char * /* O - New device URI */
2695 cupsdSanitizeURI(const char *uri
, /* I - Original device URI */
2696 char *buffer
, /* O - New device URI */
2697 int buflen
) /* I - Size of new device URI buffer */
2699 char *start
, /* Start of data after scheme */
2700 *slash
, /* First slash after scheme:// */
2701 *ptr
; /* Pointer into user@host:port part */
2705 * Range check input...
2708 if (!uri
|| !buffer
|| buflen
< 2)
2712 * Copy the device URI to the new buffer...
2715 strlcpy(buffer
, uri
, buflen
);
2718 * Find the end of the scheme:// part...
2721 if ((ptr
= strchr(buffer
, ':')) == NULL
)
2722 return (buffer
); /* No scheme: part... */
2724 for (start
= ptr
+ 1; *start
; start
++)
2729 * Find the next slash (/) in the URI...
2732 if ((slash
= strchr(start
, '/')) == NULL
)
2733 slash
= start
+ strlen(start
); /* No slash, point to the end */
2736 * Check for an @ sign before the slash...
2739 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
2742 * Found an @ sign and it is before the resource part, so we have
2743 * an authentication string. Copy the remaining URI over the
2744 * authentication string...
2747 _cups_strcpy(start
, ptr
+ 1);
2751 * Return the new device URI...
2759 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
2763 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
2765 int i
; /* Looping var */
2766 int num_options
; /* Number of default options */
2767 cups_option_t
*options
, /* Default options */
2768 *option
; /* Current option */
2769 char name
[256]; /* name-default */
2773 * Add all of the default options from the .conf files...
2776 for (num_options
= 0, i
= p
->num_options
, option
= p
->options
;
2780 if (strcmp(option
->name
, "ipp-options") &&
2781 strcmp(option
->name
, "job-sheets") &&
2782 strcmp(option
->name
, "lease-duration"))
2784 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
2785 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
2790 * Convert options to IPP attributes...
2793 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
2794 cupsFreeOptions(num_options
, options
);
2797 * Add standard -default attributes as needed...
2800 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
2801 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
2804 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
2805 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2806 "job-hold-until-default", NULL
, "no-hold");
2808 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
2809 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2810 "job-priority-default", 50);
2812 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
2813 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2814 "number-up-default", 1);
2816 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
2817 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2818 "orientation-requested-default", IPP_PORTRAIT
);
2823 * 'add_printer_filter()' - Add a MIME filter for a printer.
2828 cupsd_printer_t
*p
, /* I - Printer to add to */
2829 const char *filter
) /* I - Filter to add */
2831 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
2832 type
[MIME_MAX_TYPE
], /* Type for filter */
2833 program
[1024]; /* Program/filter name */
2834 int cost
; /* Cost of filter */
2835 mime_type_t
*temptype
; /* MIME type looping var */
2836 char filename
[1024]; /* Full filter filename */
2840 * Parse the filter string; it should be in the following format:
2842 * super/type cost program
2845 if (sscanf(filter
, "%15[^/]/%31s%d%1023s", super
, type
, &cost
, program
) != 4)
2847 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
2853 * See if the filter program exists; if not, stop the printer and flag
2857 if (strcmp(program
, "-"))
2859 if (program
[0] == '/')
2860 strlcpy(filename
, program
, sizeof(filename
));
2862 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
2864 if (access(filename
, X_OK
))
2866 snprintf(p
->state_message
, sizeof(p
->state_message
),
2867 "Filter \"%s\" for printer \"%s\" not available: %s",
2868 program
, p
->name
, strerror(errno
));
2869 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 0);
2870 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
2871 cupsdAddPrinterHistory(p
);
2873 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
2878 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
2879 * application/vnd.cups-command...
2882 if (!strcasecmp(super
, "application") &&
2883 !strcasecmp(type
, "vnd.cups-command"))
2884 p
->type
|= CUPS_PRINTER_COMMANDS
;
2887 * Add the filter to the MIME database, supporting wildcards as needed...
2890 for (temptype
= mimeFirstType(MimeDatabase
);
2892 temptype
= mimeNextType(MimeDatabase
))
2893 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
2894 !strcasecmp(temptype
->super
, super
)) &&
2895 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
2897 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2898 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
2899 p
->name
, temptype
->super
, temptype
->type
,
2900 p
->filetype
->super
, p
->filetype
->type
,
2902 mimeAddFilter(MimeDatabase
, temptype
, p
->filetype
, cost
, program
);
2908 * 'add_printer_formats()' - Add document-format-supported values for a printer.
2912 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
2914 int i
; /* Looping var */
2915 mime_type_t
*type
; /* Current MIME type */
2916 cups_array_t
*filters
; /* Filters */
2917 int num_types
; /* Number of supported types */
2918 const char **types
; /* Array of supported type names */
2919 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
2920 /* MIME type name */
2924 * Raw (and remote) queues advertise all of the supported MIME
2930 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
2931 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
2932 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
2937 * Otherwise, loop through the supported MIME types and see if there
2938 * are filters for them...
2941 if ((types
= calloc(NumMimeTypes
, sizeof(char *))) == NULL
)
2943 cupsdLogMessage(CUPSD_LOG_EMERG
,
2944 "Unable to allocate memory for \"%s\" MIME type list!",
2949 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
2950 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
2952 types
[0] = _cupsStrAlloc("application/octet-stream");
2954 for (num_types
= 1, type
= mimeFirstType(MimeDatabase
);
2956 type
= mimeNextType(MimeDatabase
))
2958 if (!strcasecmp(type
->super
, "application") &&
2959 !strcasecmp(type
->type
, "octet-stream"))
2962 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
2964 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
2966 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2967 "add_printer_formats: %s: %s needs %d filters",
2968 p
->name
, mimetype
, cupsArrayCount(filters
));
2970 cupsArrayDelete(filters
);
2971 types
[num_types
] = _cupsStrAlloc(mimetype
);
2975 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2976 "add_printer_formats: %s: %s not supported",
2980 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2981 "add_printer_formats: %s: %d supported types",
2982 p
->name
, num_types
);
2985 * Add the file formats that can be filtered...
2988 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
2989 "document-format-supported", num_types
, NULL
, types
);
2992 * Free the temporary data...
2995 for (i
= 0; i
< num_types
; i
++)
2996 _cupsStrFree(types
[i
]);
3003 * 'compare_printers()' - Compare two printers.
3006 static int /* O - Result of comparison */
3007 compare_printers(void *first
, /* I - First printer */
3008 void *second
, /* I - Second printer */
3009 void *data
) /* I - App data (not used) */
3011 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3012 ((cupsd_printer_t
*)second
)->name
));
3017 * 'transcode_nickname()' - Convert the PPD NickName to UTF-8...
3021 transcode_nickname(cupsd_printer_t
*p
, /* I - Printer */
3022 ppd_file_t
*ppd
)/* I - PPD file */
3024 cups_utf8_t utf8
[256]; /* UTF-8 version of nickname */
3025 cups_encoding_t encoding
; /* Encoding of PPD file */
3026 const char *nickptr
; /* Pointer into nickname */
3030 * See if we need to convert to UTF-8...
3033 if (!ppd
->lang_encoding
|| !strcasecmp(ppd
->lang_encoding
, "UTF-8"))
3036 * No language encoding, or encoding uses the non-standard UTF-8
3037 * value, so no transcoding is required...
3043 for (nickptr
= ppd
->nickname
; *nickptr
; nickptr
++)
3044 if (*nickptr
& 0x80)
3050 * No non-ASCII characters, so no transcoding is required...
3057 * OK, we need to transcode...
3060 if (!strcasecmp(ppd
->lang_encoding
, "ISOLatin1"))
3061 encoding
= CUPS_ISO8859_1
;
3062 else if (!strcasecmp(ppd
->lang_encoding
, "ISOLatin2"))
3063 encoding
= CUPS_ISO8859_2
;
3064 else if (!strcasecmp(ppd
->lang_encoding
, "ISOLatin5"))
3065 encoding
= CUPS_ISO8859_5
;
3066 else if (!strcasecmp(ppd
->lang_encoding
, "JIS83-RKSJ"))
3067 encoding
= CUPS_WINDOWS_932
;
3068 else if (!strcasecmp(ppd
->lang_encoding
, "MacStandard"))
3069 encoding
= CUPS_MAC_ROMAN
;
3070 else if (!strcasecmp(ppd
->lang_encoding
, "WindowsANSI"))
3071 encoding
= CUPS_WINDOWS_1252
;
3075 * Unknown encoding, treat as UTF-8...
3081 cupsCharsetToUTF8(utf8
, ppd
->nickname
, sizeof(utf8
), encoding
);
3083 cupsdSetString(&p
->make_model
, (char *)utf8
);
3088 * Yeah, yeah, gotos are evil, but code bloat is more evil...
3093 cupsdSetString(&p
->make_model
, ppd
->nickname
);
3100 * 'write_irix_config()' - Update the config files used by the IRIX
3105 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
3107 char filename
[1024]; /* Interface script filename */
3108 cups_file_t
*fp
; /* Interface script file */
3113 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3114 * tools. First the interface script that tells the tools what kind of
3115 * printer we have...
3118 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
3120 if (p
->type
& CUPS_PRINTER_CLASS
)
3122 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3124 cupsFilePuts(fp
, "#!/bin/sh\n");
3126 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
3127 IPP_TAG_TEXT
)) != NULL
)
3128 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
3129 else if (p
->type
& CUPS_PRINTER_CLASS
)
3130 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
3132 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
3134 if (p
->type
& CUPS_PRINTER_COLOR
)
3135 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
3137 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
3139 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
3140 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
3144 chmod(filename
, 0755);
3145 chown(filename
, User
, Group
);
3149 * Then the member file that tells which device file the queue is connected
3150 * to... Networked printers use "/dev/null" in this file, so that's what
3151 * we use (the actual device URI can confuse some apps...)
3154 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
3156 if (p
->type
& CUPS_PRINTER_CLASS
)
3158 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3160 cupsFilePuts(fp
, "/dev/null\n");
3164 chmod(filename
, 0644);
3165 chown(filename
, User
, Group
);
3169 * The gui_interface file is a script or program that launches a GUI
3170 * option panel for the printer, using options specified on the
3171 * command-line in the third argument. The option panel must send
3172 * any printing options to stdout on a single line when the user
3173 * accepts them, or nothing if the user cancels the dialog.
3175 * The default options panel program is /usr/bin/glpoptions, from
3176 * the ESP Print Pro software. You can select another using the
3177 * PrintcapGUI option.
3180 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
3182 if (p
->type
& CUPS_PRINTER_CLASS
)
3184 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3186 cupsFilePuts(fp
, "#!/bin/sh\n");
3187 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
3191 chmod(filename
, 0755);
3192 chown(filename
, User
, Group
);
3196 * The POD config file is needed by the printstatus command to show
3197 * the printer location and device.
3200 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
3202 if (p
->type
& CUPS_PRINTER_CLASS
)
3204 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3206 cupsFilePrintf(fp
, "Printer Class | %s\n",
3207 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
3208 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
3209 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
3210 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
3211 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
? p
->device_uri
: "");
3212 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
3213 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
3214 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
3218 chmod(filename
, 0664);
3219 chown(filename
, User
, Group
);
3225 * 'write_irix_state()' - Update the status files used by IRIX printing
3230 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
3232 char filename
[1024]; /* Interface script filename */
3233 cups_file_t
*fp
; /* Interface script file */
3234 int tag
; /* Status tag value */
3240 * The POD status file is needed for the printstatus window to
3241 * provide the current status of the printer.
3244 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
3246 if (p
->type
& CUPS_PRINTER_CLASS
)
3248 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3250 cupsFilePrintf(fp
, "Operational Status | %s\n",
3251 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
3252 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
3254 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
3255 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
3256 p
->device_uri
? p
->device_uri
: "");
3257 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
3258 p
->accepting
? "Accepting" : "Not accepting");
3259 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
3263 chmod(filename
, 0664);
3264 chown(filename
, User
, Group
);
3268 * The activeicons file is needed to provide desktop icons for printers:
3270 * [ quoted from /usr/lib/print/tagit ]
3272 * --- Type of printer tags (base values)
3274 * Dumb=66048 # 0x10200
3275 * DumbColor=66080 # 0x10220
3276 * Raster=66112 # 0x10240
3277 * ColorRaster=66144 # 0x10260
3278 * Plotter=66176 # 0x10280
3279 * PostScript=66208 # 0x102A0
3280 * ColorPostScript=66240 # 0x102C0
3281 * MonoPostScript=66272 # 0x102E0
3283 * --- Printer state modifiers for local printers
3288 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3290 * --- Printer state modifiers for network printers
3294 * NetFaulted=10 # 0xA
3295 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3298 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
3300 if (p
->type
& CUPS_PRINTER_CLASS
)
3302 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3304 if (p
->type
& CUPS_PRINTER_COLOR
)
3309 if (p
->type
& CUPS_PRINTER_REMOTE
)
3312 if (p
->state
== IPP_PRINTER_PROCESSING
)
3315 else if (p
->state
== IPP_PRINTER_STOPPED
)
3318 cupsFilePuts(fp
, "#!/bin/sh\n");
3319 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
3323 chmod(filename
, 0755);
3324 chown(filename
, User
, Group
);
3329 * The default file is needed by the printers window to show
3330 * the default printer.
3333 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
3335 if (DefaultPrinter
!= NULL
)
3337 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3339 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
3343 chmod(filename
, 0644);
3344 chown(filename
, User
, Group
);
3354 * End of "$Id: printers.c 5231 2006-03-05 17:22:27Z mike $".