home *** CD-ROM | disk | FTP | other *** search
- /* Help: (C) Copyright 1986 Michael A. Shiels
-
- This program is Copyright by Michael Shiels and may be given away and
- used in other programs as long as the Copyright notices are not removed
- or altered. No profit can be made from the distribution of this product
- except by the author.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <io.h>
- #include <conio.h>
- #include <direct.h>
- #include <process.h>
- #include <signal.h>
- #include <string.h>
-
- #include "getargs.h"
- #include "masdos.h"
- #include "maserr.h"
-
- #include "helpdir.h"
-
- #include "denv.x"
- #include "unargv.x"
- #include "getargs.x"
- #include "reargv.x"
- #include "mynewlin.x"
- #include "fptext.x"
-
- extern int Lusage( char * );
-
- static int Arg_Copy = 1;
- static int Arg_Max_Wild = 100;
- static int Arg_Max_Help = 100;
- static char *Arg_Help_Dir = "";
- static int Arg_Num_Cols = 5;
- static int Arg_Vert_Prt = 0;
-
-
- ARG Argtab[] =
- {
- { 'c', ARG_INTEGER, &Arg_Num_Cols, "Number of columns for Topics" },
- { 'd', ARG_STRING, (int *)&Arg_Help_Dir, "Help Directory" },
- { 'h', ARG_INTEGER, &Arg_Max_Help, "Maximum number of help Topics" },
- { 'v', ARG_SBOOLEAN, &Arg_Vert_Prt, "Print Topics Vertically" },
- { 'w', ARG_INTEGER, &Arg_Max_Wild, "Maximum number of wildcard searchs" },
- { '(', ARG_SBOOLEAN, &Arg_Copy, "Print Copyright Notice" },
- { '!', ARG_PROCESS, Lusage, "Long Usage" }
- };
-
- #define ARG_TABSIZE ( sizeof(Argtab) / sizeof(ARG) )
-
- void usage()
- {
- E( "Usage: Help [Topic [Subtopic]...]\n" );
- E( "\n" );
- E( "Version 1.00 Copyright (c) 1986, Michael A. Shiels\n");
- E( "\n" );
- }
-
- Lusage( dum )
- char *dum;
- {
- E( "Usage: Help [Topic [Subtopic]...]\n" );
- E( "\n");
- E( "Set the environment variable SWITCHAR to the character\n");
- E( "you wish to use for the Switch Character.\n");
- E( "\n" );
- E( "Set the environment variable HELPOPTS to the standard options\n");
- E( "you wish to have Help use every time. These can be overidden\n");
- E( "with the command line switches. (eg. HELPOPTS=( will cause it to\n");
- E( "suppress the copyright notice)\n");
- E( "\n" );
- E( "Set the environment variable GETSOPTS to the directory where\n");
- E( "you wish to store the .RC file for Help to use. These can be overidden\n");
- E( "with the command line switches. (eg. HELP.RC contains ( will\n");
- E( "cause it to supress the copyright notice)\n");
- E( "\n" );
- E2( "Case of the command line switches %s important\n", ARG_ICase ? "is not" : "is" );
- E( "\n" );
- E( "Version 1.00 Copyright (c) 1986, Michael A. Shiels. All rights reserved.\n" );
- E( "\n" );
- exit( E_USAGE );
- }
-
- #define HELPDIR "/usr/help"
-
- char olddir[101];
-
- static char helplev[101], *helplevp = helplev;
- static FILE *stderrR;
- static int oldraw;
- static int helpctrlchit = 0;
- static int helpwild = 0;
-
- extern helpreset();
- extern char *helpprompt();
- extern void helplevel();
- extern void helptopics();
- extern void helpdisplay1( char * );
- extern void helpdisplay2( char * );
- extern void helpr( char * );
-
- mynewline( where )
- FILE *where;
- {
- static int linenum = 0;
- int oldraw;
-
- fputs( "\n", where );
- if( !(++linenum % 24) )
- {
- fflush( where );
- oldraw = getraw( fileno( where ) );
- setraw( fileno( where ), 0 );
- fputs( P_REVERSE, stderr );
- fputs( P_BLINKING, stderr );
- fputs("<Hit any character for next page>",stderr);
- fputs( P_ALL_OFF, stderr );
- getch();
- setraw( fileno( where ), oldraw );
-
- fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",where);
- fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", where );
- fputs(" ",where);
- fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",where);
- fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", where );
- fflush( where );
- }
- return( 0 );
- }
-
- helpctrlc()
- {
- helpctrlchit = 1;
- signal( SIGINT, helpctrlc );
- }
-
- helpreset()
- {
- cd( olddir );
- setraw( fileno( stderrR ), 0 );
- fclose( stderrR );
- exit( 0 );
- }
-
- char *helpprompt()
- {
- static char tmpstr[103], *tmpstrp = tmpstr;
-
- tmpstr[0] = 100;
- redo:
- mynewline( stderrR );
- if( helplevp == NULL || *helplevp == NULL )
- fprintf( stderrR, "Topic? " );
- else
- fprintf( stderrR, "%s %s", helplevp, "Subtopic? " );
- fflush( stderrR );
- oldraw = getraw( fileno( stderrR ) );
- setraw( fileno( stderrR ), 0 );
- tmpstrp = cgets( tmpstr );
- setraw( fileno( stderrR ), 1 );
- mynewline( stderrR );
- if( tmpstr[2] == 26 )
- helpreset();
- if( tmpstr[2] == '?' && tmpstr[3] == '\0' )
- {
- helplevel( NULL );
- if( access( "intro.txt", 0 ) >= 0 )
- helpdisplay1( "intro.txt" );
- helptopics();
- goto redo;
- }
- else if( tmpstr[2] == '#' && tmpstr[3] == '\0' )
- {
- helplevel( NULL );
- if( access( "intro.txt", 0 ) >= 0 )
- helpdisplay1( "intro.txt" );
- goto redo;
- }
- else if( tmpstr[2] == '$' && tmpstr[3] == '\0' )
- {
- helplevel( NULL );
- helptopics();
- goto redo;
- }
- else if( tmpstr[2] == '!' && tmpstr[3] == '\0' )
- {
- spawnl( P_WAIT, getenv("COMSPEC"), getenv("COMSPEC"), NULL );
- goto redo;
- }
- else if( tmpstr[2] == '!' )
- {
- system( tmpstrp + 1 );
- goto redo;
- }
- else if( tmpstr[1] > 0 )
- return( tmpstrp );
- else
- return( NULL );
- }
-
- void helplevel( level )
- char *level;
- {
- if( (level != NULL && *level != NULL) || (helplevp != NULL && *helplevp != NULL) )
- {
- mynewline( stderrR );
- }
- if( helplevp != NULL && *helplevp != NULL )
- fprintf( stderrR, "%s%s%s", P_REVERSE, helplevp, P_ALL_OFF );
- if( level != NULL && *level != NULL )
- fprintf( stderrR, " %s%s%s", P_REVERSE, level, P_ALL_OFF );
- if( (level != NULL && *level != NULL) || (helplevp != NULL && *helplevp != NULL) )
- {
- mynewline( stderrR );
- mynewline( stderrR );
- }
- fflush( stderrR );
- }
-
- static void printdir(dirc, dirv, maxwidth)
- int dirc ;
- char **dirv ;
- {
- if( !Arg_Num_Cols )
- Arg_Num_Cols = 79 / (maxwidth+1) ;
-
- mynewline( stderrR );
- if( Arg_Vert_Prt )
- fptextv( dirc, dirv, Arg_Num_Cols, 79 / Arg_Num_Cols,
- (dirc/Arg_Num_Cols) + (dirc % Arg_Num_Cols != 0), stderrR );
- else
- fptexth( dirc, dirv, Arg_Num_Cols, 79 / Arg_Num_Cols,
- (dirc/Arg_Num_Cols) + (dirc % Arg_Num_Cols != 0), stderrR );
- mynewline( stderrR );
- }
-
- void helptopics()
- {
- char tmpstr[101], *tmpstrp = tmpstr;
- int i;
- HDIRECTORY *hdp1 = 0;
- char *nexthelp;
-
- if( (hdp1 = mk_hdir( Arg_Max_Help )) )
- {
- hdp1->sort = 1;
- hdp1->dirs = 1;
- hdp1->files = 1;
- hdir( "*.hl*" , hdp1 );
- if( (hdp1->ndirs + hdp1->nfiles) )
- {
- mynewline( stderrR );
- if( helplevp == NULL || *helplevp == NULL )
- fprintf( stderrR, "Topics Available." );
- else
- fprintf( stderrR, "Subtopics Available." );
- mynewline( stderrR );
- fflush( stderrR );
- printdir( hdp1->nfiles + hdp1->ndirs, hdp1->dirv, hdp1->width);
- }
- del_hdir( hdp1 );
- }
- else
- {
- E( "Help:helptopics: unable to create hdir structure for topic searching\n" );
- }
- }
-
- void helpdisplay1( filename )
- char *filename;
- {
- register FILE *fp;
- char buf[1024];
- int (*oldsignal)();
-
- if( getenv("HELPDISPLAY1") )
- {
- putenv("CMDLINE");
- spawnlp( P_WAIT, getenv("HELPDISPLAY1"), getenv("HELPDISPLAY1"), filename, NULL );
- }
- else
- {
- helpctrlchit = 0;
- oldsignal = signal( SIGINT, helpctrlc );
- if( (fp = fopen( filename, "r")) )
- {
- while( fgets(buf, 1024, fp) != NULL && !helpctrlchit )
- {
- buf[strlen(buf)-1] = '\0';
- fputs( buf, stderrR );
- mynewline( stderrR );
- }
- fflush( stderrR );
- fclose( fp );
- signal( SIGINT, oldsignal );
- helpctrlchit = 0;
- }
- }
- }
-
- void helpdisplay2( filename )
- char *filename;
- {
- register FILE *fp;
- char buf[1024];
- int (*oldsignal)();
-
- if( getenv("HELPDISPLAY2") )
- {
- putenv("CMDLINE");
- spawnlp( P_WAIT, getenv("HELPDISPLAY2"), getenv("HELPDISPLAY2"), filename, NULL );
- }
- else
- {
- helpctrlchit = 0;
- oldsignal = signal( SIGINT, helpctrlc );
- if( (fp = fopen( filename, "r")) )
- {
- while( fgets(buf, 1024, fp) != NULL && !helpctrlchit )
- {
- buf[strlen(buf)-1] = '\0';
- fputs( buf, stderrR );
- mynewline( stderrR );
- }
- fflush( stderrR );
- fclose( fp );
- signal( SIGINT, oldsignal );
- helpctrlchit = 0;
- }
- }
- }
-
- dohelp( retstr )
- char *retstr;
- {
- char tmpstr[101], *tmpstrp = tmpstr;
- char tmpstr2[101], *tmpstr2p = tmpstr2;
- int i;
- HDIRECTORY *hdp = 0;
- char *nexthelp;
- char *tmpp;
- int (*oldsignal)();
-
- nexthelp = next( &retstr, ' ', 0 );
- strcpy( tmpstr, nexthelp );
- strcat( tmpstr, ".hlp" );
- strcpy( tmpstr2, nexthelp );
- strcat( tmpstr2, ".hld" );
- if( chdir( tmpstr2 ) >= 0 )
- {
- strcat( helplevp, " " );
- strcat( helplevp, nexthelp );
- helpr( retstr );
- chdir( ".." );
- tmpp = strrchr( helplevp, ' ' );
- *tmpp = '\0';
- }
- else if( access( tmpstr, 0 ) >= 0 )
- {
- helplevel( nexthelp );
- if( retstr != NULL && *retstr != NULL )
- {
- fprintf( stderrR, "Help: reached end of help tree - ignoring '%s'", retstr );
- mynewline( stderrR );
- }
- helpdisplay2( tmpstr );
- }
- else
- {
- if( (hdp = mk_hdir( Arg_Max_Wild ) ) )
- {
- strcpy( tmpstr, nexthelp );
- strcat( tmpstr, "*.hl*" );
- hdp->files = 1;
- hdp->dirs = 1;
- hdp->sort = 1;
- hdir( tmpstr, hdp );
- if( ( hdp->ndirs + hdp->nfiles ) == 1 )
- {
- strcpy( tmpstr, hdp->dirv[0] );
- strcat( tmpstr, retstr );
- dohelp( tmpstrp );
- }
- else if( ( hdp->ndirs + hdp->nfiles ) == 0 )
- {
- fprintf( stderrR, "Help: unable to find help for '%s'", nexthelp );
- mynewline( stderrR );
- }
- else
- {
- i = 0;
- helpwild = 1;
- helpctrlchit = 0;
- oldsignal = signal( SIGINT, helpctrlc );
- while( i <= ( hdp->ndirs + hdp->nfiles - 1 ) && !helpctrlchit )
- {
- strcpy( tmpstr, hdp->dirv[i++] );
- strcat( tmpstr, retstr );
- dohelp( tmpstrp );
- tmpstrp = tmpstr;
- }
- signal( SIGINT, oldsignal );
- helpctrlchit = 0;
- helpwild = 0;
- }
- del_hdir( hdp );
- }
- else
- {
- E( "Help:dohelp: Unable to create hdir structure for multiple help requests\n" );
- }
- }
- }
-
- void helpr( topic )
- char *topic;
- {
- char *retstr = 1;
-
- if( topic == NULL || *topic == NULL )
- {
- helplevel( NULL );
- if( access( "intro.txt", 0 ) >= 0 )
- helpdisplay1( "intro.txt" );
- helptopics();
- }
- else
- {
- dohelp( topic );
- }
- while( retstr != NULL && !helpwild )
- {
- retstr = helpprompt();
- if( retstr != NULL && *retstr != NULL )
- {
- dohelp( retstr );
- }
- }
- }
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- char helpstr[128], *helpstrp = helpstr;
-
- reargv( &argc, &argv );
- getsopts( Argtab, ARG_TABSIZE, "HELP.RC" );
- Arg_Copy = 1;
- getopts( Argtab, ARG_TABSIZE, "HELPOPTS" );
- argc = getargs( argc, argv, Argtab, ARG_TABSIZE );
-
- if( Arg_Copy )
- E( "Help Version 1.00 Copyright (c) 1986, Michael A. Shiels. All rights reserved.\n" );
-
- signal( SIGINT, helpreset );
-
- stderrR = fopen( "con", "w" );
- if (stderrR == NULL)
- {
- E( "Help: can't open 'con' for rawmode\n");
- exit( 100 );
- }
- setraw( fileno( stderrR ), 1 );
-
- getcwd( olddir, 101 );
-
- if( Arg_Help_Dir == NULL || *Arg_Help_Dir == NULL )
- Arg_Help_Dir = getdenv("HELPDIR");
- if( Arg_Help_Dir == NULL || *Arg_Help_Dir == NULL )
- Arg_Help_Dir = HELPDIR;
-
- if( cd( Arg_Help_Dir ) < 0)
- {
- E2( "Help: Help root directory not found for %s.\n", Arg_Help_Dir );
- exit(100);
- }
-
- *argv++;
- unargv( argc-1, argv, helpstrp, 128 );
-
- if (argc >= 1)
- helpr( helpstr );
- else
- helpr( NULL );
-
- helpreset();
- }
-