home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DMAKE37S.ZIP / DMAKE / INFER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-06  |  23.6 KB  |  820 lines

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