1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * This file contains utility functions used by other tests.
  26  * @subtest
  27  */
  28 
  29 var Files = Java.type('java.nio.file.Files'),
  30     Paths = Java.type('java.nio.file.Paths'),
  31     System = Java.type('java.lang.System')
  32 
  33 var File = java.io.File
  34 var windows = System.getProperty("os.name").startsWith("Windows")
  35 var winCyg = false
  36 
  37 var outPath = {
  38     windows:0, //C:\dir
  39     mixed:1    //C:/dir
  40 }
  41 
  42 if (windows) {
  43     //Is there any better way to diffrentiate between cygwin/command prompt on windows
  44     var term = System.getenv("TERM")
  45     winCyg = term ? true:false
  46 }
  47 
  48 /**
  49  * Returns which command is selected from PATH
  50  * @param cmd name of the command searched from PATH
  51  */
  52 function which(cmd) {
  53     var path = System.getenv("PATH")
  54     var st = new java.util.StringTokenizer(path, File.pathSeparator)
  55     while (st.hasMoreTokens()) {
  56         var file = new File(st.nextToken(), cmd)
  57         if (file.exists()) {
  58             return (file.getAbsolutePath())
  59         }
  60     }
  61 }
  62 
  63 /**
  64  * Removes a given file
  65  * @param pathname name of the file
  66  */
  67 function rm(pathname) {
  68     var Path = Paths.get(pathname)
  69     if (!Files.deleteIfExists(Path))
  70         print("File \"${pathname}\" doesn't exist")
  71 }
  72     
  73 
  74 
  75 /**
  76  * Unix cygpath implementation
  77  * Supports only two outputs,windows(C:\dir\) and mixed(C:/dir/)
  78  */
  79 function cygpath(path,mode) {
  80    
  81     var newpath = path
  82     if(path.startsWith("/cygdrive/")){
  83         var str = path.substring(10)
  84         var index = str.indexOf('/',0)
  85         var drv = str.substring(0,index)
  86         var rstr = drv.toUpperCase() + ":"
  87         newpath = str.replaceFirst(drv,rstr)
  88     }
  89     if (mode == outPath.windows)
  90         return Paths.get(newpath).toString()
  91     else {
  92         newpath = newpath.replaceAll('\\\\','/')
  93         return newpath
  94     }                   
  95 
  96 }
  97 
  98 /**
  99  * convert given path based on underlying shell programme runs on
 100  */
 101 function toShellPath(path) {
 102     if (windows) {
 103         if (winCyg) {
 104             return cygpath(path,outPath.mixed)
 105         }else {
 106          var path = cygpath(path,outPath.windows)
 107          //convert '\' ->'\\',cmd shell expects this.
 108          return path.replaceAll('\\\\','\\\\\\\\')
 109        }
 110     }else {
 111         return path
 112     }
 113 } 
 114