src/relpipe-in-posixmq.cpp
branchv_0
changeset 7 12e975f807ed
parent 3 b71fc3b5e56b
equal deleted inserted replaced
6:65abb0376a0d 7:12e975f807ed
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    16  */
    17 #include <cstdlib>
    17 #include <cstdlib>
       
    18 #include <csignal>
    18 #include <vector>
    19 #include <vector>
    19 #include <memory>
    20 #include <memory>
    20 #include <regex>
    21 #include <regex>
    21 #include <algorithm>
    22 #include <algorithm>
    22 #include <unistd.h>
    23 #include <unistd.h>
    35 
    36 
    36 using namespace relpipe::cli;
    37 using namespace relpipe::cli;
    37 using namespace relpipe::writer;
    38 using namespace relpipe::writer;
    38 using namespace relpipe::in::posixmq;
    39 using namespace relpipe::in::posixmq;
    39 
    40 
       
    41 static std::shared_ptr<PosixMQCommand> command = nullptr;
       
    42 
       
    43 void finish(int sig) {
       
    44 	if (command) command->finish(sig);
       
    45 }
       
    46 
    40 int main(int argc, char** argv) {
    47 int main(int argc, char** argv) {
    41 	setlocale(LC_ALL, "");
    48 	setlocale(LC_ALL, "");
    42 	CLI::untieStdIO();
    49 	CLI::untieStdIO();
    43 	CLI cli(argc, argv);
    50 	CLI cli(argc, argv);
    44 
    51 
    45 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
    52 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
    46 
    53 
    47 	try {
    54 	try {
       
    55 		signal(SIGHUP, finish);
       
    56 		signal(SIGINT, finish);
    48 		CLIParser cliParser;
    57 		CLIParser cliParser;
    49 		Configuration configuration = cliParser.parse(cli.arguments());
    58 		Configuration configuration = cliParser.parse(cli.arguments());
    50 		PosixMQCommand command;
    59 		command.reset(new PosixMQCommand());
    51 		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
    60 		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
    52 		command.process(writer, configuration);
    61 		writer->setBufferingMode(BufferingMode::ENVIRONMENT, BufferingMode::RECORD);
       
    62 		command->process(writer, configuration);
    53 		resultCode = CLI::EXIT_CODE_SUCCESS;
    63 		resultCode = CLI::EXIT_CODE_SUCCESS;
    54 	} catch (RelpipeCLIException e) {
    64 	} catch (RelpipeCLIException e) {
    55 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
    65 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
    56 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
    66 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
    57 		resultCode = e.getExitCode();
    67 		resultCode = e.getExitCode();