[Pharo-project] image format with shebang
how hard would it be to change the image file to include a shebang on the first line? for instance: #!/usr/bin/env pharo -headless like that images would serve as "almost" script files? I image something like everything until the first 0 or newline byte is ignored when loading the image...
On 13 Oct 2012, at 20:44, Camillo Bruni wrote:
how hard would it be to change the image file to include a shebang on the first line?
for instance:
#!/usr/bin/env pharo -headless
like that images would serve as "almost" script files? I image something like everything until the first 0 or newline byte is ignored when loading the image...
It is supported, see for instance: http://marc.info/?l=squeak-vm-dev&m=124049248310307&w=2 -- Stefan Marr Software Languages Lab Vrije Universiteit Brussel Pleinlaan 2 / B-1050 Brussels / Belgium http://soft.vub.ac.be/~smarr Phone: +32 2 629 2974 Fax: +32 2 629 3525
On 2012-10-13, at 22:10, Stefan Marr <smalltalk@stefan-marr.de> wrote:
On 13 Oct 2012, at 20:44, Camillo Bruni wrote:
how hard would it be to change the image file to include a shebang on the first line?
for instance:
#!/usr/bin/env pharo -headless
like that images would serve as "almost" script files? I image something like everything until the first 0 or newline byte is ignored when loading the image...
It is supported, see for instance: http://marc.info/?l=squeak-vm-dev&m=124049248310307&w=2
awesome! strange that nobody promoted that so far... well I guess yet another of these hidden treasures ;)
On Sat, Oct 13, 2012 at 10:51 PM, Camillo Bruni <camillobruni@gmail.com> wrote:
It is supported, see for instance: http://marc.info/?l=squeak-vm-dev&m=124049248310307&w=2 awesome! strange that nobody promoted that so far... well I guess yet another of these hidden treasures ;)
so, how can we have all images start with that line? -- Damien Cassou http://damiencassou.seasidehosting.st "Success is the ability to go from one failure to another without losing enthusiasm." Winston Churchill
On 2012-10-16, at 14:49, Damien Cassou <damien.cassou@gmail.com> wrote:
On Sat, Oct 13, 2012 at 10:51 PM, Camillo Bruni <camillobruni@gmail.com> wrote:
It is supported, see for instance: http://marc.info/?l=squeak-vm-dev&m=124049248310307&w=2 awesome! strange that nobody promoted that so far... well I guess yet another of these hidden treasures ;)
so, how can we have all images start with that line?
we have to change the VM, yay! :/, to add a language side-hook: https://code.google.com/p/cog/issues/detail?id=101 so once I have a bit of time, I'll change the VM to always include this header. Load support for such images exist since the early days apparently. I will simply add a #header argument to the #snapshot method, that way the image can pass an arbitrary header to the VM on image save. best cami
I understand that it would be nice eventually to have images saved like that automatically. It is still a pity that the VM has to change for that ;-) But as far as I understood, the first 512 bytes are skipped today as well, so if you (or any program) does a binary edit of the image header, it would work, no ? On 16 Oct 2012, at 14:58, Camillo Bruni <camillobruni@gmail.com> wrote:
On 2012-10-16, at 14:49, Damien Cassou <damien.cassou@gmail.com> wrote:
On Sat, Oct 13, 2012 at 10:51 PM, Camillo Bruni <camillobruni@gmail.com> wrote:
It is supported, see for instance: http://marc.info/?l=squeak-vm-dev&m=124049248310307&w=2 awesome! strange that nobody promoted that so far... well I guess yet another of these hidden treasures ;)
so, how can we have all images start with that line?
we have to change the VM, yay! :/, to add a language side-hook:
https://code.google.com/p/cog/issues/detail?id=101
so once I have a bit of time, I'll change the VM to always include this header. Load support for such images exist since the early days apparently.
I will simply add a #header argument to the #snapshot method, that way the image can pass an arbitrary header to the VM on image save.
best cami
On 2012-10-16, at 15:08, Sven Van Caekenberghe <sven@stfx.eu> wrote:
I understand that it would be nice eventually to have images saved like that automatically.
It is still a pity that the VM has to change for that ;-)
But as far as I understood, the first 512 bytes are skipped today as well, so if you (or any program) does a binary edit of the image header, it would work, no ?
exactly :) I tried it and it works out of the box... just that the header is lost on image-save so far...
On 16 Oct 2012, at 15:17, Camillo Bruni <camillobruni@gmail.com> wrote:
On 2012-10-16, at 15:08, Sven Van Caekenberghe <sven@stfx.eu> wrote:
I understand that it would be nice eventually to have images saved like that automatically.
It is still a pity that the VM has to change for that ;-)
But as far as I understood, the first 512 bytes are skipped today as well, so if you (or any program) does a binary edit of the image header, it would work, no ?
exactly :) I tried it and it works out of the box... just that the header is lost on image-save so far...
Then maybe you should share the secret ? It would not be more than a couple of linux commands, would it ;-)
On 2012-10-16, at 15:30, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 16 Oct 2012, at 15:17, Camillo Bruni <camillobruni@gmail.com> wrote:
On 2012-10-16, at 15:08, Sven Van Caekenberghe <sven@stfx.eu> wrote:
I understand that it would be nice eventually to have images saved like that automatically.
It is still a pity that the VM has to change for that ;-)
But as far as I understood, the first 512 bytes are skipped today as well, so if you (or any program) does a binary edit of the image header, it would work, no ?
exactly :) I tried it and it works out of the box... just that the header is lost on image-save so far...
Then maybe you should share the secret ?
It would not be more than a couple of linux commands, would it ;-)
I vim'ed the start of the file to include #!/usr/bin/env cogVM -headless and manually padded it ti 512bytes, so no magic so far :P
On Tue, Oct 16, 2012 at 03:08:45PM +0200, Sven Van Caekenberghe wrote:
I understand that it would be nice eventually to have images saved like that automatically.
It is still a pity that the VM has to change for that ;-)
If you don't want to do it in the VM, you can do it with a shell script. Just make a 512 byte file that starts with the shebang line that you want to use, prepend it to the saved image file (with /bin/cat) and set file mode executable (chmod). You could also do this from a method in the image if you prefer, although chmod would require FFI or OSProcess. Dave
But as far as I understood, the first 512 bytes are skipped today as well, so if you (or any program) does a binary edit of the image header, it would work, no ?
On 16 Oct 2012, at 14:58, Camillo Bruni <camillobruni@gmail.com> wrote:
On 2012-10-16, at 14:49, Damien Cassou <damien.cassou@gmail.com> wrote:
On Sat, Oct 13, 2012 at 10:51 PM, Camillo Bruni <camillobruni@gmail.com> wrote:
It is supported, see for instance: http://marc.info/?l=squeak-vm-dev&m=124049248310307&w=2 awesome! strange that nobody promoted that so far... well I guess yet another of these hidden treasures ;)
so, how can we have all images start with that line?
we have to change the VM, yay! :/, to add a language side-hook:
https://code.google.com/p/cog/issues/detail?id=101
so once I have a bit of time, I'll change the VM to always include this header. Load support for such images exist since the early days apparently.
I will simply add a #header argument to the #snapshot method, that way the image can pass an arbitrary header to the VM on image save.
best cami
On 16 Oct 2012, at 16:13, David T. Lewis <lewis@mail.msen.com> wrote:
If you don't want to do it in the VM, you can do it with a shell script. Just make a 512 byte file that starts with the shebang line that you want to use, prepend it to the saved image file (with /bin/cat) and set file mode executable (chmod).
Well, I tried on my Ubuntu server, and I could't get it to work. I have a Pharo 2.0 image and a little test script called run.st $ cat run.st NonInteractiveTranscript stdout install. ! | webSocket | webSocket := ZnWebSocket to: 'ws://localhost:8080/ws-echo'. [ webSocket sendMessage: 'Pharo Smalltalk using Zinc WebSockets'. Transcript show: webSocket readMessage; cr ] ensure: [ webSocket close ]. ! Smalltalk quitPrimitive. I wrote another small Smalltalk script to generate the header (just to be sure) $ cat write-header.st 'header.bin' asFileReference writeStreamDo: [ :stream | | line | line := '#!!/home/ubuntu/smalltalk/bin/CogVM -vm-display-null -vm-sound-null -plugins /home/ubuntu/smalltalk/bin/'. stream binary; nextPutAll: line asByteArray; nextPut: 10; nextPutAll: (ByteArray new: 511 - line size) ]. ! Smalltalk quitPrimitive. Now I have a 512 byte header that looks like this $ hexdump -C header.bin 00000000 23 21 2f 68 6f 6d 65 2f 75 62 75 6e 74 75 2f 73 |#!/home/ubuntu/s| 00000010 6d 61 6c 6c 74 61 6c 6b 2f 62 69 6e 2f 43 6f 67 |malltalk/bin/Cog| 00000020 56 4d 20 2d 76 6d 2d 64 69 73 70 6c 61 79 2d 6e |VM -vm-display-n| 00000030 75 6c 6c 20 2d 76 6d 2d 73 6f 75 6e 64 2d 6e 75 |ull -vm-sound-nu| 00000040 6c 6c 20 2d 70 6c 75 67 69 6e 73 20 2f 68 6f 6d |ll -plugins /hom| 00000050 65 2f 75 62 75 6e 74 75 2f 73 6d 61 6c 6c 74 61 |e/ubuntu/smallta| 00000060 6c 6b 2f 62 69 6e 2f 0a 00 00 00 00 00 00 00 00 |lk/bin/.........| 00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000200 Then I concatenate the header and the working image cat header.bin zinc.image > magic.image and I copy the changes and make the image executable. The image still works $ /home/ubuntu/smalltalk/bin/CogVM -vm-display-null -vm-sound-null -plugins /home/ubuntu/smalltalk/bin/ magic.image run.st Pharo Smalltalk using Zinc WebSockets But not when run directly $ ./magic.image Segmentation fault (core dumped) Any hints ? Thx, Sven -- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
On Tue, Oct 16, 2012 at 05:32:57PM +0200, Sven Van Caekenberghe wrote:
On 16 Oct 2012, at 16:13, David T. Lewis <lewis@mail.msen.com> wrote:
If you don't want to do it in the VM, you can do it with a shell script. Just make a 512 byte file that starts with the shebang line that you want to use, prepend it to the saved image file (with /bin/cat) and set file mode executable (chmod).
Well, I tried on my Ubuntu server, and I could't get it to work.
I have a Pharo 2.0 image and a little test script called run.st
$ cat run.st NonInteractiveTranscript stdout install. ! | webSocket | webSocket := ZnWebSocket to: 'ws://localhost:8080/ws-echo'. [ webSocket sendMessage: 'Pharo Smalltalk using Zinc WebSockets'. Transcript show: webSocket readMessage; cr ] ensure: [ webSocket close ]. ! Smalltalk quitPrimitive.
I wrote another small Smalltalk script to generate the header (just to be sure)
$ cat write-header.st 'header.bin' asFileReference writeStreamDo: [ :stream | | line | line := '#!!/home/ubuntu/smalltalk/bin/CogVM -vm-display-null -vm-sound-null -plugins /home/ubuntu/smalltalk/bin/'. stream binary; nextPutAll: line asByteArray; nextPut: 10; nextPutAll: (ByteArray new: 511 - line size) ]. ! Smalltalk quitPrimitive.
Now I have a 512 byte header that looks like this
$ hexdump -C header.bin 00000000 23 21 2f 68 6f 6d 65 2f 75 62 75 6e 74 75 2f 73 |#!/home/ubuntu/s| 00000010 6d 61 6c 6c 74 61 6c 6b 2f 62 69 6e 2f 43 6f 67 |malltalk/bin/Cog| 00000020 56 4d 20 2d 76 6d 2d 64 69 73 70 6c 61 79 2d 6e |VM -vm-display-n| 00000030 75 6c 6c 20 2d 76 6d 2d 73 6f 75 6e 64 2d 6e 75 |ull -vm-sound-nu| 00000040 6c 6c 20 2d 70 6c 75 67 69 6e 73 20 2f 68 6f 6d |ll -plugins /hom| 00000050 65 2f 75 62 75 6e 74 75 2f 73 6d 61 6c 6c 74 61 |e/ubuntu/smallta| 00000060 6c 6b 2f 62 69 6e 2f 0a 00 00 00 00 00 00 00 00 |lk/bin/.........| 00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000200
Then I concatenate the header and the working image
cat header.bin zinc.image > magic.image
and I copy the changes and make the image executable.
The image still works
$ /home/ubuntu/smalltalk/bin/CogVM -vm-display-null -vm-sound-null -plugins /home/ubuntu/smalltalk/bin/ magic.image run.st Pharo Smalltalk using Zinc WebSockets
But not when run directly
$ ./magic.image Segmentation fault (core dumped)
Any hints ?
The parameters are not being passed to your VM in the way you expect. Here is what the man page for bash says: If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program. The shell executes the specified interpreter on operating systems that do not handle this exe- cutable format themselves. The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the program, followed by the name of the program, followed by the command arguments, if any. I think there is some trick you can use to pass parameters, but I can't recall the details. Maybe google can help. Dave
On 16 Oct 2012, at 18:05, David T. Lewis <lewis@mail.msen.com> wrote:
The parameters are not being passed to your VM in the way you expect. Here is what the man page for bash says:
If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program. The shell executes the specified interpreter on operating systems that do not handle this exe- cutable format themselves. The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the program, followed by the name of the program, followed by the command arguments, if any.
I think there is some trick you can use to pass parameters, but I can't recall the details. Maybe google can help.
Thanks for answering, I will do some more reading then ;-)
On 16 Oct 2012, at 18:44, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 16 Oct 2012, at 18:05, David T. Lewis <lewis@mail.msen.com> wrote:
The parameters are not being passed to your VM in the way you expect. Here is what the man page for bash says:
If the program is a file beginning with #!, the remainder of the first line specifies an interpreter for the program. The shell executes the specified interpreter on operating systems that do not handle this exe- cutable format themselves. The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the program, followed by the name of the program, followed by the command arguments, if any.
I think there is some trick you can use to pass parameters, but I can't recall the details. Maybe google can help.
Thanks for answering, I will do some more reading then ;-)
David, Your tip was the right one: I made my own wrapper script $ cat pharo #!/bin/sh script_home=$(dirname $0) script_home=$(cd $script_home && pwd) vm=$script_home/CogVM options="-vm-display-null -vm-sound-null -plugins $script_home" $vm $options $* And now, with just one executable as argument in the header $ cat header.bin #!/home/ubuntu/smalltalk/bin/pharo It all works $ ./magic.image eval 'DateAndTime now julianDayNumber' 2456217 $ ./magic.image eval '42 factorial' 1405006117752879898543142606244511569936384000000000 So, the script that is running must do something special with its own arguments. Thanks again, Sven -- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
On 16 Oct 2012, at 18:11, Yanni Chiu <yanni@rogers.com> wrote:
On 16/10/12 11:32 AM, Sven Van Caekenberghe wrote:
line := '#!!/home/ubuntu/smalltalk/bin/CogVM -vm-display-null -vm-sound-null -plugins /home/ubuntu/smalltalk/bin/'.
Is /home/ubuntu/smalltalk/bin/CogVM the executable binary or a shell script?
It is the VM executable itself, like in the Pharo VMs the last couple of months. The strange thing is, the same line works from the shell itself.
On 16/10/12 10:13 AM, David T. Lewis wrote:
If you don't want to do it in the VM, you can do it with a shell script. Just make a 512 byte file that starts with the shebang line that you want to use, prepend it to the saved image file (with /bin/cat) and set file mode executable (chmod).
[So why does "Reply" in Thunderbird now go to the sender, instead of the newsgroup. Have to use "Followup".] Neat, that worked. I used: % mkfile 512 header % vi -b header % cat header my.image > myimage % chmod +x myimage % ./myimage Is it possible to combine the VM and image into a single file?
On Tue, Oct 16, 2012 at 12:08:47PM -0400, Yanni Chiu wrote:
On 16/10/12 10:13 AM, David T. Lewis wrote:
If you don't want to do it in the VM, you can do it with a shell script. Just make a 512 byte file that starts with the shebang line that you want to use, prepend it to the saved image file (with /bin/cat) and set file mode executable (chmod).
[So why does "Reply" in Thunderbird now go to the sender, instead of the newsgroup. Have to use "Followup".]
Neat, that worked. I used:
% mkfile 512 header % vi -b header % cat header my.image > myimage % chmod +x myimage % ./myimage
Is it possible to combine the VM and image into a single file?
This would be ancient history, but check out #snapshot:embedded: in the VM for some clues. I know it has been done, but I don't know who did it or when. Dave
participants (6)
-
Camillo Bruni -
Damien Cassou -
David T. Lewis -
Stefan Marr -
Sven Van Caekenberghe -
Yanni Chiu