| 1 | /* cavernlog.cc
|
|---|
| 2 | * Run cavern inside an Aven window
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) 2005-2026 Olly Betts
|
|---|
| 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, see
|
|---|
| 18 | * <https://www.gnu.org/licenses/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include <config.h>
|
|---|
| 22 |
|
|---|
| 23 | #include "aven.h"
|
|---|
| 24 | #include "cavernlog.h"
|
|---|
| 25 | #include "filename.h"
|
|---|
| 26 | #include "mainfrm.h"
|
|---|
| 27 | #include "message.h"
|
|---|
| 28 | #include "osalloc.h"
|
|---|
| 29 |
|
|---|
| 30 | #include <algorithm>
|
|---|
| 31 |
|
|---|
| 32 | #include <errno.h>
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <stdlib.h>
|
|---|
| 35 |
|
|---|
| 36 | #include <sys/time.h>
|
|---|
| 37 | #include <sys/types.h>
|
|---|
| 38 | #include <unistd.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <wx/process.h>
|
|---|
| 41 |
|
|---|
| 42 | #ifdef _WIN32
|
|---|
| 43 | # include <io.h> // For _commit().
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 | #define GVIM_COMMAND "gvim +'call cursor($l,$c)' $f"
|
|---|
| 47 | #define VIM_COMMAND "x-terminal-emulator -e vim +'call cursor($l,$c)' $f"
|
|---|
| 48 | #define NVIM_COMMAND "x-terminal-emulator -e nvim +'call cursor($l,$c)' $f"
|
|---|
| 49 | #define GEDIT_COMMAND "gedit $f +$l:$c"
|
|---|
| 50 | // Pluma currently ignores the column, but include it assuming some future
|
|---|
| 51 | // version will add support.
|
|---|
| 52 | #define PLUMA_COMMAND "pluma +$l:$c $f"
|
|---|
| 53 | #define EMACS_COMMAND "x-terminal-emulator -e emacs +$l:$c $f"
|
|---|
| 54 | #define NANO_COMMAND "x-terminal-emulator -e nano +$l,$c $f"
|
|---|
| 55 | #define JED_COMMAND "x-terminal-emulator -e jed $f -g $l"
|
|---|
| 56 | #define KATE_COMMAND "kate -l $l -c $c $f"
|
|---|
| 57 |
|
|---|
| 58 | #ifdef __WXMSW__
|
|---|
| 59 | # define DEFAULT_EDITOR_COMMAND "notepad $f"
|
|---|
| 60 | #elif defined __WXMAC__
|
|---|
| 61 | # define DEFAULT_EDITOR_COMMAND "open -t $f"
|
|---|
| 62 | #else
|
|---|
| 63 | # define DEFAULT_EDITOR_COMMAND VIM_COMMAND
|
|---|
| 64 | #endif
|
|---|
| 65 |
|
|---|
| 66 | enum { LOG_REPROCESS = 1234, LOG_SAVE = 1235 };
|
|---|
| 67 |
|
|---|
| 68 | // New event type for signalling cavern output to process.
|
|---|
| 69 | wxDEFINE_EVENT(EVT_CAVERN_OUTPUT, wxCommandEvent);
|
|---|
| 70 |
|
|---|
| 71 | void
|
|---|
| 72 | CavernLogWindow::CheckForOutput(bool immediate)
|
|---|
| 73 | {
|
|---|
| 74 | timer.Stop();
|
|---|
| 75 | if (cavern_out == NULL) return;
|
|---|
| 76 |
|
|---|
| 77 | wxInputStream * in = cavern_out->GetInputStream();
|
|---|
| 78 |
|
|---|
| 79 | if (!in->CanRead()) {
|
|---|
| 80 | timer.StartOnce();
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | size_t real_size = log_txt.size();
|
|---|
| 85 | size_t allow = 1024;
|
|---|
| 86 | log_txt.resize(real_size + allow);
|
|---|
| 87 | in->Read(&log_txt[real_size], allow);
|
|---|
| 88 | size_t n = in->LastRead();
|
|---|
| 89 | log_txt.resize(real_size + n);
|
|---|
| 90 | if (n) {
|
|---|
| 91 | if (immediate) {
|
|---|
| 92 | ProcessCavernOutput();
|
|---|
| 93 | } else {
|
|---|
| 94 | QueueEvent(new wxCommandEvent(EVT_CAVERN_OUTPUT));
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | int
|
|---|
| 100 | CavernLogWindow::OnPaintButton(wxButton* b, int x)
|
|---|
| 101 | {
|
|---|
| 102 | if (b) {
|
|---|
| 103 | x -= 4;
|
|---|
| 104 | const wxSize& bsize = b->GetSize();
|
|---|
| 105 | x -= bsize.x;
|
|---|
| 106 | b->SetSize(x, 4, bsize.x, bsize.y);
|
|---|
| 107 | x -= 4;
|
|---|
| 108 | }
|
|---|
| 109 | return x;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void
|
|---|
| 113 | CavernLogWindow::OnPaint(wxPaintEvent&)
|
|---|
| 114 | {
|
|---|
| 115 | wxPaintDC dc(this);
|
|---|
| 116 | wxFont font = dc.GetFont();
|
|---|
| 117 | wxFont bold_font = font.Bold();
|
|---|
| 118 | wxFont underlined_font = font.Underlined();
|
|---|
| 119 | const wxRegion& region = GetUpdateRegion();
|
|---|
| 120 | const wxRect& rect = region.GetBox();
|
|---|
| 121 | int scroll_x = 0, scroll_y = 0;
|
|---|
| 122 | GetViewStart(&scroll_x, &scroll_y);
|
|---|
| 123 | int fsize = dc.GetFont().GetPixelSize().GetHeight();
|
|---|
| 124 | int limit = min((rect.y + rect.height + fsize - 1) / fsize + scroll_y, int(line_info.size()) - 1);
|
|---|
| 125 | for (int i = max(rect.y / fsize, scroll_y); i <= limit ; ++i) {
|
|---|
| 126 | LineInfo& info = line_info[i];
|
|---|
| 127 | // Leave a small margin to the left.
|
|---|
| 128 | int x = fsize / 2 - scroll_x * fsize;
|
|---|
| 129 | int y = (i - scroll_y) * fsize;
|
|---|
| 130 | unsigned offset = info.start_offset;
|
|---|
| 131 | unsigned len = info.len;
|
|---|
| 132 | if (info.link_len) {
|
|---|
| 133 | dc.SetFont(underlined_font);
|
|---|
| 134 | dc.SetTextForeground(wxColour(192, 0, 192));
|
|---|
| 135 | wxString link = wxString::FromUTF8(&log_txt[offset], info.link_len);
|
|---|
| 136 | offset += info.link_len;
|
|---|
| 137 | len -= info.link_len;
|
|---|
| 138 | dc.DrawText(link, x, y);
|
|---|
| 139 | x += info.link_pixel_width;
|
|---|
| 140 | dc.SetFont(font);
|
|---|
| 141 | }
|
|---|
| 142 | if (info.colour_len) {
|
|---|
| 143 | dc.SetTextForeground(dark_mode ? *wxWHITE : *wxBLACK);
|
|---|
| 144 | {
|
|---|
| 145 | size_t s_len = info.start_offset + info.colour_start - offset;
|
|---|
| 146 | wxString s = wxString::FromUTF8(&log_txt[offset], s_len);
|
|---|
| 147 | offset += s_len;
|
|---|
| 148 | len -= s_len;
|
|---|
| 149 | dc.DrawText(s, x, y);
|
|---|
| 150 | x += dc.GetTextExtent(s).GetWidth();
|
|---|
| 151 | }
|
|---|
| 152 | switch (info.colour) {
|
|---|
| 153 | case LOG_ERROR:
|
|---|
| 154 | dc.SetTextForeground(*wxRED);
|
|---|
| 155 | break;
|
|---|
| 156 | case LOG_WARNING:
|
|---|
| 157 | dc.SetTextForeground(wxColour(0xf2, 0x8C, 0x28));
|
|---|
| 158 | break;
|
|---|
| 159 | case LOG_INFO:
|
|---|
| 160 | dc.SetTextForeground(*wxBLUE);
|
|---|
| 161 | break;
|
|---|
| 162 | }
|
|---|
| 163 | dc.SetFont(bold_font);
|
|---|
| 164 | wxString d = wxString::FromUTF8(&log_txt[offset], info.colour_len);
|
|---|
| 165 | offset += info.colour_len;
|
|---|
| 166 | len -= info.colour_len;
|
|---|
| 167 | dc.DrawText(d, x, y);
|
|---|
| 168 | x += dc.GetTextExtent(d).GetWidth();
|
|---|
| 169 | dc.SetFont(font);
|
|---|
| 170 | }
|
|---|
| 171 | dc.SetTextForeground(dark_mode ? *wxWHITE : *wxBLACK);
|
|---|
| 172 | dc.DrawText(wxString::FromUTF8(&log_txt[offset], len), x, y);
|
|---|
| 173 | }
|
|---|
| 174 | int x = GetClientSize().x;
|
|---|
| 175 | x = OnPaintButton(ok_button, x);
|
|---|
| 176 | x = OnPaintButton(reprocess_button, x);
|
|---|
| 177 | OnPaintButton(save_button, x);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | BEGIN_EVENT_TABLE(CavernLogWindow, wxScrolledWindow)
|
|---|
| 181 | EVT_BUTTON(LOG_REPROCESS, CavernLogWindow::OnReprocess)
|
|---|
| 182 | EVT_BUTTON(LOG_SAVE, CavernLogWindow::OnSave)
|
|---|
| 183 | EVT_BUTTON(wxID_OK, CavernLogWindow::OnOK)
|
|---|
| 184 | EVT_COMMAND(wxID_ANY, EVT_CAVERN_OUTPUT, CavernLogWindow::OnCavernOutput)
|
|---|
| 185 | EVT_IDLE(CavernLogWindow::OnIdle)
|
|---|
| 186 | EVT_TIMER(wxID_ANY, CavernLogWindow::OnTimer)
|
|---|
| 187 | EVT_PAINT(CavernLogWindow::OnPaint)
|
|---|
| 188 | EVT_MOTION(CavernLogWindow::OnMouseMove)
|
|---|
| 189 | EVT_LEFT_UP(CavernLogWindow::OnLinkClicked)
|
|---|
| 190 | EVT_END_PROCESS(wxID_ANY, CavernLogWindow::OnEndProcess)
|
|---|
| 191 | END_EVENT_TABLE()
|
|---|
| 192 |
|
|---|
| 193 | wxString escape_for_shell(wxString s, bool protect_dash)
|
|---|
| 194 | {
|
|---|
| 195 | #ifdef __WXMSW__
|
|---|
| 196 | // Correct quoting rules are insane:
|
|---|
| 197 | //
|
|---|
| 198 | // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
|
|---|
| 199 | //
|
|---|
| 200 | // Thankfully wxExecute passes the command string to CreateProcess(), so
|
|---|
| 201 | // at least we don't need to quote for cmd.exe too.
|
|---|
| 202 | if (protect_dash && !s.empty() && s[0u] == '-') {
|
|---|
| 203 | // If the filename starts with a '-', protect it from being
|
|---|
| 204 | // treated as an option by prepending ".\".
|
|---|
| 205 | s.insert(0, wxT(".\\"));
|
|---|
| 206 | }
|
|---|
| 207 | if (s.empty() || s.find_first_of(wxT(" \"\t\n\v")) != s.npos) {
|
|---|
| 208 | // Need to quote.
|
|---|
| 209 | s.insert(0, wxT('"'));
|
|---|
| 210 | for (size_t p = 1; p < s.size(); ++p) {
|
|---|
| 211 | size_t backslashes = 0;
|
|---|
| 212 | while (s[p] == wxT('\\')) {
|
|---|
| 213 | ++backslashes;
|
|---|
| 214 | if (++p == s.size()) {
|
|---|
| 215 | // Escape all the backslashes, since they're before
|
|---|
| 216 | // the closing quote we add below.
|
|---|
| 217 | s.append(backslashes, wxT('\\'));
|
|---|
| 218 | goto done;
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | if (s[p] == wxT('"')) {
|
|---|
| 223 | // Escape any preceding backslashes and this quote.
|
|---|
| 224 | s.insert(p, backslashes + 1, wxT('\\'));
|
|---|
| 225 | p += backslashes + 1;
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 | done:
|
|---|
| 229 | s.append(wxT('"'));
|
|---|
| 230 | }
|
|---|
| 231 | #else
|
|---|
| 232 | size_t p = 0;
|
|---|
| 233 | if (protect_dash && !s.empty() && s[0u] == '-') {
|
|---|
| 234 | // If the filename starts with a '-', protect it from being
|
|---|
| 235 | // treated as an option by prepending "./".
|
|---|
| 236 | s.insert(0, wxT("./"));
|
|---|
| 237 | p = 2;
|
|---|
| 238 | }
|
|---|
| 239 | while (p < s.size()) {
|
|---|
| 240 | // Exclude a few safe characters which are common in filenames
|
|---|
| 241 | if (!isalnum((unsigned char)s[p]) && strchr("/._-", s[p]) == NULL) {
|
|---|
| 242 | s.insert(p, 1, wxT('\\'));
|
|---|
| 243 | ++p;
|
|---|
| 244 | }
|
|---|
| 245 | ++p;
|
|---|
| 246 | }
|
|---|
| 247 | #endif
|
|---|
| 248 | return s;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | wxString get_command_path(const wxChar * command_name)
|
|---|
| 252 | {
|
|---|
| 253 | #ifdef __WXMSW__
|
|---|
| 254 | wxString cmd;
|
|---|
| 255 | {
|
|---|
| 256 | DWORD len = 256;
|
|---|
| 257 | wchar_t *buf = NULL;
|
|---|
| 258 | while (1) {
|
|---|
| 259 | DWORD got;
|
|---|
| 260 | buf = (wchar_t*)osrealloc(buf, len * 2);
|
|---|
| 261 | got = GetModuleFileNameW(NULL, buf, len);
|
|---|
| 262 | if (got < len) break;
|
|---|
| 263 | len += len;
|
|---|
| 264 | }
|
|---|
| 265 | /* Strange Win32 nastiness - strip prefix "\\?\" if present */
|
|---|
| 266 | wchar_t *start = buf;
|
|---|
| 267 | if (wcsncmp(start, L"\\\\?\\", 4) == 0) start += 4;
|
|---|
| 268 | wchar_t * slash = wcsrchr(start, L'\\');
|
|---|
| 269 | if (slash) {
|
|---|
| 270 | cmd.assign(start, slash - start + 1);
|
|---|
| 271 | }
|
|---|
| 272 | free(buf);
|
|---|
| 273 | }
|
|---|
| 274 | #else
|
|---|
| 275 | wxString cmd = wxString::FromUTF8(msg_exepth());
|
|---|
| 276 | #endif
|
|---|
| 277 | cmd += command_name;
|
|---|
| 278 | return cmd;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | CavernLogWindow::CavernLogWindow(MainFrm * mainfrm_, const wxString & survey_, wxWindow * parent)
|
|---|
| 282 | : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
|
|---|
| 283 | wxFULL_REPAINT_ON_RESIZE),
|
|---|
| 284 | mainfrm(mainfrm_),
|
|---|
| 285 | survey(survey_),
|
|---|
| 286 | timer(this)
|
|---|
| 287 | {
|
|---|
| 288 | if (wxSystemSettings::GetAppearance().IsDark()) {
|
|---|
| 289 | SetOwnBackgroundColour(*wxBLACK);
|
|---|
| 290 | dark_mode = true;
|
|---|
| 291 | return;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | SetOwnBackgroundColour(*wxWHITE);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | CavernLogWindow::~CavernLogWindow()
|
|---|
| 298 | {
|
|---|
| 299 | timer.Stop();
|
|---|
| 300 | if (cavern_out) {
|
|---|
| 301 | wxEndBusyCursor();
|
|---|
| 302 | cavern_out->Detach();
|
|---|
| 303 | }
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | void
|
|---|
| 307 | CavernLogWindow::OnMouseMove(wxMouseEvent& e)
|
|---|
| 308 | {
|
|---|
| 309 | const auto& pos = e.GetPosition();
|
|---|
| 310 | int fsize = GetFont().GetPixelSize().GetHeight();
|
|---|
| 311 | int scroll_x = 0, scroll_y = 0;
|
|---|
| 312 | GetViewStart(&scroll_x, &scroll_y);
|
|---|
| 313 | unsigned line = pos.y / fsize + scroll_y;
|
|---|
| 314 | unsigned x = pos.x + scroll_x * fsize - fsize / 2;
|
|---|
| 315 | if (line < line_info.size() && x <= line_info[line].link_pixel_width) {
|
|---|
| 316 | SetCursor(wxCursor(wxCURSOR_HAND));
|
|---|
| 317 | } else {
|
|---|
| 318 | SetCursor(wxNullCursor);
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | void
|
|---|
| 323 | CavernLogWindow::OnLinkClicked(wxMouseEvent& e)
|
|---|
| 324 | {
|
|---|
| 325 | const auto& pos = e.GetPosition();
|
|---|
| 326 | int fsize = GetFont().GetPixelSize().GetHeight();
|
|---|
| 327 | int scroll_x = 0, scroll_y = 0;
|
|---|
| 328 | GetViewStart(&scroll_x, &scroll_y);
|
|---|
| 329 | unsigned line = pos.y / fsize + scroll_y;
|
|---|
| 330 | unsigned x = pos.x + scroll_x * fsize - fsize / 2;
|
|---|
| 331 | if (!(line < line_info.size() && x <= line_info[line].link_pixel_width))
|
|---|
| 332 | return;
|
|---|
| 333 |
|
|---|
| 334 | const char* cur = &log_txt[line_info[line].start_offset];
|
|---|
| 335 | size_t link_len = line_info[line].link_len;
|
|---|
| 336 | size_t colon = link_len;
|
|---|
| 337 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { }
|
|---|
| 338 | size_t colon2 = colon;
|
|---|
| 339 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { }
|
|---|
| 340 | if (cur[colon] != ':') {
|
|---|
| 341 | colon = colon2;
|
|---|
| 342 | colon2 = link_len;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | wxString cmd;
|
|---|
| 346 | wxChar * p = wxGetenv(wxT("SURVEXEDITOR"));
|
|---|
| 347 | if (p) {
|
|---|
| 348 | cmd = p;
|
|---|
| 349 | if (!cmd.find(wxT("$f"))) {
|
|---|
| 350 | cmd += wxT(" $f");
|
|---|
| 351 | }
|
|---|
| 352 | } else {
|
|---|
| 353 | p = wxGetenv(wxT("VISUAL"));
|
|---|
| 354 | if (!p) p = wxGetenv(wxT("EDITOR"));
|
|---|
| 355 | if (!p) {
|
|---|
| 356 | cmd = wxT(DEFAULT_EDITOR_COMMAND);
|
|---|
| 357 | } else {
|
|---|
| 358 | cmd = p;
|
|---|
| 359 | if (cmd == "gvim") {
|
|---|
| 360 | cmd = wxT(GVIM_COMMAND);
|
|---|
| 361 | } else if (cmd == "vim") {
|
|---|
| 362 | cmd = wxT(VIM_COMMAND);
|
|---|
| 363 | } else if (cmd == "nvim") {
|
|---|
| 364 | cmd = wxT(NVIM_COMMAND);
|
|---|
| 365 | } else if (cmd == "gedit") {
|
|---|
| 366 | cmd = wxT(GEDIT_COMMAND);
|
|---|
| 367 | } else if (cmd == "pluma") {
|
|---|
| 368 | cmd = wxT(PLUMA_COMMAND);
|
|---|
| 369 | } else if (cmd == "emacs") {
|
|---|
| 370 | cmd = wxT(EMACS_COMMAND);
|
|---|
| 371 | } else if (cmd == "nano") {
|
|---|
| 372 | cmd = wxT(NANO_COMMAND);
|
|---|
| 373 | } else if (cmd == "jed") {
|
|---|
| 374 | cmd = wxT(JED_COMMAND);
|
|---|
| 375 | } else if (cmd == "kate") {
|
|---|
| 376 | cmd = wxT(KATE_COMMAND);
|
|---|
| 377 | } else {
|
|---|
| 378 | // Escape any $.
|
|---|
| 379 | cmd.Replace(wxT("$"), wxT("$$"));
|
|---|
| 380 | cmd += wxT(" $f");
|
|---|
| 381 | }
|
|---|
| 382 | }
|
|---|
| 383 | }
|
|---|
| 384 | size_t i = 0;
|
|---|
| 385 | while ((i = cmd.find(wxT('$'), i)) != wxString::npos) {
|
|---|
| 386 | if (++i >= cmd.size()) break;
|
|---|
| 387 | switch ((int)cmd[i]) {
|
|---|
| 388 | case wxT('$'):
|
|---|
| 389 | cmd.erase(i, 1);
|
|---|
| 390 | break;
|
|---|
| 391 | case wxT('f'): {
|
|---|
| 392 | wxString f = escape_for_shell(wxString(cur, colon), true);
|
|---|
| 393 | cmd.replace(i - 1, 2, f);
|
|---|
| 394 | i += f.size() - 1;
|
|---|
| 395 | break;
|
|---|
| 396 | }
|
|---|
| 397 | case wxT('l'): {
|
|---|
| 398 | wxString l = escape_for_shell(wxString(cur + colon + 1, colon2 - colon - 1));
|
|---|
| 399 | cmd.replace(i - 1, 2, l);
|
|---|
| 400 | i += l.size() - 1;
|
|---|
| 401 | break;
|
|---|
| 402 | }
|
|---|
| 403 | case wxT('c'): {
|
|---|
| 404 | wxString l;
|
|---|
| 405 | if (colon2 == link_len)
|
|---|
| 406 | l = wxT("0");
|
|---|
| 407 | else
|
|---|
| 408 | l = escape_for_shell(wxString(cur + colon2 + 1, link_len - colon2 - 1));
|
|---|
| 409 | cmd.replace(i - 1, 2, l);
|
|---|
| 410 | i += l.size() - 1;
|
|---|
| 411 | break;
|
|---|
| 412 | }
|
|---|
| 413 | default:
|
|---|
| 414 | ++i;
|
|---|
| 415 | }
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | if (wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER) >= 0)
|
|---|
| 419 | return;
|
|---|
| 420 |
|
|---|
| 421 | wxString m;
|
|---|
| 422 | // TRANSLATORS: %s is replaced by the command we attempted to run.
|
|---|
| 423 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str());
|
|---|
| 424 | m += wxT(" (");
|
|---|
| 425 | m += wxString::FromUTF8(strerror(errno));
|
|---|
| 426 | m += wxT(')');
|
|---|
| 427 | wxGetApp().ReportError(m);
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | void
|
|---|
| 431 | CavernLogWindow::process(const wxString &file)
|
|---|
| 432 | {
|
|---|
| 433 | timer.Stop();
|
|---|
| 434 | if (cavern_out) {
|
|---|
| 435 | cavern_out->Detach();
|
|---|
| 436 | cavern_out = NULL;
|
|---|
| 437 | } else {
|
|---|
| 438 | wxBeginBusyCursor();
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | SetFocus();
|
|---|
| 442 | filename = file;
|
|---|
| 443 |
|
|---|
| 444 | info_count = 0;
|
|---|
| 445 | link_count = 0;
|
|---|
| 446 | log_txt.resize(0);
|
|---|
| 447 | line_info.resize(0);
|
|---|
| 448 | // Reserve enough that we won't need to grow the allocations in normal cases.
|
|---|
| 449 | log_txt.reserve(16384);
|
|---|
| 450 | line_info.reserve(256);
|
|---|
| 451 | ptr = 0;
|
|---|
| 452 | save_button = nullptr;
|
|---|
| 453 | reprocess_button = nullptr;
|
|---|
| 454 | ok_button = nullptr;
|
|---|
| 455 | DestroyChildren();
|
|---|
| 456 | SetVirtualSize(0, 0);
|
|---|
| 457 |
|
|---|
| 458 | #ifdef __WXMSW__
|
|---|
| 459 | SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1"));
|
|---|
| 460 | #else
|
|---|
| 461 | setenv("SURVEX_UTF8", "1", 1);
|
|---|
| 462 | #endif
|
|---|
| 463 |
|
|---|
| 464 | wxString escaped_file = escape_for_shell(file, true);
|
|---|
| 465 | wxString cmd = get_command_path(L"cavern");
|
|---|
| 466 | cmd = escape_for_shell(cmd, false);
|
|---|
| 467 | cmd += wxT(" -o ");
|
|---|
| 468 | cmd += escaped_file;
|
|---|
| 469 | cmd += wxT(' ');
|
|---|
| 470 | cmd += escaped_file;
|
|---|
| 471 |
|
|---|
| 472 | cavern_out = wxProcess::Open(cmd);
|
|---|
| 473 | if (!cavern_out) {
|
|---|
| 474 | wxString m;
|
|---|
| 475 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str());
|
|---|
| 476 | m += wxT(" (");
|
|---|
| 477 | m += wxString::FromUTF8(strerror(errno));
|
|---|
| 478 | m += wxT(')');
|
|---|
| 479 | wxGetApp().ReportError(m);
|
|---|
| 480 | return;
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | // We want to receive the wxProcessEvent when cavern exits.
|
|---|
| 484 | cavern_out->SetNextHandler(this);
|
|---|
| 485 |
|
|---|
| 486 | // Check for output after 500ms if we don't get an idle event sooner.
|
|---|
| 487 | timer.StartOnce(500);
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | void
|
|---|
| 491 | CavernLogWindow::ProcessCavernOutput()
|
|---|
| 492 | {
|
|---|
| 493 | // ptr gives the start of the first line we've not yet processed.
|
|---|
| 494 |
|
|---|
| 495 | size_t nl;
|
|---|
| 496 | while ((nl = log_txt.find('\n', ptr)) != std::string::npos) {
|
|---|
| 497 | if (nl == ptr || (nl - ptr == 1 && log_txt[ptr] == '\r')) {
|
|---|
| 498 | // Don't show empty lines in the window.
|
|---|
| 499 | ptr = nl + 1;
|
|---|
| 500 | continue;
|
|---|
| 501 | }
|
|---|
| 502 | size_t line_len = nl - ptr - (log_txt[nl - 1] == '\r');
|
|---|
| 503 | // FIXME: Avoid copy, use string_view?
|
|---|
| 504 | string cur(log_txt, ptr, line_len);
|
|---|
| 505 | if (log_txt[ptr] == ' ') {
|
|---|
| 506 | if (expecting_caret_line) {
|
|---|
| 507 | // FIXME: Check the line is only space, `^` and `~`?
|
|---|
| 508 | // Otherwise an error without caret info followed
|
|---|
| 509 | // by an error which contains a '^' gets
|
|---|
| 510 | // mishandled...
|
|---|
| 511 | size_t caret = cur.rfind('^');
|
|---|
| 512 | if (caret != wxString::npos) {
|
|---|
| 513 | size_t tilde = cur.rfind('~');
|
|---|
| 514 | if (tilde == wxString::npos || tilde < caret) {
|
|---|
| 515 | tilde = caret;
|
|---|
| 516 | }
|
|---|
| 517 | line_info.back().colour = line_info[line_info.size() - 2].colour;
|
|---|
| 518 | line_info.back().colour_start = caret;
|
|---|
| 519 | line_info.back().colour_len = tilde - caret + 1;
|
|---|
| 520 | expecting_caret_line = false;
|
|---|
| 521 | ptr = nl + 1;
|
|---|
| 522 | continue;
|
|---|
| 523 | }
|
|---|
| 524 | }
|
|---|
| 525 | expecting_caret_line = true;
|
|---|
| 526 | }
|
|---|
| 527 | line_info.emplace_back(ptr);
|
|---|
| 528 | line_info.back().len = line_len;
|
|---|
| 529 | size_t colon = cur.find(": ");
|
|---|
| 530 | if (colon != wxString::npos) {
|
|---|
| 531 | size_t link_len = colon;
|
|---|
| 532 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { }
|
|---|
| 533 | if (cur[colon] == ':') {
|
|---|
| 534 | line_info.back().link_len = link_len;
|
|---|
| 535 |
|
|---|
| 536 | static string info_marker = string(msg(/*info*/485)) + ':';
|
|---|
| 537 | static string warning_marker = string(msg(/*warning*/106)) + ':';
|
|---|
| 538 | static string error_marker = string(msg(/*error*/93)) + ':';
|
|---|
| 539 |
|
|---|
| 540 | size_t offset = link_len + 2;
|
|---|
| 541 | if (cur.compare(offset, info_marker.size(), info_marker) == 0) {
|
|---|
| 542 | // Show "info" marker in blue.
|
|---|
| 543 | ++info_count;
|
|---|
| 544 | line_info.back().colour = LOG_INFO;
|
|---|
| 545 | line_info.back().colour_start = offset;
|
|---|
| 546 | line_info.back().colour_len = info_marker.size() - 1;
|
|---|
| 547 | } else if (cur.compare(offset, warning_marker.size(), warning_marker) == 0) {
|
|---|
| 548 | // Show "warning" marker in orange.
|
|---|
| 549 | line_info.back().colour = LOG_WARNING;
|
|---|
| 550 | line_info.back().colour_start = offset;
|
|---|
| 551 | line_info.back().colour_len = warning_marker.size() - 1;
|
|---|
| 552 | } else if (cur.compare(offset, error_marker.size(), error_marker) == 0) {
|
|---|
| 553 | // Show "error" marker in red.
|
|---|
| 554 | line_info.back().colour = LOG_ERROR;
|
|---|
| 555 | line_info.back().colour_start = offset;
|
|---|
| 556 | line_info.back().colour_len = error_marker.size() - 1;
|
|---|
| 557 | }
|
|---|
| 558 | ++link_count;
|
|---|
| 559 | }
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | int fsize = GetFont().GetPixelSize().GetHeight();
|
|---|
| 563 | SetScrollRate(fsize, fsize);
|
|---|
| 564 |
|
|---|
| 565 | auto& info = line_info.back();
|
|---|
| 566 | info.link_pixel_width = GetTextExtent(wxString(&log_txt[ptr], info.link_len)).GetWidth();
|
|---|
| 567 | auto rest_pixel_width = GetTextExtent(wxString(&log_txt[ptr + info.link_len], info.len - info.link_len)).GetWidth();
|
|---|
| 568 | int width = max(GetVirtualSize().GetWidth(),
|
|---|
| 569 | int(fsize + info.link_pixel_width + rest_pixel_width));
|
|---|
| 570 | int height = line_info.size();
|
|---|
| 571 | SetVirtualSize(width, height * fsize);
|
|---|
| 572 | if (!link_count) {
|
|---|
| 573 | // Auto-scroll until the first diagnostic.
|
|---|
| 574 | int scroll_x = 0, scroll_y = 0;
|
|---|
| 575 | GetViewStart(&scroll_x, &scroll_y);
|
|---|
| 576 | int xs, ys;
|
|---|
| 577 | GetClientSize(&xs, &ys);
|
|---|
| 578 | Scroll(scroll_x, line_info.size() * fsize - ys);
|
|---|
| 579 | }
|
|---|
| 580 | ptr = nl + 1;
|
|---|
| 581 | }
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | void
|
|---|
| 585 | CavernLogWindow::OnEndProcess(wxProcessEvent & evt)
|
|---|
| 586 | {
|
|---|
| 587 | bool cavern_success = evt.GetExitCode() == 0;
|
|---|
| 588 |
|
|---|
| 589 | // Read and process any remaining buffered output.
|
|---|
| 590 | wxInputStream* in = cavern_out->GetInputStream();
|
|---|
| 591 | while (!in->Eof()) {
|
|---|
| 592 | CheckForOutput(true);
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | wxEndBusyCursor();
|
|---|
| 596 |
|
|---|
| 597 | delete cavern_out;
|
|---|
| 598 | cavern_out = NULL;
|
|---|
| 599 |
|
|---|
| 600 | // Initially place buttons off the right of the window - they get moved to
|
|---|
| 601 | // the desired position by OnPaintButton().
|
|---|
| 602 | wxPoint off_right(GetSize().x, 0);
|
|---|
| 603 | /* TRANSLATORS: Label for button in aven’s cavern log window which
|
|---|
| 604 | * allows the user to save the log to a file. */
|
|---|
| 605 | save_button = new wxButton(this, LOG_SAVE, wmsg(/*&Save Log*/446), off_right);
|
|---|
| 606 |
|
|---|
| 607 | /* TRANSLATORS: Label for button in aven’s cavern log window which
|
|---|
| 608 | * causes the survey data to be reprocessed. */
|
|---|
| 609 | reprocess_button = new wxButton(this, LOG_REPROCESS, wmsg(/*&Reprocess*/184), off_right);
|
|---|
| 610 |
|
|---|
| 611 | if (cavern_success) {
|
|---|
| 612 | ok_button = new wxButton(this, wxID_OK, wxString(), off_right);
|
|---|
| 613 | ok_button->SetDefault();
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | Refresh();
|
|---|
| 617 | if (!cavern_success) {
|
|---|
| 618 | return;
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | init_done = false;
|
|---|
| 622 |
|
|---|
| 623 | {
|
|---|
| 624 | wxString file3d(filename, 0, filename.length() - 3);
|
|---|
| 625 | file3d.append(wxT("3d"));
|
|---|
| 626 | if (!mainfrm->LoadData(file3d, survey)) {
|
|---|
| 627 | return;
|
|---|
| 628 | }
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 | // Don't stay on log if there there are only "info" diagnostics.
|
|---|
| 632 | if (link_count == info_count) {
|
|---|
| 633 | wxCommandEvent dummy;
|
|---|
| 634 | OnOK(dummy);
|
|---|
| 635 | }
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | void
|
|---|
| 639 | CavernLogWindow::OnReprocess(wxCommandEvent &)
|
|---|
| 640 | {
|
|---|
| 641 | process(filename);
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | void
|
|---|
| 645 | CavernLogWindow::OnSave(wxCommandEvent &)
|
|---|
| 646 | {
|
|---|
| 647 | wxString filelog(filename, 0, filename.length() - 3);
|
|---|
| 648 | #ifdef __WXMSW__
|
|---|
| 649 | // We need to consistently use `\` here.
|
|---|
| 650 | filelog.Replace("/", "\\");
|
|---|
| 651 | #endif
|
|---|
| 652 | filelog += wxT("log");
|
|---|
| 653 | #ifdef __WXMOTIF__
|
|---|
| 654 | wxString ext(wxT("*.log"));
|
|---|
| 655 | #else
|
|---|
| 656 | /* TRANSLATORS: Log files from running cavern (extension .log) */
|
|---|
| 657 | wxString ext = wmsg(/*Log files*/447);
|
|---|
| 658 | ext += wxT("|*.log");
|
|---|
| 659 | #endif
|
|---|
| 660 | wxFileDialog dlg(this, wmsg(/*Select an output filename*/319),
|
|---|
| 661 | wxString(), filelog, ext,
|
|---|
| 662 | wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
|
|---|
| 663 | if (dlg.ShowModal() != wxID_OK) return;
|
|---|
| 664 | filelog = dlg.GetPath();
|
|---|
| 665 | FILE * fh_log = wxFopen(filelog, wxT("w"));
|
|---|
| 666 | if (!fh_log) {
|
|---|
| 667 | wxGetApp().ReportError(wxString::Format(wmsg(/*Error writing to file “%s”*/7), filelog.c_str()));
|
|---|
| 668 | return;
|
|---|
| 669 | }
|
|---|
| 670 | FWRITE_(log_txt.data(), log_txt.size(), 1, fh_log);
|
|---|
| 671 | #ifdef _WIN32
|
|---|
| 672 | // Untested attempt to address https://trac.survex.com/ticket/147
|
|---|
| 673 | _commit(fileno(fh_log));
|
|---|
| 674 | #endif
|
|---|
| 675 | fclose(fh_log);
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | void
|
|---|
| 679 | CavernLogWindow::OnOK(wxCommandEvent &)
|
|---|
| 680 | {
|
|---|
| 681 | if (init_done) {
|
|---|
| 682 | mainfrm->HideLog(this);
|
|---|
| 683 | } else {
|
|---|
| 684 | mainfrm->InitialiseAfterLoad(filename, survey);
|
|---|
| 685 | init_done = true;
|
|---|
| 686 | }
|
|---|
| 687 | }
|
|---|