URI: 
       tMove diagnostic functions to util - ltk - Socket-based GUI for X11 (WIP)
  HTML git clone git://lumidify.org/ltk.git (fast, but not encrypted)
  HTML git clone https://lumidify.org/git/ltk.git (encrypted, but very slow)
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 16fa6bfa5bfc26126ac712ac03528914b0d651f9
   DIR parent ad0cb56260b44997474ce30b98f0d96013b84073
  HTML Author: lumidify <nobody@lumidify.org>
       Date:   Sat, 20 Feb 2021 20:32:24 +0100
       
       Move diagnostic functions to util
       
       Diffstat:
         M Makefile                            |       6 +++---
         M color.c                             |       4 ++--
         M ltk.h                               |       4 ----
         M ltkc.c                              |      49 ++++++++++++++++++++-----------
         M ltkd.c                              |      81 ++++++++-----------------------
         M memory.c                            |       2 +-
         M text_pango.c                        |       2 +-
         M util.c                              |      75 +++++++++++++++++++++++---------
         M util.h                              |      10 ++++++++--
       
       9 files changed, 121 insertions(+), 112 deletions(-)
       ---
   DIR diff --git a/Makefile b/Makefile
       t@@ -1,14 +1,14 @@
        include config.mk
        
       -OBJ += strtonum.o memory.o color.o util.o ltkd.o ini.o grid.o box.o scrollbar.o button.o label.o draw.o
       +OBJ += strtonum.o util.o memory.o color.o ltkd.o ini.o grid.o box.o scrollbar.o button.o label.o draw.o
        
        all: ltkd ltkc
        
        ltkd: $(OBJ)
                $(CC) -o $@ $(OBJ) $(LDFLAGS)
        
       -ltkc: ltkc.o util.o
       -        $(CC) -o $@ ltkc.o util.o
       +ltkc: ltkc.o util.o memory.o
       +        $(CC) -o $@ ltkc.o util.o memory.o
        
        %.o: %.c
                $(CC) -c -o $@ $< $(CFLAGS)
   DIR diff --git a/color.c b/color.c
       t@@ -1,3 +1,4 @@
       +#include <stdarg.h>
        #include <X11/Xlib.h>
        #include <X11/Xutil.h>
        
       t@@ -8,8 +9,7 @@ void
        ltk_color_create(Display *dpy, int screen, Colormap cm, const char *hex, LtkColor *col) {
                if (!XParseColor(dpy, cm, hex, &col->xcolor)) {
                        /* FIXME: better error reporting!!! */
       -                /* FIXME: remove obsolete ltk_err function */
       -                ltk_err("ltk_color_create");
       +                ltk_fatal("ltk_color_create");
                }
                XAllocColor(dpy, cm, &col->xcolor);
                /* FIXME: replace with XftColorAllocValue; error checking */
   DIR diff --git a/ltk.h b/ltk.h
       t@@ -154,10 +154,6 @@ typedef struct ltk_window {
        ltk_rect ltk_rect_intersect(ltk_rect r1, ltk_rect r2);
        ltk_rect ltk_rect_union(ltk_rect r1, ltk_rect r2);
        void ltk_window_invalidate_rect(ltk_window *window, ltk_rect rect);
       -void ltk_warn(const char *format, ...);
       -void ltk_fatal(const char *format, ...);
       -void ltk_warn_errno(const char *format, ...);
       -void ltk_fatal_errno(const char *format, ...);
        int ltk_create_xcolor(ltk_window *window, const char *hex, XColor *col);
        void ltk_queue_event(ltk_window *window, ltk_event_type type, const char *id, const char *data);
        int ltk_collide_rect(ltk_rect rect, int x, int y);
   DIR diff --git a/ltkc.c b/ltkc.c
       t@@ -1,6 +1,6 @@
        /*
         * This file is part of the Lumidify ToolKit (LTK)
       - * Copyright (c) 2020 lumidify <nobody@lumidify.org>
       + * Copyright (c) 2021 lumidify <nobody@lumidify.org>
         *
         * Permission is hereby granted, free of charge, to any person obtaining a copy
         * of this software and associated documentation files (the "Software"), to deal
       t@@ -24,6 +24,7 @@
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
       +#include <stdarg.h>
        #include <stddef.h>
        #include <unistd.h>
        #include <time.h>
       t@@ -32,6 +33,7 @@
        #include <sys/socket.h>
        #include <sys/un.h>
        #include "util.h"
       +#include "memory.h"
        
        #define BLK_SIZE 128
        
       t@@ -44,8 +46,12 @@ static struct {
                int out_alloc;
        } io_buffers;
        
       +static char *ltk_dir = NULL;
       +static char *sock_path = NULL;
       +static int sockfd = -1;
       +
        int main(int argc, char *argv[]) {
       -        int sockfd, maxrfd, maxwfd;
       +        int maxrfd, maxwfd;
                int infd = fileno(stdin);
                int outfd = fileno(stdout);
                struct sockaddr_un un;
       t@@ -53,7 +59,6 @@ int main(int argc, char *argv[]) {
                struct timeval tv, tv_master;
                tv_master.tv_sec = 0;
                tv_master.tv_usec = 20000;
       -        char *ltk_dir = NULL, *sock_path = NULL;
                int path_size;
        
                if (argc != 2) {
       t@@ -69,11 +74,7 @@ int main(int argc, char *argv[]) {
        
                /* 7 because of "/", ".sock", and '\0' */
                path_size = strlen(ltk_dir) + strlen(argv[1]) + 7;
       -        sock_path = malloc(path_size);
       -        if (!sock_path) {
       -                (void)fprintf(stderr, "Unable to allocate memory for socket path.\n");
       -                return 1;
       -        }
       +        sock_path = ltk_malloc(path_size);
                snprintf(sock_path, path_size, "%s/%s.sock", ltk_dir, argv[1]);
        
                if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
       t@@ -92,14 +93,10 @@ int main(int argc, char *argv[]) {
                        return -2;
                }
        
       -        io_buffers.in_buffer = malloc(BLK_SIZE);
       -        if (!io_buffers.in_buffer)
       -                return 1;
       +        io_buffers.in_buffer = ltk_malloc(BLK_SIZE);
                io_buffers.in_alloc = BLK_SIZE;
        
       -        io_buffers.out_buffer = malloc(BLK_SIZE);
       -        if (!io_buffers.out_buffer)
       -                return 1;
       +        io_buffers.out_buffer = ltk_malloc(BLK_SIZE);
                io_buffers.out_alloc = BLK_SIZE;
        
                FD_ZERO(&rallfds);
       t@@ -190,9 +187,27 @@ int main(int argc, char *argv[]) {
                        }
                }
        
       -        close(sockfd);
       -        free(ltk_dir);
       -        free(sock_path);
       +        ltk_cleanup();
        
                return 0;
        }
       +
       +void
       +ltk_log_msg(const char *mode, const char *format, va_list args) {
       +        fprintf(stderr, "ltkc %s: ", mode);
       +        vfprintf(stderr, format, args);
       +}
       +
       +void
       +ltk_cleanup() {
       +        if (sockfd >= 0)
       +                close(sockfd);
       +        if (ltk_dir)
       +                ltk_free(ltk_dir);
       +        if (sock_path)
       +                ltk_free(sock_path);
       +        if (io_buffers.in_buffer)
       +                ltk_free(io_buffers.in_buffer);
       +        if (io_buffers.out_buffer)
       +                ltk_free(io_buffers.out_buffer);
       +}
   DIR diff --git a/ltkd.c b/ltkd.c
       t@@ -1,3 +1,4 @@
       +/* FIXME Figure out how to properly print window id */
        /* FIXME: PROPERLY CLEANUP ALL THEMES */
        /* FIXME: error checking in tokenizer (is this necessary?) */
        /* FIXME: parsing doesn't work properly with bs? */
       t@@ -99,7 +100,6 @@ static ltk_window *ltk_create_window(const char *title, int x, int y,
            unsigned int w, unsigned int h);
        static void ltk_destroy_widget_hash(void);
        static void ltk_destroy_window(ltk_window *window);
       -static void ltk_cleanup(void);
        static void ltk_redraw_window(ltk_window *window);
        static void ltk_window_other_event(ltk_window *window, XEvent event);
        static void ltk_handle_event(ltk_window *window, XEvent event);
       t@@ -352,7 +352,7 @@ open_log(char *dir) {
                return f;
        }
        
       -static void
       +void
        ltk_cleanup(void) {
                if (listenfd >= 0)
                        close(listenfd);
       t@@ -387,6 +387,23 @@ ltk_quit(ltk_window *window) {
                running = 0;
        }
        
       +void
       +ltk_log_msg(const char *mode, const char *format, va_list args) {
       +        char logtime[25]; /* FIXME: This should always be big enough, right? */
       +        time_t clock;
       +        struct tm *timeptr;
       +
       +        time(&clock);
       +        timeptr = localtime(&clock);
       +        strftime(&logtime, 25, "%Y-%m-%d %H:%M:%S", timeptr);
       +
       +        if (main_window)
       +                fprintf(stderr, "%s ltkd(%d) %s: ", logtime, main_window->xwindow, mode);
       +        else
       +                fprintf(stderr, "%s ltkd(?) %s: ", logtime, mode);
       +        vfprintf(stderr, format, args);
       +}
       +
        static int
        ltk_set_root_widget_cmd(
            ltk_window *window,
       t@@ -441,66 +458,6 @@ ltk_window_invalidate_rect(ltk_window *window, ltk_rect rect) {
                        window->dirty_rect = ltk_rect_union(rect, window->dirty_rect);
        }
        
       -static void
       -print_log(const char *mode, const char *format, va_list args) {
       -        char logtime[25]; /* FIXME: This should always be big enough, right? */
       -        FILE *errfile = ltk_logfile;
       -        time_t clock;
       -        struct tm *timeptr;
       -
       -        time(&clock);
       -        timeptr = localtime(&clock);
       -        strftime(&logtime, 25, "%Y-%m-%d %H:%M:%S", timeptr);
       -
       -        if (!errfile)
       -                errfile = stderr;
       -
       -        if (main_window)
       -                fprintf(errfile, "%s ltkd(%d) %s: ", logtime, main_window->xwindow, mode);
       -        else
       -                fprintf(errfile, "%s ltkd(?) %s: ", logtime, mode);
       -        vfprintf(errfile, format, args);
       -}
       -
       -void
       -ltk_warn(const char *format, ...) {
       -        va_list args;
       -        va_start(args, format);
       -        print_log("Warning", format, args);
       -        va_end(args);
       -}
       -
       -void
       -ltk_fatal(const char *format, ...) {
       -        va_list args;
       -        va_start(args, format);
       -        print_log("Fatal", format, args);
       -        va_end(args);
       -        ltk_cleanup();
       -
       -        exit(1);
       -};
       -
       -void
       -ltk_warn_errno(const char *format, ...) {
       -        va_list args;
       -        char *errstr = strerror(errno);
       -        va_start(args, format);
       -        print_log("Warning", format, args);
       -        va_end(args);
       -        ltk_warn("system error: %s\n", errstr);
       -}
       -
       -void
       -ltk_fatal_errno(const char *format, ...) {
       -        va_list args;
       -        char *errstr = strerror(errno);
       -        va_start(args, format);
       -        print_log("Fatal", format, args);
       -        va_end(args);
       -        ltk_fatal("system error: %s\n", errstr);
       -}
       -
        /* FIXME: Proper error checking by calling functions */
        int
        ltk_create_xcolor(ltk_window *window, const char *hex, XColor *col) {
   DIR diff --git a/memory.c b/memory.c
       t@@ -29,7 +29,7 @@
        #include <string.h>
        #include <stdint.h>
        #include "color.h"
       -#include "ltk.h"
       +#include "util.h"
        
        char *
        ltk_strdup_impl(const char *s) {
   DIR diff --git a/text_pango.c b/text_pango.c
       t@@ -60,7 +60,7 @@ ltk_text_line_set_width(LtkTextLine *tl, int width) {
        LtkTextLine *
        ltk_text_line_create(Window window, uint16_t font_size, char *text, int width) {
                if (!tm.context)
       -                ltk_err("ltk_text_line_create (pango): text not initialized yet");
       +                ltk_fatal("ltk_text_line_create (pango): text not initialized yet");
                LtkTextLine *line = ltk_malloc(sizeof(LtkTextLine));
                line->text = text;
                line->font_size = font_size;
   DIR diff --git a/util.c b/util.c
       t@@ -1,6 +1,6 @@
        /*
         * This file is part of the Lumidify ToolKit (LTK)
       - * Copyright (c) 2020 lumidify <nobody@lumidify.org>
       + * Copyright (c) 2021 lumidify <nobody@lumidify.org>
         *
         * Permission is hereby granted, free of charge, to any person obtaining a copy
         * of this software and associated documentation files (the "Software"), to deal
       t@@ -26,13 +26,12 @@
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
       +#include <stdarg.h>
        
       -void
       -ltk_err(const char *msg) {
       -        perror(msg);
       -        exit(1);
       -}
       +#include "util.h"
       +#include "memory.h"
        
       +/* FIXME: Should these functions really fail on memory error? */
        char *
        ltk_read_file(const char *path, unsigned long *len) {
                FILE *f;
       t@@ -42,8 +41,7 @@ ltk_read_file(const char *path, unsigned long *len) {
                fseek(f, 0, SEEK_END);
                *len = ftell(f);
                fseek(f, 0, SEEK_SET);
       -        file_contents = malloc(*len + 1);
       -        if (!file_contents) return NULL;
       +        file_contents = ltk_malloc(*len + 1);
                fread(file_contents, 1, *len, f);
                file_contents[*len] = '\0';
                fclose(f);
       t@@ -52,15 +50,12 @@ ltk_read_file(const char *path, unsigned long *len) {
        }
        
        /* If `needed` is larger than `*alloc_size`, resize `*str` to
       -   `max(needed, *alloc_size * 2)`.
       -   Returns 1 on error, 0 otherwise. If an error occurs, `*str` and
       -   `*alloc_size` are not modified. */
       +   `max(needed, *alloc_size * 2)`. Aborts program on error. */
        int
        ltk_grow_string(char **str, int *alloc_size, int needed) {
                if (needed <= *alloc_size) return 0;
                int new_size = needed > (*alloc_size * 2) ? needed : (*alloc_size * 2);
       -        char *new = realloc(*str, new_size);
       -        if (!new) return 1;
       +        char *new = ltk_realloc(*str, new_size);
                *str = new;
                *alloc_size = new_size;
                return 0;
       t@@ -79,18 +74,22 @@ ltk_setup_directory(void) {
        
                dir_orig = getenv("LTKDIR");
                if (dir_orig) {
       -                dir = strdup(dir_orig);
       +                dir = ltk_strdup(dir_orig);
       +                /*
                        if (!dir)
                                return NULL;
       +                */
                } else {
                        uid = getuid();
                        pw = getpwuid(uid);
                        if (!pw)
                                return NULL;
                        len = strlen(pw->pw_dir);
       -                dir = malloc(len + 6);
       +                dir = ltk_malloc(len + 6);
       +                /*
                        if (!dir)
                                return NULL;
       +                */
                        strcpy(dir, pw->pw_dir);
                        strcpy(dir + len, "/.ltk");
                }
       t@@ -105,8 +104,7 @@ ltk_setup_directory(void) {
        
        /* Concatenate the two given strings and return the result.
           This allocates new memory for the result string, unlike
       -   the actual strcat.
       -   Returns NULL on error. */
       +   the actual strcat. Aborts program on error */
        char *
        ltk_strcat_useful(const char *str1, const char *str2) {
                int len1, len2;
       t@@ -114,11 +112,48 @@ ltk_strcat_useful(const char *str1, const char *str2) {
        
                len1 = strlen(str1);
                len2 = strlen(str2);
       -        ret = malloc(len1 + len2 + 1);
       -        if (!ret)
       -                return NULL;
       +        ret = ltk_malloc(len1 + len2 + 1);
                strcpy(ret, str1);
                strcpy(ret + len1, str2);
        
                return ret;
        }
       +
       +void
       +ltk_warn(const char *format, ...) {
       +        va_list args;
       +        va_start(args, format);
       +        ltk_log_msg("Warning", format, args);
       +        va_end(args);
       +}
       +
       +void
       +ltk_fatal(const char *format, ...) {
       +        va_list args;
       +        va_start(args, format);
       +        ltk_log_msg("Fatal", format, args);
       +        va_end(args);
       +        ltk_cleanup();
       +
       +        exit(1);
       +};
       +
       +void
       +ltk_warn_errno(const char *format, ...) {
       +        va_list args;
       +        char *errstr = strerror(errno);
       +        va_start(args, format);
       +        ltk_log_msg("Warning", format, args);
       +        va_end(args);
       +        ltk_warn("system error: %s\n", errstr);
       +}
       +
       +void
       +ltk_fatal_errno(const char *format, ...) {
       +        va_list args;
       +        char *errstr = strerror(errno);
       +        va_start(args, format);
       +        ltk_log_msg("Fatal", format, args);
       +        va_end(args);
       +        ltk_fatal("system error: %s\n", errstr);
       +}
   DIR diff --git a/util.h b/util.h
       t@@ -1,6 +1,6 @@
        /*
         * This file is part of the Lumidify ToolKit (LTK)
       - * Copyright (c) 2020 lumidify <nobody@lumidify.org>
       + * Copyright (c) 2021 lumidify <nobody@lumidify.org>
         *
         * Permission is hereby granted, free of charge, to any person obtaining a copy
         * of this software and associated documentation files (the "Software"), to deal
       t@@ -21,14 +21,20 @@
         * SOFTWARE.
         */
        
       +/* Requires: <stdarg.h> */
       +
        #ifndef __OpenBSD__
        long long
        strtonum(const char *numstr, long long minval, long long maxval,
            const char **errstrp);
        #endif
        
       -void ltk_err(const char *msg);
        char *ltk_read_file(const char *path, unsigned long *len);
        int ltk_grow_string(char **str, int *alloc_size, int needed);
        char *ltk_setup_directory(void);
        char *ltk_strcat_useful(const char *str1, const char *str2);
       +
       +/* Note: these are actually implemented in ltkd.c and ltkc.c (they are just
       +   declared here so they can be used by the utility functions */
       +void ltk_log_msg(const char *mode, const char *format, va_list args);
       +void ltk_cleanup(void);