parser.c

changeset 14
91f0c87b62a7
parent 13
9eb09d246c9f
child 17
a034888a2227
--- a/parser.c	Fri May 26 18:34:04 2000 +0200
+++ b/parser.c	Sat Jun 03 19:00:43 2000 +0200
@@ -6,6 +6,7 @@
  */
 
 #include <string.h>
+#include <errno.h>
 
 #include <libtu/parser.h>
 #include <libtu/misc.h>
@@ -423,7 +424,7 @@
 	Tokenizer *tokz;
 	bool ret;
 	
-	tokz=tokz_open_file(file);
+	tokz=tokz_open_file(file, NULL);
 	
 	if(tokz==NULL)
 		return FALSE;
@@ -581,35 +582,81 @@
 /* */
 
 
-static bool opt_include(Tokenizer *tokz, int n, Token *toks)
+static bool try_include(Tokenizer *tokz, const char *fname)
 {
-	const char *fname=TOK_STRING_VAL(toks+1);
-	const char *lastndx;
+	FILE *f;
+	
+	f=fopen(fname, "r");
+	
+	if(f==NULL)
+		return FALSE;
+	
+	if(!tokz_pushf_file(tokz, f, fname)){
+		fclose(f);
+		return FALSE;
+	}
+	
+	return TRUE;
+}
+
+
+static bool try_include_dir(Tokenizer *tokz, const char *dir, int dlen,
+						const char *file)
+{
 	char *tmpname;
 	bool retval;
 	
-	if(fname[0]=='/' || tokz->name==NULL)
-		goto thisdir;
-	
-	lastndx=strrchr(tokz->name, '/');
-	
-	if(lastndx==NULL)
-		goto thisdir;
-	
-	tmpname=scatn(tokz->name, lastndx-tokz->name+1, fname, -1);
+	tmpname=scatn(dir, dlen, file, -1);
 	
 	if(tmpname==NULL){
 		warn_err();
 		return FALSE;
 	}
 	
-	retval=tokz_pushf(tokz, tmpname);
-	
+	retval=try_include(tokz, tmpname);
+
 	free(tmpname);
-	
+
 	return retval;
-
-thisdir:
-	return tokz_pushf(tokz, fname);
 }
 
+
+static bool opt_include(Tokenizer *tokz, int n, Token *toks)
+{
+	const char *fname=TOK_STRING_VAL(toks+1);
+	const char *lastndx=NULL;
+	bool retval, e;
+	int i=0;
+	
+	if(fname[0]!='/' && tokz->name!=NULL)
+		lastndx=strrchr(tokz->name, '/');
+	
+	if(lastndx==NULL)
+		retval=try_include(tokz, fname);
+	else
+		retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname);
+	
+	if(retval==TRUE)
+		return TRUE;
+	
+	e=errno;
+	
+	if(tokz->includepaths!=NULL){
+		while(tokz->includepaths[i]!=NULL){
+			if(try_include_dir(tokz, tokz->includepaths[i], -1, fname))
+				return TRUE;
+			i++;
+		}
+	}
+	
+	warn_obj(fname, "%s", strerror(e));
+	
+	return FALSE;
+}
+
+
+extern void tokz_set_includepaths(Tokenizer *tokz, char **paths)
+{
+	tokz->includepaths=paths;
+}
+

mercurial