home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / dirstack.mks < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  9.9 KB

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