Welcome to little lamb

Code » qmdoc » commit c36aab6

Minor refactoring

author Olivier Brunel
2023-01-02 09:52:47 UTC
committer Olivier Brunel
2023-01-04 15:10:13 UTC
parent aff76a79d7f6090594795ce52536c976ffc28b69

Minor refactoring

main.c +23 -27

diff --git a/main.c b/main.c
index 5cbfa48..ae787c9 100644
--- a/main.c
+++ b/main.c
@@ -1075,11 +1075,17 @@ main (int argc, char *argv[])
     if (PROG) ++PROG;
     else PROG = argv[0];
 
-    int options = 0;
     char *destdir = ".";
-    const char *title = "Documentation";
-    const char *author = "";
-    const char *lang = "en";
+    struct ctx ctx = {
+        .options = 0,
+        .sa = STRALLOC_ZERO,
+        .sa_out = STRALLOC_ZERO,
+        .css = css,
+        .doc.title = "Documentation",
+        .doc.author = "",
+        .doc.lang = "en",
+        .buf.sa = STRALLOC_ZERO,
+    };
 
     int c;
     struct option opts[] = {
@@ -1095,10 +1101,10 @@ main (int argc, char *argv[])
     };
     while ((c = getopt_long(argc, argv, "a:Cd:hIl:ot:", opts, NULL)) != -1) switch (c) {
         case 'a':
-            author = optarg;
+            ctx.doc.author = optarg;
             break;
         case 'C':
-            options |= OPT_NO_CSS;
+            ctx.options |= OPT_NO_CSS;
             break;
         case 'd':
             destdir = optarg;
@@ -1106,40 +1112,30 @@ main (int argc, char *argv[])
         case 'h':
             help();
         case 'I':
-            options |= OPT_INLINE_CSS;
+            ctx.options |= OPT_INLINE_CSS;
             break;
         case 'l':
-            lang = optarg;
+            ctx.doc.lang = optarg;
             break;
         case 'o':
-            options |= OPT_OVERWRITE;
+            ctx.options |= OPT_OVERWRITE;
             break;
         case 't':
-            title = optarg;
+            ctx.doc.title = optarg;
             break;
         case '?':
             usage(1);
     }
     if (optind == argc) usage(1);
-    if ((options & (OPT_NO_CSS | OPT_INLINE_CSS)) == (OPT_NO_CSS | OPT_INLINE_CSS))
+    if ((ctx.options & (OPT_NO_CSS | OPT_INLINE_CSS)) == (OPT_NO_CSS | OPT_INLINE_CSS))
         strerr_dief1x(-ERR_USAGE, "cannot use '--no-css' and '--inline-css' together");
 
     int fddest = open(destdir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
     if (fddest < 0) strerr_diefu3sys(-ERR_IO, "open '", destdir, "'");
 
     struct page pages[argc - optind];
-    struct ctx ctx = {
-        .options = options,
-        .sa = STRALLOC_ZERO,
-        .sa_out = STRALLOC_ZERO,
-        .css = css,
-        .pages = pages,
-        .nb_pages = sizeof(pages) / sizeof(*pages),
-        .doc.title = title,
-        .doc.author = author,
-        .doc.lang = lang,
-        .buf.sa = STRALLOC_ZERO,
-    };
+    ctx.pages = pages;
+    ctx.nb_pages = sizeof(pages) / sizeof(*pages);
 
     outse("Scanning files...");
     for (int i = optind; i < argc; ++i) {
@@ -1157,11 +1153,11 @@ main (int argc, char *argv[])
         load_page_from_file(file, &pages[i - optind], &ctx.sa);
     }
 
-    if (!(options & OPT_NO_CSS)) {
-        outs((options & OPT_INLINE_CSS) ? "Loading" : "Copying");
+    if (!(ctx.options & OPT_NO_CSS)) {
+        outs((ctx.options & OPT_INLINE_CSS) ? "Loading" : "Copying");
         outse(" CSS files...");
         for (int i = 0; i < NB_CSS; ++i) {
-            if (options & OPT_INLINE_CSS) {
+            if (ctx.options & OPT_INLINE_CSS) {
                 css[i].offset = ctx.sa.len;
                 if (!openreadfileclose(css[i].file, &ctx.sa, 0)
                         || !stralloc_0(&ctx.sa))
@@ -1170,7 +1166,7 @@ main (int argc, char *argv[])
                 int from, to;
                 from = open_read(css[i].file);
                 if (from < 0) strerr_diefu3sys(-ERR_IO, "open '", css[i].file, "'");
-                if (options & OPT_OVERWRITE)
+                if (ctx.options & OPT_OVERWRITE)
                     to = openat_trunc(fddest, css[i].file);
                 else
                     to = openat_excl(fddest, css[i].file);