[f40a309] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | scan.c -- input scan conversion for the portable C I/O Library
|
---|
| 4 | Version 3 -- 1989-01-16 -- D.N. Lynx Crowe
|
---|
| 5 | =============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[b28a12e] | 8 | #include "ram.h"
|
---|
[f40a309] | 9 |
|
---|
[7258c6a] | 10 | static int16_t maxwide;
|
---|
| 11 | static int16_t (*gsub)();
|
---|
[f40a309] | 12 |
|
---|
[7258c6a] | 13 | static int8_t *scnstr;
|
---|
| 14 | static int8_t quit;
|
---|
[f40a309] | 15 |
|
---|
| 16 | /* |
---|
| 17 |
|
---|
| 18 | */
|
---|
[7258c6a] | 19 |
|
---|
[f40a309] | 20 | static int32_t getnum(int8_t *list, int8_t *values, int16_t base)
|
---|
[7258c6a] | 21 | {
|
---|
| 22 | register int32_t val;
|
---|
| 23 | register int8_t *cp;
|
---|
| 24 | int16_t c;
|
---|
[f40a309] | 25 | int16_t sign;
|
---|
| 26 |
|
---|
| 27 | if (maxwide LE 0)
|
---|
| 28 | return(0L);
|
---|
| 29 |
|
---|
| 30 | val = sign = 0;
|
---|
| 31 |
|
---|
| 32 | if ((c = (*gsub)(0)) EQ '-') {
|
---|
| 33 |
|
---|
| 34 | sign = 1;
|
---|
| 35 | --maxwide;
|
---|
| 36 |
|
---|
| 37 | } else if (c EQ '+')
|
---|
| 38 | --maxwide;
|
---|
| 39 | else
|
---|
| 40 | (*gsub)(1);
|
---|
| 41 |
|
---|
| 42 | while (maxwide--) {
|
---|
| 43 |
|
---|
| 44 | if ((cp = index(list, (*gsub)(0))) EQ NULL) {
|
---|
| 45 |
|
---|
| 46 | (*gsub)(1);
|
---|
| 47 | break;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[7258c6a] | 50 | val *= base;
|
---|
[f40a309] | 51 | val += values[(int32_t)cp - (int32_t)list];
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | if (sign)
|
---|
| 55 | val = -val;
|
---|
| 56 |
|
---|
| 57 | return(val);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /* |
---|
| 61 |
|
---|
[7258c6a] | 62 | */
|
---|
[f40a309] | 63 |
|
---|
| 64 | static int16_t skipblk(void)
|
---|
| 65 | {
|
---|
| 66 | while (isspace((*gsub)(0)))
|
---|
| 67 | ;
|
---|
| 68 |
|
---|
| 69 | if ((*gsub)(1) EQ EOF)
|
---|
| 70 | return(EOF);
|
---|
| 71 |
|
---|
| 72 | return(0);
|
---|
[7258c6a] | 73 | }
|
---|
[f40a309] | 74 |
|
---|
| 75 | static int16_t sgetc(int16_t what)
|
---|
| 76 | {
|
---|
| 77 | if (what EQ 0) {
|
---|
| 78 |
|
---|
| 79 | if (*scnstr)
|
---|
| 80 | return(*scnstr++ & 0x00FF);
|
---|
| 81 |
|
---|
| 82 | quit = TRUE;
|
---|
| 83 |
|
---|
| 84 | } else {
|
---|
| 85 |
|
---|
| 86 | if (!quit)
|
---|
| 87 | return(*--scnstr & 0x00FF);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return(EOF);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /* |
---|
[7258c6a] | 94 |
|
---|
[f40a309] | 95 | */
|
---|
| 96 |
|
---|
| 97 | int16_t scanfmt(int16_t (*getsub)(), int8_t *fmt, int16_t **args)
|
---|
| 98 | {
|
---|
| 99 |
|
---|
| 100 | #ifdef FLOAT
|
---|
[7258c6a] | 101 | double getflt(), d;
|
---|
| 102 | #endif
|
---|
| 103 |
|
---|
| 104 | int32_t lv;
|
---|
[f40a309] | 105 | int16_t c, count, dontdo, lflag, base;
|
---|
[7258c6a] | 106 | int8_t *cp;
|
---|
[f40a309] | 107 | int8_t tlist[130];
|
---|
[7258c6a] | 108 |
|
---|
[f40a309] | 109 | static int8_t list[] = "ABCDEFabcdef9876543210";
|
---|
| 110 |
|
---|
| 111 | static int8_t vals[] = {
|
---|
| 112 |
|
---|
| 113 | 10,11,12,13,14,15,10,11,12,13,14,15,9,8,7,6,5,4,3,2,1,0
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | /* |
---|
| 117 |
|
---|
| 118 | */
|
---|
| 119 | count = 0;
|
---|
| 120 | gsub = getsub;
|
---|
| 121 |
|
---|
| 122 | while (c = *fmt++) {
|
---|
| 123 |
|
---|
| 124 | if (c EQ '%') {
|
---|
| 125 |
|
---|
| 126 | lflag = dontdo = FALSE;
|
---|
| 127 | maxwide = 127;
|
---|
| 128 |
|
---|
| 129 | if (*fmt EQ '*') {
|
---|
| 130 | ++fmt;
|
---|
| 131 | dontdo = 1;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | if (isdigit(*fmt)) {
|
---|
| 135 |
|
---|
| 136 | maxwide = 0;
|
---|
| 137 |
|
---|
| 138 | do {
|
---|
| 139 |
|
---|
| 140 | maxwide = maxwide*10 + *fmt - '0';
|
---|
| 141 |
|
---|
| 142 | } while (isdigit(*++fmt));
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (*fmt EQ 'l') {
|
---|
| 146 |
|
---|
| 147 | lflag = TRUE;
|
---|
| 148 | ++fmt;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /* |
---|
| 152 |
|
---|
| 153 | */
|
---|
| 154 | switch (*fmt++) {
|
---|
| 155 |
|
---|
| 156 | case '%':
|
---|
| 157 | c = '%';
|
---|
| 158 | goto matchit;
|
---|
| 159 |
|
---|
| 160 | case 'h': /* specify short (for compatibility) */
|
---|
| 161 | lflag = FALSE;
|
---|
| 162 | goto decimal;
|
---|
| 163 |
|
---|
| 164 | case 'D':
|
---|
| 165 | lflag = TRUE;
|
---|
| 166 |
|
---|
| 167 | case 'd':
|
---|
| 168 | decimal:
|
---|
| 169 | c = 12;
|
---|
| 170 | base = 10;
|
---|
| 171 | goto getval;
|
---|
| 172 |
|
---|
| 173 | case 'X':
|
---|
| 174 | lflag = TRUE;
|
---|
| 175 |
|
---|
| 176 | case 'x':
|
---|
| 177 | c = 0;
|
---|
| 178 | base = 16;
|
---|
| 179 | goto getval;
|
---|
| 180 |
|
---|
| 181 | case 'O':
|
---|
| 182 | lflag = TRUE;
|
---|
| 183 |
|
---|
| 184 | case 'o':
|
---|
| 185 | c = 14;
|
---|
| 186 | base = 8;
|
---|
| 187 | getval:
|
---|
| 188 | if (skipblk())
|
---|
| 189 | goto ateof;
|
---|
| 190 |
|
---|
[7258c6a] | 191 | lv = getnum(&list[c], &vals[c], base);
|
---|
[f40a309] | 192 |
|
---|
| 193 | if (!dontdo) {
|
---|
| 194 |
|
---|
| 195 | if (lflag)
|
---|
| 196 | *(int32_t *)(*args++) = lv;
|
---|
| 197 | else
|
---|
| 198 | **args++ = lv;
|
---|
| 199 | ++count;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | break;
|
---|
| 203 | /* |
---|
| 204 |
|
---|
| 205 | */
|
---|
| 206 |
|
---|
| 207 | #ifdef FLOAT
|
---|
| 208 | case 'E':
|
---|
| 209 | case 'F':
|
---|
| 210 | lflag = TRUE;
|
---|
| 211 |
|
---|
| 212 | case 'e':
|
---|
| 213 | case 'f':
|
---|
| 214 | if (skipblk())
|
---|
| 215 | goto ateof;
|
---|
| 216 |
|
---|
| 217 | d = getflt(tlist);
|
---|
| 218 |
|
---|
| 219 | if (!dontdo) {
|
---|
| 220 |
|
---|
| 221 | if (lflag)
|
---|
| 222 | *(double *)(*args++) = d;
|
---|
| 223 | else
|
---|
| 224 | *(float *)(*args++) = d;
|
---|
| 225 | ++count;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | break;
|
---|
| 229 | #endif
|
---|
| 230 |
|
---|
| 231 | /* |
---|
| 232 |
|
---|
| 233 | */
|
---|
| 234 |
|
---|
| 235 | case '[':
|
---|
| 236 | lflag = FALSE;
|
---|
| 237 |
|
---|
| 238 | if (*fmt EQ '^' || *fmt EQ '~') {
|
---|
| 239 |
|
---|
| 240 | ++fmt;
|
---|
| 241 | lflag = TRUE;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | for (cp = tlist ; (c = *fmt++) != ']' ; )
|
---|
| 245 | *cp++ = c;
|
---|
| 246 |
|
---|
| 247 | *cp = 0;
|
---|
| 248 | goto string;
|
---|
| 249 |
|
---|
| 250 | case 's':
|
---|
| 251 | lflag = TRUE;
|
---|
| 252 | tlist[0] = ' ';
|
---|
| 253 | tlist[1] = '\t';
|
---|
| 254 | tlist[2] = '\n';
|
---|
| 255 | tlist[3] = 0;
|
---|
| 256 | string:
|
---|
| 257 | if (skipblk())
|
---|
| 258 | goto ateof;
|
---|
| 259 |
|
---|
| 260 | if (!dontdo)
|
---|
| 261 | cp = *args++;
|
---|
| 262 |
|
---|
| 263 | while (maxwide--) {
|
---|
| 264 |
|
---|
| 265 | if ((c = (*gsub)(0)) EQ EOF)
|
---|
| 266 | break;
|
---|
| 267 |
|
---|
| 268 | if (lflag ?
|
---|
| 269 | (index(tlist, c) NE 0) :
|
---|
| 270 | (index(tlist, c) EQ 0)) {
|
---|
| 271 |
|
---|
| 272 | (*gsub)(1); /* unget last character */
|
---|
| 273 | break;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | if (!dontdo)
|
---|
| 277 | *cp++ = c;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | if (!dontdo) {
|
---|
| 281 |
|
---|
| 282 | *cp = 0;
|
---|
| 283 | ++count;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | break;
|
---|
| 287 | /* |
---|
[7258c6a] | 288 |
|
---|
[f40a309] | 289 | */
|
---|
| 290 | case 'c':
|
---|
| 291 | if ((c = (*gsub)(0)) EQ EOF)
|
---|
| 292 | goto ateof;
|
---|
| 293 |
|
---|
| 294 | if (!dontdo) {
|
---|
| 295 |
|
---|
| 296 | *(int8_t *)(*args++) = c;
|
---|
| 297 | ++count;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | break;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | } else if (isspace(c)) {
|
---|
| 304 |
|
---|
| 305 | if (skipblk()) {
|
---|
| 306 | ateof:
|
---|
| 307 | if (count EQ 0)
|
---|
| 308 | return(EOF);
|
---|
| 309 |
|
---|
| 310 | return(count);
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | } else {
|
---|
| 314 |
|
---|
| 315 | matchit:
|
---|
| 316 | if (skipblk())
|
---|
| 317 | goto ateof;
|
---|
| 318 |
|
---|
| 319 | if ((*gsub)(0) != c)
|
---|
| 320 | return(count);
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | return(count);
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /* |
---|
| 328 |
|
---|
| 329 | */
|
---|
| 330 |
|
---|
| 331 | #ifdef FLOAT
|
---|
| 332 |
|
---|
| 333 | double
|
---|
| 334 | getflt(buffer)
|
---|
| 335 | char *buffer;
|
---|
| 336 | {
|
---|
| 337 | register int c;
|
---|
| 338 | char decpt, sign, exp;
|
---|
| 339 | register char *cp;
|
---|
| 340 | double atof();
|
---|
| 341 |
|
---|
| 342 | cp = buffer;
|
---|
| 343 | sign = exp = decpt = 0;
|
---|
| 344 |
|
---|
| 345 | while (maxwide--) {
|
---|
| 346 |
|
---|
| 347 | c = (*gsub)(0);
|
---|
| 348 |
|
---|
| 349 | if (!sign AND (c EQ '-' OR c EQ '+'))
|
---|
| 350 | sign = 1;
|
---|
| 351 | else if (!decpt AND c EQ '.')
|
---|
| 352 | decpt = 1;
|
---|
| 353 | else if (!exp AND (c EQ 'e' OR c EQ 'E')) {
|
---|
| 354 |
|
---|
| 355 | sign = 0;
|
---|
| 356 | exp = decpt = 1;
|
---|
| 357 |
|
---|
| 358 | } else if (!isdigit(c)) {
|
---|
| 359 |
|
---|
| 360 | (*gsub)(1);
|
---|
| 361 | break;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | *cp++ = c;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[7258c6a] | 367 | *cp = 0;
|
---|
[f40a309] | 368 | return(atof(buffer));
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | #endif
|
---|
| 372 |
|
---|
| 373 | /* |
---|
[6262b5c] | 374 |
|
---|
| 375 | */
|
---|
| 376 |
|
---|
| 377 | int16_t sscanf(int8_t *string, int8_t *fmt, int16_t *args)
|
---|
| 378 | {
|
---|
| 379 | scnstr = string;
|
---|
| 380 | quit = FALSE;
|
---|
| 381 | return(scanfmt(sgetc, fmt, &args));
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 |
|
---|