gboolean edit_files (const GList * files) { static gboolean made_directory = FALSE; WDialog *edit_dlg; WMenuBar *menubar; const GList *file; gboolean ok = FALSE; if (!made_directory) { char *dir; dir = mc_build_filename (mc_config_get_cache_path (), EDIT_DIR, NULL); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); dir = mc_build_filename (mc_config_get_path (), EDIT_DIR, NULL); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); dir = mc_build_filename (mc_config_get_data_path (), EDIT_DIR, NULL); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); } /* Create a new dialog and add it widgets to it */ edit_dlg = dlg_create (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback, edit_dialog_event, "[Internal File Editor]", NULL, DLG_WANT_TAB); edit_dlg->get_shortcut = edit_get_shortcut; edit_dlg->get_title = edit_get_title; menubar = menubar_new (0, 0, COLS, NULL, TRUE); add_widget (edit_dlg, menubar); edit_init_menu (menubar); add_widget (edit_dlg, buttonbar_new (TRUE)); for (file = files; file != NULL; file = g_list_next (file)) { Widget *w = WIDGET (edit_dlg); mcedit_arg_t *f = (mcedit_arg_t *) file->data; gboolean f_ok; f_ok = edit_add_window (edit_dlg, w->y + 1, w->x, w->lines - 2, w->cols, f->file_vpath, f->line_number); /* at least one file has been opened succefully */ ok = ok || f_ok; } if (ok) dlg_run (edit_dlg); if (!ok || edit_dlg->state == DLG_CLOSED) dlg_destroy (edit_dlg); return ok; }
const char * setup_init (void) { if (profile_name == NULL) { char *profile; profile = mc_config_get_full_path (MC_CONFIG_FILE); if (!exist_file (profile)) { char *inifile; inifile = mc_build_filename (mc_global.sysconfig_dir, "mc.ini", NULL); if (exist_file (inifile)) { g_free (profile); profile = inifile; } else { g_free (inifile); inifile = mc_build_filename (mc_global.share_data_dir, "mc.ini", NULL); if (!exist_file (inifile)) g_free (inifile); else { g_free (profile); profile = inifile; } } } profile_name = profile; } return profile_name; }
static char * run_mc_build_filename (int iteration) { switch (iteration) { case 0: return mc_build_filename ("test", "path", (char *) NULL); case 1: return mc_build_filename ("/test", "path/", (char *) NULL); case 2: return mc_build_filename ("/test", "pa/th", (char *) NULL); case 3: return mc_build_filename ("/test", "#vfsprefix:", "path ", (char *) NULL); case 4: return mc_build_filename ("/test", "vfsprefix://", "path ", (char *) NULL); case 5: return mc_build_filename ("/test", "vfs/../prefix:///", "p\\///ath", (char *) NULL); case 6: return mc_build_filename ("/test", "path", "..", "/test", "path/", (char *) NULL); case 7: return mc_build_filename ("", "path", (char *) NULL); case 8: return mc_build_filename ("", "/path", (char *) NULL); case 9: return mc_build_filename ("path", "", (char *) NULL); case 10: return mc_build_filename ("/path", "", (char *) NULL); case 11: return mc_build_filename ("pa", "", "th", (char *) NULL); case 12: return mc_build_filename ("/pa", "", "/th", (char *) NULL); default: return 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 char * command_completion_function (const char *_text, int state, input_complete_t flags) { char *text; static const char *path_end; static gboolean isabsolute; static int phase; static size_t text_len; static const char *const *words; static char *path; static char *cur_path; static char *cur_word; static int init_state; static const char *const bash_reserved[] = { "if", "then", "else", "elif", "fi", "case", "esac", "for", "select", "while", "until", "do", "done", "in", "function", 0 }; static const char *const bash_builtins[] = { "alias", "bg", "bind", "break", "builtin", "cd", "command", "continue", "declare", "dirs", "echo", "enable", "eval", "exec", "exit", "export", "fc", "fg", "getopts", "hash", "help", "history", "jobs", "kill", "let", "local", "logout", "popd", "pushd", "pwd", "read", "readonly", "return", "set", "shift", "source", "suspend", "test", "times", "trap", "type", "typeset", "ulimit", "umask", "unalias", "unset", "wait", 0 }; char *p, *found; SHOW_C_CTX ("command_completion_function"); if (!(flags & INPUT_COMPLETE_COMMANDS)) return 0; text = strutils_shell_unescape (_text); flags &= ~INPUT_COMPLETE_SHELL_ESC; if (state == 0) { /* Initialize us a little bit */ isabsolute = strchr (text, PATH_SEP) != NULL; if (!isabsolute) { words = bash_reserved; phase = 0; text_len = strlen (text); if (path == NULL) { path = g_strdup (getenv ("PATH")); if (path != NULL) { p = path; path_end = strchr (p, '\0'); while ((p = strchr (p, PATH_ENV_SEP)) != NULL) { *p++ = '\0'; } } } } } if (isabsolute) { p = filename_completion_function (text, state, flags); if (p != NULL) { char *temp_p = p; p = strutils_shell_escape (p); g_free (temp_p); } g_free (text); return p; } found = NULL; switch (phase) { case 0: /* Reserved words */ while (*words) { if (strncmp (*words, text, text_len) == 0) { g_free (text); return g_strdup (*(words++)); } words++; } phase++; words = bash_builtins; case 1: /* Builtin commands */ while (*words) { if (strncmp (*words, text, text_len) == 0) { g_free (text); return g_strdup (*(words++)); } words++; } phase++; if (!path) break; cur_path = path; cur_word = NULL; case 2: /* And looking through the $PATH */ while (!found) { if (!cur_word) { char *expanded; if (cur_path >= path_end) break; expanded = tilde_expand (*cur_path ? cur_path : "."); cur_word = mc_build_filename (expanded, text, NULL); g_free (expanded); canonicalize_pathname (cur_word); cur_path = strchr (cur_path, 0) + 1; init_state = state; } found = filename_completion_function (cur_word, state - init_state, flags); if (!found) { g_free (cur_word); cur_word = NULL; } } } if (found == NULL) { g_free (path); path = NULL; } else { p = strrchr (found, PATH_SEP); if (p != NULL) { char *tmp = found; found = strutils_shell_escape (p + 1); g_free (tmp); } } g_free (text); return found; }
gboolean user_menu_cmd (struct WEdit * edit_widget, const char *menu_file, int selected_entry) { char *p; char *data, **entries; int max_cols, menu_lines, menu_limit; int col, i, accept_entry = 1; int selected, old_patterns; gboolean res = FALSE; gboolean interactive = TRUE; if (!vfs_current_is_local ()) { message (D_ERROR, MSG_ERROR, "%s", _("Cannot execute commands on non-local filesystems")); return FALSE; } if (menu_file != NULL) menu = g_strdup (menu_file); else menu = g_strdup (edit_widget ? EDIT_LOCAL_MENU : MC_LOCAL_MENU); if (!exist_file (menu) || !menu_file_own (menu)) { if (menu_file != NULL) { message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu, unix_error_string (errno)); MC_PTR_FREE (menu); return FALSE; } g_free (menu); if (edit_widget) menu = mc_config_get_full_path (EDIT_HOME_MENU); else menu = mc_config_get_full_path (MC_USERMENU_FILE); if (!exist_file (menu)) { g_free (menu); menu = mc_build_filename (mc_config_get_home_dir (), edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL); if (!exist_file (menu)) { g_free (menu); menu = mc_build_filename (mc_global.sysconfig_dir, edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL); if (!exist_file (menu)) { g_free (menu); menu = mc_build_filename (mc_global.share_data_dir, edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL); } } } } if (!g_file_get_contents (menu, &data, NULL, NULL)) { message (D_ERROR, MSG_ERROR, _("Cannot open file%s\n%s"), menu, unix_error_string (errno)); MC_PTR_FREE (menu); return FALSE; } max_cols = 0; selected = 0; menu_limit = 0; entries = 0; /* Parse the menu file */ old_patterns = easy_patterns; p = check_patterns (data); for (menu_lines = col = 0; *p; str_next_char (&p)) { if (menu_lines >= menu_limit) { char **new_entries; menu_limit += MAX_ENTRIES; new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit); if (new_entries == NULL) break; entries = new_entries; new_entries += menu_limit; while (--new_entries >= &entries[menu_lines]) *new_entries = NULL; } if (col == 0 && !entries[menu_lines]) { if (*p == '#') { /* show prompt if first line of external script is #interactive */ if (selected_entry >= 0 && strncmp (p, "#silent", 7) == 0) interactive = FALSE; /* A commented menu entry */ accept_entry = 1; } else if (*p == '+') { if (*(p + 1) == '=') { /* Combined adding and default */ p = test_line (edit_widget, p + 1, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for adding the entry */ p = test_line (edit_widget, p, &accept_entry); } } else if (*p == '=') { if (*(p + 1) == '+') { /* Combined adding and default */ p = test_line (edit_widget, p + 1, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for making the entry default */ i = 1; p = test_line (edit_widget, p, &i); if (selected == 0 && i) selected = menu_lines; } } else if (*p != ' ' && *p != '\t' && str_isprint (p)) { /* A menu entry title line */ if (accept_entry) entries[menu_lines] = p; else accept_entry = 1; } } if (*p == '\n') { if (entries[menu_lines]) { menu_lines++; accept_entry = 1; } max_cols = max (max_cols, col); col = 0; } else { if (*p == '\t') *p = ' '; col++; } } if (menu_lines == 0) { message (D_ERROR, MSG_ERROR, _("No suitable entries found in %s"), menu); res = FALSE; } else { if (selected_entry >= 0) selected = selected_entry; else { Listbox *listbox; max_cols = min (max (max_cols, col), MAX_ENTRY_LEN); /* Create listbox */ listbox = create_listbox_window (menu_lines, max_cols + 2, _("User menu"), "[Menu File Edit]"); /* insert all the items found */ for (i = 0; i < menu_lines; i++) { p = entries[i]; LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0], extract_line (p, p + MAX_ENTRY_LEN), p); } /* Select the default entry */ listbox_select_entry (listbox->list, selected); selected = run_listbox (listbox); } if (selected >= 0) { execute_menu_command (edit_widget, entries[selected], interactive); res = TRUE; } do_refresh (); } easy_patterns = old_patterns; MC_PTR_FREE (menu); g_free (entries); g_free (data); return res; }
int etags_set_definition_hash (const char *tagfile, const char *start_path, const char *match_func, etags_hash_t * def_hash) { /* *INDENT-OFF* */ enum { start, in_filename, in_define } state = start; /* *INDENT-ON* */ FILE *f; static char buf[BUF_LARGE]; char *chekedstr = NULL; int num = 0; /* returned value */ char *filename = NULL; if (!match_func || !tagfile) return 0; /* open file with positions */ f = fopen (tagfile, "r"); if (f == NULL) return 0; while (fgets (buf, sizeof (buf), f)) { switch (state) { case start: if (buf[0] == 0x0C) { state = in_filename; } break; case in_filename: { size_t pos; pos = strcspn (buf, ","); g_free (filename); filename = g_strndup (buf, pos); state = in_define; break; } case in_define: if (buf[0] == 0x0C) { state = in_filename; break; } /* check if the filename matches the define pos */ chekedstr = strstr (buf, match_func); if (chekedstr) { char *longname = NULL; char *shortname = NULL; long line = 0; parse_define (chekedstr, &longname, &shortname, &line); if (num < MAX_DEFINITIONS - 1) { def_hash[num].filename_len = strlen (filename); def_hash[num].fullpath = mc_build_filename (start_path, filename, (char *) NULL); canonicalize_pathname (def_hash[num].fullpath); def_hash[num].filename = g_strdup (filename); if (shortname) { def_hash[num].short_define = g_strdup (shortname); } else { def_hash[num].short_define = g_strdup (longname); } def_hash[num].line = line; num++; } } break; default: break; } } g_free (filename); fclose (f); return num; }