summaryrefslogtreecommitdiff
path: root/wrapper
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2009-09-07 22:37:12 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2015-06-26 00:30:14 -0600
commited30436305ed7a7fe6170d90a453a9d04faecf52 (patch)
treedaad9ff049fcddb5a209a9b1eb7f1de6636b51d7 /wrapper
parente2e63f81e5ac06ecf5461cc0a8fd68077707de5d (diff)
I think I have it back to where it was last week (although, a lot cleaner, and elagant)
Diffstat (limited to 'wrapper')
-rw-r--r--wrapper/plugin-parse.c8
-rw-r--r--wrapper/plugin.c53
-rw-r--r--wrapper/plugin.h93
-rw-r--r--wrapper/rvs.c8
-rw-r--r--wrapper/rvs.h14
5 files changed, 126 insertions, 50 deletions
diff --git a/wrapper/plugin-parse.c b/wrapper/plugin-parse.c
index 8560dfd..800098f 100644
--- a/wrapper/plugin-parse.c
+++ b/wrapper/plugin-parse.c
@@ -101,11 +101,12 @@ _plugin_parse (FILE *file)
char c[2] = " \0";
char *cs = (char *)&c;
- if ( (c[0]=getc(file)) == EOF ) {
+
+ if ( (c[0]=getc(file)) == EOF )
return NULL;
- } else {
+ else
ungetc (c[0], file);
- }
+
while ( (c[0]=getc(file)) != EOF ) {
if (c[0] == '\n') {
if (strlen(string)==0) {
@@ -114,6 +115,7 @@ _plugin_parse (FILE *file)
command=_plugin_parse(file);
} else {
command->name=string;
+ command->next=_plugin_parse(file);
}
break;
} else {
diff --git a/wrapper/plugin.c b/wrapper/plugin.c
index 6a38035..6301bbb 100644
--- a/wrapper/plugin.c
+++ b/wrapper/plugin.c
@@ -20,58 +20,43 @@
#ifndef FILE_plugin_c_SEEN
#define FILE_plugin_c_SEEN
-#include <stdlib.h> /* EXIT_FAILURE */
-#include <unistd.h> /* file acces */
-#include <string.h>
-
-#include <dirent.h>
-#include <sys/stat.h>
-
#include "rvs.h"
#include "plugin.h"
#include "plugin-parse.c"
-/*#include "plugin-depend.c"*/
+
+/* directory listing for load_plugins */
+/* there seriously has to be a better way to do this */
+#include <string.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <sys/stat.h>
/* assume we're in rvs's libexecdir, where plugins are
now, load plugin plug_name,
- whose configuration file is plugin_conf
-*/
-struct tree_list *
+ whose configuration file is plugin_conf */
+struct plugin *
_plugin_load (const char *plug_name, const char *plugin_conf)
{
- struct tree_list *plugin=xmalloc( sizeof(*plugin) );
- plugin->node=stralloc(plug_name);
+ struct plugin *plugin=xmalloc( sizeof(*plugin) );
+ plugin->name=stralloc(plug_name);
plugin->next=NULL;
- plugin->child=NULL;
-
xchdir(plug_name);
FILE *file = xfopen(plugin_conf,"r");
-
- 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;
- }
-
+ plugin->commands=_plugin_parse(file);
fclose( file );
+
xchdir("..");
return plugin;
}
/* load all the plugins in libexecdir, using the config file plugin_conf */
-struct tree_list *
+struct plugin *
load_plugins (const char *libexecdir, const char *plugin_conf)
{
- struct tree_list *first=NULL;
- struct tree_list **last=&first;
- struct tree_list *plugin;
+ struct plugin *first=NULL; /* we return this */
+ struct plugin **prev=&first; /* this is the previous plugin->next */
+ struct plugin *plugin; /* this is the current plugin */
xchdir(libexecdir);
@@ -87,8 +72,8 @@ load_plugins (const char *libexecdir, const char *plugin_conf)
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;
+ *prev=plugin;/* this sets the last's `next' */
+ prev=&plugin->next;
}
}
}
diff --git a/wrapper/plugin.h b/wrapper/plugin.h
index 825c8d2..4672895 100644
--- a/wrapper/plugin.h
+++ b/wrapper/plugin.h
@@ -21,15 +21,27 @@
#define FILE_plugin_h_SEEN
#include "rvs.h"
-#include "structures.h"
-/* a single plugin command, for use as *node in binary trees */
+/* a plugin */
+/* a generic binary tree emulating a n-ary tree via linked list */
+struct plugin
+{
+ char *name;
+ struct plugin_command *commands;/* left leaf */
+ struct plugin *next; /* right leaf */
+};
+
+/* a single plugin command */
struct plugin_command
{
+ /* node */
char *name;
- struct tree_list *plugin; /* which plugin does it belong to? */
+ struct plugin *plugin; /* which plugin does it belong to? */
struct plugin_command *depend; /* what does this depend on? */
- char *depends;/* what does this depend on? */
+ char *depends;/* what does this depend on? (string) */
+
+ /* linked list */
+ struct plugin_command *next;
};
/* create a blank plugin_command */
@@ -39,13 +51,86 @@ mkcommand()
struct plugin_command *command;
command=(struct plugin_command *)xmalloc(sizeof(*command));
+ /* node */
command->name=NULL;
command->plugin=NULL;
command->depend=NULL;
command->depends=NULL;
+ /* linked list */
+ command->next=NULL;
+
return command;
}
+
+struct plugin *
+plugin_find_plugin(struct plugin *node, char *name)
+{
+
+ if (node==NULL)
+ return node;
+ else {
+ if (strcmp(node->name,name) == 0)
+ return node;
+ else
+ return plugin_find_plugin(node->next,name);
+ }
+}
+
+struct plugin_command *
+plugin_find_plugin_command(struct plugin_command *node, char *name)
+{
+ if (node==NULL)
+ return node;
+ else {
+ if (strcmp(node->name,name) == 0)
+ return node;
+ else
+ return plugin_find_plugin_command(node->next,name);
+ }
+}
+
+/* returns the first command with `name' it finds
+ Perhaps it should return a linked list of all commands with `name'?
+ This would require a dedicated struct for the function.
+ That makes me kind of want to move it to another file. */
+struct plugin_command *
+plugin_find_command(struct plugin *plugin, char *name)
+{
+ struct plugin_command *ret;
+ ret=plugin_find_plugin_command(plugin->commands,name);
+ if (ret == NULL)
+ ret=plugin_find_command(plugin->next,name);
+ return ret;
+}
+
+void
+plugin_free_command(struct plugin_command *command)
+{
+ if (command!=NULL) {
+ #ifdef DEBUG
+ printf(" + %s\n",command->name);
+ #endif
+ xfree(command->name);
+ xfree(command->depends);
+ xfree(command);
+ plugin_free_command(command->next);
+ }
+}
+
+void
+plugin_free_plugin(struct plugin *plugin)
+{
+ if (plugin!=NULL) {
+ #ifdef DEBUG
+ printf(" - %s\n",plugin->name);
+ #endif
+ xfree(plugin->name);
+ plugin_free_command(plugin->commands);
+ plugin_free_plugin(plugin->next);
+ }
+}
+
#endif
diff --git a/wrapper/rvs.c b/wrapper/rvs.c
index 2a2b71c..9125ff0 100644
--- a/wrapper/rvs.c
+++ b/wrapper/rvs.c
@@ -20,9 +20,10 @@ const char *ver="0.8c";
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
+/*#define DEBUG*/
#include "rvs.h"
#include "plugin.c"
-#include "debug.h"
+/*#include "debug.h"*/
const char *libexecdir="./plugins";
const char *plugin_conf="plugin.conf";
@@ -30,9 +31,8 @@ const char *plugin_conf="plugin.conf";
int
main ( int argc, char *argv[] )
{
- struct tree_list *plugins=load_plugins(libexecdir,plugin_conf);
- _plugin_print_plugin(plugins);
- xfree(plugins);
+ struct plugin *plugins=load_plugins(libexecdir,plugin_conf);
+ plugin_free_plugin(plugins);
return 0;
}
diff --git a/wrapper/rvs.h b/wrapper/rvs.h
index 6b4e9ee..447b533 100644
--- a/wrapper/rvs.h
+++ b/wrapper/rvs.h
@@ -34,9 +34,10 @@ void *
xmalloc (size_t size)
{
void *value = malloc (size);
- if (value == NULL)
+ if (value == NULL) {
perror(program_invocation_name);
exit(EXIT_FAILURE);
+ }
return value;
}
@@ -44,9 +45,10 @@ void *
xrealloc (void *ptr, size_t size)
{
void *value = realloc (ptr, size);
- if (value == NULL)
+ if (value == NULL) {
perror(program_invocation_name);
exit(EXIT_FAILURE);
+ }
return value;
}
@@ -59,6 +61,9 @@ xfree (void *ptr)
int
xchdir (const char *filename)
{
+ #ifdef DEBUG
+ puts (filename);
+ #endif
int ret=chdir(filename);
if (ret != 0) {
error(EXIT_FAILURE,errno,"%s/",filename);
@@ -92,11 +97,10 @@ xopendir (const char *dirname)
void
stradds(size_t *size, char **dest, char *str)
{
- if (*size > ( strlen(*dest) + strlen(str) )) {
+ if (*size > ( strlen(*dest) + strlen(str) ))
strcat(*dest, str);
- } else {
+ else {
*size = strlen(*dest) + strlen(str) + 1;
-
*dest = (char *) xrealloc (*dest, *size);
strcat(*dest, str);
}