]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/printers.c
Fix some policy issues when the policy from the printers.conf file doesn't
[thirdparty/cups.git] / scheduler / printers.c
1 /*
2 * "$Id: printers.c,v 1.93.2.67 2004/07/02 22:15:51 mike Exp $"
3 *
4 * Printer routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2004 by Easy Software Products, all rights reserved.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3142 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
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
41 * changed.
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
47 * desktop tools.
48 * write_irix_state() - Update the status files used by IRIX printing
49 * desktop tools.
50 */
51
52 /*
53 * Include necessary headers...
54 */
55
56 #include "cupsd.h"
57
58
59 /*
60 * Local functions...
61 */
62
63 #ifdef __sgi
64 static void write_irix_state(printer_t *p);
65 #endif /* __sgi */
66
67
68 /*
69 * 'AddPrinter()' - Add a printer to the system.
70 */
71
72 printer_t * /* O - New printer */
73 AddPrinter(const char *name) /* I - Name of printer */
74 {
75 printer_t *p, /* New printer */
76 *current, /* Current printer in list */
77 *prev; /* Previous printer in list */
78
79
80 /*
81 * Range check input...
82 */
83
84 LogMessage(L_DEBUG2, "AddPrinter(\"%s\")", name ? name : "(null)");
85
86 if (name == NULL)
87 return (NULL);
88
89 /*
90 * Create a new printer entity...
91 */
92
93 if ((p = calloc(1, sizeof(printer_t))) == NULL)
94 {
95 LogMessage(L_ERROR, "Unable to allocate memory for printer - %s",
96 strerror(errno));
97 return (NULL);
98 }
99
100 SetString(&p->name, name);
101 SetString(&p->info, name);
102 SetString(&p->hostname, ServerName);
103
104 SetStringf(&p->uri, "ipp://%s:%d/printers/%s", ServerName, LocalPort, name);
105 SetStringf(&p->device_uri, "file:/dev/null");
106
107 p->state = IPP_PRINTER_STOPPED;
108 p->accepting = 0;
109 p->filetype = mimeAddType(MimeDatabase, "printer", name);
110
111 SetString(&p->job_sheets[0], "none");
112 SetString(&p->job_sheets[1], "none");
113
114 SetString(&p->error_policy, "stop-printer");
115 SetString(&p->op_policy, DefaultPolicy);
116
117 p->op_policy_ptr = DefaultPolicyPtr;
118
119 if (MaxPrinterHistory)
120 p->history = calloc(MaxPrinterHistory, sizeof(ipp_t *));
121
122 /*
123 * Insert the printer in the printer list alphabetically...
124 */
125
126 for (prev = NULL, current = Printers;
127 current != NULL;
128 prev = current, current = current->next)
129 if (strcasecmp(p->name, current->name) < 0)
130 break;
131
132 /*
133 * Insert this printer before the current one...
134 */
135
136 if (prev == NULL)
137 Printers = p;
138 else
139 prev->next = p;
140
141 p->next = current;
142
143 /*
144 * Write a new /etc/printcap or /var/spool/lp/pstatus file.
145 */
146
147 WritePrintcap();
148
149 /*
150 * Bump the printer count and return...
151 */
152
153 NumPrinters ++;
154
155 return (p);
156 }
157
158
159 /*
160 * 'AddPrinterFilter()' - Add a MIME filter for a printer.
161 */
162
163 void
164 AddPrinterFilter(printer_t *p, /* I - Printer to add to */
165 const char *filter) /* I - Filter to add */
166 {
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 */
173
174
175 /*
176 * Range check input...
177 */
178
179 if (p == NULL || p->filetype == NULL || filter == NULL)
180 return;
181
182 /*
183 * Parse the filter string; it should be in the following format:
184 *
185 * super/type cost program
186 */
187
188 if (sscanf(filter, "%15[^/]/%31s%d%1023s", super, type, &cost, program) != 4)
189 {
190 LogMessage(L_ERROR, "AddPrinterFilter: Invalid filter string \"%s\"!",
191 filter);
192 return;
193 }
194
195 /*
196 * Add the filter to the MIME database, supporting wildcards as needed...
197 */
198
199 for (temptype = MimeDatabase->types, i = MimeDatabase->num_types;
200 i > 0;
201 i --, temptype ++)
202 if (((super[0] == '*' && strcasecmp((*temptype)->super, "printer") != 0) ||
203 strcasecmp((*temptype)->super, super) == 0) &&
204 (type[0] == '*' || strcasecmp((*temptype)->type, type) == 0))
205 {
206 LogMessage(L_DEBUG2, "Adding filter %s/%s %s/%s %d %s",
207 (*temptype)->super, (*temptype)->type,
208 p->filetype->super, p->filetype->type,
209 cost, program);
210 mimeAddFilter(MimeDatabase, *temptype, p->filetype, cost, program);
211 }
212 }
213
214
215 /*
216 * 'AddPrinterHistory()' - Add the current printer state to the history.
217 */
218
219 void
220 AddPrinterHistory(printer_t *p) /* I - Printer */
221 {
222 ipp_t *history; /* History collection */
223
224
225 /*
226 * Stop early if we aren't keeping history data...
227 */
228
229 if (MaxPrinterHistory <= 0)
230 return;
231
232 /*
233 * Retire old history data as needed...
234 */
235
236 p->sequence_number ++;
237
238 if (p->num_history >= MaxPrinterHistory)
239 {
240 p->num_history --;
241 ippDelete(p->history[0]);
242 memmove(p->history, p->history + 1, p->num_history * sizeof(ipp_t *));
243 }
244
245 /*
246 * Create a collection containing the current printer-state, printer-up-time,
247 * printer-state-message, and printer-state-reasons attributes.
248 */
249
250 history = ippNew();
251 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
252 p->state);
253 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
254 p->accepting);
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");
261 else
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);
269
270 p->history[p->num_history] = history;
271 p->num_history ++;
272 }
273
274
275 /*
276 * 'AddPrinterUser()' - Add a user to the ACL.
277 */
278
279 void
280 AddPrinterUser(printer_t *p, /* I - Printer */
281 const char *username) /* I - User */
282 {
283 const char **temp; /* Temporary array pointer */
284
285
286 if (!p || !username)
287 return;
288
289 if (p->num_users == 0)
290 temp = malloc(sizeof(char **));
291 else
292 temp = realloc(p->users, sizeof(char **) * (p->num_users + 1));
293
294 if (!temp)
295 return;
296
297 p->users = temp;
298 temp += p->num_users;
299
300 if ((*temp = strdup(username)) != NULL)
301 p->num_users ++;
302 }
303
304
305 /*
306 * 'CreateCommonData()' - Create the common printer data.
307 */
308
309 void
310 CreateCommonData(void)
311 {
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 */
318 {
319 IPP_PORTRAIT,
320 IPP_LANDSCAPE,
321 IPP_REVERSE_LANDSCAPE,
322 IPP_REVERSE_PORTRAIT
323 };
324 static const char * const holds[] = /* job-hold-until-supported values */
325 {
326 "no-hold",
327 "indefinite",
328 "day-time",
329 "evening",
330 "night",
331 "second-shift",
332 "third-shift",
333 "weekend"
334 };
335 static const char * const versions[] =/* ipp-versions-supported values */
336 {
337 "1.0",
338 "1.1"
339 };
340 static const ipp_op_t ops[] = /* operations-supported values */
341 {
342 IPP_PRINT_JOB,
343 IPP_VALIDATE_JOB,
344 IPP_CREATE_JOB,
345 IPP_SEND_DOCUMENT,
346 IPP_CANCEL_JOB,
347 IPP_GET_JOB_ATTRIBUTES,
348 IPP_GET_JOBS,
349 IPP_GET_PRINTER_ATTRIBUTES,
350 IPP_HOLD_JOB,
351 IPP_RELEASE_JOB,
352 IPP_PAUSE_PRINTER,
353 IPP_RESUME_PRINTER,
354 IPP_PURGE_JOBS,
355 IPP_SET_JOB_ATTRIBUTES,
356 IPP_ENABLE_PRINTER,
357 IPP_DISABLE_PRINTER,
358 CUPS_GET_DEFAULT,
359 CUPS_GET_PRINTERS,
360 CUPS_ADD_PRINTER,
361 CUPS_DELETE_PRINTER,
362 CUPS_GET_CLASSES,
363 CUPS_ADD_CLASS,
364 CUPS_DELETE_CLASS,
365 CUPS_ACCEPT_JOBS,
366 CUPS_REJECT_JOBS,
367 CUPS_GET_DEVICES,
368 CUPS_GET_PPDS,
369 IPP_RESTART_JOB
370 };
371 static const char * const charsets[] =/* charset-supported values */
372 {
373 "us-ascii",
374 "iso-8859-1",
375 "iso-8859-2",
376 "iso-8859-3",
377 "iso-8859-4",
378 "iso-8859-5",
379 "iso-8859-6",
380 "iso-8859-7",
381 "iso-8859-8",
382 "iso-8859-9",
383 "iso-8859-10",
384 "iso-8859-13",
385 "iso-8859-14",
386 "iso-8859-15",
387 "utf-8",
388 "windows-874",
389 "windows-1250",
390 "windows-1251",
391 "windows-1252",
392 "windows-1253",
393 "windows-1254",
394 "windows-1255",
395 "windows-1256",
396 "windows-1257",
397 "windows-1258",
398 "koi8-r",
399 "koi8-u",
400 };
401 static const char * const compressions[] =
402 { /* document-compression-supported values */
403 "none"
404 #ifdef HAVE_LIBZ
405 ,"gzip"
406 #endif /* HAVE_LIBZ */
407 };
408 static const char * const multiple_document_handling[] =
409 { /* multiple-document-handling-supported values */
410 "separate-documents-uncollated-copies",
411 "separate-documents-collated-copies"
412 };
413 static const char * const errors[] = /* printer-error-policy-supported values */
414 {
415 "abort-job",
416 "retry-job",
417 "stop-printer"
418 };
419
420
421 if (CommonData)
422 ippDelete(CommonData);
423
424 CommonData = ippNew();
425
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]),
430 NULL, versions);
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]),
447 NULL, charsets);
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]),
460 NULL, compressions);
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]),
479 NULL, holds);
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);
489
490 if (NumBanners > 0)
491 {
492 /*
493 * Setup the job-sheets-supported and job-sheets-default attributes...
494 */
495
496 if (Classification && !ClassifyOverride)
497 attr = ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
498 "job-sheets-supported", NULL, Classification);
499 else
500 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
501 "job-sheets-supported", NumBanners + 1, NULL, NULL);
502
503 if (attr == NULL)
504 LogMessage(L_EMERG, "SetPrinterAttrs: Unable to allocate memory for "
505 "job-sheets-supported attribute: %s!",
506 strerror(errno));
507 else if (!Classification || ClassifyOverride)
508 {
509 attr->values[0].string.text = strdup("none");
510
511 for (i = 0; i < NumBanners; i ++)
512 attr->values[i + 1].string.text = strdup(Banners[i].name);
513 }
514 }
515
516 /*
517 * Loop through the printers and update the op_policy_ptr values...
518 */
519
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;
523 }
524
525
526 /*
527 * 'DeleteAllPrinters()' - Delete all printers from the system.
528 */
529
530 void
531 DeleteAllPrinters(void)
532 {
533 printer_t *p, /* Pointer to current printer/class */
534 *next; /* Pointer to next printer in list */
535
536
537 for (p = Printers; p != NULL; p = next)
538 {
539 next = p->next;
540
541 if (!(p->type & CUPS_PRINTER_CLASS))
542 DeletePrinter(p, 0);
543 }
544 }
545
546
547 /*
548 * 'DeletePrinter()' - Delete a printer from the system.
549 */
550
551 void
552 DeletePrinter(printer_t *p, /* I - Printer to delete */
553 int update) /* I - Update printers.conf? */
554 {
555 int i; /* Looping var */
556 printer_t *current, /* Current printer in list */
557 *prev; /* Previous printer in list */
558 #ifdef __sgi
559 char filename[1024]; /* Interface script filename */
560 #endif /* __sgi */
561
562
563 DEBUG_printf(("DeletePrinter(%08x): p->name = \"%s\"...\n", p, p->name));
564
565 /*
566 * Range check input...
567 */
568
569 if (p == NULL)
570 return;
571
572 /*
573 * Find the printer in the list...
574 */
575
576 for (prev = NULL, current = Printers;
577 current != NULL;
578 prev = current, current = current->next)
579 if (p == current)
580 break;
581
582 if (current == NULL)
583 {
584 LogMessage(L_ERROR, "Tried to delete a non-existent printer %s!\n",
585 p->name);
586 return;
587 }
588
589 /*
590 * If this printer is the next for browsing, point to the next one...
591 */
592
593 if (p == BrowseNext)
594 BrowseNext = p->next;
595
596 /*
597 * Remove the printer from the list...
598 */
599
600 if (prev == NULL)
601 Printers = p->next;
602 else
603 prev->next = p->next;
604
605 NumPrinters --;
606
607 /*
608 * Stop printing on this printer...
609 */
610
611 StopPrinter(p, update);
612
613 /*
614 * Remove the dummy interface/icon/option files under IRIX...
615 */
616
617 #ifdef __sgi
618 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
619 unlink(filename);
620
621 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui",
622 p->name);
623 unlink(filename);
624
625 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
626 unlink(filename);
627
628 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
629 unlink(filename);
630
631 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
632 unlink(filename);
633
634 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
635 unlink(filename);
636 #endif /* __sgi */
637
638 /*
639 * If p is the default printer, assign the next one...
640 */
641
642 if (p == DefaultPrinter)
643 {
644 DefaultPrinter = Printers;
645
646 WritePrintcap();
647 }
648
649 /*
650 * Remove this printer from any classes and send a browse delete message...
651 */
652
653 if (!(p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)))
654 {
655 DeletePrinterFromClasses(p);
656 SendBrowseDelete(p);
657 }
658
659 /*
660 * Free all memory used by the printer...
661 */
662
663 if (p->printers != NULL)
664 free(p->printers);
665
666 if (MaxPrinterHistory)
667 {
668 for (i = 0; i < p->num_history; i ++)
669 ippDelete(p->history[i]);
670
671 free(p->history);
672 }
673
674 for (i = 0; i < p->num_reasons; i ++)
675 free(p->reasons[i]);
676
677 ippDelete(p->attrs);
678
679 DeletePrinterFilters(p);
680
681 FreePrinterUsers(p);
682 FreeQuotas(p);
683
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);
695
696 free(p);
697
698 /*
699 * Write a new /etc/printcap file...
700 */
701
702 WritePrintcap();
703 }
704
705
706 /*
707 * 'DeletePrinterFilters()' - Delete all MIME filters for a printer.
708 */
709
710 void
711 DeletePrinterFilters(printer_t *p) /* I - Printer to remove from */
712 {
713 int i; /* Looping var */
714 mime_filter_t *filter; /* MIME filter looping var */
715
716
717 /*
718 * Range check input...
719 */
720
721 if (p == NULL)
722 return;
723
724 /*
725 * Remove all filters from the MIME database that have a destination
726 * type == printer...
727 */
728
729 for (filter = MimeDatabase->filters, i = MimeDatabase->num_filters;
730 i > 0;
731 i --, filter ++)
732 if (filter->dst == p->filetype)
733 {
734 /*
735 * Delete the current filter...
736 */
737
738 MimeDatabase->num_filters --;
739
740 if (i > 1)
741 memmove(filter, filter + 1, sizeof(mime_filter_t) * (i - 1));
742
743 filter --;
744 }
745 }
746
747
748 /*
749 * 'FindDest()' - Find a destination in the list.
750 */
751
752 printer_t * /* O - Destination in list */
753 FindDest(const char *name) /* I - Name of printer or class to find */
754 {
755 printer_t *p; /* Current destination */
756 int diff; /* Difference */
757
758
759 for (p = Printers; p != NULL; p = p->next)
760 if ((diff = strcasecmp(name, p->name)) == 0)/* name == p->name */
761 return (p);
762 else if (diff < 0) /* name < p->name */
763 return (NULL);
764
765 return (NULL);
766 }
767
768
769 /*
770 * 'FindPrinter()' - Find a printer in the list.
771 */
772
773 printer_t * /* O - Printer in list */
774 FindPrinter(const char *name) /* I - Name of printer to find */
775 {
776 printer_t *p; /* Current printer */
777 int diff; /* Difference */
778
779
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 */
783 return (p);
784 else if (diff < 0) /* name < p->name */
785 return (NULL);
786
787 return (NULL);
788 }
789
790
791 /*
792 * 'FreePrinterUsers()' - Free allow/deny users.
793 */
794
795 void
796 FreePrinterUsers(printer_t *p) /* I - Printer */
797 {
798 int i; /* Looping var */
799
800
801 if (!p || !p->num_users)
802 return;
803
804 for (i = 0; i < p->num_users; i ++)
805 free((void *)p->users[i]);
806
807 free(p->users);
808
809 p->num_users = 0;
810 p->users = NULL;
811 }
812
813
814 /*
815 * 'LoadAllPrinters()' - Load printers from the printers.conf file.
816 */
817
818 void
819 LoadAllPrinters(void)
820 {
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 */
830
831
832 /*
833 * Open the printers.conf file...
834 */
835
836 snprintf(line, sizeof(line), "%s/printers.conf", ServerRoot);
837 if ((fp = cupsFileOpen(line, "r")) == NULL)
838 {
839 LogMessage(L_ERROR, "LoadAllPrinters: Unable to open %s - %s", line,
840 strerror(errno));
841 return;
842 }
843
844 /*
845 * Read printer configurations until we hit EOF...
846 */
847
848 linenum = 0;
849 p = NULL;
850
851 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
852 {
853 linenum ++;
854
855 /*
856 * Skip comment lines...
857 */
858
859 if (line[0] == '#')
860 continue;
861
862 /*
863 * Strip trailing whitespace, if any...
864 */
865
866 len = strlen(line);
867
868 while (len > 0 && isspace(line[len - 1] & 255))
869 {
870 len --;
871 line[len] = '\0';
872 }
873
874 /*
875 * Extract the name from the beginning of the line...
876 */
877
878 for (value = line; isspace(*value & 255); value ++);
879
880 for (nameptr = name; *value != '\0' && !isspace(*value & 255) &&
881 nameptr < (name + sizeof(name) - 1);)
882 *nameptr++ = *value++;
883 *nameptr = '\0';
884
885 while (isspace(*value & 255))
886 value ++;
887
888 if (name[0] == '\0')
889 continue;
890
891 /*
892 * Decode the directive...
893 */
894
895 if (!strcasecmp(name, "<Printer") ||
896 !strcasecmp(name, "<DefaultPrinter"))
897 {
898 /*
899 * <Printer name> or <DefaultPrinter name>
900 */
901
902 if (line[len - 1] == '>' && p == NULL)
903 {
904 /*
905 * Add the printer and a base file type...
906 */
907
908 line[len - 1] = '\0';
909
910 LogMessage(L_DEBUG, "LoadAllPrinters: Loading printer %s...", value);
911
912 p = AddPrinter(value);
913 p->accepting = 1;
914 p->state = IPP_PRINTER_IDLE;
915
916 /*
917 * Set the default printer as needed...
918 */
919
920 if (strcasecmp(name, "<DefaultPrinter") == 0)
921 DefaultPrinter = p;
922 }
923 else
924 {
925 LogMessage(L_ERROR, "Syntax error on line %d of printers.conf.",
926 linenum);
927 return;
928 }
929 }
930 else if (!strcasecmp(name, "</Printer>"))
931 {
932 if (p != NULL)
933 {
934 SetPrinterAttrs(p);
935 AddPrinterHistory(p);
936 p = NULL;
937 }
938 else
939 {
940 LogMessage(L_ERROR, "Syntax error on line %d of printers.conf.",
941 linenum);
942 return;
943 }
944 }
945 else if (p == NULL)
946 {
947 LogMessage(L_ERROR, "Syntax error on line %d of printers.conf.",
948 linenum);
949 return;
950 }
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"))
958 {
959 /*
960 * Set the initial queue state...
961 */
962
963 if (!strcasecmp(value, "idle"))
964 p->state = IPP_PRINTER_IDLE;
965 else if (!strcasecmp(value, "stopped"))
966 p->state = IPP_PRINTER_STOPPED;
967 }
968 else if (!strcasecmp(name, "StateMessage"))
969 {
970 /*
971 * Set the initial queue state message...
972 */
973
974 while (isspace(*value & 255))
975 value ++;
976
977 strlcpy(p->state_message, value, sizeof(p->state_message));
978 }
979 else if (!strcasecmp(name, "Accepting"))
980 {
981 /*
982 * Set the initial accepting state...
983 */
984
985 if (!strcasecmp(value, "yes") ||
986 !strcasecmp(value, "on") ||
987 !strcasecmp(value, "true"))
988 p->accepting = 1;
989 else
990 p->accepting = 0;
991 }
992 else if (!strcasecmp(name, "JobSheets"))
993 {
994 /*
995 * Set the initial job sheets...
996 */
997
998 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
999
1000 if (*valueptr)
1001 *valueptr++ = '\0';
1002
1003 SetString(&p->job_sheets[0], value);
1004
1005 while (isspace(*valueptr & 255))
1006 valueptr ++;
1007
1008 if (*valueptr)
1009 {
1010 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1011
1012 if (*valueptr)
1013 *valueptr++ = '\0';
1014
1015 SetString(&p->job_sheets[1], value);
1016 }
1017 }
1018 else if (!strcasecmp(name, "AllowUser"))
1019 {
1020 p->deny_users = 0;
1021 AddPrinterUser(p, value);
1022 }
1023 else if (!strcasecmp(name, "DenyUser"))
1024 {
1025 p->deny_users = 1;
1026 AddPrinterUser(p, value);
1027 }
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);
1038 else
1039 {
1040 /*
1041 * Something else we don't understand...
1042 */
1043
1044 LogMessage(L_ERROR, "Unknown configuration directive %s on line %d of printers.conf.",
1045 name, linenum);
1046 }
1047 }
1048
1049 cupsFileClose(fp);
1050 }
1051
1052
1053 /*
1054 * 'SaveAllPrinters()' - Save all printer definitions to the printers.conf
1055 * file.
1056 */
1057
1058 void
1059 SaveAllPrinters(void)
1060 {
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 */
1068
1069
1070 /*
1071 * Create the printers.conf file...
1072 */
1073
1074 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
1075 snprintf(backup, sizeof(backup), "%s/printers.conf.O", ServerRoot);
1076
1077 if (rename(temp, backup))
1078 LogMessage(L_ERROR, "Unable to backup printers.conf - %s", strerror(errno));
1079
1080 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1081 {
1082 LogMessage(L_ERROR, "Unable to save printers.conf - %s", strerror(errno));
1083
1084 if (rename(backup, temp))
1085 LogMessage(L_ERROR, "Unable to restore printers.conf - %s", strerror(errno));
1086 return;
1087 }
1088 else
1089 LogMessage(L_INFO, "Saving printers.conf...");
1090
1091 /*
1092 * Restrict access to the file...
1093 */
1094
1095 fchown(cupsFileNumber(fp), getuid(), Group);
1096 fchmod(cupsFileNumber(fp), ConfigFilePerm);
1097
1098 /*
1099 * Write a small header to the file...
1100 */
1101
1102 curtime = time(NULL);
1103 curdate = localtime(&curtime);
1104 strftime(temp, sizeof(temp) - 1, CUPS_STRFTIME_FORMAT, curdate);
1105
1106 cupsFilePuts(fp, "# Printer configuration file for " CUPS_SVERSION "\n");
1107 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1108
1109 /*
1110 * Write each local printer known to the system...
1111 */
1112
1113 for (printer = Printers; printer != NULL; printer = printer->next)
1114 {
1115 /*
1116 * Skip remote destinations and printer classes...
1117 */
1118
1119 if ((printer->type & CUPS_PRINTER_REMOTE) ||
1120 (printer->type & CUPS_PRINTER_CLASS) ||
1121 (printer->type & CUPS_PRINTER_IMPLICIT))
1122 continue;
1123
1124 /*
1125 * Write printers as needed...
1126 */
1127
1128 if (printer == DefaultPrinter)
1129 cupsFilePrintf(fp, "<DefaultPrinter %s>\n", printer->name);
1130 else
1131 cupsFilePrintf(fp, "<Printer %s>\n", printer->name);
1132
1133 if (printer->info)
1134 cupsFilePrintf(fp, "Info %s\n", printer->info);
1135
1136 if (printer->location)
1137 cupsFilePrintf(fp, "Location %s\n", printer->location);
1138
1139 if (printer->device_uri)
1140 cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
1141
1142 if (printer->state == IPP_PRINTER_STOPPED)
1143 {
1144 cupsFilePuts(fp, "State Stopped\n");
1145 cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
1146 }
1147 else
1148 cupsFilePuts(fp, "State Idle\n");
1149
1150 if (printer->accepting)
1151 cupsFilePuts(fp, "Accepting Yes\n");
1152 else
1153 cupsFilePuts(fp, "Accepting No\n");
1154
1155 cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
1156 printer->job_sheets[1]);
1157
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);
1161
1162 for (i = 0; i < printer->num_users; i ++)
1163 cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
1164 printer->users[i]);
1165
1166 cupsFilePrintf(fp, "OpPolicy %s\n", printer->op_policy);
1167 cupsFilePrintf(fp, "ErrorPolicy %s\n", printer->error_policy);
1168
1169 cupsFilePuts(fp, "</Printer>\n");
1170
1171 #ifdef __sgi
1172 /*
1173 * Make IRIX desktop & printer status happy
1174 */
1175
1176 write_irix_state(printer);
1177 #endif /* __sgi */
1178 }
1179
1180 cupsFileClose(fp);
1181 }
1182
1183
1184 /*
1185 * 'SetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1186 */
1187
1188 void
1189 SetPrinterAttrs(printer_t *p) /* I - Printer to setup */
1190 {
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 */
1212 int num_finishings;
1213 ipp_finish_t finishings[5]; /* finishings-supported values */
1214 static const char * const sides[3] = /* sides-supported values */
1215 {
1216 "one",
1217 "two-long-edge",
1218 "two-short-edge"
1219 };
1220
1221
1222 DEBUG_printf(("SetPrinterAttrs: entering name = %s, type = %x\n", p->name,
1223 p->type));
1224
1225 /*
1226 * Make sure that we have the common attributes defined...
1227 */
1228
1229 if (!CommonData)
1230 CreateCommonData();
1231
1232 /*
1233 * Clear out old filters and add a filter from application/vnd.cups-raw to
1234 * printer/name to handle "raw" printing by users.
1235 */
1236
1237 DeletePrinterFilters(p);
1238 AddPrinterFilter(p, "application/vnd.cups-raw 0 -");
1239
1240 /*
1241 * Figure out the authentication that is required for the printer.
1242 */
1243
1244 auth_supported = "requesting-user-name";
1245 if (!(p->type & CUPS_PRINTER_REMOTE))
1246 {
1247 if (p->type & CUPS_PRINTER_CLASS)
1248 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
1249 else
1250 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
1251
1252 if ((auth = FindBest(resource, HTTP_POST)) != NULL)
1253 {
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";
1258 }
1259 }
1260
1261 /*
1262 * Create the required IPP attributes for a printer...
1263 */
1264
1265 if (p->attrs)
1266 ippDelete(p->attrs);
1267
1268 p->attrs = ippNew();
1269
1270 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uri-supported",
1271 NULL, p->uri);
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,
1277 p->name);
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",
1283 NULL, p->uri);
1284
1285 if (p->num_users)
1286 {
1287 if (p->deny_users)
1288 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1289 "requesting-user-name-denied", p->num_users, NULL,
1290 p->users);
1291 else
1292 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1293 "requesting-user-name-allowed", p->num_users, NULL,
1294 p->users);
1295 }
1296
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);
1303
1304 if (NumBanners > 0 && !(p->type & CUPS_PRINTER_REMOTE))
1305 {
1306 /*
1307 * Setup the job-sheets-default attribute...
1308 */
1309
1310 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1311 "job-sheets-default", 2, NULL, NULL);
1312
1313 if (attr != NULL)
1314 {
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]);
1319 }
1320 }
1321
1322 printer_type = p->type;
1323
1324 p->raw = 0;
1325
1326 if (p->type & CUPS_PRINTER_REMOTE)
1327 {
1328 /*
1329 * Tell the client this is a remote printer of some type...
1330 */
1331
1332 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1333 "printer-make-and-model", NULL, p->make_model);
1334
1335 p->raw = 1;
1336 }
1337 else
1338 {
1339 /*
1340 * Assign additional attributes depending on whether this is a printer
1341 * or class...
1342 */
1343
1344 p->type &= ~CUPS_PRINTER_OPTIONS;
1345
1346 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1347 {
1348 /*
1349 * Add class-specific attributes...
1350 */
1351
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);
1355 else
1356 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1357 "printer-make-and-model", NULL, "Local Printer Class");
1358
1359 if (p->num_printers > 0)
1360 {
1361 /*
1362 * Add a list of member URIs and names...
1363 */
1364
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;
1368
1369 for (i = 0; i < p->num_printers; i ++)
1370 {
1371 if (attr != NULL)
1372 attr->values[i].string.text = strdup(p->printers[i]->uri);
1373
1374 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
1375 }
1376
1377 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1378 "member-names", p->num_printers, NULL, NULL);
1379
1380 if (attr != NULL)
1381 {
1382 for (i = 0; i < p->num_printers; i ++)
1383 attr->values[i].string.text = strdup(p->printers[i]->name);
1384 }
1385 }
1386 }
1387 else
1388 {
1389 /*
1390 * Add printer-specific attributes... Start by sanitizing the device
1391 * URI so it doesn't have a username or password in it...
1392 */
1393
1394 if (!p->device_uri)
1395 strcpy(uri, "file:/dev/null");
1396 else if (strstr(p->device_uri, "://") != NULL)
1397 {
1398 /*
1399 * http://..., ipp://..., etc.
1400 */
1401
1402 httpSeparate(p->device_uri, method, username, host, &port, resource);
1403 if (port)
1404 snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, host, port,
1405 resource);
1406 else
1407 snprintf(uri, sizeof(uri), "%s://%s%s", method, host, resource);
1408 }
1409 else
1410 {
1411 /*
1412 * file:..., serial:..., etc.
1413 */
1414
1415 strlcpy(uri, p->device_uri, sizeof(uri));
1416 }
1417
1418 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1419 uri);
1420
1421 /*
1422 * Assign additional attributes from the PPD file (if any)...
1423 */
1424
1425 p->type |= CUPS_PRINTER_BW;
1426 finishings[0] = IPP_FINISHINGS_NONE;
1427 num_finishings = 1;
1428
1429 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
1430 p->name);
1431
1432 if ((ppd = ppdOpenFile(filename)) != NULL)
1433 {
1434 /*
1435 * Add make/model and other various attributes...
1436 */
1437
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;
1447
1448 ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
1449 ppd->color_device);
1450 if (ppd->throughput)
1451 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1452 "pages-per-minute", ppd->throughput);
1453
1454 if (ppd->nickname)
1455 SetString(&p->make_model, ppd->nickname);
1456 else if (ppd->modelname)
1457 SetString(&p->make_model, ppd->modelname);
1458 else
1459 SetString(&p->make_model, "Bad PPD File");
1460
1461 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1462 "printer-make-and-model", NULL, p->make_model);
1463
1464 /*
1465 * Add media options from the PPD file...
1466 */
1467
1468 if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
1469 num_media = input_slot->num_choices;
1470 else
1471 num_media = 0;
1472
1473 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
1474 num_media += media_type->num_choices;
1475
1476 if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
1477 num_media += page_size->num_choices;
1478
1479 if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
1480 num_media += media_quality->num_choices;
1481
1482 if (num_media == 0)
1483 {
1484 LogMessage(L_CRIT, "SetPrinterAttrs: The PPD file for printer %s "
1485 "contains no media options and is therefore "
1486 "invalid!", p->name);
1487 }
1488 else
1489 {
1490 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1491 "media-supported", num_media, NULL, NULL);
1492 if (attr != NULL)
1493 {
1494 val = attr->values;
1495
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);
1499
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);
1503
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);
1507
1508 if (page_size != NULL)
1509 {
1510 for (i = 0; i < page_size->num_choices; i ++, val ++)
1511 val->string.text = strdup(page_size->choices[i].choice);
1512
1513 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default",
1514 NULL, page_size->defchoice);
1515 }
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);
1525 else
1526 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default",
1527 NULL, "none");
1528 }
1529 }
1530
1531 /*
1532 * Output bin...
1533 */
1534
1535 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
1536 {
1537 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1538 "output-bin-supported", output_bin->num_choices,
1539 NULL, NULL);
1540
1541 if (attr != NULL)
1542 {
1543 for (i = 0, val = attr->values;
1544 i < output_bin->num_choices;
1545 i ++, val ++)
1546 val->string.text = strdup(output_bin->choices[i].choice);
1547 }
1548 }
1549
1550 /*
1551 * Duplexing, etc...
1552 */
1553
1554 if (ppdFindOption(ppd, "Duplex") != NULL)
1555 {
1556 p->type |= CUPS_PRINTER_DUPLEX;
1557
1558 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "sides-supported",
1559 3, NULL, sides);
1560 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "sides-default",
1561 NULL, "one");
1562 }
1563
1564 if (ppdFindOption(ppd, "Collate") != NULL)
1565 p->type |= CUPS_PRINTER_COLLATE;
1566
1567 if (ppdFindOption(ppd, "StapleLocation") != NULL)
1568 {
1569 p->type |= CUPS_PRINTER_STAPLE;
1570 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
1571 }
1572
1573 if (ppdFindOption(ppd, "BindEdge") != NULL)
1574 {
1575 p->type |= CUPS_PRINTER_BIND;
1576 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
1577 }
1578
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;
1584 else
1585 p->type |= CUPS_PRINTER_SMALL;
1586
1587 /*
1588 * Add any filters in the PPD file...
1589 */
1590
1591 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
1592 for (i = 0; i < ppd->num_filters; i ++)
1593 {
1594 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
1595 AddPrinterFilter(p, ppd->filters[i]);
1596 }
1597
1598 if (ppd->num_filters == 0)
1599 AddPrinterFilter(p, "application/vnd.cups-postscript 0 -");
1600
1601 ppdClose(ppd);
1602
1603 printer_type = p->type;
1604 }
1605 else if (access(filename, 0) == 0)
1606 {
1607 int pline; /* PPD line number */
1608 ppd_status_t pstatus; /* PPD load status */
1609
1610
1611 pstatus = ppdLastError(&pline);
1612
1613 LogMessage(L_ERROR, "PPD file for %s cannot be loaded!", p->name);
1614
1615 if (pstatus <= PPD_ALLOC_ERROR)
1616 LogMessage(L_ERROR, "%s", strerror(errno));
1617 else
1618 LogMessage(L_ERROR, "%s on line %d.", ppdErrorString(pstatus),
1619 pline);
1620
1621 LogMessage(L_INFO, "Hint: Run \"cupstestppd %s\" and fix any errors.",
1622 filename);
1623
1624 AddPrinterFilter(p, "application/vnd.cups-postscript 0 -");
1625 }
1626 else
1627 {
1628 /*
1629 * If we have an interface script, add a filter entry for it...
1630 */
1631
1632 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
1633 p->name);
1634 if (access(filename, X_OK) == 0)
1635 {
1636 /*
1637 * Yes, we have a System V style interface script; use it!
1638 */
1639
1640 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1641 "printer-make-and-model", NULL, "Local System V Printer");
1642
1643 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
1644 ServerRoot, p->name);
1645 AddPrinterFilter(p, filename);
1646 }
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))
1651 {
1652 /*
1653 * Tell the client this is really a hard-wired remote printer.
1654 */
1655
1656 printer_type |= CUPS_PRINTER_REMOTE;
1657
1658 /*
1659 * Reset the printer-uri-supported attribute to point at the
1660 * remote printer...
1661 */
1662
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);
1666
1667 /*
1668 * Then set the make-and-model accordingly...
1669 */
1670
1671 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1672 "printer-make-and-model", NULL, "Remote Printer");
1673
1674 /*
1675 * Print all files directly...
1676 */
1677
1678 p->raw = 1;
1679 }
1680 else
1681 {
1682 /*
1683 * Otherwise we have neither - treat this as a "dumb" printer
1684 * with no PPD file...
1685 */
1686
1687 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1688 "printer-make-and-model", NULL, "Local Raw Printer");
1689
1690 p->raw = 1;
1691 }
1692 }
1693
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);
1698 }
1699 }
1700
1701 /*
1702 * Add the CUPS-specific printer-type attribute...
1703 */
1704
1705 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type",
1706 printer_type);
1707
1708 DEBUG_printf(("SetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
1709 p->type));
1710
1711 #ifdef __sgi
1712 /*
1713 * Write the IRIX printer config and status files...
1714 */
1715
1716 write_irix_config(p);
1717 write_irix_state(p);
1718 #endif /* __sgi */
1719 }
1720
1721
1722 /*
1723 * 'SetPrinterReasons()' - Set/update the reasons strings.
1724 */
1725
1726 void
1727 SetPrinterReasons(printer_t *p, /* I - Printer */
1728 const char *s) /* I - Reasons strings */
1729 {
1730 int i; /* Looping var */
1731 const char *sptr; /* Pointer into reasons */
1732 char reason[255], /* Reason string */
1733 *rptr; /* Pointer into reason */
1734
1735
1736 if (s[0] == '-' || s[0] == '+')
1737 {
1738 /*
1739 * Add/remove reasons...
1740 */
1741
1742 sptr = s + 1;
1743 }
1744 else
1745 {
1746 /*
1747 * Replace reasons...
1748 */
1749
1750 sptr = s;
1751
1752 for (i = 0; i < p->num_reasons; i ++)
1753 free(p->reasons[i]);
1754
1755 p->num_reasons = 0;
1756 }
1757
1758 /*
1759 * Loop through all of the reasons...
1760 */
1761
1762 while (*sptr)
1763 {
1764 /*
1765 * Skip leading whitespace and commas...
1766 */
1767
1768 while (isspace(*sptr & 255) || *sptr == ',')
1769 sptr ++;
1770
1771 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
1772 if (rptr < (reason + sizeof(reason) - 1))
1773 *rptr++ = *sptr;
1774
1775 if (rptr == reason)
1776 break;
1777
1778 *rptr = '\0';
1779
1780 if (s[0] == '-')
1781 {
1782 /*
1783 * Remove reason...
1784 */
1785
1786 for (i = 0; i < p->num_reasons; i ++)
1787 if (!strcasecmp(reason, p->reasons[i]))
1788 {
1789 /*
1790 * Found a match, so remove it...
1791 */
1792
1793 p->num_reasons --;
1794 free(p->reasons[i]);
1795
1796 if (i < p->num_reasons)
1797 memmove(p->reasons + i, p->reasons + i + 1,
1798 (p->num_reasons - i) * sizeof(char *));
1799
1800 i --;
1801 }
1802 }
1803 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
1804 {
1805 /*
1806 * Add reason...
1807 */
1808
1809 for (i = 0; i < p->num_reasons; i ++)
1810 if (!strcasecmp(reason, p->reasons[i]))
1811 break;
1812
1813 if (i >= p->num_reasons)
1814 {
1815 p->reasons[i] = strdup(reason);
1816 p->num_reasons ++;
1817 }
1818 }
1819 }
1820 }
1821
1822
1823 /*
1824 * 'SetPrinterState()' - Update the current state of a printer.
1825 */
1826
1827 void
1828 SetPrinterState(printer_t *p, /* I - Printer to change */
1829 ipp_pstate_t s, /* I - New state */
1830 int update) /* I - Update printers.conf? */
1831 {
1832 ipp_pstate_t old_state; /* Old printer state */
1833
1834
1835 /*
1836 * Can't set status of remote printers...
1837 */
1838
1839 if (p->type & CUPS_PRINTER_REMOTE)
1840 return;
1841
1842 /*
1843 * Set the new state...
1844 */
1845
1846 old_state = p->state;
1847 p->state = s;
1848
1849 if (old_state != s)
1850 {
1851 /*
1852 * Let the browse code know this needs to be updated...
1853 */
1854
1855 BrowseNext = p;
1856 p->state_time = time(NULL);
1857 p->browse_time = 0;
1858
1859 #ifdef __sgi
1860 write_irix_state(p);
1861 #endif /* __sgi */
1862 }
1863
1864 AddPrinterHistory(p);
1865
1866 /*
1867 * Save the printer configuration if a printer goes from idle or processing
1868 * to stopped (or visa-versa)...
1869 */
1870
1871 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
1872 update)
1873 {
1874 if (p->type & CUPS_PRINTER_CLASS)
1875 SaveAllClasses();
1876 else
1877 SaveAllPrinters();
1878 }
1879 }
1880
1881
1882 /*
1883 * 'SortPrinters()' - Sort the printer list when a printer name is changed.
1884 */
1885
1886 void
1887 SortPrinters(void)
1888 {
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 */
1893
1894
1895 do
1896 {
1897 for (did_swap = 0, current = Printers, prev = NULL; current != NULL;)
1898 if (current->next == NULL)
1899 break;
1900 else if (strcasecmp(current->name, current->next->name) > 0)
1901 {
1902 DEBUG_printf(("Swapping %s and %s...\n", current->name,
1903 current->next->name));
1904
1905 /*
1906 * Need to swap these two printers...
1907 */
1908
1909 did_swap = 1;
1910
1911 if (prev == NULL)
1912 Printers = current->next;
1913 else
1914 prev->next = current->next;
1915
1916 /*
1917 * Yes, we can all get a headache from the next bunch of pointer
1918 * swapping...
1919 */
1920
1921 next = current->next;
1922 current->next = next->next;
1923 next->next = current;
1924 prev = next;
1925 }
1926 else
1927 {
1928 prev = current;
1929 current = current->next;
1930 }
1931 }
1932 while (did_swap);
1933 }
1934
1935
1936 /*
1937 * 'StopPrinter()' - Stop a printer from printing any jobs...
1938 */
1939
1940 void
1941 StopPrinter(printer_t *p, /* I - Printer to stop */
1942 int update) /* I - Update printers.conf? */
1943 {
1944 job_t *job; /* Active print job */
1945
1946
1947 /*
1948 * Set the printer state...
1949 */
1950
1951 SetPrinterState(p, IPP_PRINTER_STOPPED, update);
1952
1953 /*
1954 * See if we have a job printing on this printer...
1955 */
1956
1957 if (p->job)
1958 {
1959 /*
1960 * Get pointer to job...
1961 */
1962
1963 job = (job_t *)p->job;
1964
1965 /*
1966 * Stop it...
1967 */
1968
1969 StopJob(job->id, 0);
1970
1971 /*
1972 * Reset the state to pending...
1973 */
1974
1975 job->state->values[0].integer = IPP_JOB_PENDING;
1976
1977 SaveJob(job->id);
1978 }
1979 }
1980
1981
1982 /*
1983 * 'ValidateDest()' - Validate a printer/class destination.
1984 */
1985
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 */
1991 {
1992 printer_t *p; /* Current printer */
1993 char localname[1024], /* Localized hostname */
1994 *lptr, /* Pointer into localized hostname */
1995 *sptr; /* Pointer into server name */
1996
1997
1998 DEBUG_printf(("ValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname, resource,
1999 dtype, printer));
2000
2001 /*
2002 * Initialize return values...
2003 */
2004
2005 if (printer)
2006 *printer = NULL;
2007
2008 *dtype = (cups_ptype_t)0;
2009
2010 /*
2011 * See if the resource is a class or printer...
2012 */
2013
2014 if (strncmp(resource, "/classes/", 9) == 0)
2015 {
2016 /*
2017 * Class...
2018 */
2019
2020 resource += 9;
2021 }
2022 else if (strncmp(resource, "/printers/", 10) == 0)
2023 {
2024 /*
2025 * Printer...
2026 */
2027
2028 resource += 10;
2029 }
2030 else
2031 {
2032 /*
2033 * Bad resource name...
2034 */
2035
2036 return (NULL);
2037 }
2038
2039 /*
2040 * See if the printer or class name exists...
2041 */
2042
2043 if ((p = FindPrinter(resource)) == NULL)
2044 p = FindClass(resource);
2045
2046 if (p == NULL && strchr(resource, '@') == NULL)
2047 return (NULL);
2048 else if (p != NULL)
2049 {
2050 if (printer)
2051 *printer = p;
2052
2053 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2054 CUPS_PRINTER_REMOTE);
2055 return (p->name);
2056 }
2057
2058 /*
2059 * Change localhost to the server name...
2060 */
2061
2062 if (strcasecmp(hostname, "localhost") == 0)
2063 hostname = ServerName;
2064
2065 strlcpy(localname, hostname, sizeof(localname));
2066
2067 if (strcasecmp(hostname, ServerName) != 0)
2068 {
2069 /*
2070 * Localize the hostname...
2071 */
2072
2073 lptr = strchr(localname, '.');
2074 sptr = strchr(ServerName, '.');
2075
2076 if (sptr != NULL && lptr != NULL)
2077 {
2078 /*
2079 * Strip the common domain name components...
2080 */
2081
2082 while (lptr != NULL)
2083 {
2084 if (strcasecmp(lptr, sptr) == 0)
2085 {
2086 *lptr = '\0';
2087 break;
2088 }
2089 else
2090 lptr = strchr(lptr + 1, '.');
2091 }
2092 }
2093 }
2094
2095 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
2096
2097 /*
2098 * Find a matching printer or class...
2099 */
2100
2101 for (p = Printers; p != NULL; p = p->next)
2102 if (strcasecmp(p->hostname, localname) == 0 &&
2103 strcasecmp(p->name, resource) == 0)
2104 {
2105 if (printer)
2106 *printer = p;
2107
2108 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2109 CUPS_PRINTER_REMOTE);
2110 return (p->name);
2111 }
2112
2113 return (NULL);
2114 }
2115
2116
2117 /*
2118 * 'WritePrintcap()' - Write a pseudo-printcap file for older applications
2119 * that need it...
2120 */
2121
2122 void
2123 WritePrintcap(void)
2124 {
2125 cups_file_t *fp; /* printcap file */
2126 printer_t *p; /* Current printer */
2127
2128
2129 #ifdef __sgi
2130 /*
2131 * Update the IRIX printer state for the default printer; if
2132 * no printers remain, then the default printer file will be
2133 * removed...
2134 */
2135
2136 write_irix_state(DefaultPrinter);
2137 #endif /* __sgi */
2138
2139 /*
2140 * See if we have a printcap file; if not, don't bother writing it.
2141 */
2142
2143 if (!Printcap[0])
2144 return;
2145
2146 /*
2147 * Open the printcap file...
2148 */
2149
2150 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
2151 return;
2152
2153 /*
2154 * Put a comment header at the top so that users will know where the
2155 * data has come from...
2156 */
2157
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",
2160 ServerRoot);
2161 cupsFilePuts(fp, "# will be lost.\n");
2162
2163 /*
2164 * Write a new printcap with the current list of printers.
2165 */
2166
2167 switch (PrintcapFormat)
2168 {
2169 case PRINTCAP_BSD:
2170 /*
2171 * Each printer is put in the file as:
2172 *
2173 * Printer1:
2174 * Printer2:
2175 * Printer3:
2176 * ...
2177 * PrinterN:
2178 */
2179
2180 if (DefaultPrinter)
2181 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
2182 DefaultPrinter->info, ServerName, DefaultPrinter->name);
2183
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);
2188 break;
2189
2190 case PRINTCAP_SOLARIS:
2191 /*
2192 * Each printer is put in the file as:
2193 *
2194 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2195 * _default:use=DefaultPrinter
2196 * Printer1:\
2197 * :bsdaddr=ServerName,Printer1:\
2198 * :description=Description:
2199 * Printer2:
2200 * :bsdaddr=ServerName,Printer2:\
2201 * :description=Description:
2202 * Printer3:
2203 * :bsdaddr=ServerName,Printer3:\
2204 * :description=Description:
2205 * ...
2206 * PrinterN:
2207 * :bsdaddr=ServerName,PrinterN:\
2208 * :description=Description:
2209 */
2210
2211 cupsFilePuts(fp, "_all:all=");
2212 for (p = Printers; p != NULL; p = p->next)
2213 cupsFilePrintf(fp, "%s%c", p->name, p->next ? ',' : '\n');
2214
2215 if (DefaultPrinter)
2216 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
2217
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 : "");
2223 break;
2224 }
2225
2226 /*
2227 * Close the file...
2228 */
2229
2230 cupsFileClose(fp);
2231 }
2232
2233
2234 #ifdef __sgi
2235 /*
2236 * 'write_irix_config()' - Update the config files used by the IRIX
2237 * desktop tools.
2238 */
2239
2240 static void
2241 write_irix_config(printer_t *p) /* I - Printer to update */
2242 {
2243 char filename[1024]; /* Interface script filename */
2244 cups_file_t *fp; /* Interface script file */
2245 int tag; /* Status tag value */
2246
2247
2248
2249 /*
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...
2253 */
2254
2255 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
2256
2257 if (p->type & CUPS_PRINTER_CLASS)
2258 unlink(filename);
2259 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2260 {
2261 cupsFilePuts(fp, "#!/bin/sh\n");
2262
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");
2268 else
2269 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
2270
2271 if (p->type & CUPS_PRINTER_COLOR)
2272 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
2273 else
2274 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
2275
2276 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
2277 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
2278
2279 cupsFileClose(fp);
2280
2281 chmod(filename, 0755);
2282 chown(filename, User, Group);
2283 }
2284
2285 /*
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...)
2289 */
2290
2291 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
2292
2293 if (p->type & CUPS_PRINTER_CLASS)
2294 unlink(filename);
2295 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2296 {
2297 cupsFilePuts(fp, "/dev/null\n");
2298
2299 cupsFileClose(fp);
2300
2301 chmod(filename, 0644);
2302 chown(filename, User, Group);
2303 }
2304
2305 /*
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.
2311 *
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.
2315 */
2316
2317 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
2318
2319 if (p->type & CUPS_PRINTER_CLASS)
2320 unlink(filename);
2321 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2322 {
2323 cupsFilePuts(fp, "#!/bin/sh\n");
2324 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
2325
2326 cupsFileClose(fp);
2327
2328 chmod(filename, 0755);
2329 chown(filename, User, Group);
2330 }
2331
2332 /*
2333 * The POD config file is needed by the printstatus command to show
2334 * the printer location and device.
2335 */
2336
2337 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
2338
2339 if (p->type & CUPS_PRINTER_CLASS)
2340 unlink(filename);
2341 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2342 {
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");
2352
2353 cupsFileClose(fp);
2354
2355 chmod(filename, 0664);
2356 chown(filename, User, Group);
2357 }
2358
2359
2360 /*
2361 * 'write_irix_state()' - Update the status files used by IRIX printing
2362 * desktop tools.
2363 */
2364
2365 static void
2366 write_irix_state(printer_t *p) /* I - Printer to update */
2367 {
2368 char filename[1024]; /* Interface script filename */
2369 cups_file_t *fp; /* Interface script file */
2370 int tag; /* Status tag value */
2371
2372
2373 if (p)
2374 {
2375 /*
2376 * The POD status file is needed for the printstatus window to
2377 * provide the current status of the printer.
2378 */
2379
2380 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
2381
2382 if (p->type & CUPS_PRINTER_CLASS)
2383 unlink(filename);
2384 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2385 {
2386 cupsFilePrintf(fp, "Operational Status | %s\n",
2387 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
2388 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
2389 "Faulted");
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);
2396
2397 cupsFileClose(fp);
2398
2399 chmod(filename, 0664);
2400 chown(filename, User, Group);
2401 }
2402
2403 /*
2404 * The activeicons file is needed to provide desktop icons for printers:
2405 *
2406 * [ quoted from /usr/lib/print/tagit ]
2407 *
2408 * --- Type of printer tags (base values)
2409 *
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
2418 *
2419 * --- Printer state modifiers for local printers
2420 *
2421 * Idle=0 # 0x0
2422 * Busy=1 # 0x1
2423 * Faulted=2 # 0x2
2424 * Unknown=3 # 0x3 (Faulted due to unknown reason)
2425 *
2426 * --- Printer state modifiers for network printers
2427 *
2428 * NetIdle=8 # 0x8
2429 * NetBusy=9 # 0x9
2430 * NetFaulted=10 # 0xA
2431 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
2432 */
2433
2434 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
2435
2436 if (p->type & CUPS_PRINTER_CLASS)
2437 unlink(filename);
2438 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2439 {
2440 if (p->type & CUPS_PRINTER_COLOR)
2441 tag = 66240;
2442 else
2443 tag = 66272;
2444
2445 if (p->type & CUPS_PRINTER_REMOTE)
2446 tag |= 8;
2447
2448 if (p->state == IPP_PRINTER_PROCESSING)
2449 tag |= 1;
2450
2451 else if (p->state == IPP_PRINTER_STOPPED)
2452 tag |= 2;
2453
2454 cupsFilePuts(fp, "#!/bin/sh\n");
2455 cupsFilePrintf(fp, "#Tag %d\n", tag);
2456
2457 cupsFileClose(fp);
2458
2459 chmod(filename, 0755);
2460 chown(filename, User, Group);
2461 }
2462 }
2463
2464 /*
2465 * The default file is needed by the printers window to show
2466 * the default printer.
2467 */
2468
2469 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
2470
2471 if (DefaultPrinter != NULL)
2472 {
2473 if ((fp = cupsFileOpen(filename, "w")) != NULL)
2474 {
2475 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
2476
2477 cupsFileClose(fp);
2478
2479 chmod(filename, 0644);
2480 chown(filename, User, Group);
2481 }
2482 }
2483 else
2484 unlink(filename);
2485 }
2486 #endif /* __sgi */
2487
2488
2489 /*
2490 * End of "$Id: printers.c,v 1.93.2.67 2004/07/02 22:15:51 mike Exp $".
2491 */