lilypond-1.3.147
[lilypond.git] / lily / main.cc
blobfe1d00cef8ab7d2e04304ae5d4012441895ca614
1 /*
2 main.cc -- implement main: entrypoints
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <stdlib.h>
10 #include <iostream.h>
11 #include <assert.h>
12 #include <locale.h>
14 #include "config.h"
16 #if HAVE_GETTEXT
17 #include <libintl.h>
18 #endif
20 #include "lily-guile.hh"
21 #include "lily-version.hh"
22 #include "all-font-metrics.hh"
23 #include "getopt-long.hh"
24 #include "misc.hh"
25 #include "string.hh"
26 #include "main.hh"
27 #include "file-path.hh"
28 #include "file-results.hh"
29 #include "debug.hh"
30 #include "lily-guile.hh"
31 #include "paper-def.hh"
32 #include "midi-def.hh"
33 #include "global-ctor.hh"
34 #include "kpath.hh"
38 Global options that can be overridden through command line.
41 /* Write dependencies file? */
42 bool dependency_global_b = false;
44 /* Prepend to dependencies */
45 String dependency_prefix_global;
47 /* Names of header fields to be dumped to a separate file. */
48 Array<String> dump_header_fieldnames_global;
50 /* Name of initialisation file. */
51 String init_name_global;
53 /* Do not calculate and write paper output? */
54 bool no_paper_global_b = false;
56 /* Selected output format.
57 One of tex, ps, scm, as. */
58 String output_format_global = "tex";
60 /* Current output name. */
61 String output_name_global;
63 /* Run in safe mode? -- FIXME: should be re-analised */
64 bool safe_global_b = false;
66 /* Verbose progress indication? */
67 bool verbose_global_b = false;
69 /* Scheme code to execute before parsing, after .scm init */
70 String init_scheme_code_string = "(begin #t ";
74 Misc. global stuff.
78 All_font_metrics *all_fonts_global_p;
79 int exit_status_global;
80 File_path global_path;
82 /* Number of current score output block. If there's more than one
83 score block, this counter will be added to the output filename. */
84 int score_count_global;
89 File globals.
92 /* The option parser */
93 static Getopt_long *oparser_p_static = 0;
96 Internationalisation kludge in two steps:
97 * use _i () to get entry in POT file
98 * call gettext () explicitely for actual "translation"
100 Note: these messages all start with lower case (ie, don't
101 follow regular localisation guidelines).
103 static Long_option_init options_static[] = {
104 /* another bug in option parser: --output=foe is taken as an abbreviation
105 for --output-format */
106 // {_i ("EXT"), "output-format", 'f', _i ("use output format EXT (scm, ps, tex or as)")},
108 {_i ("EXT"), "format", 'f', _i ("use output format EXT (scm, ps, tex or as)")},
109 {0, "help", 'h', _i ("this help")},
110 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
111 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
112 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
113 {0, "dependencies", 'M', _i ("write Makefile dependencies for every input file")},
114 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
115 {_i ("EXPR"), "evaluate", 'e',_i ("evalute EXPR as Scheme after .scm init is read")},
116 {0, "no-paper", 'm', _i ("produce MIDI output only")},
117 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
118 {0, "safe", 's', _i ("inhibit file output naming and exporting")},
119 {0, "version", 'v', _i ("print version number")},
120 {0, "verbose", 'V', _i ("verbose")},
121 {0, "warranty", 'w', _i ("show warranty and copyright")},
122 {0,0,0,0}
125 void
126 identify (ostream* os)
128 *os << gnu_lilypond_version_str ();
131 void
132 usage ()
136 No version number or newline here. It confuses help2man
138 cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond");
139 cout << "\n\n";
140 cout << _ ("Typeset music and or play MIDI from FILE");
141 cout << "\n\n";
142 cout <<
144 "LilyPond is a music typesetter. It produces beautiful sheet music\n"
145 "using a high level description file as input. LilyPond is part of \n"
146 "the GNU Project.\n"
149 cout << '\n';
150 cout << _ ("Options:");
151 cout << '\n';
152 cout << Long_option_init::table_str (options_static);
153 cout << '\n';
154 cout << _ ("This binary was compiled with the following options:")
155 << " " <<
156 #ifdef NDEBUG
157 "NDEBUG "
158 #endif
159 #ifdef NPRINT
160 "NPRINT "
161 #endif
162 #ifdef STRING_UTILS_INLINED
163 "STRING_UTILS_INLINED "
164 #endif
165 "\n"
166 "datadir: `" DIR_DATADIR "'\n"
167 "localedir: `" DIR_LOCALEDIR "'\n"
168 "\n";
171 cout << endl;
173 cout << _f ("Report bugs to %s", "bug-gnu-music@gnu.org") << endl;
176 void
177 version ()
179 identify (&cout);
180 cout << '\n';
181 cout << _f (""
182 "This is free software. It is covered by the GNU General Public License,\n"
183 "and you are welcome to change it and/or distribute copies of it under\n"
184 "certain conditions. Invoke as `%s --warranty' for more information.\n",
185 "lilypond");
186 cout << endl;
188 cout << _f ("Copyright (c) %s by", "1996--2001");
189 cout << '\n';
190 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
191 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
194 void
195 notice ()
197 cout << '\n';
198 cout << _ ("GNU LilyPond -- The music typesetter");
199 cout << '\n';
200 cout << _f ("Copyright (c) %s by", "1996--2001");
201 cout << '\n';
202 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
203 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
204 cout << '\n';
205 cout << _ (
206 " This program is free software; you can redistribute it and/or\n"
207 "modify it under the terms of the GNU General Public License version 2\n"
208 "as published by the Free Software Foundation.\n"
209 "\n"
210 " This program is distributed in the hope that it will be useful,\n"
211 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
212 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
213 "General Public License for more details.\n"
214 "\n"
215 " You should have received a copy (refer to the file COPYING) of the\n"
216 "GNU General Public License along with this program; if not, write to\n"
217 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
218 "USA.\n");
221 void
222 setup_paths ()
224 // facilitate binary distributions
225 char const *env_lily = getenv ("LILYPONDPREFIX");
226 String prefix_directory;
227 if (env_lily)
228 prefix_directory = env_lily;
230 #if HAVE_GETTEXT
231 setlocale (LC_ALL, ""); /* enable locales */
232 setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
233 String lily_locale_dir;
234 String name (PACKAGE);
235 name.to_lower ();
238 urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
239 handy for multiple source-dir runs, though...
241 if (!prefix_directory.empty_b ())
243 lily_locale_dir = prefix_directory + "/share/locale";
244 bindtextdomain (name.ch_C (), lily_locale_dir.ch_C ());
246 else
247 bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
248 textdomain (name.ch_C ());
249 #endif
251 global_path.add ("");
252 // must override (come before) "/usr/local/share/lilypond"!
253 char const *env_sz = getenv ("LILYINCLUDE");
254 if (env_sz)
255 global_path.parse_path (env_sz);
258 /* Adding mf/out make lilypond unchanged source directory, when setting
259 LILYPONDPREFIX to lilypond-x.y.z */
260 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
261 String prefix = prefix_directory;
262 if (prefix.empty_b ())
263 prefix = DIR_DATADIR;
264 for (char **s = suffixes; *s; s++)
266 String p = prefix + to_str ('/') + String (*s);
267 global_path.add (p);
269 #if !KPATHSEA
270 /* Urg: GNU make's $ (word) index starts at 1 */
271 int i = 1;
272 while (global_path.try_add (p + to_str (".") + to_str (i)))
273 i++;
274 #endif
279 Make input file name from command argument.
281 Path describes file name with added default extension,
282 ".ly" if none. "-" is stdin.
284 Path
285 distill_inname (String str)
287 Path p = split_path (str);
288 if (str.empty_b () || str == "-")
289 p.base = "-";
290 else
292 String orig_ext = p.ext;
293 char const *extensions[] = {"ly", "fly", "sly", "", 0};
294 for (int i = 0; extensions[i]; i++)
296 p.ext = orig_ext;
297 if (*extensions[i] && !p.ext.empty_b ())
298 p.ext += ".";
299 p.ext += extensions[i];
300 if (!global_path.find (p.str ()).empty_b ())
301 break;
303 /* Reshuffle extension */
304 p = split_path (p.str ());
306 return p;
309 String
310 format_to_ext (String format)
312 if (format == "tex")
313 /* .lytex change put off */
314 return "tex"; // "lytex";
315 return format;
318 void
319 main_prog (int, char**)
322 need to do this first. Engravers use lily.scm contents.
324 init_lily_guile ();
325 if (verbose_global_b)
326 progress_indication ("\n");
327 read_lily_scm_file ("lily.scm");
328 cout << endl;
330 call_constructors ();
331 all_fonts_global_p = new All_font_metrics (global_path.str ());
333 init_scheme_code_string += ")";
334 gh_eval_str ((char *)init_scheme_code_string.ch_C());
336 int p=0;
337 const char *arg ;
338 while ((arg = oparser_p_static->get_next_arg ()) || p == 0)
340 String infile;
342 if (arg)
343 infile = arg;
344 else
345 infile = "-";
347 // What/when was this supposed to do?
348 // It looks like it reset the outname_str_global for every new
349 // file, but only if user didn't specify a outname? Huh?
350 // if (outname_str_global == "")
352 Midi_def::reset_score_count ();
353 Paper_def::reset_score_count ();
356 Path inpath = distill_inname (infile);
358 /* By default, use base name of input file for output file name */
359 Path outpath = inpath;
360 if (inpath.str () != "-")
361 outpath.ext = format_to_ext (output_format_global);
363 /* By default, write output to cwd; do not copy directory part
364 of input file name */
365 outpath.root = "";
366 outpath.dir = "";
368 if (!output_name_global.empty_b ())
369 outpath = split_path (output_name_global);
371 String init;
372 if (!init_name_global.empty_b ())
373 init = init_name_global;
374 else if (!inpath.ext.empty_b ())
375 init = "init." + inpath.ext;
376 else
377 init = "init.ly";
379 /* Burp: output name communication goes through _global */
380 String save_output_name_global = output_name_global;
381 output_name_global = outpath.str ();
382 do_one_file (init, inpath.str ());
383 output_name_global = save_output_name_global;
385 p++;
387 delete oparser_p_static;
388 exit (exit_status_global);
391 static int
392 sane_putenv (char const* key, char const* value)
395 putenv is POSIX, setenv is BSD 4.3
396 Urg, but putenv blindly overwrites environment settings.
398 if (!getenv (key))
399 return putenv ((char*)((String (key) + "=" + value).ch_C ()));
400 return -1;
404 main (int argc, char **argv)
406 setup_paths ();
408 /* Prepare GUILE for heavy memory usage. If you have plenty memory,
409 this may speed up GUILE a bit. If you're short on memory, these
410 settings
412 export GUILE_INIT_SEGMENT_SIZE_1=36000
413 export GUILE_MAX_SEGMENT_SIZE=576000
415 may considerably decrease memory footprint (~*0.85), with a small
416 execution time penalty (~*1.10). */
418 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304");
419 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608");
421 ly_init_kpath (argv[0]);
423 oparser_p_static = new Getopt_long (argc, argv, options_static);
424 while (Long_option_init const * opt = (*oparser_p_static) ())
426 switch (opt->shortname_ch_)
428 case 'v':
429 version ();
430 exit (0); // we print a version anyway.
431 break;
432 case 'o':
434 String s = oparser_p_static->optional_argument_ch_C_;
435 Path p = split_path (s);
436 if (p.ext.empty_b ())
437 p.ext = format_to_ext (output_format_global);
438 output_name_global = p.str ();
440 break;
441 case 'e':
442 init_scheme_code_string +=
443 oparser_p_static->optional_argument_ch_C_;
444 break;
445 case 'w':
446 notice ();
447 exit (0);
448 break;
449 case 'f':
450 output_format_global = oparser_p_static->optional_argument_ch_C_;
451 break;
452 case 'P':
453 dependency_prefix_global = oparser_p_static->optional_argument_ch_C_;
454 break;
455 case 'H':
456 dump_header_fieldnames_global.push (oparser_p_static->optional_argument_ch_C_);
457 break;
458 case 'I':
459 global_path.push (oparser_p_static->optional_argument_ch_C_);
460 break;
461 case 'i':
462 init_name_global = oparser_p_static->optional_argument_ch_C_;
463 break;
464 case 'h':
465 usage ();
466 exit (0);
467 break;
468 case 'V':
469 verbose_global_b = true;
470 break;
471 case 's':
472 safe_global_b = true;
473 break;
474 case 'M':
475 dependency_global_b = true;
476 break;
477 case 'm':
478 no_paper_global_b = true;
479 break;
480 default:
481 assert (false);
482 break;
485 identify (&cerr);
487 #ifdef WINNT
488 gh_enter (argc, argv, main_prog);
489 #else
490 gh_enter (argc, argv, (void (*) (int, char**))main_prog);
491 #endif
493 return 0; // unreachable