commit a9f50b264cd5435d834fdc1db066019596b1e00a Author: roswold Date: Thu May 20 11:49:48 2021 -0500 initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..977993b --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: rcms +test: + ./rcms test/index.txt +rcms: + make --no-print-directory -C src + @ln -s src/rcms rcms 2>/dev/null +clean: + make --no-print-directory clean -C src + rm rcms +.PHONY: all clean test rcms diff --git a/README b/README new file mode 100644 index 0000000..bc9de06 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +Tool to generate static website from specification documents +(text files). + +See test directory for examples. diff --git a/p/index.html b/p/index.html new file mode 100644 index 0000000..e69de29 diff --git a/s.sh b/s.sh new file mode 100755 index 0000000..923c4f4 --- /dev/null +++ b/s.sh @@ -0,0 +1,3 @@ +#!/usr/bin/sh + +./rcms test/index.txt 2>&1 > p/index.html diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..38c8d06 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,11 @@ +CC= tcc +CFLAGS= -Wfatal-errors +LDFLAGS= -s +OBJS= main.o str.o rcms.o + +all: rcms +rcms: $(OBJS) + $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) +clean: + $(RM) rcms $(OBJS) +.PHONY: all clean diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..6c4843b --- /dev/null +++ b/src/main.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include"rcms.h" +#include"str.h" + +int main(int argc,char**argv) +{ + char*filename=NULL; + String str=str_new(64); + + printf("\r"); // I have no idea why this is necessary + str_cpy(&str,"Well what exactly is this?"); + + if(argc<2) + { + puts("error"); + exit(1); + } + + // Find file + assert_file_exists(argv[1]); + filename=argv[1]; + + // Read file + FILE*f=fopen(filename,"r"); + if(!f) + { + printf("error: cannot read '%s'\n",filename); + exit(3); + } + + outputfile(f); + str_free(&str); +} diff --git a/src/rcms.c b/src/rcms.c new file mode 100644 index 0000000..0f13c57 --- /dev/null +++ b/src/rcms.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include"rcms.h" +#include"str.h" + +int fpeek(FILE *f) +{ + int c; + c=fgetc(f); + ungetc(c,f); + return c; +} + +void parse(String str) +{ + String cmd=str_new(64); + char*del=" "; + char*s=strtok(str.s,del); + + // --- FILE --- + //printf("PARSE: "); + if(strcmp(s,"file")==0) + { + s=strtok(NULL,del); + //printf("FILE: '%s'\n",s); + char*fn=s; + FILE*f=fopen(fn,"r"); + char b[512]; + size_t n=0; + + if(!f) + { + printf("\nerror: cannot open file '%s'\n",fn); + exit(1); + } + + while(!feof(f)) + { + n=fread(b,1,512,f); + fwrite(b,1,n,stdout); + } + + fclose(f); + } + + // --- H1 --- + else if(strcmp(s,"h1")==0) + { + printf("

"); + while(s=strtok(NULL,del))printf("%s ",s); + //printf("%s",strtok(NULL,del)); + printf("

"); + } + + str_free(&cmd); +} + +void outputfile(FILE*in) +{ + String str=str_new(64); + enum {NORM,UNMATCHED,MATCHED}; + int state=NORM; + + while(!feof(in)) + { + char b=0; + char res=0; + char*string=NULL; + + fread(&b,1,1,in); + res=fpeek(in); + + if(b=='[' && state==NORM) + { + if(res=='[') + fread(&b,1,1,in); + + // Create command string + else + { + fread(&b,1,1,in); + str_cpy(&str,""); + state=UNMATCHED; + } + } + + else if(b==']' && state==UNMATCHED) + { + //if(res=='\n') + //fread(&b,1,1,in); + state=MATCHED; + parse(str); + } + + else if(b==']' && state==NORM) + { + fread(&b,1,1,in); + } + + if(state==UNMATCHED) + { + char t[2]={b,0}; + str_cat(&str,t); + } + else if(state==MATCHED) + state=NORM; + else if(state==NORM) + fwrite(&b,1,1,stdout); + } + + fclose(in); + str_free(&str); +} + +void assert_file_exists(char*fn) +{ + int fd=open(fn,O_RDONLY); + + if(!fd) + { + printf("error: cannot find '%s'\n",fn); + close(fd); + exit(2); + } +} diff --git a/src/rcms.h b/src/rcms.h new file mode 100644 index 0000000..4b94f45 --- /dev/null +++ b/src/rcms.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include +#include +#include +#include +#include"str.h" + +int fpeek(FILE *f); +void assert_file_exists(char*fn); +void outputfile(FILE*in); +void parse(String str); diff --git a/src/str.c b/src/str.c new file mode 100644 index 0000000..dab25a1 --- /dev/null +++ b/src/str.c @@ -0,0 +1,94 @@ +#include +#include +#include"str.h" + +String str_new(size_t n) +{ + String s={.s=NULL,.n=0}; + + s.s=malloc(n); + if(!s.s) + { + printf("error: cannot allocate string\n"); + exit(1); + } + + s.n=n; + return s; +} + +void str_cpy(String*str,char*s) +{ + size_t n; + + if(!str) + { + printf("error: cannot copy to NULL string\n"); + exit(1); + } + + if(!str->s) + { + printf("error: cannot copy to empty string\n"); + exit(1); + } + + n=strlen(s)+1; + if(str->ns,s,n); + strcpy(str->s,s); + str->n=n; +} + +void str_grow(String*str,size_t n) +{ + if(!str->s) + { + printf("error: cannot grow empty string\n"); + exit(1); + } + + if(str->n>n) + { + str->s=realloc(str->s,n); + if(!str->s) + { + printf("error: cannot reallocate string\n"); + exit(1); + } + + str->n=n; + } +} + +void str_free(String*str) +{ + if(str->s) + free(str->s); + if(str->n>0) + str->n=0; +} + +void str_cat(String*str,char*s) +{ + size_t n; + + if(!str) + { + printf("error: cannot concatenate NULL string\n"); + exit(1); + } + + if(!str->s) + { + printf("error: cannot concatenate empty string\n"); + exit(1); + } + + n=str->n+strlen(s)+1; + str_grow(str,n); + strcat(str->s,s); + str->n=n; +} diff --git a/src/str.h b/src/str.h new file mode 100644 index 0000000..551cd27 --- /dev/null +++ b/src/str.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include +#include + +/*** + * + * Tool-assisted C strings or what + * + ***/ + +typedef struct String +{ + char*s; + size_t n; +} String; + +String str_new(size_t n); +void str_cpy(String*str,char*s); +void str_free(String*str); +void str_grow(String*str,size_t n); +void str_cat(String*str,char*s); diff --git a/test/footer.html b/test/footer.html new file mode 100644 index 0000000..485eac5 --- /dev/null +++ b/test/footer.html @@ -0,0 +1,3 @@ +
+THIS IS THE END OF THE FILE +
diff --git a/test/header.html b/test/header.html new file mode 100644 index 0000000..078c05b --- /dev/null +++ b/test/header.html @@ -0,0 +1 @@ +THIS IS TTILE diff --git a/test/index.txt b/test/index.txt new file mode 100644 index 0000000..d62a8d9 --- /dev/null +++ b/test/index.txt @@ -0,0 +1,8 @@ +[file test/header.html] +[h1 Hello there people] + Okay so today we are going to go to the store and buy +stuff. You can never even know what I am talking about +unless you are a big old monkey with a lot of cigarettes. Do +you even understand what I am thinking, you large donkey +person? +[file test/footer.html]