1*40585Sbostic /*
2*40585Sbostic * Copyright (c) 1982 Regents of the University of California
3*40585Sbostic */
4*40585Sbostic #ifndef lint
5*40585Sbostic static char sccsid[] = "@(#)asio.c 4.4 2/14/82";
6*40585Sbostic #endif not lint
7*40585Sbostic
8*40585Sbostic #include <stdio.h>
9*40585Sbostic #include "as.h"
10*40585Sbostic /*
11*40585Sbostic * Block I/O routines for logical I/O concurrently in
12*40585Sbostic * more than one place in the same file.
13*40585Sbostic */
14*40585Sbostic int biofd; /* file descriptor for block I/O file */
15*40585Sbostic off_t boffset; /* physical position in logical file */
16*40585Sbostic BFILE *biobufs; /* the block I/O buffers */
17*40585Sbostic static char hexdigit [] =
18*40585Sbostic "0123456789abcdef";
19*40585Sbostic
20*40585Sbostic #define error(severity, message) \
21*40585Sbostic {yyerror(message); if (severity) delexit();}
22*40585Sbostic
Flushfield(n)23*40585Sbostic Flushfield(n)
24*40585Sbostic register int n;
25*40585Sbostic {
26*40585Sbostic while (n>0) {
27*40585Sbostic n -= 8;
28*40585Sbostic outb((bitfield >> n) & 0xFF);
29*40585Sbostic if (liston && (passno == 2))
30*40585Sbostic byte_out((bitfield >> n) & 0xFF);
31*40585Sbostic }
32*40585Sbostic bitoff=0;
33*40585Sbostic bitfield=0;
34*40585Sbostic }
35*40585Sbostic
36*40585Sbostic /*
37*40585Sbostic * Block I/O Routines
38*40585Sbostic */
39*40585Sbostic bopen(bp, off)
40*40585Sbostic struct biobuf *bp;
41*40585Sbostic off_t off;
42*40585Sbostic {
43*40585Sbostic
44*40585Sbostic bp->b_ptr = bp->b_buf;
45*40585Sbostic bp->b_nleft = BUFSIZ - off % BUFSIZ;
46*40585Sbostic bp->b_off = off;
47*40585Sbostic bp->b_link = biobufs;
48*40585Sbostic biobufs = bp;
49*40585Sbostic }
50*40585Sbostic
51*40585Sbostic int bwrerror;
52*40585Sbostic
bwrite(p,cnt,bp)53*40585Sbostic bwrite(p, cnt, bp)
54*40585Sbostic register char *p;
55*40585Sbostic register int cnt;
56*40585Sbostic register struct biobuf *bp;
57*40585Sbostic {
58*40585Sbostic register int put;
59*40585Sbostic register char *to;
60*40585Sbostic
61*40585Sbostic top:
62*40585Sbostic if (cnt == 0)
63*40585Sbostic return;
64*40585Sbostic if (bp->b_nleft) {
65*40585Sbostic put = bp->b_nleft;
66*40585Sbostic if (put > cnt)
67*40585Sbostic put = cnt;
68*40585Sbostic bp->b_nleft -= put;
69*40585Sbostic to = bp->b_ptr;
70*40585Sbostic #ifdef lint
71*40585Sbostic *to = *to;
72*40585Sbostic #endif lint
73*40585Sbostic movblk (p, to, put);
74*40585Sbostic bp->b_ptr += put;
75*40585Sbostic p += put;
76*40585Sbostic cnt -= put;
77*40585Sbostic goto top;
78*40585Sbostic }
79*40585Sbostic if (cnt >= BUFSIZ) {
80*40585Sbostic if (bp->b_ptr != bp->b_buf)
81*40585Sbostic bflush1(bp);
82*40585Sbostic put = cnt - cnt % BUFSIZ;
83*40585Sbostic if (boffset != bp->b_off)
84*40585Sbostic (void)lseek(biofd, (long)bp->b_off, 0);
85*40585Sbostic if (write(biofd, p, put) != put) {
86*40585Sbostic bwrerror = 1;
87*40585Sbostic error(1, "Output write error");
88*40585Sbostic }
89*40585Sbostic bp->b_off += put;
90*40585Sbostic boffset = bp->b_off;
91*40585Sbostic p += put;
92*40585Sbostic cnt -= put;
93*40585Sbostic goto top;
94*40585Sbostic }
95*40585Sbostic bflush1(bp);
96*40585Sbostic goto top;
97*40585Sbostic }
98*40585Sbostic
bflush()99*40585Sbostic bflush()
100*40585Sbostic {
101*40585Sbostic register struct biobuf *bp;
102*40585Sbostic
103*40585Sbostic if (bwrerror)
104*40585Sbostic return;
105*40585Sbostic for (bp = biobufs; bp; bp = bp->b_link)
106*40585Sbostic bflush1(bp);
107*40585Sbostic }
108*40585Sbostic
bflush1(bp)109*40585Sbostic bflush1(bp)
110*40585Sbostic register struct biobuf *bp;
111*40585Sbostic {
112*40585Sbostic register int cnt = bp->b_ptr - bp->b_buf;
113*40585Sbostic
114*40585Sbostic if (cnt == 0)
115*40585Sbostic return;
116*40585Sbostic if (boffset != bp->b_off)
117*40585Sbostic (void)lseek(biofd, (long)bp->b_off, 0);
118*40585Sbostic if (write(biofd, bp->b_buf, cnt) != cnt) {
119*40585Sbostic bwrerror = 1;
120*40585Sbostic error(1, "Output write error");
121*40585Sbostic }
122*40585Sbostic bp->b_off += cnt;
123*40585Sbostic boffset = bp->b_off;
124*40585Sbostic bp->b_ptr = bp->b_buf;
125*40585Sbostic bp->b_nleft = BUFSIZ;
126*40585Sbostic }
127*40585Sbostic
bflushc(bp,c)128*40585Sbostic bflushc(bp, c)
129*40585Sbostic register struct biobuf *bp;
130*40585Sbostic char c;
131*40585Sbostic {
132*40585Sbostic bflush1(bp);
133*40585Sbostic bputc(c, bp);
134*40585Sbostic }
135*40585Sbostic
movblk(src,dest,cnt)136*40585Sbostic movblk (src, dest, cnt)
137*40585Sbostic char *src, *dest;
138*40585Sbostic
139*40585Sbostic {
140*40585Sbostic while (cnt--)
141*40585Sbostic *dest++ = *src++;
142*40585Sbostic }
143*40585Sbostic
byte_out(byte)144*40585Sbostic byte_out (byte)
145*40585Sbostic u_char byte;
146*40585Sbostic {
147*40585Sbostic *layoutpos++ = hexdigit [(byte >> 4) & 0xf];
148*40585Sbostic *layoutpos++ = hexdigit [byte & 0xf];
149*40585Sbostic }
150*40585Sbostic
151*40585Sbostic word_out (word)
152*40585Sbostic u_short (word);
153*40585Sbostic {
154*40585Sbostic union
155*40585Sbostic {
156*40585Sbostic u_short _word;
157*40585Sbostic char _ch[2];
158*40585Sbostic } charr;
159*40585Sbostic
160*40585Sbostic charr._word = word;
161*40585Sbostic byte_out (charr._ch[0]);
162*40585Sbostic byte_out (charr._ch[1]);
163*40585Sbostic }
164*40585Sbostic
long_out(longword)165*40585Sbostic long_out (longword)
166*40585Sbostic u_int longword;
167*40585Sbostic {
168*40585Sbostic union
169*40585Sbostic {
170*40585Sbostic u_int _int;
171*40585Sbostic char _ch[4];
172*40585Sbostic } charr;
173*40585Sbostic
174*40585Sbostic charr._int = longword;
175*40585Sbostic byte_out (charr._ch[0]);
176*40585Sbostic byte_out (charr._ch[1]);
177*40585Sbostic byte_out (charr._ch[2]);
178*40585Sbostic byte_out (charr._ch[3]);
179*40585Sbostic }
180