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 default: 51 out.append(ch); 52 break; 53 } 54 } 55 return out.toString(); 56 } 57 58 /** 59 * Converts the provided parameters via options to command line args to sub process 60 * 61 * <p>This method will do a simplistic generic unescape for each parameter in the list. It 62 * replaces \[char] with [char]. For example, \" is converted to ". This allows string with 63 * escaped double quotes to stay as a string after being parsed by QuotationAwareTokenizer. 64 * Without this QuotationAwareTokenizer will break the string into sections if it has space in 65 * it. 66 * 67 * @param params parameters received via options 68 * @return list of string representing command line args 69 */ paramsToArgs(List<String> params)70 public static List<String> paramsToArgs(List<String> params) { 71 List<String> result = new ArrayList<>(); 72 for (String param : params) { 73 // doing a simplistic generic unescape here: \<char> is replaced with <char>; note that 74 // this may lead to incorrect results such as \n -> n, or \t -> t, but it's unclear why 75 // a command line param would have \t anyways 76 param = param.replaceAll("\\\\(.)", "$1"); 77 String[] args = QuotationAwareTokenizer.tokenizeLine(param); 78 if (args.length != 0) { 79 result.addAll(Arrays.asList(args)); 80 } 81 } 82 return result; 83 } 84 } 85