home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / dirstack.mks < prev    next >
Text File  |  1989-03-06  |  10KB  |  291 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v06i052: dirstack for MKS Toolkit Shell (IBM-PC/ATXT/AT/Clones)
  4. Reply-To: Keith Ericson <keithe@tekgvs.labs.tek.com>
  5. Organization: Tektronix, Inc., Beaverton,  OR.
  6.  
  7. Posting-number: Volume 6, Issue 52
  8. Submitted-by: keithe@tekgvs.labs.tek.com (Keith Ericson)
  9. Archive-name: dirstack.mks
  10.  
  11. [Please shar your postings, people.  ++bsa]
  12.  
  13. Herewith are
  14.  
  15.     1. dirstack.ksh
  16.  
  17.     2. color.ksh, and
  18.  
  19.     3. color.hlp
  20.  
  21. that I use with my MKS system.  Dirstack does what it sez.  Color.??? are
  22. used to set up the prompt string and give me the colors I want even if some
  23. other program comes along and changes 'em on me.  You can eliminate the
  24. "dynamic prompt string" and eliminate the need for color.???, or retain the
  25. dynamic prompt string, but eliminate the ${PSC} from the definition of PS1
  26. (near the end of the dirstack.ksh file) if you want.
  27.  
  28.     Have Fun...
  29. kEITH
  30.  
  31.  
  32. Keith Ericson  at TekLabs (resident factious factotum)
  33. Tektronix, PO 500, MS 58-383   Beaverton OR 97077    (503)627-6042
  34. UUCP:    uunet!tektronix!tekgvs!keithe           |Note:
  35.         or                   |  These addresses
  36.     uunet!tektronix!tekgvs.labs.tek.com!keithe |  may be hazardous
  37. ARPA:    keithe%tekgvs.LABS.TEK.COM@RELAY.CS.NET       |  to your mailer's
  38. CSNet:    keithe@tekgvs.LABS.TEK.COM           |  health!!
  39. "Of course, you only live one life, and you make all your mistakes,
  40.    and learn what not to do, and that's the end of you." - R.P. Feynman
  41. ===========================================================================
  42. #! /bin/sh
  43. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  44. # Contents:  DIRSTACK.KSH COLOR.KSH COLOR.HLP
  45. echo extracting 'DIRSTACK.KSH'
  46. if test -f 'DIRSTACK.KSH' -a -z "$1"; then echo Not overwriting 'DIRSTACK.KSH'; else
  47. sed 's/^X//' << \EOF > 'DIRSTACK.KSH'
  48. Xprint "Setting up the directory stack mechanism..."
  49. X# directory stack routines.
  50. X
  51. X# Bottom of stack is 0 and top of stack (TOS) is at ddepth.
  52. X
  53. X# Original for BSD ksh by David C. Stewart, Tektronix Inc., 7/31/85
  54. X# Modified for MKS Toolkit ksh by Keith Ericson, Tektronix Inc., 8/27/87
  55. X#
  56. X#    Note: for MKS, don't use the shorthand notation "[" for "test"
  57. X#    especially if you're using PC-NFS and ever do a 'cd' while the
  58. X#    current directory is a remotely-mounted filesystem.  For some
  59. X#    reason the "[" version of "test" takes abominably longer to
  60. X#    execute than does good ol' "test."  I've informed Gerry Wheeler
  61. X#    at Mortice Kern Systems about this but never heard if it had been
  62. X#    fixed.  - kde 1989Feb13
  63. X
  64. X#
  65. X# This is released to the public domain and is yours to uses as you see
  66. X#    fit. It might be nice to give credit where credit is due, however...
  67. X
  68. X#
  69. XHOST=`uname -n`
  70. X# Strip off all trailing blanks from the HOST string
  71. XHOST=${HOST%% }
  72. X
  73. Xtypeset dstack[0]=$HOME
  74. Xtypeset -i ddepth=0
  75. Xtypeset -i index
  76. X#
  77. X# dirs prints out the contents of the directory stack
  78. X#
  79. Xfunction dirs {
  80. X    index=$ddepth
  81. X    while test $index -ge 0 
  82. X    do    print -n ${dstack[index]} ''
  83. X        (( index=index-1 ))
  84. X    done
  85. X    echo
  86. X}
  87. X#
  88. X#pushd pushes entries onto the stack, or swaps TOS with the indexed-value
  89. X#    e.g., pushd +2 swaps TOS with the 3rd entry.
  90. X#    With no arguments pushd exchanges the top two dirstack elements and
  91. X#    changes to the new directory.
  92. X#
  93. Xfunction pushd {
  94. X    case $1 in
  95. X    +[0-9]|+[0-9][0-9])
  96. X        index=${1#+}
  97. X        tmp=${dstack[ddepth-index]}
  98. X        dstack[ddepth-index]=${dstack[ddepth]}
  99. X        dstack[$ddepth]=$tmp
  100. X        'cd' ${dstack[ddepth]}
  101. X        dirs
  102. X        unset tmp
  103. X        ;;
  104. X    +*)    echo 'usage: pushd [dir-name | +n]'
  105. X        ;;
  106. X    '')    if test $ddepth -eq 0 
  107. X        then    echo directory stack is empty
  108. X        else    dtemp=${dstack[ddepth-1]}
  109. X            dstack[ddepth-1]=${dstack[ddepth]}
  110. X            dstack[$ddepth]=$dtemp
  111. X            'cd' ${dstack[ddepth]}
  112. X            dirs
  113. X        fi
  114. X        ;;
  115. X    *)    'cd' $1
  116. X        if    test $? -eq 0 
  117. X        then     dstack[$ddepth+1]=$PWD
  118. X            (( ddepth=ddepth+1 ))
  119. X            dirs
  120. X        fi
  121. X        ;;
  122. X    esac
  123. Xkcd .
  124. X}
  125. X#
  126. X# Popd effectively pops the TOS entry off the stack and changes to the
  127. X#    directory named by the 'new' TOS.  If a (numeric) argument is
  128. X#    given then that element is removed and deeper elements bubbled
  129. X#    up in the stack.
  130. X#
  131. Xfunction popd {
  132. X    if test $ddepth -eq 0 
  133. X    then    echo directory stack is empty
  134. X    else    case $1 in
  135. X            '')    ;;
  136. X            +[0-9]|+[0-9][0-9])
  137. X                (( index=$ddepth-${1#+} ))
  138. X                if    test $index -lt 0 
  139. X                then     echo popd: bad argument value
  140. X                    return
  141. X                else    while    test $index -lt $ddepth 
  142. X                    do    dstack[$index]=${dstack[index+1]}
  143. X                        (( index=index+1 ))
  144. X                    done
  145. X                fi
  146. X                ;;
  147. X            *)    echo usage: popd [+n]
  148. X                return
  149. X                ;;
  150. X        esac
  151. X        (( ddepth=ddepth-1 ))
  152. X        'cd' ${dstack[ddepth]}
  153. X        dirs
  154. X    fi
  155. Xkcd .
  156. X}
  157. X
  158. X#
  159. X# The replacement directory changer function...
  160. X#
  161. X
  162. Xprint "Setting up dynamic prompt string"
  163. X
  164. Xfunction kcd {
  165. X    if    test $1 
  166. X    then
  167. X        'cd' $1
  168. X        dstack[$ddepth]=$PWD
  169. X    else
  170. X        'cd'
  171. X        dstack[$ddepth]=$HOME
  172. X    fi
  173. X#
  174. X# Now we set up the Prompt String to tell us (1) what machine we're
  175. X#    talking to (if you use rlogin to multiple machine this can help
  176. X#    retain your sanity) (maybe) and (2) which directory you're in.
  177. X#
  178. X#    the "if..then..else" replaces your home-directory-string with a
  179. X#    "~" (tilde) to reduce the length of the prompt when you're in
  180. X#    familiar territory.  Replace "c:/usr/keithe" with the appropriate
  181. X#    string in the two occurrances below.
  182. X#
  183. X    if    test "${PWD}" = "${PWD#c:/usr/keithe}" 
  184. X    then    TILDE=${PWD}
  185. X    else    TILDE="~"${PWD#c:/usr/keithe}
  186. X    fi
  187. X# ${PSC} is the "Prompt String Color" string set up by
  188. X#    /usr/bin/color.ksh
  189. XPS1="${PSC}[${HOST}]${TILDE}> "
  190. X}
  191. Xalias cd='kcd'
  192. Xalias pd='pushd .'
  193. Xalias sd='pushd'    # 'Cuz I can never remember that 'pushd' swaps the top
  194. X            #  two stack elements: 'sd' means "swap directories"
  195. Xkcd .
  196. X#
  197. EOF
  198. chars=`wc -c < 'DIRSTACK.KSH'`
  199. if test $chars !=     3877; then echo 'DIRSTACK.KSH' is $chars characters, should be     3877 characters!; fi
  200. fi
  201. echo extracting 'COLOR.KSH'
  202. if test -f 'COLOR.KSH' -a -z "$1"; then echo Not overwriting 'COLOR.KSH'; else
  203. sed 's/^X//' << \EOF > 'COLOR.KSH'
  204. XCOLOR=
  205. Xfor i in $@
  206. Xdo
  207. X    case $1 in
  208. X        HELP|help)    cat c:/usr/bin/COLOR.HLP
  209. X                exit ;;
  210. X        NORMAL|normal)    COLOR=${COLOR}'[0m'  ;;
  211. X        BRIGHT|bright)    COLOR=${COLOR}'[1m'  ;;
  212. X        DIM|dim)    COLOR=${COLOR}'[2m'  ;;
  213. X        REVERSE|reverse)    COLOR=${COLOR}'[7m' ;;
  214. X        BLACK|black)    COLOR=${COLOR}'[30m' ;;
  215. X        RED|red)    COLOR=${COLOR}'[31m' ;;
  216. X        GREEN|green)    COLOR=${COLOR}'[32m' ;;
  217. X        YELLOW|yellow)    COLOR=${COLOR}'[33m' ;;
  218. X        BLUE|blue)    COLOR=${COLOR}'[34m' ;;
  219. X        MAGENTA|magenta)    COLOR=${COLOR}'[35m' ;;
  220. X        CYAN|cyan)    COLOR=${COLOR}'[36m' ;;
  221. X        WHITE|white)    COLOR=${COLOR}'[37m' ;;
  222. X        BBLACK|bblack)    COLOR=${COLOR}'[40m' ;;
  223. X        BRED|bred)    COLOR=${COLOR}'[41m' ;;
  224. X        BGREEN|bgreen)    COLOR=${COLOR}'[42m' ;;
  225. X        BYELLOW|byellow)    COLOR=${COLOR}'[43m' ;;
  226. X        BBLUE|bblue)    COLOR=${COLOR}'[44m' ;;
  227. X        BMAGENTA|bmagenta)    COLOR=${COLOR}'[45m' ;;
  228. X        BCYAN|bcyan)    COLOR=${COLOR}'[46m' ;;
  229. X        BWHITE|bwhite)    COLOR=${COLOR}'[47m' ;;
  230. X        UNDER|under)    COLOR=${COLOR}'[4m'   ;;
  231. X        NOUNDER|nounder)    COLOR=${COLOR}'[24m' ;;
  232. X        BLINK|blink)    COLOR=${COLOR}'[5m'   ;;
  233. X        NOBLINK|noblink)    COLOR=${COLOR}'[25m' ;;
  234. X        BOLD|bold)    COLOR=${COLOR}'[1m'  ;;
  235. X        NOBOLD|nobold)    COLOR=${COLOR}'[22m' ;;
  236. X        INVISIBLE|invisible)    COLOR=${COLOR}'[8m' ;;
  237. X    esac
  238. X    shift
  239. Xdone
  240. X# let dirstack's "kcd" function know what happened here: export
  241. X#    the part of the prompt string that sets the colors
  242. Xtypeset -x PSC="${COLOR}"
  243. Xecho "${COLOR}"
  244. Xcd .
  245. EOF
  246. chars=`wc -c < 'COLOR.KSH'`
  247. if test $chars !=     1392; then echo 'COLOR.KSH' is $chars characters, should be     1392 characters!; fi
  248. fi
  249. #! /bin/sh
  250. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  251. # Contents:  COLOR.HLP
  252. echo extracting 'COLOR.HLP'
  253. if test -f 'COLOR.HLP' -a -z "$1"; then echo Not overwriting 'COLOR.HLP'; else
  254. sed 's/^X//' << \EOF > 'COLOR.HLP'
  255. XDocumentation for COLOR.KSH   [This file must be named COLOR.HLP]
  256. X
  257. X  COLOR.KSH may be used to set foreground and background colors on
  258. X  color monitors and set bold (high intensity), normal or reverse on
  259. X  any type of monitor.
  260. X
  261. X======================================================================
  262. X  Examples: COLOR NORMAL GREEN BOLD
  263. X            COLOR YELLOW BBLUE NORMAL BCYAN WHITE RED YELLOW BBLACK
  264. X            COLOR COLOR NORMAL GREEN
  265. X            COLOR MONO NORMAL BOLD
  266. X======================================================================
  267. XCOLOR.HLP FILE - THIS IS HELP FOR THE COLOR.KSH COMMAND FILE.
  268. X.
  269. X. ANSI.SYS, CONFIG.SYS (with DEVICE=ANSI.SYS) and MODE.COM must be on
  270. X.  the disk when the PC is booted.
  271. X. Type COLOR and then your choice of character, intensity, and/or
  272. X.  background color(s) from the following list of parameters:
  273. X. =====================================================================
  274. X.  ----------------------- GENERAL PARAMETERS -------------------------
  275. X. 1. normal (resets everything)       4. [no]under ([re]sets underline)
  276. X. 2. [no]bold (high intensity off/on) 5. [no]blink ([re]sets blink mode)
  277. X. 3. reverse (reverse video)          6. invisible (guess what...)
  278. X. =====================================================================
  279. X. ---------------------- FOR COLOR DISPLAYS ONLY ----------------------
  280. X. ---- FORGROUND COLORS ---------    ------ BACKGROUND COLORS ---------
  281. X. 1. black        5. blue            1. bblack          5. bblue
  282. X. 2. red          6. magenta         2. bred            6. bmagenta
  283. X. 3. green        7. cyan            3. bgreen          7. bcyan
  284. X. 4. yellow       8. white           4. byellow         8. bwhite
  285. X. =====================================================================
  286. EOF
  287. chars=`wc -c < 'COLOR.HLP'`
  288. if test $chars !=     1781; then echo 'COLOR.HLP' is $chars characters, should be     1781 characters!; fi
  289. fi
  290. exit 0
  291.