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