home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38A.ZIP / MAKE.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  29KB  |  1,026 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/make.c,v 1.1 1992/01/24 03:26:51 dvadura Exp $
  2. -- SYNOPSIS -- perform the update of all outdated targets.
  3. -- 
  4. -- DESCRIPTION
  5. --    This is where we traverse the make graph looking for targets that
  6. --    are out of date, and we try to infer how to make them if we can.
  7. --    The usual make macros are understood, as well as some new ones:
  8. --
  9. --        $$    - expands to $
  10. --        $@      - full target name
  11. --        $*      - target name with no suffix, same as $(@:db)
  12. --              or, the value of % in % meta rule recipes
  13. --        $?      - list of out of date prerequisites
  14. --        $<      - all prerequisites associated with rules line
  15. --        $&    - all prerequisites associated with target
  16. --        $>      - library name for target (if any)
  17. --        $^    - out of date prerequisites taken from value of $<
  18. --        {{    - expands to {
  19. --        }}    - expands to }
  20. --        \#    - expands to #
  21. -- 
  22. -- AUTHOR
  23. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  24. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  25. --
  26. -- COPYRIGHT
  27. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  28. -- 
  29. --      This program is free software; you can redistribute it and/or
  30. --      modify it under the terms of the GNU General Public License
  31. --      (version 1), as published by the Free Software Foundation, and
  32. --      found in the file 'LICENSE' included with this distribution.
  33. -- 
  34. --      This program is distributed in the hope that it will be useful,
  35. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  36. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37. --      GNU General Public License for more details.
  38. -- 
  39. --      You should have received a copy of the GNU General Public License
  40. --      along with this program;  if not, write to the Free Software
  41. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. --
  43. -- LOG
  44. --     $Log: make.c,v $
  45.  * Revision 1.1  1992/01/24  03:26:51  dvadura
  46.  * dmake Version 3.8, Initial revision
  47.  *
  48. */
  49.  
  50. #include "extern.h"
  51.  
  52. static    void    _drop_mac ANSI((HASHPTR));
  53. static    void    _set_recipe ANSI((char*, int));
  54. static    void    _set_tmd ANSI(());
  55. static    void    _append_file ANSI((STRINGPTR, FILE*, char*, int));
  56. static  LINKPTR _dup_prq ANSI((LINKPTR));
  57. static  char*   _prefix ANSI((char *, char *));
  58. static  char*   _pool_lookup ANSI((char *));
  59.  
  60. #define RP_GPPROLOG    0
  61. #define RP_RECIPE    1
  62. #define RP_GPEPILOG    2
  63. #define NUM_RECIPES    3
  64.  
  65. static STRINGPTR _recipes[ NUM_RECIPES ];
  66.  
  67.  
  68. PUBLIC int
  69. Make_targets()/*
  70. ================
  71.    Actually go and make the targets on the target list */
  72. {
  73.    LINKPTR lp;
  74.    int     done = 0;
  75.  
  76.    DB_ENTER( "Make_targets" );
  77.  
  78.    Read_state();
  79.    _set_recipe( ".GROUPPROLOG", RP_GPPROLOG );
  80.    _set_recipe( ".GROUPEPILOG", RP_GPEPILOG );
  81.  
  82.    Root->ce_flag  |= F_RULES|F_TARGET|F_STAT;
  83.    Root->ce_attr  |= A_NOSTATE;
  84.    Root->ce_time   = Do_time();
  85.    TALLOC( Root->ce_recipe, 1, STRING );
  86.    Root->ce_recipe->st_string = "";
  87.  
  88.    for( lp = Root->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
  89.       lp->cl_prq->ce_attr |= A_ROOT;
  90.  
  91.    while( !done ) {
  92.       int rval;
  93.  
  94.       if( (rval = Make(Root, NIL(CELL))) == -1 )
  95.      DB_RETURN(1);
  96.       else
  97.      done = Root->ce_flag & F_MADE;
  98.  
  99.       if( !rval && !done ) Wait_for_child( FALSE, -1 );
  100.    }
  101.  
  102.    for( lp = Root->ce_prq; lp != NIL(LINK); lp = lp->cl_next ) {
  103.       CELLPTR root = lp->cl_prq;
  104.       if( !(root->ce_attr & A_UPDATED) )
  105.      printf( "`%s' is up to date\n", root->CE_NAME );
  106.    }
  107.  
  108.    DB_RETURN( 0 );
  109. }
  110.  
  111.  
  112.  
  113. int
  114. Make( cp, setdirroot )/*
  115. ========================  Make a specified target */
  116. CELLPTR cp;
  117. CELLPTR setdirroot;
  118. {
  119.    register LINKPTR dp;
  120.    register CELLPTR tcp;
  121.    CELLPTR          nsetdirroot;
  122.    char            *name, *lib;
  123.    HASHPTR        m_at, m_q, m_b, m_g, m_l, m_bb, m_up;
  124.    char             *all    = NIL(char);
  125.    char             *inf    = NIL(char);
  126.    char             *outall = NIL(char);
  127.    char             *imm    = NIL(char);
  128.    int              rval    = 0;
  129.    int            push    = 0;
  130.    int             made    = F_MADE;
  131.    int            ignore;
  132.    time_t           otime   = (time_t) 1L;
  133.    time_t        ttime   = (time_t) 1L;
  134.    int            mark_made = FALSE;
  135.  
  136.    DB_ENTER( "Make" );
  137.    DB_PRINT( "mem", ("%s:-> mem %ld", cp->CE_NAME, (long) coreleft()) );
  138.  
  139.    m_q = m_b = m_g = m_l = m_bb = m_up = m_at = NIL(HASH);
  140.  
  141.    /* If we are supposed to change directories for this target then do so.
  142.     * If we do change dir, then modify the setdirroot variable to reflect
  143.     * that fact for all of the prerequisites that we will be making. */
  144.  
  145.    nsetdirroot = setdirroot;
  146.    ignore = (((cp->ce_attr|Glob_attr)&A_IGNORE) != 0);
  147.  
  148.    if( cp->ce_attr & A_SETDIR ) {
  149.       /* Change directory only if the previous .SETDIR is a different
  150.        * directory from the current one.  ie. all cells with the same .SETDIR
  151.        * attribute are assumed to come from the same directory. */
  152.  
  153.       if( (setdirroot == NIL(CELL) || setdirroot->ce_dir != cp->ce_dir) &&
  154.           (push = Push_dir(cp->ce_dir,cp->CE_NAME,ignore)) )
  155.      setdirroot = cp;
  156.    }
  157.  
  158.    DB_PRINT( "mem", ("%s:-A mem %ld", cp->CE_NAME, (long) coreleft()) );
  159.    if( cp->ce_recipe == NIL(STRING) ) {
  160.       char *dir = cp->ce_dir;
  161.       
  162.       if( Verbose & V_MAKE )
  163.      printf( "%s:  Infering prerequisite(s) and recipe for [%s]\n", Pname,
  164.           cp->CE_NAME );
  165.  
  166.       Infer_recipe( cp, setdirroot );
  167.  
  168.       /* See if the directory has changed, if it has then make sure we
  169.        * push it. */
  170.       if( dir != cp->ce_dir ) {
  171.      if( push ) Pop_dir(FALSE);
  172.          push = Push_dir( cp->ce_dir, cp->CE_NAME, ignore );
  173.      setdirroot = cp;
  174.       }
  175.    }
  176.  
  177.    tcp = cp;
  178.    do {
  179.       if( push ) {
  180.      if( tcp->ce_dir ) FREE( tcp->ce_dir );
  181.      tcp->ce_dir   = _pool_lookup(Pwd);
  182.      tcp->ce_attr |= A_SETDIR;
  183.       }
  184.       tcp->ce_setdir = nsetdirroot;
  185.       tcp = tcp->ce_all;
  186.    }
  187.    while( tcp != NIL(CELL) && tcp != cp );
  188.  
  189.    DB_PRINT( "mem", ("%s:-A mem %ld", cp->CE_NAME, (long) coreleft()) );
  190.    /* If we have not yet statted the target then do so. */
  191.    if( !(cp->ce_flag & F_STAT) && !(cp->ce_attr&A_PHONY) ) {
  192.       time_t itime = cp->ce_time;
  193.       Stat_target( cp, TRUE );
  194.  
  195.       if( cp->ce_time == (time_t)0L ) {
  196.          if( cp->ce_flag & F_INFER )
  197.         cp->ce_time = itime;
  198.       }
  199.       else
  200.          cp->ce_attr |= A_PRECIOUS;  /* File exists so don't remove it later. */
  201.  
  202.       if( Verbose & V_MAKE )
  203.      printf("%s:  Time stamp of [%s] is %ld\n",Pname,cp->CE_NAME,
  204.             cp->ce_time);
  205.    }
  206.  
  207.    DB_PRINT( "make", ("(%s, %ld, 0x%08x, 0x%04x)", cp->CE_NAME,
  208.             cp->ce_time, cp->ce_attr, cp->ce_flag) );
  209.  
  210.    if( !(cp->ce_flag & F_TARGET) && (cp->ce_time == (time_t) 0L) )
  211.       if( Makemkf ) {
  212.      rval = -1;
  213.      goto stop_making_it;
  214.       }
  215.       else if(cp->ce_prq != NIL(LINK)||(Augmake && (cp->ce_flag&F_EXPLICIT)))
  216.      /* Assume an empty recipe for a target that we have run inference on
  217.       * but do not have a set of rules for but for which we have inferred
  218.       * a list of prerequisites. */
  219.      cp->ce_flag |= F_RULES;
  220.       else
  221.      Fatal( "`%s' not found, and can't be made", cp->CE_NAME );
  222.  
  223.    DB_PRINT( "mem", ("%s:-A mem %ld", cp->CE_NAME, (long) coreleft()) );
  224.  
  225.    /* set value of $* if we have not infered a recipe, in this case $* is
  226.     * the same as $(@:db), this allows us to be compatible with BSD make */
  227.    if( cp->ce_per == NIL(char) ) cp->ce_per = "$(@:db)";
  228.  
  229.    for( dp = cp->ce_prq; dp != NIL(LINK); dp = dp->cl_next ) {
  230.       int seq;
  231.  
  232.       /* Make the prerequisite, note that if the current target has the
  233.        * .LIBRARY attribute set we pass on to the prerequisite the .LIBRARYM
  234.        * attribute and pass on the name of the current target as the library
  235.        * name, and we take it away when we are done.  */
  236.  
  237.       tcp = dp->cl_prq;
  238.       seq = (((cp->ce_attr | Glob_attr) & A_SEQ) != 0);
  239.  
  240.       if( tcp->ce_flag & F_VISITED )
  241.      if( _explode_graph(tcp, dp, setdirroot) == 0 ) {
  242.         /* didn't blow it up so see if we need to wait for it. */
  243.         if( tcp->ce_flag & F_MADE ) {
  244.            if( tcp->ce_time > ttime ) ttime = tcp->ce_time;
  245.            continue;
  246.         }
  247.         else
  248.            goto stop_making_it;
  249.      }
  250.      else
  251.         tcp = dp->cl_prq;
  252.  
  253.       if( seq && !made ) goto stop_making_it;
  254.  
  255.       if( strchr(tcp->CE_NAME, '$') ) {
  256.      /* Make this prerequisite link point at the real prerequisite we
  257.       * are after, ie figure out what the dynamic one is and point at it. */
  258.  
  259.      m_at = Def_macro( "@", cp->ce_fname, M_MULTI );
  260.      name = Expand( tcp->CE_NAME );
  261.      if( strcmp(name,cp->CE_NAME) == 0 )
  262.         Fatal( "Circular dynamic dependency generated '%s'", name );
  263.  
  264.      tcp = dp->cl_prq = Def_cell( name );
  265.      FREE( name );
  266.       }
  267.  
  268.       if( cp->ce_attr & A_LIBRARY ) {
  269.          tcp->ce_attr |= A_LIBRARYM;
  270.      tcp->ce_lib   = cp->ce_fname;
  271.       }
  272.  
  273.       if( (tcp->ce_flag & (F_INFER|F_STAT))==F_INFER && cp->ce_time >= ttime )
  274.      tcp->ce_time = cp->ce_time;
  275.  
  276.       /* Propagate parents F_REMOVE and F_INFER attribute to children.
  277.        * Make certain you do this AFTER propagating the time, since the
  278.        * time propagation test above uses the F_INFER flag to decide if
  279.        * it should do so. */
  280.       tcp->ce_flag |= cp->ce_flag & (F_REMOVE|F_INFER);
  281.  
  282.       rval |= Make(tcp, setdirroot);
  283.  
  284.       if( cp->ce_attr & A_LIBRARY )
  285.          tcp->ce_attr ^= A_LIBRARYM;
  286.  
  287.       if( rval == -1 || (seq && (rval==1)) )
  288.      goto stop_making_it;
  289.  
  290.       if( tcp->ce_time > ttime ) ttime = tcp->ce_time;
  291.       made &= tcp->ce_flag & F_MADE;
  292.    }
  293.  
  294.  
  295.    /* Do the loop again.  We are most definitely going to make the current
  296.     * cell now.  NOTE:  doing this loop here also results in a reduction
  297.     * in peak memory usage by the algorithm. */
  298.  
  299.    for( dp = cp->ce_prq; dp != NIL(LINK); dp = dp->cl_next ) {
  300.       int  tgflg;
  301.       tcp  = dp->cl_prq;
  302.       name = tcp->ce_fname;
  303.  
  304.       /* make certain that all prerequisites are made prior to advancing. */
  305.       if( !(tcp->ce_flag & F_MADE) ) goto stop_making_it;
  306.  
  307.       /* If the target is a library, then check to make certain that a member
  308.        * is newer than an object file sitting on disk.  If the disk version
  309.        * is newer then set the time stamps so that the archived member is
  310.        * replaced. */
  311.       if( cp->ce_attr & A_LIBRARY )
  312.      if( tcp->ce_time < cp->ce_time ) {
  313.         time_t mtime = Do_stat( name, tcp->ce_lib, NIL(char *) );
  314.         if( mtime < tcp->ce_time ) tcp->ce_time = cp->ce_time+1L;
  315.      }
  316.  
  317.       if( tcp->ce_time > otime ) otime = tcp->ce_time;
  318.  
  319.       all = _strapp( all, name );
  320.       if( tgflg = (dp->cl_flag & F_TARGET) ) inf = _strapp( inf, name );
  321.  
  322.       if((cp->ce_time<tcp->ce_time) || ((tcp->ce_flag & F_TARGET) && Force)) {
  323.          outall = _strapp( outall, name );
  324.          if( tgflg ) imm = _strapp( imm, name );
  325.       }
  326.    }
  327.  
  328.    DB_PRINT( "mem", ("%s:-C mem %ld", cp->CE_NAME, (long) coreleft()) );
  329.    DB_PRINT( "make", ("I make '%s' if %ld > %ld", cp->CE_NAME, otime,
  330.           cp->ce_time) );
  331.  
  332.    if( Verbose & V_MAKE && !(cp->ce_flag & F_MULTI) ) {
  333.       printf( "%s:  >>>> Making ", Pname );
  334.       if( cp->ce_count != 0 )
  335.      printf( "[%s::{%d}]\n", cp->CE_NAME, cp->ce_count );
  336.       else
  337.      printf( "[%s]\n", cp->CE_NAME );
  338.    }
  339.  
  340.    m_at = Def_macro( "@", cp->ce_fname, M_MULTI|M_EXPANDED );
  341.    m_g  = Def_macro( ">", cp->ce_lib,   M_MULTI|M_EXPANDED );
  342.    m_q  = Def_macro( "?", outall,       M_MULTI|M_EXPANDED );
  343.    m_b  = Def_macro( "<", inf,          M_MULTI|M_EXPANDED );
  344.    m_l  = Def_macro( "&", all,          M_MULTI|M_EXPANDED );
  345.    m_up = Def_macro( "^", imm,          M_MULTI|M_EXPANDED );
  346.    m_bb = Def_macro( "*", cp->ce_per,   M_MULTI );
  347.  
  348.    _recipes[ RP_RECIPE ] = cp->ce_recipe;
  349.  
  350.    /* We attempt to make the target if
  351.     *   1. it has a newer prerequisite
  352.     *   2. It is a target and Force is set
  353.     *   3. It's set of recipe lines has changed.
  354.     */
  355.    if(    Check_state(cp, _recipes, NUM_RECIPES )
  356.       || (cp->ce_time < otime)
  357.       || ((cp->ce_flag & F_TARGET) && Force)
  358.      ) {
  359.  
  360.       /* Only checking so stop as soon as we determine we will make something */
  361.       if( Check ) {
  362.      rval = -1;
  363.      goto stop_making_it;
  364.       }
  365.  
  366.       if( Verbose & V_MAKE )
  367.      printf( "%s:  Updating [%s], (%ld > %ld)\n", Pname,
  368.          cp->CE_NAME, otime, cp->ce_time );
  369.  
  370.       if( Touch ) {
  371.      name = cp->ce_fname;
  372.      lib  = cp->ce_lib;
  373.  
  374.      if( !(Glob_attr & A_SILENT) || !Trace )
  375.         if( lib == NIL(char) )
  376.            printf("touch(%s)", name );
  377.         else if( cp->ce_attr & A_SYMBOL )
  378.            printf("touch(%s((%s)))", lib, name );
  379.         else
  380.            printf("touch(%s(%s))", lib, name );
  381.  
  382.      if( !Trace )
  383.         if( Do_touch( name, lib,
  384.         (cp->ce_attr & A_SYMBOL) ? &name : NIL(char *) ) != 0 )
  385.            printf( "  not touched - non-existant" );
  386.  
  387.      printf( "\n" );
  388.      Update_time_stamp( cp );
  389.       }
  390.       else if( cp->ce_recipe != NIL(STRING) ) {
  391.      if( !(cp->ce_flag & F_SINGLE) )
  392.            rval = Exec_commands( cp );
  393.      else {
  394.         TKSTR tk;
  395.  
  396.         _drop_mac( m_q );
  397.  
  398.         if( outall && *outall ) {
  399.            SET_TOKEN( &tk, outall );
  400.  
  401.            Doing_bang = TRUE;
  402.            name = Get_token( &tk, "", FALSE );
  403.            do {
  404.           m_q->ht_value = name;
  405.  
  406.           Wait_for_completion = TRUE;    /* Reset in Exec_commands */
  407.           rval = Exec_commands( cp );
  408.           Unlink_temp_files(cp);
  409.            }
  410.            while( *(name = Get_token( &tk, "", FALSE )) != '\0' );
  411.            Doing_bang = FALSE;
  412.         }
  413.  
  414.         Update_time_stamp( cp );
  415.         m_q->ht_value = NIL(char);
  416.      }
  417.       }
  418.       else if( !(cp->ce_flag & F_RULES) && !(cp->ce_flag & F_STAT) &&
  419.            (!(cp->ce_attr & A_ROOT) || !(cp->ce_flag & F_EXPLICIT)) )
  420.      Fatal( "Don't know how to make `%s'",cp->CE_NAME );
  421.       else {
  422.          /* Empty recipe, set the flag as MADE and update the time stamp */
  423.      Update_time_stamp( cp );
  424.       }
  425.    }
  426.    else
  427.       mark_made = TRUE;
  428.  
  429.    /* Make sure everyone gets remade if Force is set */
  430.    tcp = cp;
  431.    do {
  432.       if( !(tcp->ce_flag & F_TARGET) && Force ) tcp->ce_time = Do_time();
  433.       if( mark_made ) {
  434.      tcp->ce_flag |= F_MADE;
  435.      if( tcp->ce_flag & F_MULTI ) {
  436.         LINKPTR dp;
  437.         for( dp = tcp->ce_prq; dp != NIL(LINK); dp = dp->cl_next )
  438.            tcp->ce_attr |= dp->cl_prq->ce_attr & A_UPDATED;
  439.      }
  440.       }
  441.  
  442.       tcp->ce_flag |= F_VISITED;
  443.  
  444.       /* Note:  If the prerequisite was made using a .SETDIR= attribute
  445.        *     directory then we will include the directory in the fname
  446.        *        of the target.  */
  447.       if( push ) {
  448.      char *dir   = nsetdirroot ? nsetdirroot->ce_dir : Makedir;
  449.      char *nname = Build_path(_prefix(dir,tcp->ce_dir), tcp->ce_fname);
  450.  
  451.      if( (tcp->ce_attr & A_FFNAME) && (tcp->ce_fname != NIL(char)) )
  452.         FREE( tcp->ce_fname );
  453.  
  454.      tcp->ce_fname = _strdup(nname);
  455.      tcp->ce_attr |= A_FFNAME;
  456.       }
  457.  
  458.       tcp = tcp->ce_all;
  459.    }
  460.    while( tcp != NIL(CELL) && tcp != cp );
  461.  
  462. stop_making_it:
  463.    _drop_mac( m_g  );
  464.    _drop_mac( m_q  );
  465.    _drop_mac( m_b  );
  466.    _drop_mac( m_l  );
  467.    _drop_mac( m_bb );
  468.    _drop_mac( m_up );
  469.    _drop_mac( m_at );
  470.  
  471.    while( push-- )  Pop_dir(FALSE);
  472.  
  473.    if( inf    != NIL(char) ) FREE( inf    );
  474.    if( all    != NIL(char) ) FREE( all    );
  475.    if( imm    != NIL(char) ) FREE( imm    );
  476.    if( outall != NIL(char) ) FREE( outall );
  477.  
  478.    DB_PRINT( "mem", ("%s:-< mem %ld", cp->CE_NAME, (long) coreleft()) );
  479.    DB_RETURN( rval );
  480. }
  481.  
  482.  
  483. static char *
  484. _prefix( pfx, pat )
  485. char *pfx;
  486. char *pat;
  487. {
  488.    char *opat = pat;
  489.    while( *pfx && *pat && *pfx++ == *pat++ );
  490.  
  491.    return( !*pfx ? _strspn(pat,DirBrkStr) : opat );
  492. }
  493.  
  494.  
  495. static LINKPTR
  496. _dup_prq( lp )
  497. LINKPTR lp;
  498. {
  499.    LINKPTR tlp;
  500.  
  501.    if( lp == NIL(LINK) ) return(lp);
  502.  
  503.    TALLOC(tlp, 1, LINK);
  504.    *tlp = *lp;
  505.    tlp->cl_next = _dup_prq( lp->cl_next );
  506.  
  507.    return(tlp);
  508. }
  509.  
  510.  
  511. static void
  512. _drop_mac( hp )/*
  513. ================ set a macro value to zero. */
  514. HASHPTR hp;
  515. {
  516.    if( hp && hp->ht_value != NIL(char) ) {
  517.       FREE( hp->ht_value );
  518.       hp->ht_value = NIL(char);
  519.    }
  520. }
  521.  
  522.  
  523.  
  524. int
  525. _explode_graph( cp, parent, setdirroot )/*
  526. ==========================================
  527.    Check to see if we have made the node already.  If so then don't do
  528.    it again, except if the cell's ce_setdir field is set to something other
  529.    than the value of setdirroot.  If they differ then, and we have made it
  530.    already, then make it again and set the cell's stat bit to off so that
  531.    we do the stat again.  */
  532. CELLPTR cp;
  533. LINKPTR parent;
  534. CELLPTR setdirroot;
  535. {
  536.    /* we may return if we made it already from the same setdir location,
  537.     * or if it is not a library member whose lib field is non NULL.  (if
  538.     * it is such a member then we have a line of the form:
  539.     *    lib1 lib2 .LIBRARY : member_list...
  540.     * and we have to make sure all members are up to date in both libs. */
  541.  
  542.    if( cp->ce_setdir == setdirroot &&
  543.        !((cp->ce_attr & A_LIBRARYM) && (cp->ce_lib != NIL(char))) )
  544.       return( 0 );
  545.  
  546.    /* We check to make sure that we are comming from a truly different
  547.     * directory, ie. ".SETDIR=joe : a.c b.c d.c" are all assumed to come
  548.     * from the same directory, even though setdirroot is different when
  549.     * making dependents of each of these targets. */
  550.  
  551.    if( cp->ce_setdir != NIL(CELL) &&
  552.        setdirroot != NIL(CELL) &&
  553.        cp->ce_dir &&
  554.        setdirroot->ce_dir &&
  555.        !strcmp(cp->ce_dir, setdirroot->ce_dir) )
  556.       return( 0 );
  557.  
  558.    if( Max_proc > 1 ) {
  559.       if( parent == NIL(LINK) )
  560.      Fatal( "Internal Error:  NIL parent in Make()" );
  561.  
  562.       TALLOC(parent->cl_prq, 1, CELL);
  563.       *parent->cl_prq = *cp;
  564.       cp = parent->cl_prq;
  565.       cp->ce_prq = _dup_prq(cp->ce_prq);
  566.    }
  567.    cp->ce_flag  &= ~(F_STAT|F_VISITED|F_MADE);
  568.  
  569.    /* Indicate that we exploded the graph and that the current node should
  570.     * be made. */
  571.    return(1);
  572. }
  573.  
  574.  
  575.  
  576. PUBLIC int
  577. Exec_commands( cp )/*
  578. =====================
  579.   Execute the commands one at a time that are pointed to by the rules pointer
  580.   of the target cp. If a group is indicated, then the ce_attr determines
  581.   .IGNORE and .SILENT treatment for the group.
  582.   
  583.   The function returns 0, if the command is executed and has successfully
  584.   returned, and returns 1 if the command is executing but has not yet
  585.   returned (for parallel makes).
  586.   
  587.   The F_MADE bit in the cell is guaranteed set when the command has
  588.   successfully completed.  */
  589. CELLPTR cp;
  590. {
  591.    static HASHPTR useshell = NIL(HASH);
  592.    static HASHPTR command  = NIL(HASH);
  593.    static         int   read_cmnd = 0;
  594.    register STRINGPTR    rp;
  595.    STRINGPTR            orp;
  596.    char            *cmnd;
  597.    char            *groupfile;
  598.    FILE            *tmpfile;
  599.    int            do_it;
  600.    t_attr        attr;
  601.    int            group;
  602.    int            trace;
  603.    int            rval  = 0;
  604.  
  605.    DB_ENTER( "Exec_commands" );
  606.  
  607.    attr  = Glob_attr | cp->ce_attr;
  608.    trace = Trace || !(attr & A_SILENT);
  609.    group = cp->ce_flag & F_GROUP;
  610.  
  611.    /* Do it again here for those that call us from places other than Make()
  612.     * above. */
  613.    orp = _recipes[ RP_RECIPE ];
  614.    _recipes[ RP_RECIPE ] = cp->ce_recipe;
  615.  
  616.    if( group ) {
  617.       /* Leave this assignment of Current_target here.  It is needed just
  618.        * incase the user hits ^C after the tempfile for the group recipe
  619.        * has been opened. */
  620.       Current_target = cp;
  621.       trace  = Trace || !(attr & A_SILENT);
  622.  
  623.       if( !Trace ) tmpfile = Start_temp( Grp_suff, cp, &groupfile );
  624.       if( trace )  fputs( "[\n", stdout );
  625.  
  626.       /* Emit group prolog */
  627.       if( attr & A_PROLOG )
  628.          _append_file( _recipes[RP_GPPROLOG], tmpfile, cp->CE_NAME, trace );
  629.    }
  630.  
  631.    if( !useshell  ) useshell=Def_macro("USESHELL",NIL(char),M_MULTI|M_EXPANDED);
  632.    if( !read_cmnd ) {command = GET_MACRO("COMMAND"); read_cmnd = 1;}
  633.  
  634.    /* Process commands in recipe. If in group, merely append to file.
  635.     * Otherwise, run them.  */
  636.    for( rp = _recipes[RP_RECIPE]; rp != NIL(STRING); rp=rp->st_next,FREE(cmnd)){
  637.       t_attr a_attr = A_DEFAULT;
  638.       t_attr l_attr;
  639.       char   *p;
  640.       int    new_attr = FALSE;
  641.       int    shell;
  642.  
  643.       /* Reset it for each recipe line otherwise tempfiles don't get removed.
  644.        * Since processing of $(mktmp ...) depends on Current_target being
  645.        * correctly set. */
  646.       Current_target = cp;
  647.  
  648.       /* Only check for +,-,%,@ if the recipe line begins with a '$' macro
  649.        * expansion.  Otherwise there is no way it is going to find these
  650.        * now. */
  651.       if( *rp->st_string == '$' && !group ) {
  652.          t_attr s_attr = Glob_attr;
  653.      Glob_attr |= A_SILENT;
  654.      Suppress_temp_file = TRUE;
  655.      cmnd = Expand(rp->st_string);
  656.      Suppress_temp_file = FALSE;
  657.      a_attr |= Rcp_attribute(cmnd);
  658.      FREE(cmnd);
  659.      ++new_attr;
  660.      Glob_attr = s_attr;
  661.       }
  662.  
  663.       l_attr = attr|a_attr|rp->st_attr;
  664.       shell  = ((l_attr & A_SHELL) != 0);
  665.       useshell->ht_value = (group||shell)?"yes":"no";
  666.  
  667.       cmnd = Expand( rp->st_string );
  668.  
  669.       if( new_attr ) {
  670.      char *ecmnd = cmnd;
  671.      cmnd = _strdup(_strspn(cmnd, " \t\n+-@%"));
  672.      FREE(ecmnd);
  673.       }
  674.  
  675.       /* COMMAND macro is set to "$(CMNDNAME) $(CMNDARGS)" by default, it is
  676.        * possible for the user to reset it to, for example
  677.        *    COMMAND = $(CMNDNAME) @$(mktmp $(CMNDARGS))
  678.        * in order to get a different interface for his command execution. */
  679.       if( command != NIL(HASH) && !group && *(p = _strpbrk(cmnd, " \t\n")) ) {
  680.      char *cname = cmnd;
  681.  
  682.      *p = '\0';
  683.      (void) Def_macro("CMNDNAME",cname,M_MULTI|M_EXPANDED);
  684.      (void) Def_macro("CMNDARGS",p+1,M_MULTI|M_EXPANDED);
  685.  
  686.      cmnd = Expand("$(COMMAND)");
  687.      FREE(cname);             /* cname == cmnd at this point. */
  688.       }
  689.  
  690.       Swap_on_exec = ((l_attr & A_SWAP) != 0);      /* Swapping for DOS only */
  691.       do_it = !Trace;
  692.  
  693.       if( !group && Trace && _strstr(rp->st_string,"$(MAKE)") ) {
  694.      Wait_for_completion |= Trace;
  695.      do_it = TRUE;
  696.       }
  697.  
  698.       if( group )
  699.          Append_line( cmnd, TRUE, tmpfile, cp->CE_NAME, trace, 0 );
  700.       else {
  701.      if( *_strspn(cmnd, " \t") != '\0' )
  702.         Print_cmnd(cmnd, !(do_it && (l_attr & A_SILENT)), 0);
  703.      else
  704.         do_it = FALSE;
  705.  
  706.      rval=Do_cmnd(cmnd,FALSE,do_it,cp,(l_attr&A_IGNORE)!=0, shell,
  707.               rp->st_next == NIL(STRING) );
  708.       }
  709.    }
  710.  
  711.    /* If it is a group then output the EPILOG if required and possibly
  712.     * execute the command */
  713.    if( group ) {
  714.       if( attr & A_EPILOG )    /* emit epilog */
  715.      _append_file( _recipes[RP_GPEPILOG], tmpfile, cp->CE_NAME, trace );
  716.  
  717.       if( trace ) fputs("]\n", stdout);
  718.  
  719.       if( do_it = !Trace ) Close_temp( cp, tmpfile );
  720.       rval = Do_cmnd(groupfile, TRUE, do_it, cp, (attr & A_IGNORE)!=0,
  721.              TRUE, TRUE);
  722.    }
  723.  
  724.    Wait_for_completion = FALSE;
  725.    _recipes[ RP_RECIPE ] = orp;
  726.    DB_RETURN( rval );
  727. }
  728.  
  729.  
  730. PUBLIC void
  731. Print_cmnd( cmnd, echo, map )/*
  732. ================================
  733.    This routine is called to print out the command to stdout.  If echo is
  734.    false the printing to stdout is supressed, but the new lines in the command
  735.    are still deleted. */
  736. char *cmnd;
  737. int  echo;
  738. int  map;
  739. {
  740.    register char *p;
  741.    register char *n;
  742.    char tmp[3];
  743.  
  744.    DB_ENTER( "Print_cmnd" );
  745.  
  746.    if( echo ) {
  747.       printf( "%s\n", cmnd  );
  748.       fflush(stdout);
  749.    }
  750.  
  751.    tmp[0] = ESCAPE_CHAR;
  752.    tmp[1] = CONTINUATION_CHAR;
  753.    tmp[2] = '\0';
  754.  
  755.    for( p=cmnd; *(n = _strpbrk(p,tmp)) != '\0'; )
  756.       if(*n == CONTINUATION_CHAR && n[1] == '\n') {
  757.      DB_PRINT( "make", ("fixing [%s]", p) );
  758.      strcpy( n, n+2 );
  759.      p = n;
  760.       }
  761.       else {
  762.          if( *n == ESCAPE_CHAR && map ) Map_esc( n );
  763.      p = n+1;
  764.       }
  765.  
  766.    DB_VOID_RETURN;
  767. }
  768.  
  769.  
  770.  
  771. /* These routines are used to maintain a stack of directories when making
  772.  * the targets.  If a target cd's to the directory then it is assumed that
  773.  * it will undo it when it is finished making itself. */
  774.  
  775. static STRINGPTR dir_stack = NIL(STRING);
  776.  
  777. int
  778. Push_dir( dir, name, ignore )/*
  779. ===============================
  780.    Change the current working directory to dir and save the current
  781.    working directory on the stack so that we can come back.
  782.    
  783.    If ignore is TRUE then do not complain about _ch_dir if not possible.*/
  784. char *dir;
  785. char *name;
  786. int  ignore;
  787. {
  788.    STRINGPTR   new_dir;
  789.  
  790.    DB_ENTER( "Push_dir" );
  791.  
  792.    if( dir == NIL(char)  || *dir == '\0' ) dir = Pwd;
  793.    if( *dir == '\'' && dir[strlen(dir)-1] == '\'' ) {
  794.       dir = _strdup(dir+1);
  795.       dir[strlen(dir)-1]='\0';
  796.    }
  797.    else
  798.       dir = Expand(dir);
  799.  
  800.    if( Set_dir(dir) ) {
  801.       if( !ignore )
  802.          Fatal( "Unable to change to directory `%s', target is [%s]",
  803.             dir, name );
  804.       FREE(dir);
  805.       DB_RETURN( 0 );
  806.    }
  807.  
  808.    DB_PRINT( "dir", ("Push: [%s]", dir) );
  809.    if( Verbose & V_PRINT_DIR )
  810.       printf( "%s:  Changed to directory [%s]\n", Pname, dir  );
  811.  
  812.    FREE( dir );
  813.    TALLOC( new_dir, 1, STRING );
  814.    new_dir->st_next   = dir_stack;
  815.    dir_stack          = new_dir;
  816.    new_dir->st_string = _strdup( Pwd );
  817.  
  818.    Def_macro( "PWD", Get_current_dir(), M_MULTI | M_EXPANDED );
  819.    _set_tmd();
  820.  
  821.    DB_RETURN( 1 );
  822. }
  823.  
  824.  
  825.  
  826. PUBLIC void
  827. Pop_dir(ignore)/*
  828. =================
  829.    Change the current working directory to the previous saved dir. */
  830. int ignore;
  831. {
  832.    STRINGPTR old_dir;
  833.    char      *dir;
  834.  
  835.    DB_ENTER( "Pop_dir" );
  836.  
  837.    if( dir_stack == NIL(STRING) )
  838.       if( ignore ) {
  839.          DB_VOID_RETURN;
  840.       }
  841.       else
  842.      Error( "Directory stack empty for return from .SETDIR" );
  843.  
  844.    if( Set_dir(dir = dir_stack->st_string) )
  845.       Fatal( "Could not change to directory `%s'", dir );
  846.  
  847.    Def_macro( "PWD", dir, M_MULTI | M_EXPANDED );
  848.    DB_PRINT( "dir", ("Pop: [%s]", dir) );
  849.    if( Verbose & V_PRINT_DIR )
  850.       printf( "%s:  Changed back to directory [%s]\n", Pname, dir);
  851.  
  852.    old_dir   = dir_stack;
  853.    dir_stack = dir_stack->st_next;
  854.  
  855.    FREE( old_dir->st_string );
  856.    FREE( old_dir );
  857.    _set_tmd();
  858.  
  859.    DB_VOID_RETURN;
  860. }
  861.  
  862.  
  863.  
  864. static void
  865. _set_tmd()/*
  866. ============
  867.    Set the TWD Macro */
  868. {
  869.    TKSTR md, pd;
  870.    char  *m, *p;
  871.    char  *tmd;
  872.    int   is_sep;
  873.    int   first = 1;
  874.  
  875.    SET_TOKEN( &md, Makedir );
  876.    SET_TOKEN( &pd, Pwd );
  877.  
  878.    m = Get_token( &md, DirBrkStr, FALSE );
  879.    (void) Get_token( &pd, DirBrkStr, FALSE );
  880.    is_sep = (strchr(DirBrkStr, *m) != NIL(char));
  881.    tmd = _strdup( "" );
  882.  
  883.    do {
  884.       m = Get_token( &md, DirBrkStr, FALSE );
  885.       p = Get_token( &pd, DirBrkStr, FALSE );
  886.  
  887.       if( !is_sep && strcmp(m, p) ) {    /* they differ */
  888.      char *tmp;
  889.      if( first ) {        /* They differ in the first component    */
  890.         tmd = Makedir;    /* In this case use the full path    */
  891.         break;
  892.      }
  893.  
  894.      if( *p ) tmp = Build_path( "..", tmd );
  895.      if( *m ) tmp = Build_path( tmd, m );
  896.      FREE( tmd );
  897.      tmd = _strdup( tmp );
  898.       }
  899.  
  900.       is_sep = 1-is_sep;
  901.       first  = 0;
  902.    } while (*m || *p);
  903.  
  904.    CLEAR_TOKEN( &md );
  905.    CLEAR_TOKEN( &pd );
  906.  
  907.    Def_macro( "TMD", tmd, M_MULTI | M_EXPANDED );
  908.    if( tmd != Makedir ) FREE( tmd );
  909. }
  910.  
  911.  
  912. static void
  913. _set_recipe( target, ind )/*
  914. ============================
  915.    Set up the _recipes static variable so that the slot passed in points
  916.    at the rules corresponding to the target supplied. */
  917. char *target;
  918. int  ind;
  919. {
  920.    CELLPTR cp;
  921.    HASHPTR hp;
  922.  
  923.    if( (hp = Get_name(target, Defs, FALSE)) != NIL(HASH) ) {
  924.       cp = hp->CP_OWNR;
  925.       _recipes[ ind ] = cp->ce_recipe;
  926.    }
  927.    else
  928.       _recipes[ ind ] = NIL(STRING);
  929. }
  930.  
  931.  
  932.  
  933. PUBLIC void
  934. Append_line( cmnd, newline, tmpfile, name, printit, map )
  935. char *cmnd;
  936. int  newline;
  937. FILE *tmpfile;
  938. char *name;
  939. int  printit;
  940. int  map;
  941. {
  942.    Print_cmnd( cmnd, printit, map );
  943.  
  944.    if( Trace ) return;
  945.  
  946.    fputs(cmnd, tmpfile);
  947.    if( newline ) fputc('\n', tmpfile);
  948.    fflush(tmpfile);
  949.  
  950.    if( ferror(tmpfile) )
  951.       Fatal("Write error on temporary file, while processing `%s'", name);
  952. }
  953.  
  954.  
  955.  
  956. static void
  957. _append_file( rp, tmpfile, name, printit )
  958. register STRINGPTR rp;
  959. FILE            *tmpfile;
  960. char            *name;
  961. int            printit;
  962. {
  963.    char *cmnd;
  964.  
  965.    while( rp != NIL(STRING) ) {
  966.       Append_line(cmnd = Expand(rp->st_string), TRUE, tmpfile, name, printit,0);
  967.       FREE(cmnd);
  968.       rp = rp->st_next;
  969.    }
  970. }
  971.  
  972.  
  973. #define NUM_BUCKETS    20
  974.  
  975. typedef struct strpool {
  976.    char       *string;    /* a pointer to the string value */
  977.    uint32      keyval;    /* the strings hash value     */
  978.    struct strpool *next;    /* hash table link pointer     */
  979. } POOL, *POOLPTR;
  980.  
  981. static POOLPTR strings[ NUM_BUCKETS ];
  982.  
  983. static char *
  984. _pool_lookup( str )/*
  985. =====================
  986.    Scan down the list of chained strings and see if one of them matches
  987.    the string we are looking for. */
  988. char    *str;
  989. {
  990.    register POOLPTR key;
  991.    uint32   keyval;
  992.    uint16   hv;
  993.    uint16   keyindex;
  994.    char     *string;
  995.  
  996.    DB_ENTER( "_pool_lookup" );
  997.  
  998.    if( str == NIL(char) ) DB_RETURN("");
  999.  
  1000.    hv  = Hash(str, &keyval);
  1001.    key = strings[ keyindex = (hv % NUM_BUCKETS) ];
  1002.  
  1003.    while( key != NIL(POOL) )
  1004.       if( (key->keyval != keyval) || strcmp(str, key->string) )
  1005.      key = key->next;
  1006.       else
  1007.      break;
  1008.  
  1009.    if( key == NIL(POOL) ) {
  1010.       DB_PRINT( "pool", ("Adding string [%s]", str) );
  1011.       TALLOC( key, 1, POOL );            /* not found so add string */
  1012.       
  1013.       key->string = string = _strdup(str);
  1014.       key->keyval = keyval;
  1015.  
  1016.       key->next           = strings[ keyindex ];
  1017.       strings[ keyindex ] = key;
  1018.    }
  1019.    else {
  1020.       DB_PRINT( "pool", ("Found string [%s], key->string") );
  1021.       string = key->string;
  1022.    }
  1023.  
  1024.    DB_RETURN( string );
  1025. }
  1026.