Just a little correction at the it.po file.
[midnight-commander.git] / src / cmd.c
blob7ad35fbfd32f237ab225bb6a51b38e1e66b62dd7
1 /* Routines invoked by a function key
2 They normally operate on the current panel.
4 Copyright (C) 1994, 1995 Miguel de Icaza
5 Copyright (C) 1994, 1995 Janne Kukonlehto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <config.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <errno.h>
33 #ifdef USE_NETCODE
34 #include <netdb.h>
35 #endif
37 #ifdef HAVE_MMAP
38 # include <sys/mman.h>
39 #endif
41 #include "global.h"
42 #include "cmd.h" /* Our definitions */
43 #include "fileopctx.h" /* file_op_context_new() */
44 #include "file.h" /* copy_file_file() */
45 #include "find.h" /* do_find() */
46 #include "hotlist.h" /* hotlist_cmd() */
47 #include "tree.h" /* tree_chdir() */
48 #include "subshell.h" /* use_subshell */
49 #include "cons.saver.h" /* console_flag */
50 #include "tty.h" /* LINES */
51 #include "dlg.h" /* Widget */
52 #include "view.h" /* view() */
53 #include "wtools.h" /* message() */
54 #include "widget.h" /* push_history() */
55 #include "key.h" /* application_keypad_mode() */
56 #include "win.h" /* do_enter_ca_mode() */
57 #include "dialog.h" /* do_refresh() */
58 #include "main.h" /* change_panel() */
59 #include "panel.h" /* cpanel */
60 #include "help.h" /* interactive_display() */
61 #include "user.h" /* MC_GLOBAL_MENU */
62 #include "command.h" /* cmdline */
63 #include "layout.h" /* get_current_type() */
64 #include "ext.h" /* regex_command() */
65 #include "boxes.h" /* cd_dialog() */
66 #include "setup.h" /* save_setup() */
67 #include "profile.h" /* PROFILE_NAME */
69 #ifndef MAP_FILE
70 # define MAP_FILE 0
71 #endif
73 #ifdef USE_INTERNAL_EDIT
74 # include "../edit/edit.h"
75 #endif
77 /* If set and you don't have subshell support,then C-o will give you a shell */
78 int output_starts_shell = 0;
80 /* Source routing destination */
81 int source_route = 0;
83 /* If set, use the builtin editor */
84 int use_internal_edit = 1;
88 * Execute command on a filename that can be on VFS.
89 * Errors are reported to the user.
91 static void
92 execute_with_vfs_arg (const char *command, const char *filename)
94 char *localcopy;
95 char *fn;
96 struct stat st;
97 time_t mtime;
99 /* Simplest case, this file is local */
100 if (!filename || vfs_file_is_local (filename)) {
101 execute_internal (command, filename);
102 return;
105 /* FIXME: Creation of new files on VFS is not supported */
106 if (!*filename)
107 return;
109 localcopy = mc_getlocalcopy (filename);
110 if (localcopy == NULL) {
111 message (1, MSG_ERROR, _(" Cannot fetch a local copy of %s "),
112 filename);
113 return;
117 * filename can be an entry on panel, it can be changed by executing
118 * the command, so make a copy. Smarter VFS code would make the code
119 * below unnecessary.
121 fn = g_strdup (filename);
122 mc_stat (localcopy, &st);
123 mtime = st.st_mtime;
124 execute_internal (command, localcopy);
125 mc_stat (localcopy, &st);
126 mc_ungetlocalcopy (fn, localcopy, mtime != st.st_mtime);
127 g_free (fn);
132 view_file_at_line (char *filename, int plain_view, int internal,
133 int start_line)
135 static char *viewer = 0;
136 int move_dir = 0;
139 if (plain_view) {
140 int changed_hex_mode = 0;
141 int changed_nroff_flag = 0;
142 int changed_magic_flag = 0;
144 altered_hex_mode = 0;
145 altered_nroff_flag = 0;
146 altered_magic_flag = 0;
147 if (default_hex_mode)
148 changed_hex_mode = 1;
149 if (default_nroff_flag)
150 changed_nroff_flag = 1;
151 if (default_magic_flag)
152 changed_magic_flag = 1;
153 default_hex_mode = 0;
154 default_nroff_flag = 0;
155 default_magic_flag = 0;
156 view (0, filename, &move_dir, start_line);
157 if (changed_hex_mode && !altered_hex_mode)
158 default_hex_mode = 1;
159 if (changed_nroff_flag && !altered_nroff_flag)
160 default_nroff_flag = 1;
161 if (changed_magic_flag && !altered_magic_flag)
162 default_magic_flag = 1;
163 repaint_screen ();
164 return move_dir;
166 if (internal) {
167 char view_entry[BUF_TINY];
169 if (start_line != 0)
170 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
171 start_line);
172 else
173 strcpy (view_entry, "View");
175 if (regex_command (filename, view_entry, &move_dir) == 0) {
176 view (0, filename, &move_dir, start_line);
177 repaint_screen ();
179 } else {
180 if (!viewer) {
181 viewer = getenv ("PAGER");
182 if (!viewer)
183 viewer = "view";
185 execute_with_vfs_arg (viewer, filename);
187 return move_dir;
190 /* view_file (filename, plain_view, internal)
192 * Inputs:
193 * filename: The file name to view
194 * plain_view: If set does not do any fancy pre-processing (no filtering) and
195 * always invokes the internal viewer.
196 * internal: If set uses the internal viewer, otherwise an external viewer.
199 view_file (char *filename, int plain_view, int internal)
201 return view_file_at_line (filename, plain_view, internal, 0);
204 /* scan_for_file (panel, idx, direction)
206 * Inputs:
207 * panel: pointer to the panel on which we operate
208 * idx: starting file.
209 * direction: 1, or -1
211 static int scan_for_file (WPanel *panel, int idx, int direction)
213 int i = idx + direction;
215 while (i != idx){
216 if (i < 0)
217 i = panel->count - 1;
218 if (i == panel->count)
219 i = 0;
220 if (!S_ISDIR (panel->dir.list [i].buf.st_mode))
221 return i;
222 i += direction;
224 return i;
228 * Run viewer (internal or external) on the currently selected file.
229 * If normal is 1, force internal viewer and raw mode (used for F13).
231 static void
232 do_view_cmd (int normal)
234 int dir, file_idx;
236 /* Directories are viewed by changing to them */
237 if (S_ISDIR (selection (cpanel)->buf.st_mode)
238 || link_isdir (selection (cpanel))) {
239 if (confirm_view_dir && (cpanel->marked || cpanel->dirs_marked)) {
240 if (query_dialog
241 (_(" Confirmation "), _("Files tagged, want to cd?"), 0, 2,
242 _("&Yes"), _("&No")) != 0) {
243 return;
246 if (!do_cd (selection (cpanel)->fname, cd_exact))
247 message (1, MSG_ERROR, _("Cannot change directory"));
249 return;
253 file_idx = cpanel->selected;
254 while (1) {
255 char *filename;
257 filename = cpanel->dir.list[file_idx].fname;
259 dir = view_file (filename, normal, use_internal_view);
260 if (dir == 0)
261 break;
262 file_idx = scan_for_file (cpanel, file_idx, dir);
266 /* Run user's preferred viewer on the currently selected file */
267 void
268 view_cmd (void)
270 do_view_cmd (0);
273 /* Ask for file and run user's preferred viewer on it */
274 void
275 view_file_cmd (void)
277 char *filename;
279 filename =
280 input_dialog (_(" View file "), _(" Filename:"),
281 selection (cpanel)->fname);
282 if (!filename)
283 return;
285 view_file (filename, 0, use_internal_view);
286 g_free (filename);
289 /* Run plain internal viewer on the currently selected file */
290 void
291 view_simple_cmd (void)
293 do_view_cmd (1);
296 void
297 filtered_view_cmd (void)
299 char *command;
301 command =
302 input_dialog (_(" Filtered view "),
303 _(" Filter command and arguments:"),
304 selection (cpanel)->fname);
305 if (!command)
306 return;
308 view (command, "", 0, 0);
310 g_free (command);
313 void do_edit_at_line (const char *what, int start_line)
315 static char *editor = 0;
317 #ifdef USE_INTERNAL_EDIT
318 if (use_internal_edit){
319 edit (what, start_line);
320 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
321 repaint_screen ();
322 return;
324 #endif /* USE_INTERNAL_EDIT */
325 if (!editor){
326 editor = getenv ("EDITOR");
327 if (!editor)
328 editor = get_default_editor ();
330 execute_with_vfs_arg (editor, what);
331 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
332 repaint_screen ();
335 static void
336 do_edit (const char *what)
338 do_edit_at_line (what, 0);
341 void
342 edit_cmd (void)
344 if (regex_command (selection (cpanel)->fname, "Edit", 0) == 0)
345 do_edit (selection (cpanel)->fname);
348 void
349 edit_cmd_new (void)
351 do_edit (NULL);
354 /* Invoked by F5. Copy, default to the other panel. */
355 void
356 copy_cmd (void)
358 save_cwds_stat ();
359 if (panel_operate (cpanel, OP_COPY, NULL, TRUE)) {
360 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
361 repaint_screen ();
365 /* Invoked by F6. Move/rename, default to the other panel. */
366 void ren_cmd (void)
368 save_cwds_stat ();
369 if (panel_operate (cpanel, OP_MOVE, NULL, TRUE)){
370 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
371 repaint_screen ();
375 /* Invoked by F15. Copy, default to the same panel. */
376 void copy_cmd_local (void)
378 save_cwds_stat ();
379 if (panel_operate (cpanel, OP_COPY, selection (cpanel)->fname, TRUE)){
380 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
381 repaint_screen ();
385 /* Invoked by F16. Move/rename, default to the same panel. */
386 void ren_cmd_local (void)
388 save_cwds_stat ();
389 if (panel_operate (cpanel, OP_MOVE, selection (cpanel)->fname, TRUE)){
390 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
391 repaint_screen ();
395 void
396 mkdir_cmd (void)
398 char *tempdir;
399 char *dir;
401 dir =
402 input_expand_dialog (_("Create a new Directory"),
403 _(" Enter directory name:"), "");
405 if (!dir || !*dir)
406 return;
408 if (dir[0] == '/' || dir[0] == '~')
409 tempdir = g_strdup (dir);
410 else
411 tempdir = concat_dir_and_file (cpanel->cwd, dir);
412 g_free (dir);
414 save_cwds_stat ();
415 if (my_mkdir (tempdir, 0777) == 0) {
416 update_panels (UP_OPTIMIZE, tempdir);
417 repaint_screen ();
418 select_item (cpanel);
419 g_free (tempdir);
420 return;
422 g_free (tempdir);
423 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
426 void delete_cmd (void)
428 save_cwds_stat ();
430 if (panel_operate (cpanel, OP_DELETE, NULL, TRUE)){
431 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
432 repaint_screen ();
436 void find_cmd (void)
438 do_find ();
441 static void
442 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
444 if (p->filter){
445 g_free (p->filter);
446 p->filter = 0;
448 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
449 p->filter = allocated_filter_string;
450 else
451 g_free (allocated_filter_string);
452 reread_cmd ();
455 /* Set a given panel filter expression */
456 static void
457 set_panel_filter (WPanel *p)
459 char *reg_exp;
460 char *x;
462 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
464 reg_exp = input_dialog_help (_(" Filter "),
465 _(" Set expression for filtering filenames"),
466 "[Filter...]", x);
467 if (!reg_exp)
468 return;
469 set_panel_filter_to (p, reg_exp);
472 /* Invoked from the left/right menus */
473 void filter_cmd (void)
475 WPanel *p;
477 if (!SELECTED_IS_PANEL)
478 return;
480 p = MENU_PANEL;
481 set_panel_filter (p);
484 void reread_cmd (void)
486 int flag;
488 if (get_current_type () == view_listing &&
489 get_other_type () == view_listing)
490 flag = strcmp (cpanel->cwd, opanel->cwd) ? UP_ONLY_CURRENT : 0;
491 else
492 flag = UP_ONLY_CURRENT;
494 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
495 repaint_screen ();
498 void reverse_selection_cmd (void)
500 file_entry *file;
501 int i;
503 for (i = 0; i < cpanel->count; i++){
504 file = &cpanel->dir.list [i];
505 if (S_ISDIR (file->buf.st_mode))
506 continue;
507 do_file_mark (cpanel, i, !file->f.marked);
509 paint_panel (cpanel);
512 void select_cmd (void)
514 char *reg_exp, *reg_exp_t;
515 int i;
516 int c;
517 int dirflag = 0;
519 reg_exp = input_dialog (_(" Select "), "", easy_patterns ? "*" : ".");
520 if (!reg_exp)
521 return;
523 reg_exp_t = reg_exp;
525 /* Check if they specified a directory */
526 if (*reg_exp_t == PATH_SEP){
527 dirflag = 1;
528 reg_exp_t++;
530 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
531 dirflag = 1;
532 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
535 for (i = 0; i < cpanel->count; i++){
536 if (!strcmp (cpanel->dir.list [i].fname, ".."))
537 continue;
538 if (S_ISDIR (cpanel->dir.list [i].buf.st_mode)){
539 if (!dirflag)
540 continue;
541 } else {
542 if (dirflag)
543 continue;
545 c = regexp_match (reg_exp_t, cpanel->dir.list [i].fname, match_file);
546 if (c == -1){
547 message (1, MSG_ERROR, _(" Malformed regular expression "));
548 g_free (reg_exp);
549 return;
551 if (c){
552 do_file_mark (cpanel, i, 1);
555 paint_panel (cpanel);
556 g_free (reg_exp);
559 void unselect_cmd (void)
561 char *reg_exp, *reg_exp_t;
562 int i;
563 int c;
564 int dirflag = 0;
566 reg_exp = input_dialog (_(" Unselect "),"", easy_patterns ? "*" : ".");
567 if (!reg_exp)
568 return;
570 reg_exp_t = reg_exp;
572 /* Check if they specified directory matching */
573 if (*reg_exp_t == PATH_SEP){
574 dirflag = 1;
575 reg_exp_t ++;
577 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
578 dirflag = 1;
579 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
581 for (i = 0; i < cpanel->count; i++){
582 if (!strcmp (cpanel->dir.list [i].fname, ".."))
583 continue;
584 if (S_ISDIR (cpanel->dir.list [i].buf.st_mode)){
585 if (!dirflag)
586 continue;
587 } else {
588 if (dirflag)
589 continue;
591 c = regexp_match (reg_exp_t, cpanel->dir.list [i].fname, match_file);
592 if (c == -1){
593 message (1, MSG_ERROR, _(" Malformed regular expression "));
594 g_free (reg_exp);
595 return;
597 if (c){
598 do_file_mark (cpanel, i, 0);
601 paint_panel (cpanel);
602 g_free (reg_exp);
605 /* Check if the file exists */
606 /* If not copy the default */
607 static int
608 check_for_default(char *default_file, char *file)
610 struct stat s;
611 off_t count = 0;
612 double bytes = 0;
613 FileOpContext *ctx;
615 if (mc_stat (file, &s)){
616 if (mc_stat (default_file, &s)){
617 return -1;
619 ctx = file_op_context_new ();
620 file_op_context_create_ui (ctx, OP_COPY, 0);
621 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
622 file_op_context_destroy (ctx);
624 return 0;
627 void ext_cmd (void)
629 char *buffer;
630 char *extdir;
631 int dir;
633 dir = 0;
634 if (geteuid () == 0){
635 dir = query_dialog (_("Extension file edit"),
636 _(" Which extension file you want to edit? "), 0, 2,
637 _("&User"), _("&System Wide"));
639 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
641 if (dir == 0){
642 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
643 check_for_default (extdir, buffer);
644 do_edit (buffer);
645 g_free (buffer);
646 } else if (dir == 1)
647 do_edit (extdir);
649 g_free (extdir);
650 flush_extension_file ();
653 /* where = 0 - do edit file menu for mc */
654 /* where = 1 - do edit file menu for mcedit */
655 static void
656 menu_edit_cmd (int where)
658 char *buffer;
659 char *menufile;
660 int dir = 0;
662 dir = query_dialog (
663 _(" Menu edit "),
664 _(" Which menu file do you want to edit? "),
665 0, geteuid() ? 2 : 3,
666 _("&Local"), _("&Home"), _("&System Wide")
669 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
671 switch (dir) {
672 case 0:
673 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
674 check_for_default (menufile, buffer);
675 break;
677 case 1:
678 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
679 check_for_default (menufile, buffer);
680 break;
682 case 2:
683 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
684 break;
686 default:
687 g_free (menufile);
688 return;
690 do_edit (buffer);
691 if (dir == 0)
692 chmod(buffer, 0600);
693 g_free (buffer);
694 g_free (menufile);
697 void quick_chdir_cmd (void)
699 char *target;
701 target = hotlist_cmd (LIST_HOTLIST);
702 if (!target)
703 return;
705 if (get_current_type () == view_tree)
706 tree_chdir (the_tree, target);
707 else
708 if (!do_cd (target, cd_exact))
709 message (1, MSG_ERROR, _("Cannot change directory") );
710 g_free (target);
713 /* edit file menu for mc */
714 void
715 edit_mc_menu_cmd (void)
717 menu_edit_cmd (0);
720 #ifdef USE_INTERNAL_EDIT
721 /* edit file menu for mcedit */
722 void
723 edit_user_menu_cmd (void)
725 menu_edit_cmd (1);
728 /* edit syntax file for mcedit */
729 void
730 edit_syntax_cmd (void)
732 char *buffer;
733 char *extdir;
734 int dir = 0;
736 if (geteuid () == 0) {
737 dir =
738 query_dialog (_("Syntax file edit"),
739 _(" Which syntax file you want to edit? "), 0, 2,
740 _("&User"), _("&System Wide"));
742 extdir = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
744 if (dir == 0) {
745 buffer = concat_dir_and_file (home_dir, SYNTAX_FILE);
746 check_for_default (extdir, buffer);
747 do_edit (buffer);
748 g_free (buffer);
749 } else if (dir == 1)
750 do_edit (extdir);
752 g_free (extdir);
754 #endif
756 #ifdef USE_VFS
757 void free_vfs_now (void)
759 vfs_expire (1);
762 void reselect_vfs (void)
764 char *target;
766 target = hotlist_cmd (LIST_VFSLIST);
767 if (!target)
768 return;
770 if (!do_cd (target, cd_exact))
771 message (1, MSG_ERROR, _("Cannot change directory") );
772 g_free (target);
774 #endif /* USE_VFS */
776 static int compare_files (char *name1, char *name2, off_t size)
778 int file1, file2;
779 int result = -1; /* Different by default */
781 file1 = open (name1, O_RDONLY);
782 if (file1 >= 0){
783 file2 = open (name2, O_RDONLY);
784 if (file2 >= 0){
785 #ifdef HAVE_MMAP
786 char *data1, *data2;
787 /* Ugly if jungle */
788 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
789 if (data1 != (char*) -1){
790 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
791 if (data2 != (char*) -1){
792 rotate_dash ();
793 result = memcmp (data1, data2, size);
794 munmap (data2, size);
796 munmap (data1, size);
798 #else
799 /* Don't have mmap() :( Even more ugly :) */
800 char buf1[BUFSIZ], buf2[BUFSIZ];
801 int n1, n2;
802 rotate_dash ();
805 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
806 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
807 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
808 result = (n1 != n2) || memcmp(buf1,buf2,n1);
809 #endif /* !HAVE_MMAP */
810 close (file2);
812 close (file1);
814 return result;
817 enum CompareMode {
818 compare_quick, compare_size_only, compare_thourough
821 static void
822 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
824 int i, j;
825 char *src_name, *dst_name;
827 /* No marks by default */
828 panel->marked = 0;
829 panel->total = 0;
830 panel->dirs_marked = 0;
832 /* Handle all files in the panel */
833 for (i = 0; i < panel->count; i++){
834 file_entry *source = &panel->dir.list[i];
836 /* Default: unmarked */
837 file_mark (panel, i, 0);
839 /* Skip directories */
840 if (S_ISDIR (source->buf.st_mode))
841 continue;
843 /* Search the corresponding entry from the other panel */
844 for (j = 0; j < other->count; j++){
845 if (strcmp (source->fname,
846 other->dir.list[j].fname) == 0)
847 break;
849 if (j >= other->count)
850 /* Not found -> mark */
851 do_file_mark (panel, i, 1);
852 else {
853 /* Found */
854 file_entry *target = &other->dir.list[j];
856 if (mode != compare_size_only){
857 /* Older version is not marked */
858 if (source->buf.st_mtime < target->buf.st_mtime)
859 continue;
862 /* Newer version with different size is marked */
863 if (source->buf.st_size != target->buf.st_size){
864 do_file_mark (panel, i, 1);
865 continue;
868 if (mode == compare_size_only)
869 continue;
871 if (mode == compare_quick){
872 /* Thorough compare off, compare only time stamps */
873 /* Mark newer version, don't mark version with the same date */
874 if (source->buf.st_mtime > target->buf.st_mtime){
875 do_file_mark (panel, i, 1);
877 continue;
880 /* Thorough compare on, do byte-by-byte comparison */
881 src_name = concat_dir_and_file (panel->cwd, source->fname);
882 dst_name = concat_dir_and_file (other->cwd, target->fname);
883 if (compare_files (src_name, dst_name, source->buf.st_size))
884 do_file_mark (panel, i, 1);
885 g_free (src_name);
886 g_free (dst_name);
888 } /* for (i ...) */
891 void
892 compare_dirs_cmd (void)
894 int choice;
895 enum CompareMode thorough_flag;
897 choice =
898 query_dialog (_(" Compare directories "),
899 _(" Select compare method: "), 0, 3, _("&Quick"),
900 _("&Size only"), _("&Thorough"), _("&Cancel"));
902 if (choice < 0 || choice > 2)
903 return;
904 else
905 thorough_flag = choice;
907 if (get_current_type () == view_listing
908 && get_other_type () == view_listing) {
909 compare_dir (cpanel, opanel, thorough_flag);
910 compare_dir (opanel, cpanel, thorough_flag);
911 paint_panel (cpanel);
912 paint_panel (opanel);
913 } else {
914 message (1, MSG_ERROR,
915 _(" Both panels should be in the "
916 "listing mode to use this command "));
920 void
921 history_cmd (void)
923 Listbox *listbox;
924 GList *current;
926 if (cmdline->need_push) {
927 if (push_history (cmdline, cmdline->buffer) == 2)
928 cmdline->need_push = 0;
930 if (!cmdline->history) {
931 message (1, MSG_ERROR, _(" The command history is empty "));
932 return;
934 current = g_list_first (cmdline->history);
935 listbox = create_listbox_window (60, 10, _(" Command history "),
936 "[Command Menu]");
937 while (current) {
938 LISTBOX_APPEND_TEXT (listbox, 0, (char *) current->data, current);
939 current = g_list_next(current);
941 run_dlg (listbox->dlg);
942 if (listbox->dlg->ret_value == B_CANCEL)
943 current = NULL;
944 else
945 current = listbox->list->current->data;
946 destroy_dlg (listbox->dlg);
947 g_free (listbox);
949 if (!current)
950 return;
951 cmdline->history = current;
952 assign_text (cmdline, (char *) current->data);
953 update_input (cmdline, 1);
956 void swap_cmd (void)
958 swap_panels ();
959 touchwin (stdscr);
960 repaint_screen ();
963 void
964 view_other_cmd (void)
966 static int message_flag = TRUE;
967 #ifdef HAVE_SUBSHELL_SUPPORT
968 char *new_dir = NULL;
969 char **new_dir_p;
970 #endif /* HAVE_SUBSHELL_SUPPORT */
972 if (!xterm_flag && !console_flag && !use_subshell){
973 if (message_flag)
974 message (1, MSG_ERROR, _(" Not an xterm or Linux console; \n"
975 " the panels cannot be toggled. "));
976 message_flag = FALSE;
977 } else {
978 channels_down ();
979 disable_mouse ();
980 if (clear_before_exec)
981 clr_scr ();
982 if (alternate_plus_minus)
983 numeric_keypad_mode ();
984 #ifndef HAVE_SLANG
985 /* With slang we don't want any of this, since there
986 * is no mc_raw_mode supported
988 reset_shell_mode ();
989 noecho ();
990 #endif /* !HAVE_SLANG */
991 keypad(stdscr, FALSE);
992 endwin ();
993 do_exit_ca_mode ();
994 mc_raw_mode ();
995 if (console_flag)
996 restore_console ();
998 #ifdef HAVE_SUBSHELL_SUPPORT
999 if (use_subshell){
1000 new_dir_p = vfs_current_is_local () ? &new_dir : NULL;
1001 if (invoke_subshell (NULL, VISIBLY, new_dir_p))
1002 quiet_quit_cmd(); /* User did `exit' or `logout': quit MC quietly */
1003 } else
1004 #endif /* HAVE_SUBSHELL_SUPPORT */
1006 if (output_starts_shell){
1007 fprintf (stderr,
1008 _("Type `exit' to return to the Midnight Commander"));
1009 fprintf (stderr, "\n\r\n\r");
1011 my_system (EXECUTE_AS_SHELL, shell, NULL);
1012 } else
1013 get_key_code (0);
1015 if (console_flag)
1016 handle_console (CONSOLE_SAVE);
1018 do_enter_ca_mode ();
1020 reset_prog_mode ();
1021 keypad(stdscr, TRUE);
1023 /* Prevent screen flash when user did 'exit' or 'logout' within
1024 subshell */
1025 if (quit)
1026 return;
1028 enable_mouse ();
1029 channels_up ();
1030 if (alternate_plus_minus)
1031 application_keypad_mode ();
1033 #ifdef HAVE_SUBSHELL_SUPPORT
1034 if (use_subshell){
1035 load_prompt (0, 0);
1036 if (new_dir)
1037 do_possible_cd (new_dir);
1038 if (console_flag && output_lines)
1039 show_console_contents (output_start_y,
1040 LINES-keybar_visible-output_lines-1,
1041 LINES-keybar_visible-1);
1043 #endif /* HAVE_SUBSHELL_SUPPORT */
1044 touchwin (stdscr);
1046 repaint_screen ();
1047 update_xterm_title_path ();
1051 #ifndef NATIVE_WIN32
1052 static void
1053 do_link (int symbolic_link, char *fname)
1055 char *dest, *src;
1057 if (!symbolic_link) {
1058 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
1059 dest = input_expand_dialog (_(" Link "), src, "");
1060 g_free (src);
1061 if (!dest)
1062 return;
1063 if (!*dest) {
1064 g_free (dest);
1065 return;
1067 save_cwds_stat ();
1068 if (-1 == mc_link (fname, dest))
1069 message (1, MSG_ERROR, _(" link: %s "),
1070 unix_error_string (errno));
1071 } else {
1072 char *s;
1073 char *d;
1075 /* suggest the full path for symlink */
1076 s = concat_dir_and_file (cpanel->cwd, fname);
1078 if (get_other_type () == view_listing) {
1079 d = concat_dir_and_file (opanel->cwd, fname);
1080 } else {
1081 d = g_strdup (fname);
1084 symlink_dialog (s, d, &dest, &src);
1085 g_free (d);
1086 g_free (s);
1088 if (!dest || !*dest || !src || !*src) {
1089 if (src)
1090 g_free (src);
1091 if (dest)
1092 g_free (dest);
1093 return;
1095 save_cwds_stat ();
1096 if (-1 == mc_symlink (dest, src))
1097 message (1, MSG_ERROR, _(" symlink: %s "),
1098 unix_error_string (errno));
1099 g_free (src);
1101 g_free (dest);
1102 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1103 repaint_screen ();
1106 void link_cmd (void)
1108 do_link (0, selection (cpanel)->fname);
1111 void symlink_cmd (void)
1113 char *filename = NULL;
1114 filename = selection (cpanel)->fname;
1116 if (filename) {
1117 do_link (1, filename);
1121 void edit_symlink_cmd (void)
1123 if (S_ISLNK (selection (cpanel)->buf.st_mode)) {
1124 char buffer [MC_MAXPATHLEN];
1125 char *p = NULL;
1126 int i;
1127 char *dest, *q;
1129 p = selection (cpanel)->fname;
1131 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
1133 i = readlink (p, buffer, MC_MAXPATHLEN);
1134 if (i > 0) {
1135 buffer [i] = 0;
1136 dest = input_expand_dialog (_(" Edit symlink "), q, buffer);
1137 if (dest) {
1138 if (*dest && strcmp (buffer, dest)) {
1139 save_cwds_stat ();
1140 if (-1 == mc_unlink (p)){
1141 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1142 p, unix_error_string (errno));
1143 } else {
1144 if (-1 == mc_symlink (dest, p))
1145 message (1, MSG_ERROR, _(" edit symlink: %s "),
1146 unix_error_string (errno));
1148 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1149 repaint_screen ();
1151 g_free (dest);
1154 g_free (q);
1155 } else {
1156 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1157 selection (cpanel)->fname);
1160 #endif /* !NATIVE_WIN32 */
1162 void help_cmd (void)
1164 interactive_display (NULL, "[main]");
1167 /* partly taken from dcigettext.c, returns "" for default locale */
1168 /* value should be freed by calling function g_free() */
1169 char *guess_message_value (void)
1171 static const char * const var[] = {
1172 /* The highest priority value is the `LANGUAGE' environment
1173 variable. This is a GNU extension. */
1174 "LANGUAGE",
1175 /* Setting of LC_ALL overwrites all other. */
1176 "LC_ALL",
1177 /* Next comes the name of the desired category. */
1178 "LC_MESSAGES",
1179 /* Last possibility is the LANG environment variable. */
1180 "LANG",
1181 /* NULL exit loops */
1182 NULL
1185 unsigned i = 0;
1186 char *locale = NULL;
1188 while (var[i] != NULL) {
1189 locale = getenv (var[i]);
1190 if (locale != NULL && locale[0] != '\0')
1191 break;
1192 i++;
1195 if (locale == NULL)
1196 locale = "";
1198 return g_strdup (locale);
1202 * Return a random hint. If force is not 0, ignore the timeout.
1204 char *
1205 get_random_hint (int force)
1207 char *data, *result, *eol;
1208 int len;
1209 int start;
1211 /* Do not change hints more often than one minute */
1213 #ifdef SCO_FLAVOR
1214 static time_t last;
1215 time_t now;
1217 time (&now);
1218 if (!force && (now - last) < 60)
1219 return g_strdup ("");
1220 last = now;
1221 #else
1222 static int last_sec;
1223 static struct timeval tv;
1225 gettimeofday (&tv, NULL);
1226 if (!force && !(tv.tv_sec > last_sec + 60))
1227 return g_strdup ("");
1228 last_sec = tv.tv_sec;
1229 #endif /* !SCO_FLAVOR */
1231 data = load_mc_home_file (MC_HINT, NULL);
1232 if (!data)
1233 return 0;
1235 #ifdef SCO_FLAVOR
1236 srand ((short) now);
1237 #else
1238 srand (tv.tv_sec);
1239 #endif /* !SCO_FLAVOR */
1240 /* get a random entry */
1241 len = strlen (data);
1242 start = rand () % len;
1244 for (;start; start--){
1245 if (data [start] == '\n'){
1246 start++;
1247 break;
1250 eol = strchr (&data [start], '\n');
1251 if (eol)
1252 *eol = 0;
1253 result = g_strdup (&data [start]);
1254 g_free (data);
1255 return result;
1258 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1259 static void
1260 nice_cd (char *text, char *xtext, char *help, char *prefix, int to_home)
1262 char *machine;
1263 char *cd_path;
1265 if (!SELECTED_IS_PANEL)
1266 return;
1268 machine = input_dialog_help (text,
1269 xtext,
1270 help, "");
1271 if (!machine)
1272 return;
1274 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1275 ugly as hell and leads to problems in vfs layer */
1277 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1278 cd_path = g_strconcat (machine, to_home ? "/~/" : NULL, NULL);
1279 else
1280 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : NULL, NULL);
1282 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1283 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1284 else
1285 message (1, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1286 g_free (cd_path);
1287 g_free (machine);
1289 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1292 #ifdef USE_NETCODE
1294 static char *machine_str = N_(" Enter machine name (F1 for details): ");
1296 #ifdef WITH_MCFS
1297 void netlink_cmd (void)
1299 nice_cd (_(" Link to a remote machine "), _(machine_str),
1300 "[Network File System]", "/#mc:", 1);
1302 #endif /* WITH_MCFS */
1304 void ftplink_cmd (void)
1306 nice_cd (_(" FTP to machine "), _(machine_str),
1307 "[FTP File System]", "/#ftp:", 1);
1310 void fishlink_cmd (void)
1312 nice_cd (_(" Shell link to machine "), _(machine_str),
1313 "[FIle transfer over SHell filesystem]", "/#sh:", 1);
1316 #ifdef WITH_SMBFS
1317 void smblink_cmd (void)
1319 nice_cd (_(" SMB link to machine "), _(machine_str),
1320 "[SMB File System]", "/#smb:", 0);
1322 #endif /* WITH_SMBFS */
1324 #ifdef HAVE_SETSOCKOPT
1325 void source_routing (void)
1327 char *source;
1328 struct hostent *hp;
1330 source = input_dialog (_(" Socket source routing setup "),
1331 _(" Enter host name to use as a source routing hop: "),
1332 "");
1333 if (!source)
1334 return;
1336 hp = gethostbyname (source);
1337 g_free (source);
1338 if (!hp){
1339 message (1, _(" Host name "), _(" Error while looking up IP address "));
1340 return;
1342 source_route = *((int *)hp->h_addr);
1344 #endif /* HAVE_SETSOCKOPT */
1345 #endif /* USE_NETCODE */
1347 #ifdef USE_EXT2FSLIB
1348 void undelete_cmd (void)
1350 nice_cd (_(" Undelete files on an ext2 file system "),
1351 _(" Enter device (without /dev/) to undelete\n "
1352 " files on: (F1 for details)"),
1353 "[Undelete File System]", "/#undel:", 0);
1355 #endif /* USE_EXT2FSLIB */
1357 void quick_cd_cmd (void)
1359 char *p = cd_dialog ();
1361 if (p && *p) {
1362 char *q = g_strconcat ("cd ", p, NULL);
1364 do_cd_command (q);
1365 g_free (q);
1367 if (p)
1368 g_free (p);
1371 void
1372 dirsizes_cmd (void)
1374 WPanel *panel = cpanel;
1375 int i;
1376 off_t marked;
1377 double total;
1379 for (i = 0; i < panel->count; i++)
1380 if (S_ISDIR (panel->dir.list [i].buf.st_mode) &&
1381 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1382 !panel->dirs_marked) &&
1383 strcmp (panel->dir.list [i].fname, "..") != 0) {
1384 total = 0.0l;
1385 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1386 panel->dir.list [i].buf.st_size = (off_t) total;
1387 panel->dir.list [i].f.dir_size_computed = 1;
1390 recalculate_panel_summary (panel);
1391 paint_panel (panel);
1394 void
1395 save_setup_cmd (void)
1397 char *str;
1399 save_setup ();
1400 sync_profiles ();
1401 str = g_strconcat ( _(" Setup saved to ~/"), PROFILE_NAME, NULL);
1403 message (0, _(" Setup "), str);
1404 g_free (str);
1407 static void
1408 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1410 p->user_mini_status = use_msformat;
1411 p->list_type = view_type;
1413 if (view_type == list_user || use_msformat){
1414 g_free (p->user_format);
1415 p->user_format = user;
1417 g_free (p->user_status_format [view_type]);
1418 p->user_status_format [view_type] = status;
1420 set_panel_formats (p);
1422 else {
1423 g_free (user);
1424 g_free (status);
1427 set_panel_formats (p);
1428 paint_panel (p);
1430 do_refresh ();
1433 void
1434 info_cmd_no_menu (void)
1436 if (get_display_type (0) == view_info)
1437 set_display_type (0, view_listing);
1438 else if (get_display_type (1) == view_info)
1439 set_display_type (1, view_listing);
1440 else
1441 set_display_type (cpanel == left_panel ? 1 : 0, view_info);
1444 void
1445 quick_cmd_no_menu (void)
1447 if (get_display_type (0) == view_quick)
1448 set_display_type (0, view_listing);
1449 else if (get_display_type (1) == view_quick)
1450 set_display_type (1, view_listing);
1451 else
1452 set_display_type (cpanel == left_panel ? 1 : 0, view_quick);
1455 static void
1456 switch_to_listing (int panel_index)
1458 if (get_display_type (panel_index) != view_listing)
1459 set_display_type (panel_index, view_listing);
1462 void
1463 listing_cmd (void)
1465 int view_type, use_msformat;
1466 char *user, *status;
1467 WPanel *p;
1468 int display_type;
1470 display_type = get_display_type (MENU_PANEL_IDX);
1471 if (display_type == view_listing)
1472 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1473 else
1474 p = 0;
1476 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1478 if (view_type == -1)
1479 return;
1481 switch_to_listing (MENU_PANEL_IDX);
1483 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1485 configure_panel_listing (p, view_type, use_msformat, user, status);
1488 void
1489 tree_cmd (void)
1491 set_display_type (MENU_PANEL_IDX, view_tree);
1494 void
1495 info_cmd (void)
1497 set_display_type (MENU_PANEL_IDX, view_info);
1500 void
1501 quick_view_cmd (void)
1503 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == cpanel)
1504 change_panel ();
1505 set_display_type (MENU_PANEL_IDX, view_quick);
1508 /* Handle the tree internal listing modes switching */
1509 static int
1510 set_basic_panel_listing_to (int panel_index, int listing_mode)
1512 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1514 switch_to_listing (panel_index);
1515 p->list_type = listing_mode;
1516 if (set_panel_formats (p))
1517 return 0;
1519 paint_panel (p);
1520 do_refresh ();
1521 return 1;
1524 void
1525 toggle_listing_cmd (void)
1527 int current = get_current_index ();
1528 WPanel *p = (WPanel *) get_panel_widget (current);
1529 int list_mode = p->list_type;
1530 int m;
1532 switch (list_mode){
1533 case list_full:
1534 case list_brief:
1535 m = list_long;
1536 break;
1537 case list_long:
1538 m = list_user;
1539 break;
1540 default:
1541 m = list_full;
1543 if (set_basic_panel_listing_to (current, m))
1544 return;
1545 set_basic_panel_listing_to (current, list_full);