The MASM Forum

Microsoft 64 bit MASM => MASM64 SDK => Topic started by: hutch-- on August 31, 2019, 01:08:38 AM

Title: New command line parser.
Post by: hutch-- on August 31, 2019, 01:08:38 AM
I have dabbled with this one for a while but wanted to avoid a tangled mess of allocations and de-allocations but have come up with a design that combined both library modules and a macro pair to keep it simple and tidy to use without it being a nightmare to clean up.

It is a space delimited parser that can handle single arguments or multiple arguments enclosed in double quotes so that you can pass long files names with spaces to a application that requires this type of support. This is the batch file used with this test piece, it is enclosed in the zip file below.

@echo off

p6 one   two  three     four  five    six  seven    eight

p6 "one" two "three 333" four "five 555" six "seven 777" eight

p6 one "two 222" three "four 444" five "six 666" seven "eight 888"

pause

Because of its design it should not be vulnerable to exploits that pass a command line via CreateProcess() that is far larger than the normal 260 character limit for non UNICODE paths and about the only limitation that I can find is the app that uses this code cannot have a file name that has spaces in it.

The short name is due to how I developed the idea. I started at p1 and and got the version I wanted at p6. It can also be done using 2 library modules but the macro system is simpler to use. This is the test code for the technique.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    USING r12,r13
    LOCAL parr  :QWORD
    LOCAL acnt  :QWORD

    SaveRegs

  ; --------------------------------------------
  ; get command line pointer array and arg count
  ; --------------------------------------------
    CmdBegin parr,acnt                              ; start here

  ; -----------------------------------
  ; loop through command tail arguments
  ; -----------------------------------
    mov r12, parr                                   ; load array pointer into r12
    mov r13, acnt                                   ; load argument count into r13
  lpst:
    conout QWORD PTR [r12],lf                       ; QWORD PTR for array member
    add r12, 8                                      ; add 8 to get next array member
    sub r13, 1                                      ; decrement the argument counter
    jnz lpst                                        ; loop back if not zero

    conout "arg count = ",str$(acnt),lf,lf          ; display arg count
  ; -----------------------------------

  ; ***********************************
  ; free cmd arg processing memory
  ; ***********************************
    CmdEnd                                          ; end here

    RestoreRegs
    .exit

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤