code:
CONSOLE_COMMAND(mn_search, 0)
{
int i = 0;
menu_t *curMenu;
boolean pastLast;
// if lastMatch is set, set pastLast to false so that we'll seek
// forward until we pass the last item that was matched
pastLast = !lastMatch;
if(!mn_searchstr || !mn_searchstr[0])
{
MN_ErrorMsg("invalid search string");
return;
}
M_Strlwr(mn_searchstr);
// run through every menu in the search list...
while((curMenu = mn_search_menus[i++]))
{
menu_t *curPage = curMenu;
// run through every page of the menu
while(curPage)
{
int j = 0;
menuitem_t *item;
// run through items
while((item = &(curPage->menuitems[j++])))
{
char *desc;
if(item->type == it_end)
break;
if(!item->description)
continue;
// keep seeking until we pass the last item found
if(item == lastMatch)
{
pastLast = true;
continue;
}
if(!pastLast)
continue;
desc = M_Strlwr(strdup(item->description));
// found a match
if(strstr(desc, mn_searchstr))
{
// go to it
lastMatch = item;
MN_StartMenu(curPage);
MN_ErrorMsg("found: %s", desc);
if(!is_a_gap(item))
curPage->selected = j - 1;
free(desc);
return;
}
free(desc);
}
curPage = curPage->nextpage;
}
}
if(lastMatch) // if doing a valid search, reset it now
{
lastMatch = NULL;
MN_ErrorMsg("reached end of search");
}
else
MN_ErrorMsg("no match found for '%s'", mn_searchstr);
}