Merge more advanced help support.
[jelmer/ctrlproxy.git] / src / help.c
1 /*
2         ctrlproxy: A modular IRC proxy
3         help: module for access the help system
4
5         (c) 2006 Jelmer Vernooij <jelmer@nl.linux.org>
6         (c) 2004 Wilmer van der Gaast <wilmer@gaast.net>
7
8         This program is free software; you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation; either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program; if not, write to the Free Software
20         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "internals.h"
24 #include "help.h"
25 #include "log.h"
26
27 #define BUFSIZE 1100
28
29 void help_free(help_t *h)
30 {
31         g_mapped_file_free(h->file);
32         g_hash_table_destroy(h->entries);
33         g_free(h);
34 }
35
36 GHashTable *help_build_hash(char *data, gsize len)
37 {
38         GHashTable *h = g_hash_table_new(g_str_hash, g_str_equal);
39         gsize i, k;
40         char *p;
41
42         i = 0;
43         while (i < len) {
44                 if (data[i] == '?') {
45                         /* Key starts here */
46                         k = i+1;
47                         p = g_strstr_len(data+k, len-k, "\n%\n");
48                         if (p == NULL) {
49                                 log_global(LOG_WARNING, "Error parsing help file");
50                                 g_hash_table_destroy(h);
51                                 return NULL;
52                         }
53                         p[1] = 0;
54                         i+=strlen(data+i)+2;
55                         p = g_strstr_len(data+k, len-k, "\n");
56                         p[0] = 0;
57                         g_hash_table_insert(h, data+k, p+1);
58                 } else {
59                         log_global(LOG_WARNING, "Unknown character '0x%02x' in help file", 
60                                            data[i]);
61                         g_hash_table_destroy(h);
62                         return NULL;
63                 }
64         }
65         
66         return h;
67 }
68
69 help_t *help_load_file( const char *helpfile )
70 {
71         help_t *h;
72         char *data;
73         GError *error = NULL;
74         gsize len;
75         
76         h = g_new0 (help_t, 1);
77         
78         h->file = g_mapped_file_new(helpfile, TRUE, &error);
79         if (h->file != NULL) {
80                 len = g_mapped_file_get_length(h->file);
81                 data = g_mapped_file_get_contents(h->file);
82         }
83         
84         if( h->file == NULL ) {
85                 log_global(LOG_WARNING, "Unable to open help file `%s': %s", helpfile, 
86                                   error->message);
87                 g_free( h );
88                 return NULL;
89         }
90
91         h->entries = help_build_hash(data, len);
92         if (h->entries == NULL) {
93                 g_mapped_file_free(h->file);
94                 g_free(h);
95         }
96         return h;
97 }
98
99 const char *help_get(help_t *h, const char *string)
100 {
101         if (h == NULL)
102                 return NULL;
103         return g_hash_table_lookup(h->entries, string);
104 }