npc

nanohard parser compiler
git clone git://git.kocotian.pl/npc.git
Log | Files | Refs

commit 4956ef1aa9bbdbdf07dfa9b9884588e8edd851e9
parent c0e9b0f3d6ae13bf6f02235a0e444d63be824381
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri,  9 Apr 2021 21:24:13 +0200

libsl

Diffstat:
MMakefile | 2+-
Aarg.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Alicenses/LIBSL | 24++++++++++++++++++++++++
Mnpc.c | 7+++----
Autil.c | 35+++++++++++++++++++++++++++++++++++
Autil.h | 12++++++++++++
6 files changed, 125 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ CC = gcc CFLAGS = -Wall -Wextra -Wconversion -std=c99 -pedantic -I. -SRC = npc.c str.c parse.c compile.c +SRC = npc.c str.c util.c parse.c compile.c LIB = ${SRC:.c=.o} .c.o: 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/licenses/LIBSL b/licenses/LIBSL @@ -0,0 +1,24 @@ +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> + +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/npc.c b/npc.c @@ -4,6 +4,7 @@ #include <compile.h> #include <parse.h> +#include <util.h> int main(int argc, char *argv[]) @@ -16,10 +17,8 @@ main(int argc, char *argv[]) String input; input.data = input_data; - if ((rb = read(0, input_data, 640 * 1024)) < 0) { - perror("read"); - exit(1); - } + if ((rb = read(0, input_data, 640 * 1024)) < 0) + die("read:"); input.len = (unsigned)rb; tokens = parse(input); diff --git a/util.c b/util.c @@ -0,0 +1,35 @@ +/* See licenses/LIBSL file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.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); +} diff --git a/util.h b/util.h @@ -0,0 +1,12 @@ +/* See licenses/LIBSL file for copyright and license details. */ +#ifndef _UTIL_H +#define _UTIL_H + +#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); + +#endif