#include <stdio.h>
        char *qual[11];	     /* array of qualifiers from input line */
	int n = 0;
	unsigned i;
	int argcnt, flag, m;
	int indlvl = 0;		/* level of indirect files */
	char rootfile[19];
	char *p;
	char line[133];		/* output buffer */
	FILE *help[9];		/* allow for nine levels...not likely */
	FILE **curfil;		/* pointer to current file */

main(argc,argv)
 int argc;
 char *argv[];
 {
	int c;
	int level;
	char buf[133];
	int nlvl, l, dig;

	tiatt();		/* attach terminal for CNTL/O */
	cmdlin(argc,argv);	/* read in qualifiers from command line */
	
	p = cpystr(rootfile,"[1,2]");
	p = cpystr(p, qual[0]);
	p = cpystr(p, ".HLP");
	printf ("%s %s\n", "rootfile is",rootfile);
	if ((help[indlvl] = fopen (rootfile, "R")) == NULL)
		error("cannot open ",rootfile);
	level = '1'; /* we start at level 1 help */
	curfil = help[indlvl];
	for (nlvl=1; nlvl <= argcnt; nlvl++)
	  {
	  scan: if (nlvl < argcnt)	/* search for given qualifiers */
	    {
	    search: while ((fgetss(line, 132, curfil)) != NULL)
	      {
	      if (!isdigit(c = line[0])) continue; /* not digit, skip line */
	      if ((c==level) && (index(line,qual[nlvl])==2))
		{
	        /* we have found the qualifier at indicated level */
	        /* note: dependency on exact digit-otherchar-qualifier */
  	        /*       sequence. This could be improved on, maybe get*/
	        /*       rid of the "index" function.      */
		printf ("%s\n", line);
	        break;	/* stop searching this file*/
		}
	      }
	    if (feof(curfil)) 
	      {
	      fclose(curfil);
	      if (indlvl > 0) curfil = help[--indlvl];
	      else
	      error("qualifier %s not found at level %c\n",qual[nlvl], level);
	      }
	    fgetss(line, 132, curfil);	/* read next line and process*/
	    for (m=0; line[m]==' '; m++) /* skip leading blanks */
		;
	    if (line[m] == '#')		/* # indicates a synonym */
	      {
	      strcpy (qual[nlvl], line[m]);  /* make synonym the qualifier */
	      goto search;		     /* ugh */
	      }
	    if (line[m]=='@') indirect();/*check if indirect, open file if so*/
	    }
	  else
	    { /* at lowest level, output any lower qualifiers */
	    while ((fgetss(line, 132, curfil)) != NULL)
	      {
	      if (!isdigit(c = line[0])) continue; /* skip non-digit lines*/
	      dig = atoi(line);
	      if (c >= level) /* qualifier number higher than level */
		{
		p = &buf;	/* get buf address */
		for (l=1; l <= dig; l++)	/*indent for level*/
		  p = cpystr(p, "  ");
		p = cpystr(p, &line);		/*append text*/
		fgetss(line, 132, curfil);	/*read next line*/
		for (m=0; line[m]==' '; m++)	/*skip blanks*/
			;
		if (line[m] == '#')		/*report synonyms*/
		  {
		  p = cpystr(p, "(see ");
		  p = cpystr(p, &line[m+1]);
	 	  p = cpystr(p, ")" );
		  }
		printf ("%s\n", buf);
		if (line[m] == '@') indirect(); /*open indirect file*/
		}
	        else break;	/*  we've hit the next higher topic */
	      }
	    fclose(curfil);
	    if (indlvl != 0)	/*EOF, are we indirected?*/
	      {			/* yes, we are*/
	      curfil = help[--indlvl];	/*switch to next higher file*/
	      goto scan;		/* ugh */
	      }
	    }
	  level++;			/* next higher ASCII digit */
	  }
}
indirect()	/*open indirect file */
  {
  p = cpystr(rootfile, "[1,2]");	/* build file name */
  p = cpystr(p, &(line[m+1]));
  p = cpystr(p, ".HLP");
  if ((help[++indlvl] = fopen(rootfile,"r")) == NULL)
    error ("no such file as %s", rootfile);
  curfil = help[indlvl];	/* set current file pointer to new file*/
  }
cmdlin(argc,argv) 
 int argc;
 char *argv[];
 {	/* read help qualifiers from command line */
	int cliok;

	argcnt = argc - 1;	/* save arguement count */
	if (argc > 1) 
	 cliok = (!(strcmp(argv[1],"DCL"))) || (!(strcmp(argv[1],"MCR")));
	if ((argc==1) || (!cliok))
	  {
	  printf ("%s\n", "usage: HDX help-tree [qual1 [qual2...]]");
          printf ("%s\n", "       where help-tree is either 'MCR' or 'DCL'");
	  exit(0);
	  }
	n = 0;
	while ((--argc > 0) && (n <= 9)) 
	  {
	  i = strlen(*++argv) + 1; /* plus one for EOL character */
	  if ((p = malloc(i)) == NULL)
	    printf ("no memory for qualifiers\n");
	  strcpy (p, *argv); 
	  qual[n] = p;	             /* assign pointer to qual element */
	  n++;
	  }
	printf ("%s", "Help file qualifiers for: ");
	for (n=0; n < argcnt; n++)
	  printf ("%s%c", qual[n], (n == (argcnt-1)) ? '\n' : ' ');
 }
/* function index -- find location of string t in string s */
index (s,t)	/* returns -1 if t not contained in s */
char s[], t[];
{
  int h, j, k;

  for (h=0; s[h] != '\0'; h++) 
    {
    for (j=h, k=0; t[k] != NULL && s[j] == t[k]; j++, k++)
	;
    if (t[k] == NULL)
      return (h);
    }
  return (-1);
}
