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