ack/lang/cem/libcc/stdio/doscan.c

298 lines
5.8 KiB
C
Raw Normal View History

1987-03-31 10:45:53 +00:00
/* $Header$ */
1987-01-27 15:57:55 +00:00
#include <stdio.h>
#include <ctype.h>
1988-04-15 14:43:19 +00:00
#include <varargs.h>
1987-01-27 15:57:55 +00:00
union ptr_union {
char *chr_p;
unsigned short *ushort_p;
unsigned int *uint_p;
unsigned long *ulong_p;
#ifndef NOFLOAT
float *float_p;
double *double_p;
#endif
};
static char Xtable[128];
/*
* the routine that does the job
*/
1988-04-15 14:43:19 +00:00
_doscanf (iop, format, ap)
1987-01-27 15:57:55 +00:00
register FILE *iop;
char *format; /* the format control string */
1988-04-15 14:43:19 +00:00
va_list ap;
1987-01-27 15:57:55 +00:00
{
int done = 0; /* number of items done */
int base; /* conversion base */
long val; /* an integer value */
int sign; /* sign flag */
int do_assign; /* assignment suppression flag */
unsigned width; /* width of field */
int widflag; /* width was specified */
int longflag; /* true if long */
int shortflag; /* true if short */
int done_some; /* true if we have seen some data */
int reverse; /* reverse the checking in [...] */
int kind;
register int ic;
#ifndef NOFLOAT
extern double atof();
int dotseen;
int expseen;
char buffer[128];
#endif
ic = getc(iop);
if (ic == EOF) {
done = EOF;
goto quit;
}
while (1) {
if (isspace(*format)) {
while (isspace (*format))
++format; /* skip whitespace */
while (isspace (ic)) ic = getc(iop);
}
if (!*format)
goto all_done; /* end of format */
if (ic < 0)
goto quit; /* seen an error */
if (*format != '%') {
if (ic != *format)
goto all_done;
++format;
ic = getc(iop);
continue;
}
++format;
do_assign = 1;
if (*format == '*') {
++format;
do_assign = 0;
}
if (isdigit (*format)) {
widflag = 1;
for (width = 0; isdigit (*format);)
width = width * 10 + *format++ - '0';
} else
widflag = 0; /* no width spec */
if (longflag = (tolower (*format) == 'l'))
++format;
else if (shortflag = (tolower(*format) == 'h'))
++format;
if (isupper(*format)) {
kind = tolower(*format);
longflag = 1;
}
else kind = *format;
if (kind != 'c')
while (isspace (ic))
ic = getc(iop);
done_some = 0; /* nothing yet */
switch (kind) {
case 'o':
base = 8;
goto decimal;
case 'u':
case 'd':
base = 10;
goto decimal;
case 'x':
base = 16;
if (((!widflag) || width >= 2) && ic == '0') {
ic = getc(iop);
if (tolower (ic) == 'x') {
width -= 2;
done_some = 1;
ic = getc(iop);
} else {
ungetc(ic, iop);
ic = '0';
}
}
decimal:
val = 0L; /* our result value */
sign = 0; /* assume positive */
if (!widflag)
width = 0xffff; /* very wide */
if (width && ic == '+')
ic = getc(iop);
else if (width && ic == '-') {
sign = 1;
ic = getc(iop);
}
while (width--) {
if (isdigit (ic) && ic - '0' < base)
ic -= '0';
else if (base == 16 && tolower (ic) >= 'a' && tolower (ic) <= 'f')
ic = 10 + tolower (ic) - 'a';
else
break;
val = val * base + ic;
ic = getc(iop);
done_some = 1;
}
if (do_assign) {
if (sign)
val = -val;
if (longflag)
1988-04-15 14:43:19 +00:00
*va_arg(ap, unsigned long *) = (unsigned long) val;
1987-01-27 15:57:55 +00:00
else if (shortflag)
1988-04-15 14:43:19 +00:00
*va_arg(ap, unsigned short *) = (unsigned short) val;
1987-01-27 15:57:55 +00:00
else
1988-04-15 14:43:19 +00:00
*va_arg(ap, unsigned *) = (unsigned) val;
1987-01-27 15:57:55 +00:00
}
1988-04-15 14:43:19 +00:00
if (done_some) {
1988-02-16 17:48:28 +00:00
if (do_assign) ++done;
1988-04-15 14:43:19 +00:00
}
1987-01-27 15:57:55 +00:00
else
goto all_done;
break;
case 'c':
if (!widflag)
width = 1;
1988-04-15 14:43:19 +00:00
{ register char *p;
if (do_assign)
p = va_arg(ap, char *);
while (width-- && ic >= 0) {
1987-01-27 15:57:55 +00:00
if (do_assign)
1988-04-15 14:43:19 +00:00
*p++ = (char) ic;
1987-01-27 15:57:55 +00:00
ic = getc(iop);
done_some = 1;
1988-04-15 14:43:19 +00:00
}
1987-01-27 15:57:55 +00:00
}
1988-02-16 17:48:28 +00:00
if (do_assign) {
if (done_some)
++done;
}
1987-01-27 15:57:55 +00:00
break;
case 's':
if (!widflag)
width = 0xffff;
1988-04-15 14:43:19 +00:00
{ register char *p;
if (do_assign)
p = va_arg(ap, char *);
while (width-- && !isspace (ic) && ic > 0) {
1987-01-27 15:57:55 +00:00
if (do_assign)
1988-04-15 14:43:19 +00:00
*p++ = (char) ic;
1987-01-27 15:57:55 +00:00
ic = getc(iop);
done_some = 1;
1988-04-15 14:43:19 +00:00
}
if (do_assign) /* terminate the string */
*p = '\0';
}
if (done_some) {
if (do_assign)
++done;
1987-01-27 15:57:55 +00:00
}
else
goto all_done;
break;
case '[':
if (!widflag)
width = 0xffff;
if ( *(++format) == '^' ) {
reverse = 1;
format++;
} else
reverse = 0;
{ register char *c;
for (c = Xtable; c < &Xtable[128]; c++) *c = 0;
}
while (*format && *format != ']') {
1987-11-20 12:57:51 +00:00
Xtable[*format++] = 1;
1987-01-27 15:57:55 +00:00
}
if (!*format)
goto quit;
1988-04-15 14:43:19 +00:00
{ register char *p;
if (do_assign)
p = va_arg(ap, char *);
while (width-- && !isspace (ic) && ic > 0 &&
1987-01-27 15:57:55 +00:00
(Xtable[ic] ^ reverse)) {
if (do_assign)
1988-04-15 14:43:19 +00:00
*p++ = (char) ic;
1987-01-27 15:57:55 +00:00
ic = getc(iop);
done_some = 1;
1988-04-15 14:43:19 +00:00
}
if (do_assign) /* terminate the string */
*p = '\0';
}
if (done_some) {
if (do_assign)
++done;
1987-01-27 15:57:55 +00:00
}
else
goto all_done;
break;
#ifndef NOFLOAT:
case 'e':
case 'f': {
register char *c = buffer;
if (!widflag) width = 127;
1987-01-27 15:57:55 +00:00
if (width >= 128) width = 127;
if (width && (ic == '+' || ic == '-')) {
*c++ = ic;
width--;
ic = getc(iop);
}
while (isdigit(ic) && width) {
width--;
*c++ = ic;
ic = getc(iop);
}
if (ic == '.' && width) {
width--;
*c++ = ic;
ic = getc(iop);
}
while (isdigit(ic) && width) {
width--;
*c++ = ic;
ic = getc(iop);
}
if (width && (ic == 'e' || ic == 'E')) {
width--;
*c++ = ic;
ic = getc(iop);
if (width && (ic == '+' || ic == '-')) {
width--;
*c++ = ic;
ic = getc(iop);
}
}
while (isdigit(ic) && width) {
width--;
*c++ = ic;
ic = getc(iop);
}
if (c == buffer) goto all_done;
*c = 0;
if (do_assign) {
1988-02-16 17:48:28 +00:00
done++;
1987-01-27 15:57:55 +00:00
if (longflag)
1988-04-15 14:43:19 +00:00
*va_arg(ap, double *) = atof(buffer);
1987-01-27 15:57:55 +00:00
else
1988-04-15 14:43:19 +00:00
*va_arg(ap, float *) = atof(buffer);
1987-01-27 15:57:55 +00:00
}
}
break;
#endif
} /* end switch */
++format;
}
all_done:
if (ic >= 0)
ungetc(ic, iop);
quit:
return done;
}