xref: /csrg-svn/old/vfilters/vdmp/vdmp.c (revision 33682)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 char copyright[] =
15 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
16  All rights reserved.\n";
17 #endif /* not lint */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)vdmp.c	5.2 (Berkeley) 03/08/88";
21 #endif /* not lint */
22 
23 /*
24  *  reads raster file created by cifplot and dumps it onto the
25  *  Varian or Versatec plotter.
26  *  Assumptions:
27  *	Input is from device 0.
28  *	plotter is already opened as device 1.
29  *	error output file is device 2.
30  */
31 #include <stdio.h>
32 #include <sys/vcmd.h>
33 
34 #define IN	0
35 #define OUT	1
36 
37 #define MAGIC_WORD	0xA5CF4DFA
38 
39 #define BUFSIZE		1024*128
40 #define BLOCK		1024
41 
42 static char *Sid = "@(#)vdmp.c	5.1\t5/15/85";
43 
44 int	plotmd[] = { VPLOT };
45 int	prtmd[]	= { VPRINT };
46 
47 int	inbuf[BLOCK/sizeof(int)];
48 char	buf[BUFSIZE];
49 int	lines;
50 
51 int	varian;			/* 0 for versatec, 1 for varian. */
52 int	BYTES_PER_LINE;		/* number of bytes per raster line. */
53 int	PAGE_LINES;		/* number of raster lines per page. */
54 
55 char	*name, *host, *acctfile;
56 
57 main(argc, argv)
58 	int argc;
59 	char *argv[];
60 {
61 	register int n;
62 
63 	while (--argc) {
64 		if (**++argv == '-') {
65 			switch (argv[0][1]) {
66 			case 'x':
67 				BYTES_PER_LINE = atoi(&argv[0][2]) / 8;
68 				varian = BYTES_PER_LINE == 264;
69 				break;
70 
71 			case 'y':
72 				PAGE_LINES = atoi(&argv[0][2]);
73 				break;
74 
75 			case 'n':
76 				argc--;
77 				name = *++argv;
78 				break;
79 
80 			case 'h':
81 				argc--;
82 				host = *++argv;
83 			}
84 		} else
85 			acctfile = *argv;
86 	}
87 
88 	n = read(IN, inbuf, BLOCK);
89 	if (inbuf[0] == MAGIC_WORD && n == BLOCK) {
90 		/* we have a formatted dump file */
91 		inbuf[(BLOCK/sizeof(int))-1] = 0;  /* make sure string terminates */
92 		ioctl(OUT, VSETSTATE, prtmd);
93 		write(OUT, &inbuf[4], (strlen(&inbuf[4])+1) & ~1);
94 		write(OUT, "\n", 2);
95 	} else				/* dump file not formatted */
96 		lseek(IN, 0L, 0);	/* reset in's seek pointer and plot */
97 
98 	n = putplot();
99 
100 	/* page feed */
101 	ioctl(OUT, VSETSTATE, prtmd);
102 	if (varian)
103 		write(OUT, "\f", 2);
104 	else
105 		write(OUT, "\n\n\n\n\n", 6);
106 	account(name, host, acctfile);
107 	exit(n);
108 }
109 
110 putplot()
111 {
112 	register char *cp;
113 	register int bytes, n;
114 
115 	cp = buf;
116 	bytes = 0;
117 	ioctl(OUT, VSETSTATE, plotmd);
118 	while ((n = read(IN, cp, sizeof(buf))) > 0) {
119 		if (write(OUT, cp, n) != n)
120 			return(1);
121 		bytes += n;
122 	}
123 	/*
124 	 * Make sure we send complete raster lines.
125 	 */
126 	if ((n = bytes % BYTES_PER_LINE) > 0) {
127 		n = BYTES_PER_LINE - n;
128 		for (cp = &buf[n]; cp > buf; )
129 			*--cp = 0;
130 		if (write(OUT, cp, n) != n)
131 			return(1);
132 		bytes += n;
133 	}
134 	lines += bytes / BYTES_PER_LINE;
135 	return(0);
136 }
137 
138 account(who, from, acctfile)
139 	char *who, *from, *acctfile;
140 {
141 	register FILE *a;
142 
143 	if (who == NULL || acctfile == NULL)
144 		return;
145 	if (access(acctfile, 02) || (a = fopen(acctfile, "a")) == NULL)
146 		return;
147 	/*
148 	 * Varian accounting is done by 8.5 inch pages;
149 	 * Versatec accounting is by the (12 inch) foot.
150 	 */
151 	fprintf(a, "t%6.2f\t", (double)lines / (double)PAGE_LINES);
152 	if (from != NULL)
153 		fprintf(a, "%s:", from);
154 	fprintf(a, "%s\n", who);
155 	fclose(a);
156 }
157