Last active
August 5, 2016 07:55
-
-
Save bradharms/d0c0ee212ba55376019e1702f6702166 to your computer and use it in GitHub Desktop.
Communication shim between Wsl and Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstdlib> | |
#include <vector> | |
#include <string> | |
#include <iostream> | |
#include <regex> | |
using namespace std; | |
string toUnix(char*); | |
string toWin(char*); | |
regex reIsFileNameWin ("^(?:\\.\\.|\\.|([a-zA-Z])\\:)\\\\[^\\<\\>\\:\"/|\\?\\*]+"); | |
regex reWinDrive ("^([a-zA-Z])\\:"); | |
regex reIsFileNameUnix ("^(?:\\.\\.|\\.|/mnt/([a-z]))/[^\\<\\>\\:\"\\\\|\\?\\*]+"); | |
regex reUnixMnt ("^/mnt/([a-z])"); | |
int main(int argc, char **argv) | |
{ | |
FILE *in; | |
char buff [512]; | |
string cmd ("C:\\Windows\\System32\\bash.exe"); | |
for (int i = 1; i < argc; ++i) { | |
cmd += ' ' + toUnix(argv[i]); | |
} | |
cout << cmd << endl; | |
if (!(in = popen(cmd.c_str(), "r"))) { | |
return 1; | |
} | |
// FIXME: bash.exe apparently can't pipe out to other programs? | |
while (fgets(buff, sizeof(buff), in) != NULL) { | |
cout << toWin(buff); | |
} | |
pclose(in); | |
return 0; | |
} | |
string toUnix(char* in) { | |
string s = in; | |
cmatch cm; | |
if (regex_match(in, cm, reIsFileNameWin)) { | |
s = regex_replace(s, reWinDrive, "\\mnt\\$1"); | |
if (cm.size() >= 2 && cm[1].length() > 0) { | |
s[5] = tolower(s[5]); | |
} | |
replace(s.begin(), s.end(), '\\', '/'); | |
} | |
return s; | |
} | |
string toWin(char* in) { | |
string s = in; | |
cmatch cm; | |
if (regex_match(in, cm, reIsFileNameUnix)) { | |
s = regex_replace(s, reUnixMnt, "$1:"); | |
if (cm.size() >= 2 && cm[1].length() > 0) { | |
s[0] = toupper(s[0]); | |
} | |
replace(s.begin(), s.end(), '/', '\\'); | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this actually work?