home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Deps / Deps.c next >
Encoding:
C/C++ Source or Header  |  1993-11-14  |  4.1 KB  |  245 lines  |  [TEXT/MPS ]

  1.  
  2. //
  3. // Deps.c
  4. // Archie Cobbs
  5. //
  6. // Generate MPW makefile #include file dependency list.
  7. // This is "simple 'n free" software.
  8. //
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <CursorCtl.h>
  14.  
  15. //
  16. // Create makefile dependency lists.
  17. // Ignores includes in angle brackets.
  18. //
  19.  
  20. // Defintions
  21.  
  22. #define    TRUE                    1
  23. #define    FALSE                    0
  24.  
  25. #define    DEPEND_CHAR                0xC4
  26. #define    CONTINUE_CHAR            0xB6
  27.  
  28. #define    MAX_INCLUDE_DIRS        30
  29. #define    MAX_LINE                2048
  30.  
  31. #define    ALIGN_LEFT                16
  32. #define    ALIGN_RIGHT                90
  33. #define    TAB_SKIP                4
  34.  
  35. // Prototypes
  36.  
  37. static    void        DoFile(char *name, FILE *fp, int first);
  38. static    FILE        *FindFile(char *dir, char *file);
  39. static    void        Align(int *linePosn, int where);
  40.  
  41. // Variables
  42.  
  43. static    char        *idirs[MAX_INCLUDE_DIRS];
  44. static    int            sNumDirs;
  45. static    int            sLeftAlign = ALIGN_LEFT;
  46. static    int            sRightAlign = ALIGN_RIGHT;
  47. static    int            sTabSkip = TAB_SKIP;
  48. static    int            sRecursive = FALSE;
  49.  
  50. //
  51. // Main()
  52. //
  53.  
  54. int
  55. main(int ac, char *av[])
  56. {
  57.     FILE    *fp;
  58.  
  59. // Beach ball
  60.  
  61.     InitCursorCtl(0);
  62.  
  63. // Always check current directory first
  64.  
  65.     idirs[0] = "";
  66.     sNumDirs++;
  67.  
  68. // Parse args
  69.  
  70.     for (ac--, av++; ac; ac--, av++)
  71.     {
  72.  
  73.     // Is it an option or a filename?
  74.  
  75.         if (**av == '-')
  76.         {
  77.             if (!strcmp(*av, "-i"))
  78.             {
  79.                 ac--, av++;
  80.                 if (!ac)
  81.                     goto USAGE;
  82.                 if (sNumDirs == MAX_INCLUDE_DIRS)
  83.                 {
  84.                     fprintf(stderr, "# Deps: only %d include directories allowed\n",
  85.                         MAX_INCLUDE_DIRS);
  86.                     exit(1);
  87.                 }
  88.                 idirs[sNumDirs++] = *av;
  89.             }
  90.             else if (!strcmp(*av, "-t"))
  91.             {
  92.                 ac--, av++;
  93.                 if (!ac)
  94.                     goto USAGE;
  95.                 sTabSkip = atoi(*av);
  96.             }
  97.             else if (!strcmp(*av, "-l"))
  98.             {
  99.                 ac--, av++;
  100.                 if (!ac)
  101.                     goto USAGE;
  102.                 sLeftAlign = atoi(*av);
  103.                 sLeftAlign -= sLeftAlign % sTabSkip;
  104.             }
  105.             else if (!strcmp(*av, "-r"))
  106.             {
  107.                 ac--, av++;
  108.                 if (!ac)
  109.                     goto USAGE;
  110.                 sRightAlign = atoi(*av);
  111.             }
  112.             else if (!strcmp(*av, "-R"))
  113.                 sRecursive = TRUE;
  114.             else
  115.             {
  116.               USAGE:
  117.                 fprintf(stderr, "# Usage: Deps [-R] [-t tabskip] [-l leftalign] "
  118.                                 "[-r rightalign] [-i includedir] filename ...\n");
  119.                 exit(1);
  120.             }
  121.         }
  122.         else
  123.         {
  124.             if ((fp = fopen(*av, "r")) == NULL)
  125.             {
  126.                 perror("# Deps: fopen");
  127.                 exit(1);
  128.             }
  129.             DoFile(*av, fp, TRUE);
  130.             fclose(fp);
  131.         }
  132.  
  133.         SpinCursor(32);
  134.     }
  135.  
  136.     return(0);
  137. }
  138.  
  139. //
  140. // DoFile()
  141. //
  142. // Process one file
  143. //
  144.  
  145. static    void
  146. DoFile(char *name, FILE *fp, int first)
  147. {
  148.     char        line[MAX_LINE], ifile[MAX_LINE];
  149.     int            k, lineNumber, top = first;
  150.     static int    linePosn;
  151.     FILE        *fp2;
  152.  
  153. // Look for include directives including double-quoted filenames
  154.  
  155.     for (lineNumber = 1; fgets(line, MAX_LINE, fp); lineNumber++)
  156.         if (sscanf(line, "#include \"%s", ifile) == 1)
  157.         {
  158.  
  159.         // First time?
  160.  
  161.             if (first)
  162.             {
  163.                 printf("%s.o", name); linePosn = strlen(name) + 2;
  164.                 Align(&linePosn, sLeftAlign);
  165.                 putchar(DEPEND_CHAR);
  166.                 first = FALSE;
  167.             }
  168.  
  169.         // Nuke trailing quote
  170.  
  171.             ifile[strlen(ifile) - 1] = '\0';
  172.  
  173.         // Find included file, print it out
  174.  
  175.             for (k = 0; k < sNumDirs; k++)
  176.                 if (fp2 = FindFile(idirs[k], ifile))
  177.                 {
  178.                     if (linePosn != sLeftAlign && linePosn + 1
  179.                             + strlen(idirs[k]) + strlen(ifile) > sRightAlign)
  180.                     {
  181.                         printf(" %c\n", CONTINUE_CHAR);
  182.                         linePosn = 0;
  183.                         Align(&linePosn, sLeftAlign);
  184.                         putchar(' ');
  185.                         linePosn++;
  186.                     }
  187.                     printf(" %s%s", idirs[k], ifile);
  188.                     linePosn += 1 + strlen(idirs[k]) + strlen(ifile);
  189.                     if (sRecursive)
  190.                         DoFile(NULL, fp2, FALSE);
  191.                     fclose(fp2);
  192.                     break;
  193.                 }
  194.  
  195.         // File not found
  196.  
  197.             if (k == sNumDirs)
  198.             {
  199.                 fprintf(stderr, "File %s; Line %d ", name, lineNumber);
  200.                 fprintf(stderr, "# Deps: warning: file \"%s\" not located\n", ifile);
  201.             }
  202.         }
  203.  
  204. // Done with all depedencies? Then print a newline and flush stdout
  205.  
  206.     if (top)
  207.     {
  208.         putchar('\n');
  209.         fflush(stdout);
  210.     }
  211. }
  212.  
  213. //
  214. // FindFile()
  215. //
  216.  
  217. static    FILE    *
  218. FindFile(char *dir, char *file)
  219. {
  220.     char    name[MAX_LINE];
  221.  
  222.     strcpy(name + strlen(strcpy(name, dir)), file);
  223.     return(fopen(name, "r"));
  224. }
  225.  
  226. //
  227. // Align
  228. //
  229.  
  230. static    void
  231. Align(int *linePosn, int where)
  232. {
  233.     if (*linePosn > where)
  234.     {
  235.         putchar(' ');
  236.         *linePosn += 1;
  237.         return;
  238.     }
  239.     while (*linePosn < where)
  240.     {
  241.         putchar('\t');
  242.         *linePosn += sTabSkip;
  243.         *linePosn -= *linePosn % sTabSkip;
  244.     }
  245. }