lilypond-1.1.15
[lilypond.git] / lily / main.cc
blob38dc2cb17315baa411a1f57aa1705c516bc4c4de
1 /*
2 main.cc -- implement main: entrypoints
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <stdlib.h>
10 #include <iostream.h>
11 #include <assert.h>
12 #include <locale.h>
13 #include "lily-guile.hh"
15 #include "proto.hh"
16 #include "dimensions.hh"
17 #include "plist.hh"
18 #include "getopt-long.hh"
19 #include "misc.hh"
20 #include "string.hh"
21 #include "main.hh"
22 #include "file-path.hh"
23 #include "config.hh"
24 #include "file-results.hh"
25 #include "debug.hh"
26 #include "lily-guile.hh"
27 #include "paper-def.hh"
28 #include "midi-def.hh"
30 #if HAVE_GETTEXT
31 #include <libintl.h>
32 #endif
35 bool version_ignore_global_b = false;
36 bool no_paper_global_b = false;
37 bool no_timestamps_global_b = false;
38 bool find_old_relative_b = false;
40 char const* output_global_ch = "tex";
42 String default_outname_base_global = "lelie";
43 String outname_str_global;
44 String init_str_global;
46 int default_count_global;
47 File_path global_path;
49 bool safe_global_b = false;
50 bool experimental_features_global_b = false;
51 bool dependency_global_b = false;
53 int exit_status_i_;
55 Getopt_long * oparser_global_p = 0;
57 String distill_inname_str (String name_str, String& ext_r);
59 Long_option_init theopts[] = {
60 {0, "about", 'a'},
61 {1, "output", 'o'},
62 {0, "warranty", 'w'},
63 {0, "help", 'h'},
64 {0, "test", 't'},
65 {0, "debug", 'D'},
66 {1, "init", 'i'},
67 {1, "include", 'I'},
68 {0, "no-paper", 'M'},
69 {0, "dependencies", 'd'},
70 {0, "no-timestamps", 'T'},
71 {0, "find-old-relative", 'Q'},
72 {0, "ignore-version", 'V'},
73 {1, "output-format", 'f'},
74 {0, "safe", 's'},
75 {0,0,0}
78 void
79 usage ()
81 cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond") << '\n';
82 cout << _ ("Typeset music and or play MIDI from FILE or <stdin>");
83 cout << '\n';
84 cout << '\n';
85 cout << _ ("Options:");
86 cout << '\n';
87 cout << _ (
88 " -a, --about about LilyPond\n"
90 cout << _ (
91 " -D, --debug enable debugging output\n"
93 cout << _ (
94 " -d, --dependencies write Makefile dependencies for every input file\n"
96 cout << _ (
97 " -h, --help this help\n"
99 cout << _ (
100 " -f, --output-format=X use output format X\n"
102 cout << _ (
103 " -I, --include=DIR add DIR to search path\n"
105 cout << _ (
106 " -i, --init=FILE use FILE as init file\n"
108 cout << _ (
109 " -M, --no-paper produce midi output only\n"
111 cout << _ (
112 " -o, --output=FILE set FILE as default output base\n"
114 cout << _ (
115 " -Q, --find-old-relative show all changes in relative syntax\n"
117 cout << _ (
118 " -s, --safe inhibit file output naming and exporting TeX macros\n");
119 cout << _ (
120 " -t, --test switch on experimental features\n"
122 cout << _ (
123 " -T, --no-timestamps don't timestamp the output\n"
125 cout << _ (
126 " -V, --ignore-version ignore mudela version\n"
128 cout << _ (
129 " -w, --warranty show warranty and copyright\n"
131 cout << '\n';
132 cout << _ ("GNU LilyPond was compiled with the following settings:");
133 cout << '\n';
134 cout <<
135 #ifdef NDEBUG
136 "NDEBUG "
137 #endif
138 #ifdef NPRINT
139 "NPRINT "
140 #endif
141 #ifdef STRING_UTILS_INLINED
142 "STRING_UTILS_INLINED "
143 #endif
144 "datadir=" DIR_DATADIR
145 "\n"
146 "localedir=" DIR_LOCALEDIR
148 "\n";
153 void
154 about ()
156 cout << '\n';
157 cout <<
158 #include "BLURB.hh"
159 cout << '\n';
160 cout << _ ("GNU LilyPond is Free software, see --warranty");
161 cout << '\n';
162 cout << '\n';
163 cout << _f ("Copyright (c) %s by", "1996, 1997, 1998");
164 cout << '\n';
165 cout << " " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
166 cout << " " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
167 cout << '\n';
170 void
171 notice ()
173 cout << '\n';
174 cout << _ ("GNU LilyPond -- The GNU Project music typesetter");
175 cout << '\n';
176 cout << _f ("Copyright (c) %s by", "1996, 1997, 1998");
177 cout << '\n';
178 cout << " " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
179 cout << " " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
180 cout << '\n';
181 cout << _ (
182 " This program is free software; you can redistribute it and/or\n"
183 "modify it under the terms of the GNU General Public License version 2\n"
184 "as published by the Free Software Foundation.\n"
185 "\n"
186 " This program is distributed in the hope that it will be useful,\n"
187 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
188 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
189 "General Public License for more details.\n"
190 "\n"
191 " You should have received a copy (refer to the file COPYING) of the\n"
192 "GNU General Public License along with this program; if not, write to\n"
193 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
194 "USA.\n");
197 void
198 identify ()
200 *mlog << get_version_str () << endl;
203 void
204 setup_paths ()
206 // facilitate binary distributions
207 char const *env_lily = getenv ("LILYPONDPREFIX");
208 String prefix_directory;
209 if (env_lily)
210 prefix_directory = env_lily;
212 #if HAVE_GETTEXT
213 setlocale (LC_ALL, ""); /* enable locales */
214 setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
215 String lily_locale_dir;
216 String name (PACKAGE);
217 name.to_lower ();
218 if (!prefix_directory.empty_b())
220 lily_locale_dir = prefix_directory + "/share/locale";
221 bindtextdomain (name.ch_C (), lily_locale_dir.ch_C());
223 else
224 bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
225 textdomain (name.ch_C ());
226 #endif
228 global_path.add ("");
229 // must override (come before) "/usr/local/share/lilypond"!
230 char const *env_sz = getenv ("LILYINCLUDE");
231 if (env_sz)
232 global_path.parse_path (env_sz);
235 char *suffixes[] = {"ly", "afm", "scm", "init", 0};
236 for (char **s = suffixes; *s; s++){
237 if (!prefix_directory.empty_b())
238 global_path.add (prefix_directory + to_str ('/') + String (*s));
239 else
240 global_path.add (String (DIR_DATADIR) + to_str ('/') + String(*s));
245 void
246 main_prog (int argc, char **argv)
248 default_outname_base_global = "lelie";
250 int p=0;
251 const char *arg ;
252 while ((arg= oparser_global_p->get_next_arg ()))
255 if (outname_str_global == "")
257 Midi_def::reset_default_count ();
258 Paper_def::reset_default_count ();
260 String f (arg);
261 String i;
262 f = distill_inname_str (f, i);
263 if (f == "-")
264 default_outname_base_global = "-";
265 else
267 String a,b,c,d;
268 split_path (f, a, b, c, d);
269 default_outname_base_global = c;
271 if (outname_str_global.length_i ())
272 default_outname_base_global = outname_str_global;
273 if (init_str_global.length_i ())
274 i = init_str_global;
275 else
276 i = "init" + i;
277 do_one_file (i, f);
278 p++;
280 if (!p)
282 String i;
283 if (init_str_global.length_i ())
284 i = init_str_global;
285 else
286 i = "init.ly";
287 default_outname_base_global = "-";
288 if (outname_str_global.length_i ())
289 default_outname_base_global = outname_str_global;
290 do_one_file (i, default_outname_base_global);
292 delete oparser_global_p;
293 exit( exit_status_i_);
297 main (int argc, char **argv)
299 identify ();
300 call_constructors ();
301 debug_init (); // should be first
303 setup_paths ();
305 oparser_global_p = new Getopt_long(argc, argv,theopts);
306 while (Long_option_init const * opt = (*oparser_global_p)())
308 switch (opt->shortname)
310 case 't':
311 experimental_features_global_b = true;
312 *mlog << "*** enabling experimental features, you're on your own now ***\n";
313 break;
314 case 'o':
315 outname_str_global = oparser_global_p->optional_argument_ch_C_;
316 break;
317 case 'w':
318 notice ();
319 exit (0);
320 break;
321 case 'f':
322 output_global_ch = oparser_global_p->optional_argument_ch_C_;
323 break;
324 case 'Q':
325 find_old_relative_b= true;
326 break;
327 case 'I':
328 global_path.push (oparser_global_p->optional_argument_ch_C_);
329 break;
330 case 'i':
331 init_str_global = oparser_global_p->optional_argument_ch_C_;
332 break;
333 case 'a':
334 about ();
335 exit (0);
336 case 'h':
337 usage ();
338 exit (0);
339 break;
340 case 'V':
341 version_ignore_global_b = true;
342 break;
343 case 's':
344 safe_global_b = true;
345 break;
346 case 'd':
347 dependency_global_b = true;
348 break;
349 case 'D':
350 set_debug (true);
351 break;
352 case 'M':
353 no_paper_global_b = true;
354 break;
355 case 'T':
356 no_timestamps_global_b = true;
357 break;
358 default:
359 assert (false);
360 break;
364 gh_enter (argc, argv, (void(*)())main_prog);
365 return 0; // unreachable
369 make input file name from command arg.
371 @input file name
373 @output file name with added default extension. "" is stdin.
374 in reference argument: the extension. ".ly" if none
376 String
377 distill_inname_str (String name_str, String& ext_r)
379 String str = name_str;
380 if (str.length_i ())
382 if (str != "-")
384 String a,b,c;
385 split_path (str,a,b,c,ext_r);
387 // add extension if not present.
388 // UGH. Should parametrise in list of default extensions.
389 if (ext_r.empty_b ())
391 ext_r = ".fly";
392 if (global_path.find (a+b+c+ext_r).empty_b ())
393 ext_r = ".ly";
395 str = a+b+c+ext_r;
398 else
400 str = "-";
401 ext_r = ".ly";
403 return str;