be

based editor
git clone git://git.kocotian.pl/be.git
Log | Files | Refs | README | LICENSE

commit 3cf6bbd0dfc98c5597dbea75e17739f6158b3321
parent 52e2895b6c9694bc6ab3761fda4ec950765ef753
Author: kocotian <kocotian@kocotian.pl>
Date:   Sat, 17 Apr 2021 19:54:20 +0200

libsl, Error handling

Diffstat:
MMakefile | 8+++++++-
Aarg.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mbe.c | 11++++++++---
Mconfig.mk | 4+++-
Alicenses/LIBSL | 24++++++++++++++++++++++++
Autil.c | 36++++++++++++++++++++++++++++++++++++
Autil.h | 17+++++++++++++++++
7 files changed, 145 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,10 @@ include config.mk -be: be.c +SRC = util.c +OBJ = ${SRC:.c=.o} + +.c.o: + ${CC} ${CFLAGS} -c -o $@ $^ + +be: be.c ${OBJ} ${CC} ${CFLAGS} -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/be.c b/be.c @@ -1,6 +1,8 @@ #include <termios.h> #include <unistd.h> +#include <util.h> + static void rawRestore(void); static void rawOn(void); @@ -11,7 +13,8 @@ rawOn(void) { struct termios raw; - tcgetattr(STDIN_FILENO, &origtermios); + if (tcgetattr(STDIN_FILENO, &origtermios) < 0) + die("tcgetattr:"); raw = origtermios; raw.c_cflag |= (CS8); raw.c_iflag &= (tcflag_t)~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); @@ -19,13 +22,15 @@ rawOn(void) raw.c_oflag &= (tcflag_t)~(OPOST); raw.c_cc[VMIN] = 0; raw.c_cc[VTIME] = 1; - tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) < 0) + die("tcsetattr:"); } static void rawRestore(void) { - tcsetattr(STDIN_FILENO, TCSAFLUSH, &origtermios); + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &origtermios) < 0) + die("tcsetattr:"); } int diff --git a/config.mk b/config.mk @@ -1,5 +1,7 @@ +INC = -I. + # flags -CFLAGS = -Wall -Wextra -Wconversion -std=c99 -pedantic +CFLAGS = ${INC} -Wall -Wextra -Wconversion -std=c99 -pedantic # compiler CC = gcc 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/util.c b/util.c @@ -0,0 +1,36 @@ +/* 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,17 @@ +/* See licenses/LIBSL file for copyright and license details. */ + +#ifndef _UTIL_H +#define _UTIL_H + +#define MAX(A, B) ((signed)(A) > (signed)(B) ? (signed)(A) : (signed)(B)) +#define MIN(A, B) ((signed)(A) < (signed)(B) ? (signed)(A) : (signed)(B)) +#define BETWEEN(X, A, B) ((signed)(A) <= (signed)(X) && (signed)(X) <= (signed)(B)) + +#define UMAX(A, B) ((unsigned)(A) > (unsigned)(B) ? (unsigned)(A) : (unsigned)(B)) +#define UMIN(A, B) ((unsigned)(A) < (unsigned)(B) ? (unsigned)(A) : (unsigned)(B)) +#define UBETWEEN(X, A, B) ((unsigned)(A) <= (unsigned)(X) && (unsigned)(X) <= (unsigned)(B)) + +void die(const char *fmt, ...); +void *ecalloc(size_t nmemb, size_t size); + +#endif