must clear EOF flag when reading from tty, else (since ignored)
[unix-history] / usr / src / old / vfilters / vdmp / vdmp.c
CommitLineData
34281181 1/* VDMP: version 4.7 updated %G%
59cb049a
DH
2 *
3 * reads raster file created by cifplot and dumps it onto the
4 * Varian or Versatec plotter.
abde9955
RC
5 * Assumptions:
6 * Input is from device 0.
7 * plotter is already opened as device 1.
8 * error output file is device 2.
59cb049a
DH
9 */
10#include <stdio.h>
59cb049a
DH
11#include <sys/vcmd.h>
12
34281181
RC
13#define IN 0
14#define OUT 1
15
6e4c0571
DF
16#define MAGIC_WORD 0xA5CF4DFA
17
18#define BUFSIZE 1024*128
19#define BLOCK 1024
59cb049a 20
34281181 21static char *Sid = "@(#)vdmp.c 4.3\t6/24/83";
59cb049a 22
34281181
RC
23int plotmd[] = { VPLOT };
24int prtmd[] = { VPRINT };
59cb049a 25
abde9955 26int inbuf[BLOCK/sizeof(int)];
34281181 27char buf[BUFSIZE];
abde9955 28int lines;
59cb049a 29
34281181
RC
30int varian; /* 0 for versatec, 1 for varian. */
31int BYTES_PER_LINE; /* number of bytes per raster line. */
32int PAGE_LINES; /* number of raster lines per page. */
59cb049a 33
abde9955 34char *name, *host, *acctfile;
59cb049a
DH
35
36main(argc, argv)
abde9955
RC
37 int argc;
38 char *argv[];
59cb049a 39{
abde9955
RC
40 register int n;
41
abde9955 42 while (--argc) {
34281181 43 if (**++argv == '-') {
abde9955 44 switch (argv[0][1]) {
34281181
RC
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
abde9955
RC
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;
59cb049a
DH
65 }
66
34281181 67 n = read(IN, inbuf, BLOCK);
abde9955
RC
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 */
34281181
RC
71 ioctl(OUT, VSETSTATE, prtmd);
72 write(OUT, &inbuf[4], (strlen(&inbuf[4])+1) & ~1);
73 write(OUT, "\n", 2);
abde9955 74 } else /* dump file not formatted */
34281181 75 lseek(IN, 0L, 0); /* reset in's seek pointer and plot */
abde9955
RC
76
77 n = putplot();
78
79 /* page feed */
34281181
RC
80 ioctl(OUT, VSETSTATE, prtmd);
81 if (varian)
82 write(OUT, "\f", 2);
abde9955 83 else
34281181 84 write(OUT, "\n\n\n\n\n", 6);
abde9955 85 account(name, host, acctfile);
34281181 86 exit(n);
abde9955 87}
59cb049a
DH
88
89putplot()
90{
34281181
RC
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);
abde9955
RC
115}
116
117account(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 /*
34281181 127 * Varian accounting is done by 8.5 inch pages;
abde9955
RC
128 * Versatec accounting is by the (12 inch) foot.
129 */
34281181 130 fprintf(a, "t%6.2f\t", (lines / 200.0) / PAGE_LINES);
abde9955
RC
131 if (from != NULL)
132 fprintf(a, "%s:", from);
133 fprintf(a, "%s\n", who);
134 fclose(a);
135}