ArgEnv.icl 1.9 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
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
implementation module ArgEnv

import code from "ArgEnvC.obj"

import StdEnv

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

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


getEnvSize :: !{#Char} -> Int
getEnvSize _
	= code {
		.inline getEnvSize
			ccall ArgEnvGetEnvironmentVariableSizeC "S-I"
		.end
	}

copyEnv :: !Int !{#Char} -> {#.Char}
copyEnv _ _
	= code {
		.inline copyEnv
			| Clean 1.1: use create_array
			| pushC	'?'
			| 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	1
			push_a	1
			ccall	ArgEnvGetEnvironmentVariableCharsC "SS-I"
			pop_b	1
			update_a	0 1
			pop_a	1
		.end
	}

getEnvironmentVariable :: !{#Char} -> *EnvironmentVariable
getEnvironmentVariable name
	| size == 0
		=	EnvironmentVariableUndefined
	| otherwise
		=	EnvironmentVariable (copyEnv size name`)
	where
		size
			=	getEnvSize name`
		name`
			=	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]}