Cover V02, I01
Article
Listing 1
Listing 2
Listing 3
Listing 4

jan93.tar


Listing 2

1: #
2: # Project Directory and Scratch Directory Management Script
3: # Written by Leor Zolman, 10/92
4: #
5: # For inclusion in .profile under sh or ksh shells.
6: # This code is generalized, and will work as shown under both shells,
7: # though slightly better performance may be obtained under ksh by changing
8: # invocations of pwd into interpolations of the $PWD environment variable.
9: #
10:
11: #### Project directory management: #########################################
12:
13: PROJDIR=$HOME/.Proj     # Master directory for all project definitions
14: export PROJDIR          # (required for use by showp and shows scripts)
15: NPROJDIRS=12            # Maximum number of project directories
16: SCRATCHDIRS="x y z"     # scratchpad directory memory variables
17: SHOWCHANGE=Y            # Y to show result of dir change, else N (set to N
18:                         # if your prompt already displays working directory)
19: #
20: # Master project directory control function (called by p# functions below)
21: # usages:
22: #   p proj# dir     - add given directory (. for current) to project list
23: #   p proj#         - change directory to given project
24: #   p proj# done    - drop given project from project list
25: #
26:
27: showdir() { [ "$SHOWCHANGE" = Y ] && pwd; return 0; } # show new dir, if desired
28:
29: p()
30: {
31:     [ $# -eq 0 ] && echo "p() shell function invoked incorrectly." && return
32:     [ $1 -ge 1 ] 2>/dev/null && [ $1 -le $NPROJDIRS ] 2>/dev/null &&
33:     {
34:         if [ $# -eq 2 ]; then
35:             if [ $2 = done -a -f $PROJDIR/Proj$1 ]; then
36:                 rm $PROJDIR/Proj$1
37:                 eval "unset proj$1"
38:                 return
39:             fi
40:             target=$2
41:             [ $2 = "." ] && target=`pwd`
42:             echo $target >$PROJDIR/Proj$1
43:             # Define and export environment variable proj#:
44:             eval "proj$1=$target; export proj$1"
45:         else
46:             if [ ! -f $PROJDIR/Proj$1 ]; then
47:                 echo "No Project #$1 defined."
48:                 return
49:             fi
50:             touch $PROJDIR/Proj$1
51:             cd `cat $PROJDIR/Proj$1`
52:             showdir
53:         fi
54:         return
55:     }
56:     echo "p() function called with invalid project register number: $1"
57: }
58:
59: # Create project directory, if it does not already exist:
60: [ ! -d $HOME/.Proj ] && mkdir $HOME/.Proj
61:
62: #
63: # Define all quick-access functions p#(), and define simple
64: # environment variables proj# (for active projects only) by
65: # reading in their values from the .Proj directory:
66: #
67:
68: i=1
69: while [ $i -le $NPROJDIRS ]
70: do
71:     # Define quick-access function p#():
72:     eval "p$i() { p $i "'$*'"; }"
73:
74:     # if project active, define environment variable proj#:
75:     [ -f $PROJDIR/Proj$i ] &&
76:         eval "proj$i=`cat $PROJDIR/Proj$i`; export proj$i"
77:
78:     i=`expr $i + 1`
79: done
80:
81:
82: #### scratchpad directory shorthand function/variable definitions: #########
83:
84: for X in $SCRATCHDIRS
85: do
86:     # Define assignment function setX() for each X in SCRATCHLIST (each such
87:     # function defines a directory and a corresponding environment variable)
88:     eval "set$X() { $X=\`pwd\`; echo \$$X >$PROJDIR/${X}dir; }"
89:
90:     # Define function to undefine a directory and corresponding env. var:
91:     eval "unset$X() { unset $X; rm $PROJDIR/${X}dir; }"
92:
93:     # Define quick-access function goX() for each X in SCRATCHLIST:
94:     eval "go$X() {
95:         [ \"\$$X\" != \"\" ] && cd \$$X && touch $PROJDIR/${X}dir \
96:                 && showdir && return
97:             echo directory $X undefined.; }"
98:
99:     # Define environment variable X for each *defined* X in SCRATCHLIST:
100:    [ -f $PROJDIR/${X}dir ] && read $X < $PROJDIR/${X}dir
101:done