]> git.donarmstrong.com Git - lilypond.git/blob - lily/input-file-results.cc
* Documentation/user/changing-defaults.itely (Changing defaults):
[lilypond.git] / lily / input-file-results.cc
1 /*
2   scores.cc -- implement some globals
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "config.h"
9
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <stdio.h>
13
14
15 #if HAVE_SYS_STAT_H 
16 #include <sys/stat.h>
17 #endif
18 #include <unistd.h>
19
20
21 #include "main.hh"
22 #include "score.hh"
23 #include "string.hh"
24 #include "paper-def.hh"
25 #include "warn.hh"
26 #include "parray.hh"
27 #include "file-path.hh"
28 #include "input-file-results.hh"
29 #include "my-lily-parser.hh"
30 #include "source.hh"
31 #include "lily-version.hh"
32 #include "scm-hash.hh"
33 #include "ly-modules.hh"
34
35 bool store_locations_global_b;
36
37 /*
38   TODO: should merge with My_lily_parser. 
39  */
40
41 /*
42   no ! suffix since it doesn't modify 1st argument.
43  */
44 LY_DEFINE(ly_set_point_and_click, "ly:set-point-and-click", 1, 0, 0,
45           (SCM what),
46           "Set the options for Point-and-click source specials output. The\n"
47 "argument is a symbol.  Possible options are @code{none} (no source specials),\n"
48 "@code{line} and @code{line-column}")
49 {
50   /*
51     UGH.
52    */
53   SCM val = SCM_BOOL_F;
54   if (ly_symbol2scm ("line-column") == what)
55     val = gh_eval_str ("line-column-location");
56   else if (what == ly_symbol2scm ("line"))
57     val = gh_eval_str ("line-location");
58
59   extern SCM lily_module; 
60   scm_module_define (lily_module, ly_symbol2scm ("point-and-click"), val);
61
62   store_locations_global_b =gh_procedure_p (val);
63   return SCM_UNSPECIFIED;
64 }
65
66 void
67 write_dependency_file (String fn,
68                        Array<String> targets,
69                        Array<String> deps)
70 {
71   const int WRAPWIDTH = 65;
72
73   progress_indication (_f ("dependencies output to `%s'...", fn.to_str0 ()));
74   progress_indication ("\n");
75   FILE * f = fopen  (fn.to_str0 (), "w");
76   if (!f)
77     warning (_f ("can't open file: `%s'", fn));
78
79   fprintf (f, "# Generated automatically by: %s\n", gnu_lilypond_version_string ().to_str0 ());
80   
81   String out;
82   for (int i=0; i < targets.size (); i ++)
83      out += dependency_prefix_global + targets[i] + " ";
84   out +=  ": ";
85
86   for (int i=0; i < deps.size (); i ++)
87     {
88       if (out.length () > WRAPWIDTH)
89         {
90           fprintf (f, "%s\\\n", out.to_str0 ());
91           out = "  ";
92         }
93       String dep = deps[i];
94       if (!dependency_prefix_global.is_empty ())
95         {
96           if (dep.index ('/') < 0)
97             dep = dependency_prefix_global + dep;
98         }
99       out  += " " +  dep;
100     }
101   fprintf (f, "%s\n",  out.to_str0 ());
102 }
103
104
105 void
106 Input_file_results::do_deps (String output)
107 {
108   if (dependency_global_b)
109     {
110       Path p = split_path (output);
111       p.ext = "dep";
112       write_dependency_file (p.to_string (),
113                              target_strings_,
114                              inclusion_names_);
115     }
116 }
117
118 Input_file_results::~Input_file_results ()
119 {
120   inclusion_names_.clear ();
121   if (header_)
122     header_ = SCM_EOL;
123
124   global_input_file =0;
125
126   ly_clear_anonymous_modules();
127 }
128
129
130
131 Input_file_results* global_input_file;
132
133 Input_file_results::Input_file_results (String init, String in_file, String out_file)
134 {
135   header_ = ly_make_anonymous_module ();
136   global_input_file = this;
137   score_count_ = 0;
138   sources_.set_path (&global_path);
139   
140
141   progress_indication (_f ("Now processing: `%s'", in_file.to_str0 ()));
142   progress_indication ("\n");
143
144   My_lily_parser parser (this);
145   parser.parse_file (init, in_file, out_file);
146   
147   if (parser.error_level_)
148     {
149       exit_status_global  = 1;
150       failed_files.push (in_file);
151     }
152
153   
154   do_deps (out_file);
155 }
156
157 /* Distill full input file name from command argument.  PATH describes
158    file name with added default extension, ".ly" if none.  "-" is
159    STDIN.  */
160 Path
161 distill_inname (String str)
162 {
163   Path p = split_path (str);
164   if (str.is_empty () || str == "-")
165     p.base = "-";
166   else
167     {
168       String orig_ext = p.ext;
169       char const *extensions[] = {"ly", "", 0};
170       for (int i = 0; extensions[i]; i++)
171         {
172           p.ext = orig_ext;
173           if (*extensions[i] && !p.ext.is_empty ())
174             p.ext += ".";
175           p.ext += extensions[i];
176           if (!global_path.find (p.to_string ()).is_empty ())
177               break;
178         }
179       /* Reshuffle extension */
180       p = split_path (p.to_string ());
181     }
182   return p;
183 }
184
185 /* ugr. */
186 void
187 do_one_file (char const *file)
188 {
189   String infile (file);
190   Path inpath = distill_inname (infile);
191   
192   /* By default, use base name of input file for output file name */
193   Path outpath = inpath;
194   if (inpath.to_string () != "-")
195     outpath.ext = output_format_global;
196   
197   /* By default, write output to cwd; do not copy directory part
198      of input file name */
199   outpath.root = "";
200   outpath.dir = "";
201   
202   if (!output_name_global.is_empty ())
203     outpath = split_path (output_name_global);
204   
205   String init;
206   if (!init_name_global.is_empty ())
207     init = init_name_global;
208   else
209     init = "init.ly";
210   
211   String in_file = inpath.to_string ();
212   String out_file = outpath.to_string ();
213   
214 #if 0
215   /* Code to debug memory leaks.  Cannot call from within .ly
216      since then we get the protects from the parser state too. */
217   static SCM proc ;
218   if (!proc)
219         proc = scm_c_eval_string ("dump-gc-protects");
220   scm_gc ();
221   scm_call_0 (proc);
222 #endif
223       
224   if (init.length () && global_path.find (init).is_empty ())
225     {
226       warning (_f ("can't find file: `%s'", init));
227       warning (_f ("(search path: `%s')", global_path.to_string ().to_str0 ()));
228       return;
229     }
230
231   if ((in_file != "-") && global_path.find (in_file).is_empty ())
232     {
233       warning (_f ("can't find file: `%s'", in_file));
234       return;
235     }
236
237   static  SCM proc;
238   if (!proc)
239     proc = scm_c_eval_string ("dump-gc-protects");
240
241   Input_file_results inp_file (init, in_file, out_file);
242 }