ls

simple syscall based programming language from scratch
git clone git://git.kocotian.pl/ls.git
Log | Files | Refs | README

commit 69c624a24ca49d67105d01fdaff7a73bf08caa1a
parent f9b1c25e4a18d24ad2d02461a836b58a74d3cd52
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun, 21 Feb 2021 23:26:28 +0100

simple parser, syscalls by name, makefile

Diffstat:
MMakefile | 2+-
Mlsc.c | 119++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Asyscalls.h | 339+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 455 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -12,7 +12,7 @@ options: @echo "CC = ${CC}" .c.o: - ${CC} -c ${CFLAGS} ${CPPFLAGS} $< + ${CC} -c -ggdb ${CFLAGS} ${CPPFLAGS} $< ${OBJ}: config.mk diff --git a/lsc.c b/lsc.c @@ -1,14 +1,122 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <unistd.h> -/* temporarily */ #include "arg.h" +#include "syscalls.h" #include "util.h" #define BUFSIZ 8192 +#define ISLOW(ch) ((ch) > 0x60 && (ch) < 0x7b) +#define ISUPP(ch) ((ch) > 0x40 && (ch) < 0x5b) +#define ISNUM(ch) ((ch) > 0x2f && (ch) < 0x3a) +#define ISUND(ch) ((ch) == 0x5f) +#define ISPAR(ch) ((ch) == 0x28 || (ch) == 0x29) +#define ISBRC(ch) ((ch) == 0x5b || (ch) == 0x5d) +#define ISQUOT(ch) ((ch) == 0x22) +#define ISCOMM(ch) ((ch) == 0x2c) +#define ISIGNORABLE(ch) ((ch) > 0x00 && (ch) < 0x21) + +#define ISIDENSTARTCHAR(ch) (ISUND(ch) || ISLOW(ch) || ISUPP(ch)) +#define ISIDENCHAR(ch) (ISIDENSTARTCHAR(ch) || ISNUM(ch)) +#define ISNUMCHAR(ch) (ISNUM(ch) || ((ch) > 0x60 && (ch) < 0x67) || \ + ((ch) > 0x40 && (ch) < 0x47)) + +#define ERROR(str, filename, fileline, ...) \ + printf("\033[1;97m%s:%d: \033[0m""%s"str"\n", \ + filename, fileline, errstr, __VA_ARGS__) +#define WARNING(str, filename, fileline, ...) \ + printf("\033[1;97m%s:%d: \033[0m""%s"str"\n", \ + filename, fileline, warnstr, __VA_ARGS__) + +typedef enum { + TokenNull, + TokenNumber, TokenIdentifier, TokenString, + TokenComma, + TokenParenthesis, TokenBracket +} TokenType; + +typedef struct { + char *val; + size_t len; + TokenType type; +} Token; + +static int getsyscallbyname(char *name); static void usage(void); char *argv0; +const char *warnstr = "\033[1;33mwarning: \033[0m"; +const char *errstr = "\033[1;31merror: \033[0m"; + +static int +getsyscallbyname(char *name) +{ + size_t i; + for (i = 0; i < (sizeof(syscalls) / sizeof(*syscalls)); ++i) + if (!strcmp(syscalls[i], name)) + return (int)i; + return -1; +} + +static ssize_t +parseline(char *input, size_t ilen, char *output, size_t olen, int lineno) +{ + /* === Types === + TokenNull, + TokenNumber, TokenIdentifier, TokenString, + TokenComma, + TokenParenthesis, TokenBracket */ + + TokenType type; + size_t i, j, li, tokiter; + char ch, *valstart; + *output = '\0'; + Token *tokens = malloc(sizeof(Token) * 128); + + valstart = NULL; + for (tokiter = i = j = li = type = 0; i < ilen; ++i, ++j, ++li) { + ch = input[i]; + if (!type) { + valstart = input + i; + if (ISNUM(ch)) + type = TokenNumber; + else if (ISIDENSTARTCHAR(ch)) + type = TokenIdentifier; + else if (ISQUOT(ch)) + type = TokenString; + else if (ISIGNORABLE(ch)) { + --j; + continue; + } else if (ISPAR(ch) || ISBRC(ch) || ISCOMM(ch)) { + tokens[tokiter].val = valstart; + tokens[tokiter].len = j + 1; + tokens[tokiter++].type = + ISPAR(ch) ? TokenParenthesis : + ISBRC(ch) ? TokenBracket : + TokenComma; + type = TokenNull; + j = -1; + } + } else { + if ((type == TokenNumber && !ISNUMCHAR(ch)) + || (type == TokenIdentifier && !ISIDENCHAR(ch)) + || (type == TokenString && ISQUOT(ch))) { + tokens[tokiter].val = valstart; + tokens[tokiter].len = j; + tokens[tokiter++].type = type; + if (type != TokenString) --i; + type = TokenNull; + j = -1; + } + } + } + + free(tokens); + return i; +} static void usage(void) @@ -19,16 +127,19 @@ usage(void) int main(int argc, char *argv[]) { - char buffer[BUFSIZ]; + char buffer[BUFSIZ], rbuf[BUFSIZ], cbuf[BUFSIZ]; ssize_t rb; - int line; + int lindex; + + void *data, *bss, *text; ARGBEGIN { default: usage(); } ARGEND - for (rb = line = 0; (rb = nextline(0, buffer, BUFSIZ)) > 0; ++line) { + for (rb = lindex = 0; (rb = nextline(0, buffer, BUFSIZ)) > 0; ++lindex) { write(1, buffer, rb); + parseline(buffer, rb, rbuf, BUFSIZ, lindex + 1); } } diff --git a/syscalls.h b/syscalls.h @@ -0,0 +1,339 @@ +const char *syscalls[] = { +"read", +"write", +"open", +"close", +"stat", +"fstat", +"lstat", +"poll", +"lseek", +"mmap", +"mprotect", +"munmap", +"brk", +"rt_sigaction", +"rt_sigprocmask", +"rt_sigreturn", +"ioctl", +"pread64", +"pwrite64", +"readv", +"writev", +"access", +"pipe", +"select", +"sched_yield", +"mremap", +"msync", +"mincore", +"madvise", +"shmget", +"shmat", +"shmctl", +"dup", +"dup2", +"pause", +"nanosleep", +"getitimer", +"alarm", +"setitimer", +"getpid", +"sendfile", +"socket", +"connect", +"accept", +"sendto", +"recvfrom", +"sendmsg", +"recvmsg", +"shutdown", +"bind", +"listen", +"getsockname", +"getpeername", +"socketpair", +"setsockopt", +"getsockopt", +"clone", +"fork", +"vfork", +"execve", +"exit", +"wait4", +"kill", +"uname", +"semget", +"semop", +"semctl", +"shmdt", +"msgget", +"msgsnd", +"msgrcv", +"msgctl", +"fcntl", +"flock", +"fsync", +"fdatasync", +"truncate", +"ftruncate", +"getdents", +"getcwd", +"chdir", +"fchdir", +"rename", +"mkdir", +"rmdir", +"creat", +"link", +"unlink", +"symlink", +"readlink", +"chmod", +"fchmod", +"chown", +"fchown", +"lchown", +"umask", +"gettimeofday", +"getrlimit", +"getrusage", +"sysinfo", +"times", +"ptrace", +"getuid", +"syslog", +"getgid", +"setuid", +"setgid", +"geteuid", +"getegid", +"setpgid", +"getppid", +"getpgrp", +"setsid", +"setreuid", +"setregid", +"getgroups", +"setgroups", +"setresuid", +"getresuid", +"setresgid", +"getresgid", +"getpgid", +"setfsuid", +"setfsgid", +"getsid", +"capget", +"capset", +"rt_sigpending", +"rt_sigtimedwait", +"rt_sigqueueinfo", +"rt_sigsuspend", +"sigaltstack", +"utime", +"mknod", +"uselib", +"personality", +"ustat", +"statfs", +"fstatfs", +"sysfs", +"getpriority", +"setpriority", +"sched_setparam", +"sched_getparam", +"sched_setscheduler", +"sched_getscheduler", +"sched_get_priority_max", +"sched_get_priority_min", +"sched_rr_get_interval", +"mlock", +"munlock", +"mlockall", +"munlockall", +"vhangup", +"modify_ldt", +"pivot_root", +"_sysctl", +"prctl", +"arch_prctl", +"adjtimex", +"setrlimit", +"chroot", +"sync", +"acct", +"settimeofday", +"mount", +"umount2", +"swapon", +"swapoff", +"reboot", +"sethostname", +"setdomainname", +"iopl", +"ioperm", +"create_module", +"init_module", +"delete_module", +"get_kernel_syms", +"query_module", +"quotactl", +"nfsservctl", +"getpmsg", +"putpmsg", +"afs_syscall", +"tuxcall", +"security", +"gettid", +"readahead", +"setxattr", +"lsetxattr", +"fsetxattr", +"getxattr", +"lgetxattr", +"fgetxattr", +"listxattr", +"llistxattr", +"flistxattr", +"removexattr", +"lremovexattr", +"fremovexattr", +"tkill", +"time", +"futex", +"sched_setaffinity", +"sched_getaffinity", +"set_thread_area", +"io_setup", +"io_destroy", +"io_getevents", +"io_submit", +"io_cancel", +"get_thread_area", +"lookup_dcookie", +"epoll_create", +"epoll_ctl_old", +"epoll_wait_old", +"remap_file_pages", +"getdents64", +"set_tid_address", +"restart_syscall", +"semtimedop", +"fadvise64", +"timer_create", +"timer_settime", +"timer_gettime", +"timer_getoverrun", +"timer_delete", +"clock_settime", +"clock_gettime", +"clock_getres", +"clock_nanosleep", +"exit_group", +"epoll_wait", +"epoll_ctl", +"tgkill", +"utimes", +"vserver", +"mbind", +"set_mempolicy", +"get_mempolicy", +"mq_open", +"mq_unlink", +"mq_timedsend", +"mq_timedreceive", +"mq_notify", +"mq_getsetattr", +"kexec_load", +"waitid", +"add_key", +"request_key", +"keyctl", +"ioprio_set", +"ioprio_get", +"inotify_init", +"inotify_add_watch", +"inotify_rm_watch", +"migrate_pages", +"openat", +"mkdirat", +"mknodat", +"fchownat", +"futimesat", +"newfstatat", +"unlinkat", +"renameat", +"linkat", +"symlinkat", +"readlinkat", +"fchmodat", +"faccessat", +"pselect6", +"ppoll", +"unshare", +"set_robust_list", +"get_robust_list", +"splice", +"tee", +"sync_file_range", +"vmsplice", +"move_pages", +"utimensat", +"epoll_pwait", +"signalfd", +"timerfd_create", +"eventfd", +"fallocate", +"timerfd_settime", +"timerfd_gettime", +"accept4", +"signalfd4", +"eventfd2", +"epoll_create1", +"dup3", +"pipe2", +"inotify_init1", +"preadv", +"pwritev", +"rt_tgsigqueueinfo", +"perf_event_open", +"recvmmsg", +"fanotify_init", +"fanotify_mark", +"prlimit64", +"name_to_handle_at", +"open_by_handle_at", +"clock_adjtime", +"syncfs", +"sendmmsg", +"setns", +"getcpu", +"process_vm_readv", +"process_vm_writev", +"kcmp", +"finit_module", +"sched_setattr", +"sched_getattr", +"renameat2", +"seccomp", +"getrandom", +"memfd_create", +"kexec_file_load", +"bpf", +/* these without sys_ on beginning */ +"stub_execveat", +"userfaultfd", +"membarrier", +"mlock2", +"copy_file_range", +"preadv2", +"pwritev2", +"pkey_mprotect", +"pkey_alloc", +"pkey_free", +"statx", +"io_pgetevents", +"rseq", +"pkey_mprotect", +};