muse1.7b4/
muse1.7b4/config/
muse1.7b4/doc/
muse1.7b4/run/
muse1.7b4/run/db/
muse1.7b4/src/
muse1.7b4/src/db/
muse1.7b4/src/files/
muse1.7b4/src/io/
muse1.7b4/src/prog/
muse1.7b4/src/util/
/* help.c */
/* $Id: help.c,v 1.11 1993/09/18 19:03:41 nils Exp $ */

#include <stdio.h>

/* commands for giving help */

#include "db.h"
#include "config.h"
#include "interface.h"
#include "externs.h"
#include "help.h"
#ifdef sun
#include <errno.h>
#else
#include <sys/errno.h>
#endif

void spit_file(player,filename,ff)
     dbref player;
 char *filename;
     FILE *ff;
{
  FILE *f;
  char buf[BUFFER_LEN];
  char *p;
  
  if((filename!=NULL)?((f = fopen(filename, "r")) == NULL):((f=ff)==NULL)) {
    if(filename)
      sprintf(buf, "Sorry, %s is broken.  Management has been notified.",
	      filename);
    else
      strcpy(buf,"Sorry, it's broken. management has been notified.");
    notify(player, buf);
    fputs("spit_file:", stderr);
    if(filename)
      perror(filename);
    else
      perror("");
  } else {
    while(fgets(buf, sizeof buf, f)) {
      for(p = buf; *p; p++) if(*p == '\n') {
	*p = '\0';
	break;
      }
      notify(player, buf);
    }
    fclose(f);
  }
}

/*
  void do_news(player)
  dbref player;
  {
  spit_file(player, NEWS_FILE);
  }
  */
void do_text(player, arg1, arg2)
     dbref player;
     char *arg1;
     char *arg2;
{
  char indx[20];
  char text[20];
  if (strlen(arg1) <1 || strlen(arg1)>10 || strchr(arg1,'/') || strchr(arg1,'.')) {
    notify(player, "illegal text file.");
    return;
  }
  sprintf(indx,"msgs/%sindx",arg1);
  sprintf(text,"msgs/%stext",arg1);
  do_help(player, arg2, arg1, indx, text, NULL);
}

void do_help(player, arg1, deftopic, indxfile, textfile, trigthis)
     dbref player;
     char *arg1;
     char *deftopic;
     char *indxfile;
     char *textfile;
     ATTR *trigthis;
{
  int help_found;
  help_indx entry;
  FILE *fp;
  char *p, line[LINE_SIZE+1];
  
  if ( *arg1 == '\0' )
    arg1 = deftopic;
  
  if ( (fp = fopen(indxfile, "r")) == NULL ) {
    if ( (fp = fopen(textfile, "r")) == NULL) {
      if (errno == ENOENT) {
	/* no such file or directory. no such text bundle. */
	notify(player,tprintf("No such file or directory: %s",textfile));
	return;
      } else {
        log_error(tprintf("%s: errno %d",textfile,errno));
        notify(player,tprintf("%s: error number %d",textfile,errno));
	perror(textfile);
	return;
      }
    } else {
      notify(player,tprintf("%s isn't indexed.",textfile));
      fclose(fp);
      return;
    }
  }
  while ( (help_found = fread(&entry, sizeof(help_indx), 1, fp)) == 1 )
    if ( string_prefix(entry.topic, arg1) )
      break;
  fclose(fp);
  if ( help_found <= 0 )  {
    notify(player, tprintf("No %s for '%s'.", deftopic, arg1));
    return;
  }
  
  if ( (fp = fopen(textfile, "r")) == NULL )  {
    notify(player,
	   tprintf("%s: sorry, temporarily not available.", deftopic));
    log_error(tprintf("can't open %s for reading", textfile));
    return;
  }
  if ( fseek(fp, entry.pos, 0) < 0L )  {
    notify(player,
	   tprintf("%s: sorry, temporarily not available.", deftopic));
    log_error(tprintf("seek error in file %s", textfile));
    fclose(fp);
    return;
  }
  for (;;)  {
    if ( fgets(line, LINE_SIZE, fp) == NULL )
      break;
    if ( line[0] == '&' )
      break;
    for ( p = line; *p != '\0'; p++ )
      if ( *p == '\n' )
	*p = '\0';
    notify(player, line);
  }
  fclose(fp);
  if (trigthis) {
    dbref zon;
    wptr[0] = entry.topic;
    did_it(player, player, NULL, NULL, NULL, NULL, trigthis);
    DOZONE(zon, player)
      did_it(player, zon, NULL, NULL, NULL, NULL, trigthis);
  }
}