TinyMAZE/
TinyMAZE/config/
TinyMAZE/doc/
TinyMAZE/run/msgs/
TinyMAZE/src/
TinyMAZE/src/db/
TinyMAZE/src/ident/
TinyMAZE/src/io/
TinyMAZE/src/prog/
TinyMAZE/src/softcode/
TinyMAZE/src/util/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "db.h"
#include "externs.h"

static void show_help_types(OBJ *player, char *topic)
{
  FILE *fp;
  char buf[1024], buf2[1024];
  char *p;
  char **toshow = NULL;
  int i, ctr = 0;

  if(!(fp = fopen("msgs/helptext", "r")))
  {
    notify(player, "The help system is currently offline.");
    return;
  }

  while(fgets(buf, sizeof(buf), fp))
  {
    if(*buf != '&')
      continue;

    strcpy(buf2, buf+1);
    fgets(buf, sizeof(buf), fp);
    *(buf+strlen(buf)-1) = '\0';
    if(!(p = strchr(buf, ':')))
      continue;
    *p++ = '\0';
    if(string_compare(p, topic))
      continue;
    *(buf2+strlen(buf2)-1) = '\0';

    for(p = buf2;isspace(*p);p++);     /* Get rid of white space */
    if(!ctr)
      toshow = (char **)stack_alloc(sizeof(char *)*(ctr+1), 0, 0);
    else
      toshow = (char **)stack_realloc_tmp(toshow, sizeof(char *)*(ctr+1));

    if(strlen(p) > 25)
      *(p+26) = '\0';

    toshow[ctr] = (char *)stack_alloc(strlen(p)+1, 0, 0);
    strcpy(toshow[ctr], p);
    ctr++;
  }

  if(!ctr)
    notify(player, "There is nothing in help about that.");
  else
  {
    *buf = '\0';

    notify(player, tprintf("|+B|.%s.", my_string("-", 74)));
    notify(player, tprintf("|+B|||M|%s|+B||", my_center(topic, 74)));
    notify(player, tprintf("|+B||%s|", my_string("-", 74)));

    for(i = 0;i < ctr;++i)
    {
      if(i && !(i%3))
      {
        notify(player, tprintf("|+B|| |+Y|%s |+B||", buf));
        *buf = '\0';
      }

      strcat(buf, my_ljust(toshow[i], 24));
    }

    if(*buf)
    {
      for(p = buf+strlen(buf);p-buf < 72;++p)
        *p = ' ';
      notify(player, tprintf("|+B|| |+Y|%s |+B||", buf));
    }

    notify(player, tprintf("|+B|`%s'", my_string("-", 74)));
  }

  fclose(fp);
}

static void show_helpnews(OBJ *player, char *topic, int news)
{
  FILE *fp;
  char buf[1024];
  char *p;
  const char *helpfile = "msgs/helptext";
  const char *newsfile = "msgs/newstext";
  char found = 0;

  if(!(fp = fopen((news)?newsfile:helpfile, "r")))
  {
    notify(player, tprintf("The %s system is currently offline.",
      (news)?"news":"help"));
    return;
  }

  while(fgets(buf, sizeof(buf), fp))
  {
    if(*buf != '&')
      continue;

    for(p = buf+1;isspace(*p);p++);
    if(!string_prefix(p, topic))
      continue;

    while(fgets(buf, sizeof(buf), fp))
    {
      found = 1;

      if(*buf == '&')
        break;

      if(!strncmp(buf, "type:", 5))
        continue;

      *(buf+strlen(buf)-1) = '\0';
      notify(player, buf);
    }

    break;
  }

  if(!found)
    notify(player, tprintf("No %s found on that subject.",
      (news)?"news":"help"));

  fclose(fp);
}

void do_help(OBJ *player, char *arg1, char *arg2)
{
  if(!string_compare(arg1, "type"))
  {
    show_help_types(player, arg2);
    return;
  }

  show_helpnews(player, arg1, 0);
}

void do_news(OBJ *player, char *topic)
{
  show_helpnews(player, topic, 1);
}