commit 34b4388f7fddbfe1af5164983ff624e075537a31 from: Omar Polo date: Tue Feb 08 22:27:10 2022 UTC simplify pagebundler usage: deduce the variable name from the file commit - afda2dda15548492a0e1f66f7b9917f963623ce3 commit + 34b4388f7fddbfe1af5164983ff624e075537a31 blob - 1900f6e86054d439679f189eb54ca84865cbd42b blob + 7a339054b7fe65fb41b222274b2b6c11198c7658 --- Makefile.am +++ Makefile.am @@ -90,10 +90,10 @@ PAGES = $(srcdir)/pages/about_about.gmi \ pages.c: pagebundler $(srcdir)/pages.h ${PAGES} echo > $@ echo "#include \"pages.h\"" >> $@ - ./pagebundler -f $(srcdir)/pages/about_about.gmi -v about_about >> $@ - ./pagebundler -f $(srcdir)/pages/about_blank.gmi -v about_blank >> $@ - ./pagebundler -f $(srcdir)/pages/about_crash.gmi -v about_crash >> $@ - ./pagebundler -f $(srcdir)/pages/about_help.gmi -v about_help >> $@ - ./pagebundler -f $(srcdir)/pages/about_license.gmi -v about_license >> $@ - ./pagebundler -f $(srcdir)/pages/about_new.gmi -v about_new >> $@ - ./pagebundler -f $(srcdir)/pages/bookmarks.gmi -v bookmarks >> $@ + ./pagebundler $(srcdir)/pages/about_about.gmi >> $@ + ./pagebundler $(srcdir)/pages/about_blank.gmi >> $@ + ./pagebundler $(srcdir)/pages/about_crash.gmi >> $@ + ./pagebundler $(srcdir)/pages/about_help.gmi >> $@ + ./pagebundler $(srcdir)/pages/about_license.gmi >> $@ + ./pagebundler $(srcdir)/pages/about_new.gmi >> $@ + ./pagebundler $(srcdir)/pages/bookmarks.gmi >> $@ blob - 0cf350bcd32a8adc7715bd44cc6120df62c55ef4 blob + c6213eb152fc7ca12894974ace9ea49632edc6fb --- pagebundler.c +++ pagebundler.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Omar Polo + * Copyright (c) 2022, 2021 Omar Polo * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,49 +19,60 @@ * be compiled. The generated code provides a variable that holds the * content of the original file and a _len variable with the size. * - * Usage: pagebundler -f file -v varname > outfile + * Usage: pagebundler file > outfile */ #include +#include #include #include +#include #include #include -const char *file; -const char *varname; +static void +setfname(const char *fname, char *buf, size_t siz) +{ + const char *c, *d; + size_t len; + if ((c = strrchr(fname, '/')) != NULL) + c++; + else + c = fname; + + if ((d = strrchr(fname, '.')) == NULL || c > d) + d = strchr(fname, '\0'); + + len = d - c; + if (len >= siz) { + fprintf(stderr, "file name too long: %s\n", fname); + exit(1); + } + + memcpy(buf, c, len); + buf[len] = '\0'; +} + int main(int argc, char **argv) { size_t len, r, i; - int ch, did; + int did; FILE *f; uint8_t buf[BUFSIZ]; + char varname[PATH_MAX]; - while ((ch = getopt(argc, argv, "f:v:")) != -1) { - switch (ch) { - case 'f': - file = optarg; - break; - case 'v': - varname = optarg; - break; - default: - fprintf(stderr, "%s: wrong usage\n", - argv[0]); - return 1; - } - } - - if (file == NULL || varname == NULL) { - fprintf(stderr, "%s: wrong usage\n", argv[0]); + if (argc != 2) { + fprintf(stderr, "usage: %s file\n", *argv); return 1; } - if ((f = fopen(file, "r")) == NULL) { + setfname(argv[1], varname, sizeof(varname)); + + if ((f = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: can't open %s: %s", - argv[0], file, strerror(errno)); + argv[0], argv[1], strerror(errno)); return 1; }