Skip to content

Commit 3862555

Browse files
committed
Replace core:posix usage in core:os/os2
1 parent fe9f74f commit 3862555

File tree

4 files changed

+108
-33
lines changed

4 files changed

+108
-33
lines changed

core/net/dns.odin

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ init_dns_configuration :: proc() {
4949
/*
5050
Resolve %ENVIRONMENT% placeholders in their paths.
5151
*/
52-
dns_configuration.resolv_conf, _ = replace_environment_path(dns_configuration.resolv_conf)
53-
dns_configuration.hosts_file, _ = replace_environment_path(dns_configuration.hosts_file)
52+
dns_configuration.resolv_conf = os.replace_environment_placeholders(dns_configuration.resolv_conf)
53+
dns_configuration.hosts_file = os.replace_environment_placeholders(dns_configuration.hosts_file)
5454
}
5555

5656
@(fini, private)
@@ -63,28 +63,6 @@ destroy_dns_configuration :: proc() {
6363

6464
dns_configuration := DEFAULT_DNS_CONFIGURATION
6565

66-
// Always allocates for consistency.
67-
replace_environment_path :: proc(path: string, allocator := context.allocator) -> (res: string, ok: bool) {
68-
// Nothing to replace. Return a clone of the original.
69-
if strings.count(path, "%") != 2 {
70-
return strings.clone(path, allocator), true
71-
}
72-
73-
left := strings.index(path, "%") + 1
74-
assert(left > 0 && left <= len(path)) // should be covered by there being two %
75-
76-
right := strings.index(path[left:], "%") + 1
77-
assert(right > 0 && right <= len(path)) // should be covered by there being two %
78-
79-
env_key := path[left: right]
80-
env_val := os.get_env(env_key, allocator)
81-
defer delete(env_val)
82-
83-
res, _ = strings.replace(path, path[left - 1: right + 1], env_val, 1, allocator)
84-
return res, true
85-
}
86-
87-
8866
/*
8967
Resolves a hostname to exactly one IP4 and IP6 endpoint.
9068
It's then up to you which one you use.

core/os/os.odin

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "base:intrinsics"
44
import "base:runtime"
55
import "core:io"
66
import "core:strconv"
7+
import "core:strings"
78
import "core:unicode/utf8"
89

910

@@ -210,3 +211,55 @@ heap_free :: runtime.heap_free
210211
processor_core_count :: proc() -> int {
211212
return _processor_core_count()
212213
}
214+
215+
// Always allocates for consistency.
216+
replace_environment_placeholders :: proc(path: string, allocator := context.allocator) -> (res: string) {
217+
path := path
218+
219+
sb: strings.Builder
220+
strings.builder_init_none(&sb, allocator)
221+
for len(path) > 0 {
222+
switch path[0] {
223+
case '%': // Windows
224+
when ODIN_OS == .Windows {
225+
for r, i in path[1:] {
226+
if r == '%' {
227+
env_key := path[1:i+1]
228+
env_val := get_env(env_key, context.temp_allocator)
229+
strings.write_string(&sb, env_val)
230+
path = path[i+1:] // % is part of key, so skip 1 character extra
231+
}
232+
}
233+
} else {
234+
strings.write_rune(&sb, rune(path[0]))
235+
}
236+
237+
case '$': // Posix
238+
when ODIN_OS != .Windows {
239+
env_key := ""
240+
dollar_loop: for r, i in path[1:] {
241+
switch r {
242+
case 'A'..='Z', 'a'..='z', '0'..='9', '_': // Part of key ident
243+
case:
244+
env_key = path[1:i+1]
245+
break dollar_loop
246+
}
247+
}
248+
if len(env_key) > 0 {
249+
env_val := get_env(env_key, context.temp_allocator)
250+
strings.write_string(&sb, env_val)
251+
path = path[len(env_key):]
252+
}
253+
254+
} else {
255+
strings.write_rune(&sb, rune(path[0]))
256+
}
257+
258+
case:
259+
strings.write_rune(&sb, rune(path[0]))
260+
}
261+
262+
path = path[1:]
263+
}
264+
return strings.to_string(sb)
265+
}

core/os/os2/env.odin

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package os2
22

33
import "base:runtime"
4+
import "core:strings"
45

56
// get_env retrieves the value of the environment variable named by the key
67
// It returns the value, which will be empty if the variable is not present
@@ -45,4 +46,55 @@ environ :: proc(allocator: runtime.Allocator) -> ([]string, Error) {
4546
return _environ(allocator)
4647
}
4748

49+
// Always allocates for consistency.
50+
replace_environment_placeholders :: proc(path: string, allocator: runtime.Allocator) -> (res: string) {
51+
path := path
4852

53+
sb: strings.Builder
54+
strings.builder_init_none(&sb, allocator)
55+
56+
for len(path) > 0 {
57+
switch path[0] {
58+
case '%': // Windows
59+
when ODIN_OS == .Windows {
60+
for r, i in path[1:] {
61+
if r == '%' {
62+
env_key := path[1:i+1]
63+
env_val := get_env(env_key, context.temp_allocator)
64+
strings.write_string(&sb, env_val)
65+
path = path[i+1:] // % is part of key, so skip 1 character extra
66+
}
67+
}
68+
} else {
69+
strings.write_rune(&sb, rune(path[0]))
70+
}
71+
72+
case '$': // Posix
73+
when ODIN_OS != .Windows {
74+
env_key := ""
75+
dollar_loop: for r, i in path[1:] {
76+
switch r {
77+
case 'A'..='Z', 'a'..='z', '0'..='9', '_': // Part of key ident
78+
case:
79+
env_key = path[1:i+1]
80+
break dollar_loop
81+
}
82+
}
83+
if len(env_key) > 0 {
84+
env_val := get_env(env_key, context.temp_allocator)
85+
strings.write_string(&sb, env_val)
86+
path = path[len(env_key):]
87+
}
88+
89+
} else {
90+
strings.write_rune(&sb, rune(path[0]))
91+
}
92+
93+
case:
94+
strings.write_rune(&sb, rune(path[0]))
95+
}
96+
97+
path = path[1:]
98+
}
99+
return strings.to_string(sb)
100+
}

core/os/os2/user_posix.odin

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package os2
44
import "base:runtime"
55
import "core:encoding/ini"
66
import "core:strings"
7-
import "core:sys/posix"
87

98
_user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
109
#partial switch ODIN_OS {
@@ -169,14 +168,7 @@ _xdg_user_dirs_lookup :: proc(xdg_key: string, allocator: runtime.Allocator) ->
169168

170169
for k, v in ini.iterate(&it) {
171170
if k == xdg_key {
172-
we: posix.wordexp_t
173-
defer posix.wordfree(&we)
174-
175-
if _err := posix.wordexp(strings.clone_to_cstring(v, temp_allocator), &we, nil); _err != nil || we.we_wordc != 1 {
176-
return "", .Wordexp_Failed
177-
}
178-
179-
return strings.clone_from_cstring(we.we_wordv[0], allocator)
171+
return replace_environment_placeholders(v, allocator), nil
180172
}
181173
}
182174
return

0 commit comments

Comments
 (0)