jarble / transpiler

A universal translator for programming languages
https://jarble.github.io/transpiler
MIT License
472 stars 44 forks source link

please add an example to translate bash into lua #7

Open rubo77 opened 5 years ago

rubo77 commented 5 years ago

I would like to try to translate this bash script into lua: https://github.com/Freifunk-Nord/gluon-ssid-changer/blob/2018.1.x/gluon-ssid-changer/shsrc/ssid-changer.sh

But I cannot make it run on the webinterface.

maybe add a "start now" button after you entered the code in the upper box.

I tried [this](https://jarble.github.io/transpiler/javascript/js_transpiler/test_parser.html#%7B%22inputText%22%3A%22%23!%2Fbin%2Fsh%5Cn%5Cnsafety_exit()%20%7B%5Cn%5Ctecho%20%241%2C%20exiting%20with%20error%20code%202%5Cn%5Ctexit%202%5Cn%7D%5Cnpgrep%20-f%20autoupdater%20%3E%2Fdev%2Fnull%20%26%26%20safety_exit%20'autoupdater%20running'%5CnUT%3D%24(sed%20's%2F%5C%5C..*%2F%2Fg'%20%2Fproc%2Fuptime)%5Cn%5B%20%24UT%20-gt%2060%20%5D%20%7C%7C%20safety_exit%20'less%20than%20one%20minute'%5Cn%5B%20%24(find%20%2Fvar%2Frun%20-name%20hostapd-phy*%20%7C%20wc%20-l)%20-gt%200%20%5D%20%7C%7C%20safety_exit%20'no%20hostapd-phy*'%5Cn%5Cn%23%20only%20once%20every%20timeframe%20minutes%20the%20SSID%20will%20change%20to%20the%20Offline-SSID%5Cn%23%20(set%20to%201%20minute%20to%20change%20immediately%20every%20time%20the%20router%20gets%20offline)%5CnMINUTES%3D%5C%22%24(uci%20-q%20get%20ssid-changer.settings.switch_timeframe)%5C%22%5Cn%3A%20%24%7BMINUTES%3A%3D30%7D%5Cn%5Cn%23%20the%20first%20few%20minutes%20directly%20after%20reboot%20within%20which%20an%20Offline-SSID%20always%20may%20be%20activated%5Cn%23%20(must%20be%20%3C%3D%20switch_timeframe)%5CnFIRST%3D%5C%22%24(uci%20-q%20get%20ssid-changer.settings.first)%5C%22%5Cn%3A%20%24%7BFIRST%3A%3D5%7D%5Cn%5Cn%23%20the%20Offline-SSID%20will%20start%20with%20this%20prefix%20use%20something%20short%20to%20leave%20space%20for%20the%20nodename%5Cn%23%20(no%20'~'%20allowed!)%5CnPREFIX%3D%5C%22%24(uci%20-q%20get%20ssid-changer.settings.prefix)%5C%22%5Cn%3A%20%24%7BPREFIX%3A%3D'FF_Offline_'%7D%5Cn%5Cnif%20%5B%20%5C%22%24(uci%20-q%20get%20ssid-changer.settings.enabled)%5C%22%20%3D%20'0'%20%5D%3B%20then%20%5Cn%5CtDISABLED%3D'1'%5Cnelse%5Cn%5CtDISABLED%3D'0'%5Cnfi%5Cn%5Cn%23%20generate%20the%20ssid%20with%20either%20'nodename'%2C%20'mac'%20or%20to%20use%20only%20the%20prefix%20set%20to%20'none'%5CnSETTINGS_SUFFIX%3D%5C%22%24(uci%20-q%20get%20ssid-changer.settings.suffix)%5C%22%5Cn%3A%20%24%7BSETTINGS_SUFFIX%3A%3D'nodename'%7D%5Cn%5Cnif%20%5B%20%24SETTINGS_SUFFIX%20%3D%20'nodename'%20%5D%3B%20then%5Cn%5CtSUFFIX%3D%5C%22%24(uname%20-n)%5C%22%5Cn%5Ct%23%2032%20would%20be%20possible%20as%20well%5Cn%5Ctif%20%5B%20%24%7B%23SUFFIX%7D%20-gt%20%24((30%20-%20%24%7B%23PREFIX%7D))%20%5D%3B%20then%5Cn%5Ct%5Ct%23%20calculate%20the%20length%20of%20the%20first%20part%20of%20the%20node%20identifier%20in%20the%20offline-ssid%5Cn%5Ct%5CtHALF%3D%24((%20(28%20-%20%24%7B%23PREFIX%7D%20)%20%2F%202%20))%5Cn%5Ct%5Ct%23%20jump%20to%20this%20charakter%20for%20the%20last%20part%20of%20the%20name%5Cn%5Ct%5CtSKIP%3D%24((%20%24%7B%23SUFFIX%7D%20-%20%24HALF%20))%5Cn%5Ct%5Ct%23%20use%20the%20first%20and%20last%20part%20of%20the%20nodename%20for%20nodes%20with%20long%20name%5Cn%5Ct%5CtSUFFIX%3D%24%7BSUFFIX%3A0%3A%24HALF%7D...%24%7BSUFFIX%3A%24SKIP%3A%24%7B%23SUFFIX%7D%7D%5Cn%5Ctfi%5Cnelif%20%5B%20%24SETTINGS_SUFFIX%20%3D%20'mac'%20%5D%3B%20then%5Cn%5CtSUFFIX%3D%5C%22%24(uci%20-q%20get%20network.bat0.macaddr%20%7C%20%2Fbin%2Fsed%20's%2F%3A%2F%2Fg')%5C%22%5Cnelse%5Cn%5Ct%23%20'none'%5Cn%5CtSUFFIX%3D''%5Cnfi%5Cn%5CnOFFLINE_SSID%3D%5C%22%24PREFIX%24SUFFIX%5C%22%5Cn%5Cn%23%20get%20all%20SSIDs%20(replace%20%5C%5C'%20with%20TICX%20and%20back%20to%20keep%20a%20possible%20tic%20in%20an%20SSID)%5CnONLINE_SSIDs%3D%5C%22%24(uci%20show%20%7C%20grep%20wireless.client_radio%5B0-9%5D%5C%5C.%20%7C%20grep%20ssid%20%20%7C%20awk%20-F%20'%3D'%20%20'%7Bprint%20%242%7D'%20%7C%20sed%20%5C%22s%2F%5C%5C%5C%5C%5C%5C'%2FTICX%2Fg%5C%22%20%7C%20tr%20%5C%5C'%20%5C%5C~%20%7C%20sed%20%5C%22s%2FTICX%2F%5C%5C%5C%5C%5C%5C'%2Fg%5C%22%20)%20%5C%22%5Cn%23%20if%20for%20whatever%20reason%20ONLINE_SSIDs%20is%20NULL%3A%5Cn%3A%20%24%7BONLINE_SSIDs%3A%3D%5C%22~FREIFUNK~%5C%22%7D%5Cn%5Cn%23%20temp%20file%20to%20count%20the%20offline%20incidents%20during%20switch_timeframe%5CnTMP%3D%2Ftmp%2Fssid-changer-count%5Cnif%20%5B%20!%20-f%20%24TMP%20%5D%3B%20then%20echo%20%5C%220%5C%22%3E%24TMP%3B%20fi%5CnOFF_COUNT%3D%24(cat%20%24TMP)%5Cn%5CnTQ_LIMIT_ENABLED%3D%5C%22%24(uci%20-q%20get%20ssid-changer.settings.tq_limit_enabled)%5C%22%5Cn%23%20if%20true%2C%20the%20offline%20ssid%20will%20only%20be%20set%20if%20there%20is%20no%20gateway%20reacheable%5Cn%23%20upper%20and%20lower%20limit%20to%20turn%20the%20offline_ssid%20on%20and%20off%5Cn%23%20in-between%20these%20two%20values%20the%20SSID%20will%20never%20be%20changed%20to%20preven%20it%20from%20toggeling%20every%20Minute.%5Cn%3A%20%24%7BTQ_LIMIT_ENABLED%3A%3D'0'%7D%5Cn%5Cnif%20%5B%20%24TQ_LIMIT_ENABLED%20%3D%201%20%5D%3B%20then%5Cn%5CtTQ_LIMIT_MAX%3D%5C%22%24(uci%20-q%20get%20ssid-changer.settings.tq_limit_max)%5C%22%5Cn%5Ct%23%20%20upper%20limit%2C%20above%20that%20the%20online%20SSID%20will%20be%20used%5Cn%5Ct%3A%20%24%7BTQ_LIMIT_MAX%3A%3D'45'%7D%5Cn%5CtTQ_LIMIT_MIN%3D%5C%22%24(uci%20-q%20get%20ssid-changer.settings.tq_limit_min)%5C%22%5Cn%5Ct%23%20%20lower%20limit%2C%20below%20that%20the%20offline%20SSID%20will%20be%20used%5Cn%5Ct%3A%20%24%7BTQ_LIMIT_MIN%3A%3D'35'%7D%5Cn%5Ct%23%20grep%20the%20connection%20quality%20of%20the%20currently%20used%20gateway%5Cn%5CtGATEWAY_TQ%3D%24(batctl%20gwl%20%7C%20grep%20-e%20%5C%22%5E%3D%3E%5C%22%20-e%20%5C%22%5E%5C%5C*%5C%22%20%7C%20awk%20-F%20'%5B('')%5D'%20'%7Bprint%20%242%7D'%20%7C%20tr%20-d%20%5C%22%20%5C%22)%5Cn%5Ctif%20%5B%20!%20%24GATEWAY_TQ%20%5D%3B%20then%5Cn%5Ct%5Ct%23%20there%20is%20no%20gateway%5Cn%5Ct%5CtGATEWAY_TQ%3D0%5Cn%5Ctfi%5Cn%5Cn%5CtMSG%3D%5C%22TQ%20is%20%24GATEWAY_TQ%2C%20%5C%22%5Cn%5Cn%5Ctif%20%5B%20%24GATEWAY_TQ%20-ge%20%24TQ_LIMIT_MAX%20%5D%3B%20then%5Cn%5Ct%5CtCHECK%3D1%5Cn%5Ctelif%20%5B%20%24GATEWAY_TQ%20-lt%20%24TQ_LIMIT_MIN%20%5D%3B%20then%5Cn%5Ct%5CtCHECK%3D0%5Cn%5Ctelse%5Cn%5Ct%5Ct%23%20this%20is%20just%20get%20a%20clean%20run%20if%20we%20are%20in-between%20the%20grace%20periode%5Cn%5Ct%5Ctecho%20%5C%22TQ%20is%20%24GATEWAY_TQ%2C%20do%20nothing%5C%22%5Cn%5Ct%5Ctexit%200%5Cn%5Ctfi%5Cnelse%5Cn%5CtMSG%3D%5C%22%5C%22%5Cn%5CtCHECK%3D%5C%22%24(batctl%20gwl%20-H%7Cgrep%20-v%20%5C%22gateways%20in%20range%5C%22%7Cwc%20-l)%5C%22%5Cnfi%5Cn%5CnUP%3D%24((%24UT%20%2F%2060))%5CnM%3D%24((%24UP%20%25%20%24MINUTES))%5Cn%5CnHUP_NEEDED%3D0%5Cnif%20%5B%20%5C%22%24CHECK%5C%22%20-gt%200%20%5D%20%7C%7C%20%5B%20%5C%22%24DISABLED%5C%22%20%3D%20'1'%20%5D%3B%20then%5Cn%5Ctecho%20%5C%22node%20is%20online%5C%22%5Cn%5CtLOOP%3D1%5Cn%5Ct%23%20check%20status%20for%20all%20physical%20devices%5Cn%5Ctfor%20HOSTAPD%20in%20%24(ls%20%2Fvar%2Frun%2Fhostapd-phy*)%3B%20do%5Cn%5Ct%5CtONLINE_SSID%3D%5C%22%24(echo%20%24ONLINE_SSIDs%20%7C%20awk%20-F%20'~'%20-v%20l%3D%24((LOOP*2))%20'%7Bprint%20%24l%7D')%5C%22%20%5Cn%5Ct%5CtLOOP%3D%24((LOOP%2B1))%5Cn%5Ct%5CtCURRENT_SSID%3D%5C%22%24(grep%20%5C%22%5Essid%3D%24ONLINE_SSID%5C%22%20%24HOSTAPD%20%7C%20cut%20-d%5C%22%3D%5C%22%20-f2)%5C%22%5Cn%5Ct%5Ctif%20%5B%20%5C%22%24CURRENT_SSID%5C%22%20%3D%20%5C%22%24ONLINE_SSID%5C%22%20%5D%3B%20then%5Cn%5Ct%5Ct%5Ctecho%20%5C%22SSID%20%24CURRENT_SSID%20is%20correct%2C%20nothing%20to%20do%5C%22%5Cn%5Ct%5Ct%5Ctbreak%5Cn%5Ct%5Ctfi%5Cn%5Ct%5CtCURRENT_SSID%3D%5C%22%24(grep%20%5C%22%5Essid%3D%24OFFLINE_SSID%5C%22%20%24HOSTAPD%20%7C%20cut%20-d%5C%22%3D%5C%22%20-f2)%5C%22%5Cn%5Ct%5Ctif%20%5B%20%5C%22%24CURRENT_SSID%5C%22%20%3D%20%5C%22%24OFFLINE_SSID%5C%22%20%5D%3B%20then%5Cn%5Ct%5Ct%5Ct%23%20set%20online%5Cn%5Ct%5Ct%5Ctlogger%20-s%20-t%20%5C%22gluon-ssid-changer%5C%22%20-p%205%20%24MSG%5C%22SSID%20is%20%24CURRENT_SSID%2C%20change%20to%20%24ONLINE_SSID%5C%22%5Cn%5Ct%5Ct%5Ctsed%20-i%20%5C%22s~%5Essid%3D%24CURRENT_SSID~ssid%3D%24ONLINE_SSID~%5C%22%20%24HOSTAPD%5Cn%5Ct%5Ct%5Ct%23%20HUP%20here%20would%20be%20to%20early%20for%20dualband%20devices%5Cn%5Ct%5Ct%5CtHUP_NEEDED%3D1%5Cn%5Ct%5Ctelse%5Cn%5Ct%5Ct%5Ctlogger%20-s%20-t%20%5C%22gluon-ssid-changer%5C%22%20-p%205%20%5C%22could%20not%20set%20to%20online%20state%3A%20did%20neither%20find%20SSID%20'%24ONLINE_SSID'%20nor%20'%24OFFLINE_SSID'.%20Please%20reboot%5C%22%5Cn%5Ct%5Ctfi%5Cn%5Ctdone%5Cnelif%20%5B%20%5C%22%24CHECK%5C%22%20-eq%200%20%5D%3B%20then%5Cn%5Ctecho%20%5C%22node%20is%20considered%20offline%5C%22%5Cn%5Ctif%20%5B%20%24UP%20-lt%20%24FIRST%20%5D%20%7C%7C%20%5B%20%24M%20-eq%200%20%5D%3B%20then%5Cn%5Ct%5Ct%23%20set%20SSID%20offline%2C%20only%20if%20uptime%20is%20less%20than%20FIRST%20or%20exactly%20a%20multiplicative%20of%20switch_timeframe%5Cn%5Ct%5Ctif%20%5B%20%24UP%20-lt%20%24FIRST%20%5D%3B%20then%20%5Cn%5Ct%5Ct%5CtT%3D%24FIRST%5Cn%5Ct%5Ctelse%5Cn%5Ct%5Ct%5CtT%3D%24MINUTES%5Cn%5Ct%5Ctfi%5Cn%5Ct%5Ct%23echo%20minute%20%24M%2C%20check%20if%20%24OFF_COUNT%20is%20more%20than%20half%20of%20%24T%20%5Cn%5Ct%5Ctif%20%5B%20%24OFF_COUNT%20-ge%20%24((%24T%20%2F%202))%20%5D%3B%20then%5Cn%5Ct%5Ct%5Ct%23%20node%20was%20offline%20more%20times%20than%20half%20of%20switch_timeframe%20(or%20than%20%24FIRST)%5Cn%5Ct%5Ct%5CtLOOP%3D1%5Cn%5Ct%5Ct%5Ctfor%20HOSTAPD%20in%20%24(ls%20%2Fvar%2Frun%2Fhostapd-phy*)%3B%20do%5Cn%5Ct%5Ct%5Ct%5CtONLINE_SSID%3D%5C%22%24(echo%20%24ONLINE_SSIDs%20%7C%20awk%20-F%20'~'%20-v%20l%3D%24((LOOP*2))%20'%7Bprint%20%24l%7D')%5C%22%20%5Cn%5Ct%5Ct%5Ct%5CtLOOP%3D%24((LOOP%2B1))%5Cn%5Ct%5Ct%5Ct%5CtCURRENT_SSID%3D%5C%22%24(grep%20%5C%22%5Essid%3D%24OFFLINE_SSID%5C%22%20%24HOSTAPD%20%7C%20cut%20-d%5C%22%3D%5C%22%20-f2)%5C%22%5Cn%5Ct%5Ct%5Ct%5Ctif%20%5B%20%5C%22%24CURRENT_SSID%5C%22%20%3D%20%5C%22%24OFFLINE_SSID%5C%22%20%5D%3B%20then%5Cn%5Ct%5Ct%5Ct%5Ct%5Ctecho%20%5C%22SSID%20%24CURRENT_SSID%20is%20correct%2C%20nothing%20to%20do%5C%22%5Cn%5Ct%5Ct%5Ct%5Ct%5Ctbreak%5Cn%5Ct%5Ct%5Ct%5Ctfi%5Cn%5Ct%5Ct%5Ct%5CtCURRENT_SSID%3D%5C%22%24(grep%20%5C%22%5Essid%3D%24ONLINE_SSID%5C%22%20%24HOSTAPD%20%7C%20cut%20-d%5C%22%3D%5C%22%20-f2)%5C%22%5Cn%5Ct%5Ct%5Ct%5Ctif%20%5B%20%5C%22%24CURRENT_SSID%5C%22%20%3D%20%5C%22%24ONLINE_SSID%5C%22%20%5D%3B%20then%5Cn%5Ct%5Ct%5Ct%5Ct%5Ct%23%20set%20offline%5Cn%5Ct%5Ct%5Ct%5Ct%5Ctlogger%20-s%20-t%20%5C%22gluon-ssid-changer%5C%22%20-p%205%20%24MSG%5C%22%24OFF_COUNT%20times%20offline%2C%20SSID%20is%20%24CURRENT_SSID%2C%20change%20to%20%24OFFLINE_SSID%5C%22%5Cn%5Ct%5Ct%5Ct%5Ct%5Ctsed%20-i%20%5C%22s~%5Essid%3D%24ONLINE_SSID~ssid%3D%24OFFLINE_SSID~%5C%22%20%24HOSTAPD%5Cn%5Ct%5Ct%5Ct%5Ct%5CtHUP_NEEDED%3D1%5Cn%5Ct%5Ct%5Ct%5Ctelse%5Cn%5Ct%5Ct%5Ct%5Ct%5Ctlogger%20-s%20-t%20%5C%22gluon-ssid-changer%5C%22%20-p%205%20%5C%22could%20not%20set%20to%20offline%20state%3A%20did%20neither%20find%20SSID%20'%24ONLINE_SSID'%20nor%20'%24OFFLINE_SSID'.%20Please%20reboot%5C%22%5Cn%5Ct%5Ct%5Ct%5Ctfi%5Cn%5Ct%5Ct%5Ctdone%5Cn%5Ct%5Ctfi%5Cn%5Ct%5Ct%23else%20echo%20minute%20%24M%2C%20just%20count%20%24OFF_COUNT%5Cn%5Ctfi%5Cn%5Ctecho%20%5C%22%24((%24OFF_COUNT%20%2B%201))%5C%22%3E%24TMP%5Cnfi%5Cn%5Cnif%20%5B%20%24HUP_NEEDED%20%3D%201%20%5D%3B%20then%5Cn%5Ct%23%20send%20HUP%20to%20all%20hostapd%20to%20load%20the%20new%20SSID%5Cn%5Ctkillall%20-HUP%20hostapd%5Cn%5CtHUP_NEEDED%3D0%5Cn%5Ctecho%20%5C%22HUP!%5C%22%5Cnfi%5Cn%5Cnif%20%5B%20%24M%20-eq%200%20%5D%3B%20then%5Cn%5Ct%23%20set%20counter%20to%200%20if%20the%20timeframe%20is%20over%5Cn%5Ctecho%20%5C%220%5C%22%3E%24TMP%5Cnfi%5Cn%22%2C%22inputLang%22%3A%22bash%22%2C%22outputLang%22%3A%22lua%22%7D)

but I get an error in the console:

uncaught exception: Input language "bash" not recognized

rubo77 commented 5 years ago

I see bash is not supported yet, maybe you can use the code from https://www.swag.uwaterloo.ca/bash2py/downloads/index.html to transpile bash to python first