defs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. nl='
  2. '
  3. tab=' '
  4. if(~ $#distname 0)
  5. distname=plan9
  6. wctl=/dev/null
  7. if(test -w /dev/wctl)
  8. wctl=/dev/wctl
  9. fn log {
  10. echo $* >>/srv/log
  11. }
  12. fn logprog {
  13. echo '% '^$"* >>/srv/log
  14. $* >[2=1] >>/srv/log
  15. }
  16. fn sigint {
  17. # nothing happens
  18. }
  19. fn prompt {
  20. def=()
  21. what=()
  22. if(~ $1 -d && ! ~ $#* 1){
  23. def=$2
  24. shift
  25. shift
  26. }
  27. optstr=()
  28. if(~ $1 -w && ! ~ $#* 1){
  29. optstr=$2
  30. shift
  31. shift
  32. }
  33. pr=$1
  34. shift
  35. opts=($*)
  36. if(~ $#opts 0) {
  37. suf=' '
  38. }
  39. if not if(! ~ $#optstr 0) {
  40. if(~ $optstr '')
  41. suf=' '
  42. if not {
  43. pr=$pr^' ('^$"optstr^')'
  44. suf=''
  45. }
  46. }
  47. if not {
  48. pr=$pr^' ('^$1
  49. shift
  50. for(i)
  51. pr=$pr^', '^$i
  52. pr=$pr^')'
  53. suf=''
  54. }
  55. if(~ $#def 1)
  56. pr=$pr^$suf^'['^$def^']'
  57. if not
  58. pr=$pr^$suf^'[no default]'
  59. pr=$pr^': '
  60. okay=no
  61. while(~ $okay no) {
  62. echo -n current >$wctl
  63. echo -n top >$wctl
  64. echo -n $pr >[1=2]
  65. ifs='' {rd=`{read}}
  66. if(~ $#rd 0)
  67. exit notdone
  68. if(~ $rd !*){
  69. ifs='' {rd=`{echo $rd | sed 's/!//'}}
  70. echo $rd
  71. rc -c $rd
  72. echo !$status
  73. }
  74. if not{
  75. rd=`{echo $rd}
  76. if(~ $#rd 0 || ~ $rd '')
  77. rd=$def
  78. switch($#opts){
  79. case 0
  80. if(! ~ $rd '')
  81. okay=yes
  82. case *
  83. if(~ $rd $opts)
  84. okay=yes
  85. }
  86. }
  87. }
  88. echo -n $rd >/env/rd # just in case
  89. }
  90. fn desc {
  91. echo -n ' '^$1^' - '
  92. grep '^# desc: ' $1 | sed 's/# desc: //'
  93. }
  94. fn prereq {
  95. grep '^# prereq:' $1 | sed 's/# prereq://'
  96. }
  97. fn mustdo {
  98. echo You must `{grep '^# mustdo:' $1 | sed 's/# mustdo://'}
  99. }
  100. # there's no easy way to pass shell variables
  101. # up from children to parents; the parents have
  102. # to be coerced into noticing that the environment
  103. # changed, even when in the same environment group.
  104. #
  105. # instead, we explicitly export the variables we want
  106. # to percolate, and the parent calls coherence to reread
  107. # the variables.
  108. #
  109. # we just append to the vars file, so that later exports
  110. # override earlier ones; when we call coherence,
  111. # the duplicates are thrown out.
  112. fn export {
  113. null=()
  114. nonnull=()
  115. for(i in $*){
  116. if(~ $#$i 0)
  117. null=($null $i)
  118. if not
  119. nonnull=($nonnull $i)
  120. }
  121. if(! ~ $#nonnull 0)
  122. whatis $nonnull |grep -v '^\./' >>/tmp/vars >[2]/dev/null
  123. for(i in $null)
  124. echo $i^'=()' >>/tmp/vars
  125. }
  126. fn coherence {
  127. if(test -f /tmp/vars) {
  128. grep '^[a-z]*=' /tmp/vars >/tmp/vars2
  129. v=`{sed 's/^([a-z]*)=.*/\1/' /tmp/vars2 | sort -u}
  130. . /tmp/vars2
  131. rm /tmp/vars2
  132. rm /tmp/vars
  133. export $v
  134. }
  135. }
  136. # ip device stats
  137. fn isipdevup {
  138. grep -s $1 /net/ipifc/*/status >[2]/dev/null
  139. }