home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / PAMAKE18.ZIP / MAIN.C < prev    next >
C/C++ Source or Header  |  1989-10-04  |  10KB  |  336 lines

  1. /*************************************************************************
  2. |                                                                        |
  3. |   MAIN.C                                                      04.10.89 |
  4. |   PAMAKE Utility:  main program                                        |
  5. |                                                                        |
  6. *************************************************************************/
  7.  
  8. #define VV "1.80"
  9. #define DD "04 Oct 89"
  10.  
  11. #ifdef VMS
  12. #include stdio
  13. #include "h.h"
  14. #include string
  15. #include errno
  16. #endif
  17.  
  18. #ifdef LATTICE
  19. #include <stdio.h>
  20. #include <dos.h>
  21. #include "h.h"
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #endif
  27.  
  28. #ifdef __TURBOC__
  29. #include <stdio.h>
  30. #include <dos.h>
  31. #include <io.h>
  32. #include <dir.h>
  33. #include "h.h"
  34. #include <errno.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <signal.h>
  38. #endif
  39.  
  40. #ifdef MSC
  41. #include <stdio.h>
  42. #include <dos.h>
  43. #include <io.h>
  44. #include <direct.h>
  45. #include "h.h"
  46. #include <errno.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49. #include <signal.h>
  50. #endif
  51.  
  52. char *          myname;             /* Name of this program */
  53. char *          makefile;           /* The make file  */
  54.  
  55. bool            confirm = FALSE;    /* Re-stat target after make */
  56. bool            domake = TRUE;      /* Go through the motions option  */
  57. bool            ignore = FALSE;     /* Ignore exit status option  */
  58. bool            silent = FALSE;     /* Silent option  */
  59. bool            print = FALSE;      /* Print debuging information  */
  60. bool            rules = TRUE;       /* Use inbuilt rules  */
  61. bool            dotouch = FALSE;    /* Touch files instead of making  */
  62. bool            quest = FALSE;      /* Question up-to-dateness of file  */
  63. bool            display = FALSE;    /* Display times */
  64. uchar           macrotype = '\0';   /* Environment, Command-line, etc */
  65.  
  66. FILE *          ifile[4] = {0};             /* input files */
  67. int             fln[4] = {0,0,0,0};         /* input file line numbers */
  68. char            fname[4][80] = {"stdin"};   /* input file names */
  69. int             nestlvl = 0;                /* nesting level */
  70. char            ifmessage = '#';            /* 'if' statement lead-in */
  71. unsigned char   pamakeos2 = 0;       /* 0 = DOS (or VMS), 1 = OS2 */
  72.  
  73. #ifdef PAMOS2
  74.     extern far pascal DosGetMachineMode(unsigned char far *);
  75. #define OMITBRK
  76. #endif
  77.  
  78. #ifndef VMS
  79. #ifndef OMITBRK
  80. static union REGS regs;         /* registers for BIOS calls */
  81. static unsigned int svbreak;    /* save break state */
  82.  
  83. void cleanbrk(void)
  84. {
  85.     regs.x.ax = 0x3301;         /* set break state */
  86.     regs.x.dx = svbreak;        /* save state */
  87.     intdos(®s,®s);        /* do the dos call */
  88. }
  89. #endif
  90. #endif
  91.  
  92. void
  93. usage(void)
  94. {
  95.     fprintf(stderr, "Usage: %s [-f makefile] [-deinpqrst] [macro=val ...] [target(s) ...]\n", myname);
  96.     exit(PXERROR);
  97. }
  98.  
  99. #ifndef __TSC__
  100. void
  101. fatal(msg, a1, a2, a3)
  102. char *      msg;
  103. {
  104.     fprintf(stderr, "%s: ", myname);
  105.     fprintf(stderr, msg, a1, a2, a3);
  106.     fputc('\n', stderr);
  107.     exit(PXERROR);
  108. }
  109. #endif
  110.  
  111. /* local strstr, some compiler libraries dont have it */
  112.  
  113. char *
  114. pstrstr(char * source, char * target)
  115. {
  116.     register char *         s1;
  117.     register char *         t;
  118.  
  119.     for ( ; *source ; ++source)         /* look along source */
  120.     {
  121.         for (s1 = source, t = target; *s1 == *t; ++s1)
  122.         {
  123.             if (*++t == '\0') return source;    /* match if hit end of targ */
  124.         }
  125.     }
  126.     return (char *)0;
  127. }
  128.  
  129. #ifndef VMS
  130. void
  131. #endif
  132. main(argc, argv, envp)
  133. char * *        argv;
  134. int             argc;
  135. char * *        envp;
  136. {
  137.     register char *         p;          /*  For argument processing  */
  138.     int                     estat = 0;  /*  For question  */
  139.     register struct name *  np;
  140.     int                     i;
  141.     char *                  makeflags;
  142.     int                     egiven = 0;
  143.  
  144.     myname = "pamake";
  145.  
  146.     macrotype = M_ENV;          /* doing enviroment macros */ 
  147.     while(*envp) 
  148.     {
  149.         p = strchr(*envp,'=');  /* find = in environment string */
  150.         if (p == NULL)          /* no '=' in environment string */
  151.         {                       /* never happens with COMMAND.COM */
  152.             envp++;             /* but needed for MKS Korn Shell */
  153.             continue;
  154.         }   
  155.         i = (int)(p - *envp);
  156.         strncpy(str1,*envp++,i);
  157.         str1[i] = '\0';
  158.         setmacro(str1,p+1);
  159.     }
  160.  
  161. #ifdef PAMOS2
  162.     DosGetMachineMode((uchar far *)&pamakeos2);
  163.     if (pamakeos2) setmacro("PAMAKE_OS","OS2");
  164.     else setmacro("PAMAKE_OS","DOS");
  165. #endif
  166. #ifdef PAMDOS
  167.     setmacro("PAMAKE_OS","DOS");
  168. #endif
  169. #ifdef VMS
  170.     setmacro("PAMAKE_OS","VMS");
  171. #endif    
  172.  
  173.     macrotype = '\0';
  174.  
  175.     makeflags = getenv("MAKEFLAGS");
  176.     if (makeflags) argv[0] = makeflags;
  177.     else argv[0] = "";
  178.  
  179.     while (argc > 0) 
  180.     {
  181.         if (**argv == '\0')     /* empty makeflags */
  182.         {
  183.             argc--;             /*  One less to process  */
  184.             argv++;  
  185.         } 
  186.         else if (**argv == '-')
  187.         {
  188.             argc--;             /*  One less to process  */
  189.             p = *argv++;        /*  Now processing this one  */
  190.  
  191.             while (*++p != '\0')
  192.             {
  193.                 switch(*p)
  194.                 {
  195.                 case 'c':       /*  confirm timestamp after make */
  196.                     confirm++;
  197.                     break;
  198.                 case 'd':       /*  debug with timestamps */
  199.                     display++;
  200.                     break;
  201.                 case 'e':       /*  Enviroment macros prevail */
  202.                     egiven++;
  203.                     break;
  204.                 case 'f':       /*  Alternate file name  */
  205.                     if (*(argv-1) == makeflags) break;
  206.                     if (argc-- <= 0) usage();
  207.                     makefile = *argv++;
  208.                     break;
  209.                 case 'n':       /*  Pretend mode  */
  210.                     domake = FALSE;
  211.                     break;
  212.                 case 'i':       /*  Ignore fault mode  */
  213.                     ignore = TRUE;
  214.                     break;
  215.                 case 's':       /*  Silent about commands  */
  216.                     silent = TRUE;
  217.                     break;
  218.                 case 'p':
  219.                     if (*(argv-1) != makeflags) print = TRUE;
  220.                     break;
  221.                 case 'r':
  222.                     rules = FALSE;
  223.                     break;
  224.                 case 't':
  225.                     dotouch = TRUE;
  226.                     break;
  227.                 case 'q':
  228.                     quest = TRUE;
  229.                     break;
  230.                 case 'v':
  231.                     fprintf(stderr, "\nPAMAKE Version: %s Date: %s\n\n", VV, DD);
  232.                     /* drop through */
  233.                 default:        /*  Wrong option  */
  234.                     usage();
  235.                 }
  236.             }
  237.         }
  238.         else if ((p = strchr(*argv, '=')) != NULL) 
  239.         {
  240.             char            c;
  241.  
  242.             c = *p;
  243.             *p = '\0';
  244.             macrotype = M_PERM;     /* doing command-line macros */ 
  245.             setmacro(*argv, p+1);
  246.             macrotype = '\0';
  247.             *p = c;
  248.  
  249.             argv++;
  250.             argc--;
  251.         }
  252.         else break;
  253.     }
  254.  
  255.     if ((makefile != NULL) && (strcmp(makefile, "-") == 0))  
  256.         ifile[0] = stdin;
  257.     else if (!makefile)             /*  If no file, then use default */
  258.     {
  259.         if ((ifile[0] = fopen(DEFN1, "r")) == (FILE *)0)
  260.         {
  261.             fatal("Unable to open file %s", DEFN1);
  262.         }
  263.         strncpy(fname[0],DEFN1,80);
  264.     }
  265.     else
  266.     {
  267.         if ((ifile[0] = fopen(makefile, "r")) == (FILE *)0)
  268.             fatal("Unable to open file %s", makefile);
  269.         strncpy(fname[0],makefile,80);
  270.     }
  271.  
  272.     if (egiven) markmacros();       /* make env macros perm */
  273.     if (rules) makerules();         /* use builtin rules */
  274. #ifndef VMS
  275.     setmacro("\\","\\");
  276.     setmacro("HOME",getcwd((char *)0,64));
  277.     if (pamakeos2 == 0)
  278.     {
  279. #ifndef OMITBRK
  280.         regs.x.ax = 0x3300;         /* get break state */
  281.         intdos(®s,®s);        /* do the dos call */
  282.         svbreak = regs.x.dx;        /* save state */
  283.         regs.x.ax = 0x3301;         /* set break state */
  284.         regs.x.dx = 0x0001;         /* turn it on */
  285.         intdos(®s,®s);        /* do the dos call */
  286. #endif
  287.     }
  288. #ifndef OMITBRK
  289.     atexit(cleanbrk);
  290. #endif
  291. #endif
  292. #ifdef DOLLAR
  293.     setmacro("$", "\244");          /* special high bit set */
  294. #else
  295.     setmacro("$", "$");
  296. #endif
  297.     setmacro("MAKE",myname);
  298.     setmacro("-", LIFNAME);
  299.  
  300.     input();            /*  Input all the gunga  */
  301.     lineno = 0;         /*  Any calls to error now print no line number */
  302.     ifmessage = '%';    /*  if statement lead-in now '%' */
  303.  
  304.     np = newname(".SILENT");
  305.     if (np->n_flag & N_TARG) silent = TRUE;
  306.  
  307.     np = newname(".IGNORE");
  308.     if (np->n_flag & N_TARG) ignore = TRUE;
  309.  
  310.     p = str1;
  311.     *p++ = '-';
  312.     if (egiven) *p++ = 'e';
  313.     if (ignore) *p++ = 'i';
  314.     if (!domake) *p++ = 'n';
  315.     if (quest) *p++ = 'q';
  316.     if (!rules) *p++ = 'r';
  317.     if (silent) *p++ = 's';
  318.     if (dotouch) *p++ = 't';
  319.     if (p == (str1+1)) p = str1;
  320.     *p = '\0';
  321.     setmacro("+MAKEFLAGS",str1);
  322.  
  323.     if (print) prt();       /*  Print out structures  */
  324.     precious();
  325.     if (!domake) silent = FALSE;
  326.     if (!firstname) fatal("No targets found to build");
  327.     circh();                /*  Check circles in target definitions  */
  328.  
  329.     if (!argc) estat = make(firstname, 0);
  330.     else while (argc--) estat |= make(newname(*argv++), 0);
  331.  
  332.     if (quest) exit((estat)?PXERROR:PXNORMAL);
  333.     else exit(PXNORMAL);
  334. }
  335.  
  336.