1*c7ef0cfcSnicm /* $OpenBSD: m_pattern.c,v 1.6 2023/10/17 09:52:10 nicm Exp $ */
29f1aa62bSmillert
3457960bfSmillert /****************************************************************************
4*c7ef0cfcSnicm * Copyright 2020,2021 Thomas E. Dickey *
5*c7ef0cfcSnicm * Copyright 1998-2006,2010 Free Software Foundation, Inc. *
6457960bfSmillert * *
7457960bfSmillert * Permission is hereby granted, free of charge, to any person obtaining a *
8457960bfSmillert * copy of this software and associated documentation files (the *
9457960bfSmillert * "Software"), to deal in the Software without restriction, including *
10457960bfSmillert * without limitation the rights to use, copy, modify, merge, publish, *
11457960bfSmillert * distribute, distribute with modifications, sublicense, and/or sell *
12457960bfSmillert * copies of the Software, and to permit persons to whom the Software is *
13457960bfSmillert * furnished to do so, subject to the following conditions: *
14457960bfSmillert * *
15457960bfSmillert * The above copyright notice and this permission notice shall be included *
16457960bfSmillert * in all copies or substantial portions of the Software. *
17457960bfSmillert * *
18457960bfSmillert * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
19457960bfSmillert * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
20457960bfSmillert * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21457960bfSmillert * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
22457960bfSmillert * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
23457960bfSmillert * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
24457960bfSmillert * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25457960bfSmillert * *
26457960bfSmillert * Except as contained in this notice, the name(s) of the above copyright *
27457960bfSmillert * holders shall not be used in advertising or otherwise to promote the *
28457960bfSmillert * sale, use or other dealings in this Software without prior written *
29457960bfSmillert * authorization. *
30457960bfSmillert ****************************************************************************/
31457960bfSmillert
32457960bfSmillert /****************************************************************************
3381d8c4e1Snicm * Author: Juergen Pfeifer, 1995,1997 *
34457960bfSmillert ****************************************************************************/
359f1aa62bSmillert
369f1aa62bSmillert /***************************************************************************
379f1aa62bSmillert * Module m_pattern *
389f1aa62bSmillert * Pattern matching handling *
399f1aa62bSmillert ***************************************************************************/
409f1aa62bSmillert
419f1aa62bSmillert #include "menu.priv.h"
429f1aa62bSmillert
43*c7ef0cfcSnicm MODULE_ID("$Id: m_pattern.c,v 1.6 2023/10/17 09:52:10 nicm Exp $")
449f1aa62bSmillert
459f1aa62bSmillert /*---------------------------------------------------------------------------
469f1aa62bSmillert | Facility : libnmenu
479f1aa62bSmillert | Function : char *menu_pattern(const MENU *menu)
489f1aa62bSmillert |
499f1aa62bSmillert | Description : Return the value of the pattern buffer.
509f1aa62bSmillert |
519f1aa62bSmillert | Return Values : NULL - if there is no pattern buffer allocated
529f1aa62bSmillert | EmptyString - if there is a pattern buffer but no
539f1aa62bSmillert | pattern is stored
549f1aa62bSmillert | PatternString - as expected
559f1aa62bSmillert +--------------------------------------------------------------------------*/
MENU_EXPORT(char *)56*c7ef0cfcSnicm MENU_EXPORT(char *)
5784af20ceSmillert menu_pattern(const MENU *menu)
589f1aa62bSmillert {
5981d8c4e1Snicm static char empty[] = "";
6081d8c4e1Snicm
61*c7ef0cfcSnicm T((T_CALLED("menu_pattern(%p)"), (const void *)menu));
6281d8c4e1Snicm returnPtr(menu ? (menu->pattern ? menu->pattern : empty) : 0);
639f1aa62bSmillert }
649f1aa62bSmillert
659f1aa62bSmillert /*---------------------------------------------------------------------------
669f1aa62bSmillert | Facility : libnmenu
679f1aa62bSmillert | Function : int set_menu_pattern(MENU *menu, const char *p)
689f1aa62bSmillert |
699f1aa62bSmillert | Description : Set the match pattern for a menu and position to the
709f1aa62bSmillert | first item that matches.
719f1aa62bSmillert |
729f1aa62bSmillert | Return Values : E_OK - success
739f1aa62bSmillert | E_BAD_ARGUMENT - invalid menu or pattern pointer
749f1aa62bSmillert | E_BAD_STATE - menu in user hook routine
7581d8c4e1Snicm | E_NOT_CONNECTED - no items connected to menu
769f1aa62bSmillert | E_NO_MATCH - no item matches pattern
779f1aa62bSmillert +--------------------------------------------------------------------------*/
78*c7ef0cfcSnicm MENU_EXPORT(int)
set_menu_pattern(MENU * menu,const char * p)7984af20ceSmillert set_menu_pattern(MENU *menu, const char *p)
809f1aa62bSmillert {
819f1aa62bSmillert ITEM *matchitem;
829f1aa62bSmillert int matchpos;
839f1aa62bSmillert
84*c7ef0cfcSnicm T((T_CALLED("set_menu_pattern(%p,%s)"), (void *)menu, _nc_visbuf(p)));
8581d8c4e1Snicm
869f1aa62bSmillert if (!menu || !p)
879f1aa62bSmillert RETURN(E_BAD_ARGUMENT);
889f1aa62bSmillert
899f1aa62bSmillert if (!(menu->items))
909f1aa62bSmillert RETURN(E_NOT_CONNECTED);
919f1aa62bSmillert
929f1aa62bSmillert if (menu->status & _IN_DRIVER)
939f1aa62bSmillert RETURN(E_BAD_STATE);
949f1aa62bSmillert
959f1aa62bSmillert Reset_Pattern(menu);
969f1aa62bSmillert
979f1aa62bSmillert if (!(*p))
989f1aa62bSmillert {
999f1aa62bSmillert pos_menu_cursor(menu);
1009f1aa62bSmillert RETURN(E_OK);
1019f1aa62bSmillert }
1029f1aa62bSmillert
1039f1aa62bSmillert if (menu->status & _LINK_NEEDED)
1049f1aa62bSmillert _nc_Link_Items(menu);
1059f1aa62bSmillert
1069f1aa62bSmillert matchpos = menu->toprow;
1079f1aa62bSmillert matchitem = menu->curitem;
1089f1aa62bSmillert assert(matchitem);
1099f1aa62bSmillert
1109f1aa62bSmillert while (*p)
1119f1aa62bSmillert {
11281d8c4e1Snicm if (!isprint(UChar(*p)) ||
1139f1aa62bSmillert (_nc_Match_Next_Character_In_Item_Name(menu, *p, &matchitem) != E_OK))
1149f1aa62bSmillert {
1159f1aa62bSmillert Reset_Pattern(menu);
1169f1aa62bSmillert pos_menu_cursor(menu);
1179f1aa62bSmillert RETURN(E_NO_MATCH);
1189f1aa62bSmillert }
1199f1aa62bSmillert p++;
1209f1aa62bSmillert }
1219f1aa62bSmillert
1229f1aa62bSmillert /* This is reached if there was a match. So we position to the new item */
1239f1aa62bSmillert Adjust_Current_Item(menu, matchpos, matchitem);
1249f1aa62bSmillert RETURN(E_OK);
1259f1aa62bSmillert }
1269f1aa62bSmillert
1279f1aa62bSmillert /* m_pattern.c ends here */
128