Jspice3
mkhpplot.c
Go to the documentation of this file.
1 /*************************************************************************
2  MFB graphics and miscellaneous library for protected mode MSDOS.
3  Copyright (c) Stephen R. Whiteley 1992
4  Author: Stephen R. Whiteley
5  *************************************************************************/
6 
7 #include "mfb.h"
8 #include "mfbP.h"
9 #include <dos.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 
13 #ifdef __STDC__
14 static Bool dump(long,FILE*);
15 #else
16 static Bool dump();
17 #endif
18 extern char *malloc();
19 
20 static char *hmsg = "hit any key to continue";
21 static char hc_device[10] = {'p','r','n'};
22 static int hc_port;
23 
24 
25 Bool
27 
28 /* print the screen on the laserjet printer */
29 {
30  FILE *fp;
31  struct stat b;
32  int er;
33 
34 
35  fp = fopen("hpplot","wb");
36 
37  if (!fp) {
38  PrinterErrmsg("Error: Can't open output file.",hmsg);
39  (void)MFBGetchar();
40  return (true);
41  }
42 
43  if (rasterize(fp)) {
44  PrinterErrmsg("Error: Rasterization failed.",hmsg);
45  (void)MFBGetchar();
46  return (true);
47  }
48  fclose(fp);
49 
50  if (hc_port >= 0) {
51  while (PrinterPortStatus(hc_port)) {
52  PrinterErrmsg("Error: Printer is not accessible.",
53  "Hit any key to continue, q to abort:");
54  if ((char)MFBGetchar() == 'q')
55  return (true);
56  }
57  }
58  if (stat("hpplot",&b)) {
59  PrinterErrmsg("Error: File \"hpplot\" not found.\n",hmsg);
60  (void)MFBGetchar();
61  return (true);
62  }
63 
64  fp = fopen("hpplot","rb");
65 
66  if (!fp) {
67  PrinterErrmsg("Error: File \"hpplot\" not found.\n",hmsg);
68  (void)MFBGetchar();
69  return (true);
70  }
71  er = dump((long) b.st_size,fp);
72  fclose(fp);
73  return (er);
74 }
75 
76 
77 Bool
79 
80 /* create HP Laserjet plotfile of screen */
81 FILE *fp;
82 {
83  int i, j, k, kk, len, isdev = 0;
84  char *rgen;
85  union { unsigned short o[2]; long l; } p1;
86  char cbuf, *buf, *c;
87  unsigned char mask, chr;
88  union REGS r;
89  if (!fp) return (true);
90 
91  r.x.bx = fileno(fp);
92  r.x.ax = 0x4400;
93  int86(0x21,&r,&r);
94  i = r.x.dx;
95  if (i & 0x80) { /* output is a device */
96 
97  isdev = 1;
98  /* put driver in raw mode */
99  r.x.bx = fileno(fp);
100  r.x.dx = 0xa0;
101  r.x.ax = 0x4401;
102  int86(0x21,&r,&r);
103  }
104 
105  buf = malloc(pc.bytpline + 8);
106  if (!buf) {
107  PrinterErrmsg("Out of memory!",NULL);
108  return (true);
109  }
110 
111  if (pc.mfbMODE != 4)
112 
113  /* X cursor position 250
114  * Y cursor position 400
115  * resolution 100 dpi
116  * start raster graphics at current position
117  */
118 
119  fprintf(fp,"\033*p250X\033*p400Y\033*t100R\033*r1A");
120 
121  /* raster line header */
122  sprintf(buf,"\033*b%dW",pc.bytpline);
123  len = strlen(buf);
124 
125  if (pc.mfbMODE & 2) {
126  cbuf = inp(0x3cd);
127  cbuf >>= 4;
128  p1.l = 0L;
129  for (i = 0; i < pc.ysize; i++) {
130  c = buf + len;
131  mask = 0x80;
132  chr = 0;
133  for (j = 0; j < pc.xsize; j++) {
134  if (cbuf != p1.o[1])
135  outp(0x3cd,(cbuf = p1.o[1]) << 4);
136  if (*(pc.base+p1.o[0]))
137  chr |= mask;
138  mask = (mask >> 1) | (mask << 7);
139  if (mask & 0x80) {
140  *c++ = chr;
141  chr = 0;
142  }
143  p1.l++;
144  }
145  if (isdev) {
146  for (kk = 0; kk < pc.bytpline+len; kk++)
147  putc(buf[kk],fp);
148  }
149  else {
150  if (fwrite(buf,1,pc.bytpline+len,fp) != pc.bytpline+len) {
151  free(buf);
152  return (true);
153  }
154  }
155  }
156  if (pc.mfbMODE != 4)
157  /* end raster graphics, form feed */
158  fprintf(fp,"\033*rB\014");
159  fflush(fp);
160  free(buf);
161  return (false);
162  }
163 
164  rgen = pc.base;
165  for (i = 0; i < pc.ysize; i++) {
166  c = buf + len;
167  for (j = 0; j < pc.bytpline; j++) {
168  *c = '\0';
169  for (k = 0; k < 4; k++) {
170  outpw(0x3ce,k << 8 | 4);
171  *c |= *rgen;
172  }
173  rgen++;
174  c++;
175  }
176  if (isdev) {
177  for (kk = 0; kk < pc.bytpline+len; kk++)
178  putc(buf[kk],fp);
179  }
180  else {
181  if (fwrite(buf,1,pc.bytpline+len,fp) != pc.bytpline+len) {
182  free(buf);
183  return (true);
184  }
185  }
186  }
187  if (pc.mfbMODE != 4)
188  fprintf(fp,"\033*rB\014");
189  fflush(fp);
190  free(buf);
191  return (false);
192 }
193 
194 
195 void
196 PrinterErrmsg(msg1,msg2)
197 
198 char *msg1, *msg2;
199 {
200  int x,y,w,h;
201  int l,r,t,b;
202 
203  w = MFBInfo(FONTWIDTH);
204  h = MFBInfo(FONTHEIGHT);
205  x = MFBInfo(MAXX)/2;
206  y = MFBInfo(MAXY)/2;
207  l = x - 19*w;
208  r = x + 19*w;
209  t = y + 2*h;
210  b = y - 2*h;
211 
212  MFBSetColor(0);
213  MFBBox(l,b,r,t);
214  MFBSetColor(1);
215  MFBLine(l,b,r,b);
216  MFBDrawLineTo(r,t);
217  MFBDrawLineTo(l,t);
218  MFBDrawLineTo(l,b);
219  MFBSetColor(2);
220  if (msg1)
221  MFBText(msg1,l+w,t-2*h,0);
222  if (msg2)
223  MFBText(msg2,l+w,t-3*h,0);
224 }
225 
226 
227 Bool
229 int which;
230 {
231  int i;
232  union REGS r;
233 
234  r.x.ax = 0x0200;
235  r.x.dx = which;
236  int86(0x17,&r,&r);
237  i = r.x.ax;
238  i >>= 8;
239  /* bit 5 out of paper, bit 3 io error, bit 0 timed out */
240  if (i & 0x29) return (true);
241  return (false);
242 }
243 
244 
245 static Bool
246 dump(size,fp)
247 
248 /* dump size bytes from fp to parallel port */
249 long size;
250 FILE *fp;
251 {
252  int i;
253  unsigned char *c;
254  static char *buf;
255  int bsize = 8192;
256  FILE *op;
257  union REGS r;
258 
259  if (!buf) buf = malloc(bsize);
260  if (!buf) {
261  PrinterErrmsg("Error: Out of memory.",hmsg);
262  (void)MFBGetchar();
263  return (true);
264  }
265 
266  op = fopen(hc_device,"wb");
267  if (!op) {
268  sprintf(buf,"Error: Printer driver %s not found.",hc_device);
269  PrinterErrmsg(buf,hmsg);
270  (void)MFBGetchar();
271  return (true);
272  }
273  PrinterErrmsg("Printing ...",NULL);
274 
275  if (bsize > size) bsize = size;
276 
277  /* put driver in raw mode */
278  r.x.bx = fileno(op);
279  r.x.dx = 0xa0;
280  r.x.ax = 0x4401;
281  int86(0x21,&r,&r);
282 
283  while (size >= bsize) {
284  fread(buf,bsize,1,fp);
285  for (i = 0; i < bsize; i++)
286  putc(buf[i],op);
287  size -= bsize;
288  }
289  if (size) {
290  fread(buf,size,1,fp);
291  for (i = 0; i < size; i++)
292  putc(buf[i],op);
293  }
294  PrinterErrmsg("Done",NULL);
295  fclose(op);
296  return (false);
297 }
298 
299 
300 void
302 
303 char *device;
304 {
305  strlwr(device);
306  if (device) {
307  if (!strcmp(device,"prn") || !strcmp(device,"lpt1"))
308  hc_port = 0;
309  else if (!strcmp(device,"lpt2"))
310  hc_port = 1;
311  else if (!strcmp(device,"lpt3"))
312  hc_port = 2;
313  else if (!strcmp(device,"lpt4"))
314  hc_port = 3;
315  else
316  hc_port = -1;
317  strncpy(hc_device,device,8);
318  hc_device[8] = '\0';
319  }
320  else {
321  strcpy(hc_device,"prn");
322  hc_port = 0;
323  }
324 }
325 
static char buf[MAXPROMPT]
Definition: arg.c:18
DISPDEVICE device[]
Definition: display.c:24
#define MAXY
Definition: mfb.h:522
char * strcpy()
#define FONTWIDTH
Definition: mfb.h:546
void SetPrinterDevice(char *device)
Definition: mkhpplot.c:301
int mfbMODE
Definition: mfbp.h:41
Bool makehpplot()
Definition: mkhpplot.c:26
Definition: cddefs.h:169
Definition: cddefs.h:312
int MFBInfo()
void PrinterErrmsg(char *msg1, char *msg2)
Definition: mkhpplot.c:196
#define L
Definition: parse.c:442
static int hc_port
Definition: mkhpplot.c:22
int ysize
Definition: mfbp.h:54
Bool
Definition: mfb.h:18
vidmptr base
Definition: mfbp.h:38
void MFBText()
#define NULL
Definition: spdefs.h:121
void MFBLine()
int MFBGetchar()
Definition: mfbopen.c:259
static Bool dump()
int MFBSetColor()
static double c
Definition: vectors.c:16
void MFBDrawLineTo()
Definition: fteparse.h:28
Bool rasterize(FILE *fp)
Definition: mkhpplot.c:78
Definition: cddefs.h:142
int xsize
Definition: mfbp.h:53
static char hc_device[10]
Definition: mkhpplot.c:21
#define MAXX
Definition: mfb.h:521
int bytpline
Definition: mfbp.h:39
Definition: cddefs.h:162
#define FONTHEIGHT
Definition: mfb.h:545
struct mfbpc pc
Definition: mfbopen.c:14
static char * hmsg
Definition: mkhpplot.c:20
void MFBBox()
Definition: cddefs.h:192
Bool PrinterPortStatus(int which)
Definition: mkhpplot.c:228
void free()
char * malloc()