aboutsummaryrefslogtreecommitdiff
path: root/src/common/util.odin
blob: 94348fb9ee2c854c2e7d7ece0d20a2a0ef3905c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package common

import "core:bytes"
import "core:fmt"
import "core:log"
import "core:mem"
import "core:os"
import "core:path/filepath"
import "core:path/slashpath"
import "core:strings"
import "core:time"

foreign import libc "system:c"

when ODIN_OS == .Windows {
	delimiter :: ";"
} else {
	delimiter :: ":"
}

//TODO(daniel): This is temporary and should not be needed after os2
File_Mode_User_Executable :: os.File_Mode(1 << 8)

lookup_in_path :: proc(name: string) -> (string, bool) {
	path := os.get_env("PATH", context.temp_allocator)

	for directory in strings.split_iterator(&path, delimiter) {
		when ODIN_OS == .Windows {
			possibility := filepath.join(
				elems = {directory, fmt.tprintf("%v.exe", name)},
				allocator = context.temp_allocator,
			)
			if os.exists(possibility) {
				return possibility, true
			}
		} else {
			possibility := filepath.join(elems = {directory, name}, allocator = context.temp_allocator)
			possibility = resolve_home_dir(possibility, context.temp_allocator)
			if os.exists(possibility) {
				if info, err := os.stat(possibility, context.temp_allocator);
				   err == os.ERROR_NONE && (File_Mode_User_Executable & info.mode) != 0 {
					return possibility, true
				}
			}
		}
	}

	return "", false
}

resolve_home_dir :: proc(
	path: string,
	allocator := context.allocator,
) -> (
	resolved: string,
	allocated: bool,
) #optional_ok {
	when ODIN_OS == .Windows {
		return path, false
	} else {
		if strings.has_prefix(path, "~") {
			home := os.get_env("HOME", context.temp_allocator)
			if home == "" {
				log.error("could not find $HOME in the environment to be able to resolve ~ in collection paths")
				return path, false
			}

			return filepath.join({home, path[1:]}, allocator), true
		} else if strings.has_prefix(path, "$HOME") {
			home := os.get_env("HOME", context.temp_allocator)
			if home == "" {
				log.error("could not find $HOME in the environment to be able to resolve $HOME in collection paths")
				return path, false
			}

			return filepath.join({home, path[5:]}, allocator), true
		}
		return path, false
	}
}

	FILE :: struct {}
when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .Linux || ODIN_OS == .NetBSD {

	run_executable :: proc(command: string, stdout: ^[]byte) -> (u32, bool, []byte) {
		fp := popen(strings.clone_to_cstring(command, context.temp_allocator), "r")
		if fp == nil {
			return 0, false, stdout[0:]
		}
		defer pclose(fp)

		read_buffer: [50]byte
		index: int

		current_time := time.now()

		for fgets(&read_buffer[0], size_of(read_buffer), fp) != nil {
			read := bytes.index_byte(read_buffer[:], 0)
			defer index += cast(int)read

			if read > 0 && index + cast(int)read <= len(stdout) {
				mem.copy(&stdout[index], &read_buffer[0], cast(int)read)
			}

			elapsed_time := time.now()
			duration := time.diff(current_time, elapsed_time)

			if time.duration_seconds(duration) > 20 {
				log.error("odin check timed out")
				return 0, false, stdout[0:]
			}

			current_time = elapsed_time
		}


		return 0, true, stdout[0:index]
	}

	foreign libc 
	{
		popen :: proc(command: cstring, type: cstring) -> ^FILE ---
		pclose :: proc(stream: ^FILE) -> i32 ---
		fgets :: proc "cdecl" (s: [^]byte, n: i32, stream: ^FILE) -> [^]u8 ---
	}
}

get_executable_path :: proc(allocator := context.temp_allocator) -> string {
	exe_path, ok := filepath.abs(os.args[0], context.temp_allocator)

	if !ok {
		log.error("Failed to resolve executable path")
		return ""
	}

	return filepath.dir(exe_path, allocator)
}