1 | /*
|
---|
2 | ============================================================================
|
---|
3 | rawio.c -- Some raw serial I/O routines for ROMP, et al
|
---|
4 | Version 5 -- 1987-06-11 -- D.N. Lynx Crowe
|
---|
5 |
|
---|
6 | Defines: getln(), getrln(), readln(), writeln()
|
---|
7 |
|
---|
8 | All of the following use raw BIOS calls to do their I/O.
|
---|
9 |
|
---|
10 | int
|
---|
11 | getln(unit,nb,buf)
|
---|
12 | int unit; logical unit number 1..4
|
---|
13 | int nb; buffer limit
|
---|
14 | char *buf; buffer pointer
|
---|
15 |
|
---|
16 | Reads a line from unit into buf, with a limit of nb
|
---|
17 | bytes. Does standard input editing (BS. DEL, ^X). Returns
|
---|
18 | on CR, LF, ^X, or buffer full. Returns the byte that stopped
|
---|
19 | the input, or ERR01 for buffer full. Echoes the characters as
|
---|
20 | it reads them, such that "What you see is what you get" on a crt.
|
---|
21 |
|
---|
22 | int
|
---|
23 | getrln(unit, nb, buf)
|
---|
24 | int unit; logical unit number 1..4
|
---|
25 | int nb; buffer limit
|
---|
26 | char *buf; buffer pointer
|
---|
27 |
|
---|
28 | Reads a line from unit into buf, with a limit of nb bytes.
|
---|
29 | Allows input to be cancelled by ^X. Returns on CR, LF, ^X,
|
---|
30 | ^Z, or buffer full. Returns the byte that stopped the input,
|
---|
31 | or ERR01 for buffer full.
|
---|
32 |
|
---|
33 | |
---|
34 |
|
---|
35 | int
|
---|
36 | readln(unit,nc,ctl,nb,buf)
|
---|
37 | int unit; logical unit number 1..4
|
---|
38 | int nc; length of control string ctl
|
---|
39 | char *ctl; control string pointer
|
---|
40 | int nb; buffer limit
|
---|
41 | char *buf; buffer pointer (at least nb+1 bytes)
|
---|
42 |
|
---|
43 | Reads characters from unit into buf until: 1) nb bytes
|
---|
44 | have been transferred, or, 2) one of the characters in the string
|
---|
45 | at ctl has been read. The terminating character will be in the
|
---|
46 | buffer, followed by a null byte (even if the character from ctl
|
---|
47 | was a null byte). Returns: 1) the terminating character as its value,
|
---|
48 | or 2) ERR01 if stopped by the count in nb, or 3) ERR15 for an
|
---|
49 | invalid unit. Echoes characters as it reads them, unless the
|
---|
50 | character is one of those in ctl.
|
---|
51 |
|
---|
52 | void
|
---|
53 | writeln(unit,buf)
|
---|
54 | int unit; logical unit number 0..4
|
---|
55 | char *buf; buffer pointer
|
---|
56 |
|
---|
57 | Writes the zero terminated string from buf onto unit.
|
---|
58 |
|
---|
59 | ============================================================================
|
---|
60 | */
|
---|
61 |
|
---|
62 | /* |
---|
63 | */
|
---|
64 |
|
---|
65 | #include "ram.h"
|
---|
66 |
|
---|
67 | int16_t readln(int16_t unit, int16_t nc, int8_t *ctl, int16_t nb, int8_t *buf)
|
---|
68 | {
|
---|
69 | register int8_t *cp;
|
---|
70 | register int16_t i, j;
|
---|
71 | register int8_t *bp;
|
---|
72 | register int8_t c;
|
---|
73 |
|
---|
74 |
|
---|
75 | if (unit LT 1 OR unit GT 4) /* verify unit number is in range */
|
---|
76 | return(ERR15); /* return ERR15 if not */
|
---|
77 |
|
---|
78 | bp = buf; /* setup buffer pointer */
|
---|
79 |
|
---|
80 | for (i = 0; i < nb; i++) { /* main read loop */
|
---|
81 |
|
---|
82 | c = BIOS(B_GETC, unit) & 0xFF; /* get a byte from the unit */
|
---|
83 |
|
---|
84 | *bp++ = c; /* add it to the buffer */
|
---|
85 | *bp = '\0'; /* ... followed by a zero byte */
|
---|
86 |
|
---|
87 | cp = ctl; /* setup to scan ctl for the character */
|
---|
88 |
|
---|
89 | for (j = 0; j < nc; j++) /* scan each byte of ctl */
|
---|
90 | if (*ctl++ EQ c ) /* done if we find it */
|
---|
91 | return((int16_t)c);
|
---|
92 |
|
---|
93 | BIOS(B_PUTC, unit, c); /* echo the character */
|
---|
94 | }
|
---|
95 |
|
---|
96 | return(ERR01); /* buffer full */
|
---|
97 | }
|
---|
98 |
|
---|
99 | /* |
---|
100 | */
|
---|
101 |
|
---|
102 | int16_t getln(int16_t unit, int16_t nb, int8_t *buf)
|
---|
103 | {
|
---|
104 | register int8_t *bp;
|
---|
105 | register int8_t c;
|
---|
106 | register int16_t bc;
|
---|
107 |
|
---|
108 | bc = 0; /* number of characters currently in buffer */
|
---|
109 | bp = buf; /* current buffer pointer */
|
---|
110 | *bp = '\0'; /* initial null into buffer */
|
---|
111 |
|
---|
112 | while (bc LT nb) {
|
---|
113 |
|
---|
114 | c = BIOS(B_GETC, unit) & 0xFF;
|
---|
115 |
|
---|
116 | switch (c) {
|
---|
117 |
|
---|
118 | case A_BS:
|
---|
119 | case A_DEL:
|
---|
120 |
|
---|
121 | if (bc EQ 0) {
|
---|
122 |
|
---|
123 | BIOS(B_PUTC, unit, A_BEL);
|
---|
124 | break;
|
---|
125 |
|
---|
126 | } else {
|
---|
127 |
|
---|
128 | bc--;
|
---|
129 | bp--;
|
---|
130 | *bp = '\0';
|
---|
131 | BIOS(B_PUTC, unit, A_BS);
|
---|
132 | BIOS(B_PUTC, unit, ' ');
|
---|
133 | BIOS(B_PUTC, unit, A_BS);
|
---|
134 | break;
|
---|
135 | }
|
---|
136 |
|
---|
137 | case A_CR:
|
---|
138 | case A_LF:
|
---|
139 |
|
---|
140 | *bp++ = c; /* put character in buffer */
|
---|
141 | *bp = '\0'; /* terminate line with null */
|
---|
142 | return((int16_t)c); /* return -- CR or LF hit */
|
---|
143 |
|
---|
144 | /* |
---|
145 | */
|
---|
146 |
|
---|
147 | case CTL('X'):
|
---|
148 |
|
---|
149 | *buf = '\0'; /* clear the buffer */
|
---|
150 | return((int16_t)c); /* return -- line cancelled */
|
---|
151 |
|
---|
152 | default:
|
---|
153 |
|
---|
154 | *bp++ = c; /* put character in buffer */
|
---|
155 | *bp = '\0'; /* terminate line with null */
|
---|
156 | BIOS(B_PUTC, unit, c); /* echo the character */
|
---|
157 | bc++; /* update character count */
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | return(ERR01); /* buffer full error */
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* |
---|
165 | */
|
---|
166 |
|
---|
167 | int16_t getrln(int16_t unit, int16_t nb, int8_t *buf)
|
---|
168 | {
|
---|
169 | register int8_t *bp;
|
---|
170 | register int8_t c;
|
---|
171 | register int16_t bc;
|
---|
172 |
|
---|
173 | bc = 0; /* number of characters currently in buffer */
|
---|
174 | bp = buf; /* current buffer pointer */
|
---|
175 | *bp = '\0'; /* initial null into buffer */
|
---|
176 |
|
---|
177 | while (bc LT nb) {
|
---|
178 |
|
---|
179 | c = BIOS(B_GETC, unit) & 0xFF;
|
---|
180 |
|
---|
181 | switch (c) {
|
---|
182 |
|
---|
183 | case A_CR:
|
---|
184 | case A_LF:
|
---|
185 | case CTL('Z'):
|
---|
186 |
|
---|
187 | *bp++ = c; /* put character in buffer */
|
---|
188 | *bp = '\0'; /* terminate line with null */
|
---|
189 | return((int16_t)c); /* return -- CR, LF, or ^Z hit */
|
---|
190 |
|
---|
191 | case CTL('X'):
|
---|
192 |
|
---|
193 | *buf = '\0'; /* clear the buffer */
|
---|
194 | return((int16_t)c); /* return -- line cancelled */
|
---|
195 |
|
---|
196 | default:
|
---|
197 |
|
---|
198 | *bp++ = c; /* put character in buffer */
|
---|
199 | *bp = '\0'; /* terminate line with null */
|
---|
200 | bc++; /* update character count */
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | return(ERR01); /* buffer full error */
|
---|
205 | }
|
---|
206 |
|
---|
207 | /* |
---|
208 | */
|
---|
209 |
|
---|
210 | void writeln(int16_t unit, int8_t *buf)
|
---|
211 | {
|
---|
212 | register int8_t *bp;
|
---|
213 | register int8_t c;
|
---|
214 |
|
---|
215 | bp = buf; /* setup buffer pointer */
|
---|
216 |
|
---|
217 | while (c = *bp++) /* send the string, a byte at a time */
|
---|
218 | BIOS(B_PUTC, unit, c);
|
---|
219 | }
|
---|
220 |
|
---|