2 * "$Id: printers.c 5828 2006-08-15 21:21:45Z 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
);
552 /* number-up-supported */
553 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
554 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
556 /* operations-supported */
557 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
558 "operations-supported",
559 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, (int *)ops
);
561 /* orientation-requested-supported */
562 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
563 "orientation-requested-supported", 4, (int *)orients
);
565 /* page-ranges-supported */
566 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
568 /* pdf-override-supported */
569 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
570 "pdl-override-supported", NULL
, "not-attempted");
572 /* printer-error-policy-supported */
573 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
574 "printer-error-policy-supported",
575 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
577 /* printer-op-policy-supported */
578 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
579 "printer-op-policy-supported", NumPolicies
, NULL
, NULL
);
580 for (i
= 0; i
< NumPolicies
; i
++)
581 attr
->values
[i
].string
.text
= _cupsStrAlloc(Policies
[i
]->name
);
586 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
590 cupsdDeleteAllPrinters(void)
592 cupsd_printer_t
*p
; /* Pointer to current printer/class */
595 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
597 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
598 if (!(p
->type
& CUPS_PRINTER_CLASS
))
599 cupsdDeletePrinter(p
, 0);
604 * 'cupsdDeletePrinter()' - Delete a printer from the system.
609 cupsd_printer_t
*p
, /* I - Printer to delete */
610 int update
) /* I - Update printers.conf? */
612 int i
; /* Looping var */
614 char filename
[1024]; /* Interface script filename */
618 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
622 * Save the current position in the Printers array...
625 cupsArraySave(Printers
);
628 * Stop printing on this printer...
631 cupsdStopPrinter(p
, update
);
634 * If this printer is the next for browsing, point to the next one...
639 cupsArrayFind(Printers
, p
);
640 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
644 * Remove the printer from the list...
647 cupsArrayRemove(Printers
, p
);
649 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
650 cupsArrayRemove(ImplicitPrinters
, p
);
653 * Remove the dummy interface/icon/option files under IRIX...
657 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
660 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
664 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
667 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
670 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
673 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
678 * If p is the default printer, assign a different one...
681 if (p
== DefaultPrinter
)
683 DefaultPrinter
= NULL
;
685 if (UseNetworkDefault
)
688 * Find the first network default printer and use it...
691 cupsd_printer_t
*dp
; /* New default printer */
694 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
696 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
697 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
706 * Remove this printer from any classes and send a browse delete message...
709 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
711 cupsdDeletePrinterFromClasses(p
);
712 cupsdSendBrowseDelete(p
);
716 * Free all memory used by the printer...
719 if (p
->printers
!= NULL
)
722 if (MaxPrinterHistory
)
724 for (i
= 0; i
< p
->num_history
; i
++)
725 ippDelete(p
->history
[i
]);
730 for (i
= 0; i
< p
->num_reasons
; i
++)
735 delete_printer_filters(p
);
737 mimeDeleteType(MimeDatabase
, p
->filetype
);
739 cupsdFreePrinterUsers(p
);
742 cupsdClearString(&p
->uri
);
743 cupsdClearString(&p
->hostname
);
744 cupsdClearString(&p
->name
);
745 cupsdClearString(&p
->location
);
746 cupsdClearString(&p
->make_model
);
747 cupsdClearString(&p
->info
);
748 cupsdClearString(&p
->job_sheets
[0]);
749 cupsdClearString(&p
->job_sheets
[1]);
750 cupsdClearString(&p
->device_uri
);
751 cupsdClearString(&p
->port_monitor
);
752 cupsdClearString(&p
->op_policy
);
753 cupsdClearString(&p
->error_policy
);
755 cupsArrayDelete(p
->filetypes
);
758 free(p
->browse_attrs
);
761 cupsdClearString(&p
->recoverable
);
762 #endif /* __APPLE__ */
764 cupsFreeOptions(p
->num_options
, p
->options
);
769 * Restore the previous position in the Printers array...
772 cupsArrayRestore(Printers
);
777 * 'cupsdFindDest()' - Find a destination in the list.
780 cupsd_printer_t
* /* O - Destination in list */
781 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
783 cupsd_printer_t key
; /* Search key */
786 key
.name
= (char *)name
;
787 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
792 * 'cupsdFindPrinter()' - Find a printer in the list.
795 cupsd_printer_t
* /* O - Printer in list */
796 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
798 cupsd_printer_t
*p
; /* Printer in list */
801 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
809 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
813 cupsdFreePrinterUsers(
814 cupsd_printer_t
*p
) /* I - Printer */
816 int i
; /* Looping var */
819 if (!p
|| !p
->num_users
)
822 for (i
= 0; i
< p
->num_users
; i
++)
823 free((void *)p
->users
[i
]);
833 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
837 cupsdLoadAllPrinters(void)
839 cups_file_t
*fp
; /* printers.conf file */
840 int linenum
; /* Current line number */
841 char line
[1024], /* Line from file */
842 *value
, /* Pointer to value */
843 *valueptr
; /* Pointer into value */
844 cupsd_printer_t
*p
; /* Current printer */
848 * Open the printers.conf file...
851 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
852 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
855 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
861 * Read printer configurations until we hit EOF...
867 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
870 * Decode the directive...
873 if (!strcasecmp(line
, "<Printer") ||
874 !strcasecmp(line
, "<DefaultPrinter"))
877 * <Printer name> or <DefaultPrinter name>
880 if (p
== NULL
&& value
)
883 * Add the printer and a base file type...
886 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
888 p
= cupsdAddPrinter(value
);
890 p
->state
= IPP_PRINTER_IDLE
;
893 * Set the default printer as needed...
896 if (!strcasecmp(line
, "<DefaultPrinter"))
901 cupsdLogMessage(CUPSD_LOG_ERROR
,
902 "Syntax error on line %d of printers.conf.", linenum
);
906 else if (!strcasecmp(line
, "</Printer>"))
911 * Close out the current printer...
914 cupsdSetPrinterAttrs(p
);
915 cupsdAddPrinterHistory(p
);
917 if (p
->device_uri
&& strncmp(p
->device_uri
, "file:", 5) &&
918 p
->state
!= IPP_PRINTER_STOPPED
)
921 * See if the backend exists...
924 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
927 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
928 *valueptr
= '\0'; /* Chop everything but URI scheme */
933 * Backend does not exist, stop printer...
936 p
->state
= IPP_PRINTER_STOPPED
;
937 snprintf(p
->state_message
, sizeof(p
->state_message
),
938 "Backend %s does not exist!", line
);
946 cupsdLogMessage(CUPSD_LOG_ERROR
,
947 "Syntax error on line %d of printers.conf.", linenum
);
953 cupsdLogMessage(CUPSD_LOG_ERROR
,
954 "Syntax error on line %d of printers.conf.", linenum
);
957 else if (!strcasecmp(line
, "Info"))
960 cupsdSetString(&p
->info
, value
);
962 else if (!strcasecmp(line
, "Location"))
965 cupsdSetString(&p
->location
, value
);
967 else if (!strcasecmp(line
, "DeviceURI"))
970 cupsdSetString(&p
->device_uri
, value
);
973 cupsdLogMessage(CUPSD_LOG_ERROR
,
974 "Syntax error on line %d of printers.conf.", linenum
);
978 else if (!strcasecmp(line
, "Option") && value
)
984 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
987 cupsdLogMessage(CUPSD_LOG_ERROR
,
988 "Syntax error on line %d of printers.conf.", linenum
);
991 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
993 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
997 else if (!strcasecmp(line
, "PortMonitor"))
999 if (value
&& strcmp(value
, "none"))
1000 cupsdSetString(&p
->port_monitor
, value
);
1002 cupsdClearString(&p
->port_monitor
);
1005 cupsdLogMessage(CUPSD_LOG_ERROR
,
1006 "Syntax error on line %d of printers.conf.", linenum
);
1010 else if (!strcasecmp(line
, "State"))
1013 * Set the initial queue state...
1016 if (value
&& !strcasecmp(value
, "idle"))
1017 p
->state
= IPP_PRINTER_IDLE
;
1018 else if (value
&& !strcasecmp(value
, "stopped"))
1019 p
->state
= IPP_PRINTER_STOPPED
;
1022 cupsdLogMessage(CUPSD_LOG_ERROR
,
1023 "Syntax error on line %d of printers.conf.", linenum
);
1027 else if (!strcasecmp(line
, "StateMessage"))
1030 * Set the initial queue state message...
1034 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1036 else if (!strcasecmp(line
, "StateTime"))
1039 * Set the state time...
1043 p
->state_time
= atoi(value
);
1045 else if (!strcasecmp(line
, "Accepting"))
1048 * Set the initial accepting state...
1052 (!strcasecmp(value
, "yes") ||
1053 !strcasecmp(value
, "on") ||
1054 !strcasecmp(value
, "true")))
1057 (!strcasecmp(value
, "no") ||
1058 !strcasecmp(value
, "off") ||
1059 !strcasecmp(value
, "false")))
1063 cupsdLogMessage(CUPSD_LOG_ERROR
,
1064 "Syntax error on line %d of printers.conf.", linenum
);
1068 else if (!strcasecmp(line
, "Shared"))
1071 * Set the initial shared state...
1075 (!strcasecmp(value
, "yes") ||
1076 !strcasecmp(value
, "on") ||
1077 !strcasecmp(value
, "true")))
1080 (!strcasecmp(value
, "no") ||
1081 !strcasecmp(value
, "off") ||
1082 !strcasecmp(value
, "false")))
1086 cupsdLogMessage(CUPSD_LOG_ERROR
,
1087 "Syntax error on line %d of printers.conf.", linenum
);
1091 else if (!strcasecmp(line
, "JobSheets"))
1094 * Set the initial job sheets...
1099 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1104 cupsdSetString(&p
->job_sheets
[0], value
);
1106 while (isspace(*valueptr
& 255))
1111 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1116 cupsdSetString(&p
->job_sheets
[1], value
);
1121 cupsdLogMessage(CUPSD_LOG_ERROR
,
1122 "Syntax error on line %d of printers.conf.", linenum
);
1126 else if (!strcasecmp(line
, "AllowUser"))
1131 cupsdAddPrinterUser(p
, value
);
1135 cupsdLogMessage(CUPSD_LOG_ERROR
,
1136 "Syntax error on line %d of printers.conf.", linenum
);
1140 else if (!strcasecmp(line
, "DenyUser"))
1145 cupsdAddPrinterUser(p
, value
);
1149 cupsdLogMessage(CUPSD_LOG_ERROR
,
1150 "Syntax error on line %d of printers.conf.", linenum
);
1154 else if (!strcasecmp(line
, "QuotaPeriod"))
1157 p
->quota_period
= atoi(value
);
1160 cupsdLogMessage(CUPSD_LOG_ERROR
,
1161 "Syntax error on line %d of printers.conf.", linenum
);
1165 else if (!strcasecmp(line
, "PageLimit"))
1168 p
->page_limit
= atoi(value
);
1171 cupsdLogMessage(CUPSD_LOG_ERROR
,
1172 "Syntax error on line %d of printers.conf.", linenum
);
1176 else if (!strcasecmp(line
, "KLimit"))
1179 p
->k_limit
= atoi(value
);
1182 cupsdLogMessage(CUPSD_LOG_ERROR
,
1183 "Syntax error on line %d of printers.conf.", linenum
);
1187 else if (!strcasecmp(line
, "OpPolicy"))
1190 cupsdSetString(&p
->op_policy
, value
);
1193 cupsdLogMessage(CUPSD_LOG_ERROR
,
1194 "Syntax error on line %d of printers.conf.", linenum
);
1198 else if (!strcasecmp(line
, "ErrorPolicy"))
1201 cupsdSetString(&p
->error_policy
, value
);
1204 cupsdLogMessage(CUPSD_LOG_ERROR
,
1205 "Syntax error on line %d of printers.conf.", linenum
);
1212 * Something else we don't understand...
1215 cupsdLogMessage(CUPSD_LOG_ERROR
,
1216 "Unknown configuration directive %s on line %d of printers.conf.",
1226 * 'cupsdRenamePrinter()' - Rename a printer.
1231 cupsd_printer_t
*p
, /* I - Printer */
1232 const char *name
) /* I - New name */
1235 * Remove the printer from the array(s) first...
1238 cupsArrayRemove(Printers
, p
);
1240 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1241 cupsArrayRemove(ImplicitPrinters
, p
);
1244 * Rename the printer type...
1247 mimeDeleteType(MimeDatabase
, p
->filetype
);
1248 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1251 * Rename the printer...
1254 cupsdSetString(&p
->name
, name
);
1257 * Reset printer attributes...
1260 cupsdSetPrinterAttrs(p
);
1263 * Add the printer back to the printer array(s)...
1266 cupsArrayAdd(Printers
, p
);
1268 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1269 cupsArrayAdd(ImplicitPrinters
, p
);
1274 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1279 cupsdSaveAllPrinters(void)
1281 int i
; /* Looping var */
1282 cups_file_t
*fp
; /* printers.conf file */
1283 char temp
[1024]; /* Temporary string */
1284 char backup
[1024]; /* printers.conf.O file */
1285 cupsd_printer_t
*printer
; /* Current printer class */
1286 time_t curtime
; /* Current time */
1287 struct tm
*curdate
; /* Current date */
1288 cups_option_t
*option
; /* Current option */
1292 * Create the printers.conf file...
1295 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1296 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1298 if (rename(temp
, backup
))
1300 if (errno
!= ENOENT
)
1301 cupsdLogMessage(CUPSD_LOG_ERROR
,
1302 "Unable to backup printers.conf - %s", strerror(errno
));
1305 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1307 cupsdLogMessage(CUPSD_LOG_ERROR
,
1308 "Unable to save printers.conf - %s", strerror(errno
));
1310 if (rename(backup
, temp
))
1311 cupsdLogMessage(CUPSD_LOG_ERROR
,
1312 "Unable to restore printers.conf - %s", strerror(errno
));
1316 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1319 * Restrict access to the file...
1322 fchown(cupsFileNumber(fp
), getuid(), Group
);
1323 fchmod(cupsFileNumber(fp
), 0600);
1326 * Write a small header to the file...
1329 curtime
= time(NULL
);
1330 curdate
= localtime(&curtime
);
1331 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1333 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1334 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1337 * Write each local printer known to the system...
1340 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1342 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1345 * Skip remote destinations and printer classes...
1348 if ((printer
->type
& CUPS_PRINTER_REMOTE
) ||
1349 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1350 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1354 * Write printers as needed...
1357 if (printer
== DefaultPrinter
)
1358 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1360 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1363 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
1365 if (printer
->location
)
1366 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
1368 if (printer
->device_uri
)
1369 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
1371 if (printer
->port_monitor
)
1372 cupsFilePrintf(fp
, "PortMonitor %s\n", printer
->port_monitor
);
1374 if (printer
->state
== IPP_PRINTER_STOPPED
)
1376 cupsFilePuts(fp
, "State Stopped\n");
1377 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
1380 cupsFilePuts(fp
, "State Idle\n");
1382 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1384 if (printer
->accepting
)
1385 cupsFilePuts(fp
, "Accepting Yes\n");
1387 cupsFilePuts(fp
, "Accepting No\n");
1389 if (printer
->shared
)
1390 cupsFilePuts(fp
, "Shared Yes\n");
1392 cupsFilePuts(fp
, "Shared No\n");
1394 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
1395 printer
->job_sheets
[1]);
1397 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1398 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1399 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1401 for (i
= 0; i
< printer
->num_users
; i
++)
1402 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
1405 if (printer
->op_policy
)
1406 cupsFilePrintf(fp
, "OpPolicy %s\n", printer
->op_policy
);
1407 if (printer
->error_policy
)
1408 cupsFilePrintf(fp
, "ErrorPolicy %s\n", printer
->error_policy
);
1410 for (i
= printer
->num_options
, option
= printer
->options
;
1413 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
1415 cupsFilePuts(fp
, "</Printer>\n");
1419 * Make IRIX desktop & printer status happy
1422 write_irix_state(printer
);
1431 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1435 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
1437 int i
, /* Looping var */
1438 length
; /* Length of browse attributes */
1439 char uri
[HTTP_MAX_URI
]; /* URI for printer */
1440 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1441 char filename
[1024]; /* Name of PPD file */
1442 int num_media
; /* Number of media options */
1443 cupsd_location_t
*auth
; /* Pointer to authentication element */
1444 const char *auth_supported
; /* Authentication supported */
1445 cups_ptype_t printer_type
; /* Printer type data */
1446 ppd_file_t
*ppd
; /* PPD file data */
1447 ppd_option_t
*input_slot
, /* InputSlot options */
1448 *media_type
, /* MediaType options */
1449 *page_size
, /* PageSize options */
1450 *output_bin
, /* OutputBin options */
1451 *media_quality
, /* EFMediaQualityMode options */
1452 *duplex
; /* Duplex options */
1453 ppd_attr_t
*ppdattr
; /* PPD attribute */
1454 ipp_attribute_t
*attr
; /* Attribute data */
1455 ipp_value_t
*val
; /* Attribute value */
1456 int num_finishings
; /* Number of finishings */
1457 ipp_finish_t finishings
[5]; /* finishings-supported values */
1458 cups_option_t
*option
; /* Current printer option */
1459 static const char * const sides
[3] = /* sides-supported values */
1462 "two-sided-long-edge",
1463 "two-sided-short-edge"
1467 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
1471 * Make sure that we have the common attributes defined...
1475 cupsdCreateCommonData();
1478 * Clear out old filters, if any...
1481 delete_printer_filters(p
);
1484 * Figure out the authentication that is required for the printer.
1487 auth_supported
= "requesting-user-name";
1488 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
1490 if (p
->type
& CUPS_PRINTER_CLASS
)
1491 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1493 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1495 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
1496 auth
->type
== AUTH_NONE
)
1497 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1501 if (auth
->type
== AUTH_BASIC
|| auth
->type
== AUTH_BASICDIGEST
)
1502 auth_supported
= "basic";
1503 else if (auth
->type
== AUTH_DIGEST
)
1504 auth_supported
= "digest";
1506 if (auth
->type
!= AUTH_NONE
)
1507 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1509 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1512 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1516 * Create the required IPP attributes for a printer...
1520 ippDelete(p
->attrs
);
1522 p
->attrs
= ippNew();
1524 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1525 "uri-authentication-supported", NULL
, auth_supported
);
1526 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1527 "uri-security-supported", NULL
, "none");
1528 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
1530 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1531 NULL
, p
->location
? p
->location
: "");
1532 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1533 NULL
, p
->info
? p
->info
: "");
1534 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
1540 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1541 "requesting-user-name-denied", p
->num_users
, NULL
,
1544 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1545 "requesting-user-name-allowed", p
->num_users
, NULL
,
1549 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1550 "job-quota-period", p
->quota_period
);
1551 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1552 "job-k-limit", p
->k_limit
);
1553 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1554 "job-page-limit", p
->page_limit
);
1556 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_REMOTE
))
1559 * Setup the job-sheets-default attribute...
1562 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1563 "job-sheets-default", 2, NULL
, NULL
);
1567 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
1568 Classification
: p
->job_sheets
[0]);
1569 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
1570 Classification
: p
->job_sheets
[1]);
1574 printer_type
= p
->type
;
1579 if (p
->type
& CUPS_PRINTER_REMOTE
)
1582 * Tell the client this is a remote printer of some type...
1585 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1586 "printer-uri-supported", NULL
, p
->uri
);
1589 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1590 "printer-make-and-model", NULL
, p
->make_model
);
1592 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1601 * Assign additional attributes depending on whether this is a printer
1605 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
1607 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
1612 * Add class-specific attributes...
1615 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
1616 p
->printers
[0]->make_model
)
1617 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1618 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
1620 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1621 "printer-make-and-model", NULL
, "Local Printer Class");
1623 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1624 "file:///dev/null");
1626 if (p
->num_printers
> 0)
1629 * Add a list of member URIs and names...
1632 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1633 "member-uris", p
->num_printers
, NULL
, NULL
);
1634 p
->type
|= CUPS_PRINTER_OPTIONS
;
1636 for (i
= 0; i
< p
->num_printers
; i
++)
1639 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->uri
);
1641 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
1644 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1645 "member-names", p
->num_printers
, NULL
, NULL
);
1649 for (i
= 0; i
< p
->num_printers
; i
++)
1650 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->name
);
1657 * Add printer-specific attributes... Start by sanitizing the device
1658 * URI so it doesn't have a username or password in it...
1662 strcpy(uri
, "file:/dev/null");
1663 else if (strstr(p
->device_uri
, "://") != NULL
)
1666 * http://..., ipp://..., etc.
1669 cupsdSanitizeURI(p
->device_uri
, uri
, sizeof(uri
));
1674 * file:..., serial:..., etc.
1677 strlcpy(uri
, p
->device_uri
, sizeof(uri
));
1680 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1684 * Assign additional attributes from the PPD file (if any)...
1687 p
->type
|= CUPS_PRINTER_BW
;
1688 finishings
[0] = IPP_FINISHINGS_NONE
;
1691 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
1694 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1697 * Add make/model and other various attributes...
1700 if (ppd
->color_device
)
1701 p
->type
|= CUPS_PRINTER_COLOR
;
1702 if (ppd
->variable_sizes
)
1703 p
->type
|= CUPS_PRINTER_VARIABLE
;
1704 if (!ppd
->manual_copies
)
1705 p
->type
|= CUPS_PRINTER_COPIES
;
1706 if ((ppdattr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
1707 if (ppdattr
->value
&& !strcasecmp(ppdattr
->value
, "true"))
1708 p
->type
|= CUPS_PRINTER_FAX
;
1710 ippAddBoolean(p
->attrs
, IPP_TAG_PRINTER
, "color-supported",
1712 if (ppd
->throughput
)
1713 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1714 "pages-per-minute", ppd
->throughput
);
1719 * The NickName can be localized in the character set specified
1720 * by the LanugageEncoding attribute. However, ppdOpen2() has
1721 * already converted the ppd->nickname member to UTF-8 for us
1722 * (the original attribute value is available separately)
1725 cupsdSetString(&p
->make_model
, ppd
->nickname
);
1727 else if (ppd
->modelname
)
1730 * Model name can only contain specific characters...
1733 cupsdSetString(&p
->make_model
, ppd
->modelname
);
1736 cupsdSetString(&p
->make_model
, "Bad PPD File");
1738 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1739 "printer-make-and-model", NULL
, p
->make_model
);
1742 * Add media options from the PPD file...
1745 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) != NULL
)
1746 num_media
= input_slot
->num_choices
;
1750 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
1751 num_media
+= media_type
->num_choices
;
1753 if ((page_size
= ppdFindOption(ppd
, "PageSize")) != NULL
)
1754 num_media
+= page_size
->num_choices
;
1756 if ((media_quality
= ppdFindOption(ppd
, "EFMediaQualityMode")) != NULL
)
1757 num_media
+= media_quality
->num_choices
;
1761 cupsdLogMessage(CUPSD_LOG_CRIT
,
1762 "The PPD file for printer %s contains no media "
1763 "options and is therefore invalid!", p
->name
);
1767 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1768 "media-supported", num_media
, NULL
, NULL
);
1773 if (input_slot
!= NULL
)
1774 for (i
= 0; i
< input_slot
->num_choices
; i
++, val
++)
1775 val
->string
.text
= _cupsStrAlloc(input_slot
->choices
[i
].choice
);
1777 if (media_type
!= NULL
)
1778 for (i
= 0; i
< media_type
->num_choices
; i
++, val
++)
1779 val
->string
.text
= _cupsStrAlloc(media_type
->choices
[i
].choice
);
1781 if (media_quality
!= NULL
)
1782 for (i
= 0; i
< media_quality
->num_choices
; i
++, val
++)
1783 val
->string
.text
= _cupsStrAlloc(media_quality
->choices
[i
].choice
);
1785 if (page_size
!= NULL
)
1787 for (i
= 0; i
< page_size
->num_choices
; i
++, val
++)
1788 val
->string
.text
= _cupsStrAlloc(page_size
->choices
[i
].choice
);
1790 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1791 "media-default", NULL
, page_size
->defchoice
);
1793 else if (input_slot
!= NULL
)
1794 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1795 "media-default", NULL
, input_slot
->defchoice
);
1796 else if (media_type
!= NULL
)
1797 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1798 "media-default", NULL
, media_type
->defchoice
);
1799 else if (media_quality
!= NULL
)
1800 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1801 "media-default", NULL
, media_quality
->defchoice
);
1803 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1804 "media-default", NULL
, "none");
1812 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
1814 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1815 "output-bin-supported", output_bin
->num_choices
,
1820 for (i
= 0, val
= attr
->values
;
1821 i
< output_bin
->num_choices
;
1823 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
1831 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1832 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1833 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1834 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
1835 duplex
= ppdFindOption(ppd
, "JCLDuplex");
1837 if (duplex
&& duplex
->num_choices
> 1)
1839 p
->type
|= CUPS_PRINTER_DUPLEX
;
1841 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1842 "sides-supported", 3, NULL
, sides
);
1844 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
1845 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1846 "sides-default", NULL
, "two-sided-short-edge");
1847 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
1848 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1849 "sides-default", NULL
, "two-sided-long-edge");
1851 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1852 "sides-default", NULL
, "one-sided");
1855 if (ppdFindOption(ppd
, "Collate") != NULL
)
1856 p
->type
|= CUPS_PRINTER_COLLATE
;
1858 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
1860 p
->type
|= CUPS_PRINTER_STAPLE
;
1861 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
1864 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
1866 p
->type
|= CUPS_PRINTER_BIND
;
1867 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
1870 for (i
= 0; i
< ppd
->num_sizes
; i
++)
1871 if (ppd
->sizes
[i
].length
> 1728)
1872 p
->type
|= CUPS_PRINTER_LARGE
;
1873 else if (ppd
->sizes
[i
].length
> 1008)
1874 p
->type
|= CUPS_PRINTER_MEDIUM
;
1876 p
->type
|= CUPS_PRINTER_SMALL
;
1879 * Add a filter from application/vnd.cups-raw to printer/name to
1880 * handle "raw" printing by users.
1883 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1886 * Add any filters in the PPD file...
1889 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
1890 for (i
= 0; i
< ppd
->num_filters
; i
++)
1892 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
1893 add_printer_filter(p
, ppd
->filters
[i
]);
1896 if (ppd
->num_filters
== 0)
1899 * If there are no filters, add a PostScript printing filter.
1902 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1906 * Show current and available port monitors for this printer...
1909 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "port-monitor",
1910 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
1913 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1915 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
1919 if (strstr(ppd
->protocols
, "TBCP"))
1921 else if (strstr(ppd
->protocols
, "BCP"))
1925 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1926 "port-monitor-supported", i
, NULL
, NULL
);
1928 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
1930 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1932 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
1933 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppdattr
->value
);
1937 if (strstr(ppd
->protocols
, "TBCP"))
1938 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
1939 else if (strstr(ppd
->protocols
, "BCP"))
1940 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
1944 * Close the PPD and set the type...
1949 printer_type
= p
->type
;
1951 else if (!access(filename
, 0))
1953 int pline
; /* PPD line number */
1954 ppd_status_t pstatus
; /* PPD load status */
1957 pstatus
= ppdLastError(&pline
);
1959 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
1962 if (pstatus
<= PPD_ALLOC_ERROR
)
1963 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
1965 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
1966 ppdErrorString(pstatus
), pline
);
1968 cupsdLogMessage(CUPSD_LOG_INFO
,
1969 "Hint: Run \"cupstestppd %s\" and fix any errors.",
1973 * Add a filter from application/vnd.cups-raw to printer/name to
1974 * handle "raw" printing by users.
1977 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1980 * Add a PostScript filter, since this is still possibly PS printer.
1983 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1988 * If we have an interface script, add a filter entry for it...
1991 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
1993 if (!access(filename
, X_OK
))
1996 * Yes, we have a System V style interface script; use it!
1999 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2000 "printer-make-and-model", NULL
, "Local System V Printer");
2002 snprintf(filename
, sizeof(filename
), "*/* 0 %s/interfaces/%s",
2003 ServerRoot
, p
->name
);
2004 add_printer_filter(p
, filename
);
2006 else if (p
->device_uri
&&
2007 !strncmp(p
->device_uri
, "ipp://", 6) &&
2008 (strstr(p
->device_uri
, "/printers/") != NULL
||
2009 strstr(p
->device_uri
, "/classes/") != NULL
))
2012 * Tell the client this is really a hard-wired remote printer.
2015 printer_type
|= CUPS_PRINTER_REMOTE
;
2018 * Point the printer-uri-supported attribute to the
2022 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2023 "printer-uri-supported", NULL
, p
->device_uri
);
2026 * Then set the make-and-model accordingly...
2029 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2030 "printer-make-and-model", NULL
, "Remote Printer");
2033 * Print all files directly...
2042 * Otherwise we have neither - treat this as a "dumb" printer
2043 * with no PPD file...
2046 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2047 "printer-make-and-model", NULL
, "Local Raw Printer");
2053 ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2054 "finishings-supported", num_finishings
, (int *)finishings
);
2055 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2056 "finishings-default", IPP_FINISHINGS_NONE
);
2061 * Copy the printer options into a browse attributes string we can re-use.
2064 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2066 const char *valptr
; /* Pointer into value */
2067 char *attrptr
; /* Pointer into attribute string */
2071 * Free the old browse attributes as needed...
2074 if (p
->browse_attrs
)
2075 free(p
->browse_attrs
);
2078 * Compute the length of all attributes + job-sheets, lease-duration,
2079 * and BrowseLocalOptions.
2082 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2086 length
+= strlen(option
->name
) + 2;
2090 for (valptr
= option
->value
; *valptr
; valptr
++)
2091 if (strchr(" \"\'\\", *valptr
))
2098 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2100 if (BrowseLocalOptions
)
2101 length
+= 12 + strlen(BrowseLocalOptions
);
2104 * Allocate the new string...
2107 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2108 cupsdLogMessage(CUPSD_LOG_ERROR
,
2109 "Unable to allocate %d bytes for browse data!",
2114 * Got the allocated string, now copy the options and attributes over...
2117 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2118 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2119 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2121 if (BrowseLocalOptions
)
2123 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2124 attrptr
+= strlen(attrptr
);
2127 for (i
= p
->num_options
, option
= p
->options
;
2132 strcpy(attrptr
, option
->name
);
2133 attrptr
+= strlen(attrptr
);
2139 for (valptr
= option
->value
; *valptr
; valptr
++)
2141 if (strchr(" \"\'\\", *valptr
))
2144 *attrptr
++ = *valptr
;
2154 * Populate the document-format-supported attribute...
2157 add_printer_formats(p
);
2159 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2163 * Add name-default attributes...
2166 add_printer_defaults(p
);
2170 * Write the IRIX printer config and status files...
2173 write_irix_config(p
);
2174 write_irix_state(p
);
2180 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2184 cupsdSetPrinterReasons(
2185 cupsd_printer_t
*p
, /* I - Printer */
2186 const char *s
) /* I - Reasons strings */
2188 int i
; /* Looping var */
2189 const char *sptr
; /* Pointer into reasons */
2190 char reason
[255], /* Reason string */
2191 *rptr
; /* Pointer into reason */
2194 if (s
[0] == '-' || s
[0] == '+')
2197 * Add/remove reasons...
2205 * Replace reasons...
2210 for (i
= 0; i
< p
->num_reasons
; i
++)
2211 free(p
->reasons
[i
]);
2217 * Loop through all of the reasons...
2223 * Skip leading whitespace and commas...
2226 while (isspace(*sptr
& 255) || *sptr
== ',')
2229 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2230 if (rptr
< (reason
+ sizeof(reason
) - 1))
2244 for (i
= 0; i
< p
->num_reasons
; i
++)
2245 if (!strcasecmp(reason
, p
->reasons
[i
]))
2248 * Found a match, so remove it...
2252 free(p
->reasons
[i
]);
2254 if (i
< p
->num_reasons
)
2255 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2256 (p
->num_reasons
- i
) * sizeof(char *));
2261 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2267 for (i
= 0; i
< p
->num_reasons
; i
++)
2268 if (!strcasecmp(reason
, p
->reasons
[i
]))
2271 if (i
>= p
->num_reasons
)
2273 p
->reasons
[i
] = strdup(reason
);
2282 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2286 cupsdSetPrinterState(
2287 cupsd_printer_t
*p
, /* I - Printer to change */
2288 ipp_pstate_t s
, /* I - New state */
2289 int update
) /* I - Update printers.conf? */
2291 ipp_pstate_t old_state
; /* Old printer state */
2295 * Can't set status of remote printers...
2298 if (p
->type
& CUPS_PRINTER_REMOTE
)
2302 * Set the new state...
2305 old_state
= p
->state
;
2310 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, p
, NULL
,
2311 "%s \"%s\" state changed.",
2312 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2316 * Let the browse code know this needs to be updated...
2320 p
->state_time
= time(NULL
);
2324 write_irix_state(p
);
2328 cupsdAddPrinterHistory(p
);
2331 * Save the printer configuration if a printer goes from idle or processing
2332 * to stopped (or visa-versa)...
2335 if ((old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
) &&
2338 if (p
->type
& CUPS_PRINTER_CLASS
)
2339 cupsdSaveAllClasses();
2341 cupsdSaveAllPrinters();
2347 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2351 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2352 int update
)/* I - Update printers.conf? */
2354 cupsd_job_t
*job
; /* Active print job */
2358 * Set the printer state...
2361 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2364 * See if we have a job printing on this printer...
2370 * Get pointer to job...
2373 job
= (cupsd_job_t
*)p
->job
;
2379 cupsdStopJob(job
, 0);
2382 * Reset the state to pending...
2385 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2386 job
->state_value
= IPP_JOB_PENDING
;
2394 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2398 cupsdUpdatePrinters(void)
2400 cupsd_printer_t
*p
; /* Current printer */
2404 * Loop through the printers and recreate the printer attributes
2405 * for any local printers since the policy and/or access control
2406 * stuff may have changed. Also, if browsing is disabled, remove
2407 * any remote printers...
2410 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2412 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2414 if (!Browsing
&& (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_REMOTE
)))
2416 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2417 cupsArrayRemove(ImplicitPrinters
, p
);
2419 cupsArraySave(Printers
);
2420 cupsdDeletePrinter(p
, 0);
2421 cupsArrayRestore(Printers
);
2424 else if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2425 cupsdSetPrinterAttrs(p
);
2428 * Update the operation policy pointer...
2431 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
2432 p
->op_policy_ptr
= DefaultPolicyPtr
;
2438 * 'cupsdValidateDest()' - Validate a printer/class destination.
2441 const char * /* O - Printer or class name */
2443 const char *hostname
, /* I - Host name */
2444 const char *resource
, /* I - Resource name */
2445 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
2446 cupsd_printer_t
**printer
) /* O - Printer pointer */
2448 cupsd_printer_t
*p
; /* Current printer */
2449 char localname
[1024],/* Localized hostname */
2450 *lptr
, /* Pointer into localized hostname */
2451 *sptr
; /* Pointer into server name */
2454 DEBUG_printf(("cupsdValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname
, resource
,
2458 * Initialize return values...
2464 *dtype
= (cups_ptype_t
)0;
2467 * See if the resource is a class or printer...
2470 if (!strncmp(resource
, "/classes/", 9))
2478 else if (!strncmp(resource
, "/printers/", 10))
2489 * Bad resource name...
2496 * See if the printer or class name exists...
2499 p
= cupsdFindDest(resource
);
2501 if (p
== NULL
&& strchr(resource
, '@') == NULL
)
2508 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2509 CUPS_PRINTER_REMOTE
);
2514 * Change localhost to the server name...
2517 if (!strcasecmp(hostname
, "localhost"))
2518 hostname
= ServerName
;
2520 strlcpy(localname
, hostname
, sizeof(localname
));
2522 if (!strcasecmp(hostname
, ServerName
))
2525 * Localize the hostname...
2528 lptr
= strchr(localname
, '.');
2529 sptr
= strchr(ServerName
, '.');
2531 if (sptr
!= NULL
&& lptr
!= NULL
)
2534 * Strip the common domain name components...
2537 while (lptr
!= NULL
)
2539 if (!strcasecmp(lptr
, sptr
))
2545 lptr
= strchr(lptr
+ 1, '.');
2550 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
2553 * Find a matching printer or class...
2556 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2558 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2559 if (!strcasecmp(p
->hostname
, localname
) &&
2560 !strcasecmp(p
->name
, resource
))
2565 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2566 CUPS_PRINTER_REMOTE
);
2575 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2580 cupsdWritePrintcap(void)
2582 cups_file_t
*fp
; /* printcap file */
2583 cupsd_printer_t
*p
; /* Current printer */
2588 * Update the IRIX printer state for the default printer; if
2589 * no printers remain, then the default printer file will be
2593 write_irix_state(DefaultPrinter
);
2597 * See if we have a printcap file; if not, don't bother writing it.
2600 if (!Printcap
|| !*Printcap
)
2604 * Open the printcap file...
2607 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
2611 * Put a comment header at the top so that users will know where the
2612 * data has come from...
2615 cupsFilePuts(fp
, "# This file was automatically generated by cupsd(8) from the\n");
2616 cupsFilePrintf(fp
, "# %s/printers.conf file. All changes to this file\n",
2618 cupsFilePuts(fp
, "# will be lost.\n");
2623 * Write a new printcap with the current list of printers.
2626 switch (PrintcapFormat
)
2630 * Each printer is put in the file as:
2640 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
2641 DefaultPrinter
->info
, ServerName
, DefaultPrinter
->name
);
2643 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2645 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2646 if (p
!= DefaultPrinter
)
2647 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
2648 ServerName
, p
->name
);
2651 case PRINTCAP_SOLARIS
:
2653 * Each printer is put in the file as:
2655 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2656 * _default:use=DefaultPrinter
2658 * :bsdaddr=ServerName,Printer1:\
2659 * :description=Description:
2661 * :bsdaddr=ServerName,Printer2:\
2662 * :description=Description:
2664 * :bsdaddr=ServerName,Printer3:\
2665 * :description=Description:
2668 * :bsdaddr=ServerName,PrinterN:\
2669 * :description=Description:
2672 cupsFilePuts(fp
, "_all:all=");
2673 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2675 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
2676 cupsFilePrintf(fp
, "%s%c", p
->name
,
2677 cupsArrayNext(Printers
) ? ',' : '\n');
2680 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
2682 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2684 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2685 cupsFilePrintf(fp
, "%s:\\\n"
2686 "\t:bsdaddr=%s,%s:\\\n"
2687 "\t:description=%s:\n",
2688 p
->name
, ServerName
, p
->name
, p
->info
? p
->info
: "");
2702 * 'cupsdSanitizeURI()' - Sanitize a device URI...
2705 char * /* O - New device URI */
2706 cupsdSanitizeURI(const char *uri
, /* I - Original device URI */
2707 char *buffer
, /* O - New device URI */
2708 int buflen
) /* I - Size of new device URI buffer */
2710 char *start
, /* Start of data after scheme */
2711 *slash
, /* First slash after scheme:// */
2712 *ptr
; /* Pointer into user@host:port part */
2716 * Range check input...
2719 if (!uri
|| !buffer
|| buflen
< 2)
2723 * Copy the device URI to the new buffer...
2726 strlcpy(buffer
, uri
, buflen
);
2729 * Find the end of the scheme:// part...
2732 if ((ptr
= strchr(buffer
, ':')) == NULL
)
2733 return (buffer
); /* No scheme: part... */
2735 for (start
= ptr
+ 1; *start
; start
++)
2740 * Find the next slash (/) in the URI...
2743 if ((slash
= strchr(start
, '/')) == NULL
)
2744 slash
= start
+ strlen(start
); /* No slash, point to the end */
2747 * Check for an @ sign before the slash...
2750 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
2753 * Found an @ sign and it is before the resource part, so we have
2754 * an authentication string. Copy the remaining URI over the
2755 * authentication string...
2758 _cups_strcpy(start
, ptr
+ 1);
2762 * Return the new device URI...
2770 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
2774 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
2776 int i
; /* Looping var */
2777 int num_options
; /* Number of default options */
2778 cups_option_t
*options
, /* Default options */
2779 *option
; /* Current option */
2780 char name
[256]; /* name-default */
2784 * Add all of the default options from the .conf files...
2787 for (num_options
= 0, i
= p
->num_options
, option
= p
->options
;
2791 if (strcmp(option
->name
, "ipp-options") &&
2792 strcmp(option
->name
, "job-sheets") &&
2793 strcmp(option
->name
, "lease-duration"))
2795 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
2796 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
2801 * Convert options to IPP attributes...
2804 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
2805 cupsFreeOptions(num_options
, options
);
2808 * Add standard -default attributes as needed...
2811 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
2812 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
2815 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
2816 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2817 "job-hold-until-default", NULL
, "no-hold");
2819 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
2820 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2821 "job-priority-default", 50);
2823 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
2824 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2825 "number-up-default", 1);
2827 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
2828 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2829 "orientation-requested-default", IPP_PORTRAIT
);
2834 * 'add_printer_filter()' - Add a MIME filter for a printer.
2839 cupsd_printer_t
*p
, /* I - Printer to add to */
2840 const char *filter
) /* I - Filter to add */
2842 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
2843 type
[MIME_MAX_TYPE
], /* Type for filter */
2844 program
[1024]; /* Program/filter name */
2845 int cost
; /* Cost of filter */
2846 mime_type_t
*temptype
; /* MIME type looping var */
2847 char filename
[1024]; /* Full filter filename */
2851 * Parse the filter string; it should be in the following format:
2853 * super/type cost program
2856 if (sscanf(filter
, "%15[^/]/%31s%d%1023s", super
, type
, &cost
, program
) != 4)
2858 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
2864 * See if the filter program exists; if not, stop the printer and flag
2868 if (strcmp(program
, "-"))
2870 if (program
[0] == '/')
2871 strlcpy(filename
, program
, sizeof(filename
));
2873 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
2875 if (access(filename
, X_OK
))
2877 snprintf(p
->state_message
, sizeof(p
->state_message
),
2878 "Filter \"%s\" for printer \"%s\" not available: %s",
2879 program
, p
->name
, strerror(errno
));
2880 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 0);
2881 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
2882 cupsdAddPrinterHistory(p
);
2884 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
2889 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
2890 * application/vnd.cups-command...
2893 if (!strcasecmp(super
, "application") &&
2894 !strcasecmp(type
, "vnd.cups-command"))
2895 p
->type
|= CUPS_PRINTER_COMMANDS
;
2898 * Add the filter to the MIME database, supporting wildcards as needed...
2901 for (temptype
= mimeFirstType(MimeDatabase
);
2903 temptype
= mimeNextType(MimeDatabase
))
2904 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
2905 !strcasecmp(temptype
->super
, super
)) &&
2906 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
2908 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2909 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
2910 p
->name
, temptype
->super
, temptype
->type
,
2911 p
->filetype
->super
, p
->filetype
->type
,
2913 mimeAddFilter(MimeDatabase
, temptype
, p
->filetype
, cost
, program
);
2919 * 'add_printer_formats()' - Add document-format-supported values for a printer.
2923 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
2925 int i
; /* Looping var */
2926 mime_type_t
*type
; /* Current MIME type */
2927 cups_array_t
*filters
; /* Filters */
2928 ipp_attribute_t
*attr
; /* document-format-supported attribute */
2929 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
2930 /* MIME type name */
2934 * Raw (and remote) queues advertise all of the supported MIME
2938 cupsArrayDelete(p
->filetypes
);
2939 p
->filetypes
= NULL
;
2943 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
2944 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
2945 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
2950 * Otherwise, loop through the supported MIME types and see if there
2951 * are filters for them...
2954 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
2955 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
2957 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
2959 for (type
= mimeFirstType(MimeDatabase
);
2961 type
= mimeNextType(MimeDatabase
))
2963 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
2965 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
2967 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2968 "add_printer_formats: %s: %s needs %d filters",
2969 p
->name
, mimetype
, cupsArrayCount(filters
));
2971 cupsArrayDelete(filters
);
2972 cupsArrayAdd(p
->filetypes
, type
);
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
, cupsArrayCount(p
->filetypes
) + 1);
2985 * Add the file formats that can be filtered...
2988 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
2989 !cupsArrayFind(p
->filetypes
, type
))
2994 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
2995 "document-format-supported",
2996 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
2999 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3001 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3003 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3005 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3007 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3013 * 'compare_printers()' - Compare two printers.
3016 static int /* O - Result of comparison */
3017 compare_printers(void *first
, /* I - First printer */
3018 void *second
, /* I - Second printer */
3019 void *data
) /* I - App data (not used) */
3021 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3022 ((cupsd_printer_t
*)second
)->name
));
3027 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3031 delete_printer_filters(
3032 cupsd_printer_t
*p
) /* I - Printer to remove from */
3034 mime_filter_t
*filter
; /* MIME filter looping var */
3038 * Range check input...
3045 * Remove all filters from the MIME database that have a destination
3046 * type == printer...
3049 for (filter
= mimeFirstFilter(MimeDatabase
);
3051 filter
= mimeNextFilter(MimeDatabase
))
3052 if (filter
->dst
== p
->filetype
)
3055 * Delete the current filter...
3058 mimeDeleteFilter(MimeDatabase
, filter
);
3065 * 'write_irix_config()' - Update the config files used by the IRIX
3070 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
3072 char filename
[1024]; /* Interface script filename */
3073 cups_file_t
*fp
; /* Interface script file */
3074 ipp_attribute_t
*attr
; /* Attribute data */
3078 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3079 * tools. First the interface script that tells the tools what kind of
3080 * printer we have...
3083 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
3085 if (p
->type
& CUPS_PRINTER_CLASS
)
3087 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3089 cupsFilePuts(fp
, "#!/bin/sh\n");
3091 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
3092 IPP_TAG_TEXT
)) != NULL
)
3093 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
3094 else if (p
->type
& CUPS_PRINTER_CLASS
)
3095 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
3097 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
3099 if (p
->type
& CUPS_PRINTER_COLOR
)
3100 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
3102 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
3104 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
3105 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
3109 chmod(filename
, 0755);
3110 chown(filename
, User
, Group
);
3114 * Then the member file that tells which device file the queue is connected
3115 * to... Networked printers use "/dev/null" in this file, so that's what
3116 * we use (the actual device URI can confuse some apps...)
3119 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
3121 if (p
->type
& CUPS_PRINTER_CLASS
)
3123 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3125 cupsFilePuts(fp
, "/dev/null\n");
3129 chmod(filename
, 0644);
3130 chown(filename
, User
, Group
);
3134 * The gui_interface file is a script or program that launches a GUI
3135 * option panel for the printer, using options specified on the
3136 * command-line in the third argument. The option panel must send
3137 * any printing options to stdout on a single line when the user
3138 * accepts them, or nothing if the user cancels the dialog.
3140 * The default options panel program is /usr/bin/glpoptions, from
3141 * the ESP Print Pro software. You can select another using the
3142 * PrintcapGUI option.
3145 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
3147 if (p
->type
& CUPS_PRINTER_CLASS
)
3149 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3151 cupsFilePuts(fp
, "#!/bin/sh\n");
3152 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
3156 chmod(filename
, 0755);
3157 chown(filename
, User
, Group
);
3161 * The POD config file is needed by the printstatus command to show
3162 * the printer location and device.
3165 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
3167 if (p
->type
& CUPS_PRINTER_CLASS
)
3169 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3171 cupsFilePrintf(fp
, "Printer Class | %s\n",
3172 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
3173 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
3174 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
3175 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
3176 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
? p
->device_uri
: "");
3177 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
3178 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
3179 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
3183 chmod(filename
, 0664);
3184 chown(filename
, User
, Group
);
3190 * 'write_irix_state()' - Update the status files used by IRIX printing
3195 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
3197 char filename
[1024]; /* Interface script filename */
3198 cups_file_t
*fp
; /* Interface script file */
3199 int tag
; /* Status tag value */
3205 * The POD status file is needed for the printstatus window to
3206 * provide the current status of the printer.
3209 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
3211 if (p
->type
& CUPS_PRINTER_CLASS
)
3213 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3215 cupsFilePrintf(fp
, "Operational Status | %s\n",
3216 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
3217 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
3219 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
3220 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
3221 p
->device_uri
? p
->device_uri
: "");
3222 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
3223 p
->accepting
? "Accepting" : "Not accepting");
3224 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
3228 chmod(filename
, 0664);
3229 chown(filename
, User
, Group
);
3233 * The activeicons file is needed to provide desktop icons for printers:
3235 * [ quoted from /usr/lib/print/tagit ]
3237 * --- Type of printer tags (base values)
3239 * Dumb=66048 # 0x10200
3240 * DumbColor=66080 # 0x10220
3241 * Raster=66112 # 0x10240
3242 * ColorRaster=66144 # 0x10260
3243 * Plotter=66176 # 0x10280
3244 * PostScript=66208 # 0x102A0
3245 * ColorPostScript=66240 # 0x102C0
3246 * MonoPostScript=66272 # 0x102E0
3248 * --- Printer state modifiers for local printers
3253 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3255 * --- Printer state modifiers for network printers
3259 * NetFaulted=10 # 0xA
3260 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3263 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
3265 if (p
->type
& CUPS_PRINTER_CLASS
)
3267 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3269 if (p
->type
& CUPS_PRINTER_COLOR
)
3274 if (p
->type
& CUPS_PRINTER_REMOTE
)
3277 if (p
->state
== IPP_PRINTER_PROCESSING
)
3280 else if (p
->state
== IPP_PRINTER_STOPPED
)
3283 cupsFilePuts(fp
, "#!/bin/sh\n");
3284 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
3288 chmod(filename
, 0755);
3289 chown(filename
, User
, Group
);
3294 * The default file is needed by the printers window to show
3295 * the default printer.
3298 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
3300 if (DefaultPrinter
!= NULL
)
3302 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3304 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
3308 chmod(filename
, 0644);
3309 chown(filename
, User
, Group
);
3319 * End of "$Id: printers.c 5828 2006-08-15 21:21:45Z mike $".