small highlighting correction...
[midnight-commander.git] / src / cmd.c
blob5c5a905d49d530c71dca0be25ff38f8d9e68052e
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 "tty.h"
43 #include "dir.h"
44 #include "panel.h"
45 #include "cmd.h" /* Our definitions */
46 #include "view.h" /* view() */
47 #include "dialog.h" /* query_dialog, message */
48 #include "file.h" /* the file operations */
49 #include "fileopctx.h"
50 #include "find.h" /* do_find */
51 #include "hotlist.h"
52 #include "tree.h"
53 #include "subshell.h" /* use_subshell */
54 #include "cons.saver.h"
55 #include "dlg.h" /* required by wtools.h */
56 #include "widget.h" /* required by wtools.h */
57 #include "wtools.h" /* listbox */
58 #include "command.h" /* for cmdline */
59 #include "win.h" /* do_exit_ca_mode */
60 #include "layout.h" /* get_current/other_type */
61 #include "ext.h" /* regex_command */
62 #include "key.h" /* get_key_code */
63 #include "help.h" /* interactive_display */
64 #include "boxes.h" /* cd_dialog */
65 #include "color.h"
66 #include "user.h"
67 #include "setup.h"
68 #include "profile.h"
70 #include "vfs/vfs.h"
71 #define WANT_WIDGETS
72 #include "main.h" /* global variables, global functions */
73 #ifndef MAP_FILE
74 # define MAP_FILE 0
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 /* Source routing destination */
81 int source_route = 0;
83 /* If set, use the builtin editor */
84 int use_internal_edit = 1;
86 /* Ugly hack in order to distinguish between left and right panel in menubar */
87 int is_right;
88 #define MENU_PANEL_IDX (is_right ? 1 : 0)
91 /* This is used since the parameter panel on some of the commands */
92 /* defined in this file may receive a 0 parameter if they are invoked */
93 /* The drop down menu */
95 static WPanel *
96 get_a_panel (WPanel *panel)
98 if (panel)
99 return panel;
100 if (get_current_type () == view_listing){
101 return cpanel;
102 } else
103 return other_panel;
106 /* view_file (filename, normal, internal)
108 * Inputs:
109 * filename: The file name to view
110 * plain_view: If set does not do any fancy pre-processing (no filtering) and
111 * always invokes the internal viewer.
112 * internal: If set uses the internal viewer, otherwise an external viewer.
114 int view_file_at_line (char *filename, int plain_view, int internal, int start_line)
116 static char *viewer = 0;
117 int move_dir = 0;
120 if (plain_view) {
121 int changed_hex_mode = 0;
122 int changed_nroff_flag = 0;
123 int changed_magic_flag = 0;
125 altered_hex_mode = 0;
126 altered_nroff_flag = 0;
127 altered_magic_flag = 0;
128 if (default_hex_mode)
129 changed_hex_mode = 1;
130 if (default_nroff_flag)
131 changed_nroff_flag = 1;
132 if (default_magic_flag)
133 changed_magic_flag = 1;
134 default_hex_mode = 0;
135 default_nroff_flag = 0;
136 default_magic_flag = 0;
137 view (0, filename, &move_dir, start_line);
138 if (changed_hex_mode && !altered_hex_mode)
139 default_hex_mode = 1;
140 if (changed_nroff_flag && !altered_nroff_flag)
141 default_nroff_flag = 1;
142 if (changed_magic_flag && !altered_magic_flag)
143 default_magic_flag = 1;
144 repaint_screen ();
145 return move_dir;
147 if (internal){
148 char view_entry [BUF_TINY];
150 if (start_line != 0)
151 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
152 else
153 strcpy (view_entry, "View");
155 if (!regex_command (filename, view_entry, &move_dir)){
156 view (0, filename, &move_dir, start_line);
157 repaint_screen ();
159 } else {
160 char *localcopy;
162 if (!viewer){
163 viewer = getenv ("PAGER");
164 if (!viewer)
165 viewer = "view";
167 /* The file may be a non local file, get a copy */
168 if (!vfs_file_is_local (filename)){
169 localcopy = mc_getlocalcopy (filename);
170 if (localcopy == NULL){
171 message (1, MSG_ERROR, _(" Can not fetch a local copy of %s "), filename);
172 return 0;
174 execute_internal (viewer, localcopy);
175 mc_ungetlocalcopy (filename, localcopy, 0);
176 } else
177 execute_internal (viewer, filename);
179 return move_dir;
183 view_file (char *filename, int plain_view, int internal)
185 return view_file_at_line (filename, plain_view, internal, 0);
188 /* scan_for_file (panel, idx, direction)
190 * Inputs:
191 * panel: pointer to the panel on which we operate
192 * idx: starting file.
193 * direction: 1, or -1
195 static int scan_for_file (WPanel *panel, int idx, int direction)
197 int i = idx + direction;
199 while (i != idx){
200 if (i < 0)
201 i = panel->count - 1;
202 if (i == panel->count)
203 i = 0;
204 if (!S_ISDIR (panel->dir.list [i].buf.st_mode))
205 return i;
206 i += direction;
208 return i;
211 /* do_view: Invoked as the F3/F13 key. */
212 static void do_view_cmd (WPanel *panel, int normal)
214 int dir, file_idx;
215 panel = get_a_panel (panel);
217 /* Directories are viewed by changing to them */
218 if (S_ISDIR (selection (panel)->buf.st_mode) ||
219 link_isdir (selection (panel))) {
220 if (confirm_view_dir && (panel->marked || panel->dirs_marked)){
221 if (query_dialog (_(" CD "), _("Files tagged, want to cd?"),
222 0, 2, _("&Yes"), _("&No")) == 1){
223 return;
226 if (!do_cd (selection (panel)->fname, cd_exact))
227 message (1, MSG_ERROR, _("Could not change directory") );
229 return;
233 file_idx = panel->selected;
234 while (1) {
235 char *filename;
237 filename = panel->dir.list [file_idx].fname;
239 dir = view_file (filename, normal, use_internal_view);
240 if (dir == 0)
241 break;
242 file_idx = scan_for_file (panel, file_idx, dir);
246 void view_cmd (WPanel *panel)
248 do_view_cmd (panel, 0);
251 void view_file_cmd (WPanel *panel)
253 char *filename;
255 panel = get_a_panel (panel);
256 filename = input_dialog (_(" View file "), _(" Filename:"), selection (panel)->fname);
257 if (!filename) return;
259 view_file (filename, 0, use_internal_view);
260 g_free (filename);
263 void view_simple_cmd (WPanel *panel)
265 do_view_cmd (panel, 1);
268 void filtered_view_cmd (WPanel *panel)
270 char *command;
272 panel = get_a_panel (panel);
273 command = input_dialog (_(" Filtered view "), _(" Filter command and arguments:"),
274 selection (panel)->fname);
275 if (!command)
276 return;
278 view (command, "", 0, 0);
280 g_free (command);
283 void filtered_view_cmd_cpanel (void)
285 filtered_view_cmd (cpanel);
288 void do_edit_at_line (const char *what, int start_line)
290 static char *editor = 0;
292 #ifdef USE_INTERNAL_EDIT
293 if (use_internal_edit){
294 edit (what, start_line);
295 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
296 repaint_screen ();
297 return;
299 #endif /* USE_INTERNAL_EDIT */
300 if (!editor){
301 editor = getenv ("EDITOR");
302 if (!editor)
303 editor = get_default_editor ();
305 execute_internal (editor, what);
306 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
307 repaint_screen ();
310 void
311 do_edit (const char *what)
313 do_edit_at_line (what, 1);
316 void edit_cmd (WPanel *panel)
318 panel = get_a_panel(panel);
319 if (!regex_command (selection (panel)->fname, "Edit", 0))
320 do_edit (selection (panel)->fname);
323 void edit_cmd_new (WPanel *panel)
325 do_edit ("");
328 /* Invoked by F5. Copy, default to the other panel. */
329 void copy_cmd (void)
331 save_cwds_stat ();
332 if (panel_operate (cpanel, OP_COPY, NULL, TRUE)){
333 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
334 repaint_screen ();
338 /* Invoked by F6. Move/rename, default to the other panel. */
339 void ren_cmd (void)
341 save_cwds_stat ();
342 if (panel_operate (cpanel, OP_MOVE, NULL, TRUE)){
343 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
344 repaint_screen ();
348 /* Invoked by F15. Copy, default to the same panel. */
349 void copy_cmd_local (void)
351 save_cwds_stat ();
352 if (panel_operate (cpanel, OP_COPY, selection (cpanel)->fname, TRUE)){
353 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
354 repaint_screen ();
358 /* Invoked by F16. Move/rename, default to the same panel. */
359 void ren_cmd_local (void)
361 save_cwds_stat ();
362 if (panel_operate (cpanel, OP_MOVE, selection (cpanel)->fname, TRUE)){
363 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
364 repaint_screen ();
368 void mkdir_cmd (WPanel *panel)
370 char *tempdir;
371 char *dir;
373 panel = get_a_panel (panel);
374 dir = input_expand_dialog (_("Create a new Directory"), _(" Enter directory name:") , "");
376 if (!dir)
377 return;
379 if (dir [0] && (dir [0] == '/' || dir [0] == '~'))
380 tempdir = g_strdup (dir);
381 else
382 tempdir = concat_dir_and_file (panel->cwd, dir);
383 g_free (dir);
385 save_cwds_stat ();
386 if (my_mkdir (tempdir, 0777) == 0){
387 update_panels (UP_OPTIMIZE, tempdir);
388 repaint_screen ();
389 select_item (cpanel);
390 g_free (tempdir);
391 return;
393 g_free (tempdir);
394 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
397 void delete_cmd (void)
399 save_cwds_stat ();
401 if (panel_operate (cpanel, OP_DELETE, NULL, TRUE)){
402 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
403 repaint_screen ();
407 void find_cmd (void)
409 do_find ();
412 void
413 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
415 if (p->filter){
416 g_free (p->filter);
417 p->filter = 0;
419 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
420 p->filter = allocated_filter_string;
421 else
422 g_free (allocated_filter_string);
423 reread_cmd ();
426 /* Set a given panel filter expression */
427 void set_panel_filter (WPanel *p)
429 char *reg_exp;
430 char *x;
432 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
434 reg_exp = input_dialog_help (_(" Filter "),
435 _(" Set expression for filtering filenames"),
436 "[Filter...]", x);
437 if (!reg_exp)
438 return;
439 set_panel_filter_to (p, reg_exp);
442 /* Invoked from the left/right menus */
443 void filter_cmd (void)
445 WPanel *p;
447 if (!SELECTED_IS_PANEL)
448 return;
450 p = MENU_PANEL;
451 set_panel_filter (p);
454 void reread_cmd (void)
456 int flag;
458 if (get_current_type () == view_listing &&
459 get_other_type () == view_listing)
460 flag = strcmp (cpanel->cwd, opanel->cwd) ? UP_ONLY_CURRENT : 0;
461 else
462 flag = UP_ONLY_CURRENT;
464 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
465 repaint_screen ();
468 /* Panel sorting related routines */
469 void do_re_sort (WPanel *panel)
471 panel = get_a_panel (panel);
472 panel_re_sort (panel);
475 void reverse_selection_cmd_panel (WPanel *panel)
477 file_entry *file;
478 int i;
480 for (i = 0; i < panel->count; i++){
481 file = &panel->dir.list [i];
482 if (S_ISDIR (file->buf.st_mode))
483 continue;
484 do_file_mark (panel, i, !file->f.marked);
486 paint_panel (panel);
489 void reverse_selection_cmd (void)
491 reverse_selection_cmd_panel (cpanel);
494 void select_cmd_panel (WPanel *panel)
496 char *reg_exp, *reg_exp_t;
497 int i;
498 int c;
499 int dirflag = 0;
501 reg_exp = input_dialog (_(" Select "), "", easy_patterns ? "*" : ".");
502 if (!reg_exp)
503 return;
505 reg_exp_t = reg_exp;
507 /* Check if they specified a directory */
508 if (*reg_exp_t == PATH_SEP){
509 dirflag = 1;
510 reg_exp_t++;
512 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
513 dirflag = 1;
514 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
517 for (i = 0; i < panel->count; i++){
518 if (!strcmp (panel->dir.list [i].fname, ".."))
519 continue;
520 if (S_ISDIR (panel->dir.list [i].buf.st_mode)){
521 if (!dirflag)
522 continue;
523 } else {
524 if (dirflag)
525 continue;
527 c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file);
528 if (c == -1){
529 message (1, MSG_ERROR, _(" Malformed regular expression "));
530 g_free (reg_exp);
531 return;
533 if (c){
534 do_file_mark (panel, i, 1);
537 paint_panel (panel);
538 g_free (reg_exp);
541 void select_cmd (void)
543 select_cmd_panel (cpanel);
546 void unselect_cmd_panel (WPanel *panel)
548 char *reg_exp, *reg_exp_t;
549 int i;
550 int c;
551 int dirflag = 0;
553 reg_exp = input_dialog (_(" Unselect "),"", easy_patterns ? "*" : ".");
554 if (!reg_exp)
555 return;
557 reg_exp_t = reg_exp;
559 /* Check if they specified directory matching */
560 if (*reg_exp_t == PATH_SEP){
561 dirflag = 1;
562 reg_exp_t ++;
564 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
565 dirflag = 1;
566 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
568 for (i = 0; i < panel->count; i++){
569 if (!strcmp (panel->dir.list [i].fname, ".."))
570 continue;
571 if (S_ISDIR (panel->dir.list [i].buf.st_mode)){
572 if (!dirflag)
573 continue;
574 } else {
575 if (dirflag)
576 continue;
578 c = regexp_match (reg_exp_t, panel->dir.list [i].fname, match_file);
579 if (c == -1){
580 message (1, MSG_ERROR, _(" Malformed regular expression "));
581 g_free (reg_exp);
582 return;
584 if (c){
585 do_file_mark (panel, i, 0);
588 paint_panel (panel);
589 g_free (reg_exp);
592 void unselect_cmd (void)
594 unselect_cmd_panel (cpanel);
597 /* Check if the file exists */
598 /* If not copy the default */
599 int check_for_default(char *default_file, char *file)
601 struct stat s;
602 off_t count = 0;
603 double bytes = 0;
604 FileOpContext *ctx;
606 if (mc_stat (file, &s)){
607 if (mc_stat (default_file, &s)){
608 return -1;
610 ctx = file_op_context_new ();
611 file_op_context_create_ui (ctx, OP_COPY, 0);
612 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
613 file_op_context_destroy (ctx);
615 return 0;
618 void ext_cmd (void)
620 char *buffer;
621 char *extdir;
622 int dir;
624 dir = 0;
625 if (geteuid () == 0){
626 dir = query_dialog (_("Extension file edit"),
627 _(" Which extension file you want to edit? "), 0, 2,
628 _("&User"), _("&System Wide"));
630 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
632 if (dir == 0){
633 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
634 check_for_default (extdir, buffer);
635 do_edit (buffer);
636 g_free (buffer);
637 } else if (dir == 1)
638 do_edit (extdir);
640 g_free (extdir);
641 flush_extension_file ();
644 #ifdef USE_INTERNAL_EDIT
645 void edit_syntax_cmd (void)
647 char *buffer;
648 char *extdir;
649 int dir = 0;
651 if (geteuid () == 0){
652 dir = query_dialog (_("Syntax file edit"),
653 _(" Which syntax file you want to edit? "), 0, 2,
654 _("&User"), _("&System Wide"));
656 extdir = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
658 if (dir == 0){
659 buffer = concat_dir_and_file (home_dir, SYNTAX_FILE);
660 check_for_default (extdir, buffer);
661 do_edit (buffer);
662 g_free (buffer);
663 } else if (dir == 1)
664 do_edit (extdir);
666 g_free (extdir);
668 #endif
670 /* where = 0 - do edit a file menu for mc */
671 /* where = 1 - do edit a file menu for cool edit */
672 void menu_edit_cmd (int where)
674 char *buffer;
675 char *menufile;
676 int dir = 0;
678 dir = query_dialog (
679 _(" Menu edit "),
680 _(" Which menu file will you edit ? "),
681 0, geteuid() ? 2 : 3,
682 _("&Local"), _("&Home"), _("&System Wide")
685 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
687 switch (dir) {
688 case 0:
689 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
690 check_for_default (menufile, buffer);
691 break;
693 case 1:
694 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
695 check_for_default (menufile, buffer);
696 break;
698 case 2:
699 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
700 break;
702 default:
703 g_free (menufile);
704 return;
706 do_edit (buffer);
707 if (dir == 0)
708 chmod(buffer, 0600);
709 g_free (buffer);
710 g_free (menufile);
713 void quick_chdir_cmd (void)
715 char *target;
717 target = hotlist_cmd (LIST_HOTLIST);
718 if (!target)
719 return;
721 if (get_current_type () == view_tree)
722 tree_chdir (the_tree, target);
723 else
724 if (!do_cd (target, cd_exact))
725 message (1, MSG_ERROR, _("Could not change directory") );
726 g_free (target);
729 #ifdef USE_VFS
730 void free_vfs_now (void)
732 vfs_expire (1);
735 void reselect_vfs (void)
737 char *target;
739 target = hotlist_cmd (LIST_VFSLIST);
740 if (!target)
741 return;
743 if (!do_cd (target, cd_exact))
744 message (1, MSG_ERROR, _("Could not change directory") );
745 g_free (target);
747 #endif /* USE_VFS */
749 static int compare_files (char *name1, char *name2, off_t size)
751 int file1, file2;
752 int result = -1; /* Different by default */
754 file1 = open (name1, O_RDONLY);
755 if (file1 >= 0){
756 file2 = open (name2, O_RDONLY);
757 if (file2 >= 0){
758 #ifdef HAVE_MMAP
759 char *data1, *data2;
760 /* Ugly if jungle */
761 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
762 if (data1 != (char*) -1){
763 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
764 if (data2 != (char*) -1){
765 rotate_dash ();
766 result = memcmp (data1, data2, size);
767 munmap (data2, size);
769 munmap (data1, size);
771 #else
772 /* Don't have mmap() :( Even more ugly :) */
773 char buf1[BUFSIZ], buf2[BUFSIZ];
774 int n1, n2;
775 rotate_dash ();
778 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
779 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
780 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
781 result = (n1 != n2) || memcmp(buf1,buf2,n1);
782 #endif /* !HAVE_MMAP */
783 close (file2);
785 close (file1);
787 return result;
790 enum CompareMode {
791 compare_quick, compare_size_only, compare_thourough
794 static void
795 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
797 int i, j;
798 char *src_name, *dst_name;
800 panel = get_a_panel (panel);
802 /* No marks by default */
803 panel->marked = 0;
804 panel->total = 0;
805 panel->dirs_marked = 0;
807 /* Handle all files in the panel */
808 for (i = 0; i < panel->count; i++){
809 file_entry *source = &panel->dir.list[i];
811 /* Default: unmarked */
812 file_mark (panel, i, 0);
814 /* Skip directories */
815 if (S_ISDIR (source->buf.st_mode))
816 continue;
818 /* Search the corresponding entry from the other panel */
819 for (j = 0; j < other->count; j++){
820 if (strcmp (source->fname,
821 other->dir.list[j].fname) == 0)
822 break;
824 if (j >= other->count)
825 /* Not found -> mark */
826 do_file_mark (panel, i, 1);
827 else {
828 /* Found */
829 file_entry *target = &other->dir.list[j];
831 if (mode != compare_size_only){
832 /* Older version is not marked */
833 if (source->buf.st_mtime < target->buf.st_mtime)
834 continue;
837 /* Newer version with different size is marked */
838 if (source->buf.st_size != target->buf.st_size){
839 do_file_mark (panel, i, 1);
840 continue;
843 if (mode == compare_size_only)
844 continue;
846 if (mode == compare_quick){
847 /* Thorough compare off, compare only time stamps */
848 /* Mark newer version, don't mark version with the same date */
849 if (source->buf.st_mtime > target->buf.st_mtime){
850 do_file_mark (panel, i, 1);
852 continue;
855 /* Thorough compare on, do byte-by-byte comparison */
856 src_name = concat_dir_and_file (panel->cwd, source->fname);
857 dst_name = concat_dir_and_file (other->cwd, target->fname);
858 if (compare_files (src_name, dst_name, source->buf.st_size))
859 do_file_mark (panel, i, 1);
860 g_free (src_name);
861 g_free (dst_name);
863 } /* for (i ...) */
866 void compare_dirs_cmd (void)
868 enum CompareMode thorough_flag = compare_quick;
870 thorough_flag = query_dialog (_(" Compare directories "), _(" Select compare method: "),
871 0, 3, _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
872 if (thorough_flag < 0 || thorough_flag > 2)
873 return;
874 if (get_current_type () == view_listing &&
875 get_other_type () == view_listing){
876 compare_dir (cpanel, opanel, thorough_flag);
877 compare_dir (opanel, cpanel, thorough_flag);
878 paint_panel (cpanel);
879 paint_panel (opanel);
880 } else {
881 message (1, MSG_ERROR, _(" Both panels should be on the listing view mode to use this command "));
885 void
886 history_cmd (void)
888 Listbox *listbox;
889 Hist *current;
891 if (cmdline->need_push) {
892 if (push_history (cmdline, cmdline->buffer) == 2)
893 cmdline->need_push = 0;
895 if (!cmdline->history) {
896 message (1, MSG_ERROR, _(" The command history is empty "));
897 return;
899 current = cmdline->history;
900 while (current->prev)
901 current = current->prev;
902 listbox = create_listbox_window (60, 10, _(" Command history "),
903 "[Command Menu]");
904 while (current) {
905 LISTBOX_APPEND_TEXT (listbox, 0, current->text, current);
906 current = current->next;
908 run_dlg (listbox->dlg);
909 if (listbox->dlg->ret_value == B_CANCEL)
910 current = NULL;
911 else
912 current = listbox->list->current->data;
913 destroy_dlg (listbox->dlg);
914 g_free (listbox);
916 if (!current)
917 return;
918 cmdline->history = current;
919 assign_text (cmdline, cmdline->history->text);
920 update_input (cmdline, 1);
923 void swap_cmd (void)
925 swap_panels ();
926 touchwin (stdscr);
927 repaint_screen ();
930 void
931 view_other_cmd (void)
933 static int message_flag = TRUE;
934 #ifdef HAVE_SUBSHELL_SUPPORT
935 char *new_dir = NULL;
936 char **new_dir_p;
937 #endif /* HAVE_SUBSHELL_SUPPORT */
939 if (!xterm_flag && !console_flag && !use_subshell){
940 if (message_flag)
941 message (1, MSG_ERROR, _(" Not an xterm or Linux console; \n"
942 " the panels cannot be toggled. "));
943 message_flag = FALSE;
944 } else {
945 channels_down ();
946 disable_mouse ();
947 if (clear_before_exec)
948 clr_scr ();
949 if (alternate_plus_minus)
950 numeric_keypad_mode ();
951 #ifndef HAVE_SLANG
952 /* With slang we don't want any of this, since there
953 * is no mc_raw_mode supported
955 reset_shell_mode ();
956 noecho ();
957 #endif /* !HAVE_SLANG */
958 keypad(stdscr, FALSE);
959 endwin ();
960 do_exit_ca_mode ();
961 mc_raw_mode ();
962 if (console_flag)
963 restore_console ();
965 #ifdef HAVE_SUBSHELL_SUPPORT
966 if (use_subshell){
967 new_dir_p = vfs_current_is_local () ? &new_dir : NULL;
968 if (invoke_subshell (NULL, VISIBLY, new_dir_p))
969 quiet_quit_cmd(); /* User did `exit' or `logout': quit MC quietly */
970 } else
971 #endif /* HAVE_SUBSHELL_SUPPORT */
973 if (output_starts_shell){
974 fprintf (stderr,
975 _("Type `exit' to return to the Midnight Commander"));
976 fprintf (stderr, "\n\r\n\r");
978 my_system (EXECUTE_AS_SHELL, shell, NULL);
979 } else
980 get_key_code (0);
982 if (console_flag)
983 handle_console (CONSOLE_SAVE);
985 do_enter_ca_mode ();
987 reset_prog_mode ();
988 keypad(stdscr, TRUE);
990 /* Prevent screen flash when user did 'exit' or 'logout' within
991 subshell */
992 if (quit)
993 return;
995 enable_mouse ();
996 channels_up ();
997 if (alternate_plus_minus)
998 application_keypad_mode ();
1000 #ifdef HAVE_SUBSHELL_SUPPORT
1001 if (use_subshell){
1002 load_prompt (0, 0);
1003 if (new_dir)
1004 do_possible_cd (new_dir);
1005 if (console_flag && output_lines)
1006 show_console_contents (output_start_y,
1007 LINES-keybar_visible-output_lines-1,
1008 LINES-keybar_visible-1);
1010 #endif /* HAVE_SUBSHELL_SUPPORT */
1011 touchwin (stdscr);
1013 repaint_screen ();
1017 #ifndef NATIVE_WIN32
1018 static void
1019 do_link (int symbolic_link, char *fname)
1021 char *dest, *src;
1023 if (!symbolic_link) {
1024 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
1025 dest = input_expand_dialog (_(" Link "), src, "");
1026 g_free (src);
1027 if (!dest)
1028 return;
1029 if (!*dest) {
1030 g_free (dest);
1031 return;
1033 save_cwds_stat ();
1034 if (-1 == mc_link (fname, dest))
1035 message (1, MSG_ERROR, _(" link: %s "),
1036 unix_error_string (errno));
1037 } else {
1038 char *s;
1039 char *d;
1041 /* suggest the full path for symlink */
1042 s = concat_dir_and_file (cpanel->cwd, fname);
1044 if (get_other_type () == view_listing) {
1045 d = concat_dir_and_file (opanel->cwd, fname);
1046 } else {
1047 d = g_strdup (fname);
1050 symlink_dialog (s, d, &dest, &src);
1051 g_free (d);
1052 g_free (s);
1054 if (!dest || !*dest || !src || !*src) {
1055 if (src)
1056 g_free (src);
1057 if (dest)
1058 g_free (dest);
1059 return;
1061 save_cwds_stat ();
1062 if (-1 == mc_symlink (dest, src))
1063 message (1, MSG_ERROR, _(" symlink: %s "),
1064 unix_error_string (errno));
1065 g_free (src);
1067 g_free (dest);
1068 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1069 repaint_screen ();
1072 void link_cmd (void)
1074 do_link (0, selection (cpanel)->fname);
1077 void symlink_cmd (void)
1079 char *filename = NULL;
1080 filename = selection (cpanel)->fname;
1082 if (filename) {
1083 do_link (1, filename);
1087 void edit_symlink_cmd (void)
1089 if (S_ISLNK (selection (cpanel)->buf.st_mode)) {
1090 char buffer [MC_MAXPATHLEN];
1091 char *p = NULL;
1092 int i;
1093 char *dest, *q;
1095 p = selection (cpanel)->fname;
1097 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
1099 i = readlink (p, buffer, MC_MAXPATHLEN);
1100 if (i > 0) {
1101 buffer [i] = 0;
1102 dest = input_expand_dialog (_(" Edit symlink "), q, buffer);
1103 if (dest) {
1104 if (*dest && strcmp (buffer, dest)) {
1105 save_cwds_stat ();
1106 if (-1 == mc_unlink (p)){
1107 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1108 p, unix_error_string (errno));
1109 } else {
1110 if (-1 == mc_symlink (dest, p))
1111 message (1, MSG_ERROR, _(" edit symlink: %s "),
1112 unix_error_string (errno));
1114 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1115 repaint_screen ();
1117 g_free (dest);
1120 g_free (q);
1121 } else {
1122 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1123 selection (cpanel)->fname);
1126 #endif /* !NATIVE_WIN32 */
1128 void help_cmd (void)
1130 interactive_display (NULL, "[main]");
1133 void view_panel_cmd (void)
1135 view_cmd (cpanel);
1138 void edit_panel_cmd (void)
1140 edit_cmd (cpanel);
1143 void mkdir_panel_cmd (void)
1145 mkdir_cmd (cpanel);
1148 /* partly taken from dcigettext.c, returns "" for default locale */
1149 /* value should be freed by calling function g_free() */
1150 char *guess_message_value (void)
1152 static const char * const var[] = {
1153 /* The highest priority value is the `LANGUAGE' environment
1154 variable. This is a GNU extension. */
1155 "LANGUAGE",
1156 /* Setting of LC_ALL overwrites all other. */
1157 "LC_ALL",
1158 /* Next comes the name of the desired category. */
1159 "LC_MESSAGES",
1160 /* Last possibility is the LANG environment variable. */
1161 "LANG",
1162 /* NULL exit loops */
1163 NULL
1166 unsigned i = 0;
1167 char *locale = NULL;
1169 while (var[i] != NULL) {
1170 locale = getenv (var[i]);
1171 if (locale != NULL && locale[0] != '\0')
1172 break;
1173 i++;
1176 if (locale == NULL)
1177 locale = "";
1179 return g_strdup (locale);
1182 /* Returns a random hint */
1183 char *get_random_hint (void)
1185 char *data, *result, *eol;
1186 int len;
1187 int start;
1189 /* Do not change hints more often than one minute */
1191 #ifdef SCO_FLAVOR
1192 static time_t last;
1193 time_t now;
1195 time (&now);
1196 if ((now - last) < 60)
1197 return g_strdup ("");
1198 last = now;
1199 #else
1200 static int last_sec;
1201 static struct timeval tv;
1203 gettimeofday (&tv, NULL);
1204 if (!(tv.tv_sec> last_sec+60))
1205 return g_strdup ("");
1206 last_sec = tv.tv_sec;
1207 #endif /* !SCO_FLAVOR */
1209 data = load_mc_home_file (MC_HINT, NULL);
1210 if (!data)
1211 return 0;
1213 #ifdef SCO_FLAVOR
1214 srand ((short) now);
1215 #else
1216 srand (tv.tv_sec);
1217 #endif /* !SCO_FLAVOR */
1218 /* get a random entry */
1219 len = strlen (data);
1220 start = rand () % len;
1222 for (;start; start--){
1223 if (data [start] == '\n'){
1224 start++;
1225 break;
1228 eol = strchr (&data [start], '\n');
1229 if (eol)
1230 *eol = 0;
1231 result = g_strdup (&data [start]);
1232 g_free (data);
1233 return result;
1236 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1237 static void
1238 nice_cd (char *text, char *xtext, char *help, char *prefix, int to_home)
1240 char *machine;
1241 char *cd_path;
1243 if (!SELECTED_IS_PANEL)
1244 return;
1246 machine = input_dialog_help (text,
1247 xtext,
1248 help, "");
1249 if (!machine)
1250 return;
1252 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1253 ugly as hell and leads to problems in vfs layer */
1255 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1256 cd_path = g_strconcat (machine, to_home ? "/~/" : NULL, NULL);
1257 else
1258 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : NULL, NULL);
1260 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1261 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1262 else
1263 message (1, MSG_ERROR, _(" Could not chdir to %s "), cd_path);
1264 g_free (cd_path);
1265 g_free (machine);
1267 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1270 #ifdef USE_NETCODE
1272 static char *machine_str = N_(" Enter machine name (F1 for details): ");
1274 #ifdef WITH_MCFS
1275 void netlink_cmd (void)
1277 nice_cd (_(" Link to a remote machine "), _(machine_str),
1278 "[Network File System]", "/#mc:", 1);
1280 #endif /* WITH_MCFS */
1282 void ftplink_cmd (void)
1284 nice_cd (_(" FTP to machine "), _(machine_str),
1285 "[FTP File System]", "/#ftp:", 1);
1288 void fishlink_cmd (void)
1290 nice_cd (_(" Shell link to machine "), _(machine_str),
1291 "[FIle transfer over SHell filesystem]", "/#sh:", 1);
1294 #ifdef WITH_SMBFS
1295 void smblink_cmd (void)
1297 nice_cd (_(" SMB link to machine "), _(machine_str),
1298 "[SMB File System]", "/#smb:", 0);
1300 #endif /* WITH_SMBFS */
1302 #ifdef HAVE_SETSOCKOPT
1303 void source_routing (void)
1305 char *source;
1306 struct hostent *hp;
1308 source = input_dialog (_(" Socket source routing setup "),
1309 _(" Enter host name to use as a source routing hop: "),
1310 "");
1311 if (!source)
1312 return;
1314 hp = gethostbyname (source);
1315 g_free (source);
1316 if (!hp){
1317 message (1, _(" Host name "), _(" Error while looking up IP address "));
1318 return;
1320 source_route = *((int *)hp->h_addr);
1322 #endif /* HAVE_SETSOCKOPT */
1323 #endif /* USE_NETCODE */
1325 #ifdef USE_EXT2FSLIB
1326 void undelete_cmd (void)
1328 nice_cd (_(" Undelete files on an ext2 file system "),
1329 _(" Enter device (without /dev/) to undelete\n "
1330 " files on: (F1 for details)"),
1331 "[Undelete File System]", "/#undel:", 0);
1333 #endif /* USE_EXT2FSLIB */
1335 void quick_cd_cmd (void)
1337 char *p = cd_dialog ();
1339 if (p && *p) {
1340 char *q = g_strconcat ("cd ", p, NULL);
1342 do_cd_command (q);
1343 g_free (q);
1345 if (p)
1346 g_free (p);
1349 void
1350 dirsizes_cmd (void)
1352 WPanel *panel = cpanel;
1353 int i;
1354 off_t marked;
1355 double total;
1357 for (i = 0; i < panel->count; i++)
1358 if (S_ISDIR (panel->dir.list [i].buf.st_mode) &&
1359 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1360 !panel->dirs_marked) &&
1361 strcmp (panel->dir.list [i].fname, "..") != 0) {
1362 total = 0.0l;
1363 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1364 panel->dir.list [i].buf.st_size = (off_t) total;
1365 panel->dir.list [i].f.dir_size_computed = 1;
1368 recalculate_panel_summary (panel);
1369 paint_panel (panel);
1372 void
1373 save_setup_cmd (void)
1375 char *str;
1377 save_setup ();
1378 sync_profiles ();
1379 str = g_strconcat ( _(" Setup saved to ~/"), PROFILE_NAME, NULL);
1381 message (0, _(" Setup "), str);
1382 g_free (str);
1385 void
1386 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1388 p->user_mini_status = use_msformat;
1389 p->list_type = view_type;
1391 if (view_type == list_user || use_msformat){
1392 g_free (p->user_format);
1393 p->user_format = user;
1395 g_free (p->user_status_format [view_type]);
1396 p->user_status_format [view_type] = status;
1398 set_panel_formats (p);
1400 else {
1401 g_free (user);
1402 g_free (status);
1405 set_panel_formats (p);
1406 paint_panel (p);
1408 do_refresh ();
1411 void
1412 info_cmd_no_menu (void)
1414 if (get_display_type (0) == view_info)
1415 set_display_type (0, view_listing);
1416 else if (get_display_type (1) == view_info)
1417 set_display_type (1, view_listing);
1418 else
1419 set_display_type (cpanel == left_panel ? 1 : 0, view_info);
1422 void
1423 quick_cmd_no_menu (void)
1425 if (get_display_type (0) == view_quick)
1426 set_display_type (0, view_listing);
1427 else if (get_display_type (1) == view_quick)
1428 set_display_type (1, view_listing);
1429 else
1430 set_display_type (cpanel == left_panel ? 1 : 0, view_quick);
1433 void
1434 switch_to_listing (int panel_index)
1436 if (get_display_type (panel_index) != view_listing)
1437 set_display_type (panel_index, view_listing);
1440 void
1441 listing_cmd (void)
1443 int view_type, use_msformat;
1444 char *user, *status;
1445 WPanel *p;
1446 int display_type;
1448 display_type = get_display_type (MENU_PANEL_IDX);
1449 if (display_type == view_listing)
1450 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1451 else
1452 p = 0;
1454 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1456 if (view_type == -1)
1457 return;
1459 switch_to_listing (MENU_PANEL_IDX);
1461 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1463 configure_panel_listing (p, view_type, use_msformat, user, status);
1466 void
1467 tree_cmd (void)
1469 set_display_type (MENU_PANEL_IDX, view_tree);
1472 void
1473 info_cmd (void)
1475 set_display_type (MENU_PANEL_IDX, view_info);
1478 void
1479 quick_view_cmd (void)
1481 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == cpanel)
1482 change_panel ();
1483 set_display_type (MENU_PANEL_IDX, view_quick);
1486 /* Handle the tree internal listing modes switching */
1487 static int
1488 set_basic_panel_listing_to (int panel_index, int listing_mode)
1490 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1492 switch_to_listing (panel_index);
1493 p->list_type = listing_mode;
1494 if (set_panel_formats (p))
1495 return 0;
1497 paint_panel (p);
1498 do_refresh ();
1499 return 1;
1502 void
1503 toggle_listing_cmd (void)
1505 int current = get_current_index ();
1506 WPanel *p = (WPanel *) get_panel_widget (current);
1507 int list_mode = p->list_type;
1508 int m;
1510 switch (list_mode){
1511 case list_full:
1512 case list_brief:
1513 m = list_long;
1514 break;
1515 case list_long:
1516 m = list_user;
1517 break;
1518 default:
1519 m = list_full;
1521 if (set_basic_panel_listing_to (current, m))
1522 return;
1523 set_basic_panel_listing_to (current, list_full);