22 #define _(string) gettext(string)
23 #define _N(string, string_plural, count) \
24 ngettext((string), (string_plural), (count))
26 #define _(string) (string)
27 #define _N(string, string_plural, count) \
28 ( (count) == 1 ? (string) : (string_plural) )
30 #define _F(format, ...) autosprintf(_(format), __VA_ARGS__)
31 #define _NF(format, format_plural, count, ...) \
32 autosprintf(_N((format), (format_plural), (count)), __VA_ARGS__)
34 const char *get_home_directory(void);
35 size_t get_file_size(const std::string
&path
);
36 size_t get_file_size(int fd
);
37 bool file_exists (const std::string
&path
);
38 bool copy_file(const std::string
& src
, const std::string
& dest
,
40 int create_dir(const char *dir
, int mode
= 0777);
41 int remove_file_or_dir(const char *dir
);
42 bool in_group_id (gid_t target_gid
);
43 std::string
getmemusage ();
44 void tokenize(const std::string
& str
, std::vector
<std::string
>& tokens
,
45 const std::string
& delimiters
);
46 std::string
find_executable(const std::string
& name
,
47 const std::string
& env_path
= "PATH");
48 const std::string
cmdstr_quoted(const std::string
& cmd
);
49 const std::string
cmdstr_join(const std::vector
<std::string
>& cmds
);
50 int stap_waitpid(int verbose
, pid_t pid
);
51 pid_t
stap_spawn(int verbose
, const std::vector
<std::string
>& args
);
52 pid_t
stap_spawn(int verbose
, const std::vector
<std::string
>& args
,
53 posix_spawn_file_actions_t
* fa
, const std::vector
<std::string
>& envVec
= std::vector
<std::string
> ());
54 pid_t
stap_spawn_piped(int verbose
, const std::vector
<std::string
>& args
,
55 int* child_in
=NULL
, int* child_out
=NULL
, int* child_err
=NULL
);
56 int stap_system(int verbose
, const std::vector
<std::string
>& args
,
57 bool null_out
=false, bool null_err
=false);
58 int stap_system_read(int verbose
, const std::vector
<std::string
>& args
, std::ostream
& out
);
59 int kill_stap_spawn(int sig
);
60 void assert_regexp_match (const std::string
& name
, const std::string
& value
, const std::string
& re
);
61 int regexp_match (const std::string
& value
, const std::string
& re
, std::vector
<std::string
>& matches
);
62 bool contains_glob_chars (const std::string
&str
);
63 std::string
kernel_release_from_build_tree (const std::string
&kernel_build_tree
, int verbose
= 0);
64 std::string
normalize_machine(const std::string
& machine
);
65 std::string
autosprintf(const char* format
, ...) __attribute__ ((format (printf
, 1, 2)));
66 const std::set
<std::string
>& localization_variables();
68 // stringification generics
71 template <typename IN
>
72 inline std::string
lex_cast(IN
const & in
)
74 std::ostringstream ss
;
76 throw std::runtime_error(_("bad lexical cast"));
81 template <typename OUT
>
82 inline OUT
lex_cast(std::string
const & in
)
84 std::istringstream
ss(in
);
86 if (!(ss
>> out
&& ss
.eof()))
87 throw std::runtime_error(_("bad lexical cast"));
92 // We want [u]int8_t to be treated numerically, not just extracting a char.
94 inline int8_t lex_cast(std::string
const & in
)
96 int16_t out
= lex_cast
<int16_t>(in
);
97 if (out
< -128 || out
> 127)
98 throw std::runtime_error(_("bad lexical cast"));
102 inline uint8_t lex_cast(std::string
const & in
)
104 uint16_t out
= lex_cast
<uint16_t>(in
);
105 if (out
> 0xff && out
< 0xff80) // don't error if it looks sign-extended
106 throw std::runtime_error(_("bad lexical cast"));
111 template <typename IN
>
113 lex_cast_hex(IN
const & in
)
115 std::ostringstream ss
;
116 if (!(ss
<< std::showbase
<< std::hex
<< in
<< std::dec
))
117 throw std::runtime_error(_("bad lexical cast"));
121 //Convert binary data to hex data.
122 template <typename IN
>
124 hex_dump(IN
const & in
, size_t len
)
126 std::ostringstream ss
;
128 if (!(ss
<< std::hex
<< std::setfill('0')))
129 throw std::runtime_error(_("bad lexical cast"));
131 for(i
= 0; i
< len
; i
++)
134 ss
<< std::setw(2) << temp
;
136 std::string hex
= ss
.str();
137 assert(hex
.length() == 2 * len
);
141 // Return as quoted string, so that when compiled as a C literal, it
142 // would print to the user out nicely.
143 template <typename IN
>
145 lex_cast_qstring(IN
const & in
)
147 std::stringstream ss
;
149 throw std::runtime_error(_("bad lexical cast"));
150 return lex_cast_qstring(ss
.str());
156 lex_cast_qstring(std::string
const & in
)
160 for (const char *p
= in
.c_str(); *p
; ++p
)
162 unsigned char c
= *p
;
166 // quick & dirty octal converter
167 out
+= "01234567" [(c
>> 6) & 0x07];
168 out
+= "01234567" [(c
>> 3) & 0x07];
169 out
+= "01234567" [(c
>> 0) & 0x07];
171 else if (c
== '"' || c
== '\\')
184 // Delete all values from a map-like container and clear it
185 // (The template is permissive -- be good!)
186 template <typename T
>
187 void delete_map(T
& t
)
189 for (typename
T::iterator i
= t
.begin(); i
!= t
.end(); ++i
)
195 // Returns whether a string starts with the given prefix
197 startswith(const std::string
& s
, const char * prefix
)
199 return (s
.compare(0, std::strlen(prefix
), prefix
) == 0);
203 // Returns whether a string ends with the given suffix
205 endswith(const std::string
& s
, const char * suffix
)
207 size_t s_len
= s
.size(), suffix_len
= std::strlen(suffix
);
208 if (suffix_len
> s_len
)
210 return (s
.compare(s_len
- suffix_len
, suffix_len
, suffix
) == 0);
214 // Mask our usual signals for the life of this object.
215 struct stap_sigmasker
{
221 sigaddset (&mask
, SIGHUP
);
222 sigaddset (&mask
, SIGPIPE
);
223 sigaddset (&mask
, SIGINT
);
224 sigaddset (&mask
, SIGTERM
);
225 sigprocmask (SIG_BLOCK
, &mask
, &old
);
229 sigprocmask (SIG_SETMASK
, &old
, NULL
);
234 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */