Skip to content

Commit

Permalink
Add commandline arguments splitting. (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mistuke authored Feb 18, 2017
1 parent a410b4e commit 2567e43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 25 additions & 2 deletions System/Win32/Console.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ module System.Win32.Console (
setConsoleOutputCP,
-- * Ctrl events
CtrlEvent, cTRL_C_EVENT, cTRL_BREAK_EVENT,
generateConsoleCtrlEvent
generateConsoleCtrlEvent,
-- * Command line
commandLineToArgv
) where

##include "windows_cconv.h"

import System.Win32.Types

import Foreign.C.Types (CInt(..))
import Foreign.C.String (withCWString, CWString)
import Foreign.Ptr (Ptr)
import Foreign.Storable (peek)
import Foreign.Marshal.Array (peekArray)
import Foreign.Marshal.Alloc (alloca)

foreign import WINDOWS_CCONV unsafe "windows.h GetConsoleCP"
getConsoleCP :: IO UINT

Expand Down Expand Up @@ -59,4 +68,18 @@ generateConsoleCtrlEvent e p
foreign import WINDOWS_CCONV safe "windows.h GenerateConsoleCtrlEvent"
c_GenerateConsoleCtrlEvent :: CtrlEvent -> DWORD -> IO BOOL

-- ToDo: lots more
foreign import WINDOWS_CCONV unsafe "Shellapi.h CommandLineToArgvW"
c_CommandLineToArgvW :: CWString -> Ptr CInt -> IO (Ptr CWString)

-- | This function can be used to parse commandline arguments and return
-- the split up arguments as elements in a list.
commandLineToArgv :: String -> IO [String]
commandLineToArgv [] = return []
commandLineToArgv arg =
do withCWString arg $ \c_arg -> do
alloca $ \c_size -> do
res <- c_CommandLineToArgvW c_arg c_size
size <- peek c_size
args <- peekArray (fromIntegral size) res
_ <- localFree res
mapM peekTString args
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for [`Win32` package](http://hackage.haskell.org/package/Win32)

## Unreleased GIT version

* Add `commandLineToArgv`

## 2.5.1.0 *Feb 2017*

* Add `withHandleToHANDLE` (originally found in the `ansi-terminal` library)
Expand Down

2 comments on commit 2567e43

@Mistuke
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Win32 Bindings for Haskell :: Win32 Continuous builds Build 31 is now running

@Mistuke
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Win32 Bindings for Haskell :: Win32 Continuous builds Build 31 outcome was SUCCESS
Summary: Running Build time: 00:07:03

Please sign in to comment.