-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandWindow.cpp
More file actions
359 lines (292 loc) · 10 KB
/
CommandWindow.cpp
File metadata and controls
359 lines (292 loc) · 10 KB
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
// Command window object
// Executes a command and returns the results in the command window
// Close button to close the window (but not kill the child process)
// Cancel button to kill the child process (but not close the window)
// The object deletes itself when the close button is pressed
// The command window can be a free-floating window or can be
// a window which will always float over the owner window
#include "config.h"
#include "i18n.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <fx.h>
#include "xfedefs.h"
#include "icons.h"
#include "MessageBox.h"
#include "CommandWindow.h"
// Map
FXDEFMAP(CommandWindow) CommandWindowMap[] =
{
FXMAPFUNC(SEL_COMMAND, CommandWindow::ID_CLOSE, CommandWindow::onCmdClose),
FXMAPFUNC(SEL_COMMAND, CommandWindow::ID_KILLPROCESS, CommandWindow::onCmdKillProcess),
FXMAPFUNC(SEL_UPDATE, CommandWindow::ID_KILLPROCESS, CommandWindow::onUpdKillProcess),
FXMAPFUNC(SEL_UPDATE, CommandWindow::ID_CLOSE, CommandWindow::onUpdClose),
FXMAPFUNC(SEL_CHORE, CommandWindow::ID_WATCHPROCESS, CommandWindow::onWatchProcess),
};
// Object implementation
FXIMPLEMENT(CommandWindow, DialogBox, CommandWindowMap, ARRAYNUMBER(CommandWindowMap))
// Construct window which will always float over the owner window
CommandWindow::CommandWindow(FXWindow* owner, const FXString& name, FXString strcmd, int nblines, int nbcols) :
DialogBox(owner, name, DECOR_TITLE|DECOR_BORDER|DECOR_RESIZE|DECOR_MAXIMIZE|DECOR_CLOSE, 0, 0, 0, 0, 6, 6, 6, 6, 4, 4)
{
// Get command to execute
command = strcmd;
// Bottom part
FXHorizontalFrame* buttonbox = new FXHorizontalFrame(this, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|PACK_UNIFORM_WIDTH);
new FXButton(buttonbox, _("Cl&ose"), NULL, this, ID_CLOSE, BUTTON_DEFAULT|LAYOUT_RIGHT|FRAME_RAISED|FRAME_THICK, 0, 0, 0, 0, 20, 20, 5, 5);
FXButton* cancelbutton = new FXButton(buttonbox, _("&Cancel"), NULL, this, ID_KILLPROCESS, BUTTON_INITIAL|BUTTON_DEFAULT|LAYOUT_RIGHT|FRAME_RAISED|FRAME_THICK, 0, 0, 0, 0, 20, 20, 5, 5);
// Text part
FXHorizontalFrame* textbox = new FXHorizontalFrame(this, LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN, 0, 0, 0, 0, 0, 0, 0, 0);
text = new FXText(textbox, NULL, 0, TEXT_READONLY|TEXT_WORDWRAP|LAYOUT_FILL_X|LAYOUT_FILL_Y);
text->setVisibleRows(nblines);
text->setVisibleColumns(nbcols);
appendText(_("Please wait...\n\n"));
cancelbutton->setFocus();
// Initialize variables
pid = -1;
killed = false;
closed = false;
}
// Construct free-floating window
CommandWindow::CommandWindow(FXApp* a, const FXString& name, FXString strcmd, int nblines, int nbcols) :
DialogBox(a, name, DECOR_TITLE|DECOR_BORDER|DECOR_RESIZE|DECOR_MAXIMIZE|DECOR_MINIMIZE|DECOR_CLOSE, 0, 0, 0, 0, 6, 6, 6, 6, 4, 4)
{
// Get command to execute
command = strcmd;
// Bottom part
FXHorizontalFrame* buttonbox = new FXHorizontalFrame(this, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|PACK_UNIFORM_WIDTH);
new FXButton(buttonbox, _("Cl&ose"), NULL, this, ID_CLOSE, BUTTON_DEFAULT|LAYOUT_RIGHT|FRAME_RAISED|FRAME_THICK, 0, 0, 0, 0, 20, 20, 5, 5);
FXButton* cancelbutton = new FXButton(buttonbox, _("&Cancel"), NULL, this, ID_KILLPROCESS, BUTTON_INITIAL|BUTTON_DEFAULT|LAYOUT_RIGHT|FRAME_RAISED|FRAME_THICK, 0, 0, 0, 0, 20, 20, 5, 5);
// Text part
FXHorizontalFrame* textbox = new FXHorizontalFrame(this, LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN, 0, 0, 0, 0, 0, 0, 0, 0);
text = new FXText(textbox, NULL, 0, TEXT_READONLY|TEXT_WORDWRAP|LAYOUT_FILL_X|LAYOUT_FILL_Y);
text->setVisibleRows(nblines);
text->setVisibleColumns(nbcols);
appendText(_("Please wait...\n\n"));
cancelbutton->setFocus();
// Initialize variables
pid = -1;
killed = false;
closed = false;
}
// Make window
void CommandWindow::create()
{
// Set text font
FXString fontspec;
fontspec = getApp()->reg().readStringEntry("SETTINGS", "textfont", DEFAULT_TEXT_FONT);
if (!fontspec.empty())
{
FXFont* font = new FXFont(getApp(), fontspec);
font->create();
text->setFont(font);
}
DialogBox::create();
show(PLACEMENT_OWNER);
// Execute command
execCmd(command.text());
}
// Kill process when clicking on the cancel button
long CommandWindow::onCmdKillProcess(FXObject*, FXSelector, void*)
{
kill((-1*pid), SIGTERM); // Kills the process group
killed = true;
return(0);
}
// Update cancel button
long CommandWindow::onUpdKillProcess(FXObject* sender, FXSelector, void*)
{
FXButton* btn = (FXButton*)sender;
if (!getApp()->hasChore(this, ID_WATCHPROCESS))
{
btn->disable();
}
else
{
btn->enable();
}
return(1);
}
// Update close button
long CommandWindow::onUpdClose(FXObject* sender, FXSelector, void*)
{
FXButton* btn = (FXButton*)sender;
if (!getApp()->hasChore(this, ID_WATCHPROCESS))
{
btn->enable();
}
else
{
btn->disable();
}
return(1);
}
// Execute a command and capture its output
int CommandWindow::execCmd(FXString command)
{
// Open pipes to communicate with child process
if (pipe(pipes) == -1)
{
return(-1);
}
// Create child process
pid = fork();
if (pid == -1)
{
fprintf(stderr, _("Error: Fork failed: %s\n"), strerror(errno));
return(-1);
}
if (pid == 0) // Child
{
char* args[4];
int ret1 = dup2(pipes[0], STDIN_FILENO); // Use the pipes as the new channels
int ret2 = dup2(pipes[1], STDOUT_FILENO); // (where stdout and stderr
int ret3 = dup2(pipes[1], STDERR_FILENO); // go to the same pipe!).
if ((ret1 < 0) || (ret2 < 0) || (ret3 < 0))
{
int errcode = errno;
if (errcode)
{
MessageBox::error(this, BOX_OK, _("Error"), _("Can't duplicate pipes: %s"), strerror(errcode));
}
else
{
MessageBox::error(this, BOX_OK, _("Error"), _("Can't duplicate pipes"));
}
return(-1);
}
args[0] = (char*)"sh"; // Setup arguments
args[1] = (char*)"-vc"; // to run command (option -v to display the command to execute)
args[2] = (char*)command.text(); // in a shell in
args[3] = NULL; // a new process.
setpgid(0, 0); // Allows to kill the whole group
execvp(args[0], args); // Start a new process which will execute the command.
_exit(EXIT_FAILURE); // We'll get here only if an error occurred.
}
else // Parent
{
// Make sure we get called so we can check when child has finished
getApp()->addChore(this, ID_WATCHPROCESS);
}
return(0);
}
// Watch progress of child process
long CommandWindow::onWatchProcess(FXObject*, FXSelector, void*)
{
char buf[1024];
int nread;
if (closed)
{
// The close button was pressed : just close the pipes
// and delete the object
// Close pipes
::close(pipes[0]);
::close(pipes[1]);
// Object deletes itself!
delete this;
}
else if ((waitpid(pid, NULL, WNOHANG) == 0))
{
// Child is still running, just wait
getApp()->addChore(this, ID_WATCHPROCESS);
// Read data from the running child (first, set I-O to non-blocking)
int pflags;
if ((pflags = fcntl(pipes[0], F_GETFL)) >= 0)
{
pflags |= O_NONBLOCK;
if (fcntl(pipes[0], F_SETFL, pflags) >= 0)
{
// Now read the data from the pipe
while ((nread = read(pipes[0], buf, sizeof(buf)-1)) > 0)
{
buf[nread] = '\0';
// Remove backspace characters, if any
FXString strbuf = buf;
strbuf = strbuf.substitute("\b", ".");
text->appendText(strbuf.text(), strlen(strbuf.text()));
scrollToLastLine();
if (nread < (int)(sizeof(buf)-1))
{
break;
}
}
}
}
}
else
{
// Child has finished.
// Read data from the finished child
while ((nread = read(pipes[0], buf, sizeof(buf)-1)) > 0)
{
buf[nread] = '\0';
// Remove backspace characters, if any
FXString strbuf = buf;
strbuf = strbuf.substitute("\b", ".");
text->appendText(strbuf.text(), strlen(strbuf.text()));
scrollToLastLine();
if (nread < (int)(sizeof(buf)-1))
{
break;
}
}
if (killed)
{
appendText(_("\n>>>> COMMAND CANCELLED <<<<"));
}
else
{
appendText(_("\n>>>> END OF COMMAND <<<<"));
}
scrollToLastLine();
// Close pipes
::close(pipes[0]);
::close(pipes[1]);
}
return(1);
}
// Close dialog when clicking on the close button
long CommandWindow::onCmdClose(FXObject*, FXSelector, void*)
{
getApp()->stopModal(this, true);
hide();
closed = true;
// Object deletes itself
delete this;
return(1);
}
// Change the text in the buffer to new text
void CommandWindow::setText(const char* str)
{
text->setText(str, strlen(str));
getApp()->repaint();
}
// Append new text at the end of the buffer
void CommandWindow::appendText(const char* str)
{
text->appendText(str, strlen(str));
getApp()->repaint();
}
// Scroll to the last line
void CommandWindow::scrollToLastLine(void)
{
text->makePositionVisible(text->getLength());
getApp()->repaint();
}
// Get text length
int CommandWindow::getLength(void)
{
return(text->getLength());
}
// Clean up
CommandWindow::~CommandWindow()
{
getApp()->removeChore(this, ID_WATCHPROCESS);
text = (FXText*)-1;
}