]> sourceware.org Git - systemtap.git/blob - util.h
Add the periodic.stp example to the index
[systemtap.git] / util.h
1 #ifndef UTIL_H
2 #define UTIL_H
3
4 #include "config.h"
5 #include <cstring>
6 #include <cerrno>
7 #include <string>
8 #include <vector>
9 #include <iostream>
10 #include <sstream>
11 #include <stdexcept>
12 #include <cctype>
13 #include <set>
14 #include <iomanip>
15 extern "C" {
16 #include <libintl.h>
17 #include <locale.h>
18 #include <signal.h>
19 #include <stdint.h>
20 #include <spawn.h>
21 #include <assert.h>
22 }
23
24 #include "privilege.h"
25
26 #if ENABLE_NLS
27 #define _(string) gettext(string)
28 #define _N(string, string_plural, count) \
29 ngettext((string), (string_plural), (count))
30 #else
31 #define _(string) (string)
32 #define _N(string, string_plural, count) \
33 ( (count) == 1 ? (string) : (string_plural) )
34 #endif
35 #define _F(format, ...) autosprintf(_(format), __VA_ARGS__)
36 #define _NF(format, format_plural, count, ...) \
37 autosprintf(_N((format), (format_plural), (count)), __VA_ARGS__)
38
39 const char *get_home_directory(void);
40 size_t get_file_size(const std::string &path);
41 size_t get_file_size(int fd);
42 bool file_exists (const std::string &path);
43 bool copy_file(const std::string& src, const std::string& dest,
44 bool verbose=false);
45 int create_dir(const char *dir, int mode = 0777);
46 int remove_file_or_dir(const char *dir);
47 extern "C" gid_t get_gid (const char *group_name);
48 bool in_group_id (gid_t target_gid);
49 std::string getmemusage ();
50 void tokenize(const std::string& str, std::vector<std::string>& tokens,
51 const std::string& delimiters);
52 void tokenize_cxx(const std::string& str, std::vector<std::string>& tokens);
53 std::string find_executable(const std::string& name,
54 const std::string& env_path = "PATH");
55 const std::string cmdstr_quoted(const std::string& cmd);
56 const std::string cmdstr_join(const std::vector<std::string>& cmds);
57 int stap_waitpid(int verbose, pid_t pid);
58 pid_t stap_spawn(int verbose, const std::vector<std::string>& args);
59 pid_t stap_spawn(int verbose, const std::vector<std::string>& args,
60 posix_spawn_file_actions_t* fa, const std::vector<std::string>& envVec = std::vector<std::string> ());
61 pid_t stap_spawn_piped(int verbose, const std::vector<std::string>& args,
62 int* child_in=NULL, int* child_out=NULL, int* child_err=NULL);
63 int stap_system(int verbose, const std::vector<std::string>& args,
64 bool null_out=false, bool null_err=false);
65 int stap_system_read(int verbose, const std::vector<std::string>& args, std::ostream& out);
66 int kill_stap_spawn(int sig);
67 void assert_regexp_match (const std::string& name, const std::string& value, const std::string& re);
68 int regexp_match (const std::string& value, const std::string& re, std::vector<std::string>& matches);
69 bool contains_glob_chars (const std::string &str);
70 std::string escape_glob_chars (const std::string& str);
71 std::string unescape_glob_chars (const std::string& str);
72 std::string kernel_release_from_build_tree (const std::string &kernel_build_tree, int verbose = 0);
73 std::string normalize_machine(const std::string& machine);
74 std::string autosprintf(const char* format, ...) __attribute__ ((format (printf, 1, 2)));
75 const std::set<std::string>& localization_variables();
76
77 // stringification generics
78
79
80 template <typename IN>
81 inline std::string lex_cast(IN const & in)
82 {
83 std::ostringstream ss;
84 if (!(ss << in))
85 throw std::runtime_error(_("bad lexical cast"));
86 return ss.str();
87 }
88
89
90 template <typename OUT>
91 inline OUT lex_cast(std::string const & in)
92 {
93 std::istringstream ss(in);
94 OUT out;
95 if (!(ss >> out && ss.eof()))
96 throw std::runtime_error(_("bad lexical cast"));
97 return out;
98 }
99
100
101 // We want [u]int8_t to be treated numerically, not just extracting a char.
102 template <>
103 inline int8_t lex_cast(std::string const & in)
104 {
105 int16_t out = lex_cast<int16_t>(in);
106 if (out < -128 || out > 127)
107 throw std::runtime_error(_("bad lexical cast"));
108 return out;
109 }
110 template <>
111 inline uint8_t lex_cast(std::string const & in)
112 {
113 uint16_t out = lex_cast<uint16_t>(in);
114 if (out > 0xff && out < 0xff80) // don't error if it looks sign-extended
115 throw std::runtime_error(_("bad lexical cast"));
116 return out;
117 }
118
119
120 template <typename IN>
121 inline std::string
122 lex_cast_hex(IN const & in)
123 {
124 std::ostringstream ss;
125 if (!(ss << std::showbase << std::hex << in << std::dec))
126 throw std::runtime_error(_("bad lexical cast"));
127 return ss.str();
128 }
129
130 //Convert binary data to hex data.
131 template <typename IN>
132 inline std::string
133 hex_dump(IN const & in, size_t len)
134 {
135 std::ostringstream ss;
136 unsigned i;
137 if (!(ss << std::hex << std::setfill('0')))
138 throw std::runtime_error(_("bad lexical cast"));
139
140 for(i = 0; i < len; i++)
141 {
142 int temp = in[i];
143 ss << std::setw(2) << temp;
144 }
145 std::string hex = ss.str();
146 assert(hex.length() == 2 * len);
147 return hex;
148 }
149
150 // Return as quoted string, so that when compiled as a C literal, it
151 // would print to the user out nicely.
152 template <typename IN>
153 inline std::string
154 lex_cast_qstring(IN const & in)
155 {
156 std::stringstream ss;
157 if (!(ss << in))
158 throw std::runtime_error(_("bad lexical cast"));
159 return lex_cast_qstring(ss.str());
160 }
161
162
163 template <>
164 inline std::string
165 lex_cast_qstring(std::string const & in)
166 {
167 std::string out;
168 out += '"';
169 for (const char *p = in.c_str(); *p; ++p)
170 {
171 unsigned char c = *p;
172 if (! isprint(c))
173 {
174 out += '\\';
175 // quick & dirty octal converter
176 out += "01234567" [(c >> 6) & 0x07];
177 out += "01234567" [(c >> 3) & 0x07];
178 out += "01234567" [(c >> 0) & 0x07];
179 }
180 else if (c == '"' || c == '\\')
181 {
182 out += '\\';
183 out += c;
184 }
185 else
186 out += c;
187 }
188 out += '"';
189 return out;
190 }
191
192
193 // Delete all values from a map-like container and clear it
194 // (The template is permissive -- be good!)
195 template <typename T>
196 void delete_map(T& t)
197 {
198 for (typename T::iterator i = t.begin(); i != t.end(); ++i)
199 delete i->second;
200 t.clear();
201 }
202
203
204 // Returns whether a string starts with the given prefix
205 inline bool
206 startswith(const std::string & s, const char * prefix)
207 {
208 return (s.compare(0, std::strlen(prefix), prefix) == 0);
209 }
210
211
212 // Returns whether a string ends with the given suffix
213 inline bool
214 endswith(const std::string & s, const char * suffix)
215 {
216 size_t s_len = s.size(), suffix_len = std::strlen(suffix);
217 if (suffix_len > s_len)
218 return false;
219 return (s.compare(s_len - suffix_len, suffix_len, suffix) == 0);
220 }
221
222
223 // Mask our usual signals for the life of this object.
224 struct stap_sigmasker {
225 sigset_t old;
226 stap_sigmasker()
227 {
228 sigset_t mask;
229 sigemptyset (&mask);
230 sigaddset (&mask, SIGHUP);
231 sigaddset (&mask, SIGPIPE);
232 sigaddset (&mask, SIGINT);
233 sigaddset (&mask, SIGTERM);
234 sigprocmask (SIG_BLOCK, &mask, &old);
235 }
236 ~stap_sigmasker()
237 {
238 sigprocmask (SIG_SETMASK, &old, NULL);
239 }
240 };
241
242 #endif // UTIL_H
243
244 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.047421 seconds and 5 git commands to generate.