home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / Dme / util / autorefs.c
C/C++ Source or Header  |  1989-11-27  |  4KB  |  159 lines

  1.  
  2. /*
  3.  *  AUTOREFS  refsfile docfile docfile docfile
  4.  *
  5.  *  Given one or more autodoc or .H files (e.g. intuition.doc) or include
  6.  *  files (e.g. exec/types.h), this program appends to a dme.refs file
  7.  *  <refsfile> appropriate lines.
  8.  *
  9.  *  AUTOREFS determines the file type from the extension (.h for header
  10.  *  files, otherwise assumed to be a doc file).
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. main(ac,av)
  16. char *av[];
  17. {
  18.     short i;
  19.     FILE *fi;
  20.     FILE *fo;
  21.  
  22.     if (ac == 1) {
  23.     puts("autorefs outfile docfile docfile ...");
  24.     exit(1);
  25.     }
  26.     fo = fopen(av[1], "a");
  27.     if (!fo) {
  28.     printf("unable to open %s for append\n", av[1]);
  29.     exit(1);
  30.     }
  31.     for (i = 2; i < ac; ++i) {
  32.     register char *file = av[i];
  33.     register short len = strlen(file);
  34.     short doth = 0;
  35.  
  36.     if (len >= 2 && (file[len-1] == 'h' || file[len-1] == 'H') && file[len-2] == '.')
  37.         doth = 1;
  38.  
  39.     fi = fopen(file, "r");
  40.     if (fi) {
  41.         if (doth) {
  42.         printf("Scanning .H  file: %s\n", file);
  43.         scanhfile(fi, fo, file);
  44.         } else {
  45.         printf("Scanning DOC file: %s\n", file);
  46.         scandocfile(fi, fo, file);
  47.         }
  48.         fclose(fi);
  49.     } else {
  50.         printf("Unable to read %s\n", file);
  51.     }
  52.     }
  53. }
  54.  
  55. /*
  56.  *  Find the headers for each function entry and generate a DME.REFS
  57.  *  entry for it.  The @@<N> is a short form seek position (this field
  58.  *  normally holds a search string).
  59.  */
  60.  
  61. scandocfile(fi, fo, filename)
  62. FILE *fi;
  63. FILE *fo;
  64. char *filename;
  65. {
  66.     char buf[256];
  67.     long pos = 0;
  68.  
  69.     while (fgets(buf, 256, fi)) {
  70.     register short len = strlen(buf) - 1;
  71.     register char *ptr = buf + len;
  72.     char *header, *tail;
  73.  
  74.     buf[len] = 0;
  75.     while (ptr != buf && ptr[-1] != ' ' && ptr[-1] != 9)
  76.         --ptr;
  77.     if (ptr != buf && *ptr && strncmp(buf, ptr, strlen(ptr)) == 0) {
  78.         header = ptr;
  79.         for (ptr = buf + len; ptr != buf && IsAlphaNum(ptr[-1]); --ptr);
  80.         tail = ptr;
  81.         fprintf(fo, "%-20s (^l) %s @@%ld\n", tail, filename, pos);
  82.     }
  83.     pos = ftell(fi);
  84.     }
  85. }
  86.  
  87. /*
  88.  *  Find each structure definition (stupid search, assume struct on left
  89.  *  hand side) then generate dme.refs entry from the end point of the
  90.  *  previous structure to the beginning of the next structure.    That is,
  91.  *  the reference refers to the structure and all fields before and after
  92.  *  it until the next structure (before and after).
  93.  */
  94.  
  95. scanhfile(fi, fo, filename)
  96. FILE *fi;
  97. FILE *fo;
  98. char *filename;
  99. {
  100.     char buf[256];
  101.     char sname[128];
  102.     long lin  = 1;
  103.     long lin1;
  104.     long lin2 = 1;
  105.     long pos  = 0;
  106.     long pos1;
  107.     long pos2 = 0;
  108.     short snameisvalid = 0;
  109.     short newsname = 0;
  110.  
  111.     while (fgets(buf, 256, fi)) {
  112.     register char *ptr = buf;
  113.     if (ptr[0] == ' ')  /*  allow one space before struct */
  114.         ++ptr;
  115.                 /*    ok if "union" 1 less than "struct"  */
  116.     if (strncmp(ptr, "struct", 6) == 0 || strncmp(ptr, "union", 5) == 0) {
  117.         if (snameisvalid)
  118.         fprintf(fo, "%-20s %3ld %s @@%ld\n", sname, lin-lin1, filename, pos1);
  119.         SetSName(sname, ptr+6);
  120.         snameisvalid = 0;
  121.         newsname = 1;
  122.         pos1 = pos2;
  123.         lin1 = lin2;
  124.     }
  125.     pos = ftell(fi);
  126.     ++lin;
  127.     if (strncmp(ptr, "};", 2) == 0) {
  128.         pos2 = pos;
  129.         lin2 = lin;
  130.         snameisvalid = newsname;
  131.     }
  132.     }
  133.     if (snameisvalid)
  134.     fprintf(fo, "%-20s %3ld %s @@%ld\n", sname, lin-lin1, filename, pos1);
  135. }
  136.  
  137. SetSName(buf, ptr)
  138. register char *buf, *ptr;
  139. {
  140.     while (*ptr == ' ' || *ptr == 9)
  141.     ++ptr;
  142.     while (*ptr && *ptr != '\n' && *ptr != ' ' && *ptr != 9)
  143.     *buf++ = *ptr++;
  144.     *buf = 0;
  145. }
  146.  
  147. IsAlphaNum(c)
  148. char c;
  149. {
  150.     if ((c >= 'a' && c <= 'z') ||
  151.     (c >= 'A' && c <= 'Z') ||
  152.     (c >= '0' && c <= '9') ||
  153.     (c == '_') || (c == '(') || (c == ')')
  154.     )
  155.     return(1);
  156.     return(0);
  157. }
  158.  
  159.