/
aoikwinwhich.jl
172 lines (140 loc) · 3.49 KB
/
aoikwinwhich.jl
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#
function find_exe_paths(prog)
# 8f1kRCu
env_pathext = get(ENV, "PATHEXT", "")
# 4fpQ2RB
if env_pathext == ""
# 9dqlPRg
return []
end
# 6qhHTHF
# Split into a list of extensions
ext_s = split(env_pathext, ";")
# 2pGJrMW
# Strip
ext_s = [strip(ext) for ext=ext_s]
# 2gqeHHl
# Remove empty.
# Must be done after the stripping at 2pGJrMW.
ext_s = filter(x -> x != "", ext_s)
# 2zdGM8W
# Convert to lowercase
ext_s = [lowercase(ext) for ext=ext_s]
# 2fT8aRB
# Uniquify
ext_s = unique(ext_s)
# 4ysaQVN
env_path = get(ENV, "PATH", "")
# 5gGwKZL
if env_path == ""
# 7bVmOKe
# Go ahead with "dir_path_s" being empty
dir_path_s = []
else
# 6mPI0lg
# Split into a list of dir paths
dir_path_s = split(env_path, ";")
end
# 5rT49zI
# Insert empty dir path to the beginning.
#
# Empty dir handles the case that "prog" is a path, either relative or
# absolute. See code 7rO7NIN.
unshift!(dir_path_s, "")
# 2klTv20
# Uniquify
dir_path_s = unique(dir_path_s)
# 9gTU1rI
# Check if "prog" ends with one of the file extension in "ext_s".
#
# "ext_s" are all in lowercase, ensured at 2zdGM8W.
prog_lc = lowercase(prog)
prog_has_ext = (findfirst(ext -> endswith(prog_lc, ext), ext_s)) != 0
# Index value "0" means not found.
# 6bFwhbv
exe_path_s = String[]
for dir_path=dir_path_s
# 7rO7NIN
# Synthesize a path
if dir_path == ""
path = prog
else
path = dir_path * "\\" * prog
end
# 6kZa5cq
# If "prog" ends with executable file extension
if prog_has_ext
# 3whKebE
if isfile(path)
# 2ffmxRF
push!(exe_path_s, path)
end
end
# 2sJhhEV
# Assume user has omitted the file extension
for ext=ext_s
# 6k9X6GP
# Synthesize a path with one of the file extensions in PATHEXT
path_2 = path * ext
# 6kabzQg
if isfile(path_2)
# 7dui4cD
push!(exe_path_s, path_2)
end
end
end
# 8swW6Av
# Uniquify
exe_path_s = unique(exe_path_s)
# 7y3JlnS
return exe_path_s
end
#
function main()
# 9mlJlKg
# If not exactly one command argument is given
if length(ARGS) != 1
usage = """Usage: aoikwinwhich PROG
#/ PROG can be either name or path
aoikwinwhich notepad.exe
aoikwinwhich C:\\Windows\\notepad.exe
#/ PROG can be either absolute or relative
aoikwinwhich C:\\Windows\\notepad.exe
aoikwinwhich Windows\\notepad.exe
#/ PROG can be either with or without extension
aoikwinwhich notepad.exe
aoikwinwhich notepad
aoikwinwhich C:\\Windows\\notepad.exe
aoikwinwhich C:\\Windows\\notepad"""
# 7rOUXFo
# Print program usage
print(usage)
# 3nqHnP7
# Exit
return 1
end
# 9m5B08H
# Get executable name or path
prog = ARGS[1]
# 8ulvPXM
# Find executable paths
exe_path_s = find_exe_paths(prog)
# 5fWrcaF
# If has found none
if length(exe_path_s) == 0
# 3uswpx0
# Exit
return 2
# If has found some
else
# 9xPCWuS
# Print result
print(join(exe_path_s, "\n") * "\n")
# 4s1yY1b
# Exit
return 0
end
end
# 4zKrqsC
# Program entry
exit(main())