r1602 - dev/concept/moongoo

neocool at linuxfromscratch.org neocool at linuxfromscratch.org
Mon Nov 29 18:02:47 PST 2004


Author: neocool
Date: 2004-11-29 19:02:46 -0700 (Mon, 29 Nov 2004)
New Revision: 1602

Added:
   dev/concept/moongoo/LICENSE
   dev/concept/moongoo/Makefile
   dev/concept/moongoo/alfs.c
   dev/concept/moongoo/alfs.h
   dev/concept/moongoo/book.c
   dev/concept/moongoo/book.h
   dev/concept/moongoo/moongoo.c
   dev/concept/moongoo/repl.c
   dev/concept/moongoo/repl.h
   dev/concept/moongoo/util.c
   dev/concept/moongoo/util.h
Log:
Initial import.


Added: dev/concept/moongoo/LICENSE
===================================================================
--- dev/concept/moongoo/LICENSE	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/LICENSE	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,24 @@
+                        The MIT License
+
+Copyright (c) 2004 Boris Buegling
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.

Added: dev/concept/moongoo/Makefile
===================================================================
--- dev/concept/moongoo/Makefile	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/Makefile	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,45 @@
+CC=gcc
+CFLAGS=-std=c99 -D_BSD_SOURCE -I. 
+
+CFLAGS+=`xml2-config --cflags`
+LDLIBS+=`xml2-config --libs`
+
+CFLAGS+=-Wall -Werror -g3
+
+BIN=moongoo
+OBJS=alfs.o book.o util.o repl.o
+
+BOOK=~/projects/alfs/book/index.xml
+
+.PHONY: all clean compile run memcheck debug todo upload
+
+all: run
+
+clean:
+	rm -f $(BIN) $(OBJS)
+	
+compile: $(BIN)
+
+run: compile
+	./$(BIN) $(BOOK)
+
+memcheck: compile
+	valgrind -q --tool=memcheck $(BIN) $(BOOK)
+
+debug: compile
+	@echo "Book: $(BOOK)"
+	gdb ./$(BIN)
+
+todo:
+	@grep "TODO:" *.c *.h|cut -d" " -f3-|sort
+
+upload: clean
+	@release_snap
+	@scp moongoo-*.tar.bz2 l14h.org:~/public_html/nALFS2/snap/
+	@rm -f moongoo-*.tar.bz2
+
+$(BIN): $(OBJS)
+alfs.o: alfs.h
+util.o: util.h
+book.o: book.h
+repl.o: repl.h

Added: dev/concept/moongoo/alfs.c
===================================================================
--- dev/concept/moongoo/alfs.c	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/alfs.c	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,209 @@
+#include <string.h>
+#include <unistd.h>
+
+#include <alfs.h>
+#include <util.h>
+
+xmlDocPtr doc;
+
+void print_cmd (command cmd)
+{
+	int i=0;
+
+	printf("%s ", cmd.cmd);
+	for (i=0;i<cmd.n;i++)
+		printf("%s ", cmd.arg[i]);
+	if (cmd.role!=NONE)
+		printf("(%s)", role2str(cmd.role));
+	printf("\n");
+}
+
+void print_pkg (package pkg)
+{
+	int i;
+	
+	if (!pkg.vers)
+		printf("%s\n\n", pkg.name);
+	else
+		printf("%s-%s\n\n", pkg.name, pkg.vers);
+	
+	for (i=0;i<pkg.n;i++)
+		print_cmd(pkg.build[i]);
+}		
+
+void print_subtree (xmlNodePtr node)
+{
+	while (node)
+	{
+		printf("%s(%s): %s\n", node->name, type2str(node->type), 
+			xmlNodeGetContent(node));
+		print_subtree(node->children);
+		printf("\n");
+		node=node->next;
+	}
+}
+
+void foreach (xmlNodePtr node, char *str, xml_handler_t func, void *data)
+{
+	if (!str)
+		return;
+	
+	while (node)
+	{
+		if (!strcmp(node->name, str))
+			func(node, data);
+		foreach(node->children, str, func, data);
+		node=node->next;
+	}
+}
+
+xmlNodePtr find_node (xmlNodePtr root, char *str)
+{
+	if (!str)
+		return NULL;
+
+	while (root)
+	{
+		xmlNodePtr tmp = NULL;;
+		
+		if (!strcmp(root->name, str))
+			return root;
+		tmp=find_node(root->children, str);
+		if (tmp)
+			return tmp;
+		root=root->next;
+	}
+	
+	return NULL;
+}
+
+char *find_value (xmlNodePtr node, char *str)
+{
+	xmlNodePtr moo = find_node(node, str);
+	if (!moo)
+		return NULL;
+	return xmlNodeGetContent(moo);
+}
+
+char *entity_val (char *name)
+{
+	xmlEntityPtr ent = xmlGetDocEntity(doc, name);
+	char *ret;
+	
+	if (!ent)
+		return NULL;
+	ret=(char *)malloc(ent->length+1);
+	strncpy(ret, ent->content, ent->length);
+	ret[ent->length]='\0';
+	return ret;
+}
+
+char *role2str (role role)
+{
+	switch (role)
+	{
+		case (NONE):
+			return "none";
+		case (NOEXECUTE):
+			return "noexecute";
+		case (INTERACTIVE):
+			return "interactive";
+		case (TESTSUITE):
+			return "testsuite";
+		case (INSTALL):
+			return "install";
+		case (CHROOT):
+			return "chroot";
+		default:
+			return "unknown";
+	}
+}
+
+role parse_role (xmlNodePtr node)
+{
+	char *prop = xmlGetProp(node, "role");
+
+	if (!prop)
+		return NONE;
+	if (!strcmp(prop, "noexecute"))
+		return NOEXECUTE;
+	if (!strcmp(prop, "interactive"))
+		return INTERACTIVE;
+	if (!strcmp(prop, "testsuite"))
+		return TESTSUITE;
+	if (!strcmp(prop, "install"))
+		return INSTALL;
+	if (!strcmp(prop, "chroot"))
+		return CHROOT;
+
+	fprintf(stderr, "%s is an unknown role-attribute.\n", prop);
+	
+	return NONE;
+}
+
+char *type2str (xmlElementType type)
+{
+	switch (type)
+	{
+    	case (XML_ELEMENT_NODE):
+			return "element";
+		case (XML_ATTRIBUTE_NODE):
+			return "attribute";
+    	case (XML_TEXT_NODE):
+			return "text";
+		case (XML_CDATA_SECTION_NODE):
+			return "cdata section";
+    	case (XML_ENTITY_REF_NODE):
+			return "entity ref";
+    	case (XML_ENTITY_NODE):
+			return "entity";
+    	case (XML_PI_NODE):
+			return "pi";
+    	case (XML_COMMENT_NODE):
+			return "comment";
+    	case (XML_DOCUMENT_NODE):
+			return "document";
+    	case (XML_DOCUMENT_TYPE_NODE):
+			return "document type";
+   		case (XML_DOCUMENT_FRAG_NODE):
+			return "document frag";
+    	case (XML_NOTATION_NODE):
+			return "notation";
+    	case (XML_HTML_DOCUMENT_NODE):
+			return "HTML document";
+    	case (XML_DTD_NODE):
+			return "DTD";
+    	case (XML_ELEMENT_DECL):
+			return "element declaration";
+    	case (XML_ATTRIBUTE_DECL):
+			return "attribute declaration";
+    	case (XML_ENTITY_DECL):
+			return "entity declaration";
+    	case (XML_NAMESPACE_DECL):
+			return "namespace declaration";
+    	case (XML_XINCLUDE_START):
+			return "xinclude start";
+    	case (XML_XINCLUDE_END):
+			return "xinclude end";
+   		case (XML_DOCB_DOCUMENT_NODE):
+			return "docbook";
+		default:
+			return "unknown";
+	}
+}
+
+void resolve_entities (xmlNodePtr node)
+{
+	while (node)
+	{
+		if (node->type==XML_ENTITY_REF_NODE)
+		{
+			printf("%s\n", node->parent->parent->parent->name);
+			//print_subtree(node->parent->children);
+			//node->prev->next=node->next->next;
+			//printf("%s %s\n", node->name, entity_val((char *)node->name));
+		}
+		resolve_entities(node->children);
+		node=node->next;
+	}
+}

Added: dev/concept/moongoo/alfs.h
===================================================================
--- dev/concept/moongoo/alfs.h	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/alfs.h	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,74 @@
+#ifndef __ALFS_H__
+#define __ALFS_H__
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+#include <libxml/xinclude.h>
+#include <libxml/tree.h>
+
+typedef enum
+{
+	NONE,
+	NOEXECUTE,
+	INTERACTIVE,
+	TESTSUITE,
+	INSTALL,
+	CHROOT
+} role;
+
+typedef struct
+{
+	char *name;
+	mode_t mode;
+	bool append;
+	char *content;
+} file;
+
+typedef struct
+{
+	char *cmd;
+	char **arg;
+	int n;
+	role role;
+} command;
+
+typedef struct
+{
+	char *name;
+	char *vers;
+	command *build;
+	int n;
+} package;
+
+typedef struct
+{
+	package *pkg;
+	int n;
+} profile;
+
+typedef struct
+{
+	char *orig;
+	char *repl;
+} replaceable;
+
+extern xmlDocPtr doc;
+
+typedef void (*xml_handler_t) (xmlNodePtr node, void *data);
+
+void print_subtree (xmlNodePtr node);
+void print_pkg (package pkg);
+void print_cmd (command cmd);
+
+void resolve_entities (xmlNodePtr node);
+void foreach (xmlNodePtr node, char *str, xml_handler_t func, void *data);
+xmlNodePtr find_node (xmlNodePtr root, char *str);
+char *find_value (xmlNodePtr node, char *str);
+char *entity_val (char *name);
+
+char *role2str (role role);
+char *type2str (xmlElementType type);
+role parse_role (xmlNodePtr node);
+	
+#endif

Added: dev/concept/moongoo/book.c
===================================================================
--- dev/concept/moongoo/book.c	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/book.c	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,120 @@
+#include <string.h>
+#include <stdlib.h>
+
+#include <book.h>
+#include <repl.h>
+#include <util.h>
+
+command *cmd;
+profile *prof;
+int num;
+
+void t_sect1 (xmlNodePtr node, void *data);
+void process_cmd (char *line, xmlNodePtr node);
+void __t_userinput (xmlNodePtr node, void *data);
+command *t_userinput (xmlNodePtr node, int *n);
+
+// TODO: Parse 'cat > foo' commands
+file *parse_cat (char *str)
+{
+	//file *f = (file *)malloc(sizeof(file));
+	//printf("%s\n\n", str);
+	return NULL;
+}
+
+void t_sect1 (xmlNodePtr node, void *data)
+{
+	int i;
+	char *title, *tmp;
+		
+	title = find_value(node->children, "title");
+
+	if (!title)
+	{
+		fprintf(stderr, "%s: No title found.\n", node->name);
+		return;
+	}
+
+	prof->pkg = realloc(prof->pkg, (++prof->n)*sizeof(package));
+	i = prof->n-1;
+	
+	tmp = strdog(lower_case(title), "version");
+	prof->pkg[i].vers = entity_val(tmp);
+	prof->pkg[i].name = strcut(title, 0, strlen(title)-
+		((prof->pkg[i].vers) ? 1 : 0));
+	prof->pkg[i].build = t_userinput(node->children, 
+		&prof->pkg[i].n);
+	free(tmp);
+
+	if (!prof->pkg[i].n)
+		prof->n--;
+}
+
+void process_cmd (char *line, xmlNodePtr node)
+{
+	cmd = realloc(cmd, (++num)*sizeof(command));
+	cmd[num-1].role = parse_role(node);
+	
+	if (strcnt(line, " "))
+	{
+		cmd[num-1].cmd = strcut(line, 0, whereis(line, ' '));
+		cmd[num-1].arg = tokenize(notrail(strstr(line, " "), " "), 
+				" ", &cmd[num-1].n);
+	}
+	else
+	{
+		cmd[num-1].cmd = line;
+		cmd[num-1].arg = NULL;
+		cmd[num-1].n = 0;
+	}
+}
+
+void __t_userinput (xmlNodePtr node, void *data)
+{
+	char *line;
+
+	foreach(node->children, "replaceable", (xml_handler_t)t_repl, NULL);
+	line = squeeze(xmlNodeGetContent(node));
+	line = strkill(line, "\\\n");
+
+	if (strcnt(line, "\n"))
+	{
+		char *tmp;
+		
+		while ((line) && (strlen(line)))
+		{
+			if (!strncmp(line, "cat >", 5))
+			{
+				char *t = strnstr(line, "EOF", 2);
+				parse_cat(strcut(line, 0, t-line+3));
+				t+=2;
+				line=t;
+				continue;
+			}
+			
+			tmp = strsep(&line, "\n");
+			process_cmd(tmp, node);
+		}
+	}
+	else
+		process_cmd(line, node);
+}
+
+command *t_userinput (xmlNodePtr node, int *n)
+{
+	cmd = NULL;
+	num = 0;
+	foreach(node, "userinput", (xml_handler_t)__t_userinput, NULL);
+	*n = num;
+	return cmd;
+}
+
+profile *bookasprofile (xmlNodePtr node)
+{
+	init_repl();
+	prof = (profile *)malloc(sizeof(profile));
+	prof->pkg = NULL;
+	prof->n = 0;
+	foreach(node, "sect1", (xml_handler_t)t_sect1, NULL);
+	return prof;
+}

Added: dev/concept/moongoo/book.h
===================================================================
--- dev/concept/moongoo/book.h	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/book.h	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,11 @@
+#ifndef __BOOK_H__
+#define __BOOK_H__
+
+#include <alfs.h>
+
+void t_sect1 (xmlNodePtr node, void *data);
+command *t_userinput (xmlNodePtr node, int *n);
+profile *bookasprofile (xmlNodePtr node);
+file *parse_cat (char *str);
+
+#endif

Added: dev/concept/moongoo/moongoo.c
===================================================================
--- dev/concept/moongoo/moongoo.c	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/moongoo.c	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <string.h>
+
+#include <alfs.h>
+#include <book.h>
+#include <repl.h>
+#include <util.h>
+
+int main (int argc, char **argv)
+{
+	int i;
+	xmlNodePtr cur;
+	profile *prof;
+
+	if (argc<2)
+	{
+		fprintf(stderr, "No book to parse.\n");
+		return 1;
+	}
+	
+	// TODO: Resolving of entities
+	//xmlSubstituteEntitiesDefault(1);
+	doc=xmlParseFile(argv[1]);
+	if (!doc)
+		return 2;
+	xmlXIncludeProcessFlags(doc, XML_PARSE_NONET);
+	cur=xmlDocGetRootElement(doc);
+	resolve_entities(cur);
+	
+	prof=bookasprofile(cur);
+	for (i=0;i<prof->n;i++)
+	{
+		package pkg = prof->pkg[i];
+		
+		//if (!strcmp(pkg.name, "Module-Init-Tools"))
+		//if (!strcmp(pkg.name, "Creating a file system"))
+		if (!strcmp(pkg.name, "Linux"))
+			print_pkg(pkg);
+	}
+	
+	xmlFreeDoc(doc);
+	return 0;
+}

Added: dev/concept/moongoo/repl.c
===================================================================
--- dev/concept/moongoo/repl.c	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/repl.c	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,66 @@
+#include <string.h>
+
+#include <repl.h>
+#include <alfs.h>
+
+replaceable r[REPLC];
+
+void t_repl (xmlNodePtr node, void *data)
+{
+	int i;
+	xmlNodePtr text = find_node(node->children, "text");
+	char *repl = xmlNodeGetContent(text);
+	
+	for (i=0;i<REPLC;i++)
+		if (!strcmp(repl, r[i].orig))
+		{
+			xmlNodeSetContent(node, r[i].repl);
+			return;
+		}
+	
+	fprintf(stderr, "Unknown replaceable: %s\n", repl);
+}
+
+// TODO: Make <replaceable> actually configurable
+void init_repl ()
+{
+	// LFS install partition
+	r[0].orig="[xxx]";
+	r[0].repl="/dev/discs/disc0/part5";
+	r[14].orig="[fff]";
+	r[14].repl="ext3";
+	// LFS user partition
+	r[12].orig="[zzz]";
+	r[12].repl="";
+	// Swap partition
+	r[1].orig="[yyy]";
+	r[1].repl="/dev/discs/disc0/part10";
+	// Paper size
+	r[2].orig="[paper_size]";
+	r[2].repl="A4";
+	// Network settings
+	r[3].orig="[FQDN]";
+	r[3].repl="Lenin.Ass";
+	r[4].orig="[HOSTNAME]";
+	r[4].repl="Lenin";
+	r[5].orig="[192.168.1.1]";
+	r[5].repl="192.168.1.23";
+	r[10].orig="[Your Domain Name]";
+	r[10].repl="Ass";
+	r[11].orig="[IP address of your nameserver]";
+	r[11].repl="217.237.149.161";
+	// Locales
+	r[6].orig="[ll]";
+	r[6].repl="de";
+	r[7].orig="[CC]";
+	r[7].repl="DE at euro";
+	r[8].orig="[arguments for loadkeys]";
+	r[8].repl="de-latin1-nodeadkeys";
+	r[9].orig="[arguments for setfont]";
+	r[9].repl="lat9u-16";
+	r[13].orig="[/path/to/your/keymap]";
+	r[13].repl="";
+	// Pathes
+	r[15].orig="[unpacked sources dir]";
+	r[15].repl="/usr/src";
+}	

Added: dev/concept/moongoo/repl.h
===================================================================
--- dev/concept/moongoo/repl.h	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/repl.h	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,13 @@
+#ifndef __REPL_H__
+#define __REPL_H__
+
+#include <alfs.h>
+
+#define REPLC	16
+
+extern replaceable r[REPLC];
+
+void t_repl (xmlNodePtr node, void *data);
+void init_repl ();
+
+#endif

Added: dev/concept/moongoo/util.c
===================================================================
--- dev/concept/moongoo/util.c	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/util.c	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,293 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#include <util.h>
+
+char *lower_case (char *arg)
+{
+	char *str;
+	int i;
+	
+	if (!arg)
+		return NULL;
+
+	str = (char *)malloc(strlen(arg)+1);
+	strcpy(str, arg);
+
+	for (i=0;i<strlen(str);i++)
+		if (isalpha(str[i]))
+			str[i]=tolower(str[i]);
+
+	return str;
+}
+
+char *strdog (char *str1, char *str2)
+{
+	char *ret;
+
+	if ((!str1)||(!str2))
+		return NULL;
+		
+	ret=(char *)malloc(strlen(str1)+strlen(str2)+1);
+
+	strcpy(ret, str1);
+	strcat(ret, str2);
+	return ret;
+}
+
+char *chrep (char *str, char old, char new)
+{
+	char *ret;
+	int i;
+
+	if (!str)
+		return NULL;
+	
+	ret=(char *)malloc(strlen(str)+1);
+
+	for (i=0;i<strlen(str);i++)
+		if (str[i]==old)
+			ret[i]=new;
+		else
+			ret[i]=str[i];
+	ret[i]='\0';
+
+	return ret;
+}
+
+char **tokenize (char *arg, char *token, int *i)
+{
+	int n=0;
+	char **ret = NULL, *str;
+
+	if ((!arg)||(!token))
+		return NULL;
+
+	str=(char *)malloc(strlen(arg)+1);
+	strcpy(str, arg);
+
+	while (strcnt(str, " "))
+	{
+		ret = realloc(ret, (++n)*sizeof(char *));
+		ret[n-1] = strcut(str, 0, whereis(str, ' '));
+		str = notrail(strstr(str, token), token);
+	}
+
+	ret = realloc(ret, (++n)*sizeof(char *));
+	ret[n-1] = (char *)malloc(strlen(str)+1);
+	strcpy(ret[n-1], str);
+	*i = n;
+	return ret;
+}
+
+char *strrep (char *str, char *old, char *new)
+{
+	char *ret;
+	int i, j=0, k;
+
+	if ((!str)||(!old)||(!new))
+		return NULL;
+	
+	ret=(char *)malloc(strlen(str)+1+((strlen(new)-strlen(old))*
+			strcnt(str, old)));
+
+	for (i=0;i<strlen(str);i++)
+		if (strstarts(str, i, old))
+		{
+			for (k=j;k<j+strlen(new);k++)
+				ret[k]=new[k-j];
+			i+=strlen(old)-1;
+			j+=strlen(new);
+		}
+		else
+		{
+			ret[j]=str[i];
+			j++;
+		}
+	ret[j]='\0';
+
+	return ret;
+}
+
+int strcnt (char *str, char *sstr)
+{
+	char *t, *t2;
+	int i, ret=0;
+
+	if ((!str)||(!sstr))
+		return 0;
+
+	t=(char *)malloc(strlen(str)+1);
+	strcpy(t, str);
+	t2=t;
+	while (t)
+	{
+		t=strstr(t, sstr);
+		if (t)
+		{
+			ret++;
+			for (i=0;i<strlen(sstr);i++)
+				t++;
+		}
+	}
+
+	free(t2);
+	return ret;
+}
+
+int strstarts (char *str, int begin, char *test)
+{
+	char *tmp;
+	int ret=0;
+
+	if ((!str)||(strlen(str)<begin+strlen(test)))
+		return 0;
+
+	tmp=strcutb(str, begin, strlen(test), false);
+
+	if (!strcmp(tmp, test))
+		ret=1;
+
+	free(tmp);
+	return ret;
+}
+
+char *strcutb (char *str, int begin, int count, bool blanks)
+{
+	int i, j=0, end;
+	char *ret;
+	
+	if (!str)
+		return NULL;
+	if (count<0)
+		count=0;
+	
+	ret=(char *)malloc(count+1);
+
+	if (count<1)
+	{
+		strcpy(ret, "");
+		return ret;
+	}
+
+	if (strlen(str)<begin+count)
+		end=strlen(str);
+	else
+		end=begin+count;
+	
+	for (i=begin;i<end;i++)
+		if (!((j==0)&&(blanks)&&(str[i]==' ')))
+		{
+			ret[j]=str[i];
+			j++;
+		}
+	ret[j]='\0';
+
+	return ret;
+}
+
+char *strcut (char *str, int begin, int count)
+{
+	return strcutb(str, begin, count, true);
+}
+
+int whereis (char *str, char ch)
+{
+	int i;
+
+	if (!str)
+		return -1;
+	
+	for (i=0;i<strlen(str);i++)
+		if (str[i]==ch)
+			return i;
+
+	return -1;
+}
+
+char *notrail (char *str, char *token)
+{
+	char *ret;
+	
+	if ((!str)||(!token))
+		return NULL;
+
+	ret=(char *)malloc(strlen(str)+1);
+	strcpy(ret, str);
+	while (strstarts(ret, 0, token))
+		ret+=strlen(token);
+	return ret;
+}
+
+char *squeeze (char *str)
+{
+	bool blank=false;
+	int i, len=0;
+	char *ret;
+
+	if (!str)
+		return NULL;
+
+	ret=(char *)malloc(strlen(str)+1);
+
+	for (i=0;i<strlen(str);i++)
+	{
+		if (isblank(str[i]))
+		{
+			if (blank)
+				continue;
+			blank=true;
+		}
+		else
+			blank=false;
+		
+		ret[len] = str[i];
+		len++;
+	}
+	
+	ret[len]='\0';
+	return ret;
+}
+
+char *strkill (char *str, char *tokill)
+{
+	char *cpy;
+	int i, j=0;
+
+	if ((!str)||(!tokill))
+		return NULL;
+	
+	cpy=(char *)malloc(strlen(str)+1);
+	
+	for (i=0;i<strlen(str);i++)
+		if (strstarts(str, i, tokill))
+			i+=strlen(tokill)-1;
+		else
+		{
+			cpy[j]=str[i];
+			j++;
+		}
+	cpy[j]='\0';
+
+	return cpy;
+}
+
+char *strnstr (char *haystack, char *needle, int n)
+{
+	int i;
+	char *ret = haystack;
+
+	if ((!haystack)||(!needle))
+		return NULL;
+	
+	for (i=0;i<n;i++)
+	{
+		ret = strstr(ret, needle);
+		if (strcmp(haystack, ret))
+			ret++;
+	}
+
+	return ret;
+}	

Added: dev/concept/moongoo/util.h
===================================================================
--- dev/concept/moongoo/util.h	2004-11-30 01:59:44 UTC (rev 1601)
+++ dev/concept/moongoo/util.h	2004-11-30 02:02:46 UTC (rev 1602)
@@ -0,0 +1,21 @@
+#ifndef __UTIL_H__
+#define __UTIL_H__
+
+#include <stdbool.h>
+
+char *lower_case (char *str);
+char *strdog (char *str1, char *str2);
+char *chrep (char *str, char old, char new);
+char **tokenize (char *str, char *token, int *i);
+char *strrep (char *str, char *old, char *new);
+int strcnt (char *str, char *sstr);
+int strstarts (char *str, int begin, char *test);
+char *strcutb (char *str, int begin, int count, bool blanks);
+char *strcut (char *str, int begin, int count);
+int whereis (char *str, char ch);
+char *notrail (char *str, char *token);
+char *squeeze (char *str);
+char *strkill (char *str, char *tokill);
+char *strnstr (char *haystack, char *needle, int n);
+
+#endif




More information about the alfs-log mailing list