[Frugalware-darcs] setup-current: initial import of new core

VMiklos vmiklos at frugalware.org
Thu Mar 16 17:53:11 CET 2006


[initial import of new core
VMiklos <vmiklos at frugalware.org>**20051101232328] {
adddir ./src/plugins
addfile ./src/Makefile
hunk ./src/Makefile 1
+CFLAGS ?= -Wall -march=i686 -O2 -pipe
+LDFLAGS = -ldl
+CFLAGS += $(shell pkg-config --cflags glib-2.0)
+LDFLAGS += $(shell pkg-config --libs glib-2.0)
+
+all: setup
+	$(MAKE) -C plugins
+
+setup: setup.o
+	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+
+clean:
+	rm -f setup *.o
+	$(MAKE) -C plugins clean
addfile ./src/plugins/Makefile
hunk ./src/plugins/Makefile 1
+CFLAGS ?= -Wall -march=i686 -O2 -pipe
+CFLAGS += -I..
+
+PLUGINS=foo bar
+
+all: $(PLUGINS)
+
+foo: foo.o
+	$(CC) -shared -Wl,-soname,foo.so -o foo.so foo.o
+	
+bar: bar.o
+	$(CC) -shared -Wl,-soname,bar.so -o bar.so bar.o
+
+clean:
+	rm -f $(PLUGINS) *.o
addfile ./src/plugins/bar.c
hunk ./src/plugins/bar.c 1
+#include <stdio.h>
+
+#include <setup.h>
+#include "bar.h"
+
+plugin_t plugin =
+{
+	"bar",
+	main,
+	NULL // dlopen handle
+};
+
+plugin_t *info()
+{
+	return &plugin;
+}
+
+int main(void)
+{
+	printf("bar\n");
+	return(0);
+}
addfile ./src/plugins/bar.h
hunk ./src/plugins/bar.h 1
+plugin_t *info();
+int main(void);
addfile ./src/plugins/foo.c
hunk ./src/plugins/foo.c 1
+#include <stdio.h>
+
+#include <setup.h>
+#include "foo.h"
+
+plugin_t plugin =
+{
+	"foo",
+	main,
+	NULL // dlopen handle
+};
+
+plugin_t *info()
+{
+	return &plugin;
+}
+
+int main(void)
+{
+	printf("foo\n");
+	return(0);
+}
addfile ./src/plugins/foo.h
hunk ./src/plugins/foo.h 1
+plugin_t *info();
+int main(void);
addfile ./src/setup.c
hunk ./src/setup.c 1
+#include <stdio.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <glib.h>
+#include <dlfcn.h>
+
+#include "setup.h"
+
+GList *plugin_list;
+
+int add_plugin(char *filename)
+{
+	void *handle;
+	void *(*infop) (void);
+
+	if ((handle = dlopen(filename, RTLD_NOW)) == NULL)
+	{
+		fprintf(stderr, "%s\n", dlerror());
+		return(1);
+	}
+	
+	if ((infop = dlsym(handle, "info")) == NULL)
+	{
+		fprintf(stderr, "%s\n", dlerror());
+		return(1);
+	}
+	plugin_t *plugin = infop();
+	plugin->handle = handle;
+	plugin_list = g_list_append(plugin_list, plugin);
+
+	return(0);
+}
+
+int init_plugins(char *dirname)
+{
+	char *filename, *ext;
+	DIR *dir;
+	struct dirent *ent;
+	struct stat statbuf;
+
+	dir = opendir(dirname);
+	if (!dir)
+	{
+		perror(dirname);
+		return(1);
+	}
+	while ((ent = readdir(dir)) != NULL)
+	{
+		filename = g_strdup_printf("%s/%s", dirname, ent->d_name);
+		if (!stat(filename, &statbuf) && S_ISREG(statbuf.st_mode) &&
+				(ext = strrchr(ent->d_name, '.')) != NULL)
+			if (!strcmp(ext, SHARED_LIB_EXT))
+				add_plugin(filename);
+	}
+	closedir(dir);
+	return(0);
+}
+
+int cleanup_plugins()
+{
+	int i;
+	plugin_t *plugin;
+
+	for (i=0; i<g_list_length(plugin_list); i++)
+	{
+		plugin = g_list_nth_data(plugin_list, i);
+		dlclose(plugin->handle);
+	}
+	return(0);
+}
+
+int main()
+{
+	int i;
+	plugin_t *plugin;
+	
+	init_plugins(PLUGDIR);
+	for (i=0; i<g_list_length(plugin_list); i++)
+	{
+		plugin = g_list_nth_data(plugin_list, i);
+		plugin->main();
+	}
+	cleanup_plugins();
+	return(0);
+}
addfile ./src/setup.h
hunk ./src/setup.h 1
+#define PLUGDIR "plugins"
+
+#define SHARED_LIB_EXT ".so"
+
+typedef struct
+{
+	char *name;
+	int (*main)(void);
+	void *handle;
+} plugin_t;
}


More information about the Frugalware-darcs mailing list