*** empty log message ***
[midnight-commander.git] / src / cmd.c
blobfa35b2a96ea4834923f35fa425ad7a8892c18d7d
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>
22 #ifdef __os2__
23 # define INCL_DOSFILEMGR
24 # define INCL_DOSMISC
25 # define INCL_DOSERROR
26 #endif
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <string.h>
36 #include <fcntl.h> /* open, O_RDWR */
37 #include <errno.h>
39 #ifdef USE_NETCODE
40 #include <netdb.h>
41 #endif
43 #ifdef HAVE_MMAP
44 # include <sys/mman.h>
45 #endif
47 #include "global.h"
48 #include "tty.h"
49 #include "dir.h"
50 #include "panel.h"
51 #include "cmd.h" /* Our definitions */
52 #include "view.h" /* view() */
53 #include "dialog.h" /* query_dialog, message */
54 #include "file.h" /* the file operations */
55 #include "fileopctx.h"
56 #include "find.h" /* do_find */
57 #include "hotlist.h"
58 #include "tree.h"
59 #include "subshell.h" /* use_subshell */
60 #include "cons.saver.h"
61 #include "dlg.h" /* required by wtools.h */
62 #include "widget.h" /* required by wtools.h */
63 #include "wtools.h" /* listbox */
64 #include "command.h" /* for input_w */
65 #include "win.h" /* do_exit_ca_mode */
66 #include "layout.h" /* get_current/other_type */
67 #include "ext.h" /* regex_command */
68 #include "key.h" /* get_key_code */
69 #include "help.h" /* interactive_display */
70 #include "boxes.h" /* cd_dialog */
71 #include "color.h"
72 #include "user.h"
73 #include "setup.h"
74 #include "profile.h"
76 #include "vfs/vfs.h"
77 #define WANT_WIDGETS
78 #include "main.h" /* global variables, global functions */
79 #ifndef MAP_FILE
80 # define MAP_FILE 0
81 #endif
83 /* If set and you don't have subshell support,then C-o will give you a shell */
84 int output_starts_shell = 0;
86 /* Source routing destination */
87 int source_route = 0;
89 /* If set, use the builtin editor */
90 int use_internal_edit = 1;
92 /* Ugly hack in order to distinguish between left and right panel in menubar */
93 int is_right;
94 #define MENU_PANEL_IDX (is_right ? 1 : 0)
97 /* This is used since the parameter panel on some of the commands */
98 /* defined in this file may receive a 0 parameter if they are invoked */
99 /* The drop down menu */
101 static WPanel *
102 get_a_panel (WPanel *panel)
104 if (panel)
105 return panel;
106 if (get_current_type () == view_listing){
107 return cpanel;
108 } else
109 return other_panel;
112 /* view_file (filename, normal, internal)
114 * Inputs:
115 * filename: The file name to view
116 * plain_view: If set does not do any fancy pre-processing (no filtering) and
117 * always invokes the internal viewer.
118 * internal: If set uses the internal viewer, otherwise an external viewer.
120 int view_file_at_line (char *filename, int plain_view, int internal, int start_line)
122 static char *viewer = 0;
123 int move_dir = 0;
126 if (plain_view) {
127 int changed_hex_mode = 0;
128 int changed_nroff_flag = 0;
129 int changed_magic_flag = 0;
131 altered_hex_mode = 0;
132 altered_nroff_flag = 0;
133 altered_magic_flag = 0;
134 if (default_hex_mode)
135 changed_hex_mode = 1;
136 if (default_nroff_flag)
137 changed_nroff_flag = 1;
138 if (default_magic_flag)
139 changed_magic_flag = 1;
140 default_hex_mode = 0;
141 default_nroff_flag = 0;
142 default_magic_flag = 0;
143 view (0, filename, &move_dir, start_line);
144 if (changed_hex_mode && !altered_hex_mode)
145 default_hex_mode = 1;
146 if (changed_nroff_flag && !altered_nroff_flag)
147 default_nroff_flag = 1;
148 if (changed_magic_flag && !altered_magic_flag)
149 default_magic_flag = 1;
150 repaint_screen ();
151 return move_dir;
153 if (internal){
154 char view_entry [BUF_TINY];
156 if (start_line != 0)
157 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
158 else
159 strcpy (view_entry, "View");
161 if (!regex_command (filename, view_entry, NULL, &move_dir)){
162 view (0, filename, &move_dir, start_line);
163 repaint_screen ();
165 } else {
166 char *localcopy;
168 if (!viewer){
169 viewer = getenv ("PAGER");
170 if (!viewer)
171 viewer = "view";
173 /* The file may be a non local file, get a copy */
174 if (!vfs_file_is_local (filename)){
175 localcopy = mc_getlocalcopy (filename);
176 if (localcopy == NULL){
177 message (1, MSG_ERROR, _(" Can not fetch a local copy of %s "), filename);
178 return 0;
180 execute_internal (viewer, localcopy);
181 mc_ungetlocalcopy (filename, localcopy, 0);
182 } else
183 execute_internal (viewer, filename);
185 return move_dir;
189 view_file (char *filename, int plain_view, int internal)
191 return view_file_at_line (filename, plain_view, internal, 0);
194 /* scan_for_file (panel, idx, direction)
196 * Inputs:
197 * panel: pointer to the panel on which we operate
198 * idx: starting file.
199 * direction: 1, or -1
201 static int scan_for_file (WPanel *panel, int idx, int direction)
203 int i = idx + direction;
205 while (i != idx){
206 if (i < 0)
207 i = panel->count - 1;
208 if (i == panel->count)
209 i = 0;
210 if (!S_ISDIR (panel->dir.list [i].buf.st_mode))
211 return i;
212 i += direction;
214 return i;
217 /* do_view: Invoked as the F3/F13 key. */
218 static void do_view_cmd (WPanel *panel, int normal)
220 int dir, file_idx;
221 panel = get_a_panel (panel);
223 /* Directories are viewed by changing to them */
224 if (S_ISDIR (selection (panel)->buf.st_mode) ||
225 link_isdir (selection (panel))) {
226 if (confirm_view_dir && (panel->marked || panel->dirs_marked)){
227 if (query_dialog (_(" CD "), _("Files tagged, want to cd?"),
228 0, 2, _("&Yes"), _("&No")) == 1){
229 return;
232 if (!do_cd (selection (panel)->fname, cd_exact))
233 message (1, MSG_ERROR, _("Could not change directory") );
235 return;
239 file_idx = panel->selected;
240 while (1) {
241 char *filename;
243 filename = panel->dir.list [file_idx].fname;
245 dir = view_file (filename, normal, use_internal_view);
246 if (dir == 0)
247 break;
248 file_idx = scan_for_file (panel, file_idx, dir);
252 void view_cmd (WPanel *panel)
254 do_view_cmd (panel, 0);
257 void view_file_cmd (WPanel *panel)
259 char *filename;
261 panel = get_a_panel (panel);
262 filename = input_dialog (_(" View file "), _(" Filename:"), selection (panel)->fname);
263 if (!filename) return;
265 view_file (filename, 0, use_internal_view);
266 g_free (filename);
269 void view_simple_cmd (WPanel *panel)
271 do_view_cmd (panel, 1);
274 void filtered_view_cmd (WPanel *panel)
276 char *command;
278 panel = get_a_panel (panel);
279 command = input_dialog (_(" Filtered view "), _(" Filter command and arguments:"),
280 selection (panel)->fname);
281 if (!command)
282 return;
284 view (command, "", 0, 0);
286 g_free (command);
289 void filtered_view_cmd_cpanel (void)
291 filtered_view_cmd (cpanel);
294 void do_edit_at_line (const char *what, int start_line)
296 static char *editor = 0;
298 #ifdef USE_INTERNAL_EDIT
299 if (use_internal_edit){
300 edit (what, start_line);
301 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
302 repaint_screen ();
303 return;
305 #endif /* USE_INTERNAL_EDIT */
306 if (!editor){
307 editor = getenv ("EDITOR");
308 if (!editor)
309 editor = get_default_editor ();
311 execute_internal (editor, what);
312 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
313 repaint_screen ();
316 void
317 do_edit (const char *what)
319 do_edit_at_line (what, 1);
322 void edit_cmd (WPanel *panel)
324 panel = get_a_panel(panel);
325 if (!regex_command (selection (panel)->fname, "Edit", NULL, 0))
326 do_edit (selection (panel)->fname);
329 void edit_cmd_new (WPanel *panel)
331 do_edit ("");
334 void copy_cmd (void)
336 save_cwds_stat ();
337 if (panel_operate (cpanel, OP_COPY, NULL, TRUE)){
338 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
339 repaint_screen ();
343 void ren_cmd (void)
345 save_cwds_stat ();
346 if (panel_operate (cpanel, OP_MOVE, NULL, TRUE)){
347 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
348 repaint_screen ();
352 void copymove_cmd_with_default (int copy, char *thedefault)
354 save_cwds_stat ();
355 if (panel_operate (cpanel, copy ? OP_COPY : OP_MOVE, thedefault, TRUE)){
356 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
357 repaint_screen ();
361 void mkdir_cmd (WPanel *panel)
363 char *tempdir;
364 char *dir;
366 panel = get_a_panel (panel);
367 dir = input_expand_dialog (_("Create a new Directory"), _(" Enter directory name:") , "");
369 if (!dir)
370 return;
372 if (dir [0] && (dir [0] == '/' || dir [0] == '~'))
373 tempdir = g_strdup (dir);
374 else
375 tempdir = concat_dir_and_file (panel->cwd, dir);
376 g_free (dir);
378 save_cwds_stat ();
379 if (my_mkdir (tempdir, 0777) == 0){
380 update_panels (UP_OPTIMIZE, tempdir);
381 repaint_screen ();
382 select_item (cpanel);
383 g_free (tempdir);
384 return;
386 g_free (tempdir);
387 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
390 void delete_cmd (void)
392 save_cwds_stat ();
394 if (panel_operate (cpanel, OP_DELETE, NULL, TRUE)){
395 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
396 repaint_screen ();
400 void find_cmd (void)
402 do_find ();
405 void
406 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
408 if (p->filter){
409 g_free (p->filter);
410 p->filter = 0;
412 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
413 p->filter = allocated_filter_string;
414 else
415 g_free (allocated_filter_string);
416 reread_cmd ();
419 /* Set a given panel filter expression */
420 void set_panel_filter (WPanel *p)
422 char *reg_exp;
423 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 mad_check (__FILE__, __LINE__);
452 if (get_current_type () == view_listing &&
453 get_other_type () == view_listing)
454 flag = strcmp (cpanel->cwd, opanel->cwd) ? UP_ONLY_CURRENT : 0;
455 else
456 flag = UP_ONLY_CURRENT;
458 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
459 repaint_screen ();
462 /* Panel sorting related routines */
463 void do_re_sort (WPanel *panel)
465 panel = get_a_panel (panel);
466 panel_re_sort (panel);
469 void reverse_selection_cmd_panel (WPanel *panel)
471 file_entry *file;
472 int i;
474 for (i = 0; i < panel->count; i++){
475 file = &panel->dir.list [i];
476 if (S_ISDIR (file->buf.st_mode))
477 continue;
478 do_file_mark (panel, i, !file->f.marked);
480 paint_panel (panel);
483 void reverse_selection_cmd (void)
485 reverse_selection_cmd_panel (cpanel);
488 void select_cmd_panel (WPanel *panel)
490 char *reg_exp, *reg_exp_t;
491 int i;
492 int c;
493 int dirflag = 0;
495 reg_exp = input_dialog (_(" Select "), "", easy_patterns ? "*" : ".");
496 if (!reg_exp)
497 return;
499 reg_exp_t = reg_exp;
501 /* Check if they specified a directory */
502 if (*reg_exp_t == PATH_SEP){
503 dirflag = 1;
504 reg_exp_t++;
506 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
507 dirflag = 1;
508 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
511 for (i = 0; i < panel->count; i++){
512 if (!strcmp (panel->dir.list [i].fname, ".."))
513 continue;
514 if (S_ISDIR (panel->dir.list [i].buf.st_mode)){
515 if (!dirflag)
516 continue;
517 } else {
518 if (dirflag)
519 continue;
521 c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file);
522 if (c == -1){
523 message (1, MSG_ERROR, _(" Malformed regular expression "));
524 g_free (reg_exp);
525 return;
527 if (c){
528 do_file_mark (panel, i, 1);
531 paint_panel (panel);
532 g_free (reg_exp);
535 void select_cmd (void)
537 select_cmd_panel (cpanel);
540 void unselect_cmd_panel (WPanel *panel)
542 char *reg_exp, *reg_exp_t;
543 int i;
544 int c;
545 int dirflag = 0;
547 reg_exp = input_dialog (_(" Unselect "),"", easy_patterns ? "*" : ".");
548 if (!reg_exp)
549 return;
551 reg_exp_t = reg_exp;
553 /* Check if they specified directory matching */
554 if (*reg_exp_t == PATH_SEP){
555 dirflag = 1;
556 reg_exp_t ++;
558 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
559 dirflag = 1;
560 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
562 for (i = 0; i < panel->count; i++){
563 if (!strcmp (panel->dir.list [i].fname, ".."))
564 continue;
565 if (S_ISDIR (panel->dir.list [i].buf.st_mode)){
566 if (!dirflag)
567 continue;
568 } else {
569 if (dirflag)
570 continue;
572 c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file);
573 if (c == -1){
574 message (1, MSG_ERROR, _(" Malformed regular expression "));
575 g_free (reg_exp);
576 return;
578 if (c){
579 do_file_mark (panel, i, 0);
582 paint_panel (panel);
583 g_free (reg_exp);
586 void unselect_cmd (void)
588 unselect_cmd_panel (cpanel);
591 /* Check if the file exists */
592 /* If not copy the default */
593 int check_for_default(char *default_file, char *file)
595 struct stat s;
596 off_t count = 0;
597 double bytes = 0;
598 FileOpContext *ctx;
600 if (mc_stat (file, &s)){
601 if (mc_stat (default_file, &s)){
602 return -1;
604 ctx = file_op_context_new ();
605 file_op_context_create_ui (ctx, OP_COPY, 0);
606 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
607 file_op_context_destroy (ctx);
609 return 0;
612 void ext_cmd (void)
614 char *buffer;
615 char *extdir;
616 int dir;
618 dir = 0;
619 if (geteuid () == 0){
620 dir = query_dialog (_("Extension file edit"),
621 _(" Which extension file you want to edit? "), 0, 2,
622 _("&User"), _("&System Wide"));
624 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
626 if (dir == 0){
627 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
628 check_for_default (extdir, buffer);
629 do_edit (buffer);
630 g_free (buffer);
631 } else if (dir == 1)
632 do_edit (extdir);
634 g_free (extdir);
635 flush_extension_file ();
638 /* where = 0 - do edit a file menu for mc */
639 /* where = 1 - do edit a file menu for cool edit */
640 void menu_edit_cmd (int where)
642 char *buffer;
643 char *menufile;
644 int dir = 0;
646 dir = query_dialog (
647 _(" Menu edit "),
648 _(" Which menu file will you edit ? "),
649 0, geteuid() ? 2 : 3,
650 _("&Local"), _("&Home"), _("&System Wide")
653 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
655 switch (dir) {
656 case 0:
657 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
658 check_for_default (menufile, buffer);
659 break;
661 case 1:
662 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
663 check_for_default (menufile, buffer);
664 break;
666 case 2:
667 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
668 break;
670 default:
671 g_free (menufile);
672 return;
674 do_edit (buffer);
675 if (dir == 0)
676 chmod(buffer, 0600);
677 g_free (buffer);
678 g_free (menufile);
681 void quick_chdir_cmd (void)
683 char *target;
685 target = hotlist_cmd (LIST_HOTLIST);
686 if (!target)
687 return;
689 if (get_current_type () == view_tree)
690 tree_chdir (the_tree, target);
691 else
692 if (!do_cd (target, cd_exact))
693 message (1, MSG_ERROR, _("Could not change directory") );
694 g_free (target);
697 #ifdef USE_VFS
698 void free_vfs_now (void)
700 vfs_expire (1);
703 void reselect_vfs (void)
705 char *target;
707 target = hotlist_cmd (LIST_VFSLIST);
708 if (!target)
709 return;
711 if (!do_cd (target, cd_exact))
712 message (1, MSG_ERROR, _("Could not change directory") );
713 g_free (target);
715 #endif /* USE_VFS */
717 static int compare_files (char *name1, char *name2, off_t size)
719 int file1, file2;
720 int result = -1; /* Different by default */
722 file1 = open (name1, O_RDONLY);
723 if (file1 >= 0){
724 file2 = open (name2, O_RDONLY);
725 if (file2 >= 0){
726 #ifdef HAVE_MMAP
727 char *data1, *data2;
728 /* Ugly if jungle */
729 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
730 if (data1 != (char*) -1){
731 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
732 if (data2 != (char*) -1){
733 rotate_dash ();
734 result = memcmp (data1, data2, size);
735 munmap (data2, size);
737 munmap (data1, size);
739 #else
740 /* Don't have mmap() :( Even more ugly :) */
741 char buf1[BUFSIZ], buf2[BUFSIZ];
742 int n1, n2;
743 rotate_dash ();
746 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
747 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
748 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
749 result = (n1 != n2) || memcmp(buf1,buf2,n1);
750 #endif /* !HAVE_MMAP */
751 close (file2);
753 close (file1);
755 return result;
758 enum CompareMode {
759 compare_quick, compare_size_only, compare_thourough
762 static void
763 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
765 int i, j;
766 char *src_name, *dst_name;
768 panel = get_a_panel (panel);
770 /* No marks by default */
771 panel->marked = 0;
772 panel->total = 0;
773 panel->dirs_marked = 0;
775 /* Handle all files in the panel */
776 for (i = 0; i < panel->count; i++){
777 file_entry *source = &panel->dir.list[i];
779 /* Default: unmarked */
780 file_mark (panel, i, 0);
782 /* Skip directories */
783 if (S_ISDIR (source->buf.st_mode))
784 continue;
786 /* Search the corresponding entry from the other panel */
787 for (j = 0; j < other->count; j++){
788 if (strcmp (source->fname,
789 other->dir.list[j].fname) == 0)
790 break;
792 if (j >= other->count)
793 /* Not found -> mark */
794 do_file_mark (panel, i, 1);
795 else {
796 /* Found */
797 file_entry *target = &other->dir.list[j];
799 if (mode != compare_size_only){
800 /* Older version is not marked */
801 if (source->buf.st_mtime < target->buf.st_mtime)
802 continue;
805 /* Newer version with different size is marked */
806 if (source->buf.st_size != target->buf.st_size){
807 do_file_mark (panel, i, 1);
808 continue;
811 if (mode == compare_size_only)
812 continue;
814 if (mode == compare_quick){
815 /* Thorough compare off, compare only time stamps */
816 /* Mark newer version, don't mark version with the same date */
817 if (source->buf.st_mtime > target->buf.st_mtime){
818 do_file_mark (panel, i, 1);
820 continue;
823 /* Thorough compare on, do byte-by-byte comparison */
824 src_name = concat_dir_and_file (panel->cwd, source->fname);
825 dst_name = concat_dir_and_file (other->cwd, target->fname);
826 if (compare_files (src_name, dst_name, source->buf.st_size))
827 do_file_mark (panel, i, 1);
828 g_free (src_name);
829 g_free (dst_name);
831 } /* for (i ...) */
834 void compare_dirs_cmd (void)
836 enum CompareMode thorough_flag = compare_quick;
838 thorough_flag = query_dialog (_(" Compare directories "), _(" Select compare method: "),
839 0, 3, _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
840 if (thorough_flag < 0 || thorough_flag > 2)
841 return;
842 if (get_current_type () == view_listing &&
843 get_other_type () == view_listing){
844 compare_dir (cpanel, opanel, thorough_flag);
845 compare_dir (opanel, cpanel, thorough_flag);
846 paint_panel (cpanel);
847 paint_panel (opanel);
848 } else {
849 message (1, MSG_ERROR, _(" Both panels should be on the listing view mode to use this command "));
853 void history_cmd (void)
855 Listbox *listbox;
856 Hist *current;
858 if (input_w (cmdline)->need_push){
859 if (push_history (input_w (cmdline), input_w (cmdline)->buffer) == 2)
860 input_w (cmdline)->need_push = 0;
862 if (!input_w (cmdline)->history){
863 message (1, MSG_ERROR, _(" The command history is empty "));
864 return;
866 current = input_w (cmdline)->history;
867 while (current->prev)
868 current = current->prev;
869 listbox = create_listbox_window (60, 10, _(" Command history "),
870 "[Command Menu]");
871 while (current){
872 LISTBOX_APPEND_TEXT (listbox, 0, current->text,
873 current);
874 current = current->next;
876 run_dlg (listbox->dlg);
877 if (listbox->dlg->ret_value == B_CANCEL)
878 current = NULL;
879 else
880 current = listbox->list->current->data;
881 destroy_dlg (listbox->dlg);
882 g_free (listbox);
884 if (!current)
885 return;
886 input_w (cmdline)->history = current;
887 assign_text (input_w (cmdline), input_w (cmdline)->history->text);
888 update_input (input_w (cmdline), 1);
891 void swap_cmd (void)
893 swap_panels ();
894 touchwin (stdscr);
895 repaint_screen ();
898 void
899 view_other_cmd (void)
901 static int message_flag = TRUE;
902 #ifdef HAVE_SUBSHELL_SUPPORT
903 char *new_dir = NULL;
904 char **new_dir_p;
905 #endif /* HAVE_SUBSHELL_SUPPORT */
907 if (!xterm_flag && !console_flag && !use_subshell){
908 if (message_flag)
909 message (1, MSG_ERROR, _(" Not an xterm or Linux console; \n"
910 " the panels cannot be toggled. "));
911 message_flag = FALSE;
912 } else {
913 disable_mouse ();
914 if (clear_before_exec)
915 clr_scr ();
916 if (alternate_plus_minus)
917 numeric_keypad_mode ();
918 #ifndef HAVE_SLANG
919 /* With slang we don't want any of this, since there
920 * is no mc_raw_mode supported
922 reset_shell_mode ();
923 noecho ();
924 #endif /* !HAVE_SLANG */
925 keypad(stdscr, FALSE);
926 endwin ();
927 do_exit_ca_mode ();
928 mc_raw_mode ();
929 if (console_flag)
930 restore_console ();
932 #ifdef HAVE_SUBSHELL_SUPPORT
933 if (use_subshell){
934 new_dir_p = vfs_current_is_local () ? &new_dir : NULL;
935 if (invoke_subshell (NULL, VISIBLY, new_dir_p))
936 quiet_quit_cmd(); /* User did `exit' or `logout': quit MC quietly */
937 } else
938 #endif /* HAVE_SUBSHELL_SUPPORT */
940 if (output_starts_shell){
941 fprintf (stderr,
942 _("Type `exit' to return to the Midnight Commander"));
943 fprintf (stderr, "\n\r\n\r");
945 my_system (EXECUTE_AS_SHELL, shell, NULL);
946 } else
947 get_key_code (0);
949 if (console_flag)
950 handle_console (CONSOLE_SAVE);
952 do_enter_ca_mode ();
954 reset_prog_mode ();
955 keypad(stdscr, TRUE);
956 enable_mouse ();
957 if (alternate_plus_minus)
958 application_keypad_mode ();
960 #ifdef HAVE_SUBSHELL_SUPPORT
961 if (use_subshell){
962 load_prompt (0, 0);
963 if (new_dir)
964 do_possible_cd (new_dir);
965 if (console_flag && output_lines)
966 show_console_contents (output_start_y,
967 LINES-keybar_visible-output_lines-1,
968 LINES-keybar_visible-1);
970 #endif /* HAVE_SUBSHELL_SUPPORT */
971 touchwin (stdscr);
973 /* prevent screen flash when user did 'exit' or 'logout' within
974 subshell */
975 if (!quit)
976 repaint_screen ();
980 #ifndef OS2_NT
981 static void
982 do_link (int symbolic_link, char *fname)
984 char *dest, *src;
986 if (!symbolic_link){
987 src = g_strconcat (_(" Link "), name_trunc (fname, 46),
988 _(" to:"), NULL);
989 dest = input_expand_dialog (_(" Link "), src, "");
990 g_free (src);
991 if (!dest)
992 return;
993 if (!*dest) {
994 g_free (dest);
995 return;
997 save_cwds_stat ();
998 if (-1 == mc_link (fname, dest))
999 message (1, MSG_ERROR, _(" link: %s "), unix_error_string (errno));
1000 } else {
1001 char *s;
1002 char *d;
1004 /* suggest the full path for symlink */
1005 s = concat_dir_and_file (cpanel->cwd, fname);
1007 if (get_other_type () == view_listing) {
1008 d = concat_dir_and_file (opanel->cwd, fname);
1009 } else {
1010 d = g_strdup (fname);
1013 symlink_dialog (s, d, &dest, &src);
1014 g_free (d);
1015 g_free (s);
1017 if (!dest || !*dest || !src || !*src) {
1018 if (src)
1019 g_free (src);
1020 if (dest)
1021 g_free (dest);
1022 return;
1024 save_cwds_stat ();
1025 if (-1 == mc_symlink (dest, src))
1026 message (1, MSG_ERROR, _(" symlink: %s "),
1027 unix_error_string (errno));
1028 g_free (src);
1030 g_free (dest);
1031 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1032 repaint_screen ();
1035 void link_cmd (void)
1037 do_link (0, selection (cpanel)->fname);
1040 void symlink_cmd (void)
1042 char *filename = NULL;
1043 if (is_a_desktop_panel (cpanel)) {
1044 gint i;
1045 for (i = 0; i < cpanel->count; i++)
1046 if (cpanel->dir.list [i].f.marked) {
1047 filename = cpanel->dir.list [i].fname;
1048 break;
1050 } else {
1051 filename = selection (cpanel)->fname;
1054 if (filename) {
1055 do_link (1, filename);
1059 void edit_symlink_cmd (void)
1061 if (S_ISLNK (selection (cpanel)->buf.st_mode)) {
1062 char buffer [MC_MAXPATHLEN];
1063 char *p = NULL;
1064 int i;
1065 char *dest, *q;
1067 if (is_a_desktop_panel (cpanel)) {
1068 gint i;
1069 for (i = 0; i < cpanel->count; i++)
1070 if (cpanel->dir.list [i].f.marked) {
1071 p = cpanel->dir.list [i].fname;
1072 break;
1074 } else {
1075 p = selection (cpanel)->fname;
1078 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
1080 i = readlink (p, buffer, MC_MAXPATHLEN);
1081 if (i > 0) {
1082 buffer [i] = 0;
1083 dest = input_expand_dialog (_(" Edit symlink "), q, buffer);
1084 if (dest) {
1085 if (*dest && strcmp (buffer, dest)) {
1086 save_cwds_stat ();
1087 if (-1 == mc_unlink (p)){
1088 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1089 p, unix_error_string (errno));
1090 } else {
1091 if (-1 == mc_symlink (dest, p))
1092 message (1, MSG_ERROR, _(" edit symlink: %s "),
1093 unix_error_string (errno));
1095 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1096 repaint_screen ();
1098 g_free (dest);
1101 g_free (q);
1102 } else {
1103 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1104 selection (cpanel)->fname);
1108 void other_symlink_cmd (void)
1110 char *dest, *q, *p, *r, *s, *t;
1112 if (get_other_type () != view_listing)
1113 return;
1115 if (!strcmp (selection (opanel)->fname, ".."))
1116 return;
1117 p = concat_dir_and_file (cpanel->cwd, selection (cpanel)->fname);
1118 r = concat_dir_and_file (opanel->cwd, selection (cpanel)->fname);
1120 q = g_strconcat (_(" Link symbolically "), name_trunc (p, 32), _(" to:"), NULL);
1121 dest = input_expand_dialog (_(" Relative symlink "), q, r);
1122 if (dest) {
1123 if (*dest) {
1124 t = strrchr (dest, PATH_SEP);
1125 if (t) {
1126 t[1] = 0;
1127 s = diff_two_paths (dest, p);
1128 t[1] = PATH_SEP;
1129 if (s) {
1130 save_cwds_stat ();
1131 if (-1 == mc_symlink (dest, s))
1132 message (1, MSG_ERROR, _(" relative symlink: %s "),
1133 unix_error_string (errno));
1134 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1135 repaint_screen ();
1136 g_free (s);
1140 g_free (dest);
1142 g_free (q);
1143 g_free (p);
1144 g_free (r);
1146 #endif /* !OS2_NT */
1148 void help_cmd (void)
1150 interactive_display (NULL, "[main]");
1153 void view_panel_cmd (void)
1155 view_cmd (cpanel);
1158 void edit_panel_cmd (void)
1160 edit_cmd (cpanel);
1163 void mkdir_panel_cmd (void)
1165 mkdir_cmd (cpanel);
1168 /* partly taken from dcigettext.c, returns "" for default locale */
1169 /* value should be freed by calling function g_free() */
1170 char *guess_message_value (void)
1172 const char *var[] = {
1173 /* The highest priority value is the `LANGUAGE' environment
1174 variable. This is a GNU extension. */
1175 "LANGUAGE",
1176 /* Setting of LC_ALL overwrites all other. */
1177 "LC_ALL",
1178 /* Next comes the name of the desired category. */
1179 "LC_MESSAGES",
1180 /* Last possibility is the LANG environment variable. */
1181 "LANG",
1182 /* NULL exit loops */
1183 NULL
1186 unsigned i = 0;
1187 char *locale = NULL;
1189 while (var[i] != NULL) {
1190 locale = getenv (var[i]);
1191 if (locale != NULL && locale[0] != '\0')
1192 break;
1193 i++;
1196 if (locale == NULL)
1197 locale = "";
1199 return g_strdup (locale);
1202 /* Returns a random hint */
1203 char *get_random_hint (void)
1205 char *data, *result, *eol;
1206 int len;
1207 int start;
1209 /* Do not change hints more often than one minute */
1211 #ifdef SCO_FLAVOR
1212 static time_t last;
1213 time_t now;
1215 time (&now);
1216 if ((now - last) < 60)
1217 return g_strdup ("");
1218 last = now;
1219 #else
1220 static int last_sec;
1221 static struct timeval tv;
1223 gettimeofday (&tv, NULL);
1224 if (!(tv.tv_sec> last_sec+60))
1225 return g_strdup ("");
1226 last_sec = tv.tv_sec;
1227 #endif /* !SCO_FLAVOR */
1229 data = load_mc_home_file (MC_HINT, NULL);
1230 if (!data)
1231 return 0;
1233 #ifdef SCO_FLAVOR
1234 srand ((short) now);
1235 #else
1236 srand (tv.tv_sec);
1237 #endif /* !SCO_FLAVOR */
1238 /* get a random entry */
1239 len = strlen (data);
1240 start = rand () % len;
1242 for (;start; start--){
1243 if (data [start] == '\n'){
1244 start++;
1245 break;
1248 eol = strchr (&data [start], '\n');
1249 if (eol)
1250 *eol = 0;
1251 result = g_strdup (&data [start]);
1252 g_free (data);
1253 return result;
1256 #ifdef USE_NETCODE
1258 static char *machine_str = N_(" Enter machine name (F1 for details): ");
1260 static void nice_cd (char *text, char *xtext, char *help, char *prefix, int to_home)
1262 char *machine;
1263 char *cd_path;
1265 if (!SELECTED_IS_PANEL)
1266 return;
1268 machine = input_dialog_help (text,
1269 xtext,
1270 help, "");
1271 if (!machine)
1272 return;
1274 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1275 ugly as hell and leads to problems in vfs layer */
1277 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1278 cd_path = g_strconcat (machine, to_home ? "/~/" : NULL, NULL);
1279 else
1280 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : NULL, NULL);
1282 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1283 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1284 else
1285 message (1, MSG_ERROR, _(" Could not chdir to %s "), cd_path);
1286 g_free (cd_path);
1287 g_free (machine);
1290 void netlink_cmd (void)
1292 nice_cd (_(" Link to a remote machine "), _(machine_str),
1293 "[Network File System]", "/#mc:", 1);
1296 void ftplink_cmd (void)
1298 nice_cd (_(" FTP to machine "), _(machine_str),
1299 "[FTP File System]", "/#ftp:", 1);
1302 void smblink_cmd (void)
1304 nice_cd (_(" SMB link to machine "), _(machine_str),
1305 "[SMB File System]", "/#smb:", 0);
1308 #ifdef HAVE_SETSOCKOPT
1309 void source_routing (void)
1311 char *source;
1312 struct hostent *hp;
1314 source = input_dialog (_(" Socket source routing setup "),
1315 _(" Enter host name to use as a source routing hop: "),
1316 "");
1317 if (!source)
1318 return;
1320 hp = gethostbyname (source);
1321 g_free (source);
1322 if (!hp){
1323 message (1, _(" Host name "), _(" Error while looking up IP address "));
1324 return;
1326 source_route = *((int *)hp->h_addr);
1328 #endif /* HAVE_SETSOCKOPT */
1329 #endif /* USE_NETCODE */
1331 #ifdef USE_EXT2FSLIB
1332 void undelete_cmd (void)
1334 nice_cd (_(" Undelete files on an ext2 file system "),
1335 _(" Enter device (without /dev/) to undelete\n "
1336 " files on: (F1 for details)"),
1337 "[Undelete File System]", "/#undel:", 0);
1339 #endif /* USE_EXT2FSLIB */
1341 void quick_cd_cmd (void)
1343 char *p = cd_dialog ();
1345 if (p && *p) {
1346 char *q = g_strconcat ("cd ", p, NULL);
1348 do_cd_command (q);
1349 g_free (q);
1351 if (p)
1352 g_free (p);
1355 void
1356 dirsizes_cmd (void)
1358 WPanel *panel = cpanel;
1359 int i;
1360 off_t marked;
1361 double total;
1363 for (i = 0; i < panel->count; i++)
1364 if (S_ISDIR (panel->dir.list [i].buf.st_mode) &&
1365 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1366 !panel->dirs_marked) &&
1367 strcmp (panel->dir.list [i].fname, "..") != 0) {
1368 total = 0.0l;
1369 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1370 panel->dir.list [i].buf.st_size = (off_t) total;
1371 panel->dir.list [i].f.dir_size_computed = 1;
1374 recalculate_panel_summary (panel);
1375 paint_panel (panel);
1378 void
1379 save_setup_cmd (void)
1381 char *str;
1383 save_setup ();
1384 sync_profiles ();
1385 str = g_strconcat ( _(" Setup saved to ~/"), PROFILE_NAME, NULL);
1387 message (0, _(" Setup "), str);
1388 g_free (str);
1391 void
1392 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1394 p->user_mini_status = use_msformat;
1395 p->list_type = view_type;
1397 if (view_type == list_user || use_msformat){
1398 g_free (p->user_format);
1399 p->user_format = user;
1401 g_free (p->user_status_format [view_type]);
1402 p->user_status_format [view_type] = status;
1404 set_panel_formats (p);
1406 else {
1407 g_free (user);
1408 g_free (status);
1411 set_panel_formats (p);
1412 paint_panel (p);
1414 do_refresh ();
1417 void
1418 info_cmd_no_menu (void)
1420 set_display_type (cpanel == left_panel ? 1 : 0, view_info);
1423 void
1424 quick_cmd_no_menu (void)
1426 set_display_type (cpanel == left_panel ? 1 : 0, view_quick);
1429 void
1430 switch_to_listing (int panel_index)
1432 if (get_display_type (panel_index) != view_listing)
1433 set_display_type (panel_index, view_listing);
1436 void
1437 listing_cmd (void)
1439 int view_type, use_msformat;
1440 char *user, *status;
1441 WPanel *p;
1442 int display_type;
1444 display_type = get_display_type (MENU_PANEL_IDX);
1445 if (display_type == view_listing)
1446 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1447 else
1448 p = 0;
1450 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1452 if (view_type == -1)
1453 return;
1455 switch_to_listing (MENU_PANEL_IDX);
1457 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1459 configure_panel_listing (p, view_type, use_msformat, user, status);
1462 void
1463 tree_cmd (void)
1465 set_display_type (MENU_PANEL_IDX, view_tree);
1468 void
1469 info_cmd (void)
1471 set_display_type (MENU_PANEL_IDX, view_info);
1474 void
1475 quick_view_cmd (void)
1477 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == cpanel)
1478 change_panel ();
1479 set_display_type (MENU_PANEL_IDX, view_quick);
1482 /* Handle the tree internal listing modes switching */
1483 static int
1484 set_basic_panel_listing_to (int panel_index, int listing_mode)
1486 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1488 switch_to_listing (panel_index);
1489 p->list_type = listing_mode;
1490 if (set_panel_formats (p))
1491 return 0;
1493 paint_panel (p);
1494 do_refresh ();
1495 return 1;
1498 void
1499 toggle_listing_cmd (void)
1501 int current = get_current_index ();
1502 WPanel *p = (WPanel *) get_panel_widget (current);
1503 int list_mode = p->list_type;
1504 int m;
1506 switch (list_mode){
1507 case list_full:
1508 case list_brief:
1509 m = list_long;
1510 break;
1511 case list_long:
1512 m = list_user;
1513 break;
1514 default:
1515 m = list_full;
1517 if (set_basic_panel_listing_to (current, m))
1518 return;
1519 set_basic_panel_listing_to (current, list_full);