replaced buggy concat_dir_and_file() by mhl_str_dir_plus_file()
[free-mc.git] / src / cmd.c
blobf82165c96c0d4db4313e9adde31d9d2b271ba62a
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 <mhl/string.h>
39 #include "global.h"
40 #include "cmd.h" /* Our definitions */
41 #include "fileopctx.h" /* file_op_context_new() */
42 #include "file.h" /* copy_file_file() */
43 #include "find.h" /* do_find() */
44 #include "hotlist.h" /* hotlist_cmd() */
45 #include "tree.h" /* tree_chdir() */
46 #include "subshell.h" /* use_subshell */
47 #include "cons.saver.h" /* console_flag */
48 #include "tty.h" /* LINES */
49 #include "dialog.h" /* Widget */
50 #include "view.h" /* mc_internal_viewer() */
51 #include "wtools.h" /* message() */
52 #include "widget.h" /* push_history() */
53 #include "key.h" /* application_keypad_mode() */
54 #include "win.h" /* do_enter_ca_mode() */
55 #include "main.h" /* change_panel() */
56 #include "panel.h" /* current_panel */
57 #include "help.h" /* interactive_display() */
58 #include "user.h" /* MC_GLOBAL_MENU */
59 #include "command.h" /* cmdline */
60 #include "layout.h" /* get_current_type() */
61 #include "ext.h" /* regex_command() */
62 #include "boxes.h" /* cd_dialog() */
63 #include "setup.h" /* save_setup() */
64 #include "profile.h" /* PROFILE_NAME */
65 #include "execute.h" /* toggle_panels() */
66 #include "history.h"
68 #ifndef MAP_FILE
69 # define MAP_FILE 0
70 #endif
72 #ifdef USE_INTERNAL_EDIT
73 # include "../edit/edit.h"
74 #endif
76 /* If set and you don't have subshell support,then C-o will give you a shell */
77 int output_starts_shell = 0;
79 /* If set, use the builtin editor */
80 int use_internal_edit = 1;
83 int
84 view_file_at_line (const char *filename, int plain_view, int internal,
85 int start_line)
87 static const char *viewer = NULL;
88 int move_dir = 0;
91 if (plain_view) {
92 int changed_hex_mode = 0;
93 int changed_nroff_flag = 0;
94 int changed_magic_flag = 0;
96 altered_hex_mode = 0;
97 altered_nroff_flag = 0;
98 altered_magic_flag = 0;
99 if (default_hex_mode)
100 changed_hex_mode = 1;
101 if (default_nroff_flag)
102 changed_nroff_flag = 1;
103 if (default_magic_flag)
104 changed_magic_flag = 1;
105 default_hex_mode = 0;
106 default_nroff_flag = 0;
107 default_magic_flag = 0;
108 mc_internal_viewer (NULL, filename, &move_dir, start_line);
109 if (changed_hex_mode && !altered_hex_mode)
110 default_hex_mode = 1;
111 if (changed_nroff_flag && !altered_nroff_flag)
112 default_nroff_flag = 1;
113 if (changed_magic_flag && !altered_magic_flag)
114 default_magic_flag = 1;
115 repaint_screen ();
116 return move_dir;
118 if (internal) {
119 char view_entry[BUF_TINY];
121 if (start_line != 0)
122 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
123 start_line);
124 else
125 strcpy (view_entry, "View");
127 if (regex_command (filename, view_entry, &move_dir) == 0) {
128 mc_internal_viewer (NULL, filename, &move_dir, start_line);
129 repaint_screen ();
131 } else {
132 if (!viewer) {
133 viewer = getenv ("VIEWER");
134 if (!viewer)
135 viewer = getenv ("PAGER");
136 if (!viewer)
137 viewer = "view";
139 execute_with_vfs_arg (viewer, filename);
141 return move_dir;
144 /* view_file (filename, plain_view, internal)
146 * Inputs:
147 * filename: The file name to view
148 * plain_view: If set does not do any fancy pre-processing (no filtering) and
149 * always invokes the internal viewer.
150 * internal: If set uses the internal viewer, otherwise an external viewer.
153 view_file (const char *filename, int plain_view, int internal)
155 return view_file_at_line (filename, plain_view, internal, 0);
158 /* scan_for_file (panel, idx, direction)
160 * Inputs:
161 * panel: pointer to the panel on which we operate
162 * idx: starting file.
163 * direction: 1, or -1
165 static int scan_for_file (WPanel *panel, int idx, int direction)
167 int i = idx + direction;
169 while (i != idx){
170 if (i < 0)
171 i = panel->count - 1;
172 if (i == panel->count)
173 i = 0;
174 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
175 return i;
176 i += direction;
178 return i;
182 * Run viewer (internal or external) on the currently selected file.
183 * If normal is 1, force internal viewer and raw mode (used for F13).
185 static void
186 do_view_cmd (int normal)
188 int dir, file_idx;
190 /* Directories are viewed by changing to them */
191 if (S_ISDIR (selection (current_panel)->st.st_mode)
192 || link_isdir (selection (current_panel))) {
193 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
194 if (query_dialog
195 (_(" Confirmation "), _("Files tagged, want to cd?"), 0, 2,
196 _("&Yes"), _("&No")) != 0) {
197 return;
200 if (!do_cd (selection (current_panel)->fname, cd_exact))
201 message (1, MSG_ERROR, _("Cannot change directory"));
203 repaint_screen();
204 return;
207 file_idx = current_panel->selected;
208 while (1) {
209 char *filename;
211 filename = current_panel->dir.list[file_idx].fname;
213 dir = view_file (filename, normal, use_internal_view);
214 if (dir == 0)
215 break;
216 file_idx = scan_for_file (current_panel, file_idx, dir);
219 repaint_screen();
222 /* Run user's preferred viewer on the currently selected file */
223 void
224 view_cmd (void)
226 do_view_cmd (0);
229 /* Ask for file and run user's preferred viewer on it */
230 void
231 view_file_cmd (void)
233 char *filename;
235 filename =
236 input_expand_dialog (_(" View file "), _(" Filename:"),
237 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
238 if (!filename)
239 return;
241 view_file (filename, 0, use_internal_view);
242 g_free (filename);
245 /* Run plain internal viewer on the currently selected file */
246 void
247 view_simple_cmd (void)
249 do_view_cmd (1);
252 void
253 filtered_view_cmd (void)
255 char *command;
257 command =
258 input_dialog (_(" Filtered view "),
259 _(" Filter command and arguments:"),
260 MC_HISTORY_FM_FILTERED_VIEW,
261 selection (current_panel)->fname);
262 if (!command)
263 return;
265 mc_internal_viewer (command, "", NULL, 0);
267 g_free (command);
270 void do_edit_at_line (const char *what, int start_line)
272 static const char *editor = NULL;
274 #ifdef USE_INTERNAL_EDIT
275 if (use_internal_edit){
276 edit_file (what, start_line);
277 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
278 repaint_screen ();
279 return;
281 #endif /* USE_INTERNAL_EDIT */
282 if (!editor){
283 editor = getenv ("EDITOR");
284 if (!editor)
285 editor = get_default_editor ();
287 execute_with_vfs_arg (editor, what);
288 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
289 repaint_screen ();
292 static void
293 do_edit (const char *what)
295 do_edit_at_line (what, 0);
298 void
299 edit_cmd (void)
301 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
302 do_edit (selection (current_panel)->fname);
305 void
306 edit_cmd_new (void)
308 do_edit (NULL);
311 /* Invoked by F5. Copy, default to the other panel. */
312 void
313 copy_cmd (void)
315 save_cwds_stat ();
316 if (panel_operate (current_panel, OP_COPY, 0)) {
317 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
318 repaint_screen ();
322 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
323 void ren_cmd (void)
325 save_cwds_stat ();
326 if (panel_operate (current_panel, OP_MOVE, 0)){
327 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
328 repaint_screen ();
332 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
333 void copy_cmd_local (void)
335 save_cwds_stat ();
336 if (panel_operate (current_panel, OP_COPY, 1)){
337 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
338 repaint_screen ();
342 /* Invoked by F16. Move/rename, default to the same panel. */
343 void ren_cmd_local (void)
345 save_cwds_stat ();
346 if (panel_operate (current_panel, OP_MOVE, 1)){
347 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
348 repaint_screen ();
352 void
353 mkdir_cmd (void)
355 char *dir, *absdir;
357 dir =
358 input_expand_dialog (_("Create a new Directory"),
359 _(" Enter directory name:"),
360 MC_HISTORY_FM_MKDIR, "");
361 if (!dir)
362 return;
364 if (dir[0] == '/' || dir[0] == '~')
365 absdir = g_strdup (dir);
366 else
367 absdir = mhl_str_dir_plus_file (current_panel->cwd, dir);
369 save_cwds_stat ();
370 if (my_mkdir (absdir, 0777) == 0) {
371 update_panels (UP_OPTIMIZE, dir);
372 repaint_screen ();
373 select_item (current_panel);
374 } else {
375 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
378 g_free (absdir);
379 g_free (dir);
382 void delete_cmd (void)
384 save_cwds_stat ();
386 if (panel_operate (current_panel, OP_DELETE, 0)){
387 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
388 repaint_screen ();
392 /* Invoked by F18. Remove selected file, regardless of marked files. */
393 void delete_cmd_local (void)
395 save_cwds_stat ();
397 if (panel_operate (current_panel, OP_DELETE, 1)){
398 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
399 repaint_screen ();
403 void find_cmd (void)
405 do_find ();
408 static void
409 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
411 g_free (p->filter);
412 p->filter = 0;
414 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
415 p->filter = allocated_filter_string;
416 else
417 g_free (allocated_filter_string);
418 reread_cmd ();
421 /* Set a given panel filter expression */
422 static void
423 set_panel_filter (WPanel *p)
425 char *reg_exp;
426 const char *x;
428 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
430 reg_exp = input_dialog_help (_(" Filter "),
431 _(" Set expression for filtering filenames"),
432 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
433 if (!reg_exp)
434 return;
435 set_panel_filter_to (p, reg_exp);
438 /* Invoked from the left/right menus */
439 void filter_cmd (void)
441 WPanel *p;
443 if (!SELECTED_IS_PANEL)
444 return;
446 p = MENU_PANEL;
447 set_panel_filter (p);
450 void reread_cmd (void)
452 int flag;
454 if (get_current_type () == view_listing &&
455 get_other_type () == view_listing)
456 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
457 else
458 flag = UP_ONLY_CURRENT;
460 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
461 repaint_screen ();
464 void reverse_selection_cmd (void)
466 file_entry *file;
467 int i;
469 for (i = 0; i < current_panel->count; i++){
470 file = &current_panel->dir.list [i];
471 if (S_ISDIR (file->st.st_mode))
472 continue;
473 do_file_mark (current_panel, i, !file->f.marked);
477 static void
478 select_unselect_cmd (const char *title, const char *history_name, int cmd)
480 char *reg_exp, *reg_exp_t;
481 int i;
482 int c;
483 int dirflag = 0;
485 reg_exp = input_dialog (title, "", history_name, easy_patterns ? "*" : ".");
486 if (!reg_exp)
487 return;
488 if (!*reg_exp) {
489 g_free (reg_exp);
490 return;
493 reg_exp_t = reg_exp;
495 /* Check if they specified a directory */
496 if (*reg_exp_t == PATH_SEP) {
497 dirflag = 1;
498 reg_exp_t++;
500 if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) {
501 dirflag = 1;
502 reg_exp_t[strlen (reg_exp_t) - 1] = 0;
505 for (i = 0; i < current_panel->count; i++) {
506 if (!strcmp (current_panel->dir.list[i].fname, ".."))
507 continue;
508 if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) {
509 if (!dirflag)
510 continue;
511 } else {
512 if (dirflag)
513 continue;
515 c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname,
516 match_file);
517 if (c == -1) {
518 message (1, MSG_ERROR, _(" Malformed regular expression "));
519 g_free (reg_exp);
520 return;
522 if (c) {
523 do_file_mark (current_panel, i, cmd);
526 g_free (reg_exp);
529 void select_cmd (void)
531 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", 1);
534 void unselect_cmd (void)
536 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", 0);
539 /* Check if the file exists */
540 /* If not copy the default */
541 static int
542 check_for_default(char *default_file, char *file)
544 struct stat s;
545 off_t count = 0;
546 double bytes = 0;
547 FileOpContext *ctx;
549 if (mc_stat (file, &s)){
550 if (mc_stat (default_file, &s)){
551 return -1;
553 ctx = file_op_context_new (OP_COPY);
554 file_op_context_create_ui (ctx, 0);
555 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
556 file_op_context_destroy (ctx);
558 return 0;
561 void ext_cmd (void)
563 char *buffer;
564 char *extdir;
565 int dir;
567 dir = 0;
568 if (geteuid () == 0){
569 dir = query_dialog (_("Extension file edit"),
570 _(" Which extension file you want to edit? "), 0, 2,
571 _("&User"), _("&System Wide"));
573 extdir = mhl_str_dir_plus_file (mc_home, MC_LIB_EXT);
575 if (dir == 0){
576 buffer = mhl_str_dir_plus_file (home_dir, MC_USER_EXT);
577 check_for_default (extdir, buffer);
578 do_edit (buffer);
579 g_free (buffer);
580 } else if (dir == 1)
581 do_edit (extdir);
583 g_free (extdir);
584 flush_extension_file ();
587 /* where = 0 - do edit file menu for mc */
588 /* where = 1 - do edit file menu for mcedit */
589 static void
590 menu_edit_cmd (int where)
592 char *buffer;
593 char *menufile;
594 int dir = 0;
596 dir = query_dialog (
597 _(" Menu edit "),
598 _(" Which menu file do you want to edit? "),
599 0, geteuid() ? 2 : 3,
600 _("&Local"), _("&User"), _("&System Wide")
603 menufile = mhl_str_dir_plus_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
605 switch (dir) {
606 case 0:
607 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
608 check_for_default (menufile, buffer);
609 break;
611 case 1:
612 buffer = mhl_str_dir_plus_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
613 check_for_default (menufile, buffer);
614 break;
616 case 2:
617 buffer = mhl_str_dir_plus_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
618 break;
620 default:
621 g_free (menufile);
622 return;
624 do_edit (buffer);
625 if (dir == 0)
626 chmod(buffer, 0600);
627 g_free (buffer);
628 g_free (menufile);
631 void quick_chdir_cmd (void)
633 char *target;
635 target = hotlist_cmd (LIST_HOTLIST);
636 if (!target)
637 return;
639 if (get_current_type () == view_tree)
640 tree_chdir (the_tree, target);
641 else
642 if (!do_cd (target, cd_exact))
643 message (1, MSG_ERROR, _("Cannot change directory") );
644 g_free (target);
647 /* edit file menu for mc */
648 void
649 edit_mc_menu_cmd (void)
651 menu_edit_cmd (0);
654 #ifdef USE_INTERNAL_EDIT
655 /* edit file menu for mcedit */
656 void
657 edit_user_menu_cmd (void)
659 menu_edit_cmd (1);
662 /* edit syntax file for mcedit */
663 void
664 edit_syntax_cmd (void)
666 char *buffer;
667 char *extdir;
668 int dir = 0;
670 if (geteuid () == 0) {
671 dir =
672 query_dialog (_("Syntax file edit"),
673 _(" Which syntax file you want to edit? "), 0, 2,
674 _("&User"), _("&System Wide"));
676 extdir = mhl_str_dir_plus_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
678 if (dir == 0) {
679 buffer = mhl_str_dir_plus_file (home_dir, SYNTAX_FILE);
680 check_for_default (extdir, buffer);
681 do_edit (buffer);
682 g_free (buffer);
683 } else if (dir == 1)
684 do_edit (extdir);
686 g_free (extdir);
688 #endif
690 #ifdef USE_VFS
691 void reselect_vfs (void)
693 char *target;
695 target = hotlist_cmd (LIST_VFSLIST);
696 if (!target)
697 return;
699 if (!do_cd (target, cd_exact))
700 message (1, MSG_ERROR, _("Cannot change directory") );
701 g_free (target);
703 #endif /* USE_VFS */
705 static int compare_files (char *name1, char *name2, off_t size)
707 int file1, file2;
708 int result = -1; /* Different by default */
710 file1 = open (name1, O_RDONLY);
711 if (file1 >= 0){
712 file2 = open (name2, O_RDONLY);
713 if (file2 >= 0){
714 #ifdef HAVE_MMAP
715 char *data1, *data2;
716 /* Ugly if jungle */
717 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
718 if (data1 != (char*) -1){
719 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
720 if (data2 != (char*) -1){
721 rotate_dash ();
722 result = memcmp (data1, data2, size);
723 munmap (data2, size);
725 munmap (data1, size);
727 #else
728 /* Don't have mmap() :( Even more ugly :) */
729 char buf1[BUFSIZ], buf2[BUFSIZ];
730 int n1, n2;
731 rotate_dash ();
734 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
735 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
736 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
737 result = (n1 != n2) || memcmp(buf1,buf2,n1);
738 #endif /* !HAVE_MMAP */
739 close (file2);
741 close (file1);
743 return result;
746 enum CompareMode {
747 compare_quick, compare_size_only, compare_thourough
750 static void
751 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
753 int i, j;
754 char *src_name, *dst_name;
756 /* No marks by default */
757 panel->marked = 0;
758 panel->total = 0;
759 panel->dirs_marked = 0;
761 /* Handle all files in the panel */
762 for (i = 0; i < panel->count; i++){
763 file_entry *source = &panel->dir.list[i];
765 /* Default: unmarked */
766 file_mark (panel, i, 0);
768 /* Skip directories */
769 if (S_ISDIR (source->st.st_mode))
770 continue;
772 /* Search the corresponding entry from the other panel */
773 for (j = 0; j < other->count; j++){
774 if (strcmp (source->fname,
775 other->dir.list[j].fname) == 0)
776 break;
778 if (j >= other->count)
779 /* Not found -> mark */
780 do_file_mark (panel, i, 1);
781 else {
782 /* Found */
783 file_entry *target = &other->dir.list[j];
785 if (mode != compare_size_only){
786 /* Older version is not marked */
787 if (source->st.st_mtime < target->st.st_mtime)
788 continue;
791 /* Newer version with different size is marked */
792 if (source->st.st_size != target->st.st_size){
793 do_file_mark (panel, i, 1);
794 continue;
797 if (mode == compare_size_only)
798 continue;
800 if (mode == compare_quick){
801 /* Thorough compare off, compare only time stamps */
802 /* Mark newer version, don't mark version with the same date */
803 if (source->st.st_mtime > target->st.st_mtime){
804 do_file_mark (panel, i, 1);
806 continue;
809 /* Thorough compare on, do byte-by-byte comparison */
810 src_name = mhl_str_dir_plus_file (panel->cwd, source->fname);
811 dst_name = mhl_str_dir_plus_file (other->cwd, target->fname);
812 if (compare_files (src_name, dst_name, source->st.st_size))
813 do_file_mark (panel, i, 1);
814 g_free (src_name);
815 g_free (dst_name);
817 } /* for (i ...) */
820 void
821 compare_dirs_cmd (void)
823 int choice;
824 enum CompareMode thorough_flag;
826 choice =
827 query_dialog (_(" Compare directories "),
828 _(" Select compare method: "), 0, 3, _("&Quick"),
829 _("&Size only"), _("&Thorough"), _("&Cancel"));
831 if (choice < 0 || choice > 2)
832 return;
833 else
834 thorough_flag = choice;
836 if (get_current_type () == view_listing
837 && get_other_type () == view_listing) {
838 compare_dir (current_panel, other_panel, thorough_flag);
839 compare_dir (other_panel, current_panel, thorough_flag);
840 } else {
841 message (1, MSG_ERROR,
842 _(" Both panels should be in the "
843 "listing mode to use this command "));
847 void
848 history_cmd (void)
850 Listbox *listbox;
851 GList *current;
853 if (cmdline->need_push) {
854 if (push_history (cmdline, cmdline->buffer) == 2)
855 cmdline->need_push = 0;
857 if (!cmdline->history) {
858 message (1, MSG_ERROR, _(" The command history is empty "));
859 return;
861 current = g_list_first (cmdline->history);
862 listbox = create_listbox_window (60, 10, _(" Command history "),
863 "[Command Menu]");
864 while (current) {
865 LISTBOX_APPEND_TEXT (listbox, 0, (char *) current->data, current);
866 current = g_list_next(current);
868 run_dlg (listbox->dlg);
869 if (listbox->dlg->ret_value == B_CANCEL)
870 current = NULL;
871 else
872 current = listbox->list->current->data;
873 destroy_dlg (listbox->dlg);
874 g_free (listbox);
876 if (!current)
877 return;
878 cmdline->history = current;
879 assign_text (cmdline, (char *) current->data);
880 update_input (cmdline, 1);
883 void swap_cmd (void)
885 swap_panels ();
886 touchwin (stdscr);
887 repaint_screen ();
890 void
891 view_other_cmd (void)
893 static int message_flag = TRUE;
895 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
896 if (message_flag)
897 message (1, MSG_ERROR,
898 _(" Not an xterm or Linux console; \n"
899 " the panels cannot be toggled. "));
900 message_flag = FALSE;
901 } else {
902 toggle_panels ();
906 static void
907 do_link (int symbolic_link, const char *fname)
909 char *dest = NULL, *src = NULL;
911 if (!symbolic_link) {
912 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
913 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
914 if (!dest || !*dest)
915 goto cleanup;
916 save_cwds_stat ();
917 if (-1 == mc_link (fname, dest))
918 message (1, MSG_ERROR, _(" link: %s "),
919 unix_error_string (errno));
920 } else {
921 char *s;
922 char *d;
924 /* suggest the full path for symlink */
925 s = mhl_str_dir_plus_file (current_panel->cwd, fname);
927 if (get_other_type () == view_listing) {
928 d = mhl_str_dir_plus_file (other_panel->cwd, fname);
929 } else {
930 d = g_strdup (fname);
933 symlink_dialog (s, d, &dest, &src);
934 g_free (d);
935 g_free (s);
937 if (!dest || !*dest || !src || !*src)
938 goto cleanup;
939 save_cwds_stat ();
940 if (-1 == mc_symlink (dest, src))
941 message (1, MSG_ERROR, _(" symlink: %s "),
942 unix_error_string (errno));
944 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
945 repaint_screen ();
947 cleanup:
948 g_free (src);
949 g_free (dest);
952 void link_cmd (void)
954 do_link (0, selection (current_panel)->fname);
957 void symlink_cmd (void)
959 char *filename = NULL;
960 filename = selection (current_panel)->fname;
962 if (filename) {
963 do_link (1, filename);
967 void edit_symlink_cmd (void)
969 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
970 char buffer [MC_MAXPATHLEN];
971 char *p = NULL;
972 int i;
973 char *dest, *q;
975 p = selection (current_panel)->fname;
977 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
979 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
980 if (i > 0) {
981 buffer [i] = 0;
982 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
983 if (dest) {
984 if (*dest && strcmp (buffer, dest)) {
985 save_cwds_stat ();
986 if (-1 == mc_unlink (p)){
987 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
988 p, unix_error_string (errno));
989 } else {
990 if (-1 == mc_symlink (dest, p))
991 message (1, MSG_ERROR, _(" edit symlink: %s "),
992 unix_error_string (errno));
994 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
995 repaint_screen ();
997 g_free (dest);
1000 g_free (q);
1001 } else {
1002 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1003 selection (current_panel)->fname);
1007 void help_cmd (void)
1009 interactive_display (NULL, "[main]");
1012 void
1013 user_file_menu_cmd (void)
1015 user_menu_cmd (NULL);
1018 /* partly taken from dcigettext.c, returns "" for default locale */
1019 /* value should be freed by calling function g_free() */
1020 char *guess_message_value (void)
1022 static const char * const var[] = {
1023 /* The highest priority value is the `LANGUAGE' environment
1024 variable. This is a GNU extension. */
1025 "LANGUAGE",
1026 /* Setting of LC_ALL overwrites all other. */
1027 "LC_ALL",
1028 /* Next comes the name of the desired category. */
1029 "LC_MESSAGES",
1030 /* Last possibility is the LANG environment variable. */
1031 "LANG",
1032 /* NULL exit loops */
1033 NULL
1036 unsigned i = 0;
1037 const char *locale = NULL;
1039 while (var[i] != NULL) {
1040 locale = getenv (var[i]);
1041 if (locale != NULL && locale[0] != '\0')
1042 break;
1043 i++;
1046 if (locale == NULL)
1047 locale = "";
1049 return g_strdup (locale);
1053 * Return a random hint. If force is not 0, ignore the timeout.
1055 char *
1056 get_random_hint (int force)
1058 char *data, *result, *eol;
1059 int len;
1060 int start;
1061 static int last_sec;
1062 static struct timeval tv;
1064 /* Do not change hints more often than one minute */
1065 gettimeofday (&tv, NULL);
1066 if (!force && !(tv.tv_sec > last_sec + 60))
1067 return g_strdup ("");
1068 last_sec = tv.tv_sec;
1070 data = load_mc_home_file (MC_HINT, NULL);
1071 if (!data)
1072 return 0;
1074 /* get a random entry */
1075 srand (tv.tv_sec);
1076 len = strlen (data);
1077 start = rand () % len;
1079 for (; start; start--) {
1080 if (data[start] == '\n') {
1081 start++;
1082 break;
1085 eol = strchr (&data[start], '\n');
1086 if (eol)
1087 *eol = 0;
1088 result = g_strdup (&data[start]);
1089 g_free (data);
1090 return result;
1093 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1094 static void
1095 nice_cd (const char *text, const char *xtext, const char *help,
1096 const char *history_name, const char *prefix, int to_home)
1098 char *machine;
1099 char *cd_path;
1101 if (!SELECTED_IS_PANEL)
1102 return;
1104 machine = input_dialog_help (text, xtext, help, history_name, "");
1105 if (!machine)
1106 return;
1108 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1109 ugly as hell and leads to problems in vfs layer */
1111 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1112 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1113 else
1114 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1116 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1117 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1118 else
1119 message (1, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1120 g_free (cd_path);
1121 g_free (machine);
1123 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1126 #ifdef USE_NETCODE
1128 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1130 #ifdef WITH_MCFS
1131 void netlink_cmd (void)
1133 nice_cd (_(" Link to a remote machine "), _(machine_str),
1134 "[Network File System]", ":netlink_cmd: Link to a remote ",
1135 "/#mc:", 1);
1137 #endif /* WITH_MCFS */
1139 void ftplink_cmd (void)
1141 nice_cd (_(" FTP to machine "), _(machine_str),
1142 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1145 void fishlink_cmd (void)
1147 nice_cd (_(" Shell link to machine "), _(machine_str),
1148 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1149 "/#sh:", 1);
1152 #ifdef WITH_SMBFS
1153 void smblink_cmd (void)
1155 nice_cd (_(" SMB link to machine "), _(machine_str),
1156 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1157 "/#smb:", 0);
1159 #endif /* WITH_SMBFS */
1160 #endif /* USE_NETCODE */
1162 #ifdef USE_EXT2FSLIB
1163 void undelete_cmd (void)
1165 nice_cd (_(" Undelete files on an ext2 file system "),
1166 _(" Enter device (without /dev/) to undelete\n "
1167 " files on: (F1 for details)"),
1168 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1169 "/#undel:", 0);
1171 #endif /* USE_EXT2FSLIB */
1173 void quick_cd_cmd (void)
1175 char *p = cd_dialog ();
1177 if (p && *p) {
1178 char *q = g_strconcat ("cd ", p, (char *) NULL);
1180 do_cd_command (q);
1181 g_free (q);
1183 g_free (p);
1186 void
1187 single_dirsize_cmd (void)
1189 WPanel *panel = current_panel;
1190 file_entry *entry;
1191 off_t marked;
1192 double total;
1194 entry = &(panel->dir.list[panel->selected]);
1195 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1196 total = 0.0;
1197 compute_dir_size (entry->fname, &marked, &total);
1198 entry->st.st_size = (off_t) total;
1199 entry->f.dir_size_computed = 1;
1202 if (mark_moves_down)
1203 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1205 recalculate_panel_summary (panel);
1206 panel->dirty = 1;
1209 void
1210 dirsizes_cmd (void)
1212 WPanel *panel = current_panel;
1213 int i;
1214 off_t marked;
1215 double total;
1217 for (i = 0; i < panel->count; i++)
1218 if (S_ISDIR (panel->dir.list [i].st.st_mode) &&
1219 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1220 !panel->dirs_marked) &&
1221 strcmp (panel->dir.list [i].fname, "..") != 0) {
1222 total = 0.0l;
1223 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1224 panel->dir.list [i].st.st_size = (off_t) total;
1225 panel->dir.list [i].f.dir_size_computed = 1;
1228 recalculate_panel_summary (panel);
1229 panel_re_sort (panel);
1230 panel->dirty = 1;
1233 void
1234 save_setup_cmd (void)
1236 save_setup ();
1237 sync_profiles ();
1239 message (0, _(" Setup "), _(" Setup saved to ~/%s"), PROFILE_NAME);
1242 static void
1243 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1245 p->user_mini_status = use_msformat;
1246 p->list_type = view_type;
1248 if (view_type == list_user || use_msformat){
1249 g_free (p->user_format);
1250 p->user_format = user;
1252 g_free (p->user_status_format [view_type]);
1253 p->user_status_format [view_type] = status;
1255 set_panel_formats (p);
1257 else {
1258 g_free (user);
1259 g_free (status);
1262 set_panel_formats (p);
1263 do_refresh ();
1266 void
1267 info_cmd_no_menu (void)
1269 if (get_display_type (0) == view_info)
1270 set_display_type (0, view_listing);
1271 else if (get_display_type (1) == view_info)
1272 set_display_type (1, view_listing);
1273 else
1274 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1277 void
1278 quick_cmd_no_menu (void)
1280 if (get_display_type (0) == view_quick)
1281 set_display_type (0, view_listing);
1282 else if (get_display_type (1) == view_quick)
1283 set_display_type (1, view_listing);
1284 else
1285 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1288 static void
1289 switch_to_listing (int panel_index)
1291 if (get_display_type (panel_index) != view_listing)
1292 set_display_type (panel_index, view_listing);
1295 void
1296 listing_cmd (void)
1298 int view_type, use_msformat;
1299 char *user, *status;
1300 WPanel *p;
1301 int display_type;
1303 display_type = get_display_type (MENU_PANEL_IDX);
1304 if (display_type == view_listing)
1305 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1306 else
1307 p = 0;
1309 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1311 if (view_type == -1)
1312 return;
1314 switch_to_listing (MENU_PANEL_IDX);
1316 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1318 configure_panel_listing (p, view_type, use_msformat, user, status);
1321 void
1322 tree_cmd (void)
1324 set_display_type (MENU_PANEL_IDX, view_tree);
1327 void
1328 info_cmd (void)
1330 set_display_type (MENU_PANEL_IDX, view_info);
1333 void
1334 quick_view_cmd (void)
1336 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1337 change_panel ();
1338 set_display_type (MENU_PANEL_IDX, view_quick);
1341 /* Handle the tree internal listing modes switching */
1342 static int
1343 set_basic_panel_listing_to (int panel_index, int listing_mode)
1345 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1347 switch_to_listing (panel_index);
1348 p->list_type = listing_mode;
1349 if (set_panel_formats (p))
1350 return 0;
1352 do_refresh ();
1353 return 1;
1356 void
1357 toggle_listing_cmd (void)
1359 int current = get_current_index ();
1360 WPanel *p = (WPanel *) get_panel_widget (current);
1362 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);