17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*03d5549bSToomas Soome /*	  All Rights Reserved	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Mircrosystems, Inc.
287c478bd9Sstevel@tonic-gate  * All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include "private.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate int
menu_driver(MENU * m,int c)377c478bd9Sstevel@tonic-gate menu_driver(MENU *m, int c)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	int i, n;
407c478bd9Sstevel@tonic-gate 	int top;
417c478bd9Sstevel@tonic-gate 	ITEM *current;
427c478bd9Sstevel@tonic-gate 	int ret;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	if (!m) {
457c478bd9Sstevel@tonic-gate 		return (E_BAD_ARGUMENT);
467c478bd9Sstevel@tonic-gate 	}
477c478bd9Sstevel@tonic-gate 	ret = E_OK;
487c478bd9Sstevel@tonic-gate 	if (Indriver(m)) {
497c478bd9Sstevel@tonic-gate 		return (E_BAD_STATE);
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate 	if (!Posted(m)) {
527c478bd9Sstevel@tonic-gate 		return (E_NOT_POSTED);
537c478bd9Sstevel@tonic-gate 	}
547c478bd9Sstevel@tonic-gate 	top = Top(m);
557c478bd9Sstevel@tonic-gate 	current = Current(m);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (c > KEY_MAX && c < MAX_COMMAND) {
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 		/* Clear the pattern buffer if not one of these requests */
607c478bd9Sstevel@tonic-gate 		if (c != REQ_BACK_PATTERN &&
617c478bd9Sstevel@tonic-gate 		    c != REQ_NEXT_MATCH &&
627c478bd9Sstevel@tonic-gate 		    c != REQ_PREV_MATCH) {
637c478bd9Sstevel@tonic-gate 			Pindex(m) = 0;
647c478bd9Sstevel@tonic-gate 			IthPattern(m, 0) = '\0';
657c478bd9Sstevel@tonic-gate 		}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 		switch (c) {
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		case REQ_RIGHT_ITEM: {
707c478bd9Sstevel@tonic-gate 			if (Right(current) == (ITEM *)0) {
717c478bd9Sstevel@tonic-gate 					ret = E_REQUEST_DENIED;
727c478bd9Sstevel@tonic-gate 					break;
737c478bd9Sstevel@tonic-gate 			}
747c478bd9Sstevel@tonic-gate 			current = Right(current);
757c478bd9Sstevel@tonic-gate 			break;
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 		case REQ_LEFT_ITEM: {
787c478bd9Sstevel@tonic-gate 			if (Left(current) == (ITEM *)0) {
797c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
807c478bd9Sstevel@tonic-gate 				break;
817c478bd9Sstevel@tonic-gate 			}
827c478bd9Sstevel@tonic-gate 			current = Left(current);
837c478bd9Sstevel@tonic-gate 			break;
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		case REQ_UP_ITEM: {
867c478bd9Sstevel@tonic-gate 			if (Up(current) == (ITEM *)0) {
877c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
887c478bd9Sstevel@tonic-gate 				break;
897c478bd9Sstevel@tonic-gate 			}
907c478bd9Sstevel@tonic-gate 			current = Up(current);
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		case REQ_DOWN_ITEM: {
947c478bd9Sstevel@tonic-gate 			if (Down(current) == (ITEM *)0) {
957c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
967c478bd9Sstevel@tonic-gate 				break;
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 			current = Down(current);
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 		case REQ_SCR_ULINE: {
1027c478bd9Sstevel@tonic-gate 			if (--top < 0) {
1037c478bd9Sstevel@tonic-gate 				++top;
1047c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
1057c478bd9Sstevel@tonic-gate 				break;
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 			current = Up(current);
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		case REQ_SCR_DLINE: {
1117c478bd9Sstevel@tonic-gate 			if (++top > Rows(m) - Height(m)) {
1127c478bd9Sstevel@tonic-gate 				--top;
1137c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
1147c478bd9Sstevel@tonic-gate 				break;
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 			current = Down(current);
1177c478bd9Sstevel@tonic-gate 			break;
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		case REQ_SCR_UPAGE: {
1207c478bd9Sstevel@tonic-gate 			n = min(Height(m), top);
1217c478bd9Sstevel@tonic-gate 			if (n) {
1227c478bd9Sstevel@tonic-gate 				top -= n;
1237c478bd9Sstevel@tonic-gate 				for (i = n; i--; ) {
1247c478bd9Sstevel@tonic-gate 					current = Up(current);
1257c478bd9Sstevel@tonic-gate 				}
1267c478bd9Sstevel@tonic-gate 			} else {
1277c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
1287c478bd9Sstevel@tonic-gate 				break;
1297c478bd9Sstevel@tonic-gate 			}
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 		case REQ_SCR_DPAGE: {
1337c478bd9Sstevel@tonic-gate 			n = min(Height(m), Rows(m) - Height(m) - top);
1347c478bd9Sstevel@tonic-gate 			if (n) {
1357c478bd9Sstevel@tonic-gate 				top += n;
1367c478bd9Sstevel@tonic-gate 				for (i = n; i--; ) {
1377c478bd9Sstevel@tonic-gate 					current = Down(current);
1387c478bd9Sstevel@tonic-gate 				}
1397c478bd9Sstevel@tonic-gate 			} else {
1407c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
1417c478bd9Sstevel@tonic-gate 				break;
1427c478bd9Sstevel@tonic-gate 			}
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		case REQ_FIRST_ITEM: {
1467c478bd9Sstevel@tonic-gate 			current = IthItem(m, 0);
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 		case REQ_LAST_ITEM: {
1507c478bd9Sstevel@tonic-gate 			current = IthItem(m, Nitems(m)-1);
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 		case REQ_NEXT_MATCH: {
154*03d5549bSToomas Soome 			if (IthPattern(m, 0) != '\0') {
155*03d5549bSToomas Soome 				ret = _match(m, '\0', &current);
1567c478bd9Sstevel@tonic-gate 			} else {
1577c478bd9Sstevel@tonic-gate 				if (Index(current)+1 >= Nitems(m)) {
1587c478bd9Sstevel@tonic-gate 					current = IthItem(m, 0);
1597c478bd9Sstevel@tonic-gate 				} else {
1607c478bd9Sstevel@tonic-gate 					current = IthItem(m, Index(current)+1);
1617c478bd9Sstevel@tonic-gate 				}
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 		case REQ_NEXT_ITEM: {
1667c478bd9Sstevel@tonic-gate 			if (Index(current)+1 >= Nitems(m)) {
1677c478bd9Sstevel@tonic-gate 				if (Cyclic(m)) {
1687c478bd9Sstevel@tonic-gate 					current = IthItem(m, 0);
1697c478bd9Sstevel@tonic-gate 				} else {
1707c478bd9Sstevel@tonic-gate 					ret = E_REQUEST_DENIED;
1717c478bd9Sstevel@tonic-gate 				}
1727c478bd9Sstevel@tonic-gate 			} else {
1737c478bd9Sstevel@tonic-gate 				current = IthItem(m, Index(current)+1);
1747c478bd9Sstevel@tonic-gate 			}
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 		case REQ_PREV_MATCH: {
178*03d5549bSToomas Soome 			if (IthPattern(m, 0) != '\0') {
1797c478bd9Sstevel@tonic-gate 				ret = _match(m, '\b', &current);
1807c478bd9Sstevel@tonic-gate 			} else {
1817c478bd9Sstevel@tonic-gate 				/* This differs from PREV_ITEM in that */
1827c478bd9Sstevel@tonic-gate 				/* it is cyclic */
1837c478bd9Sstevel@tonic-gate 				if (Index(current)-1 < 0) {
1847c478bd9Sstevel@tonic-gate 					current = IthItem(m, Nitems(m)-1);
1857c478bd9Sstevel@tonic-gate 				} else {
1867c478bd9Sstevel@tonic-gate 					current = IthItem(m, Index(current)-1);
1877c478bd9Sstevel@tonic-gate 				}
1887c478bd9Sstevel@tonic-gate 			}
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		case REQ_PREV_ITEM: {
1927c478bd9Sstevel@tonic-gate 			if (Index(current)-1 < 0) {
1937c478bd9Sstevel@tonic-gate 				if (Cyclic(m)) {
1947c478bd9Sstevel@tonic-gate 					current = IthItem(m, Nitems(m)-1);
1957c478bd9Sstevel@tonic-gate 				} else {
1967c478bd9Sstevel@tonic-gate 					ret = E_REQUEST_DENIED;
1977c478bd9Sstevel@tonic-gate 				}
1987c478bd9Sstevel@tonic-gate 			} else {
1997c478bd9Sstevel@tonic-gate 				current = IthItem(m, Index(current)-1);
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		case REQ_TOGGLE_ITEM: {
2047c478bd9Sstevel@tonic-gate 			if (!OneValue(m)) {
2057c478bd9Sstevel@tonic-gate 				if (Selectable(Current(m))) {
2067c478bd9Sstevel@tonic-gate 					Value(Current(m)) ^= TRUE;
2077c478bd9Sstevel@tonic-gate 					_move_post_item(m, Current(m));
2087c478bd9Sstevel@tonic-gate 					_show(m);
2097c478bd9Sstevel@tonic-gate 				} else {
2107c478bd9Sstevel@tonic-gate 					ret = E_NOT_SELECTABLE;
2117c478bd9Sstevel@tonic-gate 				}
2127c478bd9Sstevel@tonic-gate 			} else {
2137c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		case REQ_BACK_PATTERN: {
2187c478bd9Sstevel@tonic-gate 			if (Pindex(m) > 0) {
2197c478bd9Sstevel@tonic-gate 				Pindex(m) -= 1;
2207c478bd9Sstevel@tonic-gate 				IthPattern(m, Pindex(m)) = '\0';
2217c478bd9Sstevel@tonic-gate 				_position_cursor(m);
2227c478bd9Sstevel@tonic-gate 			} else {
2237c478bd9Sstevel@tonic-gate 				ret = E_REQUEST_DENIED;
2247c478bd9Sstevel@tonic-gate 			}
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 		case REQ_CLEAR_PATTERN: {
2287c478bd9Sstevel@tonic-gate 			/* This was already done at the top */
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 		default: {
2327c478bd9Sstevel@tonic-gate 			ret = E_UNKNOWN_COMMAND;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 	} else {
2367c478bd9Sstevel@tonic-gate 		if (c > 037 && c < 0177) {
2377c478bd9Sstevel@tonic-gate 			/*LINTED [E_PASS_INT_TO_SMALL_INT]*/
2387c478bd9Sstevel@tonic-gate 			ret = _match(m, c, &current);
2397c478bd9Sstevel@tonic-gate 		} else {
2407c478bd9Sstevel@tonic-gate 			ret = E_UNKNOWN_COMMAND;
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* Verify the location of the top row */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	_chk_top(m, &top, current);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* Go change the actual values of Top and Current and do init/term */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	_affect_change(m, top, current);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	return (ret);
2537c478bd9Sstevel@tonic-gate }
254