xref: /freebsd/contrib/ncurses/menu/m_pattern.c (revision 4a1a95108dd76c4259fe6c37c4471f7969b17983)
10e3d5408SPeter Wemm /****************************************************************************
24a1a9510SRong-En Fan  * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
30e3d5408SPeter Wemm  *                                                                          *
40e3d5408SPeter Wemm  * Permission is hereby granted, free of charge, to any person obtaining a  *
50e3d5408SPeter Wemm  * copy of this software and associated documentation files (the            *
60e3d5408SPeter Wemm  * "Software"), to deal in the Software without restriction, including      *
70e3d5408SPeter Wemm  * without limitation the rights to use, copy, modify, merge, publish,      *
80e3d5408SPeter Wemm  * distribute, distribute with modifications, sublicense, and/or sell       *
90e3d5408SPeter Wemm  * copies of the Software, and to permit persons to whom the Software is    *
100e3d5408SPeter Wemm  * furnished to do so, subject to the following conditions:                 *
110e3d5408SPeter Wemm  *                                                                          *
120e3d5408SPeter Wemm  * The above copyright notice and this permission notice shall be included  *
130e3d5408SPeter Wemm  * in all copies or substantial portions of the Software.                   *
140e3d5408SPeter Wemm  *                                                                          *
150e3d5408SPeter Wemm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
160e3d5408SPeter Wemm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
170e3d5408SPeter Wemm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
180e3d5408SPeter Wemm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
190e3d5408SPeter Wemm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
200e3d5408SPeter Wemm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
210e3d5408SPeter Wemm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
220e3d5408SPeter Wemm  *                                                                          *
230e3d5408SPeter Wemm  * Except as contained in this notice, the name(s) of the above copyright   *
240e3d5408SPeter Wemm  * holders shall not be used in advertising or otherwise to promote the     *
250e3d5408SPeter Wemm  * sale, use or other dealings in this Software without prior written       *
260e3d5408SPeter Wemm  * authorization.                                                           *
270e3d5408SPeter Wemm  ****************************************************************************/
280e3d5408SPeter Wemm 
290e3d5408SPeter Wemm /****************************************************************************
304a1a9510SRong-En Fan  *   Author:  Juergen Pfeifer, 1995,1997                                    *
310e3d5408SPeter Wemm  ****************************************************************************/
320e3d5408SPeter Wemm 
330e3d5408SPeter Wemm /***************************************************************************
340e3d5408SPeter Wemm * Module m_pattern                                                         *
350e3d5408SPeter Wemm * Pattern matching handling                                                *
360e3d5408SPeter Wemm ***************************************************************************/
370e3d5408SPeter Wemm 
380e3d5408SPeter Wemm #include "menu.priv.h"
390e3d5408SPeter Wemm 
404a1a9510SRong-En Fan MODULE_ID("$Id: m_pattern.c,v 1.15 2006/11/04 18:46:39 tom Exp $")
410e3d5408SPeter Wemm 
420e3d5408SPeter Wemm /*---------------------------------------------------------------------------
430e3d5408SPeter Wemm |   Facility      :  libnmenu
440e3d5408SPeter Wemm |   Function      :  char *menu_pattern(const MENU *menu)
450e3d5408SPeter Wemm |
460e3d5408SPeter Wemm |   Description   :  Return the value of the pattern buffer.
470e3d5408SPeter Wemm |
480e3d5408SPeter Wemm |   Return Values :  NULL          - if there is no pattern buffer allocated
490e3d5408SPeter Wemm |                    EmptyString   - if there is a pattern buffer but no
500e3d5408SPeter Wemm |                                    pattern is stored
510e3d5408SPeter Wemm |                    PatternString - as expected
520e3d5408SPeter Wemm +--------------------------------------------------------------------------*/
537a69bbfbSPeter Wemm NCURSES_EXPORT(char *)
547a69bbfbSPeter Wemm menu_pattern(const MENU * menu)
550e3d5408SPeter Wemm {
564a1a9510SRong-En Fan   static char empty[] = "";
574a1a9510SRong-En Fan 
584a1a9510SRong-En Fan   T((T_CALLED("menu_pattern(%p)"), menu));
594a1a9510SRong-En Fan   returnPtr(menu ? (menu->pattern ? menu->pattern : empty) : 0);
600e3d5408SPeter Wemm }
610e3d5408SPeter Wemm 
620e3d5408SPeter Wemm /*---------------------------------------------------------------------------
630e3d5408SPeter Wemm |   Facility      :  libnmenu
640e3d5408SPeter Wemm |   Function      :  int set_menu_pattern(MENU *menu, const char *p)
650e3d5408SPeter Wemm |
660e3d5408SPeter Wemm |   Description   :  Set the match pattern for a menu and position to the
670e3d5408SPeter Wemm |                    first item that matches.
680e3d5408SPeter Wemm |
690e3d5408SPeter Wemm |   Return Values :  E_OK              - success
700e3d5408SPeter Wemm |                    E_BAD_ARGUMENT    - invalid menu or pattern pointer
710e3d5408SPeter Wemm |                    E_BAD_STATE       - menu in user hook routine
724a1a9510SRong-En Fan |                    E_NOT_CONNECTED   - no items connected to menu
730e3d5408SPeter Wemm |                    E_NO_MATCH        - no item matches pattern
740e3d5408SPeter Wemm +--------------------------------------------------------------------------*/
757a69bbfbSPeter Wemm NCURSES_EXPORT(int)
767a69bbfbSPeter Wemm set_menu_pattern(MENU * menu, const char *p)
770e3d5408SPeter Wemm {
780e3d5408SPeter Wemm   ITEM *matchitem;
790e3d5408SPeter Wemm   int matchpos;
800e3d5408SPeter Wemm 
814a1a9510SRong-En Fan   T((T_CALLED("set_menu_pattern(%p,%s)"), menu, _nc_visbuf(p)));
824a1a9510SRong-En Fan 
830e3d5408SPeter Wemm   if (!menu || !p)
840e3d5408SPeter Wemm     RETURN(E_BAD_ARGUMENT);
850e3d5408SPeter Wemm 
860e3d5408SPeter Wemm   if (!(menu->items))
870e3d5408SPeter Wemm     RETURN(E_NOT_CONNECTED);
880e3d5408SPeter Wemm 
890e3d5408SPeter Wemm   if (menu->status & _IN_DRIVER)
900e3d5408SPeter Wemm     RETURN(E_BAD_STATE);
910e3d5408SPeter Wemm 
920e3d5408SPeter Wemm   Reset_Pattern(menu);
930e3d5408SPeter Wemm 
940e3d5408SPeter Wemm   if (!(*p))
950e3d5408SPeter Wemm     {
960e3d5408SPeter Wemm       pos_menu_cursor(menu);
970e3d5408SPeter Wemm       RETURN(E_OK);
980e3d5408SPeter Wemm     }
990e3d5408SPeter Wemm 
1000e3d5408SPeter Wemm   if (menu->status & _LINK_NEEDED)
1010e3d5408SPeter Wemm     _nc_Link_Items(menu);
1020e3d5408SPeter Wemm 
1030e3d5408SPeter Wemm   matchpos = menu->toprow;
1040e3d5408SPeter Wemm   matchitem = menu->curitem;
1050e3d5408SPeter Wemm   assert(matchitem);
1060e3d5408SPeter Wemm 
1070e3d5408SPeter Wemm   while (*p)
1080e3d5408SPeter Wemm     {
1094a1a9510SRong-En Fan       if (!isprint(UChar(*p)) ||
1100e3d5408SPeter Wemm 	  (_nc_Match_Next_Character_In_Item_Name(menu, *p, &matchitem) != E_OK))
1110e3d5408SPeter Wemm 	{
1120e3d5408SPeter Wemm 	  Reset_Pattern(menu);
1130e3d5408SPeter Wemm 	  pos_menu_cursor(menu);
1140e3d5408SPeter Wemm 	  RETURN(E_NO_MATCH);
1150e3d5408SPeter Wemm 	}
1160e3d5408SPeter Wemm       p++;
1170e3d5408SPeter Wemm     }
1180e3d5408SPeter Wemm 
1190e3d5408SPeter Wemm   /* This is reached if there was a match. So we position to the new item */
1200e3d5408SPeter Wemm   Adjust_Current_Item(menu, matchpos, matchitem);
1210e3d5408SPeter Wemm   RETURN(E_OK);
1220e3d5408SPeter Wemm }
1230e3d5408SPeter Wemm 
1240e3d5408SPeter Wemm /* m_pattern.c ends here */
125