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>
18 #include <sys/types.h>
27 #include "all-font-metrics.hh"
28 #include "file-name.hh"
29 #include "freetype.hh"
30 #include "getopt-long.hh"
31 #include "global-ctor.hh"
32 #include "lily-guile.hh"
33 #include "lily-version.hh"
35 #include "output-def.hh"
36 #include "string-convert.hh"
41 * Global options that can be overridden through command line.
44 /* Names of header fields to be dumped to a separate file. */
45 Array<String> dump_header_fieldnames_global;
47 /* Name of initialisation file. */
48 String init_name_global;
50 /* Selected output format.
51 One of tex, ps, scm, as.
53 String output_backend_global = "ps";
54 String output_format_global = "pdf";
56 bool is_pango_format_global;
57 bool is_TeX_format_global;
59 /* Current output name. */
60 String output_name_global;
62 /* Run in safe mode? */
63 bool be_safe_global = false;
65 /* Provide URI links to the original file */
66 bool point_and_click_global = true;
68 /* Verbose progress indication? */
69 bool be_verbose_global = false;
71 /* Scheme code to execute before parsing, after .scm init
72 This is where -e arguments are appended to.
74 String init_scheme_code_string = "(begin #t ";
76 bool make_preview = false;
77 bool make_pages = true;
80 * Miscellaneous global stuff.
82 File_path global_path;
88 static char const *AUTHORS
89 = " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
90 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
92 static char const *PROGRAM_NAME = "lilypond";
93 static char const *PROGRAM_URL = "http://lilypond.org";
95 static char const *NOTICE
96 = _i ("This program is free software. It is covered by the GNU General Public\n"
97 "License and you are welcome to change it and/or distribute copies of it\n"
98 "under certain conditions. Invoke as `%s --warranty' for more\n"
101 static char const *WARRANTY
102 = _i (" This program is free software; you can redistribute it and/or\n"
103 "modify it under the terms of the GNU General Public License version 2\n"
104 "as published by the Free Software Foundation.\n"
106 " This program is distributed in the hope that it will be useful,\n"
107 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
108 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
109 "General Public License for more details.\n"
111 " You should have received a copy (refer to the file COPYING) of the\n"
112 "GNU General Public License along with this program; if not, write to\n"
113 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
114 "Boston, MA 02111-1307, USA.\n");
116 /* Where the init files live. Typically:
117 LILYPOND_DATADIR = /usr/share/lilypond
119 String prefix_directory;
121 /* The jail specification: USER,GROUP,JAIL,DIR. */
124 /* The option parser */
125 static Getopt_long *option_parser = 0;
127 /* Internationalisation kludge in two steps:
128 * use _i () to get entry in POT file
129 * call gettext () explicitely for actual "translation" */
131 static Long_option_init options_static[]
133 {_i ("EXT"), "backend", 'b', _i ("select backend to use")},
134 {_i ("EXPR"), "evaluate", 'e',
135 _i ("set option, use -e '(ly:option-usage)' for help")},
136 /* Bug in option parser: --output =foe is taken as an abbreviation
137 for --output-format. */
138 {_i ("EXTs"), "formats", 'f', _i ("list of formats to dump")},
139 {0, "help", 'h', _i ("print this help")},
140 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
141 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
142 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
143 {_i ("FILE"), "output", 'o', _i ("write output to FILE (suffix will be added)")},
144 {_i ("USER,GROUP,JAIL,DIR"), "jail", 'j', _i ("chroot to JAIL, become USER:GROUP and cd into DIR")},
145 {0, "preview", 'p', _i ("generate a preview")},
146 {0, "no-pages", 0, _i ("don't generate full pages")},
147 {0, "png", 0, _i ("generate PNG")},
148 {0, "ps", 0, _i ("generate PostScript")},
149 {0, "dvi", 0, _i ("generate DVI")},
150 {0, "pdf", 0, _i ("generate PDF (default)")},
151 {0, "tex", 0, _i ("generate TeX")},
152 {0, "safe-mode", 's', _i ("run in safe mode")},
153 {0, "version", 'v', _i ("print version number")},
154 {0, "verbose", 'V', _i ("be verbose")},
155 {0, "warranty", 'w', _i ("show warranty and copyright")},
163 fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
164 if (char const *env = getenv ("LILYPONDPREFIX"))
165 fprintf (out, "LILYPONDPREFIX=\"%s\"\n", env);
166 fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
168 fprintf (out, "\nEffective prefix: \"%s\"\n", prefix_directory.to_str0 ());
174 printf (_f ("Copyright (c) %s by\n%s and others.",
176 AUTHORS).to_str0 ());
183 fputs (gnu_lilypond_version_string ().to_str0 (), out);
191 puts (_f (NOTICE, PROGRAM_NAME).to_str0 ());
199 /* No version number or newline here. It confuses help2man. */
200 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
202 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
204 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
206 printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
208 printf (_ ("Options:").to_str0 ());
210 printf (Long_option_init::table_string (options_static).to_str0 ());
212 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
224 printf (_ (WARRANTY).to_str0 ());
230 prefix_directory = LILYPOND_DATADIR;
231 if (char const *env = getenv ("LILYPONDPREFIX"))
232 prefix_directory = env;
234 global_path.append ("");
236 /* Adding mf/out make lilypond unchanged source directory, when setting
237 LILYPONDPREFIX to lilypond-x.y.z */
238 char *suffixes[] = {"ly", "ps", "scm", 0 };
241 for (char **s = suffixes; *s; s++)
243 String path = prefix_directory + to_string ('/') + String (*s);
248 ugh. C&P font-config.cc
251 String builddir = prefix_directory + "/mf/out/";
252 if (stat (builddir.to_str0 (), &statbuf) == 0)
254 dirs.push (builddir.to_str0 ());
258 dirs.push (prefix_directory + "/fonts/otf/");
259 dirs.push (prefix_directory + "/fonts/type1/");
260 dirs.push (prefix_directory + "/fonts/cff/");
261 dirs.push (prefix_directory + "/fonts/svg/");
262 dirs.push (prefix_directory + "/fonts/cff/");
265 for (int i = 0; i < dirs.size (); i++)
266 global_path.prepend (dirs[i]);
270 prepend_load_path (String dir)
272 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
273 scm_c_eval_string (s.to_str0 ());
276 void init_global_tweak_registry ();
277 void init_fontconfig ();
282 /* Now we chroot, setuid/setgrp and chdir. If something goes wrong,
283 we exit (this is a security-sensitive area). First we split
284 jail_spec into its components, then we retrieve the user/group id
285 (necessarily *before* chroot'ing) and finally we perform the
290 USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
293 Array<String> components = String_convert::split (jail_spec, ',');
294 if (components.size () != JAIL_MAX)
296 error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX,
297 components.size ()));
305 if (passwd * passwd = getpwnam (components[USER_NAME].to_str0 ()))
306 uid = passwd->pw_uid;
310 error (_f ("no such user: %s", components[USER_NAME]));
312 error (_f ("can't get user id from user name: %s: %s",
313 components[USER_NAME],
322 if (group * group = getgrnam (components[GROUP_NAME].to_str0 ()))
327 error (_f ("no such group: %s", components[GROUP_NAME]));
329 error (_f ("can't get group id from group name: %s: ",
330 components[GROUP_NAME],
335 if (chroot (components[JAIL].to_str0 ()))
337 error (_f ("can't chroot to: %s: %s", components[JAIL],
344 error (_f ("can't change group id to: %d: %s", gid, strerror (errno)));
350 error (_f ("can't change user id to: %d: %s", uid, strerror (errno)));
354 if (chdir (components[DIR].to_str0 ()))
356 error (_f ("can't change working directory to: %s: %s", components[DIR],
365 main_with_guile (void *, int, char **)
367 /* Engravers use lily.scm contents, need to make Guile find it.
368 Prepend onto GUILE %load-path, very ugh. */
370 prepend_load_path (prefix_directory);
371 prepend_load_path (prefix_directory + "/scm");
373 if (be_verbose_global)
375 is_TeX_format_global = (output_backend_global == "tex"
376 || output_backend_global == "texstr");
378 is_pango_format_global = !is_TeX_format_global;
381 call_constructors ();
382 init_global_tweak_registry ();
387 all_fonts_global = new All_font_metrics (global_path.to_string ());
389 init_scheme_code_string += ")";
390 scm_c_eval_string ((char *) init_scheme_code_string.to_str0 ());
392 /* We accept multiple independent music files on the command line to
393 reduce compile time when processing lots of small files.
394 Starting the GUILE engine is very time consuming. */
398 while (char const *arg = option_parser->get_next_arg ())
400 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
401 tail = SCM_CDRLOC (*tail);
403 delete option_parser;
406 if (files == SCM_EOL)
408 /* No FILE arguments is now a usage error to help newbies. If you
409 want a filter, you're not a newbie and should know to use file
415 if (! jail_spec.is_empty ())
418 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
426 setup_localisation ()
430 setlocale (LC_ALL, "");
432 /* FIXME: check if this is still true.
433 Disable localisation of float values. This breaks TeX output. */
434 setlocale (LC_NUMERIC, "C");
436 String name (PACKAGE);
438 bindtextdomain (name.to_str0 (), LOCALEDIR);
439 textdomain (name.to_str0 ());
444 add_output_format (String format)
446 if (output_format_global != "")
447 output_format_global += ",";
448 output_format_global += format;
452 parse_argv (int argc, char **argv)
454 bool show_help = false;
455 option_parser = new Getopt_long (argc, argv, options_static);
456 while (Long_option_init const *opt = (*option_parser) ())
458 switch (opt->shortname_char_)
461 if (String (opt->longname_str0_) == "png"
462 || String (opt->longname_str0_) == "pdf"
463 || String (opt->longname_str0_) == "ps"
464 || String (opt->longname_str0_) == "dvi"
465 || String (opt->longname_str0_) == "tex")
467 add_output_format (opt->longname_str0_);
469 else if (String (opt->longname_str0_) == "preview")
471 else if (String (opt->longname_str0_) == "no-pages")
481 String s = option_parser->optional_argument_str0_;
482 File_name file_name (s);
483 output_name_global = file_name.to_string ();
487 jail_spec = option_parser->optional_argument_str0_;
490 init_scheme_code_string += option_parser->optional_argument_str0_;
498 output_backend_global = option_parser->optional_argument_str0_;
502 output_format_global = option_parser->optional_argument_str0_;
506 dump_header_fieldnames_global
507 .push (option_parser->optional_argument_str0_);
510 global_path.append (option_parser->optional_argument_str0_);
513 init_name_global = option_parser->optional_argument_str0_;
519 be_verbose_global = true;
522 be_safe_global = true;
528 programming_error (to_string ("unhandled short option: %c",
529 opt->shortname_char_));
539 if (be_verbose_global)
546 main (int argc, char **argv)
548 setup_localisation ();
550 parse_argv (argc, argv);
553 scm_boot_guile (argc, argv, main_with_guile, 0);