lib/eggdrop/help.c File Reference

#include <eggdrop/eggdrop.h>

Go to the source code of this file.

Functions

int help_init ()
int help_shutdown ()
int help_set_default_path (const char *path)
int help_set_default_lang (const char *lang)
char * help_parse_syntax (xml_node_t *node)
help_summary_thelp_summarize_entry (xml_node_t *node)
int help_parse_file (const char *fname)
help_summary_thelp_lookup_summary (const char *name)
xml_node_thelp_lookup_entry (help_summary_t *entry)
static void localized_help_fname (char *buf, size_t size, const char *filename)
int help_load_by_module (const char *name)
help_search_thelp_search_new (const char *searchstr)
int help_search_end (help_search_t *search)
help_summary_thelp_search_result (help_search_t *search)

Variables

static const char rcsid [] = "$Id: help.c,v 1.20 2004-10-17 05:14:06 stdarg Exp $"
static help_file_t ** help_files = NULL
static int nhelp_files = 0
static help_section_tsections = NULL
static int nsections = 0
static char * help_path = NULL
static char * help_lang_default = NULL
static char * help_lang_fallback = "en_US"


Function Documentation

int help_init (  ) 

Definition at line 36 of file help.c.

References help_lang_default, and help_path.

Referenced by eggdrop_init().

00037 {
00038   help_path = strdup("help/");
00039   help_lang_default = strdup("en_US");
00040   return(0);
00041 }

int help_load_by_module ( const char *  name  ) 

Definition at line 210 of file help.c.

References help_lang_fallback, help_parse_file(), help_path, and localized_help_fname().

Referenced by core_init().

00211 {
00212   char fullname[256], buf[256];
00213 
00214   snprintf(fullname, sizeof(fullname), "%s-commands.xml", name);
00215   localized_help_fname(buf, sizeof(buf), fullname);
00216   if (help_parse_file(buf)) {
00217     snprintf(buf, sizeof(buf), "%s/%s/%s", help_path, help_lang_fallback, fullname);
00218     help_parse_file(buf);
00219   }
00220   return(0);
00221 }

xml_node_t* help_lookup_entry ( help_summary_t entry  ) 

Definition at line 171 of file help.c.

References help_summary_t::file, help_summary_t::name, help_file_t::name, xml_node::next_sibling, xml_node_get_str(), xml_node_lookup(), xml_node_unlink(), and xml_parse_file().

Referenced by party_help().

00172 {
00173   xml_node_t *root = xml_parse_file(entry->file->name);
00174   xml_node_t *node;
00175   char *name;
00176 
00177   node = xml_node_lookup(root, 0, "help", 0, 0);
00178   for (; node; node = node->next_sibling) {
00179     xml_node_get_str(&name, node, "name", 0, 0);
00180     if (name && !strcasecmp(name, entry->name)) {
00181       xml_node_unlink(node);
00182       break;
00183     }
00184   }
00185   return(node);
00186 }

help_summary_t* help_lookup_summary ( const char *  name  ) 

Definition at line 155 of file help.c.

References help_section_t::entries, help_summary_t::name, help_section_t::nentries, nsections, and NULL.

Referenced by party_help().

00156 {
00157   int i, j;
00158   help_section_t *section;
00159 
00160   for (i = 0; i < nsections; i++) {
00161     section = sections+i;
00162     for (j = 0; j < section->nentries; j++) {
00163       if (!strcasecmp(section->entries[j]->name, name)) {
00164           return(section->entries[j]);
00165       }
00166     }
00167   }
00168   return(NULL);
00169 }

int help_parse_file ( const char *  fname  ) 

Definition at line 107 of file help.c.

References help_section_t::entries, help_summary_t::file, help_summarize_entry(), help_file_t::name, help_section_t::name, help_section_t::nentries, xml_node::next_sibling, nhelp_files, nsections, NULL, help_file_t::ref, xml_node_delete(), xml_node_get_vars(), and xml_parse_file().

Referenced by help_load_by_module().

00108 {
00109   xml_node_t *root = xml_parse_file(fname);
00110   xml_node_t *node;
00111   char *secname;
00112   help_summary_t *entry;
00113   help_section_t *section;
00114   int i;
00115 
00116   if (!root) return(-1);
00117 
00118   xml_node_get_vars(root, "sn", "section", &secname, "help", &node);
00119   if (!secname || !node) {
00120     xml_node_delete(root);
00121     return(-1);
00122   }
00123 
00124   for (i = 0; i < nsections; i++) {
00125     if (!strcasecmp(sections[i].name, secname)) break;
00126   }
00127   if (i == nsections) {
00128     sections = realloc(sections, sizeof(*sections) * (nsections+1));
00129     sections[nsections].name = strdup(secname);
00130     sections[nsections].entries = NULL;
00131     sections[nsections].nentries = 0;
00132     nsections++;
00133   }
00134   section = sections+i;
00135 
00136   help_files = realloc(help_files, sizeof(*help_files) * (nhelp_files+1));
00137   help_files[nhelp_files] = malloc(sizeof(**help_files));
00138   help_files[nhelp_files]->name = strdup(fname);
00139   help_files[nhelp_files]->ref = 0;
00140 
00141   for (; node; node = node->next_sibling) {
00142     entry = help_summarize_entry(node);
00143     if (!entry) continue;
00144     entry->file = help_files[nhelp_files];
00145     help_files[nhelp_files]->ref++;
00146     section->entries = realloc(section->entries, sizeof(entry) * (section->nentries+1));
00147     section->entries[section->nentries] = entry;
00148     section->nentries++;
00149   }
00150   nhelp_files++;
00151   xml_node_delete(root);
00152   return(0);
00153 }

char* help_parse_syntax ( xml_node_t node  ) 

Definition at line 60 of file help.c.

References egg_mprintf(), xml_node::next_sibling, and xml_node_get_vars().

Referenced by help_summarize_entry().

00061 {
00062   xml_node_t *arg;
00063   char *name, *syntax, *args, *args2, *argname;
00064   int optional;
00065 
00066   xml_node_get_vars(node, "sn", "name", &name, "args.arg", &arg);
00067   args = strdup("");
00068   for (; arg; arg = arg->next_sibling) {
00069     xml_node_get_vars(arg, "si", "name", &argname, "optional", &optional);
00070     if (!name) continue;
00071     if (optional) args2 = egg_mprintf("%s [%s]", args, argname);
00072     else args2 = egg_mprintf("%s <%s>", args, argname);
00073     free(args);
00074     args = args2;
00075   }
00076   syntax = egg_mprintf("%s%s", name, args);
00077   free(args);
00078   return(syntax);
00079 }

int help_search_end ( help_search_t search  ) 

Definition at line 232 of file help.c.

References help_search_t::search.

Referenced by party_help().

00233 {
00234   free(search->search);
00235   free(search);
00236   return(0);
00237 }

help_search_t* help_search_new ( const char *  searchstr  ) 

Definition at line 223 of file help.c.

References help_search_t::search.

Referenced by party_help().

00224 {
00225   help_search_t *search;
00226 
00227   search = calloc(1, sizeof(*search));
00228   search->search = strdup(searchstr);
00229   return(search);
00230 }

help_summary_t* help_search_result ( help_search_t search  ) 

Definition at line 239 of file help.c.

References help_search_t::curentry, help_search_t::cursection, help_section_t::entries, help_summary_t::name, help_section_t::nentries, nsections, NULL, help_search_t::search, and wild_match().

Referenced by party_help().

00240 {
00241   help_section_t *section;
00242   help_summary_t *entry;
00243 
00244   if (search->cursection < 0 || search->curentry < 0 || !search->search) return(NULL);
00245 
00246   while (search->cursection < nsections) {
00247     section = sections+search->cursection;
00248     while (search->curentry < section->nentries) {
00249       entry = section->entries[search->curentry++];
00250       if (wild_match(search->search, entry->name)) return(entry);
00251     }
00252     search->cursection++;
00253     search->curentry = 0;
00254   }
00255   search->cursection = search->curentry = -1;
00256   return(NULL);
00257 }

int help_set_default_lang ( const char *  lang  ) 

Definition at line 54 of file help.c.

References help_lang_default, and str_redup().

00055 {
00056   str_redup(&help_lang_default, lang);
00057   return(0);
00058 }

int help_set_default_path ( const char *  path  ) 

Definition at line 48 of file help.c.

References help_path, and str_redup().

Referenced by core_init().

00049 {
00050   str_redup(&help_path, path);
00051   return(0);
00052 }

int help_shutdown (  ) 

Definition at line 43 of file help.c.

00044 {
00045   return(0);
00046 }

help_summary_t* help_summarize_entry ( xml_node_t node  ) 

Definition at line 81 of file help.c.

References help_parse_syntax(), help_summary_t::name, NULL, help_summary_t::summary, help_summary_t::syntax, and xml_node_get_vars().

Referenced by help_parse_file().

00082 {
00083   help_summary_t *entry;
00084   char *name, *summary, *syntax;
00085   int len;
00086 
00087   xml_node_get_vars(node, "ss", "name", &name, "summary", &summary);
00088   if (!name) return(NULL);
00089 
00090   syntax = help_parse_syntax(node);
00091 
00092   /* Allocate everything at once since it's static. */
00093   len = strlen(name) + strlen(syntax);
00094   if (summary) len += strlen(summary);
00095   entry = malloc(sizeof(*entry) + len + 3);
00096   entry->name = ((char *)entry) + sizeof(*entry);
00097   entry->syntax = entry->name + strlen(name)+1;
00098   entry->summary = entry->syntax + strlen(syntax)+1;
00099   strcpy(entry->name, name);
00100   strcpy(entry->syntax, syntax);
00101   if (summary) strcpy(entry->summary, summary);
00102   else entry->summary[0] = 0;
00103   free(syntax);
00104   return(entry);
00105 }

static void localized_help_fname ( char *  buf,
size_t  size,
const char *  filename 
) [static]

Definition at line 188 of file help.c.

References help_lang_default, help_path, and NULL.

Referenced by help_load_by_module().

00189 {
00190   char *lang;
00191   char *pos;
00192 
00193   lang = getenv("LANG");
00194   if (lang) {
00195     lang = strdup(lang);
00196     pos = strchr(lang, '.');
00197     if (pos == NULL) {
00198       free(lang);
00199       lang = strdup(help_lang_default);
00200     }
00201     else *pos = 0;
00202   } else {
00203     lang = strdup(help_lang_default);
00204   }
00205 
00206   snprintf(buf, size, "%s/%s/%s", help_path, lang, filename);
00207   free(lang);
00208 }


Variable Documentation

help_file_t** help_files = NULL [static]

Definition at line 26 of file help.c.

char* help_lang_default = NULL [static]

Definition at line 33 of file help.c.

Referenced by help_init(), help_set_default_lang(), and localized_help_fname().

char* help_lang_fallback = "en_US" [static]

Definition at line 34 of file help.c.

Referenced by help_load_by_module().

char* help_path = NULL [static]

Definition at line 32 of file help.c.

Referenced by help_init(), help_load_by_module(), help_set_default_path(), and localized_help_fname().

int nhelp_files = 0 [static]

Definition at line 27 of file help.c.

Referenced by help_parse_file().

int nsections = 0 [static]

Definition at line 30 of file help.c.

Referenced by help_lookup_summary(), help_parse_file(), and help_search_result().

const char rcsid[] = "$Id: help.c,v 1.20 2004-10-17 05:14:06 stdarg Exp $" [static]

Definition at line 21 of file help.c.

help_section_t* sections = NULL [static]

Definition at line 29 of file help.c.


Generated on Sun Nov 30 18:43:34 2008 for eggdrop1.9 by  doxygen 1.5.6