shithub: fsgen

ref: 7c4e47bececffe6e1a62adb2fc93ff2422a9c274
dir: /code.c/

View raw version
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"

static void
printvars(VFile *file)
{
	char **a;
	char buf[64];
	int n;
	
	for (a = file->parts; *a; a++) {
		if (!(*a)[0])
			continue;
		n = strlen(*a) - 1;
		if (!((*a)[0] == '{' && (*a)[n] == '}'))
			continue;
		strcpy(buf, *a);
		buf[n] = 0;
		print(", char *%s", buf+1);
	}
}

char *invalid = "/{}";

static void
pathtostring(char *buf, int max, char *path)
{
	char *s;
	strncpy(buf, path, max);
	buf[max-1] = 0;
	for (s = buf; *s; s++)
		if (strchr(invalid, *s))
			*s = '_';
}

void
printread(VFile *file)
{
	char buf[64];
	pathtostring(buf, sizeof(buf), file->path);
	print("static void\nfsread_%s(Req *r", buf);
	printvars(file);
	print(")\n");
}

void
printwrite(VFile *file)
{
	char buf[64];
	pathtostring(buf, sizeof(buf), file->path);
	print("static void\nfswrite_%s(Req *r", buf);
	printvars(file);
	print(")\n");
}

void
printls(VFile *file)
{
	char buf[64];
	pathtostring(buf, sizeof(buf), file->path);
	print("static void\nfsls_%s(...)\n", buf);
}

void
printpre()
{
	print("#line 0 \"preamble.inc\"\n"
#include "preamble.cinc"
	);
}

extern char* file;

void
printfs()
{
	char buf[64];
	char *s;
	
	strcpy(buf, file);
	s = strrchr(buf, '.');
	if (strcmp(s, ".fs") == 0)
		*s = 0;
	for (s = buf; *s; s++)
		if (*s == '.')
			*s = '_';
	s = strrchr(buf, '/');
	if (s)
		s++;
	else
		s = buf;
	
	print("\n#line 0 \"fshandler.inc\"\n"
#include "fshandler.cinc"
	);
	
	print("\n#line 0 \"fsgen/code.c\"");
	print("\n"
	"Srv fs = {\n"
	"	.read = fsread,\n"
	"	.walk1 = fswalk,\n"
	"};\n\n"
	"Srv*\n"
	"getfs_%s()\n", s);
	
	print("#line 0 \"fsfunc.inc\"\n"
#include "fsfunc.cinc"
	);
}