xref: /illumos-gate/usr/src/lib/libeti/menu/common/connect.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1997, by Sun Mircrosystems, Inc.
28  * All rights reserved.
29  */
30 
31 /*LINTLIBRARY*/
32 
33 #include <sys/types.h>
34 #include <stdlib.h>
35 #include "private.h"
36 
37 /* Connect and disconnect an item list from a menu */
38 
39 
40 /* Find the maximum length name and description */
41 
42 static void
43 maxlengths(MENU *m)
44 {
45 	int maxn, maxd;
46 	ITEM **ip;
47 
48 	maxn = maxd = 0;
49 	for (ip = Items(m); *ip; ip++) {
50 		if (NameLen(*ip) > maxn) {
51 			maxn = NameLen(*ip);
52 		}
53 		if (DescriptionLen(*ip) > maxd) {
54 			maxd = DescriptionLen(*ip);
55 		}
56 	}
57 	MaxName(m) = maxn;
58 	MaxDesc(m) = maxd;
59 }
60 
61 int
62 _connect(MENU *m, ITEM **items)
63 {
64 	ITEM **ip;
65 	int i;
66 
67 	/* Is the list of items connected to any other menu? */
68 	for (ip = items; *ip; ip++) {
69 		/* Return Null if item points to a menu */
70 		if (Imenu(*ip)) {
71 			return (FALSE);
72 		}
73 	}
74 
75 	for (i = 0, ip = items; *ip; ip++) {
76 		/* Return FALSE if this item is a prevoious item */
77 		if (Imenu(*ip)) {
78 			for (ip = items; *ip; ip++) {
79 				/* Reset index and menu pointers */
80 				Index(*ip) = 0;
81 				Imenu(*ip) = (MENU *) NULL;
82 			}
83 			return (FALSE);
84 		}
85 		if (OneValue(m)) {
86 			/* Set all values to FALSE if selection not allowed */
87 			Value(*ip) = FALSE;
88 		}
89 		Index(*ip) = i++;
90 		Imenu(*ip) = m;
91 	}
92 
93 	Nitems(m) = i;
94 	Items(m) = items;
95 
96 	/* Go pick up the sizes of names and descriptions */
97 	maxlengths(m);
98 
99 	/* Set up match buffer */
100 	if ((Pattern(m) = (char *)malloc((unsigned)MaxName(m)+1)) ==
101 	    (char *)0) {
102 		return (FALSE);
103 	}
104 
105 	IthPattern(m, 0) = '\0';
106 	Pindex(m) = 0;
107 	(void) set_menu_format(m, FRows(m), FCols(m));
108 	Current(m) = IthItem(m, 0);
109 	Top(m) = 0;
110 	return (TRUE);
111 }
112 
113 void
114 _disconnect(MENU *m)
115 {
116 	ITEM **ip;
117 
118 	for (ip = Items(m); *ip; ip++) {
119 		/* Release items for another menu */
120 		Imenu(*ip) = (MENU *) NULL;
121 	}
122 	free(Pattern(m));
123 	Pattern(m) = NULL;
124 	Items(m) = (ITEM **) NULL;
125 	Nitems(m) = 0;
126 }
127