home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Util / reporter < prev    next >
Encoding:
Paul Falstad's zsh script  |  1996-05-06  |  9.4 KB  |  435 lines

  1. #!/usr/local/bin/zsh
  2. #
  3. # $Id: reporter,v 2.0 1996/05/02 22:57:04 hzoli Exp $
  4. # $Source: /l/src/zsh-RCS/Util/RCS/reporter,v $
  5. #
  6. # NAME:
  7. #    reporter
  8. #
  9. # SYNOPSIS:
  10. #    reporter [all | aliases | bindings | completion | functions |
  11. #            limits | options | variables]
  12. #
  13. # DESCRIPTION:
  14. #    "reporter" prints your current environment variables, shell
  15. #    variables, limits, completion settings, and option settings to
  16. #    stdout in the form of a script.
  17. #
  18. #    If you run into a zsh bug, someone can source the output script to
  19. #    recreate most of the environment under which you were working.
  20. #
  21. #    IMPORTANT:    "source" this script, don't try to run it directly.
  22. #            Otherwise it won't report the settings for your
  23. #            current shell session.
  24. #
  25. # OPTIONS:
  26. #    All command-line options can be abbreviated.
  27. #
  28. #    "aliases"    prints only aliases.
  29. #    "bindings"    prints only "bindkey" commands.
  30. #    "completion"    prints only "compctl" commands.
  31. #    "functions"    prints "autoload" commands or actual functions.
  32. #    "limits"    prints "limit" commands for things like cputime, etc.
  33. #    "options"    prints "setopt" commands.
  34. #    "variables"    prints both shell and environment variables.
  35. #
  36. #    "all"        tries to find every useful setting under your shell.
  37. #            This is the default, and it's the same as typing all
  38. #            of the above options on the command line.
  39. #
  40. # AUTHOR:
  41. #    Karl E. Vogel <vogelke@c17mis.wpafb.af.mil>
  42. #    Control Data Systems, Inc.
  43. # CAVEATS:
  44. #    Assumes that you have the following programs in your search path:
  45. #        awk, cut, echo, grep, sed, sort
  46. #    Assumes that your C preprocessor lives in /lib/cpp or /usr/ccs/lib/cpp.
  47. #    Uses (and unsets) variables beginning with "reporter_".
  48. #
  49. # RESTRICTIONS:
  50. #    DON'T:    pretend you wrote it, sell it, or blame me if it breaks.
  51. #    DO:    as ye will an' ye harm none.
  52. #                    --Wiccan saying, I think
  53. #
  54. # BUGS:
  55. #    I'm sure there are more than a few.  To be safe, run "zsh -f" before
  56. #    sourcing the output from this script.  If you have "screen", you may
  57. #    want to use that, too; I hammered my terminal settings beyond repair
  58. #    when using an early version, and "screen" saved me from having to
  59. #    login on another terminal.
  60. #
  61. # HISTORY:
  62. #    The name was ripped off from the Emacs "reporter.el" function.
  63. #    The idea came from a mail message to the ZSH mailing list:
  64. #
  65. # Begin Configuration Section
  66. #
  67.  
  68. reporter_OSVersion="`uname -s`_`uname -r`"
  69.  
  70. #
  71. # Solaris 2.x
  72. #
  73. case ${reporter_OSVersion} in
  74.     SunOS_5.*)
  75.         CPP=${CPP:-/usr/ccs/lib/cpp}
  76.         AWK=${AWK:-nawk}    # GNU AWK doesn't come standard :-(
  77.         ;;
  78. esac
  79.  
  80. #
  81. # Default Values 
  82. #
  83.  
  84. CPP=${CPP:-/lib/cpp}
  85. AWK=${AWK:-awk}
  86.  
  87. #
  88. # End Configuration Section
  89. #
  90.  
  91. reporter_do_all=yes
  92.  
  93. for each in $*
  94. do
  95.     case "$each"
  96.     in
  97.         ali*)    reporter_do_aliases=yes; reporter_do_all=no ;;
  98.         b*)    reporter_do_bindings=yes; reporter_do_all=no ;;
  99.         c*)    reporter_do_compctl=yes; reporter_do_all=no ;;
  100.         f*)    reporter_do_fun=yes; reporter_do_all=no ;;
  101.         l*)    reporter_do_lim=yes; reporter_do_all=no ;;
  102.         o*)    reporter_do_setopt=yes; reporter_do_all=no ;;
  103.         v*)    reporter_do_vars=yes; reporter_do_all=no ;;
  104.         *)    ;;
  105.     esac
  106. done
  107.  
  108. #
  109. #    The "cshjunkiequotes" option can break some of the commands
  110. #    used in the remainder of this script, so we check for that first
  111. #    and disable it.  We'll re-enable it later.
  112. #
  113. #    This bug was reported by Henry Guillaume <henryg@tusc.com.au>
  114. #
  115.  
  116. reporter_junkiequotes="no"
  117.  
  118. if setopt | grep "cshjunkiequotes" > /dev/null
  119. then
  120.     reporter_junkiequotes="yes"
  121.     unsetopt cshjunkiequotes
  122. fi
  123.  
  124. #
  125. #    UNAME
  126. #
  127. #    This shows your system name.  It's extremely system-dependent, so
  128. #    we need a way to find out what system you're on.  The easiest
  129. #    way to do this is by using "uname", but not everyone has that,
  130. #    so first we go through the search path.
  131. #
  132. #    If we don't find it, then the only thing I can think of is to
  133. #    check what's defined in your C compiler, and code in some exceptions
  134. #    for the location of "uname" or an equivalent.  For example, Pyramid
  135. #    has "uname" only in the ATT universe.  This code assumes that
  136. #    the "-a" switch is valid for "uname".
  137. #
  138. #    This section of code sees what is defined by "cpp".  It was
  139. #    originally written by brandy@tramp.Colorado.EDU (Carl Brandauer).
  140. #    Additional error checking and sed hacking added by Ken Phelps.
  141. #
  142.  
  143. reporter_cppdef=`strings -3 ${CPP} |
  144.     sed -n '
  145.     /^[a-zA-Z_][a-zA-Z0-9_]*$/{
  146.     s/.*/#ifdef &/p
  147.     s/.* \(.*\)/"\1";/p
  148.     s/.*/#endif/p
  149.     }
  150.     ' | ${CPP} |sed '
  151.     /^[     ]*$/d
  152.     /^#/d
  153.     s/.*"\(.*\)".*/\1/'`
  154.  
  155. reporter_uname=""
  156.  
  157. for each in `echo $PATH | sed -e 's/:/ /g'`
  158. do
  159.     if [ -x $each/uname ]
  160.     then
  161.         reporter_uname="$each/uname"
  162.         break
  163.     fi
  164. done
  165.  
  166. case "$reporter_uname"
  167. in
  168.     "")    reporter_uname="echo not found on this system" ;;
  169.     *)    ;;
  170. esac
  171.  
  172. for each in $reporter_cppdef
  173. do
  174.     case "$each"
  175.     in
  176.         pyr)    reporter_uname="/bin/att uname" ;;
  177.         *)    ;;
  178.     esac
  179. done
  180.  
  181. str=`eval $reporter_uname -a`
  182.  
  183. echo '# START zsh saveset'
  184. echo '# uname: ' $str
  185. echo
  186.  
  187. unset reporter_cppdef
  188. unset reporter_uname
  189.  
  190. #
  191. #    ALIASES
  192. #
  193. #    Use "alias -L" to get a listing of the aliases in the form we want.
  194. #
  195.  
  196. if test "$reporter_do_all" = "yes" -o "$reporter_do_aliases" = "yes"
  197. then
  198.     echo '# Aliases.'
  199.     echo
  200.  
  201.     alias -L
  202. fi
  203.  
  204. #
  205. #    KEY BINDINGS
  206. #
  207. #    You may get an occasional message stating that a key is not bound,
  208. #    but I don't think this will cause any problems.
  209. #    It hasn't for me. :)
  210. #
  211. #    The sed nonsense is to make sure that a key-binding like \e" is
  212. #    handled correctly.  I don't know how to handle ranges of keys
  213. #    (like "something to something is bound to self-insert") so I ignore
  214. #    them.
  215. #
  216.  
  217. if test "$reporter_do_all" = "yes" -o "$reporter_do_bindings" = "yes"
  218. then
  219.     echo
  220.     echo "# Key bindings."
  221.     echo
  222.  
  223.     bindkey | grep -v '" to "' | cut -f1 |
  224.         sed -e 's/\^\[/\\e/' -e 's/""/\\""/' |
  225.         ${AWK} '{print "bindkey -r " $0}'
  226.     echo
  227.     bindkey | grep -v '" to "' |
  228.         sed -e 's/\^\[/\\e/' -e 's/""/\\""/' |
  229.         ${AWK} 'NF > 1 {print "bindkey " $0}'
  230. fi
  231.  
  232. #
  233. #    COMPLETION COMMANDS
  234. #    Warning:  this won't work for zsh-2.5.03.
  235. #
  236.  
  237. if test "$reporter_do_all" = "yes" -o "$reporter_do_compctl" = "yes"
  238. then
  239.     echo
  240.     echo "# Completions."
  241.     echo
  242.  
  243.     compctl -L
  244. fi
  245.  
  246. #
  247. #    FUNCTIONS
  248. #
  249.  
  250. if test "$reporter_do_all" = "yes" -o "$reporter_do_fun" = "yes"
  251. then
  252.     echo 
  253.     echo "# Undefined functions."
  254.     echo
  255.  
  256.     functions | grep "undefined" | ${AWK} '{print "autoload " $2}'
  257.  
  258.     echo 
  259.     echo "# Defined functions."
  260.     echo
  261.  
  262.     functions | grep -v "undefined"
  263. fi
  264.  
  265. #
  266. #    LIMITS
  267. #
  268. #    "cputime" has to be handled specially, because you can specify
  269. #    the time as just hours, or "minutes:seconds".
  270. #
  271.  
  272. if test "$reporter_do_all" = "yes" -o "$reporter_do_lim" = "yes"
  273. then
  274.     echo
  275.     echo '# Limits.'
  276.     echo
  277.  
  278.     (
  279.         set X `limit | grep "cputime" | grep -v "unlimited" |
  280.             sed -e 's/:/ /g'`
  281.  
  282.         if test "$#" -gt 1
  283.         then
  284.             hr=$3
  285.             min=$4
  286.             sec=$5
  287.  
  288.             if test "$hr" -gt 0
  289.             then
  290.                 echo "limit cputime ${hr}h"
  291.             else
  292.                 echo "limit cputime $min:$sec"
  293.             fi
  294.         fi
  295.     )
  296.  
  297.     limit | grep -v "cputime" | grep -v "unlimited" |
  298.         sed -e 's/Mb/m/' -e 's/Kb/k/' |
  299.         ${AWK} 'NF > 1 {print "limit " $0}'
  300. fi
  301.  
  302. #
  303. #    NON-ARRAY VARIABLES
  304. #
  305. #    We run this in a subshell to preserve the TERMCAP and TERM settings
  306. #    in the current shell.  Also, reset the prompt to show you're now
  307. #    in a test shell.  I can't find an easy way to do IFS, so I ignore it.
  308. #
  309. #    Most of the sed nonsense is to make sure that variables are quoted
  310. #    when being set.  We also have to make sure that single-quotes and
  311. #    back-quotes are escaped.  This is why variable settings are
  312. #    surrounded by double quotes; some variables like SPROMPT have single
  313. #    quotes and back-quotes, and it's just too hard to escape those
  314. #    properly when setting them.
  315. #
  316.  
  317. if test "$reporter_do_all" = "yes" -o "$reporter_do_vars" = "yes"
  318. then
  319.     echo
  320.     echo "# Non-array variables."
  321.     echo
  322.  
  323.     (
  324.         echo "TERMCAP='$TERMCAP'"
  325.         echo "TERM='$TERM'"
  326.         unset TERMCAP
  327.  
  328.         set | grep '=' | grep -v 'prompt=' |
  329.             grep -v 'reporter_do' |
  330.             grep -v '^[!#$*0?@_-]=' |
  331.             grep -v '=(' | sed -e "s/'/\\\'/g" |
  332.                         sed -e 's/`/\\`/g' |
  333.             sed -e 's/=/="/' -e 's/$/"/' |
  334.             grep -v '^IFS=' |
  335.             grep -v '^TERMCAP=' |
  336.             grep -v '^TERM='
  337.  
  338.         echo "prompt='test%'"
  339.     )
  340.  
  341. #
  342. #    ARRAY VARIABLES
  343. #
  344. #    The "grep -v" nonsense is to keep from setting shell variables
  345. #    that caused me some trouble from a script.
  346. #
  347.  
  348.     echo
  349.     echo "# Array variables."
  350.     echo
  351.  
  352.     echo "argv=()"
  353.     set | grep '=' | grep -v 'argv=' |
  354.         grep -v 'reporter_do' | grep -v '^[!#$*0?@_-]=' |
  355.         grep '=('
  356.  
  357. #
  358. #    EXPORTED VARIABLES
  359. #
  360. #    Run this in a subshell to preserve the TERM and TERMCAP setting in
  361. #    the current shell.
  362. #
  363.  
  364.     echo
  365.     echo "# Exported variables."
  366.     echo
  367.  
  368.     (
  369.         echo "export TERMCAP"
  370.         echo "export TERM"
  371.         unset TERMCAP
  372.  
  373.         export | grep -v '^[!#$*0?@_-]=' |
  374.             ${AWK} -F='=' '{print "export " $1}' |
  375.             grep -v '^TERM=' | grep -v '^TERMCAP='
  376.     )
  377. fi
  378.  
  379. #
  380. #    SETOPT
  381. #
  382. #    We exclude interactive because "setopt interactive" has no effect.
  383. #    The cshjunkiequotes option is dealt with separately; see the
  384. #    comments near the start of the script.
  385. #
  386.  
  387. if test "$reporter_do_all" = "yes" -o "$reporter_do_setopt" = "yes"
  388. then
  389.     echo
  390.     echo '# Setopt.'
  391.     echo
  392.  
  393.     (
  394.         setopt | grep -v 'interactive' | ${AWK} '{print "setopt " $0}'
  395.  
  396.         case "$reporter_junkiequotes"
  397.         in
  398.             yes)    echo "setopt cshjunkiequotes" ;;
  399.             *)    ;;
  400.         esac
  401.     ) | sort
  402. fi
  403.  
  404. echo
  405. echo '# END zsh saveset'
  406.  
  407. #
  408. #    Don't put an exit here, or you'll get a nasty surprise when you
  409. #    source this thing.  Get rid of variables created when processing
  410. #    command line.
  411. #
  412.  
  413. unset reporter_do_all
  414. unset reporter_do_aliases
  415. unset reporter_do_bindings
  416. unset reporter_do_compctl
  417. unset reporter_do_fun
  418. unset reporter_do_lim
  419. unset reporter_do_setopt
  420. unset reporter_do_vars
  421.  
  422. #
  423. #    Turn cshjunkiequotes back on if necessary.
  424. #
  425.  
  426. case "$reporter_junkiequotes"
  427. in
  428.     yes)    setopt cshjunkiequotes ;;
  429.     *)    ;;
  430. esac
  431.  
  432. unset reporter_junkiequotes
  433.  
  434.