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

  1. From mipos3!intelca!oliveb!pyramid!decwrl!sun!pitstop!sundc!seismo!uunet!munnari!basser!john Thu Apr 14 18:00:20 PDT 1988
  2. Article 347 of comp.sources.misc:
  3. Path: td2cad!mipos3!intelca!oliveb!pyramid!decwrl!sun!pitstop!sundc!seismo!uunet!munnari!basser!john
  4. From: john@basser.cs.su.oz.AU (John Mackin)
  5. Newsgroups: comp.sources.misc
  6. Subject: v02i093: Autoloading shell scripts as functions
  7. Message-ID: <1226@basser.oz>
  8. Date: 12 Apr 88 21:41:16 GMT
  9. Sender: john@basser.oz
  10. Lines: 151
  11. Approved: john@basser.cs.su.oz.AU
  12.  
  13. comp.sources.misc: Volume 2, Issue 93
  14. Submitted-By: John Mackin <john@basser.cs.su.oz.AU>
  15. Archive-Name: autoload
  16.  
  17. # This is a shell archive.  Remove anything before this line,
  18. # then unpack it by saving it in a file and typing "sh file".
  19. #
  20. # Wrapped by john on Wed Apr 13 07:08:49 EST 1988
  21. # Contents:  README autoload which
  22.  
  23. echo x - README
  24. sed 's/^@//' > "README" <<'@//E*O*F README//'
  25. @        Autoloading Shell Scripts as Functions
  26.  
  27. @    Concept and Implementation by (in alphabetical order):
  28.  
  29. @        John Mackin <john@basser.cs.su.oz.AU>
  30. @        Boyd Roberts <boyd@basser.cs.su.oz.AU>
  31.  
  32.  
  33. If you don't have a shell that supports functions, don't bother with
  34. this.  If you do, well, we developed this under the Rob Pike (Eighth
  35. Edition) shell, but we tested it on System V (seems to work as long
  36. as you don't try to export any functions) and under ksh (seems like
  37. it might work, we aren't sure though).  Caveat emptor, it's mainly the
  38. idea that matters, we're sure you can make it work on your implementation
  39. if you want to.
  40.  
  41. Suppose you have a shell function that you don't often use, that must
  42. be a function and not a script (maybe it sets environment variables).
  43. Or just say you have some big functions that you would like to have
  44. around when you want them, but that don't clutter your environment
  45. when you're not using them (for those that can export functions, that
  46. is).  Then this code is for you.  Originally inspired by the Franz
  47. Lisp `autoload' mechanism, this allows you to write your functions
  48. as scripts, and have them autoloaded as functions into the calling
  49. shell the first time they are executed.
  50.  
  51. For example, if $HOME/bin was in your path, and $HOME/bin/foo contained:
  52.  
  53. @    #!/bin/sh
  54.  
  55. @    echo This is foo.
  56.  
  57. Then if you included our autoload functions in your .profile, along
  58. with a call to autoload:
  59.  
  60. @    autoload foo
  61.  
  62. foo would originally get a function definition that would cause its
  63. autoloading if it were ever used, and after it was first used it would
  64. be defined as:
  65.  
  66. @    foo()
  67. @    {
  68. @        echo This is foo.
  69. @    }
  70.  
  71. We've included a copy of `which' after Kernighan & Pike (`The UNIX
  72. Programming Environment') so that this can be 100% self-contained.  Feel
  73. free to use a shell builtin instead if you have one that works (we
  74. do, almost...).
  75.  
  76. One `gotcha' is that if you autoload a script that uses here documents
  77. (that is, `<<') you may be surprised by the result.  It works on our
  78. shell, though; well, sort of.  I have no idea what other shells will
  79. do with it.
  80.  
  81. Another `gotcha' is that you shouldn't blindly autoload any script;
  82. rather, it requires a little bit of pre-planning.  In particular,
  83. setting environment variables cuts both ways.  What's great for
  84. a cutie that changes $TERM based on what kind of terminal your
  85. window is emulating is no fun at all if the script (and hence
  86. the function) changes PATH, or worse, IFS.
  87.  
  88. Have fun!
  89.  
  90. John Mackin, Basser Department of Computer Science,
  91. @         University of Sydney, Sydney, Australia
  92.  
  93. john@basser.oz.AU (john%basser.oz.AU@UUNET.UU.NET)
  94. {uunet,mcvax,ukc,nttlab}!munnari!basser.oz!john
  95. @//E*O*F README//
  96. chmod u=rw,g=r,o=r README
  97. echo x - autoload
  98. sed 's/^@//' > "autoload" <<'@//E*O*F autoload//'
  99. autoload()
  100. {
  101. @    for i
  102. @    do
  103. @        eval "$i()
  104. @        {
  105. @            loadit $i &&
  106. @            $i "'"$@"'"
  107. @        }
  108. @        export $i
  109. @        "
  110. @    done
  111. }
  112.  
  113. loadit()
  114. {
  115. @    if cmd=`which $1`
  116. @    then
  117. @        eval "$1()
  118. @        {
  119. @            `sed 's/\([^A-Za-z0-9_]*\)exec[     ]/\1/
  120. @                  s/\([^A-Za-z0-9_]*\)exit[     ]/\1return /
  121. @                  s/\([^A-Za-z0-9_]*\)exit$/\1return/
  122. @                  s/\([^A-Za-z0-9_]*\)trap[     ]/\1: /' $cmd`
  123. @        }
  124. @        "
  125. @    else
  126. @        echo $1: not found >&2
  127. @        return 1
  128. @    fi
  129. }
  130.  
  131. export autoload loadit
  132. @//E*O*F autoload//
  133. chmod u=rw,g=r,o=r autoload
  134. echo x - which
  135. sed 's/^@//' > "which" <<'@//E*O*F which//'
  136. # which - which command will execute?
  137.  
  138. oldpath=$PATH
  139. PATH=/bin:/usr/bin
  140.  
  141. case $# in
  142.  
  143. @    1)    ;;
  144. @    *)    echo usage: `basename $0` command-name >&2; exit 2 ;;
  145.  
  146. esac
  147.  
  148. for i in `echo $oldpath | sed 's/^:/.:/
  149. @                   s/::/:.:/g
  150. @                   s/:$/:./
  151. @                   s/:/ /g'`
  152. do
  153. @    if test -f $i/$1
  154. @    then
  155. @    echo $i/$1
  156. @    exit 0
  157. @    fi
  158. done
  159.  
  160. exit 1
  161. @//E*O*F which//
  162. chmod u=rwx,g=rx,o=rx which
  163. exit 0
  164.  
  165.  
  166.