Skip to content
Browse files

Use c++0x features in the syscall printing function

  • Loading branch information...
1 parent fb402d0 commit dc7c1c18d8a9396683e9700cd653e3353910697f @delroth delroth committed
Showing with 40 additions and 39 deletions.
  1. +38 −39 src/print.cc
  2. +2 −0 src/print.hh
View
77 src/print.cc
@@ -1,45 +1,44 @@
-#include <vector>
-#include <boost/lexical_cast.hpp>
#include "print.hh"
-#define SIG_ADD_VECT(Name, Num, Desc) { \
- name[Num] = Name; \
- desc[Num] = Desc; \
- }
-
-std::string str_signal(size_t signal)
-{
- std::vector<std::string> name;
- std::vector<std::string> desc;
-
- // name[0] and desc[0] are unused
- name.resize(20);
- desc.resize(name.size());
+#include <boost/lexical_cast.hpp>
+#include <map>
+#include <vector>
- SIG_ADD_VECT("SIGHUP", 1,
- "Hangup detected on controlling terminal or death of"
- "controlling process");
- SIG_ADD_VECT("SIGINT", 2, "Interrupt from keyboard");
- SIG_ADD_VECT("SIGQUIT", 3, "Quit from keyboard");
- SIG_ADD_VECT("SIGILL", 4, "Illegal Instruction");
- SIG_ADD_VECT("SIGTRAP", 5, "Trace/breakpoint trap");
- SIG_ADD_VECT("SIGABRT", 6, "Abort signal from abort(3)");
- SIG_ADD_VECT("SIGBUS", 7, "Bus error");
- SIG_ADD_VECT("SIGFPE", 8, "Floating point exception");
- SIG_ADD_VECT("SIGKILL", 9, "Kill signal");
- SIG_ADD_VECT("SIGUSR1", 10, "User-defined signal 1");
- SIG_ADD_VECT("SIGSEGV", 11, "Invalid memory reference");
- SIG_ADD_VECT("SIGUSR2", 12, "User-defined signal 2");
- SIG_ADD_VECT("SIGPIPE", 13, "Broken pipe: write to pipe with no readers");
- SIG_ADD_VECT("SIGALRM", 14, "Timer signal from alarm(2)");
- SIG_ADD_VECT("SIGTERM", 15, "Termination signal");
- SIG_ADD_VECT("SIGUSR1", 16, "User-defined signal 1");
- SIG_ADD_VECT("SIGUSR2", 17, "User-defined signal 2");
- SIG_ADD_VECT("SIGCONT", 18, "Continue if stopped");
- SIG_ADD_VECT("SIGSTOP", 19, "Stop process");
+typedef std::map<int, std::pair<std::string, std::string>> sig_descr_t;
- if (signal > 0 && signal < name.size())
- return (name[signal] + " [" + desc[signal] + "]");
+const sig_descr_t signals =
+{
+ { 1, { "SIGHUP", "Hangup detected" }},
+ { 2, { "SIGINT", "Program interrupted" }},
+ { 3, { "SIGQUIT", "Program quitted" }},
+ { 4, { "SIGILL", "Illegal instruction" }},
+ { 5, { "SIGTRAP", "Breakpoint trap reached" }},
+ { 6, { "SIGABRT", "Program aborted" }},
+ { 7, { "SIGBUS", "Bus error (invalid memory access)" }},
+ { 8, { "SIGFPE", "Floating point exception (division by zero?)" }},
+ { 9, { "SIGKILL", "Program killed" }},
+ { 10, { "SIGUSR1", "User-defined signal received" }},
+ { 11, { "SIGSEGV", "Segmentation fault" }},
+ { 12, { "SIGUSR2", "User-defined signal received" }},
+ { 13, { "SIGPIPE", "Broken pipe" }},
+ { 14, { "SIGALRM", "Timer triggered" }},
+ { 15, { "SIGTERM", "Program terminated" }},
+ { 16, { "SIGUSR1", "User-defined signal received" }},
+ { 17, { "SIGUSR2", "User-defined signal received" }},
+ { 18, { "SIGCONT", "Program continued" }},
+ { 19, { "SIGSTOP", "Program stopped" }}
+};
- return boost::lexical_cast<std::string>(signal);
+std::string str_signal(size_t signal)
+{
+ sig_descr_t::const_iterator it = signals.find(signal);
+ if (it != signals.end())
+ {
+ return it->second.first + " (" + it->second.second + ")";
+ }
+ else
+ {
+ std::string s = boost::lexical_cast<std::string>(signal);
+ return std::string("Unknown signal (code ") + s + ")";
+ }
}
View
2 src/print.hh
@@ -1,6 +1,8 @@
#ifndef PRINT_HH_
# define PRINT_HH_
+# include <string>
+
std::string str_signal(size_t signal);
#endif /* !PRINT_HH_ */

0 comments on commit dc7c1c1

Please sign in to comment.
Something went wrong with that request. Please try again.