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