[6bec10c] | 1 | /* cavernlog.cc |
---|
| 2 | * Run cavern inside an Aven window |
---|
| 3 | * |
---|
[a75f5a1] | 4 | * Copyright (C) 2005,2006,2010 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> |
---|
| 33 | |
---|
| 34 | // For select(): |
---|
| 35 | #ifdef HAVE_SYS_SELECT_H |
---|
| 36 | #include <sys/select.h> |
---|
| 37 | #endif |
---|
| 38 | #include <sys/time.h> |
---|
| 39 | #include <sys/types.h> |
---|
| 40 | #include <unistd.h> |
---|
| 41 | |
---|
[fb5887c] | 42 | enum { LOG_RERUN = 1234 }; |
---|
| 43 | |
---|
| 44 | BEGIN_EVENT_TABLE(CavernLogWindow, wxHtmlWindow) |
---|
| 45 | EVT_BUTTON(LOG_RERUN, CavernLogWindow::OnRerun) |
---|
| 46 | EVT_BUTTON(wxID_OK, CavernLogWindow::OnOK) |
---|
| 47 | END_EVENT_TABLE() |
---|
| 48 | |
---|
[6bec10c] | 49 | static wxString escape_for_shell(wxString s, bool protect_dash = false) |
---|
| 50 | { |
---|
| 51 | size_t p = 0; |
---|
| 52 | #ifdef __WXMSW__ |
---|
| 53 | bool needs_quotes = false; |
---|
| 54 | while (p < s.size()) { |
---|
[15322f2] | 55 | if (s[p] == wxT('"')) { |
---|
| 56 | s.insert(p, wxT('\\')); |
---|
[6bec10c] | 57 | ++p; |
---|
| 58 | needs_quotes = true; |
---|
| 59 | } |
---|
| 60 | ++p; |
---|
| 61 | } |
---|
| 62 | if (needs_quotes) { |
---|
[15322f2] | 63 | s.insert(0, wxT('"')); |
---|
| 64 | s += wxT('"'); |
---|
[6bec10c] | 65 | } |
---|
| 66 | #else |
---|
| 67 | if (protect_dash && !s.empty() && s[0u] == '-') { |
---|
| 68 | // If the filename starts with a '-', protect it from being |
---|
| 69 | // treated as an option by prepending "./". |
---|
[5627cbb] | 70 | s.insert(0, wxT("./")); |
---|
[6bec10c] | 71 | p = 2; |
---|
| 72 | } |
---|
| 73 | while (p < s.size()) { |
---|
| 74 | // Exclude a few safe characters which are common in filenames |
---|
| 75 | if (!isalnum(s[p]) && strchr("/._-", s[p]) == NULL) { |
---|
[5627cbb] | 76 | s.insert(p, wxT('\\')); |
---|
[6bec10c] | 77 | ++p; |
---|
| 78 | } |
---|
| 79 | ++p; |
---|
| 80 | } |
---|
| 81 | #endif |
---|
| 82 | return s; |
---|
| 83 | } |
---|
| 84 | |
---|
[fb5887c] | 85 | CavernLogWindow::CavernLogWindow(MainFrm * mainfrm_, wxWindow * parent) |
---|
| 86 | : wxHtmlWindow(parent), mainfrm(mainfrm_) |
---|
| 87 | { |
---|
[93ff5cc] | 88 | int fsize = parent->GetFont().GetPointSize(); |
---|
| 89 | int sizes[7] = { fsize, fsize, fsize, fsize, fsize, fsize, fsize }; |
---|
[5627cbb] | 90 | SetFonts(wxString(), wxString(), sizes); |
---|
[93ff5cc] | 91 | } |
---|
| 92 | |
---|
[6bec10c] | 93 | void |
---|
| 94 | CavernLogWindow::OnLinkClicked(const wxHtmlLinkInfo &link) |
---|
| 95 | { |
---|
| 96 | wxString href = link.GetHref(); |
---|
| 97 | wxString title = link.GetTarget(); |
---|
[5627cbb] | 98 | size_t colon = href.rfind(wxT(':')); |
---|
[6bec10c] | 99 | if (colon != wxString::npos) { |
---|
| 100 | #ifdef __WXMSW__ |
---|
[5627cbb] | 101 | wxString cmd = wxT("notepad $f"); |
---|
[6bec10c] | 102 | #else |
---|
[5627cbb] | 103 | wxString cmd = wxT("x-terminal-emulator -title $t -e vim -c $l $f"); |
---|
[6bec10c] | 104 | // wxString cmd = "x-terminal-emulator -title $t -e emacs +$l $f"; |
---|
| 105 | // wxString cmd = "x-terminal-emulator -title $t -e nano +$l $f"; |
---|
| 106 | // wxString cmd = "x-terminal-emulator -title $t -e jed -g $l $f"; |
---|
| 107 | #endif |
---|
[5627cbb] | 108 | const char * p = getenv("SURVEXEDITOR"); |
---|
[6bec10c] | 109 | if (p) { |
---|
[5627cbb] | 110 | cmd = wxString(p, wxConvUTF8); |
---|
| 111 | if (!cmd.find(wxT("$f"))) { |
---|
| 112 | cmd += wxT(" $f"); |
---|
[6bec10c] | 113 | } |
---|
| 114 | } |
---|
| 115 | size_t i = 0; |
---|
[5627cbb] | 116 | while ((i = cmd.find(wxT('$'), i)) != wxString::npos) { |
---|
[6bec10c] | 117 | if (++i >= cmd.size()) break; |
---|
| 118 | switch (cmd[i]) { |
---|
[5627cbb] | 119 | case wxT('$'): |
---|
[6bec10c] | 120 | cmd.erase(i, 1); |
---|
| 121 | break; |
---|
[5627cbb] | 122 | case wxT('f'): { |
---|
[6bec10c] | 123 | wxString f = escape_for_shell(href.substr(0, colon), true); |
---|
| 124 | cmd.replace(i - 1, 2, f); |
---|
| 125 | i += f.size() - 1; |
---|
| 126 | break; |
---|
| 127 | } |
---|
[5627cbb] | 128 | case wxT('t'): { |
---|
[6bec10c] | 129 | wxString t = escape_for_shell(title); |
---|
| 130 | cmd.replace(i - 1, 2, t); |
---|
| 131 | i += t.size() - 1; |
---|
| 132 | break; |
---|
| 133 | } |
---|
[5627cbb] | 134 | case wxT('l'): { |
---|
[6bec10c] | 135 | wxString l = escape_for_shell(href.substr(colon + 1)); |
---|
| 136 | cmd.replace(i - 1, 2, l); |
---|
| 137 | i += l.size() - 1; |
---|
| 138 | break; |
---|
| 139 | } |
---|
| 140 | default: |
---|
| 141 | ++i; |
---|
| 142 | } |
---|
| 143 | } |
---|
[d3172cc] | 144 | // FIXME: This should really use fn_str() - currently we probably can't |
---|
| 145 | // process a Unicode path on wxmsw. |
---|
[318ac31] | 146 | system(cmd.mb_str()); |
---|
[6bec10c] | 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
[5627cbb] | 150 | int |
---|
[6bec10c] | 151 | CavernLogWindow::process(const wxString &file) |
---|
| 152 | { |
---|
[fb5887c] | 153 | SetFocus(); |
---|
| 154 | filename = file; |
---|
| 155 | |
---|
[6bec10c] | 156 | char *cavern = use_path(msg_exepth(), "cavern"); |
---|
| 157 | #ifdef __WXMSW__ |
---|
[15322f2] | 158 | SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1")); |
---|
[6bec10c] | 159 | #else |
---|
[06b1227] | 160 | setenv("SURVEX_UTF8", "1", 1); |
---|
[6bec10c] | 161 | #endif |
---|
[93ff5cc] | 162 | |
---|
[6bec10c] | 163 | wxString escaped_file = escape_for_shell(file, true); |
---|
[2e4b8cd] | 164 | wxString cmd = escape_for_shell(wxString(cavern, wxConvUTF8), false); |
---|
[08b4401] | 165 | osfree(cavern); |
---|
[5627cbb] | 166 | cmd += wxT(" -o "); |
---|
[6bec10c] | 167 | cmd += escaped_file; |
---|
[5627cbb] | 168 | cmd += wxT(' '); |
---|
[6bec10c] | 169 | cmd += escaped_file; |
---|
| 170 | |
---|
[d3172cc] | 171 | // FIXME: This should really use fn_str() - currently we probably can't |
---|
| 172 | // process a Unicode path on wxmsw. |
---|
[318ac31] | 173 | FILE * cavern_out = popen(cmd.mb_str(), "r"); |
---|
[6bec10c] | 174 | if (!cavern_out) { |
---|
[5627cbb] | 175 | wxString m; |
---|
| 176 | m.Printf(wmsg(/*Couldn't open pipe: `%s'*/17), cmd.c_str()); |
---|
| 177 | m += wxT(" ("); |
---|
[2e4b8cd] | 178 | m += wxString(strerror(errno), wxConvUTF8); |
---|
[5627cbb] | 179 | m += wxT(')'); |
---|
[6bec10c] | 180 | wxGetApp().ReportError(m); |
---|
[5627cbb] | 181 | return -2; |
---|
[6bec10c] | 182 | } |
---|
| 183 | |
---|
| 184 | int cavern_fd; |
---|
| 185 | #ifdef __WXMSW__ |
---|
| 186 | cavern_fd = _fileno(cavern_out); |
---|
| 187 | #else |
---|
| 188 | cavern_fd = fileno(cavern_out); |
---|
| 189 | #endif |
---|
| 190 | assert(cavern_fd < FD_SETSIZE); // FIXME we shouldn't just assert, but what else to do? |
---|
| 191 | wxString cur; |
---|
[5627cbb] | 192 | int link_count = 0; |
---|
[6bec10c] | 193 | // We're only guaranteed one character of pushback by ungetc() but we |
---|
| 194 | // need two so for portability we implement the second ourselves. |
---|
| 195 | int left = EOF; |
---|
| 196 | while (!feof(cavern_out)) { |
---|
| 197 | fd_set rfds, efds; |
---|
| 198 | FD_ZERO(&rfds); |
---|
| 199 | FD_SET(cavern_fd, &rfds); |
---|
| 200 | FD_ZERO(&efds); |
---|
| 201 | FD_SET(cavern_fd, &efds); |
---|
[a75f5a1] | 202 | // Set timeout to 0.1 seconds. |
---|
[6bec10c] | 203 | struct timeval timeout; |
---|
| 204 | timeout.tv_sec = 0; |
---|
[a75f5a1] | 205 | timeout.tv_usec = 100000; |
---|
[6bec10c] | 206 | if (select(cavern_fd + 1, &rfds, NULL, &efds, &timeout) == 0) { |
---|
| 207 | wxYield(); |
---|
| 208 | continue; |
---|
| 209 | } |
---|
[6b088f6] | 210 | if (!FD_ISSET(cavern_fd, &rfds)) { |
---|
[6bec10c] | 211 | // Error, which pclose() should report. |
---|
| 212 | break; |
---|
| 213 | } |
---|
| 214 | int ch; |
---|
| 215 | if (left == EOF) { |
---|
| 216 | ch = getc(cavern_out); |
---|
| 217 | if (ch == EOF) break; |
---|
| 218 | } else { |
---|
| 219 | ch = left; |
---|
| 220 | left = EOF; |
---|
| 221 | } |
---|
| 222 | // Decode UTF-8 first to avoid security issues with <, >, &, etc |
---|
| 223 | // encoded using multibyte encodings. |
---|
| 224 | if (ch >= 0xc0 && ch < 0xf0) { |
---|
| 225 | int ch1 = getc(cavern_out); |
---|
| 226 | if ((ch1 & 0xc0) != 0x80) { |
---|
| 227 | left = ch1; |
---|
| 228 | } else if (ch < 0xe0) { |
---|
| 229 | /* 2 byte sequence */ |
---|
| 230 | ch = ((ch & 0x1f) << 6) | (ch1 & 0x3f); |
---|
| 231 | } else { |
---|
| 232 | /* 3 byte sequence */ |
---|
| 233 | int ch2 = getc(cavern_out); |
---|
| 234 | if ((ch2 & 0xc0) != 0x80) { |
---|
| 235 | ungetc(ch2, cavern_out); |
---|
| 236 | left = ch1; |
---|
| 237 | } else { |
---|
| 238 | ch = ((ch & 0x1f) << 12) | ((ch1 & 0x3f) << 6) | (ch2 & 0x3f); |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | switch (ch) { |
---|
| 244 | case '\r': |
---|
| 245 | // Ignore. |
---|
| 246 | break; |
---|
| 247 | case '\n': { |
---|
| 248 | if (cur.empty()) continue; |
---|
[a07ee70] | 249 | #ifndef __WXMSW__ |
---|
[6bec10c] | 250 | size_t colon = cur.find(':'); |
---|
[a07ee70] | 251 | #else |
---|
[6bec10c] | 252 | // If the path is "C:\path\to\file.svx" then don't split at the |
---|
| 253 | // : after the drive letter! FIXME: better to look for ": "? |
---|
[a07ee70] | 254 | size_t colon = cur.find(':', 2); |
---|
[6bec10c] | 255 | #endif |
---|
[a07ee70] | 256 | if (colon != wxString::npos && colon < cur.size() - 1) { |
---|
| 257 | ++colon; |
---|
| 258 | size_t i = colon; |
---|
| 259 | while (i < cur.size() - 1 && |
---|
| 260 | cur[i] >= wxT('0') && cur[i] <= wxT('9')) { |
---|
| 261 | ++i; |
---|
| 262 | } |
---|
| 263 | if (i > colon && cur[i] == wxT(':') ) { |
---|
| 264 | colon = i; |
---|
[5627cbb] | 265 | wxString tag = wxT("<a href=\""); |
---|
[a07ee70] | 266 | tag.append(cur, 0, colon); |
---|
| 267 | while (cur[++i] == wxT(' ')) { } |
---|
[5627cbb] | 268 | tag += wxT("\" target=\""); |
---|
[a07ee70] | 269 | tag.append(cur, i, wxString::npos); |
---|
[5627cbb] | 270 | tag += wxT("\">"); |
---|
[6bec10c] | 271 | cur.insert(0, tag); |
---|
[a07ee70] | 272 | cur.insert(colon + tag.size(), wxT("</a>")); |
---|
[5627cbb] | 273 | ++link_count; |
---|
[6bec10c] | 274 | } |
---|
| 275 | } |
---|
[93ff5cc] | 276 | |
---|
| 277 | // Save the scrollbar positions. |
---|
| 278 | int scroll_x = 0, scroll_y = 0; |
---|
| 279 | GetViewStart(&scroll_x, &scroll_y); |
---|
| 280 | |
---|
[5627cbb] | 281 | cur += wxT("<br>\n"); |
---|
[6bec10c] | 282 | AppendToPage(cur); |
---|
[93ff5cc] | 283 | |
---|
[5627cbb] | 284 | if (!link_count) { |
---|
| 285 | // Auto-scroll the window until we've reported a warning or |
---|
| 286 | // error. |
---|
| 287 | int x, y; |
---|
| 288 | GetVirtualSize(&x, &y); |
---|
| 289 | int xs, ys; |
---|
| 290 | GetClientSize(&xs, &ys); |
---|
| 291 | y -= ys; |
---|
| 292 | int xu, yu; |
---|
| 293 | GetScrollPixelsPerUnit(&xu, &yu); |
---|
[a75f5a1] | 294 | Scroll(scroll_x, y / yu); |
---|
| 295 | } else { |
---|
| 296 | // Restore the scrollbar positions. |
---|
| 297 | Scroll(scroll_x, scroll_y); |
---|
[5627cbb] | 298 | } |
---|
[a75f5a1] | 299 | |
---|
[6bec10c] | 300 | cur.clear(); |
---|
[a75f5a1] | 301 | Update(); |
---|
[6bec10c] | 302 | break; |
---|
| 303 | } |
---|
| 304 | case '<': |
---|
[5627cbb] | 305 | cur += wxT("<"); |
---|
[6bec10c] | 306 | break; |
---|
| 307 | case '>': |
---|
[5627cbb] | 308 | cur += wxT(">"); |
---|
[6bec10c] | 309 | break; |
---|
| 310 | case '&': |
---|
[5627cbb] | 311 | cur += wxT("&"); |
---|
[6bec10c] | 312 | break; |
---|
[77b8654] | 313 | case '"': |
---|
| 314 | cur += wxT(""); |
---|
| 315 | continue; |
---|
[6bec10c] | 316 | default: |
---|
| 317 | if (ch >= 128) { |
---|
[5627cbb] | 318 | cur += wxString::Format(wxT("&#%u;"), ch); |
---|
[6bec10c] | 319 | } else { |
---|
| 320 | cur += (char)ch; |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | } |
---|
[fb5887c] | 324 | |
---|
[93ff5cc] | 325 | int retval = pclose(cavern_out); |
---|
| 326 | if (retval) { |
---|
[fb5887c] | 327 | AppendToPage(wxT("<avenbutton default id=1234 name=\"Rerun\">")); |
---|
[93ff5cc] | 328 | if (retval == -1) { |
---|
[5627cbb] | 329 | wxString m = wxT("Problem running cavern: "); |
---|
| 330 | m += wxString(strerror(errno), wxConvUTF8); |
---|
[93ff5cc] | 331 | wxGetApp().ReportError(m); |
---|
[5627cbb] | 332 | return -2; |
---|
[93ff5cc] | 333 | } |
---|
[5627cbb] | 334 | return -1; |
---|
[6bec10c] | 335 | } |
---|
[fb5887c] | 336 | if (link_count) { |
---|
| 337 | AppendToPage(wxT("<avenbutton id=1234 name=\"Rerun\">")); |
---|
| 338 | AppendToPage(wxString::Format(wxT("<avenbutton default id=%d>"), (int)wxID_OK)); |
---|
| 339 | Update(); |
---|
| 340 | } |
---|
[5627cbb] | 341 | return link_count; |
---|
[6bec10c] | 342 | } |
---|
[fb5887c] | 343 | |
---|
| 344 | void |
---|
| 345 | CavernLogWindow::OnRerun(wxCommandEvent & e) |
---|
| 346 | { |
---|
| 347 | SetPage(wxString()); |
---|
| 348 | if (process(filename) == 0) { |
---|
| 349 | OnOK(e); |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | void |
---|
| 354 | CavernLogWindow::OnOK(wxCommandEvent &) |
---|
| 355 | { |
---|
| 356 | mainfrm->InitialiseAfterLoad(filename); |
---|
| 357 | } |
---|