flat assembler
Message board for the users of flat assembler.

Index > Windows > Command line parser

Author
Thread Post new topic Reply to topic
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 16 Sep 2004, 16:51
Hi friends,

Based on an algo coded by Jibz, here is a command line parser. It can handle literal double quote inside an argument.

The only parameter buffer should point a "buffer" of 512 bytes (=2*256) You are free to change this value.
Code:
proc ParseCmdLine,buffer
 push    esi
 push    edi
 invoke  GetCommandLine
@@:
   lea     edx,[eax-1]
 xor     eax,eax
     mov     esi,[buffer]
        lea     edi,[esi+256]
       mov     ch,32
scan:
  inc     edx
         mov     cl,byte [edx]
       or      cl,cl
       jz      finish
      cmp     cl,32
       je      scan
        inc     eax
 mov     [esi],edi
   add     esi,4
restart:
       mov     cl,byte [edx]
       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 [edi],cl
       inc     edi
next_char:
       inc     edx
 jmp     restart
end_of_line:
 mov     byte [edi],0
        inc     edi
 jmp     scan    
finish:
     pop     edi
 pop     esi
 return
endp
    


Description: Command line parser with example code
Download
Filename: Cmdline.zip
Filesize: 2.79 KB
Downloaded: 433 Time(s)


_________________
Code it... That's all...
Post 16 Sep 2004, 16:51
View user's profile Send private message Visit poster's website Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 16 Sep 2004, 17:58
Hi Vortex,
Nice piece of code. I have also similar snippet that handles command line in my programs. But my code also checks if a command line ends with LR, or LF. It isn't needed in Windows, is it? It would be good if I could remve unnceessary code.... Also, can arguments be separated with tabs? My handler also checks that.
Post 16 Sep 2004, 17:58
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 16 Sep 2004, 18:25
Hi Decard,

Thanks for your kind words. Did you check my example application "test.exe" in the attachment? This algo is designed to handle command line parameters typed in a console box, so it doesn't check for CR+LF or tab spaces.

_________________
Code it... That's all...
Post 16 Sep 2004, 18:25
View user's profile Send private message Visit poster's website Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 16 Sep 2004, 19:03
Hi,
I didn't downloaded your code then (done it right now). So, what you have written means that generally in Windows I don't need to check for CR+LF, or there could be a situation when a command line can end with this bytes?
Post 16 Sep 2004, 19:03
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 16 Sep 2004, 19:37
Hi Decard,

Just as you said, there is no need to check for CR+LF as you use the function GetCommandLine to get the parameters:
Quote:

The GetCommandLine function returns a pointer to the command-line string for the current process.

LPTSTR GetCommandLine(VOID)

Parameters

This function has no parameters.

Return Values

The return value is a pointer to the command-line string for the current process.


The value returned by GetCommandLine always points a NULL terminated string, so there is no reason to check CR+LF and tab spaces.

_________________
Code it... That's all...
Post 16 Sep 2004, 19:37
View user's profile Send private message Visit poster's website Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner 18 Sep 2004, 14:08
i just thinking to write my own piece Smile and i saw ur parser here,
just downloaded it, haven't play with it :p

sincerely,
vbVeryBeginner
Post 18 Sep 2004, 14:08
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 19 Sep 2004, 08:26
Hi vbVeryBeginner,

It's a good idea to write your own parser as it's a good programming exercise. Have a look also at the other asm forums, you can find various command line parser algos.

_________________
Code it... That's all...
Post 19 Sep 2004, 08:26
View user's profile Send private message Visit poster's website Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner 20 Sep 2004, 17:08

Code:

; +------------+
; | DATA GROUP |
; +------------+
;       cmdP            = pointer for args address storage
;       cmdArgExpect    = expected argument count
;       cmdArg          = counter for marked args
;       cmdLen          = receive or allow number of char from arg
;       cmdBuf          = buffer area for argument to be copied from GetCommandLine *should be enough
;
;       cmdf1 to cmdf3  = (D) debug wsprintf string
;       cmdDbgBuf       = (D) buffer area to store output debug message
;       _cmdArg         = coz those call in debug will modify our register, so we need to use variable
;       _cmdPlus        = add 4 per times for each looping
;       _cmdCount       = to specify which argument for user
;
; +---------------------+
; | IMPORT GROUP        |
; +---------------------+
;       KERNEL32        GetCommandLine
;                       (D) lstrlen
;
proc cmd_parse
                push esi                                ;preserve
                push edi                                ;preserve
                mov  ebx,cmdP                           ;ebx have the address for cmdP
                mov  edx,[cmdArgExpect]                 ;edx have the value for cmdArgExpect
        invoke  GetCommandLine
        
        parse_copy_start:
                mov esi,eax                             ;esi have the address from GetCommandLine
                mov edi,cmdBuf                          ;edi have the address for cmdBuf
                xor ecx,ecx                             ;clear ecx
        parse_copy_run:
                mov al,[esi]                            ;al have the byte value of esi
                inc esi                                 ;increase esi address
                cmp al,0                                ;cmp al with 0
                je  parse_copy_end                      ;assume last char found and end proc
                cmp al,0x20                             ;cmp with spacebar
                jne @f                                  ;if not, skip the marking process
                
                parse_mark:
                        cmp [cmdArg],edx                ;cmp cmdArgExpect with current marked cmdArg
                        je  parse_copy_end              ;if equal that means our job finish
                        inc edi
                        mov [ebx],edi                   ;copy esi address to cmdP
                        dec edi
                        add ebx,4                       ;increase ebx for next store mark address
                        inc [cmdArg]                    ;increase marked cmdArg
                        mov al,0                        ;turn spacebar into zero for string usage later
                        jmp @f
        @@:
                mov [edi],al                            ;cmdBuf fill with char from esi
                inc edi                                 ;prepare edi for next store char address
                inc ecx                                 ;our char counter
                jmp parse_copy_run                      ;run again until we find char = 0
        parse_copy_end:
                mov [cmdLen],ecx                        ;store ecx counter value to cmdLen

                pop esi                                 ;pop them so that they as as what
                pop edi                                 ;when we got them
        return
endp

proc cmd_debug

        cinvoke wsprintf,cmdDbgBuf,cmdf1,[cmdLen]
        stdcall stdout,cmdDbgBuf
        cinvoke wsprintf,cmdDbgBuf,cmdf2,[cmdArg]
        stdcall stdout,cmdDbgBuf
                mov eax,[cmdArg]
                mov [_cmdArg],eax
                
        @@:
                cmp [_cmdArg],0
                je  @f
                mov eax,cmdP
                add eax,[_cmdPlus]

                        cinvoke wsprintf,cmdDbgBuf,cmdf3,[_cmdCount],eax,[eax]
                        stdcall stdout,cmdDbgBuf

                dec [_cmdArg]
                inc [_cmdCount]
                add [_cmdPlus],4
                jmp @b
        @@:
        return

endp
    


hopefully it could be another reference for people, try download the file and play with it :p


Description: version 0.0.2 (feel free to use it)
Download
Filename: cmd_parser.rar
Filesize: 3.25 KB
Downloaded: 464 Time(s)



Last edited by vbVeryBeginner on 21 Sep 2004, 03:56; edited 2 times in total
Post 20 Sep 2004, 17:08
View user's profile Send private message Visit poster's website Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner 20 Sep 2004, 17:12
i don't know if there is any resemble code outthere,
but i swear (do i need that) this is 100% from my mind :p

not good enough, i should be able to skip the
dec
inc

sincerely,
vbVeryBeginner d(zz`)b
Post 20 Sep 2004, 17:12
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 20 Sep 2004, 17:49
No problem vbVeryBeginner, keep going on.

_________________
Code it... That's all...
Post 20 Sep 2004, 17:49
View user's profile Send private message Visit poster's website Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner 21 Sep 2004, 03:48
Code:
proc cmd_argv_count,argvP,argvSend
                mov eax,[argvP]                         ;eax contained our arg address
        @@:
                xor ebx,ebx                             ;clear ebx coz we need to use bl only
                mov bl,[eax]                            ;store a byte from eax
                cmp bl,0                                ;compare the byte with zero
                je  @f                                  ;job finish
                add [cmdArgv],ebx                       ;add to our cmdArgv
                inc eax                                 ;for next byte
                jmp @b
        @@:
        call    [argvSend]
        
        return  
endp
    


this proc will enable the parser to support switch

eg.

urconsole.exe -h myfile.txt
:p

hope all enjoy :p
and i just updated the version to 0.0.2, which u can download from the above link

http://board.flatassembler.net/download.php?id=1072
Post 21 Sep 2004, 03:48
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.