Hi, guys.
I really need help on parsing the command line into something like the C-style argv/argc. It looks different depending on which Win version you use, I'm going crazy here!
Please don't direct me to some lib functions. Mainly because I don't use MASM, but also because I want to make it on my own (with your help, of course :grin: )
/Leif
Posted on 2004-01-24 16:29:01 by Hugin
The GetCL function from the masm32 library is about the best I have seen. It is both in the lib and has the source code. I have translated it to GoAsm syntax for my programs.
Posted on 2004-01-24 17:59:14 by donkey
Found it in your VersionInfo.Asm.
GetCL... it's a huge amount of code for such a simple thing. But it gave me somewhere to start. Thanks. :)

/Leif
Posted on 2004-01-24 19:41:10 by Hugin
what is your assembler(or compiler) related to this post?
Posted on 2004-01-24 21:54:28 by cakmak

Found it in your VersionInfo.Asm.
GetCL... it's a huge amount of code for such a simple thing. But it gave me somewhere to start. Thanks. :)

/Leif


The source is long true, but if you run my Measure macro on it you will find that it compiles to only 270 bytes ;)
Posted on 2004-01-24 22:10:35 by donkey
argc	dd	?

argv dd 16 dup (?)

ProcessCmdLine proc
push edi
push esi
push ebx
call GetCommandLine
mov esi, eax
mov edi, OFFSET argv
xor ecx, ecx
xor ebx, ebx
xor edx, edx
@@cmss: dec esi
@@: inc esi
cmp byte ptr [esi], 20h
je @B
mov eax, esi
mov dl, 20h
cmp byte ptr [esi], 22h
sete cl
lea edx, [edx+ecx*2]
add eax, ecx
stosd
@@cm00: inc esi
cmp byte ptr [esi], 0
je @@cm01
cmp byte ptr [esi], dl
jne @@cm00
movzx eax, byte ptr [esi+1]
mov byte ptr [esi], 0
add esi, ecx
inc esi
test eax, eax
jz @@cm01
cmp byte ptr [esi], 0
je @@cm01
inc [argc]
jmp @@cmss
@@cm01: pop ebx
pop esi
pop edi
inc [argc]
ret
ProcessCmdLine endp
Posted on 2004-01-24 23:04:08 by comrade
Hugin,

The GetCL procedure is reasonably big because it handles quoted text in the command line for things like multiple words like you get with long file names and directory names.

If you need an argv argc style where you don't need to handle quoted text, you should be able to make it a fair bit shorter.

I have a design that I have not bothered to publish yet that is a lot faster but it requires a 256 character table which would make it a fair bit bigger again so its probably not worth the effort.

The most unreliable part of any command line algo is to reliably determine the end of the starting file name and where the command tail starts.

Regards,

http://www.asmcommunity.net/board/cryptmail.php?tauntspiders=in.your.face@nomail.for.you&id=2f46ed9f24413347f14439b64bdc03fd
Posted on 2004-01-25 05:59:39 by hutch--
Here's a small program that parses the commandline and prints it out... Made it quite a while ago... it should handle quotes and things properly.



.386
.Model Flat, StdCall

include windows.inc
include kernel32.inc

includelib kernel32.lib
includelib msvcrt.lib

printf PROTO C :PTR BYTE, :VARARG

.data?
argc DWORD ?
argv DWORD 64 dup (?)

.code
start:
INVOKE GetCommandLine

mov [argv], eax
xor edx, edx
xor edi, edi

parseLoop:
inc eax
mov dl, [eax]
xor dl, ' '
.IF zero?
cmp BYTE PTR [eax+1], '"'
mov [eax], dl
.IF zero?
add eax, 2
mov [argv][edi*4][4], eax
inc edi

quoteLoop:
mov dl, [eax]
inc eax
xor dl, '"'
jnz quoteLoop

mov [eax-1], dl
mov dl, [eax]
test dl, dl
jz endParsing
.ENDIF

inc eax
mov [argv][edi*4][4], eax
inc edi
jmp parseLoop
.ENDIF
xor dl, ' '
jnz parseLoop

endParsing:
mov [argc], edi

inc edi

xor esi, esi

printLoop:
.data
formatString BYTE "%s",0Dh,0Ah,0
.code
INVOKE printf, ADDR formatString, [argv][esi*4]
inc esi

dec edi
jnz printLoop

ret
end start


The actual parsing part is quite compact, so perhaps it is to your liking (I just hope it works correctly ;))
Posted on 2004-01-25 06:06:25 by Henk-Jan
Here is another example using the C run-time DLL crtdll.dll



.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include ..\crtdll.inc
includelib \masm32\lib\kernel32.lib
includelib ..\crtdll.lib

.data
cmd db 'Command=%s',13,10,'P1=%s',13,10,'P2=%s',0
errmsg db 'Only two parameters!',0

.data?
argc dd ?
argv dd ?
env dd ?

.code
start:
invoke __GetMainArgs,ADDR argc,ADDR argv,ADDR env,0
cmp argc,3
jne error
mov edx,argv
invoke printf,ADDR cmd,dword ptr [edx],dword ptr [edx+4],dword ptr [edx+8]

@@:
invoke ExitProcess,0

error:
invoke printf,ADDR errmsg
jmp @b
end start


You need to type two command line parameters to run the example.

This example available from:

http://www.asmcommunity.net/board/index.php?topic=16191
Posted on 2004-01-25 09:15:42 by Vortex
Damn, this was kinda tricky. The quotes are sometimes around all arguments, sometimes around the program path only and sometimes around all but the program path. It all depends on which Win version you use and how you execute the program (double-click on an associated file, right-click-drag->open with, double-click exe). I got it working on WinXP and Win98, haven't tested the others yet.

Thanks, guys.:alright:

/Leif
Posted on 2004-01-25 09:19:40 by Hugin
Hi Hugin,

Just a little philosophy, the saying "there is no truth" isn't also a truth? It sounds a little like a paradox, no? :)
Posted on 2004-01-25 09:43:46 by Vortex
Glad to see that someone noticed my little message!:grin:
I've been asked the same question before. I'm not claiming that my statement is true - it's just my opinion. What I mean is that I believe that _everything_ is subjective. There's no good/evil, there's no 'correct' or 'moral' way of life. There is no 'right' or 'wrong'. The universe has no such values in itself. What's right for you may be wrong to me and vice versa.

A few examples (examples - they have nothing t do with real-life):
Maybe I like to eat meat, while you think that's wrong.
Maybe I like to masturbate, while you think that's wrong.
Maybe I'm a wife-beater, while you think that's wrong.
Maybe I'm a GreenPeace activist, while you think that's wrong.
Maybe you live by Moslem ethics, while I think that's wrong.
Maybe you sell alcohol to teenagers, while I think that's wrong
Maybe you're a homosexual, while I think that's wrong.
Maybe you're married to fifteen women at a time, while I think that's wrong.

I'm not gonna expand the subject further, but the basic idea is that no moral, ethics or culture is absoulutely 'right' while the others are absolutely 'wrong'. There is no absolute, objective truth about anything. But then again, this is just my opinion and it is not more 'right' than any other. Don't get me wrong, I know what I believe in, and can have arguments with others on the subject, but I acknowledge the fact that my beliefs has nothing to do with 'truth'.

So, to answer your question: No, the saying "there is no truth" is not a truth. It is my subjective opinion. Therefore, there is no paradox.

This was the first post where I actually answered instead of asking newbie questions. :grin:

/Leffe
Posted on 2004-01-25 13:04:33 by Hugin
Hugin,

Unrelated to your technical question, here is a bit of archaic analysis that solves the problem mentioned with value judgements.

When you use a universal quantifier "ALL" or "NONE" you have a problem when you try and apply it to the statement that uses it. It is an historical class inclusion paradox and the well known ones are the paradox of the liar, Russells paradox and in set theory, Grellings paradox.

The paradox of the liar is something like,

Everything I say is false.

"Everything" functions here as "ALL" which includes the statement made above which is in a formal sense, a contradiction. "False = not-false".

You solve the problem by using what you call the "existential" quantifier "SOME" which you can modify to ideas like MOST or FEW so you never get caught with the class inclusion paradox.

Regards,
http://www.asmcommunity.net/board/cryptmail.php?tauntspiders=in.your.face@nomail.for.you&id=2f46ed9f24413347f14439b64bdc03fd
Posted on 2004-01-25 19:16:08 by hutch--
All this for a question about parsing command line... Get a life
Posted on 2004-01-26 10:44:36 by comrade
The only things that matters inside this Universe is the ABSOLUTE TRUTH.
The rest is just useless crap aka "maya"

There is no such thing as an "relative" truth or "subjective" one, this "relative" concepts are just plain lies that we learn in our childhood when our soul is destroyed by the so called "education"...

And it takes many years and/or lifes ... just to restart and see the truth, most people never recover unfortunatelly... they just think they see/know the truth and because of this they never search anymore

Besides Hummans are the best liars in the universe and the most pathetic ones as they are mainly lie themselfs

Besides any life is completly useless withou knowing the truth...

How is that for a Command Line?
Posted on 2004-01-26 11:13:35 by BogdanOntanu
Hi Bogdan,

I share all your ideas about the truth and life. There are so much lies that it's sometimes really very hard to understand what's the thruth. Do you remember the scene in Matrix where Morpheus offered the blue and yellow colored pills to Neo? It was nice. :)

Hi Hugin,

I think there must be something esthetic in the universe because of the great harmony based on mathematical principles. So, the universe must have it's own value. Everything is based on the behavior of the energy.
Posted on 2004-01-26 12:47:31 by Vortex
blue & red pills actually ;)
Posted on 2004-01-26 12:59:47 by DaRetard