diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index cc0f3b07..3dde0b0d 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -223,6 +223,7 @@ lib/tests/stdio/90-fopen-append.c lib/tests/stdio/90-fread-fwrite.c lib/tests/stdio/90-fseek.c lib/tests/stdio/90-sprintf.c +lib/tests/stdio/90-sscanf.c lib/tests/stdlib/90-strtol.c lib/tests/string/90-snprintf.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/posix/90-execlp.c lib/tests/string/90-snprintf.c +lib/tests/stdio/90-sscanf.c " fi fi diff --git a/lib/stdio/vfscanf.c b/lib/stdio/vfscanf.c index fe12649d..33b47a1b 100644 --- a/lib/stdio/vfscanf.c +++ b/lib/stdio/vfscanf.c @@ -22,62 +22,81 @@ #include #include #include +#include +#include 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 ((char const **) &q, 10); + else + { + *d = abtol ((char const **) &q, 10); + count++; + } break; } case 'e': @@ -86,24 +105,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 = (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; } default: @@ -114,7 +198,7 @@ vfscanf (FILE * stream, char const *template, va_list ap) eputs (template); eputs ("\n"); t++; - p = fgetc (stream); + r = fgetc (stream); } } t++; diff --git a/lib/stdio/vsscanf.c b/lib/stdio/vsscanf.c index a42b0dba..1c6427c1 100644 --- a/lib/stdio/vsscanf.c +++ b/lib/stdio/vsscanf.c @@ -41,6 +41,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) @@ -52,18 +61,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 ((char const **)&p, 10); - count++; + if (skip_p) + abtol ((char const **) &p, 10); + else + { + int *d = va_arg (ap, int *); + *d = abtol ((char const **) &p, 10); + count++; + } break; } case 'e': @@ -72,9 +90,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 = (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; } default: diff --git a/lib/tests/stdio/90-sscanf.c b/lib/tests/stdio/90-sscanf.c new file mode 100644 index 00000000..1a445290 --- /dev/null +++ b/lib/tests/stdio/90-sscanf.c @@ -0,0 +1,50 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2019 Jan (janneke) Nieuwenhuizen + * + * 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 . + */ + +#include +#include +#include + +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; +} diff --git a/lib/x86-mes/x86.M1 b/lib/x86-mes/x86.M1 index 5836006b..6c83e9cd 100644 --- a/lib/x86-mes/x86.M1 +++ b/lib/x86-mes/x86.M1 @@ -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