1 | /* prio.c |
---|
2 | * Printer I/O routines for Survex printer drivers |
---|
3 | * Copyright (C) 1993-1997 Olly Betts |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <stdio.h> |
---|
25 | #include <string.h> |
---|
26 | #include <stdarg.h> |
---|
27 | |
---|
28 | #include "filename.h" |
---|
29 | #include "message.h" |
---|
30 | #include "useful.h" |
---|
31 | #include "osdepend.h" |
---|
32 | #include "prio.h" |
---|
33 | |
---|
34 | #if (OS==MSDOS) |
---|
35 | /* Make DJGPP v2 use register struct-s compatible with v1 and BorlandC */ |
---|
36 | # if defined(__DJGPP__) && (__DJGPP__ >= 2) |
---|
37 | # define _NAIVE_DOS_REGS |
---|
38 | # endif |
---|
39 | # include <dos.h> |
---|
40 | #endif |
---|
41 | |
---|
42 | static FILE *fhPrn; |
---|
43 | |
---|
44 | #ifdef HAVE_POPEN |
---|
45 | static bool fPipeOpen = fFalse; |
---|
46 | #endif |
---|
47 | |
---|
48 | extern void |
---|
49 | prio_open(const char *fnmPrn) |
---|
50 | { |
---|
51 | #ifdef HAVE_POPEN |
---|
52 | if (*fnmPrn == '|') { |
---|
53 | fnmPrn++; /* skip '|' */ |
---|
54 | fhPrn = popen(fnmPrn, "w"); |
---|
55 | if (!fhPrn) fatalerror(/*Couldn't open pipe: `%s'*/17, fnmPrn); |
---|
56 | fPipeOpen = fTrue; |
---|
57 | return; |
---|
58 | } |
---|
59 | #endif |
---|
60 | fhPrn = safe_fopen(fnmPrn, "wb"); |
---|
61 | #if (OS==MSDOS) |
---|
62 | { /* For DOS, check if we're talking directly to a device, and force |
---|
63 | * "raw mode" if we are, so that ^Z ^S ^Q ^C don't get filtered */ |
---|
64 | union REGS in, out; |
---|
65 | in.x.ax = 0x4400; /* get device info */ |
---|
66 | in.x.bx = fileno(fhPrn); |
---|
67 | intdos(&in, &out); |
---|
68 | /* check call worked && file is a device */ |
---|
69 | if (!out.x.cflag && (out.h.dl & 0x80)) { |
---|
70 | in.x.ax = 0x4401; /* set device info */ |
---|
71 | in.x.dx = out.h.dl | 0x20; /* force binary mode */ |
---|
72 | intdos(&in, &out); |
---|
73 | } |
---|
74 | } |
---|
75 | #endif |
---|
76 | } |
---|
77 | |
---|
78 | static void |
---|
79 | prio_writeerror(void) |
---|
80 | { |
---|
81 | fatalerror(/*Error writing printer output*/87); |
---|
82 | } |
---|
83 | |
---|
84 | extern void |
---|
85 | prio_close(void) |
---|
86 | { |
---|
87 | #ifdef HAVE_POPEN |
---|
88 | if (fPipeOpen) { |
---|
89 | /* pclose gives return code from program or -1, so only 0 means OK */ |
---|
90 | if (ferror(fhPrn) || pclose(fhPrn)) prio_writeerror(); |
---|
91 | return; |
---|
92 | } |
---|
93 | #endif |
---|
94 | if (ferror(fhPrn) || fclose(fhPrn) == EOF) prio_writeerror(); |
---|
95 | } |
---|
96 | |
---|
97 | extern void |
---|
98 | prio_putc(int ch) |
---|
99 | { |
---|
100 | /* putc() returns EOF on write error */ |
---|
101 | if (putc(ch, fhPrn) == EOF) fatalerror(87); |
---|
102 | } |
---|
103 | |
---|
104 | extern void |
---|
105 | prio_printf(const char *format, ...) |
---|
106 | { |
---|
107 | int result; |
---|
108 | va_list args; |
---|
109 | va_start(args, format); |
---|
110 | result = vfprintf(fhPrn, format, args); |
---|
111 | va_end(args); |
---|
112 | if (result < 0) prio_writeerror(); |
---|
113 | } |
---|
114 | |
---|
115 | extern void |
---|
116 | prio_print(const char *str) |
---|
117 | { |
---|
118 | if (fputs(str, fhPrn) < 0) prio_writeerror(); |
---|
119 | } |
---|
120 | |
---|
121 | extern void |
---|
122 | prio_putpstr(const pstr *p) |
---|
123 | { |
---|
124 | prio_putbuf(p->str, p->len); |
---|
125 | } |
---|
126 | |
---|
127 | extern void |
---|
128 | prio_putbuf(const void *buf, size_t len) |
---|
129 | { |
---|
130 | /* fwrite() returns # of members successfuly written */ |
---|
131 | if (fwrite(buf, 1, len, fhPrn) != len) prio_writeerror(); |
---|
132 | } |
---|