1*ec63e07aSXin Li // Copyright 2022 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li // https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li
15*ec63e07aSXin Li #include <unistd.h>
16*ec63e07aSXin Li
17*ec63e07aSXin Li #include <fstream>
18*ec63e07aSXin Li #include <iostream>
19*ec63e07aSXin Li #include <string>
20*ec63e07aSXin Li
21*ec63e07aSXin Li #include "absl/flags/parse.h"
22*ec63e07aSXin Li #include "absl/log/globals.h"
23*ec63e07aSXin Li #include "absl/log/initialize.h"
24*ec63e07aSXin Li #include "contrib/hunspell/sandboxed.h"
25*ec63e07aSXin Li
PrintSuggest(HunspellApi & api,sapi::v::RemotePtr & hunspellrp,sapi::v::ConstCStr & word)26*ec63e07aSXin Li absl::Status PrintSuggest(HunspellApi& api, sapi::v::RemotePtr& hunspellrp,
27*ec63e07aSXin Li sapi::v::ConstCStr& word) {
28*ec63e07aSXin Li sapi::v::GenericPtr outptr;
29*ec63e07aSXin Li
30*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(
31*ec63e07aSXin Li int nlist,
32*ec63e07aSXin Li api.Hunspell_suggest(&hunspellrp, outptr.PtrAfter(), word.PtrBefore()));
33*ec63e07aSXin Li
34*ec63e07aSXin Li if (nlist == 0) {
35*ec63e07aSXin Li std::cout << "No suggestions.\n";
36*ec63e07aSXin Li return absl::OkStatus();
37*ec63e07aSXin Li }
38*ec63e07aSXin Li
39*ec63e07aSXin Li sapi::v::Array<char*> ptr_list(nlist);
40*ec63e07aSXin Li ptr_list.SetRemote(reinterpret_cast<void*>(outptr.GetValue()));
41*ec63e07aSXin Li
42*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(api.GetSandbox()->TransferFromSandboxee(&ptr_list));
43*ec63e07aSXin Li
44*ec63e07aSXin Li std::cout << "Suggestions:\n";
45*ec63e07aSXin Li for (int i = 0; i < nlist; i++) {
46*ec63e07aSXin Li sapi::v::RemotePtr sugrp(ptr_list[i]);
47*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(std::string sugestion,
48*ec63e07aSXin Li api.GetSandbox()->GetCString(sugrp));
49*ec63e07aSXin Li std::cout << sugestion[i] << "\n";
50*ec63e07aSXin Li }
51*ec63e07aSXin Li
52*ec63e07aSXin Li api.Hunspell_free_list(&hunspellrp, ptr_list.PtrNone(), nlist).IgnoreError();
53*ec63e07aSXin Li
54*ec63e07aSXin Li return absl::OkStatus();
55*ec63e07aSXin Li }
56*ec63e07aSXin Li
main(int argc,char * argv[])57*ec63e07aSXin Li int main(int argc, char* argv[]) {
58*ec63e07aSXin Li absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
59*ec63e07aSXin Li absl::ParseCommandLine(argc, argv);
60*ec63e07aSXin Li absl::InitializeLog();
61*ec63e07aSXin Li
62*ec63e07aSXin Li if (argc != 4) {
63*ec63e07aSXin Li std::cerr << "Usage:\n " << argv[0];
64*ec63e07aSXin Li std::cerr << " AFFIX_FILE FICTIONARY_FILE WORDS_TO_CHECK_FILE\n";
65*ec63e07aSXin Li return EXIT_FAILURE;
66*ec63e07aSXin Li }
67*ec63e07aSXin Li
68*ec63e07aSXin Li sapi::v::ConstCStr affix_file_name(argv[1]);
69*ec63e07aSXin Li sapi::v::ConstCStr dictionary_file_name(argv[2]);
70*ec63e07aSXin Li
71*ec63e07aSXin Li HunspellSapiSandbox sandbox(affix_file_name.GetData(),
72*ec63e07aSXin Li dictionary_file_name.GetData());
73*ec63e07aSXin Li if (!sandbox.Init().ok()) {
74*ec63e07aSXin Li std::cerr << "Unable to start sandbox\n";
75*ec63e07aSXin Li return EXIT_FAILURE;
76*ec63e07aSXin Li }
77*ec63e07aSXin Li
78*ec63e07aSXin Li HunspellApi api(&sandbox);
79*ec63e07aSXin Li absl::StatusOr<Hunhandle*> hunspell = api.Hunspell_create(
80*ec63e07aSXin Li affix_file_name.PtrBefore(), dictionary_file_name.PtrBefore());
81*ec63e07aSXin Li if (!hunspell.ok()) {
82*ec63e07aSXin Li std::cerr << "Could not initialize hunsepll\n";
83*ec63e07aSXin Li return EXIT_FAILURE;
84*ec63e07aSXin Li }
85*ec63e07aSXin Li sapi::v::RemotePtr hunspellrp(*hunspell);
86*ec63e07aSXin Li
87*ec63e07aSXin Li std::ifstream word_to_check_list(argv[3], std::ios_base::in);
88*ec63e07aSXin Li if (!word_to_check_list.is_open()) {
89*ec63e07aSXin Li std::cerr << "Could not open file of words to check\n";
90*ec63e07aSXin Li return EXIT_FAILURE;
91*ec63e07aSXin Li }
92*ec63e07aSXin Li
93*ec63e07aSXin Li std::string buf;
94*ec63e07aSXin Li while (std::getline(word_to_check_list, buf)) {
95*ec63e07aSXin Li sapi::v::ConstCStr cbuf(buf.c_str());
96*ec63e07aSXin Li absl::StatusOr<int> result =
97*ec63e07aSXin Li api.Hunspell_spell(&hunspellrp, cbuf.PtrBefore());
98*ec63e07aSXin Li if (!result.ok()) {
99*ec63e07aSXin Li std::cerr << "Could not check word\n" << result.status() << std::endl;
100*ec63e07aSXin Li return EXIT_FAILURE;
101*ec63e07aSXin Li }
102*ec63e07aSXin Li
103*ec63e07aSXin Li if (*result) {
104*ec63e07aSXin Li std::cout << "Word " << buf << " is ok\n";
105*ec63e07aSXin Li } else {
106*ec63e07aSXin Li std::cout << "Word " << buf << " is incorrect\n";
107*ec63e07aSXin Li absl::Status status = PrintSuggest(api, hunspellrp, cbuf);
108*ec63e07aSXin Li if (!status.ok()) {
109*ec63e07aSXin Li std::cerr << "Unable to get all suggestion\n" << status << std::endl;
110*ec63e07aSXin Li }
111*ec63e07aSXin Li }
112*ec63e07aSXin Li }
113*ec63e07aSXin Li
114*ec63e07aSXin Li api.Hunspell_destroy(&hunspellrp).IgnoreError();
115*ec63e07aSXin Li
116*ec63e07aSXin Li return EXIT_SUCCESS;
117*ec63e07aSXin Li }
118