xref: /original-bsd/usr.bin/window/main.c (revision 641d4200)
1 #ifndef lint
2 static	char *sccsid = "@(#)main.c	3.1 83/08/11";
3 #endif
4 
5 #include "defs.h"
6 
7 char escapec = CTRL(p);
8 
9 #define next(a) (*++*(a) ? *(a) : (*++(a) ? *(a) : (char *)usage()))
10 
11 /*ARGSUSED*/
12 main(argc, argv)
13 char **argv;
14 {
15 	register n;
16 	register char *p;
17 	char fast = 0;
18 	int imask;
19 	struct timezone timezone;
20 
21 	if (p = rindex(*argv, '/'))
22 		p++;
23 	else
24 		p = *argv;
25 	debug = strcmp(p, "a.out") == 0;
26 	while (*++argv) {
27 		if (**argv == '-') {
28 			switch (*++*argv) {
29 			case 'f':
30 				fast++;
31 				break;
32 			case 'e':
33 				setescape(next(argv));
34 				break;
35 			case 't':
36 				terse++;
37 				break;
38 			case 'd':
39 				debug++;
40 				break;
41 			default:
42 				(void) usage();
43 			}
44 		} else
45 			(void) usage();
46 	}
47 	if ((shell = getenv("SHELL")) == 0)
48 		shell = "/bin/csh";
49 	if (shellname = rindex(shell, '/'))
50 		shellname++;
51 	else
52 		shellname = shell;
53 	(void) gettimeofday(&starttime, &timezone);
54 	if (wwinit() < 0) {
55 		(void) fflush(stdout);
56 		(void) fprintf(stderr, "Can't do windows on this terminal.\n");
57 		exit(1);
58 	}
59 	if (debug) {
60 		wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
61 		(void) wwsettty(0, &wwnewtty);
62 	}
63 
64 	if ((cmdwin = wwopen(WWO_REVERSE, 1, wwncol, 0, 0, 0)) == 0) {
65 		(void) wwflush();
66 		(void) fprintf(stderr, "Can't open command window.\r\n");
67 		goto bad;
68 	}
69 	if ((framewin = wwopen(WWO_GLASS, wwnrow, wwncol, 0, 0, 0)) == 0) {
70 		(void) wwflush();
71 		(void) fprintf(stderr, "Can't open frame window.\r\n");
72 		goto bad;
73 	}
74 	wwadd(framewin, &wwhead);
75 
76 	curwin = cmdwin;
77 	wwupdate();
78 	wwflush();
79 	(void) signal(SIGCHLD, wwchild);
80 	if (!fast) {
81 		if (!terse)
82 			wwadd(cmdwin, &wwhead);
83 		if (doconfig() < 0)
84 			dodefault();
85 		if (selwin != 0) {
86 			curwin = selwin;
87 			/*
88 			Woncursor(selwin->ww_win, 0);
89 			*/
90 		}
91 		if (!terse) {
92 			wwdelete(cmdwin);
93 			reframe();
94 		}
95 	}
96 	while (!quit) {
97 		if (curwin == cmdwin) {
98 			docmd();
99 			continue;
100 		}
101 		/*
102 		 * Loop until we get some keyboard input.
103 		 */
104 		while (ibufc == 0) {
105 			wwcurtowin(curwin);
106 			wwupdate();
107 			wwflush();
108 			imask = 1 << 0;
109 			while (wwforce(&imask) < 0)
110 				;
111 			if ((imask & 1 << 0) == 0)
112 				continue;
113 			/* NOTE: ibufc == 0 */
114 			ibufp = ibuf;
115 			if ((ibufc = read(0, ibuf, sizeof ibuf)) < 0) {
116 				ibufc = 0;
117 				nreade++;
118 			} else if (ibufc == 0)
119 				nreadz++;
120 			else
121 				nreadc += ibufc;
122 			nread++;
123 		}
124 		/*
125 		 * Weird loop.  Copy the buffer to the pty
126 		 * and stopping on the escape character
127 		 * in a hopefully efficient way.
128 		 * Probably a good thing to make ibufc == 1 a special
129 		 * case.
130 		 */
131 		for (p = ibufp, n = ibufc;;) {
132 			if (--n < 0) {
133 				(void) write(curwin->ww_pty, ibufp, ibufc);
134 				ibufp = ibuf;
135 				ibufc = 0;
136 				break;
137 			} else if (*p++ == escapec) {
138 				if ((n = p - ibufp) > 1)
139 					(void) write(curwin->ww_pty,
140 						ibufp, n - 1);
141 				ibufp = p;
142 				ibufc -= n;
143 				curwin = cmdwin;
144 				break;
145 			}
146 		}
147 	}
148 	wwupdate();
149 	wwflush();
150 bad:
151 	wwend();
152 	return 0;
153 }
154 
155 usage()
156 {
157 	(void) fprintf(stderr, "window: [-e escape] [-t] [-f]\n");
158 	exit(1);
159 	return 0;			/* for lint */
160 }
161