Platform.icl 6.09 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 StdIOCommon
11
import logfile, set_return_code
Diederik van Arkel's avatar
Diederik van Arkel committed
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

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"
		}

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

Diederik van Arkel's avatar
Diederik van Arkel committed
58
batchOptions :: !*World -> (!Bool,Bool,String,*File,!*World)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
Diederik van Arkel's avatar
Diederik van Arkel committed
77
	# (_,world)	= fclose stderr world
78
79
80
	# world		= set_return_code_world (-1) world
	= world

81
82
83
84
pAbort :: !(PSt .a) -> PSt .a
pAbort ps
	= set_return_code_pst (-1) ps

Diederik van Arkel's avatar
Diederik van Arkel committed
85
installPlatformEventHandlers :: !*(PSt .l) -> *(PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
86
87
88
installPlatformEventHandlers ps
	= ps

Diederik van Arkel's avatar
Diederik van Arkel committed
89
openPlatformWindowMenu :: !*(PSt .l) -> *(PSt .l)
Diederik van Arkel's avatar
Diederik van Arkel committed
90
91
92
openPlatformWindowMenu ps
	= ps

93
94
95
96
97
98
getWindowModified :: !Id !(IOSt .l) -> (!Maybe Bool,!IOSt .l)
getWindowModified _ io = (Nothing,io)

setWindowModified :: !Id !String !Bool !(IOSt .l) -> IOSt .l
setWindowModified _ _ _ io = io

Diederik van Arkel's avatar
Diederik van Arkel committed
99
100
101
102
103
104
105
106
//====

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

section =: "Paths\0"
107
108
toolkey =: "tooldir\0"
tooltempkey =: "tooltemp\0"
Diederik van Arkel's avatar
Diederik van Arkel committed
109
110
111
envskey =: "envsdir\0"
prefskey =: "prefsdir\0"

112
113
tooldefault		=: StartUpDir +++. "\\Tools\0"
tooltempdefault	=: StartUpDir +++. "\\Temp\0"
Diederik van Arkel's avatar
Diederik van Arkel committed
114
115
116
envsdefault		=: StartUpDir +++. "\\Config\0"
prefsdefault	=: StartUpDir +++. "\\Config\0"

117
118
119
120
121
122
123
124
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
125
126
TempDir :: String
TempDir =:
Diederik van Arkel's avatar
Diederik van Arkel committed
127
	let
Diederik van Arkel's avatar
Diederik van Arkel committed
128
129
		(has_arg,arg) = get_arg "-tempdir"
		(has_env,env) = get_env "TEMPDIR"
130
		ini = get_ini inifilename section tooltempkey tooltempdefault
Diederik van Arkel's avatar
Diederik van Arkel committed
131
132
133
	in if has_arg arg (if has_env env ini)

EnvsDir :: String
Diederik van Arkel's avatar
Diederik van Arkel committed
134
135
136
137
EnvsDir =:
	let
		(has_arg,arg) = get_arg "-envsdir"
		(has_env,env) = get_env "ENVSDIR"
Diederik van Arkel's avatar
Diederik van Arkel committed
138
		ini = get_ini inifilename section envskey envsdefault
Diederik van Arkel's avatar
Diederik van Arkel committed
139
140
141
	in if has_arg arg (if has_env env ini)

PrefsDir :: String
Diederik van Arkel's avatar
Diederik van Arkel committed
142
143
144
145
PrefsDir =:
	let
		(has_arg,arg) = get_arg "-prefsdir"
		(has_env,env) = get_env "PREFSDIR"
Diederik van Arkel's avatar
Diederik van Arkel committed
146
		ini = get_ini inifilename section prefskey prefsdefault
Diederik van Arkel's avatar
Diederik van Arkel committed
147
148
	in if has_arg arg (if has_env env ini)

Diederik van Arkel's avatar
Diederik van Arkel committed
149
StartUpDir :: String
Diederik van Arkel's avatar
Diederik van Arkel committed
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
225
226
227
228
229
230
231
232
233
234
235
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
	}

236
237
238
onOSX	:: Bool
onOSX
	= False