]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-parser-scheme.cc
Bugfix: only append DIRSEP if BASE_
[lilypond.git] / lily / lily-parser-scheme.cc
1 /*
2   lily-parser-scheme.cc -- implement Lily_parser bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <unistd.h>
10
11 #include "file-name.hh"
12 #include "file-path.hh"
13 #include "main.hh"
14 #include "lily-parser.hh"
15 #include "warn.hh"
16 #include "source.hh"
17 #include "lily-lexer.hh"
18 #include "score.hh"
19 #include "lilypond-key.hh"
20 #include "ly-module.hh"
21 #include "output-def.hh"
22 #include "book.hh"
23 #include "paper-book.hh"
24 #include "file-name-map.hh"
25
26 /* Do not append `!' suffix, since 1st argument is not modified. */
27 LY_DEFINE (ly_set_point_and_click, "ly:set-point-and-click",
28            1, 0, 0, (SCM what),
29            "Deprecated.")
30 {
31   (void) what;
32   warning (_f ("deprecated function called: %s", "ly:set-point-and-click"));
33   return SCM_UNSPECIFIED;
34 }
35
36 LY_DEFINE (ly_parse_file, "ly:parse-file",
37            1, 0, 0, (SCM name),
38            "Parse a single @code{.ly} file.  "
39            "Upon failure, throw @code{ly-file-failed} key.")
40 {
41   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
42   char const *file = scm_i_string_chars (name);
43   char const *extensions[] = {"ly", "", 0};
44
45   String file_name = global_path.find (file, extensions);
46
47   /* By default, use base name of input file for output file name,
48      write output to cwd; do not use root and directory parts of input
49      file name.  */
50   File_name out_file_name (file_name);
51
52   global_path.append (out_file_name.dir_);
53
54   out_file_name.ext_ = "";
55   out_file_name.root_ = "";
56   out_file_name.dir_ = "";
57
58   /* When running from gui, generate output in .ly source directory.  */
59   if (output_name_global.is_empty ()
60       && scm_call_0 (ly_lily_module_constant ("running-from-gui?")) == SCM_BOOL_T)
61     {
62       File_name f (file);
63       f.base_ = "";
64       f.ext_ = "";
65       output_name_global = f.to_string ();
66     }
67
68   if (!output_name_global.is_empty ())
69     {
70       /* Interpret --output=DIR to mean --output=DIR/BASE.  */
71       if (is_dir (output_name_global))
72         {
73           char cwd[PATH_MAX];
74           getcwd (cwd, PATH_MAX);
75
76           if (output_name_global != cwd)
77             {
78               global_path.prepend (cwd);
79               message (_f ("Changing working directory to `%s'",
80                            output_name_global.to_str0 ()));
81               chdir (output_name_global.to_str0 ());
82               
83             }
84           output_name_global = "";
85         }
86       else      
87         out_file_name = File_name (output_name_global);
88     }
89
90   String init;
91   if (!init_name_global.is_empty ())
92     init = init_name_global;
93   else
94     init = "init.ly";
95
96   String out_file = out_file_name.to_string ();
97
98   if (init.length () && global_path.find (init).is_empty ())
99     {
100       warning (_f ("can't find init file: `%s'", init));
101       warning (_f ("(search path: `%s')",
102                    global_path.to_string ().to_str0 ()));
103       exit (2);
104     }
105
106   if ((file_name != "-") && global_path.find (file_name).is_empty ())
107     {
108       warning (_f ("can't find file: `%s'", file_name));
109       scm_throw (ly_symbol2scm ("ly-file-failed"),
110                  scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
111     }
112   else
113     {
114       Sources sources;
115       sources.set_path (&global_path);
116
117       String mapped_fn = map_file_name (file_name);
118       message (_f ("Processing `%s'", mapped_fn.to_str0 ()));
119       progress_indication ("\n");
120
121       Lily_parser *parser = new Lily_parser (&sources);
122
123       parser->parse_file (init, file_name, out_file);
124
125       bool error = parser->error_level_;
126       scm_gc_unprotect_object (parser->self_scm ());
127       parser = 0;
128       if (error)
129         /* TODO: pass renamed input file too.  */
130         scm_throw (ly_symbol2scm ("ly-file-failed"),
131                    scm_list_1 (scm_makfrom0str (file_name.to_str0 ())));
132     }
133   return SCM_UNSPECIFIED;
134 }
135
136 LY_DEFINE (ly_parse_string, "ly:parse-string",
137            1, 0, 0, (SCM ly_code),
138            "Parse the string LY_CODE.  "
139            "Upon failure, throw @code{ly-file-failed} key.")
140 {
141   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG1, __FUNCTION__, "string");
142
143   Sources sources;
144   sources.set_path (&global_path);
145   Lily_parser *parser = new Lily_parser (&sources);
146   scm_module_define (global_lily_module, ly_symbol2scm ("parser"),
147                      parser->self_scm ());
148   parser->parse_string (ly_scm2string (ly_code));
149   scm_gc_unprotect_object (parser->self_scm ());
150   parser = 0;
151
152   return SCM_UNSPECIFIED;
153 }
154
155 LY_DEFINE (ly_clone_parser, "ly:clone-parser",
156            1, 0, 0, (SCM parser_smob),
157            "Return a clone of PARSER_SMOB.")
158 {
159   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
160   Lily_parser *clone = new Lily_parser (*parser);
161
162   /* FIXME: should copy scopes too. */
163   return scm_gc_unprotect_object (clone->self_scm ());
164 }
165
166 LY_DEFINE (ly_parser_define, "ly:parser-define",
167            3, 0, 0, (SCM parser_smob, SCM symbol, SCM val),
168            "Bind SYMBOL to VAL in PARSER_SMOB's module.")
169 {
170   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
171   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
172   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
173
174   parser->lexer_->set_identifier (scm_symbol_to_string (symbol), val);
175   return SCM_UNSPECIFIED;
176 }
177
178 LY_DEFINE (ly_parser_lookup, "ly:parser-lookup",
179            2, 0, 0, (SCM parser_smob, SCM symbol),
180            "Lookup @var{symbol} in @var{parser_smob}'s module.  "
181            "Undefined is '().")
182 {
183   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
184
185   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, SCM_ARG2, __FUNCTION__, "symbol");
186   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG2, __FUNCTION__, "parser");
187
188   SCM val = parser->lexer_->lookup_identifier (ly_scm2string (scm_symbol_to_string (symbol)));
189   if (val != SCM_UNDEFINED)
190     return val;
191   else
192     return SCM_EOL;
193 }
194
195 LY_DEFINE (ly_parser_parse_string, "ly:parser-parse-string",
196            2, 0, 0, (SCM parser_smob, SCM ly_code),
197            "Parse the string LY_CODE with PARSER_SMOB."
198            "Upon failure, throw @code{ly-file-failed} key.")
199 {
200   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
201
202   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
203   SCM_ASSERT_TYPE (scm_is_string (ly_code), ly_code, SCM_ARG2, __FUNCTION__, "string");
204
205   parser->parse_string (ly_scm2string (ly_code));
206
207   return SCM_UNSPECIFIED;
208 }
209
210 /* TODO: move this to Scheme?  Why take the parser arg, and all the back
211    & forth between scm and c++? */
212 LY_DEFINE (ly_parser_print_score, "ly:parser-print-score",
213            2, 0, 0,
214            (SCM parser_smob, SCM score_smob),
215            "Print score, i.e., the classic way.")
216 {
217   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
218   Score *score = unsmob_score (score_smob);
219
220   Object_key *key = new Lilypond_general_key (0, score->user_key_, 0);
221
222   if (score->error_found_)
223     return SCM_UNSPECIFIED;
224
225   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "parser");
226   SCM_ASSERT_TYPE (score, score_smob, SCM_ARG2, __FUNCTION__, "score");
227
228   SCM header = ly_c_module_p (score->header_)
229     ? score->header_
230     : parser->lexer_->lookup_identifier ("$globalheader");
231
232   File_name outname (parser->output_basename_);
233   int *c = &parser->book_count_;
234   if (*c)
235     outname.base_ += "-" + to_string (*c);
236   (*c)++;
237
238   SCM os = scm_makfrom0str (outname.to_string ().to_str0 ());
239   SCM paper = get_paper (parser)->self_scm ();
240   for (int i = 0; i < score->defs_.size (); i++)
241     default_rendering (score->get_music (), score->defs_[i]->self_scm (),
242                        paper, header, os, key->self_scm ());
243
244   if (score->defs_.is_empty ())
245     {
246       Output_def *layout = get_layout (parser);
247       default_rendering (score->get_music (),
248                          layout->self_scm (),
249                          paper,
250                          header, os, key->self_scm ());
251       
252       scm_gc_unprotect_object (layout->self_scm ());
253     }
254
255   scm_gc_unprotect_object (paper);
256   scm_gc_unprotect_object (key->self_scm ());
257   return SCM_UNSPECIFIED;
258 }
259
260 LY_DEFINE (ly_parser_set_note_names, "ly:parser-set-note-names",
261            2, 0, 0, (SCM parser, SCM names),
262            "Replace current note names in @var{parser}. "
263            "@var{names} is an alist of symbols.  "
264            "This only has effect if the current mode is notes.")
265 {
266   Lily_parser *p = unsmob_my_lily_parser (parser);
267   SCM_ASSERT_TYPE (p, parser, SCM_ARG1, __FUNCTION__, "Lilypond parser");
268
269   if (p->lexer_->is_note_state ())
270     {
271       p->lexer_->pop_state ();
272       p->lexer_->push_note_state (alist_to_hashq (names));
273     }
274
275   return SCM_UNSPECIFIED;
276 }
277
278 LY_DEFINE (ly_parser_print_book, "ly:parser-print-book",
279            2, 0, 0, (SCM parser_smob, SCM book_smob),
280            "Print book.")
281 {
282   Lily_parser *parser = unsmob_my_lily_parser (parser_smob);
283   Book *book = unsmob_book (book_smob);
284   Output_def *bp = unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"));
285
286   SCM_ASSERT_TYPE (parser, parser_smob, SCM_ARG1, __FUNCTION__, "Lilypond parser");
287   SCM_ASSERT_TYPE (book, book_smob, SCM_ARG2, __FUNCTION__, "Book");
288
289   /*  ugh. changing argument.*/
290   book->paper_ = bp;
291
292   File_name outname (parser->output_basename_);
293   int *c = &parser->book_count_;
294   if (*c)
295     outname.base_ += "-" + to_string (*c);
296   (*c)++;
297
298   Output_def *layout = get_layout (parser);
299   Paper_book *pb = book->process (outname.to_string (), layout);
300
301   if (pb)
302     {
303       pb->output (outname.to_string ());
304       scm_gc_unprotect_object (pb->self_scm ());
305     }
306
307   scm_gc_unprotect_object (layout->self_scm ());
308   return SCM_UNSPECIFIED;
309 }
310