1 /* gdump.c	1.3	83/06/24 	by David Slattengren
2  *
3  *      This file contains a program for printing gprint raster files.
4  *   Gprint puts out a generic file, and gdump changes the size to fit
5  *   the proper device that's being used.
6  */
7 
8 #include <stdio.h>
9 #include <signal.h>
10 #include "gprint.h"
11 
12 #define LPR	"/usr/ucb/lpr"
13 
14 extern char *mktemp();
15 
16 char *picture	= "/usr/tmp/rastAXXXXXX";	/* output file name */
17 int  temp;					/* output file number */
18 char *lpargs[9]	= { "lpr", "-Pvarian", "-v",
19 		   "-s", "-r", "-J", "gdump" };
20 int  outwidth	= Vbytperlin;		/* number of chars per line to output*/
21 int  outlength	= Vylen;		/* number of lines to output */
22 
23 int  infile	= 0;		/* input file (default = stdin) */
24 int  inwidth	= Vxlen/8;	/* input file raster line length */
25 int  FileFound	= 0;		/* flag for filename on input */
26 char *arg;			/* intermediary command line argument */
27 char *file;			/* input file name */
28 char buf [Wbytperlin];		/* intermediary raster line buffer */
29 
30 
31 cleanup()		/* Called if program stopped, or ... */
32 {
33     unlink (picture);
34     exit (1);
35 }
36 
37 
38 			/* read in one line of the raster file */
39 readline()		/* returns 1 (true) if successful, 0 if not */
40 {
41     register i = inwidth;
42     register j;
43 
44     do {
45     	if ((j = read (infile, buf, inwidth)) < 1) {
46 	    if (i == inwidth)
47 		return (0);
48 	    else {			/* fill in incomplete last line */
49 		while (i != inwidth)
50 		    buf [i++] = '\0';
51 		return (1);
52 	    }
53 	}
54 	i -= j;
55     } while (i);
56     return (1);
57 }
58 
59 
60 main (argc, argv)
61 int argc;
62 char *argv[];
63 {
64     register int i;		/* multipurpose index */
65 
66 
67     lpargs [7] = picture;	/* set file for lpr to read */
68     lpargs [8] = 0;
69     while (--argc > 0)		/* Parse the command line. */
70     {
71         arg = *++argv;
72         if (arg[0] != '-') {
73 	    if (FileFound) {
74 		fprintf (stderr, "gdump: Only one file may be printed\n");
75 		exit(1);
76 	    }
77             lpargs [6] = file = arg;	/* set filename (and to lpr) */
78             FileFound = 1;
79         } else {
80             switch (*++arg) {
81 		case 'W':		/* Print to wide (versatec) device */
82 			outwidth = Wbytperlin;
83 			outlength = Wylen;
84 			lpargs[1] = "-Pversatec";
85 			break;
86 		case 'V':		/* Print to narrow (varian) device */
87 			outwidth = Vbytperlin;
88 			outlength = Vylen;
89 			lpargs[1] = "-Pvarian";
90 			break;
91 		default:
92 			printf ("unknown switch %c\n", *arg);
93 			exit (1);
94 			break;
95             }
96         }
97     }
98 
99     if (FileFound) {		/* open input file, if one exists */
100 	fclose (stdin);
101 	infile = open (file, 0);
102 	if (infile == -1) {
103             fprintf (stderr, "can't open %s", file);
104 	    exit(1);
105 	}
106     }
107 					/* clear out line buffer */
108     for (i = 0; i < Wbytperlin; buf [i++] = 0)
109 	;
110 
111     mktemp (picture);			/* make up file name */
112 
113     signal (SIGTERM, cleanup);			/* prepare to be killed */
114     if (signal (SIGINT, SIG_IGN) != SIG_IGN)	/*    or interrupted */
115 	signal (SIGINT, cleanup);
116 
117     if ((temp = creat (picture, 0666)) == -1) {
118 	fprintf (stderr, "gdump: can't create %s\n", picture);
119 	cleanup ();
120     }
121 
122 /*
123  * transfer the raster file from input to output,
124  * fixing line lengths, if necessary,
125  * and truncating to one page length
126  */
127 
128     while (readline () && outlength--) {
129 	if (write (temp, buf, outwidth) != outwidth) {
130 	    sprintf (buf, "gdump - error writing %s\n", picture);
131 	    perror (buf);
132 	    cleanup();
133 	}
134     }						/* eat the rest of input */
135     while (read (infile, buf, inwidth) > 0)	/* if there is any */
136 	;
137     close (infile);				/* clear out line buffer */
138     for (i = 0; i < Wbytperlin; buf [i++] = 0)
139 	;
140     while (outlength-- > 0) {			/* and fill out the picture */
141 	if (write (temp, buf, outwidth) != outwidth) {
142 	    sprintf (buf, "gdump - error writing %s\n", picture);
143 	    perror (buf);
144 	    cleanup();
145 	}
146     }
147 
148     execv (LPR, lpargs);
149     fprintf (stderr, "gdump: can't exec %s\n", LPR);
150     cleanup();
151 }
152