Parsing command lines

Hello all, is there a function that can safely split a command line into a FilePath to the executable and its parameters? I couldn't manage to find one. If not, what are the pitfalls in creating a cross-platform solution to this problem? Can I assume that the first space not within double quotes and not preceded by an unescaped backslash marks the end of the executable path, or is that too naive (or completely wrong)? Gergely -- http://www.fastmail.fm - A fast, anti-spam email service.

On Monday 25 of May 2009 18:57:28 Patai Gergely wrote:
Hello all,
is there a function that can safely split a command line into a FilePath to the executable and its parameters? I couldn't manage to find one. If not, what are the pitfalls in creating a cross-platform solution to this problem? Can I assume that the first space not within double quotes and not preceded by an unescaped backslash marks the end of the executable path, or is that too naive (or completely wrong)?
Gergely
There getArgs and getProgName in System module. If you want parse this parameters there is System.Console.GetOpt. All the splitting, escaping and quotes interpretaion is done by shell. All what get program when starts is list of strings. Program name is actually arbitrary string. (At least on unices, it may work a bit differently on windows). -- Khudyakov Alexey

There getArgs and getProgName in System module. If you want parse this parameters there is System.Console.GetOpt. Those will only tell me how my program was called. I want to work with command lines as data within my program, which is a completely different thing.
All the splitting, escaping and quotes interpretaion is done by shell. All what get program when starts is list of strings. Program name is actually arbitrary string. (At least on unices, it may work a bit differently on windows). That's fine, but I'll see the commands yet to be passed to the shell as strings, and I'd like to know where to split them.
Gergely -- http://www.fastmail.fm - The professional email service

On Mon, 2009-05-25 at 17:45 +0200, Patai Gergely wrote:
There getArgs and getProgName in System module. If you want parse this parameters there is System.Console.GetOpt. Those will only tell me how my program was called. I want to work with command lines as data within my program, which is a completely different thing.
All the splitting, escaping and quotes interpretaion is done by shell. All what get program when starts is list of strings. Program name is actually arbitrary string. (At least on unices, it may work a bit differently on windows).
That's fine, but I'll see the commands yet to be passed to the shell as strings, and I'd like to know where to split them.
Trying to accurately emulate how the shell expands a command line string into program argument strings is not easy. I don't know of any existing Haskell library to do this but POSIX specifies a C function wordexp() which does shell command line expansion. This might be more than you want however as it also expands *.hs wildcards, arithmetic, quote removal, ~user paths, $env vars and (optionally) $(command) substitution. Duncan

which does shell command line expansion. This might be more than you want however as it also expands *.hs wildcards, arithmetic, quote removal, ~user paths, $env vars and (optionally) $(command) substitution. I don't think I'll have to deal with very complicated cases, just more or less static commands, maybe with ~ in the path. I guess I'll just go with a naive solution first, and expand it when needed.
Gergely -- http://www.fastmail.fm - Access all of your messages and folders wherever you are
participants (3)
-
Duncan Coutts
-
Khudyakov Alexey
-
Patai Gergely