preprocessor & file not found

Ross Paterson <[email protected]>
Newsgroups gmane.comp.lang.haskell.hugs.bugs
Message-ID <[email protected]>
If I ask Hugs to read a non-existent file, e.g. "hugs Foo", I get

Reading file "/usr/local/share/hugs/lib/Prelude.hs":
Reading file "Foo":
ERROR "Foo" - Unable to open file "Foo"
Prelude>

But if I say "hugs -Fcat Foo", I get

Reading file "/usr/local/share/hugs/lib/Prelude.hs":
Reading file "Foo":
Parsingcat: Foo: No such file or directory                                      Hugs session for:
/usr/local/share/hugs/lib/Prelude.hs
Foo
Type :? for help
Main>

This is because findPathname() returns the original name if it couldn't
find a file, relying on fileInput() to fail in that situation, but popen()
always succeeds.  The preprocessor is executed with the name as argument
and fails in its own way, and Hugs doesn't know it failed.  (It thinks it
read an empty module, hence Main.)  This patch is one way of fixing that.

Index: src/input.c
===================================================================
RCS file: /home/cvs/root/hugs98/src/input.c,v
retrieving revision 1.47
diff -u -r1.47 input.c
--- src/input.c	2002/07/19 22:13:48	1.47
+++ src/input.c	2002/08/01 13:20:02
@@ -407,8 +407,9 @@
 static Bool local fileInput(nm,len)     /* prepare to input characters from*/
 String nm;                              /* named file (specified length is */
 Long   len; {                           /* used to set target for reading) */
+    inputStream = fopen(nm,FOPEN_MODE);
 #if SUPPORT_PREPROCESSOR
-    if (preprocessor) {
+    if (preprocessor && inputStream) {
 	Int reallen = strlen(preprocessor) + 1 + strlen(nm) + 1;
 	char *cmd = malloc(reallen+1);
 	if (cmd == NULL) {
@@ -423,13 +424,10 @@
 	} else {
 	    cmd[reallen] = '\0';
 	}
+	fclose(inputStream);
 	inputStream = popen(cmd,"r");
 	free(cmd);
-    } else {
-	inputStream = fopen(nm,FOPEN_MODE);
     }
-#else
-    inputStream = fopen(nm,FOPEN_MODE);
 #endif
     if (inputStream) {
 	reading      = SCRIPTFILE;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.