m_pattern.c 5.1 KB

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