author | Olivier Brunel
<jjk@jjacky.com> 2023-05-25 15:14:37 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-07-05 07:56:22 UTC |
parent | b654c14df08f71b1203c99c3b656f02e2cd2bcec |
src/qmdoc/qmdoc.c | +38 | -37 |
diff --git a/src/qmdoc/qmdoc.c b/src/qmdoc/qmdoc.c index 8f0fd23..38863f1 100644 --- a/src/qmdoc/qmdoc.c +++ b/src/qmdoc/qmdoc.c @@ -9,11 +9,11 @@ #include <time.h> #include <getopt.h> #include <errno.h> -#include <skalibs/buffer.h> -#include <skalibs/djbunix.h> -#include <skalibs/unix-transactional.h> -#include <skalibs/uint32.h> +#include <limb/buffer.h> +#include <limb/djbunix.h> +#include <limb/exitcode.h> #include <limb/output.h> +#include <limb/u32.h> #include <limb/unix-transactional.h> #include "md4c.h" #include "qmdoc.h" @@ -518,8 +518,8 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_) case MD_BLOCK_OL: { MD_BLOCK_OL_DETAIL *d = details; - char buf[UINT32_FMT]; - buf[uint32_fmt(buf, d->start)] = 0; + char buf[U32_FMT]; + buf[u32_fmt(buf, d->start)] = 0; if (!raw_str(ctx, "<ol") || (d->start >= 0 && (!raw_str(ctx, " start=") @@ -665,12 +665,12 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) ; struct page *p = &ctx->pages[ctx->cur_page]; - char year[UINT32_FMT]; + char year[U32_FMT]; struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); struct tm tm; localtime_r(&ts.tv_sec, &tm); - year[uint32_fmt(year, (uint32) 1900 + tm.tm_year)] = '\0'; + year[u32_fmt(year, (u32) 1900 + tm.tm_year)] = '\0'; if ((ctx->doc.flags & DOC_HAS_TITLE) && !raw_str(ctx, "</section>")) return ERR_PARSER_LEAVE_BLOCK; if (p->nameoff) { @@ -771,8 +771,8 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) BUFFERING_OFF(s, l); - char buf[UINT32_FMT]; - buf[uint32_fmt(buf, (uint32) ctx->title.level)] = '\0'; + char buf[U32_FMT]; + buf[u32_fmt(buf, (u32) ctx->title.level)] = '\0'; if (!raw_str(ctx, "<section id=\"") || !anchor(ctx, s, l) @@ -836,8 +836,8 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) if (!e) break; l -= ++e - s; s = e; - char buf[UINT32_FMT]; - buf[uint32_fmt(buf, (uint32) n)] = '\0'; + char buf[U32_FMT]; + buf[u32_fmt(buf, (u32) n)] = '\0'; if (!raw_str(ctx, buf) || !raw_str(ctx, "\n")) return ERR_PARSER_BUFFERED; } @@ -1025,10 +1025,10 @@ leave_span(MD_SPANTYPE type, void *details, void *ctx_) if (section < 0) return ERR_PARSER_LEAVE_SPAN; - char buf[UINT32_FMT + 2]; + char buf[U32_FMT + 2]; int e; buf[0] = '('; - e = uint32_fmt(buf + 1, (uint32) section); + e = u32_fmt(buf + 1, (u32) section); buf[1 + e] = ')'; buf[2 + e] = '\0'; @@ -1257,8 +1257,8 @@ convert_page(struct ctx *ctx, int fddest) }; int r = md_parse(sce, p->size, &parser, ctx); if (r != 0) { - char buf[UINT32_FMT]; - buf[uint32_fmt(buf, (uint32) (r < 0) ? -r : r)] = '\0'; + char buf[U32_FMT]; + buf[u32_fmt(buf, (u32) (r < 0) ? -r : r)] = '\0'; retw(ERR_PARSER, (r < 0) ? "parser internal error " : "parser error ", buf); } @@ -1267,8 +1267,8 @@ convert_page(struct ctx *ctx, int fddest) ctx->doc.flags |= DOC_BUFFERING; for ( ; ctx->toc_lvl > 0; --ctx->toc_lvl) { if (!raw_str(ctx, "</ul>")) { - char buf[UINT32_FMT]; - buf[uint32_fmt(buf, (uint32) ERR_PARSER_TOC)] = '\0'; + char buf[U32_FMT]; + buf[u32_fmt(buf, (u32) ERR_PARSER_TOC)] = '\0'; retw(ERR_PARSER, "parser internal error ", buf); } } @@ -1322,14 +1322,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)) - retwusys(ERR_MEM, "load page title from '", file, "'"); + retwusys(EX_TEMPFAIL, "load page title from '", file, "'"); char buf_[256], 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) - retwu(ERR_IO, "load page title from '", file, "'"); + retwu(EX_IOERR, "load page title from '", file, "'"); page->titleoff = page->fileoff; @@ -1367,7 +1367,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)))) - retwusys(ERR_MEM, "load page title from '", file, "'"); + retwusys(EX_TEMPFAIL, "load page title from '", file, "'"); } if (e) { int l = e - b + 1; @@ -1383,7 +1383,7 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa) b = buf; left = buffer_get(&buffer, buf, sizeof(buf)); if (left <= 0) - retwusys(ERR_IO, "load page title from '", file, "'"); + retwusys(EX_IOERR, "load page title from '", file, "'"); if (!e) begin = 0; } } @@ -1403,7 +1403,7 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa) page->size = lseek(page->fd, 0, SEEK_END); if (page->size == (off_t) -1 || lseek(page->fd, done, SEEK_SET) < 0) - retwusys(ERR_MEM, "seek into '", file, "'"); + retwusys(EX_IOERR, "seek into '", file, "'"); page->size -= done; return 0; } @@ -1547,12 +1547,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)) - dief(-ERR_USAGE, "cannot use '", "--no-css", "' and '", "--inline-css", "' together"); + dief(EX_USAGE, "cannot use '", "--no-css", "' and '", "--inline-css", "' together"); if ((ctx.options & (OPT_INDEX | OPT_NO_INDEX)) == (OPT_INDEX | OPT_NO_INDEX)) - dief(-ERR_USAGE, "cannot use '", "--index", "' and '", "--no-index", "' together"); + dief(EX_USAGE, "cannot use '", "--index", "' and '", "--no-index", "' together"); int fddest = open(destdir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); - if (fddest < 0) diefusys(-ERR_IO, "open '", destdir, "'"); + if (fddest < 0) diefusys(EX_IOERR, "open '", destdir, "'"); if (!(ctx.options & OPT_NO_TOC)) css[CSS_NO_TOC].file = NULL; @@ -1572,19 +1572,19 @@ main (int argc, char *argv[]) if (strcmp(file + len - 3, ".md")) { warn("File '", file, "' not a markdown file (*.md)"); pages[i - optind].fd = -1; - err = ERR_MISC; + err = EX_DATA_ERR; continue; } int r = load_page_from_file(file, &pages[i - optind], &ctx.sa); - if (r < 0) err = r; + if (r) err = r; if (!(ctx.options & OPT_NO_INDEX) && !strcmp(ctx.sa.s + pages[i - optind].fileoff, "index.html")) idx_page = i - optind; } - if (err < 0) diefu(-err, "load pages"); + if (err) 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), @@ -1600,12 +1600,12 @@ main (int argc, char *argv[]) /* add our internal index */ pages[0].sce = "<internal index>"; pages[0].fileoff = ctx.sa.len; - if (!stralloc_catb(&ctx.sa, "index.html", strlen("index.html") + 1)) - diefusys(-ERR_MEM, "load page title from '", pages[0].sce, "'"); + if (!stralloc_cats0(&ctx.sa, "index.html")) + diefusys(EX_TEMPFAIL, "load page title from '", pages[0].sce, "'"); pages[0].titleoff = ctx.sa.len; - if (!stralloc_catb(&ctx.sa, index_title, strlen(index_title) + 1)) - diefusys(-ERR_MEM, "load page title from '", pages[0].sce, "'"); + if (!stralloc_cats0(&ctx.sa, index_title)) + diefusys(EX_TEMPFAIL, "load page title from '", pages[0].sce, "'"); /* fd == -1 means use index_md instead of reading from fd */ pages[0].fd = -1; @@ -1629,12 +1629,12 @@ main (int argc, char *argv[]) if (header) { ctx.doc.oheader = ctx.sa.len; if (!openreadfileclose(header, &ctx.sa, 0) || !stralloc_0(&ctx.sa)) - diefusys(-ERR_IO, "load data from '", header, "'"); + diefusys(EX_IOERR, "load data from '", header, "'"); } if (footer) { ctx.doc.ofooter = ctx.sa.len; if (!openreadfileclose(footer, &ctx.sa, 0) || !stralloc_0(&ctx.sa)) - diefusys(-ERR_IO, "load data from '", footer, "'"); + diefusys(EX_IOERR, "load data from '", footer, "'"); } } @@ -1655,7 +1655,8 @@ main (int argc, char *argv[]) css[i].offset = ctx.sa.len; if (!openreadfileclose(file, &ctx.sa, 0) || !stralloc_0(&ctx.sa)) - diefusys(-ERR_IO, "load CSS from '", file, "'"); + diefusys((errno == ENOMEM) ? EX_TEMPFAIL : EX_IOERR, + "load CSS from '", file, "'"); } else { int from, to; from = open_read(file); @@ -1666,7 +1667,7 @@ main (int argc, char *argv[]) to = open_exclat(fddest, css[i].file); if (to < 0) diefusys(-ERR_IO, "create '", destdir, "/", css[i].file, "'"); if (fd_cat(from, to) < 0) - diefusys(-ERR_IO, "copy CSS to '", destdir, "/", css[i].file, "'"); + diefusys(EX_IOERR, "copy CSS to '", destdir, "/", css[i].file, "'"); fd_close(from); fd_close(to); }