ArgEnv.icl 1.46 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
 
implementation module ArgEnv

import StdEnv

:: CString :== Int
NULL :== 0

:: EnvironmentVariable
    =	EnvironmentVariableUndefined
    |   EnvironmentVariable !.{#Char}

getEnv :: !{#Char} -> (!Int, !CString)
getEnv _
	= code {
		.inline getEnv
			ccall ArgEnvGetEnvironmentVariableC "S-II"
		.end
	}

getEnvironmentVariable :: !{#Char} -> *EnvironmentVariable
getEnvironmentVariable name
	| cString == NULL
		=	EnvironmentVariableUndefined
	| otherwise
		=	EnvironmentVariable (copy size cString)
	where
		(size, cString)
			=	getEnv (name +++ "\0")


copy :: !Int !CString -> {#.Char}
copy length cString
	= code {
		.inline copy
			| Clean 1.1: use create_array
			| pushC	'\000'
			| push_b	1
			| update_b	1 2
			| update_b	0 1
			| pop_b	1
			| create_array	CHAR 0 1

			| Clean 1.2 and later: use create_array_
			create_array_	CHAR 0 1

			push_a	0
			ccall	ArgEnvCopyCStringToCleanStringC "IS-I"
			pop_b	1
		.end
	}

getCommandLineCount :: Int
getCommandLineCount 
	= code {
		.inline getCommandLineCount
			ccall ArgEnvGetCommandLineCountC "-I"
		.end
	}

getCommandLineArgument :: !Int -> (!Int, !Int)
getCommandLineArgument _
	= code {
		.inline getCommandLineArgument
			ccall ArgEnvGetCommandLineArgumentC "I-II"
		.end
	}

getArg :: !Int -> {#.Char}
getArg i
	=	copy size cString
	where
		(size, cString)
			=	getCommandLineArgument i

// Clean 1.1: getCommandLine :: {{#Char}}
getCommandLine :: {.{#Char}}
getCommandLine
	=	{getArg i \\ i <- [0 .. getCommandLineCount-1]}