[f40a309] | 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 "stddefs.h"
|
---|
| 66 | #include "errdefs.h"
|
---|
| 67 | #include "ascii.h"
|
---|
| 68 | #include "biosdefs.h"
|
---|
[7258c6a] | 69 |
|
---|
[f40a309] | 70 |
|
---|
[7258c6a] | 71 | int16_t readln(int16_t unit, int16_t nc, int8_t *ctl, int16_t nb, int8_t *buf)
|
---|
| 72 | {
|
---|
| 73 | register int8_t *cp;
|
---|
| 74 | register int16_t i, j;
|
---|
[f40a309] | 75 | register int8_t *bp;
|
---|
| 76 | register int8_t c;
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | if (unit LT 1 OR unit GT 4) /* verify unit number is in range */
|
---|
| 80 | return(ERR15); /* return ERR15 if not */
|
---|
| 81 |
|
---|
| 82 | bp = buf; /* setup buffer pointer */
|
---|
| 83 |
|
---|
| 84 | for (i = 0; i < nb; i++) { /* main read loop */
|
---|
| 85 |
|
---|
| 86 | c = BIOS(B_GETC, unit) & 0xFF; /* get a byte from the unit */
|
---|
| 87 |
|
---|
| 88 | *bp++ = c; /* add it to the buffer */
|
---|
| 89 | *bp = '\0'; /* ... followed by a zero byte */
|
---|
| 90 |
|
---|
| 91 | cp = ctl; /* setup to scan ctl for the character */
|
---|
| 92 |
|
---|
[7258c6a] | 93 | for (j = 0; j < nc; j++) /* scan each byte of ctl */
|
---|
[f40a309] | 94 | if (*ctl++ EQ c ) /* done if we find it */
|
---|
| 95 | return((int16_t)c);
|
---|
| 96 |
|
---|
| 97 | BIOS(B_PUTC, unit, c); /* echo the character */
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return(ERR01); /* buffer full */
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[7258c6a] | 103 | /* |
---|
[f40a309] | 104 | */
|
---|
[7258c6a] | 105 |
|
---|
| 106 | int16_t getln(int16_t unit, int16_t nb, int8_t *buf)
|
---|
| 107 | {
|
---|
[f40a309] | 108 | register int8_t *bp;
|
---|
| 109 | register int8_t c;
|
---|
| 110 | register int16_t bc;
|
---|
| 111 |
|
---|
| 112 | bc = 0; /* number of characters currently in buffer */
|
---|
| 113 | bp = buf; /* current buffer pointer */
|
---|
| 114 | *bp = '\0'; /* initial null into buffer */
|
---|
| 115 |
|
---|
| 116 | while (bc LT nb) {
|
---|
| 117 |
|
---|
| 118 | c = BIOS(B_GETC, unit) & 0xFF;
|
---|
| 119 |
|
---|
| 120 | switch (c) {
|
---|
| 121 |
|
---|
| 122 | case A_BS:
|
---|
| 123 | case A_DEL:
|
---|
| 124 |
|
---|
| 125 | if (bc EQ 0) {
|
---|
| 126 |
|
---|
| 127 | BIOS(B_PUTC, unit, A_BEL);
|
---|
| 128 | break;
|
---|
| 129 |
|
---|
| 130 | } else {
|
---|
| 131 |
|
---|
| 132 | bc--;
|
---|
| 133 | bp--;
|
---|
| 134 | *bp = '\0';
|
---|
| 135 | BIOS(B_PUTC, unit, A_BS);
|
---|
| 136 | BIOS(B_PUTC, unit, ' ');
|
---|
| 137 | BIOS(B_PUTC, unit, A_BS);
|
---|
| 138 | break;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | case A_CR:
|
---|
| 142 | case A_LF:
|
---|
[7258c6a] | 143 |
|
---|
[f40a309] | 144 | *bp++ = c; /* put character in buffer */
|
---|
| 145 | *bp = '\0'; /* terminate line with null */
|
---|
| 146 | return((int16_t)c); /* return -- CR or LF hit */
|
---|
| 147 |
|
---|
| 148 | /* |
---|
| 149 | */
|
---|
[7258c6a] | 150 |
|
---|
[f40a309] | 151 | case CTL('X'):
|
---|
| 152 |
|
---|
| 153 | *buf = '\0'; /* clear the buffer */
|
---|
| 154 | return((int16_t)c); /* return -- line cancelled */
|
---|
| 155 |
|
---|
| 156 | default:
|
---|
| 157 |
|
---|
| 158 | *bp++ = c; /* put character in buffer */
|
---|
| 159 | *bp = '\0'; /* terminate line with null */
|
---|
| 160 | BIOS(B_PUTC, unit, c); /* echo the character */
|
---|
| 161 | bc++; /* update character count */
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | return(ERR01); /* buffer full error */
|
---|
[7258c6a] | 166 | }
|
---|
[f40a309] | 167 |
|
---|
[7258c6a] | 168 | /* |
---|
| 169 | */
|
---|
| 170 |
|
---|
[f40a309] | 171 | int16_t getrln(int16_t unit, int16_t nb, int8_t *buf)
|
---|
| 172 | {
|
---|
| 173 | register int8_t *bp;
|
---|
| 174 | register int8_t c;
|
---|
| 175 | register int16_t bc;
|
---|
| 176 |
|
---|
| 177 | bc = 0; /* number of characters currently in buffer */
|
---|
| 178 | bp = buf; /* current buffer pointer */
|
---|
| 179 | *bp = '\0'; /* initial null into buffer */
|
---|
| 180 |
|
---|
| 181 | while (bc LT nb) {
|
---|
| 182 |
|
---|
| 183 | c = BIOS(B_GETC, unit) & 0xFF;
|
---|
| 184 |
|
---|
| 185 | switch (c) {
|
---|
| 186 |
|
---|
| 187 | case A_CR:
|
---|
[7258c6a] | 188 | case A_LF:
|
---|
[f40a309] | 189 | case CTL('Z'):
|
---|
| 190 |
|
---|
| 191 | *bp++ = c; /* put character in buffer */
|
---|
| 192 | *bp = '\0'; /* terminate line with null */
|
---|
[7258c6a] | 193 | return((int16_t)c); /* return -- CR, LF, or ^Z hit */
|
---|
[f40a309] | 194 |
|
---|
| 195 | case CTL('X'):
|
---|
| 196 |
|
---|
| 197 | *buf = '\0'; /* clear the buffer */
|
---|
| 198 | return((int16_t)c); /* return -- line cancelled */
|
---|
| 199 |
|
---|
| 200 | default:
|
---|
| 201 |
|
---|
| 202 | *bp++ = c; /* put character in buffer */
|
---|
| 203 | *bp = '\0'; /* terminate line with null */
|
---|
| 204 | bc++; /* update character count */
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[7258c6a] | 208 | return(ERR01); /* buffer full error */
|
---|
[f40a309] | 209 | }
|
---|
[7258c6a] | 210 |
|
---|
| 211 | /* |
---|
[f40a309] | 212 | */
|
---|
| 213 |
|
---|
| 214 | void writeln(int16_t unit, int8_t *buf)
|
---|
| 215 | {
|
---|
| 216 | register int8_t *bp;
|
---|
| 217 | register int8_t c;
|
---|
| 218 |
|
---|
| 219 | bp = buf; /* setup buffer pointer */
|
---|
| 220 |
|
---|
| 221 | while (c = *bp++) /* send the string, a byte at a time */
|
---|
| 222 | BIOS(B_PUTC, unit, c);
|
---|
| 223 | }
|
---|