return the number of chars written.
[unix-history] / usr / src / usr.bin / window / cmd.c
CommitLineData
f6dc361a 1#ifndef lint
f5dd0347 2static char *sccsid = "@(#)cmd.c 3.19 83/12/09";
f6dc361a
EW
3#endif
4
5#include "defs.h"
6
f6dc361a
EW
7docmd()
8{
9 register char c;
10 register struct ww *w;
f6dc361a 11
74329e4e 12 if (!terse)
bb05dfb5 13 wwadd(cmdwin, &wwhead);
c417b691 14 if (selwin != 0)
73218728 15 wwcursor(selwin, 1);
86845af3
EW
16 for (;;) {
17 while ((c = bgetc()) >= 0) {
18 if (!terse)
19 (void) wwputs("\r\n", cmdwin);
20 switch (c) {
21 default:
22 if (c == escapec)
23 goto foo;
f6dc361a 24 break;
86845af3
EW
25 case 'h': case 'j': case 'k': case 'l':
26 case CTRL(y):
27 case CTRL(e):
28 case CTRL(u):
29 case CTRL(d):
30 case CTRL(b):
31 case CTRL(f):
32 case CTRL(s):
33 case CTRL(q):
34 case CTRL([):
35 foo:
36 if (selwin == 0) {
37 error("No window.");
38 continue;
39 }
f6dc361a 40 }
86845af3
EW
41 switch (c) {
42 case '1': case '2': case '3': case '4': case '5':
43 case '6': case '7': case '8': case '9':
44 if ((w = window[c - '1']) == 0) {
7c0483b9 45 error("%c: No such window.", c);
86845af3
EW
46 break;
47 }
4c8ea7ce 48 setselwin(w);
86845af3
EW
49 if (checkproc(selwin) >= 0)
50 incmd = 0;
51 break;
52 case '%':
53 if ((w = getwin()) != 0)
54 setselwin(w);
55 break;
7c0483b9
EW
56 case CTRL(^):
57 if (lastselwin != 0) {
58 setselwin(lastselwin);
59 if (checkproc(selwin) >= 0)
60 incmd = 0;
61 } else
62 error("No previous window.");
63 break;
86845af3
EW
64 case 'c':
65 if ((w = getwin()) != 0)
66 c_close(w);
67 break;
68 case 'C':
69 c_close((struct ww *)0);
70 break;
71 case 'w':
72 c_window();
73 break;
74 case 'm':
75 if ((w = getwin()) != 0)
76 c_move(w);
77 break;
f176f953
EW
78 case 'M':
79 if ((w = getwin()) != 0)
80 movewin(w, w->ww_altpos.r,
81 w->ww_altpos.c);
82 break;
86845af3
EW
83 case 'S':
84 c_show();
85 break;
86 case 'L':
87 c_list();
88 break;
0d290ee3
EW
89 case 'v':
90 c_variable();
91 break;
86845af3
EW
92 case ':':
93 c_colon();
94 break;
95 case 'h':
96 (void) wwwrite(selwin, "\b", 1);
97 break;
98 case 'j':
99 (void) wwwrite(selwin, "\n", 1);
100 break;
101 case 'k':
102 (void) wwwrite(selwin, "\033A", 2);
103 break;
104 case 'l':
105 (void) wwwrite(selwin, "\033C", 2);
106 break;
107 case CTRL(e):
108 wwscroll(selwin, 1);
109 break;
110 case CTRL(y):
111 wwscroll(selwin, -1);
112 break;
113 case CTRL(d):
114 wwscroll(selwin, selwin->ww_w.nr / 2);
115 break;
116 case CTRL(u):
117 wwscroll(selwin, - selwin->ww_w.nr / 2);
118 break;
119 case CTRL(f):
120 wwscroll(selwin, selwin->ww_w.nr);
121 break;
122 case CTRL(b):
123 wwscroll(selwin, - selwin->ww_w.nr);
124 break;
125 case CTRL(s):
126 (void) write(selwin->ww_pty,
127 &wwwintty.ww_tchars.t_stopc, 1);
128 break;
129 case CTRL(q):
130 (void) write(selwin->ww_pty,
131 &wwwintty.ww_tchars.t_startc, 1);
132 break;
133 case CTRL(l):
134 wwredraw();
135 break;
136 case '?':
137 c_help();
138 break;
139 case CTRL([):
140 if (checkproc(selwin) >= 0)
141 incmd = 0;
142 break;
143 case CTRL(z):
144 wwsuspend();
145 break;
146 case 'q':
147 c_quit();
148 break;
149 /* undocumented commands */
150 case 's':
151 c_stat();
152 break;
57fc9db6 153#ifndef O_4_1A
86845af3
EW
154 case 't':
155 c_time(RUSAGE_SELF);
156 break;
157 case 'T':
158 c_time(RUSAGE_CHILDREN);
159 break;
57fc9db6 160#endif
f176f953
EW
161 /* debugging stuff */
162 case '&':
163 if (debug) {
164 c_debug();
165 break;
166 }
86845af3 167 default:
86845af3
EW
168 if (c == escapec) {
169 if (checkproc(selwin) >= 0) {
170 (void) write(selwin->ww_pty,
171 &escapec, 1);
172 incmd = 0;
173 }
174 } else {
175 if (!terse)
176 wwbell();
177 error("Type ? for help.");
178 }
74329e4e 179 }
f6dc361a 180 }
86845af3
EW
181 if (!incmd || quit)
182 break;
183 if (terse)
184 wwsetcursor(0, 0);
185 else {
bb05dfb5 186 (void) wwputs("Command: ", cmdwin);
86845af3
EW
187 wwcurtowin(cmdwin);
188 }
189 while (bpeekc() < 0)
190 bread();
101acab4 191 }
bb05dfb5 192 if (!quit) {
bb05dfb5
EW
193 if (!terse) {
194 wwdelete(cmdwin);
195 reframe();
196 }
73218728 197 wwcursor(selwin, 0);
86845af3 198 }
f6dc361a
EW
199}
200
201struct ww *
202getwin()
203{
204 register int c;
4c8ea7ce 205 struct ww *w = 0;
f6dc361a 206
101acab4 207 if (!terse)
bb05dfb5 208 (void) wwputs("Which window? ", cmdwin);
f2a77fe1 209 wwcurtowin(cmdwin);
b4be6cd6 210 while ((c = bgetc()) < 0)
f6dc361a 211 bread();
bb05dfb5
EW
212 if (debug && c == 'c')
213 w = cmdwin;
214 else if (debug && c == 'f')
215 w = framewin;
720ae37c
EW
216 else if (debug && c == 'b')
217 w = boxwin;
bb05dfb5
EW
218 else if (c >= '1' && c < NWINDOW + '1')
219 w = window[c - '1'];
220 if (w == 0)
221 wwbell();
101acab4 222 if (!terse)
bb05dfb5 223 (void) wwputs("\r\n", cmdwin);
f6dc361a
EW
224 return w;
225}
226
86845af3
EW
227checkproc(w)
228struct ww *w;
229{
230 if (w->ww_state != WWS_HASPROC) {
231 error("No process in window.");
232 return -1;
233 }
234 return 0;
235}
236
f6dc361a 237setselwin(w)
fe24a747
EW
238struct ww *w;
239{
f5dd0347
EW
240 if (selwin == w)
241 return;
7c0483b9 242 lastselwin = selwin;
a200c620 243 front(selwin = w, 1);
fe24a747
EW
244}
245
27b31336 246/*
f176f953 247 * wwvisible() doesn't work for tinted windows.
27b31336
EW
248 * But anything to make it faster.
249 */
a200c620 250front(w, doreframe)
f6dc361a
EW
251register struct ww *w;
252{
84ad208b
EW
253 if (!wwvisible(w) && w->ww_back != framewin) {
254 wwdelete(w);
255 wwadd(w, framewin);
fe24a747 256 reframe();
a200c620
EW
257 } else if (doreframe)
258 reframe();
fe24a747
EW
259}
260
261reframe()
262{
263 register struct ww *w;
264
265 wwunframe(framewin);
266 for (w = wwhead.ww_back; w != &wwhead; w = w->ww_back)
267 if (w->ww_hasframe) {
268 wwframe(w, framewin);
269 labelwin(w);
270 }
f6dc361a
EW
271}
272
4c8ea7ce 273labelwin(w)
f6dc361a
EW
274register struct ww *w;
275{
bb05dfb5
EW
276 int mode = w == selwin ? WWM_REV : 0;
277
278 if (w->ww_id >= 0) {
279 char buf[2];
f6dc361a 280
bb05dfb5
EW
281 buf[0] = w->ww_id + '1';
282 buf[1] = 0;
03e75950 283 wwlabel(w, framewin, 1, buf, mode);
bb05dfb5
EW
284 }
285 if (w->ww_label) {
286 int col;
287
288 if (w->ww_center) {
289 col = (w->ww_w.nc - strlen(w->ww_label)) / 2;
290 col = MAX(3, col);
291 } else
292 col = 3;
03e75950 293 wwlabel(w, framewin, col, w->ww_label, mode);
bb05dfb5 294 }
f6dc361a 295}