xref: /onnv-gate/usr/src/lib/libeti/menu/common/driver.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Mircrosystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*LINTLIBRARY*/
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include "private.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate int
menu_driver(MENU * m,int c)39*0Sstevel@tonic-gate menu_driver(MENU *m, int c)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	int i, n;
42*0Sstevel@tonic-gate 	int top;
43*0Sstevel@tonic-gate 	ITEM *current;
44*0Sstevel@tonic-gate 	int ret;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	if (!m) {
47*0Sstevel@tonic-gate 		return (E_BAD_ARGUMENT);
48*0Sstevel@tonic-gate 	}
49*0Sstevel@tonic-gate 	ret = E_OK;
50*0Sstevel@tonic-gate 	if (Indriver(m)) {
51*0Sstevel@tonic-gate 		return (E_BAD_STATE);
52*0Sstevel@tonic-gate 	}
53*0Sstevel@tonic-gate 	if (!Posted(m)) {
54*0Sstevel@tonic-gate 		return (E_NOT_POSTED);
55*0Sstevel@tonic-gate 	}
56*0Sstevel@tonic-gate 	top = Top(m);
57*0Sstevel@tonic-gate 	current = Current(m);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	if (c > KEY_MAX && c < MAX_COMMAND) {
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 		/* Clear the pattern buffer if not one of these requests */
62*0Sstevel@tonic-gate 		if (c != REQ_BACK_PATTERN &&
63*0Sstevel@tonic-gate 		    c != REQ_NEXT_MATCH &&
64*0Sstevel@tonic-gate 		    c != REQ_PREV_MATCH) {
65*0Sstevel@tonic-gate 			Pindex(m) = 0;
66*0Sstevel@tonic-gate 			IthPattern(m, 0) = '\0';
67*0Sstevel@tonic-gate 		}
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 		switch (c) {
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 		case REQ_RIGHT_ITEM: {
72*0Sstevel@tonic-gate 			if (Right(current) == (ITEM *)0) {
73*0Sstevel@tonic-gate 					ret = E_REQUEST_DENIED;
74*0Sstevel@tonic-gate 					break;
75*0Sstevel@tonic-gate 			}
76*0Sstevel@tonic-gate 			current = Right(current);
77*0Sstevel@tonic-gate 			break;
78*0Sstevel@tonic-gate 		}
79*0Sstevel@tonic-gate 		case REQ_LEFT_ITEM: {
80*0Sstevel@tonic-gate 			if (Left(current) == (ITEM *)0) {
81*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
82*0Sstevel@tonic-gate 				break;
83*0Sstevel@tonic-gate 			}
84*0Sstevel@tonic-gate 			current = Left(current);
85*0Sstevel@tonic-gate 			break;
86*0Sstevel@tonic-gate 		}
87*0Sstevel@tonic-gate 		case REQ_UP_ITEM: {
88*0Sstevel@tonic-gate 			if (Up(current) == (ITEM *)0) {
89*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
90*0Sstevel@tonic-gate 				break;
91*0Sstevel@tonic-gate 			}
92*0Sstevel@tonic-gate 			current = Up(current);
93*0Sstevel@tonic-gate 			break;
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 		case REQ_DOWN_ITEM: {
96*0Sstevel@tonic-gate 			if (Down(current) == (ITEM *)0) {
97*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
98*0Sstevel@tonic-gate 				break;
99*0Sstevel@tonic-gate 			}
100*0Sstevel@tonic-gate 			current = Down(current);
101*0Sstevel@tonic-gate 			break;
102*0Sstevel@tonic-gate 		}
103*0Sstevel@tonic-gate 		case REQ_SCR_ULINE: {
104*0Sstevel@tonic-gate 			if (--top < 0) {
105*0Sstevel@tonic-gate 				++top;
106*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
107*0Sstevel@tonic-gate 				break;
108*0Sstevel@tonic-gate 			}
109*0Sstevel@tonic-gate 			current = Up(current);
110*0Sstevel@tonic-gate 			break;
111*0Sstevel@tonic-gate 		}
112*0Sstevel@tonic-gate 		case REQ_SCR_DLINE: {
113*0Sstevel@tonic-gate 			if (++top > Rows(m) - Height(m)) {
114*0Sstevel@tonic-gate 				--top;
115*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
116*0Sstevel@tonic-gate 				break;
117*0Sstevel@tonic-gate 			}
118*0Sstevel@tonic-gate 			current = Down(current);
119*0Sstevel@tonic-gate 			break;
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 		case REQ_SCR_UPAGE: {
122*0Sstevel@tonic-gate 			n = min(Height(m), top);
123*0Sstevel@tonic-gate 			if (n) {
124*0Sstevel@tonic-gate 				top -= n;
125*0Sstevel@tonic-gate 				for (i = n; i--; ) {
126*0Sstevel@tonic-gate 					current = Up(current);
127*0Sstevel@tonic-gate 				}
128*0Sstevel@tonic-gate 			} else {
129*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
130*0Sstevel@tonic-gate 				break;
131*0Sstevel@tonic-gate 			}
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 		case REQ_SCR_DPAGE: {
135*0Sstevel@tonic-gate 			n = min(Height(m), Rows(m) - Height(m) - top);
136*0Sstevel@tonic-gate 			if (n) {
137*0Sstevel@tonic-gate 				top += n;
138*0Sstevel@tonic-gate 				for (i = n; i--; ) {
139*0Sstevel@tonic-gate 					current = Down(current);
140*0Sstevel@tonic-gate 				}
141*0Sstevel@tonic-gate 			} else {
142*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
143*0Sstevel@tonic-gate 				break;
144*0Sstevel@tonic-gate 			}
145*0Sstevel@tonic-gate 			break;
146*0Sstevel@tonic-gate 		}
147*0Sstevel@tonic-gate 		case REQ_FIRST_ITEM: {
148*0Sstevel@tonic-gate 			current = IthItem(m, 0);
149*0Sstevel@tonic-gate 			break;
150*0Sstevel@tonic-gate 		}
151*0Sstevel@tonic-gate 		case REQ_LAST_ITEM: {
152*0Sstevel@tonic-gate 			current = IthItem(m, Nitems(m)-1);
153*0Sstevel@tonic-gate 			break;
154*0Sstevel@tonic-gate 		}
155*0Sstevel@tonic-gate 		case REQ_NEXT_MATCH: {
156*0Sstevel@tonic-gate 			if (IthPattern(m, 0) != NULL) {
157*0Sstevel@tonic-gate 				ret = _match(m, NULL, &current);
158*0Sstevel@tonic-gate 			} else {
159*0Sstevel@tonic-gate 				if (Index(current)+1 >= Nitems(m)) {
160*0Sstevel@tonic-gate 					current = IthItem(m, 0);
161*0Sstevel@tonic-gate 				} else {
162*0Sstevel@tonic-gate 					current = IthItem(m, Index(current)+1);
163*0Sstevel@tonic-gate 				}
164*0Sstevel@tonic-gate 			}
165*0Sstevel@tonic-gate 			break;
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate 		case REQ_NEXT_ITEM: {
168*0Sstevel@tonic-gate 			if (Index(current)+1 >= Nitems(m)) {
169*0Sstevel@tonic-gate 				if (Cyclic(m)) {
170*0Sstevel@tonic-gate 					current = IthItem(m, 0);
171*0Sstevel@tonic-gate 				} else {
172*0Sstevel@tonic-gate 					ret = E_REQUEST_DENIED;
173*0Sstevel@tonic-gate 				}
174*0Sstevel@tonic-gate 			} else {
175*0Sstevel@tonic-gate 				current = IthItem(m, Index(current)+1);
176*0Sstevel@tonic-gate 			}
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 		case REQ_PREV_MATCH: {
180*0Sstevel@tonic-gate 			if (IthPattern(m, 0) != NULL) {
181*0Sstevel@tonic-gate 				ret = _match(m, '\b', &current);
182*0Sstevel@tonic-gate 			} else {
183*0Sstevel@tonic-gate 				/* This differs from PREV_ITEM in that */
184*0Sstevel@tonic-gate 				/* it is cyclic */
185*0Sstevel@tonic-gate 				if (Index(current)-1 < 0) {
186*0Sstevel@tonic-gate 					current = IthItem(m, Nitems(m)-1);
187*0Sstevel@tonic-gate 				} else {
188*0Sstevel@tonic-gate 					current = IthItem(m, Index(current)-1);
189*0Sstevel@tonic-gate 				}
190*0Sstevel@tonic-gate 			}
191*0Sstevel@tonic-gate 			break;
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 		case REQ_PREV_ITEM: {
194*0Sstevel@tonic-gate 			if (Index(current)-1 < 0) {
195*0Sstevel@tonic-gate 				if (Cyclic(m)) {
196*0Sstevel@tonic-gate 					current = IthItem(m, Nitems(m)-1);
197*0Sstevel@tonic-gate 				} else {
198*0Sstevel@tonic-gate 					ret = E_REQUEST_DENIED;
199*0Sstevel@tonic-gate 				}
200*0Sstevel@tonic-gate 			} else {
201*0Sstevel@tonic-gate 				current = IthItem(m, Index(current)-1);
202*0Sstevel@tonic-gate 			}
203*0Sstevel@tonic-gate 			break;
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 		case REQ_TOGGLE_ITEM: {
206*0Sstevel@tonic-gate 			if (!OneValue(m)) {
207*0Sstevel@tonic-gate 				if (Selectable(Current(m))) {
208*0Sstevel@tonic-gate 					Value(Current(m)) ^= TRUE;
209*0Sstevel@tonic-gate 					_move_post_item(m, Current(m));
210*0Sstevel@tonic-gate 					_show(m);
211*0Sstevel@tonic-gate 				} else {
212*0Sstevel@tonic-gate 					ret = E_NOT_SELECTABLE;
213*0Sstevel@tonic-gate 				}
214*0Sstevel@tonic-gate 			} else {
215*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
216*0Sstevel@tonic-gate 			}
217*0Sstevel@tonic-gate 			break;
218*0Sstevel@tonic-gate 		}
219*0Sstevel@tonic-gate 		case REQ_BACK_PATTERN: {
220*0Sstevel@tonic-gate 			if (Pindex(m) > 0) {
221*0Sstevel@tonic-gate 				Pindex(m) -= 1;
222*0Sstevel@tonic-gate 				IthPattern(m, Pindex(m)) = '\0';
223*0Sstevel@tonic-gate 				_position_cursor(m);
224*0Sstevel@tonic-gate 			} else {
225*0Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
226*0Sstevel@tonic-gate 			}
227*0Sstevel@tonic-gate 			break;
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 		case REQ_CLEAR_PATTERN: {
230*0Sstevel@tonic-gate 			/* This was already done at the top */
231*0Sstevel@tonic-gate 			break;
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 		default: {
234*0Sstevel@tonic-gate 			ret = E_UNKNOWN_COMMAND;
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 		}
237*0Sstevel@tonic-gate 	} else {
238*0Sstevel@tonic-gate 		if (c > 037 && c < 0177) {
239*0Sstevel@tonic-gate 			/*LINTED [E_PASS_INT_TO_SMALL_INT]*/
240*0Sstevel@tonic-gate 			ret = _match(m, c, &current);
241*0Sstevel@tonic-gate 		} else {
242*0Sstevel@tonic-gate 			ret = E_UNKNOWN_COMMAND;
243*0Sstevel@tonic-gate 		}
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	/* Verify the location of the top row */
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	_chk_top(m, &top, current);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	/* Go change the actual values of Top and Current and do init/term */
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	_affect_change(m, top, current);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	return (ret);
255*0Sstevel@tonic-gate }
256