1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Meta-file input and output routines |
6 |
*/ |
7 |
|
8 |
#include "meta.h" |
9 |
|
10 |
|
11 |
|
12 |
static PRIMITIVE peof = {PEOF, 0200, -1, -1, -1, -1, NULL}; |
13 |
|
14 |
|
15 |
|
16 |
#define INTARG(n) (((unsigned)inbuf[n] << 7) | (unsigned)inbuf[n+1]) |
17 |
|
18 |
|
19 |
|
20 |
readp(p, fp) /* read in primitive from file */ |
21 |
|
22 |
PRIMITIVE *p; |
23 |
FILE *fp; |
24 |
|
25 |
{ |
26 |
char inbuf[MAXARGS]; |
27 |
register int c, nargs; |
28 |
|
29 |
if (fp == NULL) fp = stdin; |
30 |
|
31 |
if ((c = getc(fp)) == EOF) { /* used to be fatal */ |
32 |
mcopy((char *)p, (char *)&peof, sizeof(PRIMITIVE)); |
33 |
return(0); |
34 |
} |
35 |
if (!(c & 0200)) |
36 |
error(USER, "readp not started on command"); |
37 |
|
38 |
p->com = c & 0177; |
39 |
if (!iscom(p->com)) |
40 |
error(USER, "bad command in readp"); |
41 |
|
42 |
for (nargs = 0; (c = getc(fp)) != EOF && !(c & 0200) && |
43 |
nargs < MAXARGS-1; nargs++) |
44 |
inbuf[nargs] = c; |
45 |
|
46 |
if (c & 0200) |
47 |
ungetc(c, fp); |
48 |
else if (nargs >= MAXARGS-1) |
49 |
error(USER, "command w/ too many arguments in readp"); |
50 |
|
51 |
c = 0; |
52 |
|
53 |
if (nargs) { |
54 |
p->arg0 = inbuf[c++]; |
55 |
nargs--; |
56 |
} |
57 |
else |
58 |
p->arg0 = 0200; |
59 |
|
60 |
if (isprim(p->com)) { |
61 |
if (nargs < 8) |
62 |
error(USER, "missing extent in readp"); |
63 |
p->xy[XMN] = INTARG(c); c += 2; |
64 |
p->xy[YMN] = INTARG(c); c += 2; |
65 |
p->xy[XMX] = INTARG(c); c += 2; |
66 |
p->xy[YMX] = INTARG(c); c += 2; |
67 |
nargs -= 8; |
68 |
} |
69 |
else |
70 |
p->xy[XMN] = p->xy[YMN] = p->xy[XMX] = p->xy[YMX] = -1; |
71 |
|
72 |
if (nargs) { |
73 |
inbuf[c+nargs] = '\0'; |
74 |
p->args = savestr(inbuf+c); |
75 |
} |
76 |
else |
77 |
p->args = NULL; |
78 |
|
79 |
return(p->com != PEOF); |
80 |
} |
81 |
|
82 |
#undef INTARG |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
#define HI7(i) ((i >> 7) & 0177) |
91 |
|
92 |
#define LO7(i) (i & 0177) |
93 |
|
94 |
|
95 |
|
96 |
writep(p, fp) /* write primitive to file */ |
97 |
|
98 |
register PRIMITIVE *p; |
99 |
FILE *fp; |
100 |
|
101 |
{ |
102 |
|
103 |
if (fp == NULL) fp = stdout; |
104 |
|
105 |
if (!iscom(p->com)) |
106 |
error(USER, "bad command in writep"); |
107 |
|
108 |
putc(p->com | 0200, fp); |
109 |
|
110 |
if (isprim(p->com)) { |
111 |
putc(p->arg0 & 0177, fp); |
112 |
putc(HI7(p->xy[XMN]), fp); putc(LO7(p->xy[XMN]), fp); |
113 |
putc(HI7(p->xy[YMN]), fp); putc(LO7(p->xy[YMN]), fp); |
114 |
putc(HI7(p->xy[XMX]), fp); putc(LO7(p->xy[XMX]), fp); |
115 |
putc(HI7(p->xy[YMX]), fp); putc(LO7(p->xy[YMX]), fp); |
116 |
} |
117 |
else if (!(p->arg0 & 0200)) |
118 |
putc(p->arg0, fp); |
119 |
|
120 |
if (p->args != NULL) |
121 |
fputs(p->args, fp); |
122 |
|
123 |
if (p->com == PDRAW || p->com == PEOF) |
124 |
if (fflush(fp) == -1) |
125 |
error(SYSTEM, "error detected writing file in writep"); |
126 |
|
127 |
} |
128 |
|
129 |
#undef HI7 |
130 |
|
131 |
#undef LO7 |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
writeof(fp) /* write end of file command to fp */ |
137 |
|
138 |
FILE *fp; |
139 |
|
140 |
{ |
141 |
|
142 |
writep(&peof, fp); |
143 |
|
144 |
} |