mescc: Mes C Library: Support gcc-4.6.4: vsscanf: 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:
Jan Nieuwenhuizen 2019-02-09 21:42:17 +01:00
parent 44add95226
commit 39693eca63
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
5 changed files with 251 additions and 38 deletions

View File

@ -202,6 +202,7 @@ lib/tests/stdio/90-fopen-append
lib/tests/stdio/90-fread-fwrite
lib/tests/stdio/90-fseek
lib/tests/stdio/90-sprintf
lib/tests/stdio/90-sscanf
lib/tests/stdlib/90-strtol
lib/tests/string/90-snprintf
lib/tests/string/90-strpbrk

View File

@ -25,58 +25,75 @@
int
vfscanf (FILE *stream, char const *template, va_list ap)
{
char p = fgetc (stream);
char r = fgetc (stream);
char const *t = template;
int count = 0;
while (*t && p != EOF)
while (*t && r != EOF)
if (*t != '%')
{
t++;
p = fgetc (stream);
r = fgetc (stream);
}
else
{
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')
c = *++t;
switch (c)
{
case '%':
{
p = fgetc (stream);
r = fgetc (stream);
break;
}
case 'c':
{
char *c = va_arg (ap, char*);
*c = p;
p = fgetc (stream);
count++;
r = fgetc (stream);
if (!skip_p)
{
char *c = va_arg (ap, char*);
*c = r;
count++;
}
break;
}
case 'd':
case 'i':
case 'u':
{
int *d = va_arg (ap, int*);
int *d = skip_p ? 0 : va_arg (ap, int*);
char buf[20];
char *q = buf;
if (p == '+' || p == '-')
if (r == '+' || r == '-')
{
*q++ = p;
p = fgetc (stream);
*q++ = r;
r = fgetc (stream);
}
while (isdigit (p))
while (isdigit (r))
{
*q++ = p;
p = fgetc (stream);
*q++ = r;
r = fgetc (stream);
}
ungetc (p, stream);
ungetc (r, stream);
*q = 0;
q = buf;
*d = abtol (&q, 10);
count++;
if (skip_p)
abtol (&q, 10);
else
{
*d = abtol (&q, 10);
count++;
}
break;
}
case 'e':
@ -85,24 +102,89 @@ vfscanf (FILE *stream, char const *template, va_list ap)
case 'E':
case 'G':
{
float *f = va_arg (ap, float*);
float *f = skip_p ? 0 : va_arg (ap, float*);
char buf[20];
char *q = buf;
if (p == '+' || p == '-')
if (r == '+' || r == '-')
{
*q++ = p;
p = fgetc (stream);
*q++ = r;
r = fgetc (stream);
}
while (isdigit (p))
while (isdigit (r))
{
*q++ = p;
p = fgetc (stream);
*q++ = r;
r = fgetc (stream);
}
ungetc (p, stream);
ungetc (r, stream);
*q = 0;
q = buf;
*f = strtod (q, &q);
count++;
if (skip_p)
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 = 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;
}
default:
@ -113,7 +195,7 @@ vfscanf (FILE *stream, char const *template, va_list ap)
eputs (template);
eputs ("\n");
t++;
p = fgetc (stream);
r = fgetc (stream);
}
}
t++;

View File

@ -21,6 +21,7 @@
#include <libmes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
int
vsscanf (char const *s, char const *template, va_list ap)
@ -38,6 +39,15 @@ vsscanf (char const *s, char const *template, va_list ap)
{
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')
c = *++t;
switch (c)
@ -49,18 +59,27 @@ vsscanf (char const *s, char const *template, va_list ap)
}
case 'c':
{
char *c = va_arg (ap, char*);
*c = *p++;
count++;
char r = *p++;
if (!skip_p)
{
char *c = va_arg (ap, char*);
*c = r;
count++;
}
break;
}
case 'd':
case 'i':
case 'u':
{
int *d = va_arg (ap, int*);
*d = abtol (&p, 10);
count++;
if (skip_p)
abtol (&p, 10);
else
{
int *d = va_arg (ap, int*);
*d = abtol (&p, 10);
count++;
}
break;
}
case 'e':
@ -69,9 +88,76 @@ vsscanf (char const *s, char const *template, va_list ap)
case 'E':
case 'G':
{
float *f = va_arg (ap, float*);
*f = strtod (p, &p);
count++;
if (skip_p)
strtod (p, &p);
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 = 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;
}
default:

View File

@ -0,0 +1,43 @@
/* -*-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 <libmes.h>
#include <stdio.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;
}

View File

@ -85,6 +85,7 @@ DEFINE mov____$i32,0x8(%ebp) c745
DEFINE mov____$i8,%al b0
DEFINE mov____$i8,%eax b0
DEFINE mov____%al,(%ebx) 8803
DEFINE mov____%al,0x32(%ebp) 8885
DEFINE mov____%al,0x8(%ebp) 8845
DEFINE mov____%ax,(%ebx) 668903
DEFINE mov____%ax,0x8(%ebp) 668945