+ly_scm2offset (SCM s)
+{
+ return Offset (gh_scm2double (ly_car (s)),
+ gh_scm2double (ly_cdr (s)));
+}
+
+SCM
+ly_type (SCM exp)
+{
+ char const * cp = "unknown";
+ if (gh_number_p (exp))
+ {
+ cp = "number";
+ }
+ else if (gh_string_p (exp))
+ {
+ cp = "string";
+ }
+ else if (gh_procedure_p (exp))
+ {
+ cp = "procedure";
+ }
+ else if (gh_boolean_p (exp))
+ {
+ cp = "boolean";
+ }
+ else if (gh_pair_p (exp))
+ {
+ cp = "list";
+ }
+
+ return ly_str02scm (cp);
+}
+
+/*
+ convert without too many decimals, and leave a space at the end.
+ */
+
+
+SCM
+ly_number2string (SCM s)
+{
+ assert (gh_number_p (s));
+
+ char str[400]; // ugh.
+
+ if (scm_exact_p (s) == SCM_BOOL_F)
+ {
+ Real r (gh_scm2double (s));
+
+ if (isinf (r) || isnan (r))
+ {
+ programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
+ r = 0.0;
+ }
+
+ sprintf (str, "%8.4f ", r);
+ }
+ else
+ {
+ sprintf (str, "%d ", gh_scm2int (s));
+ }
+
+ return ly_str02scm (str);
+}
+
+/*
+ Undef this to see if GUILE GC is causing too many swaps.
+ */
+
+// #define TEST_GC
+
+#ifdef TEST_GC
+#include <libguile/gc.h>
+
+static void *
+greet_sweep (void *dummy1, void *dummy2, void *dummy3)
+{
+ fprintf (stderr, "entering sweep\n");
+}
+
+static void *
+wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
+{
+ fprintf (stderr, "leaving sweep\n");
+}
+#endif
+
+
+#include "version.hh"
+SCM
+ly_version ()
+{
+ char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
+
+ return gh_eval_str ((char*)vs);
+}
+
+static void
+init_functions ()
+{
+ scm_c_define_gsubr ("ly-warn", 1, 0, 0,
+ (Scheme_function_unknown)ly_warning);
+ scm_c_define_gsubr ("ly-version", 0, 0, 0,
+ (Scheme_function_unknown)ly_version);
+ scm_c_define_gsubr ("ly-gulp-file", 1,0, 0,
+ (Scheme_function_unknown)ly_gulp_file);
+ scm_c_define_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
+ scm_c_define_gsubr ("ly-number->string", 1, 0,0,
+ (Scheme_function_unknown) ly_number2string);
+
+
+#ifdef TEST_GC
+ scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
+ scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
+#endif
+}
+
+ADD_SCM_INIT_FUNC (funcs, init_functions);
+
+SCM
+ly_deep_copy (SCM l)
+{
+ if (gh_pair_p (l))
+ {
+ return gh_cons (ly_deep_copy (ly_car (l)), ly_deep_copy (ly_cdr (l)));
+ }
+ else
+ return l;
+}
+
+
+
+
+SCM
+ly_assoc_chain (SCM key, SCM achain)
+{
+ if (gh_pair_p (achain))
+ {
+ SCM handle = scm_assoc (key, ly_car (achain));
+ if (gh_pair_p (handle))
+ return handle;
+ else
+ return ly_assoc_chain (key, ly_cdr (achain));
+ }
+ else
+ return SCM_BOOL_F;
+}
+
+/*
+ LIST has the form "sym1 sym2 sym3"
+ */
+SCM
+parse_symbol_list (const char * list)
+{
+ char * s = strdup (list);
+ char *orig = s;
+ SCM create_list = SCM_EOL;
+ if (!s[0] )
+ s = 0;
+
+ while (s)
+ {
+ char *next = strchr (s, ' ');
+ if (next)
+ *next++ = 0;
+
+ create_list = gh_cons (ly_symbol2scm (s), create_list);
+ s = next;
+ }
+
+ free (orig);
+ return create_list;
+}
+
+
+SCM
+ly_truncate_list (int k, SCM l )
+{
+ if (k == 0)
+ {
+ l = SCM_EOL;
+ }
+ else
+ {
+ SCM s = l;
+ k--;
+ for (; gh_pair_p (s) && k--; s = ly_cdr (s))
+ ;
+
+ if (gh_pair_p (s))
+ {
+ gh_set_cdr_x (s, SCM_EOL);
+ }
+ }
+ return l;
+}
+
+SCM my_gh_symbol2scm (const char* x)