r1627 - in dev/concept/moongoo: . libalfs syntax

neocool at linuxfromscratch.org neocool at linuxfromscratch.org
Fri Dec 3 16:08:22 PST 2004


Author: neocool
Date: 2004-12-03 17:08:21 -0700 (Fri, 03 Dec 2004)
New Revision: 1627

Added:
   dev/concept/moongoo/inc.mak
   dev/concept/moongoo/libalfs/Makefile
   dev/concept/moongoo/plugin.c
   dev/concept/moongoo/plugin.h
Modified:
   dev/concept/moongoo/Makefile
   dev/concept/moongoo/libalfs/repl.h
   dev/concept/moongoo/libalfs/util.c
   dev/concept/moongoo/libalfs/util.h
   dev/concept/moongoo/moongoo.c
   dev/concept/moongoo/syntax/Makefile
   dev/concept/moongoo/syntax/ass.c
   dev/concept/moongoo/syntax/book.c
   dev/concept/moongoo/syntax/nalfs.c
   dev/concept/moongoo/syntax/sample.c
   dev/concept/moongoo/syntax/syn.c
Log:
* Restructuring done
* Plugin interface designed and implemented
* Converted all parsers to plugins
* Some mirror stuff


Modified: dev/concept/moongoo/Makefile
===================================================================
--- dev/concept/moongoo/Makefile	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/Makefile	2004-12-04 00:08:21 UTC (rev 1627)
@@ -1,3 +1,6 @@
+CFLAGS=-I. -Ilibalfs
+LDLIBS=-Llibalfs -lalfs
+
 BIN=moongoo
 OBJS=moongoo.o build.o plugin.o
 
@@ -3,7 +6,7 @@
 ifeq ($(OPTS),)
 OPTS=
-OPTS+=~/projects/alfs/book/index.xml
+#OPTS+=~/projects/alfs/book/index.xml
 #OPTS+=~/projects/alfs/lfs-profile/LFS.xml
-#OPTS+=~/projects/alfs/ass/index.xml
+OPTS+=~/projects/alfs/ass/index.xml
 #OPTS+=~/projects/hive/unstable/profile.xml
 endif
@@ -16,3 +19,8 @@
 all: all-subdirs run
 
 include inc.mak
+
+$(BIN): $(OBJS)
+
+run: $(BIN)
+	./$(BIN) $(OPTS)

Added: dev/concept/moongoo/inc.mak
===================================================================
--- dev/concept/moongoo/inc.mak	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/inc.mak	2004-12-04 00:08:21 UTC (rev 1627)
@@ -0,0 +1,27 @@
+CC=gcc
+CFLAGS+=-std=c99 -D_BSD_SOURCE
+CFLAGS+=-Wall -Werror -g3
+
+CFLAGS+=`xml2-config --cflags`
+LDLIBS+=`xml2-config --libs` -ldl
+
+.PHONY: clean todo all-subdirs clean-subdirs
+
+all-subdirs:
+	@for dir in $(SUBDIRS) ; do \
+		(cd $$dir && $(MAKE) $(MARGS) all) || exit 1; \
+	done
+
+clean-subdirs:
+	@for dir in $(SUBDIRS) ; do \
+	    (cd $$dir && $(MAKE) $(MARGS) clean) || exit 1; \
+	done
+
+clean: clean-subdirs
+	rm -f $(BIN) $(LIB) $(LIBS) *.o
+	
+todo:
+	@grep "TODO:" *.c *.h|cut -d" " -f3-|sort
+
+%.so: %.o
+	$(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

Added: dev/concept/moongoo/libalfs/Makefile
===================================================================
--- dev/concept/moongoo/libalfs/Makefile	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/libalfs/Makefile	2004-12-04 00:08:21 UTC (rev 1627)
@@ -0,0 +1,12 @@
+CFLAGS=-I.
+
+LIB=libalfs.a
+OBJS=alfs.o devel.o repl.o util.o
+
+all: $(LIB)
+
+include ../inc.mak
+
+$(LIB): $(OBJS)
+	ar crv $@ $^
+	ranlib $@

Modified: dev/concept/moongoo/libalfs/repl.h
===================================================================
--- dev/concept/moongoo/libalfs/repl.h	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/libalfs/repl.h	2004-12-04 00:08:21 UTC (rev 1627)
@@ -3,7 +3,8 @@
 
 #include <alfs.h>
 
-#define MOO_XML		"/.nALFS/answers.xml"
+// TODO: Make moo.xml configurable
+#define MOO_XML		"moo.xml"
 
 void t_repl (xmlNodePtr node, void *data);
 replaceable *init_repl (char *fname);

Modified: dev/concept/moongoo/libalfs/util.c
===================================================================
--- dev/concept/moongoo/libalfs/util.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/libalfs/util.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -54,6 +54,20 @@
 	return ret;
 }
 
+char *strdog_path (char *str1, char *str2)
+{
+	char *ret, *tmp;
+	
+	if ((!str1)||(!str2))
+		return NULL;
+	
+	tmp = strdog(str1, "/");
+	ret = strdog(tmp, str2);
+
+	free(tmp);
+	return ret;
+}
+
 char *chrep (char *str, char old, char new)
 {
 	char *ret;

Modified: dev/concept/moongoo/libalfs/util.h
===================================================================
--- dev/concept/moongoo/libalfs/util.h	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/libalfs/util.h	2004-12-04 00:08:21 UTC (rev 1627)
@@ -41,5 +41,6 @@
 char *strkill (char *str, char *tokill);
 char *strnstr (char *haystack, char *needle, int n);
 char *cut_trail (char *str, char *delim);
+char *strdog_path (char *str1, char *str2);
 
 #endif

Modified: dev/concept/moongoo/moongoo.c
===================================================================
--- dev/concept/moongoo/moongoo.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/moongoo.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -6,7 +6,10 @@
 #include <repl.h>
 #include <util.h>
 #include <build.h>
+#include <plugin.h>
 
+#define	VERSION		"0.0.2"
+
 role default_filter[4] = { NOEXECUTE, INTERACTIVE, TESTSUITE, 0 };
 char *paralell_filter[4] = { "configure-host", "clean", "mrproper", NULL };
 char *popt_pkg[2] = { "Glibc-20041115", NULL };
@@ -16,8 +19,10 @@
 {
 	char c, *syn = NULL, *moo_xml = NULL;
 	bool quiet = false;
+	int i = 0;
 	xmlNodePtr cur;
-	//profile *prof;
+	plug_info *plugin;
+	profile *prof = NULL;
 	
 	if (argc<2)
 	{
@@ -25,6 +30,14 @@
 		return 1;
 	}
 
+	plugin = plugscan("syntax");
+
+	if (!plugin)
+	{
+		fprintf(stderr, "No syntax plugins found.\n");
+		return 2;
+	}
+
 	while ((c = getopt(argc, argv, "s:qVhc:")) != EOF)
 	{
 		switch (c)
@@ -33,10 +46,15 @@
 				if (!strcmp(optarg, "help"))
 				{
 					printf("Available syntaxes:\n");
-					printf("\tbook\t\tBook as profile (default)\n");
-					printf("\tnalfs\t\tnALFS legacy syntax\n");
-					printf("\tass\t\tALFS simple syntax\n");
-					printf("\tsyn\t\tHive profile syntax\n");
+					while (plugin[i].path)
+					{
+						char *tmp = plugarg(plugin[i].path);
+						if (strcmp(tmp, "sample"))
+							printf("\t%s\t\t%s%s\n", tmp, 
+								plugin[i].info->name, ((i==0) 
+								? " (default)" : ""));
+						i++;
+					}
 					return 0;
 				}
 				syn = (char *)malloc(strlen(optarg)+1);
@@ -46,7 +64,7 @@
 				quiet = true;
 				break;
 			case 'V':
-				printf("moongoo 0.0.1\nWritten by Boris Buegling\n");
+				printf("moongoo %s\nWritten by Boris Buegling\n", VERSION);
 				return 0;
 			case 'h':
 				printf("moongoo [OPTIONS] BOOK\n");
@@ -70,31 +88,33 @@
 	xmlXIncludeProcessFlags(doc, XML_PARSE_NOENT);
 	cur=xmlDocGetRootElement(doc);
 	
-	/*if ((!syn)||(!strcmp(syn, "book")))
-		prof=bookasprofile(cur);
+	if (syn)
+	{
+		while (plugin[i].path)
+		{
+			if (!strcmp(syn, plugarg(plugin[i].path)))
+				prof = plugin[i].info->parse(cur);
+			i++;
+		}
+	}
 	else
-	if (!strcmp(syn, "syn"))
-		prof=syn_profile(cur);
-	else
-	if (!strcmp(syn, "nalfs"))
-		prof=nalfs_profile(cur);
-	else
-	if (!strcmp(syn, "ass"))
-		prof=ass_profile(cur);
-	else
+		prof = plugin[0].info->parse(cur);
+	
+	if (!prof)
 	{
-		fprintf(stderr, "Syntax '%s' is not valid.\n", syn);
+		fprintf(stderr, "Document was not parsed correctly.\n");
+		xmlFreeDoc(doc);
 		return 1;
 	}
-
-	if ((prof) && (!quiet))
+	
+	if (!quiet)
 	{
 		build_paralell (prof, paralell_filter, popt_pkg, popt_cmd);
 		set_filter(default_filter);
-		print_pkg(*search_pkg(prof, "Glibc-20041115", 
-			"chapter-building-system"));
-		//print_profile(*prof);
-	}*/
+		/*print_pkg(*search_pkg(prof, "Glibc-20041115", 
+			"chapter-building-system"));*/
+		print_profile(*prof);
+	}
 	
 	xmlFreeDoc(doc);
 	return 0;

Added: dev/concept/moongoo/plugin.c
===================================================================
--- dev/concept/moongoo/plugin.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/plugin.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -0,0 +1,118 @@
+#include <dlfcn.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <string.h>
+#include <errno.h>
+#include <libgen.h>
+
+#include <util.h>
+#include <plugin.h>
+
+/* The plugin code is based on hyperplayer by James Lee (jbit) */
+
+plug_info *plugscan (char *dir)
+{
+	DIR *d;
+	struct dirent *ent;
+	plug_info *ret = NULL;
+	int num = 0;
+
+	d = opendir(dir);
+	if (!d)
+	{
+		perror(dir);
+		return NULL;
+	}
+
+	ent = readdir(d);
+	while (ent)
+	{
+		if (!strcmp(&ent->d_name[strlen(ent->d_name)-strlen(PLUG_EXT)],
+			PLUG_EXT))
+		{
+			char *path = strdog_path(dir, ent->d_name);
+			plug_info *moo = pluginfo(path);
+			
+			if (moo)
+			{
+				ret = realloc(ret, (++num)*sizeof(plug_info));
+				ret[num-1] = *pluginfo(path);
+				ret[num-1].path = path;
+			}
+			else
+				free(path);
+		}
+
+		ent = readdir(d);
+	}
+
+	ret = realloc(ret, (++num)*sizeof(plug_info));
+	ret[num-1].path = NULL;
+
+	closedir(d);
+	return ret;
+}
+
+plug_info *pluginfo (char *fname)
+{
+	plug_info *ret = (plug_info *)malloc(sizeof(plug_info));
+	plug_hand *hand = plugload(fname);
+	
+	if (!hand)
+		return NULL;
+	
+	ret->info = hand->getplug();
+	if (!ret->info)
+	{
+		fprintf(stderr, "No plugin info in %s.\n", fname);
+		dlclose(hand);
+		return NULL;
+	}
+
+	dlclose(hand);
+	return ret;
+}
+
+plug_hand *plugload (char *fname)
+{
+	plug_hand *ret = (plug_hand *)malloc(sizeof(plug_hand));
+	
+	ret->hand = dlopen(fname, RTLD_NOW);
+	if (!ret->hand)
+	{
+		plugerr(fname);
+		return NULL;
+	}
+
+	ret->getplug = dlsym(ret->hand, "getplug");
+	if (!ret->getplug)
+	{
+		plugerr(fname);
+		return NULL;
+	}
+	
+	return ret;
+}
+
+void plugerr (char *fname)
+{
+	char *msg = dlerror();
+	
+	if (msg)
+	{
+		msg = strstr(msg, ":");
+		msg+=2;
+		fprintf(stderr, "%s: %s\n", fname, msg);
+	}
+}
+
+void print_plug (plug_info plug)
+{
+	printf("Plugin: %s - Version: %d\n", plug.info->name, plug.info->vers);
+	printf("Location: %s (%s)\n", plug.path, plugarg(plug.path));
+}
+
+char *plugarg (char *path)
+{
+	return strkill(basename(path), "." PLUG_EXT);
+}

Added: dev/concept/moongoo/plugin.h
===================================================================
--- dev/concept/moongoo/plugin.h	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/plugin.h	2004-12-04 00:08:21 UTC (rev 1627)
@@ -0,0 +1,34 @@
+#ifndef __PLUGIN_H__
+#define __PLUGIN_H__
+
+#include <alfs.h>
+
+#define PLUG_EXT	"so"
+
+typedef struct
+{
+	char *name;
+	int vers;
+	profile *(* parse) (xmlNodePtr node);
+} t_plug;
+
+typedef struct
+{
+	t_plug *info;
+	char *path;
+} plug_info;
+
+typedef struct
+{
+	void *hand;
+	t_plug *(*getplug)(void);
+} plug_hand;
+
+plug_info *plugscan (char *dir);
+plug_info *pluginfo (char *fname);
+plug_hand *plugload (char *fname);
+void plugerr (char *fname);
+void print_plug (plug_info plug);
+char *plugarg (char *path);
+
+#endif

Modified: dev/concept/moongoo/syntax/Makefile
===================================================================
--- dev/concept/moongoo/syntax/Makefile	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/syntax/Makefile	2004-12-04 00:08:21 UTC (rev 1627)
@@ -1,3 +1,8 @@
-LIBS=
+CFLAGS=-I../libalfs -I..
+LDLIBS=-L../libalfs -lalfs
 
-all: $(LIB)
+LIBS=$(patsubst %.c,%.so,$(wildcard *.c))
+
+all: $(LIBS)
+
+include ../inc.mak

Modified: dev/concept/moongoo/syntax/ass.c
===================================================================
--- dev/concept/moongoo/syntax/ass.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/syntax/ass.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -2,9 +2,24 @@
 
 #include <alfs.h>
 #include <util.h>
+#include <plugin.h>
 
 profile *prof;
 
+profile *ass_profile (xmlNodePtr node);
+
+static t_plug ass_plugin =
+{
+	name:	"ALFS simple syntax",
+	vers:	1,
+	parse:	ass_profile
+};
+
+t_plug *getplug ()
+{
+	return &ass_plugin;
+}
+
 // TODO: Support download/directory in the ASS parser
 void process_cmd3 (char *line, xmlNodePtr node)
 {

Modified: dev/concept/moongoo/syntax/book.c
===================================================================
--- dev/concept/moongoo/syntax/book.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/syntax/book.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -4,6 +4,7 @@
 #include <alfs.h>
 #include <repl.h>
 #include <util.h>
+#include <plugin.h>
 
 command *cmd;
 int num;
@@ -15,7 +16,20 @@
 void __t_userinput (xmlNodePtr node, void *data);
 command *t_userinput (xmlNodePtr node, int *n);
 void t_chapter (xmlNodePtr node);
+profile *bookasprofile (xmlNodePtr node);
 
+static t_plug book_plugin =
+{
+	name:	"Book as profile",
+	vers:	1,
+	parse:	bookasprofile
+};
+
+t_plug *getplug ()
+{
+	return &book_plugin;
+}
+
 void t_sect1 (xmlNodePtr node, void *data)
 {
 	int i, j;

Modified: dev/concept/moongoo/syntax/nalfs.c
===================================================================
--- dev/concept/moongoo/syntax/nalfs.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/syntax/nalfs.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -3,6 +3,7 @@
 #include <alfs.h>
 #include <devel.h>
 #include <util.h>
+#include <plugin.h>
 
 // TODO: Having a big static buffer sucks
 #define BUF_LEN		3000
@@ -10,6 +11,20 @@
 profile *prof;
 char commando[BUF_LEN];
 
+profile *nalfs_profile (xmlNodePtr node);
+
+static t_plug nalfs_plugin =
+{
+	name:	"nALFS legacy syntax",
+	vers:	1,
+	parse:	nalfs_profile
+};
+
+t_plug *getplug ()
+{
+	return &nalfs_plugin;
+}
+
 void parse_unpack (xmlNodePtr node)
 {
 	node=node->children;

Modified: dev/concept/moongoo/syntax/sample.c
===================================================================
--- dev/concept/moongoo/syntax/sample.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/syntax/sample.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -1,7 +1,30 @@
+/* Moongoo plugin API example
+ *
+ * Put your plugin info into the static t_plug structure, including your
+ * parser function, which needs to take one xmlNodePtr as argument and
+ * return a pointer to a profile struct (see alfs.h). You now can just
+ * put your parser code into the parser function using the libalfs 
+ * routines. 
+ * 
+ */
+
 #include <plugin.h>
 
+profile *parse_example (xmlNodePtr node);
+
 static t_plug sample_plugin =
 {
 	name:	"Example plugin",
-	ver:	1
+	vers:	1,
+	parse:	parse_example
+};
+
+t_plug *getplug ()
+{
+	return &sample_plugin;
 }
+
+profile *parse_example (xmlNodePtr node)
+{
+	return NULL;
+}

Modified: dev/concept/moongoo/syntax/syn.c
===================================================================
--- dev/concept/moongoo/syntax/syn.c	2004-12-03 22:36:48 UTC (rev 1626)
+++ dev/concept/moongoo/syntax/syn.c	2004-12-04 00:08:21 UTC (rev 1627)
@@ -2,9 +2,24 @@
 
 #include <alfs.h>
 #include <util.h>
+#include <plugin.h>
 
 profile *prof;
 
+profile *syn_profile (xmlNodePtr node);
+
+static t_plug syn_plugin =
+{
+	name:	"Hive profile syntax",
+	vers:	1,
+	parse:	syn_profile
+};
+
+t_plug *getplug ()
+{
+	return &syn_plugin;
+}
+
 // TODO: Complete syn support
 void process_cmd2 (char *line, xmlNodePtr node)
 {




More information about the alfs-log mailing list