xref: /freebsd/contrib/ncurses/menu/m_pattern.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2006,2010 Free Software Foundation, Inc.                  *
4  *                                                                          *
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 
41 MODULE_ID("$Id: m_pattern.c,v 1.19 2020/12/12 00:38:14 tom Exp $")
42 
43 /*---------------------------------------------------------------------------
44 |   Facility      :  libnmenu
45 |   Function      :  char *menu_pattern(const MENU *menu)
46 |
47 |   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 +--------------------------------------------------------------------------*/
54 MENU_EXPORT(char *)
55 menu_pattern(const MENU *menu)
56 {
57   static char empty[] = "";
58 
59   T((T_CALLED("menu_pattern(%p)"), (const void *)menu));
60   returnPtr(menu ? (menu->pattern ? menu->pattern : empty) : 0);
61 }
62 
63 /*---------------------------------------------------------------------------
64 |   Facility      :  libnmenu
65 |   Function      :  int set_menu_pattern(MENU *menu, const char *p)
66 |
67 |   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 +--------------------------------------------------------------------------*/
76 MENU_EXPORT(int)
77 set_menu_pattern(MENU *menu, const char *p)
78 {
79   ITEM *matchitem;
80   int matchpos;
81 
82   T((T_CALLED("set_menu_pattern(%p,%s)"), (void *)menu, _nc_visbuf(p)));
83 
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 */
126