/* * stubbed execute() function... */ #include #include #include #include #include #include "tsh.h" #include "kw.h" #include "execute.h" #include "system.h" #include "args.h" #define SAFE_FD 10 runcommand(cmd, mode) CMD *cmd; { /* * runcommand needs to: * 1) tokenize the command passed in. x * 2) perform i/o redirection. * 3) set up the environment for this command (this will need to be * done for the while and for commands as well) * 4) check for builtin command (pwd, read, cd, pushd, popd, type, * et alii) * 5) execute external commands. */ char pathto[256]; char *path = getenv("PATH"); int type; struct arg_t args, io; register i; register st; int saved[SAFE_FD]; if (cmd->c_text == (char*)0 || cmd->c_text[cmd->c_fs] == 0) return (status = 0); cutup(cmd->c_text+cmd->c_fs, &args, &io); /* redirect i/o if necessary */ memset(saved, -1, sizeof saved); if (io.argc > 0) { for (i=0; i 0 && args.argv[0][0]) { /* pass arglist to doexec, which will build the command tail if need * be. */ if (type = findfile(args.argv[0], path, pathto)) { status = doexec(mode, pathto, &args, childenviron()); /* if the program vanished, we don't want to remember it */ if (status == ST_NOT) forget(args.argv[0]); } else { status = ST_NOT; fprintf(stderr, "%s: not found.\n", args.argv[0]); } } exitio: oldio(saved); exitpoint: disposeargs(&args); disposeargs(&io); return status; } /* runcommand */ /* * newio() redirects input or output. */ newio(arg, saved) char *arg; int *saved; { register fd; register newfd; register mode; /* get fd to redirect */ if (isdigit(*arg)) { for (fd=0; isdigit(*arg); arg++) fd = (fd*10) + (*arg - '0'); } else /* none specified - use defaults */ fd = (*arg == '>') ? 1 : 0; /* find out type of redirection */ mode = (*arg == '<') ? (O_RDONLY|O_BINARY) : (O_WRONLY|O_CREAT|O_TRUNC|O_BINARY); if (*arg == '>' && arg[1] == '>') { ++arg; mode |= O_APPEND; } ++arg; /* save this file descriptor so we can get it back */ if (fd < SAFE_FD && saved[fd] < 0) saved[fd] = dup(fd); /* if we're dupping one fd into another, do it; otherwise open the * file in question */ if (*arg == '&') { for (++arg, newfd=0; isdigit(*arg); ++arg) newfd = (newfd*10) + (*arg - '0'); if (*arg) { fprintf(stderr, "tsh: garbage at end of i/o redirection\n"); return 1; } } else if ((newfd = open(arg, mode, 0666)) < 0) { fprintf(stderr, "%s: permission denied\n", arg); return 1; } dup2(newfd, fd); close(newfd); return 0; } /* newio */ /* * oldio() restores all the file descriptors that were forcibly redirected */ oldio(saved) int *saved; { register i; for (i=0; ic_text+cmd->c_fs); return (ITERATOR*)0; } /* foreach */ fornext(ctl) ITERATOR *ctl; { return 0; } /* fornext */ void forgone(ctl) ITERATOR *ctl; { }