type conversion - C++ signal names passed from program parameters -
this question has answer here:
so i've got problem want call program parameter main function (let's called this: program sigquit) , pass parameter kill() , signal() functions in code.
the question i'm kindly asking is: how integer signal number char type argv[2] contains "sigquit"?
why don't build lookup table string , corresponding int
? if need signals defined on system, need like:
#include <csignal> #include <map> #include <string> #include <iostream> #include <cassert> int main(int argc, char *argv[]){ // lookup table std::map<std::string, int> lookup_table; #define add_signal(x) lookup_table.insert(std::make_pair(#x,x)); // standard signals add_signal(sigabrt); add_signal(sigfpe); add_signal(sigill); add_signal(sigint); add_signal(sigsegv); add_signal(sigterm); // implementation specific signals #ifdef sigkill add_signal(sigkill); #endif #ifdef sigstop add_signal(sigstop); #endif // ... #undef add_signal assert(argc > 1 && "no signal"); std::string sig = argv[1]; std::map<std::string, int>::iterator = lookup_table.find(sig); if(it != lookup_table.end()) std::cout << "signal " << it->first << ": " << it-> second << std::endl; else std::cout << "unknown signal " << sig << std::endl; }
it bit long write signals, can use command line (assuming posix) generate them:
kill -l | sed 's/[0-9]*)//g' | grep -v "[+-]" | xargs -n1 echo | awk '{ print "#ifdef " $0 "\n\tadd_signal(" $0 ")\n#endif" }'
Comments
Post a Comment