summaryrefslogtreecommitdiff
path: root/dmake/rulparse.c
blob: bc5b6dec81186f974890d443bf85b7a0baa54f0a (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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
/* $RCSfile: rulparse.c,v $
-- $Revision: 1.6 $
-- last change: $Author: rt $ $Date: 2004-09-08 16:07:25 $
--
-- SYNOPSIS
--      Perform semantic analysis on input
--
-- DESCRIPTION
--  This code performs semantic analysis on the input, and builds
--  the complex internal datastructure that is used to represent
--  the user makefile.
--
-- 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"

/* prototypes for local functions */
static  void    _add_global_prereq ANSI((CELLPTR));
static  int     _add_root ANSI((CELLPTR));
static  CELLPTR _build_graph ANSI((int, CELLPTR, CELLPTR));
static  char*   _build_meta ANSI((char*));
static  int _do_magic ANSI((int, char*, CELLPTR, CELLPTR, t_attr, char*));
static  void    _do_special ANSI((int, int, t_attr,char*,CELLPTR,CELLPTR,int*));
static  int _do_targets ANSI((int, t_attr, char*, CELLPTR, CELLPTR));
static  t_attr  _is_attribute ANSI((char*));
static  int _is_special ANSI((char*));
static  char*   _is_magic ANSI((char*));
static  int _is_percent ANSI((char*));
static  CELLPTR _make_multi ANSI((CELLPTR));
static  CELLPTR _replace_cell ANSI((CELLPTR,CELLPTR,CELLPTR));
static  void    _set_attributes ANSI((t_attr, char*, CELLPTR ));
static  void    _stick_at_head ANSI((CELLPTR, CELLPTR));
static  void    _set_global_attr ANSI((t_attr));


/* static variables that must persist across invocation of Parse_rule_def */
static CELLPTR    _sv_targets = NIL(CELL);
static STRINGPTR  _sv_rules   = NIL(STRING);
static STRINGPTR  _sv_crule   = NIL(STRING);
static CELLPTR    _sv_edgel   = NIL(CELL);
static LINKPTR    _sv_glb_prq = NIL(LINK);
static int    _sp_target  = FALSE;
static t_attr     _sv_attr;
static int        _sv_flag;
static int    _sv_op;
static char      *_sv_setdir;
static char   _sv_globprq_only = 0;

/* Define for global attribute mask */
#define A_GLOB   (A_PRECIOUS | A_SILENT | A_IGNORE | A_EPILOG | A_SWAP |\
          A_SHELL | A_PROLOG | A_NOINFER | A_SEQ | A_MKSARGS )


PUBLIC int
Parse_rule_def( state )/*
=========================
   Parse the rule definition contained in Buffer, and modify the state
   if appropriate.  The function returns 0, if the definition is found to
   be an illegal rule definition, and it returns 1 if it is a rule definition.
   */
int *state;
{
   TKSTR    input;      /* input string struct for token search   */
   CELLPTR  targets;    /* list of targets if any         */
   CELLPTR  prereq;     /* list of prereq if any          */
   CELLPTR  prereqtail; /* tail of prerequisite list          */
   CELLPTR  cp;     /* temporary cell pointer for list making */
   char     *result;    /* temporary storage for result       */
   char     *tok;       /* temporary pointer for tokens       */
   char         *set_dir;       /* value of setdir attribute              */
   char     *brk;       /* break char list for Get_token      */
   char         *firstrcp;      /* first recipe line, from ; in rule line */
   t_attr       attr;           /* sum of attribute flags for current tgts*/
   t_attr   at;     /* temp place to keep an attribute code   */
   int      op;     /* rule operator              */
   int      special;    /* indicate special targets in rule   */
   int      percent;    /* indicate percent rule target       */
   int      mixed_glob_prq; /* indicate mixed %-rule prereq possible  */

   DB_ENTER( "Parse_rule_def" );

   op         = 0;
   attr       = 0;
   special    = 0;
   percent    = 0;
   set_dir    = NIL( char );
   targets    = NIL(CELL);
   prereq     = NIL(CELL);
   prereqtail = NIL(CELL);
   mixed_glob_prq = 0;

   /* Check to see if the line is of the form:
    *    targets : prerequisites; first recipe line
    * If so remember the first_recipe part of the line. */

   firstrcp = strchr( Buffer, ';' );
   if( firstrcp != NIL( char ) ) {
      *firstrcp++ = 0;
      firstrcp = DmStrSpn( firstrcp, " \t" );
   }

   result = Expand( Buffer );
   for( brk=strchr(result,'\\'); brk != NIL(char); brk=strchr(brk,'\\') )
      if( brk[1] == '\n' )
     *brk = ' ';
      else
         brk++;

   DB_PRINT( "par", ("Scanning: [%s]", result) );

   SET_TOKEN( &input, result );
   brk = ":-^!|";
   Def_targets = TRUE;

   /* Scan the input rule line collecting targets, the operator, and any
    * prerequisites.  Stop when we run out of targets and prerequisites. */

   while( *(tok = Get_token( &input, brk, TRUE )) != '\0' )
      if( !op ) {
     /* we are scanning targets and attributes
      * check to see if token is an operator.  */

     op = Rule_op( tok );

     if( !op ) {
        /* define a new cell, or get old cell  */
        cp = Def_cell( tok );
        DB_PRINT( "par", ("tg_cell [%s]", tok) );

        if( (at = _is_attribute(tok)) != 0 ) {
           /* Logically OR the attributes specified into one main
            * ATTRIBUTE mask. */

           if( at == A_SETDIR ) {
              if( set_dir != NIL( char ) )
                 Warning( "Multiple .SETDIR attribute ignored" );
              else
                 set_dir = DmStrDup( tok );
           }

           attr |= at;
        }
        else {
           int tmp;

           tmp = _is_special( tok );
           if( _is_percent( tok ) ) percent++;

           if( percent )
          cp->ce_flag |= F_PERCENT;

           if( special )
              Fatal( "Special target must appear alone", tok );
           else if( !(cp->ce_flag & F_MARK) ) {
          /* Targets are kept in this list in lexically sorted order.
           * This allows for easy equality comparison of target
           * sets.*/
          CELLPTR prev,cur;
          for(prev=NIL(CELL),cur=targets;cur;prev=cur,cur=cur->ce_link)
             if(strcmp(cur->CE_NAME,cp->CE_NAME) > 0)
            break;

          cp->ce_link = cur;

          if (!prev)
             targets = cp;
          else
             prev->ce_link = cp;

          cp->ce_flag |= F_MARK | F_EXPLICIT;
          special = tmp;
           }
        }
     }
     else {
        /* found an operator so empty out break list
         * and clear mark bits on target list, setting them all to F_USED*/

        brk  = "";
        for( cp=targets; cp != NIL(CELL); cp=cp->ce_link ) {
           cp->ce_flag ^= F_MARK;
           cp->ce_flag |= F_USED;
        }

        Def_targets = FALSE;
     }
      }
      else {
         /* Scanning prerequisites so build the prerequisite list.  We use
          * F_MARK flag to make certain we have only a single copy of the
          * prerequisite in the list */

     cp = Def_cell( tok );

     if( _is_percent( tok ) ) {
        if( !percent && !attr )
           Fatal( "Syntax error in %% rule, missing %% target");
        mixed_glob_prq = 1;
     }

     if( cp->ce_flag & F_USED ) {
        if( cp->ce_attr & A_COMPOSITE )
           continue;
        else
           Fatal( "Detected circular dependency in graph at [%s]",
              cp->CE_NAME );
     }
         else if( !(cp->ce_flag & F_MARK) ) {
        DB_PRINT( "par", ("pq_cell [%s]", tok) );
        cp->ce_flag |= F_MARK;

        if( prereqtail == NIL(CELL) )   /* keep prereq's in order */
           prereq = cp;
        else
           prereqtail->ce_link = cp;

        prereqtail = cp;
        cp->ce_link = NIL(CELL);
     }
     else if( !(cp->ce_attr & A_LIBRARY) && (Verbose & V_WARNALL))
        Warning("Duplicate entry [%s] in prerequisite list",cp->CE_NAME);
      }

   /* Check to see if we have a percent rule that has only global
    * prerequisites.  If so then set the flag so that later on, we don't issue
    * an error if such targets supply an empty set of rules. */

   if( percent && !mixed_glob_prq && (prereq != NIL(CELL)) )
      _sv_globprq_only = 1;

   /* It's ok to have targets with attributes, and no prerequisites, but it's
    * not ok to have no targets and no attributes, or no operator */

   if( !op ) {
      CLEAR_TOKEN( &input );
      DB_PRINT( "par", ("Not a rule [%s]", Buffer) );
      DB_RETURN( 0 );
   }

   if( !attr && targets == NIL(CELL) ) {
      Fatal( "Missing targets or attributes in rule" );
      if( set_dir != NIL( char )) FREE( set_dir );
      DB_RETURN( 0 );
   }

   /* We have established we have a legal rules line, so we must process it.
    * In doing so we must handle any special targets.  Special targets must
    * appear alone possibly accompanied by attributes.
    * NOTE:  special != 0  ==> targets != NIL(CELL) */

   if( prereqtail != NIL(CELL) ) prereqtail->ce_link = NIL(CELL);

   /* Clear out MARK bits used in duplicate checking.  I originally wanted
    * to do this as the lists get processed but that got too error prone
    * so I bit the bullit and added these two loops. */

   for( cp=prereq;  cp != NIL(CELL); cp=cp->ce_link ) cp->ce_flag &= ~F_MARK;
   for( cp=targets; cp != NIL(CELL); cp=cp->ce_link ) cp->ce_flag &= ~F_USED;

   /* Check to see if the previous rule line was bound if, not the call
    * Bind_rules_to_targets to go and bind the line */

   if( _sv_rules != NIL(STRING) ) Bind_rules_to_targets( F_DEFAULT );

   /* Add the first recipe line to the list */
   if( firstrcp != NIL( char ) )
      Add_recipe_to_list( firstrcp, TRUE, FALSE );

   /* Save these prior to calling _do_targets, since _build_graph needs the
    * _sv_setdir value for matching edges. */
   _sv_op     = op;
   _sv_setdir = set_dir;

   if( special )
      _do_special( special, op, attr, set_dir, targets, prereq, state );
   else
      *state = _do_targets( op, attr, set_dir, targets, prereq );

   DB_RETURN( 1 );
}


PUBLIC int
Rule_op( op )/*
================
   Check the passed in op string and map it to one of the rule operators */
char *op;
{
   int ret = 0;

   DB_ENTER( "rule_op" );

   if( *op == TGT_DEP_SEP ) {
      ret = R_OP_CL;
      op++;

      /* All rule operations begin with a :, but may include any one of the
       * four modifiers.  In order for the rule to be properly mapped we must
       * check for each of the modifiers in turn, building up our return bit
       * string. */

      while( *op && ret )
         switch( *op ) {
        case ':': ret |= R_OP_DCL; op++; break;
        case '!': ret |= R_OP_BG;  op++; break;
        case '^': ret |= R_OP_UP;  op++; break;
        case '-': ret |= R_OP_MI;  op++; break;
        case '|': ret |= R_OP_OR;  op++; break;

        default : ret  = 0;  /* an invalid modifier, chuck whole string */
         }

      if( *op != '\0' ) ret = 0;
   }

   DB_RETURN( ret );
}


PUBLIC void
Add_recipe_to_list( rule, white_too, no_check )/*
=================================================
        Take the provided string and add it to the list of recipe lines
    we are saving to be added to the list of targets we have built
    previously.  If white_too == TRUE add the rule EVEN IF it contains only
        whitespace. */
char *rule;
int  white_too;
int  no_check;
{
   DB_ENTER( "Add_recipe_to_list" );

   if( rule != NIL( char ) && (*rule != '\0' || white_too) ) {
      DB_PRINT( "par", ("Adding recipe [%s]", rule) );
      _sv_crule = Def_recipe( rule, _sv_crule, white_too, no_check );

      if( _sv_rules == NIL(STRING) )
         _sv_rules = _sv_crule;
   }

   DB_VOID_RETURN;
}


PUBLIC void
Bind_rules_to_targets( flag )/*
===============================
        Take the rules we have defined and bind them with proper attributes
        to the targets that were previously defined in the parse.  The
        attributes that get passed here are merged with those that are were
        previously defined.  (namely F_SINGLE) */
int flag;
{
   CELLPTR tg;             /* pointer to current target in list */
   LINKPTR lp;         /* pointer to link cell      */
   int     magic;          /* TRUE if target is .xxx.yyy form   */
   int     tflag;          /* TRUE if we assigned targets here  */

   DB_ENTER( "Bind_rules_to_targets" );

   /* This line is needed since Parse may call us twice when the last
    * GROUP rule appears at the end of file.  In this case the rules
    * have already been bound and we want to ignore them. */

   if( _sv_targets == NIL(CELL) ) { DB_VOID_RETURN; }

   tflag  = FALSE;
   flag  |= (_sv_flag & F_SINGLE);
   flag  |= ((_sv_attr & A_GROUP) ? F_GROUP : 0);

   for( tg = _sv_targets; tg != NIL(CELL); tg = tg->ce_link ) {
      DB_PRINT( "par", ("Binding to %s, %04x", tg->CE_NAME, tg->ce_flag) );
      magic = tg->ce_flag & F_PERCENT;

#if 0
      /* Check to see if we had a rule of the form '%.o : a.h b.h ; xxx'
       * In which case we must build a NULL prq node to hold the recipe */
      if( _sv_globprq_only && (_sv_rules != NIL(STRING)) )
     _build_graph( _sv_op, tg, NIL(CELL) );
#endif

      /* NOTE:  For targets that are magic we ignore any previously defined
       *        rules.  ie. We throw away the old definition and use the new.*/
      if( !(tg->ce_flag & F_MULTI) && !magic && (tg->CE_RECIPE != NIL(STRING))
      && !_sp_target && (_sv_rules != NIL(STRING)) )
         Fatal( "Multiply defined recipe for target %s", tg->CE_NAME );

      if( (magic || _sp_target) && (_sv_rules == NIL(STRING)) &&
      !(tg->ce_flag & F_SPECIAL) && !_sv_globprq_only )
         Warning( "Empty recipe for special target %s", tg->CE_NAME );

      if( magic ) {
     CELLPTR ep;

     for( ep=_sv_edgel; ep != NIL(CELL); ep=ep->ce_link ) {
        _set_attributes( _sv_attr, _sv_setdir, ep );
        ep->ce_flag |= (F_TARGET|flag);

        if( _sv_rules != NIL(STRING) ) {
           ep->ce_recipe  = _sv_rules;
           ep->ce_indprq  = _sv_glb_prq;
        }
     }
      }
      else {
     tg->ce_attr |= _sv_attr;
     tg->ce_flag |= flag;

     if( _sv_rules != NIL(STRING) ) {
        tg->ce_recipe  = _sv_rules;
        tg->ce_flag   |= F_RULES | F_TARGET;

        /* Bind the current set of prerequisites as belonging to the
         * original recipe given for the target */
        for( lp=tg->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
          if( !(lp->cl_flag & F_USED) ) lp->cl_flag |= F_TARGET;
         }
     else for( lp=tg->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
        lp->cl_flag |= F_USED;
      }

      tflag |= _add_root(tg);
   }

   if( tflag ) Target = TRUE;
   if( _sv_setdir ) FREE(_sv_setdir);
   _sv_rules   = NIL(STRING);
   _sv_crule   = NIL(STRING);
   _sv_targets = NIL(CELL);
   _sv_glb_prq = NIL(LINK);
   _sv_edgel   = NIL(CELL);
   _sp_target  = FALSE;
   _sv_globprq_only = 0;

   DB_VOID_RETURN;
}



PUBLIC int
Set_group_attributes( list )/*
==============================
    Scan list looking for the standard @ and - (as in recipe line defs)
    and set the flags accordingly so that they apply when we bind the
    rules to the appropriate targets. */
char *list;
{
   int res = FALSE;

   if ( !((_sv_attr|Glob_attr)&A_IGNOREGROUP) ) {
      res = (*DmStrSpn(list,"@-%+ \t") == '[');
      if( res ) _sv_attr |= Rcp_attribute(list);
   }

   return(res);
}


static void
_do_special( special, op, attr, set_dir, target, prereq, state )/*
==================================================================
   Process a special target.  So far the only special targets we have
   are those recognized by the _is_special function.

   target is always only a single special target.

   NOTE:  For the cases of .IMPORT, and .INCLUDE, the cells created by the
         parser are never freed.  This is due to the fact that it is too much
      trouble to get them out of the hash table once they are defined, and
      if they are per chance used again it will be ok, anyway, since the
      cell is not really used by the code below.  */

int special;
int op;
t_attr  attr;
char    *set_dir;
CELLPTR target;
CELLPTR prereq;
int     *state;
{
   HASHPTR  hp;     /* pointer to macro def cell        */
   CELLPTR  cp;     /* temporary pointer into cells list    */
   CELLPTR  dp;     /* pointer to directory dir cell    */
   LINKPTR  lp;     /* pointer at prerequisite list     */
   char     *dir;       /* current dir to prepend       */
   char     *path;      /* resulting path to try to read    */
   char     *name;      /* File name for processing a .INCLUDE  */
   char     *tmp;       /* temporary string pointer     */
   FILE     *fil;       /* File descriptor returned by Openfile */

   DB_ENTER( "_do_special" );

   target->ce_flag = F_SPECIAL; /* mark the target as special */

   switch( special ) {
      case ST_EXPORT:
     for( ; prereq != NIL(CELL); prereq = prereq->ce_link ) {
        DB_PRINT( "par", ("Exporting [%s]", prereq->CE_NAME) );
        hp = GET_MACRO( prereq->CE_NAME );

        if( hp != NIL(HASH) ) {
           char *tmpstr = hp->ht_value;

           if( tmpstr == NIL(char) ) tmpstr = "";

           if( Write_env_string( prereq->CE_NAME, tmpstr ) != 0 )
          Warning( "Could not export %s", prereq->CE_NAME );
        }
     }
     break;

      /* Simply cause the parser to fail on the next input read */
      case ST_EXIT:
         Skip_to_eof = TRUE;
         break;

      case ST_IMPORT:
     for( ; prereq != NIL(CELL); prereq = prereq->ce_link ) {
        char *tmpstr;

        DB_PRINT( "par", ("Importing [%s]", prereq->CE_NAME) );

        if( strcmp(prereq->CE_NAME, ".EVERYTHING") == 0 ) {
           t_attr sattr = Glob_attr;
           Glob_attr |= A_SILENT;

           ReadEnvironment();

           Glob_attr = sattr;
        }
        else {
           tmpstr = Read_env_string( prereq->CE_NAME );

           if( tmpstr != NIL(char) )
          Def_macro(prereq->CE_NAME, tmpstr, M_EXPANDED|M_LITERAL);
           else
          if( !((Glob_attr | attr) & A_IGNORE) )
             Fatal("Imported macro `%s' not found",prereq->CE_NAME);
        }
     }

     attr &= ~A_IGNORE;
     break;

      case ST_INCLUDE:
      {
     int pushed     = FALSE;
     int first      = (attr & A_FIRST);
     int ignore     = (((Glob_attr | attr) & A_IGNORE) != 0);
     int found      = FALSE;
         int noinf      = (attr & A_NOINFER);
     LINKPTR prqlnk = NIL(LINK);
     LINKPTR prqlst = NIL(LINK);

     if( prereq == NIL(CELL) )  Fatal( "No .INCLUDE file(s) specified" );

     dp = Def_cell( ".INCLUDEDIRS" );

     if( (attr & A_SETDIR) && *(dir = strchr(set_dir, '=')+1) )
        pushed = Push_dir( dir, ".INCLUDE", ignore );

     for( cp=prereq; cp != NIL(CELL); cp = cp->ce_link ) {
        LINKPTR ltmp;
        TALLOC(ltmp, 1, LINK);
        ltmp->cl_prq = cp;

        if( prqlnk == NIL(LINK) )
           prqlst = ltmp;
        else
           prqlnk->cl_next = ltmp;

        prqlnk = ltmp;
     }

     for( ; prqlst != NIL(LINK); FREE(prqlst), prqlst=prqlnk ) {
        prqlnk = prqlst->cl_next;
        cp     = prqlst->cl_prq;
        name   = cp->CE_NAME;

        /* Leave this here, it ensures that prqlst gets propely free'd */
        if ( first && found )
           continue;

        if( *name == '<' ) {
           /* We have a file name enclosed in <....>
            * so get rid of the <> arround the file name */

           name++;
           if( (tmp = strrchr( name, '>' )) != NIL( char ) )
          *tmp = 0;

           if( If_root_path( name ) )
              fil = Openfile( name, FALSE, FALSE );
           else
          fil = NIL(FILE);
        }
        else
           fil = Openfile( name, FALSE, FALSE );

        if( fil == NIL(FILE) && !If_root_path( name ) ) { /*if true ==> not found in current dir*/

           /* Now we must scan the list of prerequisites for .INCLUDEDIRS
            * looking for the file in each of the specified directories.
        * if we don't find it then we issue an error.  The error
        * message is suppressed if the .IGNORE attribute of attr is
        * set.  If a file is found we call Parse on the file to
        * perform the parse and then continue on from where we left
        * off.  */

           for(lp=dp->CE_PRQ; lp && fil == NIL(FILE); lp=lp->cl_next) {
          dir  = lp->cl_prq->CE_NAME;
          if( strchr(dir, '$') ) dir = Expand(dir);
          path = Build_path( dir, name );

          DB_PRINT( "par", ("Trying to include [%s]", path) );

          fil = Openfile( path, FALSE, FALSE );
          if( dir != lp->cl_prq->CE_NAME ) FREE(dir);
           }
        }

        if (!noinf && fil == NIL(FILE)) {
           t_attr glob = Glob_attr;
           t_attr cattr = prqlst->cl_prq->ce_attr;

           prqlst->cl_next = NIL(LINK);
           Glob_attr |= (attr&A_IGNORE);
           prqlst->cl_prq->ce_attr &= ~A_FRINGE;

           fil = TryFiles(prqlst);

           Glob_attr = glob;
           prqlst->cl_prq->ce_attr |= (cattr & A_FRINGE);
        }

        if( fil != NIL(FILE) ) {
           Parse( fil );
           found = TRUE;
        }
        else if( !(ignore || first) )
           Fatal( "Include file %s, not found", name );
     }

     if ( !ignore && first && !found )
        Fatal( "No include file was found" );

     if( pushed ) Pop_dir(FALSE);
     attr &= ~(A_IGNORE|A_SETDIR|A_FIRST|A_NOINFER);
      }
      break;

      case ST_SOURCE:
      /* case ST_SUFFIXES: */
           if( prereq != NIL(CELL) )
        _do_targets( op & (R_OP_CL | R_OP_MI | R_OP_UP), attr, set_dir,
             target, prereq );
     else {
        /* The old semantics of .SOURCE were that an empty list of
         * prerequisites clears the .SOURCE list.  So we must implement
         * that here as a clearout prerequisite operation.  Since this is
         * a standard operation with the :- opcode we can simply call the
         * proper routine with the target cell and it should do the trick
         */

        if( op == R_OP_CL || (op & R_OP_MI) )
           Clear_prerequisites( target );
     }

     op &= ~(R_OP_MI | R_OP_UP);
     break;

      case ST_KEEP:
     if( Keep_state != NIL(char) ) break;
     Def_macro( ".KEEP_STATE", "_state.mk", M_EXPANDED );
     break;

      case ST_REST:
         /* The rest of the special targets can all take rules, as such they
      * must be able to affect the state of the parser. */

     {
        int s_targ = Target;

        Target     = TRUE;
        _sp_target = TRUE;
        *state     = _do_targets( op, attr, set_dir, target, prereq );
        Target     = s_targ;

        target->ce_flag |= F_TARGET;

        attr    = A_DEFAULT;
        op      = R_OP_CL;
     }
     break;

      default:break;
   }

   if( op   != R_OP_CL   ) Warning( "Modifier(s) for operator ignored" );
   if( attr != A_DEFAULT ) Warning( "Extra attributes ignored" );

   DB_VOID_RETURN;
}



static int
_do_targets( op, attr, set_dir, targets, prereq )/*
================================================= */
int op;
t_attr  attr;
char    *set_dir;
CELLPTR targets;
CELLPTR prereq;
{
   CELLPTR  tg1;        /* temporary target pointer     */
   CELLPTR  tp1;        /* temporary prerequisite pointer   */
   LINKPTR      prev_cell;  /* pointer for .UPDATEALL processing    */
   char     *p;     /* temporary char pointer       */
   int          tflag = FALSE;  /* set to TRUE if we add target to root */

   DB_ENTER( "_do_targets" );

   if( attr & A_UPDATEALL ) {
      if( targets == NIL(CELL) )
     Fatal( ".UPDATEALL attribute requires non-empty list of targets" );

      if (targets->ce_set == NIL(CELL)) {
     for(
        prev_cell=CeMeToo(targets),tg1=targets->ce_link;
        tg1 != NIL(CELL);
        tg1=tg1->ce_link
     ) {
        if (tg1->ce_set)
           Fatal( "Target [%s] appears on multiple .UPDATEALL lists",
              tg1->CE_NAME);
        tg1->ce_set = targets;
        TALLOC(prev_cell->cl_next, 1, LINK);
        prev_cell = prev_cell->cl_next;
        prev_cell->cl_prq = tg1;
     }
     targets->ce_set = targets;
      }
      else {
     LINKPTR ap;
     CELLPTR tp;

     tp = targets;
     ap = CeMeToo(targets);
     while (ap && tp && ap->cl_prq == tp && tp->ce_set == targets) {
        ap = ap->cl_next;
        tp = tp->ce_link;
     }
     if (ap || tp)
        Fatal("Inconsistent .UPDATEALL lists for target [%s]",
          targets->CE_NAME);
      }
      targets->ce_link = NIL(CELL);
   }

   for( tg1 = targets; tg1 != NIL(CELL); tg1 = tg1->ce_link ) {
      /* Check each target.  Check for inconsistencies between :: and : rule
       * sets.  :: may follow either : or :: but not the reverse.
       *
       * Any targets that contain :: rules are represented by a prerequisite
       * list hanging off the main target cell where each of the prerequisites
       * is a copy of the target cell but is not entered into the hash table.
       */
      int magic = (tg1->ce_flag & F_PERCENT) && !(tg1->ce_flag & F_MAGIC);

      if( !(op & R_OP_DCL ) && (tg1->ce_flag & F_MULTI) && !magic )
     Fatal( "':' vs '::' inconsistency in rules for %s", tg1->CE_NAME );

      if( magic ) {
     if (op & R_OP_OR)
        for(tp1=prereq; tp1; tp1=tp1->ce_link) {
           CELLPTR tmpcell = tp1->ce_link;
           tp1->ce_link = NIL(CELL);
           _build_graph(op,tg1,tp1);
           tp1->ce_link = tmpcell;
        }
     else
        prereq = _build_graph(op,tg1,prereq);
      }
      else if( !(tg1->ce_flag & F_SPECIAL) &&
        (prereq == NIL(CELL)) &&
        (p = _is_magic( tg1->CE_NAME )) != NIL(char))
         _do_magic( op, p, tg1, prereq, attr, set_dir );
      else if( op & R_OP_DCL ) {
     CELLPTR tmp_cell = _make_multi(tg1);
     tflag |= _add_root(tg1);
     targets = _replace_cell( targets, tg1, tmp_cell );
     tg1 = tmp_cell;
      }

      if( !magic ) _set_attributes( attr, set_dir, tg1 );

      /* Build the proper prerequisite list of the target.  If the `-',
       * modifier was used clear the prerequisite list before adding any
       * new prerequisites.  Else add them to the head/tail as appropriate.
       *
       * If the target has F_PERCENT set then no prerequisites are used. */

      if( !(tg1->ce_flag & F_PERCENT) ) {
     if( op & R_OP_MI ) Clear_prerequisites( tg1 );

     if( (op & R_OP_UP) && (tg1->ce_prq != NIL(LINK)) )
        _stick_at_head( tg1, prereq );
     else for( tp1=prereq; tp1 != NIL(CELL); tp1 = tp1->ce_link )
        Add_prerequisite( tg1, tp1, FALSE, FALSE );
      }
      else if( op & (R_OP_MI | R_OP_UP) )
     Warning( "Modifier(s) `^!' for %-meta target ignored" );
   }

   if(tflag)
      Target = TRUE;

   /* Check to see if we have NO targets but some attributes.  IF so then
    * apply all of the attributes to the complete list of prerequisites.
    */

   if( (targets == NIL(CELL)) && attr ) {
      if( prereq != NIL(CELL) )
     for( tp1=prereq; tp1 != NIL(CELL); tp1 = tp1->ce_link )
        _set_attributes( attr, set_dir, tp1 );
      else
     _set_global_attr( attr );
   }

   /* Now that we have built the lists of targets, the parser must parse the
    * rules if there are any.  However we must start the rule list with the
    * rule specified as via the ; kludge, if there is one */
   _sv_targets = targets;
   _sv_attr    = attr;
   _sv_flag    = ((op & R_OP_BG) ? F_SINGLE : F_DEFAULT);

   DB_RETURN( RULE_SCAN );
}


static int
_do_magic( op, dot, target, prereq, attr, set_dir )/*
=====================================================
   This function takes a magic target of the form .<chars>.<chars> or
   .<chars> and builds the appropriate % rules for that target.

   The function builds the % rule, `%.o : %.c'  from .c.o, and
   `%.a :' from .a */

int op;
char    *dot;
CELLPTR target;
CELLPTR prereq;
t_attr  attr;
char    *set_dir;
{
   CELLPTR tg;
   CELLPTR prq;
   char    *tmp, *tmp2;

   DB_ENTER( "_do_magic" );

   if( prereq != NIL(CELL) )
      Warning( "Ignoring prerequisites of old style meta-target" );

   if( dot == target->CE_NAME ) {       /* its of the form .a   */
      tg  = Def_cell( "%" );            /* ==> no prerequisite  */
      tmp = _build_meta( target->CE_NAME );
      prq = Def_cell( tmp );
      FREE( tmp );

      _build_graph( op, tg, prq );
   }
   else {
      tmp = _build_meta( dot );
      tg  = Def_cell( tmp );
      FREE( tmp );

      tmp = _build_meta( tmp2 = DmSubStr( target->CE_NAME, dot ) );
      prq = Def_cell( tmp );
      FREE( tmp  );
      FREE( tmp2 );

      _build_graph( op, tg, prq );
   }

   tg->ce_flag      |= F_PERCENT;
   target->ce_flag  |= (F_MAGIC|F_PERCENT);

   _set_attributes( attr, set_dir, tg );

   DB_RETURN(1);
}


static CELLPTR
_replace_cell( lst, cell, rep )
CELLPTR lst;
CELLPTR cell;
CELLPTR rep;
{
   register CELLPTR tp;

   if( lst == cell ) {
      rep->ce_link = lst->ce_link;
      lst = rep;
   }
   else {
      for( tp=lst; tp->ce_link != cell; tp=tp->ce_link );
      rep->ce_link = tp->ce_link->ce_link;
      tp->ce_link = rep;
   }

   return(lst);
}


static char *
_build_meta( name )/*
=====================
   Check to see if the name is of the form .c~ if so and if Augmake
   translation is enabled then return s.%.c, else return %.suff, where if the
   suffix ends in '~' then leave it be.*/
char *name;
{
   char *tmp;
   int  test = (STOBOOL(Augmake) ? name[strlen(name)-1] == '~' : 0);

   tmp = DmStrJoin( test ? "s.%" : "%", name, -1, FALSE);
   if( test ) tmp[ strlen(tmp)-1 ] = '\0';

   return(tmp);
}


static CELLPTR
_build_graph( op, target, prereq )/*
====================================
   This function is called to build the graph for the % rule given by
   target : prereq cell combination.  This function assumes that target
   is a % target and that prereq is a single % prerequisite.  R_OP_CL
   rules replace existing rules if any, only R_OP_CL works for meta-rules.
   %.o :: %.c is meaningless.   If target has ce_all set then all the cells
   on the list must match in order for the match to work.  If prereq->ce_link
   is not nil then all prerequisites listed by the link set must match also.
   This latter match is more difficult because in general the prerequisite
   sets may not be listed in the same order.

   It also assumes that target cell has F_PERCENT set already. */
int op;
CELLPTR target;
CELLPTR prereq;
{
   LINKPTR edl;
   CELLPTR edge = 0;
   CELLPTR tpq,cur;
   int match;

   DB_ENTER( "_build_graph" );
   DB_PRINT( "%", ("Building graph for [%s : %s]", target->CE_NAME,
            (prereq == NIL(CELL)) ? "" : prereq->CE_NAME) );

   tpq = NIL(CELL);
   for(cur=prereq;cur;cur=cur->ce_link) {
      char *name = cur->CE_NAME;
      int   len  = strlen(name);

      if( *name == '\'' && name[len-1]=='\'' ){
     _add_global_prereq( cur );
     name[len-1] = '\0';
     strcpy(name,name+1);
      }
      else {
     if (tpq)
        tpq->ce_link = cur;
     else
        prereq = cur;
     tpq = cur;
      }
   }
   if(tpq)
      tpq->ce_link=NIL(CELL);
   else
      prereq = NIL(CELL);

   /* Search the list of prerequisites for the current target and see if
    * any of them match the current %-meta's : prereq's pair.  NOTE that
    * %-metas are built as if they were F_MULTI targets. */
   match = FALSE;
   for(edl=target->ce_prq; !match && edl != NIL(LINK); edl=edl->cl_next) {
      LINKPTR l1,l2;
      edge = edl->cl_prq;

      DB_PRINT("%", ("Trying to match [%s]",edge?edge->CE_NAME:"(nil)"));

      /* First we match the target sets, if this fails then we don't have to
       * bother with the prerequisite sets.  The targets sets are sorted.
       * this makes life very simple. */

      l1 = CeMeToo(target);
      l2 = CeMeToo(edge);
      while(l1 && l2 && l1->cl_prq == l2->cl_prq) {
     l1=l1->cl_next;
     l2=l2->cl_next;
      }

      if (l1 || l2)
     continue;

      /* target sets match, so check prerequisites. */

      if(    (!edge->ce_prq && !prereq)
      || (   edge->ce_prq
          && (   edge->ce_dir == _sv_setdir
          || (   edge->ce_dir
              && _sv_setdir
              && !strcmp(edge->ce_dir,strchr(_sv_setdir,'=')+1)
             )
         )
         )
      ) {
     LINKPTR prql;

     /* this is a really gross way to compare two sets, it's n^2 but
      * since the sets are assumed to always be tiny, it should be ok. */
     for(tpq=prereq; tpq; tpq=tpq->ce_link) {
        for(prql=edge->ce_prq;prql;prql=prql->cl_next)
           if (prql->cl_prq == tpq)
          break;

        if(prql == NIL(LINK))
           break;

        prql->cl_prq->ce_flag |= F_MARK;
     }

     if (tpq == NIL(CELL)) {
        for(prql=edge->ce_prq;prql;prql=prql->cl_next)
           if(!(prql->cl_prq->ce_flag & F_MARK))
          break;

        if(prql == NIL(LINK))
        match = TRUE;
     }

     /* clean up the mark bits. */
     for(prql=edge->ce_prq;prql;prql=prql->cl_next)
        prql->cl_prq->ce_flag &= ~F_MARK;
      }
   }

   if( match ) {
      /* match is TRUE hence, we found an edge joining the target and the
       * prerequisite so reset the new edge so that new values replace it. */
      DB_PRINT( "%", ("It's an old edge") );

      edge->ce_dir    = NIL(char);
      edge->ce_flag  &= (F_PERCENT|F_MAGIC|F_DFA);
      edge->ce_attr  &= A_NOINFER;
   }
   else {
      DB_PRINT( "%", ("Adding a new edge") );

      edge = _make_multi(target);

      for(edl=CeMeToo(target);edl;edl=edl->cl_next) {
     if( !((tpq=edl->cl_prq)->ce_flag & F_DFA) ) {
        Add_nfa( tpq->CE_NAME );
        tpq->ce_flag |= F_DFA;
     }

     edl->cl_prq->ce_set = edge;
      }

      edge->ce_all = target->ce_all;
      target->ce_all.cl_next = NIL(LINK);
      target->ce_set = NIL(CELL);

      for(tpq=prereq; tpq; tpq=tpq->ce_link)
         Add_prerequisite(edge, tpq, FALSE, TRUE);
   }

   if( op & R_OP_DCL )
   Warning("'::' operator for meta-target '%s' ignored, ':' operator assumed.",
       target->CE_NAME );

   edge->ce_link = _sv_edgel;
   _sv_edgel = edge;
   _sv_globprq_only = 0;

   DB_RETURN(NIL(CELL));
}


static CELLPTR
_make_multi( tg )
CELLPTR tg;
{
   CELLPTR cp;

   /* This first handles the case when a : foo ; exists prior to seeing
    * a :: fee; */
   if( !(tg->ce_flag & F_MULTI) && (tg->ce_prq || tg->ce_recipe) ) {
      TALLOC(cp, 1, CELL);
      *cp = *tg;

      tg->ce_prq    = NIL(LINK);
      tg->ce_flag  |= F_RULES|F_MULTI|F_TARGET;
      tg->ce_attr  |= A_SEQ;
      tg->ce_recipe = NIL(STRING);
      tg->ce_dir    = NIL(char);
      cp->ce_count  = ++tg->ce_index;
      cp->ce_cond   = NIL(STRING);
      cp->ce_set    = NIL(CELL);
      cp->ce_all.cl_prq = cp;
      CeNotMe(cp)   = NIL(LINK);

      Add_prerequisite(tg, cp, FALSE, TRUE);
   }

   TALLOC(cp, 1, CELL);
   *cp = *tg;

   if( !(tg->ce_flag & F_MULTI) ) {
      tg->ce_prq    = NIL(LINK);
      tg->ce_flag  |= F_RULES|F_MULTI|F_TARGET;
      tg->ce_attr  |= A_SEQ;
      tg->ce_recipe = NIL(STRING);
      tg->ce_dir    = NIL(char);
      cp->ce_cond   = NIL(STRING);
   }
   else {
      cp->ce_flag  &= ~(F_RULES|F_MULTI);
      cp->ce_attr  &= ~A_SEQ;
      cp->ce_prq    = NIL(LINK);
      cp->ce_index  = 0;
      cp->ce_cond   = NIL(STRING);
   }
   cp->ce_count = ++tg->ce_index;
   cp->ce_flag |= F_TARGET;
   cp->ce_set   = NIL(CELL);
   cp->ce_all.cl_prq = cp;
   CeNotMe(cp)  = NIL(LINK);

   Add_prerequisite(tg, cp, FALSE, TRUE);
   return(cp);
}


static void
_add_global_prereq( pq )/*
==========================
    Prerequisite is a non-% prerequisite for a %-rule target, add it to
    the target's list of global prerequsites to add on match */
CELLPTR pq;
{
   register LINKPTR ln;

   for(ln=_sv_glb_prq; ln; ln=ln->cl_next)
      if(strcmp(ln->cl_prq->CE_NAME,pq->CE_NAME) == 0)
     return;

   TALLOC( ln, 1, LINK );
   ln->cl_next = _sv_glb_prq;
   ln->cl_prq  = pq;
   _sv_glb_prq = ln;
}



static void
_set_attributes( attr, set_dir, cp )/*
======================================
    Set the appropriate attributes for a cell */
t_attr  attr;
char    *set_dir;
CELLPTR cp;
{
   char   *dir = 0;

   DB_ENTER( "_set_attributes" );

   /* If .SETDIR attribute is set then we have at least .SETDIR= in the
    * set_dir string.  So go and fishout what is at the end of the =.
    * If not set and not NULL then propagate it to the target cell. */

   if( attr & A_SETDIR ) {
      char *p;
      if( (p = strchr( set_dir, '=' )) != NULL )
         dir = p + 1;

      if( cp->ce_dir )
     Warning( "Multiple .SETDIR for %s ignored", cp->CE_NAME );
      else if( *dir )
         cp->ce_dir = DmStrDup(dir);
   }
   cp->ce_attr |= attr;     /* set rest of attributes for target */

   DB_VOID_RETURN;
}



static void
_set_global_attr( attr )/*
==========================
    Handle the setting of the global attribute functions based on
    The attribute flags set in attr. */
t_attr attr;
{
   t_attr flag;

   /* Some compilers can't handle a switch on a long, and t_attr is now a long
    * integer on some systems.  foey! */
   for( flag = MAX_ATTR; flag; flag >>= 1 )
      if( flag & attr ) {
     if( flag == A_PRECIOUS)      Def_macro(".PRECIOUS",  "y", M_EXPANDED);
     else if( flag == A_SILENT)   Def_macro(".SILENT",    "y", M_EXPANDED);
     else if( flag == A_IGNORE )  Def_macro(".IGNORE",    "y", M_EXPANDED);
     else if( flag == A_EPILOG )  Def_macro(".EPILOG",    "y", M_EXPANDED);
     else if( flag == A_PROLOG )  Def_macro(".PROLOG",    "y", M_EXPANDED);
     else if( flag == A_NOINFER ) Def_macro(".NOINFER",   "y", M_EXPANDED);
     else if( flag == A_SEQ )     Def_macro(".SEQUENTIAL","y", M_EXPANDED);
     else if( flag == A_SHELL )   Def_macro(".USESHELL",  "y", M_EXPANDED);
     else if( flag == A_MKSARGS ) Def_macro(".MKSARGS",   "y", M_EXPANDED);
     else if( flag == A_SWAP )    Def_macro(".SWAP",      "y", M_EXPANDED);
      }

   attr &= ~A_GLOB;
   if( attr ) Warning( "Non global attribute(s) ignored" );
}



static void
_stick_at_head( cp, pq )/*
==========================
    Add the prerequisite list to the head of the existing prerequisite
    list */

CELLPTR cp;         /* cell for target node */
CELLPTR pq;     /* list of prerequisites to add */
{
   DB_ENTER( "_stick_at_head" );

   if( pq->ce_link != NIL(CELL) ) _stick_at_head( cp, pq->ce_link );
   Add_prerequisite( cp, pq, TRUE, FALSE );

   DB_VOID_RETURN;
}



static t_attr
_is_attribute( name )/*
=======================
   Check the passed name against the list of valid attributes and return the
   attribute index if it is, else return 0, indicating the name is not a valid
   attribute.  The present attributes are defined in dmake.h as A_xxx #defines,
   with the corresponding makefile specification:  (note they must be named
   exactly as defined below)

   Valid attributes are:  .IGNORE, .SETDIR=, .SILENT, .PRECIOUS, .LIBRARY,
                          .EPILOG, .PROLOG,  .LIBRARYM, .SYMBOL, .UPDATEALL,
              .USESHELL, .NOINFER, .PHONY, .SWAP, .SEQUENTIAL
              .NOSTATE,  .MKSARGS, .IGNOREGROUP, .GROUP, .FIRST
              .EXECUTE, .ERRREMOVE

   NOTE:  The strcmp's are OK since at most three are ever executed for any
          one attribute check, and that happens only when we can be fairly
          certain we have an attribute.  */
char *name;
{
   t_attr attr = 0;

   DB_ENTER( "_is_attribute" );

   if( *name++ == '.' )
      switch( *name )
      {
         case 'E':
        if( !strcmp(name, "EPILOG") )           attr = A_EPILOG;
        else if( !strcmp(name, "EXECUTE"))      attr = A_EXECUTE;
        else if( !strcmp(name, "ERRREMOVE"))    attr = A_ERRREMOVE;
        else attr = 0;
        break;

     /* A_FIRST implies A_IGNORE, handled in ST_INCLUDE */
         case 'F':
        attr = (strcmp(name, "FIRST")) ? 0 : A_FIRST;
        break;

     case 'G': attr = (strcmp(name, "GROUP"))    ? 0 : A_GROUP;   break;
         case 'L': attr = (strcmp(name, "LIBRARY"))  ? 0 : A_LIBRARY; break;
         case 'M': attr = (strcmp(name, "MKSARGS"))  ? 0 : A_MKSARGS; break;

         case 'I':
        if( !strcmp(name, "IGNORE") )           attr = A_IGNORE;
        else if( !strcmp(name, "IGNOREGROUP"))  attr = A_IGNOREGROUP;
        else attr = 0;
        break;

         case 'N':
        if( !strcmp(name, "NOINFER") )      attr = A_NOINFER;
        else if( !strcmp(name, "NOSTATE"))  attr = A_NOSTATE;
        else attr = 0;
        break;

         case 'U':
        if( !strcmp(name, "UPDATEALL") )    attr = A_UPDATEALL;
        else if( !strcmp(name, "USESHELL")) attr = A_SHELL;
        else attr = 0;
        break;

         case 'P':
            if( !strcmp(name, "PRECIOUS") )     attr = A_PRECIOUS;
            else if( !strcmp(name, "PROLOG") )  attr = A_PROLOG;
            else if( !strcmp(name, "PHONY") )   attr = A_PHONY;
            else attr = 0;
            break;

         case 'S':
            if( !strncmp(name, "SETDIR=", 7) )    attr = A_SETDIR;
            /* let .SILENT do nothing when -vr is active. */
            else if( !(Verbose & V_FORCEECHO) && !strcmp(name, "SILENT") ) attr = A_SILENT;
            else if( !strcmp(name, "SYMBOL") )    attr = A_SYMBOL;
            else if( !strcmp(name, "SEQUENTIAL")) attr = A_SEQ;
            else if( !strcmp(name, "SWAP"))       attr = A_SWAP;
            else attr = 0;
            break;
      }

   DB_RETURN( attr );
}



static int
_is_special( tg )/*
===================
   This function returns TRUE if the name passed in represents a special
   target, otherwise it returns false.  A special target is one that has
   a special meaning to dmake, and may require processing at the time that
   it is parsed.

   Current Special targets are:
    .GROUPPROLOG    .GROUPEPILOG    .INCLUDE    .IMPORT
    .EXPORT     .SOURCE     .SUFFIXES   .ERROR        .EXIT
    .INCLUDEDIRS    .MAKEFILES  .REMOVE     .KEEP_STATE
*/
char *tg;
{
   DB_ENTER( "_is_special" );

   if( *tg++ != '.' ) DB_RETURN( 0 );

   switch( *tg )
   {
      case 'E':
         if( !strcmp( tg, "ERROR" ) )       DB_RETURN( ST_REST     );
         else if( !strcmp( tg, "EXPORT" ) ) DB_RETURN( ST_EXPORT   );
         else if( !strcmp( tg, "EXIT" ) )   DB_RETURN( ST_EXIT     );
     break;

      case 'G':
     if( !strcmp( tg, "GROUPPROLOG" ))      DB_RETURN( ST_REST     );
     else if( !strcmp( tg, "GROUPEPILOG" )) DB_RETURN( ST_REST     );
     break;

      case 'I':
         if( !strcmp( tg, "IMPORT" ) )      DB_RETURN( ST_IMPORT   );
         else if( !strcmp( tg, "INCLUDE" ) )    DB_RETURN( ST_INCLUDE  );
     else if( !strcmp( tg, "INCLUDEDIRS" )) DB_RETURN( ST_REST     );
     break;

      case 'K':
         if( !strcmp( tg, "KEEP_STATE" ) )  DB_RETURN( ST_KEEP     );
     break;

      case 'M':
         if( !strcmp( tg, "MAKEFILES" ) )   DB_RETURN( ST_REST     );
     break;

      case 'R':
         if( !strcmp( tg, "REMOVE" ) )      DB_RETURN( ST_REST     );
     break;

      case 'S':
         if( !strncmp( tg, "SOURCE", 6 ) )  DB_RETURN( ST_SOURCE   );
         else if( !strncmp(tg, "SUFFIXES", 8 )) DB_RETURN( ST_SOURCE   );
     break;
   }

   DB_RETURN( 0 );
}



static int
_is_percent( np )/*
===================
    return TRUE if np points at a string containing a % sign */
char *np;
{
   return( (strchr(np,'%') && (*np != '\'' && np[strlen(np)-1] != '\'')) ?
       TRUE : FALSE );
}


static char *
_is_magic( np )/*
=================
    return TRUE if np points at a string of the form
          .<chars>.<chars>  or  .<chars>
    where chars are only alpha characters.

        NOTE:  reject target if it begins with ./ or ../ */
char *np;
{
   register char *n;

   n = np;
   if( *n != '.' ) return( NIL(char) );
   if (strchr(DirBrkStr, *(n+1))!=NULL || *(n+1) == '.' )
      return (NIL(char));

   for( n++; isgraph(*n) && (*n != '.'); n++ );

   if( *n != '\0' ) {
      if( *n != '.' )  return( NIL(char) );
      for( np = n++; isgraph( *n ) && (*n != '.'); n++ );
      if( *n != '\0' ) return( NIL(char) );
   }
   else if( STOBOOL(Augmake) )
      return( NIL(char) );

   /* np points at the second . of .<chars>.<chars> string.
    * if the special target is of the form .<chars> then np points at the
    * first . in the token. */

   return( np );
}


static int
_add_root(tg)
CELLPTR tg;
{
   int res = FALSE;

   if(tg == Targets)
      return(TRUE);

   if( !Target && !(tg->ce_flag & (F_SPECIAL|F_PERCENT)) ) {
      Add_prerequisite(Targets, tg, FALSE, TRUE);

      tg->ce_flag |= F_TARGET;
      tg->ce_attr |= A_FRINGE;
      res          = TRUE;
   }

   return(res);
}