From ea8a13dfa2a25f52cc245a8992fbff5d1e41106d Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 5 Sep 2009 13:00:55 -0400 Subject: work on tieing it all together --- wrapper/Makefile.in | 77 +++++++++++++++++++++++++++ wrapper/plugin-debug.c | 91 ++++++++++++++++++++++++++++++++ wrapper/plugin-depend.c | 106 +++++++++++++++++++++++++++++++++++++ wrapper/plugin-parse.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++ wrapper/plugin.c | 128 ++++++++++++++++++++++++++++++++++++++++++++ wrapper/plugin.h | 53 +++++++++++++++++++ wrapper/rvs.c | 35 +++++++++++++ wrapper/rvs.h | 95 +++++++++++++++++++++++++++++++++ wrapper/test.c | 9 ++++ 9 files changed, 731 insertions(+) create mode 100755 wrapper/Makefile.in create mode 100644 wrapper/plugin-debug.c create mode 100644 wrapper/plugin-depend.c create mode 100644 wrapper/plugin-parse.c create mode 100644 wrapper/plugin.c create mode 100644 wrapper/plugin.h create mode 100644 wrapper/rvs.c create mode 100644 wrapper/rvs.h create mode 100644 wrapper/test.c (limited to 'wrapper') diff --git a/wrapper/Makefile.in b/wrapper/Makefile.in new file mode 100755 index 0000000..85f56f7 --- /dev/null +++ b/wrapper/Makefile.in @@ -0,0 +1,77 @@ +#!/usr/bin/make -f +name = @name@ +ver = 0.8c +# Copyright (C) 2009 Luke Shumaker +# +# This file is part of rvs. +# +# rvs 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; either version 2, or (at your option) any later version. +# +# rvs 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 rvs; see the file COPYING. +# If not, write to the Free Software Foundation, +# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +export rvs ?= @name@ +# directories ###################################################### +srcdir ?= @srcdir@ +prefix ?= @prefix@ +exec_prefix ?= @exec_prefix@ +bindir ?= @bindir@ +sbindir ?= @sbindir@ +libexecdir ?= @libexecdir@ +dirs = $(srcdir) $(prefix) $(exec_prefix) $(bindir) $(sbindir) $(libexecdir)/ +#export srcdir prefix exec_prefix bindir sbindir libexecdir + +# programs ######################################################### +RVS ?= @RVS@ +SHELL ?= @SHELL@ +RM ?= rm +CP ?= cp +SED ?= sed +INSTALL ?= install +MKDIR ?= $(INSTALL) -d #mkdir -p +INSTALL_PROGRAM ?= $(INSTALL) +INSTALL_DATA ?= $(INSTALL) -m 644 +TOUCH ?= touch # This file doesn't use touch +#export RVS SHELL RM CP SED INSTALL MKDIR INSTALL_PROGRAM INSTALL_DATA TOUCH + +# phony targets #################################################### +all : rvs +.PHONY : install uninstall clean distclean dist +.SUFFIXES : .c .o +VPATH = $(srcdir)/wrapper + +# most everything ################################################## +install : $(RVS) +$(RVS) : rvs $(bindir) + $(INSTALL_PROGRAM) $< $@ + +uninstall : + $(RM) $(RVS) + +clean : + +distclean : clean + $(RM) rvs + $(RM) Makefile + +# dist ############################################################# + +d = $(name)-$(ver) +dist : $(d).tar.gz + +$(d).tar.gz : $(d) + tar -czf $@ $< + +$(d) : distclean + $(INSTALL) -m 777 -d $@ + $(CP) -r $(srcdir)/* $@ + diff --git a/wrapper/plugin-debug.c b/wrapper/plugin-debug.c new file mode 100644 index 0000000..a0c8022 --- /dev/null +++ b/wrapper/plugin-debug.c @@ -0,0 +1,91 @@ +/* Copyright (C) 2009 Luke Shumaker + + This file is part of rvs. + + rvs 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; either version 2, or (at your + option) any later version. + + rvs 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 rvs; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#ifndef FILE_plugin-debug.c_SEEN +#define FILE_plugin-debug.c_SEEN + +size_t xstrlen (const char *s) +{ + printf("xstrlen(%p)\n",s); + size_t size=0; + while (s[size] != '\0') { + printf("%i = %p `%c'\n",size,&s[size],s[size]); + size++; + } + return size; +} + +void _plugin_print_plugin_command(struct plugin_command *command) +{ + if (command != NULL) { + printf(" - %s\n",command->name); + if (command->depends != NULL) + printf(" depend string: %s\n",command->depends); + if (command->depend->plugin == NULL) { + puts (" depend: "); + } else { + printf(" depend: %s / %s\n", + command->depend->plugin->name, + command->depend->name); + } + _plugin_print_plugin_command(command->p_next); + } +} + +void _plugin_print_plugin(struct plugin *plugin) +{ + if (plugin != NULL) { + printf(" %s\n",plugin->name); + _plugin_print_plugin_command(plugin->child); + _plugin_print_plugin(plugin->next); + } +} + +void _plugin_print_depend(struct plugin_command *command,size_t indent) +{ + int i=0; + while (i < indent) { + printf(" "); + i++; + } + if (command->name == NULL) { + puts(""); + } else { + printf("%s / %s\n", + command->plugin->name, + command->name); + } + if (command->child != NULL) { + _plugin_print_depend(command->child,indent+1); + } + if (command->d_next != NULL) { + _plugin_print_depend(command->d_next,indent); + } +} + +void _plugin_print(struct plugin_tree *tree) +{ + puts("Plugins:"); + _plugin_print_plugin(tree->plugins); + puts("\nDepends:"); + _plugin_print_depend(tree->depends,0); +} + +#endif + diff --git a/wrapper/plugin-depend.c b/wrapper/plugin-depend.c new file mode 100644 index 0000000..3dfbf1b --- /dev/null +++ b/wrapper/plugin-depend.c @@ -0,0 +1,106 @@ +/* Copyright (C) 2009 Luke Shumaker + + This file is part of rvs. + + rvs 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; either version 2, or (at your + option) any later version. + + rvs 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 rvs; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#ifndef FILE_plugin-depend.c_SEEN +#define FILE_plugin-depend.c_SEEN + +/* translates a string in syntax `plugin/command' into a pointer to the commnad + DON'T plan on using the string again, it will be mutilated! + (so we go ahead and free() it) */ +struct plugin_command * +_plugin_depend_parse(struct plugin *root, char *string) +{ + char *c=strchr(string,'/'); + c[0]='\0'; + return _plugin_find_command( + _plugin_find_plugin(root,string)->child, + &c[1]); + xfree(string); +} + +/* used by _plugin_depend_add */ +void +_plugin_depend_add2(struct plugin_command *prev, struct plugin_command *next) +{ + if (prev->d_next==NULL) { + prev->d_next=next; + } else { + _plugin_depend_add2(prev->d_next,next); + } +} + +/* _plugin_depend_add(depend,depender) */ +void +_plugin_depend_add(struct plugin_command *depend, struct plugin_command *depender) +{ + if (depend->child==NULL) { + depend->child=depender; + } else { + _plugin_depend_add2(depend->child,depender); + } +} + +/* take care of depends for `command', and those after it (linked list) */ +void +_plugin_depend_command(struct plugin_command *command, struct plugin_tree *tree) +{ + if (command->depends == NULL) { + command->depend=tree->depends; + } else { + command->depend=_plugin_depend_parse(tree->plugins,command->depends); + command->depends=NULL; + } + _plugin_depend_add(command->depend,command); + if (command->p_next != NULL) + _plugin_depend_command(command->p_next,tree); +} + +/* take care of commands for a `plugin', and those after it (linked list) */ +void +_plugin_depend_plugin(struct plugin *plugin, struct plugin_tree *tree) +{ + _plugin_depend_command(plugin->child,tree); + if (plugin->next != NULL) { + _plugin_depend_plugin(plugin->next,tree); + } +} + +/* take care of all depends */ +void +_plugin_depend(struct plugin_tree *tree) +{ + /* add a blank `root' command for those that don't have depends */ + struct plugin_command *command; + command=(struct plugin_command *)xmalloc(sizeof(*command)); + command->name=NULL; + command->plugin=NULL; + command->p_next=NULL; + command->depend=NULL; + command->depends=NULL; + command->child=NULL;/*<--only variable of any value */ + command->d_next=NULL; + + tree->depends=command; + + /* parse all commands */ + _plugin_depend_plugin(tree->plugins,tree); +} + +#endif + diff --git a/wrapper/plugin-parse.c b/wrapper/plugin-parse.c new file mode 100644 index 0000000..3be7543 --- /dev/null +++ b/wrapper/plugin-parse.c @@ -0,0 +1,137 @@ +/* Copyright (C) 2009 Luke Shumaker + + This file is part of rvs. + + rvs 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; either version 2, or (at your + option) any later version. + + rvs 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 rvs; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#ifndef FILE_plugin-parse.c_SEEN +#define FILE_plugin-parse.c_SEEN + +void _plugin_parse_comment (FILE *file) +{ + char c; + while ( (c=getc(file)) != EOF ) { + if ( c == '\n' ) { + ungetc (c, file); + break; + } + } +} + +char _plugin_parse_escape(FILE *file) +{ + char c=getc(file); + switch (c) { + case 'n': + c = '\n'; + case '\\': + case '#': + case ':': + break; + default: + error(EXIT_FAILURE,0,"syntax error"); + break; + } + return c; +} + +char *_plugin_parse_depend (FILE *file) +{ + size_t nbytes = 10; + char *string = (char *)xmalloc(nbytes); + string[0]='\0'; + + char c[2] = " \0"; + char *cs = (char *)&c; + while ( (c[0]=getc(file)) != EOF ) { + if (c[0] == '\n') { + ungetc (c[0], file); + break; + } else { + switch (c[0]) { + case '\\': + c[0]=_plugin_parse_escape(file); + stradds(&nbytes,&string,cs); + break; + case '#': + _plugin_parse_comment(file); + break; + default: + stradds(&nbytes,&string,cs); + break; + } + } + } + return string; +} + +struct plugin_command *_plugin_parse (struct plugin *plugin, FILE *file) +{ + struct plugin_command *command; + command=(struct plugin_command *)xmalloc(sizeof(*command)); + + command->plugin=plugin; + command->p_next=NULL; + + command->depend=NULL; + command->depends=NULL; + command->child=NULL; + command->d_next=NULL; + + size_t nbytes = 10; + char *string = (char *)xmalloc(nbytes); + string[0]='\0'; + + char c[2] = " \0"; + char *cs = (char *)&c; + if ( (c[0]=getc(file)) == EOF ) { + return NULL; + } else { + ungetc (c[0], file); + } + while ( (c[0]=getc(file)) != EOF ) { + if (c[0] == '\n') { + if (strcmp(string,"")==0) { + xfree(command); + xfree(string); + command=_plugin_parse(plugin, file); + } else { + command->name=string; + command->p_next=_plugin_parse(plugin, file); + } + break; + } else { + switch (c[0]) { + case '\\': + c[0]=_plugin_parse_escape(file); + stradds(&nbytes,&string,cs); + break; + case '#': + _plugin_parse_comment(file); + break; + case ':': + command->depends=_plugin_parse_depend(file); + break; + default: + stradds(&nbytes,&string,cs); + break; + } + } + } + return command; +} + +#endif diff --git a/wrapper/plugin.c b/wrapper/plugin.c new file mode 100644 index 0000000..c5d8f38 --- /dev/null +++ b/wrapper/plugin.c @@ -0,0 +1,128 @@ +/* Copyright (C) 2009 Luke Shumaker + + This file is part of rvs. + + rvs 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; either version 2, or (at your + option) any later version. + + rvs 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 rvs; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#ifndef FILE_plugin.c_SEEN +#define FILE_plugin.c_SEEN + +#include +#include /* EXIT_FAILURE */ +#include /* file acces */ +#include + +#include +#include + +#include +#include + +/*#include "rvs.h"*/ +#include "plugin.h" + +/* finds a plugin with `name'. Start looking at `plugin' (linked list) */ +struct plugin * +_plugin_find_plugin(struct plugin *plugin, char *name) +{ + if (strcmp(plugin->name,name) == 0) { + return plugin; + } else { + if (plugin->next==NULL) { + error(EXIT_FAILURE,0,"cannot find plugin `%s'",name); + return NULL; + } else { + return _plugin_find_plugin(plugin->next,name); + } + } +} + +/* finds a command with `name'. Start looking at `command' (linked list) */ +struct plugin_command * +_plugin_find_command(struct plugin_command *command, char *name) +{ + if (strcmp(command->name,name) == 0) { + return command; + } else { + if (command->p_next==NULL) { + error(EXIT_FAILURE,0, + "plugin `%s' does not contain command `%s'", + command->plugin->name,name); + return NULL; + } else { + return _plugin_find_command(command->p_next,name); + } + } +} + +#include "plugin-parse.c" +#include "plugin-depend.c" + +struct plugin * +_plugin_load (char *plug_name, char *plugin_conf) +{ + struct plugin *plugin=(struct plugin *)xmalloc(sizeof(*plugin)); + char *plug_name2 = (char *)xmalloc(strlen(plug_name)+1); + strcpy(plug_name2,plug_name); + plugin->name=plug_name2; + plugin->next=NULL; + + xchdir(plug_name); + FILE *file = xfopen(plugin_conf,"r"); + + plugin->child=_plugin_parse(plugin, file); + + fclose( file ); + xchdir(".."); + return plugin; +} + +struct plugin_tree * +load_plugins (char *libexecdir, char *plugin_conf) +{ + struct plugin_tree *tree=(struct plugin_tree *)xmalloc(sizeof(*tree)); + struct plugin **last=&tree->plugins; + *last=NULL; + struct plugin *plugin; + + xchdir(libexecdir); + + DIR *cwd; + struct dirent *dirent; + int serr; + struct stat sbuf; + cwd = xopendir ("./"); + while ( (dirent = readdir (cwd)) != NULL ) { + if ((strcmp(dirent->d_name,"." )!=0)&& + (strcmp(dirent->d_name,"..")!=0)) { + serr = stat(dirent->d_name, &sbuf); + if (!serr && S_ISDIR(sbuf.st_mode)) { + plugin=_plugin_load(dirent->d_name,plugin_conf); + *last=plugin;/* this sets the last's `next' */ + last=&plugin->next; + } + } + } + closedir (cwd); + + _plugin_depend(tree); + + xchdir(".."); + return tree; +} + +#endif + diff --git a/wrapper/plugin.h b/wrapper/plugin.h new file mode 100644 index 0000000..5eff78e --- /dev/null +++ b/wrapper/plugin.h @@ -0,0 +1,53 @@ +/* Copyright (C) 2009 Luke Shumaker + + This file is part of rvs. + + rvs 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; either version 2, or (at your + option) any later version. + + rvs 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 rvs; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#ifndef FILE_plugin.h_SEEN +#define FILE_plugin.h_SEEN + +struct plugin +{ + char *name; + + struct plugin_command *child; + struct plugin *next; +}; + +struct plugin_command +{ + char *name; + + /*plugin ID*/ + struct plugin *plugin; /* which plugin does it belong to? */ + struct plugin_command *p_next; /* next command in this plugin (linked list)*/ + + /*dependancy tree*/ + struct plugin_command *depend; /* what does this depend on? */ + char *depends;/* what does this depend on? */ + struct plugin_command *child; /* what depends on this? (linked list) */ + struct plugin_command *d_next; /* next command w/ same dependancy (linked list) */ +}; + +struct plugin_tree +{ + struct plugin *plugins; + struct plugin_command *depends; +}; + +#endif + diff --git a/wrapper/rvs.c b/wrapper/rvs.c new file mode 100644 index 0000000..56162ac --- /dev/null +++ b/wrapper/rvs.c @@ -0,0 +1,35 @@ +const char *name="rvs"; +const char *ver="0.8c"; +/* Copyright (C) 2009 Luke Shumaker + + This file is part of rvs. + + rvs 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; either version 2, or (at your + option) any later version. + + rvs 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 rvs; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "rvs.h" +#include "plugin.c" + +const char *libexecdir="./plugins"; +const char *plugin_conf="plugin.conf"; + +int +main (void) +{ + struct plugin_tree *plugins=load_plugins(libexecdir,plugin_conf); + return 0; +} + diff --git a/wrapper/rvs.h b/wrapper/rvs.h new file mode 100644 index 0000000..adef3b1 --- /dev/null +++ b/wrapper/rvs.h @@ -0,0 +1,95 @@ +/* Copyright (C) 2009 Luke Shumaker + + This file is part of rvs. + + rvs 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; either version 2, or (at your + option) any later version. + + rvs 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 rvs; see the file COPYING. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#ifndef FILE_rvs.h_SEEN +#define FILE_rvs.h_SEEN + +#include +#include +#include +#include +#include +#include +#include + +void *xmalloc (size_t size) +{ + void *value = malloc (size); + if (value == NULL) + error(EXIT_FAILURE,0,"virtual memory exhausted"); + return value; +} + +void *xrealloc (void *ptr, size_t size) +{ + void *value = realloc (ptr, size); + if (value == NULL) + error(EXIT_FAILURE,0,"virtual memory exhausted"); + return value; +} + +void xfree (void *ptr) +{ + free (ptr); +} + +int xchdir (const char *filename) +{ + int ret=chdir(filename); + if (ret != 0) { + error(EXIT_FAILURE,errno,"%s/",filename); + } + return ret; +} + +FILE *xfopen (const char *filename, const char *opentype) +{ + FILE *file = fopen(filename,opentype); + /* fopen gives NULL on failure */ + if ( file == NULL ) { + error(EXIT_FAILURE,errno,"%s",filename); + } + + return file; +} + +DIR *xopendir (const char *dirname) +{ + DIR *dir = opendir (dirname); + if (dir == NULL) { + error(EXIT_FAILURE,errno,"%s/",dirname); + } + + return dir; +} + +void stradds(size_t *size, char **dest, char *str) +{ + if (*size > ( strlen(*dest) + strlen(str) )) { + strcat(*dest, str); + } else { + *size = strlen(*dest) + strlen(str) + 1; + + *dest = (char *) xrealloc (*dest, *size); + strcat(*dest, str); + } +} + +#endif + diff --git a/wrapper/test.c b/wrapper/test.c new file mode 100644 index 0000000..3a9abc9 --- /dev/null +++ b/wrapper/test.c @@ -0,0 +1,9 @@ +#include "plugin.c" +#include "plugin-debug.c" + +int main() { + struct plugin_tree *plugins=load_plugins("plugins","plugin.conf"); + _plugin_print(plugins); + return 0; +} + -- cgit v1.2.3-2-g168b