Cross-platform .hs files on Linux and Windows

I've been given a set of .hs files which contain the shebang line
#!/usr/bin/env runhaskell
and I would like them to work on Windows, but none of the Windows binaries seem
to be able to process them without a

Check this out: http://stackoverflow.com/questions/6818031/use-shebang-hashbang-in-windows-c... hth, L. On Mon, Apr 16, 2012 at 06:23:57PM +0000, Vinay Sajip wrote:
I've been given a set of .hs files which contain the shebang line
#!/usr/bin/env runhaskell
and I would like them to work on Windows, but none of the Windows binaries seem to be able to process them without a
:1:1: parse error on input `#!' These files came from a Linux machine, where they run without trouble. Is there any way I can get the Windows executables to run these files? I asked on IRC and it was suggested that I change the files to literate Haskell, but I'd rather have some way of having cross-platform operation which does not involve making changes to the scripts themselves. Is there something that can be done e.g. by using particular command line options or configuration settings?
Thanks & regards,
Vinay Sajip
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Lorenzo Bolla http://lbolla.info

Lorenzo Bolla
Check this out:
http://stackoverflow.com/questions/6818031/use-shebang-hashbang-in-windows-c...
Thanks, but that link just leads either to answers saying it can't be done, or information about using assoc and ftype to associate e.g. .hs files with runhaskell. That's not the crux of the problem: the crux is that runhaskell on Linux will run .hs files with a #!/usr/bin/env runhaskell (even though that's not a valid line in Haskell), whereas runhaskell.exe on Windows will try to parse that first line using the Haskell parser, even though it's not valid Haskell (the Linux version presumably skips that first line). Regards, Vinay Sajip

And thats expected given than the shebang is *nix specific.
On Tue, Apr 17, 2012 at 12:11 AM, Vinay Sajip
Lorenzo Bolla
writes: Check this out:
http://stackoverflow.com/questions/6818031/use-shebang-hashbang-in-windows-c...
Thanks, but that link just leads either to answers saying it can't be done, or information about using assoc and ftype to associate e.g. .hs files with runhaskell. That's not the crux of the problem: the crux is that runhaskell on Linux will run .hs files with a #!/usr/bin/env runhaskell (even though that's not a valid line in Haskell), whereas runhaskell.exe on Windows will try to parse that first line using the Haskell parser, even though it's not valid Haskell (the Linux version presumably skips that first line).
Regards,
Vinay Sajip
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Google+: https://plus.google.com/111881868112036203454

Marius Ghita
And thats expected given than the shebang is *nix specific.
Perhaps not unexpected, but not necessary either. I may be wrong, but it seems reasonable to assume there's a common code base for the Haskell Platform for Linux and Windows versions. Presumably there's code for the parser to the skip shebang line on Linux - there's no reason why it couldn't do the same on Windows. As it is, it's just an unhelpful impediment to having cross-platform scripts (unless there's a good reason for it - I couldn't think of one). Coincidentally, I am working on functionality which brings shebang line processing to Windows, primarily for Python scripts but it also works with other scripting languages (like Perl). That's not why I posted this, though - I was just given a set of scripts and want to use them on Windows without changing them just for this. Regards, Vinay Sajip

http://en.wikipedia.org/wiki/Shebang_%28Unix%29
when it occurs as the first two characters on the first line of a text
file. In this case, the program
loaderhttp://en.wikipedia.org/wiki/Loader_%28computing%29in
Unix-like http://en.wikipedia.org/wiki/Unix-like operating systems parses
the rest of the first line as an interpreter
directivehttp://en.wikipedia.org/wiki/Interpreter_directiveand
invokes the program specified after the character sequence with any
command line options specified as parameters. The name of the file being
executed is passed as the final argument.
As you can see this is not something an interpreter is supposed to do, the
responsability is delegated to the OS's program loader. You could emulate
this under Windows, but I suppose you would be using cygwin or something
similar.
On Tue, Apr 17, 2012 at 11:13 AM, Vinay Sajip
Marius Ghita
writes: And thats expected given than the shebang is *nix specific.
Perhaps not unexpected, but not necessary either. I may be wrong, but it seems reasonable to assume there's a common code base for the Haskell Platform for Linux and Windows versions. Presumably there's code for the parser to the skip shebang line on Linux - there's no reason why it couldn't do the same on Windows. As it is, it's just an unhelpful impediment to having cross-platform scripts (unless there's a good reason for it - I couldn't think of one).
Coincidentally, I am working on functionality which brings shebang line processing to Windows, primarily for Python scripts but it also works with other scripting languages (like Perl). That's not why I posted this, though - I was just given a set of scripts and want to use them on Windows without changing them just for this.
Regards,
Vinay Sajip
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Google+: https://plus.google.com/111881868112036203454

http://en.wikipedia.org/wiki/Shebang_%28Unix%29when it occurs as the first two characters on the first line of a text file. In this case, the program loader in Unix-like operating systems parses the rest of the first line as an interpreter
Marius Ghita
and invokes the program specified after the character sequence with any command line options specified as parameters. The name of the file being executed is passed as the final argument.
As you can see this is not something an interpreter is supposed to do, the responsability is delegated to the OS's program loader. You could emulate this under Windows, but I suppose you would be using cygwin or something similar.
Yes, but on Linux, if you have an executable script hello.hs like this: #!/usr/bin/env runhaskell main = putStrLn "Hello, world!" then when you invoke it as $ ./hello.hs the shell opens the file and looks for a shebang line, finds it, interprets it, and then calls e.g. /usr/bin/runhaskell hello.hs which then parses that file, skipping the shebang line, and produces the expected result. On Windows, even if we arrange for the same thing to be done at the OS level, the operation would fail, because the Windows version of runhaskell does not skip the shebang line, but tries to parse it as Haskell source. I'm not saying runhaskell needs to do anything more than skip a shebang line, on Windows, if it sees one. Otherwise, you can't have cross-platform scripts on Windows and Linux. Regards, Vinay Sajip

On Tue, 17 Apr 2012 13:06:45 +0200, Vinay Sajip
I'm not saying runhaskell needs to do anything more than skip a shebang line, on Windows, if it sees one. Otherwise, you can't have cross-platform scripts on Windows and Linux.
On my Windows XP system, the shebang line is ignored: HashBang.hs: ----✁--------- #!/usr/bin/env runhaskell main = print "Hello!" ----✃---------
runhaskell HashBang.hs "Hello!"
which runhaskell.exe C:\Program Files\Haskell Platform\2011.4.0.0\bin\runhaskell.exe
Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming --

Henk-Jan van Tuyl
On my Windows XP system, the shebang line is ignored:
HashBang.hs: ----✁--------- #!/usr/bin/env runhaskell
main = print "Hello!" ----✃---------
runhaskell HashBang.hs "Hello!"
Thank you for this information! I installed the platform on Windows XP (32-bit), and I get the same result as you. The machine where I was having the problems was a Windows 7 machine (64-bit), though I used the same Haskell Platform installer on both. Perhaps it's a Windows 7 issue, or a 32-bit vs. 64-bit issue. I'll try on a different Windows 7 machine. It's good to know that scripts are meant to be cross-platform on Linux and Windows. Regards, Vinay Sajip

Perhaps it's a Windows 7 issue, or a 32-bit vs. 64-bit issue. I'll try on a different Windows 7 machine.
Actually, I face-palmed when I found what the problem was. On the 64-bit machine, because I was short of disk space, I hadn't installed a proper editor, and used Notepad. Of course, Notepad sticks a UTF-8 BOM at the start of the file ... removing that sorted out the problem :-) Regards, Vinay Sajip

On Tue, 17 Apr 2012 22:56:23 +0200, Vinay Sajip
Actually, I face-palmed when I found what the problem was. On the 64-bit machine, because I was short of disk space, I hadn't installed a proper editor, and used Notepad. Of course, Notepad sticks a UTF-8 BOM at the start of the file ... removing that sorted out the problem :-)
But GHC should use/ignore a BOM! Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming --

Henk-Jan van Tuyl
But GHC should use/ignore a BOM!
Right, but at least there's an easy workaround. Can you reproduce this in your environment by prepending a BOM? If so, perhaps an issue should be logged. Regards, Vinay Sajip

On Wed, 18 Apr 2012 01:42:16 +0200, Vinay Sajip
Henk-Jan van Tuyl
writes: But GHC should use/ignore a BOM!
Right, but at least there's an easy workaround. Can you reproduce this in your environment by prepending a BOM? If so, perhaps an issue should be logged.
I've already tried that; if I use Notepad to convert the file to UTF-8, GHC does not accept the shebang. Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming --

Henk-Jan van Tuyl
I've already tried that; if I use Notepad to convert the file to UTF-8, GHC does not accept the shebang.
I've raised an issue: http://hackage.haskell.org/trac/ghc/ticket/6016 Thanks for your help with this. Regards, Vinay Sajip

On Wednesday 18 April 2012, 15:16:52, Vinay Sajip wrote:
Henk-Jan van Tuyl
writes: I've already tried that; if I use Notepad to convert the file to UTF-8, GHC does not accept the shebang.
That's by design. GHC treats a shebang line as a comment *if the shebang is the very first bytes of the file*. With a BOM, the shebang isn't the start of the file, so it's not treated as a comment. You might change the ticket to a feature request.

Daniel Fischer
That's by design. GHC treats a shebang line as a comment *if the shebang is the very first bytes of the file*. With a BOM, the shebang isn't the start of the file, so it's not treated as a comment.
You might change the ticket to a feature request.
I'll leave it to the Trac administrators to handle as they see fit, but I find it hard to believe this is "by design". Of course, tools like Notepad can be viewed as a little broken because they prepend the BOM, but that's behaviour that can't be completely avoided on Windows. It seems easy enough to check for a BOM and skip that, because the BOM is not strictly part of the file contents; it's just a marker indicating the encoding. The shebang line in the non-working example is the very first thing in the file's actual contents (e.g. as interpreted by Notepad). Of course, for UTF-16/UCS-2 encoded files (also common on Windows), wouldn't the compiler need to check for a BOM anyway? Regards, Vinay Sajip

On Thu, Apr 19, 2012 at 10:35, Vinay Sajip
Of course, for UTF-16/UCS-2 encoded files (also common on Windows), wouldn't the compiler need to check for a BOM anyway?
I don't think GHC makes any attempt to suport UTF16 source. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On Tue, Apr 17, 2012 at 07:06, Vinay Sajip
the shell opens the file and looks for a shebang line, finds it, interprets it,
The shell does not; the kernel does. (You *will* see it documented in some shells... as something they will try if the kernel-based one fails. Are you still running 2.9BSD?) -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (6)
-
Brandon Allbery
-
Daniel Fischer
-
Henk-Jan van Tuyl
-
Lorenzo Bolla
-
Marius Ghita
-
Vinay Sajip