Reverting on "save file position" after ml clarification...
[midnight-commander.git] / src / cmd.c
blobce2867d0bdf2f94c8326092f67ef169604266009
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 "dlg.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 "dialog.h" /* do_refresh() */
58 #include "main.h" /* change_panel() */
59 #include "panel.h" /* cpanel */
60 #include "help.h" /* interactive_display() */
61 #include "user.h" /* MC_GLOBAL_MENU */
62 #include "command.h" /* cmdline */
63 #include "layout.h" /* get_current_type() */
64 #include "ext.h" /* regex_command() */
65 #include "boxes.h" /* cd_dialog() */
66 #include "setup.h" /* save_setup() */
67 #include "profile.h" /* PROFILE_NAME */
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 /* Source routing destination */
81 int source_route = 0;
83 /* If set, use the builtin editor */
84 int use_internal_edit = 1;
87 int
88 view_file_at_line (char *filename, int plain_view, int internal,
89 int start_line)
91 static char *viewer = 0;
92 int move_dir = 0;
95 if (plain_view) {
96 int changed_hex_mode = 0;
97 int changed_nroff_flag = 0;
98 int changed_magic_flag = 0;
100 altered_hex_mode = 0;
101 altered_nroff_flag = 0;
102 altered_magic_flag = 0;
103 if (default_hex_mode)
104 changed_hex_mode = 1;
105 if (default_nroff_flag)
106 changed_nroff_flag = 1;
107 if (default_magic_flag)
108 changed_magic_flag = 1;
109 default_hex_mode = 0;
110 default_nroff_flag = 0;
111 default_magic_flag = 0;
112 view (0, filename, &move_dir, start_line);
113 if (changed_hex_mode && !altered_hex_mode)
114 default_hex_mode = 1;
115 if (changed_nroff_flag && !altered_nroff_flag)
116 default_nroff_flag = 1;
117 if (changed_magic_flag && !altered_magic_flag)
118 default_magic_flag = 1;
119 repaint_screen ();
120 return move_dir;
122 if (internal) {
123 char view_entry[BUF_TINY];
125 if (start_line != 0)
126 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
127 start_line);
128 else
129 strcpy (view_entry, "View");
131 if (regex_command (filename, view_entry, &move_dir) == 0) {
132 view (0, filename, &move_dir, start_line);
133 repaint_screen ();
135 } else {
136 char *localcopy;
138 if (!viewer) {
139 viewer = getenv ("PAGER");
140 if (!viewer)
141 viewer = "view";
143 /* The file may be a non local file, get a copy */
144 if (!vfs_file_is_local (filename)) {
145 localcopy = mc_getlocalcopy (filename);
146 if (localcopy == NULL) {
147 message (1, MSG_ERROR,
148 _(" Can not fetch a local copy of %s "),
149 filename);
150 return 0;
152 execute_internal (viewer, localcopy);
153 mc_ungetlocalcopy (filename, localcopy, 0);
154 } else
155 execute_internal (viewer, filename);
157 return move_dir;
160 /* view_file (filename, plain_view, internal)
162 * Inputs:
163 * filename: The file name to view
164 * plain_view: If set does not do any fancy pre-processing (no filtering) and
165 * always invokes the internal viewer.
166 * internal: If set uses the internal viewer, otherwise an external viewer.
169 view_file (char *filename, int plain_view, int internal)
171 return view_file_at_line (filename, plain_view, internal, 0);
174 /* scan_for_file (panel, idx, direction)
176 * Inputs:
177 * panel: pointer to the panel on which we operate
178 * idx: starting file.
179 * direction: 1, or -1
181 static int scan_for_file (WPanel *panel, int idx, int direction)
183 int i = idx + direction;
185 while (i != idx){
186 if (i < 0)
187 i = panel->count - 1;
188 if (i == panel->count)
189 i = 0;
190 if (!S_ISDIR (panel->dir.list [i].buf.st_mode))
191 return i;
192 i += direction;
194 return i;
198 * Run viewer (internal or external) on the currently selected file.
199 * If normal is 1, force internal viewer and raw mode (used for F13).
201 static void
202 do_view_cmd (int normal)
204 int dir, file_idx;
206 /* Directories are viewed by changing to them */
207 if (S_ISDIR (selection (cpanel)->buf.st_mode)
208 || link_isdir (selection (cpanel))) {
209 if (confirm_view_dir && (cpanel->marked || cpanel->dirs_marked)) {
210 if (query_dialog
211 (_(" Confirmation "), _("Files tagged, want to cd?"), 0, 2,
212 _("&Yes"), _("&No")) != 0) {
213 return;
216 if (!do_cd (selection (cpanel)->fname, cd_exact))
217 message (1, MSG_ERROR, _("Could not change directory"));
219 return;
223 file_idx = cpanel->selected;
224 while (1) {
225 char *filename;
227 filename = cpanel->dir.list[file_idx].fname;
229 dir = view_file (filename, normal, use_internal_view);
230 if (dir == 0)
231 break;
232 file_idx = scan_for_file (cpanel, file_idx, dir);
236 /* Run user's preferred viewer on the currently selected file */
237 void
238 view_cmd (void)
240 do_view_cmd (0);
243 /* Ask for file and run user's preferred viewer on it */
244 void
245 view_file_cmd (void)
247 char *filename;
249 filename =
250 input_dialog (_(" View file "), _(" Filename:"),
251 selection (cpanel)->fname);
252 if (!filename)
253 return;
255 view_file (filename, 0, use_internal_view);
256 g_free (filename);
259 /* Run plain internal viewer on the currently selected file */
260 void
261 view_simple_cmd (void)
263 do_view_cmd (1);
266 void
267 filtered_view_cmd (void)
269 char *command;
271 command =
272 input_dialog (_(" Filtered view "),
273 _(" Filter command and arguments:"),
274 selection (cpanel)->fname);
275 if (!command)
276 return;
278 view (command, "", 0, 0);
280 g_free (command);
283 void do_edit_at_line (const char *what, int start_line)
285 static char *editor = 0;
287 #ifdef USE_INTERNAL_EDIT
288 if (use_internal_edit){
289 edit (what, start_line);
290 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
291 repaint_screen ();
292 return;
294 #endif /* USE_INTERNAL_EDIT */
295 if (!editor){
296 editor = getenv ("EDITOR");
297 if (!editor)
298 editor = get_default_editor ();
300 execute_internal (editor, what);
301 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
302 repaint_screen ();
305 static void
306 do_edit (const char *what)
308 do_edit_at_line (what, 0);
311 void
312 edit_cmd (void)
314 if (regex_command (selection (cpanel)->fname, "Edit", 0) == 0)
315 do_edit (selection (cpanel)->fname);
318 void
319 edit_cmd_new (void)
321 do_edit ("");
324 /* Invoked by F5. Copy, default to the other panel. */
325 void
326 copy_cmd (void)
328 save_cwds_stat ();
329 if (panel_operate (cpanel, OP_COPY, NULL, TRUE)) {
330 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
331 repaint_screen ();
335 /* Invoked by F6. Move/rename, default to the other panel. */
336 void ren_cmd (void)
338 save_cwds_stat ();
339 if (panel_operate (cpanel, OP_MOVE, NULL, TRUE)){
340 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
341 repaint_screen ();
345 /* Invoked by F15. Copy, default to the same panel. */
346 void copy_cmd_local (void)
348 save_cwds_stat ();
349 if (panel_operate (cpanel, OP_COPY, selection (cpanel)->fname, TRUE)){
350 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
351 repaint_screen ();
355 /* Invoked by F16. Move/rename, default to the same panel. */
356 void ren_cmd_local (void)
358 save_cwds_stat ();
359 if (panel_operate (cpanel, OP_MOVE, selection (cpanel)->fname, TRUE)){
360 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
361 repaint_screen ();
365 void mkdir_cmd (void)
367 char *tempdir;
368 char *dir;
370 dir = input_expand_dialog (_("Create a new Directory"), _(" Enter directory name:") , "");
372 if (!dir)
373 return;
375 if (dir [0] && (dir [0] == '/' || dir [0] == '~'))
376 tempdir = g_strdup (dir);
377 else
378 tempdir = concat_dir_and_file (cpanel->cwd, dir);
379 g_free (dir);
381 save_cwds_stat ();
382 if (my_mkdir (tempdir, 0777) == 0){
383 update_panels (UP_OPTIMIZE, tempdir);
384 repaint_screen ();
385 select_item (cpanel);
386 g_free (tempdir);
387 return;
389 g_free (tempdir);
390 message (1, MSG_ERROR, " %s ", unix_error_string (errno));
393 void delete_cmd (void)
395 save_cwds_stat ();
397 if (panel_operate (cpanel, OP_DELETE, NULL, TRUE)){
398 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
399 repaint_screen ();
403 void find_cmd (void)
405 do_find ();
408 static void
409 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
411 if (p->filter){
412 g_free (p->filter);
413 p->filter = 0;
415 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
416 p->filter = allocated_filter_string;
417 else
418 g_free (allocated_filter_string);
419 reread_cmd ();
422 /* Set a given panel filter expression */
423 static void
424 set_panel_filter (WPanel *p)
426 char *reg_exp;
427 char *x;
429 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
431 reg_exp = input_dialog_help (_(" Filter "),
432 _(" Set expression for filtering filenames"),
433 "[Filter...]", x);
434 if (!reg_exp)
435 return;
436 set_panel_filter_to (p, reg_exp);
439 /* Invoked from the left/right menus */
440 void filter_cmd (void)
442 WPanel *p;
444 if (!SELECTED_IS_PANEL)
445 return;
447 p = MENU_PANEL;
448 set_panel_filter (p);
451 void reread_cmd (void)
453 int flag;
455 if (get_current_type () == view_listing &&
456 get_other_type () == view_listing)
457 flag = strcmp (cpanel->cwd, opanel->cwd) ? UP_ONLY_CURRENT : 0;
458 else
459 flag = UP_ONLY_CURRENT;
461 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
462 repaint_screen ();
465 void reverse_selection_cmd (void)
467 file_entry *file;
468 int i;
470 for (i = 0; i < cpanel->count; i++){
471 file = &cpanel->dir.list [i];
472 if (S_ISDIR (file->buf.st_mode))
473 continue;
474 do_file_mark (cpanel, i, !file->f.marked);
476 paint_panel (cpanel);
479 void select_cmd (void)
481 char *reg_exp, *reg_exp_t;
482 int i;
483 int c;
484 int dirflag = 0;
486 reg_exp = input_dialog (_(" Select "), "", easy_patterns ? "*" : ".");
487 if (!reg_exp)
488 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 < cpanel->count; i++){
503 if (!strcmp (cpanel->dir.list [i].fname, ".."))
504 continue;
505 if (S_ISDIR (cpanel->dir.list [i].buf.st_mode)){
506 if (!dirflag)
507 continue;
508 } else {
509 if (dirflag)
510 continue;
512 c = regexp_match (reg_exp_t, cpanel->dir.list [i].fname, match_file);
513 if (c == -1){
514 message (1, MSG_ERROR, _(" Malformed regular expression "));
515 g_free (reg_exp);
516 return;
518 if (c){
519 do_file_mark (cpanel, i, 1);
522 paint_panel (cpanel);
523 g_free (reg_exp);
526 void unselect_cmd (void)
528 char *reg_exp, *reg_exp_t;
529 int i;
530 int c;
531 int dirflag = 0;
533 reg_exp = input_dialog (_(" Unselect "),"", easy_patterns ? "*" : ".");
534 if (!reg_exp)
535 return;
537 reg_exp_t = reg_exp;
539 /* Check if they specified directory matching */
540 if (*reg_exp_t == PATH_SEP){
541 dirflag = 1;
542 reg_exp_t ++;
544 if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
545 dirflag = 1;
546 reg_exp_t [strlen(reg_exp_t) - 1] = 0;
548 for (i = 0; i < cpanel->count; i++){
549 if (!strcmp (cpanel->dir.list [i].fname, ".."))
550 continue;
551 if (S_ISDIR (cpanel->dir.list [i].buf.st_mode)){
552 if (!dirflag)
553 continue;
554 } else {
555 if (dirflag)
556 continue;
558 c = regexp_match (reg_exp_t, cpanel->dir.list [i].fname, match_file);
559 if (c == -1){
560 message (1, MSG_ERROR, _(" Malformed regular expression "));
561 g_free (reg_exp);
562 return;
564 if (c){
565 do_file_mark (cpanel, i, 0);
568 paint_panel (cpanel);
569 g_free (reg_exp);
572 /* Check if the file exists */
573 /* If not copy the default */
574 static int
575 check_for_default(char *default_file, char *file)
577 struct stat s;
578 off_t count = 0;
579 double bytes = 0;
580 FileOpContext *ctx;
582 if (mc_stat (file, &s)){
583 if (mc_stat (default_file, &s)){
584 return -1;
586 ctx = file_op_context_new ();
587 file_op_context_create_ui (ctx, OP_COPY, 0);
588 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
589 file_op_context_destroy (ctx);
591 return 0;
594 void ext_cmd (void)
596 char *buffer;
597 char *extdir;
598 int dir;
600 dir = 0;
601 if (geteuid () == 0){
602 dir = query_dialog (_("Extension file edit"),
603 _(" Which extension file you want to edit? "), 0, 2,
604 _("&User"), _("&System Wide"));
606 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
608 if (dir == 0){
609 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
610 check_for_default (extdir, buffer);
611 do_edit (buffer);
612 g_free (buffer);
613 } else if (dir == 1)
614 do_edit (extdir);
616 g_free (extdir);
617 flush_extension_file ();
620 /* where = 0 - do edit file menu for mc */
621 /* where = 1 - do edit file menu for mcedit */
622 static void
623 menu_edit_cmd (int where)
625 char *buffer;
626 char *menufile;
627 int dir = 0;
629 dir = query_dialog (
630 _(" Menu edit "),
631 _(" Which menu file do you want to edit? "),
632 0, geteuid() ? 2 : 3,
633 _("&Local"), _("&Home"), _("&System Wide")
636 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
638 switch (dir) {
639 case 0:
640 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
641 check_for_default (menufile, buffer);
642 break;
644 case 1:
645 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
646 check_for_default (menufile, buffer);
647 break;
649 case 2:
650 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
651 break;
653 default:
654 g_free (menufile);
655 return;
657 do_edit (buffer);
658 if (dir == 0)
659 chmod(buffer, 0600);
660 g_free (buffer);
661 g_free (menufile);
664 void quick_chdir_cmd (void)
666 char *target;
668 target = hotlist_cmd (LIST_HOTLIST);
669 if (!target)
670 return;
672 if (get_current_type () == view_tree)
673 tree_chdir (the_tree, target);
674 else
675 if (!do_cd (target, cd_exact))
676 message (1, MSG_ERROR, _("Could not change directory") );
677 g_free (target);
680 /* edit file menu for mc */
681 void
682 edit_mc_menu_cmd (void)
684 menu_edit_cmd (0);
687 #ifdef USE_INTERNAL_EDIT
688 /* edit file menu for mcedit */
689 void
690 edit_user_menu_cmd (void)
692 menu_edit_cmd (1);
695 /* edit syntax file for mcedit */
696 void
697 edit_syntax_cmd (void)
699 char *buffer;
700 char *extdir;
701 int dir = 0;
703 if (geteuid () == 0) {
704 dir =
705 query_dialog (_("Syntax file edit"),
706 _(" Which syntax file you want to edit? "), 0, 2,
707 _("&User"), _("&System Wide"));
709 extdir = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
711 if (dir == 0) {
712 buffer = concat_dir_and_file (home_dir, SYNTAX_FILE);
713 check_for_default (extdir, buffer);
714 do_edit (buffer);
715 g_free (buffer);
716 } else if (dir == 1)
717 do_edit (extdir);
719 g_free (extdir);
721 #endif
723 #ifdef USE_VFS
724 void free_vfs_now (void)
726 vfs_expire (1);
729 void reselect_vfs (void)
731 char *target;
733 target = hotlist_cmd (LIST_VFSLIST);
734 if (!target)
735 return;
737 if (!do_cd (target, cd_exact))
738 message (1, MSG_ERROR, _("Could not change directory") );
739 g_free (target);
741 #endif /* USE_VFS */
743 static int compare_files (char *name1, char *name2, off_t size)
745 int file1, file2;
746 int result = -1; /* Different by default */
748 file1 = open (name1, O_RDONLY);
749 if (file1 >= 0){
750 file2 = open (name2, O_RDONLY);
751 if (file2 >= 0){
752 #ifdef HAVE_MMAP
753 char *data1, *data2;
754 /* Ugly if jungle */
755 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
756 if (data1 != (char*) -1){
757 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
758 if (data2 != (char*) -1){
759 rotate_dash ();
760 result = memcmp (data1, data2, size);
761 munmap (data2, size);
763 munmap (data1, size);
765 #else
766 /* Don't have mmap() :( Even more ugly :) */
767 char buf1[BUFSIZ], buf2[BUFSIZ];
768 int n1, n2;
769 rotate_dash ();
772 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
773 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
774 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
775 result = (n1 != n2) || memcmp(buf1,buf2,n1);
776 #endif /* !HAVE_MMAP */
777 close (file2);
779 close (file1);
781 return result;
784 enum CompareMode {
785 compare_quick, compare_size_only, compare_thourough
788 static void
789 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
791 int i, j;
792 char *src_name, *dst_name;
794 /* No marks by default */
795 panel->marked = 0;
796 panel->total = 0;
797 panel->dirs_marked = 0;
799 /* Handle all files in the panel */
800 for (i = 0; i < panel->count; i++){
801 file_entry *source = &panel->dir.list[i];
803 /* Default: unmarked */
804 file_mark (panel, i, 0);
806 /* Skip directories */
807 if (S_ISDIR (source->buf.st_mode))
808 continue;
810 /* Search the corresponding entry from the other panel */
811 for (j = 0; j < other->count; j++){
812 if (strcmp (source->fname,
813 other->dir.list[j].fname) == 0)
814 break;
816 if (j >= other->count)
817 /* Not found -> mark */
818 do_file_mark (panel, i, 1);
819 else {
820 /* Found */
821 file_entry *target = &other->dir.list[j];
823 if (mode != compare_size_only){
824 /* Older version is not marked */
825 if (source->buf.st_mtime < target->buf.st_mtime)
826 continue;
829 /* Newer version with different size is marked */
830 if (source->buf.st_size != target->buf.st_size){
831 do_file_mark (panel, i, 1);
832 continue;
835 if (mode == compare_size_only)
836 continue;
838 if (mode == compare_quick){
839 /* Thorough compare off, compare only time stamps */
840 /* Mark newer version, don't mark version with the same date */
841 if (source->buf.st_mtime > target->buf.st_mtime){
842 do_file_mark (panel, i, 1);
844 continue;
847 /* Thorough compare on, do byte-by-byte comparison */
848 src_name = concat_dir_and_file (panel->cwd, source->fname);
849 dst_name = concat_dir_and_file (other->cwd, target->fname);
850 if (compare_files (src_name, dst_name, source->buf.st_size))
851 do_file_mark (panel, i, 1);
852 g_free (src_name);
853 g_free (dst_name);
855 } /* for (i ...) */
858 void
859 compare_dirs_cmd (void)
861 int choice;
862 enum CompareMode thorough_flag;
864 choice =
865 query_dialog (_(" Compare directories "),
866 _(" Select compare method: "), 0, 3, _("&Quick"),
867 _("&Size only"), _("&Thorough"), _("&Cancel"));
869 if (choice < 0 || choice > 2)
870 return;
871 else
872 thorough_flag = choice;
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,
882 _(" Both panels should be in the "
883 "listing mode to use this command "));
887 void
888 history_cmd (void)
890 Listbox *listbox;
891 Hist *current;
893 if (cmdline->need_push) {
894 if (push_history (cmdline, cmdline->buffer) == 2)
895 cmdline->need_push = 0;
897 if (!cmdline->history) {
898 message (1, MSG_ERROR, _(" The command history is empty "));
899 return;
901 current = cmdline->history;
902 while (current->prev)
903 current = current->prev;
904 listbox = create_listbox_window (60, 10, _(" Command history "),
905 "[Command Menu]");
906 while (current) {
907 LISTBOX_APPEND_TEXT (listbox, 0, current->text, current);
908 current = current->next;
910 run_dlg (listbox->dlg);
911 if (listbox->dlg->ret_value == B_CANCEL)
912 current = NULL;
913 else
914 current = listbox->list->current->data;
915 destroy_dlg (listbox->dlg);
916 g_free (listbox);
918 if (!current)
919 return;
920 cmdline->history = current;
921 assign_text (cmdline, cmdline->history->text);
922 update_input (cmdline, 1);
925 void swap_cmd (void)
927 swap_panels ();
928 touchwin (stdscr);
929 repaint_screen ();
932 void
933 view_other_cmd (void)
935 static int message_flag = TRUE;
936 #ifdef HAVE_SUBSHELL_SUPPORT
937 char *new_dir = NULL;
938 char **new_dir_p;
939 #endif /* HAVE_SUBSHELL_SUPPORT */
941 if (!xterm_flag && !console_flag && !use_subshell){
942 if (message_flag)
943 message (1, MSG_ERROR, _(" Not an xterm or Linux console; \n"
944 " the panels cannot be toggled. "));
945 message_flag = FALSE;
946 } else {
947 channels_down ();
948 disable_mouse ();
949 if (clear_before_exec)
950 clr_scr ();
951 if (alternate_plus_minus)
952 numeric_keypad_mode ();
953 #ifndef HAVE_SLANG
954 /* With slang we don't want any of this, since there
955 * is no mc_raw_mode supported
957 reset_shell_mode ();
958 noecho ();
959 #endif /* !HAVE_SLANG */
960 keypad(stdscr, FALSE);
961 endwin ();
962 do_exit_ca_mode ();
963 mc_raw_mode ();
964 if (console_flag)
965 restore_console ();
967 #ifdef HAVE_SUBSHELL_SUPPORT
968 if (use_subshell){
969 new_dir_p = vfs_current_is_local () ? &new_dir : NULL;
970 if (invoke_subshell (NULL, VISIBLY, new_dir_p))
971 quiet_quit_cmd(); /* User did `exit' or `logout': quit MC quietly */
972 } else
973 #endif /* HAVE_SUBSHELL_SUPPORT */
975 if (output_starts_shell){
976 fprintf (stderr,
977 _("Type `exit' to return to the Midnight Commander"));
978 fprintf (stderr, "\n\r\n\r");
980 my_system (EXECUTE_AS_SHELL, shell, NULL);
981 } else
982 get_key_code (0);
984 if (console_flag)
985 handle_console (CONSOLE_SAVE);
987 do_enter_ca_mode ();
989 reset_prog_mode ();
990 keypad(stdscr, TRUE);
992 /* Prevent screen flash when user did 'exit' or 'logout' within
993 subshell */
994 if (quit)
995 return;
997 enable_mouse ();
998 channels_up ();
999 if (alternate_plus_minus)
1000 application_keypad_mode ();
1002 #ifdef HAVE_SUBSHELL_SUPPORT
1003 if (use_subshell){
1004 load_prompt (0, 0);
1005 if (new_dir)
1006 do_possible_cd (new_dir);
1007 if (console_flag && output_lines)
1008 show_console_contents (output_start_y,
1009 LINES-keybar_visible-output_lines-1,
1010 LINES-keybar_visible-1);
1012 #endif /* HAVE_SUBSHELL_SUPPORT */
1013 touchwin (stdscr);
1015 repaint_screen ();
1019 #ifndef NATIVE_WIN32
1020 static void
1021 do_link (int symbolic_link, char *fname)
1023 char *dest, *src;
1025 if (!symbolic_link) {
1026 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
1027 dest = input_expand_dialog (_(" Link "), src, "");
1028 g_free (src);
1029 if (!dest)
1030 return;
1031 if (!*dest) {
1032 g_free (dest);
1033 return;
1035 save_cwds_stat ();
1036 if (-1 == mc_link (fname, dest))
1037 message (1, MSG_ERROR, _(" link: %s "),
1038 unix_error_string (errno));
1039 } else {
1040 char *s;
1041 char *d;
1043 /* suggest the full path for symlink */
1044 s = concat_dir_and_file (cpanel->cwd, fname);
1046 if (get_other_type () == view_listing) {
1047 d = concat_dir_and_file (opanel->cwd, fname);
1048 } else {
1049 d = g_strdup (fname);
1052 symlink_dialog (s, d, &dest, &src);
1053 g_free (d);
1054 g_free (s);
1056 if (!dest || !*dest || !src || !*src) {
1057 if (src)
1058 g_free (src);
1059 if (dest)
1060 g_free (dest);
1061 return;
1063 save_cwds_stat ();
1064 if (-1 == mc_symlink (dest, src))
1065 message (1, MSG_ERROR, _(" symlink: %s "),
1066 unix_error_string (errno));
1067 g_free (src);
1069 g_free (dest);
1070 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1071 repaint_screen ();
1074 void link_cmd (void)
1076 do_link (0, selection (cpanel)->fname);
1079 void symlink_cmd (void)
1081 char *filename = NULL;
1082 filename = selection (cpanel)->fname;
1084 if (filename) {
1085 do_link (1, filename);
1089 void edit_symlink_cmd (void)
1091 if (S_ISLNK (selection (cpanel)->buf.st_mode)) {
1092 char buffer [MC_MAXPATHLEN];
1093 char *p = NULL;
1094 int i;
1095 char *dest, *q;
1097 p = selection (cpanel)->fname;
1099 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
1101 i = readlink (p, buffer, MC_MAXPATHLEN);
1102 if (i > 0) {
1103 buffer [i] = 0;
1104 dest = input_expand_dialog (_(" Edit symlink "), q, buffer);
1105 if (dest) {
1106 if (*dest && strcmp (buffer, dest)) {
1107 save_cwds_stat ();
1108 if (-1 == mc_unlink (p)){
1109 message (1, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1110 p, unix_error_string (errno));
1111 } else {
1112 if (-1 == mc_symlink (dest, p))
1113 message (1, MSG_ERROR, _(" edit symlink: %s "),
1114 unix_error_string (errno));
1116 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1117 repaint_screen ();
1119 g_free (dest);
1122 g_free (q);
1123 } else {
1124 message (1, MSG_ERROR, _("`%s' is not a symbolic link"),
1125 selection (cpanel)->fname);
1128 #endif /* !NATIVE_WIN32 */
1130 void help_cmd (void)
1132 interactive_display (NULL, "[main]");
1135 /* partly taken from dcigettext.c, returns "" for default locale */
1136 /* value should be freed by calling function g_free() */
1137 char *guess_message_value (void)
1139 static const char * const var[] = {
1140 /* The highest priority value is the `LANGUAGE' environment
1141 variable. This is a GNU extension. */
1142 "LANGUAGE",
1143 /* Setting of LC_ALL overwrites all other. */
1144 "LC_ALL",
1145 /* Next comes the name of the desired category. */
1146 "LC_MESSAGES",
1147 /* Last possibility is the LANG environment variable. */
1148 "LANG",
1149 /* NULL exit loops */
1150 NULL
1153 unsigned i = 0;
1154 char *locale = NULL;
1156 while (var[i] != NULL) {
1157 locale = getenv (var[i]);
1158 if (locale != NULL && locale[0] != '\0')
1159 break;
1160 i++;
1163 if (locale == NULL)
1164 locale = "";
1166 return g_strdup (locale);
1169 /* Returns a random hint */
1170 char *get_random_hint (void)
1172 char *data, *result, *eol;
1173 int len;
1174 int start;
1176 /* Do not change hints more often than one minute */
1178 #ifdef SCO_FLAVOR
1179 static time_t last;
1180 time_t now;
1182 time (&now);
1183 if ((now - last) < 60)
1184 return g_strdup ("");
1185 last = now;
1186 #else
1187 static int last_sec;
1188 static struct timeval tv;
1190 gettimeofday (&tv, NULL);
1191 if (!(tv.tv_sec> last_sec+60))
1192 return g_strdup ("");
1193 last_sec = tv.tv_sec;
1194 #endif /* !SCO_FLAVOR */
1196 data = load_mc_home_file (MC_HINT, NULL);
1197 if (!data)
1198 return 0;
1200 #ifdef SCO_FLAVOR
1201 srand ((short) now);
1202 #else
1203 srand (tv.tv_sec);
1204 #endif /* !SCO_FLAVOR */
1205 /* get a random entry */
1206 len = strlen (data);
1207 start = rand () % len;
1209 for (;start; start--){
1210 if (data [start] == '\n'){
1211 start++;
1212 break;
1215 eol = strchr (&data [start], '\n');
1216 if (eol)
1217 *eol = 0;
1218 result = g_strdup (&data [start]);
1219 g_free (data);
1220 return result;
1223 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1224 static void
1225 nice_cd (char *text, char *xtext, char *help, char *prefix, int to_home)
1227 char *machine;
1228 char *cd_path;
1230 if (!SELECTED_IS_PANEL)
1231 return;
1233 machine = input_dialog_help (text,
1234 xtext,
1235 help, "");
1236 if (!machine)
1237 return;
1239 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1240 ugly as hell and leads to problems in vfs layer */
1242 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1243 cd_path = g_strconcat (machine, to_home ? "/~/" : NULL, NULL);
1244 else
1245 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : NULL, NULL);
1247 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1248 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1249 else
1250 message (1, MSG_ERROR, _(" Could not chdir to %s "), cd_path);
1251 g_free (cd_path);
1252 g_free (machine);
1254 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1257 #ifdef USE_NETCODE
1259 static char *machine_str = N_(" Enter machine name (F1 for details): ");
1261 #ifdef WITH_MCFS
1262 void netlink_cmd (void)
1264 nice_cd (_(" Link to a remote machine "), _(machine_str),
1265 "[Network File System]", "/#mc:", 1);
1267 #endif /* WITH_MCFS */
1269 void ftplink_cmd (void)
1271 nice_cd (_(" FTP to machine "), _(machine_str),
1272 "[FTP File System]", "/#ftp:", 1);
1275 void fishlink_cmd (void)
1277 nice_cd (_(" Shell link to machine "), _(machine_str),
1278 "[FIle transfer over SHell filesystem]", "/#sh:", 1);
1281 #ifdef WITH_SMBFS
1282 void smblink_cmd (void)
1284 nice_cd (_(" SMB link to machine "), _(machine_str),
1285 "[SMB File System]", "/#smb:", 0);
1287 #endif /* WITH_SMBFS */
1289 #ifdef HAVE_SETSOCKOPT
1290 void source_routing (void)
1292 char *source;
1293 struct hostent *hp;
1295 source = input_dialog (_(" Socket source routing setup "),
1296 _(" Enter host name to use as a source routing hop: "),
1297 "");
1298 if (!source)
1299 return;
1301 hp = gethostbyname (source);
1302 g_free (source);
1303 if (!hp){
1304 message (1, _(" Host name "), _(" Error while looking up IP address "));
1305 return;
1307 source_route = *((int *)hp->h_addr);
1309 #endif /* HAVE_SETSOCKOPT */
1310 #endif /* USE_NETCODE */
1312 #ifdef USE_EXT2FSLIB
1313 void undelete_cmd (void)
1315 nice_cd (_(" Undelete files on an ext2 file system "),
1316 _(" Enter device (without /dev/) to undelete\n "
1317 " files on: (F1 for details)"),
1318 "[Undelete File System]", "/#undel:", 0);
1320 #endif /* USE_EXT2FSLIB */
1322 void quick_cd_cmd (void)
1324 char *p = cd_dialog ();
1326 if (p && *p) {
1327 char *q = g_strconcat ("cd ", p, NULL);
1329 do_cd_command (q);
1330 g_free (q);
1332 if (p)
1333 g_free (p);
1336 void
1337 dirsizes_cmd (void)
1339 WPanel *panel = cpanel;
1340 int i;
1341 off_t marked;
1342 double total;
1344 for (i = 0; i < panel->count; i++)
1345 if (S_ISDIR (panel->dir.list [i].buf.st_mode) &&
1346 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1347 !panel->dirs_marked) &&
1348 strcmp (panel->dir.list [i].fname, "..") != 0) {
1349 total = 0.0l;
1350 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1351 panel->dir.list [i].buf.st_size = (off_t) total;
1352 panel->dir.list [i].f.dir_size_computed = 1;
1355 recalculate_panel_summary (panel);
1356 paint_panel (panel);
1359 void
1360 save_setup_cmd (void)
1362 char *str;
1364 save_setup ();
1365 sync_profiles ();
1366 str = g_strconcat ( _(" Setup saved to ~/"), PROFILE_NAME, NULL);
1368 message (0, _(" Setup "), str);
1369 g_free (str);
1372 static void
1373 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1375 p->user_mini_status = use_msformat;
1376 p->list_type = view_type;
1378 if (view_type == list_user || use_msformat){
1379 g_free (p->user_format);
1380 p->user_format = user;
1382 g_free (p->user_status_format [view_type]);
1383 p->user_status_format [view_type] = status;
1385 set_panel_formats (p);
1387 else {
1388 g_free (user);
1389 g_free (status);
1392 set_panel_formats (p);
1393 paint_panel (p);
1395 do_refresh ();
1398 void
1399 info_cmd_no_menu (void)
1401 if (get_display_type (0) == view_info)
1402 set_display_type (0, view_listing);
1403 else if (get_display_type (1) == view_info)
1404 set_display_type (1, view_listing);
1405 else
1406 set_display_type (cpanel == left_panel ? 1 : 0, view_info);
1409 void
1410 quick_cmd_no_menu (void)
1412 if (get_display_type (0) == view_quick)
1413 set_display_type (0, view_listing);
1414 else if (get_display_type (1) == view_quick)
1415 set_display_type (1, view_listing);
1416 else
1417 set_display_type (cpanel == left_panel ? 1 : 0, view_quick);
1420 static void
1421 switch_to_listing (int panel_index)
1423 if (get_display_type (panel_index) != view_listing)
1424 set_display_type (panel_index, view_listing);
1427 void
1428 listing_cmd (void)
1430 int view_type, use_msformat;
1431 char *user, *status;
1432 WPanel *p;
1433 int display_type;
1435 display_type = get_display_type (MENU_PANEL_IDX);
1436 if (display_type == view_listing)
1437 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1438 else
1439 p = 0;
1441 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1443 if (view_type == -1)
1444 return;
1446 switch_to_listing (MENU_PANEL_IDX);
1448 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1450 configure_panel_listing (p, view_type, use_msformat, user, status);
1453 void
1454 tree_cmd (void)
1456 set_display_type (MENU_PANEL_IDX, view_tree);
1459 void
1460 info_cmd (void)
1462 set_display_type (MENU_PANEL_IDX, view_info);
1465 void
1466 quick_view_cmd (void)
1468 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == cpanel)
1469 change_panel ();
1470 set_display_type (MENU_PANEL_IDX, view_quick);
1473 /* Handle the tree internal listing modes switching */
1474 static int
1475 set_basic_panel_listing_to (int panel_index, int listing_mode)
1477 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1479 switch_to_listing (panel_index);
1480 p->list_type = listing_mode;
1481 if (set_panel_formats (p))
1482 return 0;
1484 paint_panel (p);
1485 do_refresh ();
1486 return 1;
1489 void
1490 toggle_listing_cmd (void)
1492 int current = get_current_index ();
1493 WPanel *p = (WPanel *) get_panel_widget (current);
1494 int list_mode = p->list_type;
1495 int m;
1497 switch (list_mode){
1498 case list_full:
1499 case list_brief:
1500 m = list_long;
1501 break;
1502 case list_long:
1503 m = list_user;
1504 break;
1505 default:
1506 m = list_full;
1508 if (set_basic_panel_listing_to (current, m))
1509 return;
1510 set_basic_panel_listing_to (current, list_full);