/*******************************************************************
* whelp(): reads the file wfile.hlp and displays its contents on   *
*	   the screen						   *
*								   *
* created: 1990 Mtwx						   *
*******************************************************************/

/* --------------------- source code revisions, tracked by RCS ---------- */

/* $Header: Hard0:C-Compiler/src/wfile/rcs/whelp.c,v 1.0 91/08/12 20:29:52 Mtwx Exp $ */
/* $Log:	whelp.c,v $
 * Revision 1.0  91/08/12  20:29:52  Mtwx
 * Initial revision
 *  */

/* ------------------------------- includes ----------------------------- */

#include <stdio.h>
#include <stdlib.h>
#ifdef AMIGA
#include <intuition/intuitionbase.h>
#include <proto/exec.h>
#include <proto/dos.h>
#endif
#include "wfile.h"

/* ----------------------- external references ------------------ */

/* ----------------------- routines ----------------------------- */

void whelp()
{
  int i,len,nl_count=0,lines;
  char *rbuf;
  FILE *file;
  char Path[230];
#ifdef AMIGA
  BPTR lock;
  struct IntuitionBase *IntuitionBase;
#endif

  strcpy(Path,"whelp.hlp");
  lines=LINES;	  /* constant derived from wfile.h */
#ifdef AMIGA
  /* find WFILE in search-path to locate WFILE.HLP */
  strcpy(Path,"");
  lock=findpath("wfile");
  if(getpath(lock,Path)==0)
  {
    if(Path[strlen(Path)]!=':')
      strcat(Path,"/");
    strcat(Path,"wfile.hlp");
  }
  UnLock(lock);

  /* open intuition.library to gain window heights */
  if(!(IntuitionBase=(struct IntuitionBase*)
    OpenLibrary("intuition.library",0L)))
  {
    puts("Can't open intuition.library");
    printf("%d lines per window assumed!\n",LINES);
    lines=LINES;
  }
  else
  {
    lines=(IntuitionBase->ActiveWindow->Height-19)/8-1;
    CloseLibrary((struct Library *)IntuitionBase);
  }
#endif
  if((file=fopen(Path,"r"))==NULL)
  {
    perror(Path);
    puts("No help available!");
  }
  else
  {
    fseek(file,0,2);
    len=ftell(file);
    rbuf=(char *)malloc(len);
    if(rbuf==NULL)
    {
      printf("Couldn't get %d bytes of memory for help!!\n",len);
      return;
    }
    fseek(file,0,0);
    if(fread(rbuf,len,1,file) != 1)
    {
      perror("wfile.hlp");
      puts("Error reading help file!");
      return;
    }
    for(i=0;i<len;i++)
    {
      putchar(*(rbuf+i));
      if(*(rbuf+i)=='\n')
	nl_count++;
      if(nl_count==lines)
      {
	printf("\nPRESS RETURN TO CONTINUE");
	getchar();
	puts("");
	nl_count=0;
      }
    }
  }
}
