From a52826e00e42f8ced53a1639fae5678175ce30d2 Mon Sep 17 00:00:00 2001 From: zawz Date: Thu, 15 Aug 2019 14:40:35 +0200 Subject: [PATCH] Added stdin reading capability --- src/main.cpp | 60 ++++++++++++++++++++++++++++++++------------------ src/system.cpp | 2 +- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a43850a..a826508 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "device.hpp" @@ -48,7 +49,8 @@ void cleanup() void stop(int ret) { - kill(announce_thread_pid, SIGINT); + if(announce_thread_pid>0) + kill(announce_thread_pid, SIGINT); exit(ret); } @@ -62,6 +64,10 @@ int main(int argc, char* argv[]) signal(SIGINT, inthandler); signal(SIGCHLD, SIG_IGN); //not expecting returns from child processes + bool piped=false; + if (!isatty(fileno(stdin))) + piped = true; + options.addOption(Option('h',"help", false, "Display this help message")); options.addOption(Option("file-format", false, "Display file format help")); options.addOption(Option("command-tags", false, "Display for command tag help")); @@ -85,49 +91,49 @@ int main(int argc, char* argv[]) if( op->activated ) { help(); - return 0; + stop(0); } op = options.findOption("file-format"); if( op->activated ) { printf("%s\n", FILE_FORMAT); - return 0; + stop(0); } op = options.findOption("command-tags"); if( op->activated ) { printf("%s\n", COMMAND_TAGS); - return 0; + stop(0); } op = options.findOption("shell-format"); if( op->activated ) { printf("%s\n", SHELL_FORMAT); - return 0; + stop(0); } op = options.findOption('h'); if( op->activated ) { help(); - return 0; + stop(0); } op = options.findOption('L'); if( op->activated ) { sh("aseqdump -l"); - return 0; + stop(0); } op = options.findOption('l'); if( op->activated ) { sh(LIST_COMMAND); - return 0; + stop(0); } op = options.findOption('p'); if( op->activated ) { option_p(op->argument); - return 0; + stop(0); } //behavioral options @@ -138,24 +144,36 @@ int main(int argc, char* argv[]) } //no argument: display help + Filedat file; + bool no_arg=false; if (arg.size() <= 0 || arg[0] == "") { - help(); - return 0; + no_arg=true; + if(!piped) + { + help(); + stop(0); + } + } + else + { + file.setFilePath(arg[0]); } - Filedat file(arg[0]); - if (!file.readTest()) - { - fprintf(stderr, "File '%s' unavailable\n", arg[0].c_str()); - return 10; - } //main processing try { - log("Loading map file '" + arg[0] + "'\n"); - file.import_file(); + if(no_arg) + { + log("Loading map from stdin\n"); + file.import_stdin(); + } + else + { + log("Loading map file '" + arg[0] + "'\n"); + file.import_file(); + } //create commands for(int i=0 ; i #include -pid_t announce_thread_pid = -1; +pid_t announce_thread_pid = 0; void device_check() {