summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--buildrun.cxx33
-rw-r--r--elaborate.cxx12
-rw-r--r--main.cxx12
-rw-r--r--staptree.cxx33
-rw-r--r--tapsets.cxx45
-rw-r--r--translate.cxx39
-rw-r--r--util.h67
8 files changed, 80 insertions, 167 deletions
diff --git a/ChangeLog b/ChangeLog
index 3e051548..1b39027f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2006-11-08 Frank Ch. Eigler <fche@elastic.org>
+
+ * util.h (lex_cast_qstring): Move def'n here. Also quote \.
+ (stringify, lex_cast, lex_cast_hex): Move defn here.
+ * buildrun.cxx, elaborate.cxx, main.cxx, staptree.cxx: Adapt.
+
2006-11-07 Frank Ch. Eigler <fche@elastic.org>
* tapsets.cxx (profile_derived_probe_group::emit_module_decls):
diff --git a/buildrun.cxx b/buildrun.cxx
index 969ba352..3673a9b0 100644
--- a/buildrun.cxx
+++ b/buildrun.cxx
@@ -9,6 +9,7 @@
#include "config.h"
#include "buildrun.h"
#include "session.h"
+#include "util.h"
#include <fstream>
#include <sstream>
@@ -28,38 +29,6 @@ extern "C" {
using namespace std;
-// return as quoted string, with at least '"' backslash-escaped
-template <typename IN> inline string
-lex_cast_qstring(IN const & in)
-{
- stringstream ss;
- string out, out2;
- if (!(ss << in))
- throw runtime_error("bad lexical cast");
- out = ss.str();
- out2 += '"';
- for (unsigned i=0; i<out.length(); i++)
- {
- if (out[i] == '"') // XXX others?
- out2 += '\\';
- out2 += out[i];
- }
- out2 += '"';
- return out2;
-}
-
-
-template <typename T>
-static string
-stringify(T t)
-{
- ostringstream s;
- s << t;
- return s.str ();
-}
-
-
-
int
compile_pass (systemtap_session& s)
{
diff --git a/elaborate.cxx b/elaborate.cxx
index c532d0b0..06ddaf36 100644
--- a/elaborate.cxx
+++ b/elaborate.cxx
@@ -11,6 +11,7 @@
#include "parse.h"
#include "tapsets.h"
#include "session.h"
+#include "util.h"
extern "C" {
#include <sys/utsname.h>
@@ -30,17 +31,6 @@ extern "C" {
using namespace std;
-template <typename OUT, typename IN> inline OUT
-lex_cast(IN const & in)
-{
- stringstream ss;
- OUT out;
- if (!(ss << in && ss >> out))
- throw runtime_error("bad lexical cast");
- return out;
-}
-
-
// ------------------------------------------------------------------------
diff --git a/main.cxx b/main.cxx
index 88c55cc2..e7f5d7da 100644
--- a/main.cxx
+++ b/main.cxx
@@ -104,18 +104,6 @@ usage (systemtap_session& s, int exitcode)
}
-// little utility function
-
-template <typename T>
-static string
-stringify(T t)
-{
- ostringstream s;
- s << t;
- return s.str ();
-}
-
-
static void
printscript(systemtap_session& s, ostream& o)
{
diff --git a/staptree.cxx b/staptree.cxx
index fdfc516c..1656f64a 100644
--- a/staptree.cxx
+++ b/staptree.cxx
@@ -9,6 +9,8 @@
#include "config.h"
#include "staptree.h"
#include "parse.h"
+#include "util.h"
+
#include <iostream>
#include <typeinfo>
#include <sstream>
@@ -17,37 +19,6 @@
using namespace std;
-// return as quoted string, with at least '"' backslash-escaped
-template <typename IN> inline string
-lex_cast_qstring(IN const & in)
-{
- stringstream ss;
- string out, out2;
- if (!(ss << in))
- throw runtime_error("bad lexical cast");
- out = ss.str();
- out2 += '"';
- for (unsigned i=0; i<out.length(); i++)
- {
- if (out[i] == '"') // XXX others?
- out2 += '\\';
- out2 += out[i];
- }
- out2 += '"';
- return out2;
-}
-
-
-template <typename OUT, typename IN> inline OUT
-lex_cast(IN const & in)
-{
- stringstream ss;
- OUT out;
- if (!(ss << in && ss >> out))
- throw runtime_error("bad lexical cast");
- return out;
-}
-
expression::expression ():
type (pe_unknown), tok (0)
diff --git a/tapsets.cxx b/tapsets.cxx
index 3de3e5bd..9d0b7ebc 100644
--- a/tapsets.cxx
+++ b/tapsets.cxx
@@ -13,6 +13,7 @@
#include "tapsets.h"
#include "translate.h"
#include "session.h"
+#include "util.h"
#include <deque>
#include <iostream>
@@ -50,50 +51,6 @@ extern "C" {
using namespace std;
-// XXX: should standardize to these functions throughout translator
-
-template <typename OUT, typename IN> inline OUT
-lex_cast(IN const & in)
-{
- stringstream ss;
- OUT out;
- if (!(ss << in && ss >> out))
- throw runtime_error("bad lexical cast");
- return out;
-}
-
-template <typename OUT, typename IN> inline OUT
-lex_cast_hex(IN const & in)
-{
- stringstream ss;
- OUT out;
- if (!(ss << hex << showbase << in && ss >> out))
- throw runtime_error("bad lexical cast");
- return out;
-}
-
-
-// return as quoted string, with at least '"' backslash-escaped
-template <typename IN> inline string
-lex_cast_qstring(IN const & in)
-{
- stringstream ss;
- string out, out2;
- if (!(ss << in))
- throw runtime_error("bad lexical cast");
- out = ss.str();
- out2 += '"';
- for (unsigned i=0; i<out.length(); i++)
- {
- if (out[i] == '"') // XXX others?
- out2 += '\\';
- out2 += out[i];
- }
- out2 += '"';
- return out2;
-}
-
-
#if 0
static void
emit_probe_timing(derived_probe* p, translator_output* o)
diff --git a/translate.cxx b/translate.cxx
index a7406387..bed83ed6 100644
--- a/translate.cxx
+++ b/translate.cxx
@@ -13,6 +13,7 @@
#include "translate.h"
#include "session.h"
#include "tapsets.h"
+#include "util.h"
#include <iostream>
#include <set>
@@ -22,39 +23,6 @@
using namespace std;
-
-// little utility function
-
-template <typename T>
-static string
-stringify(T t)
-{
- ostringstream s;
- s << t;
- return s.str ();
-}
-
-// return as quoted string, with at least '"' backslash-escaped
-template <typename IN> inline string
-lex_cast_qstring(IN const & in)
-{
- stringstream ss;
- string out, out2;
- if (!(ss << in))
- throw runtime_error("bad lexical cast");
- out = ss.str();
- out2 += '"';
- for (unsigned i=0; i<out.length(); i++)
- {
- if (out[i] == '"') // XXX others?
- out2 += '\\';
- out2 += out[i];
- }
- out2 += '"';
- return out2;
-}
-
-
struct var;
struct tmpvar;
struct aggvar;
@@ -2591,6 +2559,11 @@ c_unparser::visit_literal_string (literal_string* e)
const string& v = e->value;
o->line() << '"';
for (unsigned i=0; i<v.size(); i++)
+ // NB: The backslash character is specifically passed through as is.
+ // This is because our parser treats "\" as an ordinary character, not
+ // an escape sequence, leaving it to the C compiler (and this function)
+ // to treat it as such. If we were to escape it, there would be no way
+ // of generating C-level escapes from script code.
if (v[i] == '"') // or other escapeworthy characters?
o->line() << '\\' << '"';
else
diff --git a/util.h b/util.h
index 01f254c7..2c8848bb 100644
--- a/util.h
+++ b/util.h
@@ -1,13 +1,72 @@
#include <string>
#include <vector>
+#include <iostream>
+#include <sstream>
+#include <stdexcept>
-const char *get_home_directory(void);
+const char *get_home_directory(void);
int copy_file(const char *src, const char *dest);
-
int create_dir(const char *dir);
-
void tokenize(const std::string& str, std::vector<std::string>& tokens,
const std::string& delimiters);
-
bool find_executable(const char *name, std::string& retpath);
+
+
+// stringification generics
+
+
+template <typename T>
+inline std::string
+stringify(T t)
+{
+ std::ostringstream s;
+ s << t;
+ return s.str ();
+}
+
+
+template <typename OUT, typename IN>
+inline OUT lex_cast(IN const & in)
+{
+ std::stringstream ss;
+ OUT out;
+ if (!(ss << in && ss >> out))
+ throw std::runtime_error("bad lexical cast");
+ return out;
+}
+
+
+template <typename OUT, typename IN>
+inline OUT
+lex_cast_hex(IN const & in)
+{
+ std::stringstream ss;
+ OUT out;
+ if (!(ss << std::ios::hex << std::ios::showbase << in && ss >> out))
+ throw std::runtime_error("bad lexical cast");
+ return out;
+}
+
+
+// Return as quoted string, so that when compiled as a C literal, it
+// would print to the user out nicely.
+template <typename IN>
+inline std::string
+lex_cast_qstring(IN const & in)
+{
+ std::stringstream ss;
+ std::string out, out2;
+ if (!(ss << in))
+ throw std::runtime_error("bad lexical cast");
+ out = ss.str();
+ out2 += '"';
+ for (unsigned i=0; i<out.length(); i++)
+ {
+ if (out[i] == '"' || out[i] == '\\') // XXX others?
+ out2 += '\\';
+ out2 += out[i];
+ }
+ out2 += '"';
+ return out2;
+}