ls

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

commit f9b1c25e4a18d24ad2d02461a836b58a74d3cd52
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun, 21 Feb 2021 19:15:50 +0100

initial, getting line

Diffstat:
AMakefile | 25+++++++++++++++++++++++++
AREADME | 5+++++
Aarg.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig.mk | 10++++++++++
Alicenses/LIBSL | 25+++++++++++++++++++++++++
Alsc.c | 34++++++++++++++++++++++++++++++++++
Autil.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autil.h | 10++++++++++
8 files changed, 221 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,25 @@ +include config.mk + +SRC = lsc.c util.c +OBJ = ${SRC:.c=.o} + +all: options lsc + +options: + @echo build flags: + @echo "CFLAGS = ${CFLAGS}" + @echo "CPPFLAGS = ${CPPFLAGS}" + @echo "CC = ${CC}" + +.c.o: + ${CC} -c ${CFLAGS} ${CPPFLAGS} $< + +${OBJ}: config.mk + +lsc: ${OBJ} + ${CC} -o $@ ${OBJ} + +clean: + rm -f lsc ${OBJ} + +.PHONY: all options clean diff --git a/README b/README @@ -0,0 +1,5 @@ +Linux Script +============ + +Linux Script is a simple programming language +based on Linux x86_64 system calls. diff --git a/arg.h b/arg.h @@ -0,0 +1,50 @@ +/* See licenses/LIBSL file for copyright and license details. */ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/config.mk b/config.mk @@ -0,0 +1,10 @@ +# paths +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/share/man + +# flags +CFLAGS = -std=c99 -Wall -Wextra -Os +CPPFLAGS = -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=700 + +# compiler and linker +CC = cc diff --git a/licenses/LIBSL b/licenses/LIBSL @@ -0,0 +1,25 @@ +MIT/X Consortium License + +© 2013-2019 Anselm R Garbe <anselm@garbe.ca> +© 2015-2019 Hiltjo Posthuma <hiltjo@codemadness.org> +© 2016 Markus Teich <markus.teich@stusta.mhn.de> +© 2015 Eric Pruitt <eric.pruitt@gmail.com> +© 2020-2021 Kacper Kocot <kocotian@kocotian.pl + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/lsc.c b/lsc.c @@ -0,0 +1,34 @@ +#include <unistd.h> + +/* temporarily */ +#include "arg.h" +#include "util.h" + +#define BUFSIZ 8192 + +static void usage(void); + +char *argv0; + +static void +usage(void) +{ + die("usage: %s", argv0); +} + +int +main(int argc, char *argv[]) +{ + char buffer[BUFSIZ]; + ssize_t rb; + int line; + + ARGBEGIN { + default: + usage(); + } ARGEND + + for (rb = line = 0; (rb = nextline(0, buffer, BUFSIZ)) > 0; ++line) { + write(1, buffer, rb); + } +} diff --git a/util.c b/util.c @@ -0,0 +1,62 @@ +/* See licenses/LIBSL file for copyright and license details. */ +/* Extended by kocotian */ + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "util.h" + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + if (!(p = calloc(nmemb, size))) + die("calloc:"); + return p; +} + +void +die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + exit(1); +} + +ssize_t +nextline(int fd, char *buf, size_t size) +{ + size_t count, rb; + int c; + + if (!size) + return 0; + + for (count = c = 0; c != '\n' && count < size - 1; count++) { + if (!((rb = read(fd, &c, 1)) > 0)) { + if (count == 0) + return -1; + break; + } + + buf[count] = c; + } + + buf[count] = '\0'; + return (ssize_t)count; +} diff --git a/util.h b/util.h @@ -0,0 +1,10 @@ +/* See licenses/LIBSL file for copyright and license details. */ +/* Extended by kocotian */ + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) + +void die(const char *fmt, ...); +void *ecalloc(size_t nmemb, size_t size); +ssize_t nextline(int fd, char *buf, size_t size);