2 * "$Id: printers.c,v 1.93.2.67 2004/07/02 22:15:51 mike Exp $"
4 * Printer routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2004 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-3142 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * AddPrinter() - Add a printer to the system.
27 * AddPrinterFilter() - Add a MIME filter for a printer.
28 * AddPrinterHistory() - Add the current printer state to the history.
29 * AddPrinterUser() - Add a user to the ACL.
30 * DeleteAllPrinters() - Delete all printers from the system.
31 * DeletePrinter() - Delete a printer from the system.
32 * DeletePrinterFilters() - Delete all MIME filters for a printer.
33 * FindPrinter() - Find a printer in the list.
34 * FreePrinterUsers() - Free allow/deny users.
35 * LoadAllPrinters() - Load printers from the printers.conf file.
36 * SaveAllPrinters() - Save all printer definitions to the printers.conf
37 * SetPrinterAttrs() - Set printer attributes based upon the PPD file.
38 * SetPrinterReasons() - Set/update the reasons strings.
39 * SetPrinterState() - Update the current state of a printer.
40 * SortPrinters() - Sort the printer list when a printer name is
42 * StopPrinter() - Stop a printer from printing any jobs...
43 * ValidateDest() - Validate a printer/class destination.
44 * WritePrintcap() - Write a pseudo-printcap file for older
45 * applications that need it...
46 * write_irix_config() - Update the config files used by the IRIX
48 * write_irix_state() - Update the status files used by IRIX printing
53 * Include necessary headers...
64 static void write_irix_state(printer_t
*p
);
69 * 'AddPrinter()' - Add a printer to the system.
72 printer_t
* /* O - New printer */
73 AddPrinter(const char *name
) /* I - Name of printer */
75 printer_t
*p
, /* New printer */
76 *current
, /* Current printer in list */
77 *prev
; /* Previous printer in list */
81 * Range check input...
84 LogMessage(L_DEBUG2
, "AddPrinter(\"%s\")", name
? name
: "(null)");
90 * Create a new printer entity...
93 if ((p
= calloc(1, sizeof(printer_t
))) == NULL
)
95 LogMessage(L_ERROR
, "Unable to allocate memory for printer - %s",
100 SetString(&p
->name
, name
);
101 SetString(&p
->info
, name
);
102 SetString(&p
->hostname
, ServerName
);
104 SetStringf(&p
->uri
, "ipp://%s:%d/printers/%s", ServerName
, LocalPort
, name
);
105 SetStringf(&p
->device_uri
, "file:/dev/null");
107 p
->state
= IPP_PRINTER_STOPPED
;
109 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
111 SetString(&p
->job_sheets
[0], "none");
112 SetString(&p
->job_sheets
[1], "none");
114 SetString(&p
->error_policy
, "stop-printer");
115 SetString(&p
->op_policy
, DefaultPolicy
);
117 p
->op_policy_ptr
= DefaultPolicyPtr
;
119 if (MaxPrinterHistory
)
120 p
->history
= calloc(MaxPrinterHistory
, sizeof(ipp_t
*));
123 * Insert the printer in the printer list alphabetically...
126 for (prev
= NULL
, current
= Printers
;
128 prev
= current
, current
= current
->next
)
129 if (strcasecmp(p
->name
, current
->name
) < 0)
133 * Insert this printer before the current one...
144 * Write a new /etc/printcap or /var/spool/lp/pstatus file.
150 * Bump the printer count and return...
160 * 'AddPrinterFilter()' - Add a MIME filter for a printer.
164 AddPrinterFilter(printer_t
*p
, /* I - Printer to add to */
165 const char *filter
) /* I - Filter to add */
167 int i
; /* Looping var */
168 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
169 type
[MIME_MAX_TYPE
], /* Type for filter */
170 program
[1024]; /* Program/filter name */
171 int cost
; /* Cost of filter */
172 mime_type_t
**temptype
; /* MIME type looping var */
176 * Range check input...
179 if (p
== NULL
|| p
->filetype
== NULL
|| filter
== NULL
)
183 * Parse the filter string; it should be in the following format:
185 * super/type cost program
188 if (sscanf(filter
, "%15[^/]/%31s%d%1023s", super
, type
, &cost
, program
) != 4)
190 LogMessage(L_ERROR
, "AddPrinterFilter: Invalid filter string \"%s\"!",
196 * Add the filter to the MIME database, supporting wildcards as needed...
199 for (temptype
= MimeDatabase
->types
, i
= MimeDatabase
->num_types
;
202 if (((super
[0] == '*' && strcasecmp((*temptype
)->super
, "printer") != 0) ||
203 strcasecmp((*temptype
)->super
, super
) == 0) &&
204 (type
[0] == '*' || strcasecmp((*temptype
)->type
, type
) == 0))
206 LogMessage(L_DEBUG2
, "Adding filter %s/%s %s/%s %d %s",
207 (*temptype
)->super
, (*temptype
)->type
,
208 p
->filetype
->super
, p
->filetype
->type
,
210 mimeAddFilter(MimeDatabase
, *temptype
, p
->filetype
, cost
, program
);
216 * 'AddPrinterHistory()' - Add the current printer state to the history.
220 AddPrinterHistory(printer_t
*p
) /* I - Printer */
222 ipp_t
*history
; /* History collection */
226 * Stop early if we aren't keeping history data...
229 if (MaxPrinterHistory
<= 0)
233 * Retire old history data as needed...
236 p
->sequence_number
++;
238 if (p
->num_history
>= MaxPrinterHistory
)
241 ippDelete(p
->history
[0]);
242 memmove(p
->history
, p
->history
+ 1, p
->num_history
* sizeof(ipp_t
*));
246 * Create a collection containing the current printer-state, printer-up-time,
247 * printer-state-message, and printer-state-reasons attributes.
251 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
253 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
255 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message",
256 NULL
, p
->state_message
);
257 if (p
->num_reasons
== 0)
258 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
259 "printer-state-reasons", NULL
,
260 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
262 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
263 "printer-state-reasons", p
->num_reasons
, NULL
,
264 (const char * const *)p
->reasons
);
265 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
266 "printer-state-time", p
->state_time
);
267 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
268 "printer-state-sequence-number", p
->sequence_number
);
270 p
->history
[p
->num_history
] = history
;
276 * 'AddPrinterUser()' - Add a user to the ACL.
280 AddPrinterUser(printer_t
*p
, /* I - Printer */
281 const char *username
) /* I - User */
283 const char **temp
; /* Temporary array pointer */
289 if (p
->num_users
== 0)
290 temp
= malloc(sizeof(char **));
292 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
298 temp
+= p
->num_users
;
300 if ((*temp
= strdup(username
)) != NULL
)
306 * 'CreateCommonData()' - Create the common printer data.
310 CreateCommonData(void)
312 int i
; /* Looping var */
313 ipp_attribute_t
*attr
; /* Attribute data */
314 printer_t
*p
; /* Current printer */
315 static const int nups
[] = /* number-up-supported values */
316 { 1, 2, 4, 6, 9, 16 };
317 static const ipp_orient_t orients
[4] =/* orientation-requested-supported values */
321 IPP_REVERSE_LANDSCAPE
,
324 static const char * const holds
[] = /* job-hold-until-supported values */
335 static const char * const versions
[] =/* ipp-versions-supported values */
340 static const ipp_op_t ops
[] = /* operations-supported values */
347 IPP_GET_JOB_ATTRIBUTES
,
349 IPP_GET_PRINTER_ATTRIBUTES
,
355 IPP_SET_JOB_ATTRIBUTES
,
371 static const char * const charsets
[] =/* charset-supported values */
401 static const char * const compressions
[] =
402 { /* document-compression-supported values */
406 #endif /* HAVE_LIBZ */
408 static const char * const multiple_document_handling
[] =
409 { /* multiple-document-handling-supported values */
410 "separate-documents-uncollated-copies",
411 "separate-documents-collated-copies"
413 static const char * const errors
[] = /* printer-error-policy-supported values */
422 ippDelete(CommonData
);
424 CommonData
= ippNew();
426 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
427 "pdl-override-supported", NULL
, "not-attempted");
428 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
429 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
431 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
432 "operations-supported",
433 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, (int *)ops
);
434 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
435 "multiple-document-jobs-supported", 1);
436 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
437 "multiple-operation-time-out", 60);
438 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
439 "multiple-document-handling-supported",
440 sizeof(multiple_document_handling
) /
441 sizeof(multiple_document_handling
[0]), NULL
,
442 multiple_document_handling
);
443 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
,
444 "charset-configured", NULL
, DefaultCharset
);
445 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
,
446 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
448 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
449 "natural-language-configured", NULL
, DefaultLanguage
);
450 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
451 "generated-natural-language-supported", NULL
, DefaultLanguage
);
452 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
453 "document-format-default", NULL
, "application/octet-stream");
454 ippAddStrings(CommonData
, IPP_TAG_PRINTER
,
455 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
456 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
457 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
458 "compression-supported",
459 sizeof(compressions
) / sizeof(compressions
[0]),
461 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
462 "job-priority-supported", 100);
463 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
464 "job-priority-default", 50);
465 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
466 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
467 "copies-default", 1);
468 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
469 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
470 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
471 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
472 "number-up-default", 1);
473 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
474 "orientation-requested-supported", 4, (int *)orients
);
475 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
476 "orientation-requested-default", IPP_PORTRAIT
);
477 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
478 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
480 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
481 "job-hold-until-default", NULL
, "no-hold");
482 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
483 "printer-op-policy-supported", NumPolicies
, NULL
, NULL
);
484 for (i
= 0; i
< NumPolicies
; i
++)
485 attr
->values
[i
].string
.text
= strdup(Policies
[i
]->name
);
486 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
487 "printer-error-policy-supported",
488 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
493 * Setup the job-sheets-supported and job-sheets-default attributes...
496 if (Classification
&& !ClassifyOverride
)
497 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
498 "job-sheets-supported", NULL
, Classification
);
500 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
501 "job-sheets-supported", NumBanners
+ 1, NULL
, NULL
);
504 LogMessage(L_EMERG
, "SetPrinterAttrs: Unable to allocate memory for "
505 "job-sheets-supported attribute: %s!",
507 else if (!Classification
|| ClassifyOverride
)
509 attr
->values
[0].string
.text
= strdup("none");
511 for (i
= 0; i
< NumBanners
; i
++)
512 attr
->values
[i
+ 1].string
.text
= strdup(Banners
[i
].name
);
517 * Loop through the printers and update the op_policy_ptr values...
520 for (p
= Printers
; p
; p
= p
->next
)
521 if ((p
->op_policy_ptr
= FindPolicy(p
->op_policy
)) == NULL
)
522 p
->op_policy_ptr
= DefaultPolicyPtr
;
527 * 'DeleteAllPrinters()' - Delete all printers from the system.
531 DeleteAllPrinters(void)
533 printer_t
*p
, /* Pointer to current printer/class */
534 *next
; /* Pointer to next printer in list */
537 for (p
= Printers
; p
!= NULL
; p
= next
)
541 if (!(p
->type
& CUPS_PRINTER_CLASS
))
548 * 'DeletePrinter()' - Delete a printer from the system.
552 DeletePrinter(printer_t
*p
, /* I - Printer to delete */
553 int update
) /* I - Update printers.conf? */
555 int i
; /* Looping var */
556 printer_t
*current
, /* Current printer in list */
557 *prev
; /* Previous printer in list */
559 char filename
[1024]; /* Interface script filename */
563 DEBUG_printf(("DeletePrinter(%08x): p->name = \"%s\"...\n", p
, p
->name
));
566 * Range check input...
573 * Find the printer in the list...
576 for (prev
= NULL
, current
= Printers
;
578 prev
= current
, current
= current
->next
)
584 LogMessage(L_ERROR
, "Tried to delete a non-existent printer %s!\n",
590 * If this printer is the next for browsing, point to the next one...
594 BrowseNext
= p
->next
;
597 * Remove the printer from the list...
603 prev
->next
= p
->next
;
608 * Stop printing on this printer...
611 StopPrinter(p
, update
);
614 * Remove the dummy interface/icon/option files under IRIX...
618 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
621 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
625 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
628 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
631 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
634 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
639 * If p is the default printer, assign the next one...
642 if (p
== DefaultPrinter
)
644 DefaultPrinter
= Printers
;
650 * Remove this printer from any classes and send a browse delete message...
653 if (!(p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
655 DeletePrinterFromClasses(p
);
660 * Free all memory used by the printer...
663 if (p
->printers
!= NULL
)
666 if (MaxPrinterHistory
)
668 for (i
= 0; i
< p
->num_history
; i
++)
669 ippDelete(p
->history
[i
]);
674 for (i
= 0; i
< p
->num_reasons
; i
++)
679 DeletePrinterFilters(p
);
684 ClearString(&p
->uri
);
685 ClearString(&p
->hostname
);
686 ClearString(&p
->name
);
687 ClearString(&p
->location
);
688 ClearString(&p
->make_model
);
689 ClearString(&p
->info
);
690 ClearString(&p
->job_sheets
[0]);
691 ClearString(&p
->job_sheets
[1]);
692 ClearString(&p
->device_uri
);
693 ClearString(&p
->op_policy
);
694 ClearString(&p
->error_policy
);
699 * Write a new /etc/printcap file...
707 * 'DeletePrinterFilters()' - Delete all MIME filters for a printer.
711 DeletePrinterFilters(printer_t
*p
) /* I - Printer to remove from */
713 int i
; /* Looping var */
714 mime_filter_t
*filter
; /* MIME filter looping var */
718 * Range check input...
725 * Remove all filters from the MIME database that have a destination
729 for (filter
= MimeDatabase
->filters
, i
= MimeDatabase
->num_filters
;
732 if (filter
->dst
== p
->filetype
)
735 * Delete the current filter...
738 MimeDatabase
->num_filters
--;
741 memmove(filter
, filter
+ 1, sizeof(mime_filter_t
) * (i
- 1));
749 * 'FindDest()' - Find a destination in the list.
752 printer_t
* /* O - Destination in list */
753 FindDest(const char *name
) /* I - Name of printer or class to find */
755 printer_t
*p
; /* Current destination */
756 int diff
; /* Difference */
759 for (p
= Printers
; p
!= NULL
; p
= p
->next
)
760 if ((diff
= strcasecmp(name
, p
->name
)) == 0)/* name == p->name */
762 else if (diff
< 0) /* name < p->name */
770 * 'FindPrinter()' - Find a printer in the list.
773 printer_t
* /* O - Printer in list */
774 FindPrinter(const char *name
) /* I - Name of printer to find */
776 printer_t
*p
; /* Current printer */
777 int diff
; /* Difference */
780 for (p
= Printers
; p
!= NULL
; p
= p
->next
)
781 if ((diff
= strcasecmp(name
, p
->name
)) == 0 &&
782 !(p
->type
& CUPS_PRINTER_CLASS
)) /* name == p->name */
784 else if (diff
< 0) /* name < p->name */
792 * 'FreePrinterUsers()' - Free allow/deny users.
796 FreePrinterUsers(printer_t
*p
) /* I - Printer */
798 int i
; /* Looping var */
801 if (!p
|| !p
->num_users
)
804 for (i
= 0; i
< p
->num_users
; i
++)
805 free((void *)p
->users
[i
]);
815 * 'LoadAllPrinters()' - Load printers from the printers.conf file.
819 LoadAllPrinters(void)
821 cups_file_t
*fp
; /* printers.conf file */
822 int linenum
; /* Current line number */
823 int len
; /* Length of line */
824 char line
[1024], /* Line from file */
825 name
[256], /* Parameter name */
826 *nameptr
, /* Pointer into name */
827 *value
, /* Pointer to value */
828 *valueptr
; /* Pointer into value */
829 printer_t
*p
; /* Current printer */
833 * Open the printers.conf file...
836 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
837 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
839 LogMessage(L_ERROR
, "LoadAllPrinters: Unable to open %s - %s", line
,
845 * Read printer configurations until we hit EOF...
851 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
856 * Skip comment lines...
863 * Strip trailing whitespace, if any...
868 while (len
> 0 && isspace(line
[len
- 1] & 255))
875 * Extract the name from the beginning of the line...
878 for (value
= line
; isspace(*value
& 255); value
++);
880 for (nameptr
= name
; *value
!= '\0' && !isspace(*value
& 255) &&
881 nameptr
< (name
+ sizeof(name
) - 1);)
882 *nameptr
++ = *value
++;
885 while (isspace(*value
& 255))
892 * Decode the directive...
895 if (!strcasecmp(name
, "<Printer") ||
896 !strcasecmp(name
, "<DefaultPrinter"))
899 * <Printer name> or <DefaultPrinter name>
902 if (line
[len
- 1] == '>' && p
== NULL
)
905 * Add the printer and a base file type...
908 line
[len
- 1] = '\0';
910 LogMessage(L_DEBUG
, "LoadAllPrinters: Loading printer %s...", value
);
912 p
= AddPrinter(value
);
914 p
->state
= IPP_PRINTER_IDLE
;
917 * Set the default printer as needed...
920 if (strcasecmp(name
, "<DefaultPrinter") == 0)
925 LogMessage(L_ERROR
, "Syntax error on line %d of printers.conf.",
930 else if (!strcasecmp(name
, "</Printer>"))
935 AddPrinterHistory(p
);
940 LogMessage(L_ERROR
, "Syntax error on line %d of printers.conf.",
947 LogMessage(L_ERROR
, "Syntax error on line %d of printers.conf.",
951 else if (!strcasecmp(name
, "Info"))
952 SetString(&p
->info
, value
);
953 else if (!strcasecmp(name
, "Location"))
954 SetString(&p
->location
, value
);
955 else if (!strcasecmp(name
, "DeviceURI"))
956 SetString(&p
->device_uri
, value
);
957 else if (!strcasecmp(name
, "State"))
960 * Set the initial queue state...
963 if (!strcasecmp(value
, "idle"))
964 p
->state
= IPP_PRINTER_IDLE
;
965 else if (!strcasecmp(value
, "stopped"))
966 p
->state
= IPP_PRINTER_STOPPED
;
968 else if (!strcasecmp(name
, "StateMessage"))
971 * Set the initial queue state message...
974 while (isspace(*value
& 255))
977 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
979 else if (!strcasecmp(name
, "Accepting"))
982 * Set the initial accepting state...
985 if (!strcasecmp(value
, "yes") ||
986 !strcasecmp(value
, "on") ||
987 !strcasecmp(value
, "true"))
992 else if (!strcasecmp(name
, "JobSheets"))
995 * Set the initial job sheets...
998 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1003 SetString(&p
->job_sheets
[0], value
);
1005 while (isspace(*valueptr
& 255))
1010 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1015 SetString(&p
->job_sheets
[1], value
);
1018 else if (!strcasecmp(name
, "AllowUser"))
1021 AddPrinterUser(p
, value
);
1023 else if (!strcasecmp(name
, "DenyUser"))
1026 AddPrinterUser(p
, value
);
1028 else if (!strcasecmp(name
, "QuotaPeriod"))
1029 p
->quota_period
= atoi(value
);
1030 else if (!strcasecmp(name
, "PageLimit"))
1031 p
->page_limit
= atoi(value
);
1032 else if (!strcasecmp(name
, "KLimit"))
1033 p
->k_limit
= atoi(value
);
1034 else if (!strcasecmp(name
, "OpPolicy"))
1035 SetString(&p
->op_policy
, value
);
1036 else if (!strcasecmp(name
, "ErrorPolicy"))
1037 SetString(&p
->error_policy
, value
);
1041 * Something else we don't understand...
1044 LogMessage(L_ERROR
, "Unknown configuration directive %s on line %d of printers.conf.",
1054 * 'SaveAllPrinters()' - Save all printer definitions to the printers.conf
1059 SaveAllPrinters(void)
1061 int i
; /* Looping var */
1062 cups_file_t
*fp
; /* printers.conf file */
1063 char temp
[1024]; /* Temporary string */
1064 char backup
[1024]; /* printers.conf.O file */
1065 printer_t
*printer
; /* Current printer class */
1066 time_t curtime
; /* Current time */
1067 struct tm
*curdate
; /* Current date */
1071 * Create the printers.conf file...
1074 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1075 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1077 if (rename(temp
, backup
))
1078 LogMessage(L_ERROR
, "Unable to backup printers.conf - %s", strerror(errno
));
1080 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1082 LogMessage(L_ERROR
, "Unable to save printers.conf - %s", strerror(errno
));
1084 if (rename(backup
, temp
))
1085 LogMessage(L_ERROR
, "Unable to restore printers.conf - %s", strerror(errno
));
1089 LogMessage(L_INFO
, "Saving printers.conf...");
1092 * Restrict access to the file...
1095 fchown(cupsFileNumber(fp
), getuid(), Group
);
1096 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1099 * Write a small header to the file...
1102 curtime
= time(NULL
);
1103 curdate
= localtime(&curtime
);
1104 strftime(temp
, sizeof(temp
) - 1, CUPS_STRFTIME_FORMAT
, curdate
);
1106 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1107 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1110 * Write each local printer known to the system...
1113 for (printer
= Printers
; printer
!= NULL
; printer
= printer
->next
)
1116 * Skip remote destinations and printer classes...
1119 if ((printer
->type
& CUPS_PRINTER_REMOTE
) ||
1120 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1121 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1125 * Write printers as needed...
1128 if (printer
== DefaultPrinter
)
1129 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1131 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1134 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
1136 if (printer
->location
)
1137 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
1139 if (printer
->device_uri
)
1140 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
1142 if (printer
->state
== IPP_PRINTER_STOPPED
)
1144 cupsFilePuts(fp
, "State Stopped\n");
1145 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
1148 cupsFilePuts(fp
, "State Idle\n");
1150 if (printer
->accepting
)
1151 cupsFilePuts(fp
, "Accepting Yes\n");
1153 cupsFilePuts(fp
, "Accepting No\n");
1155 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
1156 printer
->job_sheets
[1]);
1158 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1159 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1160 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1162 for (i
= 0; i
< printer
->num_users
; i
++)
1163 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
1166 cupsFilePrintf(fp
, "OpPolicy %s\n", printer
->op_policy
);
1167 cupsFilePrintf(fp
, "ErrorPolicy %s\n", printer
->error_policy
);
1169 cupsFilePuts(fp
, "</Printer>\n");
1173 * Make IRIX desktop & printer status happy
1176 write_irix_state(printer
);
1185 * 'SetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1189 SetPrinterAttrs(printer_t
*p
) /* I - Printer to setup */
1191 char uri
[HTTP_MAX_URI
]; /* URI for printer */
1192 char method
[HTTP_MAX_URI
], /* Method portion of URI */
1193 username
[HTTP_MAX_URI
], /* Username portion of URI */
1194 host
[HTTP_MAX_URI
], /* Host portion of URI */
1195 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1196 int port
; /* Port portion of URI */
1197 int i
; /* Looping var */
1198 char filename
[1024]; /* Name of PPD file */
1199 int num_media
; /* Number of media options */
1200 location_t
*auth
; /* Pointer to authentication element */
1201 const char *auth_supported
; /* Authentication supported */
1202 cups_ptype_t printer_type
; /* Printer type data */
1203 ppd_file_t
*ppd
; /* PPD file data */
1204 ppd_option_t
*input_slot
, /* InputSlot options */
1205 *media_type
, /* MediaType options */
1206 *page_size
, /* PageSize options */
1207 *output_bin
, /* OutputBin options */
1208 *media_quality
; /* EFMediaQualityMode options */
1209 ppd_attr_t
*ppdattr
; /* PPD attribute */
1210 ipp_attribute_t
*attr
; /* Attribute data */
1211 ipp_value_t
*val
; /* Attribute value */
1213 ipp_finish_t finishings
[5]; /* finishings-supported values */
1214 static const char * const sides
[3] = /* sides-supported values */
1222 DEBUG_printf(("SetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
1226 * Make sure that we have the common attributes defined...
1233 * Clear out old filters and add a filter from application/vnd.cups-raw to
1234 * printer/name to handle "raw" printing by users.
1237 DeletePrinterFilters(p
);
1238 AddPrinterFilter(p
, "application/vnd.cups-raw 0 -");
1241 * Figure out the authentication that is required for the printer.
1244 auth_supported
= "requesting-user-name";
1245 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
1247 if (p
->type
& CUPS_PRINTER_CLASS
)
1248 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1250 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1252 if ((auth
= FindBest(resource
, HTTP_POST
)) != NULL
)
1254 if (auth
->type
== AUTH_BASIC
|| auth
->type
== AUTH_BASICDIGEST
)
1255 auth_supported
= "basic";
1256 else if (auth
->type
== AUTH_DIGEST
)
1257 auth_supported
= "digest";
1262 * Create the required IPP attributes for a printer...
1266 ippDelete(p
->attrs
);
1268 p
->attrs
= ippNew();
1270 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-uri-supported",
1272 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1273 "uri-authentication-supported", NULL
, auth_supported
);
1274 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1275 "uri-security-supported", NULL
, "none");
1276 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
1278 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
1279 NULL
, p
->location
? p
->location
: "");
1280 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
1281 NULL
, p
->info
? p
->info
: "");
1282 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
1288 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1289 "requesting-user-name-denied", p
->num_users
, NULL
,
1292 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1293 "requesting-user-name-allowed", p
->num_users
, NULL
,
1297 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1298 "job-quota-period", p
->quota_period
);
1299 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1300 "job-k-limit", p
->k_limit
);
1301 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1302 "job-page-limit", p
->page_limit
);
1304 if (NumBanners
> 0 && !(p
->type
& CUPS_PRINTER_REMOTE
))
1307 * Setup the job-sheets-default attribute...
1310 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1311 "job-sheets-default", 2, NULL
, NULL
);
1315 attr
->values
[0].string
.text
= strdup(Classification
?
1316 Classification
: p
->job_sheets
[0]);
1317 attr
->values
[1].string
.text
= strdup(Classification
?
1318 Classification
: p
->job_sheets
[1]);
1322 printer_type
= p
->type
;
1326 if (p
->type
& CUPS_PRINTER_REMOTE
)
1329 * Tell the client this is a remote printer of some type...
1332 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1333 "printer-make-and-model", NULL
, p
->make_model
);
1340 * Assign additional attributes depending on whether this is a printer
1344 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
1346 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
1349 * Add class-specific attributes...
1352 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0)
1353 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1354 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
1356 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1357 "printer-make-and-model", NULL
, "Local Printer Class");
1359 if (p
->num_printers
> 0)
1362 * Add a list of member URIs and names...
1365 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
1366 "member-uris", p
->num_printers
, NULL
, NULL
);
1367 p
->type
|= CUPS_PRINTER_OPTIONS
;
1369 for (i
= 0; i
< p
->num_printers
; i
++)
1372 attr
->values
[i
].string
.text
= strdup(p
->printers
[i
]->uri
);
1374 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
1377 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
1378 "member-names", p
->num_printers
, NULL
, NULL
);
1382 for (i
= 0; i
< p
->num_printers
; i
++)
1383 attr
->values
[i
].string
.text
= strdup(p
->printers
[i
]->name
);
1390 * Add printer-specific attributes... Start by sanitizing the device
1391 * URI so it doesn't have a username or password in it...
1395 strcpy(uri
, "file:/dev/null");
1396 else if (strstr(p
->device_uri
, "://") != NULL
)
1399 * http://..., ipp://..., etc.
1402 httpSeparate(p
->device_uri
, method
, username
, host
, &port
, resource
);
1404 snprintf(uri
, sizeof(uri
), "%s://%s:%d%s", method
, host
, port
,
1407 snprintf(uri
, sizeof(uri
), "%s://%s%s", method
, host
, resource
);
1412 * file:..., serial:..., etc.
1415 strlcpy(uri
, p
->device_uri
, sizeof(uri
));
1418 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
1422 * Assign additional attributes from the PPD file (if any)...
1425 p
->type
|= CUPS_PRINTER_BW
;
1426 finishings
[0] = IPP_FINISHINGS_NONE
;
1429 snprintf(filename
, sizeof(filename
), "%s/ppd/%s.ppd", ServerRoot
,
1432 if ((ppd
= ppdOpenFile(filename
)) != NULL
)
1435 * Add make/model and other various attributes...
1438 if (ppd
->color_device
)
1439 p
->type
|= CUPS_PRINTER_COLOR
;
1440 if (ppd
->variable_sizes
)
1441 p
->type
|= CUPS_PRINTER_VARIABLE
;
1442 if (!ppd
->manual_copies
)
1443 p
->type
|= CUPS_PRINTER_COPIES
;
1444 if ((ppdattr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
1445 if (ppdattr
->value
&& !strcasecmp(ppdattr
->value
, "true"))
1446 p
->type
|= CUPS_PRINTER_FAX
;
1448 ippAddBoolean(p
->attrs
, IPP_TAG_PRINTER
, "color-supported",
1450 if (ppd
->throughput
)
1451 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
1452 "pages-per-minute", ppd
->throughput
);
1455 SetString(&p
->make_model
, ppd
->nickname
);
1456 else if (ppd
->modelname
)
1457 SetString(&p
->make_model
, ppd
->modelname
);
1459 SetString(&p
->make_model
, "Bad PPD File");
1461 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1462 "printer-make-and-model", NULL
, p
->make_model
);
1465 * Add media options from the PPD file...
1468 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) != NULL
)
1469 num_media
= input_slot
->num_choices
;
1473 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
1474 num_media
+= media_type
->num_choices
;
1476 if ((page_size
= ppdFindOption(ppd
, "PageSize")) != NULL
)
1477 num_media
+= page_size
->num_choices
;
1479 if ((media_quality
= ppdFindOption(ppd
, "EFMediaQualityMode")) != NULL
)
1480 num_media
+= media_quality
->num_choices
;
1484 LogMessage(L_CRIT
, "SetPrinterAttrs: The PPD file for printer %s "
1485 "contains no media options and is therefore "
1486 "invalid!", p
->name
);
1490 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1491 "media-supported", num_media
, NULL
, NULL
);
1496 if (input_slot
!= NULL
)
1497 for (i
= 0; i
< input_slot
->num_choices
; i
++, val
++)
1498 val
->string
.text
= strdup(input_slot
->choices
[i
].choice
);
1500 if (media_type
!= NULL
)
1501 for (i
= 0; i
< media_type
->num_choices
; i
++, val
++)
1502 val
->string
.text
= strdup(media_type
->choices
[i
].choice
);
1504 if (media_quality
!= NULL
)
1505 for (i
= 0; i
< media_quality
->num_choices
; i
++, val
++)
1506 val
->string
.text
= strdup(media_quality
->choices
[i
].choice
);
1508 if (page_size
!= NULL
)
1510 for (i
= 0; i
< page_size
->num_choices
; i
++, val
++)
1511 val
->string
.text
= strdup(page_size
->choices
[i
].choice
);
1513 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-default",
1514 NULL
, page_size
->defchoice
);
1516 else if (input_slot
!= NULL
)
1517 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-default",
1518 NULL
, input_slot
->defchoice
);
1519 else if (media_type
!= NULL
)
1520 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-default",
1521 NULL
, media_type
->defchoice
);
1522 else if (media_quality
!= NULL
)
1523 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-default",
1524 NULL
, media_quality
->defchoice
);
1526 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "media-default",
1535 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
1537 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
1538 "output-bin-supported", output_bin
->num_choices
,
1543 for (i
= 0, val
= attr
->values
;
1544 i
< output_bin
->num_choices
;
1546 val
->string
.text
= strdup(output_bin
->choices
[i
].choice
);
1554 if (ppdFindOption(ppd
, "Duplex") != NULL
)
1556 p
->type
|= CUPS_PRINTER_DUPLEX
;
1558 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "sides-supported",
1560 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
, "sides-default",
1564 if (ppdFindOption(ppd
, "Collate") != NULL
)
1565 p
->type
|= CUPS_PRINTER_COLLATE
;
1567 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
1569 p
->type
|= CUPS_PRINTER_STAPLE
;
1570 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
1573 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
1575 p
->type
|= CUPS_PRINTER_BIND
;
1576 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
1579 for (i
= 0; i
< ppd
->num_sizes
; i
++)
1580 if (ppd
->sizes
[i
].length
> 1728)
1581 p
->type
|= CUPS_PRINTER_LARGE
;
1582 else if (ppd
->sizes
[i
].length
> 1008)
1583 p
->type
|= CUPS_PRINTER_MEDIUM
;
1585 p
->type
|= CUPS_PRINTER_SMALL
;
1588 * Add any filters in the PPD file...
1591 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
1592 for (i
= 0; i
< ppd
->num_filters
; i
++)
1594 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
1595 AddPrinterFilter(p
, ppd
->filters
[i
]);
1598 if (ppd
->num_filters
== 0)
1599 AddPrinterFilter(p
, "application/vnd.cups-postscript 0 -");
1603 printer_type
= p
->type
;
1605 else if (access(filename
, 0) == 0)
1607 int pline
; /* PPD line number */
1608 ppd_status_t pstatus
; /* PPD load status */
1611 pstatus
= ppdLastError(&pline
);
1613 LogMessage(L_ERROR
, "PPD file for %s cannot be loaded!", p
->name
);
1615 if (pstatus
<= PPD_ALLOC_ERROR
)
1616 LogMessage(L_ERROR
, "%s", strerror(errno
));
1618 LogMessage(L_ERROR
, "%s on line %d.", ppdErrorString(pstatus
),
1621 LogMessage(L_INFO
, "Hint: Run \"cupstestppd %s\" and fix any errors.",
1624 AddPrinterFilter(p
, "application/vnd.cups-postscript 0 -");
1629 * If we have an interface script, add a filter entry for it...
1632 snprintf(filename
, sizeof(filename
), "%s/interfaces/%s", ServerRoot
,
1634 if (access(filename
, X_OK
) == 0)
1637 * Yes, we have a System V style interface script; use it!
1640 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1641 "printer-make-and-model", NULL
, "Local System V Printer");
1643 snprintf(filename
, sizeof(filename
), "*/* 0 %s/interfaces/%s",
1644 ServerRoot
, p
->name
);
1645 AddPrinterFilter(p
, filename
);
1647 else if (p
->device_uri
&&
1648 strncmp(p
->device_uri
, "ipp://", 6) == 0 &&
1649 (strstr(p
->device_uri
, "/printers/") != NULL
||
1650 strstr(p
->device_uri
, "/classes/") != NULL
))
1653 * Tell the client this is really a hard-wired remote printer.
1656 printer_type
|= CUPS_PRINTER_REMOTE
;
1659 * Reset the printer-uri-supported attribute to point at the
1663 attr
= ippFindAttribute(p
->attrs
, "printer-uri-supported", IPP_TAG_URI
);
1664 free(attr
->values
[0].string
.text
);
1665 attr
->values
[0].string
.text
= strdup(p
->device_uri
);
1668 * Then set the make-and-model accordingly...
1671 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1672 "printer-make-and-model", NULL
, "Remote Printer");
1675 * Print all files directly...
1683 * Otherwise we have neither - treat this as a "dumb" printer
1684 * with no PPD file...
1687 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
1688 "printer-make-and-model", NULL
, "Local Raw Printer");
1694 ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1695 "finishings-supported", num_finishings
, (int *)finishings
);
1696 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
1697 "finishings-default", IPP_FINISHINGS_NONE
);
1702 * Add the CUPS-specific printer-type attribute...
1705 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-type",
1708 DEBUG_printf(("SetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
1713 * Write the IRIX printer config and status files...
1716 write_irix_config(p
);
1717 write_irix_state(p
);
1723 * 'SetPrinterReasons()' - Set/update the reasons strings.
1727 SetPrinterReasons(printer_t
*p
, /* I - Printer */
1728 const char *s
) /* I - Reasons strings */
1730 int i
; /* Looping var */
1731 const char *sptr
; /* Pointer into reasons */
1732 char reason
[255], /* Reason string */
1733 *rptr
; /* Pointer into reason */
1736 if (s
[0] == '-' || s
[0] == '+')
1739 * Add/remove reasons...
1747 * Replace reasons...
1752 for (i
= 0; i
< p
->num_reasons
; i
++)
1753 free(p
->reasons
[i
]);
1759 * Loop through all of the reasons...
1765 * Skip leading whitespace and commas...
1768 while (isspace(*sptr
& 255) || *sptr
== ',')
1771 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
1772 if (rptr
< (reason
+ sizeof(reason
) - 1))
1786 for (i
= 0; i
< p
->num_reasons
; i
++)
1787 if (!strcasecmp(reason
, p
->reasons
[i
]))
1790 * Found a match, so remove it...
1794 free(p
->reasons
[i
]);
1796 if (i
< p
->num_reasons
)
1797 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
1798 (p
->num_reasons
- i
) * sizeof(char *));
1803 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1809 for (i
= 0; i
< p
->num_reasons
; i
++)
1810 if (!strcasecmp(reason
, p
->reasons
[i
]))
1813 if (i
>= p
->num_reasons
)
1815 p
->reasons
[i
] = strdup(reason
);
1824 * 'SetPrinterState()' - Update the current state of a printer.
1828 SetPrinterState(printer_t
*p
, /* I - Printer to change */
1829 ipp_pstate_t s
, /* I - New state */
1830 int update
) /* I - Update printers.conf? */
1832 ipp_pstate_t old_state
; /* Old printer state */
1836 * Can't set status of remote printers...
1839 if (p
->type
& CUPS_PRINTER_REMOTE
)
1843 * Set the new state...
1846 old_state
= p
->state
;
1852 * Let the browse code know this needs to be updated...
1856 p
->state_time
= time(NULL
);
1860 write_irix_state(p
);
1864 AddPrinterHistory(p
);
1867 * Save the printer configuration if a printer goes from idle or processing
1868 * to stopped (or visa-versa)...
1871 if ((old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
) &&
1874 if (p
->type
& CUPS_PRINTER_CLASS
)
1883 * 'SortPrinters()' - Sort the printer list when a printer name is changed.
1889 printer_t
*current
, /* Current printer */
1890 *prev
, /* Previous printer */
1891 *next
; /* Next printer */
1892 int did_swap
; /* Non-zero if we did a swap */
1897 for (did_swap
= 0, current
= Printers
, prev
= NULL
; current
!= NULL
;)
1898 if (current
->next
== NULL
)
1900 else if (strcasecmp(current
->name
, current
->next
->name
) > 0)
1902 DEBUG_printf(("Swapping %s and %s...\n", current
->name
,
1903 current
->next
->name
));
1906 * Need to swap these two printers...
1912 Printers
= current
->next
;
1914 prev
->next
= current
->next
;
1917 * Yes, we can all get a headache from the next bunch of pointer
1921 next
= current
->next
;
1922 current
->next
= next
->next
;
1923 next
->next
= current
;
1929 current
= current
->next
;
1937 * 'StopPrinter()' - Stop a printer from printing any jobs...
1941 StopPrinter(printer_t
*p
, /* I - Printer to stop */
1942 int update
) /* I - Update printers.conf? */
1944 job_t
*job
; /* Active print job */
1948 * Set the printer state...
1951 SetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
1954 * See if we have a job printing on this printer...
1960 * Get pointer to job...
1963 job
= (job_t
*)p
->job
;
1969 StopJob(job
->id
, 0);
1972 * Reset the state to pending...
1975 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
1983 * 'ValidateDest()' - Validate a printer/class destination.
1986 const char * /* O - Printer or class name */
1987 ValidateDest(const char *hostname
, /* I - Host name */
1988 const char *resource
, /* I - Resource name */
1989 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
1990 printer_t
**printer
) /* O - Printer pointer */
1992 printer_t
*p
; /* Current printer */
1993 char localname
[1024], /* Localized hostname */
1994 *lptr
, /* Pointer into localized hostname */
1995 *sptr
; /* Pointer into server name */
1998 DEBUG_printf(("ValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname
, resource
,
2002 * Initialize return values...
2008 *dtype
= (cups_ptype_t
)0;
2011 * See if the resource is a class or printer...
2014 if (strncmp(resource
, "/classes/", 9) == 0)
2022 else if (strncmp(resource
, "/printers/", 10) == 0)
2033 * Bad resource name...
2040 * See if the printer or class name exists...
2043 if ((p
= FindPrinter(resource
)) == NULL
)
2044 p
= FindClass(resource
);
2046 if (p
== NULL
&& strchr(resource
, '@') == NULL
)
2053 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2054 CUPS_PRINTER_REMOTE
);
2059 * Change localhost to the server name...
2062 if (strcasecmp(hostname
, "localhost") == 0)
2063 hostname
= ServerName
;
2065 strlcpy(localname
, hostname
, sizeof(localname
));
2067 if (strcasecmp(hostname
, ServerName
) != 0)
2070 * Localize the hostname...
2073 lptr
= strchr(localname
, '.');
2074 sptr
= strchr(ServerName
, '.');
2076 if (sptr
!= NULL
&& lptr
!= NULL
)
2079 * Strip the common domain name components...
2082 while (lptr
!= NULL
)
2084 if (strcasecmp(lptr
, sptr
) == 0)
2090 lptr
= strchr(lptr
+ 1, '.');
2095 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
2098 * Find a matching printer or class...
2101 for (p
= Printers
; p
!= NULL
; p
= p
->next
)
2102 if (strcasecmp(p
->hostname
, localname
) == 0 &&
2103 strcasecmp(p
->name
, resource
) == 0)
2108 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
2109 CUPS_PRINTER_REMOTE
);
2118 * 'WritePrintcap()' - Write a pseudo-printcap file for older applications
2125 cups_file_t
*fp
; /* printcap file */
2126 printer_t
*p
; /* Current printer */
2131 * Update the IRIX printer state for the default printer; if
2132 * no printers remain, then the default printer file will be
2136 write_irix_state(DefaultPrinter
);
2140 * See if we have a printcap file; if not, don't bother writing it.
2147 * Open the printcap file...
2150 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
2154 * Put a comment header at the top so that users will know where the
2155 * data has come from...
2158 cupsFilePuts(fp
, "# This file was automatically generated by cupsd(8) from the\n");
2159 cupsFilePrintf(fp
, "# %s/printers.conf file. All changes to this file\n",
2161 cupsFilePuts(fp
, "# will be lost.\n");
2164 * Write a new printcap with the current list of printers.
2167 switch (PrintcapFormat
)
2171 * Each printer is put in the file as:
2181 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
2182 DefaultPrinter
->info
, ServerName
, DefaultPrinter
->name
);
2184 for (p
= Printers
; p
!= NULL
; p
= p
->next
)
2185 if (p
!= DefaultPrinter
)
2186 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
2187 ServerName
, p
->name
);
2190 case PRINTCAP_SOLARIS
:
2192 * Each printer is put in the file as:
2194 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2195 * _default:use=DefaultPrinter
2197 * :bsdaddr=ServerName,Printer1:\
2198 * :description=Description:
2200 * :bsdaddr=ServerName,Printer2:\
2201 * :description=Description:
2203 * :bsdaddr=ServerName,Printer3:\
2204 * :description=Description:
2207 * :bsdaddr=ServerName,PrinterN:\
2208 * :description=Description:
2211 cupsFilePuts(fp
, "_all:all=");
2212 for (p
= Printers
; p
!= NULL
; p
= p
->next
)
2213 cupsFilePrintf(fp
, "%s%c", p
->name
, p
->next
? ',' : '\n');
2216 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
2218 for (p
= Printers
; p
!= NULL
; p
= p
->next
)
2219 cupsFilePrintf(fp
, "%s:\\\n"
2220 "\t:bsdaddr=%s,%s:\\\n"
2221 "\t:description=%s:\n",
2222 p
->name
, ServerName
, p
->name
, p
->info
? p
->info
: "");
2236 * 'write_irix_config()' - Update the config files used by the IRIX
2241 write_irix_config(printer_t
*p
) /* I - Printer to update */
2243 char filename
[1024]; /* Interface script filename */
2244 cups_file_t
*fp
; /* Interface script file */
2245 int tag
; /* Status tag value */
2250 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
2251 * tools. First the interface script that tells the tools what kind of
2252 * printer we have...
2255 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
2257 if (p
->type
& CUPS_PRINTER_CLASS
)
2259 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
2261 cupsFilePuts(fp
, "#!/bin/sh\n");
2263 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
2264 IPP_TAG_TEXT
)) != NULL
)
2265 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
2266 else if (p
->type
& CUPS_PRINTER_CLASS
)
2267 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
2269 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
2271 if (p
->type
& CUPS_PRINTER_COLOR
)
2272 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
2274 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
2276 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
2277 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
2281 chmod(filename
, 0755);
2282 chown(filename
, User
, Group
);
2286 * Then the member file that tells which device file the queue is connected
2287 * to... Networked printers use "/dev/null" in this file, so that's what
2288 * we use (the actual device URI can confuse some apps...)
2291 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
2293 if (p
->type
& CUPS_PRINTER_CLASS
)
2295 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
2297 cupsFilePuts(fp
, "/dev/null\n");
2301 chmod(filename
, 0644);
2302 chown(filename
, User
, Group
);
2306 * The gui_interface file is a script or program that launches a GUI
2307 * option panel for the printer, using options specified on the
2308 * command-line in the third argument. The option panel must send
2309 * any printing options to stdout on a single line when the user
2310 * accepts them, or nothing if the user cancels the dialog.
2312 * The default options panel program is /usr/bin/glpoptions, from
2313 * the ESP Print Pro software. You can select another using the
2314 * PrintcapGUI option.
2317 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
2319 if (p
->type
& CUPS_PRINTER_CLASS
)
2321 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
2323 cupsFilePuts(fp
, "#!/bin/sh\n");
2324 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
2328 chmod(filename
, 0755);
2329 chown(filename
, User
, Group
);
2333 * The POD config file is needed by the printstatus command to show
2334 * the printer location and device.
2337 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
2339 if (p
->type
& CUPS_PRINTER_CLASS
)
2341 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
2343 cupsFilePrintf(fp
, "Printer Class | %s\n",
2344 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
2345 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
2346 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
2347 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
2348 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
? p
->device_uri
: "");
2349 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
2350 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
2351 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
2355 chmod(filename
, 0664);
2356 chown(filename
, User
, Group
);
2361 * 'write_irix_state()' - Update the status files used by IRIX printing
2366 write_irix_state(printer_t
*p
) /* I - Printer to update */
2368 char filename
[1024]; /* Interface script filename */
2369 cups_file_t
*fp
; /* Interface script file */
2370 int tag
; /* Status tag value */
2376 * The POD status file is needed for the printstatus window to
2377 * provide the current status of the printer.
2380 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
2382 if (p
->type
& CUPS_PRINTER_CLASS
)
2384 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
2386 cupsFilePrintf(fp
, "Operational Status | %s\n",
2387 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
2388 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
2390 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
2391 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
2392 p
->device_uri
? p
->device_uri
: "");
2393 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
2394 p
->accepting
? "Accepting" : "Not accepting");
2395 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
2399 chmod(filename
, 0664);
2400 chown(filename
, User
, Group
);
2404 * The activeicons file is needed to provide desktop icons for printers:
2406 * [ quoted from /usr/lib/print/tagit ]
2408 * --- Type of printer tags (base values)
2410 * Dumb=66048 # 0x10200
2411 * DumbColor=66080 # 0x10220
2412 * Raster=66112 # 0x10240
2413 * ColorRaster=66144 # 0x10260
2414 * Plotter=66176 # 0x10280
2415 * PostScript=66208 # 0x102A0
2416 * ColorPostScript=66240 # 0x102C0
2417 * MonoPostScript=66272 # 0x102E0
2419 * --- Printer state modifiers for local printers
2424 * Unknown=3 # 0x3 (Faulted due to unknown reason)
2426 * --- Printer state modifiers for network printers
2430 * NetFaulted=10 # 0xA
2431 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
2434 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
2436 if (p
->type
& CUPS_PRINTER_CLASS
)
2438 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
2440 if (p
->type
& CUPS_PRINTER_COLOR
)
2445 if (p
->type
& CUPS_PRINTER_REMOTE
)
2448 if (p
->state
== IPP_PRINTER_PROCESSING
)
2451 else if (p
->state
== IPP_PRINTER_STOPPED
)
2454 cupsFilePuts(fp
, "#!/bin/sh\n");
2455 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
2459 chmod(filename
, 0755);
2460 chown(filename
, User
, Group
);
2465 * The default file is needed by the printers window to show
2466 * the default printer.
2469 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
2471 if (DefaultPrinter
!= NULL
)
2473 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
2475 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
2479 chmod(filename
, 0644);
2480 chown(filename
, User
, Group
);
2490 * End of "$Id: printers.c,v 1.93.2.67 2004/07/02 22:15:51 mike Exp $".