Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/source/chelp.c
1069 views
1
/*
2
* Copyright Colten Edwards (c) 1996
3
* BitchX help file system.
4
* When Chelp is called the help file is loaded from
5
* BitchX.help and saved. This file is never loaded from disk after this.
6
* Information from the help file is loaded into an array as 0-Topic.
7
* $help() also calls the same routines except this information is loaded
8
* differantly as 1-Topic. this allows us to distingush between them
9
* internally.
10
*/
11
12
#include "irc.h"
13
static char cvsrevision[] = "$Id: chelp.c 127 2011-05-02 11:43:38Z keaston $";
14
CVS_REVISION(chelp_c)
15
#include "struct.h"
16
#include "ircaux.h"
17
#include "chelp.h"
18
#include "output.h"
19
#include "hook.h"
20
#include "misc.h"
21
#include "vars.h"
22
#include "window.h"
23
#define MAIN_SOURCE
24
#include "modval.h"
25
26
#ifdef WANT_CHELP
27
int read_file (FILE *help_file, int helpfunc);
28
int in_chelp = 0;
29
30
struct chelp_entry {
31
char *title;
32
char **contents;
33
char *relates;
34
};
35
36
struct chelp_index {
37
int size;
38
struct chelp_entry *entries;
39
};
40
41
struct chelp_index bitchx_help;
42
struct chelp_index script_help;
43
44
void get_help_topic(const char *args, int helpfunc)
45
{
46
int found = 0, i;
47
char *others = NULL;
48
struct chelp_index *index = helpfunc ? &script_help : &bitchx_help;
49
size_t arglen = strlen(args);
50
51
for (i = 0; i < index->size; i++)
52
{
53
if (!my_strnicmp(index->entries[i].title, args, arglen))
54
{
55
int j;
56
char *text = NULL;
57
if (found++)
58
{
59
m_s3cat(&others, " , ", index->entries[i].title);
60
continue;
61
}
62
if (do_hook(HELPTOPIC_LIST, "%s", index->entries[i].title))
63
put_it("%s", convert_output_format("$G \002$0\002: Help on Topic: \002$1-\002",
64
"%s %s", version, index->entries[i].title));
65
for (j = 0; (text = index->entries[i].contents[j]) != NULL; j++)
66
{
67
if (do_hook(HELPSUBJECT_LIST, "%s , %s", index->entries[i].title, text))
68
{
69
in_chelp++;
70
put_it("%s", convert_output_format(text, NULL));
71
in_chelp--;
72
}
73
}
74
text = index->entries[i].relates;
75
if (text && do_hook(HELPTOPIC_LIST, "%s", text))
76
put_it("%s", convert_output_format(text, NULL));
77
}
78
else if (found)
79
break;
80
}
81
if (!found)
82
{
83
if (do_hook(HELPTOPIC_LIST, "%s", args))
84
bitchsay("No help on %s", args);
85
}
86
87
if (others && found)
88
{
89
if (do_hook(HELPTOPIC_LIST, "%d %s", found, others))
90
put_it("Other %d subjects: %s", found - 1, others);
91
}
92
new_free(&others);
93
}
94
95
BUILT_IN_COMMAND(chelp)
96
{
97
int reload = 0;
98
99
reset_display_target();
100
101
if (args && !my_strnicmp(args, "-dump", 4))
102
{
103
next_arg(args, &args);
104
reload = 1;
105
}
106
107
if (reload || !bitchx_help.size)
108
{
109
char *help_dir = NULL;
110
FILE *help_file;
111
#ifdef PUBLIC_ACCESS
112
malloc_strcpy(&help_dir, DEFAULT_BITCHX_HELP_FILE);
113
#else
114
malloc_strcpy(&help_dir, get_string_var(BITCHX_HELP_VAR));
115
#endif
116
help_file = uzfopen(&help_dir, get_string_var(LOAD_PATH_VAR), 1);
117
new_free(&help_dir);
118
if (!help_file)
119
return;
120
121
read_file(help_file, 0);
122
fclose(help_file);
123
}
124
125
if (!args || !*args)
126
userage(command, helparg);
127
else
128
get_help_topic(args, 0);
129
}
130
131
static void free_index(struct chelp_index *index)
132
{
133
int i, j;
134
135
for (i = 0; i < index->size; i++)
136
{
137
for (j = 0; index->entries[i].contents[j]; j++)
138
new_free(&index->entries[i].contents[j]);
139
new_free(&index->entries[i].contents);
140
new_free(&index->entries[i].title);
141
new_free(&index->entries[i].relates);
142
}
143
new_free(&index->entries);
144
index->size = 0;
145
}
146
147
static int compare_chelp(const void *va, const void *vb)
148
{
149
const struct chelp_entry *a = va, *b = vb;
150
return my_stricmp(a->title, b->title);
151
}
152
153
int read_file(FILE *help_file, int helpfunc)
154
{
155
char line[BIG_BUFFER_SIZE + 1];
156
int item_number = 0;
157
int topic = -1;
158
struct chelp_index *index = helpfunc ? &script_help : &bitchx_help;
159
160
free_index(index);
161
162
while (fgets(line, sizeof line, help_file))
163
{
164
size_t len = strlen(line);
165
if (line[len - 1] == '\n')
166
line[len - 1] = '\0';
167
168
if (!*line || *line == '#')
169
continue;
170
171
if (*line != ' ') /* we got a topic copy to topic */
172
{
173
if (!my_strnicmp(line, "-RELATED", 7))
174
{
175
if (topic > -1)
176
{
177
index->entries[topic].relates = m_strdup(line+8);
178
}
179
}
180
else
181
{
182
topic++;
183
item_number = 0;
184
RESIZE(index->entries, index->entries[0], topic + 1);
185
186
index->entries[topic].title = m_strdup(line);
187
index->entries[topic].contents = new_malloc(sizeof(char *));
188
index->entries[topic].contents[0] = NULL;
189
index->entries[topic].relates = NULL;
190
}
191
}
192
else if (topic > -1)
193
{ /* we found the subject material */
194
item_number++;
195
RESIZE(index->entries[topic].contents, char *, item_number + 1);
196
197
index->entries[topic].contents[item_number-1] = m_strdup(line);
198
index->entries[topic].contents[item_number] = NULL;
199
}
200
}
201
202
index->size = topic + 1;
203
204
qsort(index->entries, index->size, sizeof index->entries[0], compare_chelp);
205
206
return 0;
207
}
208
#endif
209
210