updated the .TP cleanup for coherency in the key description pages.
[midnight-commander.git] / src / cmd.c
blob6ac597e86e83ff190d5f745d54cd9a9dc3d770ca
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 "tty.h"
43 #include "dir.h"
44 #include "panel.h"
45 #include "cmd.h" /* Our definitions */
46 #include "view.h" /* view() */
47 #include "dialog.h" /* query_dialog, message */
48 #include "file.h" /* the file operations */
49 #include "fileopctx.h"
50 #include "find.h" /* do_find */
51 #include "hotlist.h"
52 #include "tree.h"
53 #include "subshell.h" /* use_subshell */
54 #include "cons.saver.h"
55 #include "dlg.h" /* required by wtools.h */
56 #include "widget.h" /* required by wtools.h */
57 #include "wtools.h" /* listbox */
58 #include "command.h" /* for cmdline */
59 #include "win.h" /* do_exit_ca_mode */
60 #include "layout.h" /* get_current/other_type */
61 #include "ext.h" /* regex_command */
62 #include "key.h" /* get_key_code */
63 #include "help.h" /* interactive_display */
64 #include "boxes.h" /* cd_dialog */
65 #include "color.h"
66 #include "user.h"
67 #include "setup.h"
68 #include "profile.h"
70 #include "vfs/vfs.h"
71 #define WANT_WIDGETS
72 #include "main.h" /* global variables, global functions */
73 #ifndef MAP_FILE
74 # define MAP_FILE 0
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;
86 /* Ugly hack in order to distinguish between left and right panel in menubar */
87 int is_right;
88 #define MENU_PANEL_IDX (is_right ? 1 : 0)
91 /* This is used since the parameter panel on some of the commands */
92 /* defined in this file may receive a 0 parameter if they are invoked */
93 /* The drop down menu */
95 static WPanel *
96 get_a_panel (WPanel *panel)
98 if (panel)
99 return panel;
100 if (get_current_type () == view_listing){
101 return cpanel;
102 } else
103 return other_panel;
106 /* view_file (filename, normal, internal)
108 * Inputs:
109 * filename: The file name to view
110 * plain_view: If set does not do any fancy pre-processing (no filtering) and
111 * always invokes the internal viewer.
112 * internal: If set uses the internal viewer, otherwise an external viewer.
115 view_file_at_line (char *filename, int plain_view, int internal,
116 int start_line)
118 static char *viewer = 0;
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 altered_hex_mode = 0;
128 altered_nroff_flag = 0;
129 altered_magic_flag = 0;
130 if (default_hex_mode)
131 changed_hex_mode = 1;
132 if (default_nroff_flag)
133 changed_nroff_flag = 1;
134 if (default_magic_flag)
135 changed_magic_flag = 1;
136 default_hex_mode = 0;
137 default_nroff_flag = 0;
138 default_magic_flag = 0;
139 view (0, filename, &move_dir, start_line);
140 if (changed_hex_mode && !altered_hex_mode)
141 default_hex_mode = 1;
142 if (changed_nroff_flag && !altered_nroff_flag)
143 default_nroff_flag = 1;
144 if (changed_magic_flag && !altered_magic_flag)
145 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 view (0, filename, &move_dir, start_line);
160 repaint_screen ();
162 } else {
163 char *localcopy;
165 if (!viewer) {
166 viewer = getenv ("PAGER");
167 if (!viewer)
168 viewer = "view";
170 /* The file may be a non local file, get a copy */
171 if (!vfs_file_is_local (filename)) {
172 localcopy = mc_getlocalcopy (filename);
173 if (localcopy == NULL) {
174 message (1, MSG_ERROR,
175 _(" Can not fetch a local copy of %s "),
176 filename);
177 return 0;
179 execute_internal (viewer, localcopy);
180 mc_ungetlocalcopy (filename, localcopy, 0);
181 } else
182 execute_internal (viewer, filename);
184 return move_dir;
188 view_file (char *filename, int plain_view, int internal)
190 return view_file_at_line (filename, plain_view, internal, 0);
193 /* scan_for_file (panel, idx, direction)
195 * Inputs:
196 * panel: pointer to the panel on which we operate
197 * idx: starting file.
198 * direction: 1, or -1
200 static int scan_for_file (WPanel *panel, int idx, int direction)
202 int i = idx + direction;
204 while (i != idx){
205 if (i < 0)
206 i = panel->count - 1;
207 if (i == panel->count)
208 i = 0;
209 if (!S_ISDIR (panel->dir.list [i].buf.st_mode))
210 return i;
211 i += direction;
213 return i;
216 /* do_view: Invoked as the F3/F13 key. */
217 static void
218 do_view_cmd (WPanel *panel, int normal)
220 int dir, file_idx;
221 panel = get_a_panel (panel);
223 /* Directories are viewed by changing to them */
224 if (S_ISDIR (selection (panel)->buf.st_mode)
225 || link_isdir (selection (panel))) {
226 if (confirm_view_dir && (panel->marked || panel->dirs_marked)) {
227 if (query_dialog
228 (_(" Confirmation "), _("Files tagged, want to cd?"), 0, 2,
229 _("&Yes"), _("&No")) != 0) {
230 return;
233 if (!do_cd (selection (panel)->fname, cd_exact))
234 message (1, MSG_ERROR, _("Could not change directory"));
236 return;
240 file_idx = panel->selected;
241 while (1) {
242 char *filename;
244 filename = panel->dir.list[file_idx].fname;
246 dir = view_file (filename, normal, use_internal_view);
247 if (dir == 0)
248 break;
249 file_idx = scan_for_file (panel, file_idx, dir);
253 void view_cmd (WPanel *panel)
255 do_view_cmd (panel, 0);
258 void view_file_cmd (WPanel *panel)
260 char *filename;
262 panel = get_a_panel (panel);
263 filename = input_dialog (_(" View file "), _(" Filename:"), selection (panel)->fname);
264 if (!filename) return;
266 view_file (filename, 0, use_internal_view);
267 g_free (filename);
270 void view_simple_cmd (WPanel *panel)
272 do_view_cmd (panel, 1);
275 void filtered_view_cmd (WPanel *panel)
277 char *command;
279 panel = get_a_panel (panel);
280 command = input_dialog (_(" Filtered view "), _(" Filter command and arguments:"),
281 selection (panel)->fname);
282 if (!command)
283 return;
285 view (command, "", 0, 0);
287 g_free (command);
290 void filtered_view_cmd_cpanel (void)
292 filtered_view_cmd (cpanel);
295 void do_edit_at_line (const char *what, int start_line)
297 static char *editor = 0;
299 #ifdef USE_INTERNAL_EDIT
300 if (use_internal_edit){
301 edit (what, start_line);
302 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
303 repaint_screen ();
304 return;
306 #endif /* USE_INTERNAL_EDIT */
307 if (!editor){
308 editor = getenv ("EDITOR");
309 if (!editor)
310 editor = get_default_editor ();
312 execute_internal (editor, what);
313 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
314 repaint_screen ();
317 void
318 do_edit (const char *what)
320 do_edit_at_line (what, 1);
323 void
324 edit_cmd (WPanel *panel)
326 panel = get_a_panel (panel);
327 if (regex_command (selection (panel)->fname, "Edit", 0) == 0)
328 do_edit (selection (panel)->fname);
331 void
332 edit_cmd_new (WPanel *panel)
334 do_edit ("");
337 /* Invoked by F5. Copy, default to the other panel. */
338 void
339 copy_cmd (void)
341 save_cwds_stat ();
342 if (panel_operate (cpanel, OP_COPY, NULL, TRUE)) {
343 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
344 repaint_screen ();
348 /* Invoked by F6. Move/rename, default to the other panel. */
349 void ren_cmd (void)
351 save_cwds_stat ();
352 if (panel_operate (cpanel, OP_MOVE, NULL, TRUE)){
353 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
354 repaint_screen ();
358 /* Invoked by F15. Copy, default to the same panel. */
359 void copy_cmd_local (void)
361 save_cwds_stat ();
362 if (panel_operate (cpanel, OP_COPY, selection (cpanel)->fname, TRUE)){
363 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
364 repaint_screen ();
368 /* Invoked by F16. Move/rename, default to the same panel. */
369 void ren_cmd_local (void)
371 save_cwds_stat ();
372 if (panel_operate (cpanel, OP_MOVE, selection (cpanel)->fname, TRUE)){
373 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
374 repaint_screen ();
378 void mkdir_cmd (WPanel *panel)
380 char *tempdir;
381 char *dir;
383 panel = get_a_panel (panel);
384 dir = input_expand_dialog (_("Create a new Directory"), _(" Enter directory name:") , "");
386 if (!dir)
387 return;
389 if (dir [0] && (dir [0] == '/' || dir [0] == '~'))
390 tempdir = g_strdup (dir);
391 else
392 tempdir = concat_dir_and_file (panel->cwd, dir);
393 g_free (dir);
395 save_cwds_stat ();
396 if (my_mkdir (tempdir, 0777) == 0){
397 update_panels (UP_OPTIMIZE, tempdir);
398 repaint_screen ();
399 select_item (cpanel);
400 g_free (tempdir);
401 return;
403 g_free (tempdir);
404 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
407 void delete_cmd (void)
409 save_cwds_stat ();
411 if (panel_operate (cpanel, OP_DELETE, NULL, TRUE)){
412 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
413 repaint_screen ();
417 void find_cmd (void)
419 do_find ();
422 void
423 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
425 if (p->filter){
426 g_free (p->filter);
427 p->filter = 0;
429 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
430 p->filter = allocated_filter_string;
431 else
432 g_free (allocated_filter_string);
433 reread_cmd ();
436 /* Set a given panel filter expression */
437 void set_panel_filter (WPanel *p)
439 char *reg_exp;
440 char *x;
442 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
444 reg_exp = input_dialog_help (_(" Filter "),
445 _(" Set expression for filtering filenames"),
446 "[Filter...]", x);
447 if (!reg_exp)
448 return;
449 set_panel_filter_to (p, reg_exp);
452 /* Invoked from the left/right menus */
453 void filter_cmd (void)
455 WPanel *p;
457 if (!SELECTED_IS_PANEL)
458 return;
460 p = MENU_PANEL;
461 set_panel_filter (p);
464 void reread_cmd (void)
466 int flag;
468 if (get_current_type () == view_listing &&
469 get_other_type () == view_listing)
470 flag = strcmp (cpanel->cwd, opanel->cwd) ? UP_ONLY_CURRENT : 0;
471 else
472 flag = UP_ONLY_CURRENT;
474 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
475 repaint_screen ();
478 /* Panel sorting related routines */
479 void do_re_sort (WPanel *panel)
481 panel = get_a_panel (panel);
482 panel_re_sort (panel);
485 void reverse_selection_cmd_panel (WPanel *panel)
487 file_entry *file;
488 int i;
490 for (i = 0; i < panel->count; i++){
491 file = &panel->dir.list [i];
492 if (S_ISDIR (file->buf.st_mode))
493 continue;
494 do_file_mark (panel, i, !file->f.marked);
496 paint_panel (panel);
499 void reverse_selection_cmd (void)
501 reverse_selection_cmd_panel (cpanel);
504 void select_cmd_panel (WPanel *panel)
506 char *reg_exp, *reg_exp_t;
507 int i;
508 int c;
509 int dirflag = 0;
511 reg_exp = input_dialog (_(" Select "), "", easy_patterns ? "*" : ".");
512 if (!reg_exp)
513 return;
515 reg_exp_t = reg_exp;
517 /* Check if they specified a directory */
518 if (*reg_exp_t == PATH_SEP){
519 dirflag = 1;
520 reg_exp_t++;
522 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
523 dirflag = 1;
524 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
527 for (i = 0; i < panel->count; i++){
528 if (!strcmp (panel->dir.list [i].fname, ".."))
529 continue;
530 if (S_ISDIR (panel->dir.list [i].buf.st_mode)){
531 if (!dirflag)
532 continue;
533 } else {
534 if (dirflag)
535 continue;
537 c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file);
538 if (c == -1){
539 message (1, MSG_ERROR, _(" Malformed regular expression "));
540 g_free (reg_exp);
541 return;
543 if (c){
544 do_file_mark (panel, i, 1);
547 paint_panel (panel);
548 g_free (reg_exp);
551 void select_cmd (void)
553 select_cmd_panel (cpanel);
556 void unselect_cmd_panel (WPanel *panel)
558 char *reg_exp, *reg_exp_t;
559 int i;
560 int c;
561 int dirflag = 0;
563 reg_exp = input_dialog (_(" Unselect "),"", easy_patterns ? "*" : ".");
564 if (!reg_exp)
565 return;
567 reg_exp_t = reg_exp;
569 /* Check if they specified directory matching */
570 if (*reg_exp_t == PATH_SEP){
571 dirflag = 1;
572 reg_exp_t ++;
574 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
575 dirflag = 1;
576 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
578 for (i = 0; i < panel->count; i++){
579 if (!strcmp (panel->dir.list [i].fname, ".."))
580 continue;
581 if (S_ISDIR (panel->dir.list [i].buf.st_mode)){
582 if (!dirflag)
583 continue;
584 } else {
585 if (dirflag)
586 continue;
588 c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file);
589 if (c == -1){
590 message (1, MSG_ERROR, _(" Malformed regular expression "));
591 g_free (reg_exp);
592 return;
594 if (c){
595 do_file_mark (panel, i, 0);
598 paint_panel (panel);
599 g_free (reg_exp);
602 void unselect_cmd (void)
604 unselect_cmd_panel (cpanel);
607 /* Check if the file exists */
608 /* If not copy the default */
609 int check_for_default(char *default_file, char *file)
611 struct stat s;
612 off_t count = 0;
613 double bytes = 0;
614 FileOpContext *ctx;
616 if (mc_stat (file, &s)){
617 if (mc_stat (default_file, &s)){
618 return -1;
620 ctx = file_op_context_new ();
621 file_op_context_create_ui (ctx, OP_COPY, 0);
622 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
623 file_op_context_destroy (ctx);
625 return 0;
628 void ext_cmd (void)
630 char *buffer;
631 char *extdir;
632 int dir;
634 dir = 0;
635 if (geteuid () == 0){
636 dir = query_dialog (_("Extension file edit"),
637 _(" Which extension file you want to edit? "), 0, 2,
638 _("&User"), _("&System Wide"));
640 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
642 if (dir == 0){
643 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
644 check_for_default (extdir, buffer);
645 do_edit (buffer);
646 g_free (buffer);
647 } else if (dir == 1)
648 do_edit (extdir);
650 g_free (extdir);
651 flush_extension_file ();
654 #ifdef USE_INTERNAL_EDIT
655 void edit_syntax_cmd (void)
657 char *buffer;
658 char *extdir;
659 int dir = 0;
661 if (geteuid () == 0){
662 dir = query_dialog (_("Syntax file edit"),
663 _(" Which syntax file you want to edit? "), 0, 2,
664 _("&User"), _("&System Wide"));
666 extdir = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
668 if (dir == 0){
669 buffer = concat_dir_and_file (home_dir, SYNTAX_FILE);
670 check_for_default (extdir, buffer);
671 do_edit (buffer);
672 g_free (buffer);
673 } else if (dir == 1)
674 do_edit (extdir);
676 g_free (extdir);
678 #endif
680 /* where = 0 - do edit a file menu for mc */
681 /* where = 1 - do edit a file menu for cool edit */
682 void menu_edit_cmd (int where)
684 char *buffer;
685 char *menufile;
686 int dir = 0;
688 dir = query_dialog (
689 _(" Menu edit "),
690 _(" Which menu file will you edit ? "),
691 0, geteuid() ? 2 : 3,
692 _("&Local"), _("&Home"), _("&System Wide")
695 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
697 switch (dir) {
698 case 0:
699 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
700 check_for_default (menufile, buffer);
701 break;
703 case 1:
704 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
705 check_for_default (menufile, buffer);
706 break;
708 case 2:
709 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
710 break;
712 default:
713 g_free (menufile);
714 return;
716 do_edit (buffer);
717 if (dir == 0)
718 chmod(buffer, 0600);
719 g_free (buffer);
720 g_free (menufile);
723 void quick_chdir_cmd (void)
725 char *target;
727 target = hotlist_cmd (LIST_HOTLIST);
728 if (!target)
729 return;
731 if (get_current_type () == view_tree)
732 tree_chdir (the_tree, target);
733 else
734 if (!do_cd (target, cd_exact))
735 message (1, MSG_ERROR, _("Could not change directory") );
736 g_free (target);
739 #ifdef USE_VFS
740 void free_vfs_now (void)
742 vfs_expire (1);
745 void reselect_vfs (void)
747 char *target;
749 target = hotlist_cmd (LIST_VFSLIST);
750 if (!target)
751 return;
753 if (!do_cd (target, cd_exact))
754 message (1, MSG_ERROR, _("Could not change directory") );
755 g_free (target);
757 #endif /* USE_VFS */
759 static int compare_files (char *name1, char *name2, off_t size)
761 int file1, file2;
762 int result = -1; /* Different by default */
764 file1 = open (name1, O_RDONLY);
765 if (file1 >= 0){
766 file2 = open (name2, O_RDONLY);
767 if (file2 >= 0){
768 #ifdef HAVE_MMAP
769 char *data1, *data2;
770 /* Ugly if jungle */
771 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
772 if (data1 != (char*) -1){
773 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
774 if (data2 != (char*) -1){
775 rotate_dash ();
776 result = memcmp (data1, data2, size);
777 munmap (data2, size);
779 munmap (data1, size);
781 #else
782 /* Don't have mmap() :( Even more ugly :) */
783 char buf1[BUFSIZ], buf2[BUFSIZ];
784 int n1, n2;
785 rotate_dash ();
788 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
789 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
790 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
791 result = (n1 != n2) || memcmp(buf1,buf2,n1);
792 #endif /* !HAVE_MMAP */
793 close (file2);
795 close (file1);
797 return result;
800 enum CompareMode {
801 compare_quick, compare_size_only, compare_thourough
804 static void
805 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
807 int i, j;
808 char *src_name, *dst_name;
810 panel = get_a_panel (panel);
812 /* No marks by default */
813 panel->marked = 0;
814 panel->total = 0;
815 panel->dirs_marked = 0;
817 /* Handle all files in the panel */
818 for (i = 0; i < panel->count; i++){
819 file_entry *source = &panel->dir.list[i];
821 /* Default: unmarked */
822 file_mark (panel, i, 0);
824 /* Skip directories */
825 if (S_ISDIR (source->buf.st_mode))
826 continue;
828 /* Search the corresponding entry from the other panel */
829 for (j = 0; j < other->count; j++){
830 if (strcmp (source->fname,
831 other->dir.list[j].fname) == 0)
832 break;
834 if (j >= other->count)
835 /* Not found -> mark */
836 do_file_mark (panel, i, 1);
837 else {
838 /* Found */
839 file_entry *target = &other->dir.list[j];
841 if (mode != compare_size_only){
842 /* Older version is not marked */
843 if (source->buf.st_mtime < target->buf.st_mtime)
844 continue;
847 /* Newer version with different size is marked */
848 if (source->buf.st_size != target->buf.st_size){
849 do_file_mark (panel, i, 1);
850 continue;
853 if (mode == compare_size_only)
854 continue;
856 if (mode == compare_quick){
857 /* Thorough compare off, compare only time stamps */
858 /* Mark newer version, don't mark version with the same date */
859 if (source->buf.st_mtime > target->buf.st_mtime){
860 do_file_mark (panel, i, 1);
862 continue;
865 /* Thorough compare on, do byte-by-byte comparison */
866 src_name = concat_dir_and_file (panel->cwd, source->fname);
867 dst_name = concat_dir_and_file (other->cwd, target->fname);
868 if (compare_files (src_name, dst_name, source->buf.st_size))
869 do_file_mark (panel, i, 1);
870 g_free (src_name);
871 g_free (dst_name);
873 } /* for (i ...) */
876 void compare_dirs_cmd (void)
878 enum CompareMode thorough_flag = compare_quick;
880 thorough_flag = query_dialog (_(" Compare directories "), _(" Select compare method: "),
881 0, 3, _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
882 if (thorough_flag < 0 || thorough_flag > 2)
883 return;
884 if (get_current_type () == view_listing &&
885 get_other_type () == view_listing){
886 compare_dir (cpanel, opanel, thorough_flag);
887 compare_dir (opanel, cpanel, thorough_flag);
888 paint_panel (cpanel);
889 paint_panel (opanel);
890 } else {
891 message (1, MSG_ERROR, _(" Both panels should be on the listing view mode to use this command "));
895 void
896 history_cmd (void)
898 Listbox *listbox;
899 Hist *current;
901 if (cmdline->need_push) {
902 if (push_history (cmdline, cmdline->buffer) == 2)
903 cmdline->need_push = 0;
905 if (!cmdline->history) {
906 message (1, MSG_ERROR, _(" The command history is empty "));
907 return;
909 current = cmdline->history;
910 while (current->prev)
911 current = current->prev;
912 listbox = create_listbox_window (60, 10, _(" Command history "),
913 "[Command Menu]");
914 while (current) {
915 LISTBOX_APPEND_TEXT (listbox, 0, current->text, current);
916 current = current->next;
918 run_dlg (listbox->dlg);
919 if (listbox->dlg->ret_value == B_CANCEL)
920 current = NULL;
921 else
922 current = listbox->list->current->data;
923 destroy_dlg (listbox->dlg);
924 g_free (listbox);
926 if (!current)
927 return;
928 cmdline->history = current;
929 assign_text (cmdline, cmdline->history->text);
930 update_input (cmdline, 1);
933 void swap_cmd (void)
935 swap_panels ();
936 touchwin (stdscr);
937 repaint_screen ();
940 void
941 view_other_cmd (void)
943 static int message_flag = TRUE;
944 #ifdef HAVE_SUBSHELL_SUPPORT
945 char *new_dir = NULL;
946 char **new_dir_p;
947 #endif /* HAVE_SUBSHELL_SUPPORT */
949 if (!xterm_flag && !console_flag && !use_subshell){
950 if (message_flag)
951 message (1, MSG_ERROR, _(" Not an xterm or Linux console; \n"
952 " the panels cannot be toggled. "));
953 message_flag = FALSE;
954 } else {
955 channels_down ();
956 disable_mouse ();
957 if (clear_before_exec)
958 clr_scr ();
959 if (alternate_plus_minus)
960 numeric_keypad_mode ();
961 #ifndef HAVE_SLANG
962 /* With slang we don't want any of this, since there
963 * is no mc_raw_mode supported
965 reset_shell_mode ();
966 noecho ();
967 #endif /* !HAVE_SLANG */
968 keypad(stdscr, FALSE);
969 endwin ();
970 do_exit_ca_mode ();
971 mc_raw_mode ();
972 if (console_flag)
973 restore_console ();
975 #ifdef HAVE_SUBSHELL_SUPPORT
976 if (use_subshell){
977 new_dir_p = vfs_current_is_local () ? &new_dir : NULL;
978 if (invoke_subshell (NULL, VISIBLY, new_dir_p))
979 quiet_quit_cmd(); /* User did `exit' or `logout': quit MC quietly */
980 } else
981 #endif /* HAVE_SUBSHELL_SUPPORT */
983 if (output_starts_shell){
984 fprintf (stderr,
985 _("Type `exit' to return to the Midnight Commander"));
986 fprintf (stderr, "\n\r\n\r");
988 my_system (EXECUTE_AS_SHELL, shell, NULL);
989 } else
990 get_key_code (0);
992 if (console_flag)
993 handle_console (CONSOLE_SAVE);
995 do_enter_ca_mode ();
997 reset_prog_mode ();
998 keypad(stdscr, TRUE);
1000 /* Prevent screen flash when user did 'exit' or 'logout' within
1001 subshell */
1002 if (quit)
1003 return;
1005 enable_mouse ();
1006 channels_up ();
1007 if (alternate_plus_minus)
1008 application_keypad_mode ();
1010 #ifdef HAVE_SUBSHELL_SUPPORT
1011 if (use_subshell){
1012 load_prompt (0, 0);
1013 if (new_dir)
1014 do_possible_cd (new_dir);
1015 if (console_flag && output_lines)
1016 show_console_contents (output_start_y,
1017 LINES-keybar_visible-output_lines-1,
1018 LINES-keybar_visible-1);
1020 #endif /* HAVE_SUBSHELL_SUPPORT */
1021 touchwin (stdscr);
1023 repaint_screen ();
1027 #ifndef NATIVE_WIN32
1028 static void
1029 do_link (int symbolic_link, char *fname)
1031 char *dest, *src;
1033 if (!symbolic_link) {
1034 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
1035 dest = input_expand_dialog (_(" Link "), src, "");
1036 g_free (src);
1037 if (!dest)
1038 return;
1039 if (!*dest) {
1040 g_free (dest);
1041 return;
1043 save_cwds_stat ();
1044 if (-1 == mc_link (fname, dest))
1045 message (1, MSG_ERROR, _(" link: %s "),
1046 unix_error_string (errno));
1047 } else {
1048 char *s;
1049 char *d;
1051 /* suggest the full path for symlink */
1052 s = concat_dir_and_file (cpanel->cwd, fname);
1054 if (get_other_type () == view_listing) {
1055 d = concat_dir_and_file (opanel->cwd, fname);
1056 } else {
1057 d = g_strdup (fname);
1060 symlink_dialog (s, d, &dest, &src);
1061 g_free (d);
1062 g_free (s);
1064 if (!dest || !*dest || !src || !*src) {
1065 if (src)
1066 g_free (src);
1067 if (dest)
1068 g_free (dest);
1069 return;
1071 save_cwds_stat ();
1072 if (-1 == mc_symlink (dest, src))
1073 message (1, MSG_ERROR, _(" symlink: %s "),
1074 unix_error_string (errno));
1075 g_free (src);
1077 g_free (dest);
1078 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1079 repaint_screen ();
1082 void link_cmd (void)
1084 do_link (0, selection (cpanel)->fname);
1087 void symlink_cmd (void)
1089 char *filename = NULL;
1090 filename = selection (cpanel)->fname;
1092 if (filename) {
1093 do_link (1, filename);
1097 void edit_symlink_cmd (void)
1099 if (S_ISLNK (selection (cpanel)->buf.st_mode)) {
1100 char buffer [MC_MAXPATHLEN];
1101 char *p = NULL;
1102 int i;
1103 char *dest, *q;
1105 p = selection (cpanel)->fname;
1107 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
1109 i = readlink (p, buffer, MC_MAXPATHLEN);
1110 if (i > 0) {
1111 buffer [i] = 0;
1112 dest = input_expand_dialog (_(" Edit symlink "), q, buffer);
1113 if (dest) {
1114 if (*dest && strcmp (buffer, dest)) {
1115 save_cwds_stat ();
1116 if (-1 == mc_unlink (p)){
1117 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1118 p, unix_error_string (errno));
1119 } else {
1120 if (-1 == mc_symlink (dest, p))
1121 message (1, MSG_ERROR, _(" edit symlink: %s "),
1122 unix_error_string (errno));
1124 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1125 repaint_screen ();
1127 g_free (dest);
1130 g_free (q);
1131 } else {
1132 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1133 selection (cpanel)->fname);
1136 #endif /* !NATIVE_WIN32 */
1138 void help_cmd (void)
1140 interactive_display (NULL, "[main]");
1143 void view_panel_cmd (void)
1145 view_cmd (cpanel);
1148 void edit_panel_cmd (void)
1150 edit_cmd (cpanel);
1153 void mkdir_panel_cmd (void)
1155 mkdir_cmd (cpanel);
1158 /* partly taken from dcigettext.c, returns "" for default locale */
1159 /* value should be freed by calling function g_free() */
1160 char *guess_message_value (void)
1162 static const char * const var[] = {
1163 /* The highest priority value is the `LANGUAGE' environment
1164 variable. This is a GNU extension. */
1165 "LANGUAGE",
1166 /* Setting of LC_ALL overwrites all other. */
1167 "LC_ALL",
1168 /* Next comes the name of the desired category. */
1169 "LC_MESSAGES",
1170 /* Last possibility is the LANG environment variable. */
1171 "LANG",
1172 /* NULL exit loops */
1173 NULL
1176 unsigned i = 0;
1177 char *locale = NULL;
1179 while (var[i] != NULL) {
1180 locale = getenv (var[i]);
1181 if (locale != NULL && locale[0] != '\0')
1182 break;
1183 i++;
1186 if (locale == NULL)
1187 locale = "";
1189 return g_strdup (locale);
1192 /* Returns a random hint */
1193 char *get_random_hint (void)
1195 char *data, *result, *eol;
1196 int len;
1197 int start;
1199 /* Do not change hints more often than one minute */
1201 #ifdef SCO_FLAVOR
1202 static time_t last;
1203 time_t now;
1205 time (&now);
1206 if ((now - last) < 60)
1207 return g_strdup ("");
1208 last = now;
1209 #else
1210 static int last_sec;
1211 static struct timeval tv;
1213 gettimeofday (&tv, NULL);
1214 if (!(tv.tv_sec> last_sec+60))
1215 return g_strdup ("");
1216 last_sec = tv.tv_sec;
1217 #endif /* !SCO_FLAVOR */
1219 data = load_mc_home_file (MC_HINT, NULL);
1220 if (!data)
1221 return 0;
1223 #ifdef SCO_FLAVOR
1224 srand ((short) now);
1225 #else
1226 srand (tv.tv_sec);
1227 #endif /* !SCO_FLAVOR */
1228 /* get a random entry */
1229 len = strlen (data);
1230 start = rand () % len;
1232 for (;start; start--){
1233 if (data [start] == '\n'){
1234 start++;
1235 break;
1238 eol = strchr (&data [start], '\n');
1239 if (eol)
1240 *eol = 0;
1241 result = g_strdup (&data [start]);
1242 g_free (data);
1243 return result;
1246 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1247 static void
1248 nice_cd (char *text, char *xtext, char *help, char *prefix, int to_home)
1250 char *machine;
1251 char *cd_path;
1253 if (!SELECTED_IS_PANEL)
1254 return;
1256 machine = input_dialog_help (text,
1257 xtext,
1258 help, "");
1259 if (!machine)
1260 return;
1262 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1263 ugly as hell and leads to problems in vfs layer */
1265 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1266 cd_path = g_strconcat (machine, to_home ? "/~/" : NULL, NULL);
1267 else
1268 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : NULL, NULL);
1270 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1271 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1272 else
1273 message (1, MSG_ERROR, _(" Could not chdir to %s "), cd_path);
1274 g_free (cd_path);
1275 g_free (machine);
1277 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1280 #ifdef USE_NETCODE
1282 static char *machine_str = N_(" Enter machine name (F1 for details): ");
1284 #ifdef WITH_MCFS
1285 void netlink_cmd (void)
1287 nice_cd (_(" Link to a remote machine "), _(machine_str),
1288 "[Network File System]", "/#mc:", 1);
1290 #endif /* WITH_MCFS */
1292 void ftplink_cmd (void)
1294 nice_cd (_(" FTP to machine "), _(machine_str),
1295 "[FTP File System]", "/#ftp:", 1);
1298 void fishlink_cmd (void)
1300 nice_cd (_(" Shell link to machine "), _(machine_str),
1301 "[FIle transfer over SHell filesystem]", "/#sh:", 1);
1304 #ifdef WITH_SMBFS
1305 void smblink_cmd (void)
1307 nice_cd (_(" SMB link to machine "), _(machine_str),
1308 "[SMB File System]", "/#smb:", 0);
1310 #endif /* WITH_SMBFS */
1312 #ifdef HAVE_SETSOCKOPT
1313 void source_routing (void)
1315 char *source;
1316 struct hostent *hp;
1318 source = input_dialog (_(" Socket source routing setup "),
1319 _(" Enter host name to use as a source routing hop: "),
1320 "");
1321 if (!source)
1322 return;
1324 hp = gethostbyname (source);
1325 g_free (source);
1326 if (!hp){
1327 message (1, _(" Host name "), _(" Error while looking up IP address "));
1328 return;
1330 source_route = *((int *)hp->h_addr);
1332 #endif /* HAVE_SETSOCKOPT */
1333 #endif /* USE_NETCODE */
1335 #ifdef USE_EXT2FSLIB
1336 void undelete_cmd (void)
1338 nice_cd (_(" Undelete files on an ext2 file system "),
1339 _(" Enter device (without /dev/) to undelete\n "
1340 " files on: (F1 for details)"),
1341 "[Undelete File System]", "/#undel:", 0);
1343 #endif /* USE_EXT2FSLIB */
1345 void quick_cd_cmd (void)
1347 char *p = cd_dialog ();
1349 if (p && *p) {
1350 char *q = g_strconcat ("cd ", p, NULL);
1352 do_cd_command (q);
1353 g_free (q);
1355 if (p)
1356 g_free (p);
1359 void
1360 dirsizes_cmd (void)
1362 WPanel *panel = cpanel;
1363 int i;
1364 off_t marked;
1365 double total;
1367 for (i = 0; i < panel->count; i++)
1368 if (S_ISDIR (panel->dir.list [i].buf.st_mode) &&
1369 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1370 !panel->dirs_marked) &&
1371 strcmp (panel->dir.list [i].fname, "..") != 0) {
1372 total = 0.0l;
1373 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1374 panel->dir.list [i].buf.st_size = (off_t) total;
1375 panel->dir.list [i].f.dir_size_computed = 1;
1378 recalculate_panel_summary (panel);
1379 paint_panel (panel);
1382 void
1383 save_setup_cmd (void)
1385 char *str;
1387 save_setup ();
1388 sync_profiles ();
1389 str = g_strconcat ( _(" Setup saved to ~/"), PROFILE_NAME, NULL);
1391 message (0, _(" Setup "), str);
1392 g_free (str);
1395 void
1396 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1398 p->user_mini_status = use_msformat;
1399 p->list_type = view_type;
1401 if (view_type == list_user || use_msformat){
1402 g_free (p->user_format);
1403 p->user_format = user;
1405 g_free (p->user_status_format [view_type]);
1406 p->user_status_format [view_type] = status;
1408 set_panel_formats (p);
1410 else {
1411 g_free (user);
1412 g_free (status);
1415 set_panel_formats (p);
1416 paint_panel (p);
1418 do_refresh ();
1421 void
1422 info_cmd_no_menu (void)
1424 if (get_display_type (0) == view_info)
1425 set_display_type (0, view_listing);
1426 else if (get_display_type (1) == view_info)
1427 set_display_type (1, view_listing);
1428 else
1429 set_display_type (cpanel == left_panel ? 1 : 0, view_info);
1432 void
1433 quick_cmd_no_menu (void)
1435 if (get_display_type (0) == view_quick)
1436 set_display_type (0, view_listing);
1437 else if (get_display_type (1) == view_quick)
1438 set_display_type (1, view_listing);
1439 else
1440 set_display_type (cpanel == left_panel ? 1 : 0, view_quick);
1443 void
1444 switch_to_listing (int panel_index)
1446 if (get_display_type (panel_index) != view_listing)
1447 set_display_type (panel_index, view_listing);
1450 void
1451 listing_cmd (void)
1453 int view_type, use_msformat;
1454 char *user, *status;
1455 WPanel *p;
1456 int display_type;
1458 display_type = get_display_type (MENU_PANEL_IDX);
1459 if (display_type == view_listing)
1460 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1461 else
1462 p = 0;
1464 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1466 if (view_type == -1)
1467 return;
1469 switch_to_listing (MENU_PANEL_IDX);
1471 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1473 configure_panel_listing (p, view_type, use_msformat, user, status);
1476 void
1477 tree_cmd (void)
1479 set_display_type (MENU_PANEL_IDX, view_tree);
1482 void
1483 info_cmd (void)
1485 set_display_type (MENU_PANEL_IDX, view_info);
1488 void
1489 quick_view_cmd (void)
1491 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == cpanel)
1492 change_panel ();
1493 set_display_type (MENU_PANEL_IDX, view_quick);
1496 /* Handle the tree internal listing modes switching */
1497 static int
1498 set_basic_panel_listing_to (int panel_index, int listing_mode)
1500 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1502 switch_to_listing (panel_index);
1503 p->list_type = listing_mode;
1504 if (set_panel_formats (p))
1505 return 0;
1507 paint_panel (p);
1508 do_refresh ();
1509 return 1;
1512 void
1513 toggle_listing_cmd (void)
1515 int current = get_current_index ();
1516 WPanel *p = (WPanel *) get_panel_widget (current);
1517 int list_mode = p->list_type;
1518 int m;
1520 switch (list_mode){
1521 case list_full:
1522 case list_brief:
1523 m = list_long;
1524 break;
1525 case list_long:
1526 m = list_user;
1527 break;
1528 default:
1529 m = list_full;
1531 if (set_basic_panel_listing_to (current, m))
1532 return;
1533 set_basic_panel_listing_to (current, list_full);