From 44c43d3254ab79407c1ab43f9e22cd61155a1c21 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Tue, 20 Oct 2009 21:40:08 +0000 Subject: [PATCH] Added a tools folder with an early version of a AppInterface definition broker --- Makefile | 42 ++++++------ project.conf | 2 +- tools/Makefile | 39 +++++++++++ tools/README | 45 +++++++++++++ tools/broker.c | 163 +++++++++++++++++++++++++++++++++++++++++++++ tools/project.conf | 10 +++ 6 files changed, 281 insertions(+), 20 deletions(-) create mode 100644 tools/Makefile create mode 100644 tools/README create mode 100644 tools/broker.c create mode 100644 tools/project.conf diff --git a/Makefile b/Makefile index e96e0d1..bec9824 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PACKAGE = libSystem VERSION = 0.1.2 -SUBDIRS = src include +SUBDIRS = include src tools RM = rm -f LN = ln -f TAR = tar -czvf @@ -21,24 +21,6 @@ dist: $(RM) -r $(PACKAGE)-$(VERSION) $(LN) -s . $(PACKAGE)-$(VERSION) @$(TAR) $(PACKAGE)-$(VERSION).tar.gz \ - $(PACKAGE)-$(VERSION)/src/appclient.c \ - $(PACKAGE)-$(VERSION)/src/appinterface.c \ - $(PACKAGE)-$(VERSION)/src/appserver.c \ - $(PACKAGE)-$(VERSION)/src/array.c \ - $(PACKAGE)-$(VERSION)/src/buffer.c \ - $(PACKAGE)-$(VERSION)/src/config.c \ - $(PACKAGE)-$(VERSION)/src/error.c \ - $(PACKAGE)-$(VERSION)/src/event.c \ - $(PACKAGE)-$(VERSION)/src/hash.c \ - $(PACKAGE)-$(VERSION)/src/object.c \ - $(PACKAGE)-$(VERSION)/src/parser.c \ - $(PACKAGE)-$(VERSION)/src/plugin.c \ - $(PACKAGE)-$(VERSION)/src/string.c \ - $(PACKAGE)-$(VERSION)/src/token.c \ - $(PACKAGE)-$(VERSION)/src/Makefile \ - $(PACKAGE)-$(VERSION)/src/appinterface.h \ - $(PACKAGE)-$(VERSION)/src/token.h \ - $(PACKAGE)-$(VERSION)/src/project.conf \ $(PACKAGE)-$(VERSION)/include/System.h \ $(PACKAGE)-$(VERSION)/include/Makefile \ $(PACKAGE)-$(VERSION)/include/project.conf \ @@ -58,6 +40,28 @@ dist: $(PACKAGE)-$(VERSION)/include/System/token.h \ $(PACKAGE)-$(VERSION)/include/System/Makefile \ $(PACKAGE)-$(VERSION)/include/System/project.conf \ + $(PACKAGE)-$(VERSION)/src/appclient.c \ + $(PACKAGE)-$(VERSION)/src/appinterface.c \ + $(PACKAGE)-$(VERSION)/src/appserver.c \ + $(PACKAGE)-$(VERSION)/src/array.c \ + $(PACKAGE)-$(VERSION)/src/buffer.c \ + $(PACKAGE)-$(VERSION)/src/config.c \ + $(PACKAGE)-$(VERSION)/src/error.c \ + $(PACKAGE)-$(VERSION)/src/event.c \ + $(PACKAGE)-$(VERSION)/src/hash.c \ + $(PACKAGE)-$(VERSION)/src/object.c \ + $(PACKAGE)-$(VERSION)/src/parser.c \ + $(PACKAGE)-$(VERSION)/src/plugin.c \ + $(PACKAGE)-$(VERSION)/src/string.c \ + $(PACKAGE)-$(VERSION)/src/token.c \ + $(PACKAGE)-$(VERSION)/src/Makefile \ + $(PACKAGE)-$(VERSION)/src/appinterface.h \ + $(PACKAGE)-$(VERSION)/src/token.h \ + $(PACKAGE)-$(VERSION)/src/project.conf \ + $(PACKAGE)-$(VERSION)/tools/broker.c \ + $(PACKAGE)-$(VERSION)/tools/Makefile \ + $(PACKAGE)-$(VERSION)/tools/README \ + $(PACKAGE)-$(VERSION)/tools/project.conf \ $(PACKAGE)-$(VERSION)/Makefile \ $(PACKAGE)-$(VERSION)/COPYING \ $(PACKAGE)-$(VERSION)/config.h \ diff --git a/project.conf b/project.conf index bfe1fe1..2e9e581 100644 --- a/project.conf +++ b/project.conf @@ -3,4 +3,4 @@ version=0.1.2 config=h dist=Makefile,COPYING,config.h -subdirs=src,include +subdirs=include,src,tools diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..785c868 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,39 @@ +TARGETS = broker +PREFIX = /usr/local +DESTDIR = +BINDIR = $(PREFIX)/bin +CC = cc +CPPFLAGSF= -I ../include +CPPFLAGS= +CFLAGSF = -W +CFLAGS = -Wall -g -O2 -pedantic +LDFLAGSF= -lSystem -L$(PREFIX)/lib -Wl,-rpath,$(PREFIX)/lib +RM = rm -f +LN = ln -f +MKDIR = mkdir -p +INSTALL = install + + +all: $(TARGETS) + +broker_OBJS = broker.o +broker_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) +broker_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) + +broker: $(broker_OBJS) + $(CC) -o broker $(broker_OBJS) $(broker_LDFLAGS) + +broker.o: broker.c + $(CC) $(broker_CFLAGS) -c broker.c + +clean: + $(RM) $(broker_OBJS) + +distclean: clean + $(RM) $(TARGETS) + +install: all + +uninstall: + +.PHONY: all clean distclean install uninstall diff --git a/tools/README b/tools/README new file mode 100644 index 0000000..fdbc0bb --- /dev/null +++ b/tools/README @@ -0,0 +1,45 @@ +broker reads a configuration file for a given AppInterface, eg: + +=== BEGIN FILE === +service=VFS + +[chmod] +ret=INT32 +arg1=STRING,pathname +arg2=UINT32,mode +=== END FILE === + +which is then translated into a C header file, eg: +=== BEGIN FILE === +/* $Id$ */ + + + +#ifndef VFS_H +# define VFS_H + +# include +# include + + +/* types */ +typedef Buffer * BUFFER; +typedef int32_t INT32; +typedef uint32_t UINT32; +typedef String * STRING; + + +/* functions */ +INT32 VFS_chmod(STRING pathname, UINT32 mode); + +#endif /* !VFS_H */ +=== END FILE === + +This service definition file and corresponding header may then be committed +along the relevant source code. + +It would also be helpful to: +- add scripting possibilities to configure (to regenerate the headers + accordingly) +- place this code directly inside AppInterface's class +- implement IN/OUT/IN_OUT diff --git a/tools/broker.c b/tools/broker.c new file mode 100644 index 0000000..49b96f7 --- /dev/null +++ b/tools/broker.c @@ -0,0 +1,163 @@ +/* $Id$ */ +/* Copyright (c) 2009 Pierre Pronchery */ +/* This file is part of DeforaOS System libSystem */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + + + +#include +#include +#include +#include +#include + +#define PACKAGE "broker" + + +/* broker */ +typedef struct _BrokerData +{ + char const * prefix; + char const * outfile; + FILE * fp; +} BrokerData; + + +/* broker */ +static void _broker_head(BrokerData * data); +static int _broker_foreach(char const * key, Hash * value, BrokerData * data); +static void _broker_foreach_arg(BrokerData * data, char const * sep, + char const * arg); +static void _broker_tail(BrokerData * data); + +static int _broker(char const * outfile, char const * filename) +{ + Config * config; + BrokerData data; + + if((config = config_new()) == NULL) + return error_print(PACKAGE); + if(config_load(config, filename) != 0) + { + config_delete(config); + return error_print(PACKAGE); + } + data.prefix = config_get(config, NULL, "service"); + if((data.outfile = outfile) == NULL) + data.fp = stdout; + else if((data.fp = fopen(outfile, "w")) == NULL) + { + config_delete(config); + return error_set_print(PACKAGE, 1, "%s: %s", outfile, + strerror(errno)); + } + _broker_head(&data); + fputs("\n\n/* functions */\n", data.fp); + hash_foreach(config, _broker_foreach, &data); + _broker_tail(&data); + if(outfile != NULL) + fclose(data.fp); + config_delete(config); + return 0; +} + +static void _broker_head(BrokerData * data) +{ + fputs("/* $Id$ */\n\n\n\n", data->fp); + if(data->prefix != NULL) + fprintf(data->fp, "%s%s%s%s%s%s", "#ifndef ", data->prefix, + "_H\n", "# define ", data->prefix, "_H\n"); + fputs("\n# include \n", data->fp); + fputs("# include \n\n", data->fp); + fputs("\n/* types */\n", data->fp); + fputs("typedef Buffer * BUFFER;\n", data->fp); + fputs("typedef int32_t INT32;\n", data->fp); + fputs("typedef uint32_t UINT32;\n", data->fp); + fputs("typedef String * STRING;\n", data->fp); +} + +static int _broker_foreach(char const * key, Hash * value, BrokerData * data) +{ + int i; + char buf[8]; + char const * p; + char const * sep = ""; + + if(key == NULL || key[0] == '\0') + return 0; + if((p = hash_get(value, "ret")) == NULL) + p = "void"; + fprintf(data->fp, "%s%s%s%s%s%s", p, " ", data->prefix, "_", key, "("); + for(i = 0; i < 3; i++) + { + snprintf(buf, sizeof(buf), "arg%d", i + 1); + if((p = hash_get(value, buf)) == NULL) + break; + _broker_foreach_arg(data, sep, p); + sep = ", "; + } + fprintf(data->fp, "%s", ");\n"); + return 0; +} + +static void _broker_foreach_arg(BrokerData * data, char const * sep, + char const * arg) +{ + char * p; + + if((p = strchr(arg, ',')) == NULL) + { + fprintf(data->fp, "%s%s", sep, arg); + return; + } + fputs(sep, data->fp); + fwrite(arg, sizeof(*arg), p - arg, data->fp); + if(*(++p) != '\0') + fprintf(data->fp, " %s", p); +} + +static void _broker_tail(BrokerData * data) +{ + if(data->prefix != NULL) + fprintf(data->fp, "%s%s%s", "\n#endif /* !", data->prefix, + "_H */\n"); +} + + +/* usage */ +static int _usage(void) +{ + fputs("Usage: broker [-o outfile] filename\n", stderr); + return 1; +} + + +/* main */ +int main(int argc, char * argv[]) +{ + int o; + char const * outfile = NULL; + + while((o = getopt(argc, argv, "o:")) != -1) + switch(o) + { + case 'o': + outfile = optarg; + break; + default: + return _usage(); + } + if(optind + 1 != argc) + return _usage(); + return (_broker(outfile, argv[optind]) == 0) ? 0 : 2; +} diff --git a/tools/project.conf b/tools/project.conf new file mode 100644 index 0000000..85ab52c --- /dev/null +++ b/tools/project.conf @@ -0,0 +1,10 @@ +targets=broker +cppflags_force=-I ../include +cflags_force=-W +cflags=-Wall -g -O2 -pedantic +ldflags_force=-lSystem -L$(PREFIX)/lib -Wl,-rpath,$(PREFIX)/lib +dist=Makefile,README + +[broker] +type=binary +sources=broker.c