BSD 4_4_Lite1 development
[unix-history] / usr / src / contrib / rc-1.4 / main.c
CommitLineData
2efe1df4
C
1/* main.c: handles initialization of rc and command line options */
2
3#include "rc.h"
4
5bool dashdee, dashee, dashvee, dashex, dashell, dasheye,
6 dashen, dashpee, interactive;
7int rc_pid;
8
9static bool dashoh;
10
11static void assigndefault(char *,...);
12static void checkfd(int, enum redirtype);
13
14extern void main(int argc, char *argv[], char *envp[]) {
15 char *dashsee[2], *dollarzero, *null[1];
16 int c;
17 initprint();
18 dashsee[0] = dashsee[1] = NULL;
19 dollarzero = argv[0];
20 rc_pid = getpid();
21 dashell = (*argv[0] == '-'); /* Unix tradition */
22 while ((c = rc_getopt(argc, argv, "nolpeivdxc:")) != -1)
23 switch (c) {
24 case 'l':
25 dashell = TRUE;
26 break;
27 case 'e':
28 dashee = TRUE;
29 break;
30 case 'i':
31 dasheye = interactive = TRUE;
32 break;
33 case 'v':
34 dashvee = TRUE;
35 break;
36 case 'x':
37 dashex = TRUE;
38 break;
39 case 'd':
40 dashdee = TRUE;
41 break;
42 case 'c':
43 dashsee[0] = rc_optarg;
44 goto quitopts;
45 case 'n':
46 dashen = TRUE;
47 break;
48 case 'p':
49 dashpee = TRUE;
50 break;
51 case 'o':
52 dashoh = TRUE;
53 break;
54 case '?':
55 exit(1);
56 }
57quitopts:
58 argv += rc_optind;
59 /* use isatty() iff -i is not set, and iff the input is not from a script or -c flag */
60 if (!dasheye && dashsee[0] == NULL && *argv == NULL)
61 interactive = isatty(0);
62 if (!dashoh) {
63 checkfd(0, rFrom);
64 checkfd(1, rCreate);
65 checkfd(2, rCreate);
66 }
67 initsignal();
68 inithash();
69 initparse();
70 assigndefault("prompt", "; ", "", (void *)0);
71 assigndefault("path", DEFAULTPATH, (void *)0);
72 assigndefault("ifs", " ", "\t", "\n", (void *)0);
73 assigndefault("pid", nprint("%d", rc_pid), (void *)0);
74 initenv(envp);
75 initinput();
76 null[0] = NULL;
77 starassign(dollarzero, null, FALSE); /* assign $0 to $* */
78 inithandler();
79 if (dashsee[0] != NULL) { /* input from the -c flag? */
80 if (*argv != NULL)
81 starassign(dollarzero, argv, FALSE);
82 pushstring(dashsee, TRUE);
83 } else if (*argv != NULL) { /* else from a file? */
84 b_dot(--argv);
85 rc_exit(getstatus());
86 } else { /* else stdin */
87 pushfd(0);
88 }
89 dasheye = FALSE;
90 doit(TRUE);
91 rc_exit(getstatus());
92}
93
94static void assigndefault(char *name,...) {
95 va_list ap;
96 List *l;
97 char *v;
98 va_start(ap, name);
99 for (l = NULL; (v = va_arg(ap, char *)) != NULL;)
100 l = append(l, word(v, NULL));
101 varassign(name, l, FALSE);
102 if (streq(name, "path"))
103 alias(name, l, FALSE);
104 va_end(ap);
105}
106
107/* open an fd on /dev/null if it is inherited closed */
108
109static void checkfd(int fd, enum redirtype r) {
110 int new = rc_open("/dev/null", r);
111 if (new != fd && new != -1)
112 close(new);
113}