blob: 36cd79841bbc202e9af66d682e9dc7c2d1252356 [file] [log] [blame]
Felipe Leme78e09632016-07-19 17:07:22 -07001/*
2 * Copyright (C) 2016 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#ifndef COMMANDLINE_H
18#define COMMANDLINE_H
19
20#include "adb.h"
21
Felipe Leme07ac8552016-07-26 12:14:39 -070022// Callback used to handle the standard streams (stdout and stderr) sent by the
23// device's upon receiving a command.
24//
25class StandardStreamsCallbackInterface {
26 public:
27 StandardStreamsCallbackInterface() {
28 }
29 // Handles the stdout output from devices supporting the Shell protocol.
30 virtual void OnStdout(const char* buffer, int length) = 0;
31
32 // Handles the stderr output from devices supporting the Shell protocol.
33 virtual void OnStderr(const char* buffer, int length) = 0;
34
35 // Indicates the communication is finished and returns the appropriate error
36 // code.
37 //
38 // |status| has the status code returning by the underlying communication
39 // channels
40 virtual int Done(int status) = 0;
41
42 protected:
43 static void OnStream(std::string* string, FILE* stream, const char* buffer, int length) {
44 if (string != nullptr) {
45 string->append(buffer, length);
46 } else {
47 fwrite(buffer, 1, length, stream);
48 fflush(stream);
49 }
50 }
51
52 private:
53 DISALLOW_COPY_AND_ASSIGN(StandardStreamsCallbackInterface);
54};
55
56// Default implementation that redirects the streams to the equilavent host
57// stream or to a string
58// passed to the constructor.
59class DefaultStandardStreamsCallback : public StandardStreamsCallbackInterface {
60 public:
61 // If |stdout_str| is non-null, OnStdout will append to it.
62 // If |stderr_str| is non-null, OnStderr will append to it.
63 DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str)
64 : stdout_str_(stdout_str), stderr_str_(stderr_str) {
65 }
66
67 void OnStdout(const char* buffer, int length) {
68 OnStream(stdout_str_, stdout, buffer, length);
69 }
70
71 void OnStderr(const char* buffer, int length) {
72 OnStream(stderr_str_, stderr, buffer, length);
73 }
74
75 int Done(int status) {
76 return status;
77 }
78
79 private:
80 std::string* stdout_str_;
81 std::string* stderr_str_;
82
83 DISALLOW_COPY_AND_ASSIGN(DefaultStandardStreamsCallback);
84};
85
86// Singleton.
87extern DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK;
88
Felipe Leme78e09632016-07-19 17:07:22 -070089int adb_commandline(int argc, const char** argv);
Felipe Leme78e09632016-07-19 17:07:22 -070090
91// Connects to the device "shell" service with |command| and prints the
92// resulting output.
Felipe Leme07ac8552016-07-26 12:14:39 -070093// if |callback| is non-null, stdout/stderr output will be handled by it.
Josh Gaob122b172017-08-16 16:57:01 -070094int send_shell_command(
95 const std::string& command, bool disable_shell_protocol,
96 StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
Felipe Leme78e09632016-07-19 17:07:22 -070097
98#endif // COMMANDLINE_H