/* This file is part of epubgrep. * Copyright © 2021 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ #include "files.hpp" #include "fs-compat.hpp" #include "options.hpp" #include "search.hpp" #include #include #include #include // For compatibility with fmt 4. #include #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { using namespace epubgrep; using boost::locale::translate; using fmt::format; using std::cerr; using std::cout; using std::string; using std::vector; // locale_generator("").name.c_str() returns "*" instead of "". That's why // the global C locale isn't changed. So we have to set it additionally. std::setlocale(LC_ALL, ""); boost::locale::generator locale_generator; locale_generator.add_messages_path("translations"); locale_generator.add_messages_path("/usr/share/locale"); locale_generator.add_messages_domain("epubgrep"); std::locale::global(locale_generator("")); cout.imbue(std::locale()); cerr.imbue(std::locale()); options::options opts; try { opts = options::parse_options(argc, argv); } catch (std::exception &e) { // Exceptions we can't recover from or ones we don't know. cerr << '\n' << translate("ERROR: ") << e.what() << '\n'; cerr << translate("Error while parsing options.") << '\n'; return EXIT_FAILURE; } if (opts.help || opts.version) { return EXIT_SUCCESS; } int return_code{EXIT_SUCCESS}; vector input_files; if (opts.input_file.empty()) { cout << "NO INPUT FILE\n"; // TODO: Read data from stdin. return EXIT_FAILURE; } for (const auto &filepath : opts.input_file) { if (!opts.recursive && !opts.dereference_recursive) { input_files.emplace_back(filepath); } else { try { auto files_in_dir{ files::list_recursive(filepath, opts.dereference_recursive)}; input_files.insert(input_files.end(), files_in_dir.begin(), files_in_dir.end()); } catch (const fs::filesystem_error &e) { if (e.code().value() == 20) { // Is not a directory. input_files.emplace_back(filepath); continue; } cerr << '\n' << format(translate("ERROR: Could not open {0:s}: {1:s}") .str() .data(), e.path1(), e.what()) << '\n'; return_code = EXIT_FAILURE; } } } search::settings search_settings; search_settings.regex = opts.regex; search_settings.grep_like = opts.grep; search_settings.ignore_case = opts.ignore_case; search_settings.raw = opts.raw; search_settings.context = opts.context; vector> matches_all; vector> futurepool; auto search_file{ [&opts, &matches_all, &search_settings](fs::path filepath) { for (const auto ®ex : opts.regexp) { try { matches_all.emplace_back( search::search(filepath, regex, search_settings)); } catch (const std::exception &e) { cerr << '\n' << translate("ERROR: ") << e.what() << '\n'; cerr << format(translate("Error while searching {0:s}.") .str() .data(), filepath) << '\n'; return EXIT_FAILURE; } } return EXIT_SUCCESS; }}; auto futures_cleanup{ [&futurepool, &return_code](const bool wait = false) { using namespace std::chrono_literals; for (auto it{futurepool.begin()}; it != futurepool.end();) { if (!wait && it->wait_for(100ms) != std::future_status::ready) { ++it; continue; } if (int ret{}; (ret = it->get()) != EXIT_SUCCESS) { return_code = ret; } futurepool.erase(it); } return EXIT_SUCCESS; }}; const auto max_threads{ [] { auto n{static_cast(std::thread::hardware_concurrency())}; return static_cast(std::ceil(n / 2 + n / 4)); }()}; for (const auto &filepath : input_files) { if (futurepool.size() >= max_threads) { futures_cleanup(); } futurepool.emplace_back( std::async(std::launch::async, search_file, filepath)); } futures_cleanup(true); for (const auto &matches_file : matches_all) { fs::path last_epub; for (const auto &match : matches_file) { if (input_files.size() > 1 && !opts.no_fn_fs) { if (match.epub_filepath != last_epub) { if (!opts.nocolor) { cout << termcolor::yellow; } cout << format(translate(" In {0:s}: \n").str().data(), fs::relative(match.epub_filepath)); last_epub = match.epub_filepath; if (!opts.nocolor) { cout << termcolor::reset; } } } vector prefix; if (!opts.no_fn_epub) { prefix.emplace_back(match.filepath); } if (!match.headline.empty()) { prefix.emplace_back(match.headline); } if (!match.page.empty()) { prefix.emplace_back("page " + match.page); } for (const auto &part : prefix) { cout << part; if (part != *(prefix.rbegin())) { cout << ", "; } } cout << ": " << match.context.first; if (!opts.nocolor) { cout << termcolor::bright_magenta; } cout << match.text; if (!opts.nocolor) { cout << termcolor::reset; } cout << match.context.second << '\n'; } } return return_code; }