devinteske

tcshrc

Aug 6th, 2019 (edited)
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 81.58 KB | None | 0 0
  1. # -*- tab-width:  4 -*- ;; Emacs
  2. # vi: set tabstop=8     :: Vi/ViM
  3. #
  4. # .tcshrc
  5. #   Author:      Devin Teske <dteske@freebsd.org>
  6. #   Last Change: 2022 June 9
  7. ############################################################ CONFIGURATION
  8.  
  9. #
  10. # ka (keep-alive) command fortune(6) file
  11. #
  12. setenv KA_FORTUNE freebsd-tips
  13.  
  14. ############################################################ GLOBALS
  15.  
  16. #
  17. # Global exit status variables
  18. #
  19. setenv SUCCESS 0
  20. setenv FAILURE 1
  21.  
  22. #
  23. # Are we running interactively?
  24. #
  25. set interactive = 0
  26. if ( $?loginsh ) set interactive = 1
  27.  
  28. #
  29. # Key bindings
  30. #
  31. if ( "$interactive" ) then
  32.     bindkey "^W" backward-delete-word
  33.     bindkey "^U" backward-kill-line
  34. endif
  35.  
  36. #
  37. # Add ~/bin to PATH
  38. #
  39. switch ( $PATH )
  40. case ~/bin:
  41. case ~/bin\:*:
  42. case *\:~/bin\:*:
  43. case *\:~/bin:
  44. case $HOME/bin:
  45. case $HOME/bin\:*:
  46. case *\:$HOME/bin\:*:
  47. case *\:$HOME/bin:
  48. case ~/bin/:
  49. case ~/bin/\:*:
  50. case *\:~/bin/\:*:
  51. case *\:~/bin/:
  52. case $HOME/bin/:
  53. case $HOME/bin/\:*:
  54. case *\:$HOME/bin/\:*:
  55. case *\:$HOME/bin/:
  56.     breaksw
  57. default:
  58.     if ( $?PATH ) setenv PATH "$PATH:"
  59.     setenv PATH "$PATH$HOME/bin"
  60. endsw
  61.  
  62. #
  63. # OS Specifics
  64. # NB: Requires uname(1) -- from base system
  65. #
  66. setenv UNAME_S `uname -s`
  67. setenv UNAME_R `uname -r`
  68. setenv UNAME_P `uname -p`
  69. setenv MESSAGES /var/log/messages
  70. if ( ! $?UNAME_s ) setenv UNAME_s $UNAME_S
  71.  
  72. #
  73. # Where are apache's log files?
  74. #
  75. switch ( $UNAME_S )
  76. case FreeBSD:
  77.     switch ( $UNAME_R )
  78.     case 4.8-*:
  79.         setenv APACHE_ERROR_LOG /usr/local/www/logs/error_log
  80.         setenv APACHE_ACCESS_LOG /usr/local/www/logs/access_log
  81.         breaksw
  82.     default:
  83.         setenv APACHE_ERROR_LOG /usr/local/apache2/logs/error_log
  84.         setenv APACHE_ACCESS_LOG /usr/local/apache2/logs/access_log
  85.     endsw
  86.     breaksw
  87. case Linux:
  88.     setenv APACHE_ERROR_LOG /etc/httpd/logs/error_log
  89.     setenv APACHE_ACCESS_LOG /etc/httpd/logs/access_log
  90.     breaksw
  91. case CYGWIN*:
  92.     setenv APACHE_ERROR_LOG /var/log/apache2/error_log
  93.     setenv APACHE_ACCESS_LOG /var/log/apache2/access_log
  94.     breaksw
  95. endsw
  96.  
  97. #
  98. # Set DISPLAY for Xming
  99. #
  100. switch ( $UNAME_S )
  101. case CYGWIN*:
  102.     setenv DISPLAY localhost:0
  103. endsw
  104.  
  105. #
  106. # For dialog(1) and Xdialog(1) menus -- mainly ssh-agent-dup in FUNCTIONS below
  107. #
  108. set DIALOG_MENU_TAGS = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  109.  
  110. #
  111. # Default directory to store dialog(1) and Xdialog(1) temporary files
  112. #
  113. if ( ! $?DIALOG_TMPDIR ) set DIALOG_TMPDIR = "/tmp"
  114.  
  115. #
  116. # Literals
  117. # NB: Required by escape, shfunction, and eshfunction
  118. #
  119. set asc = `printf '\033['`
  120. set tab = " " # Must be a literal tab
  121. set nl = "\
  122. " # END-QUOTE
  123.  
  124. ############################################################ ALIASES
  125.  
  126. unalias quietly >& /dev/null
  127. alias quietly '\!* >& /dev/null'
  128.  
  129. quietly unalias grep
  130. alias grep "grep --color"
  131.  
  132. quietly unalias have
  133. alias have 'which \!* >& /dev/null'
  134.  
  135. quietly unalias eval2
  136. alias eval2 'echo \!*; eval \!*'
  137.  
  138. alias csh_na 'echo "Not available for [t]csh"; false'
  139. alias path_munge csh_na
  140.  
  141. # settitle $title ...
  142. #
  143. # Set the title of your ANSI-compatible shell window.
  144. #
  145. alias settitle printf '"\033]2;\!*\a\033]1;\!*\a\033]0;\!*\a"'
  146.  
  147. quietly unalias escape
  148. alias escape "awk '"'                                                       \\
  149.     BEGIN { a = sprintf("%c",39) }                                      \\
  150.     {                                                                   \\
  151.         gsub(a,"&\\\\&&")                                           \\
  152.         gsub(/ /, a "\\ " a)                                        \\
  153.         gsub(/\t/, a "$tab:q" a)                                    \\
  154.         buf=buf a "$nl:q" a $0                                      \\
  155.     }                                                                   \\
  156.     END { print a substr(buf,8) a }                                     \\
  157. '\'
  158.  
  159. # ssh-agent [ssh-agent options]
  160. #
  161. # Override ``ssh-agent'' to call an alias (you can always call the real binary
  162. # by executing /usr/bin/ssh-agent) that launches a background ssh-agent that
  163. # times-out in 30 minutes.
  164. #
  165. # Will evaluate the output of /usr/bin/ssh-agent (the real ssh-agent) called
  166. # with either a known-secure set of arguments (if none are provided) or the
  167. # unmodified arguments to this alias.
  168. #
  169. # Purpose is to prevent memorizing something like ``eval "`ssh-agent ...`"''
  170. # but instead simply ``ssh-agent [...]''.
  171. #
  172. # This allows you to, for example:
  173. #
  174. #   ssh-agent
  175. #   : do some ssh-add
  176. #   : do some commits
  177. #   ssh-agent -k
  178. #   : or instead of ``ssh-agent -k'' just wait 30m for it to die
  179. #
  180. # NB: Requires ssh-agent -- from base system
  181. #
  182. quietly unalias ssh-agent
  183. alias ssh-agent 'eval `/usr/bin/ssh-agent -c -t 1800 \!*`'
  184.  
  185. # function $name $code
  186. #
  187. # Define a ``function'' that runs in the current namespace.
  188. #
  189. # NB: Evaluated context (using `eval') may not merge namespace immediately.
  190. # NB: Commands considered unsafe by the shell may not work in this context.
  191. # NB: Using builtins such as if, while, switch, and others will invoke an adhoc
  192. # parser that appends to the history.
  193. #
  194. set alias_function = '                                                       \
  195.     set __name = $argv_function[1]                                       \
  196.     set __body = $argv_function[2]:q                                     \
  197.     set __var  = "$__name:as/-/_/"                                       \
  198.     set __argv = argv_$__var                                             \
  199.     set __alias = alias_$__var                                           \
  200.     set __body = "set ALIASNAME = $__name; "$__body:q                    \
  201.     alias $__name "set $__argv = (\!"\*");"$__body:q                     \
  202.     unset $__alias                                                       \
  203.     set $__alias = $__body:q                                             \
  204. '
  205. quietly unalias function
  206. alias function "set argv_function = (\!*); "$alias_function:q
  207.  
  208. # zless [OPTIONS] $file ...
  209. #
  210. # Allow quick paging of gzip-compressed files. Any options passed in-addition
  211. # to the filename pertain to zcat(1).
  212. #
  213. # NOTE: Some systems provide zless as an actual utility. For those systems,
  214. # we'll fallback to the actual utility opposed to this alias.
  215. #
  216. have zless || alias zless 'zcat \!* | less'
  217.  
  218. alias tlog                csh_na
  219. alias cvspicker           csh_na
  220.  
  221. # hgrep ...
  222. #
  223. # Use in-place of grep(1) whenever you want to preserve the first line of
  224. # output (often the header when viewing output from ps(1), lsof(8), df(1),
  225. # netstat(8), arp(8), lsmod(8), route(8), free(1), and many many others).
  226. #
  227. alias hgrep "/bin/sh -c '"'                                       \\
  228.     timeout=0;                                                \\
  229.     [ "$BASH_VERSION" ] && timeout=1 && shift;                \\
  230.     if [ $# -gt 1 ]; then                                     \\
  231.         for arg in "$@"; do                               \\
  232.             [ -f "$arg" ] && head -1 "$arg" && break; \\
  233.         done;                                             \\
  234.     else                                                      \\
  235.     (                                                         \\
  236.         IFS=;                                             \\
  237.         read -r -t $timeout LINE;                         \\
  238.         printf "\\e[1m%s\\e[0m\\n" "$LINE"                \\
  239.     );                                                        \\
  240.     fi;                                                       \\
  241.     grep "$@"                                                 \\
  242. '"' -- /bin/sh"
  243.  
  244. alias myshopts_usage          csh_na
  245. alias myshopts                csh_na
  246. alias cvs_admin_binaries      csh_na
  247. alias cvs_admin_binaries_undo csh_na
  248. alias cvs_add_recursive       csh_na
  249.  
  250. #
  251. # zlog_wrapper aliases (see zlog_wrapper above)
  252. #
  253. # !!!the below usage statement is generated from zlog_usage below!!!
  254. # Usage: ALIAS [OPTIONS] [PATTERN]
  255. # Arguments:
  256. #   PATTERN   Awk(1) regular expression pattern to search logfile for.
  257. # Options:
  258. #   -h        Print this usage statement and exit.
  259. #   -n        Only consider non-matching lines.
  260. #   -d1,2,3   Comma-separated list of field numbers to display as the
  261. #             date/time when providing summary output (ignored if `-v'
  262. #             is passed). Default is 1,2,3. A value of `NF' prints the
  263. #             entire line.
  264. #   -s        Summary. Show summarization of lines.
  265. #   -v        Verbose. Show individual lines.
  266. #
  267. alias messages 'zlog_wrapper messages "$MESSAGES"'
  268. alias aelog 'zlog_wrapper aelog "$APACHE_ERROR_LOG" -d1,2,3,4,5'
  269. alias aalog 'zlog_wrapper aalog "$APACHE_ACCESS_LOG" -d4,5,1'
  270.  
  271. #
  272. # tlog aliases (see tlog above)
  273. #
  274. # Usage: ALIAS [OPTIONS]
  275. # Options are sent to tail(1).
  276. #
  277. alias tm 'tlog "$MESSAGES"'
  278. alias tael 'tlog "$APACHE_ERROR_LOG"'
  279. alias taal 'tlog "$APACHE_ACCESS_LOG"'
  280.  
  281. #
  282. # Random keepalive [ka] alias (based on system-availability of various
  283. # fun utilities).
  284. #
  285. alias RANDOM 'printf "%d\n" 0`od</dev/urandom|awk '\''{print $NF;exit}'\''`'
  286. @ n = 0
  287. have worms   && eval set ka$n='"worms -d 25"' && @ n++
  288. have rain    && eval set ka$n='"rain -d 50"' && @ n++
  289. have grdc    && eval set ka$n='"grdc"' && @ n++
  290. have fortune
  291. if ( $? == 0 ) then
  292.     set fort='sh -c "while :;do fortune $KA_FORTUNE;sleep 80;echo --;done"'
  293.     eval set ka$n="'$fort'"
  294.     @ n++
  295. endif
  296. if ( $n <= 0 ) then
  297.     set ka0="sh -c 'while :;do echo -n .;sleep 80;done'"
  298.     @ n++
  299. endif
  300. alias ka '@ x = `RANDOM` / ( 65537 / '$n' ); eval eval \$ka$x'
  301. unset n
  302.  
  303. alias dbcat csh_na
  304.  
  305. #
  306. # Alias for becoming root while maintaining shell customizations (in reverse-
  307. # order of preference)
  308. #
  309. have su   && alias srsu 'su -m root'
  310. have sudo && alias srsu 'sudo env tcsh'
  311. have sr   && alias srsu 'sr env tcsh'
  312.  
  313. #
  314. # Editor settings (in reverse-order of preference)
  315. #
  316. have emacs && setenv EDITOR emacs
  317. have pico  && setenv EDITOR pico
  318. have nano  && setenv EDITOR nano
  319. have ee    && setenv EDITOR ee
  320. have vi    && setenv EDITOR vi
  321. have vim
  322. if ( ! $status ) then
  323.     setenv EDITOR vim
  324.     alias vi vim
  325.     alias dview "vim +/'^\(Index\|diff\|--\|@@\|====\).*' +'set hls' +'set syntax=diff' -R"
  326. endif
  327. alias edit $EDITOR
  328. have vimdiff
  329. if ( $status ) then
  330.     alias vimdiff "vim --cmd 'set scrollbind nowrap foldmethod=diff foldcolumn=2 diff' -O"
  331. endif
  332.  
  333. #
  334. # Forthisms
  335. #
  336. # I was spending a lot of time in Forth while writing loader_menu for FreeBSD,
  337. # and couldn't stop myself from using these to no-end of pain. So I decided to
  338. # add the below to ease my pain.
  339. #
  340. alias .s 'ls'
  341.  
  342. #
  343. # View man(1) pages with vim(1)
  344. #
  345. have vim && alias man 'man -P "col -b | vim -c '"'set ft=man nomod nolist'"' -"'
  346.  
  347. #
  348. # Abbreviations
  349. #
  350. alias b  basename
  351. alias d  date
  352. alias di 'dirs -v'
  353. alias h  history
  354. alias j  jobs
  355. alias la 'ls -al'
  356. alias ll 'ls -l'
  357. alias lt 'ls -Altr'
  358. alias pd pushd
  359. alias po popd
  360. alias z  suspend
  361. alias bx BitchX
  362.  
  363. #
  364. # OS-specific Aliases (until they can be generalized)
  365. #
  366. switch ( $UNAME_S )
  367. case Linux:
  368.     #
  369.     # Make ls(1) colorful
  370.     #
  371.     alias ls='ls --color=tty'
  372.  
  373.     #
  374.     # X-related aliases
  375.     #
  376.     alias Xsession /etc/X11/xdm/Xsession
  377.     alias xsession Xsession
  378.  
  379.     breaksw
  380. case FreeBSD:
  381. case Darwin:
  382.     #
  383.     # boottime: Prints the date/time (format based on current locale)
  384.     #           that the system booted (resolution is 1-second).
  385.     # bootsec:  Prints the number of seconds since the epoch (see `%s'
  386.     #           format in date(1)) that was recorded during boot,
  387.     #           allowing you to use the `-r seconds' syntax of date(1)
  388.     #           to represent the boottime in any desired format.
  389.     #
  390.     set kbt="sysctl kern.boottime"
  391.     alias boottime "$kbt | sed -e 's/.*} //'"
  392.     alias bootsec  "$kbt | sed -e 's/.* sec = \([0-9]\{1,\}\).*/\1/'"
  393.     unset kbt
  394.  
  395.     #
  396.     # bootfile: Prints the path to the running kernel.
  397.     #
  398.     alias bootfile 'sysctl -n kern.bootfile'
  399.  
  400.     #
  401.     # Make ls(1) colorful
  402.     #
  403.     alias ls 'ls -AG'
  404.  
  405.     #
  406.     # X-related aliases
  407.     #
  408.     alias Xsession /usr/X11R6/lib/X11/xdm/Xsession
  409.     alias xsession Xsession
  410.  
  411.     breaksw
  412. endsw
  413.  
  414. ############################################################ FUNCTIONS
  415.  
  416. # cmdsubst $var [$env ...] $cmd
  417. #
  418. # Evaluate $cmd via /bin/sh and store the results in $var.
  419. # Like "set $var = `env $env /bin/sh -c $cmd:q`" except output is preserved.
  420. #
  421. # NB: This function is unused in this file
  422. # NB: Requires escape alias -- from this file
  423. # NB: Requires /bin/sh -- from base system
  424. #
  425. quietly unalias cmdsubst
  426. function cmdsubst '                                                          \
  427.     set __var = $argv_cmdsubst[1]                                        \
  428.     set __argc = $#argv_cmdsubst                                         \
  429.     @ __argc--                                                           \
  430.     set __penv = ($argv_cmdsubst[2-$__argc]:q)                           \
  431.     @ __argc++                                                           \
  432.     set __cmd = $argv_cmdsubst[$__argc]:q                                \
  433.     set __out = `env $__penv:q /bin/sh -c $__cmd:q | escape`             \
  434.     eval set $__var = $__out:q                                           \
  435. '
  436.  
  437. # evalsubst [$env ...] $cmd
  438. #
  439. # Execute $cmd via /bin/sh and evaluate the results.
  440. # Like "eval `env $env /bin/sh -c $cmd:q`" except output is preserved.
  441. #
  442. # NB: This function is unused in this file
  443. # NB: Requires escape alias -- from this file
  444. # NB: Requires /bin/sh -- from base system
  445. #
  446. quietly unalias evalsubst
  447. function evalsubst '                                                         \
  448.     set __argc = $#argv_evalsubst                                        \
  449.     @ __argc--                                                           \
  450.     set __penv = ($argv_evalsubst[1-$__argc])                            \
  451.     @ __argc++                                                           \
  452.     eval eval `env $__penv:q /bin/sh -c                                 \\
  453.         $argv_evalsubst[$__argc]:q | escape`                         \
  454. '
  455.  
  456. # shfunction $name $code
  457. #
  458. # Define a ``function'' that runs under /bin/sh.
  459. # NB: No alias is created if one already exists.
  460. #
  461. quietly unalias shfunction
  462. function shfunction '                                                        \
  463.     set __name = $argv_shfunction[1]                                     \
  464.     set __argc = $#argv_shfunction                                       \
  465.     @ __argc--                                                           \
  466.     set __penv = ($argv_shfunction[2-$__argc]:q)                         \
  467.     @ __argc++                                                           \
  468.     set __body = $argv_shfunction[$__argc]:q                             \
  469.     set __var = "$__name:as/-/_/"                                        \
  470.     set __alias = shalias_$__var                                         \
  471.     set __func = shfunc_$__var                                           \
  472.     set __interp = "env $__penv:q /bin/sh -c "\"\$"${__alias}:q"\"       \
  473.     set __body = "local FUNCNAME=$__var ALIASNAME=$__name; $__body:q"    \
  474.     set __body = "$__var(){$nl:q$__body:q$nl:q}"                         \
  475.     set $__func = $__body:q                                              \
  476.     set $__alias = $__body:q\;\ $__var\ \"\$@\"                          \
  477.     have $__name || alias $__name "$__interp /bin/sh"                    \
  478. '
  479.  
  480. # eshfunction $name $code
  481. #
  482. # Define a ``function'' that runs under /bin/sh but produces output that is
  483. # evaluated in the current shell's namespace.
  484. #
  485. # NB: No alias is created if one already exists.
  486. #
  487. quietly unalias eshfunction
  488. function eshfunction '                                                       \
  489.     set __name = $argv_eshfunction[1]                                    \
  490.     set __argc = $#argv_eshfunction                                      \
  491.     @ __argc--                                                           \
  492.     set __penv = ($argv_eshfunction[2-$__argc]:q)                        \
  493.     @ __argc++                                                           \
  494.     set __body = $argv_eshfunction[$__argc]:q                            \
  495.     set __var = "$__name:as/-/_/"                                        \
  496.     set __alias = shalias_$__var                                         \
  497.     set __func = shfunc_$__var                                           \
  498.     set __interp = "env $__penv:q /bin/sh -c "\"\$"${__alias}:q"\"       \
  499.     set __interp = "$__interp:q /bin/sh \!"\*" | escape"                 \
  500.     set __body = "local FUNCNAME=$__var ALIASNAME=$__name; $__body:q"    \
  501.     set __body = "$__var(){$nl:q$__body:q$nl:q}"                         \
  502.     set $__func = $__body:q                                              \
  503.     set $__alias = $__body:q\;\ $__var\ \"\$@\"                          \
  504.     have $__name || alias $__name                                       \\
  505.         '\''eval eval `'\''$__interp:q'\''`'\''                      \
  506. '
  507.  
  508. # quietly $cmd ...
  509. #
  510. # Execute /bin/sh $cmd while sending stdout and stderr to /dev/null.
  511. #
  512. shfunction quietly '"$@" > /dev/null 2>&1'
  513.  
  514. # have name
  515. #
  516. # Silently test for name as an available command, builtin, or other executable.
  517. #
  518. shfunction have 'type "$@" > /dev/null 2>&1'
  519.  
  520. # eval2 $cmd ...
  521. #
  522. # Print $cmd on stdout before executing it.
  523. #
  524. shfunction eval2 'echo "$*"; eval "$@"'
  525.  
  526. # fprintf $fd $fmt [$opts ...]
  527. #
  528. # Like printf, except allows you to print to a specific file-descriptor. Useful
  529. # for printing to stderr (fd=2) or some other known file-descriptor.
  530. #
  531. quietly unalias fprintf
  532. shfunction fprintf '                                                         \
  533.     fd=$1                                                                \
  534.     [ $# -gt 1 ] || return ${FAILURE:-1}                                 \
  535.     shift 1                                                              \
  536.     printf "$@" >&$fd                                                    \
  537. '
  538.  
  539. # eprintf $fmt [$opts ...]
  540. #
  541. # Like printf, except send output to stderr (fd=2).
  542. #
  543. quietly unalias eprintf
  544. shfunction eprintf \
  545.     '__fprintf=$shfunc_fprintf:q' \
  546. '                                                                            \
  547.     eval "$__fprintf"                                                    \
  548.     fprintf 2 "$@"                                                       \
  549. '
  550.  
  551. # zlog $logfile [-n] [$pattern]
  552. #
  553. # A general purpose log searching function. Given $log base pathname, this
  554. # function will search for all occurrences of $pattern in $logfile and
  555. # $logfile.N.{gz,bz2} [FreeBSD] or $logfile.N [Linux] (preserving reverse-
  556. # chronological order, searching oldest logfiles first).
  557. #
  558. # If $pattern is NULL or omitted, all lines are returned.
  559. # If -n is given, non-matching lines are returned.
  560. #
  561. # NOTE: $pattern is an awk regular expression.
  562. #
  563. # NB: Requires have() -- from this file
  564. #
  565. quietly unalias zlog
  566. shfunction zlog \
  567.     '__have=$shfunc_have:q' \
  568. ' \
  569.     eval "$__have"                                                   \
  570.                                                                      \
  571.     local use_stdin=                                                 \
  572.     if [ -t 0 ]; then                                                \
  573.         local logfile="$1"                                       \
  574.     else                                                             \
  575.         local logfile="/dev/stdin"                               \
  576.         use_stdin=1                                              \
  577.     fi                                                               \
  578.     local logfiles="$logfile" zlogs="" n=0                           \
  579.     local nomatch=                                                   \
  580.     local sudo= needsudo=                                            \
  581.                                                                      \
  582.     shift 1 # logfile                                                \
  583.                                                                      \
  584.     local OPTIND flag                                                \
  585.     while getopts n flag; do                                         \
  586.         case "$flag" in                                          \
  587.         n) nomatch=1 ;;                                          \
  588.         esac                                                     \
  589.     done                                                             \
  590.     shift $(( $OPTIND - 1 ))                                         \
  591.                                                                      \
  592.     #                                                                \
  593.     # Check for two conditions where we would need sudo              \
  594.     # 1. If the log file is not readable                             \
  595.     # 2. If the parent directory is not readable/traversable         \
  596.     #                                                                \
  597.     if [ ! "$use_stdin" ]; then                                      \
  598.         [ -r "$logfile" ] || needsudo=1                          \
  599.         case "$logfile" in                                       \
  600.         */*) [ -r "${logfile%/*}" ] || needsudo=1;;              \
  601.         esac                                                     \
  602.     fi                                                               \
  603.                                                                      \
  604.     if [ "$needsudo" ]; then                                         \
  605.         if [ "$UNAME_s" = "Linux" -a "$Linux" = "Ubuntu" ]; then \
  606.             sudo=sudo                                        \
  607.         else                                                     \
  608.             if have sr; then                                 \
  609.                 sudo=sr                                  \
  610.             elif have sudo; then                             \
  611.                 sudo=sudo                                \
  612.             fi                                               \
  613.         fi                                                       \
  614.     fi                                                               \
  615.                                                                      \
  616.     case "$UNAME_s" in                                               \
  617.     FreeBSD)                                                         \
  618.         #                                                        \
  619.         # FreeBSD rotates logs in the following fashion:         \
  620.         #                                                        \
  621.         # FILENAME        DESCRIPTION                            \
  622.         # logfile         current log file                       \
  623.         # logfile.0.gz    most-recently rotated log file         \
  624.         # logfile.1.gz    second-most recently rotated log file  \
  625.         #                                                        \
  626.         local n=0                                                \
  627.         if [ "$needsudo" ]; then                                 \
  628.             while $sudo [ -f "$logfile.$n.gz" ]; do          \
  629.                 zlogs="$logfile.$n.gz $zlogs"            \
  630.                 n=$(( $n + 1 ))                          \
  631.             done                                             \
  632.             if [ ! "$zlogs" ]; then                          \
  633.                 n=0                                      \
  634.                 while $sudo [ -f "$logfile.$n.bz2" ]; do \
  635.                     zlogs="$logfile.$n.bz2 $zlogs"   \
  636.                     n=$(( $n + 1 ))                  \
  637.                 done                                     \
  638.             fi                                               \
  639.         elif [ ! "$use_stdin" ]; then                            \
  640.             while [ -f "$logfile.$n.gz" ]; do                \
  641.                 [ -r "$logfile.$n.gz" ] || needsudo=1    \
  642.                 zlogs="$logfile.$n.gz $zlogs"            \
  643.                 n=$(( $n + 1 ))                          \
  644.             done                                             \
  645.             if [ ! "$zlogs" ]; then                          \
  646.                 n=0                                      \
  647.                 while $sudo [ -f "$logfile.$n.bz2" ]; do \
  648.                     zlogs="$logfile.$n.bz2 $zlogs"   \
  649.                     n=$(( $n + 1 ))                  \
  650.                 done                                     \
  651.             fi                                               \
  652.         fi                                                       \
  653.         ;;                                                       \
  654.     Linux)                                                           \
  655.         #                                                        \
  656.         # Linux rotates logs in the following fashion:           \
  657.         #                                                        \
  658.         # FILENAME     DESCRIPTION                               \
  659.         # logfile      current log file                          \
  660.         # logfile.1    most-recently rotated log file            \
  661.         # logfile.2    second-most recently rotated log file     \
  662.         #                                                        \
  663.         local n=1                                                \
  664.         if [ "$needsudo" ]; then                                 \
  665.             while $sudo [ -f "$logfile.$n" ]; do             \
  666.                 logfiles="$logfile.$n $logfiles"         \
  667.                 n=$(( $n + 1 ))                          \
  668.             done                                             \
  669.         elif [ ! "$use_stdin" ]; then                            \
  670.             while [ -f "$logfile.$n" ]; do                   \
  671.                 [ -r "$logfile.$n" ] || needsudo=1       \
  672.                 logfiles="$logfile.$n $logfiles"         \
  673.                 n=$(( $n + 1 ))                          \
  674.             done                                             \
  675.         fi                                                       \
  676.         ;;                                                       \
  677.     esac                                                             \
  678.                                                                      \
  679.     if [ "$nomatch" ]; then                                          \
  680.         awk_code='\''                                           \\
  681.             $0 ~ pattern { ign = 1; next }                  \\
  682.             ign && /repeated/ { next }                      \\
  683.             { ign = 0; print }                              \\
  684.         '\'' # END-QUOTE                                         \
  685.     else                                                             \
  686.         awk_code='\''                                           \\
  687.             $0 ~ pattern { ok = 1; print; next }            \\
  688.             ok && /repeated/ { print; next }                \\
  689.             { ok = 0 }                                      \\
  690.         '\'' # END-QUOTE                                         \
  691.     fi                                                               \
  692.                                                                      \
  693.     local pattern="$1"                                               \
  694.     if [ "$zlogs" ]; then                                            \
  695.         $sudo zcat $zlogs | sh -c "cat; cat $logfiles" |         \
  696.             awk -v pattern="$pattern" "$awk_code"            \
  697.     else                                                             \
  698.         $sudo cat $logfiles |                                    \
  699.             awk -v pattern="$pattern" "$awk_code"            \
  700.     fi                                                               \
  701. '
  702.  
  703. # zlog_summary $logfile [-d1,2,3] [-n] [$pattern]
  704. #
  705. # A wrapper around the zlog general purpose log searching function designed to
  706. # provide a brief summary containing the following information:
  707. #   - The first line matching $pattern
  708. #   - The timestamp of the first occurrence
  709. #   - The timestamp of the last occurrence
  710. #   - How many occurrences were found
  711. #     NOTE: Also counts "last message repeated N times" entries
  712. #
  713. # NOTE: If $pattern is NULL or omitted, the first/oldest log-entry is printed.
  714. #       In addition, the timestamps in the summary will instead be the first
  715. #       (oldest) and last (newest) entry, respectively. This serves as a sort
  716. #       of brief look at the date-range spanning the individual log files.
  717. #
  718. # NOTE: $pattern is an awk regular expression.
  719. #
  720. # By default, it is assumed that the date/time fields to be displayed are the
  721. # first, second, and third fields of each log entry, separated by whitespace.
  722. # For example, /var/log/messages has the following first three fields:
  723. #
  724. #   Jun  5 04:02:10
  725. #
  726. # However, not all logfiles display date/time entries in the above manner.
  727. # Therefore, the following options can be passed to customize which line-fields
  728. # (and how many) are displayed for the date/time:
  729. #
  730. # For example, the Apache error_log displays the date/time in five pieces:
  731. #
  732. #   [Sun May 31 04:02:14 2009]
  733. #
  734. # So to display the proper values for date-range outputs when parsing the
  735. # Apache error_log, you should pass the following parameters:
  736. #
  737. #   -d1,2,3,4,5
  738. #
  739. # You can optionally pass `-dNF' to print the whole line.
  740. #
  741. # If `-n' is given, only non-matching lines are considered.
  742. #
  743. # NB: Requires zlog() -- from this file
  744. # NB: Requires awk(1) -- from base system
  745. #
  746. quietly unalias zlog_summary
  747. shfunction zlog_summary \
  748.     '__have=$shfunc_have:q' \
  749.     '__zlog=$shfunc_zlog:q' \
  750. ' \
  751.     eval "$__have"                                                        \
  752.     eval "$__zlog"                                                        \
  753.                                                                           \
  754.     local datetime="1,2,3"                                                \
  755.     local logfile="$1"                                                    \
  756.     local nomatch=                                                        \
  757.                                                                           \
  758.     shift 1 # logfile                                                     \
  759.                                                                           \
  760.     local OPTIND flag                                                     \
  761.     while getopts d:n flag; do                                            \
  762.         case "$flag" in                                               \
  763.         d) datetime="$OPTARG";;                                       \
  764.         n) nomatch=1 ;;                                               \
  765.         esac                                                          \
  766.     done                                                                  \
  767.     shift $(( $OPTIND - 1 ))                                              \
  768.                                                                           \
  769.     zlog "$logfile" ${nomatch:+-n} "$1" |                                 \
  770.         awk -v datetime="$datetime" '\''                             \\
  771.         BEGIN {                                                      \\
  772.             hr = sprintf("%-60s", "")                            \\
  773.             gsub(" ", "-", hr)                                   \\
  774.             ndt = split(datetime, dt, /,/)                       \\
  775.         }                                                            \\
  776.         function dtparse(        n, parsed)                          \\
  777.         {                                                            \\
  778.             if (length(datetime) <= 0) return ""                 \\
  779.             parsed = ""                                          \\
  780.             for (n = 1; n <= ndt; n++)                           \\
  781.                 parsed = sprintf("%s %s", parsed, $dt[n])    \\
  782.             return substr(parsed, 2)                             \\
  783.         }                                                            \\
  784.         function summarize()                                         \\
  785.         {                                                            \\
  786.             count += /repeated/ ? $(NF-1) : 1                    \\
  787.             last = dtparse()                                     \\
  788.         }                                                            \\
  789.         ! init {                                                     \\
  790.             init = 1                                             \\
  791.             print hr                                             \\
  792.             print $0                                             \\
  793.             print hr                                             \\
  794.             print " 1st: " dtparse()                             \\
  795.         }                                                            \\
  796.         { summarize() }                                              \\
  797.         END {                                                        \\
  798.             printf "Last: %s\n", last                            \\
  799.             printf "Total Occurrences: %d\n", count              \\
  800.             print hr                                             \\
  801.         }                                                            \\
  802.     '\'' # END-QUOTE                                                      \
  803. '
  804.  
  805. # zlog_usage [$FUNCNAME]
  806. #
  807. # Print usage statement for zlog_wrapper family of functions.
  808. #
  809. # NB: Requires eprintf() -- from this file
  810. #
  811. quietly unalias zlog_usage
  812. shfunction zlog_usage \
  813.     '__eprintf=$shfunc_eprintf:q' \
  814.     '__fprintf=$shfunc_fprintf:q' \
  815. ' \
  816.     eval "$__fprintf"                 \
  817.     eval "$__eprintf"                 \
  818.                                          \
  819.     local func="${1:-$FUNCNAME}"      \
  820.     local optfmt='\''\t%-9s %s\n'\'' \
  821.     eprintf "Usage: %s [-hnv] [-d1,2,3] [PATTERN]\n" "$func" \
  822.     eprintf "Options:\n" \
  823.     eprintf "$optfmt" "PATTERN" "Awk(1) regular expression pattern to search logfile for." \
  824.     eprintf "$optfmt" "-d1,2,3" "Comma-separated list of field numbers to display as the" \
  825.     eprintf "$optfmt" "" "date/time when providing summary output (ignored if \`-v'\''" \
  826.     eprintf "$optfmt" "" "is passed). Default is 1,2,3. A value of \`NF'\'' prints the" \
  827.     eprintf "$optfmt" "" "entire line." \
  828.     eprintf "$optfmt" "-h" "Print this usage statement and exit." \
  829.     eprintf "$optfmt" "-n" "Only consider non-matching lines." \
  830.     eprintf "$optfmt" "-v" "Verbose. Show individual lines." \
  831.     return ${FAILURE:-1} \
  832. '
  833.  
  834. # zlog_wrapper $FUNCNAME $logfile [OPTIONS] [$pattern]
  835. #
  836. # A wrapper for the zlog/zlog_summary functions.
  837. #
  838. # NB: Requires zlog() zlog_summary() zlog_usage() -- from this file
  839. #
  840. quietly unalias zlog_wrapper
  841. shfunction zlog_wrapper \
  842.     '__eprintf=$shfunc_eprintf:q' \
  843.     '__fprintf=$shfunc_fprintf:q' \
  844.     '__zlog=$shfunc_zlog:q' \
  845.     '__zlog_summary=$shfunc_zlog_summary:q' \
  846.     '__zlog_usage=$shfunc_zlog_usage:q' \
  847. ' \
  848.     eval "$__eprintf"                                               \
  849.     eval "$__fprintf"                                               \
  850.     eval "$__zlog"                                                  \
  851.     eval "$__zlog_summary"                                          \
  852.     eval "$__zlog_usage"                                            \
  853.                                                                     \
  854.     local func="${1:-$FUNCNAME}"                                    \
  855.     local logfile="$2"                                              \
  856.     local datetime=                                                 \
  857.     local verbose=                                                  \
  858.     local nomatch=                                                  \
  859.                                                                     \
  860.     shift 2 # func/logfile                                          \
  861.                                                                     \
  862.     [ -t 1 ] || verbose=1 # stdout is a pipe                        \
  863.                                                                     \
  864.     local OPTIND flag                                               \
  865.     while getopts d:hnsv flag; do                                   \
  866.         case "$flag" in                                         \
  867.         d) datetime="$OPTARG" ;;                                \
  868.         n) nomatch=1 ;;                                         \
  869.         s) verbose= ;;                                          \
  870.         v) verbose=1 ;;                                         \
  871.         *) zlog_usage "$func"                                   \
  872.            return ${FAILURE:-1}                                 \
  873.         esac                                                    \
  874.     done                                                            \
  875.     shift $(( $OPTIND - 1 ))                                        \
  876.                                                                     \
  877.     set -- ${nomatch:+-n} "$@"                                      \
  878.     cmd=zlog                                                        \
  879.     if [ ! "$verbose" ]; then                                       \
  880.         cmd=zlog_summary                                        \
  881.         set -- ${datetime:+-d "$datetime"} "$@"                 \
  882.     fi                                                              \
  883.     $cmd "$logfile" "$@"                                            \
  884. '
  885.  
  886. # ssh-agent-dup [-aqn]
  887. #
  888. # Connect to an open/active ssh-agent session available to the currently
  889. # authenticated user. If more than one ssh-agent is available and the `-n' flag
  890. # is not given, provide a menu list of open/active sessions available. Allows
  891. # the user to quickly duplicate access to an ssh-agent launched in another
  892. # interactive session on the same machine or for switching between agents.
  893. #
  894. # This allows you to, for example:
  895. #
  896. #   (in shell session A)
  897. #   ssh-agent
  898. #   (in shell session B)
  899. #   ssh-agent-dup
  900. #   (now both sessions A and B can use the same agent)
  901. #
  902. # No menu is presented if only a single agent session is available (the open
  903. # session is duplicated for the active shell session). If more than one agent
  904. # is available, a menu is presented. The menu choice becomes the active agent.
  905. #
  906. # If `-a' is present, list all readable agent sockets, not just those owned by
  907. # the currently logged-in user.
  908. #
  909. # If `-q' is present, do not list agent nor keys.
  910. #
  911. # If `-n' is present, run non-interactively (good for scripts; pedantic).
  912. #
  913. # NB: Requires cexport() dialog_menutag() dialog_menutag2help() have()
  914. #     quietly() -- from this file
  915. # NB: Requires $DIALOG_TMPDIR $DIALOG_MENU_TAGS -- from this file
  916. # NB: Requires awk(1) cat(1) grep(1) id(1) ls(1) ps(1) ssh-add(1) stat(1)
  917. #     -- from base system
  918. #
  919. shfunction cexport '                                                         \
  920.     local item key value                                                 \
  921.     while [ $# -gt 0 ]; do                                               \
  922.         item="$1"                                                    \
  923.         key="${item%%=*}"                                            \
  924.         value="${item#"$key"=}"                                      \
  925.         if [ "$interactive" ]; then                                  \
  926.             echo "setenv $key $value" >&2                        \
  927.         fi                                                           \
  928.         echo "setenv $key $value"                                    \
  929.         export "$item"                                               \
  930.         shift 1 # item                                               \
  931.     done                                                                 \
  932. '
  933. quietly unalias cexport # sh only
  934. quietly unalias ssh-agent-dup
  935. eshfunction ssh-agent-dup \
  936.     'DIALOG_MENU_TAGS=$DIALOG_MENU_TAGS:q' \
  937.     'DIALOG_TMPDIR=$DIALOG_TMPDIR:q' \
  938.     '__cexport=$shfunc_cexport:q' \
  939.     '__dialog_menutag=$shfunc_dialog_menutag:q' \
  940.     '__dialog_menutag2help=$shfunc_dialog_menutag2help:q' \
  941.     '__have=$shfunc_have:q' \
  942.     '__quietly=$shfunc_quietly:q' \
  943. ' \
  944.     eval "$__cexport"                                                    \
  945.     eval "$__dialog_menutag"                                             \
  946.     eval "$__dialog_menutag2help"                                        \
  947.     eval "$__have"                                                       \
  948.     eval "$__quietly"                                                    \
  949.                                                                          \
  950.     local nsockets=0                                                     \
  951.     local t=1s # ssh-add(1) timeout                                      \
  952.     local list_all= quiet= interactive=1 noninteractive=                 \
  953.     local sockets=                                                       \
  954.     local ucomm owner socket stat pid current_user                       \
  955.                                                                          \
  956.     local OPTIND=1 OPTARG flag                                           \
  957.     while getopts anq flag; do                                           \
  958.         case "$flag" in                                              \
  959.         a) list_all=1 ;;                                             \
  960.         n) noninteractive=1 interactive= ;;                          \
  961.         q) quiet=1 ;;                                                \
  962.         \?|*)                                                        \
  963.             [ "$noninteractive" ] ||                             \
  964.                 echo "$ALIASNAME [-aq]" | ${LOLCAT:-cat} >&2 \
  965.             return ${FAILURE:-1}                                 \
  966.         esac                                                         \
  967.     done                                                                 \
  968.     shift $(( $OPTIND - 1 ))                                             \
  969.                                                                          \
  970.     case "$UNAME_s" in                                                   \
  971.     *BSD) stat="-f%Su" ;;                                                \
  972.     *) stat="-c%U"                                                       \
  973.     esac                                                                 \
  974.                                                                          \
  975.     current_user=$( id -nu )                                             \
  976.     for socket in /tmp/ssh-*/agent.[0-9]*; do                            \
  977.         # Must exist as a socket                                     \
  978.         [ -S "$socket" ] || continue                                 \
  979.                                                                      \
  980.         if have lsof; then                                           \
  981.             pid=$( lsof -t -- "$socket" 2> /dev/null ) ||        \
  982.                 continue                                     \
  983.         else                                                         \
  984.             # Must end in numbers-only (after trailing dot)      \
  985.             pid="${socket##*.}"                                  \
  986.             [ "$pid" -a "$pid" = "${pid#*[\!0-9]}" ] ||          \
  987.                 continue                                     \
  988.                                                                      \
  989.             for pid in $(( $pid + 1 )) $pid $(( $pid + 2 )) ""   \
  990.             do                                                   \
  991.                 [ "$pid" ] || break                          \
  992.                 # Potential PIDs based on socket num         \
  993.                 # Must be running pid and ssh[d]/agent       \
  994.                 ucomm=$( ps -p $pid -o ucomm= 2> /dev/null ) \
  995.                 case "$ucomm" in                             \
  996.                 ssh-agent|ssh|sshd) break ;;                 \
  997.                 esac                                         \
  998.                 pid=                                         \
  999.             done                                                 \
  1000.             [ "$pid" ] || continue                               \
  1001.         fi                                                           \
  1002.                                                                      \
  1003.         # Must be owned by the current user unless -a is used        \
  1004.         # NB: When -a is used, the socket still has to be readable   \
  1005.         if [ ! "$list_all" ]; then                                   \
  1006.             owner=$( stat $stat "$socket" 2> /dev/null ) ||      \
  1007.                 continue                                     \
  1008.             [ "$owner" = "$current_user" ] || continue           \
  1009.         fi                                                           \
  1010.                                                                      \
  1011.         sockets="$sockets $socket"                                   \
  1012.         nsockets=$(( $nsockets + 1 ))                                \
  1013.         export socketpid$nsockets=$pid                               \
  1014.     done                                                                 \
  1015.     sockets="${sockets# }"                                               \
  1016.                                                                          \
  1017.     if [ $nsockets -eq 0 ]; then                                         \
  1018.         if [ ! "$noninteractive" ]; then                             \
  1019.             local msg="$ALIASNAME: No agent sockets available"   \
  1020.             echo "$msg" | ${LOLCAT:-cat} >&2                     \
  1021.         fi                                                           \
  1022.         return ${FAILURE:-1}                                         \
  1023.     fi                                                                   \
  1024.     if [ $nsockets -eq 1 ]; then                                         \
  1025.         cexport SSH_AUTH_SOCK="$sockets"                             \
  1026.         [ "pid" ] && cexport SSH_AGENT_PID=$pid                      \
  1027.         [ "$SSH_AGENT_PID" -a ! "$quiet" ] && # show process         \
  1028.             [ "$interactive" ] &&                                \
  1029.             ps -p "$SSH_AGENT_PID" | ${LOLCAT:-cat} >&2          \
  1030.         # dump fingerprints from newly configured agent              \
  1031.         if ! [ "$quiet" -o "$noninteractive" ]; then                 \
  1032.             [ "$pid" ] && echo " NB: Use "                   \\\\\
  1033.                 "\`ssh-agent -k'\'' to kill this agent"      \
  1034.             if have timeout; then                                \
  1035.                 timeout $t ssh-add -l                        \
  1036.             else                                                 \
  1037.                 ssh-add -l                                   \
  1038.             fi                                                   \
  1039.         fi | ${LOLCAT:-cat} >&2                                      \
  1040.         return ${SUCCESS:-0}                                         \
  1041.     fi                                                                   \
  1042.                                                                          \
  1043.     # There is more than one agent available                             \
  1044.     [ "$noninteractive" ] && return ${FAILURE:-1}                        \
  1045.                                                                          \
  1046.     #                                                                    \
  1047.     # If we do not have dialog(1), just print the possible values        \
  1048.     #                                                                    \
  1049.     if ! have dialog; then                                               \
  1050.         local prefix="%3s"                                           \
  1051.         local fmt="$prefix %5s %-20s %s\n"                           \
  1052.         local num=0 choice                                           \
  1053.         local identities nloaded                                     \
  1054.                                                                      \
  1055.         sockets=$( command ls -tr $sockets ) # asc order by age      \
  1056.         printf "$fmt" "" PID USER+NKEYS COMMAND >&2                  \
  1057.         for socket in $sockets; do                                   \
  1058.             num=$(( $num + 1 ))                                  \
  1059.             eval pid=\$socketpid$num                             \
  1060.             nkeys=0                                              \
  1061.             identities=$(                                        \
  1062.                 unset interactive                            \
  1063.                 export SSH_AUTH_SOCK="$socket"               \
  1064.                 if have timeout; then                        \
  1065.                     timeout $t ssh-add -l                \
  1066.                 else                                         \
  1067.                     ssh-add -l                           \
  1068.                 fi                                           \
  1069.             ) && nkeys=$( echo "$identities" | grep -c . )       \
  1070.             printf "$fmt" $num: "$pid"                       \\\\\
  1071.                 "$( ps -p $pid -o user= )"+"$nkeys"      \\\\\
  1072.                 "$( ps -p $pid -o command= )" |              \
  1073.                 ${LOLCAT:-cat} >&2                           \
  1074.         done                                                         \
  1075.         echo >&2                                                     \
  1076.         echo -n "Select a number [$num]: " | ${LOLCAT:-cat} >&2      \
  1077.         read choice                                                  \
  1078.         : ${choice:=$num}                                            \
  1079.         case "$choice" in                                            \
  1080.         ""|*[\!0-9]*)                                                \
  1081.             echo "$ALIASNAME: Invalid choice [$choice]" |        \
  1082.                 ${LOLCAT:-cat} >&2                           \
  1083.             return ${FAILURE:-1} ;;                              \
  1084.         esac                                                         \
  1085.         if [ $choice -gt $num -o $choice -lt 1 ]; then               \
  1086.             echo "$ALIASNAME: Choice out of range [$choice]" |   \
  1087.                 ${LOLCAT:-cat} >&2                           \
  1088.             return ${FAILURE:-1}                                 \
  1089.         fi                                                           \
  1090.         set -- $sockets                                              \
  1091.         eval socket=\"\${$choice}\"                                  \
  1092.                                                                      \
  1093.         cexport SSH_AUTH_SOCK="$socket"                              \
  1094.         eval pid=\$socketpid$choice                                  \
  1095.         [ "$pid" ] && cexport SSH_AGENT_PID=$pid                     \
  1096.     else                                                                 \
  1097.         local menu_list=                                             \
  1098.                                                                      \
  1099.         sockets=$( command ls -1t $sockets ) # desc order by age     \
  1100.         have timeout || t=                                           \
  1101.         menu_list=$( echo "$sockets" |                               \
  1102.             awk -v t="$t" -v tags="$DIALOG_MENU_TAGS" '\''       \
  1103.             {                                                    \
  1104.                 if (++tagn > length(tags)) exit              \
  1105.                 if (\!match($0, /[[:digit:]]+$/)) next       \
  1106.                 pid = ENVIRON["socketpid"tagn]               \
  1107.                 cmd = sprintf("ps -p %u -o user=", pid)      \
  1108.                 cmd | getline user                           \
  1109.                 close(cmd)                                   \
  1110.                 cmd = sprintf("ps -p %u -o command=", pid)   \
  1111.                 cmd | getline command                        \
  1112.                 close(cmd)                                   \
  1113.                 nloaded = 0                                  \
  1114.                 cmd = "SSH_AUTH_SOCK=" $0                    \
  1115.                 if (t != "") cmd = cmd " timeout " t         \
  1116.                 cmd = cmd " ssh-add -l"                      \
  1117.                 while (cmd | getline identity) {             \
  1118.                     nloaded += identity ~ /^[[:digit:]]/ \
  1119.                 }                                            \
  1120.                 close(cmd)                                   \
  1121.                 printf "'\''\'\''%s\'\''\ \'\''%s\'\''\ \'\''%s\'\'''\''\n", \
  1122.                     substr(tags, tagn, 1),               \
  1123.                     sprintf("pid %u %s+%u %s", pid,      \
  1124.                         user, nloaded, command),     \
  1125.                     sprintf("%s %s",                     \
  1126.                         "SSH_AUTH_SOCK=" $0,         \
  1127.                         "SSH_AGENT_PID=" pid)        \
  1128.             }'\''                                                \
  1129.         )                                                            \
  1130.                                                                      \
  1131.         local prompt="Pick an ssh-agent to duplicate (user+nkeys):"  \
  1132.         eval dialog                                              \\\\\
  1133.             --clear --title "'\''$ALIASNAME'\''" --item-help \\\\\
  1134.             --menu "'\''$prompt'\''" 17 55 9 $menu_list      \\\\\
  1135.             >&2 2> "$DIALOG_TMPDIR/dialog.menu.$$"               \
  1136.         local retval=$?                                              \
  1137.                                                                      \
  1138.         # Return if "Cancel" (-1) or ESC (255)                       \
  1139.         [ $retval -eq ${SUCCESS:-0} ] || return $retval              \
  1140.                                                                      \
  1141.         local tag="$( dialog_menutag )"                              \
  1142.         cexport $( eval dialog_menutag2help                      \\\\\
  1143.             "'\''$tag'\''" $menu_list )                          \
  1144.     fi                                                                   \
  1145.                                                                          \
  1146.     # Attempt to show the running agent                                  \
  1147.     [ "$SSH_AGENT_PID" -a ! "$quiet" ] &&                                \
  1148.         ps -p "$SSH_AGENT_PID" | ${LOLCAT:-cat} >&2                  \
  1149.                                                                          \
  1150.     # Attempt to dump fingerprints from newly configured agent           \
  1151.     if [ ! "$quiet" ]; then                                              \
  1152.         echo "# NB: Use \`$ALIASNAME'\'' to select different agent"  \
  1153.         echo "# NB: Use \`ssh-agent -k'\'' to kill this agent"       \
  1154.         if have timeout; then                                        \
  1155.             timeout $t ssh-add -l                                \
  1156.         else                                                         \
  1157.             ssh-add -l                                           \
  1158.         fi                                                           \
  1159.     fi | ${LOLCAT:-cat} >&2                                              \
  1160. '
  1161. alias sad ssh-agent-dup
  1162.  
  1163. # openkey [-hv]
  1164. #
  1165. # Mounts my F.o thumb
  1166. #
  1167. # NB: Requires eprintf() eval2() have() -- from this file
  1168. # NB: Requires awk(1) df(1) id(1) mount(8) -- from base system
  1169. #
  1170. quietly unalias openkey
  1171. shfunction openkey \
  1172.     '__fprintf=$shfunc_fprintf:q' \
  1173.     '__eprintf=$shfunc_eprintf:q' \
  1174.     '__eval2=$shfunc_eval2:q' \
  1175.     '__have=$shfunc_have:q' \
  1176. '                                                                            \
  1177.     eval "$__fprintf"                                                    \
  1178.     eval "$__eprintf"                                                    \
  1179.     eval "$__eval2"                                                      \
  1180.     eval "$__have"                                                       \
  1181.                                                                          \
  1182.     [ "$UNAME_s" = "FreeBSD" ] ||                                        \
  1183.         { echo "$FUNCNAME: FreeBSD only!" >&2; return 1; }           \
  1184.     local OPTIND=1 OPTARG flag verbose= sudo=                            \
  1185.     while getopts hv flag; do                                            \
  1186.         case "$flag" in                                              \
  1187.         v) verbose=1 ;;                                              \
  1188.         *) local optfmt="\t%-4s %s\n"                                \
  1189.            eprintf "Usage: $FUNCNAME [-hv]\n"                        \
  1190.            eprintf "OPTIONS:\n"                                      \
  1191.            eprintf "$optfmt" "-h"                                \\\\\
  1192.                    "Print this text to stderr and return."           \
  1193.            eprintf "$optfmt" "-v"                                \\\\\
  1194.                    "Print verbose debugging information."            \
  1195.            return ${FAILURE:-1}                                      \
  1196.         esac                                                         \
  1197.     done                                                                 \
  1198.     shift $(( $OPTIND - 1 ))                                             \
  1199.     if [ "$( id -u )" != "0" ]; then                                     \
  1200.         if have sr; then                                             \
  1201.             sudo=sr                                              \
  1202.         elif have sudo; then                                         \
  1203.             sudo=sudo                                            \
  1204.         fi || {                                                      \
  1205.             eprintf "$FUNCNAME: not enough privileges\n"         \
  1206.             return ${FAILURE:-1}                                 \
  1207.         }                                                            \
  1208.     fi                                                                   \
  1209.     df -l /mnt | awk '\''                                               \\
  1210.         $NF == "/mnt" { exit found++ } END { exit \!found }         \\
  1211.     '\'' || ${verbose:+eval2} $sudo mount /mnt || return                 \
  1212.     local nfail=3                                                        \
  1213.     while [ $nfail -gt 0 ]; do                                           \
  1214.         /mnt/mount.sh -d${verbose:+v} && break                       \
  1215.         nfail=$(( $nfail - 1 ))                                      \
  1216.     done                                                                 \
  1217.     [ "$verbose" ] && df -hT /mnt/* | ( awk '\''                        \\
  1218.         NR == 1 { print > "/dev/stderr"; next } 1                   \\
  1219.     '\'' | sort -u ) 2>&1                                                \
  1220.     return ${SUCCESS:-0}                                                 \
  1221. '
  1222.  
  1223. # closekey [-ehv]
  1224. #
  1225. # Unmounts my F.o thumb
  1226. #
  1227. # NB: Requires eprintf() have() -- from this file
  1228. # NB: Requires awk(1) camcontrol(8) df(1) id(1) umount(8) -- from base system
  1229. #
  1230. quietly unalias closekey
  1231. shfunction closekey \
  1232.     '__fprintf=$shfunc_fprintf:q' \
  1233.     '__eprintf=$shfunc_eprintf:q' \
  1234.     '__have=$shfunc_have:q' \
  1235. '                                                                            \
  1236.     eval "$__fprintf"                                                    \
  1237.     eval "$__eprintf"                                                    \
  1238.     eval "$__have"                                                       \
  1239.                                                                          \
  1240.     local OPTIND=1 OPTARG flag eject= verbose= sudo=                     \
  1241.     while getopts ehv flag; do                                           \
  1242.         case "$flag" in                                              \
  1243.         e) eject=1 ;;                                                \
  1244.         v) verbose=1 ;;                                              \
  1245.         *) local optfmt="\t%-4s %s\n"                                \
  1246.            eprintf "Usage: $FUNCNAME [-ehv]\n"                       \
  1247.            eprintf "OPTIONS:\n"                                      \
  1248.            eprintf "$optfmt" "-e"                                \\\\\
  1249.                    "Eject USB media (using "\`"camcontrol eject'\'')." \
  1250.            eprintf "$optfmt" "-h"                                \\\\\
  1251.                    "Print this text to stderr and return."           \
  1252.            eprintf "$optfmt" "-v"                                \\\\\
  1253.                    "Print verbose debugging information."            \
  1254.            return ${FAILURE:-1}                                      \
  1255.         esac                                                         \
  1256.     done                                                                 \
  1257.     shift $(( $OPTIND - 1 ))                                             \
  1258.     if [ "$( id -u )" != "0" ]; then                                     \
  1259.         if have sr; then                                             \
  1260.             sudo=sr                                              \
  1261.         elif have sudo; then                                         \
  1262.             sudo=sudo                                            \
  1263.         fi || {                                                      \
  1264.             eprintf "$FUNCNAME: not enough privileges\n"         \
  1265.             return ${FAILURE:-1}                                 \
  1266.         }                                                            \
  1267.     fi                                                                   \
  1268.     [ ! -f "/mnt/umount.sh" ] ||                                         \
  1269.         ${verbose:+eval2} /mnt/umount.sh ${verbose:+-v} || return    \
  1270.     [ ! "$eject" ] || daN=$( df -l /mnt | awk '\''                      \\
  1271.         $NF == "/mnt" && match($0, "^/dev/[[:alpha:]]+[[:digit:]]+") { \\
  1272.             print substr($0, 6, RLENGTH - 5)                    \\
  1273.             exit found++                                        \\
  1274.         } END { exit ! found }                                      \\
  1275.     '\'' ) || daN=$(                                                     \
  1276.         [ "$sudo" -a "$verbose" ] &&                                 \
  1277.             echo $sudo camcontrol devlist >&2                    \
  1278.         $sudo camcontrol devlist | awk '\''                         \\
  1279.         BEGIN {                                                     \\
  1280.             camfmt = "^<%s>[[:space:]]+[^(]*"                   \\
  1281.                                                                         \\
  1282.             disk[nfind = 0] = "da[[:digit:]]+"                  \\
  1283.             find[nfind++] = "USB Flash Disk 1100"               \\
  1284.                                                                         \\
  1285.             #disk[nfind] = "device_pattern"                     \\
  1286.             #find[nfind++] = "model_pattern"                    \\
  1287.         }                                                           \\
  1288.         found = 0                                                   \\
  1289.         {                                                           \\
  1290.             for (n = 0; n < nfind; n++)                         \\
  1291.             {                                                   \\
  1292.                 if (\!match($0, sprintf(camfmt, find[n])))  \\
  1293.                     continue                            \\
  1294.                 devicestr = substr($0, RSTART + RLENGTH + 1) \\
  1295.                 gsub(/\).*/, "", devicestr)                 \\
  1296.                 ndevs = split(devicestr, devices, /,/)      \\
  1297.                 for (d = 1; d <= ndevs; d++) {              \\
  1298.                     if (devices[d] !~ "^" disk[n] "$")  \\
  1299.                         continue                    \\
  1300.                     found = 1                           \\
  1301.                     break                               \\
  1302.                 }                                           \\
  1303.                 if (found) break                            \\
  1304.             }                                                   \\
  1305.         }                                                           \\
  1306.         found && $0 = devices[d] { print; exit }                    \\
  1307.         END { exit \!found }                                        \\
  1308.     '\'' ) || return                                                     \
  1309.     [ ! -f "/mnt/umount.sh" ] ||                                         \
  1310.         ${verbose:+eval2} /mnt/umount.sh ${verbose:+-v} || return    \
  1311.     ! df -l /mnt |                                                       \
  1312.         awk '\''$NF=="/mnt"{exit found++}END{exit \!found}'\'' ||    \
  1313.         ${verbose:+eval2} $sudo umount /mnt || return                \
  1314.     [ "$eject" -a "$daN" ] &&                                            \
  1315.         ${verbose:+eval2} $sudo camcontrol eject "$daN"              \
  1316.     return ${SUCCESS:-0}                                                 \
  1317. '
  1318.  
  1319. # loadkeys [OPTIONS] [key ...]
  1320. #
  1321. # Load my SSH private keys from my F.o thumb. The `key' argument is to the
  1322. # SSH private keyfile's suffix; in example, "sf" for "id_rsa.sf" or "f.o" for
  1323. # "id_rsa.f.o" or "gh" for "id_rsa.gh".
  1324. #
  1325. # For example, to load the Sourceforge.net key, F.o key, and Github key:
  1326. #   loadkeys sf f.o gh
  1327. #
  1328. # OPTIONS:
  1329. #   -c           Close USB media after loading keys.
  1330. #   -e           Close and eject USB media after loading keys.
  1331. #   -h           Print this text to stderr and return.
  1332. #   -k           Kill running ssh-agent(1) and launch new one.
  1333. #   -n           Start a new ssh-agent, ignoring current one.
  1334. #   -t timeout   Timeout. Only used if starting ssh-agent(1).
  1335. #   -v           Print verbose debugging information.
  1336. #
  1337. # NB: Requires closekey() colorize() eprintf() openkey() ssh-agent() quietly()
  1338. #     ssh-agent-dup() -- from this file
  1339. # NB: Requires awk(1) kill(1) ps(1) ssh-add(1) -- from base system
  1340. #
  1341. quietly unalias loadkeys
  1342. shfunction ssh_agent '                                                       \
  1343.     [ $# -gt 0 ] || set -- -t 1800                                       \
  1344.     eval "$( /usr/bin/ssh-agent -s "$@" 2>&1 | grep -v Agent )"          \
  1345.     local retval=$?                                                      \
  1346.     cexport SSH_AUTH_SOCK=$SSH_AUTH_SOCK SSH_AGENT_PID=$SSH_AGENT_PID    \
  1347.     echo Agent pid $SSH_AGENT_PID >&2                                    \
  1348.     return $retval                                                       \
  1349. '
  1350. unalias ssh_agent # [e]shfunction use only
  1351. eshfunction loadkeys \
  1352.     '__cexport=$shfunc_cexport:q' \
  1353.     '__fprintf=$shfunc_fprintf:q' \
  1354.     '__eprintf=$shfunc_eprintf:q' \
  1355.     '__have=$shfunc_have:q' \
  1356.     '__openkey=$shfunc_openkey:q' \
  1357.     '__quietly=$shfunc_quietly:q' \
  1358.     '__colorize=$shfunc_colorize:q' \
  1359.     '__closekey=$shfunc_closekey:q' \
  1360.     '__ssh_agent=$shfunc_ssh_agent:q' \
  1361.     '__ssh_agent_dup=$shfunc_ssh_agent_dup:q' \
  1362. '                                                                            \
  1363.     eval "$__cexport"                                                    \
  1364.     eval "$__fprintf"                                                    \
  1365.     eval "$__eprintf"                                                    \
  1366.     eval "$__have"                                                       \
  1367.     eval "$__openkey"                                                    \
  1368.     eval "$__quietly"                                                    \
  1369.     eval "$__colorize"                                                   \
  1370.     eval "$__closekey"                                                   \
  1371.     eval "$__ssh_agent"                                                  \
  1372.     eval "$__ssh_agent_dup"                                              \
  1373.                                                                          \
  1374.     local OPTIND=1 OPTARG flag                                           \
  1375.     local close= eject= kill= new= pid= timeout= verbose=                \
  1376.     while getopts cehknt:v flag; do                                      \
  1377.         case "$flag" in                                              \
  1378.         c) close=1 ;;                                                \
  1379.         e) close=1 eject=1 ;;                                        \
  1380.         k) kill=1 ;;                                                 \
  1381.         n) new=1 ;;                                                  \
  1382.         v) verbose=1 ;;                                              \
  1383.         t) timeout="$OPTARG" ;;                                      \
  1384.         *) local optfmt="\t%-12s %s\n"                               \
  1385.            eprintf "Usage: $FUNCNAME [OPTIONS] [key ...]\n"          \
  1386.            eprintf "OPTIONS:\n"                                      \
  1387.            eprintf "$optfmt" "-c"                                \\\\\
  1388.                    "Close USB media after loading keys."             \
  1389.            eprintf "$optfmt" "-e"                                \\\\\
  1390.                    "Close and eject USB media after loading keys."   \
  1391.            eprintf "$optfmt" "-h"                                \\\\\
  1392.                    "Print this text to stderr and return."           \
  1393.            eprintf "$optfmt" "-k"                                \\\\\
  1394.                    "Kill running ssh-agent(1) and launch new one."   \
  1395.            eprintf "$optfmt" "-n"                                \\\\\
  1396.                    "Start a new ssh-agent, ignoring current one."    \
  1397.            eprintf "$optfmt" "-t timeout"                        \\\\\
  1398.                    "Timeout. Only used if starting ssh-agent(1)."    \
  1399.            eprintf "$optfmt" "-v"                                \\\\\
  1400.                    "Print verbose debugging information."            \
  1401.            return ${FAILURE:-1}                                      \
  1402.         esac                                                         \
  1403.     done                                                                 \
  1404.     shift $(( $OPTIND - 1 ))                                             \
  1405.     if [ "$kill" ]; then                                                 \
  1406.         quietly ssh-agent -k ||:                                     \
  1407.         unset SSH_AUTH_SOCK SSH_AGENT_PID                            \
  1408.         echo unsetenv SSH_AUTH_SOCK SSH_AGENT_PID                    \
  1409.     fi                                                                   \
  1410.     if [ "$new" ]; then                                                  \
  1411.         ssh_agent ${timeout:+-t"$timeout"} ||                        \
  1412.             return ${FAILURE:-1}                                 \
  1413.     elif quietly kill -0 "$SSH_AGENT_PID"; then                          \
  1414.         : already running                                            \
  1415.     elif [ "$SSH_AUTH_SOCK" ] && quietly ssh-add -l; then                \
  1416.         if have lsof; then                                           \
  1417.             cexport SSH_AGENT_PID=$(                             \
  1418.                 lsof -t -- "$SSH_AUTH_SOCK"                  \
  1419.             )                                                    \
  1420.         else                                                         \
  1421.             case "$SSH_AUTH_SOCK" in                             \
  1422.             */agent.[0-9]*)                                      \
  1423.                 pid="${SSH_AUTH_SOCK##*/agent.}"             \
  1424.                 pid="${pid%%[\!0-9]*}"                       \
  1425.                 cexport SSH_AGENT_PID=$pid                   \
  1426.                 ;;                                           \
  1427.             esac                                                 \
  1428.         fi                                                           \
  1429.     else                                                                 \
  1430.         if ! ssh_agent_dup -q; then                                  \
  1431.             ssh_agent ${timeout:+-t"$timeout"} ||                \
  1432.                 return ${FAILURE:-1}                         \
  1433.         fi                                                           \
  1434.     fi                                                                   \
  1435.     ps -p "$SSH_AGENT_PID" >&2 || return ${FAILURE:-1}                   \
  1436.     local suffix file show= load_required=                               \
  1437.     [ $# -eq 0 ] && load_required=1                                      \
  1438.     for suffix in "$@"; do                                               \
  1439.         file="/mnt/keys/id_rsa.$suffix"                              \
  1440.         ssh-add -l | awk -v file="$file" '\''                       \\
  1441.             gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, "") && \\
  1442.                 $0 == file { exit found++ }                 \\
  1443.             END { exit \!found }                                \\
  1444.         '\'' && show="$show${show:+|}$suffix" &&                     \
  1445.                     continue # already loaded                            \
  1446.         load_required=1                                              \
  1447.         break                                                        \
  1448.     done                                                                 \
  1449.     ssh-add -l | colorize -c 36                                      \\\\\
  1450.         "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)" >&2              \
  1451.     [ "$load_required" ] || return ${SUCCESS:-0}                         \
  1452.     openkey ${verbose:+-v} >&2 || return ${FAILURE:-1}                   \
  1453.     [ "$verbose" ] && ssh-add -l >&2                                     \
  1454.     local loaded_new=                                                    \
  1455.     if [ $# -gt 0 ]; then                                                \
  1456.         for suffix in "$@"; do                                       \
  1457.             file="/mnt/keys/id_rsa.$suffix"                      \
  1458.             [ -f "$file" ] || continue                           \
  1459.             ssh-add -l | awk -v file="$file" '\''               \\
  1460.                 gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, \\
  1461.                     "") && $0 == file { exit found++ }  \\
  1462.                 END { exit \!found }                        \\
  1463.             '\'' && continue                                     \
  1464.             ssh-add "$file" >&2 || continue                      \
  1465.             loaded_new=1                                         \
  1466.             show="$show${show:+|}$suffix"                        \
  1467.         done                                                         \
  1468.     else                                                                 \
  1469.         for file in /mnt/keys/id_rsa.*; do                           \
  1470.             [ -e "$file" ] || continue                           \
  1471.             [ "$file" != "${file%.[Pp][Uu][Bb]}" ] && continue   \
  1472.             ssh-add -l | awk -v file="$file" '\''               \\
  1473.                 gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, \\
  1474.                     "") && $0 == file { exit found++ }  \\
  1475.                 END { exit \!found }                         \
  1476.             '\'' && continue                                     \
  1477.             ssh-add "$file" >&2 || continue                      \
  1478.             loaded_new=1                                         \
  1479.             show="$show${show:+|}${file#/mnt/keys/id_rsa.}"      \
  1480.         done                                                         \
  1481.     fi                                                                   \
  1482.     [ "$close" ] && closekey ${verbose:+-v} ${eject:+-e} >&2             \
  1483.     [ "$loaded_new" ] && ssh-add -l | colorize -c 36                 \\\\\
  1484.         "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)" >&2              \
  1485. '
  1486.  
  1487. # unloadkeys [OPTIONS] [key ...]
  1488. #
  1489. # Unload my SSH private keys from my F.o thumb. The `key' argument is to the
  1490. # SSH private keyfile's suffix; in example, "sf" for "id_rsa.sf" or "f.o" for
  1491. # "id_rsa.f.o" or "gh" for "id_rsa.gh".
  1492. #
  1493. # For example, to unload the Sourceforge.net key, F.o key, and Github key:
  1494. #   unloadkeys sf f.o gh
  1495. #
  1496. # OPTIONS:
  1497. #   -a           Unload all keys.
  1498. #   -c           Close USB media after unloading keys.
  1499. #   -e           Close and eject USB media after unloading keys.
  1500. #   -h           Print this text to stderr and return.
  1501. #   -v           Print verbose debugging information.
  1502. #
  1503. # NB: Requires closekey() colorize() eprintf() openkey() quietly()
  1504. #     -- from this file
  1505. # NB: Requires awk(1) ps(1) ssh-add(1) -- from base system
  1506. #
  1507. quietly unalias unloadkeys
  1508. shfunction unloadkeys \
  1509.     '__fprintf=$shfunc_fprintf:q' \
  1510.     '__eprintf=$shfunc_eprintf:q' \
  1511.     '__eval2=$shfunc_eval2:q' \
  1512.     '__have=$shfunc_have:q' \
  1513.     '__openkey=$shfunc_openkey:q' \
  1514.     '__quietly=$shfunc_quietly:q' \
  1515.     '__colorize=$shfunc_colorize:q' \
  1516.     '__closekey=$shfunc_closekey:q' \
  1517. '                                                                            \
  1518.     eval "$__fprintf"                                                    \
  1519.     eval "$__eprintf"                                                    \
  1520.     eval "$__eval2"                                                      \
  1521.     eval "$__have"                                                       \
  1522.     eval "$__openkey"                                                    \
  1523.     eval "$__quietly"                                                    \
  1524.     eval "$__colorize"                                                   \
  1525.     eval "$__closekey"                                                   \
  1526.                                                                          \
  1527.     local OPTIND=1 OPTARG flag all= close= eject= verbose=               \
  1528.     while getopts acehv flag; do                                         \
  1529.         case "$flag" in                                              \
  1530.         a) all=1 ;;                                                  \
  1531.         c) close=1 ;;                                                \
  1532.         e) close=1 eject=1 ;;                                        \
  1533.         v) verbose=1 ;;                                              \
  1534.         *) local optfmt="\t%-12s %s\n"                               \
  1535.            eprintf "Usage: $FUNCNAME [OPTIONS] [key ...]\n"          \
  1536.            eprintf "OPTIONS:\n"                                      \
  1537.            eprintf "$optfmt" "-a" "Unload all keys."                 \
  1538.            eprintf "$optfmt" "-c"                                \\\\\
  1539.                    "Close USB media after loading keys."             \
  1540.            eprintf "$optfmt" "-e"                                \\\\\
  1541.                    "Close and eject USB media after loading keys."   \
  1542.            eprintf "$optfmt" "-h"                                \\\\\
  1543.                    "Print this text to stderr and return."           \
  1544.            eprintf "$optfmt" "-v"                                \\\\\
  1545.                    "Print verbose debugging information."            \
  1546.            return ${FAILURE:-1}                                      \
  1547.         esac                                                         \
  1548.     done                                                                 \
  1549.     shift $(( $OPTIND - 1 ))                                             \
  1550.     local suffix file show= unload_required=                             \
  1551.     if [ "$all" ]; then                                                  \
  1552.         unload_required=1                                            \
  1553.         shift $#                                                     \
  1554.     fi                                                                   \
  1555.     for suffix in "$@"; do                                               \
  1556.         file="/mnt/keys/id_rsa.$suffix"                              \
  1557.         ssh-add -l | awk -v file="$file" '\''                       \\
  1558.             gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, "") && \\
  1559.                 $0 == file { exit found++ }                 \\
  1560.             END { exit \!found }                                \\
  1561.         '\'' || continue # not loaded                                \
  1562.         show="$show${show:+|}$suffix"                                \
  1563.         unload_required=1                                            \
  1564.         break                                                        \
  1565.     done                                                                 \
  1566.     ssh-add -l |                                                         \
  1567.         colorize -c 31 "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)"   \
  1568.     [ "$unload_required" ] || return ${SUCCESS:-0}                       \
  1569.     openkey ${verbose:+-v} || return ${FAILURE:-1}                       \
  1570.     [ "$verbose" ] && ssh-add -l                                         \
  1571.     if [ "$all" ]; then                                                  \
  1572.         ssh-add -D                                                   \
  1573.     else                                                                 \
  1574.         for suffix in "$@"; do                                       \
  1575.             file="/mnt/keys/id_rsa.$suffix"                      \
  1576.             [ -f "$file" ] || continue                           \
  1577.             ssh-add -l | awk -v file="$file" '\''               \\
  1578.                 gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, \\
  1579.                     "") && $0 == file { exit found++ }  \\
  1580.                 END { exit \!found }                        \\
  1581.             '\'' || continue                                     \
  1582.             ssh-add -d "$file"                                   \
  1583.         done                                                         \
  1584.     fi                                                                   \
  1585.     [ "$close" ] && closekey ${verbose:+-v} ${eject:+-e}                 \
  1586.     [ "$all" ] || ssh-add -l |                                           \
  1587.         colorize -c 36 "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)"   \
  1588. '
  1589.  
  1590. # dialog_menutag
  1591. #
  1592. # Obtain the menutag chosen by the user from the most recently displayed
  1593. # dialog(1) menu and clean up any temporary files.
  1594. #
  1595. # NB: Requires quietly() -- from this file
  1596. # NB: Requires $DIALOG_TMPDIR -- from this file
  1597. # NB: Requires rm(1) -- from base system
  1598. #
  1599. quietly unalias dialog_menutag
  1600. shfunction dialog_menutag \
  1601.     '__quietly=$shfunc_quietly:q' \
  1602. '                                                                            \
  1603.     eval "$__quietly"                                                    \
  1604.                                                                          \
  1605.     local tmpfile="$DIALOG_TMPDIR/dialog.menu.$$"                        \
  1606.                                                                             \
  1607.     [ -f "$tmpfile" ] || return ${FAILURE:-1}                            \
  1608.                                                                             \
  1609.     cat "$tmpfile" 2> /dev/null                                          \
  1610.     quietly rm -f "$tmpfile"                                             \
  1611.                                                                             \
  1612.     return ${SUCCESS:-0}                                                 \
  1613. '
  1614.  
  1615. # dialog_menutag2help $tag_chosen $tag1 $item1 $help1 \
  1616. #                                 $tag2 $item2 $help2
  1617. #
  1618. # To use the `--menu' option of dialog(1) with the `--item-help' option, you
  1619. # must pass an ordered list of tag/item/help triplets on the command-line. When
  1620. # the user selects a menu option the tag for that item is printed to stderr.
  1621. #
  1622. # This function allows you to dereference the tag chosen by the user back into
  1623. # the help associated with said tag (item is discarded/ignored).
  1624. #
  1625. # Pass the tag chosen by the user as the first argument, followed by the
  1626. # ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
  1627. # as was passed to dialog(1) for consistency).
  1628. #
  1629. # If the tag cannot be found, NULL is returned.
  1630. #
  1631. quietly unalias dialog_menutag2help
  1632. shfunction dialog_menutag2help '                                             \
  1633.     local tag="$1" tagn help                                             \
  1634.     shift 1 # tag                                                        \
  1635.                                                                             \
  1636.     while [ $# -gt 0 ]; do                                               \
  1637.         tagn="$1"                                                    \
  1638.         help="$3"                                                    \
  1639.         shift 3 # tagn/item/help                                     \
  1640.                                                                             \
  1641.         if [ "$tag" = "$tagn" ]; then                                \
  1642.             echo "$help"                                         \
  1643.             return ${SUCCESS:-0}                                 \
  1644.         fi                                                           \
  1645.     done                                                                 \
  1646.     return ${FAILURE:-1}                                                 \
  1647. '
  1648.  
  1649. # colorize [-c ANSI] [-e ANSI] pattern
  1650. #
  1651. # Colorize text matching pattern with ANSI sequence (default is `31;1' for red-
  1652. # bold). Non-matching lines are printed as-is.
  1653. #
  1654. # NB: Requires awk(1) -- from base system
  1655. #
  1656. quietly unalias colorize
  1657. shfunction colorize '                                                        \
  1658.     local OPTIND=1 OPTARG flag                                           \
  1659.     local ti=                                                            \
  1660.     local te=                                                            \
  1661.     while getopts c:e: flag; do                                          \
  1662.         case "$flag" in                                              \
  1663.         c) ti="$OPTARG" ;;                                           \
  1664.         e) te="$OPTARG" ;;                                           \
  1665.         esac                                                         \
  1666.     done                                                                 \
  1667.     shift $(( $OPTIND - 1 ))                                             \
  1668.                                                                             \
  1669.     awk -v ti="${ti:-31;1}" -v te="${te:-39;22}" -v pattern="$1" '\''    \
  1670.         gsub(pattern, "\033[" ti "m&\033[" te "m")||1                \
  1671.     '\'' # END-QUOTE                                                     \
  1672. '
  1673.  
  1674.  
  1675. ############################################################ SHELL BEHAVIOR
  1676.  
  1677. #
  1678. # Set prompt style
  1679. #
  1680. set prompt='%{\033[32;1m%}%n@%m %{\033[34;1m%}%c %#%{\033[0m%} '
  1681.  
  1682. #
  1683. # Set the window title for terminals such as "xterm", "rxvt", and others
  1684. #
  1685. if ( $interactive ) then
  1686.     settitle $USER@$HOST $HOME
  1687. endif
  1688.  
  1689. #
  1690. # Make new files group-writable by default
  1691. #
  1692. umask 002
  1693.  
  1694. ############################################################ MISCELLANEOUS
  1695.  
  1696. #
  1697. # cvs(1) settings
  1698. #
  1699. setenv CVS_RSH cvs_rsh
  1700. setenv CVSROOT cvs:/repos/projects
  1701.  
  1702. #
  1703. # BitchX(1) settings
  1704. #
  1705. setenv IRCNICK devinteske
  1706. setenv IRCSERVER frenode.net
  1707. setenv IRCNAME dteske
  1708.  
  1709. #
  1710. # jail_build(8) settings
  1711. #
  1712. setenv JAIL_BUILD_DESTDIR /raid1/jails
  1713.  
  1714. #
  1715. # sysrc(8) settings
  1716. #
  1717. setenv SYSRC_VERBOSE 1
  1718.  
  1719. #
  1720. # Override the default password prompt for sudo(8). This helps differentiate
  1721. # the sudo(8) password prompt from others such as su(1), ssh(1), and login(1).
  1722. #
  1723. setenv SUDO_PROMPT '[sudo] Password:'
  1724.  
  1725. #
  1726. # Manual pages with color
  1727. #
  1728. setenv LESS_TERMCAP_mb "${asc}31m"      # turn on blinking
  1729. setenv LESS_TERMCAP_md "${asc}31m"      # turn on bold (extra bright)
  1730. setenv LESS_TERMCAP_me "${asc}m"        # turn off all attributes
  1731. setenv LESS_TERMCAP_so "${asc}1;33;44m" # begin standout mode
  1732. setenv LESS_TERMCAP_se "${asc}m"        # exit standout mode
  1733. setenv LESS_TERMCAP_us "${asc}1;34m"    # begin underline mode
  1734. setenv LESS_TERMCAP_ue "${asc}m"        # exit underline mode
  1735.  
  1736. ############################################################ MAIN
  1737.  
  1738. #
  1739. # Display some wisdom
  1740. # NB: Uncomment to enable
  1741. #
  1742. #?if ( "$interactive" ) then
  1743. #?  fortune $KA_FORTUNE
  1744. #?endif
  1745.  
  1746. #
  1747. # Quietly attach to running ssh-agent(1) unless agent already given
  1748. #
  1749. if ( ! $?SSH_AUTH_SOCK ) then
  1750.     if ( "$interactive" ) then
  1751.         ssh-agent-dup
  1752.     else
  1753.         ssh-agent-dup -n || :
  1754.     endif
  1755. endif
  1756.  
  1757. ################################################################################
  1758. # END
  1759. ################################################################################
Add Comment
Please, Sign In to add comment