From 10b376b9beb84b3fdd015eadd9f3e7ae25bfca96 Mon Sep 17 00:00:00 2001 From: Aaron Jones Date: Mon, 25 Mar 2024 08:45:00 +0000 Subject: [PATCH] libathemecore/commandhelp.c: handle indented conditions in help files I had intended while rewriting this functionality a while back to allow for help files to have indented conditions to aid readability for help file maintainers. However, I only added such a file very recently, which resulted in garbage output because I neglected to modify the function that actually processes the file's lines (rather than evaluates the conditions on them). Fixes: atheme/atheme@2a9d68b63285ce77d157 Reported-By: KindOne (Libera.Chat) --- libathemecore/commandhelp.c | 49 +++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/libathemecore/commandhelp.c b/libathemecore/commandhelp.c index 1562cd30d5..94a1835d51 100644 --- a/libathemecore/commandhelp.c +++ b/libathemecore/commandhelp.c @@ -169,37 +169,50 @@ help_display_path(struct sourceinfo *const restrict si, const char *const restri unsigned int ifnest_false = 0; unsigned int ifnest = 0; + unsigned int line = 0; char buf[BUFSIZE]; while (fgets(buf, sizeof buf, fh)) { + line++; + (void) strip(buf); (void) replace(buf, sizeof buf, "&nick&", service_name); - if (strncasecmp(buf, "#if", 3) == 0) + char *str = buf; + + if (*str == '#') { - if (ifnest_false || ! help_evaluate_condition(si, buf + 3)) - ifnest_false++; + str++; - ifnest++; - continue; - } + while (*str == ' ' || *str == '\t') + str++; - if (strncasecmp(buf, "#endif", 6) == 0) - { - if (ifnest_false) - ifnest_false--; + if (strncasecmp(str, "if ", 3) == 0 || strncasecmp(str, "if\t", 3) == 0) + { + str += 3; - if (ifnest) - ifnest--; + if (ifnest_false || ! help_evaluate_condition(si, str)) + ifnest_false++; - continue; - } + ifnest++; + } + else if (strncasecmp(str, "endif", 5) == 0) + { + if (ifnest_false) + ifnest_false--; - if (strncasecmp(buf, "#else", 5) == 0) - { - if (ifnest && ifnest_false < 2) - ifnest_false ^= 1; + if (ifnest) + ifnest--; + } + else if (strncasecmp(str, "else", 4) == 0) + { + if (ifnest && ifnest_false < 2) + ifnest_false ^= 1; + } + else + (void) slog(LG_ERROR, "%s: unrecognised directive '%s' in help file '%s' line %u", + MOWGLI_FUNC_NAME, str, path, line); continue; }