home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / make / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-02  |  4.2 KB  |  223 lines

  1. /*
  2.  *    make [-f makefile] [-ins] [target(s) ...]
  3.  *
  4.  *    (Better than EON mk but not quite as good as UNIX make)
  5.  *
  6.  *    -f makefile name
  7.  *    -i ignore exit status
  8.  *    -n Pretend to make
  9.  *    -p Print all macros & targets
  10.  *    -q Question up-to-dateness of target.  Return exit status 1 if not
  11.  *    -r Don't not use inbuilt rules
  12.  *    -s Make silently
  13.  *    -t Touch files instead of making them
  14.  *    -m Change memory requirements (EON only)
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include "h.h"
  19.  
  20. #ifdef unix
  21. #include <sys/errno.h>
  22. #endif
  23. #ifdef eon
  24. #include <sys/err.h>
  25. #endif
  26. #ifdef os9
  27. #include <errno.h>
  28. #endif
  29.  
  30.  
  31. #ifdef eon
  32. #define MEMSPACE    (16384)
  33. #endif
  34.  
  35.  
  36. char *            myname;
  37. char *            makefile;    /*  The make file  */
  38. #ifdef eon
  39. unsigned        memspace = MEMSPACE;
  40. #endif
  41.  
  42. FILE *            ifd;        /*  Input file desciptor  */
  43. bool            domake = TRUE;    /*  Go through the motions option  */
  44. bool            ignore = FALSE;    /*  Ignore exit status option  */
  45. bool            silent = FALSE;    /*  Silent option  */
  46. bool            print = FALSE;    /*  Print debuging information  */
  47. bool            rules = TRUE;    /*  Use inbuilt rules  */
  48. bool            dotouch = FALSE;/*  Touch files instead of making  */
  49. bool            quest = FALSE;    /*  Question up-to-dateness of file  */
  50.  
  51.  
  52. void
  53. main(argc, argv)
  54. int            argc;
  55. char **            argv;
  56. {
  57.     register char *        p;        /*  For argument processing  */
  58.     int            estat = 0;    /*  For question  */
  59.     register struct name *    np;
  60.  
  61.  
  62.     myname = (argc-- < 1) ? "make" : *argv++;
  63.  
  64.     while ((argc > 0) && (**argv == '-'))
  65.     {
  66.         argc--;        /*  One less to process  */
  67.         p = *argv++;    /*  Now processing this one  */
  68.  
  69.         while (*++p != '\0')
  70.         {
  71.             switch(*p)
  72.             {
  73.             case 'f':    /*  Alternate file name  */
  74.                 if (*++p == '\0')
  75.                 {
  76.                     if (argc-- <= 0)
  77.                         usage();
  78.                     p = *argv++;
  79.                 }
  80.                 makefile = p;
  81.                 goto end_of_args;
  82. #ifdef eon
  83.             case 'm':    /*  Change space requirements  */
  84.                 if (*++p == '\0')
  85.                 {
  86.                     if (argc-- <= 0)
  87.                         usage();
  88.                     p = *argv++;
  89.                 }
  90.                 memspace = atoi(p);
  91.                 goto end_of_args;
  92. #endif
  93.             case 'n':    /*  Pretend mode  */
  94.                 domake = FALSE;
  95.                 break;
  96.             case 'i':    /*  Ignore fault mode  */
  97.                 ignore = TRUE;
  98.                 break;
  99.             case 's':    /*  Silent about commands  */
  100.                 silent = TRUE;
  101.                 break;
  102.             case 'p':
  103.                 print = TRUE;
  104.                 break;
  105.             case 'r':
  106.                 rules = FALSE;
  107.                 break;
  108.             case 't':
  109.                 dotouch = TRUE;
  110.                 break;
  111.             case 'q':
  112.                 quest = TRUE;
  113.                 break;
  114.             default:    /*  Wrong option  */
  115.                 usage();
  116.             }
  117.         }
  118.     end_of_args:;
  119.     }
  120.  
  121. #ifdef eon
  122.     if (initalloc(memspace) == 0xffff)  /*  Must get memory for alloc  */
  123.         fatal("Cannot initalloc memory");
  124. #endif
  125.  
  126.     if (strcmp(makefile, "-") == 0)    /*  Can use stdin as makefile  */
  127.         ifd = stdin;
  128.     else
  129.         if (!makefile)        /*  If no file, then use default */
  130.         {
  131.             if ((ifd = fopen(DEFN1, "r")) == (FILE *)0)
  132. #ifdef eon
  133.                 if (errno != ER_NOTF)
  134.                     fatal("Can't open %s; error %02x", DEFN1, errno);
  135. #endif
  136. #ifdef unix
  137.                 if (errno != ENOENT)
  138.                     fatal("Can't open %s; error %02x", DEFN1, errno);
  139. #endif
  140. #ifndef os9
  141.             if ((ifd == (FILE *)0)
  142.                   && ((ifd = fopen(DEFN2, "r")) == (FILE *)0))
  143.                 fatal("Can't open %s", DEFN2);
  144. #else
  145.                 fatal("Can't open %s", DEFN1);
  146. #endif
  147.         }
  148.         else
  149.             if ((ifd = fopen(makefile, "r")) == (FILE *)0)
  150.                 fatal("Can't open %s", makefile);
  151.  
  152.     makerules();
  153.  
  154.     setmacro("$", "$");
  155.  
  156.     while (argc && (p = index(*argv, '=')))
  157.     {
  158.         char        c;
  159.  
  160.         c = *p;
  161.         *p = '\0';
  162.         setmacro(*argv, p+1);
  163.         *p = c;
  164.  
  165.         argv++;
  166.         argc--;
  167.     }
  168.  
  169.     input(ifd);    /*  Input all the gunga  */
  170.     fclose(ifd);    /*  Finished with makefile  */
  171.     lineno = 0;    /*  Any calls to error now print no line number */
  172.  
  173.     if (print)
  174.         prt();    /*  Print out structures  */
  175.  
  176.     np = newname(".SILENT");
  177.     if (np->n_flag & N_TARG)
  178.         silent = TRUE;
  179.  
  180.     np = newname(".IGNORE");
  181.     if (np->n_flag & N_TARG)
  182.         ignore = TRUE;
  183.  
  184.     precious();
  185.  
  186.     if (!firstname)
  187.         fatal("No targets defined");
  188.  
  189.     circh();    /*  Check circles in target definitions  */
  190.  
  191.     if (!argc)
  192.         estat = make(firstname, 0);
  193.     else while (argc--)
  194.     {
  195.         if (!print && !silent && strcmp(*argv, "love") == 0)
  196.             printf("Not war!\n");
  197.         estat |= make(newname(*argv++), 0);
  198.     }
  199.  
  200.     if (quest)
  201.         exit(estat);
  202.     else
  203.         exit(0);
  204. }
  205.  
  206.  
  207. usage()
  208. {
  209.     fprintf(stderr, "Usage: %s [-f makefile] [-inpqrst] [macro=val ...] [target(s) ...]\n", myname);
  210.     exit(1);
  211. }
  212.  
  213.  
  214. void
  215. fatal(msg, a1, a2, a3, a4, a5, a6)
  216. char    *msg;
  217. {
  218.     fprintf(stderr, "%s: ", myname);
  219.     fprintf(stderr, msg, a1, a2, a3, a4, a5, a6);
  220.     fputc('\n', stderr);
  221.     exit(1);
  222. }
  223.