xref: /dflybsd-src/usr.bin/window/cmd1.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	@(#)cmd1.c	8.1 (Berkeley) 6/6/93	*/
2*86d7f5d3SJohn Marino /*	$NetBSD: cmd1.c,v 1.7 2003/08/07 11:17:22 agc Exp $	*/
3*86d7f5d3SJohn Marino 
4*86d7f5d3SJohn Marino /*
5*86d7f5d3SJohn Marino  * Copyright (c) 1983, 1993
6*86d7f5d3SJohn Marino  *	The Regents of the University of California.  All rights reserved.
7*86d7f5d3SJohn Marino  *
8*86d7f5d3SJohn Marino  * This code is derived from software contributed to Berkeley by
9*86d7f5d3SJohn Marino  * Edward Wang at The University of California, Berkeley.
10*86d7f5d3SJohn Marino  *
11*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
12*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
13*86d7f5d3SJohn Marino  * are met:
14*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
15*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
16*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
17*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
18*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
19*86d7f5d3SJohn Marino  * 3. Neither the name of the University nor the names of its contributors
20*86d7f5d3SJohn Marino  *    may be used to endorse or promote products derived from this software
21*86d7f5d3SJohn Marino  *    without specific prior written permission.
22*86d7f5d3SJohn Marino  *
23*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*86d7f5d3SJohn Marino  * SUCH DAMAGE.
34*86d7f5d3SJohn Marino  */
35*86d7f5d3SJohn Marino 
36*86d7f5d3SJohn Marino #include "defs.h"
37*86d7f5d3SJohn Marino #include "char.h"
38*86d7f5d3SJohn Marino 
39*86d7f5d3SJohn Marino void
c_window(void)40*86d7f5d3SJohn Marino c_window(void)
41*86d7f5d3SJohn Marino {
42*86d7f5d3SJohn Marino 	int col, row, xcol, xrow;
43*86d7f5d3SJohn Marino 	int id;
44*86d7f5d3SJohn Marino 
45*86d7f5d3SJohn Marino 	if ((id = findid()) < 0)
46*86d7f5d3SJohn Marino 		return;
47*86d7f5d3SJohn Marino 	if (!terse)
48*86d7f5d3SJohn Marino 		wwputs("New window (upper left corner): ", cmdwin);
49*86d7f5d3SJohn Marino 	col = 0;
50*86d7f5d3SJohn Marino 	row = 1;
51*86d7f5d3SJohn Marino 	wwadd(boxwin, framewin->ww_back);
52*86d7f5d3SJohn Marino 	for (;;) {
53*86d7f5d3SJohn Marino 		wwbox(boxwin, row - 1, col - 1, 3, 3);
54*86d7f5d3SJohn Marino 		wwsetcursor(row, col);
55*86d7f5d3SJohn Marino 		while (wwpeekc() < 0)
56*86d7f5d3SJohn Marino 			wwiomux();
57*86d7f5d3SJohn Marino 		switch (getpos(&row, &col, row > 1, 0,
58*86d7f5d3SJohn Marino 			wwnrow - 1, wwncol - 1)) {
59*86d7f5d3SJohn Marino 		case 3:
60*86d7f5d3SJohn Marino 			wwunbox(boxwin);
61*86d7f5d3SJohn Marino 			wwdelete(boxwin);
62*86d7f5d3SJohn Marino 			return;
63*86d7f5d3SJohn Marino 		case 2:
64*86d7f5d3SJohn Marino 			wwunbox(boxwin);
65*86d7f5d3SJohn Marino 			break;
66*86d7f5d3SJohn Marino 		case 1:
67*86d7f5d3SJohn Marino 			wwunbox(boxwin);
68*86d7f5d3SJohn Marino 		case 0:
69*86d7f5d3SJohn Marino 			continue;
70*86d7f5d3SJohn Marino 		}
71*86d7f5d3SJohn Marino 		break;
72*86d7f5d3SJohn Marino 	}
73*86d7f5d3SJohn Marino 	if (!terse)
74*86d7f5d3SJohn Marino 		wwputs("\nNew window (lower right corner): ", cmdwin);
75*86d7f5d3SJohn Marino 	xcol = col;
76*86d7f5d3SJohn Marino 	xrow = row;
77*86d7f5d3SJohn Marino 	for (;;) {
78*86d7f5d3SJohn Marino 		wwbox(boxwin, row - 1, col - 1,
79*86d7f5d3SJohn Marino 			xrow - row + 3, xcol - col + 3);
80*86d7f5d3SJohn Marino 		wwsetcursor(xrow, xcol);
81*86d7f5d3SJohn Marino 		while (wwpeekc() < 0)
82*86d7f5d3SJohn Marino 			wwiomux();
83*86d7f5d3SJohn Marino 		switch (getpos(&xrow, &xcol, row, col, wwnrow - 1, wwncol - 1))
84*86d7f5d3SJohn Marino 		{
85*86d7f5d3SJohn Marino 		case 3:
86*86d7f5d3SJohn Marino 			wwunbox(boxwin);
87*86d7f5d3SJohn Marino 			wwdelete(boxwin);
88*86d7f5d3SJohn Marino 			return;
89*86d7f5d3SJohn Marino 		case 2:
90*86d7f5d3SJohn Marino 			wwunbox(boxwin);
91*86d7f5d3SJohn Marino 			break;
92*86d7f5d3SJohn Marino 		case 1:
93*86d7f5d3SJohn Marino 			wwunbox(boxwin);
94*86d7f5d3SJohn Marino 		case 0:
95*86d7f5d3SJohn Marino 			continue;
96*86d7f5d3SJohn Marino 		}
97*86d7f5d3SJohn Marino 		break;
98*86d7f5d3SJohn Marino 	}
99*86d7f5d3SJohn Marino 	wwdelete(boxwin);
100*86d7f5d3SJohn Marino 	if (!terse)
101*86d7f5d3SJohn Marino 		wwputc('\n', cmdwin);
102*86d7f5d3SJohn Marino 	wwcurtowin(cmdwin);
103*86d7f5d3SJohn Marino 	(void) openwin(id, row, col, xrow-row+1, xcol-col+1, default_nline,
104*86d7f5d3SJohn Marino 	    (char *) 0, WWT_PTY, WWU_HASFRAME, default_shellfile,
105*86d7f5d3SJohn Marino 	    default_shell);
106*86d7f5d3SJohn Marino }
107*86d7f5d3SJohn Marino 
108*86d7f5d3SJohn Marino int
getpos(int * row,int * col,int minrow,int mincol,int maxrow,int maxcol)109*86d7f5d3SJohn Marino getpos(int *row, int *col, int minrow, int mincol, int maxrow, int maxcol)
110*86d7f5d3SJohn Marino {
111*86d7f5d3SJohn Marino 	static int scount;
112*86d7f5d3SJohn Marino 	int count;
113*86d7f5d3SJohn Marino 	int c;
114*86d7f5d3SJohn Marino 	int oldrow = *row, oldcol = *col;
115*86d7f5d3SJohn Marino 
116*86d7f5d3SJohn Marino 	while ((c = wwgetc()) >= 0) {
117*86d7f5d3SJohn Marino 		switch (c) {
118*86d7f5d3SJohn Marino 		case '0': case '1': case '2': case '3': case '4':
119*86d7f5d3SJohn Marino 		case '5': case '6': case '7': case '8': case '9':
120*86d7f5d3SJohn Marino 			scount = scount * 10 + c - '0';
121*86d7f5d3SJohn Marino 			continue;
122*86d7f5d3SJohn Marino 		}
123*86d7f5d3SJohn Marino 		count = scount ? scount : 1;
124*86d7f5d3SJohn Marino 		scount = 0;
125*86d7f5d3SJohn Marino 		switch (c) {
126*86d7f5d3SJohn Marino 		case 'h':
127*86d7f5d3SJohn Marino 			if ((*col -= count) < mincol)
128*86d7f5d3SJohn Marino 				*col = mincol;
129*86d7f5d3SJohn Marino 			break;
130*86d7f5d3SJohn Marino 		case 'H':
131*86d7f5d3SJohn Marino 			*col = mincol;
132*86d7f5d3SJohn Marino 			break;
133*86d7f5d3SJohn Marino 		case 'l':
134*86d7f5d3SJohn Marino 			if ((*col += count) > maxcol)
135*86d7f5d3SJohn Marino 				*col = maxcol;
136*86d7f5d3SJohn Marino 			break;
137*86d7f5d3SJohn Marino 		case 'L':
138*86d7f5d3SJohn Marino 			*col = maxcol;
139*86d7f5d3SJohn Marino 			break;
140*86d7f5d3SJohn Marino 		case 'j':
141*86d7f5d3SJohn Marino 			if ((*row += count) > maxrow)
142*86d7f5d3SJohn Marino 				*row = maxrow;
143*86d7f5d3SJohn Marino 			break;
144*86d7f5d3SJohn Marino 		case 'J':
145*86d7f5d3SJohn Marino 			*row = maxrow;
146*86d7f5d3SJohn Marino 			break;
147*86d7f5d3SJohn Marino 		case 'k':
148*86d7f5d3SJohn Marino 			if ((*row -= count) < minrow)
149*86d7f5d3SJohn Marino 				*row = minrow;
150*86d7f5d3SJohn Marino 			break;
151*86d7f5d3SJohn Marino 		case 'K':
152*86d7f5d3SJohn Marino 			*row = minrow;
153*86d7f5d3SJohn Marino 			break;
154*86d7f5d3SJohn Marino 		case ctrl('['):
155*86d7f5d3SJohn Marino 			if (!terse)
156*86d7f5d3SJohn Marino 				wwputs("\nCancelled.  ", cmdwin);
157*86d7f5d3SJohn Marino 			return 3;
158*86d7f5d3SJohn Marino 		case '\r':
159*86d7f5d3SJohn Marino 			return 2;
160*86d7f5d3SJohn Marino 		default:
161*86d7f5d3SJohn Marino 			if (!terse)
162*86d7f5d3SJohn Marino 				wwputs("\nType [hjklHJKL] to move, return to enter position, escape to cancel.", cmdwin);
163*86d7f5d3SJohn Marino 			wwbell();
164*86d7f5d3SJohn Marino 		}
165*86d7f5d3SJohn Marino 	}
166*86d7f5d3SJohn Marino 	return oldrow != *row || oldcol != *col;
167*86d7f5d3SJohn Marino }
168