xref: /csrg-svn/old/vfilters/vdmp/vdmp.c (revision 12470)
1 /*  VDMP: version 4.7				updated 05/16/83
2  *
3  *  reads raster file created by cifplot and dumps it onto the
4  *  Varian or Versatec plotter.
5  *  Assumptions:
6  *	Input is from device 0.
7  *	plotter is already opened as device 1.
8  *	error output file is device 2.
9  */
10 #include <stdio.h>
11 #include <sys/vcmd.h>
12 
13 #define IN	0
14 #define OUT	1
15 
16 #define MAGIC_WORD	0xA5CF4DFA
17 
18 #define BUFSIZE		1024*128
19 #define BLOCK		1024
20 
21 static char *Sid = "@(#)vdmp.c	4.3\t6/24/83";
22 
23 int	plotmd[] = { VPLOT };
24 int	prtmd[]	= { VPRINT };
25 
26 int	inbuf[BLOCK/sizeof(int)];
27 char	buf[BUFSIZE];
28 int	lines;
29 
30 int	varian;			/* 0 for versatec, 1 for varian. */
31 int	BYTES_PER_LINE;		/* number of bytes per raster line. */
32 int	PAGE_LINES;		/* number of raster lines per page. */
33 
34 char	*name, *host, *acctfile;
35 
36 main(argc, argv)
37 	int argc;
38 	char *argv[];
39 {
40 	register int n;
41 
42 	while (--argc) {
43 		if (**++argv == '-') {
44 			switch (argv[0][1]) {
45 			case 'x':
46 				BYTES_PER_LINE = atoi(&argv[0][2]) / 8;
47 				varian = BYTES_PER_LINE == 264;
48 				break;
49 
50 			case 'y':
51 				PAGE_LINES = atoi(&argv[0][2]);
52 				break;
53 
54 			case 'n':
55 				argc--;
56 				name = *++argv;
57 				break;
58 
59 			case 'h':
60 				argc--;
61 				host = *++argv;
62 			}
63 		} else
64 			acctfile = *argv;
65 	}
66 
67 	n = read(IN, inbuf, BLOCK);
68 	if (inbuf[0] == MAGIC_WORD && n == BLOCK) {
69 		/* we have a formatted dump file */
70 		inbuf[(BLOCK/sizeof(int))-1] = 0;  /* make sure string terminates */
71 		ioctl(OUT, VSETSTATE, prtmd);
72 		write(OUT, &inbuf[4], (strlen(&inbuf[4])+1) & ~1);
73 		write(OUT, "\n", 2);
74 	} else				/* dump file not formatted */
75 		lseek(IN, 0L, 0);	/* reset in's seek pointer and plot */
76 
77 	n = putplot();
78 
79 	/* page feed */
80 	ioctl(OUT, VSETSTATE, prtmd);
81 	if (varian)
82 		write(OUT, "\f", 2);
83 	else
84 		write(OUT, "\n\n\n\n\n", 6);
85 	account(name, host, acctfile);
86 	exit(n);
87 }
88 
89 putplot()
90 {
91 	register char *cp;
92 	register int bytes, n;
93 
94 	cp = buf;
95 	bytes = 0;
96 	ioctl(OUT, VSETSTATE, plotmd);
97 	while ((n = read(IN, cp, sizeof(buf))) > 0) {
98 		if (write(OUT, cp, n) != n)
99 			return(1);
100 		bytes += n;
101 	}
102 	/*
103 	 * Make sure we send complete raster lines.
104 	 */
105 	if ((n = bytes % BYTES_PER_LINE) > 0) {
106 		n = BYTES_PER_LINE - n;
107 		for (cp = &buf[n]; cp > buf; )
108 			*--cp = 0;
109 		if (write(OUT, cp, n) != n)
110 			return(1);
111 		bytes += n;
112 	}
113 	lines += bytes / BYTES_PER_LINE;
114 	return(0);
115 }
116 
117 account(who, from, acctfile)
118 	char *who, *from, *acctfile;
119 {
120 	register FILE *a;
121 
122 	if (who == NULL || acctfile == NULL)
123 		return;
124 	if (access(acctfile, 02) || (a = fopen(acctfile, "a")) == NULL)
125 		return;
126 	/*
127 	 * Varian accounting is done by 8.5 inch pages;
128 	 * Versatec accounting is by the (12 inch) foot.
129 	 */
130 	fprintf(a, "t%6.2f\t", (lines / 200.0) / PAGE_LINES);
131 	if (from != NULL)
132 		fprintf(a, "%s:", from);
133 	fprintf(a, "%s\n", who);
134 	fclose(a);
135 }
136