home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / uniflex / ufhelp.c < prev    next >
C/C++ Source or Header  |  1993-08-22  |  6KB  |  196 lines

  1. #include "ufk.h"
  2. #include <sys/dir.h>
  3.  
  4. #define  MAXENT     50          /* max number of files in a help directory */
  5.  
  6. int file_typed,                 /* Number of files typed */
  7.     noprint,                    /* don't print now */
  8.     pos,                        /* printed position */
  9.     hdr_printed,                /* banner printed */
  10.     file_printed;               /* file printed */
  11.  
  12. help()
  13. {
  14.    file_typed = 0;              /* Setup initial values */
  15.    pos = 0;
  16.    noprint = FALSE;
  17.    hdr_printed = FALSE;
  18.    file_printed = FALSE;
  19.    do_help(HELPDIR,1,numprm-1,params);  /* call the workhorse */
  20.    if (!file_typed)             /* Found anything ? */
  21.       prterr(ER_NOTOPIC);       /* No, no such topic */
  22. }
  23.  
  24. do_help(directory,firstarg,filecount,filelist)
  25. char *directory,
  26.      *filelist[];
  27. int  firstarg,
  28.      filecount;
  29. {
  30.    int fp,
  31.        fp1,
  32.        size;
  33.    char hlpstr[80],
  34.         savstr[80],
  35.         save_file[80],
  36.         *pt,
  37.         *save_pt,
  38.         *calloc(),
  39.         get_info();
  40.  
  41.    *save_file = '\0';                   /* No saved info yet */
  42.    if ((fp = open(directory,0)) == -1)  /* Open current directory */
  43.       return(prterr(ER_DIROPN));
  44.    if ((pt = calloc(MAXENT,DIRSIZ)) == 0) /* Get memory for directory info */
  45.       return(prterr(ER_NOMEM));
  46.    save_pt = pt;                      /* Save so we can deallocate it later */
  47.    size = readdir(fp,pt);               /* Read all directory entry's */
  48.    while (size--)                       /* Repeat for every entry */
  49.    {
  50.       strcpy(hlpstr,directory);         /* Save current directory */
  51.       if ((firstarg - 1 >= filecount) && /* Upper level reached, ignore */
  52.            (*pt != '.'))                /* files with leading 'dot' */
  53.       {
  54.          if (!noprint)                  /* We have to print something */
  55.          {
  56.             if (!hdr_printed && (file_typed == 0))
  57.             {
  58.                hdr_printed = TRUE;      /* Print header info */
  59.                printf("Information available:\n\n");
  60.             }
  61.             prt_name(pt);               /* Print available entry */
  62.          }
  63.       }
  64.       else                              /* More levels to come */
  65.       {
  66.          if ((*pt != '.') &&            /* Ignore leading 'dot' entry's */
  67.              (hlpmatch(pt,filelist[firstarg]))) /* Check for a match */
  68.          {
  69.             strcat(hlpstr,"/");         /* Create full filespec */
  70.             hlpstr[strlen(hlpstr) + DIRSIZ] = '\0'; /* Nice terminator */
  71.             strncat(hlpstr,pt,DIRSIZ);
  72.             if (get_info(hlpstr) == '.')/* If first byte of file == '.' */
  73.             {
  74.                strcpy(savstr,directory); /* More to come */
  75.                strcat(savstr,"/.");      /* Create sub-directory spec */
  76.                savstr[strlen(savstr) + DIRSIZ] = '\0'; /* Nice terminator */
  77.                strncat(savstr,pt,DIRSIZ);
  78.                if (firstarg >= filecount) /* Save info in case ambiguous */
  79.                {
  80.                   noprint = TRUE;       /* Don't print it yet */
  81.                   strcpy(save_file,hlpstr); /* Save current spec */
  82.                }
  83.                do_help(savstr,firstarg+1,filecount,filelist); /* Recursive */
  84.                noprint = FALSE;
  85.             }
  86.             else
  87.                type_file(hlpstr);       /* Show this topic */
  88.          }
  89.       }
  90.    pt += DIRSIZ;                        /* Point to next entry */
  91.    }
  92.    if (*save_file)                      /* Some info left ? */
  93.    {
  94.       type_file(save_file);             /* Print it */
  95.       if (file_typed <= 1)              /* More to come ? */
  96.       {
  97.          printf("\nAdditional information available:\n\n");
  98.          do_help(savstr,firstarg+1,filecount,filelist); /* Recursive call */
  99.       }
  100.    }
  101.    if (pos)                             /* Printed something ? */
  102.       printf("\n");
  103.    pos = 0;
  104.    free(save_pt);                       /* Return buffer space */
  105. }
  106.  
  107. char get_info(file)
  108. char *file;
  109. {
  110.    int fp;
  111.    char v;
  112.  
  113.    if ((fp = open(file,0)) == 0)        /* Open data file */
  114.       return(prterr(ER_OPENERR));
  115.    if (read(fp,&v,1) == 0)              /* Get first byte */
  116.       return(prterr(ER_NOHELP));
  117.    close(fp);
  118.    return v;                            /* Give it back */
  119. }
  120.  
  121. readdir(fp,table)
  122. int fp;
  123. char *table;
  124. {
  125.    extern int qcmp();
  126.    char *pt;
  127.    int nument;
  128.    struct direct dirent;
  129.  
  130.    nument = 0;                          /* No valid entries yet */
  131.    pt = table;                          /* Point to start of table */
  132.    while (read(fp,&dirent,sizeof(struct direct)) != 0) /* Read dir entry */
  133.       if (dirent.d_ino != 0)            /* If not a deleted one */
  134.       {
  135.          strcpy(pt,dirent.d_name);      /* Save for later */
  136.          pt += DIRSIZ;                  /* Point to next entry */
  137.          nument++;                      /* Count this one */
  138.       }
  139.    close(fp);
  140.    qsort(table,nument,DIRSIZ,qcmp);     /* Sort it nicely */
  141.    return(nument);                      /* Return number of entries */
  142. }
  143.  
  144. qcmp(a,b)
  145. char *a, *b;
  146. {
  147.    return strncmp(a,b,DIRSIZ);          /* Compare entries for sort */
  148. }
  149.  
  150. prt_name(name)
  151. char *name;
  152. {
  153.    printf("%-14.14s ",name);            /* Print this name */
  154.    if (++pos > 4)                       /* Count position */
  155.    {
  156.       printf("\n");                     /* Time for a new line */
  157.       pos = 0;
  158.    }
  159.    file_typed++;                        /* Count file displayed */
  160. }
  161.  
  162. hlpmatch(a1,a2)
  163. char *a1,*a2;
  164. {
  165.    int cnt,
  166.        len;
  167.  
  168.    cnt = 0;                             /* No characters matched yet */
  169.    len = strlen(a2);
  170.  
  171.    while (*a1 && *a2)                   /* As long as not end of strings */
  172.       if (*a1++ == *a2++)
  173.          cnt++;                         /* These characters match */
  174.    return (cnt == len);                 /* Match against full string */
  175. }
  176.  
  177. type_file(name)
  178. char *name;
  179. {
  180.    FILE *fp;
  181.    char buf[80];
  182.  
  183.    if ((fp = fopen(name,"r")) == -1)    /* Open file to type */
  184.       return(prterr(ER_OPENERR));
  185.    if (file_printed)                    /* New line if we were here before */
  186.       printf("\n");
  187.    else
  188.       file_printed = TRUE;
  189.    printf("%s\n\n",rindex(name,'/') + 1); /* Print this filespec */
  190.    fgets(buf,80,fp);                    /* Read 'indicator for subdir' line */
  191.    while(fgets(buf,80,fp))              /* Get data */
  192.       printf("  %s",buf);               /* Send to output */
  193.    fclose(fp);
  194.    file_typed++;                        /* One more file done */
  195. }
  196.