Welcome to little lamb

Code » qmdoc » commit b1cb6b2

Also abort if one file isn't a markdown (*.md)

author Olivier Brunel
2023-01-02 16:15:20 UTC
committer Olivier Brunel
2023-01-04 15:10:17 UTC
parent 35bc567e98b3a05c0150415cb256fda185d1f3d3

Also abort if one file isn't a markdown (*.md)

Instead of just announcing & skipping, we'll also abort when an input
file isn't a markdown file.
This will make code easier/simpler later on, since all items in pages
will be pages to process, no exceptions.

main.c +15 -13

diff --git a/main.c b/main.c
index dd0cf8e..e45dce8 100644
--- a/main.c
+++ b/main.c
@@ -994,11 +994,11 @@ convert_page(struct ctx *ctx, int fddest)
     return 0;
 }
 
-static void
+static int
 load_page_from_file(const char *file, struct page *page, stralloc *sa)
 {
     page->fd = open_read(file);
-    if (page->fd < 0) strerr_diefu3sys(-ERR_IO, "open '", file, "'");
+    if (page->fd < 0) ret_strerr_warnwu3sys(ERR_IO, "open '", file, "'");
 
     const char *s = strrchr(file, '/');
     if (!s) s = file;
@@ -1008,14 +1008,14 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa)
     page->fileoff = sa->len;
     if (!stralloc_catb(sa, s, l - 2)
             || !stralloc_catb(sa, "html", 5))
-        strerr_diefu3sys(-ERR_MEM, "load page title from '", file, "'");
+        ret_strerr_warnwu3sys(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_));
 
     ssize_t left = buffer_get(&buffer, buf, sizeof(buf));
     if (left <= 0)
-        strerr_diefu3sys(-ERR_IO, "load page title from '", file, "'");
+        ret_strerr_warnwu3sys(ERR_IO, "load page title from '", file, "'");
 
     page->titleoff = page->fileoff;
 
@@ -1034,7 +1034,7 @@ 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))))
-                    strerr_diefu3sys(-ERR_MEM, "load page title from '", file, "'");
+                    ret_strerr_warnwu3sys(ERR_MEM, "load page title from '", file, "'");
                 break;
             case 2:
             case 3:
@@ -1061,15 +1061,16 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa)
             b = buf;
             left = buffer_get(&buffer, buf, sizeof(buf));
             if (left <= 0)
-                strerr_diefu3sys(-ERR_IO, "load page title from '", file, "'");
+                ret_strerr_warnwu3sys(-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)
-        strerr_diefu3sys(-ERR_MEM, "seek into '", file, "'");
+        ret_strerr_warnwu3sys(-ERR_MEM, "seek into '", file, "'");
     page->size -= done;
+    return 0;
 }
 
 static void
@@ -1185,22 +1186,25 @@ main (int argc, char *argv[])
     ctx.pages = pages;
     ctx.nb_pages = sizeof(pages) / sizeof(*pages);
 
+    int err = 0;
     outse("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")) {
-            outs("Skipping '");
-            outs(file);
-            outse("': File must be a markdown file (*.md)");
+            strerr_warnw3x("File '", file, "' not a markdown file (*.md)");
             pages[i - optind].fd = -1;
+            err = ERR_MISC;
             continue;
         }
 
-        load_page_from_file(file, &pages[i - optind], &ctx.sa);
+        int r = load_page_from_file(file, &pages[i - optind], &ctx.sa);
+        if (r < 0) err = r;
     }
 
+    if (err < 0) strerr_diefu1x(-err, "load pages");
+
     if (header || footer) {
         outse("Loading files...");
 
@@ -1244,8 +1248,6 @@ main (int argc, char *argv[])
     }
 
     for (int i = optind; i < argc; ++i) {
-        if (pages[i - optind].fd < 0) continue;
-
         outs("Converting ");
         outs(argv[i]);
         outse("...");