Escape for CreateProcess(), not cmd.exe
[survex.git] / src / cavernlog.cc
blob92f79fbf2240a5e7f6af35234daeb18dc9f1c770
1 /* cavernlog.cc
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
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include "aven.h"
26 #include "cavernlog.h"
27 #include "filename.h"
28 #include "mainfrm.h"
29 #include "message.h"
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
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>
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;\">&#xfffd;</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
53 // threads).
54 class CavernOutputEvent;
56 wxDEFINE_EVENT(wxEVT_CAVERN_OUTPUT, CavernOutputEvent);
58 class CavernOutputEvent : public wxEvent {
59 public:
60 char buf[1000];
61 int len;
62 CavernOutputEvent() : wxEvent(0, wxEVT_CAVERN_OUTPUT), len(0) { }
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;
72 #ifdef CAVERNLOG_USE_THREADS
73 class CavernThread : public wxThread {
74 protected:
75 virtual ExitCode Entry();
77 CavernLogWindow *handler;
79 wxInputStream * in;
81 public:
82 CavernThread(CavernLogWindow *handler_, wxInputStream * in_)
83 : wxThread(wxTHREAD_DETACHED), handler(handler_), in(in_) { }
85 ~CavernThread() {
86 wxCriticalSectionLocker enter(handler->thread_lock);
87 handler->thread = NULL;
91 wxThread::ExitCode
92 CavernThread::Entry()
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;
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;
111 e->len = n;
112 handler->QueueEvent(e);
116 #else
118 void
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.
127 wxMilliSleep(10);
129 if (in->CanRead()) {
130 CavernOutputEvent * e = new CavernOutputEvent();
131 in->Read(e->buf, sizeof(e->buf));
132 size_t n = in->LastRead();
133 if (n == 0) {
134 delete e;
135 return;
137 e->len = n;
138 QueueEvent(e);
141 event.RequestMore();
143 #endif
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)
152 #else
153 EVT_IDLE(CavernLogWindow::OnIdle)
154 #endif
155 EVT_END_PROCESS(wxID_ANY, CavernLogWindow::OnEndProcess)
156 END_EVENT_TABLE()
158 static wxString escape_for_shell(wxString s, bool protect_dash = false)
160 #ifdef __WXMSW__
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) {
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;
182 if (s[p] == wxT('"')) {
183 // Escape any preceding backslashes and this quote.
184 s.insert(p, backslashes + 1, wxT('\\'));
185 p += backslashes + 1;
188 done:
189 s.append(wxT('"'));
191 #else
192 size_t p = 0;
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("./"));
197 p = 2;
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('\\'));
203 ++p;
205 ++p;
207 #endif
208 return s;
211 CavernLogWindow::CavernLogWindow(MainFrm * mainfrm_, const wxString & survey_, wxWindow * parent)
212 : wxHtmlWindow(parent), mainfrm(mainfrm_), cavern_out(NULL),
213 link_count(0), end(buf), init_done(false), survey(survey_)
214 #ifdef CAVERNLOG_USE_THREADS
215 , thread(NULL)
216 #endif
218 int fsize = parent->GetFont().GetPointSize();
219 int sizes[7] = { fsize, fsize, fsize, fsize, fsize, fsize, fsize };
220 SetFonts(wxString(), wxString(), sizes);
223 CavernLogWindow::~CavernLogWindow()
225 #ifdef CAVERNLOG_USE_THREADS
226 if (thread) stop_thread();
227 #endif
228 if (cavern_out) {
229 wxEndBusyCursor();
230 cavern_out->Detach();
234 #ifdef CAVERNLOG_USE_THREADS
235 void
236 CavernLogWindow::stop_thread()
238 // Killing the subprocess by its pid is theoretically racy, but in practice
239 // it's not going to cause issues, and it's all the wxProcess API seems to
240 // allow us to do. If we don't kill the subprocess, we need to wait for it
241 // to write out some output - there seems to be no way to do the equivalent
242 // of select() with a timeout on a a wxInputStream.
244 // The only alternative to this seems to be to do:
246 // while (!s.CanRead()) {
247 // if (TestDestroy()) return (wxThread::ExitCode)0;
248 // wxMilliSleep(N);
249 // }
251 // But that makes the log window update sluggishly, and we're using a
252 // worker thread precisely to try to avoid having to do dumb stuff like
253 // this.
254 wxProcess::Kill(cavern_out->GetPid());
257 wxCriticalSectionLocker enter(thread_lock);
258 if (thread) {
259 wxThreadError res;
260 #if wxCHECK_VERSION(2,9,2)
261 res = thread->Delete(NULL, wxTHREAD_WAIT_BLOCK);
262 #else
263 res = thread->Delete();
264 #endif
265 if (res != wxTHREAD_NO_ERROR) {
266 // FIXME
271 // Wait for thread to complete.
272 while (true) {
274 wxCriticalSectionLocker enter(thread_lock);
275 if (!thread) break;
277 wxMilliSleep(1);
281 void
282 CavernLogWindow::OnClose(wxCloseEvent &)
284 if (thread) stop_thread();
285 Destroy();
287 #endif
289 void
290 CavernLogWindow::OnLinkClicked(const wxHtmlLinkInfo &link)
292 wxString href = link.GetHref();
293 wxString title = link.GetTarget();
294 size_t colon2 = href.rfind(wxT(':'));
295 if (colon2 == wxString::npos)
296 return;
297 size_t colon = href.rfind(wxT(':'), colon2 - 1);
298 if (colon == wxString::npos)
299 return;
300 #ifdef __WXMSW__
301 wxString cmd = wxT("notepad $f");
302 #elif defined __WXMAC__
303 wxString cmd = wxT("open -t $f");
304 #else
305 wxString cmd = wxT("x-terminal-emulator -e vim +'call cursor($l,$c)' $f");
306 // wxString cmd = wxT("gedit -b $f +$l:$c $f");
307 // wxString cmd = wxT("x-terminal-emulator -e emacs +$l $f");
308 // wxString cmd = wxT("x-terminal-emulator -e nano +$l $f");
309 // wxString cmd = wxT("x-terminal-emulator -e jed -g $l $f");
310 #endif
311 wxChar * p = wxGetenv(wxT("SURVEXEDITOR"));
312 if (p) {
313 cmd = p;
314 if (!cmd.find(wxT("$f"))) {
315 cmd += wxT(" $f");
318 size_t i = 0;
319 while ((i = cmd.find(wxT('$'), i)) != wxString::npos) {
320 if (++i >= cmd.size()) break;
321 switch ((int)cmd[i]) {
322 case wxT('$'):
323 cmd.erase(i, 1);
324 break;
325 case wxT('f'): {
326 wxString f = escape_for_shell(href.substr(0, colon), true);
327 cmd.replace(i - 1, 2, f);
328 i += f.size() - 1;
329 break;
331 case wxT('t'): {
332 wxString t = escape_for_shell(title);
333 cmd.replace(i - 1, 2, t);
334 i += t.size() - 1;
335 break;
337 case wxT('l'): {
338 wxString l = escape_for_shell(href.substr(colon + 1, colon2 - colon - 1));
339 cmd.replace(i - 1, 2, l);
340 i += l.size() - 1;
341 break;
343 case wxT('c'): {
344 wxString l;
345 if (colon2 >= href.size())
346 l = wxT("0");
347 else
348 l = escape_for_shell(href.substr(colon2 + 1));
349 cmd.replace(i - 1, 2, l);
350 i += l.size() - 1;
351 break;
353 default:
354 ++i;
358 if (wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER) >= 0)
359 return;
361 wxString m;
362 // TRANSLATORS: %s is replaced by the command we attempted to run.
363 m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str());
364 m += wxT(" (");
365 m += wxString(strerror(errno), wxConvUTF8);
366 m += wxT(')');
367 wxGetApp().ReportError(m);
370 void
371 CavernLogWindow::process(const wxString &file)
373 SetPage(wxString());
374 #ifdef CAVERNLOG_USE_THREADS
375 if (thread) stop_thread();
376 #endif
377 if (cavern_out) {
378 cavern_out->Detach();
379 cavern_out = NULL;
380 } else {
381 wxBeginBusyCursor();
384 SetFocus();
385 filename = file;
387 link_count = 0;
388 cur.resize(0);
389 log_txt.resize(0);
391 #ifdef __WXMSW__
392 SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1"));
393 #else
394 setenv("SURVEX_UTF8", "1", 1);
395 #endif
397 wxString escaped_file = escape_for_shell(file, true);
398 #ifdef __WXMSW__
399 wxString cmd;
401 DWORD len = 256;
402 wchar_t *buf = NULL;
403 while (1) {
404 DWORD got;
405 buf = (wchar_t*)osrealloc(buf, len * 2);
406 got = GetModuleFileNameW(NULL, buf, len);
407 if (got < len) break;
408 len += len;
410 /* Strange Win32 nastiness - strip prefix "\\?\" if present */
411 wchar_t *start = buf;
412 if (wcsncmp(start, L"\\\\?\\", 4) == 0) start += 4;
413 wchar_t * slash = wcsrchr(start, L'\\');
414 if (slash) {
415 cmd.assign(start, slash - start + 1);
417 osfree(buf);
419 cmd += L"cavern";
420 cmd = escape_for_shell(cmd, false);
421 #else
422 char *cavern = use_path(msg_exepth(), "cavern");
423 wxString cmd = escape_for_shell(wxString(cavern, wxConvUTF8), false);
424 osfree(cavern);
425 #endif
426 cmd += wxT(" -o ");
427 cmd += escaped_file;
428 cmd += wxT(' ');
429 cmd += escaped_file;
431 cavern_out = wxProcess::Open(cmd);
432 if (!cavern_out) {
433 wxString m;
434 m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str());
435 m += wxT(" (");
436 m += wxString(strerror(errno), wxConvUTF8);
437 m += wxT(')');
438 wxGetApp().ReportError(m);
439 return;
442 // We want to receive the wxProcessEvent when cavern exits.
443 cavern_out->SetNextHandler(this);
445 #ifdef CAVERNLOG_USE_THREADS
446 thread = new CavernThread(this, cavern_out->GetInputStream());
447 if (thread->Run() != wxTHREAD_NO_ERROR) {
448 wxGetApp().ReportError(wxT("Thread failed to start"));
449 delete thread;
450 thread = NULL;
452 #endif
455 void
456 CavernLogWindow::OnCavernOutput(wxCommandEvent & e_)
458 CavernOutputEvent & e = (CavernOutputEvent&)e_;
460 if (e.len > 0) {
461 ssize_t n = e.len;
462 if (size_t(n) > sizeof(buf) - (end - buf)) abort();
463 memcpy(end, e.buf, n);
464 log_txt.append((const char *)end, n);
465 end += n;
467 const unsigned char * p = buf;
469 while (p != end) {
470 int ch = *p++;
471 if (ch >= 0x80) {
472 // Decode multi-byte UTF-8 sequence.
473 if (ch < 0xc0) {
474 // Invalid UTF-8 sequence.
475 goto bad_utf8;
476 } else if (ch < 0xe0) {
477 /* 2 byte sequence */
478 if (p == end) {
479 // Incomplete UTF-8 sequence - try to read more.
480 break;
482 int ch1 = *p++;
483 if ((ch1 & 0xc0) != 0x80) {
484 // Invalid UTF-8 sequence.
485 goto bad_utf8;
487 ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f);
488 } else if (ch < 0xf0) {
489 /* 3 byte sequence */
490 if (end - p <= 1) {
491 // Incomplete UTF-8 sequence - try to read more.
492 break;
494 int ch1 = *p++;
495 ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6);
496 if ((ch1 & 0xc0) != 0x80) {
497 // Invalid UTF-8 sequence.
498 goto bad_utf8;
500 int ch2 = *p++;
501 if ((ch2 & 0xc0) != 0x80) {
502 // Invalid UTF-8 sequence.
503 goto bad_utf8;
505 ch |= (ch2 & 0x3f);
506 } else {
507 // Overlong UTF-8 sequence.
508 goto bad_utf8;
512 if (false) {
513 bad_utf8:
514 // Resync to next byte which starts a UTF-8 sequence.
515 while (p != end) {
516 if (*p < 0x80 || (*p >= 0xc0 && *p < 0xf0)) break;
517 ++p;
519 cur += badutf8_html;
520 continue;
523 switch (ch) {
524 case '\r':
525 // Ignore.
526 break;
527 case '\n': {
528 if (cur.empty()) continue;
529 #ifndef __WXMSW__
530 size_t colon = cur.find(':');
531 #else
532 // If the path is "C:\path\to\file.svx" then don't split at the
533 // : after the drive letter! FIXME: better to look for ": "?
534 size_t colon = cur.find(':', 2);
535 #endif
536 if (colon != wxString::npos && colon < cur.size() - 2) {
537 ++colon;
538 size_t i = colon;
539 while (i < cur.size() - 2 &&
540 cur[i] >= wxT('0') && cur[i] <= wxT('9')) {
541 ++i;
543 if (i > colon && cur[i] == wxT(':') ) {
544 colon = i;
545 // Check for column number.
546 while (++i < cur.size() - 2 &&
547 cur[i] >= wxT('0') && cur[i] <= wxT('9')) { }
548 if (i > colon + 1 && cur[i] == wxT(':') ) {
549 colon = i;
550 } else {
551 // If there's no colon, include a trailing ':'
552 // so that we can unambiguously split the href
553 // value up into filename, line and column.
554 ++colon;
556 wxString tag = wxT("<a href=\"");
557 tag.append(cur, 0, colon);
558 while (cur[++i] == wxT(' ')) { }
559 tag += wxT("\" target=\"");
560 wxString target(cur, i, wxString::npos);
561 target.Replace(badutf8_html, badutf8);
562 tag += target;
563 tag += wxT("\">");
564 cur.insert(0, tag);
565 size_t offset = colon + tag.size();
566 cur.insert(offset, wxT("</a>"));
567 offset += 4 + 2;
569 static const wxString & error_marker = wmsg(/*error*/93) + ":";
570 static const wxString & warning_marker = wmsg(/*warning*/4) + ":";
572 if (cur.substr(offset, error_marker.size()) == error_marker) {
573 // Show "error" marker in red.
574 cur.insert(offset, wxT("<span style=\"color:red\">"));
575 offset += 24 + error_marker.size() - 1;
576 cur.insert(offset, wxT("</span>"));
577 } else if (cur.substr(offset, warning_marker.size()) == warning_marker) {
578 // Show "warning" marker in orange.
579 cur.insert(offset, wxT("<span style=\"color:orange\">"));
580 offset += 27 + warning_marker.size() - 1;
581 cur.insert(offset, wxT("</span>"));
584 ++link_count;
588 // Save the scrollbar positions.
589 int scroll_x = 0, scroll_y = 0;
590 GetViewStart(&scroll_x, &scroll_y);
592 cur += wxT("<br>\n");
593 AppendToPage(cur);
595 if (!link_count) {
596 // Auto-scroll the window until we've reported a
597 // warning or error.
598 int x, y;
599 GetVirtualSize(&x, &y);
600 int xs, ys;
601 GetClientSize(&xs, &ys);
602 y -= ys;
603 int xu, yu;
604 GetScrollPixelsPerUnit(&xu, &yu);
605 Scroll(scroll_x, y / yu);
606 } else {
607 // Restore the scrollbar positions.
608 Scroll(scroll_x, scroll_y);
611 cur.clear();
612 break;
614 case '<':
615 cur += wxT("&lt;");
616 break;
617 case '>':
618 cur += wxT("&gt;");
619 break;
620 case '&':
621 cur += wxT("&amp;");
622 break;
623 case '"':
624 cur += wxT("&#22;");
625 continue;
626 default:
627 if (ch >= 128) {
628 cur += wxString::Format(wxT("&#%u;"), ch);
629 } else {
630 cur += (char)ch;
635 size_t left = end - p;
636 end = buf + left;
637 if (left) memmove(buf, p, left);
638 Update();
639 return;
642 if (e.len <= 0 && buf != end) {
643 // Truncated UTF-8 sequence.
644 cur += badutf8_html;
647 /* TRANSLATORS: Label for button in aven’s cavern log window which
648 * allows the user to save the log to a file. */
649 AppendToPage(wxString::Format(wxT("<avenbutton id=%d name=\"%s\">"),
650 (int)LOG_SAVE,
651 wmsg(/*Save Log*/446).c_str()));
652 wxEndBusyCursor();
653 delete cavern_out;
654 cavern_out = NULL;
655 if (e.len < 0) {
656 /* Negative length indicates non-zero exit status from cavern. */
657 /* TRANSLATORS: Label for button in aven’s cavern log window which
658 * causes the survey data to be reprocessed. */
659 AppendToPage(wxString::Format(wxT("<avenbutton default id=%d name=\"%s\">"),
660 (int)LOG_REPROCESS,
661 wmsg(/*Reprocess*/184).c_str()));
662 return;
664 AppendToPage(wxString::Format(wxT("<avenbutton id=%d name=\"%s\">"),
665 (int)LOG_REPROCESS,
666 wmsg(/*Reprocess*/184).c_str()));
667 AppendToPage(wxString::Format(wxT("<avenbutton default id=%d>"), (int)wxID_OK));
668 Update();
669 init_done = false;
672 wxString file3d(filename, 0, filename.length() - 3);
673 file3d.append(wxT("3d"));
674 if (!mainfrm->LoadData(file3d, survey)) {
675 return;
679 if (link_count == 0) {
680 wxCommandEvent dummy;
681 OnOK(dummy);
685 void
686 CavernLogWindow::OnEndProcess(wxProcessEvent & evt)
688 CavernOutputEvent * e = new CavernOutputEvent();
689 // Zero length indicates successful exit, negative length unsuccessful exit.
690 e->len = (evt.GetExitCode() == 0 ? 0 : -1);
691 QueueEvent(e);
694 void
695 CavernLogWindow::OnReprocess(wxCommandEvent &)
697 process(filename);
700 void
701 CavernLogWindow::OnSave(wxCommandEvent &)
703 wxString filelog(filename, 0, filename.length() - 3);
704 filelog += wxT("log");
705 AvenAllowOnTop ontop(mainfrm);
706 #ifdef __WXMOTIF__
707 wxString ext(wxT("*.log"));
708 #else
709 /* TRANSLATORS: Log files from running cavern (extension .log) */
710 wxString ext = wmsg(/*Log files*/447);
711 ext += wxT("|*.log");
712 #endif
713 wxFileDialog dlg(this, wmsg(/*Select an output filename*/319),
714 wxString(), filelog, ext,
715 wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
716 if (dlg.ShowModal() != wxID_OK) return;
717 filelog = dlg.GetPath();
718 FILE * fh_log = wxFopen(filelog, wxT("w"));
719 if (!fh_log) {
720 wxGetApp().ReportError(wxString::Format(wmsg(/*Error writing to file “%s”*/110), filelog.c_str()));
721 return;
723 fwrite(log_txt.data(), log_txt.size(), 1, fh_log);
724 fclose(fh_log);
727 void
728 CavernLogWindow::OnOK(wxCommandEvent &)
730 if (init_done) {
731 mainfrm->HideLog(this);
732 } else {
733 mainfrm->InitialiseAfterLoad(filename);
734 init_done = true;