/*
 *  CopyRight 1995. Nicholas Poljakov all rights reserved.
 */
/*********** help-type a help-file  **********************/
/*    pid_page-page ident. pointer                       */
/*********************************************************/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>
#include <malloc.h>
#include <stdlib.h>
#define PU  73   /* Page Up      */
#define PD  81   /* Page Down    */
#define ESC '\033'
#define TPG    50
#define MAX_FRAME 31

extern int fp_help;
extern struct index
    {  long reset;
       char ind_str[4];
       short lentxt;
    } index[TPG];
extern struct menu_frame {
    int startx, endx, starty, endy;
    unsigned char *p;
    char **menu;
    char *keys;
    int border;
    int count;
    unsigned char attrib;
    int active;
    int curx, cury;
    char *header;
    unsigned char shd;
    } frame[MAX_FRAME];


    help(pid_page)
            unsigned long *pid_page;
              {
		unsigned long i;
		unsigned n;
                unsigned ch;
		unsigned char str[81];
		char msg[40];
		char num[5];
                long pos;
                char *p_page[50];
                int x,y,k;
		char *p_str = " PgDown for more or  Esc for exit";

		memcpy(num, pid_page, 4);
		num[4] = 0x00;
                k=0;
                while ((memcmp(pid_page,index[k].ind_str,4))&&
                                         (k < 50))
                        k++;
                if (k == 50) {
                   quit(1);
                   return 0;
                }

                i=index[k].lentxt;
                if ((p_page[0] = malloc (i)) == NULL) {
                   quit(2);
                   return 0;
                }
                if ( (pos = lseek(fp_help,index[k].reset ,SEEK_SET)) ==  -1L ) {
                   quit( 4);
                   return 0;
                }
                if ( (read(fp_help,p_page[0],i)) < 0) {
                   quit (5);
                   return 0;
                }
                window(25);
                str[0] = 195;
                memset(&str[1], 196, 78);
                str[79] = 180;
		str[80] = 0x00;
		strcpy(msg, "Help page  ");
		strcat(msg,  num);
		write_string(1,2,msg, frame[25].attrib | 0x0f);
		write_string(2,0,str, frame[25].attrib);
                write_string(22,0,str, frame[25].attrib);
                n=0;

            WRC:
                x=5;
                y=1;
                while (( x < 21 )  && (n < i)) {
                     write_string(x,y,p_page[0]+n, frame[25].attrib & 0xf0);
                     n+=strlen(p_page[0] + n) + 1;
                     x++;
                }
                if (n < i) {
                     k++;
                     p_page[k] = p_page[0] + n;
                     write_string(23,(80-strlen(p_str))/2,p_str, frame[25].attrib & 0xf0);
                }
                else {
			 write_string(23,30,"Press ESC to exit...",frame[25].attrib | 0x0f);
                     }
     GC:
               ch=getch();
               if ( ch == ESC) {
                  deactivate (25);
                  return 0;
               }
               if (ch == 0) {
                    if (getch() == PU ) {
                        if (k > 1)  {
                             k-=2;
                             n=p_page[k]-p_page[0];
                             goto WRC;
                        }
                    }
                    if (getch() == PD) {
                        if ( n < i)
                             goto WRC;
                    }
               }
               goto GC;
 }
/*****************************************************************/
	quit(i)
	  int i;
	   {
	      unsigned ch;

              window(14);
              window_xy(14, 2, 0);
	      switch (i)
                {
                    case 1:
                       { window_puts(14, " Help page not found");
                         break;
                       }
                    case 3:
                       { window_puts(14, " Error open for help");
                         break;
                       }
                    case 2:
                    case 4:
                    case 5:
                       { window_puts(14, "Error read help file");
                         break;
                       }

                    while ((ch = getch()) != ESC) ;
                    deactivate(14);
                  }
        }
   /*********************** THE END ***************************/
