Jspice3
hcopy.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1992 Stephen R. Whiteley
5 ****************************************************************************/
6 
7 #include "spice.h"
8 #include "plotdefs.h"
9 
10 #ifndef MSDOS
11 
12 static void
13 message(instr, outstr)
14 
15 char *instr, *outstr;
16 {
17  if (!outstr)
18  fprintf(cp_out, "%s\n", instr);
19  else
20  strcpy(outstr, instr);
21 }
22 
23 
24 int
25 ft_makehardcopy(graph, fname, devtype, device, mesg)
26 
27 GRAPH *graph;
28 char *fname, *devtype, *device, *mesg;
29 {
30  bool printed = false;
31  char buf[BSIZE_SP];
32  char *sif = "%s saved in file \"%s\"";
33 
34  if (*device) {
35  sprintf(buf, "Printing %s on the %s printer", fname, device);
36  message(buf, mesg);
37 
38 #ifdef SYSTEM_PLOT5LPR
39  if (!strcmp(devtype, "plot5")) {
40  (void) sprintf(buf, SYSTEM_PLOT5LPR, device, fname);
41  (void) system(buf);
42  printed = true;
43  }
44 #endif
45 
46 #ifdef SYSTEM_PSLPR
47  if (!strcmp(devtype, "postscript")) {
48  (void) sprintf(buf, SYSTEM_PSLPR, device, fname);
49  (void) system(buf);
50  printed = true;
51  }
52 #endif
53  if (!strcmp(devtype, "laser")) {
54  (void) sprintf(buf, "PCL not supported yet");
55  message(buf, mesg);
56  printed = false;
57  }
58  if (!strcmp(devtype, "text")) {
59  (void) sprintf(buf, "lpr -h -P%s %s", device, fname);
60  (void) system(buf);
61  printed = true;
62  }
63  }
64 
65  if (!printed) {
66  if (!strcmp(devtype, "plot5")) {
67  sprintf(buf, sif, "Unix \"plot(1)\"", fname);
68  }
69  else if (!strcmp(devtype, "postscript")) {
70  sprintf(buf, sif, "Postscript image", fname);
71  }
72  else if (!strcmp(devtype, "laser")) {
73  sprintf(buf, sif, "PCL image", fname);
74  }
75  else if (!strcmp(devtype, "text")) {
76  sprintf(buf, sif, "Text", fname);
77  }
78  message(buf, mesg);
79  }
80  return (0);
81 }
82 
83 #else /* MSDOS */
84 
85 #include <dos.h>
86 #include <sys/stat.h>
87 
88 /* MSDOS routines to make hardcopies */
89 
90 #ifdef __STDC__
91 static void errmsg(GRAPH*,char*,char*);
92 static int port_status(int);
93 static int dump(GRAPH*,char*,long,FILE*);
94 static int input(GRAPH*);
95 #else
96 static void errmsg();
97 static int port_status();
98 static int dump();
99 static int input();
100 #endif
101 
102 static char *hmsg = "hit any key to continue";
103 
104 
105 int
106 ft_makehardcopy(graph, fname, devtype, device, mesg)
107 
108 /* returns 1 on failure */
109 GRAPH *graph;
110 char *fname, *devtype, *device, *mesg;
111 {
112  int i, j, hc_port, er;
113  char hc_device[16];
114  char buf[BSIZE_SP];
115  struct stat b;
116  FILE *fp;
117 
118  /* fname is the name of the file which contains the plot data,
119  * and must not be null.
120  * devtype contains the name of the device type the data in fname are
121  * formatted for.
122  * device contains the name of the output device or file, defaults to
123  * "prn" if null.
124  */
125 
126  if (graph)
128 
129  if (*device) {
130  if (cieq(device,"prn") || cieq(device,"lpt1"))
131  hc_port = 0;
132  else if (cieq(device,"lpt2"))
133  hc_port = 1;
134  else if (cieq(device,"lpt3"))
135  hc_port = 2;
136  else if (cieq(device,"lpt4"))
137  hc_port = 3;
138  else
139  hc_port = -1;
140 
141  for (i = 0; i < 8 && device[i] && device[i] != '.'; i++)
142  hc_device[i] = device[i];
143  if (device[i] == '.') {
144  hc_device[i] = device[i];
145  for (j = 0, i++; j < 3 && device[i]; i++, j++)
146  hc_device[i] = device[i];
147  }
148  hc_device[i] = '\0';
149  }
150  else {
151  strcpy(hc_device,"prn");
152  hc_port = 0;
153  }
154 
155  if (hc_port >= 0) {
156  while (port_status(hc_port)) {
157  errmsg(graph,"Error: Printer is not accessible.",
158  "Hit any key to continue, q to abort:");
159  if (input(graph) == 'q') {
160  goto bad;
161  }
162  }
163  }
164  if (stat(fname,&b)) {
165  sprintf(buf,"Error: File \"%s\", can't stat.\n",fname);
166  errmsg(graph,buf,graph ? hmsg : NULL);
167  if (graph)
168  (void)input(graph);
169  goto bad;
170  }
171 
172  fp = fopen(fname,"rb");
173  if (!fp) {
174  sprintf(buf,"Error: File \"%s\" not found.\n",fname);
175  errmsg(graph,buf,graph ? hmsg : NULL);
176  if (graph)
177  (void)input(graph);
178  goto bad;
179  }
180  er = dump(graph,hc_device,(long) b.st_size,fp);
181  fclose(fp);
182  if (graph)
183  PopGraphContext();
184  return (er);
185 
186 bad:
187 
188  if (eq(devtype, "plot5")) {
189  sprintf(buf, "File \"%s\" is in the Unix ", fname);
190  errmsg(graph,buf,"\"plot(1)\" format.\n");
191  }
192  else if (eq(devtype, "postscript")) {
193  sprintf(buf, "File \"%s\" may be printed on a ", fname);
194  errmsg(graph,buf,"postscript printer.\n");
195  }
196  else if (eq(devtype, "laser")) {
197  sprintf(buf, "File \"%s\" may be printed on a ", fname);
198  errmsg(graph,buf,"pcl laser printer.\n");
199  }
200  else if (eq(devtype, "text")) {
201  sprintf(buf, "File \"%s\" may be printed on a ", fname);
202  errmsg(graph,buf,"text printer.\n");
203  }
204  if (graph)
205  PopGraphContext();
206  return (1);
207 }
208 
209 
210 void
211 errmsg(graph, msg1, msg2)
212 
213 GRAPH *graph;
214 char *msg1, *msg2;
215 {
216  int x,y,w,h;
217  int l,r,t,b;
218 
219  if (graph == NULL) {
220  if (msg1)
221  fprintf(cp_err,msg1);
222  if (msg2)
223  fprintf(cp_err,msg2);
224  fflush(cp_err);
225  return;
226  }
227  w = graph->fontwidth;
228  h = graph->fontheight;
229  x = graph->absolute.width/2;
230  y = graph->absolute.height/2;
231  l = x - 19*w;
232  r = x + 19*w;
233  t = y + 2*h;
234  b = y - 2*h;
235 
236  DevSetColor(0);
237  DevBox(l,b,r,t);
238  DevSetColor(1);
239  DevLine(l,b,r,b);
240  DevLine(r,b,r,t);
241  DevLine(r,t,l,t);
242  DevLine(l,t,l,b);
243  DevSetColor(2);
244  if (msg1)
245  DevText(msg1,l+w,t-2*h);
246  if (msg2)
247  DevText(msg2,l+w,t-3*h);
248 }
249 
250 
251 static int
252 port_status(which)
253 
254 int which;
255 {
256  int i;
257  union REGS r;
258 
259  r.x.ax = 0x0200;
260  r.x.dx = which;
261  int86(0x17,&r,&r);
262  i = r.x.ax;
263  i >>= 8;
264  /* bit 5 out of paper, bit 3 io error, bit 0 timed out */
265  if (i & 0x29) return (1);
266  return (0);
267 }
268 
269 
270 static int
271 dump(graph,device,size,fp)
272 
273 /* dump size bytes from fp to parallel port */
274 GRAPH *graph;
275 char *device;
276 long size;
277 FILE *fp;
278 {
279  int i;
280  unsigned char *c;
281  static char *buf;
282  int bsize = 8192;
283  FILE *op;
284  union REGS r;
285 
286  if (!buf) buf = (char *) malloc(bsize);
287  if (!buf) {
288  errmsg(graph,"Error: Out of memory.\n",graph ? hmsg : NULL);
289  if (graph)
290  (void)input(graph);
291  return (1);
292  }
293 
294  op = fopen(device,"wb");
295  if (!op) {
296  sprintf(buf,"Error: Printer driver %s not found.\n",device);
297  errmsg(graph,buf,graph ? hmsg : NULL);
298  if (graph)
299  (void)input(graph);
300  return (1);
301  }
302  errmsg(graph,"Printing ...",NULL);
303 
304  if (bsize > size) bsize = size;
305 
306  /* put driver in raw mode */
307  r.x.bx = fileno(op);
308  r.x.dx = 0xa0;
309  r.x.ax = 0x4401;
310  int86(0x21,&r,&r);
311 
312  while (size >= bsize) {
313  fread(buf,bsize,1,fp);
314  for (i = 0; i < bsize; i++)
315  putc(buf[i],op);
316  size -= bsize;
317  }
318  if (size) {
319  fread(buf,size,1,fp);
320  for (i = 0; i < size; i++)
321  putc(buf[i],op);
322  }
323  errmsg(graph,"Done",graph ? NULL : "\n");
324  fclose(op);
325  return (0);
326 }
327 
328 
329 static int
330 input(graph)
331 
332 GRAPH *graph;
333 {
336 
337  if (!graph) {
338  response.reply.ch = getch() & 0xff;
339  if (response.reply.ch == 0)
340  response.reply.ch = 256 + getch();
341  fprintf(cp_err,"\n");
342  }
343  else {
344  request.option = char_option;
345  request.fp = (FILE*)0;
346  DevInput(&request, &response);
347  }
348  return (response.reply.ch);
349 }
350 
351 #endif
static char buf[MAXPROMPT]
Definition: arg.c:18
#define BSIZE_SP
Definition: misc.h:19
Definition: outitf.c:1049
#define eq(a, b)
Definition: misc.h:29
DISPDEVICE device[]
Definition: display.c:24
struct graph::@2 absolute
int cieq()
FILE * fp
Definition: ftegraph.h:188
char * strcpy()
OPTION option
Definition: ftegraph.h:187
int system(char *str)
Definition: libfuncs.c:85
void DevSetColor()
void DevText()
static struct sHtxt * input()
Definition: hypertxt.c:786
char * malloc()
int height
Definition: ftegraph.h:45
Definition: cddefs.h:169
Definition: cddefs.h:312
int ch
Definition: ftegraph.h:196
union response::@11 reply
static int hc_port
Definition: mkhpplot.c:22
FILE * cp_err
Definition: help.c:101
void DevBox()
int fontwidth
Definition: ftegraph.h:82
void PushGraphContext()
#define NULL
Definition: spdefs.h:121
FILE * cp_out
Definition: help.c:101
Definition: ftegraph.h:29
static Bool dump()
void DevLine()
int fontheight
Definition: ftegraph.h:82
static double c
Definition: vectors.c:16
Definition: fteparse.h:28
static char hc_device[10]
Definition: mkhpplot.c:21
void PopGraphContext()
Definition: graphdb.c:270
void DevInput()
int ft_makehardcopy(GRAPH *graph, char *fname, char *devtype, char *device, char *mesg)
Definition: hcopy.c:25
Definition: cddefs.h:162
static void message(char *instr, char *outstr)
Definition: hcopy.c:13
int width
Definition: ftegraph.h:45
static char * hmsg
Definition: mkhpplot.c:20
Definition: cddefs.h:192