initial commit

This commit is contained in:
roswold 2021-05-20 11:49:48 -05:00
commit a9f50b264c
13 changed files with 333 additions and 0 deletions

10
Makefile Normal file
View File

@ -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

4
README Normal file
View File

@ -0,0 +1,4 @@
Tool to generate static website from specification documents
(text files).
See test directory for examples.

0
p/index.html Normal file
View File

3
s.sh Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/sh
./rcms test/index.txt 2>&1 > p/index.html

11
src/Makefile Normal file
View File

@ -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

37
src/main.c Normal file
View File

@ -0,0 +1,37 @@
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#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);
}

128
src/rcms.c Normal file
View File

@ -0,0 +1,128 @@
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#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("<h1>");
while(s=strtok(NULL,del))printf("%s ",s);
//printf("%s",strtok(NULL,del));
printf("</h1>");
}
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);
}
}

12
src/rcms.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include"str.h"
int fpeek(FILE *f);
void assert_file_exists(char*fn);
void outputfile(FILE*in);
void parse(String str);

94
src/str.c Normal file
View File

@ -0,0 +1,94 @@
#include<stdio.h>
#include<string.h>
#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->n<n)
str_grow(str,n);
//memcpy(str->s,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;
}

22
src/str.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
/***
*
* 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);

3
test/footer.html Normal file
View File

@ -0,0 +1,3 @@
</br>
THIS IS THE END OF THE FILE
</br>

1
test/header.html Normal file
View File

@ -0,0 +1 @@
<title>THIS IS TTILE</title>

8
test/index.txt Normal file
View File

@ -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]