Added stdin reading capability

This commit is contained in:
zawz 2019-08-15 14:40:35 +02:00
parent 52fcd6002c
commit a52826e00e
2 changed files with 40 additions and 22 deletions

View file

@ -1,5 +1,6 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include "device.hpp" #include "device.hpp"
@ -48,7 +49,8 @@ void cleanup()
void stop(int ret) void stop(int ret)
{ {
kill(announce_thread_pid, SIGINT); if(announce_thread_pid>0)
kill(announce_thread_pid, SIGINT);
exit(ret); exit(ret);
} }
@ -62,6 +64,10 @@ int main(int argc, char* argv[])
signal(SIGINT, inthandler); signal(SIGINT, inthandler);
signal(SIGCHLD, SIG_IGN); //not expecting returns from child processes 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('h',"help", false, "Display this help message"));
options.addOption(Option("file-format", false, "Display file format help")); options.addOption(Option("file-format", false, "Display file format help"));
options.addOption(Option("command-tags", false, "Display for command tag 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 ) if( op->activated )
{ {
help(); help();
return 0; stop(0);
} }
op = options.findOption("file-format"); op = options.findOption("file-format");
if( op->activated ) if( op->activated )
{ {
printf("%s\n", FILE_FORMAT); printf("%s\n", FILE_FORMAT);
return 0; stop(0);
} }
op = options.findOption("command-tags"); op = options.findOption("command-tags");
if( op->activated ) if( op->activated )
{ {
printf("%s\n", COMMAND_TAGS); printf("%s\n", COMMAND_TAGS);
return 0; stop(0);
} }
op = options.findOption("shell-format"); op = options.findOption("shell-format");
if( op->activated ) if( op->activated )
{ {
printf("%s\n", SHELL_FORMAT); printf("%s\n", SHELL_FORMAT);
return 0; stop(0);
} }
op = options.findOption('h'); op = options.findOption('h');
if( op->activated ) if( op->activated )
{ {
help(); help();
return 0; stop(0);
} }
op = options.findOption('L'); op = options.findOption('L');
if( op->activated ) if( op->activated )
{ {
sh("aseqdump -l"); sh("aseqdump -l");
return 0; stop(0);
} }
op = options.findOption('l'); op = options.findOption('l');
if( op->activated ) if( op->activated )
{ {
sh(LIST_COMMAND); sh(LIST_COMMAND);
return 0; stop(0);
} }
op = options.findOption('p'); op = options.findOption('p');
if( op->activated ) if( op->activated )
{ {
option_p(op->argument); option_p(op->argument);
return 0; stop(0);
} }
//behavioral options //behavioral options
@ -138,24 +144,36 @@ int main(int argc, char* argv[])
} }
//no argument: display help //no argument: display help
Filedat file;
bool no_arg=false;
if (arg.size() <= 0 || arg[0] == "") if (arg.size() <= 0 || arg[0] == "")
{ {
help(); no_arg=true;
return 0; 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 //main processing
try try
{ {
log("Loading map file '" + arg[0] + "'\n"); if(no_arg)
file.import_file(); {
log("Loading map from stdin\n");
file.import_stdin();
}
else
{
log("Loading map file '" + arg[0] + "'\n");
file.import_file();
}
//create commands //create commands
for(int i=0 ; i<file.chunk().listSize() ; i++) for(int i=0 ; i<file.chunk().listSize() ; i++)
@ -179,9 +197,9 @@ int main(int argc, char* argv[])
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr << "Exception: " << e.what() << std::endl; std::cerr << "Exception: " << e.what() << std::endl;
stop(-1); stop(1);
} }
cleanup(); cleanup();
return 0; stop(0);
} }

View file

@ -9,7 +9,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
pid_t announce_thread_pid = -1; pid_t announce_thread_pid = 0;
void device_check() void device_check()
{ {