commit dc715a83638a2c710c99718d9ae7a95478913395
parent 044e25311d769ca36be0d82b1f942d487023aa01
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Tue, 11 May 2021 02:01:52 +0200
add pledge support, remove file argument option
- fix possible fclose(stdin).
- add usage() function and print it also on passing "-h".
Diffstat:
2 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/smu.1 b/smu.1
@@ -6,7 +6,6 @@ smu \- simple markup
.RB [ \-h ]
.RB [ \-v ]
.RB [ \-n ]
-.RB [ file ]
.SH DESCRIPTION
smu is a simple interpreter for a simplified markdown dialect.
.SH OPTIONS
diff --git a/smu.c b/smu.c
@@ -4,6 +4,12 @@
#include <stdlib.h>
#include <string.h>
+#ifdef __OpenBSD__
+#include <unistd.h>
+#else
+#define pledge(p1,p2) 0
+#endif
+
#define LENGTH(x) sizeof(x)/sizeof(x[0])
#define ADDC(b,i) if(i % BUFSIZ == 0) { b = realloc(b, (i + BUFSIZ) * sizeof(char)); if(!b) eprint("malloc"); } b[i]
@@ -655,6 +661,11 @@ process(const char *begin, const char *end, int newblock) {
}
}
+void
+usage(char **argv) {
+ eprint("usage: %s [-n]\n\t-n escape HTML strictly\n", argv[0]);
+}
+
int
main(int argc, char *argv[]) {
char *buffer = NULL;
@@ -662,22 +673,23 @@ main(int argc, char *argv[]) {
unsigned long len, bsize;
FILE *source = stdin;
+ if (pledge("stdio", NULL) == -1)
+ eprint("pledge");
+
for(i = 1; i < argc; i++) {
if(!strcmp("-v", argv[i]))
- eprint("simple markup %s (C) Enno Boland\n",VERSION);
+ eprint("simple markup %s\n",VERSION);
else if(!strcmp("-n", argv[i]))
nohtml = 1;
- else if(argv[i][0] != '-')
- break;
+ else if(argv[i][0] != '-' || !strcmp("-h", argv[i]))
+ usage(argv);
else if(!strcmp("--", argv[i])) {
i++;
break;
}
else
- eprint("Usage %s [-n] [file]\n -n escape html strictly\n", argv[0]);
+ usage(argv);
}
- if(i < argc && !(source = fopen(argv[i], "r")))
- eprint("Cannot open file `%s`\n",argv[i]);
bsize = 2 * BUFSIZ;
buffer = ereallocz(buffer, bsize);
len = 0;
@@ -691,7 +703,6 @@ main(int argc, char *argv[]) {
}
buffer[len] = '\0';
process(buffer, buffer + len, 1);
- fclose(source);
free(buffer);
return EXIT_SUCCESS;
}