summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <frankhwei536@gmail.com>2014-04-12 01:38:41 +0100
committerFranklin Wei <frankhwei536@gmail.com>2014-04-12 01:38:41 +0100
commiteac75c1807941382e424da830baba6dbbbca5c72 (patch)
treef66a62684797f3339a8940bad2e3c2645cc8c805
parent8200cce66420b646b690ba191e755c1ecf4414bd (diff)
downloadpish-eac75c1807941382e424da830baba6dbbbca5c72.zip
pish-eac75c1807941382e424da830baba6dbbbca5c72.tar.gz
pish-eac75c1807941382e424da830baba6dbbbca5c72.tar.bz2
pish-eac75c1807941382e424da830baba6dbbbca5c72.tar.xz
Added and implemented signal behavior
-rw-r--r--SPEC3
-rw-r--r--main.cpp22
2 files changed, 22 insertions, 3 deletions
diff --git a/SPEC b/SPEC
index 57b23c8..890c0c8 100644
--- a/SPEC
+++ b/SPEC
@@ -10,4 +10,5 @@
- morse STRING - transmit STRING in Morse code
- exec CMD [ARGS] - execute the shell command CMD with the arguments ARGS in the current working directory
- cd PATH - change the current working directory to PATH
- - pwd - transmit the current working directory in Morse code \ No newline at end of file
+ - pwd - transmit the current working directory in Morse code
+9. Upon receiving ANY signal, PiSH shall output two dashes and exit. \ No newline at end of file
diff --git a/main.cpp b/main.cpp
index 9d7b2e8..f04e5ea 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,19 +1,37 @@
+#include <cstdlib>
#include <unistd.h>
#include <led.h>
#include <morse.h>
#include <iostream> // let the user use this with a screen, too
#include <parse.h>
+#include <csignal>
using namespace std;
void parse_args(int argc, char* argv[])
{
// nothing for now
}
+void prepare_to_exit(int signum)
+{
+ cerr << "Received signal " << signum << ", exiting." << endl;
+ dash();
+ usleep(250000);
+ dash();
+ exit(signum);
+}
int main(int argc, char* argv[])
{
if(geteuid()!=0) // must be root to control LEDS!
{
- cerr << "Need to be root to control LEDs!"<< endl;
- cerr << "PiSH will work fine, but the LEDs can not be used." << endl;
+ int ret=seteuid(0);
+ if(!ret)
+ {
+ cerr << "Cannot become root."<< endl;
+ cerr << "LEDs will not be used." << endl;
+ }
+ }
+ for(int i=0;i<_NSIG;++i) // handle any and all signals by exiting
+ {
+ signal(i, &prepare_to_exit);
}
parse_args(argc, argv);
init_morse();