Fixed implicit method; some refactoring

master
Jef Roosens 2020-11-07 14:49:29 +01:00
parent 66093b4858
commit 584bcb94d1
5 changed files with 80 additions and 73 deletions

View File

@ -1,14 +1,16 @@
SRC_DIR = src
RELEASE_DIR = build/release
DEBUG_DIR = build/debug
BINARY = stj
SRC_DIR := src
BUILD_DIR := build
RELEASE_DIR := $(BUILD_DIR)/release
DEBUG_DIR := $(BUILD_DIR)/debug
BINARY := stj
CORES := $(shell nproc --all)
all: debug
.PHONY: all
clean:
@ rm -rf build
@ rm -rf $(BUILD_DIR)
.PHONY: clean
@ -18,14 +20,14 @@ run-release: release
.PHONY: run-release
release: $(RELEASE_DIR)/Makefile
@ make -C $(RELEASE_DIR)
@ make -C $(RELEASE_DIR) -j$(CORES)
.PHONY: release
$(RELEASE_DIR)/Makefile: $(SRC_DIR)/CMakeLists.txt
@ cmake -H$(SRC_DIR) -B$(RELEASE_DIR) -DCMAKE_BUILD_TYPE=Release
clean-release:
@ rm -rf build/release
@ rm -rf $(RELEASE_DIR)
.PHONY: clean-release
@ -35,12 +37,12 @@ run-debug: debug
.PHONY: run-debug
debug: $(DEBUG_DIR)/Makefile
@ make -C $(DEBUG_DIR)
@ make -C $(DEBUG_DIR) -j$(CORES)
.PHONY: debug
$(DEBUG_DIR)/Makefile: $(SRC_DIR)/CMakeLists.txt
@ cmake -H$(SRC_DIR) -B$(DEBUG_DIR) -DCMAKE_BUILD_TYPE=Debug
clean-debug:
@ rm -rf build/debug
@ rm -rf $(DEBUG_DIR)
.PHONY: clean-debug

View File

@ -18,16 +18,17 @@ project(stj VERSION 0.1)
# =====COMPILE FLAGS=====
# General flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DVERSION=\"${CMAKE_PROJECT_VERSION}\"")
add_definitions(-DVERSION="${CMAKE_PROJECT_VERSION}" -D_XOPEN_SOURCE=600)
# Debug flags
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -Wall -O0 -march=native")
# -g flag gets auto-added by CMake for the debug build
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall -O0 -march=native")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address -pedantic")
# Release flags
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Werror -march=native")
# -O3 gets added automatically by CMake
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Werror -march=native -pedantic-errors")
# =====EXECUTABLE=====

View File

@ -31,8 +31,8 @@
/* Arbitrary sizes */
#define UTF_INVALID 0xFFFD
#define UTF_SIZ 4
#define ESC_BUF_SIZ (128*UTF_SIZ)
#define UTF_SIZE 4
#define ESC_BUF_SIZ (128*UTF_SIZE)
#define ESC_ARG_SIZ 16
#define STR_BUF_SIZ ESC_BUF_SIZ
#define STR_ARG_SIZ ESC_ARG_SIZ
@ -227,50 +227,51 @@ static int iofd = 1;
static int cmdfd;
static pid_t pid;
static uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
static uchar utfbyte[UTF_SIZE + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
static uchar utfmask[UTF_SIZE + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
static Rune utfmin[UTF_SIZE + 1] = { 0, 0, 0x80, 0x800, 0x10000};
static Rune utfmax[UTF_SIZE + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
ssize_t
xwrite(int fd, const char *s, size_t len)
ssize_t xwrite(int fd, const char* s, size_t len)
{
size_t aux = len;
ssize_t r;
ssize_t result;
while (len > 0) {
r = write(fd, s, len);
if (r < 0)
return r;
len -= r;
s += r;
result = write(fd, s, len);
// This means an error occured in write, so it passes the error along
if (result < 0)
return result;
len -= result;
s += result;
}
return aux;
}
void *
xmalloc(size_t len)
// Same as malloc, but stops entire program if malloc fails
void* safe_malloc(size_t len)
{
void *p;
void *ptr;
if (!(p = malloc(len)))
if (!(ptr = malloc(len)))
die("malloc: %s\n", strerror(errno));
return p;
return ptr;
}
void *
xrealloc(void *p, size_t len)
// Same as realloc, but stops entire program if malloc fails
void* safe_realloc(void* ptr, size_t len)
{
if ((p = realloc(p, len)) == NULL)
if ((ptr = realloc(ptr, len)) == NULL)
die("realloc: %s\n", strerror(errno));
return p;
return ptr;
}
char *
xstrdup(char *s)
char * safe_strdup(char* s)
{
if ((s = strdup(s)) == NULL)
die("strdup: %s\n", strerror(errno));
@ -278,25 +279,28 @@ xstrdup(char *s)
return s;
}
size_t
utf8decode(const char *c, Rune *u, size_t clen)
size_t utf8decode(const char* chr, Rune* u, size_t chr_len)
{
size_t i, j, len, type;
Rune udecoded;
*u = UTF_INVALID;
if (!clen)
if (!chr_len) // chr_len is 0, so just return 0
return 0;
udecoded = utf8decodebyte(c[0], &len);
if (!BETWEEN(len, 1, UTF_SIZ))
udecoded = utf8decodebyte(chr[0], &len);
if (!BETWEEN(len, 1, UTF_SIZE))
return 1;
for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
for (i = 1, j = 1; i < chr_len && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf8decodebyte(chr[i], &type);
if (type != 0)
return j;
}
if (j < len)
return 0;
*u = udecoded;
utf8validate(u, len);
@ -319,7 +323,7 @@ utf8encode(Rune u, char *c)
size_t len, i;
len = utf8validate(&u, 0);
if (len > UTF_SIZ)
if (len > UTF_SIZE)
return 0;
for (i = len - 1; i != 0; --i) {
@ -379,7 +383,7 @@ base64dec(const char *src)
if (in_len % 4)
in_len += 4 - (in_len % 4);
result = dst = xmalloc(in_len / 4 * 3 + 1);
result = dst = safe_malloc(in_len / 4 * 3 + 1);
while (*src) {
int a = base64_digits[(unsigned char) base64dec_getc(&src)];
int b = base64_digits[(unsigned char) base64dec_getc(&src)];
@ -597,8 +601,8 @@ getsel(void)
if (sel.ob.x == -1)
return NULL;
bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
ptr = str = xmalloc(bufsize);
bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZE;
ptr = str = safe_malloc(bufsize);
/* append every set & selected glyph to the selection */
for (y = sel.nb.y; y <= sel.ne.y; y++) {
@ -1205,7 +1209,7 @@ tsetchar(Rune u, Glyph *attr, int x, int y)
*/
if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
utf8decode(vt100_0[u - 0x41], &u, UTF_SIZE);
if (term.line[y][x].mode & ATTR_WIDE) {
if (x+1 < term.col) {
@ -1966,7 +1970,7 @@ void
strreset(void)
{
strescseq = (STREscape){
.buf = xrealloc(strescseq.buf, STR_BUF_SIZ),
.buf = safe_realloc(strescseq.buf, STR_BUF_SIZ),
.siz = STR_BUF_SIZ,
};
}
@ -2020,7 +2024,7 @@ tdumpsel(void)
void
tdumpline(int n)
{
char buf[UTF_SIZ];
char buf[UTF_SIZE];
Glyph *bp, *end;
bp = &term.line[n][0];
@ -2303,7 +2307,7 @@ eschandle(uchar ascii)
void
tputc(Rune u)
{
char c[UTF_SIZ];
char c[UTF_SIZE];
int control;
int width, len;
Glyph *gp;
@ -2349,10 +2353,10 @@ tputc(Rune u)
* term.esc = 0;
* strhandle();
*/
if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
if (strescseq.siz > (SIZE_MAX - UTF_SIZE) / 2)
return;
strescseq.siz *= 2;
strescseq.buf = xrealloc(strescseq.buf, strescseq.siz);
strescseq.buf = safe_realloc(strescseq.buf, strescseq.siz);
}
memmove(&strescseq.buf[strescseq.len], c, len);
@ -2505,21 +2509,21 @@ tresize(int col, int row)
}
/* resize to new height */
term.line = xrealloc(term.line, row * sizeof(Line));
term.alt = xrealloc(term.alt, row * sizeof(Line));
term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
term.line = safe_realloc(term.line, row * sizeof(Line));
term.alt = safe_realloc(term.alt, row * sizeof(Line));
term.dirty = safe_realloc(term.dirty, row * sizeof(*term.dirty));
term.tabs = safe_realloc(term.tabs, col * sizeof(*term.tabs));
/* resize each row to new width, zero-pad if needed */
for (i = 0; i < minrow; i++) {
term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
term.line[i] = safe_realloc(term.line[i], col * sizeof(Glyph));
term.alt[i] = safe_realloc(term.alt[i], col * sizeof(Glyph));
}
/* allocate any new rows */
for (/* i = minrow */; i < row; i++) {
term.line[i] = xmalloc(col * sizeof(Glyph));
term.alt[i] = xmalloc(col * sizeof(Glyph));
term.line[i] = safe_malloc(col * sizeof(Glyph));
term.alt[i] = safe_malloc(col * sizeof(Glyph));
}
if (col > term.col) {
bp = term.tabs + term.col;

View File

@ -94,9 +94,9 @@ char *getsel(void);
size_t utf8encode(Rune, char *);
void *xmalloc(size_t);
void *xrealloc(void *, size_t);
char *xstrdup(char *);
void *safe_malloc(size_t);
void *safe_realloc(void *, size_t);
char *safe_strdup(char *);
/* config.h globals */
extern char *utmp;

12
src/x.c
View File

@ -264,7 +264,7 @@ clipcopy(const Arg *dummy)
xsel.clipboard = NULL;
if (xsel.primary != NULL) {
xsel.clipboard = xstrdup(xsel.primary);
xsel.clipboard = safe_strdup(xsel.primary);
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
}
@ -740,7 +740,7 @@ xresize(int col, int row)
xclear(0, 0, win.w, win.h);
/* resize to new width */
xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
xw.specbuf = safe_realloc(xw.specbuf, col * sizeof(GlyphFontSpec));
}
ushort
@ -785,7 +785,7 @@ xloadcols(void)
XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
} else {
dc.collen = MAX(LEN(colorname), 256);
dc.col = xmalloc(dc.collen * sizeof(Color));
dc.col = safe_malloc(dc.collen * sizeof(Color));
}
for (i = 0; i < dc.collen; i++)
@ -1155,7 +1155,7 @@ xinit(int cols, int rows)
XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
/* font spec buffer */
xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
xw.specbuf = safe_malloc(cols * sizeof(GlyphFontSpec));
/* Xft rendering context */
xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
@ -1310,7 +1310,7 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x
/* Allocate memory for the new cache entry. */
if (frclen >= frccap) {
frccap += 16;
frc = xrealloc(frc, frccap * sizeof(Fontcache));
frc = safe_realloc(frc, frccap * sizeof(Fontcache));
}
frc[frclen].font = XftFontOpenPattern(xw.dpy,
@ -2037,7 +2037,7 @@ main(int argc, char *argv[])
opt_embed = EARGF(usage());
break;
case 'v':
die("%s %s \n", argv0, VERSION);
die("%s\n", VERSION);
break;
default:
usage();