Another one :) This is for Tasm but i suppose easily may convert for masm32 if anyone needs to test. I know "GetCL" in masm32.lib but this algo is better for me because just i wrote it :) Anyway, any suggestion will be appreciated.

Btw, accepts quoted strings, removes unnecessary spaces if not quoted but not TAB. Anyway, who use tabs in commandline?

Regards


GetMainArgs PROC uses edi esi, argv:DWORD, argc:DWORD

;#################################################; -= written by cakmak =-
; Parameters:
; argvek dd 25 dup (0) ; 25 "param addr" array (enough is it?)
; argcnt dd 0
;
; Usage:
; call GetMainArgs, offset argvek, offset argcnt
; mov eax, offset argvek
; mov ecx, [eax+4] ; 2. param
; call MessageBoxA, 0, ecx, 0, 0
;
; Return:
; 1 : Succeeded
; 0 : NOT Succeeded
;#####################

mov ecx, argc
mov dword ptr [ecx], 0 ; if any param then argc = 0

CALL GetCommandLineA
mov edi, eax ; edi points to commandline

mov al, 20h ; hunt for spaces
repnz scasb
cmp byte ptr [edi], 0 ; does an argument is passed ?
jz NOTsucceeded

; get length of params
push edi
mov ecx, -1
mov al, 0
repnz scasb
not ecx
dec ecx ; ecx = length of All params length
mov paramsLen, ecx

; kinda lstrcpy
pop edi
push edi
mov edi, offset temporary
pop esi
repnz movsb ; parameters in "temporary".

mov edi, offset temporary ; EDI points to new string

mov eax, argv ; EAX points to array of param address
mov edx, eax ; EDX points to argv (for "putAddrToArray")

; If we arrived here then we have, at least, 1 parameter. In quote or not!
; and EDI points to this string.

mov ecx, argc
mov dword ptr [ecx], 1 ; argc = 1 , at least!

mov esi, edi
mov ecx, paramsLen

otherChar:
mov al, byte ptr [edi]
cmp al, 0 ; end the process
jz finished
cmp al, 20h ; spaces
jz space
cmp al, 22h ; quotes
jz quoted
cmp WeAreInParam, 1 ; are we in param
je skip
call putAddrToArray ; else, now we beginning new param. Get the addr

skip:
inc edi
dec ecx
jnz otherChar
jmp finished

space:
call removeSpace ; self explained.
mov WeAreInParam, 0 ; if spaces finished then we arrived next param
jmp otherChar

quoted:
call processQuotes
jmp otherChar

removeSpace:
mov byte ptr [edi], 0
inc edi
dec ecx ; for safe (for all "dec ecx" here)
cmp byte ptr [edi], 20h
je removeSpace
RETN

processQuotes:
mov byte ptr [edi], 0
inc edi
dec ecx
call putAddrToArray
@@B:
inc edi
dec ecx
cmp byte ptr [edi], 22h ; hunt for right limiter "
jne @@B
mov byte ptr [edi], 20h ; let the spaces left in place if in quotes
inc edi
dec ecx
RETN

putAddrToArray:
mov eax, edx
mov [eax], edi
add eax, 4
mov edx, eax ; EDX points to next addr of param (in array).

mov ecx, argc ; argc is "1" indexed. 1st param 1, 2nd param 2,...
inc dword ptr [ecx]

mov WeAreInParam, 1 ; since we are in start of present parameter.
RETN

finished:
mov eax, 1
RET

NOTsucceeded:
xor eax, eax
RET
; Global variables for this procedure
temporary db 300 dup (0) ; parameters string place
paramsLen dd 0 ; the length of parameters
WeAreInParam dd 0 ; FLAG for, if we finished one parameter.

GetMainArgs ENDP
Posted on 2003-07-16 17:34:30 by cakmak


mov al, 20h ; hunt for spaces
repnz scasb
cmp byte ptr [edi], 0 ; does an argument is passed ?
jz NOTsucceeded


I think you should take care of "" also.
A path file can contain spaces.
Posted on 2003-07-18 10:41:32 by BJZ
Thank you. I have slightly changed some parts already.

Regards
Posted on 2003-07-18 21:27:45 by cakmak