1 /*
2  * sf-rmdel.cc: Part of GNU CSSC.
3  *
4  *
5  *  Copyright (C) 1997, 1999, 2001, 2007, 2008, 2009, 2010, 2011, 2014,
6  *  2019 Free Software Foundation, Inc.
7  *
8  *  This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * CSSC was originally Based on MySC, by Ross Ridge, which was
22  * placed in the Public Domain.
23  *
24  *
25  * Members of the class sccs_file used for marking a delta in the SCCS
26  * files as removed.
27  *
28  */
29 
30 #include <config.h>
31 #include <algorithm>
32 #include "cssc.h"
33 #include "sccsfile.h"
34 #include "delta.h"
35 #include "delta-iterator.h"
36 #include "linebuf.h"
37 
38 namespace {
39 bool
is_seqlist_member(seq_no seq_to_find,std::vector<seq_no> const & seq_list)40 is_seqlist_member(seq_no seq_to_find, std::vector<seq_no> const &seq_list) {
41   return std::find(seq_list.begin(), seq_list.end(), seq_to_find) != seq_list.end();
42 }
43 }
44 
45 typedef enum { COPY, DELETE, INSERT} update_state;
46 
47 static int
next_state(update_state & current,int key)48 next_state(update_state& current, // this arg is MODIFIED!
49 	   int key)
50 {
51   if (current == COPY)
52     {
53       switch (key)
54 	{
55 	case 'I':
56 	  current = INSERT;
57 	  return 1;
58 	case 'D':
59 	  current = DELETE;
60 	  return 1;
61 	}
62     }
63   else
64     {
65       if ('E' == key)
66 	{
67 	  current = COPY;
68 	  return 1;
69 	}
70     }
71   return 0;		// not expected.
72 }
73 
74 
75 bool
rmdel(sid id)76 sccs_file::rmdel(sid id)
77 {
78   if (!edit_mode_ok(true))
79     return false;
80 
81   delta *d = find_delta(id);
82   if (0 == d)
83     {
84       errormsg("%s: Specified SID not found in SCCS file.", name.c_str());
85       return false;
86     }
87   const seq_no seq = d->seq();
88 
89   const_delta_iterator iter(delta_table);
90   while (iter.next())
91     {
92       if (iter->prev_seq() == seq)
93 	{
94 	  errormsg("%s: Specified SID has a successor.", name.c_str());
95 	  return false;
96 	}
97       if (is_seqlist_member(seq, iter->get_included_seqnos())
98 	  || is_seqlist_member(seq, iter->get_excluded_seqnos())
99 	  || is_seqlist_member(seq, iter->get_ignored_seqnos()))
100 	{
101 	  errormsg("%s: Specified SID is used in another delta.",
102 	       name.c_str());
103 	  return false;
104 	}
105     }
106 
107   d->set_type('R');
108 
109   FILE *out = start_update();
110   if (NULL == out)
111     return false;
112 
113   if (write(out))
114     {
115       xfile_error("Write error.");
116       return false;
117     }
118 
119   update_state state = COPY;
120   char c;
121 
122   bool retval = true;
123 
124   while (read_line(&c))
125     {
126       if (0 != c)
127 	{
128 	  check_arg();
129 	  if (strict_atous(plinebuf->c_str() + 3) == seq)
130 	    {
131 	      if (!next_state(state, c))
132 		{
133 		  corrupt("Unexpected control line");
134 		  retval = false;
135 		  break;
136 		}
137 	    }
138 	  else if (state == INSERT)
139 	    {
140 	      corrupt("Non-terminal delta!?!");
141 	      retval = false;
142 	      break;
143 	    }
144 	  else
145 	    {
146 	      fputs(plinebuf->c_str(), out);
147 	      putc('\n', out);
148 	    }
149 	}
150       else if (state != INSERT)
151 	{
152 	  fputs(plinebuf->c_str(), out);
153 	  putc('\n', out);
154 	}
155     }
156 
157   // We should end the file after an 'E', that is,
158   // in the 'COPY' state.
159   if (state != COPY)
160     {
161       corrupt("Unexpected EOF");
162       return false;
163     }
164   // Only finish write out the file if we had no problem.
165   if (true == retval)
166     {
167       retval = end_update(&out);
168     }
169   return retval;
170 }
171 
172 /* Local variables: */
173 /* mode: c++ */
174 /* End: */
175