Merge branch 'vendor/OPENSSL'
[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  */
34
35 #include <sys/types.h>
36 #include <sys/uio.h>
37 #include <paths.h>
38 #include <signal.h>
39 #include <unistd.h>
40
41 #include "doscmd.h"
42
43 static int      lpt_fd[4] = { -1, -1, -1, -1, };
44 static FILE     *lpt_file[4] = { 0, 0, 0, 0};
45 static int      direct[4] = { 0, 0, 0, 0};
46 static char     *queue[4] = { 0, 0, 0, 0};
47 static int      timeout[4] = { 30, 30, 30, 30 };
48 static int      last_poll[4] = { 0, 0, 0, 0};
49 static int      last_count[4] = { 0, 0, 0, 0};
50 static int      current_count[4] = { 0, 0, 0, 0};
51
52 static void open_printer(int printer);
53
54 void
55 int17(regcontext_t *REGS)
56 {
57     int fd;
58     u_char c;
59
60     switch (R_AH) {
61
62     case 0x00:
63         reset_poll();
64         
65         fd = lpt_fd[R_DX];
66         if (fd == -1) {
67             open_printer(R_DX);
68             fd = lpt_fd[R_DX];
69         }
70         if (fd >= 0) {
71             c = R_AL;
72             write(fd, &c, 1);
73         }
74         R_AH = 0x90;            /* printed selected */
75         current_count[R_DX]++;
76         break;
77
78     case 0x01:
79     case 0x02:
80         R_AH = 0x90;
81         break;
82
83     default:
84         unknown_int2(0x17, R_AH, REGS);
85         break;
86     }
87 }
88
89 void 
90 lpt_poll(void)
91 {
92     int i;
93     int current;
94
95     current = time(0);
96
97     for(i=0; i < 4; i++) {
98         if (lpt_fd[i] < 0)
99             continue;
100
101         if (current - last_poll[i] < timeout[i])
102             continue;
103
104         last_poll[i] = current;
105
106         if (last_count[i] == current_count[i]) {
107             if (direct[i]) {
108                 debug(D_PRINTER, "Closing printer %d\n", i);
109                 close(lpt_fd[i]);
110             } else {
111                 debug(D_PRINTER, "Closing spool printer %d\n", i);
112                 pclose(lpt_file[i]);
113             }
114             lpt_fd[i] = -1;
115             lpt_file[i] = NULL;
116         }
117
118         last_count[i] = current_count[i];
119     }
120 }
121
122
123 static void
124 open_printer(int printer)
125 {
126     char        printer_name[80];
127     char        command[120];
128     int         fd;
129     FILE        *file;
130     char        *p;
131
132     /*
133      * if printer is direct then open output device.
134      */
135     if (direct[printer]) {
136         if ((p = queue[printer]) != NULL) {
137             if ((fd = open(p, O_WRONLY|O_APPEND|O_CREAT, 0666)) < 0) {
138                 perror(p);
139                 return;
140             }
141         } else {
142             sprintf(printer_name, "%slpt%d", _PATH_DEV, printer);
143             debug(D_PRINTER, "Opening device %s\n", printer_name);
144             if ((fd = open(printer_name, O_WRONLY)) < 0) {
145                 perror(printer_name);
146                 return;
147             }
148         }
149         lpt_fd[printer] = fd;
150         return;
151     }
152
153     /*
154      * If printer is a spooled device then open pipe to spooled device
155      */
156     if (queue[printer]) {
157         strncpy(printer_name, queue[printer], sizeof(printer_name));
158         printer_name[sizeof(printer_name) - 1] = '\0';
159     } else
160         strcpy(printer_name, "lp");
161
162     snprintf(command, sizeof(command), "lpr -P %s", printer_name);
163     debug(D_PRINTER, "opening pipe to %s\n", printer_name);
164
165     if ((file = popen(command, "w")) == NULL) {
166         perror(command);
167         return;
168     }
169     lpt_file[printer] = file;
170     lpt_fd[printer] = fileno(file);
171 }
172
173 void
174 printer_direct(int printer)
175 {
176     direct[printer] = 1;
177 }
178
179 void
180 printer_spool(int printer, char *print_queue)
181 {
182     queue[printer] = print_queue ? strdup(print_queue) : 0;
183 }
184
185 void
186 printer_timeout(int printer, char *time_out)
187 {
188     if (atoi(time_out) <= 0) {
189         fprintf(stderr, "Bad timeout value on lpt%d:\n", printer+1);
190         quit(1);
191     }
192     timeout[printer] = atoi(time_out);
193 }