change dir using a menu command

David E edavid at 1net.gr
Fri Jan 23 16:21:39 UTC 2004


I did as you advised and added a "%cd" macro, I changed the code to do 2
things:

1. add an environment variable: CDFILE=~/.mc_cdfile
  (I used sprintf(dir_file,"CDFILE=%s/.mc_cdfile", home_dir) in the
   subshell.c file)

2. when the execute_menu_command() sees %cd inside the file it changed
  the variable 'change_dir' to '1' (initial value = 0),
  at the end of the function (after the menu command was executed), it
  searches for the ~/.mc_cdfile, if it exists, then it changes to the
  directory written in it.

I tried to add a remove() command after changing the directory (to
remove the $CDFILE), but for some reason it didn't work (i'm not sure i
did it right).

to use it all one has to write in a menu command is this:

  echo /path/to/new/directory > $CDFILE
  %cd


Anyway, I attached the patch files for subshell.c and user.c for this
feature, I hope this helps, I'm using it for a samba mounter script I 
wrote (also attached to this email).

David E.

On 02:53 Thu 22 Jan     , Pavel Roskin wrote:
> On Fri, 16 Jan 2004, David E wrote:
> 
> > Hi
> >
> > I've written a samba browser/mounter script, what it does is use
> > smbclient, to get a list of samba shares, and gives me a list of shares
> > using 'dialog', when I choose a share it is mounted. I've added a menu
> > item to the menu file, that will run the program, and I want mc to jump
> > to the mounted directory, I have looked around, and saw that it was
> > possible with the $MC_CONTROL_FILE variable, but then I saw that it was
> > disabled. So I'm wondering how (and if) it is possible.
> 
> It's currently impossible.  If you want to add this functionality, see
> execute_menu_command() in src/user.c.  You could add a %cd macro in a way
> similar to the recently added %view (you'll need the CVS version or
> mc-4.6.1-pre1 to see it).  Please note that mc doesn't execute the
> commands step by step.  The whole menu entry is put to a script and run as
> a whole.
> 
> -- 
> Regards,
> Pavel Roskin
> _______________________________________________
> Mc mailing list
> Mc at gnome.org
> http://mail.gnome.org/mailman/listinfo/mc


_______________________________________________________
Web Hosting from $2.5/mo, Special Offers: http://www.hostingkey.com
Free Email, Free WebMail, Free Banners Exchange: http://www.1net.gr
-------------- next part --------------
*** original/subshell.c	Sun Oct 26 08:46:02 2003
--- subshell.c	Thu Jan 22 23:57:15 2004
***************
*** 233,242 ****
--- 233,248 ----
  
  	    /* Allow alternative readline settings for MC */
  	    if (access (".mc/inputrc", R_OK) == 0)
  		putenv ("INPUTRC=.mc/inputrc");
  
+             /* david: begin CDFILE */
+             char env_cdfile[128] = "";
+             sprintf (env_cdfile, "CDFILE=%s/.mc_cdfile", home_dir);
+             putenv (env_cdfile);
+             /* david: end CDFILE */
+ 
  	    break;
  
  	/* TODO: Find a way to pass initfile to TCSH and ZSH */
  	case TCSH: case ZSH:
        	    break;
-------------- next part --------------
*** original/user.c	Thu Jan 22 18:07:20 2004
--- user.c	Thu Jan 22 23:58:47 2004
***************
*** 551,560 ****
--- 551,561 ----
      int  do_quote = 0;
      char prompt [80];
      int  col;
      char *file_name;
      int run_view = 0;
+     int change_dir = 0; /* david: change */
  
      /* Skip menu entry title line */
      commands = strchr (commands, '\n');
      if (!commands){
  	return;
***************
*** 625,636 ****
  		int i = check_format_view (commands + 1);
  		if (i) {
  		    commands += i;
  		    run_view = 1;
  		} else {
! 		    do_quote = 1; /* Default: Quote expanded macro */
! 		    expand_prefix_found = 1;
  		}
  	    } else
  		fputc (*commands, cmd_file);
  	}
      }
--- 626,646 ----
  		int i = check_format_view (commands + 1);
  		if (i) {
  		    commands += i;
  		    run_view = 1;
  		} else {
!                     i = check_format_cd (commands + 1); /* david: begin change */
!                     if (i)
!                     {
!                       commands += i;
!                       change_dir = 1;
!                     } /* david: end change */
!                     else
!                     {
! 		      do_quote = 1; /* Default: Quote expanded macro */
! 		      expand_prefix_found = 1;
!                     }
  		}
  	    } else
  		fputc (*commands, cmd_file);
  	}
      }
***************
*** 642,651 ****
--- 652,676 ----
      } else {
  	shell_execute (file_name, 0);
      }
      unlink (file_name);
      g_free (file_name);
+ 
+     if (change_dir) /* david: begin change */
+     {
+       char buffer[256] = "";
+       sprintf (buffer, "%s/.mc_cdfile", home_dir);
+       FILE *dir_file = fopen (buffer, "r");
+       if (dir_file)
+       {
+         if (fscanf (dir_file, "%255[^\n]s", buffer) == 1)
+         {
+           do_cd (buffer, cd_exact);
+         }
+       }
+       fclose (dir_file);
+     } /* david: end change */
  }
  
  /* 
  **	Check owner of the menu file. Using menu file is allowed, if
  **	owner of the menu is root or the actual user. In either case
-------------- next part --------------
#!/bin/bash
# Samba Browser Script
# Written by David Elentok. 09/01/04 17:02 
#
# Arguments:
#  -r  = reload (regenerates the share tree)
#  -mc = midnight commander mode (doesn't open an mc terminal after mounting)

tempfile="$(tempfile)"
treefile="$HOME/var/smbtree"
lastfile="$HOME/var/last_smb_mount"
mc_mode=0

if [ "$1" == "-r" -o ! -e "$treefile" ]; then
    TREE="$(smbtree)"
    echo "$TREE" > $treefile 
    shift
else
    TREE="$(cat $treefile)"
fi

if [ "$1" == "-mc" ]; then
  mc_mode=1
fi

data=$(echo "$TREE" | grep '	' | sed "s#	##g" | sed "s/[ ]/;/" | grep -v "^\\\\.[^\\]*;" | grep -v '\\IPC\$' | grep -v '\\ADMIN\$')

length=$(echo "$data" | grep -c $)

MENU=""

for x in $(seq $length)
do
  line=$(echo "$data" | sed -n ${x}p)

  share=$(echo $line | cut -d\; -f1)
  desc=$(echo $line | cut -d\; -f2)
  if [ -n "$desc" ]; then desc=" $desc"; fi

  share_check=$(echo $share | sed 's#\\#/#g')

  if [ -n "$(mount | grep $share_check)" ]; then
    stats="(*)"
  else
    stats="( )"
  fi

  MENU="$MENU '$share' '$stats$desc'"
#   echo "$MENU"

done

# read -n1

echo $MENU
eval "dialog --menu 'Samba Browser' 30 80 $length $MENU" 2> $tempfile

choice=$(cat $tempfile)

rm $tempfile

if [ -z "$choice" ]; then
  exit 0
fi

clear

share="$(echo $choice | gawk -F'\' '{print $NF}')"
name="$(echo $choice | gawk -F'\' '{print $(NF-1)}')"
dir="$HOME/mnt/$name/$share"

# ALREADY MOUNTED --------------------------------------------------------------
if [ -n "$(mount | grep $dir)" ]; then
  # NOT MC-MODE: unmount -------------------------------------------------------
  if [ $mc_mode -eq 0 ]; then
    echo "Unmounting [$dir]... "
    smbumount $dir
    exit $?
  # MC-MODE: just change directory ---------------------------------------------
  else
    echo $dir > "$lastfile"
    exit 0
  fi
fi

# NOT MOUNTED ------------------------------------------------------------------

# Create Directories
if [ ! -d "$HOME/mnt/$name" ]; then mkdir "$HOME/mnt/$name"; fi
if [ ! -d "$dir" ]; then mkdir "$dir"; fi

# Mount
echo "Mounting [$choice] at [$dir]"
smbmount $choice $dir

# IF Mounted -------------------------------------------------------------------
if [ $? -eq 0 ]; then
  if [ $mc_mode -eq 0 ]; then
    exec mc $dir
  else
    echo $dir > "$lastfile"
    exit 0
  fi
# IF NOT -----------------------------------------------------------------------
else
  rm "$lastfile"
  exit 1
fi



More information about the mc mailing list