Removed type SHELL_ESCAPED_STR in favour of plain char*
[midnight-commander.git] / src / cmd.c
blob0bab86e06a0635680de9e3d1599bcb1ee53e18c5
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/memory.h>
38 #include <mhl/string.h>
40 #include "global.h"
41 #include "cmd.h" /* Our definitions */
42 #include "fileopctx.h" /* file_op_context_new() */
43 #include "file.h" /* copy_file_file() */
44 #include "find.h" /* do_find() */
45 #include "hotlist.h" /* hotlist_cmd() */
46 #include "tree.h" /* tree_chdir() */
47 #include "subshell.h" /* use_subshell */
48 #include "cons.saver.h" /* console_flag */
49 #include "tty.h" /* LINES */
50 #include "dialog.h" /* Widget */
51 #include "view.h" /* mc_internal_viewer() */
52 #include "wtools.h" /* message() */
53 #include "widget.h" /* push_history() */
54 #include "key.h" /* application_keypad_mode() */
55 #include "win.h" /* do_enter_ca_mode() */
56 #include "main.h" /* change_panel() */
57 #include "panel.h" /* current_panel */
58 #include "help.h" /* interactive_display() */
59 #include "user.h" /* MC_GLOBAL_MENU */
60 #include "command.h" /* cmdline */
61 #include "layout.h" /* get_current_type() */
62 #include "ext.h" /* regex_command() */
63 #include "boxes.h" /* cd_dialog() */
64 #include "setup.h" /* save_setup() */
65 #include "profile.h" /* PROFILE_NAME */
66 #include "execute.h" /* toggle_panels() */
67 #include "history.h"
69 #ifndef MAP_FILE
70 # define MAP_FILE 0
71 #endif
73 #ifdef USE_INTERNAL_EDIT
74 # include "../edit/edit.h"
75 #endif
77 /* If set and you don't have subshell support,then C-o will give you a shell */
78 int output_starts_shell = 0;
80 /* If set, use the builtin editor */
81 int use_internal_edit = 1;
83 /* Automatically fills name with current selected item name on mkdir */
84 int auto_fill_mkdir_name = 1;
86 int
87 view_file_at_line (const char *filename, int plain_view, int internal,
88 int start_line)
90 static const char *viewer = NULL;
91 int move_dir = 0;
94 if (plain_view) {
95 int changed_hex_mode = 0;
96 int changed_nroff_flag = 0;
97 int changed_magic_flag = 0;
99 altered_hex_mode = 0;
100 altered_nroff_flag = 0;
101 altered_magic_flag = 0;
102 if (default_hex_mode)
103 changed_hex_mode = 1;
104 if (default_nroff_flag)
105 changed_nroff_flag = 1;
106 if (default_magic_flag)
107 changed_magic_flag = 1;
108 default_hex_mode = 0;
109 default_nroff_flag = 0;
110 default_magic_flag = 0;
111 mc_internal_viewer (NULL, filename, &move_dir, start_line);
112 if (changed_hex_mode && !altered_hex_mode)
113 default_hex_mode = 1;
114 if (changed_nroff_flag && !altered_nroff_flag)
115 default_nroff_flag = 1;
116 if (changed_magic_flag && !altered_magic_flag)
117 default_magic_flag = 1;
118 repaint_screen ();
119 return move_dir;
121 if (internal) {
122 char view_entry[BUF_TINY];
124 if (start_line != 0)
125 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
126 start_line);
127 else
128 strcpy (view_entry, "View");
130 if (regex_command (filename, view_entry, &move_dir) == 0) {
131 mc_internal_viewer (NULL, filename, &move_dir, start_line);
132 repaint_screen ();
134 } else {
135 if (!viewer) {
136 viewer = getenv ("VIEWER");
137 if (!viewer)
138 viewer = getenv ("PAGER");
139 if (!viewer)
140 viewer = "view";
142 execute_with_vfs_arg (viewer, filename);
144 return move_dir;
147 /* view_file (filename, plain_view, internal)
149 * Inputs:
150 * filename: The file name to view
151 * plain_view: If set does not do any fancy pre-processing (no filtering) and
152 * always invokes the internal viewer.
153 * internal: If set uses the internal viewer, otherwise an external viewer.
156 view_file (const char *filename, int plain_view, int internal)
158 return view_file_at_line (filename, plain_view, internal, 0);
161 /* scan_for_file (panel, idx, direction)
163 * Inputs:
164 * panel: pointer to the panel on which we operate
165 * idx: starting file.
166 * direction: 1, or -1
168 static int scan_for_file (WPanel *panel, int idx, int direction)
170 int i = idx + direction;
172 while (i != idx){
173 if (i < 0)
174 i = panel->count - 1;
175 if (i == panel->count)
176 i = 0;
177 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
178 return i;
179 i += direction;
181 return i;
185 * Run viewer (internal or external) on the currently selected file.
186 * If normal is 1, force internal viewer and raw mode (used for F13).
188 static void
189 do_view_cmd (int normal)
191 int dir, file_idx;
193 /* Directories are viewed by changing to them */
194 if (S_ISDIR (selection (current_panel)->st.st_mode)
195 || link_isdir (selection (current_panel))) {
196 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
197 if (query_dialog
198 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
199 _("&Yes"), _("&No")) != 0) {
200 return;
203 if (!do_cd (selection (current_panel)->fname, cd_exact))
204 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
206 repaint_screen();
207 return;
210 file_idx = current_panel->selected;
211 while (1) {
212 char *filename;
214 filename = current_panel->dir.list[file_idx].fname;
216 dir = view_file (filename, normal, use_internal_view);
217 if (dir == 0)
218 break;
219 file_idx = scan_for_file (current_panel, file_idx, dir);
222 repaint_screen();
225 /* Run user's preferred viewer on the currently selected file */
226 void
227 view_cmd (void)
229 do_view_cmd (0);
232 /* Ask for file and run user's preferred viewer on it */
233 void
234 view_file_cmd (void)
236 char *filename;
238 filename =
239 input_expand_dialog (_(" View file "), _(" Filename:"),
240 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
241 if (!filename)
242 return;
244 view_file (filename, 0, use_internal_view);
245 g_free (filename);
248 /* Run plain internal viewer on the currently selected file */
249 void
250 view_simple_cmd (void)
252 do_view_cmd (1);
255 void
256 filtered_view_cmd (void)
258 char *command;
260 command =
261 input_dialog (_(" Filtered view "),
262 _(" Filter command and arguments:"),
263 MC_HISTORY_FM_FILTERED_VIEW,
264 selection (current_panel)->fname);
265 if (!command)
266 return;
268 mc_internal_viewer (command, "", NULL, 0);
270 g_free (command);
273 void do_edit_at_line (const char *what, int start_line)
275 static const char *editor = NULL;
277 #ifdef USE_INTERNAL_EDIT
278 if (use_internal_edit){
279 edit_file (what, start_line);
280 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
281 repaint_screen ();
282 return;
284 #endif /* USE_INTERNAL_EDIT */
285 if (!editor){
286 editor = getenv ("EDITOR");
287 if (!editor)
288 editor = get_default_editor ();
290 execute_with_vfs_arg (editor, what);
291 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
292 repaint_screen ();
295 static void
296 do_edit (const char *what)
298 do_edit_at_line (what, 0);
301 void
302 edit_cmd (void)
304 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
305 do_edit (selection (current_panel)->fname);
308 void
309 edit_cmd_new (void)
311 do_edit (NULL);
314 /* Invoked by F5. Copy, default to the other panel. */
315 void
316 copy_cmd (void)
318 save_cwds_stat ();
319 if (panel_operate (current_panel, OP_COPY, 0)) {
320 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
321 repaint_screen ();
325 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
326 void ren_cmd (void)
328 save_cwds_stat ();
329 if (panel_operate (current_panel, OP_MOVE, 0)){
330 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
331 repaint_screen ();
335 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
336 void copy_cmd_local (void)
338 save_cwds_stat ();
339 if (panel_operate (current_panel, OP_COPY, 1)){
340 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
341 repaint_screen ();
345 /* Invoked by F16. Move/rename, default to the same panel. */
346 void ren_cmd_local (void)
348 save_cwds_stat ();
349 if (panel_operate (current_panel, OP_MOVE, 1)){
350 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
351 repaint_screen ();
355 void
356 mkdir_cmd (void)
358 char *dir, *absdir;
359 char *name = "";
361 /* If 'on' then automatically fills name with current selected item name */
362 if (auto_fill_mkdir_name)
363 name = selection (current_panel)->fname;
365 dir =
366 input_expand_dialog (_("Create a new Directory"),
367 _(" Enter directory name:"),
368 MC_HISTORY_FM_MKDIR,
369 name);
371 if (!dir)
372 return;
374 if (dir[0] == '/' || dir[0] == '~')
375 absdir = g_strdup (dir);
376 else
377 absdir = mhl_str_dir_plus_file (current_panel->cwd, dir);
379 save_cwds_stat ();
380 if (my_mkdir (absdir, 0777) == 0) {
381 update_panels (UP_OPTIMIZE, dir);
382 repaint_screen ();
383 select_item (current_panel);
384 } else {
385 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
388 g_free (absdir);
389 g_free (dir);
392 void delete_cmd (void)
394 save_cwds_stat ();
396 if (panel_operate (current_panel, OP_DELETE, 0)){
397 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
398 repaint_screen ();
402 /* Invoked by F18. Remove selected file, regardless of marked files. */
403 void delete_cmd_local (void)
405 save_cwds_stat ();
407 if (panel_operate (current_panel, OP_DELETE, 1)){
408 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
409 repaint_screen ();
413 void find_cmd (void)
415 do_find ();
418 static void
419 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
421 g_free (p->filter);
422 p->filter = 0;
424 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
425 p->filter = allocated_filter_string;
426 else
427 g_free (allocated_filter_string);
428 reread_cmd ();
431 /* Set a given panel filter expression */
432 static void
433 set_panel_filter (WPanel *p)
435 char *reg_exp;
436 const char *x;
438 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
440 reg_exp = input_dialog_help (_(" Filter "),
441 _(" Set expression for filtering filenames"),
442 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
443 if (!reg_exp)
444 return;
445 set_panel_filter_to (p, reg_exp);
448 /* Invoked from the left/right menus */
449 void filter_cmd (void)
451 WPanel *p;
453 if (!SELECTED_IS_PANEL)
454 return;
456 p = MENU_PANEL;
457 set_panel_filter (p);
460 void reread_cmd (void)
462 int flag;
464 if (get_current_type () == view_listing &&
465 get_other_type () == view_listing)
466 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
467 else
468 flag = UP_ONLY_CURRENT;
470 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
471 repaint_screen ();
474 void reverse_selection_cmd (void)
476 file_entry *file;
477 int i;
479 for (i = 0; i < current_panel->count; i++){
480 file = &current_panel->dir.list [i];
481 if (S_ISDIR (file->st.st_mode))
482 continue;
483 do_file_mark (current_panel, i, !file->f.marked);
487 static void
488 select_unselect_cmd (const char *title, const char *history_name, int cmd)
490 char *reg_exp, *reg_exp_t;
491 int i;
492 int c;
493 int dirflag = 0;
495 reg_exp = input_dialog (title, "", history_name, easy_patterns ? "*" : ".");
496 if (!reg_exp)
497 return;
498 if (!*reg_exp) {
499 g_free (reg_exp);
500 return;
503 reg_exp_t = reg_exp;
505 /* Check if they specified a directory */
506 if (*reg_exp_t == PATH_SEP) {
507 dirflag = 1;
508 reg_exp_t++;
510 if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) {
511 dirflag = 1;
512 reg_exp_t[strlen (reg_exp_t) - 1] = 0;
515 for (i = 0; i < current_panel->count; i++) {
516 if (!strcmp (current_panel->dir.list[i].fname, ".."))
517 continue;
518 if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) {
519 if (!dirflag)
520 continue;
521 } else {
522 if (dirflag)
523 continue;
525 c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname,
526 match_file);
527 if (c == -1) {
528 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
529 g_free (reg_exp);
530 return;
532 if (c) {
533 do_file_mark (current_panel, i, cmd);
536 g_free (reg_exp);
539 void select_cmd (void)
541 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", 1);
544 void unselect_cmd (void)
546 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", 0);
549 /* Check if the file exists */
550 /* If not copy the default */
551 static int
552 check_for_default(char *default_file, char *file)
554 struct stat s;
555 off_t count = 0;
556 double bytes = 0;
557 FileOpContext *ctx;
559 if (mc_stat (file, &s)){
560 if (mc_stat (default_file, &s)){
561 return -1;
563 ctx = file_op_context_new (OP_COPY);
564 file_op_context_create_ui (ctx, 0);
565 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
566 file_op_context_destroy (ctx);
568 return 0;
571 void ext_cmd (void)
573 char *buffer;
574 char *extdir;
575 int dir;
577 dir = 0;
578 if (geteuid () == 0){
579 dir = query_dialog (_("Extension file edit"),
580 _(" Which extension file you want to edit? "), D_NORMAL, 2,
581 _("&User"), _("&System Wide"));
583 extdir = mhl_str_dir_plus_file (mc_home, MC_LIB_EXT);
585 if (dir == 0){
586 buffer = mhl_str_dir_plus_file (home_dir, MC_USER_EXT);
587 check_for_default (extdir, buffer);
588 do_edit (buffer);
589 g_free (buffer);
590 } else if (dir == 1)
591 do_edit (extdir);
593 g_free (extdir);
594 flush_extension_file ();
597 /* where = 0 - do edit file menu for mc */
598 /* where = 1 - do edit file menu for mcedit */
599 static void
600 menu_edit_cmd (int where)
602 char *buffer;
603 char *menufile;
604 int dir = 0;
606 dir = query_dialog (
607 _(" Menu edit "),
608 _(" Which menu file do you want to edit? "),
609 D_NORMAL, geteuid() ? 2 : 3,
610 _("&Local"), _("&User"), _("&System Wide")
613 menufile = mhl_str_dir_plus_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
615 switch (dir) {
616 case 0:
617 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
618 check_for_default (menufile, buffer);
619 break;
621 case 1:
622 buffer = mhl_str_dir_plus_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
623 check_for_default (menufile, buffer);
624 break;
626 case 2:
627 buffer = mhl_str_dir_plus_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
628 break;
630 default:
631 g_free (menufile);
632 return;
634 do_edit (buffer);
635 if (dir == 0)
636 chmod(buffer, 0600);
637 g_free (buffer);
638 g_free (menufile);
641 void quick_chdir_cmd (void)
643 char *target;
645 target = hotlist_cmd (LIST_HOTLIST);
646 if (!target)
647 return;
649 if (get_current_type () == view_tree)
650 tree_chdir (the_tree, target);
651 else
652 if (!do_cd (target, cd_exact))
653 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
654 g_free (target);
657 /* edit file menu for mc */
658 void
659 edit_mc_menu_cmd (void)
661 menu_edit_cmd (0);
664 #ifdef USE_INTERNAL_EDIT
665 /* edit file menu for mcedit */
666 void
667 edit_user_menu_cmd (void)
669 menu_edit_cmd (1);
672 /* edit syntax file for mcedit */
673 void
674 edit_syntax_cmd (void)
676 char *buffer;
677 char *extdir;
678 int dir = 0;
680 if (geteuid () == 0) {
681 dir =
682 query_dialog (_("Syntax file edit"),
683 _(" Which syntax file you want to edit? "), D_NORMAL, 2,
684 _("&User"), _("&System Wide"));
686 extdir = mhl_str_dir_plus_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
688 if (dir == 0) {
689 buffer = mhl_str_dir_plus_file (home_dir, SYNTAX_FILE);
690 check_for_default (extdir, buffer);
691 do_edit (buffer);
692 g_free (buffer);
693 } else if (dir == 1)
694 do_edit (extdir);
696 g_free (extdir);
698 #endif
700 #ifdef USE_VFS
701 void reselect_vfs (void)
703 char *target;
705 target = hotlist_cmd (LIST_VFSLIST);
706 if (!target)
707 return;
709 if (!do_cd (target, cd_exact))
710 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
711 g_free (target);
713 #endif /* USE_VFS */
715 static int compare_files (char *name1, char *name2, off_t size)
717 int file1, file2;
718 int result = -1; /* Different by default */
720 file1 = open (name1, O_RDONLY);
721 if (file1 >= 0){
722 file2 = open (name2, O_RDONLY);
723 if (file2 >= 0){
724 #ifdef HAVE_MMAP
725 char *data1, *data2;
726 /* Ugly if jungle */
727 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
728 if (data1 != (char*) -1){
729 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
730 if (data2 != (char*) -1){
731 rotate_dash ();
732 result = memcmp (data1, data2, size);
733 munmap (data2, size);
735 munmap (data1, size);
737 #else
738 /* Don't have mmap() :( Even more ugly :) */
739 char buf1[BUFSIZ], buf2[BUFSIZ];
740 int n1, n2;
741 rotate_dash ();
744 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
745 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
746 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
747 result = (n1 != n2) || memcmp(buf1,buf2,n1);
748 #endif /* !HAVE_MMAP */
749 close (file2);
751 close (file1);
753 return result;
756 enum CompareMode {
757 compare_quick, compare_size_only, compare_thourough
760 static void
761 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
763 int i, j;
764 char *src_name, *dst_name;
766 /* No marks by default */
767 panel->marked = 0;
768 panel->total = 0;
769 panel->dirs_marked = 0;
771 /* Handle all files in the panel */
772 for (i = 0; i < panel->count; i++){
773 file_entry *source = &panel->dir.list[i];
775 /* Default: unmarked */
776 file_mark (panel, i, 0);
778 /* Skip directories */
779 if (S_ISDIR (source->st.st_mode))
780 continue;
782 /* Search the corresponding entry from the other panel */
783 for (j = 0; j < other->count; j++){
784 if (strcmp (source->fname,
785 other->dir.list[j].fname) == 0)
786 break;
788 if (j >= other->count)
789 /* Not found -> mark */
790 do_file_mark (panel, i, 1);
791 else {
792 /* Found */
793 file_entry *target = &other->dir.list[j];
795 if (mode != compare_size_only){
796 /* Older version is not marked */
797 if (source->st.st_mtime < target->st.st_mtime)
798 continue;
801 /* Newer version with different size is marked */
802 if (source->st.st_size != target->st.st_size){
803 do_file_mark (panel, i, 1);
804 continue;
807 if (mode == compare_size_only)
808 continue;
810 if (mode == compare_quick){
811 /* Thorough compare off, compare only time stamps */
812 /* Mark newer version, don't mark version with the same date */
813 if (source->st.st_mtime > target->st.st_mtime){
814 do_file_mark (panel, i, 1);
816 continue;
819 /* Thorough compare on, do byte-by-byte comparison */
820 src_name = mhl_str_dir_plus_file (panel->cwd, source->fname);
821 dst_name = mhl_str_dir_plus_file (other->cwd, target->fname);
822 if (compare_files (src_name, dst_name, source->st.st_size))
823 do_file_mark (panel, i, 1);
824 g_free (src_name);
825 g_free (dst_name);
827 } /* for (i ...) */
830 void
831 compare_dirs_cmd (void)
833 int choice;
834 enum CompareMode thorough_flag;
836 choice =
837 query_dialog (_(" Compare directories "),
838 _(" Select compare method: "), 0, D_NORMAL,
839 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
841 if (choice < 0 || choice > 2)
842 return;
843 else
844 thorough_flag = choice;
846 if (get_current_type () == view_listing
847 && get_other_type () == view_listing) {
848 compare_dir (current_panel, other_panel, thorough_flag);
849 compare_dir (other_panel, current_panel, thorough_flag);
850 } else {
851 message (D_ERROR, MSG_ERROR,
852 _(" Both panels should be in the "
853 "listing mode to use this command "));
857 void
858 history_cmd (void)
860 Listbox *listbox;
861 GList *current;
863 if (cmdline->need_push) {
864 if (push_history (cmdline, cmdline->buffer) == 2)
865 cmdline->need_push = 0;
867 if (!cmdline->history) {
868 message (D_ERROR, MSG_ERROR, _(" The command history is empty "));
869 return;
871 current = g_list_first (cmdline->history);
872 listbox = create_listbox_window (60, 10, _(" Command history "),
873 "[Command Menu]");
874 while (current) {
875 LISTBOX_APPEND_TEXT (listbox, 0, (char *) current->data, current);
876 current = g_list_next(current);
878 run_dlg (listbox->dlg);
879 if (listbox->dlg->ret_value == B_CANCEL)
880 current = NULL;
881 else
882 current = listbox->list->current->data;
883 destroy_dlg (listbox->dlg);
884 g_free (listbox);
886 if (!current)
887 return;
888 cmdline->history = current;
889 assign_text (cmdline, (char *) current->data);
890 update_input (cmdline, 1);
893 void swap_cmd (void)
895 swap_panels ();
896 touchwin (stdscr);
897 repaint_screen ();
900 void
901 view_other_cmd (void)
903 static int message_flag = TRUE;
905 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
906 if (message_flag)
907 message (D_ERROR, MSG_ERROR,
908 _(" Not an xterm or Linux console; \n"
909 " the panels cannot be toggled. "));
910 message_flag = FALSE;
911 } else {
912 toggle_panels ();
916 static void
917 do_link (int symbolic_link, const char *fname)
919 char *dest = NULL, *src = NULL;
921 if (!symbolic_link) {
922 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
923 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
924 if (!dest || !*dest)
925 goto cleanup;
926 save_cwds_stat ();
927 if (-1 == mc_link (fname, dest))
928 message (D_ERROR, MSG_ERROR, _(" link: %s "),
929 unix_error_string (errno));
930 } else {
931 char *s;
932 char *d;
934 /* suggest the full path for symlink */
935 s = mhl_str_dir_plus_file (current_panel->cwd, fname);
937 if (get_other_type () == view_listing) {
938 d = mhl_str_dir_plus_file (other_panel->cwd, fname);
939 } else {
940 d = g_strdup (fname);
943 symlink_dialog (s, d, &dest, &src);
944 g_free (d);
945 g_free (s);
947 if (!dest || !*dest || !src || !*src)
948 goto cleanup;
949 save_cwds_stat ();
950 if (-1 == mc_symlink (dest, src))
951 message (D_ERROR, MSG_ERROR, _(" symlink: %s "),
952 unix_error_string (errno));
954 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
955 repaint_screen ();
957 cleanup:
958 g_free (src);
959 g_free (dest);
962 void link_cmd (void)
964 do_link (0, selection (current_panel)->fname);
967 void symlink_cmd (void)
969 char *filename = NULL;
970 filename = selection (current_panel)->fname;
972 if (filename) {
973 do_link (1, filename);
977 void edit_symlink_cmd (void)
979 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
980 char buffer [MC_MAXPATHLEN];
981 char *p = NULL;
982 int i;
983 char *dest, *q;
985 p = selection (current_panel)->fname;
987 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
989 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
990 if (i > 0) {
991 buffer [i] = 0;
992 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
993 if (dest) {
994 if (*dest && strcmp (buffer, dest)) {
995 save_cwds_stat ();
996 if (-1 == mc_unlink (p)){
997 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
998 p, unix_error_string (errno));
999 } else {
1000 if (-1 == mc_symlink (dest, p))
1001 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
1002 unix_error_string (errno));
1004 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1005 repaint_screen ();
1007 g_free (dest);
1010 g_free (q);
1011 } else {
1012 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1013 selection (current_panel)->fname);
1017 void help_cmd (void)
1019 interactive_display (NULL, "[main]");
1022 void
1023 user_file_menu_cmd (void)
1025 user_menu_cmd (NULL);
1028 /* partly taken from dcigettext.c, returns "" for default locale */
1029 /* value should be freed by calling function g_free() */
1030 char *guess_message_value (void)
1032 static const char * const var[] = {
1033 /* The highest priority value is the `LANGUAGE' environment
1034 variable. This is a GNU extension. */
1035 "LANGUAGE",
1036 /* Setting of LC_ALL overwrites all other. */
1037 "LC_ALL",
1038 /* Next comes the name of the desired category. */
1039 "LC_MESSAGES",
1040 /* Last possibility is the LANG environment variable. */
1041 "LANG",
1042 /* NULL exit loops */
1043 NULL
1046 unsigned i = 0;
1047 const char *locale = NULL;
1049 while (var[i] != NULL) {
1050 locale = getenv (var[i]);
1051 if (locale != NULL && locale[0] != '\0')
1052 break;
1053 i++;
1056 if (locale == NULL)
1057 locale = "";
1059 return g_strdup (locale);
1063 * Return a random hint. If force is not 0, ignore the timeout.
1065 char *
1066 get_random_hint (int force)
1068 char *data, *result, *eol;
1069 int len;
1070 int start;
1071 static int last_sec;
1072 static struct timeval tv;
1074 /* Do not change hints more often than one minute */
1075 gettimeofday (&tv, NULL);
1076 if (!force && !(tv.tv_sec > last_sec + 60))
1077 return g_strdup ("");
1078 last_sec = tv.tv_sec;
1080 data = load_mc_home_file (MC_HINT, NULL);
1081 if (!data)
1082 return 0;
1084 /* get a random entry */
1085 srand (tv.tv_sec);
1086 len = strlen (data);
1087 start = rand () % len;
1089 for (; start; start--) {
1090 if (data[start] == '\n') {
1091 start++;
1092 break;
1095 eol = strchr (&data[start], '\n');
1096 if (eol)
1097 *eol = 0;
1098 result = g_strdup (&data[start]);
1099 g_free (data);
1100 return result;
1103 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1104 static void
1105 nice_cd (const char *text, const char *xtext, const char *help,
1106 const char *history_name, const char *prefix, int to_home)
1108 char *machine;
1109 char *cd_path;
1111 if (!SELECTED_IS_PANEL)
1112 return;
1114 machine = input_dialog_help (text, xtext, help, history_name, "");
1115 if (!machine)
1116 return;
1118 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1119 ugly as hell and leads to problems in vfs layer */
1121 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1122 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1123 else
1124 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1126 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1127 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1128 else
1129 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1130 g_free (cd_path);
1131 g_free (machine);
1133 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1136 #ifdef USE_NETCODE
1138 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1140 #ifdef WITH_MCFS
1141 void netlink_cmd (void)
1143 nice_cd (_(" Link to a remote machine "), _(machine_str),
1144 "[Network File System]", ":netlink_cmd: Link to a remote ",
1145 "/#mc:", 1);
1147 #endif /* WITH_MCFS */
1149 void ftplink_cmd (void)
1151 nice_cd (_(" FTP to machine "), _(machine_str),
1152 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1155 void fishlink_cmd (void)
1157 nice_cd (_(" Shell link to machine "), _(machine_str),
1158 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1159 "/#sh:", 1);
1162 #ifdef WITH_SMBFS
1163 void smblink_cmd (void)
1165 nice_cd (_(" SMB link to machine "), _(machine_str),
1166 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1167 "/#smb:", 0);
1169 #endif /* WITH_SMBFS */
1170 #endif /* USE_NETCODE */
1172 #ifdef USE_EXT2FSLIB
1173 void undelete_cmd (void)
1175 nice_cd (_(" Undelete files on an ext2 file system "),
1176 _(" Enter device (without /dev/) to undelete\n "
1177 " files on: (F1 for details)"),
1178 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1179 "/#undel:", 0);
1181 #endif /* USE_EXT2FSLIB */
1183 void quick_cd_cmd (void)
1185 char *p = cd_dialog ();
1187 if (p && *p) {
1188 char *q = g_strconcat ("cd ", p, (char *) NULL);
1190 do_cd_command (q);
1191 g_free (q);
1193 g_free (p);
1196 void
1197 single_dirsize_cmd (void)
1199 WPanel *panel = current_panel;
1200 file_entry *entry;
1201 off_t marked;
1202 double total;
1204 entry = &(panel->dir.list[panel->selected]);
1205 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1206 total = 0.0;
1207 compute_dir_size (entry->fname, &marked, &total);
1208 entry->st.st_size = (off_t) total;
1209 entry->f.dir_size_computed = 1;
1212 if (mark_moves_down)
1213 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1215 recalculate_panel_summary (panel);
1216 panel->dirty = 1;
1219 void
1220 dirsizes_cmd (void)
1222 WPanel *panel = current_panel;
1223 int i;
1224 off_t marked;
1225 double total;
1227 for (i = 0; i < panel->count; i++)
1228 if (S_ISDIR (panel->dir.list [i].st.st_mode) &&
1229 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1230 !panel->dirs_marked) &&
1231 strcmp (panel->dir.list [i].fname, "..") != 0) {
1232 total = 0.0l;
1233 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1234 panel->dir.list [i].st.st_size = (off_t) total;
1235 panel->dir.list [i].f.dir_size_computed = 1;
1238 recalculate_panel_summary (panel);
1239 panel_re_sort (panel);
1240 panel->dirty = 1;
1243 void
1244 save_setup_cmd (void)
1246 save_setup ();
1247 sync_profiles ();
1249 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"), PROFILE_NAME);
1252 static void
1253 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1255 p->user_mini_status = use_msformat;
1256 p->list_type = view_type;
1258 if (view_type == list_user || use_msformat){
1259 g_free (p->user_format);
1260 p->user_format = user;
1262 g_free (p->user_status_format [view_type]);
1263 p->user_status_format [view_type] = status;
1265 set_panel_formats (p);
1267 else {
1268 g_free (user);
1269 g_free (status);
1272 set_panel_formats (p);
1273 do_refresh ();
1276 void
1277 info_cmd_no_menu (void)
1279 if (get_display_type (0) == view_info)
1280 set_display_type (0, view_listing);
1281 else if (get_display_type (1) == view_info)
1282 set_display_type (1, view_listing);
1283 else
1284 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1287 void
1288 quick_cmd_no_menu (void)
1290 if (get_display_type (0) == view_quick)
1291 set_display_type (0, view_listing);
1292 else if (get_display_type (1) == view_quick)
1293 set_display_type (1, view_listing);
1294 else
1295 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1298 static void
1299 switch_to_listing (int panel_index)
1301 if (get_display_type (panel_index) != view_listing)
1302 set_display_type (panel_index, view_listing);
1305 void
1306 listing_cmd (void)
1308 int view_type, use_msformat;
1309 char *user, *status;
1310 WPanel *p;
1311 int display_type;
1313 display_type = get_display_type (MENU_PANEL_IDX);
1314 if (display_type == view_listing)
1315 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1316 else
1317 p = 0;
1319 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1321 if (view_type == -1)
1322 return;
1324 switch_to_listing (MENU_PANEL_IDX);
1326 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1328 configure_panel_listing (p, view_type, use_msformat, user, status);
1331 void
1332 tree_cmd (void)
1334 set_display_type (MENU_PANEL_IDX, view_tree);
1337 void
1338 info_cmd (void)
1340 set_display_type (MENU_PANEL_IDX, view_info);
1343 void
1344 quick_view_cmd (void)
1346 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1347 change_panel ();
1348 set_display_type (MENU_PANEL_IDX, view_quick);
1351 /* Handle the tree internal listing modes switching */
1352 static int
1353 set_basic_panel_listing_to (int panel_index, int listing_mode)
1355 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1357 switch_to_listing (panel_index);
1358 p->list_type = listing_mode;
1359 if (set_panel_formats (p))
1360 return 0;
1362 do_refresh ();
1363 return 1;
1366 void
1367 toggle_listing_cmd (void)
1369 int current = get_current_index ();
1370 WPanel *p = (WPanel *) get_panel_widget (current);
1372 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);