/*
 *  module p88hlp.c
 *  PASCAL-XSC help utility
 *
{*  last modification:  17.09.91  09:47   }
 *  uli 02.08.91 : hlpcopy buffers terminal output.
 *  uli 17.09.91 : new style headers.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define global extern

#include "defs.h"
/* #include "depend.h"
#include "sysutil.h"   */

#include "p88hlp.h"


#ifdef LINT_ARGS
local   void   hlpopen  (void) ;
local   void   hlpscan  (char) ;
local   void   hlpcopy  (void) ;
#else
local  void   hlpopen  () ;
local  void   hlpscan  () ;
local  void   hlpcopy  () ;
#endif


/*****************/
/*  user dialog  */
/*****************/

int tty = '\n' ;   /* char buffer for terminal input */
int    mainmenu = true ;  /* "display mainmenu" toggle */
int screenisclear = true ;
local   int screenlength = SCREENlength ; /* number of lines on screen */

global char   sysdir [] ;     /* defined in depend.c */
global char * clearstr ;
global char * clearcmd ;


						/*-------------------------*/
#ifdef LINT_ARGS
global void clearscreen  ( void)
#else
global void clearscreen ()                      /*  c l e a r s c r e e n  */
#endif
{
   if  (not screenisclear and mainmenu )
     {
       printf ("\n   <<< Press the RETURN key to continue >>>\n") ;
       READLN ;
       tty = getchar () ;
       READLN ;
    } /* continue */

   if  (clearcmd)   system (clearcmd) ;
   else             printf (clearstr) ;
   screenisclear = true ;
}

							/*-----------------*/
#ifdef LINT_ARGS
global int  readtty  (  void)
#else
global int  readtty ()                                  /*  r e a d t t y  */
  /* returns the lower case value of tty and "get"s the */
  /* next value for tty */
#endif
BEGIN(readtty)
  int ch = tty ;
  if  (isupper (ch))   ch = tolower (ch) ;
  tty = getchar () ;
  return ch ;
END /* readtty */                                       /*  r e a d t t y  */
							/*-----------------*/

							/*---------------*/
#ifdef LINT_ARGS
global void prompt  (  char * str )
#else
global void prompt (str)                                /*  p r o m p t  */
  /* prompts the string str and waits for input */
  char * str ;
#endif
BEGIN(prompt)
  while  (tty == ' ' or tty == '\t')  tty = getchar () ;
  while (tty == '\n') {
    printf(str);
    fflush (stdout) ;
    do
      tty = getchar () ;
    while  (tty == ' ' or tty == '\t') ;
  }
END /* prompt */                                        /*  p r o m p t  */
							/*---------------*/

							/*-------------*/
#ifdef LINT_ARGS
global char * scan    ( void)
#else
global char * scan   ()                                 /*   s c a n   */
  /* reads a string from terminal and allocates the string */
#endif
BEGIN (scan)
  char * str;
  int    len = 0;
  char input[256];

  while (tty == ' ' or tty == '\t')
    tty = getchar () ;                          /* skip blanks */
  while (tty != '\n'  and  tty != EOF)  {       /* read */
    input[len++] = tty;
    tty = getchar () ;
  }
  input[len++] = '\0';
  str = (char*) malloc (len);                   /* allocate */
  if (str)
    strcpy (str, input);
  else {
    printf ("\n insufficient memory\n\n");
    exit (-999);
  }
  return (str);                                 /* return string read */

END  /* scan */                                         /*   s c a n   */
							/*-------------*/

							/*------------*/
#ifdef LINT_ARGS
global void  ask (char * nam, char * * val)             /*   a s k    */
#else
global void  ask (nam, val)
  /* asks for string variable called nam and assigns */
  /* its value to *val */
  char * nam, * *val ;
#endif
BEGIN(ask)
  if (tty=='\n')  {
    if  (*val == NULL  or  **val == '\0')
      printf ("Currently empty %s\n", nam) ;
    else
      printf ("Current %s is  %s\n", nam, *val) ;
    printf ("Enter new  %s: ", nam);           /* prompt */
    fflush (stdout) ;
    tty = getchar () ;
  }
  /* if  (*val)   free (*val) ; */
  *val = scan ();

END  /* ask */                                          /*   a s k     */
							/*-------------*/


/***************/
/*  help tool  */
/***************/

local  FILE * hlpfile = NULL ;
local  char *  hlpfnam = NULL ;

							/*-----------------*/
#ifdef LINT_ARGS
global void  hlpinit  ( char * nam )
#else
global void  hlpinit (nam)                              /*  h l p i n i t  */
  char * nam ;
#endif
BEGIN (hlpinit)
  hlpfnam = nam ;
END /* hlpinit */


							/*-----------------*/
#ifdef LINT_ARGS
local  void  hlpopen  ( void)
#else
local  void  hlpopen ()                                 /*  h l p o p e n  */
  /* searches directories for the help file to be read */
#endif
BEGIN (hlpopen)
  char  hlpname [256] ;
		   /* holds the path and filename of the help file */
		   /* used only to report it */
  char * p ;

  hlpfile = fopen (hlpfnam, "r");       /* search in current directory */
  if (hlpfile)  {
    printf("Local help file found. \n");
    return ;
   }

  p = getenv (P88SYS) ;                 /* system environment variable */
  if  (p)    {
    strcat (strcpy (hlpname, p), hlpfnam) ;
    hlpfile = fopen(hlpname,"r");       /* search in system directory */
    if  (hlpfile)  return ;
  }

  strcpy (hlpname, sysdir);
  strcat (hlpname, hlpfnam);
  hlpfile = fopen(hlpname,"r");         /* search in system directory */
  if  (not hlpfile)
      printf("Help file %s not found.\n", hlpname) ;

END  /* hlpopen */                                      /*  h l p o p e n  */
							/*-----------------*/

							/*-----------------*/
#ifdef LINT_ARGS
local  void  hlpscan  ( char c )
#else
local  void  hlpscan (c)                                /*  h l p s c a n  */
  char c ;
#endif
BEGIN (hlpscan)
  int inchar = getc (hlpfile) ;
  int pass2  = false ;

  while  (c != inchar)  {
    if  (inchar == EOF)
      if  (pass2 == true)   break ;             /* not found */
      else  {
	pass2 = true ;
	rewind (hlpfile) ;                      /* reread file */
	inchar = getc (hlpfile) ;
      }

    do  {
      while  (inchar != '\n' and inchar != EOF) /* skip line */
	  inchar = getc (hlpfile) ;
      inchar = getc (hlpfile) ;                 /* get first char of line */
    } while  (inchar != '.' and inchar != EOF) ;
    inchar = getc (hlpfile) ;                   /* get second char of line */
  }
END  /* hlpscan */                                      /*  h l p s c a n  */
							/*-----------------*/

							/*-----------------*/
#ifdef LINT_ARGS
local  void  hlpcopy  ( void)
#else
local  void  hlpcopy ()                                 /*  h l p c o p y  */
#endif
BEGIN (hlpcopy)
  int inchar ;
  int line = 1 ;  /* counter */
  char buffer [100] ;
  char * p ;

  do
    inchar = getc (hlpfile) ;
  while  (inchar != '\n' and inchar != EOF) ; /* skip line */
  inchar = getc (hlpfile) ;

  do {
    p = buffer ;
    while  (inchar != '\n' and inchar != EOF) /* read line */
      { *(p++) = inchar ;
	inchar = getc (hlpfile) ;
      }
    if  (inchar == EOF)   return ;
    *(p++) = inchar ; *p = '\0' ;
    printf (buffer) ;

    inchar = getc (hlpfile) ;                 /* get first char of line */
    if  (++line >= screenlength  and  inchar != '.' and inchar != EOF)
     { line = 1 ;
       printf (" <<< Press the RETURN key to continue  or  q RETURN to quit >>>\n") ;
       READLN ;
       if  (tty == EOF)  return ;
       tty = getchar () ;
       if  ( tty == 'q'  or  tty == 'Q')
	{ READLN ;
	  break ;
	}
       READLN ;
     }
  } while  (inchar != '.' and inchar != EOF) ;
END  /* hlpcopy */                                      /*  h l p c o p y  */
							/*-----------------*/


							/*---------------*/
#ifdef LINT_ARGS
global void helpon  ( char ch )
#else
global void helpon (ch)                                 /*  h e l p o n  */
  char  ch ;
#endif
BEGIN(helpon)
  if (not hlpfile)     hlpopen ();
  if (hlpfile)  {
	 hlpscan (ch) ;
	 hlpcopy () ;
  }
  screenisclear = false ;
END  /* helpon */                                       /*  h e l p o n  */
							/*---------------*/

