]>
jfr.im git - irc/evilnet/x3.git/blob - src/helpfile.c
1 /* helpfile.c - Help file loading and display
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
6 * srvx is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 #if defined(HAVE_DIRENT_H)
31 #if defined(HAVE_SYS_STAT_H)
35 static const struct message_entry msgtab
[] = {
36 { "HFMSG_MISSING_HELPFILE", "The help file could not be found. Sorry!" },
37 { "HFMSG_HELP_NOT_STRING", "Help file error (help data was not a string)." },
41 #define DEFAULT_LINE_SIZE MAX_LINE_SIZE
42 #define DEFAULT_TABLE_SIZE 80
44 extern struct userNode
*global
, *chanserv
, *opserv
, *nickserv
;
45 struct userNode
*message_dest
;
46 struct userNode
*message_source
;
47 struct language
*lang_C
;
48 struct dict
*languages
;
50 static void language_cleanup(void)
52 dict_delete(languages
);
55 static void language_free_helpfile(void *data
)
57 struct helpfile
*hf
= data
;
61 static void language_free(void *data
)
63 struct language
*lang
= data
;
64 dict_delete(lang
->messages
);
65 dict_delete(lang
->helpfiles
);
70 static struct language
*language_alloc(const char *name
)
72 struct language
*lang
= calloc(1, sizeof(*lang
));
73 lang
->name
= strdup(name
);
74 lang
->parent
= lang_C
;
76 languages
= dict_new();
77 dict_set_free_data(languages
, language_free
);
79 dict_insert(languages
, lang
->name
, lang
);
83 /* Language names should use a lang or lang_COUNTRY type system, where
84 * lang is a two-letter code according to ISO-639-1 (or three-letter
85 * code according to ISO-639-2 for languages not in ISO-639-1), and
86 * COUNTRY is the ISO 3166 country code in all upper case.
89 * http://www.loc.gov/standards/iso639-2/
90 * http://www.loc.gov/standards/iso639-2/langhome.html
91 * http://www.iso.ch/iso/en/prods-services/iso3166ma/index.html
93 struct language
*language_find(const char *name
)
95 struct language
*lang
;
96 char alt_name
[MAXLEN
];
99 if ((lang
= dict_find(languages
, name
, NULL
)))
101 if ((uscore
= strchr(name
, '_'))) {
102 strncpy(alt_name
, name
, uscore
-name
);
103 alt_name
[uscore
-name
] = 0;
104 if ((lang
= dict_find(languages
, alt_name
, NULL
)))
108 lang_C
= language_alloc("C");
109 lang_C
->messages
= dict_new();
110 lang_C
->helpfiles
= dict_new();
115 static void language_set_messages(struct language
*lang
, dict_t dict
)
117 dict_iterator_t it
, it2
;
118 struct record_data
*rd
;
123 for (it
= dict_first(dict
), it2
= dict_first(lang_C
->messages
); it
; ) {
124 const char *msgid
= iter_key(it
);
125 int diff
= it2
? irccasecmp(msgid
, iter_key(it2
)) : -1;
130 } else if (diff
> 0) {
132 it2
= iter_next(it2
);
138 msg
= strdup(rd
->d
.qstring
);
140 case RECDB_STRING_LIST
:
141 /* XXX: maybe do an unlistify_help() type thing */
143 log_module(MAIN_LOG
, LOG_WARNING
, "Unsupported record type for message %s in language %s", msgid
, lang
->name
);
146 dict_insert(lang
->messages
, strdup(msgid
), msg
);
148 it2
= iter_next(it2
);
152 it2
= iter_next(it2
);
154 if (extra
|| missing
)
155 log_module(MAIN_LOG
, LOG_WARNING
, "In language %s, %d extra and %d missing messages.", lang
->name
, extra
, missing
);
158 static struct language
*language_read(const char *name
)
161 struct dirent
*dirent
;
162 struct language
*lang
;
164 char filename
[MAXLEN
], *uscore
;
168 /* Never try to read the C language from disk. */
169 if (!irccasecmp(name
, "C"))
172 /* Open the directory stream; if we can't, fail. */
173 snprintf(filename
, sizeof(filename
), "languages/%s", name
);
174 if (!(dir
= opendir(filename
)))
176 if (!(lang
= dict_find(languages
, name
, NULL
)))
177 lang
= language_alloc(name
);
179 /* Find the parent language. */
180 snprintf(filename
, sizeof(filename
), "languages/%s/parent", name
);
181 if (!(file
= fopen(filename
, "r"))
182 || !fgets(filename
, sizeof(filename
), file
)) {
183 strcpy(filename
, "C");
185 if (!(lang
->parent
= language_find(filename
))) {
186 uscore
= strchr(filename
, '_');
189 lang
->parent
= language_find(filename
);
192 lang
->parent
= lang_C
;
195 /* (Re-)initialize the language's dicts. */
196 dict_delete(lang
->messages
);
197 lang
->messages
= dict_new();
198 dict_set_free_keys(lang
->messages
, free
);
199 dict_set_free_data(lang
->messages
, free
);
200 lang
->helpfiles
= dict_new();
201 dict_set_free_data(lang
->helpfiles
, language_free_helpfile
);
203 /* Read all the translations from the directory. */
204 while ((dirent
= readdir(dir
))) {
205 snprintf(filename
, sizeof(filename
), "languages/%s/%s", name
, dirent
->d_name
);
206 if (!strcmp(dirent
->d_name
,"parent")) {
208 } else if (!strcmp(dirent
->d_name
, "strings.db")) {
209 dict
= parse_database(filename
);
210 language_set_messages(lang
, dict
);
212 } else if ((hf
= dict_find(lang_C
->helpfiles
, dirent
->d_name
, NULL
))) {
213 hf
= open_helpfile(filename
, hf
->expand
);
214 dict_insert(lang
->helpfiles
, hf
->name
, hf
);
223 static void language_read_list(void)
225 struct dirent
*dirent
;
228 if (!(dir
= opendir("languages")))
230 while ((dirent
= readdir(dir
))) {
231 if (dirent
->d_name
[0] == '.')
233 #ifdef HAVE_DIRENT_D_TYPE
234 if (dirent
->d_type
!= DT_DIR
)
238 char namebuf
[MAXLEN
];
240 snprintf(namebuf
, sizeof(namebuf
), "languages/%s", dirent
->d_name
);
241 if (stat(namebuf
, &sbuf
) < 0)
243 if (!S_ISDIR(sbuf
.st_mode
))
247 language_alloc(dirent
->d_name
);
252 const char *language_find_message(struct language
*lang
, const char *msgid
) {
253 struct language
*curr
;
257 for (curr
= lang
; curr
; curr
= curr
->parent
)
258 if ((msg
= dict_find(curr
->messages
, msgid
, NULL
)))
260 log_module(MAIN_LOG
, LOG_ERROR
, "Tried to find unregistered message \"%s\" (original language %s)", msgid
, lang
->name
);
264 int strlen_vis(char *str
)
267 for(count
=0;*str
;str
++)
274 table_send(struct userNode
*from
, const char *to
, unsigned int size
, irc_send_func irc_send
, struct helpfile_table table
) {
275 unsigned int ii
, jj
, len
, nreps
, reps
, tot_width
, pos
, spaces
, *max_width
;
276 char line
[MAX_LINE_SIZE
+1];
277 struct handle_info
*hi
;
281 if (IsChannelName(to
) || *to
== '$') {
285 message_dest
= GetUserH(to
);
287 log_module(MAIN_LOG
, LOG_ERROR
, "Unable to find user with nickname %s (in table_send from %s).", to
, from
->nick
);
290 hi
= message_dest
->handle_info
;
291 #ifdef WITH_PROTOCOL_P10
292 to
= message_dest
->numeric
;
295 message_source
= from
;
297 /* If size or irc_send are 0, we should try to use a default. */
301 size
= DEFAULT_TABLE_SIZE
;
302 else if (hi
->table_width
)
303 size
= hi
->table_width
;
304 else if (hi
->screen_width
)
305 size
= hi
->screen_width
;
307 size
= DEFAULT_TABLE_SIZE
;
310 {} /* use that function */
312 irc_send
= HANDLE_FLAGGED(hi
, USE_PRIVMSG
) ? irc_privmsg
: irc_notice
;
314 irc_send
= IsChannelName(to
) ? irc_privmsg
: irc_notice
;
316 /* Limit size to how much we can show at once */
317 if (size
> sizeof(line
))
320 /* Figure out how wide columns should be */
321 max_width
= alloca(table
.width
* sizeof(int));
322 for (jj
=tot_width
=0; jj
<table
.width
; jj
++) {
323 /* Find the widest width for this column */
325 for (ii
=0; ii
<table
.length
; ii
++) {
326 len
= strlen(table
.contents
[ii
][jj
]);
327 if (len
> max_width
[jj
])
330 /* Separate columns with spaces */
331 tot_width
+= max_width
[jj
] + 1;
333 /* How many rows to put in a line? */
334 if ((table
.flags
& TABLE_REPEAT_ROWS
) && (size
> tot_width
))
335 nreps
= size
/ tot_width
;
338 /* Send headers line.. */
339 if (table
.flags
& TABLE_NO_HEADERS
) {
342 /* Sending headers needs special treatment: either show them
343 * once, or repeat them as many times as we repeat the columns
345 for (pos
=ii
=0; ii
<((table
.flags
& TABLE_REPEAT_HEADERS
)?nreps
:1); ii
++) {
347 len
= strlen(table
.contents
[0][jj
]);
348 //line[pos++] = '\02'; /* bold header */
349 spaces
= max_width
[jj
] - len
;
350 if (table
.flags
& TABLE_PAD_LEFT
)
353 memcpy(line
+pos
, table
.contents
[0][jj
], len
);
355 //line[pos++] = '\02'; /* end bold header */
356 if (++jj
== table
.width
)
358 if (!(table
.flags
& TABLE_PAD_LEFT
))
365 sepsize
= strlen_vis(line
);
366 sepstr
= malloc(sepsize
+ 1);
367 memset(sepstr
, '-', sepsize
);
369 irc_send(from
, to
, sepstr
); /* ----------------- */
370 irc_send(from
, to
, line
); /* alpha beta roe */
371 irc_send(from
, to
, sepstr
); /* ----------------- */
374 /* Send the table. */
375 for (jj
=0, pos
=0, reps
=0; ii
<table
.length
; ) {
377 len
= strlen(table
.contents
[ii
][jj
]);
378 spaces
= max_width
[jj
] - len
;
379 if (table
.flags
& TABLE_PAD_LEFT
)
380 while (spaces
--) line
[pos
++] = ' ';
381 memcpy(line
+pos
, table
.contents
[ii
][jj
], len
);
383 if (++jj
== table
.width
) {
384 jj
= 0, ++ii
, ++reps
;
385 if ((reps
== nreps
) || (ii
== table
.length
)) {
387 irc_send(from
, to
, line
);
392 if (!(table
.flags
& TABLE_PAD_LEFT
))
398 if (!(table
.flags
& TABLE_NO_HEADERS
)) {
399 /* Send end bar & free its memory */
402 sepstr
[sepsize
/2-1] = 'E';
403 sepstr
[sepsize
/2] = 'n';
404 sepstr
[sepsize
/2+1]= 'd';
406 irc_send(from
, to
, sepstr
);
410 if (!(table
.flags
& TABLE_NO_FREE
)) {
411 /* Deallocate table memory (but not the string memory). */
412 for (ii
=0; ii
<table
.length
; ii
++)
413 free(table
.contents
[ii
]);
414 free(table
.contents
);
419 vsend_message(const char *dest
, struct userNode
*src
, struct handle_info
*handle
, int msg_type
, expand_func_t expand_f
, const char *format
, va_list al
)
421 void (*irc_send
)(struct userNode
*from
, const char *to
, const char *msg
);
422 static struct string_buffer input
;
423 unsigned int size
, ipos
, pos
, length
, chars_sent
, use_color
;
424 unsigned int expand_pos
, expand_ipos
, newline_ipos
;
425 char line
[MAX_LINE_SIZE
];
427 if (IsChannelName(dest
) || *dest
== '$') {
429 } else if (!(message_dest
= GetUserH(dest
))) {
430 log_module(MAIN_LOG
, LOG_ERROR
, "Unable to find user with nickname %s (in vsend_message from %s).", dest
, src
->nick
);
432 } else if (message_dest
->dead
) {
433 /* No point in sending to a user who is leaving. */
436 #ifdef WITH_PROTOCOL_P10
437 dest
= message_dest
->numeric
;
440 message_source
= src
;
441 if (!(msg_type
& MSG_TYPE_NOXLATE
)
442 && !(format
= handle_find_message(handle
, format
)))
444 /* fill in a buffer with the string */
446 string_buffer_append_vprintf(&input
, format
, al
);
448 /* figure out how to send the messages */
450 msg_type
|= (HANDLE_FLAGGED(handle
, USE_PRIVMSG
) ? 1 : 0);
451 use_color
= HANDLE_FLAGGED(handle
, MIRC_COLOR
);
452 size
= handle
->screen_width
;
453 if (size
> sizeof(line
))
459 if (!size
|| !(msg_type
& MSG_TYPE_MULTILINE
))
460 size
= DEFAULT_LINE_SIZE
;
461 switch (msg_type
& 3) {
463 irc_send
= irc_notice
;
466 irc_send
= irc_wallchops
;
470 irc_send
= irc_privmsg
;
473 /* This used to be two passes, but if you do that and allow
474 * arbitrary sizes for ${}-expansions (as with help indexes),
475 * that requires a very big intermediate buffer.
477 expand_ipos
= newline_ipos
= ipos
= 0;
478 expand_pos
= pos
= 0;
480 while (input
.list
[ipos
]) {
481 char ch
, *value
, *free_value
;
483 while ((ch
= input
.list
[ipos
]) && (ch
!= '$') && (ch
!= '\n') && (pos
< size
)) {
488 if (!input
.list
[ipos
])
490 if (input
.list
[ipos
] == '\n') {
495 unsigned int new_ipos
;
496 /* Scan backwards for a space in the input, until we hit
497 * either the last newline or the last variable expansion.
498 * Print the line up to that point, and start from there.
500 for (new_ipos
= ipos
;
501 (new_ipos
> expand_ipos
) && (new_ipos
> newline_ipos
);
503 if (input
.list
[new_ipos
] == ' ')
505 pos
-= ipos
- new_ipos
;
506 if (new_ipos
== newline_ipos
) {
507 /* Single word was too big to fit on one line; skip
508 * forward to its end and print it as a whole.
510 while (input
.list
[new_ipos
]
511 && (input
.list
[new_ipos
] != ' ')
512 && (input
.list
[new_ipos
] != '\n')
513 && (input
.list
[new_ipos
] != '$'))
514 line
[pos
++] = input
.list
[new_ipos
++];
517 while (input
.list
[ipos
] == ' ')
523 switch (input
.list
[++ipos
]) {
524 /* Literal '$' or end of string. */
530 /* The following two expand to mIRC color codes if enabled
533 value
= use_color
? "\002" : "";
536 value
= use_color
? "\017" : "";
539 value
= use_color
? "\026" : "";
542 value
= use_color
? "\037" : "";
549 value
= global
? global
->nick
: "Global";
552 value
= chanserv
? chanserv
->nick
: "ChanServ";
555 value
= opserv
? opserv
->nick
: "OpServ";
558 value
= nickserv
? nickserv
->nick
: "NickServ";
564 value
= handle
? handle
->handle
: "Account";
566 #define SEND_LINE(TRUNCED) do { \
569 if (!(msg_type & MSG_TYPE_MULTILINE) && (pos > 1) && TRUNCED) \
570 line[pos-2] = line[pos-1] = '.'; \
571 irc_send(src, dest, line); \
575 newline_ipos = ipos; \
576 if (!(msg_type & MSG_TYPE_MULTILINE)) return chars_sent; \
578 /* Custom expansion handled by helpfile-specific function. */
581 struct helpfile_expansion exp
;
582 char *name_end
= input
.list
+ ipos
+ 1, *colon
= NULL
;
584 while (*name_end
!= '}' && *name_end
!= ')' && *name_end
) {
585 if (*name_end
== ':') {
595 struct module *module = module_find(input
.list
+ ipos
+ 1);
596 if (module && module->expand_help
)
597 exp
= module->expand_help(colon
+ 1);
603 exp
= expand_f(input
.list
+ ipos
+ 1);
608 free_value
= value
= exp
.value
.str
;
613 /* Must send current line, then emit table. */
615 table_send(src
, (message_dest
? message_dest
->nick
: dest
), 0, irc_send
, exp
.value
.table
);
620 log_module(MAIN_LOG
, LOG_ERROR
, "Invalid exp.type %d from expansion function %p.", exp
.type
, expand_f
);
623 ipos
= name_end
- input
.list
;
630 value
[1] = input
.list
[ipos
];
634 while ((pos
+ strlen(value
) > size
) || strchr(value
, '\n')) {
636 avail
= size
- pos
- 1;
637 length
= strcspn(value
, "\n ");
638 if (length
<= avail
) {
639 strncpy(line
+pos
, value
, length
);
642 /* copy over spaces, until (possible) end of line */
643 while (*value
== ' ') {
645 line
[pos
++] = *value
;
649 /* word to send is too big to send now.. what to do? */
651 /* try to put it on a separate line */
654 /* already at start of line; only send part of it */
655 strncpy(line
, value
, avail
);
658 /* skip any trailing spaces */
659 while (*value
== ' ')
663 /* if we're looking at a newline, send the accumulated text */
664 if (*value
== '\n') {
669 length
= strlen(value
);
670 memcpy(line
+ pos
, value
, length
);
674 if ((pos
< size
-1) && input
.list
[ipos
]) {
689 send_message(struct userNode
*dest
, struct userNode
*src
, const char *format
, ...)
694 if (IsLocal(dest
)) return 0;
695 va_start(ap
, format
);
696 res
= vsend_message(dest
->nick
, src
, dest
->handle_info
, 0, NULL
, format
, ap
);
702 send_message_type(int msg_type
, struct userNode
*dest
, struct userNode
*src
, const char *format
, ...) {
706 if (IsLocal(dest
)) return 0;
707 va_start(ap
, format
);
708 res
= vsend_message(dest
->nick
, src
, dest
->handle_info
, msg_type
, NULL
, format
, ap
);
714 send_target_message(int msg_type
, const char *dest
, struct userNode
*src
, const char *format
, ...)
719 va_start(ap
, format
);
720 res
= vsend_message(dest
, src
, NULL
, msg_type
, NULL
, format
, ap
);
726 _send_help(struct userNode
*dest
, struct userNode
*src
, expand_func_t expand
, const char *format
, ...)
731 va_start(ap
, format
);
732 res
= vsend_message(dest
->nick
, src
, dest
->handle_info
, 12, expand
, format
, ap
);
738 send_help(struct userNode
*dest
, struct userNode
*src
, struct helpfile
*hf
, const char *topic
)
740 struct helpfile
*lang_hf
;
741 struct record_data
*rec
;
742 struct language
*curr
;
747 _send_help(dest
, src
, NULL
, "HFMSG_MISSING_HELPFILE");
750 for (curr
= (dest
->handle_info
? dest
->handle_info
->language
: lang_C
);
752 curr
= curr
->parent
) {
753 lang_hf
= dict_find(curr
->helpfiles
, hf
->name
, NULL
);
756 rec
= dict_find(lang_hf
->db
, topic
, NULL
);
757 if (rec
&& rec
->type
== RECDB_QSTRING
) {
758 _send_help(dest
, src
, hf
->expand
, rec
->d
.qstring
);
762 rec
= dict_find(hf
->db
, "<missing>", NULL
);
765 if (rec
->type
!= RECDB_QSTRING
) {
766 send_message(dest
, src
, "HFMSG_HELP_NOT_STRING");
769 _send_help(dest
, src
, hf
->expand
, rec
->d
.qstring
);
774 send_help_brief(struct userNode
*dest
, struct userNode
*src
, struct helpfile
*hf
, const char *topic
)
776 struct helpfile
*lang_hf
;
777 struct record_data
*rec
;
778 struct language
*curr
;
783 _send_help(dest
, src
, NULL
, "HFMSG_MISSING_HELPFILE");
786 for (curr
= (dest
->handle_info
? dest
->handle_info
->language
: lang_C
);
788 curr
= curr
->parent
) {
789 lang_hf
= dict_find(curr
->helpfiles
, hf
->name
, NULL
);
792 rec
= dict_find(lang_hf
->db
, topic
, NULL
);
793 if (rec
&& rec
->type
== RECDB_QSTRING
)
798 buf
= malloc(strlen(rec
->d
.qstring
) + 1);
799 strcpy(buf
, rec
->d
.qstring
);
800 *strchr(buf
, '\n') = 0;
802 res
= _send_help(dest
, src
, hf
->expand
, buf
);
807 rec
= dict_find(hf
->db
, "<missing>", NULL
);
809 return 0; /* send_message(dest, src, "MSG_TOPIC_UNKNOWN"); */
810 if (rec
->type
!= RECDB_QSTRING
)
811 return 0; /* send_message(dest, src, "HFMSG_HELP_NOT_STRING"); */
812 return _send_help(dest
, src
, hf
->expand
, rec
->d
.qstring
);
815 /* Grammar supported by this parser:
816 * condition = expr | prefix expr
817 * expr = atomicexpr | atomicexpr op atomicexpr
818 * op = '&&' | '||' | 'and' | 'or'
819 * atomicexpr = '(' expr ')' | identifier
820 * identifier = ( '0'-'9' 'A'-'Z' 'a'-'z' '-' '_' '/' )+ | ! identifier
822 * Whitespace is ignored. The parser is implemented as a recursive
823 * descent parser by functions like:
824 * static int helpfile_eval_<element>(const char *start, const char **end);
833 static const struct {
836 } helpfile_operators
[] = {
837 { "&&", OP_BOOL_AND
},
838 { "and", OP_BOOL_AND
},
839 { "||", OP_BOOL_OR
},
840 { "or", OP_BOOL_OR
},
844 static const char *identifier_chars
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_/";
846 static int helpfile_eval_expr(const char *start
, const char **end
);
847 static int helpfile_eval_atomicexpr(const char *start
, const char **end
);
848 static int helpfile_eval_identifier(const char *start
, const char **end
);
851 helpfile_eval_identifier(const char *start
, const char **end
)
853 /* Skip leading whitespace. */
854 while (isspace(*start
) && (start
< *end
))
857 log_module(MAIN_LOG
, LOG_FATAL
, "Expected identifier in helpfile condition.");
861 if (start
[0] == '!') {
862 int res
= helpfile_eval_identifier(start
+1, end
);
866 } else if (start
[0] == '/') {
868 char *id_str
, *value
;
871 strchr(identifier_chars
, sep
[0]) && (sep
< *end
);
873 memcpy(id_str
= alloca(sep
+1-start
), start
, sep
-start
);
874 id_str
[sep
-start
] = '\0';
875 value
= conf_get_data(id_str
+1, RECDB_QSTRING
);
879 return enabled_string(value
) || true_string(value
);
880 } else if ((*end
- start
>= 4) && !ircncasecmp(start
, "true", 4)) {
883 } else if ((*end
- start
>= 5) && !ircncasecmp(start
, "false", 5)) {
887 log_module(MAIN_LOG
, LOG_FATAL
, "Unexpected helpfile identifier '%.*s'.", *end
-start
, start
);
893 helpfile_eval_atomicexpr(const char *start
, const char **end
)
898 /* Skip leading whitespace. */
899 while (isspace(*start
) && (start
< *end
))
902 log_module(MAIN_LOG
, LOG_FATAL
, "Expected atomic expression in helpfile condition.");
906 /* If it's not parenthesized, it better be a valid identifier. */
908 return helpfile_eval_identifier(start
, end
);
910 /* Parse the internal expression. */
913 res
= helpfile_eval_expr(start
, &sep
);
915 /* Check for the closing parenthesis. */
916 while (isspace(*sep
) && (sep
< *end
))
918 if ((sep
== *end
) || (sep
[0] != ')')) {
919 log_module(MAIN_LOG
, LOG_FATAL
, "Expected close parenthesis at '%.*s'.", *end
-sep
, sep
);
923 /* Report the end location and result. */
929 helpfile_eval_expr(const char *start
, const char **end
)
931 const char *sep
, *sep2
;
932 unsigned int ii
, len
;
936 /* Parse the first atomicexpr. */
938 res_a
= helpfile_eval_atomicexpr(start
, &sep
);
942 /* Figure out what follows that. */
943 while (isspace(*sep
) && (sep
< *end
))
948 for (ii
= 0; helpfile_operators
[ii
].str
; ++ii
) {
949 len
= strlen(helpfile_operators
[ii
].str
);
950 if (ircncasecmp(sep
, helpfile_operators
[ii
].str
, len
))
952 op
= helpfile_operators
[ii
].op
;
955 if (op
== OP_INVALID
) {
956 log_module(MAIN_LOG
, LOG_FATAL
, "Unrecognized helpfile operator at '%.*s'.", *end
-sep
, sep
);
960 /* Parse the next atomicexpr. */
962 res_b
= helpfile_eval_atomicexpr(sep
, &sep2
);
966 /* Make sure there's no trailing garbage */
967 while (isspace(*sep2
) && (sep2
< *end
))
970 log_module(MAIN_LOG
, LOG_FATAL
, "Trailing garbage in helpfile expression: '%.*s'.", *end
-sep2
, sep2
);
974 /* Record where we stopped parsing. */
977 /* Do the logic on the subexpressions. */
980 return res_a
&& res_b
;
982 return res_a
|| res_b
;
989 helpfile_eval_condition(const char *start
, const char **end
)
993 /* Skip the prefix if there is one. */
994 for (term
= start
; isalnum(*term
) && (term
< *end
); ++term
) ;
996 if ((term
+ 2 >= *end
) || (term
[0] != ':') || (term
[1] != ' ')) {
997 log_module(MAIN_LOG
, LOG_FATAL
, "In helpfile condition '%.*s' expected prefix to end with ': '.", *end
-start
, start
);
1003 /* Evaluate the remaining string as an expression. */
1004 return helpfile_eval_expr(start
, end
);
1008 unlistify_help(const char *key
, void *data
, void *extra
)
1010 struct record_data
*rd
= data
;
1011 dict_t newdb
= extra
;
1015 dict_insert(newdb
, strdup(key
), alloc_record_data_qstring(GET_RECORD_QSTRING(rd
)));
1017 case RECDB_STRING_LIST
: {
1018 struct string_list
*slist
= GET_RECORD_STRING_LIST(rd
);
1020 unsigned int totlen
, len
, i
;
1022 for (i
=totlen
=0; i
<slist
->used
; i
++)
1023 totlen
= totlen
+ strlen(slist
->list
[i
]) + 1;
1024 dest
= alloca(totlen
+1);
1025 for (i
=totlen
=0; i
<slist
->used
; i
++) {
1026 len
= strlen(slist
->list
[i
]);
1027 memcpy(dest
+totlen
, slist
->list
[i
], len
);
1028 dest
[totlen
+len
] = '\n';
1029 totlen
= totlen
+ len
+ 1;
1032 dict_insert(newdb
, strdup(key
), alloc_record_data_qstring(dest
));
1035 case RECDB_OBJECT
: {
1038 for (it
= dict_first(GET_RECORD_OBJECT(rd
)); it
; it
= iter_next(it
)) {
1039 const char *k2
, *end
;
1042 /* Evaluate the expression for this subentry. */
1044 end
= k2
+ strlen(k2
);
1045 res
= helpfile_eval_condition(k2
, &end
);
1046 /* If the evaluation failed, bail. */
1048 log_module(MAIN_LOG
, LOG_FATAL
, " .. while processing entry '%s' condition '%s'.", key
, k2
);
1051 /* If the condition was false, try another. */
1054 /* If we cannot unlistify the contents, bail. */
1055 if (unlistify_help(key
, iter_data(it
), extra
))
1059 /* If none of the conditions apply, just omit the entry. */
1068 open_helpfile(const char *fname
, expand_func_t expand
)
1070 struct helpfile
*hf
;
1072 dict_t db
= parse_database(fname
);
1073 hf
= calloc(1, sizeof(*hf
));
1074 hf
->expand
= expand
;
1075 hf
->db
= alloc_database();
1076 dict_set_free_keys(hf
->db
, free
);
1077 if ((slash
= strrchr(fname
, '/'))) {
1078 hf
->name
= strdup(slash
+ 1);
1080 hf
->name
= strdup(fname
);
1081 dict_insert(language_find("C")->helpfiles
, hf
->name
, hf
);
1084 dict_foreach(db
, unlistify_help
, hf
->db
);
1090 void close_helpfile(struct helpfile
*hf
)
1094 free((char*)hf
->name
);
1095 free_database(hf
->db
);
1099 void message_register_table(const struct message_entry
*table
)
1103 while (table
->msgid
) {
1104 dict_insert(lang_C
->messages
, table
->msgid
, (char*)table
->format
);
1109 void helpfile_init(void)
1111 message_register_table(msgtab
);
1112 language_read_list();
1115 void helpfile_finalize(void)
1118 for (it
= dict_first(languages
); it
; it
= iter_next(it
))
1119 language_read(iter_key(it
));
1120 reg_exit_func(language_cleanup
);