ctunattach.pl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. require "$tool/built/include/ctquery.pl" ;
  2. $shell_type = "csh" ;
  3. if ( $ENV{"SHELL_TYPE"} ne "" ) {
  4. if ( $ENV{"SHELL_TYPE"} eq "sh" ) {
  5. $shell_type = "sh" ;
  6. }
  7. }
  8. # remove a value from a variable. If it is the only thing remaining in the
  9. # variable, add it to the unset list.
  10. # input is in:
  11. # $_[0] = variable
  12. # $_[1] = value
  13. #
  14. # output is in:
  15. # %newenv = an image of how we want the environment to be
  16. # @unset = a list of variables to unset
  17. sub CTUnattachMod {
  18. &CTUDebug( "in CTUnattachMod\n" ) ;
  19. local( $done ) = 0 ;
  20. # if we didn't get any data, nothing really to do
  21. if ( $_[0] eq "" ) { $done = 1 ; }
  22. if ( $_[1] eq "" ) { $done = 1 ; }
  23. # if the variable is already set to be unset, nothing really to do
  24. if ( join( " ", @unset ) =~ /$_[0]/ ) { $done = 1 ; }
  25. # if the variable isn't in newenv, move it there, if it's empty mark it
  26. # for unsetting
  27. if ( $newenv{$_[0]} eq "" ) {
  28. $newenv{$_[0]} = &CTSpoolEnv( $_[0] ) ;
  29. if ( $newenv{$_[0]} eq "" ) {
  30. push( @unset, $_[0] ) ;
  31. delete $newenv{$_[0]} ;
  32. $done = 1 ;
  33. }
  34. }
  35. # if the value does not appear in the variable, nothing really to do
  36. if ( ! ( $newenv{$_[0]} =~ /$_[1]/ ) ) { $done = 1 ; }
  37. # now down to the real work
  38. if ( ! $done ) {
  39. # if the variable is exactly the value, mark it for unsetting
  40. if ( $newenv{$_[0]} eq $_[1] ) {
  41. push( @unset, $_[0] ) ;
  42. delete $newenv{$_[0]} ;
  43. } elsif ( $newenv{$_[0]} =~ / $_[1]/ ) {
  44. local( $tmp ) = $newenv{$_[0]} ;
  45. $tmp =~ s/ $_[1]// ;
  46. $newenv{$_[0]} = $tmp ;
  47. } elsif ( $newenv{$_[0]} =~ /$_[1] / ) {
  48. local( $tmp ) = $newenv{$_[0]} ;
  49. $tmp =~ s/$_[1] // ;
  50. $newenv{$_[0]} = $tmp ;
  51. } else {
  52. print STDERR "ERROR: variable '" . $_[0] . "' contains '" .
  53. $_[1] . "' (in '" . $newenv{$_[0]} .
  54. "'), but I am too stupid to figure out how to remove it.\n" ;
  55. }
  56. }
  57. }
  58. # given the project and flavor, build the lists of variables to set/modify
  59. # input is in:
  60. # $_[0] = project
  61. # $_[1] = flavor
  62. #
  63. # output is in:
  64. # return value is config line
  65. # %newenv = an image of what we want the environment to look like
  66. # @unset = list of variables to be unset
  67. # %envsep = seperator
  68. # %envcmd = set or setenv
  69. # %envpostpend = flag that variable should be postpended
  70. sub CTUnattachCompute {
  71. &CTUDebug( "in CTUnattachCompute\n" ) ;
  72. local( $flav ) = $_[1] ;
  73. local( $spec ) = &CTResolveSpec( $_[0], $flav ) ;
  74. local( $root ) = &CTComputeRoot( $_[0], $flav, $spec ) ;
  75. if ( $spec ne "" ) {
  76. local( $proj ) = $_[0] ;
  77. $proj =~ tr/a-z/A-Z/ ;
  78. local( $item ) ;
  79. # since we don't have to worry about sub-attaches, it doesn't matter
  80. # if we scan the .init file first or not. So we won't.
  81. &CTUDebug( "extending paths\n" ) ;
  82. $item = $root . "/built/bin" ;
  83. &CTUnattachMod( "PATH", $item ) ;
  84. $item = $root . "/built/lib" ;
  85. if ( $ENV{"PENV"} eq "WIN32" ) {
  86. &CTUnattachMod( "PATH", $item ) ;
  87. }
  88. &CTUnattachMod( "LD_LIBRARY_PATH", $item ) ;
  89. &CTUnattachMod( "DYLD_LIBRARY_PATH", $item ) ;
  90. #$item = $root . "/src/all" ;
  91. #&CTUnattachMod( "CDPATH", $item ) ;
  92. $item = $root . "/built/include" ;
  93. &CTUnattachMod( "CT_INCLUDE_PATH", $item ) ;
  94. $item = $root . "/built/etc" ;
  95. &CTUnattachMod( "ETC_PATH", $item ) ;
  96. $item = $proj . ":" . $flav ;
  97. &CTUnattachMod( "CTPROJS", $item ) ;
  98. push( @unset, $proj ) ;
  99. if ( -e "$root/built/etc/$_[0].init" ) {
  100. &CTUDebug( "scanning $_[0].init file\n" ) ;
  101. local( @linesplit ) ;
  102. local( $linetmp ) ;
  103. local( $loop );
  104. local( *INITFILE ) ;
  105. if ( -x "$root/built/etc/$_[0].init" ) {
  106. open( INITFILE, "$root/built/etc/$_[0].init $_[0] $_[1] $root |" ) ;
  107. } else {
  108. open( INITFILE, "< $root/built/etc/$_[0].init" ) ;
  109. }
  110. while ( <INITFILE> ) {
  111. s/\n$// ;
  112. @linesplit = split( /\#/ ) ;
  113. $_ = $linesplit[0] ;
  114. if ( $_ =~ /^MODABS/ ) {
  115. @linesplit = split ;
  116. $linetmp = $linesplit[1] ;
  117. shift( @linesplit ) ;
  118. shift( @linesplit ) ;
  119. foreach $loop ( @linesplit ) {
  120. &CTUnattachMod( $linetmp, $loop ) ;
  121. }
  122. } elsif ( $_ =~ /^MODREL/ ) {
  123. @linesplit = split ;
  124. $linetmp = $linesplit[1] ;
  125. shift( @linesplit ) ;
  126. shift( @linesplit ) ;
  127. foreach $loop ( @linesplit ) {
  128. &CTUnattachMod( $linetmp, $root . "/" . $loop ) ;
  129. }
  130. } elsif ( $_ =~ /^SETABS/ ) {
  131. @linesplit = split ;
  132. $linetmp = $linesplit[1] ;
  133. push( @unset, $linetmp ) ;
  134. } elsif ( $_ =~ /^SETREL/ ) {
  135. @linesplit = split ;
  136. $linetmp = $linesplit[1] ;
  137. push( @unset, $linetmp ) ;
  138. } elsif ( $_ =~ /^SEP/ ) {
  139. @linesplit = split ;
  140. $envsep{$linesplit[1]} = $linesplit[2] ;
  141. } elsif ( $_ =~ /^CMD/ ) {
  142. @linesplit = split ;
  143. $envcmd{$linesplit[1]} = $linesplit[2] ;
  144. } elsif ( $_ =~ /^DOCSH/ ) {
  145. &CTUDebug( "ignoring DO command in .init file\n" ) ;
  146. } elsif ( $_ =~ /^DOSH/ ) {
  147. &CTUDebug( "ignoring DO command in .init file\n" ) ;
  148. } elsif ( $_ =~ /^DO/ ) {
  149. &CTUDebug( "ignoring DO command in .init file\n" ) ;
  150. } elsif ( $_ =~ /^POSTPEND/ ) {
  151. @linesplit = split ;
  152. $envpospend{$linesplit[1]} = 1 ;
  153. } elsif ( $_ =~ /^ATTACH/ ) {
  154. &CTUDebug( "ignoring ATTACH command in .init file\n" ) ;
  155. } else {
  156. print STDERR "Unknown .init directive '$_'\n" ;
  157. }
  158. }
  159. close( INITFILE ) ;
  160. }
  161. }
  162. &CTUDebug( "out of CTUnattachCompute\n" ) ;
  163. $spec ;
  164. }
  165. # write a script to setup the environment
  166. # Input is:
  167. # $_[0] = filename
  168. sub CTUnattachWriteScript {
  169. &CTUDebug( "in CTAttachWriteScript\n" ) ;
  170. local( *OUTFILE ) ;
  171. open( OUTFILE, ">$_[0]" ) ;
  172. print OUTFILE "#!/bin/" . $shell_type . " -f\n" ;
  173. local( $item ) ;
  174. #local( $unsetcdpath ) = 0 ;
  175. #local( $modcdpath ) = 0 ;
  176. foreach $item ( @unset ) {
  177. #if ( $item eq "CDPATH" ) { $unsetcdpath = 1 ; }
  178. if ( $shell_type eq "sh" ) {
  179. print OUTFILE "$item=\n" ;
  180. if ( $envcmd{$item} ne "set" ) {
  181. print OUTFILE "export $item\n" ;
  182. }
  183. } else {
  184. if ( $envcmd{$item} ne "" ) {
  185. print OUTFILE "un" . $envcmd{$item} . " $item\n" ;
  186. } else {
  187. print OUTFILE "unsetenv $item\n" ;
  188. }
  189. }
  190. }
  191. foreach $item ( keys %newenv ) {
  192. #if ( $item eq "CDPATH" ) { $modcdpath = 1 ; }
  193. local( $sep ) = " " ;
  194. if ( $envsep{$item} ne "" ) {
  195. $sep = $envsep{$item} ;
  196. }
  197. local( @splitlist ) = split( / +/, $newenv{$item} ) ;
  198. local( $outval ) = join( $sep, @splitlist ) ;
  199. if ( $shell_type eq "sh" ) {
  200. print OUTFILE "$item=\"" . $outval . "\"\n" ;
  201. if ( $envcmd{$item} ne "set" ) {
  202. print OUTFILE "export $item\n" ;
  203. }
  204. } else {
  205. if ( $envcmd{$item} ne "" ) {
  206. PRINT OUTFILE $envcmd{$item} . " $item " ;
  207. if ( $envcmd{$item} eq "set" ) {
  208. print OUTFILE " = ( " ;
  209. }
  210. print OUTFILE $outval ;
  211. if ( $envcmd{$item} eq "set" ) {
  212. print OUTFILE ")" ;
  213. }
  214. print OUTFILE "\n" ;
  215. } else {
  216. print OUTFILE "setenv $item \"$outval\"\n" ;
  217. }
  218. }
  219. }
  220. #if ( $unsetcdpath ) {
  221. # if ( $shell_type ne "sh" ) {
  222. # print OUTFILE "unset cdpath\n" ;
  223. # }
  224. #} elsif ( $modcdpath ) {
  225. # if ( $shell_type ne "sh" ) {
  226. # print OUTFILE "set cdpath = ( \$" . "CDPATH )\n" ;
  227. # }
  228. #}
  229. if (! $ctdebug) {
  230. print OUTFILE "rm -f $_[0]\n" ;
  231. } else {
  232. print STDERR "no self-destruct script '" . $_[0] . "'\n" ;
  233. }
  234. close( OUTFILE ) ;
  235. &CTUDebug( "out of CTUnattachWriteScript\n" ) ;
  236. }
  237. 1;