compute_dir_size() function now respect 'follow symlinks' option
[midnight-commander.git] / src / cmd.c
blobf07834c6d9217079415cd46e680f1aa3c651cf1e
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 "lib/global.h"
50 #include "lib/tty/tty.h" /* LINES, tty_touch_screen() */
51 #include "lib/tty/key.h" /* ALT() macro */
52 #include "lib/tty/win.h" /* do_enter_ca_mode() */
53 #include "lib/mcconfig.h"
54 #include "lib/search.h"
55 #include "src/viewer/mcviewer.h"
56 #include "lib/filehighlight.h" /* MC_FHL_INI_FILE */
57 #include "lib/vfs/mc-vfs/vfs.h"
58 #include "lib/fileloc.h"
59 #include "lib/strutil.h"
61 #include "cmd.h" /* Our definitions */
62 #include "fileopctx.h"
63 #include "file.h" /* file operation routines */
64 #include "find.h" /* do_find() */
65 #include "hotlist.h" /* hotlist_cmd() */
66 #include "tree.h" /* tree_chdir() */
67 #include "subshell.h" /* use_subshell */
68 #include "consaver/cons.saver.h" /* console_flag */
69 #include "dialog.h" /* Widget */
70 #include "wtools.h" /* message() */
71 #include "main.h" /* change_panel() */
72 #include "panel.h" /* current_panel */
73 #include "help.h" /* interactive_display() */
74 #include "user.h" /* MC_GLOBAL_MENU */
75 #include "command.h" /* cmdline */
76 #include "layout.h" /* get_current_type() */
77 #include "ext.h" /* regex_command() */
78 #include "boxes.h" /* cd_dialog() */
79 #include "setup.h"
80 #include "execute.h" /* toggle_panels() */
81 #include "history.h"
82 #include "dir.h"
83 #include "cmddef.h" /* CK_InputHistoryShow */
85 #ifndef MAP_FILE
86 # define MAP_FILE 0
87 #endif
89 #ifdef USE_INTERNAL_EDIT
90 # include "src/editor/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
108 SELECT_FILES_ONLY = 1 << 0,
109 SELECT_MATCH_CASE = 1 << 1,
110 SELECT_SHELL_PATTERNS = 1 << 2
111 } select_flags_t;
113 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
116 view_file_at_line (const char *filename, int plain_view, int internal, int start_line)
118 static const char *viewer = NULL;
119 int move_dir = 0;
122 if (plain_view)
124 int changed_hex_mode = 0;
125 int changed_nroff_flag = 0;
126 int changed_magic_flag = 0;
128 mcview_altered_hex_mode = 0;
129 mcview_altered_nroff_flag = 0;
130 mcview_altered_magic_flag = 0;
131 if (mcview_default_hex_mode)
132 changed_hex_mode = 1;
133 if (mcview_default_nroff_flag)
134 changed_nroff_flag = 1;
135 if (mcview_default_magic_flag)
136 changed_magic_flag = 1;
137 mcview_default_hex_mode = 0;
138 mcview_default_nroff_flag = 0;
139 mcview_default_magic_flag = 0;
140 mcview_viewer (NULL, filename, &move_dir, start_line);
141 if (changed_hex_mode && !mcview_altered_hex_mode)
142 mcview_default_hex_mode = 1;
143 if (changed_nroff_flag && !mcview_altered_nroff_flag)
144 mcview_default_nroff_flag = 1;
145 if (changed_magic_flag && !mcview_altered_magic_flag)
146 mcview_default_magic_flag = 1;
147 repaint_screen ();
148 return move_dir;
150 if (internal)
152 char view_entry[BUF_TINY];
154 if (start_line != 0)
155 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
156 else
157 strcpy (view_entry, "View");
159 if (regex_command (filename, view_entry, &move_dir) == 0)
161 mcview_viewer (NULL, filename, &move_dir, start_line);
162 repaint_screen ();
165 else
167 if (!viewer)
169 viewer = getenv ("VIEWER");
170 if (!viewer)
171 viewer = getenv ("PAGER");
172 if (!viewer)
173 viewer = "view";
175 execute_with_vfs_arg (viewer, filename);
177 return move_dir;
180 /* view_file (filename, plain_view, internal)
182 * Inputs:
183 * filename: The file name to view
184 * plain_view: If set does not do any fancy pre-processing (no filtering) and
185 * always invokes the internal viewer.
186 * internal: If set uses the internal viewer, otherwise an external viewer.
189 view_file (const char *filename, int plain_view, int internal)
191 return view_file_at_line (filename, plain_view, internal, 0);
194 /* scan_for_file (panel, idx, direction)
196 * Inputs:
197 * panel: pointer to the panel on which we operate
198 * idx: starting file.
199 * direction: 1, or -1
201 static int
202 scan_for_file (WPanel * panel, int idx, int direction)
204 int i = idx + direction;
206 while (i != idx)
208 if (i < 0)
209 i = panel->count - 1;
210 if (i == panel->count)
211 i = 0;
212 if (!S_ISDIR (panel->dir.list[i].st.st_mode))
213 return i;
214 i += direction;
216 return i;
220 * Run viewer (internal or external) on the currently selected file.
221 * If normal is 1, force internal viewer and raw mode (used for F13).
223 static void
224 do_view_cmd (int normal)
226 /* Directories are viewed by changing to them */
227 if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
229 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
231 if (query_dialog
232 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
233 _("&Yes"), _("&No")) != 0)
235 return;
238 if (!do_cd (selection (current_panel)->fname, cd_exact))
239 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
241 else
243 int dir, file_idx;
244 char *filename;
246 file_idx = current_panel->selected;
247 while (1)
249 filename = current_panel->dir.list[file_idx].fname;
251 dir = view_file (filename, normal, use_internal_view);
252 if (dir == 0)
253 break;
254 file_idx = scan_for_file (current_panel, file_idx, dir);
258 repaint_screen ();
261 /* Run user's preferred viewer on the currently selected file */
262 void
263 view_cmd (void)
265 do_view_cmd (0);
268 /* Ask for file and run user's preferred viewer on it */
269 void
270 view_file_cmd (void)
272 char *filename;
274 filename =
275 input_expand_dialog (_(" View file "), _(" Filename:"),
276 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
277 if (!filename)
278 return;
280 view_file (filename, 0, use_internal_view);
281 g_free (filename);
284 /* Run plain internal viewer on the currently selected file */
285 void
286 view_simple_cmd (void)
288 do_view_cmd (1);
291 void
292 filtered_view_cmd (void)
294 char *command;
296 command =
297 input_dialog (_(" Filtered view "),
298 _(" Filter command and arguments:"),
299 MC_HISTORY_FM_FILTERED_VIEW, selection (current_panel)->fname);
300 if (!command)
301 return;
303 mcview_viewer (command, "", NULL, 0);
305 g_free (command);
308 void
309 do_edit_at_line (const char *what, int start_line)
311 static const char *editor = NULL;
313 #ifdef USE_INTERNAL_EDIT
314 if (use_internal_edit)
315 edit_file (what, start_line);
316 else
317 #else
318 (void) start_line;
319 #endif /* USE_INTERNAL_EDIT */
321 if (editor == NULL)
323 editor = getenv ("EDITOR");
324 if (editor == NULL)
325 editor = get_default_editor ();
327 execute_with_vfs_arg (editor, what);
329 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
330 repaint_screen ();
333 static void
334 do_edit (const char *what)
336 do_edit_at_line (what, 0);
339 void
340 edit_cmd (void)
342 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
343 do_edit (selection (current_panel)->fname);
346 void
347 edit_cmd_new (void)
349 #if HAVE_CHARSET
350 source_codepage = default_source_codepage;
351 #endif
352 do_edit (NULL);
355 /* Invoked by F5. Copy, default to the other panel. */
356 void
357 copy_cmd (void)
359 save_cwds_stat ();
360 if (panel_operate (current_panel, OP_COPY, FALSE))
362 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
363 repaint_screen ();
367 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
368 void
369 rename_cmd (void)
371 save_cwds_stat ();
372 if (panel_operate (current_panel, OP_MOVE, FALSE))
374 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
375 repaint_screen ();
379 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
380 void
381 copy_cmd_local (void)
383 save_cwds_stat ();
384 if (panel_operate (current_panel, OP_COPY, TRUE))
386 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
387 repaint_screen ();
391 /* Invoked by F16. Move/rename, default to the same panel. */
392 void
393 rename_cmd_local (void)
395 save_cwds_stat ();
396 if (panel_operate (current_panel, OP_MOVE, TRUE))
398 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
399 repaint_screen ();
403 void
404 mkdir_cmd (void)
406 char *dir, *absdir;
407 const char *name = "";
409 /* If 'on' then automatically fills name with current selected item name */
410 if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
411 name = selection (current_panel)->fname;
413 dir =
414 input_expand_dialog (_("Create a new Directory"),
415 _(" Enter directory name:"), MC_HISTORY_FM_MKDIR, name);
417 if (!dir)
418 return;
420 if (*dir)
422 if (dir[0] == '/' || dir[0] == '~')
423 absdir = g_strdup (dir);
424 else
425 absdir = concat_dir_and_file (current_panel->cwd, dir);
427 save_cwds_stat ();
428 if (my_mkdir (absdir, 0777) == 0)
430 update_panels (UP_OPTIMIZE, dir);
431 repaint_screen ();
432 select_item (current_panel);
434 else
436 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
438 g_free (absdir);
440 g_free (dir);
443 void
444 delete_cmd (void)
446 save_cwds_stat ();
448 if (panel_operate (current_panel, OP_DELETE, FALSE))
450 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
451 repaint_screen ();
455 /* Invoked by F18. Remove selected file, regardless of marked files. */
456 void
457 delete_cmd_local (void)
459 save_cwds_stat ();
461 if (panel_operate (current_panel, OP_DELETE, TRUE))
463 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
464 repaint_screen ();
468 void
469 find_cmd (void)
471 do_find ();
474 static void
475 set_panel_filter_to (WPanel * p, char *allocated_filter_string)
477 g_free (p->filter);
478 p->filter = 0;
480 if (!(allocated_filter_string[0] == '*' && allocated_filter_string[1] == 0))
481 p->filter = allocated_filter_string;
482 else
483 g_free (allocated_filter_string);
484 reread_cmd ();
487 /* Set a given panel filter expression */
488 static void
489 set_panel_filter (WPanel * p)
491 char *reg_exp;
492 const char *x;
494 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
496 reg_exp = input_dialog_help (_(" Filter "),
497 _(" Set expression for filtering filenames"),
498 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
499 if (!reg_exp)
500 return;
501 set_panel_filter_to (p, reg_exp);
504 /* Invoked from the left/right menus */
505 void
506 filter_cmd (void)
508 WPanel *p;
510 if (!SELECTED_IS_PANEL)
511 return;
513 p = MENU_PANEL;
514 set_panel_filter (p);
517 void
518 reread_cmd (void)
520 int flag;
522 if (get_current_type () == view_listing && get_other_type () == view_listing)
523 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
524 else
525 flag = UP_ONLY_CURRENT;
527 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
528 repaint_screen ();
531 void
532 reverse_selection_cmd (void)
534 int i;
535 file_entry *file;
537 for (i = 0; i < current_panel->count; i++)
539 file = &current_panel->dir.list[i];
540 if ((reverse_files_only == 0) || !S_ISDIR (file->st.st_mode))
541 do_file_mark (current_panel, i, !file->f.marked);
545 static void
546 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
548 /* dialog sizes */
549 const int DX = 50;
550 const int DY = 7;
552 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
553 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
554 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
556 char *reg_exp;
557 mc_search_t *search;
558 int i;
560 QuickWidget quick_widgets[] = {
561 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
562 QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
563 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
564 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
565 QUICK_END
568 QuickDialog quick_dlg = {
569 DX, DY, -1, -1, title,
570 "[Select/Unselect Files]", quick_widgets, FALSE
573 if (quick_dialog (&quick_dlg) == B_CANCEL)
574 return;
576 if (!reg_exp)
577 return;
578 if (!*reg_exp)
580 g_free (reg_exp);
581 return;
583 search = mc_search_new (reg_exp, -1);
584 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
585 search->is_entire_line = TRUE;
586 search->is_case_sentitive = case_sens != 0;
588 for (i = 0; i < current_panel->count; i++)
590 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
591 continue;
592 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
593 continue;
595 if (mc_search_run (search, current_panel->dir.list[i].fname,
596 0, current_panel->dir.list[i].fnamelen, NULL))
597 do_file_mark (current_panel, i, do_select);
600 mc_search_free (search);
601 g_free (reg_exp);
603 /* result flags */
604 select_flags = 0;
605 if (case_sens != 0)
606 select_flags |= SELECT_MATCH_CASE;
607 if (files_only != 0)
608 select_flags |= SELECT_FILES_ONLY;
609 if (shell_patterns != 0)
610 select_flags |= SELECT_SHELL_PATTERNS;
613 void
614 select_cmd (void)
616 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", TRUE);
619 void
620 unselect_cmd (void)
622 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", FALSE);
625 void
626 ext_cmd (void)
628 char *buffer;
629 char *extdir;
630 int dir;
632 dir = 0;
633 if (geteuid () == 0)
635 dir = query_dialog (_("Extension file edit"),
636 _(" Which extension file you want to edit? "), D_NORMAL, 2,
637 _("&User"), _("&System Wide"));
639 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
641 if (dir == 0)
643 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
644 check_for_default (extdir, buffer);
645 do_edit (buffer);
646 g_free (buffer);
648 else if (dir == 1)
650 if (!exist_file (extdir))
652 g_free (extdir);
653 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
655 do_edit (extdir);
657 g_free (extdir);
658 flush_extension_file ();
661 /* edit file menu for mc */
662 void
663 edit_mc_menu_cmd (void)
665 char *buffer;
666 char *menufile;
667 int dir = 0;
669 dir = query_dialog (_(" Menu edit "),
670 _(" Which menu file do you want to edit? "),
671 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
673 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
675 if (!exist_file (menufile))
677 g_free (menufile);
678 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
681 switch (dir)
683 case 0:
684 buffer = g_strdup (MC_LOCAL_MENU);
685 check_for_default (menufile, buffer);
686 chmod (buffer, 0600);
687 break;
689 case 1:
690 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL);
691 check_for_default (menufile, buffer);
692 break;
694 case 2:
695 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
696 if (!exist_file (buffer))
698 g_free (buffer);
699 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
701 break;
703 default:
704 g_free (menufile);
705 return;
708 do_edit (buffer);
710 g_free (buffer);
711 g_free (menufile);
714 void
715 edit_fhl_cmd (void)
717 char *buffer = NULL;
718 char *fhlfile = NULL;
720 int dir;
722 dir = 0;
723 if (geteuid () == 0)
725 dir = query_dialog (_("Highlighting groups file edit"),
726 _(" Which highlighting file you want to edit? "), D_NORMAL, 2,
727 _("&User"), _("&System Wide"));
729 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
731 if (dir == 0)
733 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
734 check_for_default (fhlfile, buffer);
735 do_edit (buffer);
736 g_free (buffer);
738 else if (dir == 1)
740 if (!exist_file (fhlfile))
742 g_free (fhlfile);
743 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
745 do_edit (fhlfile);
747 g_free (fhlfile);
749 /* refresh highlighting rules */
750 mc_fhl_free (&mc_filehighlight);
751 mc_filehighlight = mc_fhl_new (TRUE);
754 void
755 quick_chdir_cmd (void)
757 char *target;
759 target = hotlist_cmd (LIST_HOTLIST);
760 if (!target)
761 return;
763 if (get_current_type () == view_tree)
764 tree_chdir (the_tree, target);
765 else
767 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
768 do_cd_command (cmd);
769 g_free (cmd);
771 g_free (target);
774 #ifdef ENABLE_VFS
775 void
776 reselect_vfs (void)
778 char *target;
780 target = hotlist_cmd (LIST_VFSLIST);
781 if (!target)
782 return;
784 if (!do_cd (target, cd_exact))
785 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
786 g_free (target);
788 #endif /* ENABLE_VFS */
790 static int
791 compare_files (char *name1, char *name2, off_t size)
793 int file1, file2;
794 int result = -1; /* Different by default */
796 if (size == 0)
797 return 0;
799 file1 = open (name1, O_RDONLY);
800 if (file1 >= 0)
802 file2 = open (name2, O_RDONLY);
803 if (file2 >= 0)
805 #ifdef HAVE_MMAP
806 char *data1, *data2;
807 /* Ugly if jungle */
808 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
809 if (data1 != (char *) -1)
811 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
812 if (data2 != (char *) -1)
814 rotate_dash ();
815 result = memcmp (data1, data2, size);
816 munmap (data2, size);
818 munmap (data1, size);
820 #else
821 /* Don't have mmap() :( Even more ugly :) */
822 char buf1[BUFSIZ], buf2[BUFSIZ];
823 int n1, n2;
824 rotate_dash ();
827 while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
828 while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
830 while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
831 result = (n1 != n2) || memcmp (buf1, buf2, n1);
832 #endif /* !HAVE_MMAP */
833 close (file2);
835 close (file1);
837 return result;
840 enum CompareMode
842 compare_quick, compare_size_only, compare_thourough
845 static void
846 compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode)
848 int i, j;
849 char *src_name, *dst_name;
851 /* No marks by default */
852 panel->marked = 0;
853 panel->total = 0;
854 panel->dirs_marked = 0;
856 /* Handle all files in the panel */
857 for (i = 0; i < panel->count; i++)
859 file_entry *source = &panel->dir.list[i];
861 /* Default: unmarked */
862 file_mark (panel, i, 0);
864 /* Skip directories */
865 if (S_ISDIR (source->st.st_mode))
866 continue;
868 /* Search the corresponding entry from the other panel */
869 for (j = 0; j < other->count; j++)
871 if (strcmp (source->fname, other->dir.list[j].fname) == 0)
872 break;
874 if (j >= other->count)
875 /* Not found -> mark */
876 do_file_mark (panel, i, 1);
877 else
879 /* Found */
880 file_entry *target = &other->dir.list[j];
882 if (mode != compare_size_only)
884 /* Older version is not marked */
885 if (source->st.st_mtime < target->st.st_mtime)
886 continue;
889 /* Newer version with different size is marked */
890 if (source->st.st_size != target->st.st_size)
892 do_file_mark (panel, i, 1);
893 continue;
896 if (mode == compare_size_only)
897 continue;
899 if (mode == compare_quick)
901 /* Thorough compare off, compare only time stamps */
902 /* Mark newer version, don't mark version with the same date */
903 if (source->st.st_mtime > target->st.st_mtime)
905 do_file_mark (panel, i, 1);
907 continue;
910 /* Thorough compare on, do byte-by-byte comparison */
911 src_name = concat_dir_and_file (panel->cwd, source->fname);
912 dst_name = concat_dir_and_file (other->cwd, target->fname);
913 if (compare_files (src_name, dst_name, source->st.st_size))
914 do_file_mark (panel, i, 1);
915 g_free (src_name);
916 g_free (dst_name);
918 } /* for (i ...) */
921 void
922 compare_dirs_cmd (void)
924 int choice;
925 enum CompareMode thorough_flag;
927 choice =
928 query_dialog (_(" Compare directories "),
929 _(" Select compare method: "), D_NORMAL, 4,
930 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
932 if (choice < 0 || choice > 2)
933 return;
935 thorough_flag = choice;
937 if (get_current_type () == view_listing && get_other_type () == view_listing)
939 compare_dir (current_panel, other_panel, thorough_flag);
940 compare_dir (other_panel, current_panel, thorough_flag);
942 else
944 message (D_ERROR, MSG_ERROR,
945 _(" Both panels should be in the " "listing mode to use this command "));
949 void
950 history_cmd (void)
952 /* show the history of command line widget */
953 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
956 void
957 swap_cmd (void)
959 swap_panels ();
960 tty_touch_screen ();
961 repaint_screen ();
964 void
965 view_other_cmd (void)
967 static int message_flag = TRUE;
969 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell)
971 if (message_flag)
972 message (D_ERROR, MSG_ERROR,
973 _(" Not an xterm or Linux console; \n" " the panels cannot be toggled. "));
974 message_flag = FALSE;
976 else
978 toggle_panels ();
982 static void
983 do_link (int symbolic_link, const char *fname)
985 char *dest = NULL, *src = NULL;
987 if (!symbolic_link)
989 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
990 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
991 if (!dest || !*dest)
992 goto cleanup;
993 save_cwds_stat ();
994 if (-1 == mc_link (fname, dest))
995 message (D_ERROR, MSG_ERROR, _(" link: %s "), unix_error_string (errno));
997 else
999 char *s;
1000 char *d;
1002 /* suggest the full path for symlink */
1003 s = concat_dir_and_file (current_panel->cwd, fname);
1005 if (get_other_type () == view_listing)
1007 d = concat_dir_and_file (other_panel->cwd, fname);
1009 else
1011 d = g_strdup (fname);
1014 symlink_dialog (s, d, &dest, &src);
1015 g_free (d);
1016 g_free (s);
1018 if (!dest || !*dest || !src || !*src)
1019 goto cleanup;
1020 save_cwds_stat ();
1021 if (-1 == mc_symlink (dest, src))
1022 message (D_ERROR, MSG_ERROR, _(" symlink: %s "), unix_error_string (errno));
1024 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1025 repaint_screen ();
1027 cleanup:
1028 g_free (src);
1029 g_free (dest);
1032 void
1033 link_cmd (void)
1035 do_link (0, selection (current_panel)->fname);
1038 void
1039 symlink_cmd (void)
1041 char *filename = NULL;
1042 filename = selection (current_panel)->fname;
1044 if (filename)
1046 do_link (1, filename);
1050 void
1051 edit_symlink_cmd (void)
1053 if (S_ISLNK (selection (current_panel)->st.st_mode))
1055 char buffer[MC_MAXPATHLEN];
1056 char *p = NULL;
1057 int i;
1058 char *dest, *q;
1060 p = selection (current_panel)->fname;
1062 q = g_strdup_printf (_(" Symlink `%s\' points to: "), str_trunc (p, 32));
1064 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
1065 if (i > 0)
1067 buffer[i] = 0;
1068 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
1069 if (dest)
1071 if (*dest && strcmp (buffer, dest))
1073 save_cwds_stat ();
1074 if (-1 == mc_unlink (p))
1076 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1077 p, unix_error_string (errno));
1079 else
1081 if (-1 == mc_symlink (dest, p))
1082 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
1083 unix_error_string (errno));
1085 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1086 repaint_screen ();
1088 g_free (dest);
1091 g_free (q);
1093 else
1095 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1096 selection (current_panel)->fname);
1100 void
1101 help_cmd (void)
1103 if (current_panel->searching)
1104 interactive_display (NULL, "[Quick search]");
1105 else
1106 interactive_display (NULL, "[main]");
1109 void
1110 user_file_menu_cmd (void)
1112 user_menu_cmd (NULL);
1116 * Return a random hint. If force is not 0, ignore the timeout.
1118 char *
1119 get_random_hint (int force)
1121 char *data, *result = NULL, *eol;
1122 int len;
1123 int start;
1124 static int last_sec;
1125 static struct timeval tv;
1126 GIConv conv;
1127 GString *buffer;
1129 /* Do not change hints more often than one minute */
1130 gettimeofday (&tv, NULL);
1131 if (!force && !(tv.tv_sec > last_sec + 60))
1132 return g_strdup ("");
1133 last_sec = tv.tv_sec;
1135 data = load_mc_home_file (mc_home, mc_home_alt, MC_HINT, NULL);
1136 if (!data)
1137 return 0;
1139 /* get a random entry */
1140 srand (tv.tv_sec);
1141 len = strlen (data);
1142 start = rand () % len;
1144 for (; start; start--)
1146 if (data[start] == '\n')
1148 start++;
1149 break;
1152 eol = strchr (&data[start], '\n');
1153 if (eol)
1154 *eol = 0;
1156 /* hint files are stored in utf-8 */
1157 /* try convert hint file from utf-8 to terminal encoding */
1158 conv = str_crt_conv_from ("UTF-8");
1159 if (conv != INVALID_CONV)
1161 buffer = g_string_new ("");
1162 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1164 result = g_strdup (buffer->str);
1166 g_string_free (buffer, TRUE);
1167 str_close_conv (conv);
1170 g_free (data);
1171 return result;
1174 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1175 static void
1176 nice_cd (const char *text, const char *xtext, const char *help,
1177 const char *history_name, const char *prefix, int to_home)
1179 char *machine;
1180 char *cd_path;
1182 if (!SELECTED_IS_PANEL)
1183 return;
1185 machine = input_dialog_help (text, xtext, help, history_name, "");
1186 if (!machine)
1187 return;
1189 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1190 ugly as hell and leads to problems in vfs layer */
1192 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1193 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1194 else
1195 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1197 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1198 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1199 else
1200 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1201 g_free (cd_path);
1202 g_free (machine);
1204 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1207 #ifdef USE_NETCODE
1209 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1211 #ifdef ENABLE_VFS_MCFS
1212 void
1213 netlink_cmd (void)
1215 nice_cd (_(" Link to a remote machine "), _(machine_str),
1216 "[Network File System]", ":netlink_cmd: Link to a remote ", "/#mc:", 1);
1218 #endif /* ENABLE_VFS_MCFS */
1220 void
1221 ftplink_cmd (void)
1223 nice_cd (_(" FTP to machine "), _(machine_str),
1224 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1227 void
1228 fishlink_cmd (void)
1230 nice_cd (_(" Shell link to machine "), _(machine_str),
1231 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1232 "/#sh:", 1);
1235 #ifdef ENABLE_VFS_SMB
1236 void
1237 smblink_cmd (void)
1239 nice_cd (_(" SMB link to machine "), _(machine_str),
1240 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "/#smb:", 0);
1242 #endif /* ENABLE_VFS_SMB */
1243 #endif /* USE_NETCODE */
1245 #ifdef USE_EXT2FSLIB
1246 void
1247 undelete_cmd (void)
1249 nice_cd (_(" Undelete files on an ext2 file system "),
1250 _(" Enter device (without /dev/) to undelete\n "
1251 " files on: (F1 for details)"),
1252 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "/#undel:", 0);
1254 #endif /* USE_EXT2FSLIB */
1256 void
1257 quick_cd_cmd (void)
1259 char *p = cd_dialog ();
1261 if (p && *p)
1263 char *q = g_strconcat ("cd ", p, (char *) NULL);
1265 do_cd_command (q);
1266 g_free (q);
1268 g_free (p);
1273 \brief calculate dirs sizes
1275 calculate dirs sizes and resort panel:
1276 dirs_selected = show size for selected dirs,
1277 otherwise = show size for dir under cursor:
1278 dir under cursor ".." = show size for all dirs,
1279 otherwise = show size for dir under cursor
1281 void
1282 smart_dirsize_cmd (void)
1284 WPanel *panel = current_panel;
1285 file_entry *entry;
1287 entry = &(panel->dir.list[panel->selected]);
1288 if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
1289 dirsizes_cmd ();
1290 else
1291 single_dirsize_cmd ();
1294 void
1295 single_dirsize_cmd (void)
1297 WPanel *panel = current_panel;
1298 file_entry *entry;
1299 off_t marked;
1300 double total;
1301 ComputeDirSizeUI *ui;
1303 entry = &(panel->dir.list[panel->selected]);
1304 if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
1306 ui = compute_dir_size_create_ui ();
1308 total = 0.0;
1310 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1311 &marked, &total, TRUE) == FILE_CONT)
1313 entry->st.st_size = (off_t) total;
1314 entry->f.dir_size_computed = 1;
1317 compute_dir_size_destroy_ui (ui);
1320 if (mark_moves_down)
1321 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1323 recalculate_panel_summary (panel);
1325 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1326 panel_re_sort (panel);
1328 panel->dirty = 1;
1331 void
1332 dirsizes_cmd (void)
1334 WPanel *panel = current_panel;
1335 int i;
1336 off_t marked;
1337 double total;
1338 ComputeDirSizeUI *ui;
1340 ui = compute_dir_size_create_ui ();
1342 for (i = 0; i < panel->count; i++)
1343 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1344 && ((panel->dirs_marked && panel->dir.list[i].f.marked)
1345 || !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
1347 total = 0.0l;
1349 if (compute_dir_size (panel->dir.list[i].fname,
1350 ui, compute_dir_size_update_ui, &marked, &total, TRUE) != FILE_CONT)
1351 break;
1353 panel->dir.list[i].st.st_size = (off_t) total;
1354 panel->dir.list[i].f.dir_size_computed = 1;
1357 compute_dir_size_destroy_ui (ui);
1359 recalculate_panel_summary (panel);
1361 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1362 panel_re_sort (panel);
1364 panel->dirty = 1;
1367 void
1368 save_setup_cmd (void)
1370 if (!save_setup ())
1371 return;
1372 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"),
1373 MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
1376 static void
1377 configure_panel_listing (WPanel * p, int list_type, int use_msformat, char *user, char *status)
1379 p->user_mini_status = use_msformat;
1380 p->list_type = list_type;
1382 if (list_type == list_user || use_msformat)
1384 g_free (p->user_format);
1385 p->user_format = user;
1387 g_free (p->user_status_format[list_type]);
1388 p->user_status_format[list_type] = status;
1390 set_panel_formats (p);
1392 else
1394 g_free (user);
1395 g_free (status);
1398 set_panel_formats (p);
1399 do_refresh ();
1402 void
1403 info_cmd_no_menu (void)
1405 if (get_display_type (0) == view_info)
1406 set_display_type (0, view_listing);
1407 else if (get_display_type (1) == view_info)
1408 set_display_type (1, view_listing);
1409 else
1410 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1413 void
1414 quick_cmd_no_menu (void)
1416 if (get_display_type (0) == view_quick)
1417 set_display_type (0, view_listing);
1418 else if (get_display_type (1) == view_quick)
1419 set_display_type (1, view_listing);
1420 else
1421 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1424 static void
1425 switch_to_listing (int panel_index)
1427 if (get_display_type (panel_index) != view_listing)
1428 set_display_type (panel_index, view_listing);
1431 void
1432 listing_cmd (void)
1434 int list_type;
1435 int use_msformat;
1436 char *user, *status;
1437 WPanel *p = NULL;
1439 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1440 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1442 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1444 if (list_type != -1)
1446 switch_to_listing (MENU_PANEL_IDX);
1447 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1448 configure_panel_listing (p, list_type, use_msformat, user, status);
1452 void
1453 tree_cmd (void)
1455 set_display_type (MENU_PANEL_IDX, view_tree);
1458 void
1459 info_cmd (void)
1461 set_display_type (MENU_PANEL_IDX, view_info);
1464 void
1465 quick_view_cmd (void)
1467 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1468 change_panel ();
1469 set_display_type (MENU_PANEL_IDX, view_quick);
1472 /* Handle the tree internal listing modes switching */
1473 static gboolean
1474 set_basic_panel_listing_to (int panel_index, int listing_mode)
1476 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1477 gboolean ok;
1479 switch_to_listing (panel_index);
1480 p->list_type = listing_mode;
1482 ok = set_panel_formats (p) == 0;
1484 if (ok)
1485 do_refresh ();
1487 return ok;
1490 void
1491 toggle_listing_cmd (void)
1493 int current = get_current_index ();
1494 WPanel *p = (WPanel *) get_panel_widget (current);
1496 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1499 void
1500 encoding_cmd (void)
1502 if (SELECTED_IS_PANEL)
1503 set_panel_encoding (MENU_PANEL);