]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/lily-parser-scheme.cc
clean up Sources
[lilypond.git] / lily / lily-parser-scheme.cc
index 7603487665096a28753677ece6aaf5de9d4a121b..a1cdfa3c035f9b5d7d26db9f3d81f7928f9930bd 100644 (file)
@@ -11,6 +11,7 @@
 #include "file-name-map.hh"
 #include "file-name.hh"
 #include "file-path.hh"
+#include "input.hh"
 #include "international.hh"
 #include "lily-lexer.hh"
 #include "lily-parser.hh"
@@ -36,7 +37,7 @@ LY_DEFINE (ly_parse_file, "ly:parse-file",
           "Upon failure, throw @code{ly-file-failed} key.")
 {
   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
-  char const *file = scm_i_string_chars (name);
+  string file = ly_scm2string (name);
   char const *extensions[] = {"ly", "", 0};
 
   string file_name = global_path.find (file, extensions);
@@ -64,20 +65,30 @@ LY_DEFINE (ly_parse_file, "ly:parse-file",
 
   if (!output_name_global.empty ())
     {
+      
       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
+      string dir;
       if (is_dir (output_name_global))
        {
-         char cwd[PATH_MAX];
-         getcwd (cwd, PATH_MAX);
-
-         if (output_name_global != cwd)
+         dir = output_name_global;
+         output_name_global = "";
+       }
+      else
+       {
+         File_name out (output_name_global);
+         if (is_dir (out.dir_part ()))
            {
-             global_path.prepend (cwd);
-             message (_f ("Changing working directory to `%s'",
-                          output_name_global.c_str ()));
-             chdir (output_name_global.c_str ());
+             dir = out.dir_part ();
+             out_file_name = out.file_part ();
            }
-         output_name_global = "";
+       }         
+
+      if (dir != "" && dir != "." && dir != get_working_directory ())
+       {
+         global_path.prepend (get_working_directory ());
+         message (_f ("Changing working directory to `%s'",
+                      dir.c_str ()));
+         chdir (dir.c_str ());
        }
       else
        out_file_name = File_name (output_name_global);
@@ -99,11 +110,12 @@ LY_DEFINE (ly_parse_file, "ly:parse-file",
       exit (2);
     }
 
-  if ((file_name != "-") && global_path.find (file_name).empty ())
+
+  bool error = false;
+  if ((file_name != "-") && file_name.empty ())
     {
-      warning (_f ("can't find file: `%s'", file_name));
-      scm_throw (ly_symbol2scm ("ly-file-failed"),
-                scm_list_1 (scm_makfrom0str (file_name.c_str ())));
+      warning (_f ("can't find file: `%s'", file));
+      error = true;
     }
   else
     {
@@ -118,14 +130,20 @@ LY_DEFINE (ly_parse_file, "ly:parse-file",
 
       parser->parse_file (init, file_name, out_file);
 
-      bool error = parser->error_level_;
+      error = parser->error_level_;
+
+      parser->clear ();
       parser->unprotect ();
-      parser = 0;
-      if (error)
-       /* TODO: pass renamed input file too.  */
-       scm_throw (ly_symbol2scm ("ly-file-failed"),
-                  scm_list_1 (scm_makfrom0str (file_name.c_str ())));
     }
+
+  /*
+    outside the if-else to ensure cleanup fo Sources object, 
+   */
+  if (error)
+    /* TODO: pass renamed input file too.  */
+    scm_throw (ly_symbol2scm ("ly-file-failed"),
+              scm_list_1 (scm_makfrom0str (file_name.c_str ())));
+  
   return SCM_UNSPECIFIED;
 }
 
@@ -228,4 +246,20 @@ LY_DEFINE (ly_parser_output_name, "ly:parser-output-name",
   return scm_makfrom0str (p->output_basename_.c_str ());
 }
 
+LY_DEFINE (ly_parser_error, "ly:parser-error",
+          2, 1, 0, (SCM parser, SCM msg, SCM input),
+          "Display an error message, and make the parser fail")
+{
+  Lily_parser *p = unsmob_lily_parser (parser);
+  SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
+  SCM_ASSERT_TYPE (scm_is_string (msg), msg, SCM_ARG2, __FUNCTION__, "string");
+  string s = ly_scm2string (msg);
+  
+  Input *i = unsmob_input (input);
+  if (i)
+    p->parser_error (*i, s);
+  else
+    p->parser_error (s);
 
+  return parser;
+}