home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 143_01 / help.c < prev    next >
Text File  |  1985-11-14  |  7KB  |  295 lines

  1. /*
  2. %CC1 $1.C -X -E5000
  3. %CLINK $1 DIO WILDEXP -N -S
  4. %DELETE    $1.CRL 
  5. */
  6. /*********************************************************************
  7. *                HELP                     *
  8. **********************************************************************
  9. *           COPYRIGHT 1983 EUGENE H. MALLORY             *
  10. *********************************************************************/
  11. #include "BDSCIO.H"
  12. #include "DIO.H"
  13. #define    MENUCOUNT 120
  14. #define    NOCONTINUE 1 /*    Set to 1 if you    want to    exit at    end of section */
  15. char menutext[MENUCOUNT][MAXLINE];
  16. char *menuptrs[MENUCOUNT];
  17. char **menuv,*cprompt,*lprompt;
  18. int menuctr,mindex;
  19. char string[MAXLINE];
  20. char fname[MAXLINE];
  21. char c;
  22. int fd1;
  23. #ifdef C80
  24. FILE *fcb1;
  25. #else
  26. char fcb1[BUFSIZ];
  27. #endif
  28. int str_comp();
  29.  
  30. main(argc,argv)
  31. char **argv;
  32. int argc;
  33.  
  34.   BEGIN
  35.     int    i,j,menuflag;
  36.     char *fvector[3];
  37.     char **fvectp;
  38.     char *disk;
  39.     int    diskno,vctr;
  40.     dioinit(&argc,argv);
  41. #ifdef C80
  42.     fcb1 = &fd1;
  43. #endif
  44.     menuflag = 0;
  45.     cprompt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  46.     lprompt = "abcdefghijklmnopqrstuvwxyz";
  47.     if (argc !=    2)
  48.       THEN
  49.     disk = "ABCDEFGHIJKLM";
  50.     vctr = 3;
  51.     diskno = bdos(25,0);
  52.     if (!diskno) vctr = 2;
  53.     fvector[0] = "DUMMY";
  54.     fvector[1] = "?:*.HLP";
  55.     fvector[1][0] =    disk[diskno];
  56.     fvector[2] = "A:*.HLP";
  57.     fvectp = fvector;
  58.     wildexp(&vctr,&fvectp);
  59. #ifdef C80
  60.     qsort(&fvectp[1],vctr-1,2,str_comp);
  61. #else
  62.     qsort(&fvectp[1],vctr-1,2,&str_comp);
  63. #endif
  64.     fvectp++;
  65.     filemenu:
  66.     i = menu(vctr-1,fvectp,cprompt);
  67.     if (i == -1) goto exithelp;
  68.     strcpy(fname,fvectp[i]);
  69.     fd1 = fopen(fname,fcb1);
  70.     if (fd1    == ERROR){
  71.         typef("HELP: Unable    to open    %s",fname);
  72.         error("");
  73.     }
  74.     menuflag = 1;
  75.       ENDIF
  76.     else
  77.       THEN
  78.     strcpy(fname,argv[1]);
  79.     strcat(fname,".hlp");
  80.     fd1 = fopen(fname,fcb1);
  81.     if (fd1==ERROR)    
  82.       THEN
  83.         strcpy(fname,"a:");
  84.         strcat(fname,argv[1]);
  85.         strcat(fname,".hlp");
  86.         fd1    = fopen(fname,fcb1);
  87.       ENDIF
  88.     if (fd1==ERROR)    
  89.       THEN
  90.         dioflush();
  91.         typef("HELP: Unable    to find    help for: %s. \n",fname);
  92.         error("");
  93.       ENDIF
  94.       ENDIF
  95.     if (!fgets(string,fcb1))
  96.       THEN
  97.     dioflush();
  98.     typef("HELP: Help file %s empty.",fname);
  99.     error("");
  100.       ENDIF
  101.     menuctr = 0;
  102. #ifdef C80
  103.     for    (i=0;i<MENUCOUNT;i++) menuptrs[i] = menutext[i];
  104. #else
  105.     for    (i=0;i<MENUCOUNT;i++) menuptrs[i] = &menutext[i];
  106. #endif
  107.     menuv = &menuptrs[0];
  108.     if (string[0] == ':')
  109.       THEN
  110.     /* Non indexed file, build index */
  111.     do
  112.       LOOP
  113.         string[strlen(string)-1] = 0;
  114.         if (string[0] == ':')
  115.         strcpy(menutext[menuctr++],string);
  116.         if (menuctr    == MENUCOUNT)
  117.         break;
  118.       ENDLOOP
  119.     while (fgets(string,fcb1));
  120.     fclose(fcb1);
  121.     fopen(fname,fcb1);
  122.     fgets(string,fcb1);
  123.       ENDIF
  124.     else
  125.       THEN
  126.     /* Indexed file, build index */
  127.     do
  128.       LOOP
  129.         string[strlen(string)-1] = 0;
  130.         strcpy(menutext[menuctr++],string);
  131.         if (!fgets(string,fcb1) || menuctr == MENUCOUNT)
  132.           THEN
  133.         /* No colons and no index. */
  134.         fclose(fcb1);
  135.         fopen(fname,fcb1);
  136.         typef("\n\014");
  137.         string[0] = ':';
  138.         fgets(&string[1],fcb1);
  139.         helprint(!menuflag);
  140.         if (menuflag) goto filemenu;
  141.         goto exithelp;
  142.           ENDIF
  143.       ENDLOOP
  144.     while (string[0] != ':');
  145.       ENDIF
  146. /****************************************************************
  147. *      Now,    index is built and the first line read        *
  148. *    past the index.                        *
  149. ****************************************************************/
  150.     again:
  151.     if (menuctr    == 1) /* if only one choice */
  152.       THEN
  153.     helprint(0);
  154.     if (menuflag) goto filemenu;
  155.     else goto exithelp;
  156.       ENDIF
  157.     mindex=menu(menuctr,menuv,lprompt);
  158.     if (mindex == -1 &&    menuflag) goto filemenu;
  159.     if (mindex == -1) goto exithelp;
  160.     while (mindex)
  161.       LOOP
  162.     if (!fgets(string,fcb1))
  163.       THEN
  164.         error("HELP: Ran off end of    help file while    searching.");
  165.       ENDIF
  166.     if (string[0] == ':') mindex--;
  167.       ENDLOOP
  168.     helprint(0);
  169.     fclose(fcb1);
  170.     fopen(fname,fcb1);
  171.     while (fgets(string,fcb1))
  172.       LOOP
  173.     if (string[0] == ':') break;
  174.       ENDLOOP
  175.     goto again;
  176.     exithelp:
  177. #ifndef    C80
  178.     fclose(fcb1);
  179. #endif
  180.     dioflush();
  181.   END
  182.  
  183. helprint(exitflag)
  184. int exitflag;
  185.   BEGIN
  186.     char c;
  187.     int    lctr,noteof;
  188.     printmore:
  189.     if (DIOOUT)    puts(&string[1]);
  190.     typef("%s",&string[1]);
  191.     lctr = 1;
  192.     while (noteof = fgets(string,fcb1))
  193.       LOOP
  194.     if (string[0] == ':') break;
  195.     lctr++;
  196.     if ((string[0] == '\014' || string[0] == ';' ||    lctr ==    21))
  197.       THEN
  198.         lctr = 0;
  199.         typef("\n(More available, hit CR to    continue, ESC to quit.)\n");
  200.         c =    bdos(1,0) & 0xff;
  201.         typef("\n\014");
  202.         if (c == '\003') exit(0);      /* CTL C    */
  203.         if (c == '\033') goto quit;
  204.         if (string[0] == '\014' || string[0] == ';') 
  205.           THEN
  206.         if (DIOOUT) puts(&string[1]);
  207.         typef("%s",&string[1]);
  208.         lctr++;
  209.           ENDIF
  210.         else
  211.           THEN
  212.         if (DIOOUT) puts(string);
  213.         typef("%s",string);
  214.           ENDIF
  215.         continue;
  216.       ENDIF
  217.     if (DIOOUT) puts(string);
  218.     typef("%s",string);
  219.       ENDLOOP
  220.     if (exitflag) goto quit;
  221.     if (!noteof) 
  222.     typef("\n(End of file, CR to return    to menu.)\n");
  223.     else if (NOCONTINUE)
  224.     typef("\n(End of section, CR to return to menu.)\n");
  225.     else
  226.     typef("\n(End of section, CR to continue, ESC to quit.)\n");
  227.     c =    bdos(1,0) & 0xff;
  228.     if (c == '\003') exit(0);      /* CTL C    */
  229.     if (c == '\033' || !noteof || NOCONTINUE) 
  230.       THEN
  231.     quit: 
  232.     return(0);
  233.       ENDIF
  234.     typef("\n\014");
  235.     goto printmore;
  236.   END
  237.  
  238. menu(icounter,strings,prompt)
  239. char **strings,*prompt;
  240. int icounter;
  241.   BEGIN
  242.     int    max,bias,i,j;
  243.     bias = 0;
  244.     lup:
  245.     typef("\n\014");
  246.     if ((icounter-bias)    > 26) max = 26;
  247.     else max = icounter-bias;
  248.     for    (i=0;i<(max+1)/2;i++)
  249.       LOOP
  250.     typef("%c) %s",prompt[i],strings[i+bias]);
  251.     if (i+bias+(max+1)/2 < icounter)
  252.       THEN
  253.         for    (j=0;j<=37-strlen(strings[i+bias]);j++)    typef("    ");
  254.         typef("%c) %s\n",
  255.         prompt[i+(max+1)/2],
  256.         strings[i+bias+(max+1)/2]);
  257.       ENDIF
  258.     else typef("\n");
  259.       ENDLOOP
  260.     typef("\n\n");
  261.     if (icounter > bias+26)
  262.       typef("More selections are available.\n");
  263.     typef("Type    selection, ESC to exit,    CR for more, - to backup :");
  264.     c =    bdos(1,0);
  265.     typef("\n\014");
  266.     if (c == '\003') exit(0);      /* CTL C    */
  267.     if (c == '\033') return(-1);      /* ESC   */
  268.     if (c == '\r' && (icounter > bias+26)) bias    += 26;
  269.     else if (c == '\r' && (icounter <= bias+26)) bias =    0;
  270.     if (c == '-') bias -= 26;
  271.     if (bias ==    -26) bias = icounter-26;
  272.     if (bias < 0) bias = 0;
  273.     if (!isalpha(c)) goto lup;
  274.     for    (i=0;toupper(c)    != toupper(prompt[i]);i++);
  275.     if (i>=max)    goto lup;
  276.     if (i+bias >= icounter) goto lup;
  277.     return(i+bias);
  278.   END
  279.  
  280. int str_comp(s,t)
  281. char **s, **t;
  282.   BEGIN
  283.     char *s1, *t1;
  284.     int    i;
  285.     s1 = *s;
  286.     t1 = *t;
  287.     for    (i=0;i<MAXLINE;i++)
  288.       LOOP
  289.     if (t1[i] != s1[i]) 
  290.     return(    s1[i] -    t1[i] );
  291.     if (s1[i] == '\0') return( 0 );
  292.       ENDLOOP
  293.     return( 0);
  294.   END
  295.