home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume38 / fixdepend / part01 / fixdepend.c next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  5.0 KB  |  181 lines

  1. /*
  2.  *  fixdepend - Fixup the dpendency list generated by makedepend to properly
  3.  *              reference library members.
  4.  *
  5.  *  This program and source is copyright 1993 by Chris Olson, All Rights
  6.  *  Reserved.  Permission is granted for personal use, and it may be
  7.  *  distributed by any means as long as no more than a fair duplication
  8.  *  and media costs are charged.  If this program is going to be sold as
  9.  *  part of a larger product, please contact the author at the following
  10.  *  electronic address:
  11.  *
  12.  *    chris@Sterling.COM
  13.  *
  14.  *  fixdepend -l library [-f Makefile] [-s "marker-line"] object ...
  15.  *
  16.  *  Chris Olson
  17.  *  v1.0
  18.  *  06/10/93
  19.  */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <sys/param.h>
  23.  
  24. static char *File = "Makefile";
  25. static char *Lib = "";
  26. static char *Start = "# DO NOT DELETE THIS LINE -- make depend depends on it.";
  27.  
  28. /*
  29.  *  Tear apart the command line and fill in all of the variables.  Used instead
  30.  *  of 'getarg' because I could not guarentee a 'getarg' on all systems this
  31.  *  program might be ported to.
  32.  */
  33. static int parse_args (argc, argv)
  34.    int argc;    /* Number of arguments on the command line. */
  35.    char **argv;    /* The argument list */
  36. {
  37.    int i;    /* Simple loop counter. */
  38.  
  39.    for (i = 1; i < argc; i++)
  40.       if (argv[i][0] != '-')
  41.          return i;
  42.       else
  43.          switch (argv[i][1]) {
  44.          case 'f':
  45.             if (argv[i][2] == '\0')
  46.                File = argv[++i];
  47.             else
  48.                File = &argv[i][2];
  49.             break;
  50.          case 'l':
  51.             if (argv[i][2] == '\0')
  52.                Lib = argv[++i];
  53.             else
  54.                Lib = &argv[i][2];
  55.             break;
  56.          case 's':
  57.             if (argv[i][2] == '\0')
  58.                Start = argv[++i];
  59.             else
  60.                Start = &argv[i][2];
  61.             break;
  62.          default:
  63.             fprintf(stderr, "Bad option '%s'.\n", argv[i]);
  64.             exit(-1);
  65.          }
  66.    return i;
  67. } /* parse_args */
  68.  
  69. /*
  70.  *  Rename a file.  Does NOT work across volumes, but does not need to for this
  71.  *  program.
  72.  */
  73. static int mv(init, new)
  74.    char *init;    /* Initial name of the file. */
  75.    char *new;    /* New name for the file.    */
  76. {
  77.    unlink(new);    /* Don't care if this fails... */
  78.    if (link(init, new))
  79.       return -1;
  80.    return unlink(init);
  81. } /* mv */
  82.  
  83. /*
  84.  *  Main part of the code.
  85.  */
  86. int main (argc, argv)
  87.    int argc;    /* Number of command line arguments. */
  88.    char **argv;    /* Command line arguments.           */
  89. {
  90.    FILE *fp_in;            /* Stream reading from (Makefile.bak).       */
  91.    FILE *fp_out;        /* Stream writing too (new Makefile)         */
  92.    char *colon;            /* Position of the colon in the string.      */
  93.    char line[BUFSIZ];        /* IO buffer.                                */
  94.    char save_file[MAXPATHLEN];    /* Orginal filename.                         */
  95.    int i;            /* Loop counter.                             */
  96.    int start;            /* Where in the argument list the files are. */
  97.  
  98.    /*
  99.     *  Parse the command line.
  100.     */
  101.    start = parse_args(argc, argv);
  102.    if (!strlen(Lib)) {
  103.       fprintf(stderr, "You must specify a library name!\n");
  104.       exit(-1);
  105.    }
  106.  
  107.    /*
  108.     *  Make a backup copy of the makefile, and open up the files.
  109.     */
  110.    sprintf(save_file, "%s.bak", File);
  111.    if (mv(File, save_file) == -1) {
  112.       fprintf(stderr, "Unable to make backup file '%s'\n", save_file);
  113.       exit(-1);
  114.    }
  115.    if ((fp_out = fopen(File, "w")) == NULL) {
  116.       fprintf(stderr, "Unable to open new '%s'.\n", File);
  117.       if (mv(save_file, File))
  118.          fprintf(stderr, "Unable to recover '%s', origional in '%s'.\n",
  119.                  File, save_file);
  120.       exit(-1);
  121.    }
  122.    if ((fp_in = fopen(save_file, "r")) == NULL) {
  123.       fprintf(stderr, "Can't open '%s' for reading.\n", File);
  124.       if (mv(save_file, File))
  125.          fprintf(stderr, "Unable to recover '%s', origional in '%s'.\n",
  126.                  File, save_file);
  127.       exit(-1);
  128.    }
  129.  
  130.    /*
  131.     *  Copy the file unchanged until we find the line of code where
  132.     *  makedepend left the do-not-edit-beyond-this-point comment.
  133.     */
  134.    while (fgets(line, BUFSIZ, fp_in)) {
  135.       fputs(line, fp_out);
  136.       if (!strncmp(Start, line, strlen(Start)))
  137.          break;
  138.    }
  139.  
  140.    /*
  141.     *  Now adjust the lines for the objects requested.
  142.     */
  143.    while (fgets(line, BUFSIZ, fp_in)) {
  144.       if ((colon = strchr(line, ':')) == NULL) {
  145.      /*
  146.       *  Not a dependency intro, just copy it out.
  147.       */
  148.          fputs(line, fp_out);
  149.          continue;
  150.       }
  151.  
  152.       /*
  153.        * Terminate the item, and mark the rest of line.
  154.        */
  155.       *(colon++) = '\0';
  156.       for (i = start; i < argc; i++)
  157.          if (!strcmp(line, argv[i])) {
  158.         /*
  159.          *  Adjust the reference to refer to a library.
  160.          */
  161.             *strchr(line, '.') = '\0';
  162.         fputs(Lib, fp_out);
  163.             fprintf(fp_out, "(%s.o):", Lib, line);
  164.         fputs(colon, fp_out);
  165.             break;
  166.          }
  167.       if (i == argc)
  168.      /*
  169.       *  Not one of the ones we where looking for, just output it.
  170.       */
  171.          fprintf(fp_out, "%s:%s", line, colon);
  172.    }
  173.  
  174.    /*
  175.     *  Close all of the files and exit...
  176.     */
  177.    fclose(fp_in);
  178.    fclose(fp_out);
  179.    exit(0);
  180. } /*  main */
  181.