void mcview_done (mcview_t * view) { /* Save current file position */ if (mcview_remember_file_position && view->filename_vpath != NULL) { save_file_position (view->filename_vpath, -1, 0, view->hex_mode ? view->hex_cursor : view->dpy_start, view->saved_bookmarks); view->saved_bookmarks = NULL; } /* Write back the global viewer mode */ mcview_default_hex_mode = view->hex_mode; mcview_default_nroff_flag = view->text_nroff_mode; mcview_default_magic_flag = view->magic_mode; mcview_global_wrap_mode = view->text_wrap_mode; /* Free memory used by the viewer */ /* view->widget needs no destructor */ vfs_path_free (view->filename_vpath); view->filename_vpath = NULL; vfs_path_free (view->workdir_vpath); view->workdir_vpath = NULL; MC_PTR_FREE (view->command); mcview_close_datasource (view); /* the growing buffer is freed with the datasource */ coord_cache_free (view->coord_cache), view->coord_cache = NULL; if (view->converter == INVALID_CONV) view->converter = str_cnv_from_term; if (view->converter != str_cnv_from_term) { str_close_conv (view->converter); view->converter = str_cnv_from_term; } mc_search_free (view->search); view->search = NULL; g_free (view->last_search_string); view->last_search_string = NULL; mcview_nroff_seq_free (&view->search_nroff_seq); mcview_hexedit_free_change_list (view); if (mc_global.mc_run_mode == MC_RUN_VIEWER && view->dir != NULL) { /* mcviewer is the owner of file list */ dir_list_clean (view->dir); g_free (view->dir->list); g_free (view->dir_idx); g_free (view->dir); } view->dir = NULL; }
static void mc_fhl_filter_free (void *data) { mc_fhl_filter_t *filter = (mc_fhl_filter_t *) data; g_free (filter->fgcolor); g_free (filter->bgcolor); mc_search_free (filter->search_condition); g_free (filter); }
void file_op_context_destroy (FileOpContext * ctx) { g_return_if_fail (ctx != NULL); file_op_context_destroy_ui (ctx); mc_search_free (ctx->search_handle); /** \todo FIXME: do we need to free ctx->dest_mask? */ g_free (ctx); }
gboolean mc_search (const gchar * pattern, const gchar * str, mc_search_type_t type) { gboolean ret; mc_search_t *search = mc_search_new (pattern, -1); if (search == NULL) return FALSE; search->search_type = type; search->is_case_sensitive = TRUE; if (type == MC_SEARCH_T_GLOB) search->is_entire_line = TRUE; ret = mc_search_run (search, str, 0, strlen (str), NULL); mc_search_free (search); return ret; }
gboolean mcview_dialog_search (WView * view) { char *exp = NULL; int qd_result; size_t num_of_types = 0; gchar **list_of_types; list_of_types = mc_search_get_types_strings_array (&num_of_types); { quick_widget_t quick_widgets[] = { /* *INDENT-OFF* */ QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above, INPUT_LAST_TEXT, MC_HISTORY_SHARED_SEARCH, &exp, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE), QUICK_SEPARATOR (TRUE), QUICK_START_COLUMNS, QUICK_RADIO (num_of_types, (const char **) list_of_types, (int *) &mcview_search_options.type, NULL), QUICK_NEXT_COLUMN, QUICK_CHECKBOX (N_("Cas&e sensitive"), &mcview_search_options.case_sens, NULL), QUICK_CHECKBOX (N_("&Backwards"), &mcview_search_options.backwards, NULL), QUICK_CHECKBOX (N_("&Whole words"), &mcview_search_options.whole_words, NULL), #ifdef HAVE_CHARSET QUICK_CHECKBOX (N_("&All charsets"), &mcview_search_options.all_codepages, NULL), #endif QUICK_STOP_COLUMNS, QUICK_BUTTONS_OK_CANCEL, QUICK_END /* *INDENT-ON* */ }; quick_dialog_t qdlg = { -1, -1, 58, N_("Search"), "[Input Line Keys]", quick_widgets, NULL, NULL }; qd_result = quick_dialog (&qdlg); } g_strfreev (list_of_types); if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0')) { g_free (exp); return FALSE; } #ifdef HAVE_CHARSET { GString *tmp; tmp = str_convert_to_input (exp); g_free (exp); exp = g_string_free (tmp, FALSE); } #endif g_free (view->last_search_string); view->last_search_string = exp; mcview_nroff_seq_free (&view->search_nroff_seq); mc_search_free (view->search); #ifdef HAVE_CHARSET view->search = mc_search_new (view->last_search_string, cp_source); #else view->search = mc_search_new (view->last_search_string, NULL); #endif view->search_nroff_seq = mcview_nroff_seq_new (view); if (view->search != NULL) { view->search->search_type = mcview_search_options.type; #ifdef HAVE_CHARSET view->search->is_all_charsets = mcview_search_options.all_codepages; #endif view->search->is_case_sensitive = mcview_search_options.case_sens; view->search->whole_words = mcview_search_options.whole_words; view->search->search_fn = mcview_search_cmd_callback; view->search->update_fn = mcview_search_update_cmd_callback; } return (view->search != NULL); }
gboolean editcmd_dialog_search_show (WEdit * edit) { char *search_text; size_t num_of_types; gchar **list_of_types; int dialog_result; list_of_types = mc_search_get_types_strings_array (&num_of_types); { quick_widget_t quick_widgets[] = { /* *INDENT-OFF* */ QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above, INPUT_LAST_TEXT, MC_HISTORY_SHARED_SEARCH, &search_text, NULL, FALSE, FALSE, INPUT_COMPLETE_NONE), QUICK_SEPARATOR (TRUE), QUICK_START_COLUMNS, QUICK_RADIO (num_of_types, (const char **) list_of_types, (int *) &edit_search_options.type, NULL), QUICK_NEXT_COLUMN, QUICK_CHECKBOX (N_("Cas&e sensitive"), &edit_search_options.case_sens, NULL), QUICK_CHECKBOX (N_("&Backwards"), &edit_search_options.backwards, NULL), QUICK_CHECKBOX (N_("In se&lection"), &edit_search_options.only_in_selection, NULL), QUICK_CHECKBOX (N_("&Whole words"), &edit_search_options.whole_words, NULL), #ifdef HAVE_CHARSET QUICK_CHECKBOX (N_("&All charsets"), &edit_search_options.all_codepages, NULL), #endif QUICK_STOP_COLUMNS, QUICK_START_BUTTONS (TRUE, TRUE), QUICK_BUTTON (N_("&OK"), B_ENTER, NULL, NULL), QUICK_BUTTON (N_("&Find all"), B_USER, NULL, NULL), QUICK_BUTTON (N_("&Cancel"), B_CANCEL, NULL, NULL), QUICK_END /* *INDENT-ON* */ }; quick_dialog_t qdlg = { -1, -1, 58, N_("Search"), "[Input Line Keys]", quick_widgets, NULL, NULL }; dialog_result = quick_dialog (&qdlg); } g_strfreev (list_of_types); if ((dialog_result == B_CANCEL) || (search_text == NULL) || (search_text[0] == '\0')) { g_free (search_text); return FALSE; } if (dialog_result == B_USER) search_create_bookmark = TRUE; #ifdef HAVE_CHARSET { GString *tmp; tmp = str_convert_to_input (search_text); if (tmp != NULL) { g_free (search_text); search_text = g_string_free (tmp, FALSE); } } #endif g_free (edit->last_search_string); edit->last_search_string = search_text; mc_search_free (edit->search); #ifdef HAVE_CHARSET edit->search = mc_search_new (edit->last_search_string, cp_source); #else edit->search = mc_search_new (edit->last_search_string, NULL); #endif if (edit->search != NULL) { edit->search->search_type = edit_search_options.type; #ifdef HAVE_CHARSET edit->search->is_all_charsets = edit_search_options.all_codepages; #endif edit->search->is_case_sensitive = edit_search_options.case_sens; edit->search->whole_words = edit_search_options.whole_words; edit->search->search_fn = edit_search_cmd_callback; edit->search->update_fn = edit_search_update_callback; } return (edit->search != NULL); }
int regex_command_for (void *target, const vfs_path_t * filename_vpath, const char *action, vfs_path_t ** script_vpath) { char *p, *q, *r, c; size_t file_len; gboolean found = FALSE; gboolean error_flag = FALSE; int ret = 0; struct stat mystat; int view_at_line_number; char *include_target; int include_target_len; int have_type = 0; /* Flag used by regex_check_type() */ if (filename_vpath == NULL) return 0; if (script_vpath != NULL) *script_vpath = NULL; /* Check for the special View:%d parameter */ if (strncmp (action, "View:", 5) == 0) { view_at_line_number = atoi (action + 5); action = "View"; } else { view_at_line_number = 0; } if (data == NULL) { char *extension_file; gboolean mc_user_ext = TRUE; gboolean home_error = FALSE; extension_file = mc_config_get_full_path (MC_FILEBIND_FILE); if (!exist_file (extension_file)) { g_free (extension_file); check_stock_mc_ext: extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL); if (!exist_file (extension_file)) { g_free (extension_file); extension_file = mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL); } mc_user_ext = FALSE; } g_file_get_contents (extension_file, &data, NULL, NULL); g_free (extension_file); if (data == NULL) return 0; if (strstr (data, "default/") == NULL) { if (strstr (data, "regex/") == NULL && strstr (data, "shell/") == NULL && strstr (data, "type/") == NULL) { g_free (data); data = NULL; if (!mc_user_ext) { char *title; title = g_strdup_printf (_(" %s%s file error"), mc_global.sysconfig_dir, MC_LIB_EXT); message (D_ERROR, title, _("The format of the %smc.ext " "file has changed with version 3.0. It seems that " "the installation failed. Please fetch a fresh " "copy from the Midnight Commander package."), mc_global.sysconfig_dir); g_free (title); return 0; } home_error = TRUE; goto check_stock_mc_ext; } } if (home_error) { char *filebind_filename; char *title; filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE); title = g_strdup_printf (_("%s file error"), filebind_filename); message (D_ERROR, title, _("The format of the %s file has " "changed with version 3.0. You may either want to copy " "it from %smc.ext or use that file as an example of how to write it."), filebind_filename, mc_global.sysconfig_dir); g_free (filebind_filename); g_free (title); } } mc_stat (filename_vpath, &mystat); include_target = NULL; include_target_len = 0; file_len = vfs_path_len (filename_vpath); for (p = data; *p != '\0'; p++) { for (q = p; *q == ' ' || *q == '\t'; q++) ; if (*q == '\n' || *q == '\0') p = q; /* empty line */ if (*p == '#') /* comment */ while (*p != '\0' && *p != '\n') p++; if (*p == '\n') continue; if (*p == '\0') break; if (p == q) { /* i.e. starts in the first column, should be * keyword/descNL */ gboolean case_insense; found = FALSE; q = strchr (p, '\n'); if (q == NULL) q = strchr (p, '\0'); c = *q; *q = '\0'; if (include_target) { if ((strncmp (p, "include/", 8) == 0) && (strncmp (p + 8, include_target, include_target_len) == 0)) found = TRUE; } else if (strncmp (p, "regex/", 6) == 0) { mc_search_t *search; p += 6; case_insense = (strncmp (p, "i/", 2) == 0); if (case_insense) p += 2; search = mc_search_new (p, -1); if (search != NULL) { search->search_type = MC_SEARCH_T_REGEX; search->is_case_sensitive = !case_insense; found = mc_search_run (search, vfs_path_as_str (filename_vpath), 0, file_len, NULL); mc_search_free (search); } } else if (strncmp (p, "directory/", 10) == 0) { if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, vfs_path_as_str (filename_vpath), MC_SEARCH_T_REGEX)) found = TRUE; } else if (strncmp (p, "shell/", 6) == 0) { int (*cmp_func) (const char *s1, const char *s2, size_t n) = strncmp; p += 6; case_insense = (strncmp (p, "i/", 2) == 0); if (case_insense) { p += 2; cmp_func = strncasecmp; } if (*p == '.' && file_len >= (size_t) (q - p)) { if (cmp_func (p, vfs_path_as_str (filename_vpath) + file_len - (q - p), q - p) == 0) found = TRUE; } else { if ((size_t) (q - p) == file_len && cmp_func (p, vfs_path_as_str (filename_vpath), q - p) == 0) found = TRUE; } } else if (strncmp (p, "type/", 5) == 0) { GError *error = NULL; p += 5; case_insense = (strncmp (p, "i/", 2) == 0); if (case_insense) p += 2; found = regex_check_type (filename_vpath, p, &have_type, case_insense, &error); if (error != NULL) { g_error_free (error); error_flag = TRUE; /* leave it if file cannot be opened */ } } else if (strncmp (p, "default/", 8) == 0) found = TRUE; *q = c; p = q; if (*p == '\0') break; } else { /* List of actions */ p = q; q = strchr (p, '\n'); if (q == NULL) q = strchr (p, '\0'); if (found && !error_flag) { r = strchr (p, '='); if (r != NULL) { c = *r; *r = '\0'; if (strcmp (p, "Include") == 0) { char *t; include_target = p + 8; t = strchr (include_target, '\n'); if (t != NULL) *t = '\0'; include_target_len = strlen (include_target); if (t != NULL) *t = '\n'; *r = c; p = q; found = FALSE; if (*p == '\0') break; continue; } if (strcmp (action, p) != 0) *r = c; else { *r = c; for (p = r + 1; *p == ' ' || *p == '\t'; p++) ; /* Empty commands just stop searching * through, they don't do anything */ if (p < q) { vfs_path_t *sv; sv = exec_extension (target, filename_vpath, r + 1, view_at_line_number); if (script_vpath != NULL) *script_vpath = sv; else exec_cleanup_script (sv); ret = 1; } break; } } } p = q; if (*p == '\0') break; } } if (error_flag) ret = -1; return ret; }
static gboolean regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type, gboolean case_insense, GError ** error) { gboolean found = FALSE; /* Following variables are valid if *have_type is 1 */ static char content_string[2048]; #ifdef HAVE_CHARSET static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */ #endif static size_t content_shift = 0; static int got_data = 0; if (!use_file_to_check_type) return FALSE; if (*have_type == 0) { vfs_path_t *localfile_vpath; const char *realname; /* name used with "file" */ #ifdef HAVE_CHARSET int got_encoding_data; #endif /* HAVE_CHARSET */ /* Don't repeate even unsuccessful checks */ *have_type = 1; localfile_vpath = mc_getlocalcopy (filename_vpath); if (localfile_vpath == NULL) { g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Cannot fetch a local copy of %s"), vfs_path_as_str (filename_vpath))); return FALSE; } realname = vfs_path_get_last_path_str (localfile_vpath); #ifdef HAVE_CHARSET got_encoding_data = is_autodetect_codeset_enabled ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0; if (got_encoding_data > 0) { char *pp; int cp_id; pp = strchr (encoding_id, '\n'); if (pp != NULL) *pp = '\0'; cp_id = get_codepage_index (encoding_id); if (cp_id == -1) cp_id = default_source_codepage; do_set_codepage (cp_id); } #endif /* HAVE_CHARSET */ mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE); got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string)); if (got_data > 0) { char *pp; size_t real_len; pp = strchr (content_string, '\n'); if (pp != NULL) *pp = '\0'; real_len = strlen (realname); if (strncmp (content_string, realname, real_len) == 0) { /* Skip "realname: " */ content_shift = real_len; if (content_string[content_shift] == ':') { /* Solaris' file prints tab(s) after ':' */ for (content_shift++; content_string[content_shift] == ' ' || content_string[content_shift] == '\t'; content_shift++) ; } } } else { /* No data */ content_string[0] = '\0'; } vfs_path_free (localfile_vpath); } if (got_data == -1) { g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Pipe failed"))); return FALSE; } if (content_string[0] != '\0') { mc_search_t *search; search = mc_search_new (ptr, -1); if (search != NULL) { search->search_type = MC_SEARCH_T_REGEX; search->is_case_sensitive = !case_insense; found = mc_search_run (search, content_string + content_shift, 0, -1, NULL); mc_search_free (search); } else { g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Regular expression error"))); } } return found; }
gboolean mcview_dialog_search (mcview_t * view) { int SEARCH_DLG_MIN_HEIGHT = 12; int SEARCH_DLG_HEIGHT_SUPPLY = 3; int SEARCH_DLG_WIDTH = 58; char *exp = NULL; int qd_result; size_t num_of_types; gchar **list_of_types = mc_search_get_types_strings_array (&num_of_types); int SEARCH_DLG_HEIGHT = SEARCH_DLG_MIN_HEIGHT + num_of_types - SEARCH_DLG_HEIGHT_SUPPLY; QuickWidget quick_widgets[] = { QUICK_BUTTON (6, 10, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, N_("&Cancel"), B_CANCEL, NULL), QUICK_BUTTON (2, 10, SEARCH_DLG_HEIGHT - 3, SEARCH_DLG_HEIGHT, N_("&OK"), B_ENTER, NULL), #ifdef HAVE_CHARSET QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 8, SEARCH_DLG_HEIGHT, N_("&All charsets"), &mcview_search_options.all_codepages), #endif QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 7, SEARCH_DLG_HEIGHT, N_("&Whole words"), &mcview_search_options.whole_words), QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 6, SEARCH_DLG_HEIGHT, N_("&Backwards"), &mcview_search_options.backwards), QUICK_CHECKBOX (SEARCH_DLG_WIDTH / 2 + 3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, N_("Cas&e sensitive"), &mcview_search_options.case_sens), QUICK_RADIO (3, SEARCH_DLG_WIDTH, 5, SEARCH_DLG_HEIGHT, num_of_types, (const char **) list_of_types, (int *) &mcview_search_options.type), QUICK_INPUT (3, SEARCH_DLG_WIDTH, 3, SEARCH_DLG_HEIGHT, INPUT_LAST_TEXT, SEARCH_DLG_WIDTH - 6, 0, MC_HISTORY_SHARED_SEARCH, &exp), QUICK_LABEL (3, SEARCH_DLG_WIDTH, 2, SEARCH_DLG_HEIGHT, N_("Enter search string:")), QUICK_END }; QuickDialog Quick_input = { SEARCH_DLG_WIDTH, SEARCH_DLG_HEIGHT, -1, -1, N_("Search"), "[Input Line Keys]", quick_widgets, NULL, NULL, FALSE }; qd_result = quick_dialog (&Quick_input); g_strfreev (list_of_types); if ((qd_result == B_CANCEL) || (exp == NULL) || (exp[0] == '\0')) { g_free (exp); return FALSE; } #ifdef HAVE_CHARSET { GString *tmp = str_convert_to_input (exp); if (tmp) { g_free (exp); exp = g_string_free (tmp, FALSE); } } #endif g_free (view->last_search_string); view->last_search_string = exp; mcview_nroff_seq_free (&view->search_nroff_seq); mc_search_free (view->search); view->search = mc_search_new (view->last_search_string, -1); view->search_nroff_seq = mcview_nroff_seq_new (view); if (view->search != NULL) { view->search->search_type = mcview_search_options.type; view->search->is_all_charsets = mcview_search_options.all_codepages; view->search->is_case_sensitive = mcview_search_options.case_sens; view->search->whole_words = mcview_search_options.whole_words; view->search->search_fn = mcview_search_cmd_callback; view->search->update_fn = mcview_search_update_cmd_callback; } return (view->search != NULL); }
static void select_unselect_cmd (const char *title, const char *history_name, gboolean do_select) { /* dialog sizes */ const int DX = 50; const int DY = 7; int files_only = (select_flags & SELECT_FILES_ONLY) != 0; int case_sens = (select_flags & SELECT_MATCH_CASE) != 0; int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0; char *reg_exp; mc_search_t *search; int i; QuickWidget quick_widgets[] = { QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns), QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens), QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only), QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, ®_exp), QUICK_END }; QuickDialog quick_dlg = { DX, DY, -1, -1, title, "[Select/Unselect Files]", quick_widgets, NULL, FALSE }; if (quick_dialog (&quick_dlg) == B_CANCEL) return; if (!reg_exp) return; if (!*reg_exp) { g_free (reg_exp); return; } search = mc_search_new (reg_exp, -1); search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX; search->is_entire_line = TRUE; search->is_case_sensitive = case_sens != 0; for (i = 0; i < current_panel->count; i++) { if (strcmp (current_panel->dir.list[i].fname, "..") == 0) continue; if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0) continue; if (mc_search_run (search, current_panel->dir.list[i].fname, 0, current_panel->dir.list[i].fnamelen, NULL)) do_file_mark (current_panel, i, do_select); } mc_search_free (search); g_free (reg_exp); /* result flags */ select_flags = 0; if (case_sens != 0) select_flags |= SELECT_MATCH_CASE; if (files_only != 0) select_flags |= SELECT_FILES_ONLY; if (shell_patterns != 0) select_flags |= SELECT_SHELL_PATTERNS; }