#!/usr/bin/env perl # pushdir implementation in perl # usage: # cd "`ppushd-bin command args`" # where command can be one of: init, dirs, pushd, popd. init clears t +he dirstack. # the dirstacks are associated to the pid of the shell, so you should +call # ppushd-bin init >/dev/null # from every shell before you use the program. # I recommend adding the following snippets to your bashrc to use this +: # pdirs() { cd "`ppushd-bin dirs "$@"`"; } # pinit() { cd "`ppushd-bin init "$@"`"; } # ppushd() { cd "`ppushd-bin pushd "$@"`"; } # ppopd() { cd "`ppushd-bin popd "$@"`"; } # pinit # you may have to adapt these to work in other shells, # eg. converting the functions to tcsh aliases # bugs: # might not work for dirs with newlines in them. # the -nclpv switches or numeric arguments aren't implemented. # might work differently for symlinks than the shell does. # not well tested. use warnings; use strict; use Fcntl "LOCK_EX"; use Cwd; my $ppid = getppid(); 1 <= @ARGV or die q{usage: cd "`ppushd-bin command args`"\nwhere command is one +of: init, dirs, pushd, popd\n}; my($cmd, @arg) = @ARGV; my $wd = getcwd(); defined($ENV{"HOME"}) or die "HOME"; my $datname = $ENV{"HOME"} . "/.ppushd.dat"; my $D; open $D, "+<", $datname or $!{"ENOENT"} and open $D, "+>", $datname or die "error: cannot open data file: $!"; flock $D, LOCK_EX() or die "error: cannot lock data file exclusiviely: $!"; my @dat = <$D>; my $dat = ""; for my $n (0 .. @dat - 1) { $dat[$n] =~ /^(-?\d+)\s(.+)/ or next; $ppid == $1 and do { $dat = $2; splice @dat, $n, 1; last; }; } my @stk = map { pack "H*", $_ } $dat =~ /(\S+)/g; if ($cmd eq "init") { @arg and warn "warning: no arguments expected to init"; @stk = (); } elsif ($cmd eq "dirs") { print STDERR join(" ", @stk, $wd), "\n"; } elsif ($cmd eq "pushd") { if (0 == @arg) { 1 <= @stk or die "error: directory stack empty when trying to swap top +elements"; ($stk[-1], $wd) = ($wd, $stk[-1]); } elsif (1 == @arg) { push @stk, $wd; chdir $arg[0] or die "cannot chdir: $!"; $wd = getcwd(); } else { die "error: at most one argument expected after pushd"; } } elsif ($cmd eq "popd") { 1 <= @stk or die "error: directory stack empty when trying to popd"; @arg and die "error: no arguments expected to popd"; $wd = pop @stk; } else { die "error: invalid command"; } print $wd or die "error: cannot print wd"; 0 != @stk and push @dat, $ppid . " " . join(" ", map { unpack "H*", $_ } @stk) . + "\n"; seek $D, 0, 0 or die "error: cannot seek data file\n"; sub quickdump { die "as you might have just lost the dirstacks of all shells, here +'s a quick dump:\n@dat"; } print $D @dat or do { warn "error: cannot write to data file: $!"; die quickdump(); }; 0 <= (my $o = tell $D) or do { warn "error: cannot tell data file: $!"; die quickdump(); }; truncate $D, $o or do { warn "error: cannot tell data file: $!"; die quickdump(); }; close $D or do { warn "error: cannot tell data file: $!"; die quickdump(); }; __END__

In reply to Ppushd - emulate pushd and popd commands of the shell by ambrus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.