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