#include "rc.h"
static void b_break(char **), b_cd(char **), b_eval(char **), b_exit(char **),
- b_newpgrp(char **), b_return(char **), b_shift(char **), b_umask(char **),
- b_wait(char **), b_limit(char **), b_echo(char **);
+ b_newpgrp(char **), b_return(char **), b_shift(char **), b_umask(char **),
+ b_wait(char **), b_limit(char **), b_echo(char **);
static struct {
- builtin_t *p;
- char *name;
+ builtin_t *p;
+ char *name;
} builtins[] = {
- { b_break, "break" },
- { b_builtin, "builtin" },
- { b_cd, "cd" },
- { b_echo, "echo" },
- { b_eval, "eval" },
- { b_exec, "exec" },
- { b_exit, "exit" },
- { b_limit, "limit" },
- { b_newpgrp, "newpgrp" },
- { b_return, "return" },
- { b_shift, "shift" },
- { b_umask, "umask" },
- { b_wait, "wait" },
- { b_dot, "." },
+ { b_break, "break" },
+ { b_builtin, "builtin" },
+ { b_cd, "cd" },
+ { b_echo, "echo" },
+ { b_eval, "eval" },
+ { b_exec, "exec" },
+ { b_exit, "exit" },
+ { b_limit, "limit" },
+ { b_newpgrp, "newpgrp" },
+ { b_return, "return" },
+ { b_shift, "shift" },
+ { b_umask, "umask" },
+ { b_wait, "wait" },
+ { b_dot, "." },
};
extern builtin_t *isbuiltin(char *s) {
#include "rc.h"
char* ARGV0;
- bool dashee, dasheye, dashen, dashpee, interactive;
+ bool interactive;
pid_t rc_pid;
+static bool dashEYE, dashell, dashoh;
+
static void assigndefault(char *,...);
static void checkfd(int, enum redirtype);
void usage(void) {
- printf("Usage: %s [OPTION...] [FILE [ARG...]]\n", ARGV0);
+ printf("Usage: %s [OPTION...] [FILE [ARG...]]\n", ARGV0);
}
-
+ /* open an fd on /dev/null if it is inherited closed */
+ static void checkfd(int fd, enum redirtype r) {
+ int new = rc_open("/dev/null", r);
+ if (new != fd && new != -1)
+ close(new);
+ }
+
extern int main(int argc, char *argv[], char *envp[]) {
- char *dollarzero, *null[1];
- initprint();
- ARGV0 = dollarzero = argv[0];
- rc_pid = getpid();
+ char *dashsee[2], *dollarzero, *null[1];
+ initprint();
+ dashsee[0] = dashsee[1] = NULL;
+ dollarzero = argv[0];
+ rc_pid = getpid();
+ dashell = (*argv[0] == '-'); /* login shell, per unix tradition */
+
+ OPTBEGIN {
+ case 'e': dashee = true; break;
+ case 'l': dashell = true; break;
- case 'n': dashen = true; break;
+ case 'o': dashoh = true; break;
+ case 'p': dashpee = true; break;
- case 'i':
- dasheye = true;
- interactive = true;
- break;
- case 'I':
- dashEYE = true;
- interactive = false;
- break;
+ case 'c':
+ dashsee[0] = EOPTARG(usage());
+ break;
+ } OPTEND;
- /* use isatty() iff neither -i nor -I is set, and iff the input is not from a script or -c flags */
- interactive = isatty(0);
- checkfd(0, rFrom);
- checkfd(1, rCreate);
- checkfd(2, rCreate);
-
- initsignal();
- inithash();
- initparse();
- assigndefault("ifs", " ", "\t", "\n", (void *)0);
- assigndefault("path", DEFAULTPATH, (void *)0);
- assigndefault("pid", nprint("%d", rc_pid), (void *)0);
- assigndefault("prompt", "; ", "", (void *)0);
- assigndefault("version", VERSION, "$Release: @(#)" PACKAGE " " VERSION " " RELDATE " $", (void *)0);
- initenv(envp);
- initinput();
- null[0] = NULL;
- starassign(dollarzero, null, false); /* assign $0 to $* */
- inithandler();
+ /* use isatty() iff neither -i nor -I is set, and iff the input is not from a script or -c flags */
- if (!dasheye && !dashEYE && dashsee[0] == NULL && (*argv == NULL))
- interactive = isatty(0);
- if (!dashoh) {
- checkfd(0, rFrom);
- checkfd(1, rCreate);
- checkfd(2, rCreate);
- }
++ interactive = isatty(0);
++ checkfd(0, rFrom);
++ checkfd(1, rCreate);
++ checkfd(2, rCreate);
+
- pushfd(0); // Read stdin
- doit(true);
- rc_exit(getstatus());
- return 0; /* Never really reached. */
+ initsignal();
+ inithash();
+ initparse();
+ assigndefault("ifs", " ", "\t", "\n", (void *)0);
- #ifdef DEFAULTPATH
+ assigndefault("path", DEFAULTPATH, (void *)0);
- #endif
+ assigndefault("pid", nprint("%d", rc_pid), (void *)0);
+ assigndefault("prompt", "; ", "", (void *)0);
+ assigndefault("version", VERSION, "$Release: @(#)" PACKAGE " " VERSION " " RELDATE " $", (void *)0);
+ initenv(envp);
+ initinput();
+ null[0] = NULL;
+ starassign(dollarzero, null, false); /* assign $0 to $* */
+ inithandler();
++
++ pushfd(0); // setup to read stdin
+
- if (dashell) {
- char *rcrc;
- int fd;
-
- rcrc = concat(varlookup("home"), word("/.rcrc", NULL))->w;
- fd = rc_open(rcrc, rFrom);
- if (fd == -1) {
- if (errno != ENOENT)
- uerror(rcrc);
- } else {
- bool push_interactive;
-
- pushfd(fd);
- push_interactive = interactive;
- interactive = false;
- doit(true);
- interactive = push_interactive;
- close(fd);
- }
- }
-
- if (dashsee[0] != NULL) { /* input from -c or -s? */
- if (*argv != NULL)
- starassign(dollarzero, argv, false);
- pushstring(dashsee, true);
- } else if (*argv != NULL) { /* else from a file? */
- b_dot(--argv);
- rc_exit(getstatus());
- } else { /* else stdin */
- pushfd(0);
- }
- dasheye = false;
+ doit(true);
+ rc_exit(getstatus());
+ return 0; /* Never really reached. */
}
- static void assigndefault(char *name,...) {
- va_list ap;
- List *l;
- char *v;
- va_start(ap, name);
- for (l = NULL; (v = va_arg(ap, char *)) != NULL;)
- l = append(l, word(v, NULL));
- varassign(name, l, false);
- set_exportable(name, false);
- if (streq(name, "path"))
- alias(name, l, false);
- va_end(ap);
- }
-
- /* open an fd on /dev/null if it is inherited closed */
-
- static void checkfd(int fd, enum redirtype r) {
- int new = rc_open("/dev/null", r);
- if (new != fd && new != -1)
- close(new);
+ static void assigndefault(char *name, ...) {
+ va_list ap;
+ List *l;
+ char *v;
+ va_start(ap, name);
+ for (l = NULL; (v = va_arg(ap, char *)) != NULL;)
+ l = append(l, word(v, NULL));
+ varassign(name, l, false);
+ set_exportable(name, false);
+ if (streq(name, "path"))
+ alias(name, l, false);
+ va_end(ap);
}
-