summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog1
-rw-r--r--src/input.c13
-rw-r--r--src/input.h7
-rw-r--r--src/main.c19
4 files changed, 19 insertions, 21 deletions
diff --git a/ChangeLog b/ChangeLog
index fb6a9ae..5cb38a5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2005-03-28  Herbert Xu <herbert@gondor.apana.org.au>
 
 	* Removed some unnecessary inclusions of input.h.
+	* Generalise setinputfile for use in read_profile/readcmdfile.
 
 2005-03-25  Gerrit Pape <pape@smarden.org>
 
diff --git a/src/input.c b/src/input.c
index 86d7578..8c874c6 100644
--- a/src/input.c
+++ b/src/input.c
@@ -433,15 +433,18 @@ popstring(void)
  * old input onto the stack first.
  */
 
-void
-setinputfile(const char *fname, int push)
+int
+setinputfile(const char *fname, int flags)
 {
 	int fd;
 	int fd2;
 
 	INTOFF;
-	if ((fd = open(fname, O_RDONLY)) < 0)
+	if ((fd = open(fname, O_RDONLY)) < 0) {
+		if (flags & INPUT_NOFILE_OK)
+			goto out;
 		sh_error("Can't open %s", fname);
+	}
 	if (fd < 10) {
 		fd2 = copyfd(fd, 10);
 		close(fd);
@@ -449,8 +452,10 @@ setinputfile(const char *fname, int push)
 			sh_error("Out of file descriptors");
 		fd = fd2;
 	}
-	setinputfd(fd, push);
+	setinputfd(fd, flags & INPUT_PUSH_FILE);
+out:
 	INTON;
+	return fd;
 }
 
 
diff --git a/src/input.h b/src/input.h
index cf18242..2b24c86 100644
--- a/src/input.h
+++ b/src/input.h
@@ -40,6 +40,11 @@
 
 /* PEOF (the end of file marker) is defined in syntax.h */
 
+enum {
+	INPUT_PUSH_FILE = 1,
+	INPUT_NOFILE_OK = 2,
+};
+
 /*
  * The input line number.  Input.c just defines this variable, and saves
  * and restores it when files are pushed and popped.  The user of this
@@ -56,7 +61,7 @@ int preadbuffer(void);
 void pungetc(void);
 void pushstring(char *, void *);
 void popstring(void);
-void setinputfile(const char *, int);
+int setinputfile(const char *, int);
 void setinputfd(int, int);
 void setinputstring(char *);
 void popfile(void);
diff --git a/src/main.c b/src/main.c
index b6dea21..886c359 100644
--- a/src/main.c
+++ b/src/main.c
@@ -270,13 +270,7 @@ cmdloop(int top)
 STATIC void
 read_profile(const char *name)
 {
-	int fd;
-
-	INTOFF;
-	if ((fd = open(name, O_RDONLY)) >= 0)
-		setinputfd(fd, 1);
-	INTON;
-	if (fd < 0)
+	if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
 		return;
 	cmdloop(0);
 	popfile();
@@ -291,14 +285,7 @@ read_profile(const char *name)
 void
 readcmdfile(char *name)
 {
-	int fd;
-
-	INTOFF;
-	if ((fd = open(name, O_RDONLY)) >= 0)
-		setinputfd(fd, 1);
-	else
-		sh_error("Can't open %s", name);
-	INTON;
+	setinputfile(name, INPUT_PUSH_FILE);
 	cmdloop(0);
 	popfile();
 }
@@ -347,7 +334,7 @@ dotcmd(int argc, char **argv)
 		char *fullname;
 
 		fullname = find_dot_file(argv[1]);
-		setinputfile(fullname, 1);
+		setinputfile(fullname, INPUT_PUSH_FILE);
 		commandname = fullname;
 		cmdloop(0);
 		popfile();