1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.util; 18 19 import java.util.ArrayList; 20 import java.util.Arrays; 21 import java.util.List; 22 23 /** 24 * Utility class for escaping strings for specific formats. 25 * Include methods to escape strings that are being passed to the Android Shell. 26 */ 27 public class StringEscapeUtils { 28 29 /** 30 * Escapes a {@link String} for use in an Android shell command. 31 * 32 * @param str the {@link String} to escape 33 * @return the Android shell escaped {@link String} 34 */ escapeShell(String str)35 public static String escapeShell(String str) { 36 if (str == null) { 37 return null; 38 } 39 StringBuilder out = new StringBuilder(); 40 for (int i = 0; i < str.length(); ++i) { 41 char ch = str.charAt(i); 42 // TODO: add other characters as needed. 43 switch (ch) { 44 case '$': 45 out.append("\\$"); 46 break; 47 case '\\': 48 out.append("\\\\"); 49 break; 50 case '>': 51 out.append("\\>"); 52 break; 53 case '<': 54 out.append("\\<"); 55 break; 56 case '|': 57 out.append("\\|"); 58 break; 59 default: 60 out.append(ch); 61 break; 62 } 63 } 64 return out.toString(); 65 } 66 67 /** 68 * Converts the provided parameters via options to command line args to sub process 69 * 70 * <p>This method will do a simplistic generic unescape for each parameter in the list. It 71 * replaces \[char] with [char]. For example, \" is converted to ". This allows string with 72 * escaped double quotes to stay as a string after being parsed by QuotationAwareTokenizer. 73 * Without this QuotationAwareTokenizer will break the string into sections if it has space in 74 * it. 75 * 76 * @param params parameters received via options 77 * @return list of string representing command line args 78 */ paramsToArgs(List<String> params)79 public static List<String> paramsToArgs(List<String> params) { 80 List<String> result = new ArrayList<>(); 81 for (String param : params) { 82 // doing a simplistic generic unescape here: \<char> is replaced with <char>; note that 83 // this may lead to incorrect results such as \n -> n, or \t -> t, but it's unclear why 84 // a command line param would have \t anyways 85 param = param.replaceAll("\\\\(.)", "$1"); 86 String[] args = QuotationAwareTokenizer.tokenizeLine(param, /* Logging */ false); 87 if (args.length != 0) { 88 result.addAll(Arrays.asList(args)); 89 } 90 } 91 return result; 92 } 93 } 94