Platform.icl 5.78 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1
2
3
4
implementation module Platform

import StdPSt
import ArgEnv
5
import StdArray, StdEnum, StdList, StdClass, StdBool, StdMisc
Diederik van Arkel's avatar
Diederik van Arkel committed
6
7
8
9
import UtilIO
from StdSystem import dirseparator
import code from library "platform_kernel_library"
import clCCall_12, ostoolbox
10
import logfile, set_return_code
Diederik van Arkel's avatar
Diederik van Arkel committed
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

PlatformDependant win mac :== win

cl_args =: getCommandLine

filter_opts [] = []
filter_opts [h:t]
	| h.[0] == '-'	= filter_opts (drop 1 t)
	= [h:filter_opts t]

get_arg n
	= get_arg n [a \\ a <-: cl_args]
where
	get_arg n [] = (False,"")
	get_arg n [h:t]
		| h == n = case t of [] -> (True,""); [h:_] -> (True,h)
		= get_arg n t

get_env n
	# e = getEnvironmentVariable n
	= case e of
		EnvironmentVariableUndefined	-> (False,"")
		EnvironmentVariable v			-> (True,v)

get_ini file section key default`
	#! (buffer,tb) = winMakeCString (createArray 256 '@') OSNewToolbox
	   (size,tb) = GetPrivateProfileString section key default` buffer 256 file tb
	   (buffer,tb) = winGetCStringAndFree buffer tb
	= buffer%(0,size-1)
where
	GetPrivateProfileString :: !String !String !String !CSTR !Int !String !*OSToolbox -> (!Int,!*OSToolbox)
	GetPrivateProfileString _ _ _ _ _ _ _ = code {
		ccall GetPrivateProfileStringA@24 "PsssIIs:I:I"
		}

Diederik van Arkel's avatar
Diederik van Arkel committed
46
initPlatformCommandLine :: !*(PSt .l) -> (![String],!*PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
47
48
49
50
51
52
53
54
55
56
initPlatformCommandLine ps
	# args	= cl_args
	# args	= [arg \\ arg <-: args]
	# args = filter_opts args
	| isEmpty args
		= ([],ps)
	# files	= tl args
	# files	= map GetLongPathName files
	= (files, ps)

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
batchOptions :: !*World -> (!Bool,!Bool,!String,!*File,!*World)
batchOptions world
	=	case [arg \\ arg <-: getCommandLine] of
			[_, "--batch-build", prj]
				-> batch False prj world
			[_, "--batch-force-build", prj]
				-> batch True prj world
			_
				->	(True, abort "force_update", abort "project file", abort "logfile", world)
where
	batch force_update prj world
		# (ok,logfile,world)	= openLogfile prj world
		| not ok
			=   (False, force_update, prj, logfile, wAbort ("--batch-build failed while opening logfile.\n") world)
			=	(False, force_update, prj, logfile, world)

wAbort :: !String !*World -> *World
wAbort message world
	# stderr	= fwrites message stderr
//	# (_,world)	= fclose stderr world
	# world		= set_return_code_world (-1) world
	= world

Diederik van Arkel's avatar
Diederik van Arkel committed
80
installPlatformEventHandlers :: !*(PSt .l) -> *(PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
81
82
83
installPlatformEventHandlers ps
	= ps

Diederik van Arkel's avatar
Diederik van Arkel committed
84
openPlatformWindowMenu :: !*(PSt .l) -> *(PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
85
86
87
88
89
90
91
92
93
94
95
openPlatformWindowMenu ps
	= ps

//====

inifilename
	# apppath = winGetModulePath
	# s = size apppath
	=: apppath%(0,s-4) +++. "ini\0"

section =: "Paths\0"
96
97
toolkey =: "tooldir\0"
tooltempkey =: "tooltemp\0"
Diederik van Arkel's avatar
Diederik van Arkel committed
98
99
100
envskey =: "envsdir\0"
prefskey =: "prefsdir\0"

101
102
tooldefault		=: StartUpDir +++. "\\Tools\0"
tooltempdefault	=: StartUpDir +++. "\\Temp\0"
Diederik van Arkel's avatar
Diederik van Arkel committed
103
104
105
envsdefault		=: StartUpDir +++. "\\Config\0"
prefsdefault	=: StartUpDir +++. "\\Config\0"

106
107
108
109
110
111
112
113
ToolsDir :: String
ToolsDir =:
	let
		(has_arg,arg) = get_arg "-tooldir"
		(has_env,env) = get_env "TOOLDIR"
		ini = get_ini inifilename section toolkey tooldefault
	in if has_arg arg (if has_env env ini)

Diederik van Arkel's avatar
Diederik van Arkel committed
114
TooltempDir :: String
Diederik van Arkel's avatar
Diederik van Arkel committed
115
116
117
118
TooltempDir =:
	let
		(has_arg,arg) = get_arg "-tooltemp"
		(has_env,env) = get_env "TOOLTEMP"
119
		ini = get_ini inifilename section tooltempkey tooltempdefault
Diederik van Arkel's avatar
Diederik van Arkel committed
120
121
122
	in if has_arg arg (if has_env env ini)

EnvsDir :: String
Diederik van Arkel's avatar
Diederik van Arkel committed
123
124
125
126
EnvsDir =:
	let
		(has_arg,arg) = get_arg "-envsdir"
		(has_env,env) = get_env "ENVSDIR"
Diederik van Arkel's avatar
Diederik van Arkel committed
127
		ini = get_ini inifilename section envskey envsdefault
Diederik van Arkel's avatar
Diederik van Arkel committed
128
129
130
	in if has_arg arg (if has_env env ini)

PrefsDir :: String
Diederik van Arkel's avatar
Diederik van Arkel committed
131
132
133
134
PrefsDir =:
	let
		(has_arg,arg) = get_arg "-prefsdir"
		(has_env,env) = get_env "PREFSDIR"
Diederik van Arkel's avatar
Diederik van Arkel committed
135
		ini = get_ini inifilename section prefskey prefsdefault
Diederik van Arkel's avatar
Diederik van Arkel committed
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
	in if has_arg arg (if has_env env ini)

StartUpDir :: !String
StartUpDir =: expand_8_3_names_in_path (RemoveFileName WinGetModulePath)

RemoveFileName :: !String -> String;
RemoveFileName path
	| found	= (path % (0, dec position));
			= path;
where 
	(found,position)	= LastColon path last;
	last				= dec (size path);
		
LastColon :: !String !Int -> (!Bool, !Int);
LastColon s i
	| i <= 0
		= (False,0);
	| dirseparator==s.[i]
	 	= (True, i);
		= LastColon s (dec i);

//-- expand_8_3_names_in_path

FindFirstFile :: !String -> (!Int,!String);
FindFirstFile file_name
	# find_data = createArray 318 '\0';
	# handle = FindFirstFile_ file_name find_data;
	= (handle,find_data);

FindFirstFile_ :: !String !String -> Int;
FindFirstFile_ file_name find_data
	= code {
		ccall FindFirstFileA@8 "Pss:I"
	}

FindClose :: !Int -> Int;
FindClose handle = code {
		ccall FindClose@4 "PI:I"
	}

find_null_char_in_string :: !Int !String -> Int;
find_null_char_in_string i s
	| i<size s && s.[i]<>'\0'
		= find_null_char_in_string (i+1) s;
		= i;

find_data_file_name find_data
	# i = find_null_char_in_string 44 find_data;
	= find_data % (44,i-1);

find_first_file_and_close :: !String -> (!Bool,!String);
find_first_file_and_close file_name
	# (handle,find_data) = FindFirstFile file_name;
	| handle <> (-1)
		# r = FindClose handle;
		| r==r
			= (True,find_data);
			= (False,find_data);
		= (False,"");

find_last_backslash_in_string i s
	| i<0
		= (False,-1);
	| s.[i]=='\\'
		= (True,i);
		= find_last_backslash_in_string (i-1) s;

expand_8_3_names_in_path :: !{#Char} -> {#Char};
expand_8_3_names_in_path path_and_file_name
	# (found_backslash,back_slash_index) = find_last_backslash_in_string (size path_and_file_name-1) path_and_file_name;
	| not found_backslash
		= path_and_file_name;
	# path = expand_8_3_names_in_path (path_and_file_name % (0,back_slash_index-1));
	# file_name = path_and_file_name % (back_slash_index+1,size path_and_file_name-1);
	# path_and_file_name = path+++"\\"+++file_name;
	# (ok,find_data) = find_first_file_and_close (path_and_file_name+++"\0");
	| ok
		= path+++"\\"+++find_data_file_name find_data;
		= path_and_file_name;

WinGetModulePath ::  {#Char}
WinGetModulePath
	= code
	{
		.inline WinGetModulePath
			ccall WinGetModulePath "-S"
		.end
	}