void mcview_done (WView * 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; MC_PTR_FREE (view->last_search_string); 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_args_clean_temp_help_strings (void) { MC_PTR_FREE (mc_args__loc__colors_string); MC_PTR_FREE (mc_args__loc__footer_string); MC_PTR_FREE (mc_args__loc__header_string); MC_PTR_FREE (mc_args__loc__usage_string); }
void str_uninit_strings (void) { if (str_cnv_not_convert != INVALID_CONV) g_iconv_close (str_cnv_not_convert); /* NULL-ize pointers to avoid double free in unit tests */ MC_PTR_FREE (term_encoding); MC_PTR_FREE (codeset); }
static void undelfs_shutdown (void) { if (fs) ext2fs_close (fs); fs = NULL; MC_PTR_FREE (ext2_fname); MC_PTR_FREE (delarray); MC_PTR_FREE (block_buf); }
static void mcview_continue_search_cmd (mcview_t * view) { if (view->last_search_string != NULL) mcview_search (view, FALSE); else { /* find last search string in history */ GList *history; history = history_get (MC_HISTORY_SHARED_SEARCH); if (history != NULL && history->data != NULL) { view->last_search_string = (gchar *) g_strdup (history->data); history = g_list_first (history); g_list_free_full (history, g_free); #ifdef HAVE_CHARSET view->search = mc_search_new (view->last_search_string, -1, cp_source); #else view->search = mc_search_new (view->last_search_string, -1, NULL); #endif view->search_nroff_seq = mcview_nroff_seq_new (view); if (view->search == NULL) { /* if not... then ask for an expression */ MC_PTR_FREE (view->last_search_string); mcview_search (view, TRUE); } else { 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; mcview_search (view, FALSE); } } else { /* if not... then ask for an expression */ MC_PTR_FREE (view->last_search_string); mcview_search (view, TRUE); } } }
static FILE * open_include_file (const char *filename) { FILE *f; MC_PTR_FREE (error_file_name); error_file_name = g_strdup (filename); if (g_path_is_absolute (filename)) return fopen (filename, "r"); g_free (error_file_name); error_file_name = g_build_filename (mc_config_get_data_path (), EDIT_DIR, filename, (char *) NULL); f = fopen (error_file_name, "r"); if (f != NULL) return f; g_free (error_file_name); error_file_name = g_build_filename (mc_global.sysconfig_dir, "syntax", filename, (char *) NULL); f = fopen (error_file_name, "r"); if (f != NULL) return f; g_free (error_file_name); error_file_name = g_build_filename (mc_global.share_data_dir, "syntax", filename, (char *) NULL); return fopen (error_file_name, "r"); }
void mcview_nroff_seq_free (mcview_nroff_t ** nroff) { if (nroff == NULL || *nroff == NULL) return; MC_PTR_FREE (*nroff); }
void mc_skin_deinit (void) { tty_color_free_all_tmp (); tty_color_free_all_non_tmp (); MC_PTR_FREE (mc_skin__default.name); g_hash_table_destroy (mc_skin__default.colors); mc_skin__default.colors = NULL; MC_PTR_FREE (mc_skin__default.description); mc_config_deinit (mc_skin__default.config); mc_skin__default.config = NULL; mc_skin_is_init = FALSE; }
void mc_shell_deinit (void) { if (mc_global.shell != NULL) { g_free (mc_global.shell->path); MC_PTR_FREE (mc_global.shell); } }
void mc_fhl_free (mc_fhl_t ** fhl) { if (fhl == NULL || *fhl == NULL) return; mc_fhl_clear (*fhl); MC_PTR_FREE (*fhl); }
void mcview_close_datasource (mcview_t * view) { switch (view->datasource) { case DS_NONE: break; case DS_STDIO_PIPE: if (view->ds_stdio_pipe != NULL) { (void) pclose (view->ds_stdio_pipe); mcview_display (view); close_error_pipe (D_NORMAL, NULL); view->ds_stdio_pipe = NULL; } mcview_growbuf_free (view); break; case DS_VFS_PIPE: if (view->ds_vfs_pipe != -1) { (void) mc_close (view->ds_vfs_pipe); view->ds_vfs_pipe = -1; } mcview_growbuf_free (view); break; case DS_FILE: (void) mc_close (view->ds_file_fd); view->ds_file_fd = -1; MC_PTR_FREE (view->ds_file_data); break; case DS_STRING: MC_PTR_FREE (view->ds_string_data); break; default: #ifdef HAVE_ASSERT_H assert (!"Unknown datasource type") #endif ; } view->datasource = DS_NONE; }
void file_op_context_destroy_ui (file_op_context_t * ctx) { if (ctx != NULL && ctx->ui != NULL) { file_op_context_ui_t *ui = (file_op_context_ui_t *) ctx->ui; dlg_run_done (ui->op_dlg); dlg_destroy (ui->op_dlg); MC_PTR_FREE (ctx->ui); } }
static int learn_button (WButton * button, int action) { WDialog *d; char *seq; (void) button; d = create_message (D_ERROR, _("Teach me a key"), _("Please press the %s\n" "and then wait until this message disappears.\n\n" "Then, press it again to see if OK appears\n" "next to its button.\n\n" "If you want to escape, press a single Escape key\n" "and wait as well."), _(key_name_conv_tab[action - B_USER].longname)); mc_refresh (); if (learnkeys[action - B_USER].sequence != NULL) MC_PTR_FREE (learnkeys[action - B_USER].sequence); seq = learn_key (); if (seq != NULL) { /* Esc hides the dialog and do not allow definitions of * regular characters */ gboolean seq_ok = FALSE; if (*seq != '\0' && strcmp (seq, "\\e") != 0 && strcmp (seq, "\\e\\e") != 0 && strcmp (seq, "^m") != 0 && strcmp (seq, "^i") != 0 && (seq[1] != '\0' || *seq < ' ' || *seq > '~')) { learnchanged = TRUE; learnkeys[action - B_USER].sequence = seq; seq = convert_controls (seq); seq_ok = define_sequence (key_name_conv_tab[action - B_USER].code, seq, MCKEY_NOACTION); } if (!seq_ok) message (D_NORMAL, _("Cannot accept this key"), _("You have entered \"%s\""), seq); g_free (seq); } dlg_run_done (d); dlg_destroy (d); widget_select (learnkeys[action - B_USER].button); return 0; /* Do not kill learn_dlg */ }
static void debug_out (char *start, char *end, int cond) { static char *msg; if (start == NULL && end == NULL) { /* Show output */ if (debug_flag && msg) { size_t len; len = strlen (msg); if (len != 0) msg[len - 1] = 0; message (D_NORMAL, _("Debug"), "%s", msg); } debug_flag = 0; MC_PTR_FREE (msg); } else { const char *type; char *p; /* Save debug info for later output */ if (!debug_flag) return; /* Save the result of the condition */ if (debug_error) { type = _("ERROR:"); debug_error = 0; } else if (cond) type = _("True:"); else type = _("False:"); /* This is for debugging, don't need to be super efficient. */ if (end == NULL) p = g_strdup_printf ("%s %s %c \n", msg ? msg : "", type, *start); else p = g_strdup_printf ("%s %s %.*s \n", msg ? msg : "", type, (int) (end - start), start); g_free (msg); msg = p; } }
void dlg_set_title (WDialog * h, const char *title) { MC_PTR_FREE (h->title); /* Strip existing spaces, add one space before and after the title */ if (title != NULL && title[0] != '\0') { char *t; t = g_strstrip (g_strdup (title)); if (t[0] != '\0') h->title = g_strdup_printf (" %s ", t); g_free (t); } }
void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...) { lc_mc_search->error = code; MC_PTR_FREE (lc_mc_search->error_str); if (format != NULL) { va_list args; va_start (args, format); lc_mc_search->error_str = g_strdup_vprintf (format, args); va_end (args); } }
void aspell_clean (void) { if (global_speller == NULL) return; if (global_speller->speller != NULL) mc_delete_aspell_speller (global_speller->speller); if (global_speller->config != NULL) mc_delete_aspell_config (global_speller->config); MC_PTR_FREE (global_speller); g_module_close (spell_module); spell_module = NULL; }
void dir_list_clean (dir_list * list) { int i; for (i = 0; i < list->len; i++) { file_entry_t *fentry; fentry = &list->list[i]; MC_PTR_FREE (fentry->fname); } list->len = 0; /* reduce memory usage */ dir_list_grow (list, DIR_LIST_MIN_SIZE - list->size); }
void groupbox_set_title (WGroupbox * g, const char *title) { MC_PTR_FREE (g->title); /* Strip existing spaces, add one space before and after the title */ if (title != NULL && *title != '\0') { char *t; t = g_strstrip (g_strdup (title)); g->title = g_strconcat (" ", t, " ", (char *) NULL); g_free (t); } widget_redraw (WIDGET (g)); }
void aspell_init (void) { AspellCanHaveError *error = NULL; if (strcmp (spell_language, "NONE") == 0) return; if (global_speller != NULL) return; global_speller = g_try_malloc (sizeof (spell_t)); if (global_speller == NULL) return; if (!spell_available ()) { MC_PTR_FREE (global_speller); return; } global_speller->config = mc_new_aspell_config (); global_speller->speller = NULL; if (spell_language != NULL) mc_aspell_config_replace (global_speller->config, "lang", spell_language); error = mc_new_aspell_speller (global_speller->config); if (mc_aspell_error_number (error) == 0) global_speller->speller = mc_to_aspell_speller (error); else { edit_error_dialog (_("Error"), mc_aspell_error_message (error)); mc_delete_aspell_can_have_error (error); aspell_clean (); } }
static char * resolve_symlinks (const vfs_path_t * vpath) { char *p, *p2; char *buf, *buf2, *q, *r, c; struct stat mybuf; if (vpath->relative) return NULL; p = p2 = g_strdup (vfs_path_as_str (vpath)); r = buf = g_malloc (MC_MAXPATHLEN); buf2 = g_malloc (MC_MAXPATHLEN); *r++ = PATH_SEP; *r = 0; do { q = strchr (p + 1, PATH_SEP); if (!q) { q = strchr (p + 1, 0); if (q == p + 1) break; } c = *q; *q = 0; if (mc_lstat (vpath, &mybuf) < 0) { MC_PTR_FREE (buf); goto ret; } if (!S_ISLNK (mybuf.st_mode)) strcpy (r, p + 1); else { int len; len = mc_readlink (vpath, buf2, MC_MAXPATHLEN - 1); if (len < 0) { MC_PTR_FREE (buf); goto ret; } buf2[len] = 0; if (IS_PATH_SEP (*buf2)) strcpy (buf, buf2); else strcpy (r, buf2); } canonicalize_pathname (buf); r = strchr (buf, 0); if (*r == '\0' || !IS_PATH_SEP (r[-1])) /* FIXME: this condition is always true because r points to the EOL */ { *r++ = PATH_SEP; *r = '\0'; } *q = c; p = q; } while (c != '\0'); if (*buf == '\0') strcpy (buf, PATH_SEP_STR); else if (IS_PATH_SEP (r[-1]) && r != buf + 1) r[-1] = '\0'; ret: g_free (buf2); g_free (p2); return buf; }
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; }
static int edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size) { FILE *g = NULL; char *fg, *bg, *attrs; char last_fg[32] = "", last_bg[32] = "", last_attrs[64] = ""; char whole_right[512]; char whole_left[512]; char *l = NULL; int save_line = 0, line = 0; context_rule_t *c = NULL; gboolean no_words = TRUE; int result = 0; args[0] = NULL; edit->is_case_insensitive = FALSE; strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890"); strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890"); edit->rules = g_ptr_array_new (); if (edit->defines == NULL) edit->defines = g_tree_new ((GCompareFunc) strcmp); while (TRUE) { char **a; size_t len; int argc; line++; l = NULL; len = read_one_line (&l, f); if (len != 0) xx_lowerize_line (edit, l, len); else { if (g == NULL) break; fclose (f); f = g; g = NULL; line = save_line + 1; MC_PTR_FREE (error_file_name); MC_PTR_FREE (l); len = read_one_line (&l, f); if (len == 0) break; xx_lowerize_line (edit, l, len); } argc = get_args (l, args, args_size); a = args + 1; if (args[0] == NULL) { /* do nothing */ } else if (strcmp (args[0], "include") == 0) { if (g != NULL || argc != 2) { result = line; break; } g = f; f = open_include_file (args[1]); if (f == NULL) { MC_PTR_FREE (error_file_name); result = line; break; } save_line = line; line = 0; } else if (strcmp (args[0], "caseinsensitive") == 0) { edit->is_case_insensitive = TRUE; } else if (strcmp (args[0], "wholechars") == 0) { check_a; if (strcmp (*a, "left") == 0) { a++; g_strlcpy (whole_left, *a, sizeof (whole_left)); } else if (strcmp (*a, "right") == 0) { a++; g_strlcpy (whole_right, *a, sizeof (whole_right)); } else { g_strlcpy (whole_left, *a, sizeof (whole_left)); g_strlcpy (whole_right, *a, sizeof (whole_right)); } a++; check_not_a; } else if (strcmp (args[0], "context") == 0) { syntax_keyword_t *k; check_a; if (edit->rules->len == 0) { /* first context is the default */ if (strcmp (*a, "default") != 0) break_a; a++; c = g_new0 (context_rule_t, 1); g_ptr_array_add (edit->rules, c); c->left = g_strdup (" "); c->right = g_strdup (" "); } else { /* Start new context. */ c = g_new0 (context_rule_t, 1); g_ptr_array_add (edit->rules, c); if (strcmp (*a, "exclusive") == 0) { a++; c->between_delimiters = TRUE; } check_a; if (strcmp (*a, "whole") == 0) { a++; c->whole_word_chars_left = g_strdup (whole_left); c->whole_word_chars_right = g_strdup (whole_right); } else if (strcmp (*a, "wholeleft") == 0) { a++; c->whole_word_chars_left = g_strdup (whole_left); } else if (strcmp (*a, "wholeright") == 0) { a++; c->whole_word_chars_right = g_strdup (whole_right); } check_a; if (strcmp (*a, "linestart") == 0) { a++; c->line_start_left = TRUE; } check_a; c->left = g_strdup (*a++); check_a; if (strcmp (*a, "linestart") == 0) { a++; c->line_start_right = TRUE; } check_a; c->right = g_strdup (*a++); c->first_left = *c->left; c->first_right = *c->right; } c->keyword = g_ptr_array_new (); k = g_new0 (syntax_keyword_t, 1); g_ptr_array_add (c->keyword, k); no_words = FALSE; subst_defines (edit->defines, a, &args[ARGS_LEN]); fg = *a; if (*a != NULL) a++; bg = *a; if (*a != NULL) a++; attrs = *a; if (*a != NULL) a++; g_strlcpy (last_fg, fg != NULL ? fg : "", sizeof (last_fg)); g_strlcpy (last_bg, bg != NULL ? bg : "", sizeof (last_bg)); g_strlcpy (last_attrs, attrs != NULL ? attrs : "", sizeof (last_attrs)); k->color = this_try_alloc_color_pair (fg, bg, attrs); k->keyword = g_strdup (" "); check_not_a; } else if (strcmp (args[0], "spellcheck") == 0) { if (c == NULL) { result = line; break; } c->spelling = TRUE; } else if (strcmp (args[0], "keyword") == 0) { context_rule_t *last_rule; syntax_keyword_t *k; if (no_words) break_a; check_a; last_rule = CONTEXT_RULE (g_ptr_array_index (edit->rules, edit->rules->len - 1)); k = g_new0 (syntax_keyword_t, 1); g_ptr_array_add (last_rule->keyword, k); if (strcmp (*a, "whole") == 0) { a++; k->whole_word_chars_left = g_strdup (whole_left); k->whole_word_chars_right = g_strdup (whole_right); } else if (strcmp (*a, "wholeleft") == 0) { a++; k->whole_word_chars_left = g_strdup (whole_left); } else if (strcmp (*a, "wholeright") == 0) { a++; k->whole_word_chars_right = g_strdup (whole_right); } check_a; if (strcmp (*a, "linestart") == 0) { a++; k->line_start = TRUE; } check_a; if (strcmp (*a, "whole") == 0) break_a; k->keyword = g_strdup (*a++); subst_defines (edit->defines, a, &args[ARGS_LEN]); fg = *a; if (*a != NULL) a++; bg = *a; if (*a != NULL) a++; attrs = *a; if (*a != NULL) a++; if (fg == NULL) fg = last_fg; if (bg == NULL) bg = last_bg; if (attrs == NULL) attrs = last_attrs; k->color = this_try_alloc_color_pair (fg, bg, attrs); check_not_a; } else if (*(args[0]) == '#') { /* do nothing for comment */ } else if (strcmp (args[0], "file") == 0) { break; } else if (strcmp (args[0], "define") == 0) { char *key = *a++; char **argv; if (argc < 3) break_a; argv = g_tree_lookup (edit->defines, key); if (argv != NULL) mc_defines_destroy (NULL, argv, NULL); else key = g_strdup (key); argv = g_new (char *, argc - 1); g_tree_insert (edit->defines, key, argv); while (*a != NULL) *argv++ = g_strdup (*a++); *argv = NULL; } else {
static char * filename_completion_function (const char *text, int state, input_complete_t flags) { static DIR *directory = NULL; static char *filename = NULL; static char *dirname = NULL; static char *users_dirname = NULL; static size_t filename_len; int isdir = 1, isexec = 0; static vfs_path_t *dirname_vpath = NULL; struct dirent *entry = NULL; SHOW_C_CTX ("filename_completion_function"); if (text && (flags & INPUT_COMPLETE_SHELL_ESC)) { char *u_text; char *result; char *e_result; u_text = strutils_shell_unescape (text); result = filename_completion_function (u_text, state, flags & (~INPUT_COMPLETE_SHELL_ESC)); g_free (u_text); e_result = strutils_shell_escape (result); g_free (result); return e_result; } /* If we're starting the match process, initialize us a bit. */ if (state == 0) { const char *temp; g_free (dirname); g_free (filename); g_free (users_dirname); vfs_path_free (dirname_vpath); if ((*text != '\0') && (temp = strrchr (text, PATH_SEP)) != NULL) { filename = g_strdup (++temp); dirname = g_strndup (text, temp - text); } else { dirname = g_strdup ("."); filename = g_strdup (text); } /* We aren't done yet. We also support the "~user" syntax. */ /* Save the version of the directory that the user typed. */ users_dirname = dirname; dirname = tilde_expand (dirname); canonicalize_pathname (dirname); dirname_vpath = vfs_path_from_str (dirname); /* Here we should do something with variable expansion and `command`. Maybe a dream - UNIMPLEMENTED yet. */ directory = mc_opendir (dirname_vpath); filename_len = strlen (filename); } /* Now that we have some state, we can read the directory. */ while (directory && (entry = mc_readdir (directory))) { if (!str_is_valid_string (entry->d_name)) continue; /* Special case for no filename. All entries except "." and ".." match. */ if (filename_len == 0) { if (DIR_IS_DOT (entry->d_name) || DIR_IS_DOTDOT (entry->d_name)) continue; } else { /* Otherwise, if these match up to the length of filename, then it may be a match. */ if ((entry->d_name[0] != filename[0]) || ((NLENGTH (entry)) < filename_len) || strncmp (filename, entry->d_name, filename_len)) continue; } isdir = 1; isexec = 0; { struct stat tempstat; vfs_path_t *tmp_vpath; tmp_vpath = vfs_path_build_filename (dirname, entry->d_name, (char *) NULL); /* Unix version */ if (mc_stat (tmp_vpath, &tempstat) == 0) { uid_t my_uid = getuid (); gid_t my_gid = getgid (); if (!S_ISDIR (tempstat.st_mode)) { isdir = 0; if ((!my_uid && (tempstat.st_mode & 0111)) || (my_uid == tempstat.st_uid && (tempstat.st_mode & 0100)) || (my_gid == tempstat.st_gid && (tempstat.st_mode & 0010)) || (tempstat.st_mode & 0001)) isexec = 1; } } else { /* stat failed, strange. not a dir in any case */ isdir = 0; } vfs_path_free (tmp_vpath); } if ((flags & INPUT_COMPLETE_COMMANDS) && (isexec || isdir)) break; if ((flags & INPUT_COMPLETE_CD) && isdir) break; if (flags & (INPUT_COMPLETE_FILENAMES)) break; } if (entry == NULL) { if (directory) { mc_closedir (directory); directory = NULL; } MC_PTR_FREE (dirname); vfs_path_free (dirname_vpath); dirname_vpath = NULL; MC_PTR_FREE (filename); MC_PTR_FREE (users_dirname); return NULL; } { GString *temp; temp = g_string_sized_new (16); if (users_dirname != NULL && (users_dirname[0] != '.' || users_dirname[1] != '\0')) { g_string_append (temp, users_dirname); /* We need a '/' at the end. */ if (!IS_PATH_SEP (temp->str[temp->len - 1])) g_string_append_c (temp, PATH_SEP); } g_string_append (temp, entry->d_name); if (isdir) g_string_append_c (temp, PATH_SEP); return g_string_free (temp, FALSE); } }
static int edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size) { FILE *g = NULL; char *fg, *bg, *attrs; char last_fg[32] = "", last_bg[32] = "", last_attrs[64] = ""; char whole_right[512]; char whole_left[512]; char *l = 0; int save_line = 0, line = 0; struct context_rule **r, *c = NULL; int num_words = -1, num_contexts = -1; int result = 0; int argc; int i, j; int alloc_contexts = MAX_CONTEXTS, alloc_words_per_context = MAX_WORDS_PER_CONTEXT, max_alloc_words_per_context = MAX_WORDS_PER_CONTEXT; args[0] = NULL; edit->is_case_insensitive = FALSE; strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890"); strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890"); r = edit->rules = g_malloc0 (alloc_contexts * sizeof (struct context_rule *)); if (!edit->defines) edit->defines = g_tree_new ((GCompareFunc) strcmp); for (;;) { char **a; size_t len; line++; l = 0; len = read_one_line (&l, f); if (len == 0) { if (g) { fclose (f); f = g; g = 0; line = save_line + 1; MC_PTR_FREE (error_file_name); MC_PTR_FREE (l); len = read_one_line (&l, f); if (len == 0) break; else xx_lowerize_line (edit, l, len); } else { break; } } else { xx_lowerize_line (edit, l, len); } argc = get_args (l, args, args_size); a = args + 1; if (!args[0]) { /* do nothing */ } else if (!strcmp (args[0], "include")) { if (g || argc != 2) { result = line; break; } g = f; f = open_include_file (args[1]); if (!f) { MC_PTR_FREE (error_file_name); result = line; break; } save_line = line; line = 0; } else if (!strcmp (args[0], "caseinsensitive")) { edit->is_case_insensitive = TRUE; } else if (!strcmp (args[0], "wholechars")) { check_a; if (!strcmp (*a, "left")) { a++; g_strlcpy (whole_left, *a, sizeof (whole_left)); } else if (!strcmp (*a, "right")) { a++; g_strlcpy (whole_right, *a, sizeof (whole_right)); } else { g_strlcpy (whole_left, *a, sizeof (whole_left)); g_strlcpy (whole_right, *a, sizeof (whole_right)); } a++; check_not_a; } else if (!strcmp (args[0], "context")) { check_a; if (num_contexts == -1) { if (strcmp (*a, "default")) { /* first context is the default */ break_a; } a++; c = r[0] = g_malloc0 (sizeof (struct context_rule)); c->left = g_strdup (" "); c->right = g_strdup (" "); num_contexts = 0; } else { /* Terminate previous context. */ r[num_contexts - 1]->keyword[num_words] = NULL; c = r[num_contexts] = g_malloc0 (sizeof (struct context_rule)); if (!strcmp (*a, "exclusive")) { a++; c->between_delimiters = 1; } check_a; if (!strcmp (*a, "whole")) { a++; c->whole_word_chars_left = g_strdup (whole_left); c->whole_word_chars_right = g_strdup (whole_right); } else if (!strcmp (*a, "wholeleft")) { a++; c->whole_word_chars_left = g_strdup (whole_left); } else if (!strcmp (*a, "wholeright")) { a++; c->whole_word_chars_right = g_strdup (whole_right); } check_a; if (!strcmp (*a, "linestart")) { a++; c->line_start_left = 1; } check_a; c->left = g_strdup (*a++); check_a; if (!strcmp (*a, "linestart")) { a++; c->line_start_right = 1; } check_a; c->right = g_strdup (*a++); c->first_left = *c->left; c->first_right = *c->right; } c->keyword = g_malloc (alloc_words_per_context * sizeof (struct key_word *)); num_words = 1; c->keyword[0] = g_malloc0 (sizeof (struct key_word)); subst_defines (edit->defines, a, &args[1024]); fg = *a; if (*a) a++; bg = *a; if (*a) a++; attrs = *a; if (*a) a++; g_strlcpy (last_fg, fg ? fg : "", sizeof (last_fg)); g_strlcpy (last_bg, bg ? bg : "", sizeof (last_bg)); g_strlcpy (last_attrs, attrs ? attrs : "", sizeof (last_attrs)); c->keyword[0]->color = this_try_alloc_color_pair (fg, bg, attrs); c->keyword[0]->keyword = g_strdup (" "); check_not_a; alloc_words_per_context = MAX_WORDS_PER_CONTEXT; if (++num_contexts >= alloc_contexts) { struct context_rule **tmp; alloc_contexts += 128; tmp = g_realloc (r, alloc_contexts * sizeof (struct context_rule *)); r = tmp; } } else if (!strcmp (args[0], "spellcheck")) { if (!c) { result = line; break; } c->spelling = 1; } else if (!strcmp (args[0], "keyword")) { struct key_word *k; if (num_words == -1) break_a; check_a; k = r[num_contexts - 1]->keyword[num_words] = g_malloc0 (sizeof (struct key_word)); if (!strcmp (*a, "whole")) { a++; k->whole_word_chars_left = g_strdup (whole_left); k->whole_word_chars_right = g_strdup (whole_right); } else if (!strcmp (*a, "wholeleft")) { a++; k->whole_word_chars_left = g_strdup (whole_left); } else if (!strcmp (*a, "wholeright")) { a++; k->whole_word_chars_right = g_strdup (whole_right); } check_a; if (!strcmp (*a, "linestart")) { a++; k->line_start = 1; } check_a; if (!strcmp (*a, "whole")) { break_a; } k->keyword = g_strdup (*a++); k->first = *k->keyword; subst_defines (edit->defines, a, &args[1024]); fg = *a; if (*a) a++; bg = *a; if (*a) a++; attrs = *a; if (*a) a++; if (!fg) fg = last_fg; if (!bg) bg = last_bg; if (!attrs) attrs = last_attrs; k->color = this_try_alloc_color_pair (fg, bg, attrs); check_not_a; if (++num_words >= alloc_words_per_context) { struct key_word **tmp; alloc_words_per_context += 1024; if (alloc_words_per_context > max_alloc_words_per_context) max_alloc_words_per_context = alloc_words_per_context; tmp = g_realloc (c->keyword, alloc_words_per_context * sizeof (struct key_word *)); c->keyword = tmp; } } else if (*(args[0]) == '#') { /* do nothing for comment */ } else if (!strcmp (args[0], "file")) { break; } else if (!strcmp (args[0], "define")) { char *key = *a++; char **argv; if (argc < 3) break_a; argv = g_tree_lookup (edit->defines, key); if (argv != NULL) mc_defines_destroy (NULL, argv, NULL); else key = g_strdup (key); argv = g_new (char *, argc - 1); g_tree_insert (edit->defines, key, argv); while (*a != NULL) { *argv++ = g_strdup (*a++); } *argv = NULL; } else { /* anything else is an error */