Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ class Client : public ClientInterface {

bool OpenBrowser(const std::string &url) override;

// The most primitive Call method
// This method won't change the server_status_ even
// when version mismatch happens. In this case,
// just return false.
bool Call(const commands::Input &input, commands::Output *output);

private:
friend class ClientTestPeer;

Expand Down Expand Up @@ -216,12 +222,6 @@ class Client : public ClientInterface {
// re-issue session id if it is not available.
bool EnsureCallCommand(commands::Input *input, commands::Output *output);

// The most primitive Call method
// This method won't change the server_status_ even
// when version mismatch happens. In this case,
// just return false.
bool Call(const commands::Input &input, commands::Output *output);

// first invoke Call() command and check the
// protocol_version. When protocol version mismatch,
// client goes to FATAL state
Expand Down
1 change: 1 addition & 0 deletions src/unix/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ genrule(
":icons",
"//gui/tool:mozc_tool",
"//server:mozc_server",
"//unix/cli:mozc_client_cli",
"//unix/emacs:mozc_emacs_helper",
"//unix/ibus:gen_mozc_xml",
"//unix/ibus:ibus_mozc",
Expand Down
54 changes: 54 additions & 0 deletions src/unix/cli/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

load(
"//:build_defs.bzl",
"mozc_cc_binary",
"mozc_cc_library",
"mozc_cc_test",
)

package(default_visibility = ["//visibility:private"])

mozc_cc_binary(
name = "mozc_client_cli",
srcs = ["mozc_client_cli.cc"],
visibility = ["//unix:__subpackages__"],
deps = [
"//base:init_mozc",
"//base:version",
"//client",
"//config:config_handler",
"//protocol:commands_cc_proto",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
],
)
94 changes: 94 additions & 0 deletions src/unix/cli/mozc_client_cli.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2010-2021, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <iostream>
#include <sstream>
#include <string>

#include "absl/flags/flag.h"
#include "base/init_mozc.h"
#include "client/client.h"
#include "google/protobuf/json/json.h"
#include "protocol/commands.pb.h"

ABSL_FLAG(std::string, format, "jsonl", "Format for input/output");

namespace mozc::cli {
namespace {
// Main loop, which takes JSON Lines as "mozc.commands.Input"s and
// print corresponding results returned by Mozc server in JSON lines.
bool ProcessLoopJSONL() {
client::Client client;
std::string line;
while (std::getline(std::cin, line)) {
mozc::commands::Input input;
auto status = google::protobuf::json::JsonStringToMessage(line, &input);
if (!status.ok()) {
std::cerr << "Failed to parse an input JSON as mozc.commands.Input: <"
<< line << ">: " << status.ToString() << std::endl;
return false;
}
mozc::commands::Output output;
if (!client.Call(input, &output)) {
return false;
}
std::string outputJSON;
status = google::protobuf::json::MessageToJsonString(output, &outputJSON);
if (!status.ok()) {
std::cerr << "Failed to generate an output JSON for mozc.commands.Output: "
<< status.ToString() << std::endl;
return false;
}
std::cout << outputJSON << std::endl;
}
return true;
}

} // namespace
} // namespace mozc::cli

int main(int argc, char **argv) {
mozc::InitMozc(argv[0], &argc, &argv);

bool success = false;
const auto& format = absl::GetFlag(FLAGS_format);
if (format == "jsonl") {
success = mozc::cli::ProcessLoopJSONL();
} else if (format == "text") {
// TODO
} else if (format == "binary") {
// TODO
} else if (format == "s-expressionl") {
// TODO
} else {
std::cerr << "Unsupported format: <" << format << ">" << std::endl;
}

return success ? 0 : 1;
}