From fe358325bd443a076732259bd2a50c97f12d7602 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Sat, 8 Mar 2025 17:47:47 -0500 Subject: support giving - argument to read from stdin --- main.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 8dc8a6e..17d0c7c 100644 --- a/main.c +++ b/main.c @@ -102,6 +102,8 @@ typedef enum { } LineMode; LineMode lm_chg(LineMode from, LineMode to, Str *out, Arena *a) { +#undef S +#define S(s) {s,sizeof(s)-1} static Str op[] = { [LINE_BLANK] = S(""), [LINE_PARA] = S("
"),
@@ -141,6 +143,8 @@ LineMode lm_chg(LineMode from, LineMode to, Str *out, Arena *a) {
[LINE_CODE] = S("\n"),
[LINE_BQUOT] = S("
\n"),
};
+#undef S
+#define S(s) (Str){s,sizeof(s)-1}
if (from == to) {
str_cat(out, cont[from], a);
} else {
@@ -317,15 +321,24 @@ int main(int argc, const char **argv) {
opts.hvarv[opts.hvarc++] = param;
break;
default:
- opts.from_stdin = 0;
- f = fopen(str_to_cstr(param, &scratch), "r/o");
- if (wdoc(f, &doc, &perm, &scratch)) {
- fwrite(param.s, 1, param.n, stderr);
- fprintf(stderr, ": %s\n", strerror(errno));
- return 1;
+ if (str_eql(param, S("-"))) {
+ if (wdoc(stdin, &doc, &perm, &scratch)) {
+ fprintf(stderr, "error reading document"
+ " from stdin: %s\n",
+ strerror(errno));
+ return 1;
+ }
+ } else {
+ opts.from_stdin = 0;
+ f = fopen(str_to_cstr(param, &scratch), "r/o");
+ if (wdoc(f, &doc, &perm, &scratch)) {
+ fwrite(param.s, 1, param.n, stderr);
+ fprintf(stderr, ": %s\n", strerror(errno));
+ return 1;
+ }
+ fclose(f);
+ break;
}
- fclose(f);
- break;
}
scratch = reset;
}
@@ -344,7 +357,12 @@ int main(int argc, const char **argv) {
}
if (opts.from_stdin) {
- wdoc(stdin, &doc, &perm, &scratch);
+ if (wdoc(stdin, &doc, &perm, &scratch)) {
+ fprintf(stderr, "error reading document from stdin: "
+ "%s\n", strerror(errno));
+ return 1;
+
+ }
}
if (doc && opts.standalone) {
--
cgit 1.4.1-2-gfad0