static int mc_def_ungetlocalcopy (const vfs_path_t * filename_vpath, const vfs_path_t * local_vpath, gboolean has_changed) { int fdin = -1, fdout = -1; const char *local; local = vfs_path_get_last_path_str (local_vpath); if (has_changed) { char buffer[BUF_1K * 8]; ssize_t i; if (vfs_path_get_last_path_vfs (filename_vpath)->write == NULL) goto failed; fdin = open (local, O_RDONLY); if (fdin == -1) goto failed; fdout = mc_open (filename_vpath, O_WRONLY | O_TRUNC); if (fdout == -1) goto failed; while ((i = read (fdin, buffer, sizeof (buffer))) > 0) if (mc_write (fdout, buffer, (size_t) i) != i) goto failed; if (i == -1) goto failed; if (close (fdin) == -1) { fdin = -1; goto failed; } fdin = -1; if (mc_close (fdout) == -1) { fdout = -1; goto failed; } } unlink (local); return 0; failed: message (D_ERROR, _("Changes to file lost"), "%s", vfs_path_get_last_path_str (filename_vpath)); if (fdout != -1) mc_close (fdout); if (fdin != -1) close (fdin); unlink (local); return -1; }
void edit_mc_menu_cmd (void) { vfs_path_t *buffer_vpath; vfs_path_t *menufile_vpath; int dir = 0; query_set_sel (1); dir = query_dialog (_("Menu edit"), _("Which menu file do you want to edit?"), D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide")); menufile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, MC_GLOBAL_MENU, NULL); if (!exist_file (vfs_path_get_last_path_str (menufile_vpath))) { vfs_path_free (menufile_vpath); menufile_vpath = vfs_path_build_filename (mc_global.share_data_dir, MC_GLOBAL_MENU, NULL); } switch (dir) { case 0: buffer_vpath = vfs_path_from_str (MC_LOCAL_MENU); check_for_default (menufile_vpath, buffer_vpath); chmod (vfs_path_get_last_path_str (buffer_vpath), 0600); break; case 1: buffer_vpath = mc_config_get_full_vpath (MC_USERMENU_FILE); check_for_default (menufile_vpath, buffer_vpath); break; case 2: buffer_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, MC_GLOBAL_MENU, NULL); if (!exist_file (vfs_path_get_last_path_str (buffer_vpath))) { vfs_path_free (buffer_vpath); buffer_vpath = vfs_path_build_filename (mc_global.share_data_dir, MC_GLOBAL_MENU, NULL); } break; default: vfs_path_free (menufile_vpath); return; } do_edit (buffer_vpath); vfs_path_free (buffer_vpath); vfs_path_free (menufile_vpath); }
char * execute_get_external_cmd_opts_from_config (const char *command, const vfs_path_t * filename_vpath, long start_line) { char *str_from_config, *return_str; char *parameter; if (filename_vpath == NULL) return g_strdup (""); parameter = g_shell_quote (vfs_path_get_last_path_str (filename_vpath)); if (start_line <= 0) return parameter; str_from_config = execute_get_opts_from_cfg (command, "%filename"); return_str = str_replace_all (str_from_config, "%filename", parameter); g_free (parameter); g_free (str_from_config); str_from_config = return_str; parameter = g_strdup_printf ("%ld", start_line); return_str = str_replace_all (str_from_config, "%lineno", parameter); g_free (parameter); g_free (str_from_config); return return_str; }
/* *INDENT-OFF* */ END_PARAMETRIZED_TEST /* *INDENT-ON* */ /* --------------------------------------------------------------------------------------------- */ /* @Test(dataSource = "test_vfs_path_relative_ds") */ /* *INDENT-OFF* */ START_PARAMETRIZED_TEST (test_vfs_path_relative_clone, test_vfs_path_relative_ds) /* *INDENT-ON* */ { /* given */ vfs_path_t *vpath, *cloned_vpath; vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON); /* when */ cloned_vpath = vfs_path_clone (vpath); /* then */ mctest_assert_int_eq (cloned_vpath->relative, TRUE); mctest_assert_str_eq (vfs_path_get_last_path_str (cloned_vpath), data->expected_last_path_in_element); mctest_assert_str_eq (vfs_path_as_str (cloned_vpath), data->expected_path); vfs_path_free (vpath); vfs_path_free (cloned_vpath); }
static cb_ret_t mcview_handle_editkey (mcview_t * view, int key) { struct hexedit_change_node *node; int byte_val; /* Has there been a change at this position? */ node = view->change_list; while ((node != NULL) && (node->offset != view->hex_cursor)) node = node->next; if (!view->hexview_in_text) { /* Hex editing */ unsigned int hexvalue = 0; if (key >= '0' && key <= '9') hexvalue = 0 + (key - '0'); else if (key >= 'A' && key <= 'F') hexvalue = 10 + (key - 'A'); else if (key >= 'a' && key <= 'f') hexvalue = 10 + (key - 'a'); else return MSG_NOT_HANDLED; if (node != NULL) byte_val = node->value; else mcview_get_byte (view, view->hex_cursor, &byte_val); if (view->hexedit_lownibble) byte_val = (byte_val & 0xf0) | (hexvalue); else byte_val = (byte_val & 0x0f) | (hexvalue << 4); } else { /* Text editing */ if (key < 256 && key != '\t') byte_val = key; else return MSG_NOT_HANDLED; } if ((view->filename_vpath != NULL) && (*(vfs_path_get_last_path_str (view->filename_vpath)) != '\0') && (view->change_list == NULL)) view->locked = lock_file (view->filename_vpath); if (node == NULL) { node = g_new (struct hexedit_change_node, 1); node->offset = view->hex_cursor; node->value = byte_val; mcview_enqueue_change (&view->change_list, node); }
static char * exec_get_file_name (const vfs_path_t * filename_vpath) { if (!do_local_copy) return quote_func (vfs_path_get_last_path_str (filename_vpath), 0); if (localfilecopy_vpath == NULL) { struct stat mystat; localfilecopy_vpath = mc_getlocalcopy (filename_vpath); if (localfilecopy_vpath == NULL) return NULL; mc_stat (localfilecopy_vpath, &mystat); localmtime = mystat.st_mtime; } return quote_func (vfs_path_get_last_path_str (localfilecopy_vpath), 0); }
static int get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen) { char *tmp; int ret; tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0); ret = get_popen_information (FILE_CMD, tmp, buf, buflen); g_free (tmp); return ret; }
static vfs_path_t * mc_def_getlocalcopy (const vfs_path_t * filename_vpath) { vfs_path_t *tmp_vpath = NULL; int fdin = -1, fdout = -1; ssize_t i; char buffer[BUF_1K * 8]; struct stat mystat; fdin = mc_open (filename_vpath, O_RDONLY | O_LINEAR); if (fdin == -1) goto fail; fdout = vfs_mkstemps (&tmp_vpath, "vfs", vfs_path_get_last_path_str (filename_vpath)); if (fdout == -1) goto fail; while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) { if (write (fdout, buffer, i) != i) goto fail; } if (i == -1) goto fail; i = mc_close (fdin); fdin = -1; if (i == -1) goto fail; i = close (fdout); fdout = -1; if (i == -1) { fdout = -1; goto fail; } if (mc_stat (filename_vpath, &mystat) != -1) mc_chmod (tmp_vpath, mystat.st_mode); return tmp_vpath; fail: vfs_path_free (tmp_vpath); if (fdout != -1) close (fdout); if (fdin != -1) mc_close (fdin); return NULL; }
/** Draw the status line at the top of the screen. The size of the filename * field varies depending on the width of the screen and the length of * the filename. */ void edit_status (WEdit * edit) { const int w = edit->widget.owner->cols; const size_t status_size = w + 1; char *const status = g_malloc (status_size); int status_len; const char *fname = ""; int fname_len; const int gap = 3; /* between the filename and the status */ const int right_gap = 5; /* at the right end of the screen */ const int preferred_fname_len = 16; status_string (edit, status, status_size); status_len = (int) str_term_width1 (status); if (edit->filename_vpath != NULL) fname = vfs_path_get_last_path_str (edit->filename_vpath); fname_len = str_term_width1 (fname); if (fname_len < preferred_fname_len) fname_len = preferred_fname_len; if (fname_len + gap + status_len + right_gap >= w) { if (preferred_fname_len + gap + status_len + right_gap >= w) fname_len = preferred_fname_len; else fname_len = w - (gap + status_len + right_gap); fname = str_trunc (fname, fname_len); } dlg_move (edit->widget.owner, 0, 0); tty_setcolor (STATUSBAR_COLOR); printwstr (fname, fname_len + gap); printwstr (status, w - (fname_len + gap)); if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH) { size_t percent = 100; if (edit->total_lines + 1 != 0) percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1); dlg_move (edit->widget.owner, 0, w - 5); tty_printf (" %3d%%", percent); } g_free (status); }
static inline void edit_status_fullscreen (WEdit * edit, int color) { Widget *h = WIDGET (WIDGET (edit)->owner); const int w = h->cols; const size_t status_size = w + 1; char *const status = g_malloc (status_size); int status_len; const char *fname = ""; int fname_len; const int gap = 3; /* between the filename and the status */ const int right_gap = 5; /* at the right end of the screen */ const int preferred_fname_len = 16; status_string (edit, status, status_size); status_len = (int) str_term_width1 (status); if (edit->filename_vpath != NULL) fname = vfs_path_get_last_path_str (edit->filename_vpath); fname_len = str_term_width1 (fname); if (fname_len < preferred_fname_len) fname_len = preferred_fname_len; if (fname_len + gap + status_len + right_gap >= w) { if (preferred_fname_len + gap + status_len + right_gap >= w) fname_len = preferred_fname_len; else fname_len = w - (gap + status_len + right_gap); fname = str_trunc (fname, fname_len); } widget_move (h, 0, 0); tty_setcolor (color); printwstr (fname, fname_len + gap); printwstr (status, w - (fname_len + gap)); if (simple_statusbar && w > EDITOR_MINIMUM_TERMINAL_WIDTH) { int percent; percent = edit_buffer_calc_percent (&edit->buffer, edit->buffer.curs1); widget_move (h, 0, w - 6 - 6); tty_printf (" %3d%%", percent); } g_free (status); }
static void mcview_display_status (WView * view) { const screen_dimen top = view->status_area.top; const screen_dimen left = view->status_area.left; const screen_dimen width = view->status_area.width; const screen_dimen height = view->status_area.height; const char *file_label; if (height < 1) return; tty_setcolor (STATUSBAR_COLOR); tty_draw_hline (WIDGET (view)->y + top, WIDGET (view)->x + left, ' ', width); file_label = view->filename_vpath != NULL ? vfs_path_get_last_path_str (view->filename_vpath) : view->command != NULL ? view->command : ""; if (width > 40) { widget_move (view, top, width - 32); if (view->hex_mode) tty_printf ("0x%08" PRIxMAX, (uintmax_t) view->hex_cursor); else { char buffer[BUF_TRUNC_LEN + 1]; size_trunc_len (buffer, BUF_TRUNC_LEN, mcview_get_filesize (view), 0, panels_options.kilobyte_si); tty_printf ("%9" PRIuMAX "/%s%s %s", (uintmax_t) view->dpy_end, buffer, mcview_may_still_grow (view) ? "+" : " ", #ifdef HAVE_CHARSET mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) : #endif ""); } } widget_move (view, top, left); if (width > 40) tty_print_string (str_fit_to_term (file_label, width - 34, J_LEFT_FIT)); else tty_print_string (str_fit_to_term (file_label, width - 5, J_LEFT_FIT)); if (width > 26) mcview_display_percent (view, view->hex_mode ? view->hex_cursor : view->dpy_end); }
void execute_with_vfs_arg (const char *command, const vfs_path_t * filename_vpath) { vfs_path_t *localcopy_vpath = NULL; const vfs_path_t *do_execute_vpath; time_t mtime; if (!execute_prepare_with_vfs_arg (filename_vpath, &localcopy_vpath, &mtime)) return; do_execute_vpath = (localcopy_vpath == NULL) ? filename_vpath : localcopy_vpath; do_execute (command, vfs_path_get_last_path_str (do_execute_vpath), EXECUTE_INTERNAL); execute_cleanup_with_vfs_arg (filename_vpath, &localcopy_vpath, &mtime); }
/* *INDENT-OFF* */ START_PARAMETRIZED_TEST (test_vfs_path_relative, test_vfs_path_relative_ds) /* *INDENT-ON* */ { /* given */ vfs_path_t *vpath; /* when */ vpath = vfs_path_from_str_flags (data->input_path, VPF_NO_CANON); /* then */ mctest_assert_int_eq (vpath->relative, TRUE); mctest_assert_str_eq (vfs_path_get_last_path_str (vpath), data->expected_last_path_in_element); mctest_assert_str_eq (vfs_path_as_str (vpath), data->expected_path); vfs_path_free (vpath); }
static int get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen) { char *tmp, *lang, *args; int ret; tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0); lang = name_quote (autodetect_codeset, 0); args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL); ret = get_popen_information ("enca", args, buf, buflen); g_free (args); g_free (lang); g_free (tmp); return ret; }
void edit_fhl_cmd (void) { vfs_path_t *fhlfile_vpath = NULL; int dir; dir = 0; if (geteuid () == 0) { dir = query_dialog (_("Highlighting groups file edit"), _("Which highlighting file you want to edit?"), D_NORMAL, 2, _("&User"), _("&System Wide")); } fhlfile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, NULL); if (dir == 0) { vfs_path_t *buffer_vpath; buffer_vpath = mc_config_get_full_vpath (MC_FHL_INI_FILE); check_for_default (fhlfile_vpath, buffer_vpath); do_edit (buffer_vpath); vfs_path_free (buffer_vpath); } else if (dir == 1) { if (!exist_file (vfs_path_get_last_path_str (fhlfile_vpath))) { vfs_path_free (fhlfile_vpath); fhlfile_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, NULL); } do_edit (fhlfile_vpath); } vfs_path_free (fhlfile_vpath); /* refresh highlighting rules */ mc_fhl_free (&mc_filehighlight); mc_filehighlight = mc_fhl_new (TRUE); }
void ext_cmd (void) { vfs_path_t *extdir_vpath; int dir; dir = 0; if (geteuid () == 0) { dir = query_dialog (_("Extension file edit"), _("Which extension file you want to edit?"), D_NORMAL, 2, _("&User"), _("&System Wide")); } extdir_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL); if (dir == 0) { vfs_path_t *buffer_vpath; buffer_vpath = mc_config_get_full_vpath (MC_FILEBIND_FILE); check_for_default (extdir_vpath, buffer_vpath); do_edit (buffer_vpath); vfs_path_free (buffer_vpath); } else if (dir == 1) { if (!exist_file (vfs_path_get_last_path_str (extdir_vpath))) { vfs_path_free (extdir_vpath); extdir_vpath = vfs_path_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL); } do_edit (extdir_vpath); } vfs_path_free (extdir_vpath); flush_extension_file (); }
static inline void edit_status_window (WEdit * edit) { Widget *w = WIDGET (edit); int y, x; int cols = w->cols; tty_setcolor (STATUSBAR_COLOR); if (cols > 5) { const char *fname = N_("NoName"); if (edit->filename_vpath != NULL) fname = vfs_path_get_last_path_str (edit->filename_vpath); #ifdef ENABLE_NLS else fname = _(fname); #endif edit_move (2, 0); tty_printf ("[%s]", str_term_trim (fname, w->cols - 8 - 6)); } tty_getyx (&y, &x); x -= w->x; x += 4; if (x + 6 <= cols - 2 - 6) { edit_move (x, 0); tty_printf ("[%c%c%c%c]", edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-', edit->modified ? 'M' : '-', macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O'); } if (cols > 30) { edit_move (2, w->lines - 1); tty_printf ("%3ld %5ld/%ld %6ld/%ld", edit->curs_col + edit->over_col, edit->buffer.curs_line + 1, edit->buffer.lines + 1, (long) edit->buffer.curs1, (long) edit->buffer.size); } /* * If we are at the end of file, print <EOF>, * otherwise print the current character as is (if printable), * as decimal and as hex. */ if (cols > 46) { edit_move (32, w->lines - 1); if (edit->buffer.curs1 >= edit->buffer.size) tty_print_string ("[<EOF> ]"); #ifdef HAVE_CHARSET else if (edit->utf8) { unsigned int cur_utf; int char_length = 1; cur_utf = edit_buffer_get_utf (&edit->buffer, edit->buffer.curs1, &char_length); if (char_length <= 0) cur_utf = edit_buffer_get_current_byte (&edit->buffer); tty_printf ("[%05d 0x%04X]", cur_utf, cur_utf); } #endif else { unsigned char cur_byte; cur_byte = edit_buffer_get_current_byte (&edit->buffer); tty_printf ("[%05d 0x%04X]", (unsigned int) cur_byte, (unsigned int) cur_byte); } } }
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; }