lib: vsscanf: Support gcc-4.6.4: Support *, [, s.
* lib/stdio/vsscanf.c (vsscanf): Support *, [, s. * lib/stdio/vfscanf.c (vfscanf): Likewise. * lib/tests/stdio/90-sscanf.c: Test it. * build-aux/check-mescc.sh (tests): Run it. * lib/x86-mes/x86.M1 (mov____%al,0x32(%ebp): New macro.
This commit is contained in:
parent
571ea5ce9e
commit
c45cbff8ba
|
@ -223,6 +223,7 @@ lib/tests/stdio/90-fopen-append.c
|
||||||
lib/tests/stdio/90-fread-fwrite.c
|
lib/tests/stdio/90-fread-fwrite.c
|
||||||
lib/tests/stdio/90-fseek.c
|
lib/tests/stdio/90-fseek.c
|
||||||
lib/tests/stdio/90-sprintf.c
|
lib/tests/stdio/90-sprintf.c
|
||||||
|
lib/tests/stdio/90-sscanf.c
|
||||||
lib/tests/stdlib/90-strtol.c
|
lib/tests/stdlib/90-strtol.c
|
||||||
lib/tests/string/90-snprintf.c
|
lib/tests/string/90-snprintf.c
|
||||||
lib/tests/string/90-strpbrk.c
|
lib/tests/string/90-strpbrk.c
|
||||||
|
@ -291,6 +292,7 @@ lib/tests/scaffold/70-extern.c
|
||||||
lib/tests/stdio/80-sscanf.c
|
lib/tests/stdio/80-sscanf.c
|
||||||
lib/tests/posix/90-execlp.c
|
lib/tests/posix/90-execlp.c
|
||||||
lib/tests/string/90-snprintf.c
|
lib/tests/string/90-snprintf.c
|
||||||
|
lib/tests/stdio/90-sscanf.c
|
||||||
"
|
"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -22,62 +22,81 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
vfscanf (FILE * stream, char const *template, va_list ap)
|
vfscanf (FILE * stream, char const *template, va_list ap)
|
||||||
{
|
{
|
||||||
char p = fgetc (stream);
|
char r = fgetc (stream);
|
||||||
char const *t = template;
|
char const *t = template;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while (*t && p != EOF)
|
while (*t && r != EOF)
|
||||||
if (*t != '%')
|
if (*t != '%')
|
||||||
{
|
{
|
||||||
t++;
|
t++;
|
||||||
p = fgetc (stream);
|
r = fgetc (stream);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
t++;
|
t++;
|
||||||
char c = *t;
|
char c = *t;
|
||||||
|
int skip_p = 0;
|
||||||
|
int length = -1;
|
||||||
|
if (c == '*')
|
||||||
|
skip_p = 1;
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
{
|
||||||
|
length = abtol (&t, 10);
|
||||||
|
c = *t;
|
||||||
|
}
|
||||||
if (c == 'l')
|
if (c == 'l')
|
||||||
c = *++t;
|
c = *++t;
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '%':
|
case '%':
|
||||||
{
|
{
|
||||||
p = fgetc (stream);
|
r = fgetc (stream);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'c':
|
case 'c':
|
||||||
{
|
{
|
||||||
char *c = va_arg (ap, char *);
|
r = fgetc (stream);
|
||||||
*c = p;
|
if (!skip_p)
|
||||||
p = fgetc (stream);
|
{
|
||||||
count++;
|
char *c = va_arg (ap, char *);
|
||||||
|
*c = r;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd':
|
case 'd':
|
||||||
case 'i':
|
case 'i':
|
||||||
case 'u':
|
case 'u':
|
||||||
{
|
{
|
||||||
int *d = va_arg (ap, int *);
|
int *d = skip_p ? 0 : va_arg (ap, int *);
|
||||||
char buf[20];
|
char buf[20];
|
||||||
char *q = buf;
|
char *q = buf;
|
||||||
if (p == '+' || p == '-')
|
if (r == '+' || r == '-')
|
||||||
{
|
{
|
||||||
*q++ = p;
|
*q++ = r;
|
||||||
p = fgetc (stream);
|
r = fgetc (stream);
|
||||||
}
|
}
|
||||||
while (isdigit (p))
|
while (isdigit (r))
|
||||||
{
|
{
|
||||||
*q++ = p;
|
*q++ = r;
|
||||||
p = fgetc (stream);
|
r = fgetc (stream);
|
||||||
}
|
}
|
||||||
ungetc (p, stream);
|
ungetc (r, stream);
|
||||||
*q = 0;
|
*q = 0;
|
||||||
q = buf;
|
q = buf;
|
||||||
*d = abtol (&q, 10);
|
if (skip_p)
|
||||||
count++;
|
abtol ((char const **) &q, 10);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*d = abtol ((char const **) &q, 10);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'e':
|
case 'e':
|
||||||
|
@ -86,24 +105,89 @@ vfscanf (FILE * stream, char const *template, va_list ap)
|
||||||
case 'E':
|
case 'E':
|
||||||
case 'G':
|
case 'G':
|
||||||
{
|
{
|
||||||
float *f = va_arg (ap, float *);
|
float *f = skip_p ? 0 : va_arg (ap, float *);
|
||||||
char buf[20];
|
char buf[20];
|
||||||
char *q = buf;
|
char *q = buf;
|
||||||
if (p == '+' || p == '-')
|
if (r == '+' || r == '-')
|
||||||
{
|
{
|
||||||
*q++ = p;
|
*q++ = r;
|
||||||
p = fgetc (stream);
|
r = fgetc (stream);
|
||||||
}
|
}
|
||||||
while (isdigit (p))
|
while (isdigit (r))
|
||||||
{
|
{
|
||||||
*q++ = p;
|
*q++ = r;
|
||||||
p = fgetc (stream);
|
r = fgetc (stream);
|
||||||
}
|
}
|
||||||
ungetc (p, stream);
|
ungetc (r, stream);
|
||||||
*q = 0;
|
*q = 0;
|
||||||
q = buf;
|
q = buf;
|
||||||
*f = strtod (q, &q);
|
if (skip_p)
|
||||||
count++;
|
strtod (q, &q);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*f = strtod (q, &q);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
char *s = skip_p ? 0 : va_arg (ap, char *);
|
||||||
|
while (r && !isspace (r) && (length == -1 || length--))
|
||||||
|
{
|
||||||
|
if (!skip_p)
|
||||||
|
*s++ = r;
|
||||||
|
r = fgetc (stream);
|
||||||
|
}
|
||||||
|
if (!skip_p)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
*s = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case '[':
|
||||||
|
{
|
||||||
|
char *s = skip_p ? 0 : va_arg (ap, char *);
|
||||||
|
char set[1024];
|
||||||
|
int i = 0;
|
||||||
|
int not_in_set_p = 0;
|
||||||
|
t++;
|
||||||
|
if (*t == '^')
|
||||||
|
{
|
||||||
|
not_in_set_p = 1;
|
||||||
|
t++;
|
||||||
|
}
|
||||||
|
if (*t == ']' || *t == '-')
|
||||||
|
set[i++] = *t++;
|
||||||
|
while (*t && *t != ']')
|
||||||
|
{
|
||||||
|
if (*t == '-')
|
||||||
|
{
|
||||||
|
char end = *t++;
|
||||||
|
for (char x = set[i - 1] + 1; x < end; x++)
|
||||||
|
set[i++] = x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
set[i++] = *t++;
|
||||||
|
}
|
||||||
|
set[i] = 0;
|
||||||
|
while (r && (length == -1 || length--))
|
||||||
|
{
|
||||||
|
int match = (int) (long) strchr (set, r);
|
||||||
|
if (not_in_set_p)
|
||||||
|
match = !match;
|
||||||
|
if (!match)
|
||||||
|
break;
|
||||||
|
if (!skip_p)
|
||||||
|
*s++ = r;
|
||||||
|
r = fgetc (stream);
|
||||||
|
}
|
||||||
|
if (!skip_p)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
*s = 0;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -114,7 +198,7 @@ vfscanf (FILE * stream, char const *template, va_list ap)
|
||||||
eputs (template);
|
eputs (template);
|
||||||
eputs ("\n");
|
eputs ("\n");
|
||||||
t++;
|
t++;
|
||||||
p = fgetc (stream);
|
r = fgetc (stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t++;
|
t++;
|
||||||
|
|
|
@ -41,6 +41,15 @@ vsscanf (char const *s, char const *template, va_list ap)
|
||||||
{
|
{
|
||||||
t++;
|
t++;
|
||||||
char c = *t;
|
char c = *t;
|
||||||
|
int skip_p = 0;
|
||||||
|
int length = -1;
|
||||||
|
if (c == '*')
|
||||||
|
skip_p = 1;
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
{
|
||||||
|
length = abtol (&t, 10);
|
||||||
|
c = *t;
|
||||||
|
}
|
||||||
if (c == 'l')
|
if (c == 'l')
|
||||||
c = *++t;
|
c = *++t;
|
||||||
switch (c)
|
switch (c)
|
||||||
|
@ -52,18 +61,27 @@ vsscanf (char const *s, char const *template, va_list ap)
|
||||||
}
|
}
|
||||||
case 'c':
|
case 'c':
|
||||||
{
|
{
|
||||||
char *c = va_arg (ap, char *);
|
char r = *p++;
|
||||||
*c = *p++;
|
if (!skip_p)
|
||||||
count++;
|
{
|
||||||
|
char *c = va_arg (ap, char *);
|
||||||
|
*c = r;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'd':
|
case 'd':
|
||||||
case 'i':
|
case 'i':
|
||||||
case 'u':
|
case 'u':
|
||||||
{
|
{
|
||||||
int *d = va_arg (ap, int *);
|
if (skip_p)
|
||||||
*d = abtol ((char const **)&p, 10);
|
abtol ((char const **) &p, 10);
|
||||||
count++;
|
else
|
||||||
|
{
|
||||||
|
int *d = va_arg (ap, int *);
|
||||||
|
*d = abtol ((char const **) &p, 10);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'e':
|
case 'e':
|
||||||
|
@ -72,9 +90,76 @@ vsscanf (char const *s, char const *template, va_list ap)
|
||||||
case 'E':
|
case 'E':
|
||||||
case 'G':
|
case 'G':
|
||||||
{
|
{
|
||||||
float *f = va_arg (ap, float *);
|
if (skip_p)
|
||||||
*f = strtod (p, &p);
|
strtod (p, &p);
|
||||||
count++;
|
else
|
||||||
|
{
|
||||||
|
float *f = va_arg (ap, float *);
|
||||||
|
*f = strtod (p, &p);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
char *s = skip_p ? 0 : va_arg (ap, char *);
|
||||||
|
char r = *p;
|
||||||
|
while (r && !isspace (r) && (length == -1 || length--))
|
||||||
|
{
|
||||||
|
if (!skip_p)
|
||||||
|
*s++ = r;
|
||||||
|
r = *++p;
|
||||||
|
}
|
||||||
|
if (!skip_p)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
*s = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case '[':
|
||||||
|
{
|
||||||
|
char *s = skip_p ? 0 : va_arg (ap, char *);
|
||||||
|
char set[1024];
|
||||||
|
int i = 0;
|
||||||
|
int not_in_set_p = 0;
|
||||||
|
t++;
|
||||||
|
if (*t == '^')
|
||||||
|
{
|
||||||
|
not_in_set_p = 1;
|
||||||
|
t++;
|
||||||
|
}
|
||||||
|
if (*t == ']' || *t == '-')
|
||||||
|
set[i++] = *t++;
|
||||||
|
while (*t && *t != ']')
|
||||||
|
{
|
||||||
|
if (*t == '-')
|
||||||
|
{
|
||||||
|
char end = *t++;
|
||||||
|
for (char x = set[i - 1] + 1; x < end; x++)
|
||||||
|
set[i++] = x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
set[i++] = *t++;
|
||||||
|
}
|
||||||
|
set[i] = 0;
|
||||||
|
char r = *p;
|
||||||
|
while (r && (length == -1 || length--))
|
||||||
|
{
|
||||||
|
int match = (int) (long) strchr (set, r);
|
||||||
|
if (not_in_set_p)
|
||||||
|
match = !match;
|
||||||
|
if (!match)
|
||||||
|
break;
|
||||||
|
if (!skip_p)
|
||||||
|
*s++ = r;
|
||||||
|
r = *++p;
|
||||||
|
}
|
||||||
|
if (!skip_p)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
*s = 0;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
/* -*-comment-start: "//";comment-end:""-*-
|
||||||
|
* GNU Mes --- Maxwell Equations of Software
|
||||||
|
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
||||||
|
*
|
||||||
|
* This file is part of GNU Mes.
|
||||||
|
*
|
||||||
|
* GNU Mes is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 3 of the License, or (at
|
||||||
|
* your option) any later version.
|
||||||
|
*
|
||||||
|
* GNU Mes is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <mes/lib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main ()
|
||||||
|
{
|
||||||
|
char buf_abc[20];
|
||||||
|
char buf_xxx[20];
|
||||||
|
char buf_0_9[20];
|
||||||
|
int i = sscanf ("@abcxxx x0", "@%5[a-bc]%s%[^0-9]", &buf_abc, &buf_xxx, &buf_0_9);
|
||||||
|
if (i != 3)
|
||||||
|
return 1;
|
||||||
|
eputs ("buf_abc=");
|
||||||
|
eputs (buf_abc);
|
||||||
|
eputs ("\n");
|
||||||
|
eputs ("buf_xxx=");
|
||||||
|
eputs (buf_xxx);
|
||||||
|
eputs ("\n");
|
||||||
|
eputs ("buf_0_9=");
|
||||||
|
eputs (buf_0_9);
|
||||||
|
eputs ("\n");
|
||||||
|
if (strcmp (buf_abc, "abc"))
|
||||||
|
return 2;
|
||||||
|
if (strcmp (buf_xxx, "xxx"))
|
||||||
|
return 3;
|
||||||
|
if (strcmp (buf_0_9, " x"))
|
||||||
|
return 4;
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -85,6 +85,7 @@ DEFINE mov____$i32,0x8(%ebp) c745
|
||||||
DEFINE mov____$i8,%al b0
|
DEFINE mov____$i8,%al b0
|
||||||
DEFINE mov____$i8,%eax b0
|
DEFINE mov____$i8,%eax b0
|
||||||
DEFINE mov____%al,(%ebx) 8803
|
DEFINE mov____%al,(%ebx) 8803
|
||||||
|
DEFINE mov____%al,0x32(%ebp) 8885
|
||||||
DEFINE mov____%al,0x8(%ebp) 8845
|
DEFINE mov____%al,0x8(%ebp) 8845
|
||||||
DEFINE mov____%ax,(%ebx) 668903
|
DEFINE mov____%ax,(%ebx) 668903
|
||||||
DEFINE mov____%ax,0x8(%ebp) 668945
|
DEFINE mov____%ax,0x8(%ebp) 668945
|
||||||
|
|
Loading…
Reference in New Issue