renamed a few files
[mereo.git] / src / cmdline.asm
blob22ebdd72a866a34ea0c133618e51b3a673e09e46
1 ; Mereo - An HTTP server.
2 ; Copyright (C) 2008 Jelle Geerts
4 ; This program is free software: you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation, either version 3 of the License, or
7 ; (at your option) any later version.
9 ; This program is distributed in the hope that it will be useful,
10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ; GNU General Public License for more details.
14 ; You should have received a copy of the GNU General Public License
15 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ; Returns pointer to commandline arguments if a commandline has been found.
18 ; Returns 0 if no commandline arguments found.
19 GetCmdLineArgs proc uses edi
20 LOCAL CmdLine :DWORD
21 LOCAL LastLoop :DWORD
23 invoke GetCommandLine
24 mov edi, eax
25 mov [CmdLine], eax
26 invoke StrLen, edi
27 mov ecx, eax ; ECX = string length of commandline
28 cld ; Clear Direction Flag (search forward)
29 cmp byte ptr [edi], '"'
30 jnz @@CMDLINE_SEARCH_SPACE ; Search next space (use as separator)
31 mov eax, '"' ; Else, search for next " character (use as separator)
32 inc edi ; +1 to search after the first " character
33 repnz scasb ; Do a search
34 jnz @@CMDLINE_NO_ARGS ; Not found?!
35 jmp @@CMDLINE_PARSE
36 @@CMDLINE_SEARCH_SPACE:
37 ; ECX = string length of commandline
38 mov eax, ' '
39 repnz scasb
40 jnz @@CMDLINE_ERROR
41 jmp @@CMDLINE_DONE
42 @@CMDLINE_PARSE:
43 cmp byte ptr [edi], 0 ; Commandline ends already?
44 jz @@CMDLINE_NO_ARGS
45 inc edi ; +1 to point to first argument
46 cmp byte ptr [edi], 0 ; Commandline ends already?
47 jz @@CMDLINE_NO_ARGS ; No arguments passed on commandline
48 jmp @@CMDLINE_DONE
49 @@CMDLINE_ERROR:
50 mov eax, -1
51 jmp @@CMDLINE_DONE
52 @@CMDLINE_NO_ARGS:
53 xor eax, eax ; Indicate no arguments found
54 ret
55 @@CMDLINE_DONE:
56 mov eax, edi
57 ret
58 GetCmdLineArgs endp
60 ; Returns a pointer to the requested argument from the commandline.
61 ; Everything between the '"' characters not parsed. e.g. "test".
63 ; Also every argument (i.e. "test test2", separated by spaces) not
64 ; starting with the first character of the argument to search for will
65 ; not be parsed. This is done to prevent the parser from thinking that
66 ; an argument has been found within another argument.
67 ; e.g. "-h .\ta-del" would otherwise let the parser think that "-de" has
68 ; been found when we were searching for "-h" instead.
70 ; Returns the pointer on success, or -1 on failure.
71 GetCmdLineArgPtr proc uses ebx esi edi lpszCommandLine:DWORD, lpszArg:DWORD
72 LOCAL fDontParse :DWORD
74 mov [fDontParse], FALSE
75 mov esi, [lpszCommandLine]
77 ; Loop through all characters until a matching character was found
78 @@LOOP:
79 lodsb
80 or al, al
81 jnz @F
82 ; Failure
83 xor eax, eax
84 dec eax
85 ret
87 @@:
88 ; Don't parse characters between '"' characters. e.g.: "test" (here, test is not parsed)
89 .if al == '"'
90 xor [fDontParse], TRUE ; Set DontParse flag to TRUE if it was FALSE, and FALSE if it was TRUE
91 .elseif [fDontParse] == FALSE
92 ; Check if the argument starts with the first character of the argument that we are in search of.
93 invoke StrLen, [lpszArg]
94 mov ecx, eax
95 mov al, ' '
96 mov edi, esi
97 dec edi
98 std
99 repnz scasb
101 inc edi
102 mov edx, [lpszArg]
103 mov dl, [edx]
105 mov eax, [lpszCommandLine]
106 cmp byte ptr [eax], dl
107 jz @F
108 cmp dl, byte ptr [edi+1]
109 jnz @@LOOP
112 mov ebx, [lpszArg]
113 invoke StrLen, ebx
114 push eax
115 push ebx
116 push esi
117 dec dword ptr [esp] ; one less because "lodsb" increased ESI
118 call StrCmpIn
119 .if eax == 0
120 ; now check if a space or NULL byte follows the argument (should be)
121 invoke StrLen, ebx
122 mov edx, esi
123 add edx, eax
124 cmp byte ptr [edx-1], 0
125 jz @F
126 cmp byte ptr [edx-1], ' '
127 jnz @@LOOP
130 ; success
131 .while byte ptr [edi] == ' '
132 inc edi
133 .endw
134 mov eax, esi
135 dec eax ; one less because "lodsb" increased ESI
136 sub eax, [lpszCommandLine]
137 jmp @@RET
138 .endif
139 .endif
140 jmp @@LOOP
142 @@RET:
144 GetCmdLineArgPtr endp
146 ; Returns the index of where the string after the requested argument
147 ; from the commandline.was found.
149 ; Returns the index in EAX and the length of the string in ECX on success.
150 ; On failure (i.e. argument or string not found) a value of -1 is returned.
151 GetCmdLineArgString proc uses esi edi lpszCommandLine:DWORD, lpszArg:DWORD
152 invoke GetCmdLineArgPtr, [lpszCommandLine], [lpszArg]
153 cmp eax, -1
154 jz @@RET
156 add eax, [lpszCommandLine]
157 mov edi, eax
158 add edi, 2
159 cmp byte ptr [edi], 0 ; If null ..
160 jz @@PARSING_ERROR ; .. then the argument is invalid
161 inc edi
162 mov esi, edi
163 invoke StrLen, edi
164 mov ecx, eax
165 cmp byte ptr [edi], '"'
166 jz @@SEARCH_QUOTMARK
167 mov al, ' '
169 repnz scasb
170 pushfd
171 pop eax
172 cmp byte ptr [edi], 0
173 lea edi, [edi+1] ; EDI = EDI + 1 (without modifying CPU flags)
174 jz @@DONE
175 dec edi ; Decrease if not NULL (means not last argument and length already calculated correctly)
176 push eax
177 popfd
178 jnz @@PARSING_ERROR
179 jmp @@DONE
181 @@SEARCH_QUOTMARK:
182 mov eax, '"'
184 inc edi
185 inc esi
186 repnz scasb
187 jnz @@PARSING_ERROR
189 @@DONE:
190 mov ecx, edi
191 sub ecx, esi
192 mov eax, esi
193 sub eax, [lpszCommandLine]
195 invoke StrLen, esi
196 inc eax ; preserve one NULL byte (always needed, we can't garantuee it's already
197 ; preserved because the string not always ends with a '"' character)
198 invoke GlobalAlloc, GPTR, eax
199 cmp eax, -1
200 jz @@PARSING_ERROR
201 mov ecx, edi
202 sub ecx, esi
203 push eax
204 invoke lstrcpyn, eax, esi, ecx
205 pop eax
206 jmp @@RET
208 @@PARSING_ERROR:
209 mov eax, -1
211 @@RET:
213 GetCmdLineArgString endp
215 ; Retrieves the integer after the requested argument from the commandline.
216 ; Returns the pointer on success, or -1 on failure.
217 GetCmdLineArgInt proc uses ebx esi edi lpszCommandLine:DWORD, lpszArg:DWORD
218 LOCAL lpIntAscii[16] :BYTE
220 invoke GetCmdLineArgPtr, [lpszCommandLine], [lpszArg]
221 cmp eax, -1
222 jz @@RET
224 mov ebx, [lpszCommandLine]
225 add eax, 2
226 cmp byte ptr [ebx+eax], ' ' ; Check if there is a space between the port and the argument
227 jnz @@PARSING_ERROR
228 inc ebx
229 add ebx, eax ; EBX = pointer to port number
230 mov edi, ebx
231 invoke StrLen, ebx
232 mov ebx, edi
233 mov eax, ' '
234 mov ecx, eax
235 ; cld
236 repnz scasb
237 jz @@GET_INT_LENGTH_TILL_SPACE
238 invoke StrLen, ebx
239 mov ecx, eax
240 inc ecx
241 jmp @@COPY_INT
243 @@GET_INT_LENGTH_TILL_SPACE:
244 mov ecx, edi
245 sub ecx, ebx
246 ; jmp @@COPY_INT
248 @@COPY_INT:
249 invoke lstrcpyn, ADDR lpIntAscii, ebx, ecx
250 invoke atodw, ADDR lpIntAscii
251 jmp @@RET
253 @@PARSING_ERROR:
254 mov eax, -1
256 @@RET:
258 GetCmdLineArgInt endp