flat assembler
Message board for the users of flat assembler.

Index > Windows > Command line parsing

Author
Thread Post new topic Reply to topic
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Mar 2007, 17:22
Hi, i'm implementing some command line parsing to FASMLIB. I've come upon strange behavior of WinAPI regarding usage of quotes and their escaping:

behavior is decsribed here.

It seems to be some kind of all-compatible fix or something. Do you think i should follow this behavior, or do it differently?
Post 22 Mar 2007, 17:22
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Ehtyar



Joined: 26 Sep 2006
Posts: 51
Ehtyar 22 Mar 2007, 20:37
I recently began exploring windows command line parsing myself. It's certainly far more hassle to parse it yourself than to let windows do it. I would definitely suggest using CommandLineToArgvW over your own algo.

Ehtyar.
Post 22 Mar 2007, 20:37
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Mar 2007, 20:55
i can't:
MSDN wrote:
Minimum operating systems: Windows NT Workstation 3.5, Windows NT Server 3.5, Windows 2000 Professional, Windows 2000 Server, Windows XP, Windows Server 2003


also it's unicode based, i would have to make 2 extra conversions (not THAT a big problem but still...)
Post 22 Mar 2007, 20:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
asmfan



Joined: 11 Aug 2006
Posts: 392
Location: Russian
asmfan 22 Mar 2007, 21:13
My one-pass parser i've done some time ago (of course with paper and pencil for the first time)) based on theory of finite automats;)
;;It doesn't handle those aspects (multiple /) that vid posted link to.


Description:
Download
Filename: CL.rar
Filesize: 1.84 KB
Downloaded: 280 Time(s)


_________________
Any offers?
Post 22 Mar 2007, 21:13
View user's profile Send private message Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas 30 Mar 2007, 22:19
And I will contribute with my command line parser function. It doesn't understand "\" either, only quotes, its kinda incomplete, may have bugs and doesn't check for errors or lack of memory. Call this function at your own peril Twisted Evil !!!

It does have some features: returns dword aligned ASCIIZ strings in argv/argc format, and, as a special bonus, these strings are prefixed by a dword containing their lenght, like Pascal strings. I find this useful.


Description: Command line parser function
Download
Filename: cmdproc.zip
Filesize: 1.79 KB
Downloaded: 255 Time(s)

Post 30 Mar 2007, 22:19
View user's profile Send private message Reply with quote
hidden



Joined: 14 Feb 2007
Posts: 49
hidden 31 Mar 2007, 03:46
I also have written my command line parser. I don't like an idea to escape quotes by "\", I'm using double quotes to produce a single, like in assembler. And I don't pars all args in one call
Code:
proc GetArg, line, num, buf, len
        xchg    esi, [line]
        xchg    edi, [buf]
        xchg    ecx, [num]
        dec     [len]

        jecxz   .cur

 .lp1:  lodsb
        or      al, al
        jz      .ret
        cmp     al, '"'
        jne     .nq1

 .lp2:  lodsb
        or      al, al
        jz      .ret
        cmp     al, '"'
        jne     .lp2
        lodsb
        or      al, al
        jz      .ret
        cmp     al, '"'
        je      .lp2

 .nq1:  cmp     al, ' '
        jne     .lp1

 @@:    lodsb
        cmp     al, ' '
        je      @b
        dec     esi

        loop    .lp1

 .cur:  mov     ecx, [len]
 .lp3:  lodsb
        or      al, al
        jz      .end
 .chs:  cmp     al, ' '
        je      .end
        cmp     al, '"'
        jne     .nq2

 .gq2:  lodsb
        or      al, al
        jz      .ret
        cmp     al, '"'
        jne     .gq1

 .nq2:  stosb
        loop    .lp3

 .end:  mov     eax, [len]
        sub     eax, ecx
        jz      .ret
        mov     byte[edi], 0
        inc     edi
 .ret:  mov     esi, [line]
        mov     edi, [buf]
        mov     ecx, [num]
        ret

 .lp4:  lodsb
        or      al, al
        jz      .ret
        cmp     al, '"'
        jne     .gq1

        lodsb
        or      al, al
        jz      .ret
        cmp     al, '"'
        jne     .chs

 .gq1:  stosb
        loop    .lp4
        jmp     .end

 .err:  xor     eax, eax
        jmp     .ret
endp    
Post 31 Mar 2007, 03:46
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 02 Apr 2007, 07:52
you can also check function GetParameter in template there: http://board.flatassembler.net/topic.php?t=6663
Post 02 Apr 2007, 07:52
View user's profile Send private message Visit poster's website Reply with quote
weiss



Joined: 03 Jan 2006
Posts: 25
weiss 06 Apr 2007, 22:55
there is __getmainargs() in msvcrt.dll if anyone wants it already done for them.

Code:
proc start
    local stinfo    :STARTUPINFO
    local bWildCard :DWORD
    local pEnv      :DWORD
    local pArgv     :DWORD
    local nArgc     :DWORD

    mov   [bWildCard],FALSE
    ccall [__getmainargs],addr nArgc,addr pArgv,addr pEnv,[bWildCard],addr stinfo
    stdcall main,dword [nArgc],dword [pArgv]
    ccall [exit],0
endp

proc main uses esi ebx edi, argc:DWORD, argv:DWORD

   .if [argc] = 4
      ; do code
   .else
      stdcall usage,dword [argv]
   .endif
   ret
endp    


probably better to rewrite that..but thats the general idea
Post 06 Apr 2007, 22:55
View user's profile Send private message Reply with quote
weiss



Joined: 03 Jan 2006
Posts: 25
weiss 09 May 2007, 23:03
having problems assembling the above code to 64-bits..anybody figure out the errors i get.

Code:
format PE64 console 4.0

include 'include\win64w.inc'

entry start

section '.data' data readable writeable

    usage TCHAR 10,'Usage:%s <FILE>',10,0

section '.code' code readable executable

proc start
    local nArgc     :QWORD
    local pArgv     :QWORD
    local pEnv      :QWORD
    local bWildCard :QWORD
    local stinfo    :STARTUPINFO

    invoke __wgetmainargs,nArgc,pArgv,pEnv,[bWildCard],stinfo

    mov rbx,[pArgv]

    .if qword[nArgc] = 2
       invoke wprintf,usage,[rbx+8*0]
    .endif

    invoke exit,0
endp

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
    user32,'USER32.DLL',\
    msvcrt,'MSVCRT.DLL'

  include 'include\api\kernel32.inc'
  include 'include\api\user32.inc'

  import msvcrt,\
         __wgetmainargs,'__wgetmainargs',\
         exit,'exit',\
         wprintf,'wprintf'    



errors are..

Quote:
flat assembler version 1.67.21 (384470 kilobytes memory)
hello.asm [21]:
invoke __wgetmainargs,nArgc,pArgv,pEnv,[bWildCard],stinfo
include\macro/proc64.inc [5] invoke [0]:
{ common fastcall [proc],arg }
include\macro/proc64.inc [61] fastcall [53]:
mov rcx,arg
error: invalid value.
Post 09 May 2007, 23:03
View user's profile Send private message Reply with quote
Chewy509



Joined: 19 Jun 2003
Posts: 297
Location: Bris-vegas, Australia
Chewy509 11 May 2007, 00:11
I've attached the snippets for the cmd line parsing used within the b0 compiler, used to create a normal argc, argv type environment with ANSI strings (not Unicode strings).

The function of interest is SetArgCV();

Hope this helps anyone in the future.

Zip files contains both b0 and asm code. (Easy to read the b0 code).

PS. Target = Win x64.


Description:
Download
Filename: b0_cmd.zip
Filesize: 5.62 KB
Downloaded: 245 Time(s)

Post 11 May 2007, 00:11
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.