Advertisement
Guest User

wslpath2

a guest
May 28th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.39 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. // Repository: https://github.com/laurent22/wslpath
  5.  
  6. function usage() {
  7. return 'wslpath [-m|-u|-w|-h] NAME[:line[:col]]
  8.  
  9. Output type options:
  10.  
  11.  -w           (default) prints Windows form of NAME (C:\WINNT)
  12.  -m           like -w, but with regular slashes (C:/WINNT)
  13.  -u           prints Unix form of NAME (/mnt/c/winnt)
  14.  
  15. Other options:
  16.  
  17.  -h           displays usage information
  18.  
  19. If no output type is selected, the program will try to detect the form of
  20. NAME and print the opposite type (eg. will print Windows form for Unix
  21. path).';
  22. }
  23.  
  24. $lxssPath_ = null;
  25. function lxssPath() {
  26.     global $lxssPath_;
  27.     if ($lxssPath_) return $lxssPath_;
  28.     $lxssPath_ = trim(exec('cmd.exe /c "echo %LOCALAPPDATA%" 2> /dev/null')) . "\\lxss";
  29.     return $lxssPath_;
  30. }
  31.  
  32. function pathType($path) {
  33.     preg_match('/^([A-Za-z]):(.*)$/', $path, $r);
  34.     if (count($r) > 1) return 'win';
  35.     if (strpos($path, '/') === 0) return 'unix';
  36.     if (strpos($path, '~') === 0) return 'unix';
  37.     if (strpos($path, "\\") !== false) return 'win';
  38.     if (strpos($path, '/') !== false) return 'unix';
  39.     return 'unix';
  40. }
  41.  
  42. function slashJoin($part1, $part2, $slash) {
  43.     $part1 = rtrim($part1, "/\\");
  44.     $part2 = ltrim($part2, "/\\");
  45.     return $part1 . $slash . $part2;
  46. }
  47.  
  48. function setLastSlash($path, $addIt, $slash) {
  49.     $path = rtrim($path, "/\\");
  50.     if (!$addIt) return $path;
  51.     return $path . $slash;
  52. }
  53.  
  54. function toSlash($path, $slash) {
  55.     $path = str_replace("\\", $slash, $path);
  56.     $path = str_replace('/', $slash, $path);
  57.     return $path;
  58. }
  59.  
  60. function explodeLineColNumbers($path) {
  61.     preg_match('/(.*):(\d+):(\d+)/', $path, $r);
  62.     if (count($r) >= 4) {
  63.         return array(
  64.             'path' => $r[1],
  65.             'line' => $r[2],
  66.             'col' => $r[3],
  67.         );
  68.     }
  69.  
  70.     preg_match('/(.*):(\d+)/', $path, $r);
  71.     if (count($r) >= 3) {
  72.         return array(
  73.             'path' => $r[1],
  74.             'line' => $r[2],
  75.         );
  76.     }
  77.  
  78.     return array('path' => $path);
  79. }
  80.  
  81. function pathToWin($inputPath) {
  82.     $path = $inputPath[0] != '/' ? slashJoin(getcwd(), $inputPath, '/') : $inputPath;
  83.     $path = realpath($path);
  84.     if (!$path) return toSlash($inputPath, "\\");
  85.  
  86.     if (strpos($path, '/mnt/') === 0) {
  87.         $initialDir = getcwd();
  88.  
  89.         $inputDir = dirname($path);
  90.  
  91.         $ok = @chdir($inputDir);
  92.         if (!$ok) return toSlash($inputPath, "\\");
  93.  
  94.         $output = trim(exec('cmd.exe /c cd 2> /dev/null', $output));
  95.         $output = slashJoin($output, basename($path), "\\");
  96.     } else {
  97.         $output = slashJoin(lxssPath(), $path, "\\");
  98.     }
  99.  
  100.     $c = $inputPath[strlen($inputPath) - 1];
  101.     $output = setLastSlash($output, $c == '/' || $c == "\\", "\\");
  102.  
  103.     return toSlash($output, "\\");
  104. }
  105.  
  106. function pathToUnix($winPath) {
  107.     preg_match('/([A-Za-z]):(.*)/', $winPath, $r);
  108.    
  109.     if (count($r) < 3) {
  110.         $output = slashJoin(getcwd(), $winPath, '/');
  111.     } else {
  112.         $drive = '/mnt/' . strtolower($r[1]);
  113.         $output = slashJoin($drive, $r[2], '/');
  114.     }
  115.  
  116.     $c = $winPath[strlen($winPath) - 1];
  117.     $output = setLastSlash($output, $c == '/' || $c == "\\", "\\");
  118.  
  119.     return toSlash($output, '/');
  120. }
  121.  
  122. function main($args) {
  123.     array_splice($args, 0, 1);
  124.  
  125.     $operation = null;
  126.     $inputPath = null;
  127.     $mixedMode = false;
  128.  
  129.     while (count($args)) {
  130.         $arg = $args[0];
  131.  
  132.         if ($arg == '-h') {
  133.             echo usage() . "\n";
  134.             return;
  135.         }
  136.  
  137.         if ($arg == '-w') {
  138.             $operation = 'to_win';
  139.             array_splice($args, 0, 1);
  140.             continue;
  141.         }
  142.  
  143.         if ($arg == '-u') {
  144.             $operation = 'to_unix';
  145.             array_splice($args, 0, 1);
  146.             continue;
  147.         }
  148.  
  149.         if ($arg == '-m') {
  150.             $mixedMode = true;
  151.             $operation = 'to_win';
  152.             array_splice($args, 0, 1);
  153.             continue;
  154.         }
  155.  
  156.         if (strpos($arg, '-') !== 0) {
  157.             $inputPath = trim($arg);
  158.             break;
  159.         }
  160.  
  161.         throw new Exception('Unknown option: ' . $arg);
  162.     }
  163.  
  164.     if (!$inputPath) throw new Exception('No path provided');
  165.  
  166.     if (!$operation) {
  167.         $operation = pathType($inputPath) == 'win' ? 'to_unix' : 'to_win';
  168.     }
  169.  
  170.     $pathInfo = explodeLineColNumbers($inputPath);
  171.     $outputPath = '';
  172.  
  173.     if ($operation == 'to_win') {
  174.         $outputPath = pathToWin($pathInfo['path']);
  175.         if ($mixedMode) $outputPath = toSlash($outputPath, '/');
  176.     }
  177.  
  178.     if ($operation == 'to_unix') {
  179.         $outputPath = pathToUnix($pathInfo['path']);
  180.     }
  181.  
  182.     if (isset($pathInfo['line'])) $outputPath .= ':' . $pathInfo['line'];
  183.     if (isset($pathInfo['col'])) $outputPath .= ':' . $pathInfo['col'];
  184.  
  185.     echo $outputPath . "\n";
  186. }
  187.  
  188. try {
  189.     main($argv);
  190. } catch (Exception $e) {
  191.     echo $e->getMessage() . "\n\n" . usage() . "\n";
  192.     die(1);
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement