mb/google/brya/var/orisa: Update Type C DisplayPort HPD Configuration
[coreboot2.git] / payloads / libpayload / curses / menu / m_item_new.c
bloba10ec4b5b61977e87b25c4a483ce85e3cf950164
1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer, 1995,1997 *
31 ****************************************************************************/
33 /***************************************************************************
34 * Module m_item_new *
35 * Create and destroy menu items *
36 * Set and get marker string for menu *
37 ***************************************************************************/
39 #include "menu.priv.h"
41 #if USE_WIDEC_SUPPORT
42 #if HAVE_WCTYPE_H
43 #include <wctype.h>
44 #endif
45 #endif
47 MODULE_ID("$Id: m_item_new.c,v 1.30 2010/01/23 21:20:11 tom Exp $")
49 /*---------------------------------------------------------------------------
50 | Facility : libnmenu
51 | Function : bool Is_Printable_String(const char *s)
53 | Description : Checks whether or not the string contains only printable
54 | characters.
56 | Return Values : TRUE - if string is printable
57 | FALSE - if string contains non-printable characters
58 +--------------------------------------------------------------------------*/
59 static bool
60 Is_Printable_String(const char *s)
62 int result = TRUE;
64 #if USE_WIDEC_SUPPORT
65 int count = mbstowcs(0, s, 0);
66 wchar_t *temp = 0;
68 assert(s);
70 if (count > 0
71 && (temp = typeCalloc(wchar_t, (2 + (unsigned)count))) != 0)
73 int n;
75 mbstowcs(temp, s, (unsigned)count);
76 for (n = 0; n < count; ++n)
77 if (!iswprint((wint_t) temp[n]))
79 result = FALSE;
80 break;
82 free(temp);
84 #else
85 assert(s);
86 while (*s)
88 if (!isprint(UChar(*s)))
90 result = FALSE;
91 break;
93 s++;
95 #endif
96 return result;
99 /*---------------------------------------------------------------------------
100 | Facility : libnmenu
101 | Function : ITEM *new_item(char *name, char *description)
103 | Description : Create a new item with name and description. Return
104 | a pointer to this new item.
105 | N.B.: an item must(!) have a name.
107 | Return Values : The item pointer or NULL if creation failed.
108 +--------------------------------------------------------------------------*/
109 NCURSES_EXPORT(ITEM *)
110 new_item(const char *name, const char *description)
112 ITEM *item;
114 T((T_CALLED("new_item(\"%s\", \"%s\")"),
115 name ? name : "",
116 description ? description : ""));
118 if (!name || (*name == '\0') || !Is_Printable_String(name))
120 item = (ITEM *) 0;
121 SET_ERROR(E_BAD_ARGUMENT);
123 else
125 item = typeCalloc(ITEM, 1);
126 if (item)
128 *item = _nc_Default_Item; /* hope we have struct assignment */
130 item->name.length = strlen(name);
131 item->name.str = name;
133 if (description && (*description != '\0') &&
134 Is_Printable_String(description))
136 item->description.length = strlen(description);
137 item->description.str = description;
139 else
141 item->description.length = 0;
142 item->description.str = (char *)0;
145 else
146 SET_ERROR(E_SYSTEM_ERROR);
148 returnItem(item);
151 /*---------------------------------------------------------------------------
152 | Facility : libnmenu
153 | Function : int free_item(ITEM *item)
155 | Description : Free the allocated storage for this item.
156 | N.B.: a connected item can't be freed.
158 | Return Values : E_OK - success
159 | E_BAD_ARGUMENT - invalid value has been passed
160 | E_CONNECTED - item is still connected to a menu
161 +--------------------------------------------------------------------------*/
162 NCURSES_EXPORT(int)
163 free_item(ITEM * item)
165 T((T_CALLED("free_item(%p)"), (void *)item));
167 if (!item)
168 RETURN(E_BAD_ARGUMENT);
170 if (item->imenu)
171 RETURN(E_CONNECTED);
173 free(item);
175 RETURN(E_OK);
178 /*---------------------------------------------------------------------------
179 | Facility : libnmenu
180 | Function : int set_menu_mark( MENU *menu, const char *mark )
182 | Description : Set the mark string used to indicate the current
183 | item (single-valued menu) or the selected items
184 | (multi-valued menu).
185 | The mark argument may be NULL, in which case no
186 | marker is used.
187 | This might be a little bit tricky, because this may
188 | affect the geometry of the menu, which we don't allow
189 | if it is already posted.
191 | Return Values : E_OK - success
192 | E_BAD_ARGUMENT - an invalid value has been passed
193 | E_SYSTEM_ERROR - no memory to store mark
194 +--------------------------------------------------------------------------*/
195 NCURSES_EXPORT(int)
196 set_menu_mark(MENU * menu, const char *mark)
198 unsigned l;
200 T((T_CALLED("set_menu_mark(%p,%s)"), (void *)menu, _nc_visbuf(mark)));
202 if (mark && (*mark != '\0') && Is_Printable_String(mark))
203 l = strlen(mark);
204 else
205 l = 0;
207 if (menu)
209 char *old_mark = menu->mark;
210 unsigned short old_status = menu->status;
212 if (menu->status & _POSTED)
214 /* If the menu is already posted, the geometry is fixed. Then
215 we can only accept a mark with exactly the same length */
216 if (menu->marklen != (int)l)
217 RETURN(E_BAD_ARGUMENT);
219 menu->marklen = l;
220 if (l)
222 menu->mark = strdup(mark);
223 if (menu->mark)
225 strcpy(menu->mark, mark);
226 if (menu != &_nc_Default_Menu)
227 menu->status |= _MARK_ALLOCATED;
229 else
231 menu->mark = old_mark;
232 menu->marklen = (old_mark != 0) ? strlen(old_mark) : 0;
233 RETURN(E_SYSTEM_ERROR);
236 else
237 menu->mark = (char *)0;
239 if ((old_status & _MARK_ALLOCATED) && old_mark)
240 free(old_mark);
242 if (menu->status & _POSTED)
244 _nc_Draw_Menu(menu);
245 _nc_Show_Menu(menu);
247 else
249 /* Recalculate the geometry */
250 _nc_Calculate_Item_Length_and_Width(menu);
253 else
255 returnCode(set_menu_mark(&_nc_Default_Menu, mark));
257 RETURN(E_OK);
260 /*---------------------------------------------------------------------------
261 | Facility : libnmenu
262 | Function : char *menu_mark(const MENU *menu)
264 | Description : Return a pointer to the marker string
266 | Return Values : The marker string pointer or NULL if no marker defined
267 +--------------------------------------------------------------------------*/
268 NCURSES_EXPORT(const char *)
269 menu_mark(const MENU * menu)
271 T((T_CALLED("menu_mark(%p)"), (const void *)menu));
272 returnPtr(Normalize_Menu(menu)->mark);
275 /* m_item_new.c */