File: files_editline.c

package info (click to toggle)
cfengine3 3.2.4-2%2Bnmu1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,976 kB
  • sloc: ansic: 66,527; sh: 11,600; yacc: 407; makefile: 288
file content (1658 lines) | stat: -rw-r--r-- 44,797 bytes parent folder | download
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
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
/* 

   Copyright (C) Cfengine AS

   This file is part of Cfengine 3 - written and maintained by Cfengine AS.
 
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; version 3.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License  
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

  To the extent this program is licensed as part of the Enterprise
  versions of Cfengine, the applicable Commerical Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

/*****************************************************************************/
/*                                                                           */
/* File: files_edit_operators.c                                              */
/*                                                                           */
/*****************************************************************************/

#include "cf3.defs.h"
#include "cf3.extern.h"

/*****************************************************************************/

enum editlinetypesequence
   {
   elp_vars,
   elp_classes,
   elp_delete,
   elp_columns,
   elp_insert,
   elp_replace,
   elp_reports,
   elp_none
   };

char *EDITLINETYPESEQUENCE[] =
   {
   "vars",
   "classes",
   "delete_lines",
   "field_edits",
   "insert_lines",
   "replace_patterns",
   "reports",
   NULL
   };

static void KeepEditLinePromise(struct Promise *pp);
static void VerifyLineDeletions(struct Promise *pp);
static void VerifyColumnEdits(struct Promise *pp);
static void VerifyPatterns(struct Promise *pp);
static void VerifyLineInsertions(struct Promise *pp);
static int InsertMissingLinesToRegion(struct Item **start,struct Item *begin_ptr,struct Item *end_ptr,struct Attributes a,struct Promise *pp);
static int InsertMissingLinesAtLocation(struct Item **start,struct Item *begin_ptr,struct Item *end_ptr,struct Item *location,struct Item *prev,struct Attributes a,struct Promise *pp);
static int DeletePromisedLinesMatching(struct Item **start,struct Item *begin,struct Item *end,struct Attributes a,struct Promise *pp);
static int InsertMissingLineAtLocation(char *newline,struct Item **start,struct Item *location,struct Item *prev,struct Attributes a,struct Promise *pp);
static int InsertCompoundLineAtLocation(char *newline,struct Item **start,struct Item *location,struct Item *prev,struct Attributes a,struct Promise *pp);
static int ReplacePatterns(struct Item *start,struct Item *end,struct Attributes a,struct Promise *pp);
static int EditColumns(struct Item *file_start,struct Item *file_end,struct Attributes a,struct Promise *pp);
static int EditLineByColumn(struct Rlist **columns,struct Attributes a,struct Promise *pp);
static int EditColumn(struct Rlist **columns,struct Attributes a,struct Promise *pp);
static int SanityCheckInsertions(struct Attributes a);
static int SanityCheckDeletions(struct Attributes a,struct Promise *pp);
static int SelectLine(char *line,struct Attributes a,struct Promise *pp);
static int NotAnchored(char *s);
static void EditClassBanner(enum editlinetypesequence type);
static int SelectRegion(struct Item *start,struct Item **begin_ptr,struct Item **end_ptr,struct Attributes a,struct Promise *pp);

/*****************************************************************************/
/* Level                                                                     */
/*****************************************************************************/

int ScheduleEditLineOperations(char *filename,struct Bundle *bp,struct Attributes a,struct Promise *parentp)

{ enum editlinetypesequence type;
  struct SubType *sp;
  struct Promise *pp;
  char lockname[CF_BUFSIZE];
  char *bp_stack = THIS_BUNDLE;
  struct CfLock thislock;
  int pass;

snprintf(lockname,CF_BUFSIZE-1,"masterfilelock-%s",filename);
thislock = AcquireLock(lockname,VUQNAME,CFSTARTTIME,a,parentp,true);

if (thislock.lock == NULL)
   {
   return false;
   }
  
NewScope("edit");
NewScalar("edit","filename",filename,cf_str);

/* Reset the done state for every call here, since bundle is reusable */

for (type = 0; EDITLINETYPESEQUENCE[type] != NULL; type++)
   {
   if ((sp = GetSubTypeForBundle(EDITLINETYPESEQUENCE[type],bp)) == NULL)
      {
      continue;      
      }
   
   for (pp = sp->promiselist; pp != NULL; pp=pp->next)
      {
      pp->donep = false;
      }
   }

for (pass = 1; pass < CF_DONEPASSES; pass++)
   {
   for (type = 0; EDITLINETYPESEQUENCE[type] != NULL; type++)
      {
      EditClassBanner(type);
      
      if ((sp = GetSubTypeForBundle(EDITLINETYPESEQUENCE[type],bp)) == NULL)
         {
         continue;      
         }
      
      BannerSubSubType(bp->name,sp->name);
      THIS_BUNDLE = bp->name;
      SetScope(bp->name);
            
      for (pp = sp->promiselist; pp != NULL; pp=pp->next)
         {
         pp->edcontext = parentp->edcontext;
         pp->this_server = filename;
         pp->donep = &(pp->done);

         ExpandPromise(cf_agent,bp->name,pp,KeepEditLinePromise);
         
         if (Abort())
            {
            THIS_BUNDLE = bp_stack;
            DeleteScope("edit");
            YieldCurrentLock(thislock);
            return false;
            }         
         }
      }
   }

DeleteScope("edit");
SetScope(parentp->bundle);
THIS_BUNDLE = bp_stack;
YieldCurrentLock(thislock);
return true;
}

/***************************************************************************/
/* Level                                                                   */
/***************************************************************************/

static void EditClassBanner(enum editlinetypesequence type)

{ struct Item *ip;
  int i;
 
if (type != elp_delete)   /* Just parsed all local classes */
   {
   return;
   }

CfOut(cf_verbose,"","     ??  Private class context\n");

for (i = 0; i < CF_ALPHABETSIZE; i++)
   {
   for (ip = VADDCLASSES.list[i]; ip != NULL; ip=ip->next)
      {
      CfOut(cf_verbose,"","     ??       %s\n",ip->name);
      }
   }

CfOut(cf_verbose,"","\n");
}

/***************************************************************************/

static void KeepEditLinePromise(struct Promise *pp)

{ char *sp = NULL;
 
if (!IsDefinedClass(pp->classes))
   {
   CfOut(cf_verbose,"","\n");
   CfOut(cf_verbose,"","   .  .  .  .  .  .  .  .  .  .  .  .  .  .  . \n");
   CfOut(cf_verbose,"","   Skipping whole next edit promise, as context %s is not relevant\n",pp->classes);
   CfOut(cf_verbose,"","   .  .  .  .  .  .  .  .  .  .  .  .  .  .  . \n");
   return;
   }

if (pp->done)
   {
//   return;
   }

if (VarClassExcluded(pp,&sp))
   {
   CfOut(cf_verbose,"","\n");
   CfOut(cf_verbose,"",". . . . . . . . . . . . . . . . . . . . . . . . . . . . \n");
   CfOut(cf_verbose,"","Skipping whole next edit promise (%s), as var-context %s is not relevant\n",pp->promiser,sp);
   CfOut(cf_verbose,"",". . . . . . . . . . . . . . . . . . . . . . . . . . . . \n");
   return;
   }

PromiseBanner(pp);

if (strcmp("classes",pp->agentsubtype) == 0)
   {
   KeepClassContextPromise(pp);
   return;
   }

if (strcmp("delete_lines",pp->agentsubtype) == 0)
   {
   VerifyLineDeletions(pp);
   return;
   }

if (strcmp("field_edits",pp->agentsubtype) == 0)
   {
   VerifyColumnEdits(pp);
   return;
   }

if (strcmp("insert_lines",pp->agentsubtype) == 0)
   {
   VerifyLineInsertions(pp);
   return;
   }

if (strcmp("replace_patterns",pp->agentsubtype) == 0)
   {
   VerifyPatterns(pp);
   return;
   }

if (strcmp("reports",pp->agentsubtype) == 0)
   {
   VerifyReportPromise(pp);
   return;
   }
}

/***************************************************************************/
/* Level                                                                   */
/***************************************************************************/

static void VerifyLineDeletions(struct Promise *pp)

{ struct Item **start = &(pp->edcontext->file_start);
  struct Attributes a = {{0}};
  struct Item *begin_ptr,*end_ptr;
  struct CfLock thislock;
  char lockname[CF_BUFSIZE];

/* *(pp->donep) = true;	*/
	 
a = GetDeletionAttributes(pp);
a.transaction.ifelapsed = CF_EDIT_IFELAPSED;

if (!SanityCheckDeletions(a,pp))
   {
   cfPS(cf_error,CF_INTERPT,"",pp,a," !! The promised line deletion (%s) is inconsistent",pp->promiser);
   return;
   }

/* Are we working in a restricted region? */

if (!a.haveregion)
   {
   begin_ptr = CF_UNDEFINED_ITEM;
   end_ptr = CF_UNDEFINED_ITEM;
   }
else if (!SelectRegion(*start,&begin_ptr,&end_ptr,a,pp))
   {
   if (a.region.include_end || a.region.include_start)
      {
      cfPS(cf_verbose,CF_INTERPT,"",pp,a," !! The promised line deletion (%s) could not select an edit region in %s (this is a good thing, as policy suggests deleting the markers)",pp->promiser,pp->this_server);
      }
   else
      {
      cfPS(cf_inform,CF_INTERPT,"",pp,a," !! The promised line deletion (%s) could not select an edit region in %s (but the delimiters were expected in the file)",pp->promiser,pp->this_server);
      }
   return;
   }

snprintf(lockname,CF_BUFSIZE-1,"deleteline-%s-%s",pp->promiser,pp->this_server);
thislock = AcquireLock(lockname,VUQNAME,CFSTARTTIME,a,pp,true);

if (thislock.lock == NULL)
   {
   return;
   }

if (DeletePromisedLinesMatching(start,begin_ptr,end_ptr,a,pp))
   {
   (pp->edcontext->num_edits)++;
   }

YieldCurrentLock(thislock);
}

/***************************************************************************/

static void VerifyColumnEdits(struct Promise *pp)

{ struct Item **start = &(pp->edcontext->file_start);
  struct Attributes a = {{0}};
  struct Item *begin_ptr,*end_ptr;
  struct CfLock thislock;
  char lockname[CF_BUFSIZE];
  
/* *(pp->donep) = true; */

a = GetColumnAttributes(pp);
a.transaction.ifelapsed = CF_EDIT_IFELAPSED;

if (a.column.column_separator == NULL)
   {
   cfPS(cf_error,CF_WARN,"",pp,a,"No field_separator in promise to edit by column for %s",pp->promiser);
   PromiseRef(cf_error,pp);
   return;
   }

if (a.column.select_column <= 0)
   {
   cfPS(cf_error,CF_WARN,"",pp,a,"No select_field in promise to edit %s",pp->promiser);
   PromiseRef(cf_error,pp);
   return;   
   }

if (!a.column.column_value)
   {
   cfPS(cf_error,CF_WARN,"",pp,a,"No field_value is promised to column_edit %s",pp->promiser);
   PromiseRef(cf_error,pp);
   return;   
   }

/* Are we working in a restricted region? */

if (!a.haveregion)
   {
   begin_ptr = *start;
   end_ptr =NULL; // EndOfList(*start);
   }
else if (!SelectRegion(*start,&begin_ptr,&end_ptr,a,pp))
   {
   cfPS(cf_error,CF_INTERPT,"",pp,a," !! The promised column edit (%s) could not select an edit region in %s",pp->promiser,pp->this_server);
   return;
   }

/* locate and split line */

snprintf(lockname,CF_BUFSIZE-1,"column-%s-%s",pp->promiser,pp->this_server);
thislock = AcquireLock(lockname,VUQNAME,CFSTARTTIME,a,pp,true);

if (thislock.lock == NULL)
   {
   return;
   }

if (EditColumns(begin_ptr,end_ptr,a,pp))
   {
   (pp->edcontext->num_edits)++;
   }

YieldCurrentLock(thislock);
}

/***************************************************************************/

static void VerifyPatterns(struct Promise *pp)

{ struct Item **start = &(pp->edcontext->file_start);
  struct Attributes a = {{0}};
  struct Item *begin_ptr,*end_ptr;
  struct CfLock thislock;
  char lockname[CF_BUFSIZE];
  
/* *(pp->donep) = true; */

CfOut(cf_verbose,""," -> Looking at pattern %s\n",pp->promiser);

/* Are we working in a restricted region? */

a = GetReplaceAttributes(pp);
a.transaction.ifelapsed = CF_EDIT_IFELAPSED;

if (!a.replace.replace_value)
   {
   cfPS(cf_error,CF_INTERPT,"",pp,a," !! The promised pattern replace (%s) had no replacement string",pp->promiser);
   return;
   }

if (!a.haveregion)
   {
   begin_ptr = *start;
   end_ptr = NULL; //EndOfList(*start);
   }
else if (!SelectRegion(*start,&begin_ptr,&end_ptr,a,pp))
   {
   cfPS(cf_error,CF_INTERPT,"",pp,a," !! The promised pattern replace (%s) could not select an edit region in %s",pp->promiser,pp->this_server);
   return;
   }

snprintf(lockname,CF_BUFSIZE-1,"replace-%s-%s",pp->promiser,pp->this_server);
thislock = AcquireLock(lockname,VUQNAME,CFSTARTTIME,a,pp,true);

if (thislock.lock == NULL)
   {
   return;
   }

/* Make sure back references are expanded */

if (ReplacePatterns(begin_ptr,end_ptr,a,pp))
   {
   (pp->edcontext->num_edits)++;
   }

DeleteScope("match"); // because this might pollute the parent promise in next iteration

YieldCurrentLock(thislock);
}

/***************************************************************************/

static void VerifyLineInsertions(struct Promise *pp)

{ struct Item **start = &(pp->edcontext->file_start), *match, *prev;
  struct Item *begin_ptr,*end_ptr;
  struct Attributes a = {{0}};
  struct CfLock thislock;
  char lockname[CF_BUFSIZE];
  
/* *(pp->donep) = true; */
  
a = GetInsertionAttributes(pp);
a.transaction.ifelapsed = CF_EDIT_IFELAPSED;

if (!SanityCheckInsertions(a))
   {
   cfPS(cf_error,CF_INTERPT,"",pp,a," !! The promised line insertion (%s) breaks its own promises",pp->promiser);
   return;
   }

/* Are we working in a restricted region? */

if (!a.haveregion)
   {
   begin_ptr = *start;
   end_ptr = NULL; //EndOfList(*start);
   }
else if (!SelectRegion(*start,&begin_ptr,&end_ptr,a,pp))
   {
   cfPS(cf_error,CF_INTERPT,"",pp,a," !! The promised line insertion (%s) could not select an edit region in %s",pp->promiser,pp->this_server);
   return;
   }

snprintf(lockname,CF_BUFSIZE-1,"insertline-%s-%s",pp->promiser,pp->this_server);
thislock = AcquireLock(lockname,VUQNAME,CFSTARTTIME,a,pp,true);

if (thislock.lock == NULL)
   {
   return;
   }

/* Are we looking for an anchored line inside the region? */

if (a.location.line_matching == NULL)
   {
   if (InsertMissingLinesToRegion(start,begin_ptr,end_ptr,a,pp))
      {
      (pp->edcontext->num_edits)++;
      }
   }
else
   {
   if (!SelectItemMatching(*start,a.location.line_matching,begin_ptr,end_ptr,&match,&prev,a.location.first_last))
      {
      cfPS(cf_error,CF_INTERPT,"",pp,a," !! The promised line insertion (%s) could not select a locator matching regex \"%s\" in %s",pp->promiser,a.location.line_matching,pp->this_server);
      YieldCurrentLock(thislock);
      return;
      }

   if (InsertMissingLinesAtLocation(start,begin_ptr,end_ptr,match,prev,a,pp))
      {
      (pp->edcontext->num_edits)++;
      }
   }

YieldCurrentLock(thislock);
}

/***************************************************************************/
/* Level                                                                   */
/***************************************************************************/

static int SelectRegion(struct Item *start,struct Item **begin_ptr,struct Item **end_ptr,struct Attributes a,struct Promise *pp)

/*

This should provide pointers to the first and last line of text that include the
delimiters, since we need to include those in case they are being deleted, etc.
It returns true if a match was identified, else false.

If no such region matches, begin_ptr and end_ptr should point to CF_UNDEFINED_ITEM

*/
    
{ struct Item *ip,*beg = CF_UNDEFINED_ITEM,*end = CF_UNDEFINED_ITEM;

for (ip = start; ip != NULL; ip = ip->next)
   {
   if (a.region.select_start)
      {
      if (beg == CF_UNDEFINED_ITEM && FullTextMatch(a.region.select_start,ip->name))
         {
         if (!a.region.include_start)
            {
            if (ip->next == NULL)
               {
               cfPS(cf_verbose,CF_INTERPT,"",pp,a," !! The promised start pattern (%s) found an empty region at the end of file %s",a.region.select_start,pp->this_server);
               return false;
               }
            }

         beg = ip;
         continue;
         }
      }

   if (a.region.select_end && beg != CF_UNDEFINED_ITEM)
      {
      if (end == CF_UNDEFINED_ITEM && FullTextMatch(a.region.select_end,ip->name))
         {
         end = ip;
         break;
         }
      }

   if (beg != CF_UNDEFINED_ITEM && end != CF_UNDEFINED_ITEM)
      {
      break;
      }
   }

*begin_ptr = beg;
*end_ptr = end;

if (beg == CF_UNDEFINED_ITEM && a.region.select_start)
   {
   cfPS(cf_verbose,CF_INTERPT,"",pp,a," !! The promised start pattern (%s) was not found when selecting edit region in %s",a.region.select_start,pp->this_server);
   return false;
   }

/*if (end == CF_UNDEFINED_ITEM)
   {
   end = NULL;
   return false;
   }
*/
return true;
}

/***************************************************************************/

static int InsertMissingLinesToRegion(struct Item **start,struct Item *begin_ptr,struct Item *end_ptr,struct Attributes a,struct Promise *pp)

{ struct Item *ip, *prev = CF_UNDEFINED_ITEM;

/* find prev for region */

if (IsItemInRegion(pp->promiser,begin_ptr,end_ptr,a,pp))
   {
   cfPS(cf_verbose,CF_NOP,"",pp,a," -> Promised line \"%s\" exists within selected region of %s (promise kept)",pp->promiser,pp->this_server);
   return false;
   }

if (*start == NULL)
   {
   return InsertMissingLinesAtLocation(start,begin_ptr,end_ptr,*start,prev,a,pp);
   }

if (a.location.before_after == cfe_before)
   {
   for (ip = *start; ip != NULL; ip=ip->next)
      {
      if (ip == begin_ptr)
         {
         return InsertMissingLinesAtLocation(start,begin_ptr,end_ptr,ip,prev,a,pp);
         }
      
      prev = ip;
      }   
   }

if (a.location.before_after == cfe_after)
   {
   for (ip = *start; ip != NULL; ip=ip->next)
      {
      if (ip->next != NULL && ip->next == end_ptr)
         {
         return InsertMissingLinesAtLocation(start,begin_ptr,end_ptr,ip,prev,a,pp);
         }

      if (ip->next == NULL)
         {
         return InsertMissingLinesAtLocation(start,begin_ptr,end_ptr,ip,prev,a,pp);
         }
      
      prev = ip;
      }   
   }

return false;
}

/***************************************************************************/

static int InsertMissingLinesAtLocation(struct Item **start,struct Item *begin_ptr,struct Item *end_ptr,struct Item *location,struct Item *prev,struct Attributes a,struct Promise *pp)

{ FILE *fin;
  char buf[CF_BUFSIZE],exp[CF_EXPANDSIZE];
  struct Item *loc = NULL;
  int retval = false;

if (a.sourcetype && strcmp(a.sourcetype,"file") == 0)
   {
   if ((fin = fopen(pp->promiser,"r")) == NULL)
      {
      cfPS(cf_error,CF_INTERPT,"fopen",pp,a,"Could not read file %s",pp->promiser);
      return false;
      }

   loc = location;

   while (!feof(fin))
      {
      buf[0] = '\0';
      fgets(buf,CF_BUFSIZE,fin);
      StripTrailingNewline(buf);

      if (feof(fin) && strlen(buf) == 0)
         {
         break;
         }
      
      if (a.expandvars)
         {
         ExpandScalar(buf,exp);
         }
      else
         {
         strcpy(exp,buf);
         }

      if (!SelectLine(exp,a,pp))
         {
         continue;
         }

      if (IsItemInRegion(exp,begin_ptr,end_ptr,a,pp))
         {
         cfPS(cf_verbose,CF_NOP,"",pp,a," -> Promised file line \"%s\" exists within file %s (promise kept)",exp,pp->this_server);
         continue;
         }

      retval |= InsertCompoundLineAtLocation(exp,start,loc,prev,a,pp);

      if (prev && prev != CF_UNDEFINED_ITEM)
         {
         prev = prev->next;
         }

      if (loc)
         {
         loc = loc->next;
         }
      }
   
   fclose(fin);
   return retval;
   }
else
   {
   int multiline = a.sourcetype && strcmp(a.sourcetype,"preserve_block") == 0;
   int need_insert = false;

   if (strchr(pp->promiser,'\n') != NULL) /* Multi-line string */
      {
      char *sp;
      loc = location;

      for (sp = pp->promiser; sp <= pp->promiser+strlen(pp->promiser); sp++)
         {
         memset(buf,0,CF_BUFSIZE);
         sscanf(sp,"%[^\n]",buf);
         sp += strlen(buf);
         
         if (!SelectLine(buf,a,pp))
            {
            continue;
            }

         if (IsItemInRegion(buf,begin_ptr,end_ptr,a,pp))
            {
            cfPS(cf_verbose,CF_NOP,"",pp,a," -> Promised file line \"%s\" exists within file %s (promise kept)",buf,pp->this_server);
            continue;
            }

         if (!multiline)
            {
            retval |= InsertCompoundLineAtLocation(buf,start,loc,prev,a,pp);
         
            if (prev && prev != CF_UNDEFINED_ITEM)
               {
               prev = prev->next;
               }
            
            if (loc)
               {
               loc = loc->next;
               }
            }
         else
            {
            need_insert = true;            
            }
         }
      
      if (need_insert)
         {         
         for (sp = pp->promiser; sp <= pp->promiser+strlen(pp->promiser); sp++)
            {
            memset(buf,0,CF_BUFSIZE);
            sscanf(sp,"%[^\n]",buf);
            sp += strlen(buf);
            
            retval |= InsertCompoundLineAtLocation(buf,start,loc,prev,a,pp);
            
            if (prev && prev != CF_UNDEFINED_ITEM)
               {
               prev = prev->next;
               }
            
            if (loc)
               {
               loc = loc->next;
               }
            }
         }
      
      return retval;
      }
   else
      {
      return InsertCompoundLineAtLocation(pp->promiser,start,location,prev,a,pp);
      }
   }
}

/***************************************************************************/
    
static int DeletePromisedLinesMatching(struct Item **start,struct Item *begin,struct Item *end,struct Attributes a,struct Promise *pp)

{ struct Item *ip,*np = NULL,*lp,*initiator = begin,*terminator = NULL;
  int i,in_region = false, retval = false, matches, noedits = true;
  char *sp,buf[CF_BUFSIZE];

if (start == NULL)
   {
   return false;
   }

// Get a pointer from before the region so we can patch the hole later

if (begin == CF_UNDEFINED_ITEM)
   {
   initiator = *start;
   }
else
   {
   if (a.region.include_start)
      {
      initiator = begin;
      }
   else
      {
      initiator = begin->next;
      }
   }

if (end == CF_UNDEFINED_ITEM)
   {
   terminator = NULL;
   }
else
   {
   if (a.region.include_end)
      {
      terminator = end->next;
      }
   else
      {
      terminator = end;
      }
   }

// Now do the deletion

for (ip = initiator; ip != terminator && ip != NULL; ip = np)
   {
   if (a.not_matching)
      {
      matches = !MatchRegion(pp->promiser,*start,ip,terminator);
      }
   else
      {
      matches = MatchRegion(pp->promiser,*start,ip,terminator);
      }

   if (matches)
      {
      CfOut(cf_verbose,""," -> Multi-line region (size %d) matched text in the file",matches);
      }
   else
      {
      CfOut(cf_verbose,""," -> Multi-line region didn't match text in the file");
      }
      
   if (!SelectLine(ip->name,a,pp)) // Start search from location
      {
      np = ip->next;
      continue;
      }

   if (matches)
      {
      CfOut(cf_verbose,""," -> Delete chunk of %d lines\n",matches,ip->name);
      
      if (a.transaction.action == cfa_warn)
         {
         cfPS(cf_error,CF_WARN,"",pp,a," -> Need to delete line \"%s\" from %s - but only a warning was promised",ip->name,pp->this_server);
         np = ip->next;
         noedits = false;
         }
      else
         {
         for (i = 1; i <= matches; i++)
            {                     
            cfPS(cf_verbose,CF_CHG,"",pp,a," -> Deleting the promised line %d \"%s\" from %s",i,ip->name,pp->this_server);
            retval = true;
            noedits = false;

            if (ip->name != NULL)
               {
               free(ip->name);
               }

            np = ip->next;
            free((char *)ip);
            
            lp = ip;

            if (ip == *start)
               {
               if (initiator == *start)
                  {
                  initiator = np;
                  }
               *start = np;
               }
            else
               {
               if (ip == initiator)
                  {
                  initiator = *start;
                  }
               
               for (lp = initiator; lp->next != ip; lp=lp->next)
                  {
                  }

               lp->next = np;
               }

            (pp->edcontext->num_edits)++;

            ip = np;
            }
         }
      }
   else
      {
      np = ip->next;
      }
   }

if (noedits)
   {
   cfPS(cf_verbose,CF_NOP,"",pp,a," -> No need to delete lines from %s, ok",pp->this_server);
   }

return retval;
}

/********************************************************************/

static int ReplacePatterns(struct Item *file_start,struct Item *file_end,struct Attributes a,struct Promise *pp)

{ char replace[CF_EXPANDSIZE],line_buff[CF_EXPANDSIZE];
  char before[CF_BUFSIZE],after[CF_BUFSIZE];
  int match_len,start_off,end_off,once_only = false,retval = false;
  struct Item *ip;
  int notfound = true, cutoff = 1, replaced=false;

if (a.replace.occurrences && (strcmp(a.replace.occurrences,"first") == 0))
   {
   CfOut(cf_inform,"","WARNING! Setting replace-occurrences policy to \"first\" is not convergent");
   once_only = true;
   }

for (ip = file_start; ip != NULL && ip != file_end; ip=ip->next)
   {
   if (ip->name == NULL)
      {
      continue;
      }

   cutoff = 1;
   strncpy(line_buff,ip->name,CF_BUFSIZE);
   replaced = false;
   match_len = 0;

   while (BlockTextMatch(pp->promiser,line_buff,&start_off,&end_off))
      {
      if (match_len == strlen(line_buff))
         {
         CfOut(cf_verbose,""," -> Improper convergent expression matches defacto convergence, so accepting");
         break;
         }      

      if (cutoff++ > CF_MAX_REPLACE)
         {
         CfOut(cf_verbose,""," !! Too many replacements on this line");
         break;
         }
      
      match_len = end_off - start_off;
      ExpandScalar(a.replace.replace_value,replace);

      CfOut(cf_verbose,""," -> Verifying replacement of \"%s\" with \"%s\" (%d)\n",pp->promiser,replace,cutoff);  

      before[0] = after[0] = '\0';         

      // Model the partial substitution in line_buff to check convergence
      
      strncat(before,line_buff,start_off);
      strncat(after,line_buff+end_off,sizeof(after) - 1);
      snprintf(line_buff,CF_EXPANDSIZE-1,"%s%s",before,replace);
      notfound = false;
      replaced = true;
      
      // Model the full substitution in line_buff
      
      snprintf(line_buff,CF_EXPANDSIZE-1,"%s%s%s",before,replace,after);
            
      if (once_only)
         {
         CfOut(cf_verbose,""," -> Replace first occurrence only (warning, this is not a convergent policy)");
         break;
         }
      }

   if (NotAnchored(pp->promiser) && BlockTextMatch(pp->promiser,line_buff,&start_off,&end_off))
      {
      cfPS(cf_error,CF_INTERPT,"",pp,a," -> Promised replacement \"%s\" on line \"%s\" for pattern \"%s\" is not convergent while editing %s",line_buff,ip->name,pp->promiser,pp->this_server);
      CfOut(cf_error,"","Because the regular expression \"%s\" still matches the replacement string \"%s\"",pp->promiser,line_buff);
      PromiseRef(cf_error,pp);
      break;
      }
   
   if (a.transaction.action == cfa_warn)
      {
      cfPS(cf_verbose,CF_WARN,"",pp,a," -> Need to replace line \"%s\" in %s - but only a warning was promised",pp->promiser,pp->this_server);
      continue;
      }
   else if (replaced)
      {
      free(ip->name);
      ip->name = strdup(line_buff);
      cfPS(cf_verbose,CF_CHG,"",pp,a," -> Replaced pattern \"%s\" in %s",pp->promiser,pp->this_server);
      (pp->edcontext->num_edits)++;
      retval = true;

      CfOut(cf_verbose,""," -> << (%d)\"%s\"\n",cutoff,ip->name);
      CfOut(cf_verbose,""," -> >> (%d)\"%s\"\n",cutoff,line_buff);

      if (once_only)
         {
         CfOut(cf_verbose,""," -> Replace first occurrence only (warning, this is not a convergent policy)");
         break;
         }

      if (BlockTextMatch(pp->promiser,ip->name,&start_off,&end_off))
         {
         cfPS(cf_inform,CF_INTERPT,"",pp,a," -> Promised replacement \"%s\" for pattern \"%s\" is not properly convergent while editing %s",ip->name,pp->promiser,pp->this_server);
         CfOut(cf_inform,"","Because the regular expression \"%s\" still matches the end-state replacement string \"%s\"",pp->promiser,line_buff);
         PromiseRef(cf_inform,pp);
         }
      }
   }

if (notfound)
   {
   cfPS(cf_verbose,CF_NOP,"",pp,a," -> No pattern \"%s\" in %s",pp->promiser,pp->this_server);
   }

return retval;
}

/********************************************************************/

static int EditColumns(struct Item *file_start,struct Item *file_end,struct Attributes a,struct Promise *pp)

{ char separator[CF_MAXVARSIZE]; 
  int s,e,retval = false;
  struct Item *ip;
  struct Rlist *columns = NULL;

if (!ValidateRegEx(pp->promiser))
{
   return false;
}

for (ip = file_start; ip != file_end; ip=ip->next)
   {
   if (ip->name == NULL)
      {
      continue;
      }

   if (!FullTextMatch(pp->promiser,ip->name))
      {
      continue;
      }
   else
      {
      CfOut(cf_verbose,""," - Matched line (%s)\n",ip->name);
      }

   if (!BlockTextMatch(a.column.column_separator,ip->name,&s,&e))
      {
      cfPS(cf_verbose,CF_INTERPT,"",pp,a," ! Field edit - no fields found by promised pattern %s in %s",a.column.column_separator,pp->this_server);
      return false;
      }

   if (e-s > CF_MAXVARSIZE / 2)
      {
      CfOut(cf_error,""," !! Line split criterion matches a huge part of the line -- seems to be in error");
      return false;
      }
   
   strncpy(separator,ip->name+s,e-s);
   separator[e-s]='\0';

   columns = SplitRegexAsRList(ip->name,a.column.column_separator,CF_INFINITY,a.column.blanks_ok);
   retval = EditLineByColumn(&columns,a,pp);

   if (retval)
      {
      free(ip->name);
      ip->name = Rlist2String(columns,separator);
      }
   
   DeleteRlist(columns);
   }

return retval;
}

/***************************************************************************/

static int SanityCheckInsertions(struct Attributes a)

{ long not = 0;
  long with = 0;
  long ok = true;
  struct Rlist *rp;
  enum insert_match opt;
  int exact = false, ignore_something = false;
  int multiline = a.sourcetype && strcmp(a.sourcetype,"preserve_block") == 0;

if (a.line_select.startwith_from_list)
   {
   with++;
   }

if (a.line_select.not_startwith_from_list)
   {
   not++;
   }

if (a.line_select.match_from_list)
   {
   with++;
   }

if (a.line_select.not_match_from_list)
   {
   not++;
   }

if (a.line_select.contains_from_list)
   {
   with++;
   }

if (a.line_select.not_contains_from_list)
   {
   not++;
   }

if (not > 1)
   {
   CfOut(cf_error,""," !! Line insertion selection promise is meaningless - the alternatives are mutually exclusive (only one is allowed)");
   ok = false;
   }

if (with && not)
   {
   CfOut(cf_error,""," !! Line insertion selection promise is meaningless - cannot mix positive and negative constraints");
   ok = false;
   }

for (rp = a.insert_match; rp != NULL; rp=rp->next)
   {
   opt = String2InsertMatch(rp->item);

   switch (opt)
      {
      case cf_exact_match:
          exact = true;
          break;
      default:
          ignore_something = true;
          if (multiline)
             {
             CfOut(cf_error,""," !! Line insertion should not use whitespace policy with preserve_block");
             ok = false;
             }
          break;
      }
   }

if (exact && ignore_something)
   {
   CfOut(cf_error,""," !! Line insertion selection promise is meaningless - cannot mix exact_match with other ignore whitespace options");
   ok = false;
   }

return ok;
}

/***************************************************************************/

static int SanityCheckDeletions(struct Attributes a,struct Promise *pp)

{
if (strchr(pp->promiser,'\n') != NULL) /* Multi-line string */
   {
   if (a.not_matching)
      {
      CfOut(cf_error,""," !! Makes no sense to promise multi-line delete with not_matching. Cannot be satisfied for all lines as a block.");
      }
   }

return true;
}

/***************************************************************************/
/* Level                                                                   */
/***************************************************************************/

static int InsertCompoundLineAtLocation(char *newline,struct Item **start,struct Item *location,struct Item *prev,struct Attributes a,struct Promise *pp)

{
  int result = false;
  char buf[CF_EXPANDSIZE];

if (strchr(newline,'\n') != NULL) /* Multi-line string */
   {
   char *sp;
   
   for (sp = newline; sp <= newline+strlen(newline); sp++)
      {
      memset(buf,0,CF_BUFSIZE);
      sscanf(sp,"%2048[^\n]",buf);
      sp += strlen(buf);

      if (!SelectLine(buf,a,pp))
         {
         continue;
         }
      
      result |= InsertMissingLineAtLocation(buf,start,location,prev,a,pp);
      }
   }
else
   {
   result |= InsertMissingLineAtLocation(newline,start,location,prev,a,pp);
   }

return result;
}

/***************************************************************************/

static int InsertMissingLineAtLocation(char *newline,struct Item **start,struct Item *location,struct Item *prev,struct Attributes a,struct Promise *pp)

/* Check line neighbourhood in whole file to avoid edge effects */
    
{
if (prev == CF_UNDEFINED_ITEM) /* Insert at first line */
   {
   if (a.location.before_after == cfe_before)
      {
      if (*start == NULL)
         {
         if (a.transaction.action == cfa_warn)
            {
            cfPS(cf_error,CF_WARN,"",pp,a," -> Need to insert the promised line \"%s\" in %s - but only a warning was promised",newline,pp->this_server);
            return true;
            }
         else
            {
            PrependItemList(start,newline);
            (pp->edcontext->num_edits)++;
            cfPS(cf_verbose,CF_CHG,"",pp,a," -> Inserting the promised line \"%s\" into %s",newline,pp->this_server);
            return true;
            }
         }
      
      if (strcmp((*start)->name,newline) != 0)
         {
         if (a.transaction.action == cfa_warn)
            {
            cfPS(cf_error,CF_WARN,"",pp,a," -> Need to prepend the promised line \"%s\" to %s - but only a warning was promised",newline,pp->this_server);
            return true;
            }
         else
            {
            PrependItemList(start,newline);
            (pp->edcontext->num_edits)++;
            cfPS(cf_verbose,CF_CHG,"",pp,a," -> Prepending the promised line \"%s\" to %s",newline,pp->this_server);
            return true;
            }
         }
      else
         {
         cfPS(cf_verbose,CF_NOP,"",pp,a," -> Promised line \"%s\" exists at start of file %s (promise kept)",newline,pp->this_server);
         return false;
         }
      }
   }
 
if (a.location.before_after == cfe_before)
   {
   if (NeighbourItemMatches(*start,location,newline,cfe_before,a,pp))
      {
      cfPS(cf_verbose,CF_NOP,"",pp,a," -> Promised line \"%s\" exists before locator in (promise kept)",newline);
      return false;
      }
   else
      {
      if (a.transaction.action == cfa_warn)
         {
         cfPS(cf_error,CF_WARN,"",pp,a," -> Need to insert line \"%s\" into %s but only a warning was promised",newline,pp->this_server);
         return true;
         }
      else
         {
         InsertAfter(start,prev,newline);
         (pp->edcontext->num_edits)++;
         cfPS(cf_verbose,CF_CHG,"",pp,a," -> Inserting the promised line \"%s\" into %s before locator",newline,pp->this_server);
         return true;
         }
      }
   }
else
   {
   if (NeighbourItemMatches(*start,location,newline,cfe_after,a,pp))
      {
      cfPS(cf_verbose,CF_NOP,"",pp,a," -> Promised line \"%s\" exists after locator (promise kept)",newline);
      return false;
      }
   else
      {
      if (a.transaction.action == cfa_warn)
         {
         cfPS(cf_error,CF_WARN,"",pp,a," -> Need to insert line \"%s\" in %s but only a warning was promised",newline,pp->this_server);
         return true;
         }
      else
         {
         InsertAfter(start,location,newline);
         cfPS(cf_verbose,CF_CHG,"",pp,a," -> Inserting the promised line \"%s\" into %s after locator",newline,pp->this_server);
         (pp->edcontext->num_edits)++;
         return true;
         }
      }
   }
}

/***************************************************************************/

static int EditLineByColumn(struct Rlist **columns,struct Attributes a,struct Promise *pp)

{ struct Rlist *rp,*this_column = NULL;
  char sep[CF_MAXVARSIZE];
  int i,count = 0,retval = false;

/* Now break up the line into a list - not we never remove an item/column */
 
for (rp = *columns; rp != NULL; rp=rp->next)
    {
    count++;
    
    if (count == a.column.select_column)
       {
       CfOut(cf_verbose,""," -> Stopped at field %d\n",count);
       break;
       }
    }

if (a.column.select_column > count)
   {
   if (!a.column.extend_columns)
      {
      cfPS(cf_error,CF_INTERPT,"",pp,a," !! The file %s has only %d fields, but there is a promise for field %d",pp->this_server,count,a.column.select_column);
      return false;
      }
   else
      {
      for (i = 0; i < (a.column.select_column - count); i++)
         {
         AppendRScalar(columns,strdup(""),CF_SCALAR);
         }

      count = 0;
      
      for (rp = *columns; rp != NULL; rp=rp->next)
         {
         count++;         
         if (count == a.column.select_column)
            {
            CfOut(cf_verbose,""," -> Stopped at column/field %d\n",count);
            break;
            }
         }
      }
   }

if (a.column.value_separator != '\0')
   {
   /* internal separator, single char so split again */

   if (strcmp(rp->item,a.column.column_value) == 0)
      {
      retval = false;
      }
   else
      {
      this_column = SplitStringAsRList(rp->item,a.column.value_separator);
      retval = EditColumn(&this_column,a,pp);
      }
   
   if (retval)
      {
      if (a.transaction.action == cfa_warn)
         {
         cfPS(cf_error,CF_WARN,"",pp,a," -> Need to edit field in %s but only warning promised",pp->this_server);
         retval = false;
         }
      else
         {
         cfPS(cf_inform,CF_CHG,"",pp,a," -> Edited field inside file object %s",pp->this_server);
         (pp->edcontext->num_edits)++;
         free(rp->item);
         sep[0] = a.column.value_separator;
         sep[1] = '\0';
         rp->item = Rlist2String(this_column,sep);
         }
      }
   else
      {
      cfPS(cf_verbose,CF_NOP,"",pp,a," -> No need to edit field in %s",pp->this_server);
      }

   DeleteRlist(this_column);
   return retval;
   }
else
   {
   /* No separator, so we set the whole field to the value */

   if (a.column.column_operation && strcmp(a.column.column_operation,"delete") == 0)
      {
      if (a.transaction.action == cfa_warn)
         {
         cfPS(cf_error,CF_WARN,"",pp,a," -> Need to delete field field value %s in %s but only a warning was promised",rp->item,pp->this_server);
         return false;
         }
      else
         {
         cfPS(cf_inform,CF_CHG,"",pp,a," -> Deleting column field value %s in %s",rp->item,pp->this_server);
         (pp->edcontext->num_edits)++;
         free(rp->item);
         rp->item = strdup("");
         return true;
         }
      }
   else
      {
      if (a.transaction.action == cfa_warn)
         {
         cfPS(cf_error,CF_WARN,"",pp,a," -> Need to set column field value %s to %s in %s but only a warning was promised",rp->item,a.column.column_value,pp->this_server);
         return false;
         }
      else
         {
         cfPS(cf_inform,CF_CHG,"",pp,a," -> Setting whole column field value %s to %s in %s",rp->item,a.column.column_value,pp->this_server);
         free(rp->item);
         rp->item = strdup(a.column.column_value);
         (pp->edcontext->num_edits)++;
         return true;
         }
      }
   }

cfPS(cf_verbose,CF_NOP,"",pp,a," -> No need to edit column field value %s in %s",a.column.column_value,pp->this_server);

return false;
}

/***************************************************************************/

static int SelectLine(char *line,struct Attributes a,struct Promise *pp)

{ struct Rlist *rp,*c;
  int s,e;
  char *selector;

if ((c = a.line_select.startwith_from_list))
   {
   for (rp = c; rp != NULL; rp=rp->next)
      {
      selector = (char *)(rp->item);

      if (strncmp(selector,line,strlen(selector)) == 0)
         {
         return true;
         }      
      }

   return false;
   }

if ((c = a.line_select.not_startwith_from_list))
   {
   for (rp = c; rp != NULL; rp=rp->next)
      {
      selector = (char *)(rp->item);
      
      if (strncmp(selector,line,strlen(selector)) == 0)
         {
         return false;
         }      
      }

   return true;
   }

if ((c = a.line_select.match_from_list))
   {
   for (rp = c; rp != NULL; rp=rp->next)
      {
      selector = (char *)(rp->item);
      
      if (FullTextMatch(selector,line))
         {
         return true;
         }
      }
   
   return false;
   }

if ((c = a.line_select.not_match_from_list))
   {
   for (rp = c; rp != NULL; rp=rp->next)
      {
      selector = (char *)(rp->item);
      
      if (FullTextMatch(selector,line))
         {
         return false;
         }
      }

   return true;
   }

if ((c = a.line_select.contains_from_list))
   {
   for (rp = c; rp != NULL; rp=rp->next)
      {
      selector = (char *)(rp->item);
      
      if (BlockTextMatch(selector,line,&s,&e))
         {
         return true;
         }            
      }
   
   return false;
   }

if ((c = a.line_select.not_contains_from_list))
   {
   for (rp = c; rp != NULL; rp=rp->next)
      {
      selector = (char *)(rp->item);
      
      if (BlockTextMatch(selector,line,&s,&e))
         {
         return false;
         }            
      }

   return true;
   }

return true;
}

/***************************************************************************/
/* Level                                                                   */
/***************************************************************************/

static int EditColumn(struct Rlist **columns,struct Attributes a,struct Promise *pp)

{ struct Rlist *rp, *found;
 int retval = false;

if (a.column.column_operation && strcmp(a.column.column_operation,"delete") == 0)
   {
   if ((found = KeyInRlist(*columns,a.column.column_value)))
      {
      CfOut(cf_inform,""," -> Deleting column field sub-value %s in %s",a.column.column_value,pp->this_server);
      DeleteRlistEntry(columns,found);
      return true;
      }
   else
      {
      return false;
      }
   }

if (a.column.column_operation && strcmp(a.column.column_operation,"set") == 0)
   {
   if (RlistLen(*columns) == 1)
      {
      if (strcmp((*columns)->item,a.column.column_value) == 0)
         {
         CfOut(cf_verbose,""," -> Field sub-value set as promised\n");
         return false;
         }
      }

   CfOut(cf_inform,""," -> Setting field sub-value %s in %s",a.column.column_value,pp->this_server);
   DeleteRlist(*columns);
   *columns = NULL;
   IdempPrependRScalar(columns,a.column.column_value,CF_SCALAR);

   return true;
   }

if (a.column.column_operation && strcmp(a.column.column_operation,"prepend") == 0)
   {
   if (IdempPrependRScalar(columns,a.column.column_value,CF_SCALAR))
      {
      CfOut(cf_inform,""," -> Prepending field sub-value %s in %s",a.column.column_value,pp->this_server);
      return true;
      }
   else
      {
      return false;
      }
   }

if (a.column.column_operation && strcmp(a.column.column_operation,"alphanum") == 0)
   {
   if (IdempPrependRScalar(columns,a.column.column_value,CF_SCALAR))
      {
      retval = true;
      }
   
   rp = AlphaSortRListNames(*columns);
   *columns = rp;
   return retval;
   }

/* default operation is append */

if (IdempAppendRScalar(columns,a.column.column_value,CF_SCALAR))
   {
   return true;
   }
else
   {
   return false;
   }

return false;
}

/********************************************************************/

static int NotAnchored(char *s)
{
if (*s != '^')
   {
   return true;
   }

if (*(s+strlen(s)-1) != '$')
   {
   return true;
   }

return false;
}