Merge "core: Rename libchromeos into libbrillo"
diff --git a/adb/commandline.cpp b/adb/commandline.cpp
index a915a33..2632b1f 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -487,7 +487,11 @@
                     state = 1;
                     break;
                 case '~':
-                    if(state == 1) state++;
+                    if(state == 1) {
+                        state++;
+                    } else {
+                        state = 0;
+                    }
                     break;
                 case '.':
                     if(state == 2) {
@@ -1351,15 +1355,20 @@
         // argv[0] is always "shell".
         --argc;
         ++argv;
-        std::string shell_type_arg;
+        int t_arg_count = 0;
         while (argc) {
             if (!strcmp(argv[0], "-T") || !strcmp(argv[0], "-t")) {
                 if (!CanUseFeature(features, kFeatureShell2)) {
                     fprintf(stderr, "error: target doesn't support PTY args -Tt\n");
                     return 1;
                 }
-                shell_type_arg = (argv[0][1] == 'T') ? kShellServiceArgRaw
-                                                     : kShellServiceArgPty;
+                // Like ssh, -t arguments are cumulative so that multiple -t's
+                // are needed to force a PTY.
+                if (argv[0][1] == 't') {
+                    ++t_arg_count;
+                } else {
+                    t_arg_count = -1;
+                }
                 --argc;
                 ++argv;
             } else if (!strcmp(argv[0], "-x")) {
@@ -1371,6 +1380,33 @@
             }
         }
 
+        std::string shell_type_arg;
+        if (CanUseFeature(features, kFeatureShell2)) {
+            if (t_arg_count < 0) {
+                shell_type_arg = kShellServiceArgRaw;
+            } else if (t_arg_count == 0) {
+                // If stdin isn't a TTY, default to a raw shell; this lets
+                // things like `adb shell < my_script.sh` work as expected.
+                // Otherwise leave |shell_type_arg| blank which uses PTY for
+                // interactive shells and raw for non-interactive.
+                if (!isatty(STDIN_FILENO)) {
+                    shell_type_arg = kShellServiceArgRaw;
+                }
+            } else if (t_arg_count == 1) {
+                // A single -t arg isn't enough to override implicit -T.
+                if (!isatty(STDIN_FILENO)) {
+                    fprintf(stderr,
+                            "Remote PTY will not be allocated because stdin is not a terminal.\n"
+                            "Use multiple -t options to force remote PTY allocation.\n");
+                    shell_type_arg = kShellServiceArgRaw;
+                } else {
+                    shell_type_arg = kShellServiceArgPty;
+                }
+            } else {
+                shell_type_arg = kShellServiceArgPty;
+            }
+        }
+
         std::string command;
         if (argc) {
             // We don't escape here, just like ssh(1). http://b/20564385.
diff --git a/crash_reporter/crash_sender b/crash_reporter/crash_sender
index 5b859a8..95204a4 100755
--- a/crash_reporter/crash_sender
+++ b/crash_reporter/crash_sender
@@ -98,6 +98,10 @@
   log -t "${TAG}" "$@"
 }
 
+lwarn() {
+  lecho -psyslog.warn "$@"
+}
+
 # Returns true if mock is enabled.
 is_mock() {
   [ -f "${MOCK_CRASH_SENDING}" ] && return 0
@@ -294,6 +298,11 @@
   fi
 }
 
+# Return the log string filtered with only JSON-safe white-listed characters.
+filter_log_string() {
+  echo "$1" | tr -cd '[:alnum:]_.\-:;'
+}
+
 send_crash() {
   local meta_path="$1"
   local report_payload="$(get_key_value "${meta_path}" "payload")"
@@ -434,8 +443,7 @@
     ret=$?
     if [ ${ret} -ne 0 ]; then
       proxy=''
-      lecho -psyslog.warn \
-        "Listing proxies failed with exit code ${ret}"
+      lwarn "Listing proxies failed with exit code ${ret}"
     else
       proxy=$(echo "${proxy}" | head -1)
     fi
@@ -446,7 +454,7 @@
   local curl_stderr="${TMP_DIR}/curl_stderr"
 
   set +e
-  curl "${url}" -v ${proxy:+--proxy "$proxy"} \
+  curl "${url}" -f -v ${proxy:+--proxy "$proxy"} \
     --capath "${RESTRICTED_CERTIFICATES_PATH}" --ciphers HIGH \
     -F "prod=${product}" \
     -F "ver=${version}" \
@@ -466,22 +474,17 @@
 
   if [ ${curl_result} -eq 0 ]; then
     local id="$(cat "${report_id}")"
-    local product_name
     local timestamp="$(date +%s)"
-    case ${product} in
-    Chrome_ChromeOS)
-      if is_official_image; then
-        product_name="Chrome"
-      else
-        product_name="Chromium"
-      fi
-      ;;
-    *)
-      product_name="Brillo"
-      ;;
-    esac
-    printf '%s,%s,%s\n' \
-      "${timestamp}" "${id}" "${product_name}" >> "${CRASH_LOG}"
+    local filter_prod="$(filter_log_string "${product}")"
+    local filter_exec="$(filter_log_string "${exec_name}")"
+    if [ "${filter_prod}" != "${product}" ]; then
+      lwarn "Product name filtered to: ${filter_prod}."
+    fi
+    if [ "${filter_exec}" != "${exec_name}" ]; then
+      lwarn "Exec name filtered to: ${filter_exec}."
+    fi
+    printf "{'time':%s,'id':'%s','product':'%s','exec_name':'%s'}\n" \
+      "${timestamp}" "${id}" "${filter_prod}" "${filter_exec}" >> "${CRASH_LOG}"
     lecho "Crash report receipt ID ${id}"
   else
     lecho "Crash sending failed with exit code ${curl_result}: " \