File: sccsfile.cc

package info (click to toggle)
cssc 1.2.0-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 3,564 kB
  • ctags: 1,387
  • sloc: cpp: 13,240; sh: 4,771; ansic: 2,992; perl: 342; makefile: 338; awk: 11
file content (1297 lines) | stat: -rw-r--r-- 27,027 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
/*
 * sccsfile.cc: Part of GNU CSSC.
 * 
 * 
 *    Copyright (C) 1997,1998,1999,2001,2003,2004,2007,2008 Free Software Foundation, Inc. 
 * 
 *    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, either version 3 of the License, or
 *    (at your option) any later version.
 *    
 *    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, see <http://www.gnu.org/licenses/>.
 * 
 * CSSC was originally Based on MySC, by Ross Ridge, which was 
 * placed in the Public Domain.
 *
 *
 * Common members of the class sccs_file and its subclasses.  Most of
 * the members in this file are used to read from the SCCS file.
 *
 */

#include "cssc.h"
#include "sccsfile.h"
#include "delta-table.h"
#include "delta-iterator.h"
#include "linebuf.h"
#include "quit.h"
#include "err_no.h"
#include "mylist.h"

#ifdef STDC_HEADERS
#include <ctype.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>		// SEEK_SET on SunOS.
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>		/* fstat(), struct stat */
#endif


#ifdef CONFIG_SCCS_IDS
static const char rcs_id[] = "CSSC $Id: sccsfile.cc,v 1.67 2008/01/06 19:42:23 jay Exp $";
#endif

#if defined(HAVE_FILENO) && defined(HAVE_FSTAT)
/* If an SCCS file has a link count greater than one, then the normal 
 * process of updating the file will break the link.  We try to detect this 
 * even if the file is being opened to reading only, to give an early
 * warning (and because SCCS does so).
 */
static int just_one_link(FILE *f)
{
  int fd = fileno(f);
  if (fd >= 0)
    {
      struct stat st;
      if (0 != fstat(fd, &st))
	{
	  /* We cannot stat the file descriptor.  Perhaps there is a 
	   * filesystem functionality issue.   If that's the case then we
	   * will give it the benefit of the doubt on the link coutn front. 
	   */
	  return 1;  /* We're happy with the file */
	}
      if (st.st_nlink > 1)
	return 0;		/* We don't like it. */
    }
  return 1;			/* OK. */
}
#else
static int just_one_link(FILE *f)
{
  /* Without fileno(), we have no way of checking. */
  return 1;
}
#endif


/* Static member for opening a SCCS file and then calculating its checksum. */
#define f f_is_also_a_class_member_variable
FILE *
sccs_file::open_sccs_file(const char *name,
			  enum _mode mode,
			  int *sump,
			  bool* isBKFile)
{
  FILE *f_local;

#ifdef CONFIG_OPEN_SCCS_FILES_IN_BINARY_MODE
  f_local = fopen(name, "rb");
#else
  if (mode == UPDATE)
    f_local = fopen(name, "r+");
  else
    f_local = fopen(name, "r");
#endif

  if (f_local == NULL)
    {
      const char *purpose = (mode == UPDATE) ? "update" : "reading";
      s_missing_quit("Cannot open SCCS file %s for %s", name, purpose);
      /*NOTEACHED*/
      return NULL;
    }

  if (!just_one_link(f_local))
    {
      // xx: issue error message here
      errormsg("%s had a hard link count which is greater than one.\n"
	       "This means that the normal process of updating the file\n"
	       "would break the hard link.  This error is therefore fatal,\n"
	       "please fix the problem.\n",
	       name);
      (void)fclose(f_local);
      return NULL;
    }

  bool badMagic = false;
  if (getc(f_local) != '\001')
    {
      badMagic = true;
    }
  else
    {
      char magicMarker = getc(f_local);
      if (magicMarker == 'H')
	{
	  if (READ == mode)
	    {
	      /* We support read-only access to BK files. */
	      warning("%s is a BitKeeper file.", name);
	    }
	  else
	    {
	      errormsg("%s: This is a BitKeeper file, and only read-only "
		       "access to BitKeeper files is supported at the moment, "
		       "sorry.\n",
		       name);
	      (void)fclose(f_local);
	      return NULL;
	    }
	  // Inform the caller that this is a BK file.
	  // NB: this is the parameter, not member variable	  
	  *isBKFile = true;	
	}
      else if (magicMarker != 'h')
	{
	  badMagic = true;
	}
    }

  if (badMagic)
    {
      (void)fclose(f_local);
      s_corrupt_quit("%s: No SCCS-file magic number.  "
		     "Did you specify the right file?", name);
      /*NOTEACHED*/
      return NULL;
    }
  
  
  int c;
  errno = 0;
  while ( (c=getc(f_local)) != CONFIG_EOL_CHARACTER)
    {
      if (EOF == c)
	{
	  const int saved_errno = errno;
	  (void)fclose(f_local);
	  errno = saved_errno;
	  if (errno)
	    {
	      perror(name);
	    }
	  else
	    {
	      s_corrupt_quit("%s: Unexpected EOF.", name);
	      /*NOTEACHED*/
	    }
	  return NULL;
	}
    }
  
  int sum = 0u;
  
  while ((c=getc(f_local)) != EOF)
    sum += (char) c;	// Yes, I mean plain char, not signed, not unsigned.
  
  if (ferror(f_local))
    {
      perror(name);
      (void)fclose(f_local);
      return NULL;
    }
  
  
  *sump = sum & 0xFFFFu;
  
#ifdef CONFIG_OPEN_SCCS_FILES_IN_BINARY_MODE
  fclose(f_local);
  if (mode == UPDATE)
    f_local = fopen(name, "r+");
  else
    f_local = fopen(name, "r");
  
  if (NULL == f_local)
    {
      perror(name);
      return NULL;
    }
  
#else
  rewind(f_local);
  if (ferror(f_local))
    {
      perror(name);
      (void)fclose(f_local);
      return NULL;
    }
#endif
  return f_local;
}
#undef f


/* Reads a line from the SCCS file and increments the current line number.
   If the end of file is reached it returns -1.  If it's a control line
   (it starts with ^A) then the control character (the second character)
   is returned.  Otherwise 0 is returned. */

int
sccs_file::read_line() {
	if (read_line_param(f)) {
		if (ferror(f)) {
			errormsg_with_errno("%s: Read error.", name.c_str());
		}
		return -1;
	} 

	lineno++;
	if ( bufchar(0) == '\001')
	  {
	    return bufchar(1);
	  }
	return 0;
}


/* Quits with a message saying that SCCS file is corrupt. */

NORETURN
sccs_file::corrupt(const char *fmt, ...) const {
  char buf[80];
  const char *p;
  
  va_list ap;
  va_start(ap, fmt);
  if (-1 == vsnprintf(buf, sizeof(buf), fmt, ap))
    {
      warning("%s: error message too long for buffer, so the "
	      "next message will lack some relevant detail", 
	      name.c_str());
      p = fmt;			// best effort
    }
  else
    {
      p = buf;
    }
  s_corrupt_quit("%s: line %d: Corrupted SCCS file. (%s)",
		 name.c_str(), lineno, p);
}


/* Checks that a control line has at least one argument. */

void
sccs_file::check_arg() const {
	if (bufchar(2) != ' ') {
		corrupt("Missing arg");
	}
}


/* Checks the a control line has no arguments. */

void
sccs_file::check_noarg() const {
	if (bufchar(2) != '\0') {
		corrupt("Unexpected arg");
	}
}


/* Converts an ASCII string to an unsigned short, quiting if the
   string isn't a valid number. */

unsigned short
sccs_file::strict_atous(const char *s) const
{
  long n = 0;
  
  char c;
  while ( 0 != (c=*s++) )
    {
      if (!isdigit((unsigned char)c))
	{
	  corrupt("Invalid number");
	}
      n = n * 10 + (c - '0');
      if (n > 65535L)
	{
	  corrupt("Number too big");
	}
    }
  
  return (unsigned short) n;
}

// Convert a number field in an SCCS file to a 
// number.  Fields representing numbers in 
// SCCS files should top out at 9999.

unsigned long
sccs_file::strict_atoul_idu(const char *s) const
{
  unsigned long n = 0;
  bool found_ws = false;
  const unsigned long limit = 99999uL;
  char c;

  /* Unix System III pads to the left with spaces in the 
   * numbers, while more modern versions of SCCS pad to 
   * the left with zeroes.   We don't allow left-pad with 
   * whitespace characters other than an actual space.
   */
  while (' ' == *s)
    {
      ++s;
      found_ws = true;
    }

  if (found_ws)
    {
      warning("%s contains spaces in the line counts in its delta table.",
	      name.c_str());
      if ((UPDATE == mode) || (FIX_CHECKSUM == mode))
	{
	  warning("These leading spaces will be converted to leading zeroes.");
	}
    }
  
  
  while ( 0 != (c=*s++) )
    {
      if (!isdigit((unsigned char)c))
	{
	  corrupt("Invalid number");
	}
      n = n * 10 + (c - '0');
    }
  if (n > limit)
    {
      warning("%s: line %d: number field exceeds %lu.", 
	      name.c_str(), lineno, limit);
    }
  
  return n;
}

/* Reads a delta from the SCCS file's delta table and adds it to the
   delta table. */

void
sccs_file::read_delta() {

	/* The current line should be an 's' control line */

	ASSERT(bufchar(1) == 's');
	check_arg();
	
	char *args[7];		/* Stores the result of spliting a line */

	if (plinebuf->split(3, args, 3, '/') != 3)
	  {
	    corrupt("Two /'s expected");
	  }

	// TODO: use constructor here?
	delta tmp;	/* The new delta */

	tmp.inserted  = strict_atoul_idu(args[0]);
	tmp.deleted   = strict_atoul_idu(args[1]);
	tmp.unchanged = strict_atoul_idu(args[2]);

	if (read_line() != 'd') {
		corrupt("Expected '@d'");
	}

	check_arg();

	plinebuf->split(3, args, 7, ' ');

	tmp.type = args[0][0];
	tmp.id = sid(args[1]);
	tmp.date = sccs_date(args[2], args[3]);
	tmp.user = args[4];
	tmp.seq = strict_atous(args[5]);
	tmp.prev_seq = strict_atous(args[6]);

	if ((tmp.type != 'R' && tmp.type != 'D') || args[0][1] != '\0') {
		corrupt("Bad delta type");
	}
	if (!tmp.id.valid()) {
		corrupt("Bad SID");
	}
	if (!tmp.date.valid()) {
		corrupt("Bad Date/Time");
	}

	/* Read in any lists of included, excluded or ignored seq. no's. */

	int c = read_line();
	int i;
	const char *start;
	bool bDebug = getenv("CSSC_SHOW_SEQSTATE") ? true : false;
	for(i = 0; i < 3; i++) {
		if (c == "ixg"[i]) {

		  switch (c)
		    {
		    case 'i':
		      tmp.have_includes = true;
		      break;

		    case 'x':
		      {
			warning("feature not fully tested: "
				"excluded delta in SID %s ",
				tmp.id.as_string().c_str());
			tmp.have_excludes = true;
		      }
		      break;

		    case 'g':
		      tmp.have_ignores = true;
		      break;
		    }
		  
		  if (bufchar(2) != ' ')
		    {
		      c = read_line(); // throw line away.
		      continue;
		    }
		  	
			check_arg();

			start = plinebuf->c_str() + 3;
			do {
				// In C++, strchr() is overloaded so that 
				// it returns const char* if the first 
				// argument is const char*, and char* only if 
				// the first argument is char*.
				const char *end = strchr(start, ' ');
				if (end != NULL) {
				  //*end++ = '\0';
				  const char *p = plinebuf->c_str();
				  plinebuf->set_char(end-p, 0);
				  ASSERT(*end == 0);
				  ++end;
				}
				seq_no seq = strict_atous(start);
				switch (c) {
				case 'i':
				  if (bDebug)
				    {
				      fprintf(stderr, "Including seq %lu\n",
					      (unsigned long)seq);
				    }

					tmp.included.add(seq);
					break;

				case 'x':
				  if (bDebug)
				    {
				      fprintf(stderr, "Excluding seq %lu\n",
					      (unsigned long)seq);
				    }
					tmp.excluded.add(seq);
					break;

				case 'g':
					tmp.ignored.add(seq);
					break;
				}
				start = end;
			} while (start != NULL);

			c = read_line();
		}
	}

	// According to Hyman Rosen <hymie@jyacc.com>, it is possible
	// to have a ^A m line which has no argument.  Therefore we don't
	// use check_arg().  

	// According to Hyman Rosen <hymie@jyacc.com>, it is sometimes
	// possible to have ^Am lines after ^Ac lines, as well as the
	// more usual before.  Hence we now cope with both.

	while (c == 'm' || c == 'c')
	  {
	    if (c == 'm')
	      {
		if (bufchar(2) == ' ')
		  {
		    tmp.mrs.add(plinebuf->c_str() + 3);
		  }
	      }
	    else if (c == 'c') 
	      {
		/* Larry McVoy's extensions for BitKeeper and BitSCCS
		 * add in extra stuff like "^AcSyadayada".  Real SCCS
		 * doesn't mind about that, so at Larry's request, we
		 * tolerate it too.   No idea what these lines mean though.
		 * Ask <lm@bitmover.com> for more information.  Anyway, 
		 * normal comment lines look like "^Ac yadayada" instead,
		 * and check_arg() exists to check for the space.   Hence, 
		 * to support Larry's extensions, we don't call check_arg()
		 * here.
		 */
		if (is_bk_file)
		  {
		    check_bk_comment(c, bufchar(2));
		  }
		else
		  {
		    check_arg();
		    if (bufchar(2) != ' ')
		      {
			saw_unknown_feature("Unknown special comment "
					    "intro '%c%c'",
					    c, bufchar(2));
		      }
		  }
		tmp.comments.add(plinebuf->c_str() + 3);
	      }
	    
	    c = read_line();
	  }
	

	if (c != 'e') {
		corrupt("Expected '@e'");
	}

	check_noarg();

	ASSERT(0 != delta_table);
	delta_table->add(tmp);
}


/* Check for BK flags */
void
sccs_file::check_bk_flag(char flagchar) const
{
  switch (flagchar)
    {
    case 'x':
      return;

    default:
      corrupt("Unknown flag '%c'.", flagchar);
    }
}


/* Find out if it is OK to change the file - called by cdc, rmdel, get -e
 */
bool
sccs_file::edit_mode_ok(bool editing) const
{
  if (editing && is_bk_file)
    {
      errormsg("%s: This is a BitKeeper file.  Checking BitKeeper files out "
	       "for editing (or otherwise modifying them) is not supported "
	       "at the moment, sorry.\n",
	       name.c_str());
      return false;
    }
  else
    {
      return true;
    }
}


/* Check for BK comments */
void
sccs_file::check_bk_comment(char ch, char arg) const
{
  ASSERT(is_bk_file);
  
  switch (arg)
    {
    case 'B':
    case 'C':
    case 'F':
    case 'H':
    case 'K':
    case 'M':
    case 'O':
    case 'P':
    case 'R':
    case 'S':
    case 'T':
    case 'V':
    case 'X':
    case 'Z':
      return;

    case ' ': // this is the normal SCCS case.
      return;

    default:
      saw_unknown_feature("Unknown special comment intro '%c%c' "
			  "in BitKeeper file\n",
			  ch, arg);
    }
}


/* Seeks on the SCCS file to the start of the body.  This function 
   may be rewritten as fseek() doesn't always work too well on
   text files. */
// JAY: use fgetpos()/fsetpos() instead?
bool
sccs_file::seek_to_body()
{
  if (fseek(f, body_offset, SEEK_SET) != 0)
    {
      // this quit should NOT be fatal; we should proceed 
      // to the next file if we can.
      errormsg("%s: fseek() failed!", name.c_str());
      return false;
    }
  lineno = body_lineno;
  return true;
}

bool sccs_file::checksum_ok() const
{
  return checksum_valid;
}


/* Returns the module name of the SCCS file. */

mystring
sccs_file::get_module_name() const
{
  if (flags.module)
    return *flags.module;
  else
    return name.gfile();
}

/* Constructor for the class sccs_file.  Unless the SCCS file is being
   created it reads in the all but the body of the file.  The file is
   locked if it isn't only being read.  */

sccs_file::sccs_file(sccs_name &n, enum _mode m)
  : name(n), f(0), mode(m), lineno(0), xfile_created(false), is_bk_file(false)
{
  delta_table = new cssc_delta_table;
  plinebuf     = new cssc_linebuf;
  ASSERT(0 != delta_table);
  
  if (!name.valid())
    {
      ctor_fail(-1,
		"%s: Not an SCCS file.  Did you specify the right file?",
		name.c_str());
    }
  
  flags.no_id_keywords_is_fatal = 0;
  flags.branch = 0;
  flags.floor = 0;
  flags.ceiling = 0;
  flags.default_sid = sid::null_sid();
  flags.null_deltas = 0;
  flags.joint_edit = 0;
  flags.all_locked = 0;
  flags.encoded = 0;
  flags.executable = 0;
  flags.mr_checker = 0;
  flags.module = 0;
  flags.type = 0;
  flags.reserved = 0;
  flags.user_def = 0;
  
  ASSERT(!flags.default_sid.valid());

  if (mode != READ)
    {
      if (name.lock())
	{
	  ctor_fail(-1, "%s: SCCS file is locked.  Try again later.",
	       name.c_str());
	}
    }
  
  if (mode == CREATE)
    {
      /* f is NULL in this case. */
      return;
    }

  // Even if we are going to change the s-file, we do it by writing
  // a new x-file and then renaming it.   This means that we open
  // the s-file read-only.
  signed int sum = 0;
  f = open_sccs_file(name.c_str(), READ, &sum, &is_bk_file);
  
  if (mode != READ)
    {
      if (!edit_mode_ok(true))
	{
	  ctor_fail(-1,
		    "%s: Editing is not supported for BitKeeper files.\n",
		    name.c_str());
	}
    }
  
  /* open_sccs_file() returns normally if everything went OK, or if 
   * there was an IO error on an apparently valid file.  If this is 
   * the case, perror() will already have been called.
   */
  if (NULL == f)
    {
      ctor_fail(-1, "%s: Cannot open SCCS file.\n", name.c_str());
    }
  
  int c = read_line();
  
  // open_sccs_file() should have already checked that the first line
  // is ^Ah or ^Ah, so this assertion is really just checking that
  // open_sccs_file() did the right thing.
  if (is_bk_file)
    {
      ASSERT(c == 'H');
    }
  else
    {
      ASSERT(c == 'h');
    }

  /* the checksum is represented in the file as decimal.
   */
  signed int given_sum = 0;
  const char *format;
  if (is_bk_file)
    format = "%*cH%d";
  else
    format = "%*ch%d";
  
  if (1 != sscanf(plinebuf->c_str(), format, &given_sum))
    {
      errormsg("Expected checksum line, found line beginning '%.3s'\n",
	       plinebuf->c_str());
      checksum_valid = false;
    }
  else
    {
      given_sum &= 0xFFFFu;
      checksum_valid = (given_sum == sum);
      
      if (false == checksum_valid)
	{
	  if (FIX_CHECKSUM == mode)
	    {
	      // This supports the -z option of admin.
	      checksum_valid = true;
	    }
	  else
	    {
	      warning("%s: bad checksum "
		      "(expected=%d, calculated %d).\n",
		      name.c_str(), given_sum, sum);
	    }
	}
    }
  if (!checksum_valid)
    {
      // Todo: throw exception here?
    }
  
  c = read_line();
  while (c == 's')
    {
      read_delta();
      c = read_line();
    }
  
  if (c != 'u')
    {
      corrupt("Expected '@u'");
    }
  
  check_noarg();
  
  c = read_line();
  while (c != 'U')
    {
      if (c != 0)
	{
	  corrupt("User name expected.");
	}
      users.add(plinebuf->c_str());
      c = read_line();
    }

  /* Sun's Code Manager sometimes emits lines of the form "^AU 0" and
   * so these lines fail the "no argument" check.  So we no longer do
   * that check for "^AU" lines.  A file including lines of this type
   * was provided by Marko Rauhamaa <marko@tekelec.com>.
   */
  /* check_noarg(); */
  
  c = read_line();
  while (c == 'f')
    {
      check_arg();

      if (bufchar(3) == '\0'
	  || (bufchar(4) != '\0' && bufchar(4) != ' '))
	{
	  corrupt("Bad flag arg.");
	}

      // We have to be careful to not crash on input lines like 
      // "^Af v".  That is, bufchar[4] may well be zero!
      // Thanks to William W. Austin <bill@baustin.alph.att.com>
      // for this diagnosis.
      const char *arg = 0;
      bool got_arg = false;
      if (bufchar(4) == ' ')
	{
	  arg = plinebuf->c_str() + 5;
	  got_arg = true;
	}
      else
	{
	  arg = "";
	}
      
      switch (bufchar(3)) {
      case 't':
	set_type_flag(arg);
	break;
	
      case 'v':
	set_mr_checker_flag(arg);
	break;
	
      case 'i':
	flags.no_id_keywords_is_fatal = 1;
	break;
	
      case 'b':
	flags.branch = 1;
	break;
	
      case 'm':
	set_module_flag(arg);
	break;
	
      case 'f':
	flags.floor = release(arg);
	if (!flags.floor.valid())
	  {
	    corrupt("Bad 'f' flag");
	  }
	break;
	
      case 'c':
	flags.ceiling = release(arg);
	if (!flags.ceiling.valid())
	  {
	    corrupt("Bad 'c' flag");
	  }
	break;

      case 'd':
	flags.default_sid = sid(arg);
	if (!flags.default_sid.valid())
	  {
	    corrupt("Bad 'd' flag");
	  }
	break;
	
      case 'n':
	flags.null_deltas = 1;
	break;
	
      case 'j':
	flags.joint_edit = 1;
	break;
	
      case 'l':
	if (got_arg && strcmp(arg, "a") == 0)
	  {
	    flags.all_locked = 1;
	  }
	else
	  {
	    flags.locked = release_list(arg);
	  }
	break;
	
      case 'q':
	set_user_flag(arg);
	break;
	
      case 'z':
	set_reserved_flag(arg);
	break;

      case 'x':
	// The 'x' flag is supported by SCO's version of SCCS.
	// When this flag is set, the g-file is marked executable.
	flags.executable = 1;
	break;
	
      case 'y':
	// The 'y' flag is supported by Solaris 8 and above.
	// It controls the expansion of '%' keywords.  If the 
	// y flag is set, its value is a list of keywords that will 
	// be expanded.  Otherwise, all known keywords will be expanded.
	set_expanded_keyword_flag(arg);
	break;
	
      case 'e':
	if (got_arg && '1' == *arg)
	  flags.encoded = 1;
	else if (got_arg && '0' == *arg)
	  flags.encoded = 0;
	else
	  corrupt("Bad value '%c' for 'e' flag.", arg[0]);
	break;
	
      default:
	if (is_bk_file)
	  {
	    check_bk_flag(bufchar(3));
	  }
	else
	  {
	    corrupt("Unknown flag '%c'.", bufchar(3));
	  }
      }
      
      c = read_line();
    }
  
  if (c != 't')
    {
      corrupt("Expected '@t'");
    }
  
  /* Sun's Code Manager sometimes emits lines of the form "^At 0" and
   * so these lines fail the "no argument" check.  So we no longer do
   * that check for "^At" lines.  A file including lines of this type
   * was provided by Marko Rauhamaa <marko@tekelec.com>.
   */
  /*check_noarg();*/
  
  c = read_line();
  while (c == 0)
    {
      comments.add(plinebuf->c_str());
      c = read_line();
    }
  
  if (c != 'T')
    {
      corrupt("Expected '@T'");
    }
  
  /* Sun's Code Manager sometimes emits lines of the form "^AT 0" and
   * so these lines fail the "no argument" check.  So we no longer do
   * that check for "^AT" lines.  A file including lines of this type
   * was provided by Marko Rauhamaa <marko@tekelec.com>.
   */
  /*check_noarg();*/
  
  body_offset = ftell(f);
  if (body_offset == -1L)
    {
      ctor_fail(errno, "ftell() failed.");
    }
  
  body_lineno = lineno;
}


/* Find the SID of the most recently created delta with the same release
   and level as the requested SID. */
   
sid
sccs_file::find_most_recent_sid(sid id) const {
	sccs_date newest;
	sid found;

	ASSERT(0 != delta_table);
	const_delta_iterator iter(delta_table);

	while (iter.next()) {
		if (id.trunk_match(iter->id)) {
			if (found.is_null() || newest < iter->date) {
				newest = iter->date;
				found = iter->id;
			}
		}
	}
	return found;
}

bool
sccs_file::find_most_recent_sid(sid& s, sccs_date& d) const
{
  s = sid();
  d = sccs_date();
  bool found = false;
  
  ASSERT(0 != delta_table);

  const_delta_iterator iter(delta_table);
  while (iter.next())
    {
      if (!found || iter->date > d)
	{
	  d = iter->date;
	  s = iter->id;
	  found = true;
	}
    }
  return found;
}

void sccs_file::
set_mr_checker_flag(const char *s)
{
  if (flags.mr_checker)
    delete flags.mr_checker;
  
  flags.mr_checker = new mystring(s);
}

void sccs_file::
set_module_flag(const char *s)
{
  if (flags.module)
    delete flags.module;
  
  flags.module = new mystring(s);
}

void  sccs_file::
set_user_flag(const char *s)
{
  if (flags.user_def)
    delete flags.user_def;
  
  flags.user_def = new mystring(s);
}

void sccs_file::
set_type_flag(const char *s)
{
  if (flags.type)
    delete flags.type;
  
  flags.type = new mystring(s);
}

void sccs_file::
set_reserved_flag(const char *s)
{
  if (flags.reserved)
    delete flags.reserved;
  
  flags.reserved = new mystring(s);
}


void sccs_file::
set_expanded_keyword_flag(const char *s)
{
  // Clear any existing contents.
  flags.substitued_flag_letters = myset<char>();

  // The list of keyword letters is space-separated, but all of the
  // keywords are one character long.
  while (*s)
    {
      if (!isspace((unsigned char)*s))
	{
	  flags.substitued_flag_letters.add(*s);
	}
      ++s;
    }
}

int
sccs_file::read_line_param(FILE *f)
{
  if (plinebuf->read_line(f))
    {
      return 1;
    }
  // chomp the newline from the end of the line.
  // TODO: make me 8-bit clean!
  (*plinebuf)[strlen(plinebuf->c_str()) - 1] = '\0';
  return 0;
}

int
sccs_file::is_delta_creator(const char *user, sid id) const
{
  const delta *d = find_delta(id);
  return (d != 0) && (strcmp(d->user.c_str(), user) == 0);
}


const delta * sccs_file::find_delta(sid id) const
{
  ASSERT(0 != delta_table);
  return delta_table->find(id);
}

const delta * sccs_file::find_any_delta(sid id) const
{
  ASSERT(0 != delta_table);
  return delta_table->find_any(id);
}

delta * sccs_file::find_delta(sid id) 
{
  ASSERT(0 != delta_table);
  return delta_table->find(id);
}

seq_no sccs_file::highest_delta_seqno() const
{
  ASSERT(0 != delta_table);
  return delta_table->highest_seqno();
}

sid sccs_file::highest_delta_release() const
{
  ASSERT(0 != delta_table);
  return delta_table->highest_release();
}

sid sccs_file::seq_to_sid(seq_no seq) const
{
  ASSERT(0 != delta_table);
  return delta_table->delta_at_seq(seq).id;
}


/* Destructor for class sccs_file. */

sccs_file::~sccs_file()
{
  if (mode != READ)
    {
      name.unlock();
    }
  
  if (mode != CREATE)
    {
      ASSERT(0 != f);		// catch multiple destruction.
      fclose(f);
      f = 0;
    }

  if (xfile_created)
    {
      remove(name.xfile().c_str());
    }
  
  ASSERT(0 != delta_table); 	// catch multiple destruction.
  delete delta_table;
  delta_table = 0;
  
  ASSERT(0 != plinebuf); 	// catch multiple destruction.
  delete plinebuf;
  plinebuf = 0;
}


char sccs_file::bufchar(int pos) const
{
  return (*plinebuf)[pos];
}


bool sccs_file::branches_allowed() const
{
  return 0 != flags.branch;
}

bool sccs_file::executable_flag_set() const
{
  return 0 != flags.executable;
}


/* There are some features that we don't properly understand. 
 * If we see them, we should abandon any attempt to modify the 
 * file.   We call saw_unknown_feature() when we see one.  It 
 * checks what mode we're using the SCCS file in, and reacts
 * accordingly.
 */
void sccs_file::saw_unknown_feature(const char *fmt, ...) const
{
  va_list ap;

  va_start(ap, fmt);

  /* If we are not modifying the file, just issue a warning.  Otherwise, 
   * abandon the attempt to edit it.
   */
  switch (mode)
    {
    case READ:
    case FIX_CHECKSUM:
      v_unknown_feature_warning(fmt, ap);
      break;
      
    case UPDATE:
    case CREATE:
      s_unrecognised_feature_quit(fmt, ap);
      /*NOTREACHED*/
      break;
    }
}

bool sccs_file::
print_subsituted_flags_list(FILE *out, const char* separator) const
{
  const mylist<char> members = flags.substitued_flag_letters.list();
  for (int i=0; i<members.length(); ++i)
    {
      // print a space separator if one is required.
      if (i > 0)
	{
	  if (printf_failed(fprintf(out, "%s", separator)))
	    return false;
	}

	  
      // print the keyword letter.
      if (printf_failed(fprintf(out, "%c", members[i])))
	return false;
    }
  return true;
}

bool sccs_file::
is_known_keyword_char(char c)
{
  return strchr("MIRLBSDHTEGUYFPQCZWA", c) != NULL;
}

// Explicit template instantiations.
template class mylist<char>;
template class mylist<unsigned short>;
//template void mylist<unsigned short>::add(unsigned short const&);
template void mylist<mystring>::add(mystring const&);

/* Local variables: */
/* mode: c++ */
/* End: */