static gboolean mcview_find (mcview_search_status_msg_t * ssm, off_t search_start, off_t search_end, gsize * len) { WView *view = ssm->view; view->search_numNeedSkipChar = 0; search_cb_char_curr_index = -1; if (mcview_search_options.backwards) { search_end = mcview_get_filesize (view); while (search_start >= 0) { gboolean ok; view->search_nroff_seq->index = search_start; mcview_nroff_seq_info (view->search_nroff_seq); if (search_end > search_start + (off_t) view->search->original_len && mc_search_is_fixed_search_str (view->search)) search_end = search_start + view->search->original_len; ok = mc_search_run (view->search, (void *) ssm, search_start, search_end, len); if (ok && view->search->normal_offset == search_start) { if (view->text_nroff_mode) view->search->normal_offset++; return TRUE; } /* We abort the search in case of a pattern error, or if the user aborts the search. In other words: in all cases except "string not found". */ if (!ok && view->search->error != MC_SEARCH_E_NOTFOUND) return FALSE; search_start--; } mc_search_set_error (view->search, MC_SEARCH_E_NOTFOUND, "%s", _(STR_E_NOTFOUND)); return FALSE; } view->search_nroff_seq->index = search_start; mcview_nroff_seq_info (view->search_nroff_seq); return mc_search_run (view->search, (void *) ssm, search_start, search_end, len); }
static int mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry_t * fe) { (void) fhl; if (mc_filter->search_condition == NULL) return -1; if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL)) return mc_filter->color_pair_index; return -1; }
static gboolean mcview_find (mcview_t * view, gsize search_start, gsize * len) { gsize search_end; view->search_numNeedSkipChar = 0; search_cb_char_curr_index = -1; if (mcview_search_options.backwards) { search_end = mcview_get_filesize (view); while ((int) search_start >= 0) { view->search_nroff_seq->index = search_start; mcview_nroff_seq_info (view->search_nroff_seq); if (search_end > search_start + view->search->original_len && mc_search_is_fixed_search_str (view->search)) search_end = search_start + view->search->original_len; if (mc_search_run (view->search, (void *) view, search_start, search_end, len) && view->search->normal_offset == (off_t) search_start) { if (view->text_nroff_mode) view->search->normal_offset++; return TRUE; } search_start--; } view->search->error_str = g_strdup (_("Search string not found")); return FALSE; } view->search_nroff_seq->index = search_start; mcview_nroff_seq_info (view->search_nroff_seq); return mc_search_run (view->search, (void *) view, search_start, mcview_get_filesize (view), len); }
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; }
static void sftpfs_fill_config_entity_from_string (sftpfs_ssh_config_entity_t * config_entity, char *buffer) { int i; for (i = 0; config_variables[i].pattern != NULL; i++) { if (mc_search_run (config_variables[i].pattern_regexp, buffer, 0, strlen (buffer), NULL)) { int value_offset; char *value; int *pointer_int; char **pointer_str; gboolean *pointer_bool; /* Calculate start of value in string */ value_offset = mc_search_getstart_result_by_num (config_variables[i].pattern_regexp, 1); value = &buffer[value_offset]; switch (config_variables[i].type) { case STRING: pointer_str = POINTER_TO_STRUCTURE_MEMBER (char **); *pointer_str = g_strdup (value); break; case FILENAME: pointer_str = POINTER_TO_STRUCTURE_MEMBER (char **); *pointer_str = sftpfs_correct_file_name (value); break; case INTEGER: pointer_int = POINTER_TO_STRUCTURE_MEMBER (int *); *pointer_int = atoi (value); break; case BOOLEAN: pointer_bool = POINTER_TO_STRUCTURE_MEMBER (gboolean *); *pointer_bool = strcasecmp (value, "True") == 0; break; default: continue; } return; } }
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; }
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; }