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