Welcome to little lamb

Code » qmdoc » commit c2ad072

Refactor to latest limb/skalibs

author Olivier Brunel
2023-01-06 09:00:05 UTC
committer Olivier Brunel
2023-01-06 09:00:05 UTC
parent 6e1e02ba629c85cb2069642f241ca414e1b36c4d

Refactor to latest limb/skalibs

Makefile +16 -4
main.c +61 -71
md4c.c +1 -1
md4c.h +1 -1

diff --git a/Makefile b/Makefile
index be08377..891e3eb 100644
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,24 @@
+-include config.mk
+
 SRCS = main.c md4c.c
 OBJS = $(SRCS:.c=.o)
 DEPS = $(SRCS:.c=.d)
 
 CSS = struct.css common.css light.css dark.css
 
-#CFLAGS = -g -O3 -Wall -I./limb/include
-LDFLAGS = -L./limb -llimb -L/lib/skalibs -lskarnet
-all: qmdoc qmdoc.css
+BUILD = qmdoc qmdoc.css
+
+
+ifeq ($(DEBUG),1)
+	CFLAGS = -g -Og
+else
+	CFLAGS = -O3
+endif
+CFLAGS += -Wall -isystem ../limb/include -isystem ../skalibs/src/include
+LDFLAGS = -L../limb -llimb -L../skalibs -lskarnet
+
+
+all: $(BUILD)
 
 qmdoc: $(OBJS)
 	$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $@
@@ -20,6 +32,6 @@ qmdoc.css: $(CSS)
 	$(CC) $(CFLAGS) -MMD -MP -o $@ -c $<
 
 clean:
-	rm -f qmdoc $(OBJS) $(DEPS)
+	rm -f $(BUILD) $(OBJS) $(DEPS)
 
 $(OBJS):
diff --git a/main.c b/main.c
index abf1355..f7051ce 100644
--- a/main.c
+++ b/main.c
@@ -9,13 +9,12 @@
 #include <time.h>
 #include <getopt.h>
 #include <errno.h>
-#include <skalibs/strerr2.h>
 #include <skalibs/buffer.h>
 #include <skalibs/djbunix.h>
+#include <skalibs/unix-transactional.h>
 #include <skalibs/uint32.h>
 #include <limb/output.h>
-#include <limb/djbunix2.h>
-#include <err.h>
+#include <limb/unix-transactional.h>
 #include "qmdoc.h"
 #include "md4c.h"
 
@@ -997,7 +996,7 @@ load_source(struct ctx *ctx, size_t *salen)
         return ERR_MEM;
 
     if (allread(p->fd, ctx->sa.s + *salen, p->size) != p->size)
-        ret_strerr_warnwu1sys(ERR_IO, "read source file");
+        retwusys(ERR_IO, "read source file");
     ctx->sa.len += p->size;
 
     /* ending on a new line allows parser optimization */
@@ -1016,10 +1015,10 @@ convert_page(struct ctx *ctx, int fddest)
     const char *dst = ctx->sa.s + p->fileoff;
     int fd;
     if (ctx->options & OPT_OVERWRITE)
-        fd = openat_trunc(fddest, dst);
+        fd = open_truncat(fddest, dst);
     else
-        fd = openat_excl(fddest, dst);
-    if (fd < 0) ret_strerr_warnwu1sys(ERR_IO, "create destination");
+        fd = open_exclat(fddest, dst);
+    if (fd < 0) retwusys(ERR_IO, "create destination");
 
     size_t salen;
     const char *sce;
@@ -1051,8 +1050,7 @@ convert_page(struct ctx *ctx, int fddest)
     if (r != 0) {
         char buf[UINT32_FMT];
         buf[uint32_fmt(buf, (uint32) (r < 0) ? -r : r)] = '\0';
-        ret_strerr_warnw2x(ERR_PARSER, (r < 0) ? "parser internal error " : "parser error ",
-                           buf);
+        retw(ERR_PARSER, (r < 0) ? "parser internal error " : "parser error ", buf);
     }
 
     if (!(ctx->options & OPT_NO_TOC)) {
@@ -1062,7 +1060,7 @@ convert_page(struct ctx *ctx, int fddest)
             if (!raw_str(ctx, "</ul>")) {
                 char buf[UINT32_FMT];
                 buf[uint32_fmt(buf, (uint32) ERR_PARSER_TOC)] = '\0';
-                ret_strerr_warnwu2x(ERR_PARSER, "parser internal error ", buf);
+                retw(ERR_PARSER, "parser internal error ", buf);
             }
         }
         ctx->doc.flags &= ~DOC_BUFFERING;
@@ -1078,7 +1076,7 @@ convert_page(struct ctx *ctx, int fddest)
             || allwrite(fd, ctx->sa_out.s + ctx->otoc, ctx->sa_out.len - ctx->otoc)
             != ctx->sa_out.len - ctx->otoc
        )
-        ret_strerr_warnwu1sys(ERR_IO, "write destination");
+        retwusys(ERR_IO, "write destination");
     fd_close(fd);
 
     /* reset sa_out position */
@@ -1097,7 +1095,7 @@ static int
 load_page_from_file(const char *file, struct page *page, stralloc *sa)
 {
     page->fd = open_read(file);
-    if (page->fd < 0) ret_strerr_warnwu3sys(ERR_IO, "open '", file, "'");
+    if (page->fd < 0) retwusys(ERR_IO, "open '", file, "'");
 
     page->sce = strrchr(file, '/');
     if (!page->sce) page->sce = file;
@@ -1107,14 +1105,14 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa)
     page->fileoff = sa->len;
     if (!stralloc_catb(sa, page->sce, l - 2)
             || !stralloc_catb(sa, "html", 5))
-        ret_strerr_warnwu3sys(ERR_MEM, "load page title from '", file, "'");
+        retwusys(ERR_MEM, "load page title from '", file, "'");
 
     char buf_[256], buf[sizeof(buf_)];
-    buffer_t buffer = BUFFER_INIT(&fd_readv, page->fd, buf_, sizeof(buf_));
+    buffer buffer = BUFFER_INIT(&fd_readv, page->fd, buf_, sizeof(buf_));
 
     ssize_t left = buffer_get(&buffer, buf, sizeof(buf));
     if (left <= 0)
-        ret_strerr_warnwu3sys(ERR_IO, "load page title from '", file, "'");
+        retwu(ERR_IO, "load page title from '", file, "'");
 
     page->titleoff = page->fileoff;
 
@@ -1133,16 +1131,14 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa)
                         && (!stralloc_catb(sa, b + ((is_hdr) ? 2 : 0),
                                            ((e) ? e - b : left) - ((is_hdr) ? 2 : 0))
                             || (e && !stralloc_0(sa))))
-                    ret_strerr_warnwu3sys(ERR_MEM, "load page title from '", file, "'");
+                    retwusys(ERR_MEM, "load page title from '", file, "'");
                 break;
             case 2:
             case 3:
                 break;
             default:
                 if (begin && is_hdr && !warned) {
-                    errs("warning: header too long in '");
-                    errs(file);
-                    errse("': Only 3 lines supported");
+                    err("warning: header too long in '", file, "': Only 4 lines supported");
                     warned = 1;
                 }
         }
@@ -1160,14 +1156,14 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa)
             b = buf;
             left = buffer_get(&buffer, buf, sizeof(buf));
             if (left <= 0)
-                ret_strerr_warnwu3sys(-ERR_IO, "load page title from '", file, "'");
+                retwusys(ERR_IO, "load page title from '", file, "'");
             if (!e) begin = 0;
         }
     }
 
     page->size = lseek(page->fd, 0, SEEK_END);
     if (page->size == (off_t) -1 || lseek(page->fd, done, SEEK_SET) < 0)
-        ret_strerr_warnwu3sys(-ERR_MEM, "seek into '", file, "'");
+        retwusys(ERR_MEM, "seek into '", file, "'");
     page->size -= done;
     return 0;
 }
@@ -1175,29 +1171,30 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa)
 static void
 help(void)
 {
-    outh(" -a, --author AUTHOR          Set AUTHOR as author (in meta in footer)\n"
-         " -b, --buttons                Put Previous & Next buttons on pages\n"
-         " -C, --no-css                 Do not use CSS (still process --css if any)\n"
-         " -c, --css FILE               Add FILE as additional CSS\n"
-         " -d, --destdir DIR            Write files into DIR\n"
-         " -F, --footer FILE            Insert FILE as common footer\n"
-         " -H, --header FILE            Insert FILE as common header\n"
-         " -h, --help                   Show this help screen and exit\n"
-         " -I, --inline-css             Use inline CSS instead of external files\n"
-         " -i, --index                  Force index mode\n"
-         " -l, --lang LNG               Set LNG as language attribute\n"
-         " -o, --overwrite              Overwrite destination files if already exist\n"
-         " -s, --subtitle TEXT          Set TEXT as general subtitle\n"
-         " -T, --no-toc                 Don't write a TOC on each page. Implies --no-index\n"
-         " -t, --title TITLE            Set TITLE as general (across all pages) title\n"
-         " -X, --no-index               Disable index mode\n"
-        );
+    diehelp(0, "[OPTION..] FILE...",
+            " -a, --author AUTHOR          Set AUTHOR as author (in meta in footer)\n"
+            " -b, --buttons                Put Previous & Next buttons on pages\n"
+            " -C, --no-css                 Do not use CSS (still process --css if any)\n"
+            " -c, --css FILE               Add FILE as additional CSS\n"
+            " -d, --destdir DIR            Write files into DIR\n"
+            " -F, --footer FILE            Insert FILE as common footer\n"
+            " -H, --header FILE            Insert FILE as common header\n"
+            " -h, --help                   Show this help screen and exit\n"
+            " -I, --inline-css             Use inline CSS instead of external files\n"
+            " -i, --index                  Force index mode\n"
+            " -l, --lang LNG               Set LNG as language attribute\n"
+            " -o, --overwrite              Overwrite destination files if already exist\n"
+            " -s, --subtitle TEXT          Set TEXT as general subtitle\n"
+            " -T, --no-toc                 Don't write a TOC on each page. Implies --no-index\n"
+            " -t, --title TITLE            Set TITLE as general (across all pages) title\n"
+            " -X, --no-index               Disable index mode\n"
+           );
 }
 
 static void
 usage(int err)
 {
-    strerr_dieusage((err) ? -ERR_USAGE : -ERR_NONE, "[OPTION..] FILE...");
+    dieusage((err) ? -ERR_USAGE : -ERR_NONE, "[OPTION..] FILE...");
 }
 
 int
@@ -1295,12 +1292,12 @@ main (int argc, char *argv[])
     }
     if (optind == argc) usage(1);
     if ((ctx.options & (OPT_NO_CSS | OPT_INLINE_CSS)) == (OPT_NO_CSS | OPT_INLINE_CSS))
-        strerr_dief5x(-ERR_USAGE, "cannot use '", "--no-css", "' and '", "--inline-css", "' together");
+        dief(-ERR_USAGE, "cannot use '", "--no-css", "' and '", "--inline-css", "' together");
     if ((ctx.options & (OPT_INDEX | OPT_NO_INDEX)) == (OPT_INDEX | OPT_NO_INDEX))
-        strerr_dief5x(-ERR_USAGE, "cannot use '", "--index", "' and '", "--no-index", "' together");
+        dief(-ERR_USAGE, "cannot use '", "--index", "' and '", "--no-index", "' together");
 
     int fddest = open(destdir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
-    if (fddest < 0) strerr_diefu3sys(-ERR_IO, "open '", destdir, "'");
+    if (fddest < 0) diefusys(-ERR_IO, "open '", destdir, "'");
 
     if (!(ctx.options & OPT_NO_TOC)) css[CSS_NO_TOC].file = NULL;
 
@@ -1311,13 +1308,13 @@ main (int argc, char *argv[])
 
     int err = 0;
     int idx_page = -1;
-    outse("Scanning pages...");
+    out("Scanning pages...");
     for (int i = optind; i < argc; ++i) {
         const char *file = argv[i];
         size_t len = strlen(file);
 
         if (strcmp(file + len - 3, ".md")) {
-            strerr_warnw3x("File '", file, "' not a markdown file (*.md)");
+            warn("File '", file, "' not a markdown file (*.md)");
             pages[i - optind].fd = -1;
             err = ERR_MISC;
             continue;
@@ -1331,7 +1328,7 @@ main (int argc, char *argv[])
             idx_page = i - optind;
     }
 
-    if (err < 0) strerr_diefu1x(-err, "load pages");
+    if (err < 0) diefu(-err, "load pages");
 
     /* enable FULL TOC unless disabled (OPT_NO_INDEX) if:
      * - index was given as first page, or none given (add our internal tpl),
@@ -1348,11 +1345,11 @@ main (int argc, char *argv[])
             pages[0].sce = "<internal index>";
             pages[0].fileoff = ctx.sa.len;
             if (!stralloc_catb(&ctx.sa, "index.html", strlen("index.html") + 1))
-                strerr_diefu3sys(-ERR_MEM, "load page title from '", pages[0].sce, "'");
+                diefusys(-ERR_MEM, "load page title from '", pages[0].sce, "'");
 
             pages[0].titleoff = ctx.sa.len;
             if (!stralloc_catb(&ctx.sa, index_title, strlen(index_title) + 1))
-                strerr_diefu3sys(-ERR_MEM, "load page title from '", pages[0].sce, "'");
+                diefusys(-ERR_MEM, "load page title from '", pages[0].sce, "'");
 
             /* fd == -1 means use index_md instead of reading from fd */
             pages[0].fd = -1;
@@ -1370,41 +1367,40 @@ main (int argc, char *argv[])
     }
 
     if (header || footer) {
-        outse("Loading files...");
+        out("Loading files...");
 
         if (header) {
             ctx.doc.oheader = ctx.sa.len;
             if (!openreadfileclose(header, &ctx.sa, 0) || !stralloc_0(&ctx.sa))
-                strerr_diefu3sys(-ERR_IO, "load data from '", header, "'");
+                diefusys(-ERR_IO, "load data from '", header, "'");
         }
         if (footer) {
             ctx.doc.ofooter = ctx.sa.len;
             if (!openreadfileclose(footer, &ctx.sa, 0) || !stralloc_0(&ctx.sa))
-                strerr_diefu3sys(-ERR_IO, "load data from '", footer, "'");
+                diefusys(-ERR_IO, "load data from '", footer, "'");
         }
     }
 
     if (!(ctx.options & OPT_NO_CSS) || ctx.css[CSS_CUSTOM].file) {
-        outs((ctx.options & OPT_INLINE_CSS) ? "Loading" : "Copying");
-        outse(" CSS files...");
+        out((ctx.options & OPT_INLINE_CSS) ? "Loading" : "Copying", " CSS files...");
         for (int i = 0; i < NB_CSS; ++i) {
             if (!css[i].file) continue;
             if (ctx.options & OPT_INLINE_CSS) {
                 css[i].offset = ctx.sa.len;
                 if (!openreadfileclose(css[i].file, &ctx.sa, 0)
                         || !stralloc_0(&ctx.sa))
-                    strerr_diefu3sys(-ERR_IO, "load CSS from '", css[i].file, "'");
+                    diefusys(-ERR_IO, "load CSS from '", css[i].file, "'");
             } else {
                 int from, to;
                 from = open_read(css[i].file);
-                if (from < 0) strerr_diefu3sys(-ERR_IO, "open '", css[i].file, "'");
+                if (from < 0) diefusys(-ERR_IO, "open '", css[i].file, "'");
                 if (ctx.options & OPT_OVERWRITE)
-                    to = openat_trunc(fddest, css[i].file);
+                    to = open_truncat(fddest, css[i].file);
                 else
-                    to = openat_excl(fddest, css[i].file);
-                if (to < 0) strerr_diefu5sys(-ERR_IO, "create '", destdir, "/", css[i].file, "'");
+                    to = open_exclat(fddest, css[i].file);
+                if (to < 0) diefusys(-ERR_IO, "create '", destdir, "/", css[i].file, "'");
                 if (fd_cat(from, to) < 0)
-                    strerr_diefu5sys(-ERR_IO, "copy CSS to '", destdir, "/", css[i].file, "'");
+                    diefusys(-ERR_IO, "copy CSS to '", destdir, "/", css[i].file, "'");
                 fd_close(from);
                 fd_close(to);
             }
@@ -1415,34 +1411,28 @@ main (int argc, char *argv[])
         if ((ctx.doc.flags & DOC_FULL_TOC) && ctx.cur_page == 0)
             continue;
 
-        outs("Converting ");
-        outs(pages[ctx.cur_page].sce);
-        outse("...");
+        out("Converting ", pages[ctx.cur_page].sce, "...");
 
         int r = convert_page(&ctx, fddest);
-        if (r < 0) strerr_diefu7x(-r, "convert '", pages[ctx.cur_page].sce,
-                                  "' to '", destdir, "/",
-                                  ctx.sa.s + pages[ctx.cur_page].fileoff, "'");
+        if (r < 0) diefu(-r, "convert '", pages[ctx.cur_page].sce, "' to '",
+                         destdir, "/", ctx.sa.s + pages[ctx.cur_page].fileoff, "'");
     }
 
     if (ctx.doc.flags & DOC_FULL_TOC) {
         ctx.cur_page = 0;
 
-        outs("Converting ");
-        outs(pages[ctx.cur_page].sce);
-        outse("...");
+        out("Converting ", pages[ctx.cur_page].sce, "...");
 
         ctx.doc.flags |= DOC_IS_INDEX;
         int r = convert_page(&ctx, fddest);
-        if (r < 0) strerr_diefu7x(-r, "convert '", pages[ctx.cur_page].sce,
-                                  "' to '", destdir, "/",
-                                  ctx.sa.s + pages[ctx.cur_page].fileoff, "'");
+        if (r < 0) diefu(-r, "convert '", pages[ctx.cur_page].sce, "' to '",
+                         destdir, "/", ctx.sa.s + pages[ctx.cur_page].fileoff, "'");
     }
 
     stralloc_free(&ctx.buf.sa);
     stralloc_free(&ctx.sa_out);
     stralloc_free(&ctx.sa);
 
-    outse("done.");
+    out("done.");
     return 0;
 }
diff --git a/md4c.c b/md4c.c
index 586ad87..4a2f861 120000
--- a/md4c.c
+++ b/md4c.c
@@ -1 +1 @@
-md4c/src/md4c.c
\ No newline at end of file
+../md4c/src/md4c.c
\ No newline at end of file
diff --git a/md4c.h b/md4c.h
index 151b21c..9a4f178 120000
--- a/md4c.h
+++ b/md4c.h
@@ -1 +1 @@
-md4c/src/md4c.h
\ No newline at end of file
+../md4c/src/md4c.h
\ No newline at end of file