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