Merge branch 'vendor/BINUTILS220' into bu220
[dragonfly.git] / usr.bin / doscmd / int17.c
1 /*
2  * Copyright (c) 1992, 1993, 1996
3  *      Berkeley Software Design, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Berkeley Software
16  *      Design, Inc.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      BSDI int17.c,v 2.2 1996/04/08 19:32:48 bostic Exp
31  *
32  * $FreeBSD: src/usr.bin/doscmd/int17.c,v 1.4.2.3 2002/04/25 11:04:51 tg Exp $
33  * $DragonFly: src/usr.bin/doscmd/int17.c,v 1.2 2003/06/17 04:29:26 dillon Exp $
34  */
35
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 #include <paths.h>
39 #include <signal.h>
40 #include <unistd.h>
41
42 #include "doscmd.h"
43
44 static int      lpt_fd[4] = { -1, -1, -1, -1, };
45 static FILE     *lpt_file[4] = { 0, 0, 0, 0};
46 static int      direct[4] = { 0, 0, 0, 0};
47 static char     *queue[4] = { 0, 0, 0, 0};
48 static int      timeout[4] = { 30, 30, 30, 30 };
49 static int      last_poll[4] = { 0, 0, 0, 0};
50 static int      last_count[4] = { 0, 0, 0, 0};
51 static int      current_count[4] = { 0, 0, 0, 0};
52
53 static void open_printer(int printer);
54
55 void
56 int17(regcontext_t *REGS)
57 {
58     int fd;
59     u_char c;
60
61     switch (R_AH) {
62
63     case 0x00:
64         reset_poll();
65         
66         fd = lpt_fd[R_DX];
67         if (fd == -1) {
68             open_printer(R_DX);
69             fd = lpt_fd[R_DX];
70         }
71         if (fd >= 0) {
72             c = R_AL;
73             write(fd, &c, 1);
74         }
75         R_AH = 0x90;            /* printed selected */
76         current_count[R_DX]++;
77         break;
78
79     case 0x01:
80     case 0x02:
81         R_AH = 0x90;
82         break;
83
84     default:
85         unknown_int2(0x17, R_AH, REGS);
86         break;
87     }
88 }
89
90 void 
91 lpt_poll(void)
92 {
93     int i;
94     int current;
95
96     current = time(0);
97
98     for(i=0; i < 4; i++) {
99         if (lpt_fd[i] < 0)
100             continue;
101
102         if (current - last_poll[i] < timeout[i])
103             continue;
104
105         last_poll[i] = current;
106
107         if (last_count[i] == current_count[i]) {
108             if (direct[i]) {
109                 debug(D_PRINTER, "Closing printer %d\n", i);
110                 close(lpt_fd[i]);
111             } else {
112                 debug(D_PRINTER, "Closing spool printer %d\n", i);
113                 pclose(lpt_file[i]);
114             }
115             lpt_fd[i] = -1;
116             lpt_file[i] = 0;
117         }
118
119         last_count[i] = current_count[i];
120     }
121 }
122
123
124 static void
125 open_printer(int printer)
126 {
127     char        printer_name[80];
128     char        command[120];
129     int         fd;
130     FILE        *file;
131     char        *p;
132
133     /*
134      * if printer is direct then open output device.
135      */
136     if (direct[printer]) {
137         if ((p = queue[printer]) != 0) {
138             if ((fd = open(p, O_WRONLY|O_APPEND|O_CREAT, 0666)) < 0) {
139                 perror(p);
140                 return;
141             }
142         } else {
143             sprintf(printer_name, "%slpt%d", _PATH_DEV, printer);
144             debug(D_PRINTER, "Opening device %s\n", printer_name);
145             if ((fd = open(printer_name, O_WRONLY)) < 0) {
146                 perror(printer_name);
147                 return;
148             }
149         }
150         lpt_fd[printer] = fd;
151         return;
152     }
153
154     /*
155      * If printer is a spooled device then open pipe to spooled device
156      */
157     if (queue[printer]) {
158         strncpy(printer_name, queue[printer], sizeof(printer_name));
159         printer_name[sizeof(printer_name) - 1] = '\0';
160     } else
161         strcpy(printer_name, "lp");
162
163     snprintf(command, sizeof(command), "lpr -P %s", printer_name);
164     debug(D_PRINTER, "opening pipe to %s\n", printer_name);
165
166     if ((file = popen(command, "w")) == 0) {
167         perror(command);
168         return;
169     }
170     lpt_file[printer] = file;
171     lpt_fd[printer] = fileno(file);
172 }
173
174 void
175 printer_direct(int printer)
176 {
177     direct[printer] = 1;
178 }
179
180 void
181 printer_spool(int printer, char *print_queue)
182 {
183     queue[printer] = print_queue ? strdup(print_queue) : 0;
184 }
185
186 void
187 printer_timeout(int printer, char *time_out)
188 {
189     if (atoi(time_out) <= 0) {
190         fprintf(stderr, "Bad timeout value on lpt%d:\n", printer+1);
191         quit(1);
192     }
193     timeout[printer] = atoi(time_out);
194 }