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
|
/***************************************************************************
begin : Sat Sept 9 2003
edit : Tue Mar 20 2007
copyright : (C) 2003 by Jeroen Wijnhout, 2007 by Thomas Braun
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "kilelyxserver.h"
#include <sys/stat.h>
#include <stdlib.h> //getenv
#include <unistd.h> //read
#include <fcntl.h>
#include "kileactions.h"
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqsocketnotifier.h>
#include <tqregexp.h>
#include <tqdir.h>
#include "kiledebug.h"
#include <klocale.h>
KileLyxServer::KileLyxServer(bool startMe) :
m_perms( S_IRUSR | S_IWUSR ),m_running(false)
{
KILE_DEBUG() << "===KileLyxServer::KileLyxServer(bool" << startMe << ")===" << endl;
m_pipeIn.setAutoDelete(true);
m_notifier.setAutoDelete(true);
m_file.setAutoDelete(false);
m_tempDir = new KTempDir();
if(!m_tempDir)
return;
m_tempDir->setAutoDelete(true);
m_links << ".lyxpipe.in" << ".lyx/lyxpipe.in";
m_links << ".lyxpipe.out" << ".lyx/lyxpipe.out";
for(uint i = 0; i< m_links.count() ; i++)
{
m_pipes.append( m_tempDir->name() + m_links[i] );
m_links[i].prepend(TQDir::homeDirPath() + '/' );
KILE_DEBUG() << "m_pipes[" << i << "]=" << m_pipes[i] << endl;
KILE_DEBUG() << "m_links[" << i << "]=" << m_links[i] << endl;
}
if (startMe)
start();
}
KileLyxServer::~KileLyxServer()
{
stop();
removePipes();
delete m_tempDir;
}
bool KileLyxServer::start()
{
if (m_running)
stop();
KILE_DEBUG() << "Starting the LyX server..." << endl;
if (openPipes())
{
TQSocketNotifier *notifier;
TQPtrListIterator<TQFile> it(m_pipeIn);
while (it.current())
{
if ((*it)->name().right(3) == ".in" )
{
notifier = new TQSocketNotifier((*it)->handle(), TQSocketNotifier::Read, this);
connect(notifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(receive(int)));
m_notifier.append(notifier);
KILE_DEBUG() << "Created notifier for " << (*it)->name() << endl;
}
else
KILE_DEBUG() << "No notifier created for " << (*it)->name() << endl;
++it;
}
m_running=true;
}
return m_running;
}
bool KileLyxServer::openPipes()
{
KILE_DEBUG() << "===bool KileLyxServer::openPipes()===" << endl;
bool opened = false;
TQFileInfo pipeInfo,linkInfo;
TQFile *file;
struct stat buf;
struct stat *stats = &buf;
for (uint i=0; i < m_pipes.count(); ++i)
{
pipeInfo.setFile(m_pipes[i]);
linkInfo.setFile(m_links[i]);
TQFile::remove(linkInfo.absFilePath());
linkInfo.refresh();
if ( !pipeInfo.exists() )
{
//create the dir first
if ( !TQFileInfo(pipeInfo.dirPath(true)).exists() )
if ( mkdir(TQFile::encodeName( pipeInfo.dirPath() ), m_perms | S_IXUSR) == -1 )
{
kdError() << "Could not create directory for pipe" << endl;
continue;
}
else
KILE_DEBUG() << "Created directory " << pipeInfo.dirPath() << endl;
if ( mkfifo(TQFile::encodeName( pipeInfo.absFilePath() ), m_perms) != 0 )
{
kdError() << "Could not create pipe: " << pipeInfo.absFilePath() << endl;
continue;
}
else
KILE_DEBUG() << "Created pipe: " << pipeInfo.absFilePath() << endl;
}
if ( symlink(TQFile::encodeName(pipeInfo.absFilePath()),TQFile::encodeName(linkInfo.absFilePath())) != 0 )
{
kdError() << "Could not create symlink: " << linkInfo.absFilePath() << " --> " << pipeInfo.absFilePath() << endl;
continue;
}
file = new TQFile(pipeInfo.absFilePath());
pipeInfo.refresh();
if( pipeInfo.exists() && file->open(IO_ReadWrite) ) // in that order we don't create the file if it does not exist
{
KILE_DEBUG() << "Opened file: " << pipeInfo.absFilePath() << endl;
fstat(file->handle(),stats);
if( !S_ISFIFO(stats->st_mode) )
{
kdError() << "The file " << pipeInfo.absFilePath() << "we just created is not a pipe!" << endl;
file->close();
delete file;
continue;
}
else
{ // everything is correct :)
m_pipeIn.append(file);
m_file.insert(file->handle(),file);
opened=true;
}
}
else {
kdError() << "Could not open " << pipeInfo.absFilePath() << endl;
delete file;
}
}
return opened;
}
void KileLyxServer::stop()
{
KILE_DEBUG() << "Stopping the LyX server..." << endl;
TQPtrListIterator<TQFile> it(m_pipeIn);
while (it.current())
{
(*it)->close();
++it;
}
m_pipeIn.clear();
m_notifier.clear();
m_running=false;
}
void KileLyxServer::removePipes()
{
for ( uint i = 0; i < m_links.count(); ++i)
TQFile::remove(m_links[i]);
for ( uint i = 0; i < m_pipes.count(); ++i)
TQFile::remove(m_pipes[i]);
}
void KileLyxServer::processLine(const TQString &line)
{
KILE_DEBUG() << "===void KileLyxServer::processLine(const TQString " << line << ")===" << endl;
TQRegExp reCite(":citation-insert:(.*)$");
TQRegExp reBibtexdbadd(":bibtex-database-add:(.*)$");
TQRegExp rePaste(":paste:(.*)$");
if( line.find(reCite) != -1 )
emit(insert(KileAction::TagData(i18n("Cite"), "\\cite{"+reCite.cap(1)+'}')));
else if( line.find(reBibtexdbadd) != -1 )
emit(insert(KileAction::TagData(i18n("BibTeX db add"), "\\bibliography{"+ reBibtexdbadd.cap(1) + '}')));
else if( line.find(rePaste) != -1)
emit(insert(KileAction::TagData(i18n("Paste"), rePaste.cap(1))));
}
void KileLyxServer::receive(int fd)
{
if (m_file[fd])
{
int bytesRead;
int const size = 256;
char buffer[size];
if ((bytesRead = read(fd, buffer, size - 1)) > 0 )
{
buffer[bytesRead] = '\0'; // turn it into a c string
TQStringList cmds = TQStringList::split('\n', TQString(buffer).stripWhiteSpace());
for ( uint i = 0; i < cmds.count(); ++i )
processLine(cmds[i]);
}
}
}
#include "kilelyxserver.moc"
|