17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5437427fbSps * Common Development and Distribution License (the "License").
6437427fbSps * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
227c478bd9Sstevel@tonic-gate /* All Rights Reserved */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
26437427fbSps * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
277c478bd9Sstevel@tonic-gate * Use is subject to license terms.
28*48bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <setjmp.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include "lp.h"
417c478bd9Sstevel@tonic-gate #include "msgs.h"
427c478bd9Sstevel@tonic-gate #include "printers.h"
437c478bd9Sstevel@tonic-gate #include "requests.h"
447c478bd9Sstevel@tonic-gate #include "form.h"
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define WHO_AM_I I_AM_LPADMIN
477c478bd9Sstevel@tonic-gate #include "oam.h"
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #include "lpadmin.h"
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate extern void mount_unmount();
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate extern short printer_status;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate extern char *cur_pwheel,
577c478bd9Sstevel@tonic-gate *disable_reason,
587c478bd9Sstevel@tonic-gate *reject_reason;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate extern FORM formbuf;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static int again();
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate static void disable(),
657c478bd9Sstevel@tonic-gate enable(),
667c478bd9Sstevel@tonic-gate accept(),
677c478bd9Sstevel@tonic-gate reject(),
687c478bd9Sstevel@tonic-gate cancel(),
697c478bd9Sstevel@tonic-gate sigpipe(),
707c478bd9Sstevel@tonic-gate sigother();
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static jmp_buf cleanup_env,
737c478bd9Sstevel@tonic-gate pipe_env;
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /**
767c478bd9Sstevel@tonic-gate ** do_align() - SET UP PRINTER TO PRINT ALIGNMENT PATTERNS
777c478bd9Sstevel@tonic-gate **/
787c478bd9Sstevel@tonic-gate
do_align(printer,form,pwheel)797c478bd9Sstevel@tonic-gate int do_align (printer, form, pwheel)
807c478bd9Sstevel@tonic-gate char *printer,
817c478bd9Sstevel@tonic-gate *form,
827c478bd9Sstevel@tonic-gate *pwheel;
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate short status;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate char *req_id = 0,
877c478bd9Sstevel@tonic-gate *file_prefix,
887c478bd9Sstevel@tonic-gate *rfile,
897c478bd9Sstevel@tonic-gate *fifo,
907c478bd9Sstevel@tonic-gate buffer[MSGMAX];
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate long printer_chk;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate int try;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate FILE *align_fp,
977c478bd9Sstevel@tonic-gate *fifo_fp;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate REQUEST req;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate void (*old_sighup)(),
1027c478bd9Sstevel@tonic-gate (*old_sigint)(),
1037c478bd9Sstevel@tonic-gate (*old_sigquit)(),
1047c478bd9Sstevel@tonic-gate (*old_sigterm)();
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * Having reached this point means we've already fetched
1097c478bd9Sstevel@tonic-gate * the form definition. Now get the alignment pattern.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate if (getform(form, (FORM *)0, (FALERT *)0, &align_fp) == -1) {
1127c478bd9Sstevel@tonic-gate LP_ERRMSG2 (ERROR, E_LP_GETFORM, form, PERROR);
1137c478bd9Sstevel@tonic-gate done (1);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate if (!align_fp) {
1167c478bd9Sstevel@tonic-gate LP_ERRMSG1 (WARNING, E_ADM_NOALIGN, form);
1177c478bd9Sstevel@tonic-gate return (0);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * Having reached this far also means we've already obtained
1227c478bd9Sstevel@tonic-gate * the printer status from the Spooler. We'll be changing the
1237c478bd9Sstevel@tonic-gate * status of the printer and queue and will have to restore
1247c478bd9Sstevel@tonic-gate * the disable/reject reasons.
1257c478bd9Sstevel@tonic-gate * NOTE: We can't restore the dates!
1267c478bd9Sstevel@tonic-gate */
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * Construct a request to print a ``file'' for copy. The
1317c478bd9Sstevel@tonic-gate * equivalent "lp" command (with a filename) would be:
1327c478bd9Sstevel@tonic-gate *
1337c478bd9Sstevel@tonic-gate * lp -p printer -H immediate -f form -T type -S charset -c -P 1-N
1347c478bd9Sstevel@tonic-gate *
1357c478bd9Sstevel@tonic-gate * "type", "charset", and "N" are derived from the form def'n.
1367c478bd9Sstevel@tonic-gate * This command would make us next to print ONCE THE FORM IS
1377c478bd9Sstevel@tonic-gate * MOUNTED.
1387c478bd9Sstevel@tonic-gate *
1397c478bd9Sstevel@tonic-gate * NOTE: Don't bother with the -S charset if it isn't mandatory,
1407c478bd9Sstevel@tonic-gate * so we won't get a rejection. Also, we use either the print
1417c478bd9Sstevel@tonic-gate * wheel given in the -S option or, lacking that, the currently
1427c478bd9Sstevel@tonic-gate * mounted print wheel. (The former WILL be mounted.) This also
1437c478bd9Sstevel@tonic-gate * avoids a rejection by the Spooler.
1447c478bd9Sstevel@tonic-gate */
1457c478bd9Sstevel@tonic-gate req.copies = 1;
1467c478bd9Sstevel@tonic-gate req.destination = printer;
1477c478bd9Sstevel@tonic-gate /* req.file_list = 0; This is done later. */
1487c478bd9Sstevel@tonic-gate req.form = form;
1497c478bd9Sstevel@tonic-gate req.actions = ACT_IMMEDIATE | ACT_FAST;
1507c478bd9Sstevel@tonic-gate req.alert = 0;
1517c478bd9Sstevel@tonic-gate req.options = "nobanner";
1527c478bd9Sstevel@tonic-gate req.priority = 20; /* it doesn't matter */
1537c478bd9Sstevel@tonic-gate sprintf ((req.pages = "1-999999")+2, "%d", formbuf.np);
1547c478bd9Sstevel@tonic-gate req.charset = NAME_ANY; /* Don't restrict the request */
1557c478bd9Sstevel@tonic-gate req.modes = 0;
1567c478bd9Sstevel@tonic-gate req.title = "Aligning Form";
1577c478bd9Sstevel@tonic-gate req.input_type = formbuf.conttype;
1587c478bd9Sstevel@tonic-gate req.user = getname();
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * The following code is sensitive to interrupts: We must
1637c478bd9Sstevel@tonic-gate * catch interrupts so to restore the printer to its original
1647c478bd9Sstevel@tonic-gate * state, but if we get interrupted while receiving a message
1657c478bd9Sstevel@tonic-gate * from the Spooler, we can't issue additional messages because
1667c478bd9Sstevel@tonic-gate * the old responses still in the response queue will confuse us.
1677c478bd9Sstevel@tonic-gate * Thus while sending/receiving a message we ignore signals.
1687c478bd9Sstevel@tonic-gate */
1697c478bd9Sstevel@tonic-gate if (setjmp(cleanup_env) != 0)
1707c478bd9Sstevel@tonic-gate done (1);
1717c478bd9Sstevel@tonic-gate trap_signals (); /* make sure we've done this once */
1727c478bd9Sstevel@tonic-gate old_sighup = signal(SIGHUP, sigother);
1737c478bd9Sstevel@tonic-gate old_sigint = signal(SIGINT, sigother);
1747c478bd9Sstevel@tonic-gate old_sigquit = signal(SIGQUIT, sigother);
1757c478bd9Sstevel@tonic-gate old_sigterm = signal(SIGTERM, sigother);
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate * We'll try the following twice, first with the page list
1797c478bd9Sstevel@tonic-gate * set as above. If the request gets refused because there's
1807c478bd9Sstevel@tonic-gate * no filter to convert the content, we'll try again without
1817c478bd9Sstevel@tonic-gate * the page list. I don't think the number-of-pages-in-a-form
1827c478bd9Sstevel@tonic-gate * feature is likely to be used much, so why hassle the
1837c478bd9Sstevel@tonic-gate * administrator?
1847c478bd9Sstevel@tonic-gate #if defined(WARN_OF_TOO_MANY_LINES)
185*48bbca81SDaniel Hoffman * However, do warn them.
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate try = 0;
1907c478bd9Sstevel@tonic-gate Again: try++;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate * Have the Spooler allocate a request file and another file
1947c478bd9Sstevel@tonic-gate * for our use. We'll delete the other file and recreate it
1957c478bd9Sstevel@tonic-gate * as a FIFO. We can do this because "lpadmin" can only be run
1967c478bd9Sstevel@tonic-gate * (successfully) by an administrator. This is the key to what
1977c478bd9Sstevel@tonic-gate * we're doing! We are submitting a named pipe (FIFO) for
1987c478bd9Sstevel@tonic-gate * printing, which gives us a connection to the printer
1997c478bd9Sstevel@tonic-gate * through any filters needed!
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate BEGIN_CRITICAL
2037c478bd9Sstevel@tonic-gate send_message (S_ALLOC_FILES, 2);
2047c478bd9Sstevel@tonic-gate if (mrecv(buffer, MSGMAX) != R_ALLOC_FILES) {
2057c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_LP_MRECV);
2067c478bd9Sstevel@tonic-gate done (1);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate END_CRITICAL
2097c478bd9Sstevel@tonic-gate (void)getmessage (buffer, R_ALLOC_FILES, &status, &file_prefix);
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate switch (status) {
2127c478bd9Sstevel@tonic-gate case MOK:
2137c478bd9Sstevel@tonic-gate break;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate case MNOMEM:
2167c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_LP_MNOMEM);
2177c478bd9Sstevel@tonic-gate done (1);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate if (!(rfile = malloc((unsigned int)strlen(file_prefix) + 2 + 1))) {
2217c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_LP_MALLOC);
2227c478bd9Sstevel@tonic-gate done (1);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate sprintf (rfile, "%s-1", file_prefix);
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate if (!(fifo = makepath(Lp_Temp, rfile, (char *)0))) {
2287c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_LP_MALLOC);
2297c478bd9Sstevel@tonic-gate done (1);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate req.file_list = 0;
2327c478bd9Sstevel@tonic-gate addlist (&req.file_list, fifo);
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate if (
2357c478bd9Sstevel@tonic-gate Unlink(fifo) == -1
2367c478bd9Sstevel@tonic-gate || Mknod(fifo, S_IFIFO | 0600, 0) == -1
2377c478bd9Sstevel@tonic-gate ) {
2387c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_ADM_NFIFO, PERROR);
2397c478bd9Sstevel@tonic-gate done (1);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate * In quick succession,
2447c478bd9Sstevel@tonic-gate *
2457c478bd9Sstevel@tonic-gate * - mount the form,
2467c478bd9Sstevel@tonic-gate * - disable the printer,
2477c478bd9Sstevel@tonic-gate * - make the Spooler accept requests (if need be),
2487c478bd9Sstevel@tonic-gate * - submit the request,
2497c478bd9Sstevel@tonic-gate * - make the Spooler reject requests (if need be).
2507c478bd9Sstevel@tonic-gate *
2517c478bd9Sstevel@tonic-gate * We want to minimize the window when another request can
2527c478bd9Sstevel@tonic-gate * be submitted ahead of ours. Though this window is small,
2537c478bd9Sstevel@tonic-gate * it is a flaw in our design. Disabling the printer will
2547c478bd9Sstevel@tonic-gate * help, because it will stop any request that is printing
2557c478bd9Sstevel@tonic-gate * (if the form is already mounted) and will prevent any other
2567c478bd9Sstevel@tonic-gate * request from printing. (We disable the printer AFTER trying
2577c478bd9Sstevel@tonic-gate * to mount the form, because we don't disable a printer for a
2587c478bd9Sstevel@tonic-gate * regular mount, and we'd like to make this mount APPEAR to
2597c478bd9Sstevel@tonic-gate * be as similar as possible.)
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate if (try == 1) {
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate mount_unmount (S_MOUNT, printer, NB(form), NB(pwheel));
2657c478bd9Sstevel@tonic-gate /* This will die if the mount fails, leaving */
2667c478bd9Sstevel@tonic-gate /* the Spooler to clean up our files. */
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate if (!(printer_status & PS_DISABLED))
2697c478bd9Sstevel@tonic-gate disable (printer, CUZ_MOUNTING, 0);
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate if (printer_status & PS_REJECTED)
2727c478bd9Sstevel@tonic-gate accept (printer);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate if (setjmp(cleanup_env) != 0) {
2757c478bd9Sstevel@tonic-gate if (printer_status & PS_DISABLED)
2767c478bd9Sstevel@tonic-gate disable (printer, disable_reason, 1);
2777c478bd9Sstevel@tonic-gate if (printer_status & PS_REJECTED)
2787c478bd9Sstevel@tonic-gate reject (printer, reject_reason);
2797c478bd9Sstevel@tonic-gate if (req_id && *req_id)
2807c478bd9Sstevel@tonic-gate cancel (req_id);
2817c478bd9Sstevel@tonic-gate done (1);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate sprintf (rfile, "%s-0", file_prefix);
2867c478bd9Sstevel@tonic-gate if (putrequest(rfile, &req) == -1) {
2877c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_PUTREQUEST, PERROR);
2887c478bd9Sstevel@tonic-gate goto Done;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate BEGIN_CRITICAL
2917c478bd9Sstevel@tonic-gate send_message (S_PRINT_REQUEST, rfile);
2927c478bd9Sstevel@tonic-gate if (mrecv(buffer, MSGMAX) != R_PRINT_REQUEST) {
2937c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_LP_MRECV);
2947c478bd9Sstevel@tonic-gate done (1);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate END_CRITICAL
2977c478bd9Sstevel@tonic-gate (void)getmessage (buffer, R_PRINT_REQUEST, &status, &req_id, &printer_chk);
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate switch (status) {
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate case MNOFILTER:
3027c478bd9Sstevel@tonic-gate if (try == 1) {
3037c478bd9Sstevel@tonic-gate req.pages = 0;
3047c478bd9Sstevel@tonic-gate goto Again;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_ADM_NFILTER);
3077c478bd9Sstevel@tonic-gate goto Done;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate case MOK:
3107c478bd9Sstevel@tonic-gate #if defined(WARN_OF_TOO_MANY_LINES)
3117c478bd9Sstevel@tonic-gate if (!req.pages)
3127c478bd9Sstevel@tonic-gate LP_ERRMSG1 (WARNING, E_ADM_NPAGES, formbuf.np);
3137c478bd9Sstevel@tonic-gate #endif
3147c478bd9Sstevel@tonic-gate break;
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate case MERRDEST:
3177c478bd9Sstevel@tonic-gate accept (printer); /* someone snuck a reject in! */
3187c478bd9Sstevel@tonic-gate goto Again;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate case MNOMEM:
3217c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_LP_MNOMEM);
3227c478bd9Sstevel@tonic-gate goto Done;
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate case MNODEST:
3257c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_PGONE, printer);
3267c478bd9Sstevel@tonic-gate goto Done;
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate case MNOOPEN: /* not quite, but close */
3297c478bd9Sstevel@tonic-gate LP_ERRMSG (ERROR, E_ADM_ERRDEST);
3307c478bd9Sstevel@tonic-gate goto Done;
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate case MDENYDEST:
3337c478bd9Sstevel@tonic-gate if (printer_chk) {
3347c478bd9Sstevel@tonic-gate char reason[1024],
3357c478bd9Sstevel@tonic-gate *cp = reason;
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate if (printer_chk & PCK_TYPE)
3387c478bd9Sstevel@tonic-gate cp += sprintf(cp, "printer type, ");
3397c478bd9Sstevel@tonic-gate if (printer_chk & PCK_CHARSET)
3407c478bd9Sstevel@tonic-gate cp += sprintf(cp, "character set, ");
3417c478bd9Sstevel@tonic-gate if (printer_chk & PCK_CPI)
3427c478bd9Sstevel@tonic-gate cp += sprintf(cp, "character pitch, ");
3437c478bd9Sstevel@tonic-gate if (printer_chk & PCK_LPI)
3447c478bd9Sstevel@tonic-gate cp += sprintf(cp, "line pitch, ");
3457c478bd9Sstevel@tonic-gate if (printer_chk & PCK_WIDTH)
3467c478bd9Sstevel@tonic-gate cp += sprintf(cp, "page width, ");
3477c478bd9Sstevel@tonic-gate if (printer_chk & PCK_LENGTH)
3487c478bd9Sstevel@tonic-gate cp += sprintf(cp, "page length, ");
3497c478bd9Sstevel@tonic-gate if (printer_chk & PCK_BANNER)
3507c478bd9Sstevel@tonic-gate cp += sprintf(cp, "nobanner, ");
3517c478bd9Sstevel@tonic-gate cp[-2] = 0;
3527c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_PTRCHK, reason);
3537c478bd9Sstevel@tonic-gate goto Done;
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate /*fall through*/
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate case MUNKNOWN:
3587c478bd9Sstevel@tonic-gate case MNOMEDIA:
3597c478bd9Sstevel@tonic-gate case MDENYMEDIA:
3607c478bd9Sstevel@tonic-gate case MNOMOUNT:
3617c478bd9Sstevel@tonic-gate case MNOSPACE:
3627c478bd9Sstevel@tonic-gate case MNOPERM:
3637c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_BADSTATUS, status);
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate Done: if (!(printer_status & PS_DISABLED))
3667c478bd9Sstevel@tonic-gate enable (printer);
3677c478bd9Sstevel@tonic-gate if (printer_status & PS_REJECTED)
3687c478bd9Sstevel@tonic-gate reject (printer, reject_reason);
3697c478bd9Sstevel@tonic-gate done (1);
3707c478bd9Sstevel@tonic-gate /*NOTREACHED*/
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate if (printer_status & PS_REJECTED)
3747c478bd9Sstevel@tonic-gate reject (printer, reject_reason);
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate * Enable printing, to start the interface program going.
3787c478bd9Sstevel@tonic-gate * Because of our precautions above, our request SHOULD be
3797c478bd9Sstevel@tonic-gate * the one that prints!
3807c478bd9Sstevel@tonic-gate */
3817c478bd9Sstevel@tonic-gate enable (printer);
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate * Open the FIFO. One problem: This will hang until the
3857c478bd9Sstevel@tonic-gate * interface program opens the other end!!
3867c478bd9Sstevel@tonic-gate */
3877c478bd9Sstevel@tonic-gate if (!(fifo_fp = fopen(fifo, "w"))) {
3887c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_ADM_NFIFO, PERROR);
3897c478bd9Sstevel@tonic-gate done (1);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate * Loop, dumping the ENTIRE alignment pattern to the FIFO
3947c478bd9Sstevel@tonic-gate * each time. SIGPIPE probably means the printer faulted.
3957c478bd9Sstevel@tonic-gate */
3967c478bd9Sstevel@tonic-gate if (setjmp(pipe_env) == 0) {
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate * Don't send a form feed after the last copy, since
3997c478bd9Sstevel@tonic-gate * the interface program does that. To implement this,
4007c478bd9Sstevel@tonic-gate * we send the form feed BEFORE the alignment pattern;
4017c478bd9Sstevel@tonic-gate * this way we can simply not send it the first time.
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate char * ff = 0;
4047c478bd9Sstevel@tonic-gate char * ff_before = 0;
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate * If we'll be inserting page breaks between alignment
4087c478bd9Sstevel@tonic-gate * patterns, look up the control sequence for this.
4097c478bd9Sstevel@tonic-gate *
4107c478bd9Sstevel@tonic-gate * MORE: We currently don't have the smarts to figure out
4117c478bd9Sstevel@tonic-gate * WHICH printer type the Spooler will pick; we would need
4127c478bd9Sstevel@tonic-gate * to steal some of its code for that (see pickfilter.c)
4137c478bd9Sstevel@tonic-gate * The best we do so far is use the alignment pattern's
4147c478bd9Sstevel@tonic-gate * content type, if known.
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate if (filebreak) {
4177c478bd9Sstevel@tonic-gate if (
4187c478bd9Sstevel@tonic-gate formbuf.conttype
4197c478bd9Sstevel@tonic-gate && searchlist_with_terminfo(
4207c478bd9Sstevel@tonic-gate formbuf.conttype,
4217c478bd9Sstevel@tonic-gate T /* having "filebreak" => OK */
4227c478bd9Sstevel@tonic-gate )
4237c478bd9Sstevel@tonic-gate )
4247c478bd9Sstevel@tonic-gate tidbit (formbuf.conttype, "ff", &ff);
4257c478bd9Sstevel@tonic-gate else
4267c478bd9Sstevel@tonic-gate tidbit (*T, "ff", &ff);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate signal (SIGPIPE, sigpipe);
4307c478bd9Sstevel@tonic-gate do {
4317c478bd9Sstevel@tonic-gate register int n;
4327c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
4337c478bd9Sstevel@tonic-gate
4347c478bd9Sstevel@tonic-gate if (ff_before && *ff_before)
4357c478bd9Sstevel@tonic-gate fputs (ff_before, fifo_fp);
4367c478bd9Sstevel@tonic-gate ff_before = ff;
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate rewind (align_fp);
4397c478bd9Sstevel@tonic-gate while ((n = fread(buf, 1, BUFSIZ, align_fp)) > 0)
4407c478bd9Sstevel@tonic-gate fwrite (buf, 1, n, fifo_fp);
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate fflush (fifo_fp);
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate } while (again());
4457c478bd9Sstevel@tonic-gate fclose (align_fp);
4467c478bd9Sstevel@tonic-gate signal (SIGPIPE, SIG_DFL);
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate } else {
4497c478bd9Sstevel@tonic-gate cancel (req_id);
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate #define P(X) printf (X)
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate P("We were interrupted while printing the alignment pattern;\n");
4547c478bd9Sstevel@tonic-gate P("check the printer. The form is mounted, so you will have to\n");
4557c478bd9Sstevel@tonic-gate P("unmount it if you need to print more alignment patterns later.\n");
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * Disable the printer, if needed, and close the FIFO.
4607c478bd9Sstevel@tonic-gate * Use the wait version of the disable, so our request isn't
4617c478bd9Sstevel@tonic-gate * stopped, and do it before closing the FIFO, so another request
4627c478bd9Sstevel@tonic-gate * can't start printing if it isn't supposed to.
4637c478bd9Sstevel@tonic-gate */
4647c478bd9Sstevel@tonic-gate if (printer_status & PS_DISABLED)
4657c478bd9Sstevel@tonic-gate disable (printer, disable_reason, 1);
4667c478bd9Sstevel@tonic-gate fclose (fifo_fp);
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate signal (SIGHUP, old_sighup);
4697c478bd9Sstevel@tonic-gate signal (SIGINT, old_sigint);
4707c478bd9Sstevel@tonic-gate signal (SIGQUIT, old_sigquit);
4717c478bd9Sstevel@tonic-gate signal (SIGTERM, old_sigterm);
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate return (1);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate /**
4777c478bd9Sstevel@tonic-gate ** accept() - MAKE PRINTER ACCEPT REQUESTS
4787c478bd9Sstevel@tonic-gate **/
4797c478bd9Sstevel@tonic-gate
accept(printer)4807c478bd9Sstevel@tonic-gate static void accept (printer)
4817c478bd9Sstevel@tonic-gate char *printer;
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate int rc;
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate BEGIN_CRITICAL
4867c478bd9Sstevel@tonic-gate send_message (S_ACCEPT_DEST, printer);
4877c478bd9Sstevel@tonic-gate rc = output(R_ACCEPT_DEST);
4887c478bd9Sstevel@tonic-gate END_CRITICAL
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate switch (rc) {
4917c478bd9Sstevel@tonic-gate case MOK:
4927c478bd9Sstevel@tonic-gate case MERRDEST: /* someone may have snuck in an accept */
4937c478bd9Sstevel@tonic-gate break;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate case MNODEST: /* make up your mind, Spooler! */
4967c478bd9Sstevel@tonic-gate case MNOPERM: /* taken care of up front */
4977c478bd9Sstevel@tonic-gate default:
4987c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_BADSTATUS, rc);
4997c478bd9Sstevel@tonic-gate done (1);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate return;
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate /**
5057c478bd9Sstevel@tonic-gate ** reject() - MAKE PRINTER REJECT REQUESTS
5067c478bd9Sstevel@tonic-gate **/
5077c478bd9Sstevel@tonic-gate
reject(printer,reason)5087c478bd9Sstevel@tonic-gate static void reject (printer, reason)
5097c478bd9Sstevel@tonic-gate char *printer,
5107c478bd9Sstevel@tonic-gate *reason;
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate int rc;
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate BEGIN_CRITICAL
5157c478bd9Sstevel@tonic-gate send_message (S_REJECT_DEST, printer, reason);
5167c478bd9Sstevel@tonic-gate rc = output(R_REJECT_DEST);
5177c478bd9Sstevel@tonic-gate END_CRITICAL
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate switch (rc) {
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate case MOK:
5227c478bd9Sstevel@tonic-gate case MERRDEST: /* someone may have snuck in a reject */
5237c478bd9Sstevel@tonic-gate break;
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate case MNODEST: /* make up your mind, Spooler! */
5267c478bd9Sstevel@tonic-gate case MNOPERM: /* taken care of up front */
5277c478bd9Sstevel@tonic-gate default:
5287c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_BADSTATUS, rc);
5297c478bd9Sstevel@tonic-gate done (1);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate return;
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate /**
5357c478bd9Sstevel@tonic-gate ** enable() - ENABLE THE PRINTER
5367c478bd9Sstevel@tonic-gate **/
5377c478bd9Sstevel@tonic-gate
enable(printer)5387c478bd9Sstevel@tonic-gate static void enable (printer)
5397c478bd9Sstevel@tonic-gate char *printer;
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate int rc;
5427c478bd9Sstevel@tonic-gate
5437c478bd9Sstevel@tonic-gate BEGIN_CRITICAL
5447c478bd9Sstevel@tonic-gate send_message (S_ENABLE_DEST, printer);
5457c478bd9Sstevel@tonic-gate rc = output(R_ENABLE_DEST);
5467c478bd9Sstevel@tonic-gate END_CRITICAL
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate switch (rc) {
5497c478bd9Sstevel@tonic-gate case MOK:
5507c478bd9Sstevel@tonic-gate case MERRDEST: /* someone may have snuck in an enable */
5517c478bd9Sstevel@tonic-gate break;
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate case MNODEST: /* make up your mind, Spooler! */
5547c478bd9Sstevel@tonic-gate case MNOPERM: /* taken care of up front */
5557c478bd9Sstevel@tonic-gate default:
5567c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_BADSTATUS, rc);
5577c478bd9Sstevel@tonic-gate done (1);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate return;
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /**
5637c478bd9Sstevel@tonic-gate ** disable() - DISABLE THE PRINTER
5647c478bd9Sstevel@tonic-gate **/
5657c478bd9Sstevel@tonic-gate
disable(printer,reason,when)5667c478bd9Sstevel@tonic-gate static void disable (printer, reason, when)
5677c478bd9Sstevel@tonic-gate char *printer,
5687c478bd9Sstevel@tonic-gate *reason;
5697c478bd9Sstevel@tonic-gate int when;
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate int rc;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate BEGIN_CRITICAL
5747c478bd9Sstevel@tonic-gate send_message (S_DISABLE_DEST, printer, reason, when);
5757c478bd9Sstevel@tonic-gate rc = output(R_DISABLE_DEST);
5767c478bd9Sstevel@tonic-gate END_CRITICAL
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate switch (rc) {
5797c478bd9Sstevel@tonic-gate case MOK:
5807c478bd9Sstevel@tonic-gate case MERRDEST: /* someone may have snuck in a disable */
5817c478bd9Sstevel@tonic-gate break;
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate case MNODEST: /* make up your mind, Spooler! */
5847c478bd9Sstevel@tonic-gate case MNOPERM: /* taken care of up front */
5857c478bd9Sstevel@tonic-gate default:
5867c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_BADSTATUS, rc);
5877c478bd9Sstevel@tonic-gate done (1);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate return;
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate /**
5937c478bd9Sstevel@tonic-gate ** cancel() - MAKE PRINTER ACCEPT REQUESTS
5947c478bd9Sstevel@tonic-gate **/
5957c478bd9Sstevel@tonic-gate
cancel(req_id)5967c478bd9Sstevel@tonic-gate static void cancel (req_id)
5977c478bd9Sstevel@tonic-gate char *req_id;
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate int rc;
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate BEGIN_CRITICAL
6027c478bd9Sstevel@tonic-gate send_message (S_CANCEL_REQUEST, req_id);
6037c478bd9Sstevel@tonic-gate rc = output(R_CANCEL_REQUEST);
6047c478bd9Sstevel@tonic-gate END_CRITICAL
6057c478bd9Sstevel@tonic-gate
6067c478bd9Sstevel@tonic-gate switch (rc) {
6077c478bd9Sstevel@tonic-gate case MOK:
6087c478bd9Sstevel@tonic-gate case MUNKNOWN:
6097c478bd9Sstevel@tonic-gate case M2LATE:
6107c478bd9Sstevel@tonic-gate break;
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate case MNOPERM:
6137c478bd9Sstevel@tonic-gate default:
6147c478bd9Sstevel@tonic-gate LP_ERRMSG1 (ERROR, E_LP_BADSTATUS, rc);
6157c478bd9Sstevel@tonic-gate done (1);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate return;
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate /**
6217c478bd9Sstevel@tonic-gate ** again()
6227c478bd9Sstevel@tonic-gate **/
6237c478bd9Sstevel@tonic-gate
again()6247c478bd9Sstevel@tonic-gate static int again ()
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate char answer[BUFSIZ];
6277c478bd9Sstevel@tonic-gate
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate for (;;) {
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate printf (
6327c478bd9Sstevel@tonic-gate gettext("Press return to print an alignment pattern [q to quit]: ")
6337c478bd9Sstevel@tonic-gate );
6347c478bd9Sstevel@tonic-gate
635437427fbSps if (!fgets(answer, sizeof (answer), stdin))
636437427fbSps return (0);
637437427fbSps
638437427fbSps answer[strlen(answer) -1] = '\0';
639437427fbSps
6407c478bd9Sstevel@tonic-gate if (
641437427fbSps STREQU(answer, "q")
6427c478bd9Sstevel@tonic-gate || STREQU(answer, "n")
6437c478bd9Sstevel@tonic-gate || STREQU(answer, "no")
6447c478bd9Sstevel@tonic-gate )
6457c478bd9Sstevel@tonic-gate return (0);
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate else if (
6487c478bd9Sstevel@tonic-gate !*answer
6497c478bd9Sstevel@tonic-gate || STREQU(answer, "y")
6507c478bd9Sstevel@tonic-gate || STREQU(answer, "yes")
6517c478bd9Sstevel@tonic-gate )
6527c478bd9Sstevel@tonic-gate return (1);
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate printf (gettext("Sorry?\n"));
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate /**
6597c478bd9Sstevel@tonic-gate ** sigpipe()
6607c478bd9Sstevel@tonic-gate ** sigother()
6617c478bd9Sstevel@tonic-gate **/
6627c478bd9Sstevel@tonic-gate
sigpipe()6637c478bd9Sstevel@tonic-gate static void sigpipe ()
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate signal (SIGPIPE, SIG_IGN);
6667c478bd9Sstevel@tonic-gate longjmp (pipe_env, 1);
6677c478bd9Sstevel@tonic-gate /*NOTREACHED*/
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate
sigother(sig)6707c478bd9Sstevel@tonic-gate static void sigother (sig)
6717c478bd9Sstevel@tonic-gate int sig;
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate signal (sig, SIG_IGN);
6747c478bd9Sstevel@tonic-gate longjmp (cleanup_env, 1);
6757c478bd9Sstevel@tonic-gate /*NOTREACHED*/
6767c478bd9Sstevel@tonic-gate }
677