[6bec10c] | 1 | /* cavernlog.cc |
---|
| 2 | * Run cavern inside an Aven window |
---|
| 3 | * |
---|
[2a00719] | 4 | * Copyright (C) 2005,2006,2010,2011,2012,2014,2015,2016 Olly Betts |
---|
[6bec10c] | 5 | * |
---|
| 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. |
---|
| 10 | * |
---|
| 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. |
---|
| 15 | * |
---|
| 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 |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #ifdef HAVE_CONFIG_H |
---|
| 22 | # include <config.h> |
---|
| 23 | #endif |
---|
| 24 | |
---|
| 25 | #include "aven.h" |
---|
| 26 | #include "cavernlog.h" |
---|
| 27 | #include "filename.h" |
---|
[fb5887c] | 28 | #include "mainfrm.h" |
---|
[6bec10c] | 29 | #include "message.h" |
---|
| 30 | |
---|
[baeae66] | 31 | #include <errno.h> |
---|
[6bec10c] | 32 | #include <stdio.h> |
---|
[a90632c] | 33 | #include <stdlib.h> |
---|
[6bec10c] | 34 | |
---|
| 35 | // For select(): |
---|
| 36 | #ifdef HAVE_SYS_SELECT_H |
---|
| 37 | #include <sys/select.h> |
---|
| 38 | #endif |
---|
| 39 | #include <sys/time.h> |
---|
| 40 | #include <sys/types.h> |
---|
| 41 | #include <unistd.h> |
---|
| 42 | |
---|
[15ba0b5] | 43 | #include <wx/process.h> |
---|
| 44 | |
---|
[0e81a88] | 45 | enum { LOG_REPROCESS = 1234, LOG_SAVE = 1235 }; |
---|
[fb5887c] | 46 | |
---|
[e768f29] | 47 | static const wxString badutf8_html( |
---|
| 48 | wxT("<span style=\"color:white;background-color:red;\">�</span>")); |
---|
| 49 | static const wxString badutf8(wxUniChar(0xfffd)); |
---|
| 50 | |
---|
[fc626ae] | 51 | // New event type for passing a chunk of cavern output from the worker thread |
---|
[15ba0b5] | 52 | // to the main thread (or from the idle event handler if we're not using |
---|
| 53 | // threads). |
---|
[fc626ae] | 54 | class CavernOutputEvent; |
---|
| 55 | |
---|
| 56 | wxDEFINE_EVENT(wxEVT_CAVERN_OUTPUT, CavernOutputEvent); |
---|
| 57 | |
---|
| 58 | class CavernOutputEvent : public wxEvent { |
---|
| 59 | public: |
---|
| 60 | char buf[1000]; |
---|
| 61 | int len; |
---|
| 62 | CavernOutputEvent() : wxEvent(0, wxEVT_CAVERN_OUTPUT), len(0) { } |
---|
| 63 | |
---|
| 64 | wxEvent * Clone() const { |
---|
| 65 | CavernOutputEvent * e = new CavernOutputEvent(); |
---|
| 66 | e->len = len; |
---|
| 67 | if (len > 0) memcpy(e->buf, buf, len); |
---|
| 68 | return e; |
---|
| 69 | } |
---|
| 70 | }; |
---|
| 71 | |
---|
[15ba0b5] | 72 | #ifdef CAVERNLOG_USE_THREADS |
---|
[fc626ae] | 73 | class CavernThread : public wxThread { |
---|
| 74 | protected: |
---|
| 75 | virtual ExitCode Entry(); |
---|
| 76 | |
---|
| 77 | CavernLogWindow *handler; |
---|
| 78 | |
---|
[15ba0b5] | 79 | wxInputStream * in; |
---|
[fc626ae] | 80 | |
---|
| 81 | public: |
---|
[15ba0b5] | 82 | CavernThread(CavernLogWindow *handler_, wxInputStream * in_) |
---|
| 83 | : wxThread(wxTHREAD_DETACHED), handler(handler_), in(in_) { } |
---|
[fc626ae] | 84 | |
---|
| 85 | ~CavernThread() { |
---|
| 86 | wxCriticalSectionLocker enter(handler->thread_lock); |
---|
| 87 | handler->thread = NULL; |
---|
| 88 | } |
---|
| 89 | }; |
---|
| 90 | |
---|
| 91 | wxThread::ExitCode |
---|
| 92 | CavernThread::Entry() |
---|
| 93 | { |
---|
[15ba0b5] | 94 | while (true) { |
---|
| 95 | CavernOutputEvent * e = new CavernOutputEvent(); |
---|
| 96 | in->Read(e->buf, sizeof(e->buf)); |
---|
| 97 | size_t n = in->LastRead(); |
---|
| 98 | if (n == 0 || TestDestroy()) { |
---|
| 99 | delete e; |
---|
| 100 | return (wxThread::ExitCode)0; |
---|
| 101 | } |
---|
| 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); |
---|
| 105 | n += in->LastRead(); |
---|
| 106 | if (TestDestroy()) { |
---|
| 107 | delete e; |
---|
| 108 | return (wxThread::ExitCode)0; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | e->len = n; |
---|
| 112 | handler->QueueEvent(e); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | #else |
---|
| 117 | |
---|
| 118 | void |
---|
| 119 | CavernLogWindow::OnIdle(wxIdleEvent& event) |
---|
| 120 | { |
---|
| 121 | if (cavern_out == NULL) return; |
---|
| 122 | |
---|
| 123 | wxInputStream * in = cavern_out->GetInputStream(); |
---|
| 124 | |
---|
| 125 | if (!in->CanRead()) { |
---|
| 126 | // Avoid a tight busy-loop on idle events. |
---|
| 127 | wxMilliSleep(10); |
---|
| 128 | } |
---|
| 129 | if (in->CanRead()) { |
---|
[fc626ae] | 130 | CavernOutputEvent * e = new CavernOutputEvent(); |
---|
[15ba0b5] | 131 | in->Read(e->buf, sizeof(e->buf)); |
---|
| 132 | size_t n = in->LastRead(); |
---|
| 133 | if (n == 0) { |
---|
[fc626ae] | 134 | delete e; |
---|
[15ba0b5] | 135 | return; |
---|
[fc626ae] | 136 | } |
---|
[15ba0b5] | 137 | e->len = n; |
---|
| 138 | QueueEvent(e); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | event.RequestMore(); |
---|
[fc626ae] | 142 | } |
---|
| 143 | #endif |
---|
| 144 | |
---|
[fb5887c] | 145 | BEGIN_EVENT_TABLE(CavernLogWindow, wxHtmlWindow) |
---|
[81e1aa4] | 146 | EVT_BUTTON(LOG_REPROCESS, CavernLogWindow::OnReprocess) |
---|
[0e81a88] | 147 | EVT_BUTTON(LOG_SAVE, CavernLogWindow::OnSave) |
---|
[fb5887c] | 148 | EVT_BUTTON(wxID_OK, CavernLogWindow::OnOK) |
---|
[15ba0b5] | 149 | EVT_COMMAND(wxID_ANY, wxEVT_CAVERN_OUTPUT, CavernLogWindow::OnCavernOutput) |
---|
[0415bac] | 150 | #ifdef CAVERNLOG_USE_THREADS |
---|
[fc626ae] | 151 | EVT_CLOSE(CavernLogWindow::OnClose) |
---|
| 152 | #else |
---|
[8991d7f] | 153 | EVT_IDLE(CavernLogWindow::OnIdle) |
---|
[fc626ae] | 154 | #endif |
---|
[15ba0b5] | 155 | EVT_END_PROCESS(wxID_ANY, CavernLogWindow::OnEndProcess) |
---|
[fb5887c] | 156 | END_EVENT_TABLE() |
---|
| 157 | |
---|
[549eb37] | 158 | wxString escape_for_shell(wxString s, bool protect_dash) |
---|
[6bec10c] | 159 | { |
---|
| 160 | #ifdef __WXMSW__ |
---|
[faf83be] | 161 | // Correct quoting rules are insane: |
---|
| 162 | // |
---|
[764fe32] | 163 | // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx |
---|
[faf83be] | 164 | // |
---|
| 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) { |
---|
| 168 | // Need to quote. |
---|
| 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('\\')) { |
---|
| 173 | ++backslashes; |
---|
| 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('\\')); |
---|
| 178 | goto done; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if (s[p] == wxT('"')) { |
---|
| 183 | // Escape any preceding backslashes and this quote. |
---|
| 184 | s.insert(p, backslashes + 1, wxT('\\')); |
---|
| 185 | p += backslashes + 1; |
---|
[eff69a7] | 186 | } |
---|
[6bec10c] | 187 | } |
---|
[faf83be] | 188 | done: |
---|
| 189 | s.append(wxT('"')); |
---|
[6bec10c] | 190 | } |
---|
| 191 | #else |
---|
[faf83be] | 192 | size_t p = 0; |
---|
[6bec10c] | 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 "./". |
---|
[5627cbb] | 196 | s.insert(0, wxT("./")); |
---|
[6bec10c] | 197 | p = 2; |
---|
| 198 | } |
---|
| 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) { |
---|
[6baad4a] | 202 | s.insert(p, 1, wxT('\\')); |
---|
[6bec10c] | 203 | ++p; |
---|
| 204 | } |
---|
| 205 | ++p; |
---|
| 206 | } |
---|
| 207 | #endif |
---|
| 208 | return s; |
---|
| 209 | } |
---|
| 210 | |
---|
[549eb37] | 211 | wxString get_command_path(const wxChar * command_name) |
---|
| 212 | { |
---|
| 213 | #ifdef __WXMSW__ |
---|
| 214 | wxString cmd; |
---|
| 215 | { |
---|
| 216 | DWORD len = 256; |
---|
| 217 | wchar_t *buf = NULL; |
---|
| 218 | while (1) { |
---|
| 219 | DWORD got; |
---|
| 220 | buf = (wchar_t*)osrealloc(buf, len * 2); |
---|
| 221 | got = GetModuleFileNameW(NULL, buf, len); |
---|
| 222 | if (got < len) break; |
---|
| 223 | len += len; |
---|
| 224 | } |
---|
| 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'\\'); |
---|
| 229 | if (slash) { |
---|
| 230 | cmd.assign(start, slash - start + 1); |
---|
| 231 | } |
---|
| 232 | osfree(buf); |
---|
| 233 | } |
---|
| 234 | #else |
---|
| 235 | wxString cmd = wxString(msg_exepth(), wxConvUTF8); |
---|
| 236 | #endif |
---|
| 237 | cmd += command_name; |
---|
| 238 | return cmd; |
---|
| 239 | } |
---|
| 240 | |
---|
[d7b53e3] | 241 | CavernLogWindow::CavernLogWindow(MainFrm * mainfrm_, const wxString & survey_, wxWindow * parent) |
---|
[8991d7f] | 242 | : wxHtmlWindow(parent), mainfrm(mainfrm_), cavern_out(NULL), |
---|
| 243 | link_count(0), end(buf), init_done(false), survey(survey_) |
---|
[0415bac] | 244 | #ifdef CAVERNLOG_USE_THREADS |
---|
[fc626ae] | 245 | , thread(NULL) |
---|
| 246 | #endif |
---|
[fb5887c] | 247 | { |
---|
[93ff5cc] | 248 | int fsize = parent->GetFont().GetPointSize(); |
---|
| 249 | int sizes[7] = { fsize, fsize, fsize, fsize, fsize, fsize, fsize }; |
---|
[5627cbb] | 250 | SetFonts(wxString(), wxString(), sizes); |
---|
[93ff5cc] | 251 | } |
---|
| 252 | |
---|
[8991d7f] | 253 | CavernLogWindow::~CavernLogWindow() |
---|
| 254 | { |
---|
[15ba0b5] | 255 | #ifdef CAVERNLOG_USE_THREADS |
---|
| 256 | if (thread) stop_thread(); |
---|
| 257 | #endif |
---|
[c1144fe] | 258 | if (cavern_out) { |
---|
| 259 | wxEndBusyCursor(); |
---|
[15ba0b5] | 260 | cavern_out->Detach(); |
---|
[c1144fe] | 261 | } |
---|
[8991d7f] | 262 | } |
---|
| 263 | |
---|
[0415bac] | 264 | #ifdef CAVERNLOG_USE_THREADS |
---|
[fc626ae] | 265 | void |
---|
| 266 | CavernLogWindow::stop_thread() |
---|
| 267 | { |
---|
[15ba0b5] | 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. |
---|
| 273 | // |
---|
| 274 | // The only alternative to this seems to be to do: |
---|
| 275 | // |
---|
| 276 | // while (!s.CanRead()) { |
---|
| 277 | // if (TestDestroy()) return (wxThread::ExitCode)0; |
---|
| 278 | // wxMilliSleep(N); |
---|
| 279 | // } |
---|
| 280 | // |
---|
| 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 |
---|
| 283 | // this. |
---|
| 284 | wxProcess::Kill(cavern_out->GetPid()); |
---|
| 285 | |
---|
[fc626ae] | 286 | { |
---|
| 287 | wxCriticalSectionLocker enter(thread_lock); |
---|
| 288 | if (thread) { |
---|
[2a00719] | 289 | wxThreadError res; |
---|
| 290 | #if wxCHECK_VERSION(2,9,2) |
---|
| 291 | res = thread->Delete(NULL, wxTHREAD_WAIT_BLOCK); |
---|
| 292 | #else |
---|
| 293 | res = thread->Delete(); |
---|
| 294 | #endif |
---|
| 295 | if (res != wxTHREAD_NO_ERROR) { |
---|
[fc626ae] | 296 | // FIXME |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | // Wait for thread to complete. |
---|
| 302 | while (true) { |
---|
| 303 | { |
---|
| 304 | wxCriticalSectionLocker enter(thread_lock); |
---|
| 305 | if (!thread) break; |
---|
| 306 | } |
---|
| 307 | wxMilliSleep(1); |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | void |
---|
| 312 | CavernLogWindow::OnClose(wxCloseEvent &) |
---|
| 313 | { |
---|
| 314 | if (thread) stop_thread(); |
---|
| 315 | Destroy(); |
---|
| 316 | } |
---|
| 317 | #endif |
---|
| 318 | |
---|
[6bec10c] | 319 | void |
---|
| 320 | CavernLogWindow::OnLinkClicked(const wxHtmlLinkInfo &link) |
---|
| 321 | { |
---|
| 322 | wxString href = link.GetHref(); |
---|
| 323 | wxString title = link.GetTarget(); |
---|
[544a46c] | 324 | size_t colon2 = href.rfind(wxT(':')); |
---|
| 325 | if (colon2 == wxString::npos) |
---|
| 326 | return; |
---|
| 327 | size_t colon = href.rfind(wxT(':'), colon2 - 1); |
---|
[3d3fb6c] | 328 | if (colon == wxString::npos) |
---|
| 329 | return; |
---|
[6bec10c] | 330 | #ifdef __WXMSW__ |
---|
[3d3fb6c] | 331 | wxString cmd = wxT("notepad $f"); |
---|
[89a1b72] | 332 | #elif defined __WXMAC__ |
---|
[3d3fb6c] | 333 | wxString cmd = wxT("open -t $f"); |
---|
[6bec10c] | 334 | #else |
---|
[f1e3ed3] | 335 | wxString cmd = wxT("x-terminal-emulator -e vim +'call cursor($l,$c)' $f"); |
---|
[3d3fb6c] | 336 | // wxString cmd = wxT("gedit -b $f +$l:$c $f"); |
---|
[f1e3ed3] | 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"); |
---|
[6bec10c] | 340 | #endif |
---|
[b8ba399] | 341 | wxChar * p = wxGetenv(wxT("SURVEXEDITOR")); |
---|
[3d3fb6c] | 342 | if (p) { |
---|
[b8ba399] | 343 | cmd = p; |
---|
[3d3fb6c] | 344 | if (!cmd.find(wxT("$f"))) { |
---|
| 345 | cmd += wxT(" $f"); |
---|
[6bec10c] | 346 | } |
---|
[3d3fb6c] | 347 | } |
---|
| 348 | size_t i = 0; |
---|
| 349 | while ((i = cmd.find(wxT('$'), i)) != wxString::npos) { |
---|
| 350 | if (++i >= cmd.size()) break; |
---|
| 351 | switch ((int)cmd[i]) { |
---|
| 352 | case wxT('$'): |
---|
| 353 | cmd.erase(i, 1); |
---|
| 354 | break; |
---|
| 355 | case wxT('f'): { |
---|
| 356 | wxString f = escape_for_shell(href.substr(0, colon), true); |
---|
| 357 | cmd.replace(i - 1, 2, f); |
---|
| 358 | i += f.size() - 1; |
---|
| 359 | break; |
---|
| 360 | } |
---|
| 361 | case wxT('t'): { |
---|
| 362 | wxString t = escape_for_shell(title); |
---|
| 363 | cmd.replace(i - 1, 2, t); |
---|
| 364 | i += t.size() - 1; |
---|
| 365 | break; |
---|
| 366 | } |
---|
| 367 | case wxT('l'): { |
---|
| 368 | wxString l = escape_for_shell(href.substr(colon + 1, colon2 - colon - 1)); |
---|
| 369 | cmd.replace(i - 1, 2, l); |
---|
| 370 | i += l.size() - 1; |
---|
| 371 | break; |
---|
[1d71195] | 372 | } |
---|
[3d3fb6c] | 373 | case wxT('c'): { |
---|
| 374 | wxString l; |
---|
[b2b852d] | 375 | if (colon2 >= href.size() - 1) |
---|
[3d3fb6c] | 376 | l = wxT("0"); |
---|
| 377 | else |
---|
| 378 | l = escape_for_shell(href.substr(colon2 + 1)); |
---|
| 379 | cmd.replace(i - 1, 2, l); |
---|
| 380 | i += l.size() - 1; |
---|
| 381 | break; |
---|
| 382 | } |
---|
| 383 | default: |
---|
| 384 | ++i; |
---|
[6bec10c] | 385 | } |
---|
[3d3fb6c] | 386 | } |
---|
[faf83be] | 387 | |
---|
| 388 | if (wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER) >= 0) |
---|
[3d3fb6c] | 389 | return; |
---|
[faf83be] | 390 | |
---|
[3d3fb6c] | 391 | wxString m; |
---|
[736f7df] | 392 | // TRANSLATORS: %s is replaced by the command we attempted to run. |
---|
[3d3fb6c] | 393 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str()); |
---|
| 394 | m += wxT(" ("); |
---|
| 395 | m += wxString(strerror(errno), wxConvUTF8); |
---|
| 396 | m += wxT(')'); |
---|
| 397 | wxGetApp().ReportError(m); |
---|
[6bec10c] | 398 | } |
---|
| 399 | |
---|
[8991d7f] | 400 | void |
---|
[6bec10c] | 401 | CavernLogWindow::process(const wxString &file) |
---|
| 402 | { |
---|
[00f4e69] | 403 | SetPage(wxString()); |
---|
[0415bac] | 404 | #ifdef CAVERNLOG_USE_THREADS |
---|
[fc626ae] | 405 | if (thread) stop_thread(); |
---|
| 406 | #endif |
---|
[c1144fe] | 407 | if (cavern_out) { |
---|
[15ba0b5] | 408 | cavern_out->Detach(); |
---|
[15033fd] | 409 | cavern_out = NULL; |
---|
[c1144fe] | 410 | } else { |
---|
| 411 | wxBeginBusyCursor(); |
---|
| 412 | } |
---|
| 413 | |
---|
[fb5887c] | 414 | SetFocus(); |
---|
| 415 | filename = file; |
---|
| 416 | |
---|
[8991d7f] | 417 | link_count = 0; |
---|
| 418 | cur.resize(0); |
---|
[0e81a88] | 419 | log_txt.resize(0); |
---|
| 420 | |
---|
[6bec10c] | 421 | #ifdef __WXMSW__ |
---|
[15322f2] | 422 | SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1")); |
---|
[6bec10c] | 423 | #else |
---|
[06b1227] | 424 | setenv("SURVEX_UTF8", "1", 1); |
---|
[6bec10c] | 425 | #endif |
---|
[93ff5cc] | 426 | |
---|
[6bec10c] | 427 | wxString escaped_file = escape_for_shell(file, true); |
---|
[549eb37] | 428 | wxString cmd = get_command_path(L"cavern"); |
---|
[9e50f755] | 429 | cmd = escape_for_shell(cmd, false); |
---|
[5627cbb] | 430 | cmd += wxT(" -o "); |
---|
[6bec10c] | 431 | cmd += escaped_file; |
---|
[5627cbb] | 432 | cmd += wxT(' '); |
---|
[6bec10c] | 433 | cmd += escaped_file; |
---|
| 434 | |
---|
[15ba0b5] | 435 | cavern_out = wxProcess::Open(cmd); |
---|
[6bec10c] | 436 | if (!cavern_out) { |
---|
[5627cbb] | 437 | wxString m; |
---|
[3d3fb6c] | 438 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str()); |
---|
[5627cbb] | 439 | m += wxT(" ("); |
---|
[2e4b8cd] | 440 | m += wxString(strerror(errno), wxConvUTF8); |
---|
[5627cbb] | 441 | m += wxT(')'); |
---|
[6bec10c] | 442 | wxGetApp().ReportError(m); |
---|
[8991d7f] | 443 | return; |
---|
[6bec10c] | 444 | } |
---|
| 445 | |
---|
[15ba0b5] | 446 | // We want to receive the wxProcessEvent when cavern exits. |
---|
| 447 | cavern_out->SetNextHandler(this); |
---|
[40b02e8] | 448 | |
---|
[0415bac] | 449 | #ifdef CAVERNLOG_USE_THREADS |
---|
[15ba0b5] | 450 | thread = new CavernThread(this, cavern_out->GetInputStream()); |
---|
[fc626ae] | 451 | if (thread->Run() != wxTHREAD_NO_ERROR) { |
---|
| 452 | wxGetApp().ReportError(wxT("Thread failed to start")); |
---|
| 453 | delete thread; |
---|
| 454 | thread = NULL; |
---|
| 455 | } |
---|
[15ba0b5] | 456 | #endif |
---|
[fc626ae] | 457 | } |
---|
| 458 | |
---|
| 459 | void |
---|
| 460 | CavernLogWindow::OnCavernOutput(wxCommandEvent & e_) |
---|
| 461 | { |
---|
| 462 | CavernOutputEvent & e = (CavernOutputEvent&)e_; |
---|
| 463 | |
---|
| 464 | if (e.len > 0) { |
---|
| 465 | ssize_t n = e.len; |
---|
[27222f8] | 466 | if (size_t(n) > sizeof(buf) - (end - buf)) abort(); |
---|
[fc626ae] | 467 | memcpy(end, e.buf, n); |
---|
[330cb03] | 468 | log_txt.append((const char *)end, n); |
---|
| 469 | end += n; |
---|
[b3ee5f5] | 470 | |
---|
| 471 | const unsigned char * p = buf; |
---|
| 472 | |
---|
| 473 | while (p != end) { |
---|
| 474 | int ch = *p++; |
---|
| 475 | if (ch >= 0x80) { |
---|
| 476 | // Decode multi-byte UTF-8 sequence. |
---|
| 477 | if (ch < 0xc0) { |
---|
[184d6d4] | 478 | // Invalid UTF-8 sequence. |
---|
| 479 | goto bad_utf8; |
---|
[b3ee5f5] | 480 | } else if (ch < 0xe0) { |
---|
| 481 | /* 2 byte sequence */ |
---|
| 482 | if (p == end) { |
---|
| 483 | // Incomplete UTF-8 sequence - try to read more. |
---|
| 484 | break; |
---|
| 485 | } |
---|
| 486 | int ch1 = *p++; |
---|
| 487 | if ((ch1 & 0xc0) != 0x80) { |
---|
[184d6d4] | 488 | // Invalid UTF-8 sequence. |
---|
| 489 | goto bad_utf8; |
---|
[b3ee5f5] | 490 | } |
---|
| 491 | ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f); |
---|
| 492 | } else if (ch < 0xf0) { |
---|
| 493 | /* 3 byte sequence */ |
---|
| 494 | if (end - p <= 1) { |
---|
| 495 | // Incomplete UTF-8 sequence - try to read more. |
---|
| 496 | break; |
---|
| 497 | } |
---|
| 498 | int ch1 = *p++; |
---|
| 499 | ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6); |
---|
| 500 | if ((ch1 & 0xc0) != 0x80) { |
---|
[184d6d4] | 501 | // Invalid UTF-8 sequence. |
---|
| 502 | goto bad_utf8; |
---|
[b3ee5f5] | 503 | } |
---|
| 504 | int ch2 = *p++; |
---|
| 505 | if ((ch2 & 0xc0) != 0x80) { |
---|
[184d6d4] | 506 | // Invalid UTF-8 sequence. |
---|
| 507 | goto bad_utf8; |
---|
[b3ee5f5] | 508 | } |
---|
| 509 | ch |= (ch2 & 0x3f); |
---|
[6bec10c] | 510 | } else { |
---|
[184d6d4] | 511 | // Overlong UTF-8 sequence. |
---|
| 512 | goto bad_utf8; |
---|
[6bec10c] | 513 | } |
---|
| 514 | } |
---|
| 515 | |
---|
[e768f29] | 516 | if (false) { |
---|
| 517 | bad_utf8: |
---|
| 518 | // Resync to next byte which starts a UTF-8 sequence. |
---|
| 519 | while (p != end) { |
---|
| 520 | if (*p < 0x80 || (*p >= 0xc0 && *p < 0xf0)) break; |
---|
| 521 | ++p; |
---|
| 522 | } |
---|
| 523 | cur += badutf8_html; |
---|
| 524 | continue; |
---|
| 525 | } |
---|
| 526 | |
---|
[b3ee5f5] | 527 | switch (ch) { |
---|
| 528 | case '\r': |
---|
| 529 | // Ignore. |
---|
| 530 | break; |
---|
| 531 | case '\n': { |
---|
| 532 | if (cur.empty()) continue; |
---|
[a07ee70] | 533 | #ifndef __WXMSW__ |
---|
[b3ee5f5] | 534 | size_t colon = cur.find(':'); |
---|
[a07ee70] | 535 | #else |
---|
[b3ee5f5] | 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); |
---|
[6bec10c] | 539 | #endif |
---|
[b3ee5f5] | 540 | if (colon != wxString::npos && colon < cur.size() - 2) { |
---|
| 541 | ++colon; |
---|
| 542 | size_t i = colon; |
---|
| 543 | while (i < cur.size() - 2 && |
---|
| 544 | cur[i] >= wxT('0') && cur[i] <= wxT('9')) { |
---|
| 545 | ++i; |
---|
| 546 | } |
---|
| 547 | if (i > colon && cur[i] == wxT(':') ) { |
---|
[da96015] | 548 | colon = i; |
---|
[b3ee5f5] | 549 | // Check for column number. |
---|
| 550 | while (++i < cur.size() - 2 && |
---|
| 551 | cur[i] >= wxT('0') && cur[i] <= wxT('9')) { } |
---|
[f9f0f0c] | 552 | bool have_column = (i > colon + 1 && cur[i] == wxT(':')); |
---|
| 553 | if (have_column) { |
---|
[b3ee5f5] | 554 | colon = i; |
---|
[544a46c] | 555 | } else { |
---|
| 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. |
---|
| 559 | ++colon; |
---|
[b3ee5f5] | 560 | } |
---|
| 561 | wxString tag = wxT("<a href=\""); |
---|
| 562 | tag.append(cur, 0, colon); |
---|
| 563 | while (cur[++i] == wxT(' ')) { } |
---|
| 564 | tag += wxT("\" target=\""); |
---|
[e768f29] | 565 | wxString target(cur, i, wxString::npos); |
---|
| 566 | target.Replace(badutf8_html, badutf8); |
---|
| 567 | tag += target; |
---|
[b3ee5f5] | 568 | tag += wxT("\">"); |
---|
| 569 | cur.insert(0, tag); |
---|
[40b02e8] | 570 | size_t offset = colon + tag.size(); |
---|
| 571 | cur.insert(offset, wxT("</a>")); |
---|
| 572 | offset += 4 + 2; |
---|
[8991d7f] | 573 | |
---|
[f9f0f0c] | 574 | if (!have_column) --offset; |
---|
| 575 | |
---|
[8991d7f] | 576 | static const wxString & error_marker = wmsg(/*error*/93) + ":"; |
---|
| 577 | static const wxString & warning_marker = wmsg(/*warning*/4) + ":"; |
---|
| 578 | |
---|
[40b02e8] | 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\">")); |
---|
[58055e11] | 582 | offset += 24 + error_marker.size() - 1; |
---|
[40b02e8] | 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\">")); |
---|
[58055e11] | 587 | offset += 27 + warning_marker.size() - 1; |
---|
[40b02e8] | 588 | cur.insert(offset, wxT("</span>")); |
---|
| 589 | } |
---|
| 590 | |
---|
[b3ee5f5] | 591 | ++link_count; |
---|
[da96015] | 592 | } |
---|
[6bec10c] | 593 | } |
---|
[93ff5cc] | 594 | |
---|
[b3ee5f5] | 595 | // Save the scrollbar positions. |
---|
| 596 | int scroll_x = 0, scroll_y = 0; |
---|
| 597 | GetViewStart(&scroll_x, &scroll_y); |
---|
[93ff5cc] | 598 | |
---|
[b3ee5f5] | 599 | cur += wxT("<br>\n"); |
---|
| 600 | AppendToPage(cur); |
---|
[93ff5cc] | 601 | |
---|
[b3ee5f5] | 602 | if (!link_count) { |
---|
[8991d7f] | 603 | // Auto-scroll the window until we've reported a |
---|
| 604 | // warning or error. |
---|
[b3ee5f5] | 605 | int x, y; |
---|
| 606 | GetVirtualSize(&x, &y); |
---|
| 607 | int xs, ys; |
---|
| 608 | GetClientSize(&xs, &ys); |
---|
| 609 | y -= ys; |
---|
| 610 | int xu, yu; |
---|
| 611 | GetScrollPixelsPerUnit(&xu, &yu); |
---|
| 612 | Scroll(scroll_x, y / yu); |
---|
| 613 | } else { |
---|
| 614 | // Restore the scrollbar positions. |
---|
| 615 | Scroll(scroll_x, scroll_y); |
---|
| 616 | } |
---|
[a75f5a1] | 617 | |
---|
[b3ee5f5] | 618 | cur.clear(); |
---|
| 619 | break; |
---|
[6bec10c] | 620 | } |
---|
[b3ee5f5] | 621 | case '<': |
---|
| 622 | cur += wxT("<"); |
---|
| 623 | break; |
---|
| 624 | case '>': |
---|
| 625 | cur += wxT(">"); |
---|
| 626 | break; |
---|
| 627 | case '&': |
---|
| 628 | cur += wxT("&"); |
---|
| 629 | break; |
---|
| 630 | case '"': |
---|
| 631 | cur += wxT(""); |
---|
| 632 | continue; |
---|
| 633 | default: |
---|
| 634 | if (ch >= 128) { |
---|
| 635 | cur += wxString::Format(wxT("&#%u;"), ch); |
---|
| 636 | } else { |
---|
| 637 | cur += (char)ch; |
---|
| 638 | } |
---|
| 639 | } |
---|
[6bec10c] | 640 | } |
---|
[b3ee5f5] | 641 | |
---|
| 642 | size_t left = end - p; |
---|
| 643 | end = buf + left; |
---|
| 644 | if (left) memmove(buf, p, left); |
---|
[8991d7f] | 645 | Update(); |
---|
| 646 | return; |
---|
[6bec10c] | 647 | } |
---|
[8991d7f] | 648 | |
---|
[15ba0b5] | 649 | if (e.len <= 0 && buf != end) { |
---|
[184d6d4] | 650 | // Truncated UTF-8 sequence. |
---|
[e768f29] | 651 | cur += badutf8_html; |
---|
[fc626ae] | 652 | } |
---|
[fb5887c] | 653 | |
---|
[0e81a88] | 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\">"), |
---|
| 657 | (int)LOG_SAVE, |
---|
| 658 | wmsg(/*Save Log*/446).c_str())); |
---|
[c1144fe] | 659 | wxEndBusyCursor(); |
---|
[15ba0b5] | 660 | delete cavern_out; |
---|
[8991d7f] | 661 | cavern_out = NULL; |
---|
[15ba0b5] | 662 | if (e.len < 0) { |
---|
| 663 | /* Negative length indicates non-zero exit status from cavern. */ |
---|
[736f7df] | 664 | /* TRANSLATORS: Label for button in aven’s cavern log window which |
---|
| 665 | * causes the survey data to be reprocessed. */ |
---|
[bbed692] | 666 | AppendToPage(wxString::Format(wxT("<avenbutton default id=%d name=\"%s\">"), |
---|
| 667 | (int)LOG_REPROCESS, |
---|
[2341559] | 668 | wmsg(/*Reprocess*/184).c_str())); |
---|
[8991d7f] | 669 | return; |
---|
[6bec10c] | 670 | } |
---|
[bbed692] | 671 | AppendToPage(wxString::Format(wxT("<avenbutton id=%d name=\"%s\">"), |
---|
| 672 | (int)LOG_REPROCESS, |
---|
[d7b53e3] | 673 | wmsg(/*Reprocess*/184).c_str())); |
---|
| 674 | AppendToPage(wxString::Format(wxT("<avenbutton default id=%d>"), (int)wxID_OK)); |
---|
| 675 | Update(); |
---|
| 676 | init_done = false; |
---|
[fb5887c] | 677 | |
---|
[15ba0b5] | 678 | { |
---|
| 679 | wxString file3d(filename, 0, filename.length() - 3); |
---|
| 680 | file3d.append(wxT("3d")); |
---|
| 681 | if (!mainfrm->LoadData(file3d, survey)) { |
---|
| 682 | return; |
---|
| 683 | } |
---|
[d7b53e3] | 684 | } |
---|
[15ba0b5] | 685 | |
---|
[8991d7f] | 686 | if (link_count == 0) { |
---|
| 687 | wxCommandEvent dummy; |
---|
| 688 | OnOK(dummy); |
---|
[fb5887c] | 689 | } |
---|
[15ba0b5] | 690 | } |
---|
| 691 | |
---|
| 692 | void |
---|
| 693 | CavernLogWindow::OnEndProcess(wxProcessEvent & evt) |
---|
| 694 | { |
---|
| 695 | CavernOutputEvent * e = new CavernOutputEvent(); |
---|
| 696 | // Zero length indicates successful exit, negative length unsuccessful exit. |
---|
| 697 | e->len = (evt.GetExitCode() == 0 ? 0 : -1); |
---|
| 698 | QueueEvent(e); |
---|
[fb5887c] | 699 | } |
---|
| 700 | |
---|
[0e81a88] | 701 | void |
---|
[330cb03] | 702 | CavernLogWindow::OnReprocess(wxCommandEvent &) |
---|
[8991d7f] | 703 | { |
---|
| 704 | process(filename); |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | void |
---|
[0e81a88] | 708 | CavernLogWindow::OnSave(wxCommandEvent &) |
---|
| 709 | { |
---|
| 710 | wxString filelog(filename, 0, filename.length() - 3); |
---|
| 711 | filelog += wxT("log"); |
---|
| 712 | AvenAllowOnTop ontop(mainfrm); |
---|
| 713 | #ifdef __WXMOTIF__ |
---|
| 714 | wxString ext(wxT("*.log")); |
---|
| 715 | #else |
---|
[bb71423] | 716 | /* TRANSLATORS: Log files from running cavern (extension .log) */ |
---|
[0e81a88] | 717 | wxString ext = wmsg(/*Log files*/447); |
---|
| 718 | ext += wxT("|*.log"); |
---|
| 719 | #endif |
---|
| 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(); |
---|
[3206c12] | 725 | FILE * fh_log = wxFopen(filelog, wxT("w")); |
---|
[0e81a88] | 726 | if (!fh_log) { |
---|
| 727 | wxGetApp().ReportError(wxString::Format(wmsg(/*Error writing to file “%s”*/110), filelog.c_str())); |
---|
| 728 | return; |
---|
| 729 | } |
---|
| 730 | fwrite(log_txt.data(), log_txt.size(), 1, fh_log); |
---|
| 731 | fclose(fh_log); |
---|
| 732 | } |
---|
| 733 | |
---|
[fb5887c] | 734 | void |
---|
| 735 | CavernLogWindow::OnOK(wxCommandEvent &) |
---|
| 736 | { |
---|
[d7b53e3] | 737 | if (init_done) { |
---|
| 738 | mainfrm->HideLog(this); |
---|
| 739 | } else { |
---|
[5e0b9f9d] | 740 | mainfrm->InitialiseAfterLoad(filename, survey); |
---|
[d7b53e3] | 741 | init_done = true; |
---|
| 742 | } |
---|
[fb5887c] | 743 | } |
---|