2 * Run cavern inside an Aven window
4 * Copyright (C) 2005,2006,2010,2011,2012,2014,2015,2016 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "cavernlog.h"
36 #ifdef HAVE_SYS_SELECT_H
37 #include <sys/select.h>
40 #include <sys/types.h>
43 #include <wx/process.h>
45 enum { LOG_REPROCESS
= 1234, LOG_SAVE
= 1235 };
47 static const wxString
badutf8_html(
48 wxT("<span style=\"color:white;background-color:red;\">�</span>"));
49 static const wxString
badutf8(wxUniChar(0xfffd));
51 // New event type for passing a chunk of cavern output from the worker thread
52 // to the main thread (or from the idle event handler if we're not using
54 class CavernOutputEvent
;
56 wxDEFINE_EVENT(wxEVT_CAVERN_OUTPUT
, CavernOutputEvent
);
58 class CavernOutputEvent
: public wxEvent
{
62 CavernOutputEvent() : wxEvent(0, wxEVT_CAVERN_OUTPUT
), len(0) { }
64 wxEvent
* Clone() const {
65 CavernOutputEvent
* e
= new CavernOutputEvent();
67 if (len
> 0) memcpy(e
->buf
, buf
, len
);
72 #ifdef CAVERNLOG_USE_THREADS
73 class CavernThread
: public wxThread
{
75 virtual ExitCode
Entry();
77 CavernLogWindow
*handler
;
82 CavernThread(CavernLogWindow
*handler_
, wxInputStream
* in_
)
83 : wxThread(wxTHREAD_DETACHED
), handler(handler_
), in(in_
) { }
86 wxCriticalSectionLocker
enter(handler
->thread_lock
);
87 handler
->thread
= NULL
;
95 CavernOutputEvent
* e
= new CavernOutputEvent();
96 in
->Read(e
->buf
, sizeof(e
->buf
));
97 size_t n
= in
->LastRead();
98 if (n
== 0 || TestDestroy()) {
100 return (wxThread::ExitCode
)0;
102 if (n
== 1 && e
->buf
[0] == '\n') {
103 // Don't send an event with just a blank line in.
104 in
->Read(e
->buf
+ 1, sizeof(e
->buf
) - 1);
108 return (wxThread::ExitCode
)0;
112 handler
->QueueEvent(e
);
119 CavernLogWindow::OnIdle(wxIdleEvent
& event
)
121 if (cavern_out
== NULL
) return;
123 wxInputStream
* in
= cavern_out
->GetInputStream();
125 if (!in
->CanRead()) {
126 // Avoid a tight busy-loop on idle events.
130 CavernOutputEvent
* e
= new CavernOutputEvent();
131 in
->Read(e
->buf
, sizeof(e
->buf
));
132 size_t n
= in
->LastRead();
145 BEGIN_EVENT_TABLE(CavernLogWindow
, wxHtmlWindow
)
146 EVT_BUTTON(LOG_REPROCESS
, CavernLogWindow::OnReprocess
)
147 EVT_BUTTON(LOG_SAVE
, CavernLogWindow::OnSave
)
148 EVT_BUTTON(wxID_OK
, CavernLogWindow::OnOK
)
149 EVT_COMMAND(wxID_ANY
, wxEVT_CAVERN_OUTPUT
, CavernLogWindow::OnCavernOutput
)
150 #ifdef CAVERNLOG_USE_THREADS
151 EVT_CLOSE(CavernLogWindow::OnClose
)
153 EVT_IDLE(CavernLogWindow::OnIdle
)
155 EVT_END_PROCESS(wxID_ANY
, CavernLogWindow::OnEndProcess
)
158 wxString
escape_for_shell(wxString s
, bool protect_dash
)
161 // Correct quoting rules are insane:
163 // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
165 // Thankfully wxExecute passes the command string to CreateProcess(), so
166 // at least we don't need to quote for cmd.exe too.
167 if (s
.empty() || s
.find_first_of(wxT(" \"\t\n\v")) != s
.npos
) {
169 s
.insert(0, wxT('"'));
170 for (size_t p
= 1; p
< s
.size(); ++p
) {
171 size_t backslashes
= 0;
172 while (s
[p
] == wxT('\\')) {
174 if (++p
== s
.size()) {
175 // Escape all the backslashes, since they're before
176 // the closing quote we add below.
177 s
.append(backslashes
, wxT('\\'));
182 if (s
[p
] == wxT('"')) {
183 // Escape any preceding backslashes and this quote.
184 s
.insert(p
, backslashes
+ 1, wxT('\\'));
185 p
+= backslashes
+ 1;
193 if (protect_dash
&& !s
.empty() && s
[0u] == '-') {
194 // If the filename starts with a '-', protect it from being
195 // treated as an option by prepending "./".
196 s
.insert(0, wxT("./"));
199 while (p
< s
.size()) {
200 // Exclude a few safe characters which are common in filenames
201 if (!isalnum(s
[p
]) && strchr("/._-", s
[p
]) == NULL
) {
202 s
.insert(p
, 1, wxT('\\'));
211 wxString
get_command_path(const wxChar
* command_name
)
220 buf
= (wchar_t*)osrealloc(buf
, len
* 2);
221 got
= GetModuleFileNameW(NULL
, buf
, len
);
222 if (got
< len
) break;
225 /* Strange Win32 nastiness - strip prefix "\\?\" if present */
226 wchar_t *start
= buf
;
227 if (wcsncmp(start
, L
"\\\\?\\", 4) == 0) start
+= 4;
228 wchar_t * slash
= wcsrchr(start
, L
'\\');
230 cmd
.assign(start
, slash
- start
+ 1);
235 wxString cmd
= wxString(msg_exepth(), wxConvUTF8
);
241 CavernLogWindow::CavernLogWindow(MainFrm
* mainfrm_
, const wxString
& survey_
, wxWindow
* parent
)
242 : wxHtmlWindow(parent
), mainfrm(mainfrm_
), cavern_out(NULL
),
243 link_count(0), end(buf
), init_done(false), survey(survey_
)
244 #ifdef CAVERNLOG_USE_THREADS
248 int fsize
= parent
->GetFont().GetPointSize();
249 int sizes
[7] = { fsize
, fsize
, fsize
, fsize
, fsize
, fsize
, fsize
};
250 SetFonts(wxString(), wxString(), sizes
);
253 CavernLogWindow::~CavernLogWindow()
255 #ifdef CAVERNLOG_USE_THREADS
256 if (thread
) stop_thread();
260 cavern_out
->Detach();
264 #ifdef CAVERNLOG_USE_THREADS
266 CavernLogWindow::stop_thread()
268 // Killing the subprocess by its pid is theoretically racy, but in practice
269 // it's not going to cause issues, and it's all the wxProcess API seems to
270 // allow us to do. If we don't kill the subprocess, we need to wait for it
271 // to write out some output - there seems to be no way to do the equivalent
272 // of select() with a timeout on a a wxInputStream.
274 // The only alternative to this seems to be to do:
276 // while (!s.CanRead()) {
277 // if (TestDestroy()) return (wxThread::ExitCode)0;
281 // But that makes the log window update sluggishly, and we're using a
282 // worker thread precisely to try to avoid having to do dumb stuff like
284 wxProcess::Kill(cavern_out
->GetPid());
287 wxCriticalSectionLocker
enter(thread_lock
);
290 #if wxCHECK_VERSION(2,9,2)
291 res
= thread
->Delete(NULL
, wxTHREAD_WAIT_BLOCK
);
293 res
= thread
->Delete();
295 if (res
!= wxTHREAD_NO_ERROR
) {
301 // Wait for thread to complete.
304 wxCriticalSectionLocker
enter(thread_lock
);
312 CavernLogWindow::OnClose(wxCloseEvent
&)
314 if (thread
) stop_thread();
320 CavernLogWindow::OnLinkClicked(const wxHtmlLinkInfo
&link
)
322 wxString href
= link
.GetHref();
323 wxString title
= link
.GetTarget();
324 size_t colon2
= href
.rfind(wxT(':'));
325 if (colon2
== wxString::npos
)
327 size_t colon
= href
.rfind(wxT(':'), colon2
- 1);
328 if (colon
== wxString::npos
)
331 wxString cmd
= wxT("notepad $f");
332 #elif defined __WXMAC__
333 wxString cmd
= wxT("open -t $f");
335 wxString cmd
= wxT("x-terminal-emulator -e vim +'call cursor($l,$c)' $f");
336 // wxString cmd = wxT("gedit -b $f +$l:$c $f");
337 // wxString cmd = wxT("x-terminal-emulator -e emacs +$l $f");
338 // wxString cmd = wxT("x-terminal-emulator -e nano +$l $f");
339 // wxString cmd = wxT("x-terminal-emulator -e jed -g $l $f");
341 wxChar
* p
= wxGetenv(wxT("SURVEXEDITOR"));
344 if (!cmd
.find(wxT("$f"))) {
349 while ((i
= cmd
.find(wxT('$'), i
)) != wxString::npos
) {
350 if (++i
>= cmd
.size()) break;
351 switch ((int)cmd
[i
]) {
356 wxString f
= escape_for_shell(href
.substr(0, colon
), true);
357 cmd
.replace(i
- 1, 2, f
);
362 wxString t
= escape_for_shell(title
);
363 cmd
.replace(i
- 1, 2, t
);
368 wxString l
= escape_for_shell(href
.substr(colon
+ 1, colon2
- colon
- 1));
369 cmd
.replace(i
- 1, 2, l
);
375 if (colon2
>= href
.size() - 1)
378 l
= escape_for_shell(href
.substr(colon2
+ 1));
379 cmd
.replace(i
- 1, 2, l
);
388 if (wxExecute(cmd
, wxEXEC_ASYNC
|wxEXEC_MAKE_GROUP_LEADER
) >= 0)
392 // TRANSLATORS: %s is replaced by the command we attempted to run.
393 m
.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd
.c_str());
395 m
+= wxString(strerror(errno
), wxConvUTF8
);
397 wxGetApp().ReportError(m
);
401 CavernLogWindow::process(const wxString
&file
)
404 #ifdef CAVERNLOG_USE_THREADS
405 if (thread
) stop_thread();
408 cavern_out
->Detach();
422 SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1"));
424 setenv("SURVEX_UTF8", "1", 1);
427 wxString escaped_file
= escape_for_shell(file
, true);
428 wxString cmd
= get_command_path(L
"cavern");
429 cmd
= escape_for_shell(cmd
, false);
435 cavern_out
= wxProcess::Open(cmd
);
438 m
.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd
.c_str());
440 m
+= wxString(strerror(errno
), wxConvUTF8
);
442 wxGetApp().ReportError(m
);
446 // We want to receive the wxProcessEvent when cavern exits.
447 cavern_out
->SetNextHandler(this);
449 #ifdef CAVERNLOG_USE_THREADS
450 thread
= new CavernThread(this, cavern_out
->GetInputStream());
451 if (thread
->Run() != wxTHREAD_NO_ERROR
) {
452 wxGetApp().ReportError(wxT("Thread failed to start"));
460 CavernLogWindow::OnCavernOutput(wxCommandEvent
& e_
)
462 CavernOutputEvent
& e
= (CavernOutputEvent
&)e_
;
466 if (size_t(n
) > sizeof(buf
) - (end
- buf
)) abort();
467 memcpy(end
, e
.buf
, n
);
468 log_txt
.append((const char *)end
, n
);
471 const unsigned char * p
= buf
;
476 // Decode multi-byte UTF-8 sequence.
478 // Invalid UTF-8 sequence.
480 } else if (ch
< 0xe0) {
481 /* 2 byte sequence */
483 // Incomplete UTF-8 sequence - try to read more.
487 if ((ch1
& 0xc0) != 0x80) {
488 // Invalid UTF-8 sequence.
491 ch
= ((ch
& 0x1f) << 6) | (ch1
& 0x3f);
492 } else if (ch
< 0xf0) {
493 /* 3 byte sequence */
495 // Incomplete UTF-8 sequence - try to read more.
499 ch
= ((ch
& 0x1f) << 12) | ((ch1
& 0x3f) << 6);
500 if ((ch1
& 0xc0) != 0x80) {
501 // Invalid UTF-8 sequence.
505 if ((ch2
& 0xc0) != 0x80) {
506 // Invalid UTF-8 sequence.
511 // Overlong UTF-8 sequence.
518 // Resync to next byte which starts a UTF-8 sequence.
520 if (*p
< 0x80 || (*p
>= 0xc0 && *p
< 0xf0)) break;
532 if (cur
.empty()) continue;
534 size_t colon
= cur
.find(':');
536 // If the path is "C:\path\to\file.svx" then don't split at the
537 // : after the drive letter! FIXME: better to look for ": "?
538 size_t colon
= cur
.find(':', 2);
540 if (colon
!= wxString::npos
&& colon
< cur
.size() - 2) {
543 while (i
< cur
.size() - 2 &&
544 cur
[i
] >= wxT('0') && cur
[i
] <= wxT('9')) {
547 if (i
> colon
&& cur
[i
] == wxT(':') ) {
549 // Check for column number.
550 while (++i
< cur
.size() - 2 &&
551 cur
[i
] >= wxT('0') && cur
[i
] <= wxT('9')) { }
552 bool have_column
= (i
> colon
+ 1 && cur
[i
] == wxT(':'));
556 // If there's no colon, include a trailing ':'
557 // so that we can unambiguously split the href
558 // value up into filename, line and column.
561 wxString tag
= wxT("<a href=\"");
562 tag
.append(cur
, 0, colon
);
563 while (cur
[++i
] == wxT(' ')) { }
564 tag
+= wxT("\" target=\"");
565 wxString
target(cur
, i
, wxString::npos
);
566 target
.Replace(badutf8_html
, badutf8
);
570 size_t offset
= colon
+ tag
.size();
571 cur
.insert(offset
, wxT("</a>"));
574 if (!have_column
) --offset
;
576 static const wxString
& error_marker
= wmsg(/*error*/93) + ":";
577 static const wxString
& warning_marker
= wmsg(/*warning*/4) + ":";
579 if (cur
.substr(offset
, error_marker
.size()) == error_marker
) {
580 // Show "error" marker in red.
581 cur
.insert(offset
, wxT("<span style=\"color:red\">"));
582 offset
+= 24 + error_marker
.size() - 1;
583 cur
.insert(offset
, wxT("</span>"));
584 } else if (cur
.substr(offset
, warning_marker
.size()) == warning_marker
) {
585 // Show "warning" marker in orange.
586 cur
.insert(offset
, wxT("<span style=\"color:orange\">"));
587 offset
+= 27 + warning_marker
.size() - 1;
588 cur
.insert(offset
, wxT("</span>"));
595 // Save the scrollbar positions.
596 int scroll_x
= 0, scroll_y
= 0;
597 GetViewStart(&scroll_x
, &scroll_y
);
599 cur
+= wxT("<br>\n");
603 // Auto-scroll the window until we've reported a
606 GetVirtualSize(&x
, &y
);
608 GetClientSize(&xs
, &ys
);
611 GetScrollPixelsPerUnit(&xu
, &yu
);
612 Scroll(scroll_x
, y
/ yu
);
614 // Restore the scrollbar positions.
615 Scroll(scroll_x
, scroll_y
);
635 cur
+= wxString::Format(wxT("&#%u;"), ch
);
642 size_t left
= end
- p
;
644 if (left
) memmove(buf
, p
, left
);
649 if (e
.len
<= 0 && buf
!= end
) {
650 // Truncated UTF-8 sequence.
654 /* TRANSLATORS: Label for button in aven’s cavern log window which
655 * allows the user to save the log to a file. */
656 AppendToPage(wxString::Format(wxT("<avenbutton id=%d name=\"%s\">"),
658 wmsg(/*Save Log*/446).c_str()));
663 /* Negative length indicates non-zero exit status from cavern. */
664 /* TRANSLATORS: Label for button in aven’s cavern log window which
665 * causes the survey data to be reprocessed. */
666 AppendToPage(wxString::Format(wxT("<avenbutton default id=%d name=\"%s\">"),
668 wmsg(/*Reprocess*/184).c_str()));
671 AppendToPage(wxString::Format(wxT("<avenbutton id=%d name=\"%s\">"),
673 wmsg(/*Reprocess*/184).c_str()));
674 AppendToPage(wxString::Format(wxT("<avenbutton default id=%d>"), (int)wxID_OK
));
679 wxString
file3d(filename
, 0, filename
.length() - 3);
680 file3d
.append(wxT("3d"));
681 if (!mainfrm
->LoadData(file3d
, survey
)) {
686 if (link_count
== 0) {
687 wxCommandEvent dummy
;
693 CavernLogWindow::OnEndProcess(wxProcessEvent
& evt
)
695 CavernOutputEvent
* e
= new CavernOutputEvent();
696 // Zero length indicates successful exit, negative length unsuccessful exit.
697 e
->len
= (evt
.GetExitCode() == 0 ? 0 : -1);
702 CavernLogWindow::OnReprocess(wxCommandEvent
&)
708 CavernLogWindow::OnSave(wxCommandEvent
&)
710 wxString
filelog(filename
, 0, filename
.length() - 3);
711 filelog
+= wxT("log");
712 AvenAllowOnTop
ontop(mainfrm
);
714 wxString
ext(wxT("*.log"));
716 /* TRANSLATORS: Log files from running cavern (extension .log) */
717 wxString ext
= wmsg(/*Log files*/447);
718 ext
+= wxT("|*.log");
720 wxFileDialog
dlg(this, wmsg(/*Select an output filename*/319),
721 wxString(), filelog
, ext
,
722 wxFD_SAVE
|wxFD_OVERWRITE_PROMPT
);
723 if (dlg
.ShowModal() != wxID_OK
) return;
724 filelog
= dlg
.GetPath();
725 FILE * fh_log
= wxFopen(filelog
, wxT("w"));
727 wxGetApp().ReportError(wxString::Format(wmsg(/*Error writing to file “%s”*/110), filelog
.c_str()));
730 fwrite(log_txt
.data(), log_txt
.size(), 1, fh_log
);
735 CavernLogWindow::OnOK(wxCommandEvent
&)
738 mainfrm
->HideLog(this);
740 mainfrm
->InitialiseAfterLoad(filename
, survey
);