Sort the chelp index after loading - ensures that all topics are reachable.

Use correct define for PUBLIC_ACCESS.

Some cleanups - separate out help index freeing, change get_help_topic() to return
void.


git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@127 13b04d17-f746-0410-82c6-800466cd88b0
This commit is contained in:
Kevin Easton
2011-05-02 11:43:38 +00:00
parent 1838a37a6c
commit c5b5fdd4af
2 changed files with 53 additions and 52 deletions

View File

@@ -41,7 +41,7 @@ struct chelp_index {
struct chelp_index bitchx_help; struct chelp_index bitchx_help;
struct chelp_index script_help; struct chelp_index script_help;
char *get_help_topic(const char *args, int helpfunc) void get_help_topic(const char *args, int helpfunc)
{ {
int found = 0, i; int found = 0, i;
char *others = NULL; char *others = NULL;
@@ -90,62 +90,64 @@ char *get_help_topic(const char *args, int helpfunc)
put_it("Other %d subjects: %s", found - 1, others); put_it("Other %d subjects: %s", found - 1, others);
} }
new_free(&others); new_free(&others);
if (helpfunc)
return m_strdup(empty_string);
return NULL;
} }
BUILT_IN_COMMAND(chelp) BUILT_IN_COMMAND(chelp)
{ {
static int first_time = 1; int reload = 0;
reset_display_target(); reset_display_target();
if (args && *args == '-' && !my_strnicmp(args, "-dump", 4))
if (args && !my_strnicmp(args, "-dump", 4))
{ {
int i, j;
next_arg(args, &args); next_arg(args, &args);
first_time = 1; reload = 1;
if (bitchx_help.entries)
{
for (i = 0; i < bitchx_help.size; i++)
{
if (bitchx_help.entries[i].contents)
{
for (j =0; bitchx_help.entries[i].contents[j]; j++)
new_free(&bitchx_help.entries[i].contents[j]);
}
new_free(&bitchx_help.entries[i].contents);
new_free(&bitchx_help.entries[i].title);
new_free(&bitchx_help.entries[i].relates);
}
new_free(&bitchx_help.entries);
bitchx_help.size = 0;
}
} }
if (first_time)
if (reload || !bitchx_help.size)
{ {
char *help_dir = NULL; char *help_dir = NULL;
FILE *help_file; FILE *help_file;
#ifdef PUBLIC_SYSTEM #ifdef PUBLIC_ACCESS
malloc_sprintf(&help_dir, "%s", DEFAULT_BITCHX_HELP_FILE); malloc_strcpy(&help_dir, DEFAULT_BITCHX_HELP_FILE);
#else #else
malloc_sprintf(&help_dir, "%s", get_string_var(BITCHX_HELP_VAR)); malloc_strcpy(&help_dir, get_string_var(BITCHX_HELP_VAR));
#endif #endif
if (!(help_file = uzfopen(&help_dir, get_string_var(LOAD_PATH_VAR), 1))) help_file = uzfopen(&help_dir, get_string_var(LOAD_PATH_VAR), 1);
{
new_free(&help_dir);
return;
}
new_free(&help_dir); new_free(&help_dir);
first_time = 0; if (!help_file)
return;
read_file(help_file, 0); read_file(help_file, 0);
fclose(help_file); fclose(help_file);
} }
if (!args || !*args) if (!args || !*args)
{
userage(command, helparg); userage(command, helparg);
return; else
get_help_topic(args, 0);
}
static void free_index(struct chelp_index *index)
{
int i, j;
for (i = 0; i < index->size; i++)
{
for (j = 0; index->entries[i].contents[j]; j++)
new_free(&index->entries[i].contents[j]);
new_free(&index->entries[i].contents);
new_free(&index->entries[i].title);
new_free(&index->entries[i].relates);
} }
get_help_topic(args, 0); new_free(&index->entries);
index->size = 0;
}
static int compare_chelp(const void *va, const void *vb)
{
const struct chelp_entry *a = va, *b = vb;
return my_stricmp(a->title, b->title);
} }
int read_file(FILE *help_file, int helpfunc) int read_file(FILE *help_file, int helpfunc)
@@ -155,6 +157,8 @@ int read_file(FILE *help_file, int helpfunc)
int topic = -1; int topic = -1;
struct chelp_index *index = helpfunc ? &script_help : &bitchx_help; struct chelp_index *index = helpfunc ? &script_help : &bitchx_help;
free_index(index);
while (fgets(line, sizeof line, help_file)) while (fgets(line, sizeof line, help_file))
{ {
size_t len = strlen(line); size_t len = strlen(line);
@@ -197,6 +201,8 @@ int read_file(FILE *help_file, int helpfunc)
index->size = topic + 1; index->size = topic + 1;
qsort(index->entries, index->size, sizeof index->entries[0], compare_chelp);
return 0; return 0;
} }
#endif #endif

View File

@@ -4770,19 +4770,16 @@ BUILT_IN_FUNCTION(function_umask, words)
RETURN_INT(umask(new_umask)); RETURN_INT(umask(new_umask));
} }
extern char *get_help_topic (const char *, int);
extern int read_file (FILE *, int);
BUILT_IN_FUNCTION(function_help, words) BUILT_IN_FUNCTION(function_help, words)
{ {
#ifdef PUBLIC_ACCESS #if defined(WANT_CHELP) && !defined(PUBLIC_ACCESS)
RETURN_INT(0); extern void get_help_topic(const char *, int);
#else extern int read_file(FILE *, int);
#ifdef WANT_CHELP
char *filename = NULL, *subject = NULL; char *filename = NULL, *subject = NULL;
static int first_time = 1; static int first_time = 1;
FILE *help_file; FILE *help_file;
GET_STR_ARG(subject, words); GET_STR_ARG(subject, words);
if (words && *words) if (words && *words)
words = next_arg(filename, &words); words = next_arg(filename, &words);
@@ -4802,11 +4799,9 @@ FILE *help_file;
read_file(help_file, 1); read_file(help_file, 1);
fclose(help_file); fclose(help_file);
} else if (first_time) RETURN_EMPTY; } else if (first_time) RETURN_EMPTY;
return get_help_topic(subject, (filename) ? 1 : 0); get_help_topic(subject, (filename) ? 1 : 0);
#else #endif
RETURN_EMPTY; RETURN_EMPTY;
#endif
#endif
} }
BUILT_IN_FUNCTION(function_isuser, words) BUILT_IN_FUNCTION(function_isuser, words)