std::string format_paragraph(const std::string &text_to_wrap, int indent_column, const std::string &indent_first_line, std::string indent_following_lines, int wrap_column, const char *break_chars) { return to_utf8(format_paragraph(to_wide(text_to_wrap), indent_column, to_wide(indent_first_line), to_wide(indent_following_lines), wrap_column, to_wide(break_chars))); }
bool click_link(const std::string& link, std::string& error_msg) { std::wstring wlink = to_wide(link); HINSTANCE h = ShellExecute( nullptr, // HWND hwnd L"open", // LPCTSTR lpOperation wlink.c_str(), // LPCTSTR lpFile NULL, // LPCTSTR lpParameters NULL, // LPCTSTR lpDirectory SW_SHOWNORMAL // INT nShowCmd ); const int h_int = (int) h; if (h_int < 32) { switch (h_int) { case 0: error_msg = "The operating system is out of memory or resources."; break; case ERROR_FILE_NOT_FOUND: error_msg = "The specified file was not found."; break; case ERROR_PATH_NOT_FOUND: error_msg = "The specified path was not found."; break; case ERROR_BAD_FORMAT: error_msg = "The.exe file is invalid(non - Win32.exe or error in.exe image)."; break; case SE_ERR_ACCESSDENIED: error_msg = "The operating system denied access to the specified file."; break; case SE_ERR_ASSOCINCOMPLETE: error_msg = "The file name association is incomplete or invalid."; break; case SE_ERR_DDEBUSY: error_msg = "The DDE transaction could not be completed because other DDE transactions were being processed."; break; case SE_ERR_DDEFAIL: error_msg = "The DDE transaction failed."; break; case SE_ERR_DDETIMEOUT: error_msg = "The DDE transaction could not be completed because the request timed out."; break; case SE_ERR_DLLNOTFOUND: error_msg = "The specified DLL was not found."; break; //case SE_ERR_FNF: error_msg = "The specified file was not found."; break; case SE_ERR_NOASSOC: error_msg = "There is no application associated with the given file name extension.This error will also be returned if you attempt to print a file that is not printable."; break; case SE_ERR_OOM: error_msg = "There was not enough memory to complete the operation."; break; //case SE_ERR_PNF: error_msg = "The specified path was not found."; break; case SE_ERR_SHARE: error_msg = "A sharing violation occurred."; break; default: error_msg = "unknown ShellExecute() error: " + std::to_string(h_int); break; } return false; } return true; }
size_t mm_stdio_c::_write(const void *buffer, size_t size) { HANDLE h_stdout = GetStdHandle(STD_OUTPUT_HANDLE); if (INVALID_HANDLE_VALUE == h_stdout) return 0; DWORD file_type = GetFileType(h_stdout); bool is_console = false; if ((FILE_TYPE_UNKNOWN != file_type) && ((file_type & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR)) { DWORD dummy; is_console = GetConsoleMode(h_stdout, &dummy); } if (is_console) { const std::wstring &w = to_wide(g_cc_stdio->utf8(std::string(static_cast<const char *>(buffer), size))); DWORD bytes_written = 0; WriteConsoleW(h_stdout, w.c_str(), w.length(), &bytes_written, NULL); return bytes_written; } size_t bytes_written = fwrite(buffer, 1, size, stdout); fflush(stdout); m_cached_size = -1; return bytes_written; }
int system(std::string const &command) { std::wstring wcommand = to_wide(command); auto mem = memory_c::clone(wcommand.c_str(), (wcommand.length() + 1) * sizeof(wchar_t)); STARTUPINFOW si; PROCESS_INFORMATION pi; memset(&si, 0, sizeof(STARTUPINFOW)); memset(&pi, 0, sizeof(PROCESS_INFORMATION)); auto result = ::CreateProcessW(NULL, // application name (use only cmd line) reinterpret_cast<wchar_t *>(mem->get_buffer()), // full command line NULL, // security attributes: defaults for both NULL, // the process and its main thread FALSE, // inherit handles if we use pipes CREATE_NO_WINDOW, // process creation flags NULL, // environment (use the same) NULL, // current directory (use the same) &si, // startup info (unused here) &pi // process info ); // Wait until child process exits. WaitForSingleObject(pi.hProcess, INFINITE); // Close process and thread handles. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return !result ? -1 : 0; }
static void test_max_count_is_handled_correctly(void) { char *const keys = format_str("%udd", INT_MAX); wchar_t *const keysw = to_wide(keys); assert_false(IS_KEYS_RET_CODE(execute_keys(keysw))); assert_int_equal(INT_MAX, last_command_count); free(keysw); free(keys); }
static void complete_next(const hist_t *hist, size_t len) { if(hist_is_empty(hist)) { return; } if(input_stat.history_search != HIST_SEARCH) { if(input_stat.cmd_pos <= 0) { restore_user_input(); return; } input_stat.cmd_pos--; } else { int pos = input_stat.cmd_pos; int len = input_stat.hist_search_len; while(--pos >= 0) { wchar_t *const buf = to_wide(hist->items[pos]); if(wcsncmp(input_stat.line, buf, len) == 0) { free(buf); break; } free(buf); } if(pos < 0) { restore_user_input(); return; } input_stat.cmd_pos = pos; } (void)replace_input_line(hist->items[input_stat.cmd_pos]); update_cmdline(); if(input_stat.cmd_pos > len - 1) { input_stat.cmd_pos = len - 1; } }
/* Returns number of character positions allocated by the string on the * screen. On issues will try to do the best, to make visible at least * something. */ static size_t get_width_on_screen(const char str[]) { size_t length = (size_t)-1; wchar_t *const wide = to_wide(str); if(wide != NULL) { length = vifm_wcswidth(wide, (size_t)-1); free(wide); } if(length == (size_t)-1) { length = utf8_nstrlen(str); } return length; }
void enter_prompt_mode(const wchar_t *prompt, const char *cmd, prompt_cb cb, complete_cmd_func complete) { wchar_t *buf; sub_mode_ptr = cb; sub_mode = PROMPT_SUBMODE; buf = to_wide(cmd); if(buf == NULL) return; prepare_cmdline_mode(prompt, buf, complete); free(buf); }
/* Returns number of character positions allocated by the string on the * screen. On issues will try to do the best, to make visible at least * something. */ static size_t get_width_on_screen(const char str[]) { size_t length = (size_t)-1; wchar_t *const wide = to_wide(str); if(wide != NULL) { length = wcswidth(wide, (size_t)-1); free(wide); } if(length == (size_t)-1) { length = get_normal_utf8_string_length(str); } return length; }
void enter_prompt_mode(const wchar_t prompt[], const char cmd[], prompt_cb cb, complete_cmd_func complete, int allow_ee) { wchar_t *buf; sub_mode_ptr = cb; sub_mode = PROMPT_SUBMODE; sub_mode_allows_ee = allow_ee; buf = to_wide(cmd); if(buf == NULL) return; prepare_cmdline_mode(prompt, buf, complete); free(buf); }
static void complete_with_shared(const char *server, const char *file) { NET_API_STATUS res; size_t len = strlen(file); do { PSHARE_INFO_502 buf_ptr; DWORD er = 0, tr = 0, resume = 0; wchar_t *wserver = to_wide(server + 2); if(wserver == NULL) { show_error_msg("Memory Error", "Unable to allocate enough memory"); return; } res = NetShareEnum(wserver, 502, (LPBYTE *)&buf_ptr, -1, &er, &tr, &resume); free(wserver); if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA) { PSHARE_INFO_502 p; DWORD i; p = buf_ptr; for(i = 1; i <= er; i++) { char buf[512]; WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)p->shi502_netname, -1, buf, sizeof(buf), NULL, NULL); strcat(buf, "/"); if(strnoscmp(buf, file, len) == 0) { char *const escaped = escape_filename(buf, 1); vle_compl_add_match(escaped); free(escaped); } p++; } NetApiBufferFree(buf_ptr); } } while(res == ERROR_MORE_DATA); }
static void complete_prev(const hist_t *hist, size_t len) { if(hist_is_empty(hist)) { return; } if(input_stat.history_search != HIST_SEARCH) { if(input_stat.cmd_pos == hist->pos) return; input_stat.cmd_pos++; } else { int pos = input_stat.cmd_pos; int len = input_stat.hist_search_len; while(++pos <= hist->pos) { wchar_t *buf; buf = to_wide(hist->items[pos]); if(wcsncmp(input_stat.line, buf, len) == 0) { free(buf); break; } free(buf); } if(pos > hist->pos) return; input_stat.cmd_pos = pos; } (void)replace_input_line(hist->items[input_stat.cmd_pos]); update_cmdline(); if(input_stat.cmd_pos > len - 1) { input_stat.cmd_pos = len - 1; } }
int get_wide(void) { static char mbs[MB_LEN_MAX + 1] = ""; static int mbi = 0; int c; wchar_t wc; if (mb_cur_max >= (int)sizeof (mbs)) { yyerror("max multibyte character size too big"); mbi = 0; return (T_NULL); } for (;;) { if ((mbi == mb_cur_max) || ((c = get_byte()) == EOF)) { /* * end of the byte sequence reached, but no * valid wide decoding. fatal error. */ mbi = 0; yyerror("not a valid character encoding"); return (T_NULL); } mbs[mbi++] = c; mbs[mbi] = 0; /* does it decode? */ if (to_wide(&wc, mbs) >= 0) { break; } } mbi = 0; if ((category != T_CHARMAP) && (category != T_WIDTH)) { if (check_charmap(wc) < 0) { yyerror("no symbolic name for character"); return (T_NULL); } } yylval.wc = wc; return (T_CHAR); }
/* Gets next dot completion from history of commands. Returns new string, caller * should free it. */ static wchar_t * next_dot_completion(void) { size_t len; char *last; wchar_t *wide; if(input_stat.dot_pos <= cfg.cmd_hist.pos) { last = get_last_argument(cfg.cmd_hist.items[input_stat.dot_pos++], &len); } else { last = ""; len = 0; } last = strdup(last); last[len] = '\0'; wide = to_wide(last); free(last); return wide; }
wchar_t * substitute_specs(const char cmd[]) { wchar_t *buf, *p; const size_t len = strlen(cmd) + 1; wchar_t *const cmdw = to_wide(cmd); wchar_t *s = cmdw; buf = malloc(len*sizeof(wchar_t)); if(cmdw == NULL || buf == NULL) { free(cmdw); free(buf); return NULL; } p = buf; while(*s != L'\0') { const key_pair_t *const pair = find_notation(s); if(pair == NULL) { *p++ = *s++; } else { wcscpy(p, pair->key); p += wcslen(p); s += pair->len; } } *p = L'\0'; assert(p + 1 - buf <= len && "Destination buffer was too small."); free(cmdw); return buf; }
int start_background_job(const char *cmd, int skip_errors) { job_t *job = NULL; #ifndef _WIN32 pid_t pid; int error_pipe[2]; char *command; command = cfg.fast_run ? fast_run_complete(cmd) : strdup(cmd); if(command == NULL) { return -1; } if(pipe(error_pipe) != 0) { show_error_msg("File pipe error", "Error creating pipe"); free(command); return -1; } if((pid = fork()) == -1) { free(command); return -1; } if(pid == 0) { extern char **environ; int nullfd; /* Redirect stderr to write end of pipe. */ if(dup2(error_pipe[1], STDERR_FILENO) == -1) { perror("dup2"); _Exit(EXIT_FAILURE); } close(STDIN_FILENO); close(STDOUT_FILENO); /* Close read end of pipe. */ close(error_pipe[0]); /* Attach stdout, stdin to /dev/null. */ nullfd = open("/dev/null", O_RDWR); if(nullfd != -1) { if(dup2(nullfd, STDIN_FILENO) == -1) { perror("dup2 for stdin"); _Exit(EXIT_FAILURE); } if(dup2(nullfd, STDOUT_FILENO) == -1) { perror("dup2 for stdout"); _Exit(EXIT_FAILURE); } } setpgid(0, 0); execve(get_execv_path(cfg.shell), make_execv_array(cfg.shell, command), environ); _Exit(127); } else { /* Close write end of pipe. */ close(error_pipe[1]); job = add_background_job(pid, command, error_pipe[0], BJT_COMMAND); if(job == NULL) { free(command); return -1; } } free(command); #else BOOL ret; STARTUPINFOW startup = {}; PROCESS_INFORMATION pinfo; char *command; char *sh_cmd; wchar_t *wide_cmd; command = cfg.fast_run ? fast_run_complete(cmd) : strdup(cmd); if(command == NULL) { return -1; } sh_cmd = win_make_sh_cmd(command); free(command); wide_cmd = to_wide(sh_cmd); ret = CreateProcessW(NULL, wide_cmd, NULL, NULL, 0, 0, NULL, NULL, &startup, &pinfo); free(wide_cmd); if(ret != 0) { CloseHandle(pinfo.hThread); job = add_background_job(pinfo.dwProcessId, sh_cmd, pinfo.hProcess, BJT_COMMAND); if(job == NULL) { free(sh_cmd); return -1; } } free(sh_cmd); if(ret == 0) { return 1; } #endif if(job != NULL) { job->skip_errors = skip_errors; } return 0; }
/* Runs command in a background and redirects its stdout and stderr streams to * file streams which are set. Returns (pid_t)0 or (pid_t)-1 on error. */ static pid_t background_and_capture_internal(char cmd[], int user_sh, FILE **out, FILE **err, int out_pipe[2], int err_pipe[2]) { wchar_t *args[4]; char cwd[PATH_MAX]; int code; wchar_t *final_wide_cmd; wchar_t *wide_sh = NULL; if(_dup2(out_pipe[1], _fileno(stdout)) != 0) return (pid_t)-1; if(_dup2(err_pipe[1], _fileno(stderr)) != 0) return (pid_t)-1; cwd[0] = '\0'; if(get_cwd(cwd, sizeof(cwd)) != NULL) { if(is_unc_path(cwd)) { (void)chdir(get_tmpdir()); } } final_wide_cmd = to_wide(cmd); wide_sh = to_wide(user_sh ? cfg.shell : "cmd"); if(!user_sh || curr_stats.shell_type == ST_CMD) { args[0] = wide_sh; args[1] = L"/C"; args[2] = final_wide_cmd; args[3] = NULL; } else { args[0] = wide_sh; args[1] = L"-c"; args[2] = final_wide_cmd; args[3] = NULL; } code = _wspawnvp(P_NOWAIT, args[0], (const wchar_t **)args); free(wide_sh); free(final_wide_cmd); if(is_unc_path(cwd)) { (void)chdir(cwd); } if(code == 0) { return (pid_t)-1; } if((*out = _fdopen(out_pipe[0], "r")) == NULL) return (pid_t)-1; if((*err = _fdopen(err_pipe[0], "r")) == NULL) { fclose(*out); return (pid_t)-1; } return 0; }
additional_parts_dialog::additional_parts_dialog(wxWindow *parent, mmg_file_t const &file) : wxDialog(parent, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) , m_file{&file} , m_primary_file_name{m_file->file_name} , m_files{ m_file->is_playlist ? m_file->playlist_files : m_file->other_files } { SetTitle(m_file->is_playlist ? Z("View files in playlist") : Z("Additional source file parts")); // Create controls m_lv_files = new wxListView(this, ID_ADDPARTS_LV_FILES, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxSUNKEN_BORDER); m_b_add = new wxButton(this, ID_ADDPARTS_B_ADD, Z("&Add")); m_b_remove = new wxButton(this, ID_ADDPARTS_B_REMOVE, Z("&Remove")); m_b_up = new wxButton(this, ID_ADDPARTS_B_UP, Z("&Up")); m_b_down = new wxButton(this, ID_ADDPARTS_B_DOWN, Z("&Down")); m_b_sort = new wxButton(this, ID_ADDPARTS_B_SORT, Z("&Sort")); m_b_close = new wxButton(this, ID_ADDPARTS_B_CLOSE, Z("&Close")); auto *st_title = new wxStaticText(this, wxID_ANY, m_file->is_playlist ? Z("View files in playlist") : Z("Edit additional source file parts")); auto *st_primary_file_name = new wxStaticText(this, wxID_ANY, Z("Primary file name:")); auto *st_directory = new wxStaticText(this, wxID_ANY, Z("Directory:")); wxString text; if (m_file->is_playlist) { text = wxString( Z("The following list shows the files that make up this playlist.") ) + wxT(" ") + Z("This list is for informational purposes only and cannot be changed."); } else { text = wxString( Z("The following parts are read after the primary file as if they were all part of one big file.") ) + wxT(" ") + Z("Typical use cases include reading VOBs from a DVD (e.g. VTS_01_1.VOB, VTS_01_2.VOB, VTS_01_3.VOB)."); } auto *st_additional_parts = new wxStaticText(this, wxID_ANY, wxString{ format_paragraph(to_wide(text), 0, L"", L"", 80) }.Strip()); auto *sl_title = new wxStaticLine(this); auto *tc_primary_file_name = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY); auto *tc_directory = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY); // Setup controls wxListItem column_item; column_item.SetMask(wxLIST_MASK_TEXT); column_item.SetText(Z("File name")); m_lv_files->InsertColumn(0, column_item); column_item.SetText(Z("Directory")); m_lv_files->InsertColumn(1, column_item); auto dummy = m_lv_files->InsertItem(0, wxT("some long file name dummy.mkv")); m_lv_files->SetItem(dummy, 1, wxT("and the path is even longer but hey such is life")); m_lv_files->SetColumnWidth(0, wxLIST_AUTOSIZE); m_lv_files->SetColumnWidth(1, wxLIST_AUTOSIZE); m_lv_files->DeleteItem(0); repopulate(); tc_primary_file_name->SetValue(m_primary_file_name.GetFullName()); tc_directory->SetValue(m_primary_file_name.GetPath()); enable_buttons(); // Create layout auto *siz_all = new wxBoxSizer(wxVERTICAL); siz_all->Add(st_title, 0, wxALL, 5); siz_all->Add(sl_title, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5); auto *siz_primary_file_name = new wxFlexGridSizer(2, 2, 5, 5); siz_primary_file_name->AddGrowableCol(1); siz_primary_file_name->Add(st_primary_file_name); siz_primary_file_name->Add(tc_primary_file_name, 0, wxGROW); siz_primary_file_name->Add(st_directory); siz_primary_file_name->Add(tc_directory, 0, wxGROW); siz_all->Add(siz_primary_file_name, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5); siz_all->Add(st_additional_parts, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5); auto *siz_buttons = new wxBoxSizer(wxVERTICAL); siz_buttons->Add(m_b_add, 0, wxBOTTOM, 5); siz_buttons->Add(m_b_remove, 0, wxBOTTOM, 15); siz_buttons->Add(m_b_up, 0, wxBOTTOM, 5); siz_buttons->Add(m_b_down, 0, wxBOTTOM, 15); siz_buttons->Add(m_b_sort, 0, wxBOTTOM, 0); siz_buttons->AddStretchSpacer(); siz_buttons->Add(m_b_close); auto *siz_controls = new wxBoxSizer(wxHORIZONTAL); siz_controls->Add(m_lv_files, 1, wxGROW); siz_controls->Add(siz_buttons, 0, wxGROW | wxLEFT, 5); siz_all->Add(siz_controls, 1, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5); SetSizerAndFit(siz_all); siz_all->SetSizeHints(this); SetSize(wxSize(700, 400)); // Run ShowModal(); }
inline std::wstring to_wide(wxFileName const &source) { return to_wide(source.GetFullPath()); }