]> git.lyx.org Git - lyx.git/blob - src/vc-backend.C
prevent crash when inserting minipage in table cell,
[lyx.git] / src / vc-backend.C
1 #include <config.h>
2
3 #include "vc-backend.h"
4 #include "debug.h"
5 #include "buffer.h"
6 #include "frontends/LyXView.h"
7 #include "funcrequest.h"
8
9 #include "support/FileInfo.h"
10 #include "support/path.h"
11 #include "support/filetools.h"
12 #include "support/lstrings.h"
13 #include "support/systemcall.h"
14
15 #include <boost/regex.hpp>
16
17 #include <fstream>
18
19 using namespace lyx::support;
20
21 #ifndef CXX_GLOBAL_CSTD
22 using std::asctime;
23 using std::gmtime;
24 #endif
25
26 using std::endl;
27 using std::ifstream;
28 using std::getline;
29 using boost::regex;
30 using boost::regex_match;
31
32 #ifndef USE_INCLUDED_STRING
33 using boost::smatch;
34 #else
35 using boost::cmatch;
36 #endif
37
38
39 int VCS::doVCCommand(string const & cmd, string const & path)
40 {
41         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
42         Systemcall one;
43         Path p(path);
44         int const ret = one.startscript(Systemcall::Wait, cmd);
45         return ret;
46 }
47
48
49 RCS::RCS(string const & m)
50 {
51         master_ = m;
52         scanMaster();
53 }
54
55
56 string const RCS::find_file(string const & file)
57 {
58         string tmp(file);
59         // Check if *,v exists.
60         tmp += ",v";
61         FileInfo f;
62         lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
63                              << tmp << endl;
64         if (f.newFile(tmp).readable()) {
65                 lyxerr[Debug::LYXVC] << "Yes " << file
66                                      << " is under rcs." << endl;
67                 return tmp;
68         } else {
69                 // Check if RCS/*,v exists.
70                 tmp = AddName(AddPath(OnlyPath(file), "RCS"), file);
71                 tmp += ",v";
72                 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
73                                      << tmp << endl;
74                 if (f.newFile(tmp).readable()) {
75                         lyxerr[Debug::LYXVC] << "Yes " << file
76                                              << " it is under rcs."<< endl;
77                         return tmp;
78                 }
79         }
80         return string();
81 }
82
83
84 void RCS::retrieve(string const & file)
85 {
86         lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
87         VCS::doVCCommand("co -q -r \""
88                          + file + '"',
89                          string());
90 }
91
92
93 void RCS::scanMaster()
94 {
95         lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
96
97         ifstream ifs(master_.c_str());
98
99         string token;
100         bool read_enough = false;
101
102         while (!read_enough && ifs >> token) {
103                 lyxerr[Debug::LYXVC]
104                         << "LyXVC::scanMaster: current lex text: `"
105                         << token << '\'' << endl;
106
107                 if (token.empty())
108                         continue;
109                 else if (token == "head") {
110                         // get version here
111                         string tmv;
112                         ifs >> tmv;
113                         tmv = rtrim(tmv, ";");
114                         version_ = tmv;
115                         lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
116                                              << tmv << endl;
117                 } else if (contains(token, "access")
118                            || contains(token, "symbols")
119                            || contains(token, "strict")) {
120                         // nothing
121                 } else if (contains(token, "locks")) {
122                         // get locker here
123                         if (contains(token, ";")) {
124                                 locker_ = "Unlocked";
125                                 vcstatus = UNLOCKED;
126                                 continue;
127                         }
128                         string tmpt;
129                         string s1;
130                         string s2;
131                         do {
132                                 ifs >> tmpt;
133                                 s1 = rtrim(tmpt, ";");
134                                 // tmp is now in the format <user>:<version>
135                                 s1 = split(s1, s2, ':');
136                                 // s2 is user, and s1 is version
137                                 if (s1 == version_) {
138                                         locker_ = s2;
139                                         vcstatus = LOCKED;
140                                         break;
141                                 }
142                         } while (!contains(tmpt, ";"));
143
144                 } else if (token == "comment") {
145                         // we don't need to read any further than this.
146                         read_enough = true;
147                 } else {
148                         // unexpected
149                         lyxerr[Debug::LYXVC]
150                                 << "LyXVC::scanMaster(): unexpected token"
151                                 << endl;
152                 }
153         }
154 }
155
156
157 void RCS::registrer(string const & msg)
158 {
159         string cmd = "ci -q -u -i -t-\"";
160         cmd += msg;
161         cmd += "\" \"";
162         cmd += OnlyFilename(owner_->fileName());
163         cmd += '"';
164         doVCCommand(cmd, owner_->filePath());
165 }
166
167
168 void RCS::checkIn(string const & msg)
169 {
170         doVCCommand("ci -q -u -m\"" + msg + "\" \""
171                     + OnlyFilename(owner_->fileName()) + '"',
172                     owner_->filePath());
173 }
174
175
176 void RCS::checkOut()
177 {
178         owner_->markClean();
179         doVCCommand("co -q -l \""
180                     + OnlyFilename(owner_->fileName()) + '"',
181                     owner_->filePath());
182 }
183
184
185 void RCS::revert()
186 {
187         doVCCommand("co -f -u" + version() + " \""
188                     + OnlyFilename(owner_->fileName()) + '"',
189                     owner_->filePath());
190         // We ignore changes and just reload!
191         owner_->markClean();
192 }
193
194
195 void RCS::undoLast()
196 {
197         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
198         doVCCommand("rcs -o" + version() + " \""
199                     + OnlyFilename(owner_->fileName()) + '"',
200                     owner_->filePath());
201 }
202
203
204 void RCS::getLog(string const & tmpf)
205 {
206         doVCCommand("rlog \""
207                     + OnlyFilename(owner_->fileName()) + "\" > "
208                     + tmpf, owner_->filePath());
209 }
210
211
212 CVS::CVS(string const & m, string const & f)
213 {
214         master_ = m;
215         file_ = f;
216         scanMaster();
217 }
218
219
220 string const CVS::find_file(string const & file)
221 {
222         // First we look for the CVS/Entries in the same dir
223         // where we have file.
224         string const dir = OnlyPath(file) + "/CVS/Entries";
225         string const tmpf = "/" + OnlyFilename(file) + "/";
226         lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
227                              << "' for `" << tmpf << '\'' << endl;
228         FileInfo const f(dir);
229         if (f.readable()) {
230                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
231                 // and see if we can find this file. We do a fast and
232                 // dirty parse here.
233                 ifstream ifs(dir.c_str());
234                 string line;
235                 while (getline(ifs, line)) {
236                         lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
237                         if (contains(line, tmpf)) return dir;
238                 }
239         }
240         return string();
241 }
242
243
244 void CVS::scanMaster()
245 {
246         lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n     Checking: "
247                              << master_ << endl;
248         // Ok now we do the real scan...
249         ifstream ifs(master_.c_str());
250         string tmpf = "/" + OnlyFilename(file_) + "/";
251         lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
252         string line;
253         regex reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
254         while (getline(ifs, line)) {
255                 lyxerr[Debug::LYXVC] << "\t  line: " << line << endl;
256                 if (contains(line, tmpf)) {
257                         // Ok extract the fields.
258 #ifndef USE_INCLUDED_STRING
259                         smatch sm;
260 #else
261                         cmatch sm;
262 #endif
263                         regex_match(STRCONV(line), sm, reg);
264
265                         //sm[0]; // whole matched string
266                         //sm[1]; // filename
267                         version_ = STRCONV(sm.str(2));
268                         string const file_date = STRCONV(sm.str(3));
269
270                         //sm[4]; // options
271                         //sm[5]; // tag or tagdate
272                         FileInfo fi(file_);
273                         // FIXME: must double check file is stattable/existing
274                         time_t mod = fi.getModificationTime();
275                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
276                         lyxerr[Debug::LYXVC]
277                                 <<  "Date in Entries: `" << file_date
278                                 << "'\nModification date of file: `"
279                                 << mod_date << '\'' << endl;
280                         if (file_date == mod_date) {
281                                 locker_ = "Unlocked";
282                                 vcstatus = UNLOCKED;
283                         } else {
284                                 // Here we should also to some more checking
285                                 // to see if there are conflicts or not.
286                                 locker_ = "Locked";
287                                 vcstatus = LOCKED;
288                         }
289                         break;
290                 }
291         }
292 }
293
294
295 void CVS::registrer(string const & msg)
296 {
297         doVCCommand("cvs -q add -m \"" + msg + "\" \""
298                     + OnlyFilename(owner_->fileName()) + '"',
299                     owner_->filePath());
300 }
301
302
303 void CVS::checkIn(string const & msg)
304 {
305         doVCCommand("cvs -q commit -m \"" + msg + "\" \""
306                     + OnlyFilename(owner_->fileName()) + '"',
307                     owner_->filePath());
308 }
309
310
311 void CVS::checkOut()
312 {
313         // cvs update or perhaps for cvs this should be a noop
314         lyxerr << "Sorry not implemented." << endl;
315 }
316
317
318 void CVS::revert()
319 {
320         // Reverts to the version in CVS repository and
321         // gets the updated version from the repository.
322         string const fil = OnlyFilename(owner_->fileName());
323
324         doVCCommand("rm -f \"" + fil + "\"; cvs update \"" + fil + '"',
325                     owner_->filePath());
326         owner_->markClean();
327 }
328
329
330 void CVS::undoLast()
331 {
332         // merge the current with the previous version
333         // in a reverse patch kind of way, so that the
334         // result is to revert the last changes.
335         lyxerr << "Sorry not implemented." << endl;
336 }
337
338
339 void CVS::getLog(string const & tmpf)
340 {
341         doVCCommand("cvs log \""
342                     + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,
343                     owner_->filePath());
344 }