summaryrefslogtreecommitdiff
path: root/dmake/infer.c
blob: 9f160d3ced92682dde28b7df4a3ebe5db6e0f4df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/* $RCSfile: infer.c,v $
-- $Revision: 1.8 $
-- last change: $Author: ihi $ $Date: 2007-10-15 15:39:49 $
--
-- SYNOPSIS
--      Infer how to make a target.
--
-- DESCRIPTION
--  This file contains the code to infer a recipe, and possibly some new
--  prerequisites for a target which dmake does not know how to make, or
--  has no explicit recipe.
--
--  The inference fails if no path through the inference graph can be
--  found by which we can make the target.
--
-- AUTHOR
--      Dennis Vadura, dvadura@dmake.wticorp.com
--
-- WWW
--      http://dmake.wticorp.com/
--
-- COPYRIGHT
--      Copyright (c) 1996,1997 by WTI Corp.  All rights reserved.
--
--      This program is NOT free software; you can redistribute it and/or
--      modify it under the terms of the Software License Agreement Provided
--      in the file <distribution-root>/readme/license.txt.
--
-- LOG
--      Use cvs log to obtain detailed change logs.
*/

#include "extern.h"

/* attributes that get transfered from the % start cell to the inferred
 * cells. */

#define A_TRANSFER  (A_EPILOG | A_PRECIOUS | A_SILENT | A_SHELL | A_SETDIR |\
             A_SEQ | A_LIBRARY | A_IGNORE | A_PROLOG | A_SWAP |\
             A_PHONY | A_NOSTATE )


/* Define local static functions */
static DFALINKPTR dfa_subset  ANSI((DFALINKPTR, DFASETPTR));
static void       free_dfas   ANSI((DFALINKPTR));
static int    count_dots  ANSI((char *));
static char *     buildname   ANSI((char *, char *, char *));
static void   free_icells ANSI((void));
static ICELLPTR   union_iset  ANSI((ICELLPTR, ICELLPTR));
static ICELLPTR   add_iset    ANSI((ICELLPTR,ICELLPTR,CELLPTR,DFALINKPTR,
                     CELLPTR,int,int,char *,char *, int));
static ICELLPTR   derive_prerequisites ANSI((ICELLPTR, ICELLPTR *));
static char *     dump_inf_chain ANSI((ICELLPTR, int, int));

#ifdef DBUG
static void   _dump_dfa_stack ANSI((DFALINKPTR, DFASETPTR));
static void   _dump_iset ANSI(( char *, ICELLPTR ));
#endif


PUBLIC void
Infer_recipe( cp, setdirroot )/*
================================
   Perform a breadth-first search of the inference graph and return if
   possible an inferred set of prerequisites for making the current target. */
CELLPTR cp;
CELLPTR setdirroot;
{
  ICELLPTR nomatch, match;

  DB_ENTER("Infer_recipe");

  if( cp->ce_attr & A_NOINFER ) {DB_VOID_RETURN;}

  DB_PRINT("inf", ("Inferring rule for [%s]", cp->CE_NAME));

  match = NIL(ICELL);
  {
    char *tmp;
    nomatch = add_iset( NIL(ICELL), NIL(ICELL), NIL(CELL), NIL(DFALINK),
                        setdirroot, Prep+count_dots(cp->CE_NAME), 0,
                        tmp = DmStrDup(cp->CE_NAME), NIL(char),
                        cp->ce_time != (time_t)0L);
    FREE(tmp);
  }

  /* Make sure we try whole heartedly to infer at least one suffix */
  if( nomatch->ic_dmax == 0 ) ++nomatch->ic_dmax;

  DB_EXECUTE( "inf", _dump_iset("nomatch",nomatch); );

  /* If nomatch is non-empty there was no match with an existing
   * prerrequisite, try to derive one. */
  while( nomatch != NIL(ICELL) ) {
    ICELLPTR new_nomatch = NIL(ICELL);
    ICELLPTR ic, pmatch, mmatch;
    CELLPTR  prereq;

    for( ic=nomatch; ic != NIL(ICELL); ic=ic->ic_next ) {
      int ipush = FALSE;

      if( ic->ic_dir ) ipush = Push_dir(ic->ic_dir, ic->ic_name, FALSE);
      match = union_iset(match, derive_prerequisites(ic, &new_nomatch));
      if( ipush ) Pop_dir(FALSE);
    }

    DB_EXECUTE( "inf", _dump_iset("match",match); );
    DB_EXECUTE( "inf", _dump_iset("nomatch",new_nomatch); );

    /* We have now deduced the two sets MATCH and NOMATCH.  MATCH holds the
     * set of edges that we encountered that matched.  If this set is empty
     * then we can apply transitive closure (if enabled) to the elements of
     * NOMATCH to see if we can find some other method to make the target.
     *
     * If MATCH is non-empty, we have found a method for making the target.
     * It is the shortest method for doing so (ie. uses fewest number of
     * steps).  If MATCH contains more than one element then we have a
     * possible ambiguity.
     */
    if( match == NIL(ICELL) ) {
      nomatch = new_nomatch;

      /* Skip the rest and try one level deeper. */
      if( Transitive ) continue;

      goto all_done;
    }

    /* Ok, we have a set of possible matches in MATCH, we should check the
     * set for ambiguity.  If more than one inference path exists of the
     * same depth, then we may issue an ambiguous inference error message.
     *
     * The message is suppressed if MATCH contains two elements and one of
     * them is the empty-prerequisite-rule.  In this case we ignore the
     * ambiguity and take the rule that infers the prerequisite.
     *
     * Also if there are any chains that rely on a non-existant prerequisite
     * that may get made because it has a recipe then we prefer any that
     * rely on existing final prerequisites over those that we have to make.
     */

    /* Split out those that have to be made from those that end in
     * prerequisites that already exist. */
    pmatch = mmatch = NIL(ICELL);
    for(; match; match = ic ) {
      /* This loop checks all possible matches. */
      DB_PRINT("inf", ("Target [%s] : prerequisite [%s]",
                       match->ic_meta->CE_NAME, match->ic_name));

      ic = match->ic_next;
      match->ic_next = NIL(ICELL);

      if( match->ic_exists )
        pmatch = union_iset(pmatch, match);
      else
        mmatch = union_iset(mmatch, match);
    }

    /* Prefer %-targets with existing prerequisites. */
    if( pmatch )
      match = pmatch;
    else
      match = mmatch;

    /* Make sure it is unique. It would be easy to check
     * match->ic_meta->ce_prq for existence and prefer no prerequisites
     * over prerequisites that are present, but we are currently not
     * doing it. */
    if( match->ic_next != NIL(ICELL) ) {
      int count = 1;

      Warning( "Ambiguous inference chains for target '%s'", cp->CE_NAME );
      for( ic=match; ic; ic=ic->ic_next )
        (void) dump_inf_chain(ic, TRUE, count++);
      Warning( "First matching rule is chosen.");
    }

    /* MATCH now points at the derived prerequisite chain(s).  We must now
     * take cp, and construct the correct graph so that the make may
     * proceed.  */

    /* The folowing shows only the first element, i.e. the last matching
     * recipe that was found.  */
    if( Verbose & V_INFER ) {
      char *tmp = dump_inf_chain(match, TRUE, FALSE);
      printf("%s:  Inferring prerequistes and recipes using:\n%s:  ... %s\n",
             Pname, Pname, tmp );
      FREE(tmp);
    }

    pmatch = NIL(ICELL);
    prereq = NIL(CELL);

    /* This loop treats the inferred targets last to first. */
    while( match ) {
      CELLPTR infcell=NIL(CELL);

      /* Compute the inferred prerequisite first. */
      if( match->ic_name ) {
        if( match->ic_meta )
          infcell = Def_cell( match->ic_name );
        else
          infcell = cp;

        infcell->ce_flag |= F_TARGET;

        if( infcell != cp ) {
          infcell->ce_flag |= F_INFER|F_REMOVE;
          DB_PRINT("remove", ("Mark for deletion [%s]",
                              infcell->CE_NAME));
        }

        if( !match->ic_flag )
          infcell->ce_attr |= A_NOINFER;
      }

      /* Add global prerequisites from previous rule if there are any and
       * the recipe. */
      if( pmatch ) {
        CELLPTR imeta = pmatch->ic_meta;
        LINKPTR lp;

        DB_PRINT("inf", ("%%-target [%s] - infered target [%s]\n",
                         imeta->CE_NAME, infcell->CE_NAME));

        infcell->ce_per   = pmatch->ic_dfa->dl_per;
        infcell->ce_attr |= (imeta->ce_attr & A_TRANSFER);

        /* The .PHONY mechanism relies on having phony targets not
         * being STATed and having a zero time stamp. While inferring
         * the this target it might have been created and stated
         * therefore these values need to be reset. */
        if( infcell->ce_attr & A_PHONY ){
          infcell->ce_time =  0L;
          infcell->ce_flag &= ~F_STAT;
        }

        if( !(infcell->ce_flag & F_RULES) ) {
          infcell->ce_flag |= (imeta->ce_flag&(F_SINGLE|F_GROUP))|F_RULES;
          infcell->ce_recipe = imeta->ce_recipe;
        }

        /* Add any conditional macro definitions that may be associated
         * with the inferred cell. */
        if (imeta->ce_cond != NIL(STRING)) {
          STRINGPTR sp,last;

          last = infcell->ce_cond;
          for(sp=imeta->ce_cond; sp; sp=sp->st_next) {
            STRINGPTR new;
            TALLOC(new, 1, STRING);
            new->st_string = DmStrDup(sp->st_string);
            if(last)
              last->st_next = new;
            else
              infcell->ce_cond = new;
            last = new;
          }
        }

        pmatch->ic_dfa->dl_per = NIL(char);

        /* If infcell already had a .SETDIR directory set then modify it
         * based on whether it was the original cell or some intermediary. */
        if( imeta->ce_dir ) {
          if( infcell->ce_dir && infcell == cp ) {
            /* cp->ce_dir was set and we have pushed the directory prior
             * to calling this routine.
             * We build a new path by appending imeta->ce_dir to the
             * current directory of the original cell.
             * We should therefore pop it and push the new concatenated
             * directory required by the inference.
             * This leaks memory as cp->ce_dir is not freed before
             * setting the new the new infcell->ce_dir value but as
             * the pointer could be a `A_POOL` member we accept this. */
            infcell->ce_dir = DmStrDup(Build_path(infcell->ce_dir,
                                                  imeta->ce_dir));
          }
          else {
            /* Inherit a copy of the .SETDIR value. Use a copy because
             * the original could have been freed in the meantime
             * in Make() by the FREE() before _pool_lookup(). This can
             * also leak if infcell->ce_dir was set before. */
            infcell->ce_dir = DmStrDup(imeta->ce_dir);
          }
        }

        for( lp=imeta->ce_indprq; lp != NIL(LINK); lp=lp->cl_next ) {
          char    *name = lp->cl_prq->CE_NAME;
          CELLPTR tcp;

          name = buildname( cp->CE_NAME, name, infcell->ce_per );
          tcp  = Def_cell( name );
          tcp->ce_flag |= F_REMOVE;
          Add_prerequisite( infcell, tcp, FALSE, FALSE );

          if( Verbose & V_INFER )
            printf( "%s:  Inferred indirect prerequisite [%s]\n",
                    Pname, name );
          FREE(name);
        }
      }

      /* Add the previous cell as the prerequisite */
      if( prereq )
        (Add_prerequisite(infcell,prereq,FALSE,FALSE))->cl_flag |=F_TARGET;

      pmatch = match;   /* Previous member in inference chain ... */
      prereq = infcell; /* is a prerequisite to the next match.   */
      /* ip->ic_parent is the next target in the inference chain to be
       * build. If it is empty we are done. */
      match  = match->ic_parent;
    }

    DB_PRINT("inf", ("Terminated due to a match"));
    break;
  }

 all_done:
  free_icells();

  DB_VOID_RETURN;
}


static ICELLPTR
derive_prerequisites( ic, nnmp )/*
===================================
   Take a cell and derive a set of prerequisites from the cell.  Categorize
   them into those that MATCH (ie. those that we found in the file system),
   and those that do not match NOMATCH that we may possibly have a look at
   later.  When we process the next level of the breadth-first search.

   Once MATCH is non-empty we will stop inserting elements into NOMATCH
   since we know that either MATCH is successful and unique or it will
   issue an ambiguity error.  We will never go on to look at elements
   in NOMATCH after wards. */
ICELLPTR ic;
ICELLPTR *nnmp;
{
   ICELLPTR   match = NIL(ICELL);
   DFALINKPTR pdfa;
   DFALINKPTR dfas;

   DB_ENTER("derive_prerequisites");

   DB_PRINT("inf", ("for [%s]\n", ic->ic_name));

   /* If none of the inference nodes match then forget about the inference.
    * The user did not tell us how to make such a target.  We also stop the
    * Inference if the new set of DFA's is a proper subset of a previous
    * subset and it's PREP counts exceed the value of Prep.
    */
   dfas = dfa_subset( Match_dfa(ic->ic_name), &ic->ic_dfastack );

   DB_EXECUTE("inf", _dump_dfa_stack(dfas, &ic->ic_dfastack); );

   /* Ok, we have nothing here to work with so return an empty cell. */
   if( dfas == NIL(DFALINK) ) {
      DB_PRINT( "mem", ("%s:<- mem %ld",ic->ic_name, (long)coreleft()));
      DB_PRINT( "inf", ("<<< Exit, no dfas, cp = %04x", NIL(CELL)) );
      DB_RETURN( NIL(ICELL) );
   }

   /* Save the dfas, we are going to use on the stack for this cell. */
   ic->ic_dfastack.df_set = dfas;

   /* Run through the %-meta cells, build the prerequisite cells.  For each
    * %-meta go through it's list of edges and try to use each in turn to
    * deduce a likely prerequisite.  We perform a breadth-first search
    * matching the first path that results in a unique method for making the
    * target. */
   for( pdfa = dfas; pdfa != NIL(DFALINK); pdfa = pdfa->dl_next ) {
      LINK tl;
      LINKPTR edge;
      CELLPTR pmeta;

      pmeta = pdfa->dl_meta;
      DB_PRINT( "inf", ("Using dfa:  [%s]", pmeta->CE_NAME) );

      /* If the %-meta is a singleton meta then deal with it differently from
       * the case when it is a bunch of %-meta's found on the original entries
       * prerequisite list. */
      if( pmeta->ce_flag & F_MULTI )
     edge = pmeta->ce_prq;
      else {
     tl.cl_prq = pmeta;
     tl.cl_next = NIL(LINK);
     edge = &tl;
      }

      /* Now run through the list of prerequisite edge's for the %-meta. */
      for( ; edge != NIL(LINK); edge = edge->cl_next ) {
     HASHPTR  thp = 0;      /* temporary hash table pointer     */
     HASH     iprqh;    /* hash cell for new prerequisite   */
     CELL     iprq;     /* inferred prerequisite to look for    */
     CELLPTR  idirroot; /* Inferred prerequisite root       */
     CELLPTR  nidirroot;    /* Inferred prerequisite root       */
     STRINGPTR ircp = 0;    /* Inferred prerequisites recipe    */
     char     *idir;    /* directory to CD to.          */
     int      ipush = 0;    /* flag for push on inferred prereq     */
     char     *name = NIL(char);        /* prerequisite name    */
     CELLPTR  meta = edge->cl_prq;
     int      trans;
     int      noinf;
     int      exists;

     /* Name of the prerequisite, can be empty. */
     if( meta->ce_prq )
        name = meta->ce_prq->cl_prq->CE_NAME;

     DB_PRINT( "inf", ("Trying edge from [%s] to [%s] for [%s]",
           meta->CE_NAME, name?name:"(nil)", ic->ic_name) );

     /* Set the temp CELL used for building prerequisite candidates to
      * all zero so that we don't have to keep initializing all the
      * fields. */
     {
        register char *s = (char *) &iprq;
        register int   n = sizeof(CELL);
        while( n ) { *s++ = '\0'; n--; }
     }

     nidirroot = idirroot = ic->ic_setdirroot;
     iprq.ce_name = &iprqh;

     if( name ) {
        /* Build the prerequisite name from the %-meta prerequisite given
         * for the %-meta rule. */
        int dmax_fix;
        iprqh.ht_name = buildname( ic->ic_name, name, pdfa->dl_per );
        if((dmax_fix = (count_dots(name)-count_dots(meta->CE_NAME))) < 0)
           dmax_fix = 0;

        if( !strcmp(ic->ic_name, iprqh.ht_name) ||
        (count_dots(iprqh.ht_name) > ic->ic_dmax + dmax_fix) ) {
           FREE( iprqh.ht_name );
           continue;
        }

        DB_PRINT( "inf", ("Checking prerequisite [%s]", iprqh.ht_name) );

        /* See if the prerequisite CELL has been previously defined.  If
         * it has, then make a copy of it into iprq, and use it to try
         * the inference.  We make the copy so that we don't modify the
         * stat of the inferred cell if the inference fails.
         */
        thp = Get_name( iprqh.ht_name, Defs, FALSE );
        if(thp != NIL(HASH)) {
           iprq = *thp->CP_OWNR;
           /* Check if a recipe for this target exists. Targets with F_MULTI
        * set need each cell checked for existing recipes.
        */
           if( iprq.ce_flag & F_MULTI ) {
          /* Walk through all cells of this target. */
          LINKPTR mtcp = iprq.ce_prq;
          ircp = NIL(STRING);
          for( ; mtcp != NIL(LINK); mtcp = mtcp->cl_next ) {
             /* If a recipe is found stop searching and set ircp to that result.
              * ircp is not used but only checked if it is set.
              */
             if( mtcp->cl_prq->ce_recipe != NIL(STRING) ) {
            ircp = mtcp->cl_prq->ce_recipe;
            break;
             }
          }
           }
           else
          ircp = iprq.ce_recipe;
        }
        else
           ircp = NIL(STRING);
     }
     else
        iprqh.ht_name = NIL(char);


     /* If the %-meta has a .SETDIR set then we change to the new
      * directory prior to performing the stat of the new prerequisite.
      * If the change of directory fails then the rule is droped from
      * further consideration.
      */
     if( iprq.ce_dir ) {
        if( (ipush = Push_dir(iprq.ce_dir, iprqh.ht_name, TRUE)) != 0 ) {
           nidirroot = thp->CP_OWNR;
           idir      = Pwd;
        }
        else {
           if( iprqh.ht_name ) FREE( iprqh.ht_name );
           continue;
        }
     }
     else
        idir = NIL(char);


     /* Stat the inferred prerequisite.
      */
     if( name ) {
        if( Verbose & V_INFER )
           printf( "%s:  Trying prerequisite [%s] for [%s]\n", Pname,
               iprqh.ht_name, ic->ic_name );

        /* irpq is a temporary target cell, a stat will not be remembered. */
        if( !(iprq.ce_flag & F_STAT) ) Stat_target(&iprq, FALSE, FALSE);
     }


     /* If the STAT succeeded or if the prerequisite has a recipe for
      * making it then it's a match and a candidate for getting infered.
      * Otherwise it is not a match, and we cannot yet tell if it is
      * going to be a successful path to follow, so we save it for
      * later consideration.
      */
     noinf = ((Glob_attr)&A_NOINFER);
     if( meta->ce_prq )
        noinf |= ((meta->ce_prq->cl_prq->ce_attr)&A_NOINFER);
     trans = Transitive || !noinf;

     /* If no prereq is given treat it as if it is existing. */
     exists = (iprq.ce_time != (time_t)0L) || (name == NIL(char));

     if( exists || (ircp != NIL(STRING)) || !name ) {
        match = add_iset( match, ic, meta, pdfa, idirroot, ic->ic_dmax,
                   trans, iprq.ce_name->ht_name, idir, exists );
        DB_PRINT("inf",("Added to MATCH %s",iprq.ce_name->ht_name));
     }
     else if( !noinf && match == NIL(ICELL) ) {
        *nnmp = add_iset( *nnmp, ic, meta, pdfa, nidirroot, ic->ic_dmax,
                   trans, iprq.ce_name->ht_name, idir, exists );
        DB_PRINT("inf",("Added to NOMATCH %s",iprq.ce_name->ht_name));
     }

     /* If we pushed a directory for the inferred prerequisite then
      * pop it.
      */
     if( ipush ) Pop_dir(FALSE);
     if( iprqh.ht_name ) FREE(iprqh.ht_name);
      }
   }

   DB_RETURN(match);
}


static char *
buildname( tg, meta, per )/*
============================
   Replace '%' with per in meta. Expand the result and return it. */
char *tg; /* Current target name. */
char *meta;
char *per;
{
   char    *name;

   name = Apply_edit( meta, "%", per, FALSE, FALSE );
   /* Handle infered dynamic prerequisites. */
   if( strchr(name, '$') ) {
      HASHPTR m_at;
      char *tmp;

      /* Set $@ so that a Expand() can use it and remove it afterwards. */
      /* Is $@ already expanded? FIXME: Remove this check later. */
      if( *DmStrPbrk( tg, "${}" ) != '\0' )
     Fatal("$@ [%s] not fully expanded!", tg);
      m_at = Def_macro( "@", DO_WINPATH(tg), M_MULTI|M_EXPANDED );
      tmp = Expand( name );

      if( m_at->ht_value != NIL(char) ) {
     FREE( m_at->ht_value );
     m_at->ht_value = NIL(char);
      }

      /* Free name if Apply_edit() did something. */
      if( name != meta ) FREE( name );
      name = tmp;
   }
   else if( name == meta )
      name = DmStrDup( name );

   return(name);
}


static DFALINKPTR
dfa_subset( pdfa, stack )/*
============================
   This is the valid DFA subset computation.  Whenever a CELL has a Match_dfa
   subset computed this algorithm is run to see if any of the previously
   computed sets on the DFA stack are proper subsets of the new set.  If they
   are, then any elements of the matching subset whose Prep counts exceed
   the allowed maximum given by Prep are removed from the computed DFA set,
   and hence from consideration, thereby cutting off the cycle in the
   inference graph. */
DFALINKPTR     pdfa;
register DFASETPTR stack;
{
   register DFALINKPTR element;
   DFALINKPTR          nelement;

   DB_ENTER( "dfa_subset" );

   DB_PRINT("inf",("Computing DFA subset, PREP = %d",Prep));
   DB_EXECUTE("inf", _dump_dfa_stack(pdfa, stack); );

   for(; pdfa != NIL(DFALINK) && stack != NIL(DFASET); stack = stack->df_next) {
      int subset = TRUE;

      for( element=stack->df_set; subset && element != NIL(DFALINK);
           element=element->dl_next ) {
         register DFALINKPTR subel;

     for( subel = pdfa;
          subel != NIL(DFALINK) && (subel->dl_meta != element->dl_meta);
          subel = subel->dl_next );

     DB_PRINT("inf",("Looking for %s, (%s)",element->dl_meta->CE_NAME,
             (subel != NIL(DFALINK))?"succ":"fail"));

     if( (subset = (subel != NIL(DFALINK))) != 0 )
        element->dl_member = subel;
      }

      if( subset )
     for( element=stack->df_set; element != NIL(DFALINK);
          element=element->dl_next ) {
        DFALINKPTR mem = element->dl_member;
        int        npr = element->dl_prep + 1;

        if( npr > Prep )
           mem->dl_delete++;
        else
           mem->dl_prep = npr;
     }
   }

   for( element = pdfa; element != NIL(DFALINK); element = nelement ) {
      nelement = element->dl_next;

      if( element->dl_delete ) {
     /* A member of the subset has a PREP count equal to PREP, so
      * it should not be considered further in the inference, hence
      * we remove it from the doubly linked set list */
     if( element == pdfa )
        pdfa = element->dl_next;
     else
        element->dl_prev->dl_next = element->dl_next;

     if( element->dl_next != NIL(DFALINK) )
        element->dl_next->dl_prev = element->dl_prev;

     DB_PRINT("inf", ("deleting dfa [%s]", element->dl_meta->CE_NAME));
     FREE( element->dl_per );
     FREE( element );
      }
   }

   DB_RETURN( pdfa );
}



static void
free_dfas( chain )/*
=====================
   Free the list of DFA's constructed by Match_dfa, and linked together by
   LINK cells.  FREE the % value as well, as long as it isn't NIL. */
DFALINKPTR chain;
{
   register DFALINKPTR tl;

   DB_ENTER( "free_dfas" );

   for( tl=chain; tl != NIL(DFALINK); chain = tl ) {
      tl = tl->dl_next;

      DB_PRINT( "inf", ("Freeing DFA [%s], %% = [%s]", chain->dl_meta->CE_NAME,
                chain->dl_per) );

      if( chain->dl_per != NIL(char) ) FREE( chain->dl_per );
      FREE( chain );
   }

   DB_VOID_RETURN;
}


static int
count_dots( name )/*
=====================*/
char *name;
{
   register char *p;
   register int  i = 0;

   for( p = name; *p; p++ ) if(*p == '.') i++;

   return( i );
}


static ICELLPTR _icells = NIL(ICELL);
#ifdef DBUG
static int _icell_cost = 0;
#endif

static ICELLPTR
add_iset( iset, parent, meta, dfa, setdirroot, dmax, noinf, name, dir, exists)
ICELLPTR   iset;
ICELLPTR   parent;
CELLPTR    meta;
DFALINKPTR dfa;
CELLPTR    setdirroot;
int    dmax;
int    noinf;
char      *name;
char      *dir;
int    exists;
{
   ICELLPTR icell;

   DB_ENTER("add_iset");
   TALLOC(icell, 1, ICELL);

   DB_EXECUTE("inf", _icell_cost+=(sizeof(ICELL)+strlen(dir?dir:"")+strlen(name?name:"")+2););

   icell->ic_meta = meta;
   icell->ic_dfa  = dfa;
   icell->ic_setdirroot = setdirroot;

   if( parent ) icell->ic_dfastack.df_next = &parent->ic_dfastack;

   icell->ic_dmax = dmax;
   icell->ic_dir = DmStrDup(dir);
   icell->ic_name = DmStrDup(name);
   icell->ic_parent = parent;
   icell->ic_next = iset;
   icell->ic_flag = noinf;
   icell->ic_exists = exists;

   icell->ic_link = _icells;
   _icells = icell;

   DB_RETURN(icell);
}


static void
free_icells()
{
   register ICELLPTR ic;

   DB_ENTER("free_icells");

   for( ; _icells; _icells = ic ) {
      ic = _icells->ic_link;

      free_dfas(_icells->ic_dfastack.df_set);
      if( _icells->ic_dir ) FREE(_icells->ic_dir);
      if( _icells->ic_name) FREE(_icells->ic_name);
      FREE(_icells);
   }

   DB_PRINT("inf",("Used %d memory for icells",_icell_cost));
   DB_EXECUTE("inf", _icell_cost=0; );

   DB_VOID_RETURN;
}


static ICELLPTR
union_iset( iset, uset )
ICELLPTR iset;
ICELLPTR uset;
{
   register ICELLPTR ic;

   if( iset == NIL(ICELL) ) return(uset);

   for( ic=iset; ic->ic_next != NIL(ICELL); ic=ic->ic_next );
   ic->ic_next = uset;

   return(iset);
}


static char *
dump_inf_chain( ip, flag, print )/*
===================================
Return string with infered prerequisites.
flag == TRUE adds the top of the chain.
print == TRUE prints to screen with number "print" and returns NULL. */
ICELLPTR ip;
int      flag;
int  print;
{
   char *tmp;

   if( ip == NIL(ICELL) ) return(NIL(char));

   /* ip->ic_parent is the target to be build after ip. */
   tmp = dump_inf_chain(ip->ic_parent, FALSE, FALSE);

   if( ip->ic_meta ) {
      tmp = DmStrJoin(tmp, "(", -1, TRUE);
      tmp = DmStrJoin(tmp, ip->ic_meta->CE_NAME, -1, TRUE);

      if( ip->ic_dir && !*ip->ic_dir ) {
     tmp = DmStrJoin(tmp, "[", -1, TRUE);
     if( strncmp(Makedir,ip->ic_dir, strlen(Makedir)) )
        tmp = DmStrJoin(tmp, ip->ic_dir, -1, TRUE);
     else
        tmp = DmStrJoin(tmp, ip->ic_dir+strlen(Makedir)+1, -1, TRUE);
     tmp = DmStrJoin(tmp, "]", -1, TRUE);
      }
      tmp = DmStrJoin(tmp, (ip->ic_name)?") -->":")", -1, TRUE);
   }

   if( ip->ic_name ) tmp = DmStrApp( tmp, ip->ic_name );

   if( flag && ip->ic_meta->ce_prq) {
      tmp = DmStrJoin(tmp, "(", -1, TRUE);
      tmp = DmStrJoin(tmp, ip->ic_meta->ce_prq->cl_prq->CE_NAME, -1, TRUE);
      tmp = DmStrJoin(tmp, ")", -1, TRUE);
   }

   if( print ) {
      fprintf( stderr, "%s:  %2d. %s\n", Pname, print, tmp );
      FREE(tmp);
      tmp = NIL(char);
   }

   return(tmp);
}


#ifdef DBUG
static void
_dump_dfa_stack(dfas, dfa_stack)
DFALINKPTR dfas;
DFASETPTR  dfa_stack;
{
   register DFALINKPTR pdfa;
   char      *tmp = NIL(char);
   DFASETPTR ds;

   for( pdfa = dfas; pdfa != NIL(DFALINK); pdfa = pdfa->dl_next )
      tmp = DmStrApp( tmp, pdfa->dl_meta->CE_NAME );

   tmp = DmStrApp( tmp, ":: {" );
   for( ds = dfa_stack; ds != NIL(DFASET); ds = ds->df_next ) {
      tmp = DmStrApp( tmp, "[" );
      for( pdfa = ds->df_set; pdfa != NIL(DFALINK); pdfa = pdfa->dl_next )
     tmp = DmStrApp( tmp, pdfa->dl_meta->CE_NAME );
      tmp = DmStrApp( tmp, "]" );
   }
   tmp = DmStrApp( tmp, "}" );

   printf( "DFA set and stack contents:\n%s\n", tmp );
   FREE(tmp);
}


static void
_dump_iset( name, iset )
char     *name;
ICELLPTR iset;
{
   int cell = 0;

   printf( "**** ISET for %s\n", name );
   for( ; iset != NIL(ICELL); iset = iset->ic_next ){
      printf( "cell %d\n", cell++ );
      if( iset->ic_meta )
     printf( "edge: %s --> %s\n", iset->ic_meta->CE_NAME,
         iset->ic_meta->ce_prq ?
         iset->ic_meta->ce_prq->cl_prq->CE_NAME :
         "(nil)" );
      else
     printf( "edge: (nil)\n" );

      if( iset->ic_dfa )
     printf( "dfa: %s\n", iset->ic_dfa->dl_meta->CE_NAME );
      else
     printf( "dfa: (nil)\n" );

      printf( "sdr: %p\n", iset->ic_setdirroot );
      _dump_dfa_stack(iset->ic_dfastack.df_set, &iset->ic_dfastack);

      printf( "dmax: %d\n", iset->ic_dmax );
      printf( "name: %s\n", iset->ic_name );
      printf( "dir:  %s\n", iset->ic_dir?iset->ic_dir:"(nil)" );

      printf( "parent: " );
      if( iset->ic_parent )
    if( iset->ic_parent->ic_meta )
       printf( "%s --> %s\n",
               iset->ic_parent->ic_meta->CE_NAME,
           iset->ic_parent->ic_meta->ce_prq ?
           iset->ic_parent->ic_meta->ce_prq->cl_prq->CE_NAME :
           "(nil)" );
    else
       printf( "(nil)\n" );
      else
     printf( "(nil)\n" );
   }
   printf( "==================================\n" );
}
#endif