2 main.cc -- implement main () entrypoint.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
16 #include <sys/types.h>
31 #include "all-font-metrics.hh"
32 #include "file-name.hh"
33 #include "freetype.hh"
34 #include "getopt-long.hh"
35 #include "global-ctor.hh"
36 #include "lily-guile.hh"
37 #include "lily-version.hh"
39 #include "output-def.hh"
40 #include "string-convert.hh"
45 * Global options that can be overridden through command line.
48 /* Names of header fields to be dumped to a separate file. */
49 Array<String> dump_header_fieldnames_global;
51 /* Name of initialisation file. */
52 String init_name_global;
54 /* Selected output backend
55 One of (gnome, ps [default], scm, svg, tex, texstr)") */
56 String output_backend_global = "ps";
57 /* Output formats to generate. */
58 String output_format_global = "pdf";
60 bool is_pango_format_global;
61 bool is_TeX_format_global;
63 /* Current output name. */
64 String output_name_global;
66 /* Run in safe mode? */
67 bool be_safe_global = false;
69 /* Provide URI links to the original file */
70 bool point_and_click_global = true;
72 /* Verbose progress indication? */
73 bool be_verbose_global = false;
75 /* Scheme code to execute before parsing, after .scm init.
76 This is where -e arguments are appended to. */
77 String init_scheme_code_string;
78 String init_scheme_variables;
80 /* Generate preview of first system. */
81 bool make_preview = false;
83 /* Generate printed output. */
84 bool make_print = true;
87 * Miscellaneous global stuff.
89 File_path global_path;
95 static char const *AUTHORS
96 = " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
97 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
99 static char const *PROGRAM_NAME = "lilypond";
100 static char const *PROGRAM_URL = "http://lilypond.org";
102 static char const *NOTICE
103 = _i ("This program is free software. It is covered by the GNU General Public\n"
104 "License and you are welcome to change it and/or distribute copies of it\n"
105 "under certain conditions. Invoke as `%s --warranty' for more\n"
108 static char const *WARRANTY
109 = _i (" This program is free software; you can redistribute it and/or\n"
110 "modify it under the terms of the GNU General Public License version 2\n"
111 "as published by the Free Software Foundation.\n"
113 " This program is distributed in the hope that it will be useful,\n"
114 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
115 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
116 "General Public License for more details.\n"
118 " You should have received a copy (refer to the file COPYING) of the\n"
119 "GNU General Public License along with this program; if not, write to\n"
120 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
121 "Boston, MA 02111-1307, USA.\n");
123 /* Where the init files live. Typically:
124 LILYPOND_DATADIR = /usr/share/lilypond
126 String prefix_directory;
128 /* The jail specification: USER,GROUP,JAIL,DIR. */
131 /* The option parser */
132 static Getopt_long *option_parser = 0;
134 /* Internationalisation kludge in two steps:
135 * use _i () to get entry in POT file
136 * call gettext () explicitely for actual "translation" */
138 static Long_option_init options_static[]
140 {_i ("BACK"), "backend", 'b', _i ("use backend BACK (gnome, ps [default],\n scm, svg, tex, texstr)")},
142 {_i ("SYM=VAL"), "define-default", 'd',
143 _i ("define a default Scheme setting.")},
145 {_i ("EXPR"), "evaluate", 'e', _i ("set scheme option, for help use\n -e '(ly:option-usage)'")},
146 /* Bug in option parser: --output =foe is taken as an abbreviation
147 for --output-format. */
148 {_i ("FORMATs"), "formats", 'f', _i ("dump FORMAT,... Also as separate options:")},
149 {0, "dvi", 0, _i ("generate DVI (tex backend only)")},
150 {0, "pdf", 0, _i ("generate PDF (default)")},
151 {0, "png", 0, _i ("generate PNG")},
152 {0, "ps", 0, _i ("generate PostScript")},
153 {0, "tex", 0, _i ("generate TeX (tex backend only)")},
154 {0, "help", 'h', _i ("print this help")},
155 {_i ("FIELD"), "header", 'H', _i ("dump a header field to file BASENAME.FIELD")},
156 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
157 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
158 {_i ("FILE"), "output", 'o', _i ("write output to FILE (suffix will be added)")},
160 {_i ("USER,GROUP,JAIL,DIR"), "jail", 'j', _i ("chroot to JAIL, become USER:GROUP\n and cd into DIR")},
162 {0, "no-print", 0, _i ("do not generate printed output")},
163 {0, "preview", 'p', _i ("generate a preview of the first system")},
164 {0, "safe-mode", 's', _i ("run in safe mode")},
165 {0, "version", 'v', _i ("print version number")},
166 {0, "verbose", 'V', _i ("be verbose")},
167 {0, "warranty", 'w', _i ("show warranty and copyright")},
171 #define LILYPOND_DATADIR PACKAGE_DATADIR "/" TOPLEVEL_VERSION
174 env_var_info (FILE *out, char const* key)
176 if (char const *value = getenv (key))
177 fprintf (out, "%s=\"%s\"\n", key, value);
184 fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
185 env_var_info (out, "LILYPONDPREFIX");
186 fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
188 fprintf (out, "\nEffective prefix: \"%s\"\n", prefix_directory.to_str0 ());
191 env_var_info (out, "FONTCONFIG_FILE");
192 env_var_info (out, "FONTCONFIG_PATH");
193 env_var_info (out, "GS_FONTPATH");
194 env_var_info (out, "GS_LIB");
195 env_var_info (out, "GUILE_LOAD_PATH");
196 env_var_info (out, "PANGO_RC_FILE");
197 env_var_info (out, "PATH");
204 printf (_f ("Copyright (c) %s by\n%s and others.",
206 AUTHORS).to_str0 ());
213 fputs (gnu_lilypond_version_string ().to_str0 (), out);
221 puts (_f (NOTICE, PROGRAM_NAME).to_str0 ());
226 LY_DEFINE (ly_usage, "ly:usage",
228 "Print usage message.")
230 /* No version number or newline here. It confuses help2man. */
231 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
233 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
235 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
237 printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
239 printf (_ ("Options:").to_str0 ());
241 printf (Long_option_init::table_string (options_static).to_str0 ());
243 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
246 return SCM_UNSPECIFIED;
256 printf (_ (WARRANTY).to_str0 ());
261 sane_putenv (char const* key, String value, bool overwrite = true)
263 if (overwrite || !getenv (key))
265 String combine = String (key) + "=" + value;
266 char *s = strdup (combine.to_str0 ());
273 set_env_file (char const* key, String value)
276 return sane_putenv (key, value, false);
277 else if (be_verbose_global)
278 warning (_f ("no such file: %s", value));
283 prepend_env_path (char const *key, String value)
287 if (char const* cur = getenv (key))
288 value += to_string (PATHSEP) + cur;
289 return sane_putenv (key, value.to_str0 ());
291 else if (be_verbose_global)
292 warning (_f ("no such directory: %s", value));
297 dir_name (String const file_name)
299 String s = file_name;
300 s.substitute ('\\', '/');
301 s = s.left_string (s.index_last ('/'));
307 # include <winbase.h>
311 setup_paths (char const* argv0)
313 prefix_directory = LILYPOND_DATADIR;
317 if (getenv ("LILYPOND_VERBOSE"))
318 be_verbose_global = true;
320 String bindir = dir_name (argv0);
321 String argv0_prefix = dir_name (bindir);
322 if (argv0_prefix != dir_name (dir_name (dir_name (prefix_directory))))
324 if (be_verbose_global)
325 warning (_f ("argv0 relocation: argv0=%s, prefix=%s", argv0,
327 String datadir = argv0_prefix + "/share";
328 String libdir = argv0_prefix + "/lib";
329 String localedir = datadir + "/locale";
330 String sysconfdir = argv0_prefix + "/etc";
331 String argv0_lilypond_datadir = datadir + "/lilypond/" TOPLEVEL_VERSION;
333 if (is_dir (argv0_lilypond_datadir))
334 prefix_directory = argv0_lilypond_datadir;
337 if (is_dir (localedir))
338 bindtextdomain ("lilypond", localedir.to_str0 ());
341 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf");
343 char font_dir[PATH_MAX];
344 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
345 prepend_env_path ("GS_FONTPATH", font_dir);
348 /* FIXME: *cough* 8.15 *cough* */
349 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/8.15/fonts");
350 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/Resource");
351 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/lib");
353 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
354 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
355 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
357 prepend_env_path ("GUILE_LOAD_PATH", datadir
358 + to_string ("/guile/%d.%d",
359 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
360 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
361 prepend_env_path ("PATH", bindir);
365 #endif /* ARGV0_RELOCATION */
367 /* FIXME: use LILYPOND_DATADIR. */
368 if (char const *env = getenv ("LILYPONDPREFIX"))
371 /* Normalize file name. */
372 env = File_name (env).to_string ().get_copy_str0 ();
374 prefix_directory = env;
377 global_path.append ("");
379 /* Adding mf/out make lilypond unchanged source directory, when setting
380 LILYPONDPREFIX to lilypond-x.y.z */
381 char *suffixes[] = {"ly", "ps", "scm", 0 };
384 for (char **s = suffixes; *s; s++)
386 String path = prefix_directory + to_string ('/') + String (*s);
391 ugh. C&P font-config.cc
394 String builddir = prefix_directory + "/mf/out/";
395 if (stat (builddir.to_str0 (), &statbuf) == 0)
397 dirs.push (builddir.to_str0 ());
401 dirs.push (prefix_directory + "/fonts/otf/");
402 dirs.push (prefix_directory + "/fonts/type1/");
403 dirs.push (prefix_directory + "/fonts/cff/");
404 dirs.push (prefix_directory + "/fonts/svg/");
405 dirs.push (prefix_directory + "/fonts/cff/");
408 for (int i = 0; i < dirs.size (); i++)
409 global_path.prepend (dirs[i]);
413 prepend_load_path (String dir)
415 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
416 scm_c_eval_string (s.to_str0 ());
419 void init_global_tweak_registry ();
420 void init_fontconfig ();
426 /* Now we chroot, setuid/setgrp and chdir. If something goes wrong,
427 we exit (this is a security-sensitive area). First we split
428 jail_spec into its components, then we retrieve the user/group id
429 (necessarily *before* chroot'ing) and finally we perform the
434 USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
437 Array<String> components = String_convert::split (jail_spec, ',');
438 if (components.size () != JAIL_MAX)
440 error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX,
441 components.size ()));
449 if (passwd * passwd = getpwnam (components[USER_NAME].to_str0 ()))
450 uid = passwd->pw_uid;
454 error (_f ("no such user: %s", components[USER_NAME]));
456 error (_f ("can't get user id from user name: %s: %s",
457 components[USER_NAME],
466 if (group * group = getgrnam (components[GROUP_NAME].to_str0 ()))
471 error (_f ("no such group: %s", components[GROUP_NAME]));
473 error (_f ("can't get group id from group name: %s: %s",
474 components[GROUP_NAME],
479 if (chroot (components[JAIL].to_str0 ()))
481 error (_f ("can't chroot to: %s: %s", components[JAIL],
488 error (_f ("can't change group id to: %d: %s", gid, strerror (errno)));
494 error (_f ("can't change user id to: %d: %s", uid, strerror (errno)));
498 if (chdir (components[DIR].to_str0 ()))
500 error (_f ("can't change working directory to: %s: %s", components[DIR],
510 main_with_guile (void *, int, char **)
512 /* Engravers use lily.scm contents, need to make Guile find it.
513 Prepend onto GUILE %load-path, very ugh. */
515 prepend_load_path (prefix_directory);
516 prepend_load_path (prefix_directory + "/scm");
518 if (be_verbose_global)
520 is_TeX_format_global = (output_backend_global == "tex"
521 || output_backend_global == "texstr");
523 is_pango_format_global = !is_TeX_format_global;
526 call_constructors ();
527 init_global_tweak_registry ();
532 all_fonts_global = new All_font_metrics (global_path.to_string ());
535 if (!init_scheme_variables.is_empty ()
536 || !init_scheme_code_string.is_empty ())
538 init_scheme_variables = "(ly:set-option 'command-line-settings (list "
539 + init_scheme_variables + "))";
541 init_scheme_code_string
543 + init_scheme_variables
544 + init_scheme_code_string
547 char const *str0 = init_scheme_code_string.to_str0 ();
549 if (be_verbose_global)
550 progress_indication (_f("Evaluating %s", str0));
551 scm_c_eval_string ((char *) str0);
555 /* We accept multiple independent music files on the command line to
556 reduce compile time when processing lots of small files.
557 Starting the GUILE engine is very time consuming. */
561 while (char const *arg = option_parser->get_next_arg ())
563 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
564 tail = SCM_CDRLOC (*tail);
566 delete option_parser;
570 if (!jail_spec.is_empty ())
574 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
582 setup_localisation ()
586 setlocale (LC_ALL, "");
588 /* FIXME: check if this is still true.
589 Disable localisation of float values. This breaks TeX output. */
590 setlocale (LC_NUMERIC, "C");
592 String localedir = LOCALEDIR;
593 if (char const *env = getenv ("LILYPOND_LOCALEDIR"))
596 bindtextdomain ("lilypond", localedir.to_str0 ());
597 textdomain ("lilypond");
602 add_output_format (String format)
604 if (output_format_global != "")
605 output_format_global += ",";
606 output_format_global += format;
610 parse_argv (int argc, char **argv)
612 bool show_help = false;
613 option_parser = new Getopt_long (argc, argv, options_static);
614 while (Long_option_init const *opt = (*option_parser) ())
616 switch (opt->shortname_char_)
619 if (String (opt->longname_str0_) == "dvi"
620 || String (opt->longname_str0_) == "pdf"
621 || String (opt->longname_str0_) == "png"
622 || String (opt->longname_str0_) == "ps"
623 || String (opt->longname_str0_) == "tex")
624 add_output_format (opt->longname_str0_);
625 else if (String (opt->longname_str0_) == "preview")
627 else if (String (opt->longname_str0_) == "no-pages")
633 String arg (option_parser->optional_argument_str0_);
634 int eq = arg.index ('=');
641 key = arg.left_string (eq);
642 val = arg.right_string (arg.length () - eq - 1);
645 init_scheme_variables
646 += "(cons \'" + key + " " + val + ")\n";
656 String s = option_parser->optional_argument_str0_;
657 File_name file_name (s);
658 output_name_global = file_name.to_string ();
662 jail_spec = option_parser->optional_argument_str0_;
665 init_scheme_code_string += option_parser->optional_argument_str0_;
673 output_backend_global = option_parser->optional_argument_str0_;
677 output_format_global = option_parser->optional_argument_str0_;
681 dump_header_fieldnames_global
682 .push (option_parser->optional_argument_str0_);
685 global_path.append (option_parser->optional_argument_str0_);
688 init_name_global = option_parser->optional_argument_str0_;
694 be_verbose_global = true;
697 be_safe_global = true;
703 programming_error (to_string ("unhandled short option: %c",
704 opt->shortname_char_));
714 if (be_verbose_global)
721 main (int argc, char **argv)
723 setup_localisation ();
724 setup_paths (argv[0]);
725 parse_argv (argc, argv);
726 if (isatty (STDIN_FILENO))
729 scm_boot_guile (argc, argv, main_with_guile, 0);
731 /* Only reachable if GUILE exits. That is an error. */