[Git][ghc/ghc][master] 4 commits: Allow spaces in GHCi :script file names

Marge Bot gitlab at gitlab.haskell.org
Thu May 21 16:16:20 UTC 2020



 Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
a8c27cf6 by Stefan Holdermans at 2020-05-21T12:16:08-04:00
Allow spaces in GHCi :script file names

This patch updates the user interface of GHCi so that file names passed
to the ':script' command may contain spaces escaped with a backslash.

For example:

  :script foo\ bar.script

The implementation uses a modified version of 'words' that does not
break on escaped spaces.

Fixes #18027.

- - - - -
82663959 by Stefan Holdermans at 2020-05-21T12:16:08-04:00
Add extra tests for GHCi :script syntax checks

The syntax for GHCi's ":script" command allows for only a single file
name to be passed as an argument. This patch adds a test for the cases
in which a file name is missing or multiple file names are passed.

Related to #T18027.

- - - - -
a0b79e1b by Stefan Holdermans at 2020-05-21T12:16:08-04:00
Allow GHCi :script file names in double quotes

This patch updates the user interface of GHCi so that file names passed
to the ':script' command can be wrapped in double quotes.

For example:

  :script "foo bar.script"

The implementation uses a modified version of 'words' that treats
character sequences enclosed in double quotes as single words.

Fixes #18027.

- - - - -
cf566330 by Stefan Holdermans at 2020-05-21T12:16:08-04:00
Update documentation for GHCi :script

This patch adds the fixes that allow for file names containing spaces to
be passed to GHCi's ':script' command to the release notes for 8.12 and
expands the user-guide documentation for ':script' by mentioning how
such file names can be passed.

Related to #18027.

- - - - -


10 changed files:

- docs/users_guide/8.12.1-notes.rst
- docs/users_guide/ghci.rst
- ghc/GHCi/UI.hs
- + testsuite/tests/ghci/should_fail/T18027a.script
- + testsuite/tests/ghci/should_fail/T18027a.stderr
- testsuite/tests/ghci/should_fail/all.T
- + testsuite/tests/ghci/should_run/T18027 SPACE IN FILE NAME.script
- + testsuite/tests/ghci/should_run/T18027.script
- + testsuite/tests/ghci/should_run/T18027.stdout
- testsuite/tests/ghci/should_run/all.T


Changes:

=====================================
docs/users_guide/8.12.1-notes.rst
=====================================
@@ -100,6 +100,9 @@ Compiler
 GHCi
 ~~~~
 
+- The ``:script`` command now allows for file names that contain spaces to
+  passed as arguments: either by enclosing the file names in double quotes or by
+  escaping spaces in file names with a backslash. (#18027)
 
 Runtime system
 ~~~~~~~~~~~~~~


=====================================
docs/users_guide/ghci.rst
=====================================
@@ -2695,9 +2695,11 @@ commonly used commands.
 
 .. ghci-cmd:: :script; [⟨n⟩] ⟨filename⟩
 
-    Executes the lines of a file as a series of GHCi commands. This
-    command is compatible with multiline statements as set by
-    :ghci-cmd:`:set +m`
+    Executes the lines of a file as a series of GHCi commands. The syntax for
+    file-name arguments respects shell quoting rules, i.e., file names
+    containing spaces can be enclosed in double quotes or with spaces escaped
+    with a backslash. This command is compatible with multiline statements as
+    set by :ghci-cmd:`:set +m`
 
 .. ghci-cmd:: :set; [⟨option⟩ ...]
 


=====================================
ghc/GHCi/UI.hs
=====================================
@@ -2263,10 +2263,26 @@ quit _ = return True
 
 scriptCmd :: String -> InputT GHCi ()
 scriptCmd ws = do
-  case words ws of
+  case words' ws of
     [s]    -> runScript s
     _      -> throwGhcException (CmdLineError "syntax:  :script <filename>")
 
+-- | A version of 'words' that treats sequences enclosed in double quotes as
+-- single words and that does not break on backslash-escaped spaces.
+-- E.g., 'words\' "\"lorem ipsum\" dolor"' and 'words\' "lorem\\ ipsum dolor"'
+-- yield '["lorem ipsum", "dolor"]'.
+-- Used to scan for file paths in 'scriptCmd'.
+words' :: String -> [String]
+words' s = case dropWhile isSpace s of
+  "" -> []
+  s'@('\"' : _) | [(w, s'')] <- reads s' -> w : words' s''
+  s' -> go id s'
+ where
+  go acc []                          = [acc []]
+  go acc ('\\' : c : cs) | isSpace c = go (acc . (c :)) cs
+  go acc (c : cs) | isSpace c = acc [] : words' cs
+                  | otherwise = go (acc . (c :)) cs
+
 runScript :: String    -- ^ filename
            -> InputT GHCi ()
 runScript filename = do


=====================================
testsuite/tests/ghci/should_fail/T18027a.script
=====================================
@@ -0,0 +1,2 @@
+:script
+:script one two


=====================================
testsuite/tests/ghci/should_fail/T18027a.stderr
=====================================
@@ -0,0 +1,2 @@
+syntax:  :script <filename>
+syntax:  :script <filename>


=====================================
testsuite/tests/ghci/should_fail/all.T
=====================================
@@ -4,3 +4,4 @@ test('T15055', normalise_version('ghc'), ghci_script, ['T15055.script'])
 test('T16013', [], ghci_script, ['T16013.script'])
 test('T16287', [], ghci_script, ['T16287.script'])
 test('T18052b', [], ghci_script, ['T18052b.script'])
+test('T18027a', [], ghci_script, ['T18027a.script'])


=====================================
testsuite/tests/ghci/should_run/T18027 SPACE IN FILE NAME.script
=====================================
@@ -0,0 +1 @@
+42


=====================================
testsuite/tests/ghci/should_run/T18027.script
=====================================
@@ -0,0 +1,2 @@
+:script T18027\ SPACE\ IN\ FILE\ NAME.script
+:script "T18027 SPACE IN FILE NAME.script"


=====================================
testsuite/tests/ghci/should_run/T18027.stdout
=====================================
@@ -0,0 +1,2 @@
+42
+42


=====================================
testsuite/tests/ghci/should_run/all.T
=====================================
@@ -64,3 +64,4 @@ test('T15633b',
 
 test('T16096', just_ghci, ghci_script, ['T16096.script'])
 test('T507', just_ghci, ghci_script, ['T507.script'])
+test('T18027', just_ghci, ghci_script, ['T18027.script'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/b7a6b2f4c690a9711339462114a538a85dcb7d83...cf5663300c3d8b8b3c7dc2cd0dce2c923ec68987

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/b7a6b2f4c690a9711339462114a538a85dcb7d83...cf5663300c3d8b8b3c7dc2cd0dce2c923ec68987
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200521/7d5ad72f/attachment-0001.html>


More information about the ghc-commits mailing list