env_linux.odin 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //+private
  2. package os2
  3. import "base:runtime"
  4. import "base:intrinsics"
  5. import "core:sync"
  6. import "core:slice"
  7. import "core:strings"
  8. // TODO: IF NO_CRT:
  9. // Override the libc environment functions' weak linkage to
  10. // allow us to interact with 3rd party code that DOES link
  11. // to libc. Otherwise, our environment can be out of sync.
  12. // ELSE:
  13. // Just use the libc.
  14. NOT_FOUND :: -1
  15. // the environment is a 0 delimited list of <key>=<value> strings
  16. _env: [dynamic]string
  17. _env_mutex: sync.Mutex
  18. // We need to be able to figure out if the environment variable
  19. // is contained in the original environment or not. This also
  20. // serves as a flag to determine if we have built _env.
  21. _org_env_begin: uintptr
  22. _org_env_end: uintptr
  23. // Returns value + index location into _env
  24. // or -1 if not found
  25. _lookup :: proc(key: string) -> (value: string, idx: int) {
  26. sync.mutex_lock(&_env_mutex)
  27. defer sync.mutex_unlock(&_env_mutex)
  28. for entry, i in _env {
  29. if k, v := _kv_from_entry(entry); k == key {
  30. return v, i
  31. }
  32. }
  33. return "", -1
  34. }
  35. _lookup_env :: proc(key: string, allocator: runtime.Allocator) -> (value: string, found: bool) {
  36. if _org_env_begin == 0 {
  37. _build_env()
  38. }
  39. if v, idx := _lookup(key); idx != -1 {
  40. found = true
  41. value, _ = clone_string(v, allocator)
  42. }
  43. return
  44. }
  45. _set_env :: proc(key, v_new: string) -> bool {
  46. if _org_env_begin == 0 {
  47. _build_env()
  48. }
  49. // all key values are stored as "key=value\x00"
  50. kv_size := len(key) + len(v_new) + 2
  51. if v_curr, idx := _lookup(key); idx != NOT_FOUND {
  52. if v_curr == v_new {
  53. return true
  54. }
  55. sync.mutex_lock(&_env_mutex)
  56. defer sync.mutex_unlock(&_env_mutex)
  57. unordered_remove(&_env, idx)
  58. if !_is_in_org_env(v_curr) {
  59. // We allocated this key-value. Possibly resize and
  60. // overwrite the value only. Otherwise, treat as if it
  61. // wasn't in the environment in the first place.
  62. k_addr, v_addr := _kv_addr_from_val(v_curr, key)
  63. if len(v_new) > len(v_curr) {
  64. k_addr = ([^]u8)(heap_resize(k_addr, kv_size))
  65. if k_addr == nil {
  66. return false
  67. }
  68. v_addr = &k_addr[len(key) + 1]
  69. }
  70. intrinsics.mem_copy_non_overlapping(v_addr, raw_data(v_new), len(v_new))
  71. v_addr[len(v_new)] = 0
  72. append(&_env, string(k_addr[:kv_size]))
  73. return true
  74. }
  75. }
  76. k_addr := ([^]u8)(heap_alloc(kv_size))
  77. if k_addr == nil {
  78. return false
  79. }
  80. intrinsics.mem_copy_non_overlapping(k_addr, raw_data(key), len(key))
  81. k_addr[len(key)] = '='
  82. val_slice := k_addr[len(key) + 1:]
  83. intrinsics.mem_copy_non_overlapping(&val_slice[0], raw_data(v_new), len(v_new))
  84. val_slice[len(v_new)] = 0
  85. sync.mutex_lock(&_env_mutex)
  86. append(&_env, string(k_addr[:kv_size - 1]))
  87. sync.mutex_unlock(&_env_mutex)
  88. return true
  89. }
  90. _unset_env :: proc(key: string) -> bool {
  91. if _org_env_begin == 0 {
  92. _build_env()
  93. }
  94. v: string
  95. i: int
  96. if v, i = _lookup(key); i == -1 {
  97. return false
  98. }
  99. sync.mutex_lock(&_env_mutex)
  100. unordered_remove(&_env, i)
  101. sync.mutex_unlock(&_env_mutex)
  102. if _is_in_org_env(v) {
  103. return true
  104. }
  105. // if we got this far, the envrionment variable
  106. // existed AND was allocated by us.
  107. k_addr, _ := _kv_addr_from_val(v, key)
  108. heap_free(k_addr)
  109. return true
  110. }
  111. _clear_env :: proc() {
  112. sync.mutex_lock(&_env_mutex)
  113. defer sync.mutex_unlock(&_env_mutex)
  114. for kv in _env {
  115. if !_is_in_org_env(kv) {
  116. heap_free(raw_data(kv))
  117. }
  118. }
  119. clear(&_env)
  120. // nothing resides in the original environment either
  121. _org_env_begin = ~uintptr(0)
  122. _org_env_end = ~uintptr(0)
  123. }
  124. _environ :: proc(allocator: runtime.Allocator) -> []string {
  125. if _org_env_begin == 0 {
  126. _build_env()
  127. }
  128. env := make([]string, len(_env), allocator)
  129. sync.mutex_lock(&_env_mutex)
  130. defer sync.mutex_unlock(&_env_mutex)
  131. for entry, i in _env {
  132. env[i], _ = clone_string(entry, allocator)
  133. }
  134. return env
  135. }
  136. // The entire environment is stored as 0 terminated strings,
  137. // so there is no need to clone/free individual variables
  138. export_cstring_environment :: proc(allocator: runtime.Allocator) -> []cstring {
  139. if _org_env_begin == 0 {
  140. // The environment has not been modified, so we can just
  141. // send the original environment
  142. org_env := _get_original_env()
  143. n: int
  144. for ; org_env[n] != nil; n += 1 {}
  145. return slice.clone(org_env[:n + 1], allocator)
  146. }
  147. // NOTE: already terminated by nil pointer via + 1
  148. env := make([]cstring, len(_env) + 1, allocator)
  149. sync.mutex_lock(&_env_mutex)
  150. defer sync.mutex_unlock(&_env_mutex)
  151. for entry, i in _env {
  152. env[i] = cstring(raw_data(entry))
  153. }
  154. return env
  155. }
  156. _build_env :: proc() {
  157. sync.mutex_lock(&_env_mutex)
  158. defer sync.mutex_unlock(&_env_mutex)
  159. if _org_env_begin != 0 {
  160. return
  161. }
  162. _env = make(type_of(_env), heap_allocator())
  163. cstring_env := _get_original_env()
  164. _org_env_begin = uintptr(rawptr(cstring_env[0]))
  165. for i := 0; cstring_env[i] != nil; i += 1 {
  166. bytes := ([^]u8)(cstring_env[i])
  167. n := len(cstring_env[i])
  168. _org_env_end = uintptr(&bytes[n])
  169. append(&_env, string(bytes[:n]))
  170. }
  171. }
  172. _get_original_env :: #force_inline proc() -> [^]cstring {
  173. // essentially &argv[argc] which should be a nil pointer!
  174. #no_bounds_check env: [^]cstring = &runtime.args__[len(runtime.args__)]
  175. assert(env[0] == nil)
  176. return &env[1]
  177. }
  178. _kv_from_entry :: #force_inline proc(entry: string) -> (k, v: string) {
  179. eq_idx := strings.index_byte(entry, '=')
  180. if eq_idx == -1 {
  181. return entry, ""
  182. }
  183. return entry[:eq_idx], entry[eq_idx + 1:]
  184. }
  185. _kv_addr_from_val :: #force_inline proc(val: string, key: string) -> ([^]u8, [^]u8) {
  186. v_addr := raw_data(val)
  187. k_addr := ([^]u8)(&v_addr[-(len(key) + 1)])
  188. return k_addr, v_addr
  189. }
  190. _is_in_org_env :: #force_inline proc(env_data: string) -> bool {
  191. addr := uintptr(raw_data(env_data))
  192. return addr >= _org_env_begin && addr < _org_env_end
  193. }