From e4056eb228c9d12bdf63387e55b11f65125af3ec Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 9 Apr 2018 21:01:05 -0400 Subject: [PATCH] minor refactoring to get rid of the utils.c file --- Makefile | 1 - inc/edit.h | 5 ----- inc/stdc.h | 4 ---- lib/utils.c | 43 ------------------------------------------- lib/x11.c | 10 ++++++++-- tide.c | 20 ++++++++++++++++++++ 6 files changed, 28 insertions(+), 55 deletions(-) delete mode 100644 lib/utils.c diff --git a/Makefile b/Makefile index a2703c4..b01ff21 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,6 @@ MAN1 = docs/tide.1 LIBEDIT_OBJS = \ lib/buf.o \ lib/utf8.o \ - lib/utils.o \ lib/job.o \ lib/view.o \ lib/x11.o \ diff --git a/inc/edit.h b/inc/edit.h index 3354d06..527c739 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -1,8 +1,3 @@ -/* Utility Functions - *****************************************************************************/ -char* stringdup(const char* str); -char* strmcat(char* first, ...); - /* Buffer management functions *****************************************************************************/ /* undo/redo list item */ diff --git a/inc/stdc.h b/inc/stdc.h index 3d867af..e70805c 100644 --- a/inc/stdc.h +++ b/inc/stdc.h @@ -42,10 +42,6 @@ typedef int64_t int64; typedef uintptr_t uintptr; typedef intptr_t intptr; -/* Generic Death Function - *****************************************************************************/ -void die(const char* msgfmt, ...) __attribute__((__noreturn__)); - /* Option Parsing * * This following macros implement a simple POSIX-style option parsing strategy. diff --git a/lib/utils.c b/lib/utils.c deleted file mode 100644 index b44127e..0000000 --- a/lib/utils.c +++ /dev/null @@ -1,43 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -void die(const char* msgfmt, ...) { - va_list args; - va_start(args, msgfmt); - fprintf(stderr, "error: "); - vfprintf(stderr, msgfmt, args); - va_end(args); - if (*msgfmt && msgfmt[strlen(msgfmt)-1] == ':') - fprintf(stderr, " %s", strerror(errno)); - fprintf(stderr, "\n"); - exit(EXIT_FAILURE); -} - -char* strmcat(char* first, ...) { - va_list args; - /* calculate the length of the final string */ - size_t len = strlen(first); - va_start(args, first); - for (char* s = NULL; (s = va_arg(args, char*));) - len += strlen(s); - va_end(args); - /* allocate the final string and copy the args into it */ - char *str = malloc(len+1), *curr = str; - while (first && *first) *(curr++) = *(first++); - va_start(args, first); - for (char* s = NULL; (s = va_arg(args, char*));) - while (s && *s) *(curr++) = *(s++); - va_end(args); - /* null terminate and return */ - *curr = '\0'; - return str; -} diff --git a/lib/x11.c b/lib/x11.c index c33d098..e640e0e 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -19,6 +19,7 @@ #undef Region /******************************************************************************/ +static void die(const char* msg); static uint32_t special_keys(uint32_t key); static uint32_t getkey(XEvent* e); @@ -272,13 +273,13 @@ XFont x11_font_load(char* name) { die("Could not init fontconfig.\n"); FcPattern* pattern = FcNameParse((FcChar8 *)name); if (!pattern) - die("can't open font %s\n", name); + die("could not parse font name\n"); /* load the base font */ FcResult result; FcPattern* match = XftFontMatch(X.display, X.screen, pattern, &result); if (!match || !(font->base.match = XftFontOpenPattern(X.display, match))) - die("could not load default font: %s", name); + die("could not load base font"); /* get base font extents */ XGlyphInfo extents; @@ -783,3 +784,8 @@ static void draw_glyphs(size_t x, size_t y, UGlyph* glyphs, size_t rlen, size_t eol = false, rlen -= numspecs; } } + +static void die(const char* msg) { + perror(msg); + exit(EXIT_FAILURE); +} diff --git a/tide.c b/tide.c index ee4469b..87f04ac 100644 --- a/tide.c +++ b/tide.c @@ -80,6 +80,26 @@ static void cmd_exec(char* cmd) { job_start(execcmd, input, len, (op != '<' ? curr : edit)); } +static char* strmcat(char* first, ...) { + va_list args; + /* calculate the length of the final string */ + size_t len = strlen(first); + va_start(args, first); + for (char* s = NULL; (s = va_arg(args, char*));) + len += strlen(s); + va_end(args); + /* allocate the final string and copy the args into it */ + char *str = malloc(len+1), *curr = str; + while (first && *first) *(curr++) = *(first++); + va_start(args, first); + for (char* s = NULL; (s = va_arg(args, char*));) + while (s && *s) *(curr++) = *(s++); + va_end(args); + /* null terminate and return */ + *curr = '\0'; + return str; +} + static void cmd_execwitharg(char* cmd, char* arg) { cmd = (arg ? strmcat(cmd, " '", arg, "'", 0) : strmcat(cmd)); cmd_exec(cmd); -- 2.49.0