retro-unu: add -t support to command line

FossilOrigin-Name: 29a9b00f47a3a1a3924138b8b9191492c5028d140a72fa2aa686ca001d0cd098
This commit is contained in:
crc 2020-01-21 12:58:06 +00:00
parent 853363e8a0
commit 8ead0fbfba
3 changed files with 31 additions and 7 deletions

View file

@ -3,3 +3,7 @@
- remove deprecated words
as{ }as " prefix:"
- examplse
markdown.retro

View file

@ -1,4 +1,4 @@
.Dd January 2019
.Dd January 2020
.Dt RETRO-UNU 1
.Os
.Sh RETRO-UNU
@ -6,6 +6,7 @@
.Nd "a modern, pragmatic forth development system"
.Sh SYNOPSIS
.Nm
.Op Fl t
filename
.Sh DESCRIPTION
RETRO is a modern, pragmatic Forth drawing influences from many
@ -15,5 +16,12 @@ to various uses.
.Nm
is a tool for extracting code from fenced blocks in literate
sources. It will write output to stdout.
.Sh OPTIONS
.Bl -tag -width -indent
.It Fl t
Include any test blocks in the file.
.It filename
Extract code blocks from the specified file.
.El
.Sh AUTHORS
.An Charles Childers Aq Mt crc@forthworks.com

View file

@ -2,7 +2,9 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define KiB * 1024
void read_line(FILE *file, char *line_buffer) {
int ch, count;
if (file == NULL || line_buffer == NULL)
@ -19,16 +21,19 @@ void read_line(FILE *file, char *line_buffer) {
}
line_buffer[count] = '\0';
}
char source[16 KiB];
int fenced(char *s)
int fenced(char *s, int include_tests)
{
int a = strcmp(s, "```");
int b = strcmp(s, "~~~");
if (a == 0) return 1;
if (a == 0 && include_tests == 1) return 1;
if (b == 0) return 1;
return 0;
}
void extract(char *fname) {
void extract(char *fname, int include_tests) {
char *buffer = (char *)source;
char fence[4];
FILE *fp;
@ -41,7 +46,7 @@ void extract(char *fname) {
read_line(fp, buffer);
strncpy(fence, buffer, 3);
fence[3] = '\0';
if (fenced(fence)) {
if (fenced(fence, include_tests)) {
if (inBlock == 0)
inBlock = 1;
else
@ -53,14 +58,21 @@ void extract(char *fname) {
}
fclose(fp);
}
int main(int argc, char **argv) {
int i = 1;
int tests = 0;
if (argc > 1) {
while (i < argc) {
extract(argv[i++]);
if (strcmp(argv[i], "-t") == 0) {
tests = 1;
i++;
}
else
extract(argv[i++], tests);
}
}
else
printf("unu\n(c) 2013-2019 charles childers\n\nTry:\n %s filename\n", argv[0]);
printf("unu\n(c) 2013-2020 charles childers\n\nTry:\n %s filename\n", argv[0]);
return 0;
}