diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index 2f512998..58cf2b56 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -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 diff --git a/lib/stdio/vfscanf.c b/lib/stdio/vfscanf.c index f7f7077d..420a74a0 100644 --- a/lib/stdio/vfscanf.c +++ b/lib/stdio/vfscanf.c @@ -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++; diff --git a/lib/stdio/vsscanf.c b/lib/stdio/vsscanf.c index e323b420..0e958a19 100644 --- a/lib/stdio/vsscanf.c +++ b/lib/stdio/vsscanf.c @@ -21,6 +21,7 @@ #include #include #include +#include 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: diff --git a/lib/tests/stdio/90-sscanf.c b/lib/tests/stdio/90-sscanf.c new file mode 100644 index 00000000..af7bcbe4 --- /dev/null +++ b/lib/tests/stdio/90-sscanf.c @@ -0,0 +1,43 @@ +/* -*-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 + +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 bb2a2f24..f224c632 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