* lily/stem.cc (get_default_stem_end_position): lengthen stems for
[lilypond.git] / lily / main.cc
blob6ea0664d18d9fa0f6d7e1a1ab8047bd1a18a4400
1 /*
2 main.cc -- implement main: entrypoints
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <assert.h>
12 #include <locale.h>
13 #include <string.h>
15 #include "config.h"
17 #if HAVE_GETTEXT
18 #include <libintl.h>
19 #endif
21 #include "lily-guile.hh"
22 #include "lily-version.hh"
23 #include "all-font-metrics.hh"
24 #include "getopt-long.hh"
25 #include "misc.hh"
26 #include "string.hh"
27 #include "main.hh"
28 #include "file-path.hh"
29 #include "input-file-results.hh"
30 #include "warn.hh"
31 #include "lily-guile.hh"
32 #include "paper-def.hh"
33 #include "midi-def.hh"
34 #include "global-ctor.hh"
35 #include "kpath.hh"
37 static int sane_putenv (char const* key, char const* value, bool overwrite = false);
40 Global options that can be overridden through command line.
43 /* Write dependencies file? */
44 bool dependency_global_b = false;
46 /* Prepend to dependencies */
47 String dependency_prefix_global;
49 /* Names of header fields to be dumped to a separate file. */
50 Array<String> dump_header_fieldnames_global;
52 /* Name of initialisation file. */
53 String init_name_global;
55 /* Do not calculate and write paper output? */
56 bool no_paper_global_b = false;
58 /* Selected output format.
59 One of tex, ps, scm, as. */
60 String output_format_global = "tex";
62 /* Current output name. */
63 String output_name_global;
65 /* Run in safe mode? -- FIXME: should be re-analised */
66 bool safe_global_b = false;
68 /* Verbose progress indication? */
69 bool verbose_global_b = false;
71 /* Scheme code to execute before parsing, after .scm init */
72 String init_scheme_code_string = "(begin #t ";
76 Misc. global stuff.
80 All_font_metrics *all_fonts_global;
81 int exit_status_global;
82 File_path global_path;
84 /* Number of current score output block. If there's more than one
85 score block, this counter will be added to the output filename. */
86 int score_count_global;
91 File globals.
94 /* The option parser */
95 static Getopt_long *oparser_p_static = 0;
98 Internationalisation kludge in two steps:
99 * use _i () to get entry in POT file
100 * call gettext () explicitely for actual "translation"
102 Note: these messages all start with lower case (ie, don't
103 follow regular localisation guidelines).
105 static Long_option_init options_static[] = {
106 {_i ("EXPR"), "evaluate", 'e',
107 _i ("set options, use -e '(ly-option-usage)' for help")},
108 /* another bug in option parser: --output=foe is taken as an abbreviation
109 for --output-format */
110 {_i ("EXT"), "format", 'f', _i ("use output format EXT")},
111 {0, "help", 'h', _i ("print this help")},
112 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
113 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
114 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
115 {0, "dependencies", 'M', _i ("write Makefile dependencies for every input file")},
116 {0, "no-paper", 'm', _i ("produce MIDI output only")},
117 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
118 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
119 #if 0
121 should audit again.
123 {0, "safe", 's', _i ("inhibit file output naming and exporting")},
124 #endif
125 {0, "version", 'v', _i ("print version number")},
126 {0, "verbose", 'V', _i ("be verbose")},
127 {0, "warranty", 'w', _i ("show warranty and copyright")},
128 {0,0,0,0}
131 void
132 identify (FILE *out)
134 fputs (gnu_lilypond_version_string ().to_str0 (), out);
137 void
138 dirinfo (FILE *out)
140 fputs ("\n", out);
141 fprintf (out, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR);
142 fprintf (out, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR);
143 fprintf (out, "localedir: `%s'\n", LOCALEDIR);
145 char *lilypond_prefix = getenv ("LILYPONDPREFIX");
146 fprintf (out, "LILYPONDPREFIX: `%s'\n",
147 (lilypond_prefix ? lilypond_prefix : ""));
150 void
151 usage ()
153 printf ("\n");
154 /* No version number or newline here. It confuses help2man. */
155 printf (_f ("Usage: %s [OPTIONS]... FILE...", "lilypond").to_str0 ());
156 printf ("\n\n");
157 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
158 printf ("\n\n");
159 printf(_ ("LilyPond produces beautiful music notation.\n"
160 "For more information, see http://lilypond.org/"
161 ).to_str0 ());
163 printf ("\n");
164 printf (_ ("Options:").to_str0 ());
165 printf ("\n");
166 printf (Long_option_init::table_string (options_static).to_str0 ());
167 printf ("\n");
168 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
169 printf ("\n");
170 printf ("\n");
173 void
174 version ()
176 identify (stdout);
177 printf ("\n");
178 printf (_f (
179 "This is free software. It is covered by the GNU General Public License,\n"
180 "and you are welcome to change it and/or distribute copies of it under\n"
181 "certain conditions. Invoke as `%s --warranty' for more information.\n",
182 "lilypond").to_str0 ());
183 printf ("\n");
185 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
186 printf ("\n");
187 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
188 printf (" Jan Nieuwenhuizen <janneke@gnu.org>\n");
191 void
192 notice ()
194 printf ("\n");
195 printf (_ ("GNU LilyPond -- The music typesetter").to_str0 ());
196 printf ("\n");
197 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
198 printf ("\n");
199 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
200 printf (" Jan Nieuwenhuizen <janneke@gnu.org>\n");
201 printf ("\n");
202 printf ( _ (
203 " This program is free software; you can redistribute it and/or\n"
204 "modify it under the terms of the GNU General Public License version 2\n"
205 "as published by the Free Software Foundation.\n"
206 "\n"
207 " This program is distributed in the hope that it will be useful,\n"
208 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
209 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
210 "General Public License for more details.\n"
211 "\n"
212 " You should have received a copy (refer to the file COPYING) of the\n"
213 "GNU General Public License along with this program; if not, write to\n"
214 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,\n"
215 "USA.\n").to_str0 ());
219 /* Where LilyPond's init files live. Typically:
220 LILYPOND_DATADIR = /usr/local/share/lilypond
221 LOCAL_LILYPOND_DATADIR = /usr/local/share/lilypond/1.5.68 */
222 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
224 void
225 setup_paths ()
227 if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
228 prefix_directory[1] = lilypond_prefix;
230 #if HAVE_GETTEXT
231 /* Enable locales */
232 setlocale (LC_ALL, "");
233 /* Mustn't have commas in TeX output... */
234 setlocale (LC_NUMERIC, "C");
235 String lily_locale_dir;
236 String name (PACKAGE);
237 name.to_lower ();
238 bindtextdomain (name.to_str0 (), LOCALEDIR);
239 textdomain (name.to_str0 ());
240 #endif
242 global_path.add ("");
245 /* Adding mf/out make lilypond unchanged source directory, when setting
246 LILYPONDPREFIX to lilypond-x.y.z */
247 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
249 for (unsigned i = 0; prefix_directory[i]; i++)
250 for (char **s = suffixes; *s; s++)
252 String p = prefix_directory[i] + to_string ('/') + String (*s);
253 global_path.prepend (p);
255 #if !KPATHSEA
256 /* Urg: GNU make's $ (word) index starts at 1 */
257 int i = 1;
258 while (global_path.try_add (p + to_string (".") + to_string (i)))
259 i++;
260 #endif
265 Make input file name from command argument.
267 Path describes file name with added default extension,
268 ".ly" if none. "-" is stdin.
270 Path
271 distill_inname (String str)
273 Path p = split_path (str);
274 if (str.empty_b () || str == "-")
275 p.base = "-";
276 else
278 String orig_ext = p.ext;
279 char const *extensions[] = {"ly", "", 0};
280 for (int i = 0; extensions[i]; i++)
282 p.ext = orig_ext;
283 if (*extensions[i] && !p.ext.empty_b ())
284 p.ext += ".";
285 p.ext += extensions[i];
286 if (!global_path.find (p.to_string ()).empty_b ())
287 break;
289 /* Reshuffle extension */
290 p = split_path (p.to_string ());
292 return p;
295 String
296 format_to_ext (String format)
298 if (format == "tex")
299 /* .lytex change put off */
300 return "tex"; // "lytex";
301 return format;
304 void
305 prepend_load_path (String dir)
307 String s = "(set! %load-path (cons \""
308 + dir
309 + "\" %load-path))";
310 scm_c_eval_string (s.to_str0 ());
313 void
314 main_prog (void *, int, char **)
316 /* Engravers use lily.scm contents, need to make Guile find it.
317 Prepend onto GUILE %load-path, very ugh. */
319 for (unsigned i = 0; prefix_directory[i]; i++)
321 prepend_load_path (prefix_directory[i]);
323 /* Junk this. We should make real modules iso. just loading files. */
324 prepend_load_path (String (prefix_directory[i]) + "/scm");
327 if (verbose_global_b)
328 dirinfo (stderr);
330 ly_init_guile ();
331 call_constructors ();
333 progress_indication ("\n");
335 all_fonts_global = new All_font_metrics (global_path.to_string ());
337 init_scheme_code_string += ")";
338 gh_eval_str ((char *)init_scheme_code_string.to_str0 ());
340 int p=0;
341 const char *arg = oparser_p_static->get_next_arg ();
343 /* Only exit until after running init_scheme_code, for
344 (ly-option-usage) or
345 -e "(ly-set-option 'help #t)" */
346 if (!arg)
348 usage ();
349 /* No FILE arguments is now a usage error */
350 exit (2);
355 String infile (arg);
356 Midi_def::reset_score_count ();
357 Paper_def::reset_score_count ();
359 Path inpath = distill_inname (infile);
361 /* By default, use base name of input file for output file name */
362 Path outpath = inpath;
363 if (inpath.to_string () != "-")
364 outpath.ext = format_to_ext (output_format_global);
366 /* By default, write output to cwd; do not copy directory part
367 of input file name */
368 outpath.root = "";
369 outpath.dir = "";
371 if (!output_name_global.empty_b ())
372 outpath = split_path (output_name_global);
374 String init;
375 if (!init_name_global.empty_b ())
376 init = init_name_global;
377 else
378 init = "init.ly";
380 /* Burp: output name communication goes through _global */
381 String save_output_name_global = output_name_global;
382 output_name_global = outpath.to_string ();
383 do_one_file (init, inpath.to_string ());
384 output_name_global = save_output_name_global;
386 p++;
387 } while ((arg = oparser_p_static->get_next_arg ()));
388 delete oparser_p_static;
389 oparser_p_static = 0;
390 exit (exit_status_global);
394 static int
395 sane_putenv (char const* key, char const* value, bool overwrite)
397 if (overwrite || !getenv (key))
399 String combine = String (key) + "=" + String (value);
400 char * s = strdup(combine.to_str0 ());
401 return putenv (s);
403 return -1;
407 main (int argc, char **argv)
409 setup_paths ();
413 These settings hopefully prepare lily for a lot of memory usage.
415 In practice the effect on GC times is barely measurable -- larger
416 segments cost slighly less time for the conservative marking. (but
417 not impressively much)
420 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
421 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
423 init_kpath (argv[0]);
425 bool help_b = false;
426 oparser_p_static = new Getopt_long (argc, argv, options_static);
427 while (Long_option_init const * opt = (*oparser_p_static) ())
429 switch (opt->shortname_char_)
431 case 'v':
432 version ();
433 exit (0); // we print a version anyway.
434 break;
435 case 'o':
437 String s = oparser_p_static->optional_argument_str0_;
438 Path p = split_path (s);
439 if (s != "-" && p.ext.empty_b ())
440 p.ext = format_to_ext (output_format_global);
441 output_name_global = p.to_string ();
443 break;
444 case 'e':
445 init_scheme_code_string +=
446 oparser_p_static->optional_argument_str0_;
447 break;
448 case 'w':
449 notice ();
450 exit (0);
451 break;
452 case 'f':
453 if (oparser_p_static->optional_argument_str0_ == "help")
455 printf("See http://lilypond.org/wiki?OutputFormats for more information.\n"\
456 "This option is for developers only.\n");
457 exit (0);
459 output_format_global = oparser_p_static->optional_argument_str0_;
460 break;
461 case 'P':
462 dependency_prefix_global = oparser_p_static->optional_argument_str0_;
463 break;
464 case 'H':
465 dump_header_fieldnames_global.push (oparser_p_static->optional_argument_str0_);
466 break;
467 case 'I':
468 global_path.push (oparser_p_static->optional_argument_str0_);
469 break;
470 case 'i':
471 init_name_global = oparser_p_static->optional_argument_str0_;
472 break;
473 case 'h':
474 help_b = true;
475 break;
476 case 'V':
477 verbose_global_b = true;
478 break;
479 case 's':
480 safe_global_b = true;
481 break;
482 case 'M':
483 dependency_global_b = true;
484 break;
485 case 'm':
486 no_paper_global_b = true;
487 break;
488 default:
489 assert (false);
490 break;
494 if (help_b)
496 usage ();
497 if (verbose_global_b)
498 dirinfo (stdout);
499 exit (0);
502 scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
504 return 0; // unreachable