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

  1. /* RCS      -- $Header: /u5/dvadura/src/public/dmake/src/RCS/infer.c,v 1.1 1994/10/06 17:41:36 dvadura Exp $
  2. -- SYNOPSIS -- infer how to make a target.
  3. -- 
  4. -- DESCRIPTION
  5. --    This file contains the code to infer a recipe, and possibly some new
  6. --    prerequisites for a target which dmake does not know how to make, or
  7. --    has no explicit recipe.
  8. --
  9. --    The inference fails if no path through the inference graph can be
  10. --    found by which we can make the target.
  11. -- 
  12. -- AUTHOR
  13. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  14. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  15. --
  16. -- COPYRIGHT
  17. --      Copyright (c) 1992,1994 by Dennis Vadura.  All rights reserved.
  18. -- 
  19. --      This program is free software; you can redistribute it and/or
  20. --      modify it under the terms of the GNU General Public License
  21. --      (version 1), as published by the Free Software Foundation, and
  22. --      found in the file 'LICENSE' included with this distribution.
  23. -- 
  24. --      This program is distributed in the hope that it will be useful,
  25. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  26. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27. --      GNU General Public License for more details.
  28. -- 
  29. --      You should have received a copy of the GNU General Public License
  30. --      along with this program;  if not, write to the Free Software
  31. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. --
  33. -- LOG
  34. --     $Log: infer.c,v $
  35.  * Revision 1.1  1994/10/06  17:41:36  dvadura
  36.  * dmake Release Version 4.0, Initial revision
  37.  *
  38. */
  39.  
  40. #include "extern.h"
  41.  
  42. /* attributes that get transfered from the % start cell to the inferred
  43.  * cells. */
  44.  
  45. #define A_TRANSFER  (A_EPILOG | A_PRECIOUS | A_SILENT | A_SHELL | A_SETDIR |\
  46.              A_SEQ | A_LIBRARY | A_IGNORE | A_PROLOG | A_SWAP |\
  47.              A_NOSTATE )
  48.  
  49.  
  50. /* Define local static functions */
  51. static DFALINKPTR dfa_subset  ANSI((DFALINKPTR, DFASETPTR));
  52. static void       free_dfas   ANSI((DFALINKPTR));
  53. static int      count_dots  ANSI((char *));
  54. static char *      buildname  ANSI((char *, char *, char *));
  55. static void      free_icells ANSI(());
  56. static ICELLPTR   union_iset  ANSI((ICELLPTR, ICELLPTR));
  57. static ICELLPTR   add_iset    ANSI((ICELLPTR,ICELLPTR,CELLPTR,DFALINKPTR,
  58.                      CELLPTR,int,int,char *,char *, int));
  59. static ICELLPTR   derive_prerequisites ANSI((ICELLPTR, ICELLPTR *));
  60. static char *      dump_inf_chain ANSI((ICELLPTR, int, int));
  61.  
  62.  
  63. PUBLIC void
  64. Infer_recipe( cp, setdirroot )/*
  65. ================================
  66.    Perform a breadth-first search of the inference graph and return if
  67.    possible an inferred set of prerequisites for making the current target. */
  68. CELLPTR cp;
  69. CELLPTR setdirroot;
  70. {
  71.    ICELLPTR nomatch, match;
  72.  
  73.    DB_ENTER("Infer_recipe");
  74.  
  75.    if( cp->ce_attr & A_NOINFER ) {DB_VOID_RETURN;}
  76.  
  77.    match = NIL(ICELL);
  78.    nomatch = add_iset( NIL(ICELL), NIL(ICELL), NIL(CELL), NIL(DFALINK),
  79.             setdirroot, Prep+count_dots(cp->CE_NAME), 0,
  80.             DmStrDup(cp->CE_NAME), NIL(char),
  81.             cp->ce_time != (time_t)0L);
  82.  
  83.    /* Make sure we try whole heartedly to infer at least one suffix */
  84.    if( nomatch->ic_dmax == 0 ) ++nomatch->ic_dmax;
  85.  
  86.    DB_EXECUTE( "inf", _dump_iset("nomatch",nomatch); );
  87.  
  88.    while( nomatch != NIL(ICELL) ) {
  89.       ICELLPTR new_nomatch = NIL(ICELL);
  90.       ICELLPTR ic, pmatch, mmatch;
  91.       CELLPTR  prereq;
  92.       int      first;
  93.  
  94.       for( ic=nomatch; ic != NIL(ICELL); ic=ic->ic_next ) {
  95.      int ipush = FALSE;
  96.  
  97.      if( ic->ic_dir ) ipush = Push_dir(ic->ic_dir, ic->ic_name, FALSE);
  98.      match = union_iset(match, derive_prerequisites(ic, &new_nomatch));
  99.      if( ipush ) Pop_dir(FALSE);
  100.       }
  101.  
  102.       DB_EXECUTE( "inf", _dump_iset("match",match); );
  103.       DB_EXECUTE( "inf", _dump_iset("nomatch",new_nomatch); );
  104.  
  105.       /* We have now deduced the two sets MATCH and NOMATCH.  MATCH holds the
  106.        * set of edges that we encountered that matched.  If this set is empty
  107.        * then we can apply transitive closure (if enabled) to the elements of
  108.        * NOMATCH to see if we can find some other method to make the target.
  109.        *
  110.        * If MATCH is non-empty, we have found a method for making the target.
  111.        * It is the shortest method for doing so (ie. uses fewest number of
  112.        * steps).  If MATCH contains more than one element then we have a
  113.        * possible ambiguity.
  114.        */
  115.       if( match == NIL(ICELL) ) {
  116.      nomatch = new_nomatch;
  117.      if( Transitive ) continue;
  118.      goto all_done;
  119.       }
  120.  
  121.       /* Ok, we have a set of possible matches in MATCH, we should check the
  122.        * set for ambiguity.  If more than one inference path exists of the
  123.        * same depth, then we may issue an ambiguous inference error message.
  124.        *
  125.        * The message is suppressed if MATCH contains two elements and one of
  126.        * them is the empty-prerequisite-rule.  In this case we ignore the
  127.        * ambiguity and take the rule that infers the prerequisite.
  128.        *
  129.        * Also if there are any chains that rely on a non-existant prerequisite
  130.        * that may get made because it has a recipe then we prefer any that
  131.        * rely on existing final prerequisites over those that we have to make.
  132.        */
  133.  
  134.       /* Split out those that have to be made from those that end in
  135.        * prerequisites that already exist. */
  136.       pmatch = mmatch = NIL(ICELL);
  137.       for(; match; match = ic ) {
  138.      ic = match->ic_next;
  139.      match->ic_next = NIL(ICELL);
  140.  
  141.      if( match->ic_exists )
  142.         pmatch = union_iset(pmatch, match);
  143.      else
  144.         mmatch = union_iset(mmatch, match);
  145.       }
  146.  
  147.       if( pmatch )
  148.      match = pmatch;
  149.       else
  150.      match = mmatch;
  151.  
  152.       /* Make sure it is unique */
  153.       if( match->ic_next != NIL(ICELL) ) {
  154.      int dump = (match->ic_next->ic_next != NIL(ICELL));
  155.  
  156.      /* Check for definite ambiguity */
  157.      if( !dump )
  158.         if( (match->ic_meta->ce_prq && match->ic_next->ic_meta->ce_prq) ||
  159.             (!match->ic_meta->ce_prq && !match->ic_next->ic_meta->ce_prq)  )
  160.            dump = TRUE;
  161.         else if(!match->ic_meta->ce_prq && match->ic_next->ic_meta->ce_prq )
  162.            match = match->ic_next;
  163.  
  164.      if( dump ) {
  165.         int count = 1;
  166.  
  167.         Continue = TRUE;
  168.         Error( "Ambiguous inference chains for target '%s'", cp->CE_NAME );
  169.         for( ic=match; ic; ic=ic->ic_next )
  170.            (void) dump_inf_chain(ic, TRUE, count++);
  171.         Fatal( "resolve ambiguity before proceeding.");
  172.         /*NOTREACHED*/
  173.      }
  174.       }
  175.  
  176.       /* MATCH now points at the derived recipe.  We must now take cp, and
  177.        * construct the correct graph so that the make may proceed. */
  178.  
  179.       if( Verbose & V_INFER ) {
  180.      char *tmp = dump_inf_chain(match, TRUE, FALSE);
  181.      printf("%s:  Inferring prerequistes and recipes using:\n%s:  ... %s\n",
  182.          Pname, Pname, tmp );
  183.      FREE(tmp);      }
  184.  
  185.       pmatch = NIL(ICELL);
  186.       prereq = NIL(CELL);
  187.       first  = TRUE;
  188.  
  189.       while( match ) {
  190.          CELLPTR infcell=NIL(CELL);
  191.  
  192.      /* Compute the inferred prerequisite first. */
  193.      if( match->ic_name ) {
  194.         if( match->ic_meta )
  195.            infcell = Def_cell( match->ic_name );
  196.         else
  197.            infcell = cp;
  198.  
  199.         infcell->ce_flag |= F_TARGET;
  200.  
  201.         if( infcell != cp ) {
  202.            infcell->ce_flag |= F_INFER;
  203.            if( !first ) infcell->ce_flag |= F_REMOVE;
  204.         }
  205.  
  206.         if( !match->ic_flag )
  207.            infcell->ce_attr |= A_NOINFER;
  208.  
  209.         first = FALSE;
  210.      }
  211.  
  212.      /* Add global prerequisites from previous rule if there are any and
  213.       * the recipe. */
  214.      if( pmatch ) {
  215.         CELLPTR imeta = pmatch->ic_meta;
  216.         LINKPTR lp;
  217.  
  218.         infcell->ce_per   = pmatch->ic_dfa->dl_per;
  219.         infcell->ce_attr |= (imeta->ce_attr & A_TRANSFER);
  220.  
  221.         if( !(infcell->ce_flag & F_RULES) ) {
  222.            infcell->ce_flag |= (imeta->ce_flag&(F_SINGLE|F_GROUP))|F_RULES;
  223.            infcell->ce_recipe = imeta->ce_recipe;
  224.         }
  225.  
  226.         /* Add any conditional macro definitions that may be associated
  227.          * with the inferred cell. */
  228.         if (imeta->ce_cond != NIL(STRING)) {
  229.            STRINGPTR sp,last;
  230.            
  231.            last = infcell->ce_cond;
  232.            for(sp=imeta->ce_cond; sp; sp=sp->st_next) {
  233.           STRINGPTR new;
  234.           TALLOC(new, 1, STRING);
  235.           new->st_string = DmStrDup(sp->st_string);
  236.           if(last)
  237.              last->st_next = new;
  238.           else
  239.              infcell->ce_cond = new;
  240.           last = new;
  241.            }
  242.         }
  243.  
  244.         pmatch->ic_dfa->dl_per = NIL(char);
  245.  
  246.         /* If infcell already had a directory set then modify it based on
  247.          * whether it was the original cell or some intermediary. */
  248.         if( imeta->ce_dir )
  249.            if( infcell->ce_dir && infcell == cp ) {
  250.           /* cp->ce_dir was set and we have pushed the directory prior
  251.            * to calling this routine.  We should therefore pop it and
  252.            * push the new concatenated directory required by the
  253.            * inference. */
  254.           infcell->ce_dir=DmStrDup(Build_path(infcell->ce_dir,
  255.                              imeta->ce_dir));
  256.            }
  257.            else
  258.           infcell->ce_dir = imeta->ce_dir;
  259.  
  260.         for( lp=imeta->ce_indprq; lp != NIL(LINK); lp=lp->cl_next ) {
  261.            char    *name = lp->cl_prq->CE_NAME;
  262.            CELLPTR tcp;
  263.  
  264.            name = buildname( cp->CE_NAME, name, infcell->ce_per );
  265.            tcp  = Def_cell( name );
  266.            tcp->ce_flag |= F_REMOVE;
  267.            Add_prerequisite( infcell, tcp, FALSE, FALSE );
  268.  
  269.            if( Verbose & V_INFER )
  270.           printf( "%s:  Inferred indirect prerequisite [%s]\n",
  271.               Pname, name );
  272.            FREE(name);
  273.         }
  274.      }
  275.  
  276.      /* Add the previous cell as the prerequisite */
  277.      if( prereq )
  278.         (Add_prerequisite(infcell,prereq,FALSE,FALSE))->cl_flag |=F_TARGET;
  279.  
  280.      pmatch = match;
  281.      prereq = infcell;
  282.      match  = match->ic_parent;
  283.       }
  284.  
  285.       DB_PRINT("inf", ("Terminated due to a match"));
  286.       break;
  287.    }
  288.  
  289. all_done:
  290.    free_icells();
  291.  
  292.    DB_VOID_RETURN;
  293. }
  294.  
  295.  
  296. static ICELLPTR
  297. derive_prerequisites( ic, nnmp )/*
  298. ===================================
  299.    Take a cell and derive a set of prerequisites from the cell.  Categorize
  300.    them into those that MATCH (ie. those that we found in the file system),
  301.    and those that do not match NOMATCH that we may possibly have a look at
  302.    later.  When we process the next level of the breadth-first search.
  303.    
  304.    Once MATCH is non-empty we will stop inserting elements into NOMATCH
  305.    since we know that either MATCH is successful and unique or it will
  306.    issue an ambiguity error.  We will never go on to look at elements
  307.    in NOMATCH after wards. */
  308. ICELLPTR ic;
  309. ICELLPTR *nnmp;
  310. {
  311.    ICELLPTR   match = NIL(ICELL);
  312.    DFALINKPTR pdfa;
  313.    DFALINKPTR dfas;
  314.  
  315.    DB_ENTER("derive_prerequisites");
  316.  
  317.    /* If none of the inference nodes match then forget about the inference.
  318.     * The user did not tell us how to make such a target.  We also stop the
  319.     * Inference if the new set of DFA's is a proper subset of a previous
  320.     * subset and it's PREP counts exceed the value of Prep.
  321.     */
  322.    dfas = dfa_subset( Match_dfa(ic->ic_name), &ic->ic_dfastack );
  323.  
  324.    DB_EXECUTE("inf", _dump_dfa_stack(dfas, &ic->ic_dfastack); );
  325.  
  326.    /* Ok, we have nothing here to work with so return an empty cell. */
  327.    if( dfas == NIL(DFALINK) ) {
  328.       DB_PRINT( "mem", ("%s:<- mem %ld",ic->ic_name, (long)coreleft()));
  329.       DB_PRINT( "inf", ("<<< Exit, no dfas, cp = %04x", NIL(CELL)) );
  330.       DB_RETURN( NIL(ICELL) );
  331.    }
  332.  
  333.    /* Save the dfas, we are going to use on the stack for this cell. */
  334.    ic->ic_dfastack.df_set = dfas;
  335.  
  336.    /* Run through the %-meta cells, build the prerequisite cells.  For each
  337.     * %-meta go through it's list of edges and try to use each in turn to
  338.     * deduce a likely prerequisite.  We perform a breadth-first search
  339.     * matching the first path that results in a unique method for making the
  340.     * target. */
  341.    for( pdfa = dfas; pdfa != NIL(DFALINK); pdfa = pdfa->dl_next ) {
  342.       LINK tl;
  343.       LINKPTR edge;
  344.       CELLPTR pmeta;
  345.  
  346.       pmeta = pdfa->dl_meta;
  347.       DB_PRINT( "inf", ("Using dfa:  [%s]", pmeta->CE_NAME) );
  348.  
  349.       /* If the %-meta is a singleton meta then deal with it differently from
  350.        * the case when it is a bunch of %-meta's found on the original entries
  351.        * prerequisite list. */
  352.       if( pmeta->ce_flag & F_MULTI )
  353.      edge = pmeta->ce_prq;
  354.       else {
  355.      tl.cl_prq = pmeta;
  356.      tl.cl_next = NIL(LINK);
  357.      edge = &tl;
  358.       }
  359.  
  360.       /* Now run through the list of prerequisite edge's for the %-meta. */
  361.       for( ; edge != NIL(LINK); edge = edge->cl_next ) {
  362.      HASHPTR  thp;        /* temporary hash table pointer        */
  363.      HASH     iprqh;    /* hash cell for new prerequisite    */
  364.      CELL     iprq;        /* inferred prerequisite to look for    */
  365.      CELLPTR  idirroot;    /* Inferred prerequisite root        */
  366.      CELLPTR  nidirroot;    /* Inferred prerequisite root        */
  367.      STRINGPTR ircp;    /* Inferred prerequisites recipe    */
  368.      char     *idir;    /* directory to CD to.            */
  369.      int      ipush = 0;    /* flag for push on inferred prereq     */
  370.      char     *name = NIL(char);         /* prerequisite name    */
  371.      CELLPTR  meta = edge->cl_prq;
  372.      int      dmax_fix;
  373.      int      trans;
  374.      int      noinf;
  375.      int      exists;
  376.      
  377.      if( meta->ce_prq )
  378.         name = meta->ce_prq->cl_prq->CE_NAME;
  379.  
  380.      DB_PRINT( "inf", ("Trying edge from [%s] to [%s] for [%s]",
  381.            meta->CE_NAME, name?name:"(nil)", ic->ic_name) );
  382.  
  383.      /* Set the temp CELL used for building prerequisite candidates to
  384.       * all zero so that we don't have to keep initializing all the
  385.       * fields. */
  386.      {
  387.         register char *s = (char *) &iprq;
  388.         register int   n = sizeof(CELL);
  389.         while( n ) { *s++ = '\0'; n--; }
  390.      }
  391.  
  392.      nidirroot = idirroot = ic->ic_setdirroot;
  393.      iprq.ce_name = &iprqh;
  394.  
  395.      if( name ) {
  396.         /* Build the prerequisite name from the %-meta prerequisite given
  397.          * for the %-meta rule. */
  398.         iprqh.ht_name = buildname( ic->ic_name, name, pdfa->dl_per );
  399.         if((dmax_fix = (count_dots(name)-count_dots(meta->CE_NAME))) < 0)
  400.            dmax_fix = 0;
  401.  
  402.         if( !strcmp(ic->ic_name, iprqh.ht_name) ||
  403.         (count_dots(iprqh.ht_name) > ic->ic_dmax + dmax_fix) ) {
  404.            FREE( iprqh.ht_name );
  405.            continue;
  406.         }
  407.  
  408.         DB_PRINT( "inf", ("Checking prerequisite [%s]", iprqh.ht_name) );
  409.  
  410.         /* See if the prerequisite CELL has been previously defined.  If
  411.          * it has, then make a copy of it into iprq, and use it to try
  412.          * the inference.  We make the copy so that we don't modify the
  413.          * stat of the inferred cell if the inference fails.
  414.          */
  415.         thp = Get_name( iprqh.ht_name, Defs, FALSE );
  416.         if(thp != NIL(HASH)) {
  417.            iprq = *thp->CP_OWNR;
  418.            ircp = iprq.ce_recipe;
  419.         }
  420.         else
  421.            ircp = NIL(STRING);
  422.      }
  423.      else
  424.         iprqh.ht_name = NIL(char);
  425.  
  426.  
  427.      /* If the %-meta has a .SETDIR set then we change to the new
  428.       * directory prior to performing the stat of the new prerequisite.
  429.       * If the change of directory fails then the rule is droped from
  430.       * further consideration.
  431.       */
  432.      if( iprq.ce_dir ) {
  433.         if( (ipush = Push_dir(iprq.ce_dir, iprqh.ht_name, TRUE)) != 0 ) {
  434.            nidirroot = thp->CP_OWNR;
  435.            idir      = Pwd;
  436.         }
  437.         else {
  438.            if( iprqh.ht_name ) FREE( iprqh.ht_name );
  439.            continue;
  440.         }
  441.      }
  442.      else
  443.         idir = NIL(char);
  444.  
  445.  
  446.      /* Stat the inferred prerequisite.
  447.       */
  448.      if( name ) {
  449.         if( Verbose & V_INFER )
  450.            printf( "%s:  Trying prerequisite [%s] for [%s]\n", Pname,
  451.                iprqh.ht_name, ic->ic_name );
  452.  
  453.         if( !(iprq.ce_flag & F_STAT) ) Stat_target(&iprq, FALSE, FALSE);
  454.      }
  455.  
  456.  
  457.      /* If the STAT succeeded or if the prerequisite has a recipe for
  458.       * making it then it's a match and a candidate for getting infered.
  459.       * Otherwise it is not a match, and we cannot yet tell if it is
  460.       * going to be a successful path to follow, so we save it for
  461.       * later consideration.
  462.       */
  463.      noinf = ((Glob_attr)&A_NOINFER);
  464.      if( meta->ce_prq )
  465.         noinf |= ((meta->ce_prq->cl_prq->ce_attr)&A_NOINFER);
  466.      trans = Transitive || !noinf;
  467.      exists = (iprq.ce_time != (time_t)0L);
  468.  
  469.      if( exists || (ircp != NIL(STRING)) || !name ) {
  470.         match = add_iset( match, ic, meta, pdfa, idirroot, ic->ic_dmax,
  471.                    trans, iprq.ce_name->ht_name, idir, exists );
  472.         DB_PRINT("inf",("Added to MATCH %s",iprq.ce_name->ht_name));
  473.      }
  474.      else if( !noinf && match == NIL(ICELL) ) {
  475.         *nnmp = add_iset( *nnmp, ic, meta, pdfa, nidirroot, ic->ic_dmax,
  476.                    trans, iprq.ce_name->ht_name, idir, exists );
  477.         DB_PRINT("inf",("Added to NOMATCH %s",iprq.ce_name->ht_name));
  478.      }
  479.  
  480.      /* If we pushed a directory for the inferred prerequisite then
  481.       * pop it.
  482.       */
  483.      if( ipush ) Pop_dir(FALSE);
  484.      if( iprqh.ht_name ) FREE(iprqh.ht_name);
  485.       }
  486.    }
  487.  
  488.    DB_RETURN(match);
  489. }
  490.  
  491.  
  492. static char *
  493. buildname( tg, meta, per )
  494. char *tg;
  495. char *meta;
  496. char *per;
  497. {
  498.    char    *name;
  499.  
  500.    name = Apply_edit( meta, "%", per, FALSE, FALSE );
  501.    if( strchr(name, '$') ) {
  502.       HASHPTR m_at;
  503.       char *tmp;
  504.  
  505.       m_at = Def_macro( "@", tg, M_MULTI );
  506.       tmp = Expand( name );
  507.  
  508.       if( m_at->ht_value != NIL(char) ) {
  509.      FREE( m_at->ht_value );
  510.      m_at->ht_value = NIL(char);
  511.       }
  512.  
  513.       if( name != meta ) FREE( name );
  514.       name = tmp;
  515.    }
  516.    else if( name == meta )
  517.       name = DmStrDup( name );
  518.  
  519.    return(name);
  520. }
  521.  
  522.  
  523. static DFALINKPTR
  524. dfa_subset( pdfa, stack )/*
  525. ============================
  526.    This is the valid DFA subset computation.  Whenever a CELL has a Match_dfa
  527.    subset computed this algorithm is run to see if any of the previously
  528.    computed sets on the DFA stack are proper subsets of the new set.  If they
  529.    are, then any elements of the matching subset whose Prep counts exceed
  530.    the allowed maximum given by Prep are removed from the computed DFA set,
  531.    and hence from consideration, thereby cutting off the cycle in the
  532.    inference graph. */
  533. DFALINKPTR       pdfa;
  534. register DFASETPTR stack;
  535. {
  536.    register DFALINKPTR element;
  537.    DFALINKPTR          nelement;
  538.  
  539.    DB_ENTER( "dfa_subset" );
  540.  
  541.    DB_PRINT("inf",("Computing DFA subset, PREP = %d",Prep));
  542.    DB_EXECUTE("inf", _dump_dfa_stack(pdfa, stack); );
  543.  
  544.    for(; pdfa != NIL(DFALINK) && stack != NIL(DFASET); stack = stack->df_next) {
  545.       int subset = TRUE;
  546.  
  547.       for( element=stack->df_set; subset && element != NIL(DFALINK);
  548.            element=element->dl_next ) {
  549.          register DFALINKPTR subel;
  550.  
  551.      for( subel = pdfa;
  552.           subel != NIL(DFALINK) && (subel->dl_meta != element->dl_meta);
  553.           subel = subel->dl_next );
  554.  
  555.      DB_PRINT("inf",("Looking for %s, (%s)",element->dl_meta->CE_NAME,
  556.              (subel != NIL(DFALINK))?"succ":"fail"));
  557.  
  558.      if( (subset = (subel != NIL(DFALINK))) != 0 )
  559.         element->dl_member = subel;
  560.       }
  561.  
  562.       if( subset )
  563.      for( element=stack->df_set; element != NIL(DFALINK);
  564.           element=element->dl_next ) {
  565.         DFALINKPTR mem = element->dl_member;
  566.         int        npr = element->dl_prep + 1;
  567.  
  568.         if( npr > Prep )
  569.            mem->dl_delete++;
  570.         else
  571.            mem->dl_prep = npr;
  572.      }
  573.    }
  574.  
  575.    for( element = pdfa; element != NIL(DFALINK); element = nelement ) {
  576.       nelement = element->dl_next;
  577.  
  578.       if( element->dl_delete ) {
  579.      /* A member of the subset has a PREP count equal to PREP, so
  580.       * it should not be considered further in the inference, hence
  581.       * we remove it from the doubly linked set list */
  582.      if( element == pdfa )
  583.         pdfa = element->dl_next;
  584.      else
  585.         element->dl_prev->dl_next = element->dl_next;
  586.  
  587.      if( element->dl_next != NIL(DFALINK) )
  588.         element->dl_next->dl_prev = element->dl_prev;
  589.  
  590.      DB_PRINT("inf", ("deleting dfa [%s]", element->dl_meta->CE_NAME));
  591.      FREE( element->dl_per );
  592.      FREE( element );
  593.       }
  594.    }
  595.  
  596.    DB_RETURN( pdfa );
  597. }
  598.  
  599.  
  600.  
  601. static void
  602. free_dfas( chain )/*
  603. =====================
  604.    Free the list of DFA's constructed by Match_dfa, and linked together by
  605.    LINK cells.  FREE the % value as well, as long as it isn't NIL. */
  606. DFALINKPTR chain;
  607. {
  608.    register DFALINKPTR tl;
  609.  
  610.    DB_ENTER( "free_dfas" );
  611.  
  612.    for( tl=chain; tl != NIL(DFALINK); chain = tl ) {
  613.       tl = tl->dl_next;
  614.  
  615.       DB_PRINT( "inf", ("Freeing DFA [%s], %% = [%s]", chain->dl_meta->CE_NAME,
  616.                 chain->dl_per) );
  617.  
  618.       if( chain->dl_per != NIL(char) ) FREE( chain->dl_per );
  619.       FREE( chain );
  620.    }
  621.  
  622.    DB_VOID_RETURN;
  623. }
  624.  
  625.  
  626. static int
  627. count_dots( name )/*
  628. =====================*/
  629. char *name;
  630. {
  631.    register char *p;
  632.    register int  i = 0;
  633.  
  634.    for( p = name; *p; p++ ) if(*p == '.') i++;
  635.  
  636.    return( i );
  637. }
  638.  
  639.  
  640. static ICELLPTR _icells = NIL(ICELL);
  641. #ifdef DBUG
  642. static int _icell_cost = 0;
  643. #endif
  644.  
  645. static ICELLPTR
  646. add_iset( iset, parent, meta, dfa, setdirroot, dmax, noinf, name, dir, exists)
  647. ICELLPTR   iset;
  648. ICELLPTR   parent;
  649. CELLPTR    meta;
  650. DFALINKPTR dfa;
  651. CELLPTR    setdirroot;
  652. int       dmax;
  653. int       noinf;
  654. char      *name;
  655. char      *dir;
  656. int       exists;
  657. {
  658.    ICELLPTR icell;
  659.  
  660.    DB_ENTER("add_iset");
  661.    TALLOC(icell, 1, ICELL);
  662.  
  663.    DB_EXECUTE("inf", _icell_cost+=(sizeof(ICELL)+strlen(dir?dir:"")+strlen(name?name:"")+2););
  664.  
  665.    icell->ic_meta = meta;
  666.    icell->ic_dfa  = dfa;
  667.    icell->ic_setdirroot = setdirroot;
  668.  
  669.    if( parent ) icell->ic_dfastack.df_next = &parent->ic_dfastack;
  670.  
  671.    icell->ic_dmax = dmax;
  672.    icell->ic_dir = DmStrDup(dir);
  673.    icell->ic_name = DmStrDup(name);
  674.    icell->ic_parent = parent;
  675.    icell->ic_next = iset;
  676.    icell->ic_flag = noinf;
  677.    icell->ic_exists = exists;
  678.  
  679.    icell->ic_link = _icells;
  680.    _icells = icell;
  681.  
  682.    DB_RETURN(icell);
  683. }
  684.  
  685.  
  686. static void
  687. free_icells()
  688. {
  689.    register ICELLPTR ic;
  690.  
  691.    DB_ENTER("free_icells");
  692.  
  693.    for( ; _icells; _icells = ic ) {
  694.       ic = _icells->ic_link;
  695.  
  696.       free_dfas(_icells->ic_dfastack.df_set);
  697.       if( _icells->ic_dir ) FREE(_icells->ic_dir);
  698.       if( _icells->ic_name) FREE(_icells->ic_name);
  699.       FREE(_icells);
  700.    }
  701.  
  702.    DB_PRINT("inf",("Used %d memory for icells",_icell_cost));
  703.    DB_EXECUTE("inf", _icell_cost=0; );
  704.  
  705.    DB_VOID_RETURN;
  706. }
  707.  
  708.  
  709. static ICELLPTR
  710. union_iset( iset, uset )
  711. ICELLPTR iset;
  712. ICELLPTR uset;
  713. {
  714.    register ICELLPTR ic;
  715.  
  716.    if( iset == NIL(ICELL) ) return(uset);
  717.  
  718.    for( ic=iset; ic->ic_next != NIL(ICELL); ic=ic->ic_next );
  719.    ic->ic_next = uset;
  720.  
  721.    return(iset);
  722. }
  723.  
  724.  
  725. static char *
  726. dump_inf_chain( ip, flag, print )/*
  727. ====================================*/
  728. ICELLPTR ip;
  729. int      flag;
  730. int     print;
  731. {
  732.    char *tmp;
  733.  
  734.    if( ip == NIL(ICELL) ) return(NIL(char));
  735.  
  736.    tmp = dump_inf_chain(ip->ic_parent, FALSE, FALSE);
  737.  
  738.    if( ip->ic_meta ) {
  739.       tmp = DmStrJoin(tmp, "(", -1, TRUE);
  740.       tmp = DmStrJoin(tmp, ip->ic_meta->CE_NAME, -1, TRUE);
  741.  
  742.       if( ip->ic_dir && !*ip->ic_dir ) {
  743.      tmp = DmStrJoin(tmp, "[", -1, TRUE);
  744.      if( strncmp(Makedir,ip->ic_dir, strlen(Makedir)) )
  745.         tmp = DmStrJoin(tmp, ip->ic_dir, -1, TRUE);
  746.      else
  747.         tmp = DmStrJoin(tmp, ip->ic_dir+strlen(Makedir)+1, -1, TRUE);
  748.      tmp = DmStrJoin(tmp, "]", -1, TRUE);
  749.       }
  750.       tmp = DmStrJoin(tmp, (ip->ic_name)?") -->":")", -1, TRUE);
  751.    }
  752.    
  753.    if( ip->ic_name ) tmp = DmStrApp( tmp, ip->ic_name );
  754.  
  755.    if( flag && ip->ic_meta->ce_prq) {
  756.       tmp = DmStrJoin(tmp, "(", -1, TRUE);
  757.       tmp = DmStrJoin(tmp, ip->ic_meta->ce_prq->cl_prq->CE_NAME, -1, TRUE);
  758.       tmp = DmStrJoin(tmp, ")", -1, TRUE);
  759.    }
  760.  
  761.    if( print ) {
  762.       fprintf( stderr, "%s:  %2d. %s\n", Pname, print, tmp );
  763.       FREE(tmp);
  764.       tmp = NIL(char);
  765.    }
  766.  
  767.    return(tmp);
  768. }
  769.  
  770.  
  771. #ifdef DBUG
  772. _dump_dfa_stack(dfas, dfa_stack)
  773. DFALINKPTR dfas;
  774. DFASETPTR  dfa_stack;
  775. {
  776.    register DFALINKPTR pdfa;
  777.    char      *tmp = NIL(char);
  778.    DFASETPTR ds;
  779.  
  780.    for( pdfa = dfas; pdfa != NIL(DFALINK); pdfa = pdfa->dl_next )
  781.       tmp = DmStrApp( tmp, pdfa->dl_meta->CE_NAME );
  782.  
  783.    tmp = DmStrApp( tmp, ":: {" );
  784.    for( ds = dfa_stack; ds != NIL(DFASET); ds = ds->df_next ) {
  785.       tmp = DmStrApp( tmp, "[" );
  786.       for( pdfa = ds->df_set; pdfa != NIL(DFALINK); pdfa = pdfa->dl_next )
  787.      tmp = DmStrApp( tmp, pdfa->dl_meta->CE_NAME );
  788.       tmp = DmStrApp( tmp, "]" );
  789.    }
  790.    tmp = DmStrApp( tmp, "}" );
  791.  
  792.    printf( "DFA set and stack contents:\n%s\n", tmp );
  793.    FREE(tmp);
  794. }
  795.  
  796.  
  797. _dump_iset( name, iset )
  798. char     *name;
  799. ICELLPTR iset;
  800. {
  801.    int cell = 0;
  802.  
  803.    printf( "**** ISET for %s\n", name );
  804.    for( ; iset != NIL(ICELL); iset = iset->ic_next ){
  805.       printf( "cell %d\n", cell++ );
  806.       if( iset->ic_meta )
  807.      printf( "edge: %s --> %s\n", iset->ic_meta->CE_NAME,
  808.          iset->ic_meta->ce_prq ?
  809.          iset->ic_meta->ce_prq->cl_prq->CE_NAME :
  810.          "(nil)" );
  811.       else
  812.      printf( "edge: (nil)\n" );
  813.  
  814.       if( iset->ic_dfa )
  815.      printf( "dfa: %s\n", iset->ic_dfa->dl_meta->CE_NAME );
  816.       else
  817.      printf( "dfa: (nil)\n" );
  818.  
  819.       printf( "sdr: %04x\n", iset->ic_setdirroot );
  820.       _dump_dfa_stack(iset->ic_dfastack.df_set, &iset->ic_dfastack);
  821.  
  822.       printf( "dmax: %d\n", iset->ic_dmax );
  823.       printf( "name: %s\n", iset->ic_name );
  824.       printf( "dir:  %s\n", iset->ic_dir?iset->ic_dir:"(nil)" );
  825.  
  826.       printf( "parent: " );
  827.       if( iset->ic_parent )
  828.     if( iset->ic_parent->ic_meta )
  829.        printf( "%s --> %s\n",
  830.                iset->ic_parent->ic_meta->CE_NAME,
  831.            iset->ic_parent->ic_meta->ce_prq ?
  832.            iset->ic_parent->ic_meta->ce_prq->cl_prq->CE_NAME :
  833.            "(nil)" );
  834.     else
  835.        printf( "(nil)\n" );
  836.       else
  837.      printf( "(nil)\n" );
  838.    }
  839.    printf( "==================================\n" );
  840. }
  841. #endif
  842.