home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / make.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  36KB  |  1,268 lines

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