add reading from file feature back, improve man page - smu - smu - simple markup (Markdown) processor (fork, fixes + features) (HTM) git clone git://git.codemadness.org/smu (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 9c68585ed395bc9db7339433746ec0dd8298cabc (DIR) parent 5573f301eb9eb97b251ff499ddca138417dcf43c (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org> Date: Thu, 13 May 2021 14:39:48 +0200 add reading from file feature back, improve man page Diffstat: M smu.1 | 16 ++++++++++------ M smu.c | 31 ++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 15 deletions(-) --- (DIR) diff --git a/smu.1 b/smu.1 @@ -1,4 +1,4 @@ -.Dd May 11, 2021 +.Dd May 13, 2021 .Dt SMU 1 .Os .Sh NAME @@ -10,19 +10,23 @@ .Op Fl l .Op Fl n .Op Fl v +.Op Ar file .Sh DESCRIPTION .Nm -is a simple interpreter for a simplified markdown dialect. +is a simple interpreter for a simplified Markdown dialect. +When a +.Ar file +is specified it will read from the file, otherwise it reads from stdin. The options are as follows: .Bl -tag -width Ds .It Fl h -prints usage information to stderr, then exits. +Prints usage information to stderr, then exits. .It Fl l -set loading="lazy" hint attribute for images if it has a width and a height. +Set loading="lazy" hint attribute for images if it has a width and a height. .It Fl n -escapes all HTML tags. +Escapes all HTML tags. .It Fl v -prints version information to stderr, then exits. +Prints version information to stderr, then exits. .El .Sh EXIT STATUS .Ex -std (DIR) diff --git a/smu.c b/smu.c @@ -1,4 +1,5 @@ #include <ctype.h> +#include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -702,33 +703,43 @@ process(const char *begin, const char *end, int newblock) void usage(char **argv) { - eprint("usage: %s [-l] [-n]\n", argv[0]); + eprint("usage: %s [-l] [-n] [file]\n", argv[0]); } int main(int argc, char *argv[]) { + FILE *source = stdin; char *buffer = NULL; int s, i; unsigned long len, bsize; - if (pledge("stdio", NULL) == -1) - eprint("pledge"); - for (i = 1; i < argc; i++) { - if (!strcmp("-v", argv[i])) + if (!strcmp("-v", argv[i])) { eprint("smu v%s\n", VERSION); - else if (!strcmp("-n", argv[i])) + } else if (!strcmp("-n", argv[i])) { nohtml = 1; - else if (!strcmp("-l", argv[i])) + } else if (!strcmp("-l", argv[i])) { lazyimg = 1; - else + } else if (argv[i][0] != '-') { + break; /* file specified */ + } else if (!strcmp("--", argv[i])) { + i++; + break; + } else { usage(argv); + } } + if (i < argc && !(source = fopen(argv[i], "r"))) + eprint("fopen: %s: %s\n", argv[i], strerror(errno)); + + if (pledge("stdio", NULL) == -1) + eprint("pledge"); + bsize = 2 * BUFSIZ; buffer = ereallocz(buffer, bsize); len = 0; - while ((s = fread(buffer + len, 1, BUFSIZ, stdin))) { + while ((s = fread(buffer + len, 1, BUFSIZ, source))) { len += s; if (BUFSIZ + len + 1 > bsize) { bsize += BUFSIZ; @@ -739,6 +750,8 @@ main(int argc, char *argv[]) buffer[len] = '\0'; process(buffer, buffer + len, 1); free(buffer); + if (source != stdin) + fclose(source); return 0; }