[421b7d2] | 1 | /* prio.c |
---|
[d1b1380] | 2 | * Printer I/O routines for Survex printer drivers |
---|
| 3 | * Copyright (C) 1993-1997 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 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. |
---|
[846746e] | 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 |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 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 |
---|
[1b71c05] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[a420b49] | 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
[d1b1380] | 23 | |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <stdarg.h> |
---|
| 27 | |
---|
[d8f3e3b] | 28 | #include "filename.h" |
---|
| 29 | #include "message.h" |
---|
[d1b1380] | 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 */ |
---|
[3afeb897] | 36 | # if defined(__DJGPP__) && (__DJGPP__ >= 2) |
---|
| 37 | # define _NAIVE_DOS_REGS |
---|
[d1b1380] | 38 | # endif |
---|
| 39 | # include <dos.h> |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | static FILE *fhPrn; |
---|
| 43 | |
---|
[d8f3e3b] | 44 | #ifdef HAVE_POPEN |
---|
[a420b49] | 45 | static bool fPipeOpen = fFalse; |
---|
[d1b1380] | 46 | #endif |
---|
| 47 | |
---|
[a420b49] | 48 | extern void |
---|
| 49 | prio_open(const char *fnmPrn) |
---|
| 50 | { |
---|
[d8f3e3b] | 51 | #ifdef HAVE_POPEN |
---|
[a420b49] | 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; |
---|
[d1b1380] | 57 | return; |
---|
| 58 | } |
---|
| 59 | #endif |
---|
[a420b49] | 60 | fhPrn = safe_fopen(fnmPrn, "wb"); |
---|
[d1b1380] | 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; |
---|
[a420b49] | 65 | in.x.ax = 0x4400; /* get device info */ |
---|
| 66 | in.x.bx = fileno(fhPrn); |
---|
| 67 | intdos(&in, &out); |
---|
[d1b1380] | 68 | /* check call worked && file is a device */ |
---|
| 69 | if (!out.x.cflag && (out.h.dl & 0x80)) { |
---|
[421b7d2] | 70 | in.x.ax = 0x4401; /* set device info */ |
---|
| 71 | in.x.dx = out.h.dl | 0x20; /* force binary mode */ |
---|
| 72 | intdos(&in, &out); |
---|
[d1b1380] | 73 | } |
---|
| 74 | } |
---|
| 75 | #endif |
---|
| 76 | } |
---|
| 77 | |
---|
[a420b49] | 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 | { |
---|
[d8f3e3b] | 87 | #ifdef HAVE_POPEN |
---|
[d1b1380] | 88 | if (fPipeOpen) { |
---|
[a420b49] | 89 | /* pclose gives return code from program or -1, so only 0 means OK */ |
---|
[a07ba6d] | 90 | if (ferror(fhPrn) || pclose(fhPrn)) prio_writeerror(); |
---|
[d1b1380] | 91 | return; |
---|
| 92 | } |
---|
| 93 | #endif |
---|
[a07ba6d] | 94 | if (ferror(fhPrn) || fclose(fhPrn) == EOF) prio_writeerror(); |
---|
[d1b1380] | 95 | } |
---|
| 96 | |
---|
[a420b49] | 97 | extern void |
---|
| 98 | prio_putc(int ch) |
---|
| 99 | { |
---|
[1da3ccc] | 100 | /* putc() returns EOF on write error */ |
---|
| 101 | if (putc(ch, fhPrn) == EOF) fatalerror(87); |
---|
[d1b1380] | 102 | } |
---|
| 103 | |
---|
[a420b49] | 104 | extern void |
---|
| 105 | prio_printf(const char *format, ...) |
---|
| 106 | { |
---|
[d1b1380] | 107 | int result; |
---|
| 108 | va_list args; |
---|
[a420b49] | 109 | va_start(args, format); |
---|
| 110 | result = vfprintf(fhPrn, format, args); |
---|
[d1b1380] | 111 | va_end(args); |
---|
[a420b49] | 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(); |
---|
[d1b1380] | 119 | } |
---|
| 120 | |
---|
[a420b49] | 121 | extern void |
---|
| 122 | prio_putpstr(const pstr *p) |
---|
| 123 | { |
---|
[f4aec00] | 124 | prio_putbuf(p->str, p->len); |
---|
[d8f3e3b] | 125 | } |
---|
| 126 | |
---|
[a420b49] | 127 | extern void |
---|
| 128 | prio_putbuf(const void *buf, size_t len) |
---|
| 129 | { |
---|
[d1b1380] | 130 | /* fwrite() returns # of members successfuly written */ |
---|
[a420b49] | 131 | if (fwrite(buf, 1, len, fhPrn) != len) prio_writeerror(); |
---|
[d1b1380] | 132 | } |
---|