home *** CD-ROM | disk | FTP | other *** search
-
- //
- // Deps.c
- // Archie Cobbs
- //
- // Generate MPW makefile #include file dependency list.
- // This is "simple 'n free" software.
- //
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <CursorCtl.h>
-
- //
- // Create makefile dependency lists.
- // Ignores includes in angle brackets.
- //
-
- // Defintions
-
- #define TRUE 1
- #define FALSE 0
-
- #define DEPEND_CHAR 0xC4
- #define CONTINUE_CHAR 0xB6
-
- #define MAX_INCLUDE_DIRS 30
- #define MAX_LINE 2048
-
- #define ALIGN_LEFT 16
- #define ALIGN_RIGHT 90
- #define TAB_SKIP 4
-
- // Prototypes
-
- static void DoFile(char *name, FILE *fp, int first);
- static FILE *FindFile(char *dir, char *file);
- static void Align(int *linePosn, int where);
-
- // Variables
-
- static char *idirs[MAX_INCLUDE_DIRS];
- static int sNumDirs;
- static int sLeftAlign = ALIGN_LEFT;
- static int sRightAlign = ALIGN_RIGHT;
- static int sTabSkip = TAB_SKIP;
- static int sRecursive = FALSE;
-
- //
- // Main()
- //
-
- int
- main(int ac, char *av[])
- {
- FILE *fp;
-
- // Beach ball
-
- InitCursorCtl(0);
-
- // Always check current directory first
-
- idirs[0] = "";
- sNumDirs++;
-
- // Parse args
-
- for (ac--, av++; ac; ac--, av++)
- {
-
- // Is it an option or a filename?
-
- if (**av == '-')
- {
- if (!strcmp(*av, "-i"))
- {
- ac--, av++;
- if (!ac)
- goto USAGE;
- if (sNumDirs == MAX_INCLUDE_DIRS)
- {
- fprintf(stderr, "# Deps: only %d include directories allowed\n",
- MAX_INCLUDE_DIRS);
- exit(1);
- }
- idirs[sNumDirs++] = *av;
- }
- else if (!strcmp(*av, "-t"))
- {
- ac--, av++;
- if (!ac)
- goto USAGE;
- sTabSkip = atoi(*av);
- }
- else if (!strcmp(*av, "-l"))
- {
- ac--, av++;
- if (!ac)
- goto USAGE;
- sLeftAlign = atoi(*av);
- sLeftAlign -= sLeftAlign % sTabSkip;
- }
- else if (!strcmp(*av, "-r"))
- {
- ac--, av++;
- if (!ac)
- goto USAGE;
- sRightAlign = atoi(*av);
- }
- else if (!strcmp(*av, "-R"))
- sRecursive = TRUE;
- else
- {
- USAGE:
- fprintf(stderr, "# Usage: Deps [-R] [-t tabskip] [-l leftalign] "
- "[-r rightalign] [-i includedir] filename ...\n");
- exit(1);
- }
- }
- else
- {
- if ((fp = fopen(*av, "r")) == NULL)
- {
- perror("# Deps: fopen");
- exit(1);
- }
- DoFile(*av, fp, TRUE);
- fclose(fp);
- }
-
- SpinCursor(32);
- }
-
- return(0);
- }
-
- //
- // DoFile()
- //
- // Process one file
- //
-
- static void
- DoFile(char *name, FILE *fp, int first)
- {
- char line[MAX_LINE], ifile[MAX_LINE];
- int k, lineNumber, top = first;
- static int linePosn;
- FILE *fp2;
-
- // Look for include directives including double-quoted filenames
-
- for (lineNumber = 1; fgets(line, MAX_LINE, fp); lineNumber++)
- if (sscanf(line, "#include \"%s", ifile) == 1)
- {
-
- // First time?
-
- if (first)
- {
- printf("%s.o", name); linePosn = strlen(name) + 2;
- Align(&linePosn, sLeftAlign);
- putchar(DEPEND_CHAR);
- first = FALSE;
- }
-
- // Nuke trailing quote
-
- ifile[strlen(ifile) - 1] = '\0';
-
- // Find included file, print it out
-
- for (k = 0; k < sNumDirs; k++)
- if (fp2 = FindFile(idirs[k], ifile))
- {
- if (linePosn != sLeftAlign && linePosn + 1
- + strlen(idirs[k]) + strlen(ifile) > sRightAlign)
- {
- printf(" %c\n", CONTINUE_CHAR);
- linePosn = 0;
- Align(&linePosn, sLeftAlign);
- putchar(' ');
- linePosn++;
- }
- printf(" %s%s", idirs[k], ifile);
- linePosn += 1 + strlen(idirs[k]) + strlen(ifile);
- if (sRecursive)
- DoFile(NULL, fp2, FALSE);
- fclose(fp2);
- break;
- }
-
- // File not found
-
- if (k == sNumDirs)
- {
- fprintf(stderr, "File %s; Line %d ", name, lineNumber);
- fprintf(stderr, "# Deps: warning: file \"%s\" not located\n", ifile);
- }
- }
-
- // Done with all depedencies? Then print a newline and flush stdout
-
- if (top)
- {
- putchar('\n');
- fflush(stdout);
- }
- }
-
- //
- // FindFile()
- //
-
- static FILE *
- FindFile(char *dir, char *file)
- {
- char name[MAX_LINE];
-
- strcpy(name + strlen(strcpy(name, dir)), file);
- return(fopen(name, "r"));
- }
-
- //
- // Align
- //
-
- static void
- Align(int *linePosn, int where)
- {
- if (*linePosn > where)
- {
- putchar(' ');
- *linePosn += 1;
- return;
- }
- while (*linePosn < where)
- {
- putchar('\t');
- *linePosn += sTabSkip;
- *linePosn -= *linePosn % sTabSkip;
- }
- }