1 | /* cavernlog.cc |
---|
2 | * Run cavern inside an Aven window |
---|
3 | * |
---|
4 | * Copyright (C) 2005-2024 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, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
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 | #define GVIM_COMMAND "gvim +'call cursor($l,$c)' $f" |
---|
43 | #define VIM_COMMAND "x-terminal-emulator -e vim +'call cursor($l,$c)' $f" |
---|
44 | #define NVIM_COMMAND "x-terminal-emulator -e nvim +'call cursor($l,$c)' $f" |
---|
45 | #define GEDIT_COMMAND "gedit $f +$l:$c" |
---|
46 | // Pluma currently ignores the column, but include it assuming some future |
---|
47 | // version will add support. |
---|
48 | #define PLUMA_COMMAND "pluma +$l:$c $f" |
---|
49 | #define EMACS_COMMAND "x-terminal-emulator -e emacs +$l:$c $f" |
---|
50 | #define NANO_COMMAND "x-terminal-emulator -e nano +$l,$c $f" |
---|
51 | #define JED_COMMAND "x-terminal-emulator -e jed $f -g $l" |
---|
52 | #define KATE_COMMAND "kate -l $l -c $c $f" |
---|
53 | |
---|
54 | #ifdef __WXMSW__ |
---|
55 | # define DEFAULT_EDITOR_COMMAND "notepad $f" |
---|
56 | #elif defined __WXMAC__ |
---|
57 | # define DEFAULT_EDITOR_COMMAND "open -t $f" |
---|
58 | #else |
---|
59 | # define DEFAULT_EDITOR_COMMAND VIM_COMMAND |
---|
60 | #endif |
---|
61 | |
---|
62 | enum { LOG_REPROCESS = 1234, LOG_SAVE = 1235 }; |
---|
63 | |
---|
64 | static const wxString badutf8_html( |
---|
65 | wxT("<span style=\"color:white;background-color:red;\">�</span>")); |
---|
66 | static const wxString badutf8(wxUniChar(0xfffd)); |
---|
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(*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(*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 | } |
---|
289 | |
---|
290 | CavernLogWindow::~CavernLogWindow() |
---|
291 | { |
---|
292 | timer.Stop(); |
---|
293 | if (cavern_out) { |
---|
294 | wxEndBusyCursor(); |
---|
295 | cavern_out->Detach(); |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | void |
---|
300 | CavernLogWindow::OnMouseMove(wxMouseEvent& e) |
---|
301 | { |
---|
302 | const auto& pos = e.GetPosition(); |
---|
303 | int fsize = GetFont().GetPixelSize().GetHeight(); |
---|
304 | int scroll_x = 0, scroll_y = 0; |
---|
305 | GetViewStart(&scroll_x, &scroll_y); |
---|
306 | unsigned line = pos.y / fsize + scroll_y; |
---|
307 | unsigned x = pos.x + scroll_x * fsize - fsize / 2; |
---|
308 | if (line < line_info.size() && x <= line_info[line].link_pixel_width) { |
---|
309 | SetCursor(wxCursor(wxCURSOR_HAND)); |
---|
310 | } else { |
---|
311 | SetCursor(wxNullCursor); |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | void |
---|
316 | CavernLogWindow::OnLinkClicked(wxMouseEvent& e) |
---|
317 | { |
---|
318 | const auto& pos = e.GetPosition(); |
---|
319 | int fsize = GetFont().GetPixelSize().GetHeight(); |
---|
320 | int scroll_x = 0, scroll_y = 0; |
---|
321 | GetViewStart(&scroll_x, &scroll_y); |
---|
322 | unsigned line = pos.y / fsize + scroll_y; |
---|
323 | unsigned x = pos.x + scroll_x * fsize - fsize / 2; |
---|
324 | if (!(line < line_info.size() && x <= line_info[line].link_pixel_width)) |
---|
325 | return; |
---|
326 | |
---|
327 | const char* cur = &log_txt[line_info[line].start_offset]; |
---|
328 | size_t link_len = line_info[line].link_len; |
---|
329 | size_t colon = link_len; |
---|
330 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { } |
---|
331 | size_t colon2 = colon; |
---|
332 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { } |
---|
333 | if (cur[colon] != ':') { |
---|
334 | colon = colon2; |
---|
335 | colon2 = link_len; |
---|
336 | } |
---|
337 | |
---|
338 | wxString cmd; |
---|
339 | wxChar * p = wxGetenv(wxT("SURVEXEDITOR")); |
---|
340 | if (p) { |
---|
341 | cmd = p; |
---|
342 | if (!cmd.find(wxT("$f"))) { |
---|
343 | cmd += wxT(" $f"); |
---|
344 | } |
---|
345 | } else { |
---|
346 | p = wxGetenv(wxT("VISUAL")); |
---|
347 | if (!p) p = wxGetenv(wxT("EDITOR")); |
---|
348 | if (!p) { |
---|
349 | cmd = wxT(DEFAULT_EDITOR_COMMAND); |
---|
350 | } else { |
---|
351 | cmd = p; |
---|
352 | if (cmd == "gvim") { |
---|
353 | cmd = wxT(GVIM_COMMAND); |
---|
354 | } else if (cmd == "vim") { |
---|
355 | cmd = wxT(VIM_COMMAND); |
---|
356 | } else if (cmd == "nvim") { |
---|
357 | cmd = wxT(NVIM_COMMAND); |
---|
358 | } else if (cmd == "gedit") { |
---|
359 | cmd = wxT(GEDIT_COMMAND); |
---|
360 | } else if (cmd == "pluma") { |
---|
361 | cmd = wxT(PLUMA_COMMAND); |
---|
362 | } else if (cmd == "emacs") { |
---|
363 | cmd = wxT(EMACS_COMMAND); |
---|
364 | } else if (cmd == "nano") { |
---|
365 | cmd = wxT(NANO_COMMAND); |
---|
366 | } else if (cmd == "jed") { |
---|
367 | cmd = wxT(JED_COMMAND); |
---|
368 | } else if (cmd == "kate") { |
---|
369 | cmd = wxT(KATE_COMMAND); |
---|
370 | } else { |
---|
371 | // Escape any $. |
---|
372 | cmd.Replace(wxT("$"), wxT("$$")); |
---|
373 | cmd += wxT(" $f"); |
---|
374 | } |
---|
375 | } |
---|
376 | } |
---|
377 | size_t i = 0; |
---|
378 | while ((i = cmd.find(wxT('$'), i)) != wxString::npos) { |
---|
379 | if (++i >= cmd.size()) break; |
---|
380 | switch ((int)cmd[i]) { |
---|
381 | case wxT('$'): |
---|
382 | cmd.erase(i, 1); |
---|
383 | break; |
---|
384 | case wxT('f'): { |
---|
385 | wxString f = escape_for_shell(wxString(cur, colon), true); |
---|
386 | cmd.replace(i - 1, 2, f); |
---|
387 | i += f.size() - 1; |
---|
388 | break; |
---|
389 | } |
---|
390 | case wxT('l'): { |
---|
391 | wxString l = escape_for_shell(wxString(cur + colon + 1, colon2 - colon - 1)); |
---|
392 | cmd.replace(i - 1, 2, l); |
---|
393 | i += l.size() - 1; |
---|
394 | break; |
---|
395 | } |
---|
396 | case wxT('c'): { |
---|
397 | wxString l; |
---|
398 | if (colon2 == link_len) |
---|
399 | l = wxT("0"); |
---|
400 | else |
---|
401 | l = escape_for_shell(wxString(cur + colon2 + 1, link_len - colon2 - 1)); |
---|
402 | cmd.replace(i - 1, 2, l); |
---|
403 | i += l.size() - 1; |
---|
404 | break; |
---|
405 | } |
---|
406 | default: |
---|
407 | ++i; |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | if (wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER) >= 0) |
---|
412 | return; |
---|
413 | |
---|
414 | wxString m; |
---|
415 | // TRANSLATORS: %s is replaced by the command we attempted to run. |
---|
416 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str()); |
---|
417 | m += wxT(" ("); |
---|
418 | m += wxString::FromUTF8(strerror(errno)); |
---|
419 | m += wxT(')'); |
---|
420 | wxGetApp().ReportError(m); |
---|
421 | } |
---|
422 | |
---|
423 | void |
---|
424 | CavernLogWindow::process(const wxString &file) |
---|
425 | { |
---|
426 | timer.Stop(); |
---|
427 | if (cavern_out) { |
---|
428 | cavern_out->Detach(); |
---|
429 | cavern_out = NULL; |
---|
430 | } else { |
---|
431 | wxBeginBusyCursor(); |
---|
432 | } |
---|
433 | |
---|
434 | SetFocus(); |
---|
435 | filename = file; |
---|
436 | |
---|
437 | info_count = 0; |
---|
438 | link_count = 0; |
---|
439 | log_txt.resize(0); |
---|
440 | line_info.resize(0); |
---|
441 | // Reserve enough that we won't need to grow the allocations in normal cases. |
---|
442 | log_txt.reserve(16384); |
---|
443 | line_info.reserve(256); |
---|
444 | ptr = 0; |
---|
445 | save_button = nullptr; |
---|
446 | reprocess_button = nullptr; |
---|
447 | ok_button = nullptr; |
---|
448 | DestroyChildren(); |
---|
449 | SetVirtualSize(0, 0); |
---|
450 | |
---|
451 | #ifdef __WXMSW__ |
---|
452 | SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1")); |
---|
453 | #else |
---|
454 | setenv("SURVEX_UTF8", "1", 1); |
---|
455 | #endif |
---|
456 | |
---|
457 | wxString escaped_file = escape_for_shell(file, true); |
---|
458 | wxString cmd = get_command_path(L"cavern"); |
---|
459 | cmd = escape_for_shell(cmd, false); |
---|
460 | cmd += wxT(" -o "); |
---|
461 | cmd += escaped_file; |
---|
462 | cmd += wxT(' '); |
---|
463 | cmd += escaped_file; |
---|
464 | |
---|
465 | cavern_out = wxProcess::Open(cmd); |
---|
466 | if (!cavern_out) { |
---|
467 | wxString m; |
---|
468 | m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str()); |
---|
469 | m += wxT(" ("); |
---|
470 | m += wxString::FromUTF8(strerror(errno)); |
---|
471 | m += wxT(')'); |
---|
472 | wxGetApp().ReportError(m); |
---|
473 | return; |
---|
474 | } |
---|
475 | |
---|
476 | // We want to receive the wxProcessEvent when cavern exits. |
---|
477 | cavern_out->SetNextHandler(this); |
---|
478 | |
---|
479 | // Check for output after 500ms if we don't get an idle event sooner. |
---|
480 | timer.StartOnce(500); |
---|
481 | } |
---|
482 | |
---|
483 | void |
---|
484 | CavernLogWindow::ProcessCavernOutput() |
---|
485 | { |
---|
486 | // ptr gives the start of the first line we've not yet processed. |
---|
487 | |
---|
488 | size_t nl; |
---|
489 | while ((nl = log_txt.find('\n', ptr)) != std::string::npos) { |
---|
490 | if (nl == ptr || (nl - ptr == 1 && log_txt[ptr] == '\r')) { |
---|
491 | // Don't show empty lines in the window. |
---|
492 | ptr = nl + 1; |
---|
493 | continue; |
---|
494 | } |
---|
495 | size_t line_len = nl - ptr - (log_txt[nl - 1] == '\r'); |
---|
496 | // FIXME: Avoid copy, use string_view? |
---|
497 | string cur(log_txt, ptr, line_len); |
---|
498 | if (log_txt[ptr] == ' ') { |
---|
499 | if (expecting_caret_line) { |
---|
500 | // FIXME: Check the line is only space, `^` and `~`? |
---|
501 | // Otherwise an error without caret info followed |
---|
502 | // by an error which contains a '^' gets |
---|
503 | // mishandled... |
---|
504 | size_t caret = cur.rfind('^'); |
---|
505 | if (caret != wxString::npos) { |
---|
506 | size_t tilde = cur.rfind('~'); |
---|
507 | if (tilde == wxString::npos || tilde < caret) { |
---|
508 | tilde = caret; |
---|
509 | } |
---|
510 | line_info.back().colour = line_info[line_info.size() - 2].colour; |
---|
511 | line_info.back().colour_start = caret; |
---|
512 | line_info.back().colour_len = tilde - caret + 1; |
---|
513 | expecting_caret_line = false; |
---|
514 | ptr = nl + 1; |
---|
515 | continue; |
---|
516 | } |
---|
517 | } |
---|
518 | expecting_caret_line = true; |
---|
519 | } |
---|
520 | line_info.emplace_back(ptr); |
---|
521 | line_info.back().len = line_len; |
---|
522 | size_t colon = cur.find(": "); |
---|
523 | if (colon != wxString::npos) { |
---|
524 | size_t link_len = colon; |
---|
525 | while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { } |
---|
526 | if (cur[colon] == ':') { |
---|
527 | line_info.back().link_len = link_len; |
---|
528 | |
---|
529 | static string info_marker = string(msg(/*info*/485)) + ':'; |
---|
530 | static string warning_marker = string(msg(/*warning*/106)) + ':'; |
---|
531 | static string error_marker = string(msg(/*error*/93)) + ':'; |
---|
532 | |
---|
533 | size_t offset = link_len + 2; |
---|
534 | if (cur.compare(offset, info_marker.size(), info_marker) == 0) { |
---|
535 | // Show "info" marker in blue. |
---|
536 | ++info_count; |
---|
537 | line_info.back().colour = LOG_INFO; |
---|
538 | line_info.back().colour_start = offset; |
---|
539 | line_info.back().colour_len = info_marker.size() - 1; |
---|
540 | } else if (cur.compare(offset, warning_marker.size(), warning_marker) == 0) { |
---|
541 | // Show "warning" marker in orange. |
---|
542 | line_info.back().colour = LOG_WARNING; |
---|
543 | line_info.back().colour_start = offset; |
---|
544 | line_info.back().colour_len = warning_marker.size() - 1; |
---|
545 | } else if (cur.compare(offset, error_marker.size(), error_marker) == 0) { |
---|
546 | // Show "error" marker in red. |
---|
547 | line_info.back().colour = LOG_ERROR; |
---|
548 | line_info.back().colour_start = offset; |
---|
549 | line_info.back().colour_len = error_marker.size() - 1; |
---|
550 | } |
---|
551 | ++link_count; |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | int fsize = GetFont().GetPixelSize().GetHeight(); |
---|
556 | SetScrollRate(fsize, fsize); |
---|
557 | |
---|
558 | auto& info = line_info.back(); |
---|
559 | info.link_pixel_width = GetTextExtent(wxString(&log_txt[ptr], info.link_len)).GetWidth(); |
---|
560 | auto rest_pixel_width = GetTextExtent(wxString(&log_txt[ptr + info.link_len], info.len - info.link_len)).GetWidth(); |
---|
561 | int width = max(GetVirtualSize().GetWidth(), |
---|
562 | int(fsize + info.link_pixel_width + rest_pixel_width)); |
---|
563 | int height = line_info.size(); |
---|
564 | SetVirtualSize(width, height * fsize); |
---|
565 | if (!link_count) { |
---|
566 | // Auto-scroll until the first diagnostic. |
---|
567 | int scroll_x = 0, scroll_y = 0; |
---|
568 | GetViewStart(&scroll_x, &scroll_y); |
---|
569 | int xs, ys; |
---|
570 | GetClientSize(&xs, &ys); |
---|
571 | Scroll(scroll_x, line_info.size() * fsize - ys); |
---|
572 | } |
---|
573 | ptr = nl + 1; |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | void |
---|
578 | CavernLogWindow::OnEndProcess(wxProcessEvent & evt) |
---|
579 | { |
---|
580 | bool cavern_success = evt.GetExitCode() == 0; |
---|
581 | |
---|
582 | // Read and process any remaining buffered output. |
---|
583 | wxInputStream* in = cavern_out->GetInputStream(); |
---|
584 | while (!in->Eof()) { |
---|
585 | CheckForOutput(true); |
---|
586 | } |
---|
587 | |
---|
588 | wxEndBusyCursor(); |
---|
589 | |
---|
590 | delete cavern_out; |
---|
591 | cavern_out = NULL; |
---|
592 | |
---|
593 | // Initially place buttons off the right of the window - they get moved to |
---|
594 | // the desired position by OnPaintButton(). |
---|
595 | wxPoint off_right(GetSize().x, 0); |
---|
596 | /* TRANSLATORS: Label for button in aven’s cavern log window which |
---|
597 | * allows the user to save the log to a file. */ |
---|
598 | save_button = new wxButton(this, LOG_SAVE, wmsg(/*&Save Log*/446), off_right); |
---|
599 | |
---|
600 | /* TRANSLATORS: Label for button in aven’s cavern log window which |
---|
601 | * causes the survey data to be reprocessed. */ |
---|
602 | reprocess_button = new wxButton(this, LOG_REPROCESS, wmsg(/*&Reprocess*/184), off_right); |
---|
603 | |
---|
604 | if (cavern_success) { |
---|
605 | ok_button = new wxButton(this, wxID_OK, wxString(), off_right); |
---|
606 | ok_button->SetDefault(); |
---|
607 | } |
---|
608 | |
---|
609 | Refresh(); |
---|
610 | if (!cavern_success) { |
---|
611 | return; |
---|
612 | } |
---|
613 | |
---|
614 | init_done = false; |
---|
615 | |
---|
616 | { |
---|
617 | wxString file3d(filename, 0, filename.length() - 3); |
---|
618 | file3d.append(wxT("3d")); |
---|
619 | if (!mainfrm->LoadData(file3d, survey)) { |
---|
620 | return; |
---|
621 | } |
---|
622 | } |
---|
623 | |
---|
624 | // Don't stay on log if there there are only "info" diagnostics. |
---|
625 | if (link_count == info_count) { |
---|
626 | wxCommandEvent dummy; |
---|
627 | OnOK(dummy); |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | void |
---|
632 | CavernLogWindow::OnReprocess(wxCommandEvent &) |
---|
633 | { |
---|
634 | process(filename); |
---|
635 | } |
---|
636 | |
---|
637 | void |
---|
638 | CavernLogWindow::OnSave(wxCommandEvent &) |
---|
639 | { |
---|
640 | wxString filelog(filename, 0, filename.length() - 3); |
---|
641 | #ifdef __WXMSW__ |
---|
642 | // We need to consistently use `\` here. |
---|
643 | filelog.Replace("/", "\\"); |
---|
644 | #endif |
---|
645 | filelog += wxT("log"); |
---|
646 | #ifdef __WXMOTIF__ |
---|
647 | wxString ext(wxT("*.log")); |
---|
648 | #else |
---|
649 | /* TRANSLATORS: Log files from running cavern (extension .log) */ |
---|
650 | wxString ext = wmsg(/*Log files*/447); |
---|
651 | ext += wxT("|*.log"); |
---|
652 | #endif |
---|
653 | wxFileDialog dlg(this, wmsg(/*Select an output filename*/319), |
---|
654 | wxString(), filelog, ext, |
---|
655 | wxFD_SAVE|wxFD_OVERWRITE_PROMPT); |
---|
656 | if (dlg.ShowModal() != wxID_OK) return; |
---|
657 | filelog = dlg.GetPath(); |
---|
658 | FILE * fh_log = wxFopen(filelog, wxT("w")); |
---|
659 | if (!fh_log) { |
---|
660 | wxGetApp().ReportError(wxString::Format(wmsg(/*Error writing to file “%s”*/7), filelog.c_str())); |
---|
661 | return; |
---|
662 | } |
---|
663 | FWRITE_(log_txt.data(), log_txt.size(), 1, fh_log); |
---|
664 | fclose(fh_log); |
---|
665 | } |
---|
666 | |
---|
667 | void |
---|
668 | CavernLogWindow::OnOK(wxCommandEvent &) |
---|
669 | { |
---|
670 | if (init_done) { |
---|
671 | mainfrm->HideLog(this); |
---|
672 | } else { |
---|
673 | mainfrm->InitialiseAfterLoad(filename, survey); |
---|
674 | init_done = true; |
---|
675 | } |
---|
676 | } |
---|