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