PennMUSH Community

Changeset 1157

Show
Ignore:
Timestamp:
11/22/07 12:42:08 (9 months ago)
Author:
shawnw
Message:

#6091: help foo* matching only one topic

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 1.8.3/branches/devel/CHANGES.183

    r1156 r1157  
    3232   and/or socket timeouts instead. (The main event loop still uses 
    3333   select() for now.) 
     34 * A wildcard help topic search (help foo*) that only matches one 
     35   entry will display that entry. Suggested by Cheetah. 
    3436 
    3537Flags and powers: 
  • 1.8.3/branches/devel/hdrs/parse.h

    r905 r1157  
    8383 
    8484/* Split a sep-delimited string into individual elements */ 
    85 extern int list2arr(char *r[], int max, char *list, char sep); 
     85int list2arr(char *r[], int max, char *list, char sep); 
    8686/* The reverse */ 
    87 extern void arr2list(char *r[], int max, char *list, char **lp, char *sep); 
     87void arr2list(char *r[], int max, char *list, char **lp, const char *sep); 
    8888/* Split a sep-delimited string into individual elements. 
    8989 * Uses mush_strdup, so freearr() is required on all 
    9090 * list2arr_ansi()'d arrays (r) */ 
    91 extern int list2arr_ansi(char *r[], int max, char *list, char sep); 
     91int list2arr_ansi(char *r[], int max, char *list, char sep); 
    9292/* Free an array generated by list2arr_ansi */ 
    93 extern void freearr(char *r[], int size); 
     93void freearr(char *r[], int size); 
    9494 
    9595/* All function declarations follow the format: */ 
  • 1.8.3/branches/devel/src/funlist.c

    r1151 r1157  
    139139 */ 
    140140void 
    141 arr2list(char *r[], int max, char *list, char **lp, char *sep) 
     141arr2list(char *r[], int max, char *list, char **lp, const char *sep) 
    142142{ 
    143143  int i; 
  • 1.8.3/branches/devel/src/help.c

    r1051 r1157  
    3232static const char *string_spitfile(help_file * help_dat, char *arg1); 
    3333static help_indx *help_find_entry(help_file * help_dat, const char *the_topic); 
    34 static const char *list_matching_entries(char *pattern, help_file * help_dat, 
    35                                          const char *sep); 
    36 static const char *normalize_entry(help_file * help_dat, char *arg1); 
     34static char** list_matching_entries(const char *pattern, 
     35                    help_file *help_dat, int *len); 
     36static void free_entry_list(char **); 
     37static const char *normalize_entry(help_file * help_dat, const char *arg1); 
    3738 
    3839static void help_build_index(help_file * h, int restricted); 
     
    6869  } 
    6970 
    70   if (wildcard(arg_left)) 
    71     notify_format(player, T("Here are the entries which match '%s':\n%s"), 
    72                   arg_left, list_matching_entries(arg_left, h, ", ")); 
    73   else 
     71  if (wildcard(arg_left)) { 
     72    int len = 0; 
     73    char **entries; 
     74 
     75    entries = list_matching_entries(arg_left, h, &len); 
     76    if (len == 0) 
     77      notify_format(player, T("No entries matching '%s' were found."), 
     78            arg_left); 
     79    else if (len == 1) 
     80      do_new_spitfile(player, *entries, h); 
     81    else { 
     82      char buff[BUFFER_LEN]; 
     83      char *bp; 
     84 
     85      bp = buff; 
     86      arr2list(entries, len, buff, &bp, ", "); 
     87      *bp = '\0'; 
     88      notify_format(player, T("Here are the entries which match '%s':\n%s"), 
     89            arg_left, buff); 
     90    } 
     91    free_entry_list(entries); 
     92  } else 
    7493    do_new_spitfile(player, arg_left, h); 
    7594} 
    7695 
    77 /** Initialize the helpfile hashtable, which contains the names of the 
     96/** Initialize the helpfile hashtable, which contains the names of thes 
    7897 * help files. 
    7998 */ 
     
    453472 
    454473  if (wildcard(args[1])) { 
    455     const char *entries = list_matching_entries(args[1], h, ", "); 
    456     if (*entries) 
    457       safe_str(entries, buff, bp); 
     474    char **entries; 
     475    int len = 0; 
     476    entries = list_matching_entries(args[1], h, &len); 
     477    if (len == 0) 
     478      safe_str(T("No matching help topics."), buff, bp); 
    458479    else 
    459       safe_str(T("No matching help topics."), buff, bp); 
     480      arr2list(entries, len, buff, bp, ", "); 
     481    free_entry_list(entries); 
    460482  } else 
    461483    safe_str(string_spitfile(h, args[1]), buff, bp); 
     
    466488{ 
    467489  help_file *h; 
     490  char **entries; 
     491  int len = 0; 
    468492  const char *sep = " "; 
    469493 
     
    479503  if (nargs > 2) 
    480504    sep = args[2]; 
    481   safe_str(list_matching_entries(args[1], h, sep), buff, bp); 
     505 
     506  entries = list_matching_entries(args[1], h, &len); 
     507  if (entries) { 
     508    arr2list(entries, len, buff, bp, sep); 
     509    free_entry_list(entries); 
     510  } 
    482511} 
    483512 
    484513static const char * 
    485 normalize_entry(help_file * help_dat, char *arg1) 
     514normalize_entry(help_file * help_dat, const char *arg1) 
    486515{ 
    487516  static char the_topic[LINE_SIZE + 2]; 
     
    491520  else if (*arg1 == '&') 
    492521    return T("#-1 INVALID ENTRY"); 
    493   if (strlen(arg1) > LINE_SIZE) 
    494     *(arg1 + LINE_SIZE) = '\0'; 
    495  
    496522  if (help_dat->admin) 
    497     sprintf(the_topic, "&%s", arg1); 
     523    snprintf(the_topic, LINE_SIZE, "&%s", arg1); 
    498524  else 
    499     strcpy(the_topic, arg1); 
     525    mush_strncpy(the_topic, arg1, LINE_SIZE); 
    500526  return the_topic; 
    501527} 
     
    542568 
    543569/** Return a string with all help entries that match a pattern */ 
    544 static const char
    545 list_matching_entries(char *pattern, help_file * help_dat, const char *sep
    546 { 
    547   static char buff[BUFFER_LEN]
     570static char *
     571list_matching_entries(const char *pattern, help_file *help_dat, int *len
     572{ 
     573  char **buff
    548574  int offset; 
    549   char *bp; 
    550575  size_t n; 
    551   int len = 0; 
    552576 
    553577  if (help_dat->admin) 
     
    561585    help_indx *entry = NULL; 
    562586    strcpy(the_topic, normalize_entry(help_dat, pattern)); 
    563     if (!help_dat->indx || help_dat->entries == 0) 
    564       return T("#-1 NO INDEX FOR FILE"); 
     587    if (!help_dat->indx || help_dat->entries == 0) { 
     588      *len = 0; 
     589      return NULL; 
     590    } 
    565591    entry = help_find_entry(help_dat, the_topic); 
    566     if (!entry) 
    567       return (char *) ""; 
    568     return (char *) (entry->topic + offset); 
    569   } 
    570  
    571   bp = buff; 
    572  
    573   if (sep) 
    574     len = strlen(sep); 
     592    if (!entry) { 
     593      *len = 0; 
     594      return NULL; 
     595    } else { 
     596      *len = 1; 
     597      buff = mush_malloc(sizeof(char**), "help.search"); 
     598      *buff = entry->topic + offset; 
     599      return buff; 
     600    } 
     601  } 
     602 
     603  buff = mush_calloc(help_dat->entries, sizeof(char*), "help.search"); 
     604  *len = 0; 
    575605 
    576606  for (n = 0; n < help_dat->entries; n++) 
    577607    if (quick_wild(pattern, help_dat->indx[n].topic + offset)) { 
    578       safe_str(help_dat->indx[n].topic + offset, buff, &bp); 
    579       if (sep) 
    580         safe_strl(sep, len, buff, &bp); 
    581     } 
    582  
    583   if (bp > buff) 
    584     *(bp - len) = '\0'; 
    585   else { 
    586     *bp = '\0'; 
    587   } 
     608      buff[*len] = help_dat->indx[n].topic + offset; 
     609      *len += 1; 
     610    } 
    588611 
    589612  return buff; 
    590613} 
     614 
     615static void 
     616free_entry_list(char **entries)  
     617{ 
     618  if (entries) 
     619    mush_free(entries, "help.search"); 
     620}