150276Speter/****************************************************************************
2166124Srafan * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
30166124Srafan *   Author:  Juergen Pfeifer, 1995,1997                                    *
3150276Speter ****************************************************************************/
3250276Speter
3350276Speter/***************************************************************************
3450276Speter* Module m_pattern                                                         *
3550276Speter* Pattern matching handling                                                *
3650276Speter***************************************************************************/
3750276Speter
3850276Speter#include "menu.priv.h"
3950276Speter
40166124SrafanMODULE_ID("$Id: m_pattern.c,v 1.15 2006/11/04 18:46:39 tom Exp $")
4150276Speter
4250276Speter/*---------------------------------------------------------------------------
4350276Speter|   Facility      :  libnmenu
4450276Speter|   Function      :  char *menu_pattern(const MENU *menu)
4550276Speter|
4650276Speter|   Description   :  Return the value of the pattern buffer.
4750276Speter|
4850276Speter|   Return Values :  NULL          - if there is no pattern buffer allocated
4950276Speter|                    EmptyString   - if there is a pattern buffer but no
5050276Speter|                                    pattern is stored
5150276Speter|                    PatternString - as expected
5250276Speter+--------------------------------------------------------------------------*/
5376726SpeterNCURSES_EXPORT(char *)
54166124Srafanmenu_pattern(const MENU * menu)
5550276Speter{
56166124Srafan  static char empty[] = "";
57166124Srafan
58166124Srafan  T((T_CALLED("menu_pattern(%p)"), menu));
59166124Srafan  returnPtr(menu ? (menu->pattern ? menu->pattern : empty) : 0);
6050276Speter}
6150276Speter
6250276Speter/*---------------------------------------------------------------------------
6350276Speter|   Facility      :  libnmenu
6450276Speter|   Function      :  int set_menu_pattern(MENU *menu, const char *p)
6550276Speter|
6650276Speter|   Description   :  Set the match pattern for a menu and position to the
6750276Speter|                    first item that matches.
6850276Speter|
6950276Speter|   Return Values :  E_OK              - success
7050276Speter|                    E_BAD_ARGUMENT    - invalid menu or pattern pointer
71166124Srafan|                    E_BAD_STATE       - menu in user hook routine
7250276Speter|                    E_NOT_CONNECTED   - no items connected to menu
7350276Speter|                    E_NO_MATCH        - no item matches pattern
7450276Speter+--------------------------------------------------------------------------*/
7576726SpeterNCURSES_EXPORT(int)
76166124Srafanset_menu_pattern(MENU * menu, const char *p)
7750276Speter{
7850276Speter  ITEM *matchitem;
79166124Srafan  int matchpos;
80166124Srafan
81166124Srafan  T((T_CALLED("set_menu_pattern(%p,%s)"), menu, _nc_visbuf(p)));
82166124Srafan
83166124Srafan  if (!menu || !p)
8450276Speter    RETURN(E_BAD_ARGUMENT);
85166124Srafan
8650276Speter  if (!(menu->items))
8750276Speter    RETURN(E_NOT_CONNECTED);
88166124Srafan
89166124Srafan  if (menu->status & _IN_DRIVER)
9050276Speter    RETURN(E_BAD_STATE);
91166124Srafan
9250276Speter  Reset_Pattern(menu);
93166124Srafan
9450276Speter  if (!(*p))
9550276Speter    {
9650276Speter      pos_menu_cursor(menu);
9750276Speter      RETURN(E_OK);
9850276Speter    }
99166124Srafan
100166124Srafan  if (menu->status & _LINK_NEEDED)
10150276Speter    _nc_Link_Items(menu);
102166124Srafan
103166124Srafan  matchpos = menu->toprow;
10450276Speter  matchitem = menu->curitem;
10550276Speter  assert(matchitem);
106166124Srafan
107166124Srafan  while (*p)
10850276Speter    {
109166124Srafan      if (!isprint(UChar(*p)) ||
110166124Srafan	  (_nc_Match_Next_Character_In_Item_Name(menu, *p, &matchitem) != E_OK))
11150276Speter	{
11250276Speter	  Reset_Pattern(menu);
11350276Speter	  pos_menu_cursor(menu);
11450276Speter	  RETURN(E_NO_MATCH);
11550276Speter	}
11650276Speter      p++;
117166124Srafan    }
118166124Srafan
11950276Speter  /* This is reached if there was a match. So we position to the new item */
120166124Srafan  Adjust_Current_Item(menu, matchpos, matchitem);
12150276Speter  RETURN(E_OK);
12250276Speter}
12350276Speter
12450276Speter/* m_pattern.c ends here */
125