From 4daf75cefd7ca514d0188bae3adc8f61a63940e5 Mon Sep 17 00:00:00 2001 From: herbert Date: Mon, 28 Mar 2005 14:13:59 +1000 Subject: Generalise setinputfile for use in read_profile/readcmdfile. --- src/input.c | 13 +++++++++---- src/input.h | 7 ++++++- src/main.c | 19 +++---------------- 3 files changed, 18 insertions(+), 21 deletions(-) (limited to 'src') 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(); -- cgit 1.4.1