Ticket #1920: Issues with deba and debd VFS
[midnight-commander.git] / src / cmd.c
blob4f5bff78d6c5da14069eb5b002bafcaeff8db0ce
1 /* Routines invoked by a function key
2 They normally operate on the current panel.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /** \file cmd.c
22 * \brief Source: routines invoked by a function key
24 * They normally operate on the current panel.
27 #include <config.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #ifdef HAVE_MMAP
36 # include <sys/mman.h>
37 #endif
38 #ifdef USE_NETCODE
39 #include <netdb.h>
40 #endif
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <fcntl.h>
44 #include <pwd.h>
45 #include <grp.h>
46 #include <sys/time.h>
48 #include "global.h"
50 #include "../src/tty/tty.h" /* LINES, tty_touch_screen() */
51 #include "../src/tty/key.h" /* ALT() macro */
52 #include "../src/tty/win.h" /* do_enter_ca_mode() */
54 #include "../src/mcconfig/mcconfig.h"
55 #include "../src/search/search.h"
56 #include "../src/viewer/mcviewer.h"
57 #include "../src/filehighlight/fhl.h" /* MC_FHL_INI_FILE */
59 #include "cmd.h" /* Our definitions */
60 #include "fileopctx.h"
61 #include "file.h" /* file operation routines */
62 #include "find.h" /* do_find() */
63 #include "hotlist.h" /* hotlist_cmd() */
64 #include "tree.h" /* tree_chdir() */
65 #include "subshell.h" /* use_subshell */
66 #include "cons.saver.h" /* console_flag */
67 #include "dialog.h" /* Widget */
68 #include "wtools.h" /* message() */
69 #include "main.h" /* change_panel() */
70 #include "panel.h" /* current_panel */
71 #include "help.h" /* interactive_display() */
72 #include "user.h" /* MC_GLOBAL_MENU */
73 #include "command.h" /* cmdline */
74 #include "layout.h" /* get_current_type() */
75 #include "ext.h" /* regex_command() */
76 #include "boxes.h" /* cd_dialog() */
77 #include "setup.h"
78 #include "execute.h" /* toggle_panels() */
79 #include "history.h"
80 #include "strutil.h"
81 #include "dir.h"
82 #include "cmddef.h" /* CK_InputHistoryShow */
83 #include "fileloc.h"
85 #ifndef MAP_FILE
86 # define MAP_FILE 0
87 #endif
89 #ifdef USE_INTERNAL_EDIT
90 # include "../edit/edit.h"
91 #endif
93 /* If set and you don't have subshell support,then C-o will give you a shell */
94 int output_starts_shell = 0;
96 /* If set, use the builtin editor */
97 int use_internal_edit = 1;
99 /* Automatically fills name with current selected item name on mkdir */
100 int auto_fill_mkdir_name = 1;
102 /* if set, only selection of files is inverted */
103 int reverse_files_only = 1;
105 /* selection flags */
106 typedef enum {
107 SELECT_FILES_ONLY = 1 << 0,
108 SELECT_MATCH_CASE = 1 << 1,
109 SELECT_SHELL_PATTERNS = 1 << 2
110 } select_flags_t;
112 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
115 view_file_at_line (const char *filename, int plain_view, int internal,
116 int start_line)
118 static const char *viewer = NULL;
119 int move_dir = 0;
122 if (plain_view) {
123 int changed_hex_mode = 0;
124 int changed_nroff_flag = 0;
125 int changed_magic_flag = 0;
127 mcview_altered_hex_mode = 0;
128 mcview_altered_nroff_flag = 0;
129 mcview_altered_magic_flag = 0;
130 if (mcview_default_hex_mode)
131 changed_hex_mode = 1;
132 if (mcview_default_nroff_flag)
133 changed_nroff_flag = 1;
134 if (mcview_default_magic_flag)
135 changed_magic_flag = 1;
136 mcview_default_hex_mode = 0;
137 mcview_default_nroff_flag = 0;
138 mcview_default_magic_flag = 0;
139 mcview_viewer (NULL, filename, &move_dir, start_line);
140 if (changed_hex_mode && !mcview_altered_hex_mode)
141 mcview_default_hex_mode = 1;
142 if (changed_nroff_flag && !mcview_altered_nroff_flag)
143 mcview_default_nroff_flag = 1;
144 if (changed_magic_flag && !mcview_altered_magic_flag)
145 mcview_default_magic_flag = 1;
146 repaint_screen ();
147 return move_dir;
149 if (internal) {
150 char view_entry[BUF_TINY];
152 if (start_line != 0)
153 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
154 start_line);
155 else
156 strcpy (view_entry, "View");
158 if (regex_command (filename, view_entry, &move_dir) == 0) {
159 mcview_viewer (NULL, filename, &move_dir, start_line);
160 repaint_screen ();
162 } else {
163 if (!viewer) {
164 viewer = getenv ("VIEWER");
165 if (!viewer)
166 viewer = getenv ("PAGER");
167 if (!viewer)
168 viewer = "view";
170 execute_with_vfs_arg (viewer, filename);
172 return move_dir;
175 /* view_file (filename, plain_view, internal)
177 * Inputs:
178 * filename: The file name to view
179 * plain_view: If set does not do any fancy pre-processing (no filtering) and
180 * always invokes the internal viewer.
181 * internal: If set uses the internal viewer, otherwise an external viewer.
184 view_file (const char *filename, int plain_view, int internal)
186 return view_file_at_line (filename, plain_view, internal, 0);
189 /* scan_for_file (panel, idx, direction)
191 * Inputs:
192 * panel: pointer to the panel on which we operate
193 * idx: starting file.
194 * direction: 1, or -1
196 static int scan_for_file (WPanel *panel, int idx, int direction)
198 int i = idx + direction;
200 while (i != idx){
201 if (i < 0)
202 i = panel->count - 1;
203 if (i == panel->count)
204 i = 0;
205 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
206 return i;
207 i += direction;
209 return i;
213 * Run viewer (internal or external) on the currently selected file.
214 * If normal is 1, force internal viewer and raw mode (used for F13).
216 static void
217 do_view_cmd (int normal)
219 /* Directories are viewed by changing to them */
220 if (S_ISDIR (selection (current_panel)->st.st_mode)
221 || link_isdir (selection (current_panel))) {
222 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
223 if (query_dialog
224 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
225 _("&Yes"), _("&No")) != 0) {
226 return;
229 if (!do_cd (selection (current_panel)->fname, cd_exact))
230 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
231 } else {
232 int dir, file_idx;
233 char *filename;
235 file_idx = current_panel->selected;
236 while (1) {
237 filename = current_panel->dir.list[file_idx].fname;
239 dir = view_file (filename, normal, use_internal_view);
240 if (dir == 0)
241 break;
242 file_idx = scan_for_file (current_panel, file_idx, dir);
246 repaint_screen();
249 /* Run user's preferred viewer on the currently selected file */
250 void
251 view_cmd (void)
253 do_view_cmd (0);
256 /* Ask for file and run user's preferred viewer on it */
257 void
258 view_file_cmd (void)
260 char *filename;
262 filename =
263 input_expand_dialog (_(" View file "), _(" Filename:"),
264 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
265 if (!filename)
266 return;
268 view_file (filename, 0, use_internal_view);
269 g_free (filename);
272 /* Run plain internal viewer on the currently selected file */
273 void
274 view_simple_cmd (void)
276 do_view_cmd (1);
279 void
280 filtered_view_cmd (void)
282 char *command;
284 command =
285 input_dialog (_(" Filtered view "),
286 _(" Filter command and arguments:"),
287 MC_HISTORY_FM_FILTERED_VIEW,
288 selection (current_panel)->fname);
289 if (!command)
290 return;
292 mcview_viewer (command, "", NULL, 0);
294 g_free (command);
297 void
298 do_edit_at_line (const char *what, int start_line)
300 static const char *editor = NULL;
302 #ifdef USE_INTERNAL_EDIT
303 if (use_internal_edit)
304 edit_file (what, start_line);
305 else
306 #else
307 (void) start_line;
308 #endif /* USE_INTERNAL_EDIT */
310 if (editor == NULL) {
311 editor = getenv ("EDITOR");
312 if (editor == NULL)
313 editor = get_default_editor ();
315 execute_with_vfs_arg (editor, what);
317 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
318 repaint_screen ();
321 static void
322 do_edit (const char *what)
324 do_edit_at_line (what, 0);
327 void
328 edit_cmd (void)
330 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
331 do_edit (selection (current_panel)->fname);
334 void
335 edit_cmd_new (void)
337 do_edit (NULL);
340 /* Invoked by F5. Copy, default to the other panel. */
341 void
342 copy_cmd (void)
344 save_cwds_stat ();
345 if (panel_operate (current_panel, OP_COPY, 0)) {
346 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
347 repaint_screen ();
351 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
352 void rename_cmd (void)
354 save_cwds_stat ();
355 if (panel_operate (current_panel, OP_MOVE, 0)){
356 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
357 repaint_screen ();
361 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
362 void copy_cmd_local (void)
364 save_cwds_stat ();
365 if (panel_operate (current_panel, OP_COPY, 1)){
366 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
367 repaint_screen ();
371 /* Invoked by F16. Move/rename, default to the same panel. */
372 void rename_cmd_local (void)
374 save_cwds_stat ();
375 if (panel_operate (current_panel, OP_MOVE, 1)){
376 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
377 repaint_screen ();
381 void
382 mkdir_cmd (void)
384 char *dir, *absdir;
385 const char *name = "";
387 /* If 'on' then automatically fills name with current selected item name */
388 if (auto_fill_mkdir_name)
389 name = selection (current_panel)->fname;
391 dir =
392 input_expand_dialog (_("Create a new Directory"),
393 _(" Enter directory name:"),
394 MC_HISTORY_FM_MKDIR,
395 name);
397 if (!dir)
398 return;
400 if (dir[0] == '/' || dir[0] == '~')
401 absdir = g_strdup (dir);
402 else
403 absdir = concat_dir_and_file (current_panel->cwd, dir);
405 save_cwds_stat ();
406 if (my_mkdir (absdir, 0777) == 0) {
407 update_panels (UP_OPTIMIZE, dir);
408 repaint_screen ();
409 select_item (current_panel);
410 } else {
411 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
414 g_free (absdir);
415 g_free (dir);
418 void delete_cmd (void)
420 save_cwds_stat ();
422 if (panel_operate (current_panel, OP_DELETE, 0)){
423 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
424 repaint_screen ();
428 /* Invoked by F18. Remove selected file, regardless of marked files. */
429 void delete_cmd_local (void)
431 save_cwds_stat ();
433 if (panel_operate (current_panel, OP_DELETE, 1)){
434 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
435 repaint_screen ();
439 void find_cmd (void)
441 do_find ();
444 static void
445 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
447 g_free (p->filter);
448 p->filter = 0;
450 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
451 p->filter = allocated_filter_string;
452 else
453 g_free (allocated_filter_string);
454 reread_cmd ();
457 /* Set a given panel filter expression */
458 static void
459 set_panel_filter (WPanel *p)
461 char *reg_exp;
462 const char *x;
464 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
466 reg_exp = input_dialog_help (_(" Filter "),
467 _(" Set expression for filtering filenames"),
468 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
469 if (!reg_exp)
470 return;
471 set_panel_filter_to (p, reg_exp);
474 /* Invoked from the left/right menus */
475 void filter_cmd (void)
477 WPanel *p;
479 if (!SELECTED_IS_PANEL)
480 return;
482 p = MENU_PANEL;
483 set_panel_filter (p);
486 void reread_cmd (void)
488 int flag;
490 if (get_current_type () == view_listing &&
491 get_other_type () == view_listing)
492 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
493 else
494 flag = UP_ONLY_CURRENT;
496 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
497 repaint_screen ();
500 void
501 reverse_selection_cmd (void)
503 int i;
504 file_entry *file;
506 for (i = 0; i < current_panel->count; i++) {
507 file = &current_panel->dir.list [i];
508 if ((reverse_files_only == 0) || !S_ISDIR (file->st.st_mode))
509 do_file_mark (current_panel, i, !file->f.marked);
513 static void
514 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
516 /* dialog sizes */
517 const int DX = 50;
518 const int DY = 7;
520 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
521 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
522 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
524 char *reg_exp;
525 mc_search_t *search;
526 int i;
528 QuickWidget quick_widgets[] =
530 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
531 QUICK_CHECKBOX (DX/2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
532 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
533 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
534 QUICK_END
537 QuickDialog quick_dlg =
539 DX, DY, -1, -1, title,
540 "[Select/Unselect Files]", quick_widgets, FALSE
543 if (quick_dialog (&quick_dlg) == B_CANCEL)
544 return;
546 if (!reg_exp)
547 return;
548 if (!*reg_exp) {
549 g_free (reg_exp);
550 return;
552 search = mc_search_new (reg_exp, -1);
553 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
554 search->is_entire_line = TRUE;
555 search->is_case_sentitive = case_sens != 0;
557 for (i = 0; i < current_panel->count; i++) {
558 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
559 continue;
560 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
561 continue;
563 if (mc_search_run (search, current_panel->dir.list[i].fname,
564 0, current_panel->dir.list[i].fnamelen, NULL))
565 do_file_mark (current_panel, i, do_select);
568 mc_search_free (search);
569 g_free (reg_exp);
571 /* result flags */
572 select_flags = 0;
573 if (case_sens != 0)
574 select_flags |= SELECT_MATCH_CASE;
575 if (files_only != 0)
576 select_flags |= SELECT_FILES_ONLY;
577 if (shell_patterns != 0)
578 select_flags |= SELECT_SHELL_PATTERNS;
581 void select_cmd (void)
583 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", TRUE);
586 void unselect_cmd (void)
588 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", FALSE);
591 void ext_cmd (void)
593 char *buffer;
594 char *extdir;
595 int dir;
597 dir = 0;
598 if (geteuid () == 0){
599 dir = query_dialog (_("Extension file edit"),
600 _(" Which extension file you want to edit? "), D_NORMAL, 2,
601 _("&User"), _("&System Wide"));
603 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
605 if (dir == 0){
606 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
607 check_for_default (extdir, buffer);
608 do_edit (buffer);
609 g_free (buffer);
610 } else if (dir == 1) {
611 if (!exist_file(extdir)) {
612 g_free (extdir);
613 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
615 do_edit (extdir);
617 g_free (extdir);
618 flush_extension_file ();
621 /* edit file menu for mc */
622 void
623 edit_mc_menu_cmd (void)
625 char *buffer;
626 char *menufile;
627 int dir = 0;
629 dir = query_dialog (
630 _(" Menu edit "),
631 _(" Which menu file do you want to edit? "),
632 D_NORMAL, geteuid() ? 2 : 3,
633 _("&Local"), _("&User"), _("&System Wide")
636 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
638 if (!exist_file(menufile)) {
639 g_free (menufile);
640 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
643 switch (dir) {
644 case 0:
645 buffer = g_strdup (MC_LOCAL_MENU);
646 check_for_default (menufile, buffer);
647 chmod (buffer, 0600);
648 break;
650 case 1:
651 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL);
652 check_for_default (menufile, buffer);
653 break;
655 case 2:
656 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
657 if (!exist_file(buffer)) {
658 g_free (buffer);
659 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
661 break;
663 default:
664 g_free (menufile);
665 return;
668 do_edit (buffer);
670 g_free (buffer);
671 g_free (menufile);
674 void edit_fhl_cmd (void)
676 char *buffer = NULL;
677 char *fhlfile = NULL;
679 int dir;
681 dir = 0;
682 if (geteuid () == 0){
683 dir = query_dialog (_("Highlighting groups file edit"),
684 _(" Which highlighting file you want to edit? "), D_NORMAL, 2,
685 _("&User"), _("&System Wide"));
687 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
689 if (dir == 0){
690 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
691 check_for_default (fhlfile, buffer);
692 do_edit (buffer);
693 g_free (buffer);
694 } else if (dir == 1) {
695 if (!exist_file(fhlfile)) {
696 g_free (fhlfile);
697 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
699 do_edit (fhlfile);
701 g_free (fhlfile);
703 /* refresh highlighting rules */
704 mc_fhl_free (&mc_filehighlight);
705 mc_filehighlight = mc_fhl_new (TRUE);
708 void quick_chdir_cmd (void)
710 char *target;
712 target = hotlist_cmd (LIST_HOTLIST);
713 if (!target)
714 return;
716 if (get_current_type () == view_tree)
717 tree_chdir (the_tree, target);
718 else {
719 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
720 do_cd_command (cmd);
721 g_free (cmd);
723 g_free (target);
726 #ifdef ENABLE_VFS
727 void reselect_vfs (void)
729 char *target;
731 target = hotlist_cmd (LIST_VFSLIST);
732 if (!target)
733 return;
735 if (!do_cd (target, cd_exact))
736 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
737 g_free (target);
739 #endif /* ENABLE_VFS */
741 static int compare_files (char *name1, char *name2, off_t size)
743 int file1, file2;
744 int result = -1; /* Different by default */
746 if (size == 0)
747 return 0;
749 file1 = open (name1, O_RDONLY);
750 if (file1 >= 0){
751 file2 = open (name2, O_RDONLY);
752 if (file2 >= 0){
753 #ifdef HAVE_MMAP
754 char *data1, *data2;
755 /* Ugly if jungle */
756 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
757 if (data1 != (char*) -1){
758 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
759 if (data2 != (char*) -1){
760 rotate_dash ();
761 result = memcmp (data1, data2, size);
762 munmap (data2, size);
764 munmap (data1, size);
766 #else
767 /* Don't have mmap() :( Even more ugly :) */
768 char buf1[BUFSIZ], buf2[BUFSIZ];
769 int n1, n2;
770 rotate_dash ();
773 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
774 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
775 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
776 result = (n1 != n2) || memcmp(buf1,buf2,n1);
777 #endif /* !HAVE_MMAP */
778 close (file2);
780 close (file1);
782 return result;
785 enum CompareMode {
786 compare_quick, compare_size_only, compare_thourough
789 static void
790 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
792 int i, j;
793 char *src_name, *dst_name;
795 /* No marks by default */
796 panel->marked = 0;
797 panel->total = 0;
798 panel->dirs_marked = 0;
800 /* Handle all files in the panel */
801 for (i = 0; i < panel->count; i++){
802 file_entry *source = &panel->dir.list[i];
804 /* Default: unmarked */
805 file_mark (panel, i, 0);
807 /* Skip directories */
808 if (S_ISDIR (source->st.st_mode))
809 continue;
811 /* Search the corresponding entry from the other panel */
812 for (j = 0; j < other->count; j++){
813 if (strcmp (source->fname,
814 other->dir.list[j].fname) == 0)
815 break;
817 if (j >= other->count)
818 /* Not found -> mark */
819 do_file_mark (panel, i, 1);
820 else {
821 /* Found */
822 file_entry *target = &other->dir.list[j];
824 if (mode != compare_size_only){
825 /* Older version is not marked */
826 if (source->st.st_mtime < target->st.st_mtime)
827 continue;
830 /* Newer version with different size is marked */
831 if (source->st.st_size != target->st.st_size){
832 do_file_mark (panel, i, 1);
833 continue;
836 if (mode == compare_size_only)
837 continue;
839 if (mode == compare_quick){
840 /* Thorough compare off, compare only time stamps */
841 /* Mark newer version, don't mark version with the same date */
842 if (source->st.st_mtime > target->st.st_mtime){
843 do_file_mark (panel, i, 1);
845 continue;
848 /* Thorough compare on, do byte-by-byte comparison */
849 src_name = concat_dir_and_file (panel->cwd, source->fname);
850 dst_name = concat_dir_and_file (other->cwd, target->fname);
851 if (compare_files (src_name, dst_name, source->st.st_size))
852 do_file_mark (panel, i, 1);
853 g_free (src_name);
854 g_free (dst_name);
856 } /* for (i ...) */
859 void
860 compare_dirs_cmd (void)
862 int choice;
863 enum CompareMode thorough_flag;
865 choice =
866 query_dialog (_(" Compare directories "),
867 _(" Select compare method: "), D_NORMAL, 4,
868 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
870 if (choice < 0 || choice > 2)
871 return;
872 else
873 thorough_flag = choice;
875 if (get_current_type () == view_listing
876 && get_other_type () == view_listing) {
877 compare_dir (current_panel, other_panel, thorough_flag);
878 compare_dir (other_panel, current_panel, thorough_flag);
879 } else {
880 message (D_ERROR, MSG_ERROR,
881 _(" Both panels should be in the "
882 "listing mode to use this command "));
886 void
887 history_cmd (void)
889 /* show the history of command line widget */
890 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
893 void swap_cmd (void)
895 swap_panels ();
896 tty_touch_screen ();
897 repaint_screen ();
900 void
901 view_other_cmd (void)
903 static int message_flag = TRUE;
905 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
906 if (message_flag)
907 message (D_ERROR, MSG_ERROR,
908 _(" Not an xterm or Linux console; \n"
909 " the panels cannot be toggled. "));
910 message_flag = FALSE;
911 } else {
912 toggle_panels ();
916 static void
917 do_link (int symbolic_link, const char *fname)
919 char *dest = NULL, *src = NULL;
921 if (!symbolic_link) {
922 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
923 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
924 if (!dest || !*dest)
925 goto cleanup;
926 save_cwds_stat ();
927 if (-1 == mc_link (fname, dest))
928 message (D_ERROR, MSG_ERROR, _(" link: %s "),
929 unix_error_string (errno));
930 } else {
931 char *s;
932 char *d;
934 /* suggest the full path for symlink */
935 s = concat_dir_and_file (current_panel->cwd, fname);
937 if (get_other_type () == view_listing) {
938 d = concat_dir_and_file (other_panel->cwd, fname);
939 } else {
940 d = g_strdup (fname);
943 symlink_dialog (s, d, &dest, &src);
944 g_free (d);
945 g_free (s);
947 if (!dest || !*dest || !src || !*src)
948 goto cleanup;
949 save_cwds_stat ();
950 if (-1 == mc_symlink (dest, src))
951 message (D_ERROR, MSG_ERROR, _(" symlink: %s "),
952 unix_error_string (errno));
954 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
955 repaint_screen ();
957 cleanup:
958 g_free (src);
959 g_free (dest);
962 void link_cmd (void)
964 do_link (0, selection (current_panel)->fname);
967 void symlink_cmd (void)
969 char *filename = NULL;
970 filename = selection (current_panel)->fname;
972 if (filename) {
973 do_link (1, filename);
977 void edit_symlink_cmd (void)
979 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
980 char buffer [MC_MAXPATHLEN];
981 char *p = NULL;
982 int i;
983 char *dest, *q;
985 p = selection (current_panel)->fname;
987 q = g_strdup_printf (_(" Symlink `%s\' points to: "), str_trunc (p, 32));
989 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
990 if (i > 0) {
991 buffer [i] = 0;
992 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
993 if (dest) {
994 if (*dest && strcmp (buffer, dest)) {
995 save_cwds_stat ();
996 if (-1 == mc_unlink (p)){
997 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
998 p, unix_error_string (errno));
999 } else {
1000 if (-1 == mc_symlink (dest, p))
1001 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
1002 unix_error_string (errno));
1004 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1005 repaint_screen ();
1007 g_free (dest);
1010 g_free (q);
1011 } else {
1012 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1013 selection (current_panel)->fname);
1017 void help_cmd (void)
1019 interactive_display (NULL, "[main]");
1022 void
1023 user_file_menu_cmd (void)
1025 user_menu_cmd (NULL);
1028 /* partly taken from dcigettext.c, returns "" for default locale */
1029 /* value should be freed by calling function g_free() */
1030 char *guess_message_value (void)
1032 static const char * const var[] = {
1033 /* Setting of LC_ALL overwrites all other. */
1034 /* Do not use LANGUAGE for check user locale and drowing hints */
1035 "LC_ALL",
1036 /* Next comes the name of the desired category. */
1037 "LC_MESSAGES",
1038 /* Last possibility is the LANG environment variable. */
1039 "LANG",
1040 /* NULL exit loops */
1041 NULL
1044 unsigned i = 0;
1045 const char *locale = NULL;
1047 while (var[i] != NULL) {
1048 locale = getenv (var[i]);
1049 if (locale != NULL && locale[0] != '\0')
1050 break;
1051 i++;
1054 if (locale == NULL)
1055 locale = "";
1057 return g_strdup (locale);
1061 * Return a random hint. If force is not 0, ignore the timeout.
1063 char *
1064 get_random_hint (int force)
1066 char *data, *result = NULL, *eol;
1067 int len;
1068 int start;
1069 static int last_sec;
1070 static struct timeval tv;
1071 GIConv conv;
1072 GString *buffer;
1074 /* Do not change hints more often than one minute */
1075 gettimeofday (&tv, NULL);
1076 if (!force && !(tv.tv_sec > last_sec + 60))
1077 return g_strdup ("");
1078 last_sec = tv.tv_sec;
1080 data = load_mc_home_file (MC_HINT, NULL);
1081 if (!data)
1082 return 0;
1084 /* get a random entry */
1085 srand (tv.tv_sec);
1086 len = strlen (data);
1087 start = rand () % len;
1089 for (; start; start--) {
1090 if (data[start] == '\n') {
1091 start++;
1092 break;
1095 eol = strchr (&data[start], '\n');
1096 if (eol)
1097 *eol = 0;
1099 /* hint files are stored in utf-8 */
1100 /* try convert hint file from utf-8 to terminal encoding */
1101 conv = str_crt_conv_from ("UTF-8");
1102 if (conv != INVALID_CONV) {
1103 buffer = g_string_new ("");
1104 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE) {
1105 result = g_strdup (buffer->str);
1107 g_string_free (buffer, TRUE);
1108 str_close_conv (conv);
1111 g_free (data);
1112 return result;
1115 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1116 static void
1117 nice_cd (const char *text, const char *xtext, const char *help,
1118 const char *history_name, const char *prefix, int to_home)
1120 char *machine;
1121 char *cd_path;
1123 if (!SELECTED_IS_PANEL)
1124 return;
1126 machine = input_dialog_help (text, xtext, help, history_name, "");
1127 if (!machine)
1128 return;
1130 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1131 ugly as hell and leads to problems in vfs layer */
1133 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1134 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1135 else
1136 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1138 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1139 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1140 else
1141 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1142 g_free (cd_path);
1143 g_free (machine);
1145 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1148 #ifdef USE_NETCODE
1150 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1152 #ifdef ENABLE_VFS_MCFS
1153 void netlink_cmd (void)
1155 nice_cd (_(" Link to a remote machine "), _(machine_str),
1156 "[Network File System]", ":netlink_cmd: Link to a remote ",
1157 "/#mc:", 1);
1159 #endif /* ENABLE_VFS_MCFS */
1161 void ftplink_cmd (void)
1163 nice_cd (_(" FTP to machine "), _(machine_str),
1164 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1167 void fishlink_cmd (void)
1169 nice_cd (_(" Shell link to machine "), _(machine_str),
1170 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1171 "/#sh:", 1);
1174 #ifdef ENABLE_VFS_SMB
1175 void smblink_cmd (void)
1177 nice_cd (_(" SMB link to machine "), _(machine_str),
1178 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1179 "/#smb:", 0);
1181 #endif /* ENABLE_VFS_SMB */
1182 #endif /* USE_NETCODE */
1184 #ifdef USE_EXT2FSLIB
1185 void undelete_cmd (void)
1187 nice_cd (_(" Undelete files on an ext2 file system "),
1188 _(" Enter device (without /dev/) to undelete\n "
1189 " files on: (F1 for details)"),
1190 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1191 "/#undel:", 0);
1193 #endif /* USE_EXT2FSLIB */
1195 void quick_cd_cmd (void)
1197 char *p = cd_dialog ();
1199 if (p && *p) {
1200 char *q = g_strconcat ("cd ", p, (char *) NULL);
1202 do_cd_command (q);
1203 g_free (q);
1205 g_free (p);
1210 \brief calculate dirs sizes
1212 calculate dirs sizes and resort panel:
1213 dirs_selected = show size for selected dirs,
1214 otherwise = show size for dir under cursor:
1215 dir under cursor ".." = show size for all dirs,
1216 otherwise = show size for dir under cursor
1218 void
1219 smart_dirsize_cmd (void)
1221 WPanel *panel = current_panel;
1222 file_entry *entry;
1224 entry = &(panel->dir.list[panel->selected]);
1225 if ( ( S_ISDIR (entry->st.st_mode) && ( strcmp(entry->fname, "..") == 0 ) )
1226 || panel->dirs_marked )
1227 dirsizes_cmd ();
1228 else
1229 single_dirsize_cmd ();
1232 void
1233 single_dirsize_cmd (void)
1235 WPanel *panel = current_panel;
1236 file_entry *entry;
1237 off_t marked;
1238 double total;
1239 ComputeDirSizeUI *ui;
1241 entry = &(panel->dir.list[panel->selected]);
1242 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1243 ui = compute_dir_size_create_ui ();
1245 total = 0.0;
1247 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1248 &marked, &total) == FILE_CONT) {
1249 entry->st.st_size = (off_t) total;
1250 entry->f.dir_size_computed = 1;
1253 compute_dir_size_destroy_ui (ui);
1256 if (mark_moves_down)
1257 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1259 recalculate_panel_summary (panel);
1261 if ( current_panel->current_sort_field->sort_routine == (sortfn *) sort_size )
1262 panel_re_sort (panel);
1264 panel->dirty = 1;
1267 void
1268 dirsizes_cmd (void)
1270 WPanel *panel = current_panel;
1271 int i;
1272 off_t marked;
1273 double total;
1274 ComputeDirSizeUI *ui;
1276 ui = compute_dir_size_create_ui ();
1278 for (i = 0; i < panel->count; i++)
1279 if (S_ISDIR (panel->dir.list [i].st.st_mode)
1280 && ((panel->dirs_marked && panel->dir.list [i].f.marked)
1281 || !panel->dirs_marked)
1282 && strcmp (panel->dir.list [i].fname, "..") != 0) {
1283 total = 0.0l;
1285 if (compute_dir_size (panel->dir.list [i].fname,
1286 ui, compute_dir_size_update_ui,
1287 &marked, &total) != FILE_CONT)
1288 break;
1290 panel->dir.list [i].st.st_size = (off_t) total;
1291 panel->dir.list [i].f.dir_size_computed = 1;
1294 compute_dir_size_destroy_ui (ui);
1296 recalculate_panel_summary (panel);
1298 if ( current_panel->current_sort_field->sort_routine == (sortfn *) sort_size )
1299 panel_re_sort (panel);
1301 panel->dirty = 1;
1304 void
1305 save_setup_cmd (void)
1307 if (! save_setup ())
1308 return;
1309 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"),
1310 MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
1313 static void
1314 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1316 p->user_mini_status = use_msformat;
1317 p->list_type = view_type;
1319 if (view_type == list_user || use_msformat){
1320 g_free (p->user_format);
1321 p->user_format = user;
1323 g_free (p->user_status_format [view_type]);
1324 p->user_status_format [view_type] = status;
1326 set_panel_formats (p);
1328 else {
1329 g_free (user);
1330 g_free (status);
1333 set_panel_formats (p);
1334 do_refresh ();
1337 void
1338 info_cmd_no_menu (void)
1340 if (get_display_type (0) == view_info)
1341 set_display_type (0, view_listing);
1342 else if (get_display_type (1) == view_info)
1343 set_display_type (1, view_listing);
1344 else
1345 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1348 void
1349 quick_cmd_no_menu (void)
1351 if (get_display_type (0) == view_quick)
1352 set_display_type (0, view_listing);
1353 else if (get_display_type (1) == view_quick)
1354 set_display_type (1, view_listing);
1355 else
1356 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1359 static void
1360 switch_to_listing (int panel_index)
1362 if (get_display_type (panel_index) != view_listing)
1363 set_display_type (panel_index, view_listing);
1366 void
1367 listing_cmd (void)
1369 int view_type, use_msformat;
1370 char *user, *status;
1371 WPanel *p;
1372 int display_type;
1374 display_type = get_display_type (MENU_PANEL_IDX);
1375 if (display_type == view_listing)
1376 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1377 else
1378 p = 0;
1380 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1382 if (view_type == -1)
1383 return;
1385 switch_to_listing (MENU_PANEL_IDX);
1387 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1389 configure_panel_listing (p, view_type, use_msformat, user, status);
1392 void
1393 tree_cmd (void)
1395 set_display_type (MENU_PANEL_IDX, view_tree);
1398 void
1399 info_cmd (void)
1401 set_display_type (MENU_PANEL_IDX, view_info);
1404 void
1405 quick_view_cmd (void)
1407 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1408 change_panel ();
1409 set_display_type (MENU_PANEL_IDX, view_quick);
1412 /* Handle the tree internal listing modes switching */
1413 static int
1414 set_basic_panel_listing_to (int panel_index, int listing_mode)
1416 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1418 switch_to_listing (panel_index);
1419 p->list_type = listing_mode;
1420 if (set_panel_formats (p))
1421 return 0;
1423 do_refresh ();
1424 return 1;
1427 void
1428 toggle_listing_cmd (void)
1430 int current = get_current_index ();
1431 WPanel *p = (WPanel *) get_panel_widget (current);
1433 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1436 void
1437 encoding_cmd (void)
1439 if (SELECTED_IS_PANEL)
1440 set_panel_encoding (MENU_PANEL);