summaryrefslogtreecommitdiff
path: root/c/rvs.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/rvs.c')
-rw-r--r--c/rvs.c214
1 files changed, 214 insertions, 0 deletions
diff --git a/c/rvs.c b/c/rvs.c
new file mode 100644
index 0000000..6c43a79
--- /dev/null
+++ b/c/rvs.c
@@ -0,0 +1,214 @@
+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 <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+#include <error.h>
+
+const char *libexecdir="./plugins";
+const char *plugin_conf="plugin.conf";
+
+const size_t strgran=10;
+
+void *xmalloc (size_t size)
+{
+ register void *value = malloc (size);
+ if (value == 0)
+ error(EXIT_FAILURE,0,"virtual memory exhausted");
+ return value;
+}
+
+void *xrealloc (void *ptr, size_t size)
+{
+ register void *value = realloc (ptr, size);
+ if (value == 0)
+ error(EXIT_FAILURE,0,"virtual memory exhausted");
+ return value;
+}
+
+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;
+
+ char *string;
+ string = (char *) xrealloc (*dest, *size);
+ strcat(string, str);
+ }
+}
+
+void load_plugin_comment (FILE *file)
+{
+ char c;
+ while ( (c=getc(file)) != EOF ) {
+ if ( c == '\n' ) {
+ ungetc (c, file);
+ break;
+ }
+ }
+}
+
+int load_plugin_command (FILE *file);
+
+int load_plugin_depend (FILE *file)
+{
+ int nbytes = 10;
+ char *string = (char *) malloc(nbytes);
+
+ char c[2] = " \0";
+ char *cs = &c;
+ while ( (c[0]=getc(file)) != EOF ) {
+ if (c[0] == '\n') {
+ printf("%s | ",string);
+ ungetc (c[0], file);
+ break;
+ } else {
+ switch (c[0]) {
+ case '\\':
+ c[0]=getc(file);
+ switch (c[0]) {
+ case 'n':
+ c[0] = '\n';
+ case '\\':
+ case '#':
+ stradds(&nbytes,&string,cs);
+ break;
+ default:
+ error(EXIT_FAILURE,0,"syntax error");
+ break;
+ }
+ break;
+ case '#':
+ load_plugin_comment(file);
+ break;
+ default:
+ stradds(&nbytes,&string,cs);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+int load_plugin_command (FILE *file)
+{
+ int nbytes = 10;
+ char *string = (char *) malloc(nbytes);
+
+ char c[2] = " \0";
+ char *cs = &c;
+ while ( (c[0]=getc(file)) != EOF ) {
+ if (c[0] == '\n') {
+ printf("%s\n",string);
+ load_plugin_command(file);
+ break;
+ } else {
+ switch (c[0]) {
+ case '\\':
+ c[0]=getc(file);
+ switch (c[0]) {
+ case 'n':
+ c[0] = '\n';
+ case '\\':
+ case '#':
+ case ':':
+ stradds(&nbytes,&string,cs);
+ break;
+ default:
+ error(EXIT_FAILURE,0,"syntax error");
+ break;
+ }
+ break;
+ case '#':
+ load_plugin_comment(file);
+ break;
+ case ':':
+ load_plugin_depend(file);
+ break;
+ default:
+ stradds(&nbytes,&string,cs);
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+int load_plugin (char *plug_name)
+{
+ printf("loading plugin `%s'\n",plug_name);
+
+ chdir(plug_name);
+ FILE *file = fopen(plugin_conf,"r");
+
+ /* fopen gives NULL on failure */
+ if ( file == NULL ) {
+ error(EXIT_FAILURE,errno,
+ "%s/%s/%s",libexecdir,plug_name,plugin_conf);
+ } else {
+ load_plugin_command(file);
+ }
+ fclose( file );
+ chdir("..");
+ return 0;
+}
+
+int load_plugins ()
+{
+ chdir(libexecdir);
+
+ DIR *dp;
+ struct dirent *ep;
+ int serr;
+ struct stat sbuf;
+ dp = opendir ("./");
+ if (dp != NULL) {
+ while (ep = readdir (dp)) {
+ if ((strcmp(ep->d_name,"." )!=0)&&
+ (strcmp(ep->d_name,"..")!=0)) {
+ serr = stat(ep->d_name, &sbuf);
+ if (!serr && S_ISDIR(sbuf.st_mode)) {
+ load_plugin(ep->d_name);
+ }
+ }
+ }
+ (void) closedir (dp);
+ }
+ else
+ error(EXIT_FAILURE,errno,"%s/",libexecdir);
+ return 0;
+}
+
+int main (void)
+{
+ load_plugins();
+ return 0;
+}
+