Page 1 of 2

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

Posted: 28 Jan 2016 07:17
by npocmaka_
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")

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

Posted: 28 Jan 2016 07:31
by Squashman
Thank you for taking the time to put this together.

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

Posted: 28 Jan 2016 08:29
by npocmaka_
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

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

Posted: 28 Jan 2016 08:46
by Squashman
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

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

Posted: 28 Jan 2016 09:09
by npocmaka_
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

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

Posted: 28 Jan 2016 11:50
by Jer
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

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

Posted: 28 Jan 2016 13:22
by npocmaka_
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.

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

Posted: 29 Jan 2016 02:19
by npocmaka_
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 %*

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

Posted: 29 Jan 2016 03:30
by npocmaka_
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"

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

Posted: 29 Jan 2016 05:18
by Meerkat
Hmm... Like AutoIt, AutoHotKey uses semi-colon(;) as comments...

Hope this could help.

Meerkat

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

Posted: 29 Jan 2016 05:20
by npocmaka_
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

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

Posted: 29 Jan 2016 06:49
by npocmaka_
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.

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

Posted: 29 Jan 2016 08:53
by npocmaka_
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.

Re: Advanced Batch features via auxiliary .exe programs

Posted: 27 Mar 2016 15:44
by Bonaparte
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");

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

Posted: 15 Sep 2016 16:18
by misol101
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