diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2009-09-06 21:02:08 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2015-06-26 00:30:14 -0600 |
commit | 3420365915d2a9ca75c7ec18d72ff7c1955de757 (patch) | |
tree | b1b835c8bab8d0d6d2fd97870022109c4122b017 /wrapper | |
parent | e1c9e116260d56fce4d1722c62434f9ddb2b229d (diff) |
I'm going to come out and say it, I fucked it up :(
Diffstat (limited to 'wrapper')
-rw-r--r-- | wrapper/plugin-debug.c | 91 | ||||
-rw-r--r-- | wrapper/plugin-depend.c | 36 | ||||
-rw-r--r-- | wrapper/plugin-parse.c | 30 | ||||
-rw-r--r-- | wrapper/plugin.c | 78 | ||||
-rw-r--r-- | wrapper/plugin.h | 31 | ||||
-rw-r--r-- | wrapper/rvs.c | 4 | ||||
-rw-r--r-- | wrapper/rvs.h | 31 |
7 files changed, 107 insertions, 194 deletions
diff --git a/wrapper/plugin-debug.c b/wrapper/plugin-debug.c deleted file mode 100644 index 793b07d..0000000 --- a/wrapper/plugin-debug.c +++ /dev/null @@ -1,91 +0,0 @@ -/* 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: <none>"); - } 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("<none>"); - } 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 index 11a87eb..d562489 100644 --- a/wrapper/plugin-depend.c +++ b/wrapper/plugin-depend.c @@ -20,18 +20,36 @@ #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 +/* translates a string in format `plugin/command' into a pointer to the command DON'T plan on using the string again, it will be mutilated! - (so we go ahead and free() it) */ + (so we go ahead and free() it and set the pointer to NULL) */ struct plugin_command * -_plugin_depend_parse (char *string, struct plugin_command *root) +_plugin_depend_parse (char **string, struct plugin_command *root) { - char *c=strchr(string,'/'); - c[0]='\0'; - return _plugin_find_command( - _plugin_find_plugin(root,string)->child, - &c[1]); - xfree(string); + struct plugin_command *command=NULL; + + if (*string==NULL) + *string=root; + else { + /* *string is in the format `plugin/command' */ + /* char *del=delimeter */ + char *del=strchr(*string,'/'); + del[0]='\0'; + /// *string = PLUGIN_NAME + /// &del[1] = COMMAND_NAME + struct plugin *plugin; + plugin =_plugin_find_plugin(root, *string ); + if (plugin==NULL) + error(EXIT_FAILURE,0,"cannot find plugin `%s'",*string); + command=_plugin_find_plugin_command(plugin->child, &del[1] ); + if (command==NULL) + error(EXIT_FAILURE,0, + "plugin `%s' does not contain command `%s'", + *string,&del[1]); + xfree(*string); + *string=NULL; + } + return command; } /* used by _plugin_depend_add */ diff --git a/wrapper/plugin-parse.c b/wrapper/plugin-parse.c index 7fd04bb..8560dfd 100644 --- a/wrapper/plugin-parse.c +++ b/wrapper/plugin-parse.c @@ -20,7 +20,17 @@ #ifndef FILE_plugin_parse_c_SEEN #define FILE_plugin_parse_c_SEEN -void _plugin_parse_comment (FILE *file) +#include <stdio.h> + +/* these 2 are only used in _plugin_parse_escape */ +#include <stdlib.h> +#include <error.h> + +#include "rvs.h" +#include "plugin.h" + +void +_plugin_parse_comment (FILE *file) { char c; while ( (c=getc(file)) != EOF ) { @@ -31,7 +41,8 @@ void _plugin_parse_comment (FILE *file) } } -char _plugin_parse_escape(FILE *file) +char +_plugin_parse_escape(FILE *file) { char c=getc(file); switch (c) { @@ -48,7 +59,8 @@ char _plugin_parse_escape(FILE *file) return c; } -char *_plugin_parse_depend (FILE *file) +char * +_plugin_parse_depend (FILE *file) { size_t nbytes = 10; char *string = (char *)xmalloc(nbytes); @@ -78,7 +90,8 @@ char *_plugin_parse_depend (FILE *file) return string; } -struct plugin_command *_plugin_parse (struct plugin *plugin, FILE *file) +struct plugin_command * +_plugin_parse (FILE *file) { struct plugin_command *command=mkcommand(); @@ -95,13 +108,12 @@ struct plugin_command *_plugin_parse (struct plugin *plugin, FILE *file) } while ( (c[0]=getc(file)) != EOF ) { if (c[0] == '\n') { - if (strcmp(string,"")==0) { + if (strlen(string)==0) { xfree(command); xfree(string); - command=_plugin_parse(plugin, file); + command=_plugin_parse(file); } else { command->name=string; - command->p_next=_plugin_parse(plugin, file); } break; } else { @@ -114,7 +126,8 @@ struct plugin_command *_plugin_parse (struct plugin *plugin, FILE *file) _plugin_parse_comment(file); break; case ':': - command->depends=_plugin_parse_depend(file); + command->depends=_plugin_parse_depend( + file); break; default: stradds(&nbytes,&string,cs); @@ -126,3 +139,4 @@ struct plugin_command *_plugin_parse (struct plugin *plugin, FILE *file) } #endif + diff --git a/wrapper/plugin.c b/wrapper/plugin.c index 1851941..6a38035 100644 --- a/wrapper/plugin.c +++ b/wrapper/plugin.c @@ -20,86 +20,62 @@ #ifndef FILE_plugin_c_SEEN #define FILE_plugin_c_SEEN -#include <stdio.h> #include <stdlib.h> /* EXIT_FAILURE */ #include <unistd.h> /* file acces */ #include <string.h> -#include <errno.h> -#include <error.h> - #include <dirent.h> #include <sys/stat.h> #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" +/*#include "plugin-depend.c"*/ -struct plugin * +/* assume we're in rvs's libexecdir, where plugins are + now, load plugin plug_name, + whose configuration file is plugin_conf +*/ +struct tree_list * _plugin_load (const char *plug_name, const 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; + struct tree_list *plugin=xmalloc( sizeof(*plugin) ); + plugin->node=stralloc(plug_name); plugin->next=NULL; + plugin->child=NULL; xchdir(plug_name); FILE *file = xfopen(plugin_conf,"r"); - plugin->child=_plugin_parse(plugin, file); + struct tree_list **last=&plugin->child; + + struct plugin_command *command; + while ( (command=_plugin_parse(file))!=NULL ) { + command->plugin=plugin; + + struct tree_list *com=xmalloc( sizeof(*com) ); + com->node=command; + *last=com;/* this sets the last's `next' */ + last=&com->next; + } fclose( file ); xchdir(".."); return plugin; } -struct plugin_tree * +/* load all the plugins in libexecdir, using the config file plugin_conf */ +struct tree_list * load_plugins (const char *libexecdir, const char *plugin_conf) { - struct plugin_tree *tree=(struct plugin_tree *)xmalloc(sizeof(*tree)); - struct plugin **last=&tree->plugins; - *last=NULL; - struct plugin *plugin; + struct tree_list *first=NULL; + struct tree_list **last=&first; + struct tree_list *plugin; xchdir(libexecdir); + /* Yeah, I know this next bit is pretty ugly. */ DIR *cwd; struct dirent *dirent; int serr; @@ -119,7 +95,7 @@ load_plugins (const char *libexecdir, const char *plugin_conf) closedir (cwd); xchdir(".."); - return tree; + return first; } #endif diff --git a/wrapper/plugin.h b/wrapper/plugin.h index 5273866..825c8d2 100644 --- a/wrapper/plugin.h +++ b/wrapper/plugin.h @@ -21,50 +21,31 @@ #define FILE_plugin_h_SEEN #include "rvs.h" +#include "structures.h" -struct plugin -{ - char *name; - - struct plugin_command *child; - struct plugin *next; -}; - +/* a single plugin command, for use as *node in binary trees */ 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 tree_list *plugin; /* which plugin does it belong to? */ 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) */ }; +/* create a blank plugin_command */ struct plugin_command * mkcommand() { 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; - command->d_next=NULL; + return command; } -struct plugin_tree -{ - struct plugin *plugins; - struct plugin_command *depends; -}; - #endif diff --git a/wrapper/rvs.c b/wrapper/rvs.c index 7063cce..2a2b71c 100644 --- a/wrapper/rvs.c +++ b/wrapper/rvs.c @@ -22,6 +22,7 @@ const char *ver="0.8c"; #include "rvs.h" #include "plugin.c" +#include "debug.h" const char *libexecdir="./plugins"; const char *plugin_conf="plugin.conf"; @@ -29,7 +30,8 @@ const char *plugin_conf="plugin.conf"; int main ( int argc, char *argv[] ) { - struct plugin_tree *plugins=load_plugins(libexecdir,plugin_conf); + struct tree_list *plugins=load_plugins(libexecdir,plugin_conf); + _plugin_print_plugin(plugins); xfree(plugins); return 0; } diff --git a/wrapper/rvs.h b/wrapper/rvs.h index 8268613..6b4e9ee 100644 --- a/wrapper/rvs.h +++ b/wrapper/rvs.h @@ -30,32 +30,34 @@ extern char *program_invocation_name; -void *xmalloc (size_t size) +void * +xmalloc (size_t size) { void *value = malloc (size); if (value == NULL) perror(program_invocation_name); exit(EXIT_FAILURE); - /*error(EXIT_FAILURE,0,"virtual memory exhausted");*/ return value; } -void *xrealloc (void *ptr, size_t size) +void * +xrealloc (void *ptr, size_t size) { void *value = realloc (ptr, size); if (value == NULL) perror(program_invocation_name); exit(EXIT_FAILURE); - /*error(EXIT_FAILURE,0,"virtual memory exhausted");*/ return value; } -void xfree (void *ptr) +void +xfree (void *ptr) { free (ptr); } -int xchdir (const char *filename) +int +xchdir (const char *filename) { int ret=chdir(filename); if (ret != 0) { @@ -64,7 +66,8 @@ int xchdir (const char *filename) return ret; } -FILE *xfopen (const char *filename, const char *opentype) +FILE * +xfopen (const char *filename, const char *opentype) { FILE *file = fopen(filename,opentype); /* fopen gives NULL on failure */ @@ -75,7 +78,8 @@ FILE *xfopen (const char *filename, const char *opentype) return file; } -DIR *xopendir (const char *dirname) +DIR * +xopendir (const char *dirname) { DIR *dir = opendir (dirname); if (dir == NULL) { @@ -85,7 +89,8 @@ DIR *xopendir (const char *dirname) return dir; } -void stradds(size_t *size, char **dest, char *str) +void +stradds(size_t *size, char **dest, char *str) { if (*size > ( strlen(*dest) + strlen(str) )) { strcat(*dest, str); @@ -97,5 +102,13 @@ void stradds(size_t *size, char **dest, char *str) } } +char * +stralloc (const char *string) +{ + char *copy = (char *)xmalloc(strlen(string)+1); + strcpy(copy,string); + return copy; +} + #endif |