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