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