Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell runCommand in Windows

As a simple exercise, I'm trying to change the prompt in Win7's Command Prompt window with the following little program:

module Main where
 import System.Environment
 import System.Process 

 p :: String -> String 
 p name = "Prompt " ++ name ++ "\r\n"

 main :: IO ()
 main = do
     putStrLn ("Give me a name:")
     name <- getLine
     putStrLn (p name)
     pid <- runCommand $ p name

Although it runs fine in the Command Prompt window, it does not actually change the prompt. Entering the same command on the command line by hand does change it. When I use "system" (which returns an exit code) rather than "runCommand" (which just returns a pid) it gives "ExitSuccess", but still doesn't change the prompt.

like image 515
user3150422 Avatar asked Sep 23 '26 13:09

user3150422


1 Answers

This isn't a Haskell issue per-se, just that you can't easily write a program in any language that will change the environment of its parent process.

In your scenario the prompt is controlled by the PROMPT environment variable, and the "parent process" is the Command Prompt (cmd.exe) that you launched your Haskell program from.

I would suggest that instead of trying to alter the parent cmd.exe process, you spawn a new cmd.exe (also with runCommand) after you have changed the prompt. You should wait for this process to finish with waitForProcess on the PID returned from runCommand, otherwise your Haskell program will exit with the child shell still running.

You will probably also have to switch from running the Prompt command to actually changing the PROMPT environment variable directly in your Haskell process, because what actually happens when you run the Prompt command as above is that a new cmd.exe is started just to run that command, so the environment change is immediately thrown away.

You can edit the environment with the setenv package.

like image 192
GS - Apologise to Monica Avatar answered Sep 26 '26 06:09

GS - Apologise to Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!