summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/rcs.c
blob: bf6d13c6294fe812b272be81ec5f12fc5e389ab9 (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
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
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
/*	$OpenBSD: rcs.c,v 1.19 2004/12/18 17:20:40 jfb Exp $	*/
/*
 * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>

#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "rcs.h"
#include "log.h"

#define RCS_BUFSIZE     16384 
#define RCS_BUFEXTSIZE   8192


/* RCS token types */
#define RCS_TOK_ERR     -1
#define RCS_TOK_EOF      0
#define RCS_TOK_NUM      1
#define RCS_TOK_ID       2
#define RCS_TOK_STRING   3
#define RCS_TOK_SCOLON   4
#define RCS_TOK_COLON    5


#define RCS_TOK_HEAD     8
#define RCS_TOK_BRANCH   9
#define RCS_TOK_ACCESS   10
#define RCS_TOK_SYMBOLS  11
#define RCS_TOK_LOCKS    12
#define RCS_TOK_COMMENT  13
#define RCS_TOK_EXPAND   14
#define RCS_TOK_DATE     15
#define RCS_TOK_AUTHOR   16
#define RCS_TOK_STATE    17
#define RCS_TOK_NEXT     18
#define RCS_TOK_BRANCHES 19
#define RCS_TOK_DESC     20
#define RCS_TOK_LOG      21
#define RCS_TOK_TEXT     22
#define RCS_TOK_STRICT   23

#define RCS_ISKEY(t)    (((t) >= RCS_TOK_HEAD) && ((t) <= RCS_TOK_BRANCHES))


#define RCS_NOSCOL   0x01   /* no terminating semi-colon */
#define RCS_VOPT     0x02   /* value is optional */


/* opaque parse data */
struct rcs_pdata {
	u_int  rp_lines;

	char  *rp_buf;
	size_t rp_blen;
	char  *rp_bufend;

	/* pushback token buffer */
	char   rp_ptok[128];
	int    rp_pttype;       /* token type, RCS_TOK_ERR if no token */

	FILE  *rp_file;
};


struct rcs_line {
	char *rl_line;
	int   rl_lineno;
	TAILQ_ENTRY(rcs_line) rl_list;
};
TAILQ_HEAD(rcs_tqh, rcs_line);

struct rcs_foo {
	int       rl_nblines;
	char     *rl_data;
	struct rcs_tqh rl_lines;
};

static int  rcs_parse_admin     (RCSFILE *);
static int  rcs_parse_delta     (RCSFILE *);
static int  rcs_parse_deltatext (RCSFILE *);

static int      rcs_parse_access    (RCSFILE *);
static int      rcs_parse_symbols   (RCSFILE *);
static int      rcs_parse_locks     (RCSFILE *);
static int      rcs_parse_branches  (RCSFILE *, struct rcs_delta *);
static void     rcs_freedelta       (struct rcs_delta *);
static void     rcs_freepdata       (struct rcs_pdata *);
static int      rcs_gettok          (RCSFILE *);
static int      rcs_pushtok         (RCSFILE *, const char *, int);
static int      rcs_growbuf         (RCSFILE *);
static int      rcs_patch_lines     (struct rcs_foo *, struct rcs_foo *);

static struct rcs_delta*  rcs_findrev    (RCSFILE *, RCSNUM *);
static struct rcs_foo*    rcs_splitlines (const char *);
static void               rcs_freefoo    (struct rcs_foo *);

#define RCS_TOKSTR(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_buf
#define RCS_TOKLEN(rfp)   ((struct rcs_pdata *)rfp->rf_pdata)->rp_blen


static struct rcs_key {
	char  rk_str[16];
	int   rk_id;
	int   rk_val;
	int   rk_flags;
} rcs_keys[] = {
	{ "access",   RCS_TOK_ACCESS,   RCS_TOK_ID,     RCS_VOPT     },
	{ "author",   RCS_TOK_AUTHOR,   RCS_TOK_STRING, 0            },
	{ "branch",   RCS_TOK_BRANCH,   RCS_TOK_NUM,    RCS_VOPT     },
	{ "branches", RCS_TOK_BRANCHES, RCS_TOK_NUM,    RCS_VOPT     },
	{ "comment",  RCS_TOK_COMMENT,  RCS_TOK_STRING, RCS_VOPT     },
	{ "date",     RCS_TOK_DATE,     RCS_TOK_NUM,    0            },
	{ "desc",     RCS_TOK_DESC,     RCS_TOK_STRING, RCS_NOSCOL   },
	{ "expand",   RCS_TOK_EXPAND,   RCS_TOK_STRING, RCS_VOPT     },
	{ "head",     RCS_TOK_HEAD,     RCS_TOK_NUM,    RCS_VOPT     },
	{ "locks",    RCS_TOK_LOCKS,    RCS_TOK_ID,     0            },
	{ "log",      RCS_TOK_LOG,      RCS_TOK_STRING, RCS_NOSCOL   },
	{ "next",     RCS_TOK_NEXT,     RCS_TOK_NUM,    RCS_VOPT     },
	{ "state",    RCS_TOK_STATE,    RCS_TOK_STRING, RCS_VOPT     },
	{ "strict",   RCS_TOK_STRICT,   0,              0,           },
	{ "symbols",  RCS_TOK_SYMBOLS,  0,              0            },
	{ "text",     RCS_TOK_TEXT,     RCS_TOK_STRING, RCS_NOSCOL   },
};

#define RCS_NKEYS   (sizeof(rcs_keys)/sizeof(rcs_keys[0]))


/*
 * rcs_open()
 *
 * Open a file containing RCS-formatted information.  The file's path is
 * given in <path>, and the opening mode is given in <mode>, which is either
 * RCS_MODE_READ, RCS_MODE_WRITE, or RCS_MODE_RDWR.  If the mode requests write
 * access and the file does not exist, it will be created.
 * The file isn't actually parsed by rcs_open(); parsing is delayed until the
 * first operation that requires information from the file.
 * Returns a handle to the opened file on success, or NULL on failure.
 */
RCSFILE*
rcs_open(const char *path, u_int mode)
{
	RCSFILE *rfp;
	struct stat st;

	if ((stat(path, &st) == -1) && (errno == ENOENT) &&
	   !(mode & RCS_MODE_WRITE)) {
		cvs_log(LP_ERRNO, "cannot open RCS file `%s'", path);
		return (NULL);
	}

	rfp = (RCSFILE *)malloc(sizeof(*rfp));
	if (rfp == NULL) {
		cvs_log(LP_ERRNO, "failed to allocate RCS file structure");
		return (NULL);
	}
	memset(rfp, 0, sizeof(*rfp));

	rfp->rf_head = rcsnum_alloc();
	if (rfp->rf_head == NULL) {
		free(rfp);
		return (NULL);
	}

	rfp->rf_branch = rcsnum_alloc();
	if (rfp->rf_branch == NULL) {
		rcs_close(rfp);
		return (NULL);
	}

	rfp->rf_path = strdup(path);
	if (rfp->rf_path == NULL) {
		cvs_log(LP_ERRNO, "failed to duplicate RCS file path");
		rcs_close(rfp);
		return (NULL);
	}

	rcsnum_aton(RCS_HEAD_INIT, NULL, rfp->rf_head);

	rfp->rf_ref = 1;
	rfp->rf_flags |= RCS_RF_SLOCK;
	rfp->rf_mode = mode;

	TAILQ_INIT(&(rfp->rf_delta));
	TAILQ_INIT(&(rfp->rf_symbols));
	TAILQ_INIT(&(rfp->rf_locks));

	if (rcs_parse(rfp) < 0) {
		rcs_close(rfp);
		return (NULL);
	}

	return (rfp);
}


/*
 * rcs_close()
 *
 * Close an RCS file handle.
 */
void
rcs_close(RCSFILE *rfp)
{
	struct rcs_delta *rdp;
	struct rcs_lock *rlp;
	struct rcs_sym *rsp;

	if (rfp->rf_ref > 1) {
		rfp->rf_ref--;
		return;
	}

	while (!TAILQ_EMPTY(&(rfp->rf_delta))) {
		rdp = TAILQ_FIRST(&(rfp->rf_delta));
		TAILQ_REMOVE(&(rfp->rf_delta), rdp, rd_list);
		rcs_freedelta(rdp);
	}

	while (!TAILQ_EMPTY(&(rfp->rf_symbols))) {
		rsp = TAILQ_FIRST(&(rfp->rf_symbols));
		TAILQ_REMOVE(&(rfp->rf_symbols), rsp, rs_list);
		rcsnum_free(rsp->rs_num);
		free(rsp->rs_name);
		free(rsp);
	}

	while (!TAILQ_EMPTY(&(rfp->rf_locks))) {
		rlp = TAILQ_FIRST(&(rfp->rf_locks));
		TAILQ_REMOVE(&(rfp->rf_locks), rlp, rl_list);
		rcsnum_free(rlp->rl_num);
		free(rlp);
	}

	if (rfp->rf_head != NULL)
		rcsnum_free(rfp->rf_head);
	if (rfp->rf_branch != NULL)
		rcsnum_free(rfp->rf_branch);

	if (rfp->rf_path != NULL)
		free(rfp->rf_path);
	if (rfp->rf_comment != NULL)
		free(rfp->rf_comment);
	if (rfp->rf_expand != NULL)
		free(rfp->rf_expand);
	if (rfp->rf_desc != NULL)
		free(rfp->rf_desc);
	free(rfp);
}


/*
 * rcs_write()
 *
 * Write the contents of the RCS file handle <rfp> to disk in the file whose
 * path is in <rf_path>.
 * Returns 0 on success, or -1 on failure.
 */
int
rcs_write(RCSFILE *rfp)
{
	FILE *fp;
	char buf[1024], numbuf[64], *cp;
	size_t rlen, len;
	struct rcs_sym *symp;
	struct rcs_delta *rdp;

	if (rfp->rf_flags & RCS_RF_SYNCED)
		return (0);

	fp = fopen(rfp->rf_path, "w");
	if (fp == NULL) {
		cvs_log(LP_ERRNO, "failed to open RCS output file `%s'",
		    rfp->rf_path);
		return (-1);
	}

	rcsnum_tostr(rfp->rf_head, numbuf, sizeof(numbuf));
	fprintf(fp, "head\t%s;\n", numbuf);
	fprintf(fp, "access;\n");

	fprintf(fp, "symbols\n");
	TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
		rcsnum_tostr(symp->rs_num, numbuf, sizeof(numbuf));
		snprintf(buf, sizeof(buf), "%s:%s", symp->rs_name, numbuf);
		fprintf(fp, "\t%s", buf);
		if (symp != TAILQ_LAST(&(rfp->rf_symbols), rcs_slist))
			fputc('\n', fp);
	}
	fprintf(fp, ";\n");

	fprintf(fp, "locks;");

	if (rfp->rf_flags & RCS_RF_SLOCK)
		fprintf(fp, " strict;");
	fputc('\n', fp);

	if (rfp->rf_comment != NULL)
		fprintf(fp, "comment\t@%s@;\n", rfp->rf_comment);

	if (rfp->rf_expand != NULL)
		fprintf(fp, "expand @ %s @;\n", rfp->rf_expand);

	fprintf(fp, "\n\n");

	TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
		fprintf(fp, "%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
		    sizeof(numbuf)));
		fprintf(fp, "date\t%d.%02d.%02d.%02d.%02d.%02d;",
		    rdp->rd_date.tm_year, rdp->rd_date.tm_mon + 1,
		    rdp->rd_date.tm_mday, rdp->rd_date.tm_hour,
		    rdp->rd_date.tm_min, rdp->rd_date.tm_sec);
		fprintf(fp, "\tauthor %s;\tstate %s;\n",
		    rdp->rd_author, rdp->rd_state);
		fprintf(fp, "branches;\n");
		fprintf(fp, "next\t%s;\n\n", rcsnum_tostr(rdp->rd_next,
		    numbuf, sizeof(numbuf)));
	}

	fprintf(fp, "\ndesc\n@%s@\n\n", rfp->rf_desc);

	/* deltatexts */
	TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
		fprintf(fp, "\n%s\n", rcsnum_tostr(rdp->rd_num, numbuf,
		    sizeof(numbuf)));
		fprintf(fp, "log\n@%s@\ntext\n@", rdp->rd_log);

		cp = rdp->rd_text;
		do {
			len = sizeof(buf);
			rlen = rcs_stresc(1, cp, buf, &len);
			fprintf(fp, "%s", buf);
			cp += rlen;
		} while (len != 0);
		fprintf(fp, "@\n\n");
	}
	fclose(fp);

	rfp->rf_flags |= RCS_RF_SYNCED;

	return (0);
}


/*
 * rcs_addsym()
 *
 * Add a symbol to the list of symbols for the RCS file <rfp>.  The new symbol
 * is named <sym> and is bound to the RCS revision <snum>.
 * Returns 0 on success, or -1 on failure.
 */
int
rcs_addsym(RCSFILE *rfp, const char *sym, RCSNUM *snum)
{
	struct rcs_sym *symp;

	/* first look for duplication */
	TAILQ_FOREACH(symp, &(rfp->rf_symbols), rs_list) {
		if (strcmp(symp->rs_name, sym) == 0) {
			cvs_log(LP_ERR, "attempt to add duplicate symbol `%s'",
			    sym);
			return (-1);
		}
	}

	symp = (struct rcs_sym *)malloc(sizeof(*symp));
	if (symp == NULL) {
		cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
		return (-1);
	}

	symp->rs_name = strdup(sym);
	if (symp->rs_name == NULL) {
		cvs_log(LP_ERRNO, "failed to duplicate symbol");
		free(symp);
		return (-1);
	}

	symp->rs_num = rcsnum_alloc();
	if (symp->rs_num == NULL) {
		free(symp->rs_name);
		free(symp);
		return (-1);
	}
	rcsnum_cpy(snum, symp->rs_num, 0);

	TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);

	/* not synced anymore */
	rfp->rf_flags &= ~RCS_RF_SYNCED;

	return (0);
}


/*
 * rcs_patch()
 *
 * Apply an RCS-format patch pointed to by <patch> to the file contents
 * found in <data>.
 * Returns 0 on success, or -1 on failure.
 */

BUF*
rcs_patch(const char *data, const char *patch)
{
	struct rcs_foo *dlines, *plines;
	struct rcs_line *lp;
	size_t len;
	int lineno;
	BUF *res;

	len = strlen(data);
	res = cvs_buf_alloc(len, BUF_AUTOEXT);
	if (res == NULL)
		return (NULL);

	dlines = rcs_splitlines(data);
	if (dlines == NULL) {
		cvs_buf_free(res);
		return (NULL);
	}

	plines = rcs_splitlines(patch);
	if (plines == NULL) {
		cvs_buf_free(res);
		rcs_freefoo(dlines);
		return (NULL);
	}

	if (rcs_patch_lines(dlines, plines) < 0) {
		cvs_buf_free(res);
		rcs_freefoo(plines);
		rcs_freefoo(dlines);
		return (NULL);
	}

	lineno = 0;
	TAILQ_FOREACH(lp, &dlines->rl_lines, rl_list) {
		if (lineno != 0)
			cvs_buf_fappend(res, "%s\n", lp->rl_line);
		lineno++;
	}

	rcs_freefoo(dlines);
	rcs_freefoo(plines);
	return (res);
}

static int
rcs_patch_lines(struct rcs_foo *dlines, struct rcs_foo *plines)
{
	char op, *ep;
	struct rcs_line *lp, *dlp, *ndlp;
	int i, lineno, nbln;

	dlp = TAILQ_FIRST(&(dlines->rl_lines));
	lp = TAILQ_FIRST(&(plines->rl_lines));

	/* skip first bogus line */
	for (lp = TAILQ_NEXT(lp, rl_list); lp != NULL;
	    lp = TAILQ_NEXT(lp, rl_list)) {
		op = *(lp->rl_line);
		lineno = (int)strtol((lp->rl_line + 1), &ep, 10);
		if ((lineno > dlines->rl_nblines) || (lineno <= 0) ||
		    (*ep != ' ')) {
			cvs_log(LP_ERR,
			    "invalid line specification in RCS patch");
			return (NULL);
		}
		ep++;
		nbln = (int)strtol(ep, &ep, 10);
		if ((nbln <= 0) || (*ep != '\0')) {
			cvs_log(LP_ERR,
			    "invalid line number specification in RCS patch");
			return (NULL);
		}

		/* find the appropriate line */
		for (;;) {
			if (dlp == NULL)
				break;
			if (dlp->rl_lineno == lineno)
				break;
			if (dlp->rl_lineno > lineno) {
				dlp = TAILQ_PREV(dlp, rcs_tqh, rl_list);
			} else if (dlp->rl_lineno < lineno) {
				ndlp = TAILQ_NEXT(dlp, rl_list);
				if (ndlp->rl_lineno > lineno)
					break;
				dlp = ndlp;
			}
		}
		if (dlp == NULL) {
			cvs_log(LP_ERR,
			    "can't find referenced line in RCS patch");
			return (NULL);
		}

		if (op == 'd') {
			for (i = 0; (i < nbln) && (dlp != NULL); i++) {
				ndlp = TAILQ_NEXT(dlp, rl_list);
				TAILQ_REMOVE(&(dlines->rl_lines), dlp, rl_list);
				dlp = ndlp;
			}
		} else if (op == 'a') {
			for (i = 0; i < nbln; i++) {
				ndlp = lp;
				lp = TAILQ_NEXT(lp, rl_list);
				if (lp == NULL) {
					cvs_log(LP_ERR, "truncated RCS patch");
					return (-1);
				}
				TAILQ_REMOVE(&(plines->rl_lines), lp, rl_list);
				TAILQ_INSERT_AFTER(&(dlines->rl_lines), dlp,
				    lp, rl_list);
				dlp = lp;

				/* we don't want lookup to block on those */
				lp->rl_lineno = lineno;

				lp = ndlp;
			}
		} else {
			cvs_log(LP_ERR, "unknown RCS patch operation `%c'", op);
			return (-1);
		}

		/* last line of the patch, done */
		if (lp->rl_lineno == plines->rl_nblines)
			break;
	}

	/* once we're done patching, rebuild the line numbers */
	lineno = 0;
	TAILQ_FOREACH(lp, &(dlines->rl_lines), rl_list)
		lp->rl_lineno = lineno++;
	dlines->rl_nblines = lineno - 1;

	return (0);
}


/*
 * rcs_getrev()
 *
 * Get the whole contents of revision <rev> from the RCSFILE <rfp>.  The
 * returned buffer is dynamically allocated and should be released using
 * cvs_buf_free() once the caller is done using it.
 */

BUF*
rcs_getrev(RCSFILE *rfp, RCSNUM *rev)
{
	int res;
	size_t len;
	void *bp;
	RCSNUM *crev;
	BUF *rbuf;
	struct rcs_delta *rdp = NULL;

	res = rcsnum_cmp(rfp->rf_head, rev, 0);
	if (res == 1) {
		cvs_log(LP_ERR, "sorry, can't travel in the future yet");
		return (NULL);
	} else {
		rdp = rcs_findrev(rfp, rfp->rf_head);
		if (rdp == NULL) {
			cvs_log(LP_ERR, "failed to get RCS HEAD revision");
			return (NULL);
		}

		len = strlen(rdp->rd_text);
		rbuf = cvs_buf_alloc(len, BUF_AUTOEXT);
		if (rbuf == NULL)
			return (NULL);
		cvs_buf_append(rbuf, rdp->rd_text, len);

		if (res != 0) {
			/* Apply patches backwards to get the right version.
			 * This will need some rework to support sub branches.
			 */
			crev = rcsnum_alloc();
			if (crev == NULL) {
				cvs_buf_free(rbuf);
				return (NULL);
			}
			rcsnum_cpy(rfp->rf_head, crev, 0);
			do {
				crev->rn_id[crev->rn_len - 1]--;
				rdp = rcs_findrev(rfp, crev);
				if (rdp == NULL) {
					rcsnum_free(crev);
					cvs_buf_free(rbuf);
					return (NULL);
				}

				cvs_buf_putc(rbuf, '\0');
				bp = cvs_buf_release(rbuf);
				rbuf = rcs_patch((char *)bp, rdp->rd_text);
				if (rbuf == NULL)
					break;
			} while (rcsnum_cmp(crev, rev, 0) != 0);

			rcsnum_free(crev);
		}
	}

	return (rbuf);
}


/*
 * rcs_gethead()
 *
 * Get the head revision for the RCS file <rf>.
 */
BUF*
rcs_gethead(RCSFILE *rf)
{
	return rcs_getrev(rf, rf->rf_head);
}


/*
 * rcs_getrevbydate()
 *
 * Get an RCS revision by a specific date.
 */

RCSNUM*
rcs_getrevbydate(RCSFILE *rfp, struct tm *date)
{
	return (NULL);
}


/*
 * rcs_findrev()
 *
 * Find a specific revision's delta entry in the tree of the RCS file <rfp>.
 * The revision number is given in <rev>.
 * Returns a pointer to the delta on success, or NULL on failure.
 */

static struct rcs_delta*
rcs_findrev(RCSFILE *rfp, RCSNUM *rev)
{
	u_int cmplen;
	struct rcs_delta *rdp;
	struct rcs_dlist *hp;
	int found;
	
	cmplen = 2;
	hp = &(rfp->rf_delta);

	do {
		found = 0;
		TAILQ_FOREACH(rdp, hp, rd_list) {
			if (rcsnum_cmp(rdp->rd_num, rev, cmplen) == 0) {
				if (cmplen == rev->rn_len)
					return (rdp);

				hp = &(rdp->rd_snodes);
				cmplen += 2;
				found = 1;
				break;
			}
		}
	} while (found && cmplen < rev->rn_len);

	return (NULL);
}


/*
 * rcs_parse()
 *
 * Parse the contents of file <path>, which are in the RCS format.
 * Returns 0 on success, or -1 on failure.
 */

int
rcs_parse(RCSFILE *rfp)
{
	int ret;
	struct rcs_pdata *pdp;

	if (rfp->rf_flags & RCS_RF_PARSED)
		return (0);

	pdp = (struct rcs_pdata *)malloc(sizeof(*pdp));
	if (pdp == NULL) {
		cvs_log(LP_ERRNO, "failed to allocate RCS parser data");
		return (-1);
	}
	memset(pdp, 0, sizeof(*pdp));

	pdp->rp_lines = 0;
	pdp->rp_pttype = RCS_TOK_ERR;

	pdp->rp_file = fopen(rfp->rf_path, "r");
	if (pdp->rp_file == NULL) {
		cvs_log(LP_ERRNO, "failed to open RCS file `%s'", rfp->rf_path);
		rcs_freepdata(pdp);
		return (-1);
	}

	pdp->rp_buf = (char *)malloc(RCS_BUFSIZE);
	if (pdp->rp_buf == NULL) {
		cvs_log(LP_ERRNO, "failed to allocate RCS parser buffer");
		rcs_freepdata(pdp);
		return (-1);
	}
	pdp->rp_blen = RCS_BUFSIZE;
	pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;

	/* ditch the strict lock */
	rfp->rf_flags &= ~RCS_RF_SLOCK;
	rfp->rf_pdata = pdp;

	if (rcs_parse_admin(rfp) < 0) {
		rcs_freepdata(pdp);
		return (-1);
	}

	for (;;) {
		ret = rcs_parse_delta(rfp);
		if (ret == 0)
			break;
		else if (ret == -1) {
			rcs_freepdata(pdp);
			return (-1);
		}
	}

	ret = rcs_gettok(rfp);
	if (ret != RCS_TOK_DESC) {
		cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
		    RCS_TOKSTR(rfp));
		rcs_freepdata(pdp);
		return (-1);
	}

	ret = rcs_gettok(rfp);
	if (ret != RCS_TOK_STRING) {
		cvs_log(LP_ERR, "token `%s' found where RCS desc expected",
		    RCS_TOKSTR(rfp));
		rcs_freepdata(pdp);
		return (-1);
	}

	rfp->rf_desc = strdup(RCS_TOKSTR(rfp));
	if (rfp->rf_desc == NULL) {
		cvs_log(LP_ERRNO, "failed to duplicate rcs token");
		rcs_freepdata(pdp);
		return (-1);
	}

	for (;;) {
		ret = rcs_parse_deltatext(rfp);
		if (ret == 0)
			break;
		else if (ret == -1) {
			rcs_freepdata(pdp);
			return (-1);
		}
	}

	cvs_log(LP_DEBUG, "RCS file `%s' parsed OK (%u lines)", rfp->rf_path,
	    pdp->rp_lines);

	rcs_freepdata(pdp);

	rfp->rf_pdata = NULL;
	rfp->rf_flags |= RCS_RF_PARSED|RCS_RF_SYNCED;

	return (0);
}


/*
 * rcs_parse_admin()
 *
 * Parse the administrative portion of an RCS file.
 * Returns 0 on success, or -1 on failure.
 */

static int
rcs_parse_admin(RCSFILE *rfp)
{
	u_int i;
	int tok, ntok, hmask;
	struct rcs_key *rk;

	/* hmask is a mask of the headers already encountered */
	hmask = 0;
	for (;;) {
		tok = rcs_gettok(rfp);
		if (tok == RCS_TOK_ERR) {
			cvs_log(LP_ERR, "parse error in RCS admin section");
			return (-1);
		} else if (tok == RCS_TOK_NUM) {
			/* assume this is the start of the first delta */
			rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
			return (0);
		}

		rk = NULL;
		for (i = 0; i < RCS_NKEYS; i++)
			if (rcs_keys[i].rk_id == tok)
				rk = &(rcs_keys[i]);

		if (hmask & (1 << tok)) {
			cvs_log(LP_ERR, "duplicate RCS key");
			return (-1);
		}
		hmask |= (1 << tok);

		switch (tok) {
		case RCS_TOK_HEAD:
		case RCS_TOK_BRANCH:
		case RCS_TOK_COMMENT:
		case RCS_TOK_EXPAND:
			ntok = rcs_gettok(rfp);
			if (ntok == RCS_TOK_SCOLON)
				break;
			if (ntok != rk->rk_val) {
				cvs_log(LP_ERR,
				    "invalid value type for RCS key `%s'",
				    rk->rk_str);
			}

			if (tok == RCS_TOK_HEAD) {
				rcsnum_aton(RCS_TOKSTR(rfp), NULL,
				    rfp->rf_head);
			} else if (tok == RCS_TOK_BRANCH) {
				rcsnum_aton(RCS_TOKSTR(rfp), NULL,
				    rfp->rf_branch);
			} else if (tok == RCS_TOK_COMMENT) {
				rfp->rf_comment = strdup(RCS_TOKSTR(rfp));
				if (rfp->rf_comment == NULL) {
					cvs_log(LP_ERRNO,
					    "failed to duplicate rcs token");
					return (-1);
				}
			} else if (tok == RCS_TOK_EXPAND) {
				rfp->rf_expand = strdup(RCS_TOKSTR(rfp));
				if (rfp->rf_expand == NULL) {
					cvs_log(LP_ERRNO,
					    "failed to duplicate rcs token");
					return (-1);
				}
			}

			/* now get the expected semi-colon */
			ntok = rcs_gettok(rfp);
			if (ntok != RCS_TOK_SCOLON) {
				cvs_log(LP_ERR,
				    "missing semi-colon after RCS `%s' key",
				    rk->rk_str);	
				return (-1);
			}
			break;
		case RCS_TOK_ACCESS:
			rcs_parse_access(rfp);
			break;
		case RCS_TOK_SYMBOLS:
			rcs_parse_symbols(rfp);
			break;
		case RCS_TOK_LOCKS:
			rcs_parse_locks(rfp);
			break;
		default:
			cvs_log(LP_ERR,
			    "unexpected token `%s' in RCS admin section",
			    RCS_TOKSTR(rfp));
			return (-1);
		}
	}

	return (0);
}


/*
 * rcs_parse_delta()
 *
 * Parse an RCS delta section and allocate the structure to store that delta's
 * information in the <rfp> delta list.
 * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
 * -1 on error.
 */

static int
rcs_parse_delta(RCSFILE *rfp)
{
	int ret, tok, ntok, hmask;
	u_int i;
	char *tokstr;
	RCSNUM *datenum;
	struct rcs_delta *rdp;
	struct rcs_key *rk;

	rdp = (struct rcs_delta *)malloc(sizeof(*rdp));
	if (rdp == NULL) {
		cvs_log(LP_ERRNO, "failed to allocate RCS delta structure");
		return (-1);
	}
	memset(rdp, 0, sizeof(*rdp));

	rdp->rd_num = rcsnum_alloc();
	if (rdp->rd_num == NULL) {
		rcs_freedelta(rdp);
		return (-1);
	}
	rdp->rd_next = rcsnum_alloc();
	if (rdp->rd_next == NULL) {
		rcs_freedelta(rdp);
		return (-1);
	}

	TAILQ_INIT(&(rdp->rd_branches));

	tok = rcs_gettok(rfp);
	if (tok != RCS_TOK_NUM) {
		cvs_log(LP_ERR, "unexpected token `%s' at start of delta",
		    RCS_TOKSTR(rfp));
		rcs_freedelta(rdp);
		return (-1);
	}
	rcsnum_aton(RCS_TOKSTR(rfp), NULL, rdp->rd_num);

	hmask = 0;
	ret = 0;
	tokstr = NULL;

	for (;;) {
		tok = rcs_gettok(rfp);
		if (tok == RCS_TOK_ERR) {
			cvs_log(LP_ERR, "parse error in RCS delta section");
			rcs_freedelta(rdp);
			return (-1);
		} else if (tok == RCS_TOK_NUM || tok == RCS_TOK_DESC) {
			rcs_pushtok(rfp, RCS_TOKSTR(rfp), tok);
			ret = (tok == RCS_TOK_NUM ? 1 : 0);
			break;
		}

		rk = NULL;
		for (i = 0; i < RCS_NKEYS; i++)
			if (rcs_keys[i].rk_id == tok)
				rk = &(rcs_keys[i]);

		if (hmask & (1 << tok)) {
			cvs_log(LP_ERR, "duplicate RCS key");
			rcs_freedelta(rdp);
			return (-1);
		}
		hmask |= (1 << tok);

		switch (tok) {
		case RCS_TOK_DATE:
		case RCS_TOK_AUTHOR:
		case RCS_TOK_STATE:
		case RCS_TOK_NEXT:
			ntok = rcs_gettok(rfp);
			if (ntok == RCS_TOK_SCOLON) {
				if (rk->rk_flags & RCS_VOPT)
					break;
				else {
					cvs_log(LP_ERR, "missing mandatory "
					    "value to RCS key `%s'",
					    rk->rk_str);
					rcs_freedelta(rdp);
					return (-1);
				}
			}

			if (ntok != rk->rk_val) {
				cvs_log(LP_ERR,
				    "invalid value type for RCS key `%s'",
				    rk->rk_str);
				rcs_freedelta(rdp);
				return (-1);
			}

			if (tokstr != NULL)
				free(tokstr);
			tokstr = strdup(RCS_TOKSTR(rfp));
			if (tokstr == NULL) {
				cvs_log(LP_ERRNO,
				    "failed to duplicate rcs token");
				rcs_freedelta(rdp);
				return (-1);
			}

			/* now get the expected semi-colon */
			ntok = rcs_gettok(rfp);
			if (ntok != RCS_TOK_SCOLON) {
				cvs_log(LP_ERR,
				    "missing semi-colon after RCS `%s' key",
				    rk->rk_str);	
				rcs_freedelta(rdp);
				return (-1);
			}

			if (tok == RCS_TOK_DATE) {
				datenum = rcsnum_alloc();
				if (datenum == NULL) {
					rcs_freedelta(rdp);
					return (-1);
				}
				rcsnum_aton(tokstr, NULL, datenum);
				if (datenum->rn_len != 6) {
					cvs_log(LP_ERR,
					    "RCS date specification has %s "
					    "fields",
					    (datenum->rn_len > 6) ? "too many" :
					    "missing");
					rcs_freedelta(rdp);
				}
				rdp->rd_date.tm_year = datenum->rn_id[0];
				if (rdp->rd_date.tm_year >= 1900)
					rdp->rd_date.tm_year -= 1900;
				rdp->rd_date.tm_mon = datenum->rn_id[1] - 1;
				rdp->rd_date.tm_mday = datenum->rn_id[2];
				rdp->rd_date.tm_hour = datenum->rn_id[3];
				rdp->rd_date.tm_min = datenum->rn_id[4];
				rdp->rd_date.tm_sec = datenum->rn_id[5];
				rcsnum_free(datenum);
			} else if (tok == RCS_TOK_AUTHOR) {
				rdp->rd_author = tokstr;
				tokstr = NULL;
			} else if (tok == RCS_TOK_STATE) {
				rdp->rd_state = tokstr;
				tokstr = NULL;
			} else if (tok == RCS_TOK_NEXT) {
				rcsnum_aton(tokstr, NULL, rdp->rd_next);
			}
			break;
		case RCS_TOK_BRANCHES:
			rcs_parse_branches(rfp, rdp);
			break;
		default:
			cvs_log(LP_ERR,
			    "unexpected token `%s' in RCS delta",
			    RCS_TOKSTR(rfp));
			rcs_freedelta(rdp);
			return (-1);
		}
	}

	if (tokstr != NULL)
		free(tokstr);

	TAILQ_INSERT_TAIL(&(rfp->rf_delta), rdp, rd_list);

	return (ret);
}


/*
 * rcs_parse_deltatext()
 *
 * Parse an RCS delta text section and fill in the log and text field of the
 * appropriate delta section.
 * Returns 1 if the section was parsed OK, 0 if it is the last delta, and
 * -1 on error.
 */

static int
rcs_parse_deltatext(RCSFILE *rfp)
{
	int tok;
	RCSNUM *tnum;
	struct rcs_delta *rdp;

	tok = rcs_gettok(rfp);
	if (tok == RCS_TOK_EOF)
		return (0);

	if (tok != RCS_TOK_NUM) {
		cvs_log(LP_ERR,
		    "unexpected token `%s' at start of RCS delta text",
		    RCS_TOKSTR(rfp));
		return (-1);
	}

	tnum = rcsnum_alloc();
	if (tnum == NULL)
		return (-1);
	rcsnum_aton(RCS_TOKSTR(rfp), NULL, tnum);

	TAILQ_FOREACH(rdp, &(rfp->rf_delta), rd_list) {
		if (rcsnum_cmp(tnum, rdp->rd_num, 0) == 0)
			break;
	}
	rcsnum_free(tnum);

	if (rdp == NULL) {
		cvs_log(LP_ERR, "RCS delta text `%s' has no matching delta",
		    RCS_TOKSTR(rfp));
		return (-1);
	}

	tok = rcs_gettok(rfp);
	if (tok != RCS_TOK_LOG) {
		cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
		    RCS_TOKSTR(rfp));
		return (-1);
	}

	tok = rcs_gettok(rfp);
	if (tok != RCS_TOK_STRING) {
		cvs_log(LP_ERR, "unexpected token `%s' where RCS log expected",
		    RCS_TOKSTR(rfp));
		return (-1);
	}
	rdp->rd_log = strdup(RCS_TOKSTR(rfp));
	if (rdp->rd_log == NULL) {
		cvs_log(LP_ERRNO, "failed to copy RCS deltatext log");
		return (-1);
	}

	tok = rcs_gettok(rfp);
	if (tok != RCS_TOK_TEXT) {
		cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
		    RCS_TOKSTR(rfp));
		return (-1);
	}

	tok = rcs_gettok(rfp);
	if (tok != RCS_TOK_STRING) {
		cvs_log(LP_ERR, "unexpected token `%s' where RCS text expected",
		    RCS_TOKSTR(rfp));
		return (-1);
	}

	rdp->rd_text = strdup(RCS_TOKSTR(rfp));
	if (rdp->rd_text == NULL) {
		cvs_log(LP_ERRNO, "failed to copy RCS delta text");
		return (-1);
	}

	return (1);
}


/*
 * rcs_parse_access()
 *
 * Parse the access list given as value to the `access' keyword.
 * Returns 0 on success, or -1 on failure.
 */

static int
rcs_parse_access(RCSFILE *rfp)
{
	int type;

	while ((type = rcs_gettok(rfp)) != RCS_TOK_SCOLON) {
		if (type != RCS_TOK_ID) {
			cvs_log(LP_ERR, "unexpected token `%s' in access list",
			    RCS_TOKSTR(rfp));
			return (-1);
		}
	}

	return (0);
}


/*
 * rcs_parse_symbols()
 *
 * Parse the symbol list given as value to the `symbols' keyword.
 * Returns 0 on success, or -1 on failure.
 */

static int
rcs_parse_symbols(RCSFILE *rfp)
{
	int type;
	struct rcs_sym *symp;

	for (;;) {
		type = rcs_gettok(rfp);
		if (type == RCS_TOK_SCOLON)
			break;

		if (type != RCS_TOK_STRING) {
			cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
			    RCS_TOKSTR(rfp));
			return (-1);
		}

		symp = (struct rcs_sym *)malloc(sizeof(*symp));
		if (symp == NULL) {
			cvs_log(LP_ERRNO, "failed to allocate RCS symbol");
			return (-1);
		}
		symp->rs_name = strdup(RCS_TOKSTR(rfp));
		if (symp->rs_name == NULL) {
			cvs_log(LP_ERRNO, "failed to duplicate rcs token");
			free(symp);
			return (-1);
		}

		symp->rs_num = rcsnum_alloc();
		if (symp->rs_num == NULL) {
			cvs_log(LP_ERRNO, "failed to allocate rcsnum info");
			free(symp);
			return (-1);
		}

		type = rcs_gettok(rfp);
		if (type != RCS_TOK_COLON) {
			cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
			    RCS_TOKSTR(rfp));
			rcsnum_free(symp->rs_num);
			free(symp->rs_name);
			free(symp);
			return (-1);
		}

		type = rcs_gettok(rfp);
		if (type != RCS_TOK_NUM) {
			cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
			    RCS_TOKSTR(rfp));
			rcsnum_free(symp->rs_num);
			free(symp->rs_name);
			free(symp);
			return (-1);
		}

		if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, symp->rs_num) < 0) {
			cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
			    RCS_TOKSTR(rfp));
			rcsnum_free(symp->rs_num);
			free(symp->rs_name);
			free(symp);
			return (-1);
		}

		TAILQ_INSERT_HEAD(&(rfp->rf_symbols), symp, rs_list);
	}

	return (0);
}


/*
 * rcs_parse_locks()
 *
 * Parse the lock list given as value to the `locks' keyword.
 * Returns 0 on success, or -1 on failure.
 */

static int
rcs_parse_locks(RCSFILE *rfp)
{
	int type;
	struct rcs_lock *lkp;

	for (;;) {
		type = rcs_gettok(rfp);
		if (type == RCS_TOK_SCOLON)
			break;

		if (type != RCS_TOK_ID) {
			cvs_log(LP_ERR, "unexpected token `%s' in lock list",
			    RCS_TOKSTR(rfp));
			return (-1);
		}

		lkp = (struct rcs_lock *)malloc(sizeof(*lkp));
		if (lkp == NULL) {
			cvs_log(LP_ERRNO, "failed to allocate RCS lock");
			return (-1);
		}
		lkp->rl_num = rcsnum_alloc();
		if (lkp->rl_num == NULL) {
			free(lkp);
			return (-1);
		}

		type = rcs_gettok(rfp);
		if (type != RCS_TOK_COLON) {
			cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
			    RCS_TOKSTR(rfp));
			free(lkp);
			return (-1);
		}

		type = rcs_gettok(rfp);
		if (type != RCS_TOK_NUM) {
			cvs_log(LP_ERR, "unexpected token `%s' in symbol list",
			    RCS_TOKSTR(rfp));
			free(lkp);
			return (-1);
		}

		if (rcsnum_aton(RCS_TOKSTR(rfp), NULL, lkp->rl_num) < 0) {
			cvs_log(LP_ERR, "failed to parse RCS NUM `%s'",
			    RCS_TOKSTR(rfp));
			free(lkp);
			return (-1);
		}

		TAILQ_INSERT_HEAD(&(rfp->rf_locks), lkp, rl_list);
	}

	/* check if we have a `strict' */
	type = rcs_gettok(rfp);
	if (type != RCS_TOK_STRICT) {
		rcs_pushtok(rfp, RCS_TOKSTR(rfp), type);
	} else {
		rfp->rf_flags |= RCS_RF_SLOCK;

		type = rcs_gettok(rfp);
		if (type != RCS_TOK_SCOLON) {
			cvs_log(LP_ERR,
			    "missing semi-colon after `strict' keyword");
			return (-1);
		}
	}

	return (0);
}

/*
 * rcs_parse_branches()
 *
 * Parse the list of branches following a `branches' keyword in a delta.
 * Returns 0 on success, or -1 on failure.
 */

static int
rcs_parse_branches(RCSFILE *rfp, struct rcs_delta *rdp)
{
	int type;
	struct rcs_branch *brp;

	for (;;) {
		type = rcs_gettok(rfp);
		if (type == RCS_TOK_SCOLON)
			break;

		if (type != RCS_TOK_NUM) {
			cvs_log(LP_ERR,
			    "unexpected token `%s' in list of branches",
			    RCS_TOKSTR(rfp));
			return (-1);
		}

		brp = (struct rcs_branch *)malloc(sizeof(*brp));
		if (brp == NULL) {
			cvs_log(LP_ERRNO, "failed to allocate RCS branch");
			return (-1);
		}
		brp->rb_num = rcsnum_alloc();
		if (brp->rb_num == NULL) {
			free(brp);
			return (-1);
		}

		rcsnum_aton(RCS_TOKSTR(rfp), NULL, brp->rb_num);

		TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
	}

	return (0);
}


/*
 * rcs_freedelta()
 *
 * Free the contents of a delta structure.
 */

static void
rcs_freedelta(struct rcs_delta *rdp)
{
	struct rcs_branch *rb;
	struct rcs_delta *crdp;

	if (rdp->rd_num != NULL)
		rcsnum_free(rdp->rd_num);
	if (rdp->rd_next != NULL)
		rcsnum_free(rdp->rd_next);

	if (rdp->rd_author != NULL)
		free(rdp->rd_author);
	if (rdp->rd_state != NULL)
		free(rdp->rd_state);
	if (rdp->rd_log != NULL)
		free(rdp->rd_log);
	if (rdp->rd_text != NULL)
		free(rdp->rd_text);

	while ((rb = TAILQ_FIRST(&(rdp->rd_branches))) != NULL) {
		TAILQ_REMOVE(&(rdp->rd_branches), rb, rb_list);
		rcsnum_free(rb->rb_num);
		free(rb);
	}

	while ((crdp = TAILQ_FIRST(&(rdp->rd_snodes))) != NULL) {
		TAILQ_REMOVE(&(rdp->rd_snodes), crdp, rd_list);
		rcs_freedelta(crdp);
	}

	free(rdp);
}


/*
 * rcs_freepdata()
 *
 * Free the contents of the parser data structure.
 */

static void
rcs_freepdata(struct rcs_pdata *pd)
{
	if (pd->rp_file != NULL)
		(void)fclose(pd->rp_file);
	if (pd->rp_buf != NULL)
		free(pd->rp_buf);
	free(pd);
}


/*
 * rcs_gettok()
 *
 * Get the next RCS token from the string <str>.
 */

static int
rcs_gettok(RCSFILE *rfp)
{
	u_int i;
	int ch, last, type;
	size_t len;
	char *bp;
	struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;

	type = RCS_TOK_ERR;
	bp = pdp->rp_buf;
	*bp = '\0';

	if (pdp->rp_pttype != RCS_TOK_ERR) {
		type = pdp->rp_pttype;
		strlcpy(pdp->rp_buf, pdp->rp_ptok, pdp->rp_blen);
		pdp->rp_pttype = RCS_TOK_ERR;
		return (type);
	}

	/* skip leading whitespace */
	/* XXX we must skip backspace too for compatibility, should we? */
	do {
		ch = getc(pdp->rp_file);
		if (ch == '\n')
			pdp->rp_lines++;
	} while (isspace(ch));

	if (ch == EOF) {
		type = RCS_TOK_EOF;
	} else if (ch == ';') {
		type = RCS_TOK_SCOLON;
	} else if (ch == ':') {
		type = RCS_TOK_COLON;
	} else if (isalpha(ch)) {
		type = RCS_TOK_STRING;
		*(bp++) = ch;
		for (;;) {
			ch = getc(pdp->rp_file);
			if (!isalnum(ch) && ch != '_' && ch != '-') {
				ungetc(ch, pdp->rp_file);
				break;
			}
			*(bp++) = ch;
			if (bp == pdp->rp_bufend - 1) {
				len = bp - pdp->rp_buf;
				if (rcs_growbuf(rfp) < 0) {
					type = RCS_TOK_ERR;
					break;
				}
				bp = pdp->rp_buf + len;
			}
		}
		*bp = '\0';

		if (type != RCS_TOK_ERR) {
			for (i = 0; i < RCS_NKEYS; i++) {
				if (strcmp(rcs_keys[i].rk_str,
				    pdp->rp_buf) == 0) {
					type = rcs_keys[i].rk_id;
					break;
				}
			}
		}
	} else if (ch == '@') {
		/* we have a string */
		type = RCS_TOK_STRING;
		for (;;) {
			ch = getc(pdp->rp_file);
			if (ch == '@') {
				ch = getc(pdp->rp_file);
				if (ch != '@') {
					ungetc(ch, pdp->rp_file);
					break;
				}
			} else if (ch == '\n')
				pdp->rp_lines++;

			*(bp++) = ch;
			if (bp == pdp->rp_bufend - 1) {
				len = bp - pdp->rp_buf;
				if (rcs_growbuf(rfp) < 0) {
					type = RCS_TOK_ERR;
					break;
				}
				bp = pdp->rp_buf + len;
			}
		}

		*bp = '\0';
	} else if (isdigit(ch)) {
		*(bp++) = ch;
		last = ch;
		type = RCS_TOK_NUM;

		for (;;) {
			ch = getc(pdp->rp_file);
			if (bp == pdp->rp_bufend)
				break;
			if (!isdigit(ch) && ch != '.') {
				ungetc(ch, pdp->rp_file);
				break;
			}

			if (last == '.' && ch == '.') {
				type = RCS_TOK_ERR;
				break;
			}
			last = ch;
			*(bp++) = ch;
		}
		*bp = '\0';
	}

	return (type);
}


/*
 * rcs_pushtok()
 *
 * Push a token back in the parser's token buffer.
 */

static int
rcs_pushtok(RCSFILE *rfp, const char *tok, int type)
{
	struct rcs_pdata *pdp = (struct rcs_pdata *)rfp->rf_pdata;

	if (pdp->rp_pttype != RCS_TOK_ERR)
		return (-1);

	pdp->rp_pttype = type;
	strlcpy(pdp->rp_ptok, tok, sizeof(pdp->rp_ptok));
	return (0);
}


/*
 * rcs_stresc()
 *
 * Performs either escaping or unescaping of the string stored in <str>.
 * The operation is to escape special RCS characters if the <esc> argument
 * is 1, or unescape otherwise.  The result is stored in the <buf> destination
 * buffer, and <blen> must originally point to the size of <buf>.
 * Returns the number of bytes which have been read from the source <str> and
 * operated on.  The <blen> parameter will contain the number of bytes
 * actually copied in <buf>.
 */

size_t
rcs_stresc(int esc, const char *str, char *buf, size_t *blen)
{
	size_t rlen;
	const char *sp;
	char *bp, *bep;

	rlen = 0;
	bp = buf;
	bep = buf + *blen - 1;

	for (sp = str; (*sp != '\0') && (bp <= (bep - 1)); sp++) {
		if (*sp == '@') {
			if (esc) {
				if (bp > (bep - 2))
					break;
				*(bp++) = '@';
			} else {
				sp++;
				if (*sp != '@') {
					cvs_log(LP_WARN,
					    "unknown escape character `%c' in "
					    "RCS file", *sp);
					if (*sp == '\0')
						break;
				}
			}
		}

		*(bp++) = *sp;
	}

	*bp = '\0';
	*blen = (bp - buf);
	return (sp - str);
}


/*
 * rcs_splitlines()
 *
 * Split the contents of a file into a list of lines.
 */

static struct rcs_foo*
rcs_splitlines(const char *fcont)
{
	char *dcp;
	struct rcs_foo *foo;
	struct rcs_line *lp;

	foo = (struct rcs_foo *)malloc(sizeof(*foo));
	if (foo == NULL) {
		cvs_log(LP_ERR, "failed to allocate line structure");
		return (NULL);
	}
	TAILQ_INIT(&(foo->rl_lines));
	foo->rl_nblines = 0;
	foo->rl_data = strdup(fcont);
	if (foo->rl_data == NULL) {
		cvs_log(LP_ERRNO, "failed to copy file contents");
		free(foo);
		return (NULL);
	}

	/*
	 * Add a first bogus line with line number 0.  This is used so we
	 * can position the line pointer before 1 when changing the first line
	 * in rcs_patch().
	 */
	lp = (struct rcs_line *)malloc(sizeof(*lp));
	if (lp == NULL)
		return (NULL);

	lp->rl_line = NULL;
	lp->rl_lineno = 0;
	TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);


	for (dcp = foo->rl_data; *dcp != '\0';) {
		lp = (struct rcs_line *)malloc(sizeof(*lp));
		if (lp == NULL) {
			cvs_log(LP_ERR, "failed to allocate line entry");
			return (NULL);
		}

		lp->rl_line = dcp;
		lp->rl_lineno = ++(foo->rl_nblines);
		TAILQ_INSERT_TAIL(&(foo->rl_lines), lp, rl_list);

		dcp = strchr(dcp, '\n');
		if (dcp == NULL) {
			break;
		}
		*(dcp++) = '\0';
	}

	return (foo);
}

static void
rcs_freefoo(struct rcs_foo *fp)
{
	struct rcs_line *lp;

	while ((lp = TAILQ_FIRST(&fp->rl_lines)) != NULL) {
		TAILQ_REMOVE(&fp->rl_lines, lp, rl_list);
		free(lp);
	}
	free(fp->rl_data);
	free(fp);
}

/*
 * rcs_growbuf()
 *
 * Attempt to grow the internal parse buffer for the RCS file <rf> by
 * RCS_BUFEXTSIZE.
 * In case of failure, the original buffer is left unmodified.
 * Returns 0 on success, or -1 on failure.
 */

static int
rcs_growbuf(RCSFILE *rf)
{
	void *tmp;
	struct rcs_pdata *pdp = (struct rcs_pdata *)rf->rf_pdata;

	tmp = realloc(pdp->rp_buf, pdp->rp_blen + RCS_BUFEXTSIZE);
	if (tmp == NULL) {
		cvs_log(LP_ERRNO, "failed to grow RCS parse buffer");
		return (-1);
	}

	pdp->rp_buf = (char *)tmp;
	pdp->rp_blen += RCS_BUFEXTSIZE;
	pdp->rp_bufend = pdp->rp_buf + pdp->rp_blen - 1;

	return (0);
}