2 * "$Id: printers.c 5724 2006-07-12 19:42:35Z 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
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1500 if (auth
->type
== AUTH_BASIC
|| auth
->type
== AUTH_BASICDIGEST
)
1501 auth_supported
= "basic";
1502 else if (auth
->type
== AUTH_DIGEST
)
1503 auth_supported
= "digest";
1505 if (auth
->type
!= AUTH_NONE
)
1506 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1508 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1511 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1515 * Create the required IPP attributes for a printer...
1519 ippDelete(p
->attrs
);
1521 p
->attrs
= ippNew();
1523 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1524 "uri-authentication-supported", NULL
, auth_supported
);
1525 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1526 "uri-security-supported", NULL
, "none");
1527 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
1529 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1530 NULL
, p
->location
? p
->location
: "");
1531 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1532 NULL
, p
->info
? p
->info
: "");
1533 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
1539 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1540 "requesting-user-name-denied", p
->num_users
, NULL
,
1543 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1544 "requesting-user-name-allowed", p
->num_users
, NULL
,
1548 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1549 "job-quota-period", p
->quota_period
);
1550 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1551 "job-k-limit", p
->k_limit
);
1552 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1553 "job-page-limit", p
->page_limit
);
1555 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_REMOTE
))
1558 * Setup the job-sheets-default attribute...
1561 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1562 "job-sheets-default", 2, NULL
, NULL
);
1566 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
1567 Classification
: p
->job_sheets
[0]);
1568 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
1569 Classification
: p
->job_sheets
[1]);
1573 printer_type
= p
->type
;
1577 if (p
->type
& CUPS_PRINTER_REMOTE
)
1580 * Tell the client this is a remote printer of some type...
1583 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1584 "printer-uri-supported", NULL
, p
->uri
);
1587 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1588 "printer-make-and-model", NULL
, p
->make_model
);
1590 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1598 * Assign additional attributes depending on whether this is a printer
1602 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
1604 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
1609 * Add class-specific attributes...
1612 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
1613 p
->printers
[0]->make_model
)
1614 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1615 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
1617 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1618 "printer-make-and-model", NULL
, "Local Printer Class");
1620 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1621 "file:///dev/null");
1623 if (p
->num_printers
> 0)
1626 * Add a list of member URIs and names...
1629 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1630 "member-uris", p
->num_printers
, NULL
, NULL
);
1631 p
->type
|= CUPS_PRINTER_OPTIONS
;
1633 for (i
= 0; i
< p
->num_printers
; i
++)
1636 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->uri
);
1638 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
1641 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1642 "member-names", p
->num_printers
, NULL
, NULL
);
1646 for (i
= 0; i
< p
->num_printers
; i
++)
1647 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->name
);
1654 * Add printer-specific attributes... Start by sanitizing the device
1655 * URI so it doesn't have a username or password in it...
1659 strcpy(uri
, "file:/dev/null");
1660 else if (strstr(p
->device_uri
, "://") != NULL
)
1663 * http://..., ipp://..., etc.
1666 cupsdSanitizeURI(p
->device_uri
, uri
, sizeof(uri
));
1671 * file:..., serial:..., etc.
1674 strlcpy(uri
, p
->device_uri
, sizeof(uri
));
1677 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1681 * Assign additional attributes from the PPD file (if any)...
1684 p
->type
|= CUPS_PRINTER_BW
;
1685 finishings
[0] = IPP_FINISHINGS_NONE
;
1688 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
1691 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1694 * Add make/model and other various attributes...
1697 if (ppd
->color_device
)
1698 p
->type
|= CUPS_PRINTER_COLOR
;
1699 if (ppd
->variable_sizes
)
1700 p
->type
|= CUPS_PRINTER_VARIABLE
;
1701 if (!ppd
->manual_copies
)
1702 p
->type
|= CUPS_PRINTER_COPIES
;
1703 if ((ppdattr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
1704 if (ppdattr
->value
&& !strcasecmp(ppdattr
->value
, "true"))
1705 p
->type
|= CUPS_PRINTER_FAX
;
1707 ippAddBoolean(p
->attrs
, IPP_TAG_PRINTER
, "color-supported",
1709 if (ppd
->throughput
)
1710 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1711 "pages-per-minute", ppd
->throughput
);
1716 * The NickName can be localized in the character set specified
1717 * by the LanugageEncoding attribute. However, ppdOpen2() has
1718 * already converted the ppd->nickname member to UTF-8 for us
1719 * (the original attribute value is available separately)
1722 cupsdSetString(&p
->make_model
, ppd
->nickname
);
1724 else if (ppd
->modelname
)
1727 * Model name can only contain specific characters...
1730 cupsdSetString(&p
->make_model
, ppd
->modelname
);
1733 cupsdSetString(&p
->make_model
, "Bad PPD File");
1735 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1736 "printer-make-and-model", NULL
, p
->make_model
);
1739 * Add media options from the PPD file...
1742 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) != NULL
)
1743 num_media
= input_slot
->num_choices
;
1747 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
1748 num_media
+= media_type
->num_choices
;
1750 if ((page_size
= ppdFindOption(ppd
, "PageSize")) != NULL
)
1751 num_media
+= page_size
->num_choices
;
1753 if ((media_quality
= ppdFindOption(ppd
, "EFMediaQualityMode")) != NULL
)
1754 num_media
+= media_quality
->num_choices
;
1758 cupsdLogMessage(CUPSD_LOG_CRIT
,
1759 "The PPD file for printer %s contains no media "
1760 "options and is therefore invalid!", p
->name
);
1764 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1765 "media-supported", num_media
, NULL
, NULL
);
1770 if (input_slot
!= NULL
)
1771 for (i
= 0; i
< input_slot
->num_choices
; i
++, val
++)
1772 val
->string
.text
= _cupsStrAlloc(input_slot
->choices
[i
].choice
);
1774 if (media_type
!= NULL
)
1775 for (i
= 0; i
< media_type
->num_choices
; i
++, val
++)
1776 val
->string
.text
= _cupsStrAlloc(media_type
->choices
[i
].choice
);
1778 if (media_quality
!= NULL
)
1779 for (i
= 0; i
< media_quality
->num_choices
; i
++, val
++)
1780 val
->string
.text
= _cupsStrAlloc(media_quality
->choices
[i
].choice
);
1782 if (page_size
!= NULL
)
1784 for (i
= 0; i
< page_size
->num_choices
; i
++, val
++)
1785 val
->string
.text
= _cupsStrAlloc(page_size
->choices
[i
].choice
);
1787 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1788 "media-default", NULL
, page_size
->defchoice
);
1790 else if (input_slot
!= NULL
)
1791 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1792 "media-default", NULL
, input_slot
->defchoice
);
1793 else if (media_type
!= NULL
)
1794 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1795 "media-default", NULL
, media_type
->defchoice
);
1796 else if (media_quality
!= NULL
)
1797 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1798 "media-default", NULL
, media_quality
->defchoice
);
1800 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1801 "media-default", NULL
, "none");
1809 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
1811 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1812 "output-bin-supported", output_bin
->num_choices
,
1817 for (i
= 0, val
= attr
->values
;
1818 i
< output_bin
->num_choices
;
1820 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
1828 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1829 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1830 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1831 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
1832 duplex
= ppdFindOption(ppd
, "JCLDuplex");
1834 if (duplex
&& duplex
->num_choices
> 1)
1836 p
->type
|= CUPS_PRINTER_DUPLEX
;
1838 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1839 "sides-supported", 3, NULL
, sides
);
1841 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
1842 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1843 "sides-default", NULL
, "two-sided-short-edge");
1844 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
1845 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1846 "sides-default", NULL
, "two-sided-long-edge");
1848 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1849 "sides-default", NULL
, "one-sided");
1852 if (ppdFindOption(ppd
, "Collate") != NULL
)
1853 p
->type
|= CUPS_PRINTER_COLLATE
;
1855 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
1857 p
->type
|= CUPS_PRINTER_STAPLE
;
1858 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
1861 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
1863 p
->type
|= CUPS_PRINTER_BIND
;
1864 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
1867 for (i
= 0; i
< ppd
->num_sizes
; i
++)
1868 if (ppd
->sizes
[i
].length
> 1728)
1869 p
->type
|= CUPS_PRINTER_LARGE
;
1870 else if (ppd
->sizes
[i
].length
> 1008)
1871 p
->type
|= CUPS_PRINTER_MEDIUM
;
1873 p
->type
|= CUPS_PRINTER_SMALL
;
1876 * Add a filter from application/vnd.cups-raw to printer/name to
1877 * handle "raw" printing by users.
1880 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1883 * Add any filters in the PPD file...
1886 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
1887 for (i
= 0; i
< ppd
->num_filters
; i
++)
1889 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
1890 add_printer_filter(p
, ppd
->filters
[i
]);
1893 if (ppd
->num_filters
== 0)
1896 * If there are no filters, add a PostScript printing filter.
1899 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1903 * Show current and available port monitors for this printer...
1906 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "port-monitor",
1907 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
1910 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1912 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
1916 if (strstr(ppd
->protocols
, "TBCP"))
1918 else if (strstr(ppd
->protocols
, "BCP"))
1922 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1923 "port-monitor-supported", i
, NULL
, NULL
);
1925 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
1927 for (i
= 1, ppdattr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
1929 i
++, ppdattr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
1930 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppdattr
->value
);
1934 if (strstr(ppd
->protocols
, "TBCP"))
1935 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
1936 else if (strstr(ppd
->protocols
, "BCP"))
1937 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
1941 * Close the PPD and set the type...
1946 printer_type
= p
->type
;
1948 else if (!access(filename
, 0))
1950 int pline
; /* PPD line number */
1951 ppd_status_t pstatus
; /* PPD load status */
1954 pstatus
= ppdLastError(&pline
);
1956 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
1959 if (pstatus
<= PPD_ALLOC_ERROR
)
1960 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
1962 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
1963 ppdErrorString(pstatus
), pline
);
1965 cupsdLogMessage(CUPSD_LOG_INFO
,
1966 "Hint: Run \"cupstestppd %s\" and fix any errors.",
1970 * Add a filter from application/vnd.cups-raw to printer/name to
1971 * handle "raw" printing by users.
1974 add_printer_filter(p
, "application/vnd.cups-raw 0 -");
1977 * Add a PostScript filter, since this is still possibly PS printer.
1980 add_printer_filter(p
, "application/vnd.cups-postscript 0 -");
1985 * If we have an interface script, add a filter entry for it...
1988 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
1990 if (!access(filename
, X_OK
))
1993 * Yes, we have a System V style interface script; use it!
1996 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1997 "printer-make-and-model", NULL
, "Local System V Printer");
1999 snprintf(filename
, sizeof(filename
), "*/* 0 %s/interfaces/%s",
2000 ServerRoot
, p
->name
);
2001 add_printer_filter(p
, filename
);
2003 else if (p
->device_uri
&&
2004 !strncmp(p
->device_uri
, "ipp://", 6) &&
2005 (strstr(p
->device_uri
, "/printers/") != NULL
||
2006 strstr(p
->device_uri
, "/classes/") != NULL
))
2009 * Tell the client this is really a hard-wired remote printer.
2012 printer_type
|= CUPS_PRINTER_REMOTE
;
2015 * Point the printer-uri-supported attribute to the
2019 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2020 "printer-uri-supported", NULL
, p
->device_uri
);
2023 * Then set the make-and-model accordingly...
2026 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2027 "printer-make-and-model", NULL
, "Remote Printer");
2030 * Print all files directly...
2038 * Otherwise we have neither - treat this as a "dumb" printer
2039 * with no PPD file...
2042 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2043 "printer-make-and-model", NULL
, "Local Raw Printer");
2049 ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2050 "finishings-supported", num_finishings
, (int *)finishings
);
2051 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2052 "finishings-default", IPP_FINISHINGS_NONE
);
2057 * Copy the printer options into a browse attributes string we can re-use.
2060 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2062 const char *valptr
; /* Pointer into value */
2063 char *attrptr
; /* Pointer into attribute string */
2067 * Free the old browse attributes as needed...
2070 if (p
->browse_attrs
)
2071 free(p
->browse_attrs
);
2074 * Compute the length of all attributes + job-sheets, lease-duration,
2075 * and BrowseLocalOptions.
2078 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2082 length
+= strlen(option
->name
) + 2;
2086 for (valptr
= option
->value
; *valptr
; valptr
++)
2087 if (strchr(" \"\'\\", *valptr
))
2094 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2096 if (BrowseLocalOptions
)
2097 length
+= 12 + strlen(BrowseLocalOptions
);
2100 * Allocate the new string...
2103 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2104 cupsdLogMessage(CUPSD_LOG_ERROR
,
2105 "Unable to allocate %d bytes for browse data!",
2110 * Got the allocated string, now copy the options and attributes over...
2113 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2114 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2115 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2117 if (BrowseLocalOptions
)
2119 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2120 attrptr
+= strlen(attrptr
);
2123 for (i
= p
->num_options
, option
= p
->options
;
2128 strcpy(attrptr
, option
->name
);
2129 attrptr
+= strlen(attrptr
);
2135 for (valptr
= option
->value
; *valptr
; valptr
++)
2137 if (strchr(" \"\'\\", *valptr
))
2140 *attrptr
++ = *valptr
;
2150 * Populate the document-format-supported attribute...
2153 add_printer_formats(p
);
2155 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2159 * Add name-default attributes...
2162 add_printer_defaults(p
);
2166 * Write the IRIX printer config and status files...
2169 write_irix_config(p
);
2170 write_irix_state(p
);
2176 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2180 cupsdSetPrinterReasons(
2181 cupsd_printer_t
*p
, /* I - Printer */
2182 const char *s
) /* I - Reasons strings */
2184 int i
; /* Looping var */
2185 const char *sptr
; /* Pointer into reasons */
2186 char reason
[255], /* Reason string */
2187 *rptr
; /* Pointer into reason */
2190 if (s
[0] == '-' || s
[0] == '+')
2193 * Add/remove reasons...
2201 * Replace reasons...
2206 for (i
= 0; i
< p
->num_reasons
; i
++)
2207 free(p
->reasons
[i
]);
2213 * Loop through all of the reasons...
2219 * Skip leading whitespace and commas...
2222 while (isspace(*sptr
& 255) || *sptr
== ',')
2225 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2226 if (rptr
< (reason
+ sizeof(reason
) - 1))
2240 for (i
= 0; i
< p
->num_reasons
; i
++)
2241 if (!strcasecmp(reason
, p
->reasons
[i
]))
2244 * Found a match, so remove it...
2248 free(p
->reasons
[i
]);
2250 if (i
< p
->num_reasons
)
2251 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2252 (p
->num_reasons
- i
) * sizeof(char *));
2257 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2263 for (i
= 0; i
< p
->num_reasons
; i
++)
2264 if (!strcasecmp(reason
, p
->reasons
[i
]))
2267 if (i
>= p
->num_reasons
)
2269 p
->reasons
[i
] = strdup(reason
);
2278 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2282 cupsdSetPrinterState(
2283 cupsd_printer_t
*p
, /* I - Printer to change */
2284 ipp_pstate_t s
, /* I - New state */
2285 int update
) /* I - Update printers.conf? */
2287 ipp_pstate_t old_state
; /* Old printer state */
2291 * Can't set status of remote printers...
2294 if (p
->type
& CUPS_PRINTER_REMOTE
)
2298 * Set the new state...
2301 old_state
= p
->state
;
2306 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED
, p
, NULL
,
2307 "%s \"%s\" state changed.",
2308 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2312 * Let the browse code know this needs to be updated...
2316 p
->state_time
= time(NULL
);
2320 write_irix_state(p
);
2324 cupsdAddPrinterHistory(p
);
2327 * Save the printer configuration if a printer goes from idle or processing
2328 * to stopped (or visa-versa)...
2331 if ((old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
) &&
2334 if (p
->type
& CUPS_PRINTER_CLASS
)
2335 cupsdSaveAllClasses();
2337 cupsdSaveAllPrinters();
2343 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2347 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2348 int update
)/* I - Update printers.conf? */
2350 cupsd_job_t
*job
; /* Active print job */
2354 * Set the printer state...
2357 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2360 * See if we have a job printing on this printer...
2366 * Get pointer to job...
2369 job
= (cupsd_job_t
*)p
->job
;
2375 cupsdStopJob(job
, 0);
2378 * Reset the state to pending...
2381 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2382 job
->state_value
= IPP_JOB_PENDING
;
2390 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2394 cupsdUpdatePrinters(void)
2396 cupsd_printer_t
*p
; /* Current printer */
2400 * Loop through the printers and recreate the printer attributes
2401 * for any local printers since the policy and/or access control
2402 * stuff may have changed. Also, if browsing is disabled, remove
2403 * any remote printers...
2406 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2408 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2410 if (!Browsing
&& (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_REMOTE
)))
2412 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2413 cupsArrayRemove(ImplicitPrinters
, p
);
2415 cupsArraySave(Printers
);
2416 cupsdDeletePrinter(p
, 0);
2417 cupsArrayRestore(Printers
);
2420 else if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2421 cupsdSetPrinterAttrs(p
);
2424 * Update the operation policy pointer...
2427 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
2428 p
->op_policy_ptr
= DefaultPolicyPtr
;
2434 * 'cupsdValidateDest()' - Validate a printer/class destination.
2437 const char * /* O - Printer or class name */
2439 const char *hostname
, /* I - Host name */
2440 const char *resource
, /* I - Resource name */
2441 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
2442 cupsd_printer_t
**printer
) /* O - Printer pointer */
2444 cupsd_printer_t
*p
; /* Current printer */
2445 char localname
[1024],/* Localized hostname */
2446 *lptr
, /* Pointer into localized hostname */
2447 *sptr
; /* Pointer into server name */
2450 DEBUG_printf(("cupsdValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname
, resource
,
2454 * Initialize return values...
2460 *dtype
= (cups_ptype_t
)0;
2463 * See if the resource is a class or printer...
2466 if (!strncmp(resource
, "/classes/", 9))
2474 else if (!strncmp(resource
, "/printers/", 10))
2485 * Bad resource name...
2492 * See if the printer or class name exists...
2495 p
= cupsdFindDest(resource
);
2497 if (p
== NULL
&& strchr(resource
, '@') == NULL
)
2504 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2505 CUPS_PRINTER_REMOTE
);
2510 * Change localhost to the server name...
2513 if (!strcasecmp(hostname
, "localhost"))
2514 hostname
= ServerName
;
2516 strlcpy(localname
, hostname
, sizeof(localname
));
2518 if (!strcasecmp(hostname
, ServerName
))
2521 * Localize the hostname...
2524 lptr
= strchr(localname
, '.');
2525 sptr
= strchr(ServerName
, '.');
2527 if (sptr
!= NULL
&& lptr
!= NULL
)
2530 * Strip the common domain name components...
2533 while (lptr
!= NULL
)
2535 if (!strcasecmp(lptr
, sptr
))
2541 lptr
= strchr(lptr
+ 1, '.');
2546 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
2549 * Find a matching printer or class...
2552 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2554 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2555 if (!strcasecmp(p
->hostname
, localname
) &&
2556 !strcasecmp(p
->name
, resource
))
2561 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2562 CUPS_PRINTER_REMOTE
);
2571 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2576 cupsdWritePrintcap(void)
2578 cups_file_t
*fp
; /* printcap file */
2579 cupsd_printer_t
*p
; /* Current printer */
2584 * Update the IRIX printer state for the default printer; if
2585 * no printers remain, then the default printer file will be
2589 write_irix_state(DefaultPrinter
);
2593 * See if we have a printcap file; if not, don't bother writing it.
2596 if (!Printcap
|| !*Printcap
)
2600 * Open the printcap file...
2603 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
2607 * Put a comment header at the top so that users will know where the
2608 * data has come from...
2611 cupsFilePuts(fp
, "# This file was automatically generated by cupsd(8) from the\n");
2612 cupsFilePrintf(fp
, "# %s/printers.conf file. All changes to this file\n",
2614 cupsFilePuts(fp
, "# will be lost.\n");
2619 * Write a new printcap with the current list of printers.
2622 switch (PrintcapFormat
)
2626 * Each printer is put in the file as:
2636 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
2637 DefaultPrinter
->info
, ServerName
, DefaultPrinter
->name
);
2639 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2641 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2642 if (p
!= DefaultPrinter
)
2643 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
2644 ServerName
, p
->name
);
2647 case PRINTCAP_SOLARIS
:
2649 * Each printer is put in the file as:
2651 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2652 * _default:use=DefaultPrinter
2654 * :bsdaddr=ServerName,Printer1:\
2655 * :description=Description:
2657 * :bsdaddr=ServerName,Printer2:\
2658 * :description=Description:
2660 * :bsdaddr=ServerName,Printer3:\
2661 * :description=Description:
2664 * :bsdaddr=ServerName,PrinterN:\
2665 * :description=Description:
2668 cupsFilePuts(fp
, "_all:all=");
2669 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2671 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
2672 cupsFilePrintf(fp
, "%s%c", p
->name
,
2673 cupsArrayNext(Printers
) ? ',' : '\n');
2676 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
2678 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2680 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2681 cupsFilePrintf(fp
, "%s:\\\n"
2682 "\t:bsdaddr=%s,%s:\\\n"
2683 "\t:description=%s:\n",
2684 p
->name
, ServerName
, p
->name
, p
->info
? p
->info
: "");
2698 * 'cupsdSanitizeURI()' - Sanitize a device URI...
2701 char * /* O - New device URI */
2702 cupsdSanitizeURI(const char *uri
, /* I - Original device URI */
2703 char *buffer
, /* O - New device URI */
2704 int buflen
) /* I - Size of new device URI buffer */
2706 char *start
, /* Start of data after scheme */
2707 *slash
, /* First slash after scheme:// */
2708 *ptr
; /* Pointer into user@host:port part */
2712 * Range check input...
2715 if (!uri
|| !buffer
|| buflen
< 2)
2719 * Copy the device URI to the new buffer...
2722 strlcpy(buffer
, uri
, buflen
);
2725 * Find the end of the scheme:// part...
2728 if ((ptr
= strchr(buffer
, ':')) == NULL
)
2729 return (buffer
); /* No scheme: part... */
2731 for (start
= ptr
+ 1; *start
; start
++)
2736 * Find the next slash (/) in the URI...
2739 if ((slash
= strchr(start
, '/')) == NULL
)
2740 slash
= start
+ strlen(start
); /* No slash, point to the end */
2743 * Check for an @ sign before the slash...
2746 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
2749 * Found an @ sign and it is before the resource part, so we have
2750 * an authentication string. Copy the remaining URI over the
2751 * authentication string...
2754 _cups_strcpy(start
, ptr
+ 1);
2758 * Return the new device URI...
2766 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
2770 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
2772 int i
; /* Looping var */
2773 int num_options
; /* Number of default options */
2774 cups_option_t
*options
, /* Default options */
2775 *option
; /* Current option */
2776 char name
[256]; /* name-default */
2780 * Add all of the default options from the .conf files...
2783 for (num_options
= 0, i
= p
->num_options
, option
= p
->options
;
2787 if (strcmp(option
->name
, "ipp-options") &&
2788 strcmp(option
->name
, "job-sheets") &&
2789 strcmp(option
->name
, "lease-duration"))
2791 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
2792 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
2797 * Convert options to IPP attributes...
2800 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
2801 cupsFreeOptions(num_options
, options
);
2804 * Add standard -default attributes as needed...
2807 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
2808 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
2811 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
2812 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2813 "job-hold-until-default", NULL
, "no-hold");
2815 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
2816 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2817 "job-priority-default", 50);
2819 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
2820 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2821 "number-up-default", 1);
2823 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
2824 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
2825 "orientation-requested-default", IPP_PORTRAIT
);
2830 * 'add_printer_filter()' - Add a MIME filter for a printer.
2835 cupsd_printer_t
*p
, /* I - Printer to add to */
2836 const char *filter
) /* I - Filter to add */
2838 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
2839 type
[MIME_MAX_TYPE
], /* Type for filter */
2840 program
[1024]; /* Program/filter name */
2841 int cost
; /* Cost of filter */
2842 mime_type_t
*temptype
; /* MIME type looping var */
2843 char filename
[1024]; /* Full filter filename */
2847 * Parse the filter string; it should be in the following format:
2849 * super/type cost program
2852 if (sscanf(filter
, "%15[^/]/%31s%d%1023s", super
, type
, &cost
, program
) != 4)
2854 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
2860 * See if the filter program exists; if not, stop the printer and flag
2864 if (strcmp(program
, "-"))
2866 if (program
[0] == '/')
2867 strlcpy(filename
, program
, sizeof(filename
));
2869 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
2871 if (access(filename
, X_OK
))
2873 snprintf(p
->state_message
, sizeof(p
->state_message
),
2874 "Filter \"%s\" for printer \"%s\" not available: %s",
2875 program
, p
->name
, strerror(errno
));
2876 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 0);
2877 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
2878 cupsdAddPrinterHistory(p
);
2880 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
2885 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
2886 * application/vnd.cups-command...
2889 if (!strcasecmp(super
, "application") &&
2890 !strcasecmp(type
, "vnd.cups-command"))
2891 p
->type
|= CUPS_PRINTER_COMMANDS
;
2894 * Add the filter to the MIME database, supporting wildcards as needed...
2897 for (temptype
= mimeFirstType(MimeDatabase
);
2899 temptype
= mimeNextType(MimeDatabase
))
2900 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
2901 !strcasecmp(temptype
->super
, super
)) &&
2902 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
2904 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2905 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
2906 p
->name
, temptype
->super
, temptype
->type
,
2907 p
->filetype
->super
, p
->filetype
->type
,
2909 mimeAddFilter(MimeDatabase
, temptype
, p
->filetype
, cost
, program
);
2915 * 'add_printer_formats()' - Add document-format-supported values for a printer.
2919 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
2921 int i
; /* Looping var */
2922 mime_type_t
*type
; /* Current MIME type */
2923 cups_array_t
*filters
; /* Filters */
2924 ipp_attribute_t
*attr
; /* document-format-supported attribute */
2925 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
2926 /* MIME type name */
2930 * Raw (and remote) queues advertise all of the supported MIME
2934 cupsArrayDelete(p
->filetypes
);
2935 p
->filetypes
= NULL
;
2939 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
2940 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
2941 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
2946 * Otherwise, loop through the supported MIME types and see if there
2947 * are filters for them...
2950 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
2951 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
2953 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
2955 for (type
= mimeFirstType(MimeDatabase
);
2957 type
= mimeNextType(MimeDatabase
))
2959 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
2961 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
2963 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2964 "add_printer_formats: %s: %s needs %d filters",
2965 p
->name
, mimetype
, cupsArrayCount(filters
));
2967 cupsArrayDelete(filters
);
2968 cupsArrayAdd(p
->filetypes
, type
);
2971 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2972 "add_printer_formats: %s: %s not supported",
2976 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2977 "add_printer_formats: %s: %d supported types",
2978 p
->name
, cupsArrayCount(p
->filetypes
) + 1);
2981 * Add the file formats that can be filtered...
2984 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
2985 !cupsArrayFind(p
->filetypes
, type
))
2990 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
2991 "document-format-supported",
2992 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
2995 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
2997 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
2999 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3001 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3003 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3009 * 'compare_printers()' - Compare two printers.
3012 static int /* O - Result of comparison */
3013 compare_printers(void *first
, /* I - First printer */
3014 void *second
, /* I - Second printer */
3015 void *data
) /* I - App data (not used) */
3017 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3018 ((cupsd_printer_t
*)second
)->name
));
3023 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3027 delete_printer_filters(
3028 cupsd_printer_t
*p
) /* I - Printer to remove from */
3030 mime_filter_t
*filter
; /* MIME filter looping var */
3034 * Range check input...
3041 * Remove all filters from the MIME database that have a destination
3042 * type == printer...
3045 for (filter
= mimeFirstFilter(MimeDatabase
);
3047 filter
= mimeNextFilter(MimeDatabase
))
3048 if (filter
->dst
== p
->filetype
)
3051 * Delete the current filter...
3054 mimeDeleteFilter(MimeDatabase
, filter
);
3061 * 'write_irix_config()' - Update the config files used by the IRIX
3066 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
3068 char filename
[1024]; /* Interface script filename */
3069 cups_file_t
*fp
; /* Interface script file */
3070 ipp_attribute_t
*attr
; /* Attribute data */
3074 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3075 * tools. First the interface script that tells the tools what kind of
3076 * printer we have...
3079 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
3081 if (p
->type
& CUPS_PRINTER_CLASS
)
3083 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3085 cupsFilePuts(fp
, "#!/bin/sh\n");
3087 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
3088 IPP_TAG_TEXT
)) != NULL
)
3089 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
3090 else if (p
->type
& CUPS_PRINTER_CLASS
)
3091 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
3093 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
3095 if (p
->type
& CUPS_PRINTER_COLOR
)
3096 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
3098 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
3100 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
3101 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
3105 chmod(filename
, 0755);
3106 chown(filename
, User
, Group
);
3110 * Then the member file that tells which device file the queue is connected
3111 * to... Networked printers use "/dev/null" in this file, so that's what
3112 * we use (the actual device URI can confuse some apps...)
3115 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
3117 if (p
->type
& CUPS_PRINTER_CLASS
)
3119 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3121 cupsFilePuts(fp
, "/dev/null\n");
3125 chmod(filename
, 0644);
3126 chown(filename
, User
, Group
);
3130 * The gui_interface file is a script or program that launches a GUI
3131 * option panel for the printer, using options specified on the
3132 * command-line in the third argument. The option panel must send
3133 * any printing options to stdout on a single line when the user
3134 * accepts them, or nothing if the user cancels the dialog.
3136 * The default options panel program is /usr/bin/glpoptions, from
3137 * the ESP Print Pro software. You can select another using the
3138 * PrintcapGUI option.
3141 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
3143 if (p
->type
& CUPS_PRINTER_CLASS
)
3145 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3147 cupsFilePuts(fp
, "#!/bin/sh\n");
3148 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
3152 chmod(filename
, 0755);
3153 chown(filename
, User
, Group
);
3157 * The POD config file is needed by the printstatus command to show
3158 * the printer location and device.
3161 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
3163 if (p
->type
& CUPS_PRINTER_CLASS
)
3165 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3167 cupsFilePrintf(fp
, "Printer Class | %s\n",
3168 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
3169 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
3170 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
3171 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
3172 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
? p
->device_uri
: "");
3173 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
3174 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
3175 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
3179 chmod(filename
, 0664);
3180 chown(filename
, User
, Group
);
3186 * 'write_irix_state()' - Update the status files used by IRIX printing
3191 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
3193 char filename
[1024]; /* Interface script filename */
3194 cups_file_t
*fp
; /* Interface script file */
3195 int tag
; /* Status tag value */
3201 * The POD status file is needed for the printstatus window to
3202 * provide the current status of the printer.
3205 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
3207 if (p
->type
& CUPS_PRINTER_CLASS
)
3209 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3211 cupsFilePrintf(fp
, "Operational Status | %s\n",
3212 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
3213 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
3215 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
3216 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
3217 p
->device_uri
? p
->device_uri
: "");
3218 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
3219 p
->accepting
? "Accepting" : "Not accepting");
3220 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
3224 chmod(filename
, 0664);
3225 chown(filename
, User
, Group
);
3229 * The activeicons file is needed to provide desktop icons for printers:
3231 * [ quoted from /usr/lib/print/tagit ]
3233 * --- Type of printer tags (base values)
3235 * Dumb=66048 # 0x10200
3236 * DumbColor=66080 # 0x10220
3237 * Raster=66112 # 0x10240
3238 * ColorRaster=66144 # 0x10260
3239 * Plotter=66176 # 0x10280
3240 * PostScript=66208 # 0x102A0
3241 * ColorPostScript=66240 # 0x102C0
3242 * MonoPostScript=66272 # 0x102E0
3244 * --- Printer state modifiers for local printers
3249 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3251 * --- Printer state modifiers for network printers
3255 * NetFaulted=10 # 0xA
3256 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3259 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
3261 if (p
->type
& CUPS_PRINTER_CLASS
)
3263 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3265 if (p
->type
& CUPS_PRINTER_COLOR
)
3270 if (p
->type
& CUPS_PRINTER_REMOTE
)
3273 if (p
->state
== IPP_PRINTER_PROCESSING
)
3276 else if (p
->state
== IPP_PRINTER_STOPPED
)
3279 cupsFilePuts(fp
, "#!/bin/sh\n");
3280 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
3284 chmod(filename
, 0755);
3285 chown(filename
, User
, Group
);
3290 * The default file is needed by the printers window to show
3291 * the default printer.
3294 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
3296 if (DefaultPrinter
!= NULL
)
3298 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
3300 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
3304 chmod(filename
, 0644);
3305 chown(filename
, User
, Group
);
3315 * End of "$Id: printers.c 5724 2006-07-12 19:42:35Z mike $".