/src/tools/liberty_environment.e

http://github.com/tybor/Liberty · Specman e · 147 lines · 120 code · 7 blank · 20 comment · 9 complexity · ee951036ccc1cca575a34864f52d5ed3 MD5 · raw file

  1. -- This file is part of Liberty Eiffel.
  2. --
  3. -- Liberty Eiffel is free software: you can redistribute it and/or modify
  4. -- it under the terms of the GNU General Public License as published by
  5. -- the Free Software Foundation, version 3 of the License.
  6. --
  7. -- Liberty Eiffel is distributed in the hope that it will be useful,
  8. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. -- GNU General Public License for more details.
  11. --
  12. -- You should have received a copy of the GNU General Public License
  13. -- along with Liberty Eiffel. If not, see <http://www.gnu.org/licenses/>.
  14. --
  15. expanded class LIBERTY_ENVIRONMENT
  16. insert
  17. LOGGING
  18. SYSTEM
  19. export
  20. {ANY} copy, is_equal, standard_is_equal, generating_type;
  21. {} all
  22. end
  23. feature {ANY}
  24. item (a_name: ABSTRACT_STRING): STRING is
  25. require
  26. a_name /= Void
  27. local
  28. name: FIXED_STRING
  29. value: STRING
  30. n, v: POINTER
  31. do
  32. name := a_name.intern
  33. value := environment.fast_reference_at(name)
  34. if value /= Void then
  35. Result := once ""
  36. Result.copy(value)
  37. else
  38. n := name.to_external
  39. v := basic_getenv(n)
  40. if v.is_not_null then
  41. Result := once ""
  42. Result.from_external_copy(v)
  43. end
  44. end
  45. end
  46. set (a_name, a_value: ABSTRACT_STRING) is
  47. require
  48. a_name /= Void
  49. a_value /= Void
  50. local
  51. name: FIXED_STRING
  52. value: STRING
  53. do
  54. name := a_name.intern
  55. value := environment.fast_reference_at(name)
  56. if value /= Void then
  57. value.clear_count
  58. value.append(a_value)
  59. else
  60. environment.add(a_value.out, name)
  61. end
  62. log.trace.put_string(a_name)
  63. log.trace.put_string(once " = ")
  64. log.trace.put_line(a_value)
  65. ensure
  66. item(a_name).is_equal(a_value.out)
  67. end
  68. feature {ANY}
  69. substitute (line: STRING) is
  70. -- The only one accepted notation is: ${...}. The substitution is performed in `line'.
  71. --| Taken from SmartEiffel: SYSTEM_TOOLS.environment_variable_substitution - but de-recursivated
  72. require
  73. line /= Void
  74. local
  75. i, state, offset_start, offset_end: INTEGER; c: CHARACTER; value, variable: STRING; done: BOOLEAN
  76. do
  77. from
  78. variable := once ""
  79. until
  80. done
  81. loop
  82. from
  83. state := 0
  84. i := line.lower
  85. until
  86. i > line.upper
  87. loop
  88. c := line.item(i)
  89. inspect
  90. state
  91. when 0 then
  92. -- Initial state.
  93. if c = '$' then
  94. state := 1
  95. offset_start := i
  96. end
  97. when 1 then
  98. -- "$" read.
  99. if c = '{' then
  100. state := 2
  101. variable.clear_count
  102. else
  103. state := 0
  104. end
  105. when 2 then
  106. -- "${" read.
  107. if c = '}' then
  108. state := 3
  109. offset_end := i
  110. i := line.upper
  111. else
  112. variable.extend(c)
  113. end
  114. else -- First correct variable found.
  115. end
  116. i := i + 1
  117. end
  118. if state = 3 then
  119. -- One variable found:
  120. value := item(variable)
  121. if value = Void then
  122. done := True
  123. else
  124. variable.copy(line)
  125. line.keep_head(offset_start - 1)
  126. line.append(value)
  127. variable.remove_head(offset_end)
  128. line.append(variable)
  129. end
  130. else
  131. done := True
  132. end
  133. end
  134. end
  135. feature {}
  136. environment: DICTIONARY[STRING, FIXED_STRING] is
  137. once
  138. create {HASHED_DICTIONARY[STRING, FIXED_STRING]} Result.make
  139. end
  140. end