Skip to content

Commit

Permalink
libathemecore/commandhelp.c: handle indented conditions in help files
Browse files Browse the repository at this point in the history
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: 2a9d68b63285ce77d157
Reported-By: KindOne (Libera.Chat)
  • Loading branch information
aaronmdjones committed Mar 25, 2024
1 parent 6748323 commit 10b376b
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions libathemecore/commandhelp.c
Expand Up @@ -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;
}
Expand Down

0 comments on commit 10b376b

Please sign in to comment.