2 * "$Id: printers.c 5686 2006-06-21 21:02:56Z 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 * cupsdFindPrinter() - Find a printer in the list.
32 * cupsdFreePrinterUsers() - Free allow/deny users.
33 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
34 * cupsdRenamePrinter() - Rename a printer.
35 * cupsdSaveAllPrinters() - Save all printer definitions to the
37 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
39 * cupsdSetPrinterReasons() - Set/update the reasons strings.
40 * cupsdSetPrinterState() - Update the current state of a printer.
41 * cupsdStopPrinter() - Stop a printer from printing any jobs...
42 * cupsdUpdatePrinters() - Update printers after a partial reload.
43 * cupsdValidateDest() - Validate a printer/class destination.
44 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
45 * applications that need it...
46 * cupsdSanitizeURI() - Sanitize a device URI...
47 * add_printer_defaults() - Add name-default attributes to the printer
49 * add_printer_filter() - Add a MIME filter for a printer.
50 * add_printer_formats() - Add document-format-supported values for
52 * compare_printers() - Compare two printers.
53 * delete_printer_filters() - Delete all MIME filters for a printer.
54 * write_irix_config() - Update the config files used by the IRIX
56 * write_irix_state() - Update the status files used by IRIX
57 * printing desktop tools.
61 * Include necessary headers...
72 static void add_printer_defaults(cupsd_printer_t
*p
);
73 static void add_printer_filter(cupsd_printer_t
*p
, const char *filter
);
74 static void add_printer_formats(cupsd_printer_t
*p
);
75 static int compare_printers(void *first
, void *second
, void *data
);
76 static void delete_printer_filters(cupsd_printer_t
*p
);
78 static void write_irix_config(cupsd_printer_t
*p
);
79 static void write_irix_state(cupsd_printer_t
*p
);
84 * 'cupsdAddPrinter()' - Add a printer to the system.
87 cupsd_printer_t
* /* O - New printer */
88 cupsdAddPrinter(const char *name
) /* I - Name of printer */
90 cupsd_printer_t
*p
; /* New printer */
94 * Range check input...
97 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddPrinter(\"%s\")", name
);
100 * Create a new printer entity...
103 if ((p
= calloc(1, sizeof(cupsd_printer_t
))) == NULL
)
105 cupsdLogMessage(CUPSD_LOG_CRIT
, "Unable to allocate memory for printer - %s",
110 cupsdSetString(&p
->name
, name
);
111 cupsdSetString(&p
->info
, name
);
112 cupsdSetString(&p
->hostname
, ServerName
);
114 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/printers/%s", ServerName
, LocalPort
, name
);
115 cupsdSetStringf(&p
->device_uri
, "file:/dev/null");
117 p
->state
= IPP_PRINTER_STOPPED
;
118 p
->state_time
= time(NULL
);
120 p
->shared
= DefaultShared
;
121 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
123 cupsdSetString(&p
->job_sheets
[0], "none");
124 cupsdSetString(&p
->job_sheets
[1], "none");
126 cupsdSetString(&p
->error_policy
, "stop-printer");
127 cupsdSetString(&p
->op_policy
, DefaultPolicy
);
129 p
->op_policy_ptr
= DefaultPolicyPtr
;
131 if (MaxPrinterHistory
)
132 p
->history
= calloc(MaxPrinterHistory
, sizeof(ipp_t
*));
135 * Insert the printer in the printer list alphabetically...
139 Printers
= cupsArrayNew(compare_printers
, NULL
);
141 cupsArrayAdd(Printers
, p
);
143 if (!ImplicitPrinters
)
144 ImplicitPrinters
= cupsArrayNew(compare_printers
, NULL
);
147 * Return the new printer...
155 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
159 cupsdAddPrinterHistory(
160 cupsd_printer_t
*p
) /* I - Printer */
162 ipp_t
*history
; /* History collection */
166 * Stop early if we aren't keeping history data...
169 if (MaxPrinterHistory
<= 0)
173 * Retire old history data as needed...
176 p
->sequence_number
++;
178 if (p
->num_history
>= MaxPrinterHistory
)
181 ippDelete(p
->history
[0]);
182 memmove(p
->history
, p
->history
+ 1, p
->num_history
* sizeof(ipp_t
*));
186 * Create a collection containing the current printer-state, printer-up-time,
187 * printer-state-message, and printer-state-reasons attributes.
191 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
193 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
195 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-shared", p
->shared
);
196 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message",
197 NULL
, p
->state_message
);
200 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
201 "com.apple.print.recoverable-message", NULL
, p
->recoverable
);
202 #endif /* __APPLE__ */
203 if (p
->num_reasons
== 0)
204 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
205 "printer-state-reasons", NULL
,
206 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
208 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
209 "printer-state-reasons", p
->num_reasons
, NULL
,
210 (const char * const *)p
->reasons
);
211 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
212 "printer-state-change-time", p
->state_time
);
213 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
214 "printer-state-sequence-number", p
->sequence_number
);
216 p
->history
[p
->num_history
] = history
;
222 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
227 cupsd_printer_t
*p
, /* I - Printer */
228 const char *username
) /* I - User */
230 const char **temp
; /* Temporary array pointer */
236 if (p
->num_users
== 0)
237 temp
= malloc(sizeof(char **));
239 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
245 temp
+= p
->num_users
;
247 if ((*temp
= strdup(username
)) != NULL
)
253 * 'cupsdCreateCommonData()' - Create the common printer data.
257 cupsdCreateCommonData(void)
259 int i
; /* Looping var */
260 ipp_attribute_t
*attr
; /* Attribute data */
261 cups_dir_t
*dir
; /* Notifier directory */
262 cups_dentry_t
*dent
; /* Notifier directory entry */
263 cups_array_t
*notifiers
; /* Notifier array */
264 char filename
[1024], /* Filename */
265 *notifier
; /* Current notifier */
266 static const int nups
[] = /* number-up-supported values */
267 { 1, 2, 4, 6, 9, 16 };
268 static const ipp_orient_t orients
[4] =/* orientation-requested-supported values */
272 IPP_REVERSE_LANDSCAPE
,
275 static const char * const holds
[] = /* job-hold-until-supported values */
286 static const char * const versions
[] =/* ipp-versions-supported values */
291 static const ipp_op_t ops
[] = /* operations-supported values */
298 IPP_GET_JOB_ATTRIBUTES
,
300 IPP_GET_PRINTER_ATTRIBUTES
,
306 IPP_SET_JOB_ATTRIBUTES
,
307 IPP_CREATE_PRINTER_SUBSCRIPTION
,
308 IPP_CREATE_JOB_SUBSCRIPTION
,
309 IPP_GET_SUBSCRIPTION_ATTRIBUTES
,
310 IPP_GET_SUBSCRIPTIONS
,
311 IPP_RENEW_SUBSCRIPTION
,
312 IPP_CANCEL_SUBSCRIPTION
,
313 IPP_GET_NOTIFICATIONS
,
329 CUPS_AUTHENTICATE_JOB
,
332 static const char * const charsets
[] =/* charset-supported values */
337 static const char * const compressions
[] =
338 { /* document-compression-supported values */
342 #endif /* HAVE_LIBZ */
344 static const char * const multiple_document_handling
[] =
345 { /* multiple-document-handling-supported values */
346 "separate-documents-uncollated-copies",
347 "separate-documents-collated-copies"
349 static const char * const errors
[] = /* printer-error-policy-supported values */
355 static const char * const notify_attrs
[] =
356 { /* notify-attributes-supported values */
357 "printer-state-change-time",
358 "notify-lease-expiration-time",
359 "notify-subscriber-user-name"
361 static const char * const notify_events
[] =
362 { /* notify-events-supported values */
364 "job-config-changed",
371 "printer-config-changed",
373 "printer-finishings-changed",
374 "printer-media-changed",
378 "printer-state-changed",
388 ippDelete(CommonData
);
390 CommonData
= ippNew();
393 * This list of attributes is sorted to improve performance when the
394 * client provides a requested-attributes attribute...
397 /* charset-configured */
398 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
,
399 "charset-configured", NULL
, DefaultCharset
);
401 /* charset-supported */
402 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
,
403 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
406 /* compression-supported */
407 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
408 "compression-supported",
409 sizeof(compressions
) / sizeof(compressions
[0]),
412 /* copies-supported */
413 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
415 /* document-format-default */
416 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
417 "document-format-default", NULL
, "application/octet-stream");
419 /* generated-natural-language-supported */
420 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
421 "generated-natural-language-supported", NULL
, DefaultLanguage
);
423 /* ipp-versions-supported */
424 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
425 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
428 /* job-hold-until-supported */
429 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
430 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
433 /* job-priority-supported */
434 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
435 "job-priority-supported", 100);
437 /* job-sheets-supported */
438 if (cupsArrayCount(Banners
) > 0)
441 * Setup the job-sheets-supported attribute...
444 if (Classification
&& !ClassifyOverride
)
445 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
446 "job-sheets-supported", NULL
, Classification
);
448 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
449 "job-sheets-supported", cupsArrayCount(Banners
) + 1,
453 cupsdLogMessage(CUPSD_LOG_EMERG
,
454 "Unable to allocate memory for "
455 "job-sheets-supported attribute: %s!", strerror(errno
));
456 else if (!Classification
|| ClassifyOverride
)
458 cupsd_banner_t
*banner
; /* Current banner */
461 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
463 for (i
= 1, banner
= (cupsd_banner_t
*)cupsArrayFirst(Banners
);
465 i
++, banner
= (cupsd_banner_t
*)cupsArrayNext(Banners
))
466 attr
->values
[i
].string
.text
= _cupsStrAlloc(banner
->name
);
470 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
471 "job-sheets-supported", NULL
, "none");
473 /* multiple-document-handling-supported */
474 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
475 "multiple-document-handling-supported",
476 sizeof(multiple_document_handling
) /
477 sizeof(multiple_document_handling
[0]), NULL
,
478 multiple_document_handling
);
480 /* multiple-document-jobs-supported */
481 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
482 "multiple-document-jobs-supported", 1);
484 /* multiple-operation-time-out */
485 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
486 "multiple-operation-time-out", 60);
488 /* natural-language-configured */
489 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
490 "natural-language-configured", NULL
, DefaultLanguage
);
492 /* notify-attributes-supported */
493 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
494 "notify-attributes-supported",
495 (int)(sizeof(notify_attrs
) / sizeof(notify_attrs
[0])),
498 /* notify-lease-duration-default */
499 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
500 "notify-lease-duration-default", DefaultLeaseDuration
);
502 /* notify-lease-duration-supported */
503 ippAddRange(CommonData
, IPP_TAG_PRINTER
,
504 "notify-lease-duration-supported", 0,
505 MaxLeaseDuration
? MaxLeaseDuration
: 2147483647);
507 /* notify-max-events-supported */
508 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
509 "notify-max-events-supported", MaxEvents
);
511 /* notify-events-default */
512 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
513 "notify-events-default", NULL
, "job-completed");
515 /* notify-events-supported */
516 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
517 "notify-events-supported",
518 (int)(sizeof(notify_events
) / sizeof(notify_events
[0])),
519 NULL
, notify_events
);
521 /* notify-pull-method-supported */
522 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
523 "notify-pull-method-supported", NULL
, "ippget");
525 /* notify-schemes-supported */
526 snprintf(filename
, sizeof(filename
), "%s/notifier", ServerBin
);
527 if ((dir
= cupsDirOpen(filename
)) != NULL
)
529 notifiers
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
531 while ((dent
= cupsDirRead(dir
)) != NULL
)
532 if (S_ISREG(dent
->fileinfo
.st_mode
) &&
533 (dent
->fileinfo
.st_mode
& S_IXOTH
) != 0)
534 cupsArrayAdd(notifiers
, _cupsStrAlloc(dent
->filename
));
536 if (cupsArrayCount(notifiers
) > 0)
538 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
539 "notify-schemes-supported",
540 cupsArrayCount(notifiers
), NULL
, NULL
);
542 for (i
= 0, notifier
= (char *)cupsArrayFirst(notifiers
);
544 i
++, notifier
= (char *)cupsArrayNext(notifiers
))
545 attr
->values
[i
].string
.text
= notifier
;
548 cupsArrayDelete(notifiers
);
551 /* number-up-supported */
552 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
553 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
555 /* operations-supported */
556 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
557 "operations-supported",
558 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, (int *)ops
);
560 /* orientation-requested-supported */
561 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
562 "orientation-requested-supported", 4, (int *)orients
);
564 /* page-ranges-supported */
565 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
567 /* pdf-override-supported */
568 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
569 "pdl-override-supported", NULL
, "not-attempted");
571 /* printer-error-policy-supported */
572 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
573 "printer-error-policy-supported",
574 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
576 /* printer-op-policy-supported */
577 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
578 "printer-op-policy-supported", NumPolicies
, NULL
, NULL
);
579 for (i
= 0; i
< NumPolicies
; i
++)
580 attr
->values
[i
].string
.text
= _cupsStrAlloc(Policies
[i
]->name
);
585 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
589 cupsdDeleteAllPrinters(void)
591 cupsd_printer_t
*p
; /* Pointer to current printer/class */
594 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
596 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
597 if (!(p
->type
& CUPS_PRINTER_CLASS
))
598 cupsdDeletePrinter(p
, 0);
603 * 'cupsdDeletePrinter()' - Delete a printer from the system.
608 cupsd_printer_t
*p
, /* I - Printer to delete */
609 int update
) /* I - Update printers.conf? */
611 int i
; /* Looping var */
613 char filename
[1024]; /* Interface script filename */
617 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
621 * Save the current position in the Printers array...
624 cupsArraySave(Printers
);
627 * Stop printing on this printer...
630 cupsdStopPrinter(p
, update
);
633 * If this printer is the next for browsing, point to the next one...
638 cupsArrayFind(Printers
, p
);
639 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
643 * Remove the printer from the list...
646 cupsArrayRemove(Printers
, p
);
649 * Remove the dummy interface/icon/option files under IRIX...
653 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
656 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
660 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
663 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
666 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
669 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
674 * If p is the default printer, assign a different one...
677 if (p
== DefaultPrinter
)
679 DefaultPrinter
= NULL
;
681 if (UseNetworkDefault
)
684 * Find the first network default printer and use it...
687 cupsd_printer_t
*dp
; /* New default printer */
690 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
692 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
693 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
702 * Remove this printer from any classes and send a browse delete message...
705 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
707 cupsdDeletePrinterFromClasses(p
);
708 cupsdSendBrowseDelete(p
);
712 * Free all memory used by the printer...
715 if (p
->printers
!= NULL
)
718 if (MaxPrinterHistory
)
720 for (i
= 0; i
< p
->num_history
; i
++)
721 ippDelete(p
->history
[i
]);
726 for (i
= 0; i
< p
->num_reasons
; i
++)
731 delete_printer_filters(p
);
733 mimeDeleteType(MimeDatabase
, p
->filetype
);
735 cupsdFreePrinterUsers(p
);
738 cupsdClearString(&p
->uri
);
739 cupsdClearString(&p
->hostname
);
740 cupsdClearString(&p
->name
);
741 cupsdClearString(&p
->location
);
742 cupsdClearString(&p
->make_model
);
743 cupsdClearString(&p
->info
);
744 cupsdClearString(&p
->job_sheets
[0]);
745 cupsdClearString(&p
->job_sheets
[1]);
746 cupsdClearString(&p
->device_uri
);
747 cupsdClearString(&p
->port_monitor
);
748 cupsdClearString(&p
->op_policy
);
749 cupsdClearString(&p
->error_policy
);
751 cupsArrayDelete(p
->filetypes
);
754 free(p
->browse_attrs
);
757 cupsdClearString(&p
->recoverable
);
758 #endif /* __APPLE__ */
760 cupsFreeOptions(p
->num_options
, p
->options
);
765 * Restore the previous position in the Printers array...
768 cupsArrayRestore(Printers
);
773 * 'cupsdFindDest()' - Find a destination in the list.
776 cupsd_printer_t
* /* O - Destination in list */
777 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
779 cupsd_printer_t key
; /* Search key */
782 key
.name
= (char *)name
;
783 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
788 * 'cupsdFindPrinter()' - Find a printer in the list.
791 cupsd_printer_t
* /* O - Printer in list */
792 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
794 cupsd_printer_t
*p
; /* Printer in list */
797 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
805 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
809 cupsdFreePrinterUsers(
810 cupsd_printer_t
*p
) /* I - Printer */
812 int i
; /* Looping var */
815 if (!p
|| !p
->num_users
)
818 for (i
= 0; i
< p
->num_users
; i
++)
819 free((void *)p
->users
[i
]);
829 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
833 cupsdLoadAllPrinters(void)
835 cups_file_t
*fp
; /* printers.conf file */
836 int linenum
; /* Current line number */
837 char line
[1024], /* Line from file */
838 *value
, /* Pointer to value */
839 *valueptr
; /* Pointer into value */
840 cupsd_printer_t
*p
; /* Current printer */
844 * Open the printers.conf file...
847 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
848 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
851 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
857 * Read printer configurations until we hit EOF...
863 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
866 * Decode the directive...
869 if (!strcasecmp(line
, "<Printer") ||
870 !strcasecmp(line
, "<DefaultPrinter"))
873 * <Printer name> or <DefaultPrinter name>
876 if (p
== NULL
&& value
)
879 * Add the printer and a base file type...
882 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
884 p
= cupsdAddPrinter(value
);
886 p
->state
= IPP_PRINTER_IDLE
;
889 * Set the default printer as needed...
892 if (!strcasecmp(line
, "<DefaultPrinter"))
897 cupsdLogMessage(CUPSD_LOG_ERROR
,
898 "Syntax error on line %d of printers.conf.", linenum
);
902 else if (!strcasecmp(line
, "</Printer>"))
907 * Close out the current printer...
910 cupsdSetPrinterAttrs(p
);
911 cupsdAddPrinterHistory(p
);
913 if (p
->device_uri
&& strncmp(p
->device_uri
, "file:", 5) &&
914 p
->state
!= IPP_PRINTER_STOPPED
)
917 * See if the backend exists...
920 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
923 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
924 *valueptr
= '\0'; /* Chop everything but URI scheme */
929 * Backend does not exist, stop printer...
932 p
->state
= IPP_PRINTER_STOPPED
;
933 snprintf(p
->state_message
, sizeof(p
->state_message
),
934 "Backend %s does not exist!", line
);
942 cupsdLogMessage(CUPSD_LOG_ERROR
,
943 "Syntax error on line %d of printers.conf.", linenum
);
949 cupsdLogMessage(CUPSD_LOG_ERROR
,
950 "Syntax error on line %d of printers.conf.", linenum
);
953 else if (!strcasecmp(line
, "Info"))
956 cupsdSetString(&p
->info
, value
);
958 else if (!strcasecmp(line
, "Location"))
961 cupsdSetString(&p
->location
, value
);
963 else if (!strcasecmp(line
, "DeviceURI"))
966 cupsdSetString(&p
->device_uri
, value
);
969 cupsdLogMessage(CUPSD_LOG_ERROR
,
970 "Syntax error on line %d of printers.conf.", linenum
);
974 else if (!strcasecmp(line
, "Option") && value
)
980 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
983 cupsdLogMessage(CUPSD_LOG_ERROR
,
984 "Syntax error on line %d of printers.conf.", linenum
);
987 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
989 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
993 else if (!strcasecmp(line
, "PortMonitor"))
995 if (value
&& strcmp(value
, "none"))
996 cupsdSetString(&p
->port_monitor
, value
);
998 cupsdClearString(&p
->port_monitor
);
1001 cupsdLogMessage(CUPSD_LOG_ERROR
,
1002 "Syntax error on line %d of printers.conf.", linenum
);
1006 else if (!strcasecmp(line
, "State"))
1009 * Set the initial queue state...
1012 if (value
&& !strcasecmp(value
, "idle"))
1013 p
->state
= IPP_PRINTER_IDLE
;
1014 else if (value
&& !strcasecmp(value
, "stopped"))
1015 p
->state
= IPP_PRINTER_STOPPED
;
1018 cupsdLogMessage(CUPSD_LOG_ERROR
,
1019 "Syntax error on line %d of printers.conf.", linenum
);
1023 else if (!strcasecmp(line
, "StateMessage"))
1026 * Set the initial queue state message...
1030 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1032 else if (!strcasecmp(line
, "StateTime"))
1035 * Set the state time...
1039 p
->state_time
= atoi(value
);
1041 else if (!strcasecmp(line
, "Accepting"))
1044 * Set the initial accepting state...
1048 (!strcasecmp(value
, "yes") ||
1049 !strcasecmp(value
, "on") ||
1050 !strcasecmp(value
, "true")))
1053 (!strcasecmp(value
, "no") ||
1054 !strcasecmp(value
, "off") ||
1055 !strcasecmp(value
, "false")))
1059 cupsdLogMessage(CUPSD_LOG_ERROR
,
1060 "Syntax error on line %d of printers.conf.", linenum
);
1064 else if (!strcasecmp(line
, "Shared"))
1067 * Set the initial shared state...
1071 (!strcasecmp(value
, "yes") ||
1072 !strcasecmp(value
, "on") ||
1073 !strcasecmp(value
, "true")))
1076 (!strcasecmp(value
, "no") ||
1077 !strcasecmp(value
, "off") ||
1078 !strcasecmp(value
, "false")))
1082 cupsdLogMessage(CUPSD_LOG_ERROR
,
1083 "Syntax error on line %d of printers.conf.", linenum
);
1087 else if (!strcasecmp(line
, "JobSheets"))
1090 * Set the initial job sheets...
1095 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1100 cupsdSetString(&p
->job_sheets
[0], value
);
1102 while (isspace(*valueptr
& 255))
1107 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1112 cupsdSetString(&p
->job_sheets
[1], value
);
1117 cupsdLogMessage(CUPSD_LOG_ERROR
,
1118 "Syntax error on line %d of printers.conf.", linenum
);
1122 else if (!strcasecmp(line
, "AllowUser"))
1127 cupsdAddPrinterUser(p
, value
);
1131 cupsdLogMessage(CUPSD_LOG_ERROR
,
1132 "Syntax error on line %d of printers.conf.", linenum
);
1136 else if (!strcasecmp(line
, "DenyUser"))
1141 cupsdAddPrinterUser(p
, value
);
1145 cupsdLogMessage(CUPSD_LOG_ERROR
,
1146 "Syntax error on line %d of printers.conf.", linenum
);
1150 else if (!strcasecmp(line
, "QuotaPeriod"))
1153 p
->quota_period
= atoi(value
);
1156 cupsdLogMessage(CUPSD_LOG_ERROR
,
1157 "Syntax error on line %d of printers.conf.", linenum
);
1161 else if (!strcasecmp(line
, "PageLimit"))
1164 p
->page_limit
= atoi(value
);
1167 cupsdLogMessage(CUPSD_LOG_ERROR
,
1168 "Syntax error on line %d of printers.conf.", linenum
);
1172 else if (!strcasecmp(line
, "KLimit"))
1175 p
->k_limit
= atoi(value
);
1178 cupsdLogMessage(CUPSD_LOG_ERROR
,
1179 "Syntax error on line %d of printers.conf.", linenum
);
1183 else if (!strcasecmp(line
, "OpPolicy"))
1186 cupsdSetString(&p
->op_policy
, value
);
1189 cupsdLogMessage(CUPSD_LOG_ERROR
,
1190 "Syntax error on line %d of printers.conf.", linenum
);
1194 else if (!strcasecmp(line
, "ErrorPolicy"))
1197 cupsdSetString(&p
->error_policy
, value
);
1200 cupsdLogMessage(CUPSD_LOG_ERROR
,
1201 "Syntax error on line %d of printers.conf.", linenum
);
1208 * Something else we don't understand...
1211 cupsdLogMessage(CUPSD_LOG_ERROR
,
1212 "Unknown configuration directive %s on line %d of printers.conf.",
1222 * 'cupsdRenamePrinter()' - Rename a printer.
1227 cupsd_printer_t
*p
, /* I - Printer */
1228 const char *name
) /* I - New name */
1231 * Remove the printer from the array(s) first...
1234 cupsArrayRemove(Printers
, p
);
1236 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1237 cupsArrayRemove(ImplicitPrinters
, p
);
1240 * Rename the printer type...
1243 mimeDeleteType(MimeDatabase
, p
->filetype
);
1244 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1247 * Rename the printer...
1250 cupsdSetString(&p
->name
, name
);
1253 * Reset printer attributes...
1256 cupsdSetPrinterAttrs(p
);
1259 * Add the printer back to the printer array(s)...
1262 cupsArrayAdd(Printers
, p
);
1263 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1264 cupsArrayAdd(ImplicitPrinters
, p
);
1269 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1274 cupsdSaveAllPrinters(void)
1276 int i
; /* Looping var */
1277 cups_file_t
*fp
; /* printers.conf file */
1278 char temp
[1024]; /* Temporary string */
1279 char backup
[1024]; /* printers.conf.O file */
1280 cupsd_printer_t
*printer
; /* Current printer class */
1281 time_t curtime
; /* Current time */
1282 struct tm
*curdate
; /* Current date */
1283 cups_option_t
*option
; /* Current option */
1287 * Create the printers.conf file...
1290 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1291 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1293 if (rename(temp
, backup
))
1295 if (errno
!= ENOENT
)
1296 cupsdLogMessage(CUPSD_LOG_ERROR
,
1297 "Unable to backup printers.conf - %s", strerror(errno
));
1300 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1302 cupsdLogMessage(CUPSD_LOG_ERROR
,
1303 "Unable to save printers.conf - %s", strerror(errno
));
1305 if (rename(backup
, temp
))
1306 cupsdLogMessage(CUPSD_LOG_ERROR
,
1307 "Unable to restore printers.conf - %s", strerror(errno
));
1311 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1314 * Restrict access to the file...
1317 fchown(cupsFileNumber(fp
), getuid(), Group
);
1318 fchmod(cupsFileNumber(fp
), 0600);
1321 * Write a small header to the file...
1324 curtime
= time(NULL
);
1325 curdate
= localtime(&curtime
);
1326 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1328 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1329 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1332 * Write each local printer known to the system...
1335 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1337 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1340 * Skip remote destinations and printer classes...
1343 if ((printer
->type
& CUPS_PRINTER_REMOTE
) ||
1344 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1345 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1349 * Write printers as needed...
1352 if (printer
== DefaultPrinter
)
1353 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1355 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1358 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
1360 if (printer
->location
)
1361 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
1363 if (printer
->device_uri
)
1364 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
1366 if (printer
->port_monitor
)
1367 cupsFilePrintf(fp
, "PortMonitor %s\n", printer
->port_monitor
);
1369 if (printer
->state
== IPP_PRINTER_STOPPED
)
1371 cupsFilePuts(fp
, "State Stopped\n");
1372 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
1375 cupsFilePuts(fp
, "State Idle\n");
1377 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1379 if (printer
->accepting
)
1380 cupsFilePuts(fp
, "Accepting Yes\n");
1382 cupsFilePuts(fp
, "Accepting No\n");
1384 if (printer
->shared
)
1385 cupsFilePuts(fp
, "Shared Yes\n");
1387 cupsFilePuts(fp
, "Shared No\n");
1389 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
1390 printer
->job_sheets
[1]);
1392 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1393 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1394 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1396 for (i
= 0; i
< printer
->num_users
; i
++)
1397 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
1400 if (printer
->op_policy
)
1401 cupsFilePrintf(fp
, "OpPolicy %s\n", printer
->op_policy
);
1402 if (printer
->error_policy
)
1403 cupsFilePrintf(fp
, "ErrorPolicy %s\n", printer
->error_policy
);
1405 for (i
= printer
->num_options
, option
= printer
->options
;
1408 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
1410 cupsFilePuts(fp
, "</Printer>\n");
1414 * Make IRIX desktop & printer status happy
1417 write_irix_state(printer
);
1426 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1430 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
1432 int i
, /* Looping var */
1433 length
; /* Length of browse attributes */
1434 char uri
[HTTP_MAX_URI
]; /* URI for printer */
1435 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1436 char filename
[1024]; /* Name of PPD file */
1437 int num_media
; /* Number of media options */
1438 cupsd_location_t
*auth
; /* Pointer to authentication element */
1439 const char *auth_supported
; /* Authentication supported */
1440 cups_ptype_t printer_type
; /* Printer type data */
1441 ppd_file_t
*ppd
; /* PPD file data */
1442 ppd_option_t
*input_slot
, /* InputSlot options */
1443 *media_type
, /* MediaType options */
1444 *page_size
, /* PageSize options */
1445 *output_bin
, /* OutputBin options */
1446 *media_quality
, /* EFMediaQualityMode options */
1447 *duplex
; /* Duplex options */
1448 ppd_attr_t
*ppdattr
; /* PPD attribute */
1449 ipp_attribute_t
*attr
; /* Attribute data */
1450 ipp_value_t
*val
; /* Attribute value */
1451 int num_finishings
; /* Number of finishings */
1452 ipp_finish_t finishings
[5]; /* finishings-supported values */
1453 cups_option_t
*option
; /* Current printer option */
1454 static const char * const sides
[3] = /* sides-supported values */
1457 "two-sided-long-edge",
1458 "two-sided-short-edge"
1462 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
1466 * Make sure that we have the common attributes defined...
1470 cupsdCreateCommonData();
1473 * Clear out old filters, if any...
1476 delete_printer_filters(p
);
1479 * Figure out the authentication that is required for the printer.
1482 auth_supported
= "requesting-user-name";
1483 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
1485 if (p
->type
& CUPS_PRINTER_CLASS
)
1486 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1488 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1490 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
)
1491 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1495 if (auth
->type
== AUTH_BASIC
|| auth
->type
== AUTH_BASICDIGEST
)
1496 auth_supported
= "basic";
1497 else if (auth
->type
== AUTH_DIGEST
)
1498 auth_supported
= "digest";
1500 if (auth
->type
!= AUTH_NONE
)
1501 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1503 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1506 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1510 * Create the required IPP attributes for a printer...
1514 ippDelete(p
->attrs
);
1516 p
->attrs
= ippNew();
1518 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1519 "uri-authentication-supported", NULL
, auth_supported
);
1520 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1521 "uri-security-supported", NULL
, "none");
1522 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
1524 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1525 NULL
, p
->location
? p
->location
: "");
1526 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1527 NULL
, p
->info
? p
->info
: "");
1528 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
1534 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1535 "requesting-user-name-denied", p
->num_users
, NULL
,
1538 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1539 "requesting-user-name-allowed", p
->num_users
, NULL
,
1543 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1544 "job-quota-period", p
->quota_period
);
1545 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1546 "job-k-limit", p
->k_limit
);
1547 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1548 "job-page-limit", p
->page_limit
);
1550 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_REMOTE
))
1553 * Setup the job-sheets-default attribute...
1556 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1557 "job-sheets-default", 2, NULL
, NULL
);
1561 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
1562 Classification
: p
->job_sheets
[0]);
1563 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
1564 Classification
: p
->job_sheets
[1]);
1568 printer_type
= p
->type
;
1572 if (p
->type
& CUPS_PRINTER_REMOTE
)
1575 * Tell the client this is a remote printer of some type...
1578 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1579 "printer-uri-supported", NULL
, p
->uri
);
1582 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1583 "printer-make-and-model", NULL
, p
->make_model
);
1585 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1593 * Assign additional attributes depending on whether this is a printer
1597 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
1599 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
1604 * Add class-specific attributes...
1607 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
1608 p
->printers
[0]->make_model
)
1609 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1610 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
1612 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1613 "printer-make-and-model", NULL
, "Local Printer Class");
1615 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1616 "file:///dev/null");
1618 if (p
->num_printers
> 0)
1621 * Add a list of member URIs and names...
1624 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1625 "member-uris", p
->num_printers
, NULL
, NULL
);
1626 p
->type
|= CUPS_PRINTER_OPTIONS
;
1628 for (i
= 0; i
< p
->num_printers
; i
++)
1631 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->uri
);
1633 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
1636 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1637 "member-names", p
->num_printers
, NULL
, NULL
);
1641 for (i
= 0; i
< p
->num_printers
; i
++)
1642 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->name
);
1649 * Add printer-specific attributes... Start by sanitizing the device
1650 * URI so it doesn't have a username or password in it...
1654 strcpy(uri
, "file:/dev/null");
1655 else if (strstr(p
->device_uri
, "://") != NULL
)
1658 * http://..., ipp://..., etc.
1661 cupsdSanitizeURI(p
->device_uri
, uri
, sizeof(uri
));
1666 * file:..., serial:..., etc.
1669 strlcpy(uri
, p
->device_uri
, sizeof(uri
));
1672 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1676 * Assign additional attributes from the PPD file (if any)...
1679 p
->type
|= CUPS_PRINTER_BW
;
1680 finishings
[0] = IPP_FINISHINGS_NONE
;
1683 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
1686 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1689 * Add make/model and other various attributes...
1692 if (ppd
->color_device
)
1693 p
->type
|= CUPS_PRINTER_COLOR
;
1694 if (ppd
->variable_sizes
)
1695 p
->type
|= CUPS_PRINTER_VARIABLE
;
1696 if (!ppd
->manual_copies
)
1697 p
->type
|= CUPS_PRINTER_COPIES
;
1698 if ((ppdattr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
1699 if (ppdattr
->value
&& !strcasecmp(ppdattr
->value
, "true"))
1700 p
->type
|= CUPS_PRINTER_FAX
;
1702 ippAddBoolean(p
->attrs
, IPP_TAG_PRINTER
, "color-supported",
1704 if (ppd
->throughput
)
1705 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1706 "pages-per-minute", ppd
->throughput
);
1711 * The NickName can be localized in the character set specified
1712 * by the LanugageEncoding attribute. However, ppdOpen2() has
1713 * already converted the ppd->nickname member to UTF-8 for us
1714 * (the original attribute value is available separately)
1717 cupsdSetString(&p
->make_model
, ppd
->nickname
);
1719 else if (ppd
->modelname
)
1722 * Model name can only contain specific characters...
1725 cupsdSetString(&p
->make_model
, ppd
->modelname
);
1728 cupsdSetString(&p
->make_model
, "Bad PPD File");
1730 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1731 "printer-make-and-model", NULL
, p
->make_model
);
1734 * Add media options from the PPD file...
1737 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) != NULL
)
1738 num_media
= input_slot
->num_choices
;
1742 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
1743 num_media
+= media_type
->num_choices
;
1745 if ((page_size
= ppdFindOption(ppd
, "PageSize")) != NULL
)
1746 num_media
+= page_size
->num_choices
;
1748 if ((media_quality
= ppdFindOption(ppd
, "EFMediaQualityMode")) != NULL
)
1749 num_media
+= media_quality
->num_choices
;
1753 cupsdLogMessage(CUPSD_LOG_CRIT
,
1754 "The PPD file for printer %s contains no media "
1755 "options and is therefore invalid!", p
->name
);
1759 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1760 "media-supported", num_media
, NULL
, NULL
);
1765 if (input_slot
!= NULL
)
1766 for (i
= 0; i
< input_slot
->num_choices
; i
++, val
++)
1767 val
->string
.text
= _cupsStrAlloc(input_slot
->choices
[i
].choice
);
1769 if (media_type
!= NULL
)
1770 for (i
= 0; i
< media_type
->num_choices
; i
++, val
++)
1771 val
->string
.text
= _cupsStrAlloc(media_type
->choices
[i
].choice
);
1773 if (media_quality
!= NULL
)
1774 for (i
= 0; i
< media_quality
->num_choices
; i
++, val
++)
1775 val
->string
.text
= _cupsStrAlloc(media_quality
->choices
[i
].choice
);
1777 if (page_size
!= NULL
)
1779 for (i
= 0; i
< page_size
->num_choices
; i
++, val
++)
1780 val
->string
.text
= _cupsStrAlloc(page_size
->choices
[i
].choice
);
1782 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1783 "media-default", NULL
, page_size
->defchoice
);
1785 else if (input_slot
!= NULL
)
1786 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1787 "media-default", NULL
, input_slot
->defchoice
);
1788 else if (media_type
!= NULL
)
1789 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1790 "media-default", NULL
, media_type
->defchoice
);
1791 else if (media_quality
!= NULL
)
1792 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1793 "media-default", NULL
, media_quality
->defchoice
);
1795 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1796 "media-default", NULL
, "none");
1804 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
1806 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1807 "output-bin-supported", output_bin
->num_choices
,
1812 for (i
= 0, val
= attr
->values
;
1813 i
< output_bin
->num_choices
;
1815 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
1823 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1824 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1825 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1826 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
1827 duplex
= ppdFindOption(ppd
, "JCLDuplex");
1829 if (duplex
&& duplex
->num_choices
> 1)
1831 p
->type
|= CUPS_PRINTER_DUPLEX
;
1833 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1834 "sides-supported", 3, NULL
, sides
);
1836 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
1837 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1838 "sides-default", NULL
, "two-sided-short-edge");
1839 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
1840 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1841 "sides-default", NULL
, "two-sided-long-edge");
1843 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1844 "sides-default", NULL
, "one-sided");
1847 if (ppdFindOption(ppd
, "Collate") != NULL
)
1848 p
->type
|= CUPS_PRINTER_COLLATE
;
1850 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
1852 p
->type
|= CUPS_PRINTER_STAPLE
;
1853 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
1856 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
1858 p
->type
|= CUPS_PRINTER_BIND
;
1859 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
1862 for (i
= 0; i
< ppd
->num_sizes
; i
++)
1863 if (ppd
->sizes
[i
].length
> 1728)
1864 p
->type
|= CUPS_PRINTER_LARGE
;
1865 else if (ppd
->sizes
[i
].length
> 1008)
1866 p
->type
|= CUPS_PRINTER_MEDIUM
;
1868 p
->type
|= CUPS_PRINTER_SMALL
;
1871 * Add a filter from application/vnd.cups-raw to printer/name to
1872 * handle "raw" printing by users.
1875 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1878 * Add any filters in the PPD file...
1881 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
1882 for (i
= 0; i
< ppd
->num_filters
; i
++)
1884 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
1885 add_printer_filter(p
, ppd
->filters
[i
]);
1888 if (ppd
->num_filters
== 0)
1891 * If there are no filters, add a PostScript printing filter.
1894 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1898 * Show current and available port monitors for this printer...
1901 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "port-monitor",
1902 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
1905 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1907 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
1911 if (strstr(ppd
->protocols
, "TBCP"))
1913 else if (strstr(ppd
->protocols
, "BCP"))
1917 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1918 "port-monitor-supported", i
, NULL
, NULL
);
1920 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
1922 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1924 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
1925 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppdattr
->value
);
1929 if (strstr(ppd
->protocols
, "TBCP"))
1930 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
1931 else if (strstr(ppd
->protocols
, "BCP"))
1932 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
1936 * Close the PPD and set the type...
1941 printer_type
= p
->type
;
1943 else if (!access(filename
, 0))
1945 int pline
; /* PPD line number */
1946 ppd_status_t pstatus
; /* PPD load status */
1949 pstatus
= ppdLastError(&pline
);
1951 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
1954 if (pstatus
<= PPD_ALLOC_ERROR
)
1955 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
1957 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
1958 ppdErrorString(pstatus
), pline
);
1960 cupsdLogMessage(CUPSD_LOG_INFO
,
1961 "Hint: Run \"cupstestppd %s\" and fix any errors.",
1965 * Add a filter from application/vnd.cups-raw to printer/name to
1966 * handle "raw" printing by users.
1969 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1972 * Add a PostScript filter, since this is still possibly PS printer.
1975 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1980 * If we have an interface script, add a filter entry for it...
1983 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
1985 if (!access(filename
, X_OK
))
1988 * Yes, we have a System V style interface script; use it!
1991 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1992 "printer-make-and-model", NULL
, "Local System V Printer");
1994 snprintf(filename
, sizeof(filename
), "*/* 0 %s/interfaces/%s",
1995 ServerRoot
, p
->name
);
1996 add_printer_filter(p
, filename
);
1998 else if (p
->device_uri
&&
1999 !strncmp(p
->device_uri
, "ipp://", 6) &&
2000 (strstr(p
->device_uri
, "/printers/") != NULL
||
2001 strstr(p
->device_uri
, "/classes/") != NULL
))
2004 * Tell the client this is really a hard-wired remote printer.
2007 printer_type
|= CUPS_PRINTER_REMOTE
;
2010 * Point the printer-uri-supported attribute to the
2014 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2015 "printer-uri-supported", NULL
, p
->device_uri
);
2018 * Then set the make-and-model accordingly...
2021 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2022 "printer-make-and-model", NULL
, "Remote Printer");
2025 * Print all files directly...
2033 * Otherwise we have neither - treat this as a "dumb" printer
2034 * with no PPD file...
2037 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2038 "printer-make-and-model", NULL
, "Local Raw Printer");
2044 ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2045 "finishings-supported", num_finishings
, (int *)finishings
);
2046 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2047 "finishings-default", IPP_FINISHINGS_NONE
);
2052 * Copy the printer options into a browse attributes string we can re-use.
2055 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2057 const char *valptr
; /* Pointer into value */
2058 char *attrptr
; /* Pointer into attribute string */
2062 * Free the old browse attributes as needed...
2065 if (p
->browse_attrs
)
2066 free(p
->browse_attrs
);
2069 * Compute the length of all attributes + job-sheets, lease-duration,
2070 * and BrowseLocalOptions.
2073 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2077 length
+= strlen(option
->name
) + 2;
2081 for (valptr
= option
->value
; *valptr
; valptr
++)
2082 if (strchr(" \"\'\\", *valptr
))
2089 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2091 if (BrowseLocalOptions
)
2092 length
+= 12 + strlen(BrowseLocalOptions
);
2095 * Allocate the new string...
2098 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2099 cupsdLogMessage(CUPSD_LOG_ERROR
,
2100 "Unable to allocate %d bytes for browse data!",
2105 * Got the allocated string, now copy the options and attributes over...
2108 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2109 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2110 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2112 if (BrowseLocalOptions
)
2114 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2115 attrptr
+= strlen(attrptr
);
2118 for (i
= p
->num_options
, option
= p
->options
;
2123 strcpy(attrptr
, option
->name
);
2124 attrptr
+= strlen(attrptr
);
2130 for (valptr
= option
->value
; *valptr
; valptr
++)
2132 if (strchr(" \"\'\\", *valptr
))
2135 *attrptr
++ = *valptr
;
2145 * Populate the document-format-supported attribute...
2148 add_printer_formats(p
);
2150 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2154 * Add name-default attributes...
2157 add_printer_defaults(p
);
2161 * Write the IRIX printer config and status files...
2164 write_irix_config(p
);
2165 write_irix_state(p
);
2171 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2175 cupsdSetPrinterReasons(
2176 cupsd_printer_t
*p
, /* I - Printer */
2177 const char *s
) /* I - Reasons strings */
2179 int i
; /* Looping var */
2180 const char *sptr
; /* Pointer into reasons */
2181 char reason
[255], /* Reason string */
2182 *rptr
; /* Pointer into reason */
2185 if (s
[0] == '-' || s
[0] == '+')
2188 * Add/remove reasons...
2196 * Replace reasons...
2201 for (i
= 0; i
< p
->num_reasons
; i
++)
2202 free(p
->reasons
[i
]);
2208 * Loop through all of the reasons...
2214 * Skip leading whitespace and commas...
2217 while (isspace(*sptr
& 255) || *sptr
== ',')
2220 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2221 if (rptr
< (reason
+ sizeof(reason
) - 1))
2235 for (i
= 0; i
< p
->num_reasons
; i
++)
2236 if (!strcasecmp(reason
, p
->reasons
[i
]))
2239 * Found a match, so remove it...
2243 free(p
->reasons
[i
]);
2245 if (i
< p
->num_reasons
)
2246 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2247 (p
->num_reasons
- i
) * sizeof(char *));
2252 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2258 for (i
= 0; i
< p
->num_reasons
; i
++)
2259 if (!strcasecmp(reason
, p
->reasons
[i
]))
2262 if (i
>= p
->num_reasons
)
2264 p
->reasons
[i
] = strdup(reason
);
2273 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2277 cupsdSetPrinterState(
2278 cupsd_printer_t
*p
, /* I - Printer to change */
2279 ipp_pstate_t s
, /* I - New state */
2280 int update
) /* I - Update printers.conf? */
2282 ipp_pstate_t old_state
; /* Old printer state */
2286 * Can't set status of remote printers...
2289 if (p
->type
& CUPS_PRINTER_REMOTE
)
2293 * Set the new state...
2296 old_state
= p
->state
;
2301 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, p
, NULL
,
2302 "%s \"%s\" state changed.",
2303 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2307 * Let the browse code know this needs to be updated...
2311 p
->state_time
= time(NULL
);
2315 write_irix_state(p
);
2319 cupsdAddPrinterHistory(p
);
2322 * Save the printer configuration if a printer goes from idle or processing
2323 * to stopped (or visa-versa)...
2326 if ((old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
) &&
2329 if (p
->type
& CUPS_PRINTER_CLASS
)
2330 cupsdSaveAllClasses();
2332 cupsdSaveAllPrinters();
2338 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2342 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2343 int update
)/* I - Update printers.conf? */
2345 cupsd_job_t
*job
; /* Active print job */
2349 * Set the printer state...
2352 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2355 * See if we have a job printing on this printer...
2361 * Get pointer to job...
2364 job
= (cupsd_job_t
*)p
->job
;
2370 cupsdStopJob(job
, 0);
2373 * Reset the state to pending...
2376 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2377 job
->state_value
= IPP_JOB_PENDING
;
2385 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2389 cupsdUpdatePrinters(void)
2391 cupsd_printer_t
*p
; /* Current printer */
2395 * Loop through the printers and recreate the printer attributes
2396 * for any local printers since the policy and/or access control
2397 * stuff may have changed. Also, if browsing is disabled, remove
2398 * any remote printers...
2401 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2403 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2405 if (!Browsing
&& (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_REMOTE
)))
2407 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2408 cupsArrayRemove(ImplicitPrinters
, p
);
2410 cupsArraySave(Printers
);
2411 cupsdDeletePrinter(p
, 0);
2412 cupsArrayRestore(Printers
);
2415 else if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2416 cupsdSetPrinterAttrs(p
);
2419 * Update the operation policy pointer...
2422 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
2423 p
->op_policy_ptr
= DefaultPolicyPtr
;
2429 * 'cupsdValidateDest()' - Validate a printer/class destination.
2432 const char * /* O - Printer or class name */
2434 const char *hostname
, /* I - Host name */
2435 const char *resource
, /* I - Resource name */
2436 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
2437 cupsd_printer_t
**printer
) /* O - Printer pointer */
2439 cupsd_printer_t
*p
; /* Current printer */
2440 char localname
[1024],/* Localized hostname */
2441 *lptr
, /* Pointer into localized hostname */
2442 *sptr
; /* Pointer into server name */
2445 DEBUG_printf(("cupsdValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname
, resource
,
2449 * Initialize return values...
2455 *dtype
= (cups_ptype_t
)0;
2458 * See if the resource is a class or printer...
2461 if (!strncmp(resource
, "/classes/", 9))
2469 else if (!strncmp(resource
, "/printers/", 10))
2480 * Bad resource name...
2487 * See if the printer or class name exists...
2490 p
= cupsdFindDest(resource
);
2492 if (p
== NULL
&& strchr(resource
, '@') == NULL
)
2499 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2500 CUPS_PRINTER_REMOTE
);
2505 * Change localhost to the server name...
2508 if (!strcasecmp(hostname
, "localhost"))
2509 hostname
= ServerName
;
2511 strlcpy(localname
, hostname
, sizeof(localname
));
2513 if (!strcasecmp(hostname
, ServerName
))
2516 * Localize the hostname...
2519 lptr
= strchr(localname
, '.');
2520 sptr
= strchr(ServerName
, '.');
2522 if (sptr
!= NULL
&& lptr
!= NULL
)
2525 * Strip the common domain name components...
2528 while (lptr
!= NULL
)
2530 if (!strcasecmp(lptr
, sptr
))
2536 lptr
= strchr(lptr
+ 1, '.');
2541 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
2544 * Find a matching printer or class...
2547 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2549 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2550 if (!strcasecmp(p
->hostname
, localname
) &&
2551 !strcasecmp(p
->name
, resource
))
2556 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2557 CUPS_PRINTER_REMOTE
);
2566 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2571 cupsdWritePrintcap(void)
2573 cups_file_t
*fp
; /* printcap file */
2574 cupsd_printer_t
*p
; /* Current printer */
2579 * Update the IRIX printer state for the default printer; if
2580 * no printers remain, then the default printer file will be
2584 write_irix_state(DefaultPrinter
);
2588 * See if we have a printcap file; if not, don't bother writing it.
2591 if (!Printcap
|| !*Printcap
)
2595 * Open the printcap file...
2598 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
2602 * Put a comment header at the top so that users will know where the
2603 * data has come from...
2606 cupsFilePuts(fp
, "# This file was automatically generated by cupsd(8) from the\n");
2607 cupsFilePrintf(fp
, "# %s/printers.conf file. All changes to this file\n",
2609 cupsFilePuts(fp
, "# will be lost.\n");
2614 * Write a new printcap with the current list of printers.
2617 switch (PrintcapFormat
)
2621 * Each printer is put in the file as:
2631 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
2632 DefaultPrinter
->info
, ServerName
, DefaultPrinter
->name
);
2634 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2636 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2637 if (p
!= DefaultPrinter
)
2638 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
2639 ServerName
, p
->name
);
2642 case PRINTCAP_SOLARIS
:
2644 * Each printer is put in the file as:
2646 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2647 * _default:use=DefaultPrinter
2649 * :bsdaddr=ServerName,Printer1:\
2650 * :description=Description:
2652 * :bsdaddr=ServerName,Printer2:\
2653 * :description=Description:
2655 * :bsdaddr=ServerName,Printer3:\
2656 * :description=Description:
2659 * :bsdaddr=ServerName,PrinterN:\
2660 * :description=Description:
2663 cupsFilePuts(fp
, "_all:all=");
2664 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2666 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
2667 cupsFilePrintf(fp
, "%s%c", p
->name
,
2668 cupsArrayNext(Printers
) ? ',' : '\n');
2671 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
2673 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2675 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2676 cupsFilePrintf(fp
, "%s:\\\n"
2677 "\t:bsdaddr=%s,%s:\\\n"
2678 "\t:description=%s:\n",
2679 p
->name
, ServerName
, p
->name
, p
->info
? p
->info
: "");
2693 * 'cupsdSanitizeURI()' - Sanitize a device URI...
2696 char * /* O - New device URI */
2697 cupsdSanitizeURI(const char *uri
, /* I - Original device URI */
2698 char *buffer
, /* O - New device URI */
2699 int buflen
) /* I - Size of new device URI buffer */
2701 char *start
, /* Start of data after scheme */
2702 *slash
, /* First slash after scheme:// */
2703 *ptr
; /* Pointer into user@host:port part */
2707 * Range check input...
2710 if (!uri
|| !buffer
|| buflen
< 2)
2714 * Copy the device URI to the new buffer...
2717 strlcpy(buffer
, uri
, buflen
);
2720 * Find the end of the scheme:// part...
2723 if ((ptr
= strchr(buffer
, ':')) == NULL
)
2724 return (buffer
); /* No scheme: part... */
2726 for (start
= ptr
+ 1; *start
; start
++)
2731 * Find the next slash (/) in the URI...
2734 if ((slash
= strchr(start
, '/')) == NULL
)
2735 slash
= start
+ strlen(start
); /* No slash, point to the end */
2738 * Check for an @ sign before the slash...
2741 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
2744 * Found an @ sign and it is before the resource part, so we have
2745 * an authentication string. Copy the remaining URI over the
2746 * authentication string...
2749 _cups_strcpy(start
, ptr
+ 1);
2753 * Return the new device URI...
2761 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
2765 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
2767 int i
; /* Looping var */
2768 int num_options
; /* Number of default options */
2769 cups_option_t
*options
, /* Default options */
2770 *option
; /* Current option */
2771 char name
[256]; /* name-default */
2775 * Add all of the default options from the .conf files...
2778 for (num_options
= 0, i
= p
->num_options
, option
= p
->options
;
2782 if (strcmp(option
->name
, "ipp-options") &&
2783 strcmp(option
->name
, "job-sheets") &&
2784 strcmp(option
->name
, "lease-duration"))
2786 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
2787 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
2792 * Convert options to IPP attributes...
2795 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
2796 cupsFreeOptions(num_options
, options
);
2799 * Add standard -default attributes as needed...
2802 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
2803 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
2806 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
2807 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2808 "job-hold-until-default", NULL
, "no-hold");
2810 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
2811 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2812 "job-priority-default", 50);
2814 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
2815 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2816 "number-up-default", 1);
2818 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
2819 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2820 "orientation-requested-default", IPP_PORTRAIT
);
2825 * 'add_printer_filter()' - Add a MIME filter for a printer.
2830 cupsd_printer_t
*p
, /* I - Printer to add to */
2831 const char *filter
) /* I - Filter to add */
2833 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
2834 type
[MIME_MAX_TYPE
], /* Type for filter */
2835 program
[1024]; /* Program/filter name */
2836 int cost
; /* Cost of filter */
2837 mime_type_t
*temptype
; /* MIME type looping var */
2838 char filename
[1024]; /* Full filter filename */
2842 * Parse the filter string; it should be in the following format:
2844 * super/type cost program
2847 if (sscanf(filter
, "%15[^/]/%31s%d%1023s", super
, type
, &cost
, program
) != 4)
2849 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
2855 * See if the filter program exists; if not, stop the printer and flag
2859 if (strcmp(program
, "-"))
2861 if (program
[0] == '/')
2862 strlcpy(filename
, program
, sizeof(filename
));
2864 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
2866 if (access(filename
, X_OK
))
2868 snprintf(p
->state_message
, sizeof(p
->state_message
),
2869 "Filter \"%s\" for printer \"%s\" not available: %s",
2870 program
, p
->name
, strerror(errno
));
2871 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 0);
2872 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
2873 cupsdAddPrinterHistory(p
);
2875 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
2880 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
2881 * application/vnd.cups-command...
2884 if (!strcasecmp(super
, "application") &&
2885 !strcasecmp(type
, "vnd.cups-command"))
2886 p
->type
|= CUPS_PRINTER_COMMANDS
;
2889 * Add the filter to the MIME database, supporting wildcards as needed...
2892 for (temptype
= mimeFirstType(MimeDatabase
);
2894 temptype
= mimeNextType(MimeDatabase
))
2895 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
2896 !strcasecmp(temptype
->super
, super
)) &&
2897 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
2899 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2900 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
2901 p
->name
, temptype
->super
, temptype
->type
,
2902 p
->filetype
->super
, p
->filetype
->type
,
2904 mimeAddFilter(MimeDatabase
, temptype
, p
->filetype
, cost
, program
);
2910 * 'add_printer_formats()' - Add document-format-supported values for a printer.
2914 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
2916 int i
; /* Looping var */
2917 mime_type_t
*type
; /* Current MIME type */
2918 cups_array_t
*filters
; /* Filters */
2919 ipp_attribute_t
*attr
; /* document-format-supported attribute */
2920 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
2921 /* MIME type name */
2925 * Raw (and remote) queues advertise all of the supported MIME
2929 cupsArrayDelete(p
->filetypes
);
2930 p
->filetypes
= NULL
;
2934 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
2935 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
2936 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
2941 * Otherwise, loop through the supported MIME types and see if there
2942 * are filters for them...
2945 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
2946 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
2948 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
2950 for (type
= mimeFirstType(MimeDatabase
);
2952 type
= mimeNextType(MimeDatabase
))
2954 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
2956 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
2958 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2959 "add_printer_formats: %s: %s needs %d filters",
2960 p
->name
, mimetype
, cupsArrayCount(filters
));
2962 cupsArrayDelete(filters
);
2963 cupsArrayAdd(p
->filetypes
, type
);
2966 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2967 "add_printer_formats: %s: %s not supported",
2971 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2972 "add_printer_formats: %s: %d supported types",
2973 p
->name
, cupsArrayCount(p
->filetypes
) + 1);
2976 * Add the file formats that can be filtered...
2979 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
2980 !cupsArrayFind(p
->filetypes
, type
))
2985 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
2986 "document-format-supported",
2987 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
2990 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
2992 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
2994 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
2996 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
2998 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3004 * 'compare_printers()' - Compare two printers.
3007 static int /* O - Result of comparison */
3008 compare_printers(void *first
, /* I - First printer */
3009 void *second
, /* I - Second printer */
3010 void *data
) /* I - App data (not used) */
3012 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3013 ((cupsd_printer_t
*)second
)->name
));
3018 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3022 delete_printer_filters(
3023 cupsd_printer_t
*p
) /* I - Printer to remove from */
3025 mime_filter_t
*filter
; /* MIME filter looping var */
3029 * Range check input...
3036 * Remove all filters from the MIME database that have a destination
3037 * type == printer...
3040 for (filter
= mimeFirstFilter(MimeDatabase
);
3042 filter
= mimeNextFilter(MimeDatabase
))
3043 if (filter
->dst
== p
->filetype
)
3046 * Delete the current filter...
3049 mimeDeleteFilter(MimeDatabase
, filter
);
3056 * 'write_irix_config()' - Update the config files used by the IRIX
3061 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
3063 char filename
[1024]; /* Interface script filename */
3064 cups_file_t
*fp
; /* Interface script file */
3065 ipp_attribute_t
*attr
; /* Attribute data */
3069 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3070 * tools. First the interface script that tells the tools what kind of
3071 * printer we have...
3074 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
3076 if (p
->type
& CUPS_PRINTER_CLASS
)
3078 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3080 cupsFilePuts(fp
, "#!/bin/sh\n");
3082 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
3083 IPP_TAG_TEXT
)) != NULL
)
3084 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
3085 else if (p
->type
& CUPS_PRINTER_CLASS
)
3086 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
3088 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
3090 if (p
->type
& CUPS_PRINTER_COLOR
)
3091 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
3093 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
3095 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
3096 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
3100 chmod(filename
, 0755);
3101 chown(filename
, User
, Group
);
3105 * Then the member file that tells which device file the queue is connected
3106 * to... Networked printers use "/dev/null" in this file, so that's what
3107 * we use (the actual device URI can confuse some apps...)
3110 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
3112 if (p
->type
& CUPS_PRINTER_CLASS
)
3114 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3116 cupsFilePuts(fp
, "/dev/null\n");
3120 chmod(filename
, 0644);
3121 chown(filename
, User
, Group
);
3125 * The gui_interface file is a script or program that launches a GUI
3126 * option panel for the printer, using options specified on the
3127 * command-line in the third argument. The option panel must send
3128 * any printing options to stdout on a single line when the user
3129 * accepts them, or nothing if the user cancels the dialog.
3131 * The default options panel program is /usr/bin/glpoptions, from
3132 * the ESP Print Pro software. You can select another using the
3133 * PrintcapGUI option.
3136 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
3138 if (p
->type
& CUPS_PRINTER_CLASS
)
3140 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3142 cupsFilePuts(fp
, "#!/bin/sh\n");
3143 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
3147 chmod(filename
, 0755);
3148 chown(filename
, User
, Group
);
3152 * The POD config file is needed by the printstatus command to show
3153 * the printer location and device.
3156 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
3158 if (p
->type
& CUPS_PRINTER_CLASS
)
3160 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3162 cupsFilePrintf(fp
, "Printer Class | %s\n",
3163 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
3164 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
3165 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
3166 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
3167 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
? p
->device_uri
: "");
3168 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
3169 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
3170 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
3174 chmod(filename
, 0664);
3175 chown(filename
, User
, Group
);
3181 * 'write_irix_state()' - Update the status files used by IRIX printing
3186 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
3188 char filename
[1024]; /* Interface script filename */
3189 cups_file_t
*fp
; /* Interface script file */
3190 int tag
; /* Status tag value */
3196 * The POD status file is needed for the printstatus window to
3197 * provide the current status of the printer.
3200 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
3202 if (p
->type
& CUPS_PRINTER_CLASS
)
3204 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3206 cupsFilePrintf(fp
, "Operational Status | %s\n",
3207 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
3208 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
3210 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
3211 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
3212 p
->device_uri
? p
->device_uri
: "");
3213 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
3214 p
->accepting
? "Accepting" : "Not accepting");
3215 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
3219 chmod(filename
, 0664);
3220 chown(filename
, User
, Group
);
3224 * The activeicons file is needed to provide desktop icons for printers:
3226 * [ quoted from /usr/lib/print/tagit ]
3228 * --- Type of printer tags (base values)
3230 * Dumb=66048 # 0x10200
3231 * DumbColor=66080 # 0x10220
3232 * Raster=66112 # 0x10240
3233 * ColorRaster=66144 # 0x10260
3234 * Plotter=66176 # 0x10280
3235 * PostScript=66208 # 0x102A0
3236 * ColorPostScript=66240 # 0x102C0
3237 * MonoPostScript=66272 # 0x102E0
3239 * --- Printer state modifiers for local printers
3244 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3246 * --- Printer state modifiers for network printers
3250 * NetFaulted=10 # 0xA
3251 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3254 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
3256 if (p
->type
& CUPS_PRINTER_CLASS
)
3258 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3260 if (p
->type
& CUPS_PRINTER_COLOR
)
3265 if (p
->type
& CUPS_PRINTER_REMOTE
)
3268 if (p
->state
== IPP_PRINTER_PROCESSING
)
3271 else if (p
->state
== IPP_PRINTER_STOPPED
)
3274 cupsFilePuts(fp
, "#!/bin/sh\n");
3275 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
3279 chmod(filename
, 0755);
3280 chown(filename
, User
, Group
);
3285 * The default file is needed by the printers window to show
3286 * the default printer.
3289 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
3291 if (DefaultPrinter
!= NULL
)
3293 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3295 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
3299 chmod(filename
, 0644);
3300 chown(filename
, User
, Group
);
3310 * End of "$Id: printers.c 5686 2006-06-21 21:02:56Z mike $".