]> Trent Huber's Code - xpmview.git/commitdiff
Add ability to clean build, more refactoring
authorTrent Huber <trentmhuber@gmail.com>
Sat, 19 Apr 2025 02:47:03 +0000 (22:47 -0400)
committerTrent Huber <trentmhuber@gmail.com>
Sat, 19 Apr 2025 02:47:03 +0000 (22:47 -0400)
16 files changed:
.gitignore
build.c
build.h
clean.c [new file with mode: 0644]
external/build.c
external/cbs
src/build.c
src/colors/build.c
src/colors/gencolors.c
src/font/build.c
src/font/genfont.c
src/main.c
src/options.c
src/utilities.c [moved from src/error.c with 70% similarity]
src/utilities.h [moved from src/error.h with 54% similarity]
src/xpm.c

index c685519a375be24df8ed1031599d1673bdc530ee..c38734d8110810d1f4629ab5491def6a684ffd9a 100644 (file)
@@ -1,2 +1,3 @@
 *build
+*clean
 *.o
diff --git a/build.c b/build.c
index fc92b2b851c31c402bf7ed489c2abbb9a2767081..9275936e32f9ca886d5ae7a57ec73aa78e02ce17 100644 (file)
--- a/build.c
+++ b/build.c
@@ -1,3 +1,6 @@
+#define ROOT
+#include "build.h"
+
 #include "external/cbs/cbs.c"
 
 int main(void) {
@@ -6,5 +9,10 @@ int main(void) {
    build("external/");
    build("src/");
 
+   cflags = (char *[]){CFCBS, NULL};
+   compile("clean", CBS, NULL);
+
+   load('x', "clean", "clean", NULL);
+
    return 0;
 }
diff --git a/build.h b/build.h
index 4e79208312982b43373127206ab9f79105747fbe..dfcf343ec5bb33933823b3df65c146f6b7a4413b 100644 (file)
--- a/build.h
+++ b/build.h
@@ -1,3 +1,6 @@
+/* Switch the following #define to change whether or not raylib is built as a
+ * static or dynamic library; be sure to `./clean' before you `./build' again.
+ */
 // #define RLDYNAMIC
 
 #ifdef RLDYNAMIC
@@ -6,23 +9,34 @@
 #define RLEXT ".a"
 #endif
 
-#define SRC ROOT "src/"
-
+#define CBSPATH ROOT "external/cbs/"
+#define CBS CBSPATH "cbs.c"
 #define RLROOT ROOT "external/raylib/"
+#define RLLIB RLROOT "raylib" RLEXT
 #define RLSRC RLROOT "src/"
 #define RLHDR RLSRC "raylib"
-#define RLLIB RLROOT "raylib" RLEXT
+#define SIMPLEXPM ROOT "bin/simplexpm"
+#define SRC ROOT "src/"
+#define UTILS SRC "utilities"
 
+#define CFCBS "-I" CBSPATH
 #define CFRAYLIB "-I" RLSRC
 #define CFSRC "-I" SRC
 
 #ifdef __APPLE__
-#define LFRAYLIB \
+#define RLLFLAGS \
    "-framework", "Cocoa", \
    "-framework", "CoreVideo", \
    "-framework", "GLUT", \
    "-framework", "IOKit", \
    "-framework", "OpenGL"
 #else
-#define LFRAYLIB "-lm"
+#define RLLFLAGS "-lm"
+#endif
+#ifdef RLDYNAMIC
+#define LFEXTERNAL RLLFLAGS
+#define LFRAYLIB NULL
+#else
+#define LFEXTERNAL NULL
+#define LFRAYLIB RLLFLAGS
 #endif
diff --git a/clean.c b/clean.c
new file mode 100644 (file)
index 0000000..cddf955
--- /dev/null
+++ b/clean.c
@@ -0,0 +1,48 @@
+#define ROOT
+#include "build.h"
+
+#define COLORS SRC "colors.c"
+#define FIND "/usr/bin/find"
+#define FONT SRC "font.c"
+#define GENCOLORS SRC "colors/gencolors"
+#define GENFONT SRC "font/genfont"
+#define RM "/bin/rm"
+
+#include "external/cbs/cbs.c"
+
+int main(void) {
+   char *what, **whos, **regexes, ***rms;
+   size_t i;
+   pid_t cpid;
+
+   what = "removal";
+
+   // Remove build executables and object files
+   whos = (char *[2]){"{*/*build,*/*/*build}", "{*/*.o,*/*/*.o,*/*/*/*.o}"};
+   regexes = (char *[2]){".*[^\\.]/build$", ".*\\.o$"};
+   for (i = 0; i < 2; ++i) {
+       if ((cpid = fork()) == 0)
+           run(FIND, (char *[]){"find", ".", "-regex", regexes[i],
+                                "-exec", "rm", "{}", "+", NULL}, what, whos[i]);
+       await(cpid, what, whos[i]);
+   }
+
+   /* Remove raylib library, application executables,
+    * automatically generated source files, and ourself
+    */
+   whos = (char *[4]){extend(RLLIB, RLEXT),
+                      "{" GENCOLORS "," GENFONT "," SIMPLEXPM "}",
+                      "{" COLORS "," FONT "}", "clean"};
+   rms = (char **[4]){(char *[3]){whos[0]},
+                      (char *[3]){GENCOLORS, GENFONT, SIMPLEXPM},
+                      (char *[3]){COLORS, FONT}, (char *[3]){whos[3]}};
+   for (i = 0; i < 4; ++i) {
+       if ((cpid = fork()) == 0)
+           run(RM, (char *[]){"rm", rms[i][0], rms[i][1], rms[i][2], NULL},
+               what, whos[i]);
+       await(cpid, what, whos[i]);
+   }
+   free(whos[0]);
+
+   return EXIT_SUCCESS;
+}
index 5f44c4e2831c36ed0a5c01ff5e586f664dc66b39..f8e5d9ed3a0b1a4ed2adb05eef6cd44ca6863a6f 100644 (file)
@@ -7,22 +7,22 @@
    RLSRC "rtextures", RLSRC "utils", \
    RLSRC "rcore", RLSRC "rglfw"
 
-#ifdef RLDYNAMIC
-#define CFRLGLOBAL "-DPLATFORM_DESKTOP", "-fPIC"
+#ifdef __APPLE__
+#define CFGRAPHICS "-x", "objective-c"
 #else
-#define CFRLGLOBAL "-DPLATFORM_DESKTOP"
+#define CFGRAPHICS "-D_GLFW_X11"
 #endif
-#define CFRLGLFW "-I" RLSRC "external/glfw/include"
-#ifdef __APPLE__
-#define CFRLX "-x", "objective-c"
+#define CFGLFW "-I" RLSRC "external/glfw/include"
+#ifdef RLDYNAMIC
+#define CFGLOBALS "-DPLATFORM_DESKTOP", "-fPIC"
 #else
-#define CFRLX "-D_GLFW_X11"
+#define CFGLOBALS "-DPLATFORM_DESKTOP"
 #endif
 
 #ifdef RLDYNAMIC
-#define RLTYPE 'd'
+#define LIBTYPE 'd'
 #else
-#define RLTYPE 's'
+#define LIBTYPE 's'
 #endif
 
 #include "cbs/cbs.c"
@@ -34,17 +34,15 @@ int main(void) {
    build(NULL);
 
    src = (char *[]){RLSRCS, NULL};
-   cflags = (char *[]){CFRLGLOBAL, NULL};
+   cflags = (char *[]){CFGLOBALS, NULL};
    for (i = 0; i < 6; ++i) compile(src[i], NULL);
-   cflags = (char *[]){CFRLGLOBAL, CFRLGLFW, NULL};
+   cflags = (char *[]){CFGLFW, CFGLOBALS, NULL};
    compile(src[6], NULL);
-   cflags = (char *[]){CFRLGLOBAL, CFRLGLFW, CFRLX, NULL};
+   cflags = (char *[]){CFGRAPHICS, CFGLFW, CFGLOBALS, NULL};
    compile(src[7], NULL);
 
-#ifdef RLDYNAMIC
-   lflags = (char *[]){LFRAYLIB, NULL};
-#endif
-   load(RLTYPE, RLLIB, RLSRCS, NULL);
+   lflags = (char *[]){LFEXTERNAL, NULL};
+   load(LIBTYPE, RLLIB, RLSRCS, NULL);
 
-   return 0;
+   return EXIT_SUCCESS;
 }
index 3f59e2fa436ef5d7e527fd39e52a050536669db7..a7ad152e9248d770b63684c5e019348457100a1e 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 3f59e2fa436ef5d7e527fd39e52a050536669db7
+Subproject commit a7ad152e9248d770b63684c5e019348457100a1e
index c199a03ee7154fbf23fa239272c65fc3195425ad..d828156c6494b780992e9253a30bc4e92a05c717 100644 (file)
@@ -1,32 +1,25 @@
 #define ROOT "../"
 #include "../build.h"
 
-#define BIN ROOT "bin/"
-#define CBS ROOT "external/cbs/"
-
-#define CFCBS "-I" CBS
-
 #include "../external/cbs/cbs.c"
 
 int main(void) {
    build(NULL);
 
-   compile("error", NULL);
+   compile("utilities", NULL);
 
    build("colors/");
    build("font/");
 
    cflags = (char *[]){CFRAYLIB, NULL};
-   compile("main", "error", RLHDR, "xpm", "font.c", NULL);
+   compile("main", "utilities", "options", RLHDR, "xpm", "font.c", NULL);
    cflags = NULL;
-   compile("options", "error", NULL);
+   compile("options", "utilities", NULL);
    cflags = (char *[]){CFRAYLIB, CFCBS, NULL};
-   compile("xpm", "error", RLHDR, "xpm", CBS "cbs.c", "colors.c", NULL);
+   compile("xpm", "utilities", RLHDR, "xpm", CBS, "colors.c", NULL);
 
-#ifndef RLDYNAMIC
    lflags = (char *[]){LFRAYLIB, NULL};
-#endif
-   load('x', BIN "simplexpm", "error", "main", "options", "xpm", RLLIB, NULL);
+   load('x', SIMPLEXPM, "utilities", "main", "options", "xpm", RLLIB, NULL);
 
-   return 0;
+   return EXIT_SUCCESS;
 }
index d6fcbfba2b613d61d4a2a055349e7ba0714dc424..7e83836dc9142b75f92225234ce42d7d240b27e7 100644 (file)
@@ -1,18 +1,20 @@
 #define ROOT "../../"
 #include "../../build.h"
 
+#define COLORS SRC "colors.c"
+
 #include "../../external/cbs/cbs.c"
 
 int main(void) {
    build(NULL);
 
    cflags = (char *[]){CFSRC, NULL};
-   compile("gencolors", SRC "error", NULL);
-   load('x', "gencolors", SRC "error", "gencolors", NULL);
+   compile("gencolors", UTILS, NULL);
+   load('x', "gencolors", "gencolors", UTILS, NULL);
 
-   if (modified(SRC "colors.c", "gencolors.c")
-       || modified(SRC "colors.c", "rgb.txt"))
-       run("gencolors", (char *[]){"./gencolors", NULL}, "execution", "gencolors");
+   if (modified(COLORS, "gencolors.c") || modified(COLORS, "rgb.txt"))
+       run("gencolors", (char *[]){"./gencolors", "rgb.txt", COLORS, NULL},
+           "execution", "gencolors");
 
-   return 0;
+   return EXIT_SUCCESS;
 }
index f9d0ea17645644ec9d815f41a9e1416ce4970d18..b5dd82c07c957112f74433f6bcad0d1739a6399f 100644 (file)
@@ -7,70 +7,74 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "error.h"
+#include "utilities.h"
 
-int main(void) {
-   int result, infd, outfd, i;
+int main(int argc, char **argv) {
+   int result, infd, outfd;
    struct stat instat;
-   size_t len;
+   size_t l, i;
    void *map;
    char *p;
-   long r, g, b;
+   unsigned char r, g, b;
 
-   result = 1;
-   if ((infd = open("rgb.txt", O_RDONLY)) == -1) {
-       xpmerror("Unable to open `rgb.txt' for reading");
+   result = EXIT_FAILURE;
+   if (argc != 3) {
+       xpmerror("Incorrect number of arguments: %s <input.txt> <output.c>", argv[0]);
+       return result;
+   }
+   if ((infd = open(argv[1], O_RDONLY)) == -1) {
+       xpmerror("Unable to open `%s' for reading", argv[1]);
        return result;
    }
    if (fstat(infd, &instat) == -1) {
-       xpmerror("Unable to stat `rgb.txt'");
+       xpmerror("Unable to stat `%s'", argv[1]);
        goto closein;
    }
-   len = instat.st_size;
-   if ((p = map = mmap(NULL, len, PROT_READ | PROT_WRITE,
+   l = instat.st_size;
+   if ((p = map = mmap(NULL, l, PROT_READ | PROT_WRITE,
                        MAP_PRIVATE, infd, 0)) == MAP_FAILED) {
-       xpmerror("Unable to map `rgb.txt' to memory");
+       xpmerror("Unable to map `%s' to memory", argv[1]);
        goto closein;
    }
-   if ((outfd = open("../colors.c", O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
-       xpmerror("Unable to open `../colors.c' for writing");
+   if ((outfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
+       xpmerror("Unable to open `%s' for writing", argv[2]);
        goto munmap;
    }
 
-   result = 0;
+   result = EXIT_SUCCESS;
    if (dprintf(outfd, "struct color {\n"
                "\tchar *name;\n"
                "\tunsigned int value;\n"
                "};\n\n"
                "static struct color colors[] = {\n"
                "\t{\"None\", 0x00ffffff},\n") == -1) {
-       xpmerror("Unable to write to `../colors.c'");
+       xpmerror("Unable to write to `%s'", argv[2]);
        goto closeout;
    }
-   for (i = 1; p < (char *)map + len; ++i) {
+   for (i = 1; p < (char *)map + l; ++i) {
        r = strtol(p, &p, 10);
        g = strtol(p, &p, 10);
        b = strtol(p, &p, 10);
-       if (dprintf(outfd, "\t{\"%s\", 0x%02lx%02lx%02lx},\n",
+       if (dprintf(outfd, "\t{\"%s\", 0x%02x%02x%02x},\n",
                    strsep(&p, "\n") + 2, r, g, b) == -1) {
-           xpmerror("Unable to write to `../colors.c'");
+           xpmerror("Unable to write to `%s'", argv[2]);
            goto closeout;
        }
    }
-   if (dprintf(outfd, "};\n\nstatic size_t numcolors = %d;\n", i) == -1)
-       xpmerror("Unable to write to `../colors.c'");
+   if (dprintf(outfd, "};\n\nstatic size_t numcolors = %zu;\n", i) == -1)
+       xpmerror("Unable to write to `%s'", argv[2]);
 
 closeout:
    if (close(outfd) == -1)
-       xpmerror("Unable to close `../colors.c'");
+       xpmerror("Unable to close `%s'", argv[2]);
 
 munmap:
-   if (munmap(map, len) == -1)
-       xpmerror("Unable to unmap memory associated with `rgb.txt'");
+   if (munmap(map, l) == -1)
+       xpmerror("Unable to unmap memory associated with `%s'", argv[1]);
 
 closein:
    if (close(infd) == -1)
-       xpmerror("Unable to close `rgb.txt'");
+       xpmerror("Unable to close `%s'", argv[1]);
 
    return result;
 }
index 87f5e39311d2e732f2abc1e77b82c43e50fe7d12..989db8bdb1ba64f18cfc4aa6ec7a7e60fb4346a8 100644 (file)
@@ -1,21 +1,22 @@
 #define ROOT "../../"
 #include "../../build.h"
 
+#define FONT SRC "font.c"
+
 #include "../../external/cbs/cbs.c"
 
 int main(void) {
    build(NULL);
 
    cflags = (char *[]){CFRAYLIB, CFSRC, NULL};
-   compile("genfont", SRC "error", RLHDR, NULL);
+   compile("genfont", UTILS, RLHDR, NULL);
 
-#ifndef RLDYNAMIC
    lflags = (char *[]){LFRAYLIB, NULL};
-#endif
-   load('x', "genfont", SRC "error", "genfont", RLLIB, NULL);
+   load('x', "genfont", "genfont", UTILS, RLLIB, NULL);
 
-   if (modified(SRC "font.c", "genfont.c") || modified(SRC "font.c", "font.ttf"))
-       run("genfont", (char *[]){"./genfont", NULL}, "execution", "genfont");
+   if (modified(FONT, "genfont.c") || modified(FONT, "font.ttf"))
+       run("genfont", (char *[]){"./genfont", "font.ttf", FONT, NULL},
+           "execution", "genfont");
 
-   return 0;
+   return EXIT_SUCCESS;
 }
index 4fe33c178be5748c42d0a504285d7f993cd08ad4..764916e7feda79d0e19a1a19b88a07f4ec7cc4d0 100644 (file)
@@ -1,19 +1,23 @@
-#include <stdio.h>
 #include <stdlib.h>
 
-#include "error.h"
 #include "raylib.h"
+#include "utilities.h"
 
-int main(void) {
+int main(int argc, char **argv) {
    Font font;
 
+   if (argc != 3) {
+       xpmerror("Incorrect number of arguments: %s <input.ttf> <output.c>", argv[0]);
+       return EXIT_FAILURE;
+   }
+
    SetTraceLogLevel(LOG_WARNING);
    InitWindow(0, 0, "");
 
-   font = LoadFontEx("font.ttf", 48, NULL, 95);
-   if (!ExportFontAsCode(font, "../font.c")) {
-       xpmerror("Unable to generate `font.c' from `font.ttf'");
-       exit(EXIT_FAILURE);
+   font = LoadFontEx(argv[1], 48, NULL, 95);
+   if (!ExportFontAsCode(font, argv[2])) {
+       xpmerror("Unable to generate `%s' from `%s'", argv[2], argv[1]);
+       return EXIT_FAILURE;
    }
 
    UnloadFont(font);
@@ -22,5 +26,5 @@ int main(void) {
     * the transient application we open to load the font
     */
 
-   return 0;
+   return EXIT_SUCCESS;
 }
index 89d0e93b954ea877d4d46e20375fd7fc2717ae47..b0365088a0215ae3b042e99347a5746e63cc2e79 100644 (file)
@@ -1,5 +1,4 @@
 #include <setjmp.h>
-#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -7,29 +6,73 @@
 #include <sys/time.h>
 #include <unistd.h>
 
-#include "error.h"
 #include "options.h"
 #define SUPPORT_IMAGE_EXPORT
 #include "raylib.h"
+#include "utilities.h"
 #include "xpm.h"
 
 extern bool isGpuReady;
 
 #include "font.c"
 
+static void getinput(char **xp, Texture2D *tp, Image *ip) {
+   FilePathList files;
+   static int mode = DEFAULT;
+   KeyboardKey key;
+   size_t len;
+
+   if (IsFileDropped()) {
+       files = LoadDroppedFiles();
+       if (*xp) RL_FREE(*xp);
+       *xp = xpmalloc(FILENAME_MAX);
+       strcpy(*xp, files.paths[0]);
+       UnloadDroppedFiles(files);
+       *tp = gettexture(*xp, ip, mode);
+   } else switch ((key = GetKeyPressed())) {
+   case KEY_R:
+       if (*xp) *tp = gettexture(*xp, ip, mode);
+   case KEY_NULL:
+       break;
+   case KEY_S:
+       if (tp->id == 0) break;
+       len = strlen(*xp);
+       strncpy(*xp + len - 4, ".png", 4);
+       ExportImage(*ip, *xp);
+       strncpy(*xp + len - 4, ".xpm", 4);
+       break;
+   default:
+       switch (key) {
+       case KEY_M:
+           mode = MODEM;
+           break;
+       case KEY_FOUR:
+           mode = MODEG4;
+           break;
+       case KEY_G:
+           mode = MODEG;
+           break;
+       case KEY_C:
+           mode = MODEC;
+           break;
+       default:
+           return;
+       }
+       if (*xp) *tp = gettexture(NULL, ip, mode);
+   }
+}
+
 int main(int argc, char **argv) {
-   int debug, mode;
+   int debug;
    char *xpm, *welcome, *error;
    Image image;
    Texture2D texture;
    Font font;
-   FilePathList files;
-   KeyboardKey key;
-   size_t len, width, height;
+   size_t width, height;
    float scale;
    Vector2 pos, dim;
 
-   if (!options(argc, argv, &debug, &xpm)) return 1;
+   if (!options(argc, argv, &debug, &xpm)) return EXIT_FAILURE;
 
    if (!debug) SetTraceLogLevel(LOG_ERROR);
    InitWindow(800, 600, "simplexpm");
@@ -38,51 +81,13 @@ int main(int argc, char **argv) {
    SetExitKey(KEY_Q);
 
    image = (Image){0};
-   mode = DEFAULT;
-   texture = gettexture(xpm, &image, mode);
-   if (xpm && texture.id == 0) return 1;
+   texture = gettexture(xpm, &image, DEFAULT);
+   if (xpm && texture.id == 0) return EXIT_FAILURE;
    font = LoadFont_Font();
    welcome = "Drag and drop an XPM file here";
    error = "Unable to parse XPM file:\n see console for details";
    while (!WindowShouldClose()) {
-       if (IsFileDropped()) {
-           files = LoadDroppedFiles();
-           if (xpm) RL_FREE(xpm);
-           xpm = RL_CALLOC(FILENAME_MAX, 1);
-           TextCopy(xpm, files.paths[0]);
-           UnloadDroppedFiles(files);
-           texture = gettexture(xpm, &image, mode);
-       } else switch ((key = GetKeyPressed())) {
-       case KEY_R:
-           if (xpm) texture = gettexture(xpm, &image, mode);
-           break;
-       case KEY_S:
-           if (texture.id == 0) break;
-           len = strlen(xpm);
-           strncpy(xpm + len - 4, ".png", 4);
-           ExportImage(image, xpm);
-           strncpy(xpm + len - 4, ".xpm", 4);
-           break;
-       default:
-           switch (key) {
-           case KEY_M:
-               mode = MODEM;
-               break;
-           case KEY_FOUR:
-               mode = MODEG4;
-               break;
-           case KEY_G:
-               mode = MODEG;
-               break;
-           case KEY_C:
-               mode = MODEC;
-               break;
-           default:
-               continue;
-           }
-           if (xpm) texture = gettexture(NULL, &image, mode);
-       case KEY_NULL:;
-       }
+       getinput(&xpm, &texture, &image);
 
        BeginDrawing();
 
@@ -111,10 +116,10 @@ int main(int argc, char **argv) {
        EndDrawing();
    }
 
-   if (xpm) RL_FREE(xpm);
+   if (xpm) free(xpm);
 
    UnloadTexture(texture);
    CloseWindow();
 
-   return 0;
+   return EXIT_SUCCESS;
 }
index 6c3afb2777b6e82fddb38e13f868272a81461790..62966dc5eef4af51a920fef887b317cfa4552380 100644 (file)
@@ -4,7 +4,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "error.h"
+#include "utilities.h"
 
 static void usage(char *prog, int fd) {
    dprintf(fd, "Usage: %s [-dh] [-f file]\n"
@@ -21,25 +21,26 @@ static void usage(char *prog, int fd) {
            "\t c        Color visual mode\n", prog);
 }
 
-int options(int argc, char **argv, int *debug, char **xpmp) {
+int options(int argc, char **argv, int *debug, char **xp) {
    int opt, result, dnfd;
 
    *debug = 0;
-   *xpmp = NULL;
+   *xp = NULL;
    while ((opt = getopt(argc, argv, "df:h")) != -1) switch (opt) {
    case 'd':
        *debug = 1;
        break;
    case 'f':
-       *xpmp = strdup(optarg);
+       *xp = xpmalloc(FILENAME_MAX);
+       strcpy(*xp, optarg);
        break;
    case 'h':
        usage(argv[0], STDOUT_FILENO);
-       exit(0);
+       exit(EXIT_SUCCESS);
    case '?':
    default:
        usage(argv[0], STDERR_FILENO);
-       exit(1);
+       exit(EXIT_FAILURE);
    }
 
    result = 1;
@@ -53,7 +54,7 @@ int options(int argc, char **argv, int *debug, char **xpmp) {
            result = 0;
        } else if (dup2(dnfd, STDOUT_FILENO) == -1) {
            xpmerror("Unable to redirect stdout to `/dev/null'");
-           exit(1);
+           exit(EXIT_FAILURE);
        }
        if (close(dnfd) == -1) {
            xpmerror("Unable to close `/dev/null'");
similarity index 70%
rename from src/error.c
rename to src/utilities.c
index 13e89a694d87852309b84a3913fd975ea77f0168..00d780f611f2811864b94f28e0269c2437effb41 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/errno.h>
 #include <unistd.h>
@@ -17,3 +18,12 @@ void xpmerror(char *fmt, ...) {
    }
    dprintf(STDERR_FILENO, "\n");
 }
+
+void *xpmalloc(size_t s) {
+   void *r;
+
+   if ((r = malloc(s))) return memset(r, 0, s);
+
+   xpmerror("Memory allocation");
+   exit(EXIT_FAILURE);
+}
similarity index 54%
rename from src/error.h
rename to src/utilities.h
index 47501f3ec7d76b3f8f9ed50d2f41c6fa575c6cf6..d73b62c36622417f6cd5056bc4912e48d221c21d 100644 (file)
@@ -1 +1,2 @@
 void xpmerror(char *fmt, ...);
+void *xpmalloc(size_t s);
index 01f144e5ddbb2c1cc8415ab6691bb844fb7773d4..9b83f631cf6be4e75486058df4f2ad9e63616235 100644 (file)
--- a/src/xpm.c
+++ b/src/xpm.c
@@ -2,10 +2,14 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 
-#include "error.h"
 #include "raylib.h"
+#include "utilities.h"
 #include "xpm.h"
 
+#define TMP "/tmp/xpm"
+#define TMPSRC TMP ".c"
+#define TMPLIB "/tmp/libxpm" DYEXT
+
 #include "cbs.c"
 #include "colors.c"
 
@@ -21,15 +25,6 @@ static int space(char c) {
    return c == ' ' || c == '\t';
 }
 
-static void *zalloc(size_t s) {
-   void *r;
-
-   if ((r  = calloc(1, s))) return r;
-
-   xpmerror("Memory allocation");
-   exit(EXIT_FAILURE);
-}
-
 static char *arrname(char *p, size_t l) {
    size_t step;
    char *start, *r;
@@ -45,7 +40,7 @@ static char *arrname(char *p, size_t l) {
    start = p;
    for (; !space(*p) && *p != '['; ++p, --l) if (l == 0) return NULL;
    l = p - start;
-   r = zalloc(l + 1);
+   r = xpmalloc(l + 1);
    strncpy(r, start, l);
 
    return r;
@@ -69,9 +64,6 @@ static int key2mode(char **strp) {
    case 'm':
        r = MODEM;
        break;
-   case 's':
-       r = SYMBOLIC;
-       break;
    case 'g':
        if (**strp == '4') {
            ++*strp;
@@ -86,6 +78,9 @@ static int key2mode(char **strp) {
    default:
        xpmerror("Unknown key `%c'", *(*strp - 1));
        r = NUMMODES;
+       break;
+   case 's':
+       r = SYMBOLIC;
    }
 
    while (space(**strp)) ++*strp;
@@ -158,8 +153,8 @@ static Image parse(char **data, long *sizep) {
    }
 
    // Colors
-   chars = zalloc(ncolors * cpp * sizeof*chars);
-   colors = zalloc(NUMMODES * ncolors * sizeof*colors);
+   chars = xpmalloc(ncolors * cpp * sizeof*chars);
+   colors = xpmalloc(NUMMODES * ncolors * sizeof*colors);
    for (i = 0; i < ncolors; ++i) {
        p = data[1 + i];
        strncpy(chars + i * cpp, p, cpp);
@@ -179,7 +174,7 @@ static Image parse(char **data, long *sizep) {
    }
 
    // Pixels
-   pixels = zalloc(NUMMODES * height * width * sizeof*pixels);
+   pixels = xpmalloc(NUMMODES * height * width * sizeof*pixels);
    j = width;
    l = 0;
    for (i = 0, pp = &data[1 + ncolors];
@@ -217,7 +212,7 @@ static Image process(char *xpm) {
    int xpmfd, srcfd, e, cpid, status;
    struct stat xstat;
    size_t l, offset;
-   char *map, *p, *a, **data;
+   char *map, *p, *a, *tmp, **data;
    void *d;
    long *sizep;
 
@@ -247,52 +242,49 @@ static Image process(char *xpm) {
        goto munmap;
    }
 
-   if ((srcfd = open("/tmp/xpm.c", O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
-       xpmerror("Unable to open `/tmp/xpm.c'");
+   if ((srcfd = open(TMPSRC, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
+       xpmerror("Unable to open `" TMPSRC "'");
        goto munmap;
    }
    e = !writeall(srcfd, p, l - offset)
        || dprintf(srcfd, "\n\nlong size = sizeof %s / sizeof*%s;\n", a, a) < 0;
    if (close(srcfd) == -1) {
-       xpmerror("Unable to close `/tmp/xpm.c'");
+       xpmerror("Unable to close `" TMPSRC "'");
        goto munmap;
    }
    if (e) {
-       xpmerror("Unable to write to `/tmp/xpm.c'");
+       xpmerror("Unable to write to `" TMPSRC "'");
        goto munmap;
    }
 
    if ((cpid = fork()) == 0) {
-       compile("/tmp/xpm", NULL);
-       load('d', "/tmp/xpm", "/tmp/xpm", NULL);
+       compile(TMP, NULL);
+       load('d', TMP, TMP, NULL);
        exit(EXIT_SUCCESS);
    }
    if (cpid == -1 || waitpid(cpid, &status, 0) == -1
        || !WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) {
-       xpmerror("Unable to create `/tmp/libxpm" DYEXT "'");
+       xpmerror("Unable to create `" TMPLIB "'");
        goto munmap;
    }
 
-   if ((d = dlopen("/tmp/libxpm" DYEXT, RTLD_LAZY)) == NULL) {
-       xpmerror("Unable to load `/tmp/libxpm" DYEXT "': %s", dlerror());
+   if ((d = dlopen(TMPLIB, RTLD_LAZY)) == NULL) {
+       xpmerror("Unable to load `" TMPLIB "': %s", dlerror());
        goto munmap;
    }
    if ((data = (char **)dlsym(d, a)) == NULL) {
-       xpmerror("Unable to load image data from `/tmp/libxpm" DYEXT "': `%s'",
-                dlerror());
+       xpmerror("Unable to load image data from `" TMPLIB "': `%s'", dlerror());
        goto dlclose;
    }
    if ((sizep = (long *)dlsym(d, "size")) == NULL) {
-       xpmerror("Unable to load image length from `/tmp/libxpm" DYEXT "': `%s'",
-             dlerror());
+       xpmerror("Unable to load image length from `" TMPLIB "': `%s'", dlerror());
        goto dlclose;
    }
 
    result = parse(data, sizep);
 
 dlclose:
-   if (dlclose(d))
-       xpmerror("Unable to unload `/tmp/libxpm" DYEXT "': %s", dlerror());
+   if (dlclose(d)) xpmerror("Unable to unload `" TMPLIB "': %s", dlerror());
 
 munmap:
    if (munmap(map, l) == -1) xpmerror("Unable to unmap `%s' from memory", xpm);