From 4ae57a7fb13e68c5e6f1c1246a867555dbd986db Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 29 Aug 2022 13:57:47 +0200 Subject: user/lua: implement the bare minimum for it to link and "run" --- Makefile | 12 +++++++----- ports/lua | 2 +- src/user/lib/ctype.c | 29 +++++++++++++++++++++++++++-- src/user/lib/include/ctype.h | 1 + src/user/lib/include/locale.h | 5 ++++- src/user/lib/include/stdio.h | 4 +++- src/user/lib/math.c | 32 ++++++++++++++++++++++++++++++++ src/user/lib/printf.c | 9 +++++++++ src/user/lib/stdio/file.c | 16 +++++++++++++++- src/user/lib/stdio/misc.c | 21 +++++++++++++++++++++ src/user/lib/string.c | 38 ++++++++++++++++++++++++++++++++++++++ src/user/lib/time.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/user/lib/unistd.c | 1 - 13 files changed, 201 insertions(+), 12 deletions(-) create mode 100644 src/user/lib/math.c create mode 100644 src/user/lib/time.c diff --git a/Makefile b/Makefile index 37caf0d..64811d7 100644 --- a/Makefile +++ b/Makefile @@ -7,15 +7,16 @@ CHECK = sparse CFLAGS += -g -std=gnu99 -ffreestanding -O2 -ftrack-macro-expansion=0 CFLAGS += -Wall -Wextra -Wold-style-definition -Werror=implicit-function-declaration -CFLAGS += -mgeneral-regs-only -Wno-address-of-packed-member +CFLAGS += -Wno-address-of-packed-member CFLAGS += -Isrc/ -Isrc/shared/include/ -KERNEL_CFLAGS = $(CFLAGS) -mno-sse +KERNEL_CFLAGS = $(CFLAGS) -mno-sse -mgeneral-regs-only USER_CFLAGS = $(CFLAGS) -Isrc/user/lib/include/ SPARSEFLAGS = -Wno-non-pointer-null LFLAGS = -ffreestanding -O2 -nostdlib -lgcc -Wl,-zmax-page-size=4096 -Wl,--no-warn-mismatch -QFLAGS = -no-reboot +# TODO optimize memory use +QFLAGS = -no-reboot -m 1g ifdef NET_DIRECT QFLAGS += -nic socket,model=rtl8139,connect=:1234,mac=52:54:00:ca:77:1a,id=n1 else @@ -37,8 +38,9 @@ define from_sources endef -.PHONY: all boot debug lint check clean -all: out/boot.iso out/libm.a check +.PHONY: all portdeps boot debug lint check clean +all: portdeps out/boot.iso check +portdeps: out/libc.a out/libm.a boot: all out/hdd qemu-system-x86_64 -drive file=out/boot.iso,format=raw,media=disk $(QFLAGS) -serial stdio diff --git a/ports/lua b/ports/lua index fc8e8a7..6517954 100644 --- a/ports/lua +++ b/ports/lua @@ -14,7 +14,7 @@ prep() { } case $1 in - install) (prep; make generic "CC=cc" "MYCFLAGS=-Werror") ;; + install) (prep; make generic "CC=cc" "MYCFLAGS=-Werror" && make install "INSTALL_TOP=$PREFIX") ;; clean) (prep; make clean) ;; *) echo "usage: $0 install|clean"; false ;; esac \ No newline at end of file diff --git a/src/user/lib/ctype.c b/src/user/lib/ctype.c index 7d3a707..fa49a07 100644 --- a/src/user/lib/ctype.c +++ b/src/user/lib/ctype.c @@ -1,21 +1,40 @@ #include +int isalnum(int c) { + return isalpha(c) || isdigit(c); +} + int isalpha(int c) { return islower(c) || isupper(c); } -int isalnum(int c) { - return isalpha(c) || isdigit(c); +int iscntrl(int c) { + return c <= 0x1f || c == 0x7f; } int isdigit(int c) { return '0' <= c && c <= '9'; } +int isgraph(int c) { + return isalpha(c) || isdigit(c) || ispunct(c); +} + int islower(int c) { return 'a' <= c && c <= 'z'; } +int isprint(int c) { + return isgraph(c) || c == ' '; +} + +int ispunct(int c) { + return ('!' <= c && c <= '/') + || (':' <= c && c <= '@') + || ('[' <= c && c <= '`') + || ('{' <= c && c <= '~'); +} + int isspace(int c) { return c == ' ' || c == '\f' @@ -29,6 +48,12 @@ int isupper(int c) { return 'A' <= c && c <= 'Z'; } +int isxdigit(int c) { + return ('0' <= c && c <= '9') + || ('A' <= c && c <= 'F') + || ('a' <= c && c <= 'f'); +} + int tolower(int c) { if (isupper(c)) return c - 'A' + 'a'; return c; diff --git a/src/user/lib/include/ctype.h b/src/user/lib/include/ctype.h index 02d7179..1ebb111 100644 --- a/src/user/lib/include/ctype.h +++ b/src/user/lib/include/ctype.h @@ -6,6 +6,7 @@ int iscntrl(int c); int isdigit(int c); int isgraph(int c); int islower(int c); +int isprint(int c); int ispunct(int c); int isspace(int c); int isupper(int c); diff --git a/src/user/lib/include/locale.h b/src/user/lib/include/locale.h index 263dfb5..1221375 100644 --- a/src/user/lib/include/locale.h +++ b/src/user/lib/include/locale.h @@ -67,4 +67,7 @@ static inline struct lconv *localeconv(void) { return &locale; } -char *setlocale(int category, const char *locale); +static inline char *setlocale(int category, const char *locale) { + (void)category; (void)locale; + return NULL; +} diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index 84fb515..dd6a078 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -27,6 +27,8 @@ int printf(const char *restrict fmt, ...); int fprintf(FILE *restrict f, const char *restrict fmt, ...); +int sprintf(char *restrict s, const char *restrict fmt, ...); + int vprintf(const char *restrict fmt, va_list ap); int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap); @@ -56,7 +58,7 @@ int fgetc(FILE *f); int getc(FILE *f); int fputc(int c, FILE *f); int putc(int c, FILE *f); -int ungetc(int c, FILE *stream); +int ungetc(int c, FILE *f); int fseek(FILE *f, long offset, int whence); int fseeko(FILE *f, off_t offset, int whence); diff --git a/src/user/lib/math.c b/src/user/lib/math.c new file mode 100644 index 0000000..db1f2ec --- /dev/null +++ b/src/user/lib/math.c @@ -0,0 +1,32 @@ +#include +#include + +int abs(int i) { + return i < 0 ? -i : i; +} + + +// TODO port a libm +#pragma GCC diagnostic ignored "-Wunused-parameter" +double acos(double x) { __libc_panic("unimplemented"); } +double asin(double x) { __libc_panic("unimplemented"); } +double atan2(double x, double y) { __libc_panic("unimplemented"); } +double cos(double x) { __libc_panic("unimplemented"); } +double cosh(double x) { __libc_panic("unimplemented"); } +double sin(double x) { __libc_panic("unimplemented"); } +double sinh(double x) { __libc_panic("unimplemented"); } +double tan(double x) { __libc_panic("unimplemented"); } +double tanh(double x) { __libc_panic("unimplemented"); } + +double fabs(double x) { __libc_panic("unimplemented"); } +double floor(double x) { __libc_panic("unimplemented"); } +double ceil(double x) { __libc_panic("unimplemented"); } +double log(double x) { __libc_panic("unimplemented"); } +double log2(double x) { __libc_panic("unimplemented"); } +double log10(double x) { __libc_panic("unimplemented"); } +double exp(double x) { __libc_panic("unimplemented"); } +double fmod(double x, double y) { __libc_panic("unimplemented"); } +double frexp(double num, int *exp) { __libc_panic("unimplemented"); } +double ldexp(double x, int exp) { __libc_panic("unimplemented"); } +double pow(double x, double y) { __libc_panic("unimplemented"); } +double sqrt(double x) { __libc_panic("unimplemented"); } diff --git a/src/user/lib/printf.c b/src/user/lib/printf.c index cb6561b..a2f21d8 100644 --- a/src/user/lib/printf.c +++ b/src/user/lib/printf.c @@ -31,6 +31,15 @@ int fprintf(FILE *restrict f, const char *restrict fmt, ...) { return ret; } +int sprintf(char *restrict s, const char *restrict fmt, ...) { + int ret; + va_list argp; + va_start(argp, fmt); + ret = vsnprintf(s, ~0, fmt, argp); + va_end(argp); + return ret; +} + int vprintf(const char *restrict fmt, va_list ap) { return vfprintf(stdout, fmt, ap); } diff --git a/src/user/lib/stdio/file.c b/src/user/lib/stdio/file.c index 63b004d..8c0fc57 100644 --- a/src/user/lib/stdio/file.c +++ b/src/user/lib/stdio/file.c @@ -1,11 +1,12 @@ #include "file.h" -#include #include +#include #include #include #include #include #include +#include static FILE _stdin_null = { .fd = STDIN_FILENO }; static FILE _stdout_null = { .fd = STDOUT_FILENO }; @@ -124,6 +125,13 @@ int pclose(FILE *f) { return -1; } +// TODO tmpfile() +FILE *tmpfile(void) { + errno = ENOSYS; + return NULL; +} + + int fextflags(FILE *f, int extflags) { int old = f->extflags; f->extflags = extflags; @@ -227,6 +235,12 @@ int fputc(int c, FILE *f) { } int putc(int c, FILE *f) { return fputc(c, f); } +// TODO ungetc +int ungetc(int c, FILE *f) { + (void)c; (void)f; + __libc_panic("unimplemented"); +} + int fseek(FILE *f, long offset, int whence) { return fseeko(f, offset, whence); } diff --git a/src/user/lib/stdio/misc.c b/src/user/lib/stdio/misc.c index 8f872ec..d74c197 100644 --- a/src/user/lib/stdio/misc.c +++ b/src/user/lib/stdio/misc.c @@ -1,5 +1,7 @@ #include #include +#include +#include void perror(const char *s) { if (s) fprintf(stderr, "%s: ", s); @@ -23,3 +25,22 @@ off_t lseek(int fd, off_t off, int whence) { errno = ENOSYS; return -1; } + +int remove(const char *path) { + return unlink(path); +} + +// TODO! VFSOP_MOVE +int rename(const char *old, const char *new) { + (void)old; (void)new; + errno = ENOSYS; + return -1; +} + +// TODO tmpnam +char *tmpnam(char *s) { + static char buf[L_tmpnam]; + if (!s) s = buf; + strcpy(s, "/tmp/tmpnam"); + return s; +} diff --git a/src/user/lib/string.c b/src/user/lib/string.c index 8424574..386cffa 100644 --- a/src/user/lib/string.c +++ b/src/user/lib/string.c @@ -47,6 +47,12 @@ long strtol(const char *restrict s, char **restrict end, int base) { return res * sign; } +#include +double strtod(const char *restrict s, char **restrict end) { + (void)s; (void)end; + __libc_panic("unimplemented"); +} + char *strchr(const char *s, int c) { for (; *s; s++) { if (*s == c) return (char*)s; @@ -66,6 +72,13 @@ size_t strcspn(const char *s, const char *reject) { return l; } +char *strpbrk(const char *s1, const char *s2) { + for (; *s1; s1++) { + if (strchr(s2, *s1)) return (char*)s1; + } + return NULL; +} + char *strtok(char *restrict s, const char *restrict sep) { static char *state; return strtok_r(s, sep, &state); @@ -95,3 +108,28 @@ int strncmp(const char *s1, const char *s2, size_t n) { if (*s1 < *s2) return -1; else return 1; } + +int strcoll(const char *s1, const char *s2) { + return strcmp(s1, s2); +} + +// TODO implement strstr using Boyer-Moore +char *strstr(const char *s1, const char *s2) { + size_t l1 = strlen(s1), l2 = strlen(s2); + for (; l2 <= l1; s1++, l1--) { + if (memcmp(s1, s2, l2) == 0) return (char*)s1; + } + return NULL; +} + +char *strcpy(char *restrict s1, const char *restrict s2) { + char *ret = s1; + while (*s2) *s1++ = *s2++; + return ret; +} + +// TODO strerror mapping +char *strerror(int errnum) { + (void)errnum; + return "unknown error"; +} diff --git a/src/user/lib/time.c b/src/user/lib/time.c new file mode 100644 index 0000000..4709ecd --- /dev/null +++ b/src/user/lib/time.c @@ -0,0 +1,43 @@ +#include +#include + +// TODO time +time_t time(time_t *tloc) { + time_t ret = 0; + if (tloc) *tloc = ret; + return ret; +} + +clock_t clock(void) { + return 0; +} + +struct tm *gmtime(const time_t *timer) { + (void)timer; + errno = ENOSYS; + return NULL; +} + +struct tm *localtime(const time_t *timer) { + (void)timer; + errno = ENOSYS; + return NULL; +} + +time_t mktime(struct tm *timeptr) { + (void)timeptr; + return 0; +} + +double difftime(time_t time1, time_t time0) { + (void)time1; (void)time0; + return 0; +} + +size_t strftime( + char *restrict s, size_t maxsize, + const char *restrict format, const struct tm *restrict timeptr) +{ + (void)s; (void)maxsize; (void)format; (void)timeptr; + return 0; +} diff --git a/src/user/lib/unistd.c b/src/user/lib/unistd.c index ce43551..ed8d77f 100644 --- a/src/user/lib/unistd.c +++ b/src/user/lib/unistd.c @@ -141,7 +141,6 @@ char *getcwd(char *buf, size_t capacity) { size_t absolutepath(char *out, const char *in, size_t size) { const char *realcwd = getrealcwd(); size_t len, pos = 0; - _klogf("realcwd == %x\n", (long)__initialcwd); if (!in) return strlen(realcwd) + 1; if (!(in[0] == '/')) { -- cgit v1.2.3