Replaced 6 with & as & where meant
[midnight-commander.git] / src / cmd.c
blob7892bd3adb9502116040d25abb7e34833403c0eb
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 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 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_MMAP
30 # include <sys/mman.h>
31 #endif
32 #ifdef USE_NETCODE
33 #include <netdb.h>
34 #endif
35 #include <unistd.h>
37 #include "global.h"
38 #include "cmd.h" /* Our definitions */
39 #include "fileopctx.h" /* file_op_context_new() */
40 #include "file.h" /* copy_file_file() */
41 #include "find.h" /* do_find() */
42 #include "hotlist.h" /* hotlist_cmd() */
43 #include "tree.h" /* tree_chdir() */
44 #include "subshell.h" /* use_subshell */
45 #include "cons.saver.h" /* console_flag */
46 #include "tty.h" /* LINES */
47 #include "dialog.h" /* Widget */
48 #include "view.h" /* mc_internal_viewer() */
49 #include "wtools.h" /* message() */
50 #include "widget.h" /* push_history() */
51 #include "key.h" /* application_keypad_mode() */
52 #include "win.h" /* do_enter_ca_mode() */
53 #include "main.h" /* change_panel() */
54 #include "panel.h" /* current_panel */
55 #include "help.h" /* interactive_display() */
56 #include "user.h" /* MC_GLOBAL_MENU */
57 #include "command.h" /* cmdline */
58 #include "layout.h" /* get_current_type() */
59 #include "ext.h" /* regex_command() */
60 #include "boxes.h" /* cd_dialog() */
61 #include "setup.h" /* save_setup() */
62 #include "profile.h" /* PROFILE_NAME */
63 #include "execute.h" /* toggle_panels() */
64 #include "history.h"
66 #ifndef MAP_FILE
67 # define MAP_FILE 0
68 #endif
70 #ifdef USE_INTERNAL_EDIT
71 # include "../edit/edit.h"
72 #endif
74 /* If set and you don't have subshell support,then C-o will give you a shell */
75 int output_starts_shell = 0;
77 /* If set, use the builtin editor */
78 int use_internal_edit = 1;
81 int
82 view_file_at_line (const char *filename, int plain_view, int internal,
83 int start_line)
85 static const char *viewer = NULL;
86 int move_dir = 0;
89 if (plain_view) {
90 int changed_hex_mode = 0;
91 int changed_nroff_flag = 0;
92 int changed_magic_flag = 0;
94 altered_hex_mode = 0;
95 altered_nroff_flag = 0;
96 altered_magic_flag = 0;
97 if (default_hex_mode)
98 changed_hex_mode = 1;
99 if (default_nroff_flag)
100 changed_nroff_flag = 1;
101 if (default_magic_flag)
102 changed_magic_flag = 1;
103 default_hex_mode = 0;
104 default_nroff_flag = 0;
105 default_magic_flag = 0;
106 mc_internal_viewer (NULL, filename, &move_dir, start_line);
107 if (changed_hex_mode && !altered_hex_mode)
108 default_hex_mode = 1;
109 if (changed_nroff_flag && !altered_nroff_flag)
110 default_nroff_flag = 1;
111 if (changed_magic_flag && !altered_magic_flag)
112 default_magic_flag = 1;
113 repaint_screen ();
114 return move_dir;
116 if (internal) {
117 char view_entry[BUF_TINY];
119 if (start_line != 0)
120 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
121 start_line);
122 else
123 strcpy (view_entry, "View");
125 if (regex_command (filename, view_entry, &move_dir) == 0) {
126 mc_internal_viewer (NULL, filename, &move_dir, start_line);
127 repaint_screen ();
129 } else {
130 if (!viewer) {
131 viewer = getenv ("VIEWER");
132 if (!viewer)
133 viewer = getenv ("PAGER");
134 if (!viewer)
135 viewer = "view";
137 execute_with_vfs_arg (viewer, filename);
139 return move_dir;
142 /* view_file (filename, plain_view, internal)
144 * Inputs:
145 * filename: The file name to view
146 * plain_view: If set does not do any fancy pre-processing (no filtering) and
147 * always invokes the internal viewer.
148 * internal: If set uses the internal viewer, otherwise an external viewer.
151 view_file (const char *filename, int plain_view, int internal)
153 return view_file_at_line (filename, plain_view, internal, 0);
156 /* scan_for_file (panel, idx, direction)
158 * Inputs:
159 * panel: pointer to the panel on which we operate
160 * idx: starting file.
161 * direction: 1, or -1
163 static int scan_for_file (WPanel *panel, int idx, int direction)
165 int i = idx + direction;
167 while (i != idx){
168 if (i < 0)
169 i = panel->count - 1;
170 if (i == panel->count)
171 i = 0;
172 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
173 return i;
174 i += direction;
176 return i;
180 * Run viewer (internal or external) on the currently selected file.
181 * If normal is 1, force internal viewer and raw mode (used for F13).
183 static void
184 do_view_cmd (int normal)
186 int dir, file_idx;
188 /* Directories are viewed by changing to them */
189 if (S_ISDIR (selection (current_panel)->st.st_mode)
190 || link_isdir (selection (current_panel))) {
191 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
192 if (query_dialog
193 (_(" Confirmation "), _("Files tagged, want to cd?"), 0, 2,
194 _("&Yes"), _("&No")) != 0) {
195 return;
198 if (!do_cd (selection (current_panel)->fname, cd_exact))
199 message (1, MSG_ERROR, _("Cannot change directory"));
201 repaint_screen();
202 return;
205 file_idx = current_panel->selected;
206 while (1) {
207 char *filename;
209 filename = current_panel->dir.list[file_idx].fname;
211 dir = view_file (filename, normal, use_internal_view);
212 if (dir == 0)
213 break;
214 file_idx = scan_for_file (current_panel, file_idx, dir);
217 repaint_screen();
220 /* Run user's preferred viewer on the currently selected file */
221 void
222 view_cmd (void)
224 do_view_cmd (0);
227 /* Ask for file and run user's preferred viewer on it */
228 void
229 view_file_cmd (void)
231 char *filename;
233 filename =
234 input_expand_dialog (_(" View file "), _(" Filename:"),
235 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
236 if (!filename)
237 return;
239 view_file (filename, 0, use_internal_view);
240 g_free (filename);
243 /* Run plain internal viewer on the currently selected file */
244 void
245 view_simple_cmd (void)
247 do_view_cmd (1);
250 void
251 filtered_view_cmd (void)
253 char *command;
255 command =
256 input_dialog (_(" Filtered view "),
257 _(" Filter command and arguments:"),
258 MC_HISTORY_FM_FILTERED_VIEW,
259 selection (current_panel)->fname);
260 if (!command)
261 return;
263 mc_internal_viewer (command, "", NULL, 0);
265 g_free (command);
268 void do_edit_at_line (const char *what, int start_line)
270 static const char *editor = NULL;
272 #ifdef USE_INTERNAL_EDIT
273 if (use_internal_edit){
274 edit_file (what, start_line);
275 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
276 repaint_screen ();
277 return;
279 #endif /* USE_INTERNAL_EDIT */
280 if (!editor){
281 editor = getenv ("EDITOR");
282 if (!editor)
283 editor = get_default_editor ();
285 execute_with_vfs_arg (editor, what);
286 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
287 repaint_screen ();
290 static void
291 do_edit (const char *what)
293 do_edit_at_line (what, 0);
296 void
297 edit_cmd (void)
299 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
300 do_edit (selection (current_panel)->fname);
303 void
304 edit_cmd_new (void)
306 do_edit (NULL);
309 /* Invoked by F5. Copy, default to the other panel. */
310 void
311 copy_cmd (void)
313 save_cwds_stat ();
314 if (panel_operate (current_panel, OP_COPY, 0)) {
315 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
316 repaint_screen ();
320 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
321 void ren_cmd (void)
323 save_cwds_stat ();
324 if (panel_operate (current_panel, OP_MOVE, 0)){
325 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
326 repaint_screen ();
330 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
331 void copy_cmd_local (void)
333 save_cwds_stat ();
334 if (panel_operate (current_panel, OP_COPY, 1)){
335 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
336 repaint_screen ();
340 /* Invoked by F16. Move/rename, default to the same panel. */
341 void ren_cmd_local (void)
343 save_cwds_stat ();
344 if (panel_operate (current_panel, OP_MOVE, 1)){
345 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
346 repaint_screen ();
350 void
351 mkdir_cmd (void)
353 char *dir, *absdir;
355 dir =
356 input_expand_dialog (_("Create a new Directory"),
357 _(" Enter directory name:"),
358 MC_HISTORY_FM_MKDIR, "");
359 if (!dir)
360 return;
362 if (dir[0] == '/' || dir[0] == '~')
363 absdir = g_strdup (dir);
364 else
365 absdir = concat_dir_and_file (current_panel->cwd, dir);
367 save_cwds_stat ();
368 if (my_mkdir (absdir, 0777) == 0) {
369 update_panels (UP_OPTIMIZE, dir);
370 repaint_screen ();
371 select_item (current_panel);
372 } else {
373 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
376 g_free (absdir);
377 g_free (dir);
380 void delete_cmd (void)
382 save_cwds_stat ();
384 if (panel_operate (current_panel, OP_DELETE, 0)){
385 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
386 repaint_screen ();
390 /* Invoked by F18. Remove selected file, regardless of marked files. */
391 void delete_cmd_local (void)
393 save_cwds_stat ();
395 if (panel_operate (current_panel, OP_DELETE, 1)){
396 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
397 repaint_screen ();
401 void find_cmd (void)
403 do_find ();
406 static void
407 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
409 g_free (p->filter);
410 p->filter = 0;
412 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
413 p->filter = allocated_filter_string;
414 else
415 g_free (allocated_filter_string);
416 reread_cmd ();
419 /* Set a given panel filter expression */
420 static void
421 set_panel_filter (WPanel *p)
423 char *reg_exp;
424 const char *x;
426 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
428 reg_exp = input_dialog_help (_(" Filter "),
429 _(" Set expression for filtering filenames"),
430 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
431 if (!reg_exp)
432 return;
433 set_panel_filter_to (p, reg_exp);
436 /* Invoked from the left/right menus */
437 void filter_cmd (void)
439 WPanel *p;
441 if (!SELECTED_IS_PANEL)
442 return;
444 p = MENU_PANEL;
445 set_panel_filter (p);
448 void reread_cmd (void)
450 int flag;
452 if (get_current_type () == view_listing &&
453 get_other_type () == view_listing)
454 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
455 else
456 flag = UP_ONLY_CURRENT;
458 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
459 repaint_screen ();
462 void reverse_selection_cmd (void)
464 file_entry *file;
465 int i;
467 for (i = 0; i < current_panel->count; i++){
468 file = &current_panel->dir.list [i];
469 if (S_ISDIR (file->st.st_mode))
470 continue;
471 do_file_mark (current_panel, i, !file->f.marked);
475 static void
476 select_unselect_cmd (const char *title, const char *history_name, int cmd)
478 char *reg_exp, *reg_exp_t;
479 int i;
480 int c;
481 int dirflag = 0;
483 reg_exp = input_dialog (title, "", history_name, easy_patterns ? "*" : ".");
484 if (!reg_exp)
485 return;
486 if (!*reg_exp) {
487 g_free (reg_exp);
488 return;
491 reg_exp_t = reg_exp;
493 /* Check if they specified a directory */
494 if (*reg_exp_t == PATH_SEP) {
495 dirflag = 1;
496 reg_exp_t++;
498 if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) {
499 dirflag = 1;
500 reg_exp_t[strlen (reg_exp_t) - 1] = 0;
503 for (i = 0; i < current_panel->count; i++) {
504 if (!strcmp (current_panel->dir.list[i].fname, ".."))
505 continue;
506 if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) {
507 if (!dirflag)
508 continue;
509 } else {
510 if (dirflag)
511 continue;
513 c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname,
514 match_file);
515 if (c == -1) {
516 message (1, MSG_ERROR, _(" Malformed regular expression "));
517 g_free (reg_exp);
518 return;
520 if (c) {
521 do_file_mark (current_panel, i, cmd);
524 g_free (reg_exp);
527 void select_cmd (void)
529 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", 1);
532 void unselect_cmd (void)
534 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", 0);
537 /* Check if the file exists */
538 /* If not copy the default */
539 static int
540 check_for_default(char *default_file, char *file)
542 struct stat s;
543 off_t count = 0;
544 double bytes = 0;
545 FileOpContext *ctx;
547 if (mc_stat (file, &s)){
548 if (mc_stat (default_file, &s)){
549 return -1;
551 ctx = file_op_context_new (OP_COPY);
552 file_op_context_create_ui (ctx, 0);
553 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
554 file_op_context_destroy (ctx);
556 return 0;
559 void ext_cmd (void)
561 char *buffer;
562 char *extdir;
563 int dir;
565 dir = 0;
566 if (geteuid () == 0){
567 dir = query_dialog (_("Extension file edit"),
568 _(" Which extension file you want to edit? "), 0, 2,
569 _("&User"), _("&System Wide"));
571 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
573 if (dir == 0){
574 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
575 check_for_default (extdir, buffer);
576 do_edit (buffer);
577 g_free (buffer);
578 } else if (dir == 1)
579 do_edit (extdir);
581 g_free (extdir);
582 flush_extension_file ();
585 /* where = 0 - do edit file menu for mc */
586 /* where = 1 - do edit file menu for mcedit */
587 static void
588 menu_edit_cmd (int where)
590 char *buffer;
591 char *menufile;
592 int dir = 0;
594 dir = query_dialog (
595 _(" Menu edit "),
596 _(" Which menu file do you want to edit? "),
597 0, geteuid() ? 2 : 3,
598 _("&Local"), _("&User"), _("&System Wide")
601 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
603 switch (dir) {
604 case 0:
605 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
606 check_for_default (menufile, buffer);
607 break;
609 case 1:
610 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
611 check_for_default (menufile, buffer);
612 break;
614 case 2:
615 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
616 break;
618 default:
619 g_free (menufile);
620 return;
622 do_edit (buffer);
623 if (dir == 0)
624 chmod(buffer, 0600);
625 g_free (buffer);
626 g_free (menufile);
629 void quick_chdir_cmd (void)
631 char *target;
633 target = hotlist_cmd (LIST_HOTLIST);
634 if (!target)
635 return;
637 if (get_current_type () == view_tree)
638 tree_chdir (the_tree, target);
639 else
640 if (!do_cd (target, cd_exact))
641 message (1, MSG_ERROR, _("Cannot change directory") );
642 g_free (target);
645 /* edit file menu for mc */
646 void
647 edit_mc_menu_cmd (void)
649 menu_edit_cmd (0);
652 #ifdef USE_INTERNAL_EDIT
653 /* edit file menu for mcedit */
654 void
655 edit_user_menu_cmd (void)
657 menu_edit_cmd (1);
660 /* edit syntax file for mcedit */
661 void
662 edit_syntax_cmd (void)
664 char *buffer;
665 char *extdir;
666 int dir = 0;
668 if (geteuid () == 0) {
669 dir =
670 query_dialog (_("Syntax file edit"),
671 _(" Which syntax file you want to edit? "), 0, 2,
672 _("&User"), _("&System Wide"));
674 extdir = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
676 if (dir == 0) {
677 buffer = concat_dir_and_file (home_dir, SYNTAX_FILE);
678 check_for_default (extdir, buffer);
679 do_edit (buffer);
680 g_free (buffer);
681 } else if (dir == 1)
682 do_edit (extdir);
684 g_free (extdir);
686 #endif
688 #ifdef USE_VFS
689 void reselect_vfs (void)
691 char *target;
693 target = hotlist_cmd (LIST_VFSLIST);
694 if (!target)
695 return;
697 if (!do_cd (target, cd_exact))
698 message (1, MSG_ERROR, _("Cannot change directory") );
699 g_free (target);
701 #endif /* USE_VFS */
703 static int compare_files (char *name1, char *name2, off_t size)
705 int file1, file2;
706 int result = -1; /* Different by default */
708 file1 = open (name1, O_RDONLY);
709 if (file1 >= 0){
710 file2 = open (name2, O_RDONLY);
711 if (file2 >= 0){
712 #ifdef HAVE_MMAP
713 char *data1, *data2;
714 /* Ugly if jungle */
715 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
716 if (data1 != (char*) -1){
717 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
718 if (data2 != (char*) -1){
719 rotate_dash ();
720 result = memcmp (data1, data2, size);
721 munmap (data2, size);
723 munmap (data1, size);
725 #else
726 /* Don't have mmap() :( Even more ugly :) */
727 char buf1[BUFSIZ], buf2[BUFSIZ];
728 int n1, n2;
729 rotate_dash ();
732 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
733 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
734 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
735 result = (n1 != n2) || memcmp(buf1,buf2,n1);
736 #endif /* !HAVE_MMAP */
737 close (file2);
739 close (file1);
741 return result;
744 enum CompareMode {
745 compare_quick, compare_size_only, compare_thourough
748 static void
749 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
751 int i, j;
752 char *src_name, *dst_name;
754 /* No marks by default */
755 panel->marked = 0;
756 panel->total = 0;
757 panel->dirs_marked = 0;
759 /* Handle all files in the panel */
760 for (i = 0; i < panel->count; i++){
761 file_entry *source = &panel->dir.list[i];
763 /* Default: unmarked */
764 file_mark (panel, i, 0);
766 /* Skip directories */
767 if (S_ISDIR (source->st.st_mode))
768 continue;
770 /* Search the corresponding entry from the other panel */
771 for (j = 0; j < other->count; j++){
772 if (strcmp (source->fname,
773 other->dir.list[j].fname) == 0)
774 break;
776 if (j >= other->count)
777 /* Not found -> mark */
778 do_file_mark (panel, i, 1);
779 else {
780 /* Found */
781 file_entry *target = &other->dir.list[j];
783 if (mode != compare_size_only){
784 /* Older version is not marked */
785 if (source->st.st_mtime < target->st.st_mtime)
786 continue;
789 /* Newer version with different size is marked */
790 if (source->st.st_size != target->st.st_size){
791 do_file_mark (panel, i, 1);
792 continue;
795 if (mode == compare_size_only)
796 continue;
798 if (mode == compare_quick){
799 /* Thorough compare off, compare only time stamps */
800 /* Mark newer version, don't mark version with the same date */
801 if (source->st.st_mtime > target->st.st_mtime){
802 do_file_mark (panel, i, 1);
804 continue;
807 /* Thorough compare on, do byte-by-byte comparison */
808 src_name = concat_dir_and_file (panel->cwd, source->fname);
809 dst_name = concat_dir_and_file (other->cwd, target->fname);
810 if (compare_files (src_name, dst_name, source->st.st_size))
811 do_file_mark (panel, i, 1);
812 g_free (src_name);
813 g_free (dst_name);
815 } /* for (i ...) */
818 void
819 compare_dirs_cmd (void)
821 int choice;
822 enum CompareMode thorough_flag;
824 choice =
825 query_dialog (_(" Compare directories "),
826 _(" Select compare method: "), 0, 3, _("&Quick"),
827 _("&Size only"), _("&Thorough"), _("&Cancel"));
829 if (choice < 0 || choice > 2)
830 return;
831 else
832 thorough_flag = choice;
834 if (get_current_type () == view_listing
835 && get_other_type () == view_listing) {
836 compare_dir (current_panel, other_panel, thorough_flag);
837 compare_dir (other_panel, current_panel, thorough_flag);
838 } else {
839 message (1, MSG_ERROR,
840 _(" Both panels should be in the "
841 "listing mode to use this command "));
845 void
846 history_cmd (void)
848 Listbox *listbox;
849 GList *current;
851 if (cmdline->need_push) {
852 if (push_history (cmdline, cmdline->buffer) == 2)
853 cmdline->need_push = 0;
855 if (!cmdline->history) {
856 message (1, MSG_ERROR, _(" The command history is empty "));
857 return;
859 current = g_list_first (cmdline->history);
860 listbox = create_listbox_window (60, 10, _(" Command history "),
861 "[Command Menu]");
862 while (current) {
863 LISTBOX_APPEND_TEXT (listbox, 0, (char *) current->data, current);
864 current = g_list_next(current);
866 run_dlg (listbox->dlg);
867 if (listbox->dlg->ret_value == B_CANCEL)
868 current = NULL;
869 else
870 current = listbox->list->current->data;
871 destroy_dlg (listbox->dlg);
872 g_free (listbox);
874 if (!current)
875 return;
876 cmdline->history = current;
877 assign_text (cmdline, (char *) current->data);
878 update_input (cmdline, 1);
881 void swap_cmd (void)
883 swap_panels ();
884 touchwin (stdscr);
885 repaint_screen ();
888 void
889 view_other_cmd (void)
891 static int message_flag = TRUE;
893 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
894 if (message_flag)
895 message (1, MSG_ERROR,
896 _(" Not an xterm or Linux console; \n"
897 " the panels cannot be toggled. "));
898 message_flag = FALSE;
899 } else {
900 toggle_panels ();
904 static void
905 do_link (int symbolic_link, const char *fname)
907 char *dest = NULL, *src = NULL;
909 if (!symbolic_link) {
910 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
911 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
912 if (!dest || !*dest)
913 goto cleanup;
914 save_cwds_stat ();
915 if (-1 == mc_link (fname, dest))
916 message (1, MSG_ERROR, _(" link: %s "),
917 unix_error_string (errno));
918 } else {
919 char *s;
920 char *d;
922 /* suggest the full path for symlink */
923 s = concat_dir_and_file (current_panel->cwd, fname);
925 if (get_other_type () == view_listing) {
926 d = concat_dir_and_file (other_panel->cwd, fname);
927 } else {
928 d = g_strdup (fname);
931 symlink_dialog (s, d, &dest, &src);
932 g_free (d);
933 g_free (s);
935 if (!dest || !*dest || !src || !*src)
936 goto cleanup;
937 save_cwds_stat ();
938 if (-1 == mc_symlink (dest, src))
939 message (1, MSG_ERROR, _(" symlink: %s "),
940 unix_error_string (errno));
942 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
943 repaint_screen ();
945 cleanup:
946 g_free (src);
947 g_free (dest);
950 void link_cmd (void)
952 do_link (0, selection (current_panel)->fname);
955 void symlink_cmd (void)
957 char *filename = NULL;
958 filename = selection (current_panel)->fname;
960 if (filename) {
961 do_link (1, filename);
965 void edit_symlink_cmd (void)
967 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
968 char buffer [MC_MAXPATHLEN];
969 char *p = NULL;
970 int i;
971 char *dest, *q;
973 p = selection (current_panel)->fname;
975 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
977 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
978 if (i > 0) {
979 buffer [i] = 0;
980 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
981 if (dest) {
982 if (*dest && strcmp (buffer, dest)) {
983 save_cwds_stat ();
984 if (-1 == mc_unlink (p)){
985 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
986 p, unix_error_string (errno));
987 } else {
988 if (-1 == mc_symlink (dest, p))
989 message (1, MSG_ERROR, _(" edit symlink: %s "),
990 unix_error_string (errno));
992 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
993 repaint_screen ();
995 g_free (dest);
998 g_free (q);
999 } else {
1000 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1001 selection (current_panel)->fname);
1005 void help_cmd (void)
1007 interactive_display (NULL, "[main]");
1010 void
1011 user_file_menu_cmd (void)
1013 user_menu_cmd (NULL);
1016 /* partly taken from dcigettext.c, returns "" for default locale */
1017 /* value should be freed by calling function g_free() */
1018 char *guess_message_value (void)
1020 static const char * const var[] = {
1021 /* The highest priority value is the `LANGUAGE' environment
1022 variable. This is a GNU extension. */
1023 "LANGUAGE",
1024 /* Setting of LC_ALL overwrites all other. */
1025 "LC_ALL",
1026 /* Next comes the name of the desired category. */
1027 "LC_MESSAGES",
1028 /* Last possibility is the LANG environment variable. */
1029 "LANG",
1030 /* NULL exit loops */
1031 NULL
1034 unsigned i = 0;
1035 const char *locale = NULL;
1037 while (var[i] != NULL) {
1038 locale = getenv (var[i]);
1039 if (locale != NULL && locale[0] != '\0')
1040 break;
1041 i++;
1044 if (locale == NULL)
1045 locale = "";
1047 return g_strdup (locale);
1051 * Return a random hint. If force is not 0, ignore the timeout.
1053 char *
1054 get_random_hint (int force)
1056 char *data, *result, *eol;
1057 int len;
1058 int start;
1059 static int last_sec;
1060 static struct timeval tv;
1062 /* Do not change hints more often than one minute */
1063 gettimeofday (&tv, NULL);
1064 if (!force && !(tv.tv_sec > last_sec + 60))
1065 return g_strdup ("");
1066 last_sec = tv.tv_sec;
1068 data = load_mc_home_file (MC_HINT, NULL);
1069 if (!data)
1070 return 0;
1072 /* get a random entry */
1073 srand (tv.tv_sec);
1074 len = strlen (data);
1075 start = rand () % len;
1077 for (; start; start--) {
1078 if (data[start] == '\n') {
1079 start++;
1080 break;
1083 eol = strchr (&data[start], '\n');
1084 if (eol)
1085 *eol = 0;
1086 result = g_strdup (&data[start]);
1087 g_free (data);
1088 return result;
1091 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1092 static void
1093 nice_cd (const char *text, const char *xtext, const char *help,
1094 const char *history_name, const char *prefix, int to_home)
1096 char *machine;
1097 char *cd_path;
1099 if (!SELECTED_IS_PANEL)
1100 return;
1102 machine = input_dialog_help (text, xtext, help, history_name, "");
1103 if (!machine)
1104 return;
1106 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1107 ugly as hell and leads to problems in vfs layer */
1109 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1110 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1111 else
1112 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1114 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1115 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1116 else
1117 message (1, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1118 g_free (cd_path);
1119 g_free (machine);
1121 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1124 #ifdef USE_NETCODE
1126 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1128 #ifdef WITH_MCFS
1129 void netlink_cmd (void)
1131 nice_cd (_(" Link to a remote machine "), _(machine_str),
1132 "[Network File System]", ":netlink_cmd: Link to a remote ",
1133 "/#mc:", 1);
1135 #endif /* WITH_MCFS */
1137 void ftplink_cmd (void)
1139 nice_cd (_(" FTP to machine "), _(machine_str),
1140 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1143 void fishlink_cmd (void)
1145 nice_cd (_(" Shell link to machine "), _(machine_str),
1146 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1147 "/#sh:", 1);
1150 #ifdef WITH_SMBFS
1151 void smblink_cmd (void)
1153 nice_cd (_(" SMB link to machine "), _(machine_str),
1154 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1155 "/#smb:", 0);
1157 #endif /* WITH_SMBFS */
1158 #endif /* USE_NETCODE */
1160 #ifdef USE_EXT2FSLIB
1161 void undelete_cmd (void)
1163 nice_cd (_(" Undelete files on an ext2 file system "),
1164 _(" Enter device (without /dev/) to undelete\n "
1165 " files on: (F1 for details)"),
1166 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1167 "/#undel:", 0);
1169 #endif /* USE_EXT2FSLIB */
1171 void quick_cd_cmd (void)
1173 char *p = cd_dialog ();
1175 if (p && *p) {
1176 char *q = g_strconcat ("cd ", p, (char *) NULL);
1178 do_cd_command (q);
1179 g_free (q);
1181 g_free (p);
1184 void
1185 single_dirsize_cmd (void)
1187 WPanel *panel = current_panel;
1188 file_entry *entry;
1189 off_t marked;
1190 double total;
1192 entry = &(panel->dir.list[panel->selected]);
1193 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1194 total = 0.0;
1195 compute_dir_size (entry->fname, &marked, &total);
1196 entry->st.st_size = (off_t) total;
1197 entry->f.dir_size_computed = 1;
1200 if (mark_moves_down)
1201 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1203 recalculate_panel_summary (panel);
1204 panel->dirty = 1;
1207 void
1208 dirsizes_cmd (void)
1210 WPanel *panel = current_panel;
1211 int i;
1212 off_t marked;
1213 double total;
1215 for (i = 0; i < panel->count; i++)
1216 if (S_ISDIR (panel->dir.list [i].st.st_mode) &&
1217 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1218 !panel->dirs_marked) &&
1219 strcmp (panel->dir.list [i].fname, "..") != 0) {
1220 total = 0.0l;
1221 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1222 panel->dir.list [i].st.st_size = (off_t) total;
1223 panel->dir.list [i].f.dir_size_computed = 1;
1226 recalculate_panel_summary (panel);
1227 panel_re_sort (panel);
1228 panel->dirty = 1;
1231 void
1232 save_setup_cmd (void)
1234 save_setup ();
1235 sync_profiles ();
1237 message (0, _(" Setup "), _(" Setup saved to ~/%s"), PROFILE_NAME);
1240 static void
1241 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1243 p->user_mini_status = use_msformat;
1244 p->list_type = view_type;
1246 if (view_type == list_user || use_msformat){
1247 g_free (p->user_format);
1248 p->user_format = user;
1250 g_free (p->user_status_format [view_type]);
1251 p->user_status_format [view_type] = status;
1253 set_panel_formats (p);
1255 else {
1256 g_free (user);
1257 g_free (status);
1260 set_panel_formats (p);
1261 do_refresh ();
1264 void
1265 info_cmd_no_menu (void)
1267 if (get_display_type (0) == view_info)
1268 set_display_type (0, view_listing);
1269 else if (get_display_type (1) == view_info)
1270 set_display_type (1, view_listing);
1271 else
1272 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1275 void
1276 quick_cmd_no_menu (void)
1278 if (get_display_type (0) == view_quick)
1279 set_display_type (0, view_listing);
1280 else if (get_display_type (1) == view_quick)
1281 set_display_type (1, view_listing);
1282 else
1283 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1286 static void
1287 switch_to_listing (int panel_index)
1289 if (get_display_type (panel_index) != view_listing)
1290 set_display_type (panel_index, view_listing);
1293 void
1294 listing_cmd (void)
1296 int view_type, use_msformat;
1297 char *user, *status;
1298 WPanel *p;
1299 int display_type;
1301 display_type = get_display_type (MENU_PANEL_IDX);
1302 if (display_type == view_listing)
1303 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1304 else
1305 p = 0;
1307 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1309 if (view_type == -1)
1310 return;
1312 switch_to_listing (MENU_PANEL_IDX);
1314 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1316 configure_panel_listing (p, view_type, use_msformat, user, status);
1319 void
1320 tree_cmd (void)
1322 set_display_type (MENU_PANEL_IDX, view_tree);
1325 void
1326 info_cmd (void)
1328 set_display_type (MENU_PANEL_IDX, view_info);
1331 void
1332 quick_view_cmd (void)
1334 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1335 change_panel ();
1336 set_display_type (MENU_PANEL_IDX, view_quick);
1339 /* Handle the tree internal listing modes switching */
1340 static int
1341 set_basic_panel_listing_to (int panel_index, int listing_mode)
1343 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1345 switch_to_listing (panel_index);
1346 p->list_type = listing_mode;
1347 if (set_panel_formats (p))
1348 return 0;
1350 do_refresh ();
1351 return 1;
1354 void
1355 toggle_listing_cmd (void)
1357 int current = get_current_index ();
1358 WPanel *p = (WPanel *) get_panel_widget (current);
1360 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);