polyglots/hybrids/chimeras with (popular) windows non native languages.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

polyglots/hybrids/chimeras with (popular) windows non native languages.

#1 Post by npocmaka_ » 28 Jan 2016 07:17

because of the link limitations I'll put the links in code blocks

here's what we have here:

Code: Select all

jscript/vbscript/whs/hta:
http://www.dostips.com/forum/viewtopic.php?f=3&t=5543&start=30


powershell:
http://www.dostips.com/forum/viewtopic.php?t=5526

.net
http://www.dostips.com/forum/viewtopic.php?t=5260


Some other techniques:

Code: Select all

autoit (search on the page for autoit) and kixtart:
http://www.robvanderwoude.com/clevertricks.php

seems to be popular for perl:
https://en.wikipedia.org/wiki/Polyglot_(computing)

java:
http://stackoverflow.com/questions/28190126/how-can-i-embed-java-code-into-batch-scriptis-it-possible-to-create-java-bat

this can be used for node.js/javascript/jscript:
https://gist.github.com/yaauie/959862

bash:
http://stackoverflow.com/questions/17510688/single-script-to-run-in-both-windows-batch-and-linux-bash




here's my attempt with python 2.7 (does not work with python 3)

Code: Select all

0<''' :label

@echo off
echo batch
python %~f0 %*
exit /b 0

'''

print "something"



for python 3.5 this can be used but it prints one redundant "&":

Code: Select all

0<0# :& @goto :python
'''
:python
@echo off
echo batch part
python %~f0 %*
exit /b 0
'''
print ("something")


ADSalways can be tried also..


XML based languages are easy here's an apache ant test:

Code: Select all

<!-- :
@echo off
echo batch message
call ant -f %~f0 %*
exit /b 0
-->

<project default="echo">

    <target name="echo">
        <echo message="ant message"/>
    </target>



</project>



EDIT
Didn't pay enough attention to the siberia's man post.

This work for all python versions:

Code: Select all

0<0# : ^
'''
@echo off
echo batch
python %~f0 %*
exit /b 0
'''

print("something")
Last edited by npocmaka_ on 28 Jan 2016 08:05, edited 3 times in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#2 Post by Squashman » 28 Jan 2016 07:31

Thank you for taking the time to put this together.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#3 Post by npocmaka_ » 28 Jan 2016 08:29

here's a PHP polyglot:


Code: Select all

<?php/* :

@echo off
echo batch part
php -f "%~f0"
exit /b 0

*/?>

<?php  print "php part\n";?>



Next on the list is Ruby and may be Lua (ideas for other languages?)


every part of PHP executable code must be put in <?php ...?> block . The other things are printed directly to the console (because it is mainly web development language)
with /**/ you can have multi-line comments. So redirection priority trick is used.

PHP has also REPL capabilities with PHP -r switch which is used extensively here.

EDIT.
Turns out I'm not the inventor of this technique -> http://whitescreen.nicolaas.net/program ... p-polyglot
Last edited by npocmaka_ on 29 Jan 2016 02:09, edited 3 times in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#4 Post by Squashman » 28 Jan 2016 08:46

npocmaka_ wrote:Next on the list is Ruby and may be Lua (ideas for other languages?)

I guess the possibilities could be endless. So.....
"Now go do... that voodoo... that YOU do... SO WELL...!" - Hedley Lamarr

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#5 Post by npocmaka_ » 28 Jan 2016 09:09

Squashman wrote:
npocmaka_ wrote:
I guess the possibilities could be endless. So.....
"Now go do... that voodoo... that YOU do... SO WELL...!" - Hedley Lamarr


Image


Ruby was really easy .
With @variableName you can define instance variables. :


Code: Select all

@break #^
=begin

@echo off
echo batch part
ruby "%~f0" %*
exit /b 0

=end
puts 'ruby part'


#comment is one line comment

=begin
is for multi line comments
=end


with @break defines ruby instance variable and is silent in batch. with # is started one line comment and the ^ is to hide the next line for the batch script where the ruby comment starts.


Ruby also comes with REPL tool - IRB. Can be used for one liners like echo 5+5|irb

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#6 Post by Jer » 28 Jan 2016 11:50

Can you tell me what the cryptic parts are doing and what my error is about.
Thanks.

Code: Select all

0<0# :& @goto :python
'''
:python
@echo on
echo batch part
c:\python34\python.exe %~f0 %*
exit /b 0
'''
print ("something")


C:\Temp\DOSBatch> &
The system cannot find the file specified.

C:\Temp\DOSBatch>echo batch part
batch part

C:\Temp\DOSBatch>c:\python34\python.exe C:\Temp\DOSBatch\Test17.bat
something

C:\Temp\DOSBatch>exit /b 0

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#7 Post by npocmaka_ » 28 Jan 2016 13:22

Jer wrote:Can you tell me what the cryptic parts are doing and what my error is about.
Thanks.



Hola.

I've already edited my first post.
Better use the script proposed by siberia-man:

Code: Select all

0<0# : ^
'''
@echo off
echo batch
python %~f0 %*
exit /b 0
'''

print("something")


there are differences between python 2* and 3* and this will work for all of them.

The first line will understood by python like
0<0 #comment

and the second line as a start of multi line comment.

Batch on the other side will parse the first two lines like:

:0<0# '''


First the redirection has bigger prio than the other commands and this will put the semicolon at the start and then this will be taken as label.
Second the caret "^" will escape the new line and the three quotes will go the first line

So the first two lines are ignored by batch and are a start of multiline comment for the python.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#8 Post by npocmaka_ » 29 Jan 2016 02:19

clojure is so easy that I feel ashamed to post it here (semicolon is a comment character as in every LISP like language):


Code: Select all

;@echo off
;call clojure "%~f0" %*
;exit /b 0
(println (format "Hello world"))


Clojure is intended as a REPL tool and you can use pretty complex one liners with it.
Clojure comes without installer/caller and is de facto just a jar file so its to you to create a proper calling script.
In my case I've put the clojure folder in the %PATH% and I've put there this 'script':

@java -cp "%~dp0clojure-1.8.0.jar"; clojure.main %*

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#9 Post by npocmaka_ » 29 Jan 2016 03:30

groovy

the same as jscript/node.js

Code: Select all

0<0/* :
@echo off

echo batch part
groovy "%~f0" %*
exit /b %errorlevel%

*/

println "Groovy part"

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#10 Post by Meerkat » 29 Jan 2016 05:18

Hmm... Like AutoIt, AutoHotKey uses semi-colon(;) as comments...

Hope this could help.

Meerkat
Last edited by Meerkat on 29 Jan 2016 05:32, edited 1 time in total.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#11 Post by npocmaka_ » 29 Jan 2016 05:20

Meerkat wrote:Hmm... Like AutoIt, AutoHotKey uses semi-colon(;) as comments...

Hope [url=rosettacode.org/wiki/Comments]this[/url] could help.

Meerkat



yep. Also Kixtart .Here are examples - http://www.robvanderwoude.com/clevertricks.php

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#12 Post by npocmaka_ » 29 Jan 2016 06:49

Squirrel

I don't know where I can download binaries from .I've saw only source code here
but at least the build with visual studio was easy:

Code: Select all

::print("");/*
@echo off
echo batch part
sq.exe "%~f0" %*
exit /b 0
*/
print("Squirrel part\n");



Code: Select all

language reference
http://www.squirrel-lang.org/doc/squirrel3.html#d0e410


with :: you can access global scope the environment objects .This includes the print function. Comments are in C style so making a hybrid was easy.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#13 Post by npocmaka_ » 29 Jan 2016 08:53

Scala

Code: Select all

@deprecated("Used for batch polyglot", "1.0")/* 2>nul 
  @echo off
  echo batch part
  call scala -howtorun:script "%~f0" %*
  exit /b %errorlevel%
*/
object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Scala part")
  }
}


Scala launcher is a batch file so call in front of it is needed.
It has -howtorun option that can be used to surpass file extension checking.
Scala inherits from java some annotations and deprecated can be used outside of an object , so basically it is java-like hybrid without need to worry about file extensions.


Some other languages that I've tried and failed
R - it allows single statements as 0<0 but prints in the console FALSE which is irritating
DLang - compiler is file extension sensitive though it supports java like annotations .
Lua - does not support single statements.

Bonaparte
Posts: 1
Joined: 27 Mar 2016 15:30

Re: Advanced Batch features via auxiliary .exe programs

#14 Post by Bonaparte » 27 Mar 2016 15:44

Found a nice solution to combine php and bat.
Along with php-wfio this can be an extremely powerful combination.

Code: Select all

<?/*# :
@echo off

echo cmd
php "%~f0" %*
echo cmd again

exit /b

*/

// Use https://github.com/kenjiuno/php-wfio/releases to work with the filesystem.

echo "php\n";
if (count($_SERVER['argv']) > 1) print_r($_SERVER['argv']);

if (!empty($_SERVER['argv'][1]) && $_SERVER['argv'][1] === 'notepad') {
   $WshShell = new COM("WScript.Shell");
   $oExec = $WshShell->Run("xidel.exe", 3, true);
}

//$wmi = new COM("winmgmts:{impersonationLevel=impersonate}!//./root/cimv2");

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: polyglots/hybrids/chimeras with (popular) windows non native languages.

#15 Post by misol101 » 15 Sep 2016 16:18

Saw that Lua was missing here, this worked for me:

Code: Select all

--[=====[ >nul 2>nul
@echo off
lua %~f0 %*
exit /b 0
--]=====]

--lua code here


Post Reply