import Control.Monad import Data.List import System.Console.Readline import System.Environment getShellCompl :: String -> IO [String] getShellCompl s | s /= "" && last s /= ' ' = do fl <- filenameCompletionFunction s c <- commandCompletionFunction s return $ sort . nub $ fl ++ c | otherwise = return [] commandCompletionFunction :: String -> IO [String] commandCompletionFunction str | '/' `elem` str = return [] | otherwise = do p <- getEnv "PATH" cl p where cl = liftM (nub . rmPath . concat) . mapM fCF . map addToPath . split ':' addToPath = flip (++) ("/" ++ str) fCF = filenameCompletionFunction rmPath :: [String] -> [String] rmPath s = map (reverse . fst . break (=='/') . reverse) s split :: Eq a => a -> [a] -> [[a]] split _ [] = [] split e l = f : split e (rest ls) where (f,ls) = span (/=e) l rest s | s == [] = [] | otherwise = tail s main = do a <- getArgs putStrLn =<< fmap (show . length) (getShellCompl $ a !! 0)