* extfs/trpm: Fix quoting issues.
[midnight-commander.git] / src / cmd.c
blobdf897d54a1f19890183e5642dcb69cdb3d3f2c9e
1 /* Routines invoked by a function key
2 They normally operate on the current panel.
4 Copyright (C) 1994, 1995 Miguel de Icaza
5 Copyright (C) 1994, 1995 Janne Kukonlehto
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <config.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <errno.h>
33 #ifdef USE_NETCODE
34 #include <netdb.h>
35 #endif
37 #ifdef HAVE_MMAP
38 # include <sys/mman.h>
39 #endif
41 #include "global.h"
42 #include "cmd.h" /* Our definitions */
43 #include "fileopctx.h" /* file_op_context_new() */
44 #include "file.h" /* copy_file_file() */
45 #include "find.h" /* do_find() */
46 #include "hotlist.h" /* hotlist_cmd() */
47 #include "tree.h" /* tree_chdir() */
48 #include "subshell.h" /* use_subshell */
49 #include "cons.saver.h" /* console_flag */
50 #include "tty.h" /* LINES */
51 #include "dialog.h" /* Widget */
52 #include "view.h" /* view() */
53 #include "wtools.h" /* message() */
54 #include "widget.h" /* push_history() */
55 #include "key.h" /* application_keypad_mode() */
56 #include "win.h" /* do_enter_ca_mode() */
57 #include "main.h" /* change_panel() */
58 #include "panel.h" /* current_panel */
59 #include "help.h" /* interactive_display() */
60 #include "user.h" /* MC_GLOBAL_MENU */
61 #include "command.h" /* cmdline */
62 #include "layout.h" /* get_current_type() */
63 #include "ext.h" /* regex_command() */
64 #include "boxes.h" /* cd_dialog() */
65 #include "setup.h" /* save_setup() */
66 #include "profile.h" /* PROFILE_NAME */
67 #include "execute.h" /* toggle_panels() */
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;
84 int
85 view_file_at_line (const char *filename, int plain_view, int internal,
86 int start_line)
88 static const char *viewer = NULL;
89 int move_dir = 0;
92 if (plain_view) {
93 int changed_hex_mode = 0;
94 int changed_nroff_flag = 0;
95 int changed_magic_flag = 0;
97 altered_hex_mode = 0;
98 altered_nroff_flag = 0;
99 altered_magic_flag = 0;
100 if (default_hex_mode)
101 changed_hex_mode = 1;
102 if (default_nroff_flag)
103 changed_nroff_flag = 1;
104 if (default_magic_flag)
105 changed_magic_flag = 1;
106 default_hex_mode = 0;
107 default_nroff_flag = 0;
108 default_magic_flag = 0;
109 view (0, filename, &move_dir, start_line);
110 if (changed_hex_mode && !altered_hex_mode)
111 default_hex_mode = 1;
112 if (changed_nroff_flag && !altered_nroff_flag)
113 default_nroff_flag = 1;
114 if (changed_magic_flag && !altered_magic_flag)
115 default_magic_flag = 1;
116 repaint_screen ();
117 return move_dir;
119 if (internal) {
120 char view_entry[BUF_TINY];
122 if (start_line != 0)
123 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
124 start_line);
125 else
126 strcpy (view_entry, "View");
128 if (regex_command (filename, view_entry, &move_dir) == 0) {
129 view (0, filename, &move_dir, start_line);
130 repaint_screen ();
132 } else {
133 if (!viewer) {
134 viewer = getenv ("PAGER");
135 if (!viewer)
136 viewer = "view";
138 execute_with_vfs_arg (viewer, filename);
140 return move_dir;
143 /* view_file (filename, plain_view, internal)
145 * Inputs:
146 * filename: The file name to view
147 * plain_view: If set does not do any fancy pre-processing (no filtering) and
148 * always invokes the internal viewer.
149 * internal: If set uses the internal viewer, otherwise an external viewer.
152 view_file (const char *filename, int plain_view, int internal)
154 return view_file_at_line (filename, plain_view, internal, 0);
157 /* scan_for_file (panel, idx, direction)
159 * Inputs:
160 * panel: pointer to the panel on which we operate
161 * idx: starting file.
162 * direction: 1, or -1
164 static int scan_for_file (WPanel *panel, int idx, int direction)
166 int i = idx + direction;
168 while (i != idx){
169 if (i < 0)
170 i = panel->count - 1;
171 if (i == panel->count)
172 i = 0;
173 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
174 return i;
175 i += direction;
177 return i;
181 * Run viewer (internal or external) on the currently selected file.
182 * If normal is 1, force internal viewer and raw mode (used for F13).
184 static void
185 do_view_cmd (int normal)
187 int dir, file_idx;
189 /* Directories are viewed by changing to them */
190 if (S_ISDIR (selection (current_panel)->st.st_mode)
191 || link_isdir (selection (current_panel))) {
192 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
193 if (query_dialog
194 (_(" Confirmation "), _("Files tagged, want to cd?"), 0, 2,
195 _("&Yes"), _("&No")) != 0) {
196 return;
199 if (!do_cd (selection (current_panel)->fname, cd_exact))
200 message (1, MSG_ERROR, _("Cannot change directory"));
202 return;
206 file_idx = current_panel->selected;
207 while (1) {
208 char *filename;
210 filename = current_panel->dir.list[file_idx].fname;
212 dir = view_file (filename, normal, use_internal_view);
213 if (dir == 0)
214 break;
215 file_idx = scan_for_file (current_panel, file_idx, dir);
219 /* Run user's preferred viewer on the currently selected file */
220 void
221 view_cmd (void)
223 do_view_cmd (0);
226 /* Ask for file and run user's preferred viewer on it */
227 void
228 view_file_cmd (void)
230 char *filename;
232 filename =
233 input_expand_dialog (_(" View file "), _(" Filename:"),
234 selection (current_panel)->fname);
235 if (!filename)
236 return;
238 view_file (filename, 0, use_internal_view);
239 g_free (filename);
242 /* Run plain internal viewer on the currently selected file */
243 void
244 view_simple_cmd (void)
246 do_view_cmd (1);
249 void
250 filtered_view_cmd (void)
252 char *command;
254 command =
255 input_dialog (_(" Filtered view "),
256 _(" Filter command and arguments:"),
257 selection (current_panel)->fname);
258 if (!command)
259 return;
261 view (command, "", 0, 0);
263 g_free (command);
266 void do_edit_at_line (const char *what, int start_line)
268 static const char *editor = NULL;
270 #ifdef USE_INTERNAL_EDIT
271 if (use_internal_edit){
272 edit_file (what, start_line);
273 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
274 repaint_screen ();
275 return;
277 #endif /* USE_INTERNAL_EDIT */
278 if (!editor){
279 editor = getenv ("EDITOR");
280 if (!editor)
281 editor = get_default_editor ();
283 execute_with_vfs_arg (editor, what);
284 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
285 repaint_screen ();
288 static void
289 do_edit (const char *what)
291 do_edit_at_line (what, 0);
294 void
295 edit_cmd (void)
297 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
298 do_edit (selection (current_panel)->fname);
301 void
302 edit_cmd_new (void)
304 do_edit (NULL);
307 /* Invoked by F5. Copy, default to the other panel. */
308 void
309 copy_cmd (void)
311 save_cwds_stat ();
312 if (panel_operate (current_panel, OP_COPY, 0)) {
313 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
314 repaint_screen ();
318 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
319 void ren_cmd (void)
321 save_cwds_stat ();
322 if (panel_operate (current_panel, OP_MOVE, 0)){
323 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
324 repaint_screen ();
328 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
329 void copy_cmd_local (void)
331 save_cwds_stat ();
332 if (panel_operate (current_panel, OP_COPY, 1)){
333 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
334 repaint_screen ();
338 /* Invoked by F16. Move/rename, default to the same panel. */
339 void ren_cmd_local (void)
341 save_cwds_stat ();
342 if (panel_operate (current_panel, OP_MOVE, 1)){
343 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
344 repaint_screen ();
348 void
349 mkdir_cmd (void)
351 char *tempdir;
352 char *dir;
354 dir =
355 input_expand_dialog (_("Create a new Directory"),
356 _(" Enter directory name:"), "");
358 if (!dir || !*dir)
359 return;
361 if (dir[0] == '/' || dir[0] == '~')
362 tempdir = g_strdup (dir);
363 else
364 tempdir = concat_dir_and_file (current_panel->cwd, dir);
365 g_free (dir);
367 save_cwds_stat ();
368 if (my_mkdir (tempdir, 0777) == 0) {
369 update_panels (UP_OPTIMIZE, tempdir);
370 repaint_screen ();
371 select_item (current_panel);
372 g_free (tempdir);
373 return;
375 g_free (tempdir);
376 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
379 void delete_cmd (void)
381 save_cwds_stat ();
383 if (panel_operate (current_panel, OP_DELETE, 0)){
384 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
385 repaint_screen ();
389 /* Invoked by F18. Remove selected file, regardless of marked files. */
390 void delete_cmd_local (void)
392 save_cwds_stat ();
394 if (panel_operate (current_panel, OP_DELETE, 1)){
395 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
396 repaint_screen ();
400 void find_cmd (void)
402 do_find ();
405 static void
406 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
408 g_free (p->filter);
409 p->filter = 0;
411 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
412 p->filter = allocated_filter_string;
413 else
414 g_free (allocated_filter_string);
415 reread_cmd ();
418 /* Set a given panel filter expression */
419 static void
420 set_panel_filter (WPanel *p)
422 char *reg_exp;
423 const char *x;
425 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
427 reg_exp = input_dialog_help (_(" Filter "),
428 _(" Set expression for filtering filenames"),
429 "[Filter...]", x);
430 if (!reg_exp)
431 return;
432 set_panel_filter_to (p, reg_exp);
435 /* Invoked from the left/right menus */
436 void filter_cmd (void)
438 WPanel *p;
440 if (!SELECTED_IS_PANEL)
441 return;
443 p = MENU_PANEL;
444 set_panel_filter (p);
447 void reread_cmd (void)
449 int flag;
451 if (get_current_type () == view_listing &&
452 get_other_type () == view_listing)
453 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
454 else
455 flag = UP_ONLY_CURRENT;
457 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
458 repaint_screen ();
461 void reverse_selection_cmd (void)
463 file_entry *file;
464 int i;
466 for (i = 0; i < current_panel->count; i++){
467 file = &current_panel->dir.list [i];
468 if (S_ISDIR (file->st.st_mode))
469 continue;
470 do_file_mark (current_panel, i, !file->f.marked);
474 static void
475 select_unselect_cmd (const char *title, int cmd)
477 char *reg_exp, *reg_exp_t;
478 int i;
479 int c;
480 int dirflag = 0;
482 reg_exp = input_dialog (title, "", easy_patterns ? "*" : ".");
483 if (!reg_exp)
484 return;
485 if (!*reg_exp) {
486 g_free (reg_exp);
487 return;
490 reg_exp_t = reg_exp;
492 /* Check if they specified a directory */
493 if (*reg_exp_t == PATH_SEP) {
494 dirflag = 1;
495 reg_exp_t++;
497 if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) {
498 dirflag = 1;
499 reg_exp_t[strlen (reg_exp_t) - 1] = 0;
502 for (i = 0; i < current_panel->count; i++) {
503 if (!strcmp (current_panel->dir.list[i].fname, ".."))
504 continue;
505 if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) {
506 if (!dirflag)
507 continue;
508 } else {
509 if (dirflag)
510 continue;
512 c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname,
513 match_file);
514 if (c == -1) {
515 message (1, MSG_ERROR, _(" Malformed regular expression "));
516 g_free (reg_exp);
517 return;
519 if (c) {
520 do_file_mark (current_panel, i, cmd);
523 g_free (reg_exp);
526 void select_cmd (void)
528 select_unselect_cmd (_(" Select "), 1);
531 void unselect_cmd (void)
533 select_unselect_cmd (_(" Unselect "), 0);
536 /* Check if the file exists */
537 /* If not copy the default */
538 static int
539 check_for_default(char *default_file, char *file)
541 struct stat s;
542 off_t count = 0;
543 double bytes = 0;
544 FileOpContext *ctx;
546 if (mc_stat (file, &s)){
547 if (mc_stat (default_file, &s)){
548 return -1;
550 ctx = file_op_context_new (OP_COPY);
551 file_op_context_create_ui (ctx, 0);
552 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
553 file_op_context_destroy (ctx);
555 return 0;
558 void ext_cmd (void)
560 char *buffer;
561 char *extdir;
562 int dir;
564 dir = 0;
565 if (geteuid () == 0){
566 dir = query_dialog (_("Extension file edit"),
567 _(" Which extension file you want to edit? "), 0, 2,
568 _("&User"), _("&System Wide"));
570 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
572 if (dir == 0){
573 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
574 check_for_default (extdir, buffer);
575 do_edit (buffer);
576 g_free (buffer);
577 } else if (dir == 1)
578 do_edit (extdir);
580 g_free (extdir);
581 flush_extension_file ();
584 /* where = 0 - do edit file menu for mc */
585 /* where = 1 - do edit file menu for mcedit */
586 static void
587 menu_edit_cmd (int where)
589 char *buffer;
590 char *menufile;
591 int dir = 0;
593 dir = query_dialog (
594 _(" Menu edit "),
595 _(" Which menu file do you want to edit? "),
596 0, geteuid() ? 2 : 3,
597 _("&Local"), _("&Home"), _("&System Wide")
600 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
602 switch (dir) {
603 case 0:
604 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
605 check_for_default (menufile, buffer);
606 break;
608 case 1:
609 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
610 check_for_default (menufile, buffer);
611 break;
613 case 2:
614 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
615 break;
617 default:
618 g_free (menufile);
619 return;
621 do_edit (buffer);
622 if (dir == 0)
623 chmod(buffer, 0600);
624 g_free (buffer);
625 g_free (menufile);
628 void quick_chdir_cmd (void)
630 char *target;
632 target = hotlist_cmd (LIST_HOTLIST);
633 if (!target)
634 return;
636 if (get_current_type () == view_tree)
637 tree_chdir (the_tree, target);
638 else
639 if (!do_cd (target, cd_exact))
640 message (1, MSG_ERROR, _("Cannot change directory") );
641 g_free (target);
644 /* edit file menu for mc */
645 void
646 edit_mc_menu_cmd (void)
648 menu_edit_cmd (0);
651 #ifdef USE_INTERNAL_EDIT
652 /* edit file menu for mcedit */
653 void
654 edit_user_menu_cmd (void)
656 menu_edit_cmd (1);
659 /* edit syntax file for mcedit */
660 void
661 edit_syntax_cmd (void)
663 char *buffer;
664 char *extdir;
665 int dir = 0;
667 if (geteuid () == 0) {
668 dir =
669 query_dialog (_("Syntax file edit"),
670 _(" Which syntax file you want to edit? "), 0, 2,
671 _("&User"), _("&System Wide"));
673 extdir = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
675 if (dir == 0) {
676 buffer = concat_dir_and_file (home_dir, SYNTAX_FILE);
677 check_for_default (extdir, buffer);
678 do_edit (buffer);
679 g_free (buffer);
680 } else if (dir == 1)
681 do_edit (extdir);
683 g_free (extdir);
685 #endif
687 #ifdef USE_VFS
688 void reselect_vfs (void)
690 char *target;
692 target = hotlist_cmd (LIST_VFSLIST);
693 if (!target)
694 return;
696 if (!do_cd (target, cd_exact))
697 message (1, MSG_ERROR, _("Cannot change directory") );
698 g_free (target);
700 #endif /* USE_VFS */
702 static int compare_files (char *name1, char *name2, off_t size)
704 int file1, file2;
705 int result = -1; /* Different by default */
707 file1 = open (name1, O_RDONLY);
708 if (file1 >= 0){
709 file2 = open (name2, O_RDONLY);
710 if (file2 >= 0){
711 #ifdef HAVE_MMAP
712 char *data1, *data2;
713 /* Ugly if jungle */
714 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
715 if (data1 != (char*) -1){
716 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
717 if (data2 != (char*) -1){
718 rotate_dash ();
719 result = memcmp (data1, data2, size);
720 munmap (data2, size);
722 munmap (data1, size);
724 #else
725 /* Don't have mmap() :( Even more ugly :) */
726 char buf1[BUFSIZ], buf2[BUFSIZ];
727 int n1, n2;
728 rotate_dash ();
731 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
732 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
733 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
734 result = (n1 != n2) || memcmp(buf1,buf2,n1);
735 #endif /* !HAVE_MMAP */
736 close (file2);
738 close (file1);
740 return result;
743 enum CompareMode {
744 compare_quick, compare_size_only, compare_thourough
747 static void
748 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
750 int i, j;
751 char *src_name, *dst_name;
753 /* No marks by default */
754 panel->marked = 0;
755 panel->total = 0;
756 panel->dirs_marked = 0;
758 /* Handle all files in the panel */
759 for (i = 0; i < panel->count; i++){
760 file_entry *source = &panel->dir.list[i];
762 /* Default: unmarked */
763 file_mark (panel, i, 0);
765 /* Skip directories */
766 if (S_ISDIR (source->st.st_mode))
767 continue;
769 /* Search the corresponding entry from the other panel */
770 for (j = 0; j < other->count; j++){
771 if (strcmp (source->fname,
772 other->dir.list[j].fname) == 0)
773 break;
775 if (j >= other->count)
776 /* Not found -> mark */
777 do_file_mark (panel, i, 1);
778 else {
779 /* Found */
780 file_entry *target = &other->dir.list[j];
782 if (mode != compare_size_only){
783 /* Older version is not marked */
784 if (source->st.st_mtime < target->st.st_mtime)
785 continue;
788 /* Newer version with different size is marked */
789 if (source->st.st_size != target->st.st_size){
790 do_file_mark (panel, i, 1);
791 continue;
794 if (mode == compare_size_only)
795 continue;
797 if (mode == compare_quick){
798 /* Thorough compare off, compare only time stamps */
799 /* Mark newer version, don't mark version with the same date */
800 if (source->st.st_mtime > target->st.st_mtime){
801 do_file_mark (panel, i, 1);
803 continue;
806 /* Thorough compare on, do byte-by-byte comparison */
807 src_name = concat_dir_and_file (panel->cwd, source->fname);
808 dst_name = concat_dir_and_file (other->cwd, target->fname);
809 if (compare_files (src_name, dst_name, source->st.st_size))
810 do_file_mark (panel, i, 1);
811 g_free (src_name);
812 g_free (dst_name);
814 } /* for (i ...) */
817 void
818 compare_dirs_cmd (void)
820 int choice;
821 enum CompareMode thorough_flag;
823 choice =
824 query_dialog (_(" Compare directories "),
825 _(" Select compare method: "), 0, 3, _("&Quick"),
826 _("&Size only"), _("&Thorough"), _("&Cancel"));
828 if (choice < 0 || choice > 2)
829 return;
830 else
831 thorough_flag = choice;
833 if (get_current_type () == view_listing
834 && get_other_type () == view_listing) {
835 compare_dir (current_panel, other_panel, thorough_flag);
836 compare_dir (other_panel, current_panel, thorough_flag);
837 } else {
838 message (1, MSG_ERROR,
839 _(" Both panels should be in the "
840 "listing mode to use this command "));
844 void
845 history_cmd (void)
847 Listbox *listbox;
848 GList *current;
850 if (cmdline->need_push) {
851 if (push_history (cmdline, cmdline->buffer) == 2)
852 cmdline->need_push = 0;
854 if (!cmdline->history) {
855 message (1, MSG_ERROR, _(" The command history is empty "));
856 return;
858 current = g_list_first (cmdline->history);
859 listbox = create_listbox_window (60, 10, _(" Command history "),
860 "[Command Menu]");
861 while (current) {
862 LISTBOX_APPEND_TEXT (listbox, 0, (char *) current->data, current);
863 current = g_list_next(current);
865 run_dlg (listbox->dlg);
866 if (listbox->dlg->ret_value == B_CANCEL)
867 current = NULL;
868 else
869 current = listbox->list->current->data;
870 destroy_dlg (listbox->dlg);
871 g_free (listbox);
873 if (!current)
874 return;
875 cmdline->history = current;
876 assign_text (cmdline, (char *) current->data);
877 update_input (cmdline, 1);
880 void swap_cmd (void)
882 swap_panels ();
883 touchwin (stdscr);
884 repaint_screen ();
887 void
888 view_other_cmd (void)
890 static int message_flag = TRUE;
892 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
893 if (message_flag)
894 message (1, MSG_ERROR,
895 _(" Not an xterm or Linux console; \n"
896 " the panels cannot be toggled. "));
897 message_flag = FALSE;
898 } else {
899 toggle_panels ();
903 static void
904 do_link (int symbolic_link, const char *fname)
906 char *dest = NULL, *src = NULL;
908 if (!symbolic_link) {
909 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
910 dest = input_expand_dialog (_(" Link "), src, "");
911 if (!dest || !*dest)
912 goto cleanup;
913 save_cwds_stat ();
914 if (-1 == mc_link (fname, dest))
915 message (1, MSG_ERROR, _(" link: %s "),
916 unix_error_string (errno));
917 } else {
918 char *s;
919 char *d;
921 /* suggest the full path for symlink */
922 s = concat_dir_and_file (current_panel->cwd, fname);
924 if (get_other_type () == view_listing) {
925 d = concat_dir_and_file (other_panel->cwd, fname);
926 } else {
927 d = g_strdup (fname);
930 symlink_dialog (s, d, &dest, &src);
931 g_free (d);
932 g_free (s);
934 if (!dest || !*dest || !src || !*src)
935 goto cleanup;
936 save_cwds_stat ();
937 if (-1 == mc_symlink (dest, src))
938 message (1, MSG_ERROR, _(" symlink: %s "),
939 unix_error_string (errno));
941 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
942 repaint_screen ();
944 cleanup:
945 g_free (src);
946 g_free (dest);
949 void link_cmd (void)
951 do_link (0, selection (current_panel)->fname);
954 void symlink_cmd (void)
956 char *filename = NULL;
957 filename = selection (current_panel)->fname;
959 if (filename) {
960 do_link (1, filename);
964 void edit_symlink_cmd (void)
966 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
967 char buffer [MC_MAXPATHLEN];
968 char *p = NULL;
969 int i;
970 char *dest, *q;
972 p = selection (current_panel)->fname;
974 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
976 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
977 if (i > 0) {
978 buffer [i] = 0;
979 dest = input_expand_dialog (_(" Edit symlink "), q, buffer);
980 if (dest) {
981 if (*dest && strcmp (buffer, dest)) {
982 save_cwds_stat ();
983 if (-1 == mc_unlink (p)){
984 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
985 p, unix_error_string (errno));
986 } else {
987 if (-1 == mc_symlink (dest, p))
988 message (1, MSG_ERROR, _(" edit symlink: %s "),
989 unix_error_string (errno));
991 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
992 repaint_screen ();
994 g_free (dest);
997 g_free (q);
998 } else {
999 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1000 selection (current_panel)->fname);
1004 void help_cmd (void)
1006 interactive_display (NULL, "[main]");
1009 void
1010 user_file_menu_cmd (void)
1012 user_menu_cmd (NULL);
1015 /* partly taken from dcigettext.c, returns "" for default locale */
1016 /* value should be freed by calling function g_free() */
1017 char *guess_message_value (void)
1019 static const char * const var[] = {
1020 /* The highest priority value is the `LANGUAGE' environment
1021 variable. This is a GNU extension. */
1022 "LANGUAGE",
1023 /* Setting of LC_ALL overwrites all other. */
1024 "LC_ALL",
1025 /* Next comes the name of the desired category. */
1026 "LC_MESSAGES",
1027 /* Last possibility is the LANG environment variable. */
1028 "LANG",
1029 /* NULL exit loops */
1030 NULL
1033 unsigned i = 0;
1034 const char *locale = NULL;
1036 while (var[i] != NULL) {
1037 locale = getenv (var[i]);
1038 if (locale != NULL && locale[0] != '\0')
1039 break;
1040 i++;
1043 if (locale == NULL)
1044 locale = "";
1046 return g_strdup (locale);
1050 * Return a random hint. If force is not 0, ignore the timeout.
1052 char *
1053 get_random_hint (int force)
1055 char *data, *result, *eol;
1056 int len;
1057 int start;
1058 static int last_sec;
1059 static struct timeval tv;
1061 /* Do not change hints more often than one minute */
1062 gettimeofday (&tv, NULL);
1063 if (!force && !(tv.tv_sec > last_sec + 60))
1064 return g_strdup ("");
1065 last_sec = tv.tv_sec;
1067 data = load_mc_home_file (MC_HINT, NULL);
1068 if (!data)
1069 return 0;
1071 /* get a random entry */
1072 srand (tv.tv_sec);
1073 len = strlen (data);
1074 start = rand () % len;
1076 for (; start; start--) {
1077 if (data[start] == '\n') {
1078 start++;
1079 break;
1082 eol = strchr (&data[start], '\n');
1083 if (eol)
1084 *eol = 0;
1085 result = g_strdup (&data[start]);
1086 g_free (data);
1087 return result;
1090 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1091 static void
1092 nice_cd (const char *text, const char *xtext, const char *help, const char *prefix, int to_home)
1094 char *machine;
1095 char *cd_path;
1097 if (!SELECTED_IS_PANEL)
1098 return;
1100 machine = input_dialog_help (text,
1101 xtext,
1102 help, "");
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]", "/#mc:", 1);
1134 #endif /* WITH_MCFS */
1136 void ftplink_cmd (void)
1138 nice_cd (_(" FTP to machine "), _(machine_str),
1139 "[FTP File System]", "/#ftp:", 1);
1142 void fishlink_cmd (void)
1144 nice_cd (_(" Shell link to machine "), _(machine_str),
1145 "[FIle transfer over SHell filesystem]", "/#sh:", 1);
1148 #ifdef WITH_SMBFS
1149 void smblink_cmd (void)
1151 nice_cd (_(" SMB link to machine "), _(machine_str),
1152 "[SMB File System]", "/#smb:", 0);
1154 #endif /* WITH_SMBFS */
1155 #endif /* USE_NETCODE */
1157 #ifdef USE_EXT2FSLIB
1158 void undelete_cmd (void)
1160 nice_cd (_(" Undelete files on an ext2 file system "),
1161 _(" Enter device (without /dev/) to undelete\n "
1162 " files on: (F1 for details)"),
1163 "[Undelete File System]", "/#undel:", 0);
1165 #endif /* USE_EXT2FSLIB */
1167 void quick_cd_cmd (void)
1169 char *p = cd_dialog ();
1171 if (p && *p) {
1172 char *q = g_strconcat ("cd ", p, (char *) NULL);
1174 do_cd_command (q);
1175 g_free (q);
1177 g_free (p);
1180 void
1181 dirsizes_cmd (void)
1183 WPanel *panel = current_panel;
1184 int i;
1185 off_t marked;
1186 double total;
1188 for (i = 0; i < panel->count; i++)
1189 if (S_ISDIR (panel->dir.list [i].st.st_mode) &&
1190 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1191 !panel->dirs_marked) &&
1192 strcmp (panel->dir.list [i].fname, "..") != 0) {
1193 total = 0.0l;
1194 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1195 panel->dir.list [i].st.st_size = (off_t) total;
1196 panel->dir.list [i].f.dir_size_computed = 1;
1199 recalculate_panel_summary (panel);
1200 panel->dirty = 1;
1203 void
1204 save_setup_cmd (void)
1206 char *str;
1208 save_setup ();
1209 sync_profiles ();
1210 str = g_strconcat ( _(" Setup saved to ~/"), PROFILE_NAME, (char *) NULL);
1212 message (0, _(" Setup "), str);
1213 g_free (str);
1216 static void
1217 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1219 p->user_mini_status = use_msformat;
1220 p->list_type = view_type;
1222 if (view_type == list_user || use_msformat){
1223 g_free (p->user_format);
1224 p->user_format = user;
1226 g_free (p->user_status_format [view_type]);
1227 p->user_status_format [view_type] = status;
1229 set_panel_formats (p);
1231 else {
1232 g_free (user);
1233 g_free (status);
1236 set_panel_formats (p);
1237 do_refresh ();
1240 void
1241 info_cmd_no_menu (void)
1243 if (get_display_type (0) == view_info)
1244 set_display_type (0, view_listing);
1245 else if (get_display_type (1) == view_info)
1246 set_display_type (1, view_listing);
1247 else
1248 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1251 void
1252 quick_cmd_no_menu (void)
1254 if (get_display_type (0) == view_quick)
1255 set_display_type (0, view_listing);
1256 else if (get_display_type (1) == view_quick)
1257 set_display_type (1, view_listing);
1258 else
1259 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1262 static void
1263 switch_to_listing (int panel_index)
1265 if (get_display_type (panel_index) != view_listing)
1266 set_display_type (panel_index, view_listing);
1269 void
1270 listing_cmd (void)
1272 int view_type, use_msformat;
1273 char *user, *status;
1274 WPanel *p;
1275 int display_type;
1277 display_type = get_display_type (MENU_PANEL_IDX);
1278 if (display_type == view_listing)
1279 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1280 else
1281 p = 0;
1283 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1285 if (view_type == -1)
1286 return;
1288 switch_to_listing (MENU_PANEL_IDX);
1290 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1292 configure_panel_listing (p, view_type, use_msformat, user, status);
1295 void
1296 tree_cmd (void)
1298 set_display_type (MENU_PANEL_IDX, view_tree);
1301 void
1302 info_cmd (void)
1304 set_display_type (MENU_PANEL_IDX, view_info);
1307 void
1308 quick_view_cmd (void)
1310 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1311 change_panel ();
1312 set_display_type (MENU_PANEL_IDX, view_quick);
1315 /* Handle the tree internal listing modes switching */
1316 static int
1317 set_basic_panel_listing_to (int panel_index, int listing_mode)
1319 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1321 switch_to_listing (panel_index);
1322 p->list_type = listing_mode;
1323 if (set_panel_formats (p))
1324 return 0;
1326 do_refresh ();
1327 return 1;
1330 void
1331 toggle_listing_cmd (void)
1333 int current = get_current_index ();
1334 WPanel *p = (WPanel *) get_panel_widget (current);
1335 int list_mode = p->list_type;
1336 int m;
1338 switch (list_mode){
1339 case list_full:
1340 case list_brief:
1341 m = list_long;
1342 break;
1343 case list_long:
1344 m = list_user;
1345 break;
1346 default:
1347 m = list_full;
1349 if (set_basic_panel_listing_to (current, m))
1350 return;
1351 set_basic_panel_listing_to (current, list_full);