The program may be run by
ProgPath\ProgName  FilePath\FileName

Both components of CommandLine may contain 'spaces'.
How to extract right FileName
Posted on 2006-04-04 10:11:43 by MikDay
i always use GetCl function from masm32.lib
Posted on 2006-04-04 10:19:04 by s5vi
The filename should be enclosed in quotes of it's got any spaces.

So you'll find something like:

C:\masm32\project.exe params...

or:

"C:\Program Files\My Project\my project.exe" params...
Posted on 2006-04-04 12:01:21 by QvasiModo
GetCommandLineW
followed by
CommandLineToArgvW

Quick to code, documented, clean & clear.
Posted on 2006-04-04 12:47:39 by ti_mo_n
Here is a simple routine to parse commad line parameters, it can handle parameters separated by space characters.

; 512 bytes should be allocated for the buffer

ParseCmdLine PROC uses esi edi buffer:DWORD

invoke GetCommandLine
lea edx,
xor eax,eax
mov esi,buffer
lea edi,
mov ch,32
scan:
inc edx
mov cl,byte PTR
or cl,cl
jz finish
cmp cl,32
je scan

inc eax
mov dword PTR ,edi
add esi,4
restart:
mov cl,byte PTR
or cl,cl
jz finish
cmp cl,ch
je end_of_line
cmp cl,34
jne @f
xor ch,32
jmp next_char
@@:
mov byte PTR ,cl
inc edi
next_char:
inc edx
jmp restart
end_of_line:
mov byte PTR ,0
inc edi
jmp scan
finish:
ret

ParseCmdLine ENDP
Attachments:
Posted on 2006-04-05 12:10:26 by Vortex
Here's a parser I did a few years ago, it parses the command line (or if there is not command line it parses text up to a 'quit' command.


;=========================================================
; String Parser Example (parser.asm)
; Bryant Keller <bkeller@codegurus.org>
; visit my homepage at http://codefoo.ath.cx/
;=========================================================
; Searches for a collection of strings and executes an
; associated procedure when the string is found.
;
; Now, if string is not passed on the command line, the
; program cycles a prompt for input until the word quit,
; case sensitive, appears and causes an exit.
;
; The ; character now signifies the end of a line, hence
; causing the parser to ignore following commands. This,
; therefore, makes ; act as a comment signifier.
;-- -------------------------------------------------------

.386
.model flat, stdcall
option casemap: none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

parser proto :dword
process_word proto :dword
command1 proto
command2 proto
command3 proto
command4 proto
command5 proto

table_item struct
istring dd ? ; address of string
ilength dd ? ; length of string
ilabel dd ? ; address of procedure
table_item ends

.data
szc1 db "command1",0 ; first command
szc2 db "command2",0 ; second command
szc3 db "command3",0 ; third command
szc4 db "command4",0 ; fourth command
szc5 db "command5",0 ; fifth command

; command/proc lookup table
itable table_item < szc1, 8, command1 > ; associate the first command with the command1 procedure
table_item < szc2, 8, command2 > ; associate the second command with the command2 procedure
table_item < szc3, 8, command3 > ; associate the third command with the command3 procedure
table_item < szc4, 8, command4 > ; associate the fourth command with the command4 procedure
table_item < szc5, 8, command5 > ; associate the fifth command with the command5 procedure
table_item < 0, 0, 0 > ; blank terminator required by process_word

; other strings used in application
sztitle db "parser_test",0
c1string db "Command 1",13,10,0
c2string db "Command 2",13,10,0
c3string db "Command 3",13,10,0
c4string db "Command 4",13,10,0
c5string db "Command 5",13,10,0

prompt db "-",0
command_line db 2025 dup(0)

.data?
dwlen dd ?

.code
application proc
call
@@: inc eax
cmp byte ptr , 0Dh
je @f
cmp byte ptr , 00h
je @f
cmp byte ptr , 0Ah
je @f
cmp byte ptr , ' '
jne @b
inc eax
lea edx, command_line
push edx
push eax
push edx
call
call
cmp eax, 00h
jne @@parse_command_line

@@: lea edx, command_line
lea ebx, prompt
push 2024
push edx
push ebx
push 2025
push edx
call
call
call

lea edx, command_line
        cmp dword ptr , 'tiuq'
        je @@finished

@@parse_it:
push edx
call
jmp @b

@@parse_command_line:
lea edx, command_line
push edx
call
cmp eax, 00h
jne @@finished
jmp @f
lperr:
BYTE "Parsing failed!",13,10,0
@@: push lperr
call

@@finished:
push NULL
call
xor eax, eax
ret
application endp

parser proc uses edi esi string:dword
local pcheap:DWORD
local lplast:DWORD
local szword:DWORD
local strlen:DWORD

push string
call
cmp eax, 00h
jne @f
xor eax, eax
ret
@@: mov strlen, eax

call
cmp eax, 00h
jne @f
xor eax, eax
ret
@@: mov pcheap, eax

push strlen
push HEAP_ZERO_MEMORY
push pcheap
call
cmp eax, 00h
jne @f
xor eax, eax
ret
@@: mov szword, eax

mov edi, szword
mov esi, string
mov ecx, strlen
mov lplast, edi

@@continue:
cmp ecx, 00h
je @@end_of_string
mov al, byte ptr
cmp al, 0Dh
je @@end_of_string
cmp al, 3Bh
je @@end_of_string
cmp al, 00h
je @@end_of_string
cmp al, 20h
je @@end_of_word
cmp al, 09h
je @@end_of_word

@@add_char:
mov byte ptr , al
dec ecx
inc esi
inc edi
xor eax, eax
jmp @@continue

@@end_of_word:
mov byte ptr , 00h
push lplast
call
dec ecx
inc esi
inc edi
xor eax, eax
mov lplast, edi
jmp @@continue

@@end_of_string:
mov byte ptr , 00h
push lplast
call

push szword
push NULL
push pcheap
call
cmp eax, 00h
jne @f
xor eax, eax
ret
@@: mov eax, 1
ret
parser endp

process_word proc uses esi edi szword:dword
local wrdlen:DWORD

pushad

push szword
call
cmp eax, 00h
jne @f
xor eax, eax
ret
@@: mov wrdlen, eax

lea ebx, itable
assume ebx:ptr table_item

@@continue:
mov eax, .ilength
cmp eax, 00h
je @@unknown_word

mov eax, .ilength
cmp wrdlen, eax
jne @@do_next

mov esi, .istring
mov edi, szword
mov ecx, wrdlen

@@: mov al, byte ptr
cmp al, byte ptr
jne @@do_next
cmp ecx, 00h
je @f
inc esi
inc edi
dec ecx
jmp @b
@@: call .ilabel

assume ebx:nothing
popad

mov eax, 1
ret

@@do_next:
add ebx, 04h
jmp @@continue

@@unknown_word:

assume ebx:nothing
popad

mov eax, 1
ret
process_word endp

command1 proc
lea ebx, c1string
push ebx
call
ret
command1 endp

command2 proc
lea ebx, c2string
push ebx
call
ret
command2 endp

command3 proc
lea ebx, c3string
push ebx
call
ret
command3 endp

command4 proc
lea ebx, c4string
push ebx
call
ret
command4 endp

command5 proc
lea ebx, c5string
push ebx
call
ret
command5 endp

end application
Posted on 2006-04-05 12:33:14 by Synfire
Hi guys. Seeing your own (?) routines, I'd like to ask what is the point of writing something like a command-line parser, when you can use 2 WinAPI functions to parse it? Do you know something I dont? :P Are these Windows functions bugged, ot something? :|
Posted on 2006-04-05 14:08:19 by ti_mo_n
CommandLineToArgW is, afaik, NT-only - and not everybody wants to do unicode anyway :)

GetCL and friends from the masm32 libraries are a bad choice, since there's a 128 (or was it 160?) byte buffer overflow limit.

I wrote a cute little parser that does things that would be hard to do in a HLL :). It doesn't overwrite the original buffer, and tries to be efficient about it's memory allocations. It's limited to... *shrug*... some 10-100 thousand of parameters, because it uses the stack for {ofs,len} pairs while scanning.
Attachments:
Posted on 2006-04-05 15:56:07 by f0dder