blob: 6fbef236f2e6aa67c858f8f07ccea3789d8957cd [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2006,2010 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Juergen Pfeifer, 1995,1997 *
32 ****************************************************************************/
33
34/***************************************************************************
35* Module m_pattern *
36* Pattern matching handling *
37***************************************************************************/
38
39#include "menu.priv.h"
40
micky3879b9f5e72025-07-08 18:04:53 -040041MODULE_ID("$Id: m_pattern.c,v 1.20 2021/06/17 21:20:30 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042
43/*---------------------------------------------------------------------------
micky3879b9f5e72025-07-08 18:04:53 -040044| Facility : libnmenu
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053045| Function : char *menu_pattern(const MENU *menu)
micky3879b9f5e72025-07-08 18:04:53 -040046|
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047| Description : Return the value of the pattern buffer.
48|
49| Return Values : NULL - if there is no pattern buffer allocated
50| EmptyString - if there is a pattern buffer but no
51| pattern is stored
52| PatternString - as expected
53+--------------------------------------------------------------------------*/
micky3879b9f5e72025-07-08 18:04:53 -040054MENU_EXPORT(char *)
55menu_pattern(const MENU *menu)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053056{
57 static char empty[] = "";
58
Steve Kondikae271bc2015-11-15 02:50:53 +010059 T((T_CALLED("menu_pattern(%p)"), (const void *)menu));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053060 returnPtr(menu ? (menu->pattern ? menu->pattern : empty) : 0);
61}
62
63/*---------------------------------------------------------------------------
micky3879b9f5e72025-07-08 18:04:53 -040064| Facility : libnmenu
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053065| Function : int set_menu_pattern(MENU *menu, const char *p)
micky3879b9f5e72025-07-08 18:04:53 -040066|
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053067| Description : Set the match pattern for a menu and position to the
68| first item that matches.
69|
70| Return Values : E_OK - success
71| E_BAD_ARGUMENT - invalid menu or pattern pointer
72| E_BAD_STATE - menu in user hook routine
73| E_NOT_CONNECTED - no items connected to menu
74| E_NO_MATCH - no item matches pattern
75+--------------------------------------------------------------------------*/
micky3879b9f5e72025-07-08 18:04:53 -040076MENU_EXPORT(int)
77set_menu_pattern(MENU *menu, const char *p)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053078{
79 ITEM *matchitem;
80 int matchpos;
81
Steve Kondikae271bc2015-11-15 02:50:53 +010082 T((T_CALLED("set_menu_pattern(%p,%s)"), (void *)menu, _nc_visbuf(p)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053083
84 if (!menu || !p)
85 RETURN(E_BAD_ARGUMENT);
86
87 if (!(menu->items))
88 RETURN(E_NOT_CONNECTED);
89
90 if (menu->status & _IN_DRIVER)
91 RETURN(E_BAD_STATE);
92
93 Reset_Pattern(menu);
94
95 if (!(*p))
96 {
97 pos_menu_cursor(menu);
98 RETURN(E_OK);
99 }
100
101 if (menu->status & _LINK_NEEDED)
102 _nc_Link_Items(menu);
103
104 matchpos = menu->toprow;
105 matchitem = menu->curitem;
106 assert(matchitem);
107
108 while (*p)
109 {
110 if (!isprint(UChar(*p)) ||
111 (_nc_Match_Next_Character_In_Item_Name(menu, *p, &matchitem) != E_OK))
112 {
113 Reset_Pattern(menu);
114 pos_menu_cursor(menu);
115 RETURN(E_NO_MATCH);
116 }
117 p++;
118 }
119
120 /* This is reached if there was a match. So we position to the new item */
121 Adjust_Current_Item(menu, matchpos, matchitem);
122 RETURN(E_OK);
123}
124
125/* m_pattern.c ends here */