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>
26 #include "all-font-metrics.hh"
27 #include "file-name.hh"
28 #include "getopt-long.hh"
29 #include "global-ctor.hh"
31 #include "lily-guile.hh"
32 #include "lily-version.hh"
34 #include "output-def.hh"
36 #include "freetype.hh"
37 #include "string-convert.hh"
40 * Global options that can be overridden through command line.
43 /* Names of header fields to be dumped to a separate file. */
44 Array<String> dump_header_fieldnames_global;
46 /* Name of initialisation file. */
47 String init_name_global;
49 /* Selected output format.
50 One of tex, ps, scm, as.
52 String output_backend_global = "ps";
53 String output_format_global = "pdf";
54 bool is_pango_format_global;
56 /* Current output name. */
57 String output_name_global;
59 /* Run in safe mode? */
60 bool be_safe_global = false;
62 /* Verbose progress indication? */
63 bool be_verbose_global = false;
65 /* Scheme code to execute before parsing, after .scm init
66 This is where -e arguments are appended to.
68 String init_scheme_code_string = "(begin #t ";
70 bool make_preview = false;
71 bool make_pages = true;
75 * Miscellaneous global stuff.
77 File_path global_path;
84 static char const *AUTHORS =
85 " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
86 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
88 static char const *PROGRAM_NAME = "lilypond";
89 static char const *PROGRAM_URL = "http://lilypond.org";
91 static char const *NOTICE =
92 _f ("This program is free software. It is covered by the GNU General Public\n"
93 "License and you are welcome to change it and/or distribute copies of it\n"
94 "under certain conditions. Invoke as `%s --warranty' for more\n"
95 "information.\n", "lilypond").to_str0 ();
97 static char const *WARRANTY =
98 _i (" This program is free software; you can redistribute it and/or\n"
99 "modify it under the terms of the GNU General Public License version 2\n"
100 "as published by the Free Software Foundation.\n"
102 " This program is distributed in the hope that it will be useful,\n"
103 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
104 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
105 "General Public License for more details.\n"
107 " You should have received a copy (refer to the file COPYING) of the\n"
108 "GNU General Public License along with this program; if not, write to\n"
109 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
110 "Boston, MA 02111-1307, USA.\n");
113 /* Where the init files live. Typically:
114 LILYPOND_DATADIR = /usr/share/lilypond
115 LOCAL_LILYPOND_DATADIR = /usr/share/lilypond/<VERSION> */
116 char const *prefix_directories[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
118 /* The jail specification: USER,GROUP,JAIL,DIR. */
121 /* The option parser */
122 static Getopt_long *option_parser = 0;
124 /* Internationalisation kludge in two steps:
125 * use _i () to get entry in POT file
126 * call gettext () explicitely for actual "translation" */
128 static Long_option_init options_static[] =
130 {_i ("EXT"), "backend", 'b', _i ("select backend to use")},
131 {_i ("EXPR"), "evaluate", 'e',
132 _i ("set option, use -e '(ly:option-usage)' for help")},
133 /* Bug in option parser: --output =foe is taken as an abbreviation
134 for --output-format. */
135 {_i ("EXTs"), "formats", 'f', _i ("list of formats to dump")},
136 {0, "help", 'h', _i ("print this help")},
137 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
138 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
139 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
140 {_i ("FILE"), "output", 'o', _i ("write output to FILE (suffix will be added)")},
141 {_i ("USER,GROUP,JAIL,DIR"), "jail", 'j', _i ("chroot to JAIL, become USER:GROUP and cd into DIR")},
142 {0, "preview", 'p', _i ("generate a preview")},
143 {0, "no-pages", 0, _i ("don't generate full pages")},
144 {0, "png", 0, _i ("generate PNG")},
145 {0, "ps", 0, _i ("generate PostScript")},
146 {0, "dvi", 0, _i ("generate DVI")},
147 {0, "pdf", 0, _i ("generate PDF (default)")},
148 {0, "tex", 0, _i ("generate TeX")},
149 {0, "safe-mode", 's', _i ("run in safe mode")},
150 {0, "version", 'v', _i ("print version number")},
151 {0, "verbose", 'V', _i ("be verbose")},
152 {0, "warranty", 'w', _i ("show warranty and copyright")},
160 fprintf (out, "LILYPOND_DATADIR =\"%s\"\n", LILYPOND_DATADIR);
161 fprintf (out, "LOCAL_LILYPOND_DATADIR =\"\%s\"\n", LOCAL_LILYPOND_DATADIR);
162 fprintf (out, "LOCALEDIR =\"%s\"\n", LOCALEDIR);
164 char *lilypond_prefix = getenv ("LILYPONDPREFIX");
165 fprintf (out, "LILYPONDPREFIX =\"%s\"\n",
166 (lilypond_prefix ? lilypond_prefix : ""));
172 printf (_f ("Copyright (c) %s by\n%s and others.",
174 AUTHORS).to_str0 ());
181 fputs (gnu_lilypond_version_string ().to_str0 (), out);
189 printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
197 /* No version number or newline here. It confuses help2man. */
198 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
200 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
202 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
204 printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
206 printf (_ ("Options:").to_str0 ());
208 printf (Long_option_init::table_string (options_static).to_str0 ());
210 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
222 printf (_ (WARRANTY).to_str0 ());
228 if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
229 prefix_directories[1] = lilypond_prefix;
231 global_path.append ("");
233 /* Adding mf/out make lilypond unchanged source directory, when setting
234 LILYPONDPREFIX to lilypond-x.y.z */
235 char *suffixes[] = {"ly", "cff", "otf", "mf/out", "scm", "tfm", "ps", "svg",
238 for (unsigned i = 0; prefix_directories[i]; i++)
239 for (char **s = suffixes; *s; s++)
241 String p = prefix_directories[i] + to_string ('/') + String (*s);
242 global_path.prepend (p);
245 /* Urg: GNU make's $ (word) index starts at 1 */
247 while (global_path.try_append (p + to_string (".") + to_string (i)))
254 prepend_load_path (String dir)
256 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
257 scm_c_eval_string (s.to_str0 ());
260 void init_global_tweak_registry ();
261 void init_fontconfig ();
267 /* Now we chroot, setuid/setgrp and chdir. If something goes wrong, we exit (this is a
268 security-sensitive area). First we split jail_spec into its components, then we
269 retrieve the user/group id (necessarily *before* chroot'ing!) and finally we perform
270 the actual actions. */
272 Array<String> components = String_convert::split(jail_spec, ',');
273 if (components.size() < 3)
275 error (_ ("too few elements in jail specification"));
278 if (components.size() > 4)
280 error (_ ("too many elements in jail specification"));
285 char *user_name = components[0].get_str0 ();
286 char *group_name = components[1].get_str0 ();
287 char *jail = components[2].get_str0 ();
288 char *wd = components[3].get_str0 ();
291 struct passwd *passwd = getpwnam(user_name);
295 error (_ ("user not found"));
297 error(_f ("can't get user id from user name (%s)", strerror (errno)));
300 uid = passwd->pw_uid;
303 struct group *group = getgrnam(group_name);
307 error (_ ("group not found"));
309 error(_f ("can't get group id from group name (%s)", strerror (errno)));
316 error (_f ("can't chroot (%s)", strerror (errno)));
322 error (_f ("can't change group id (%s)", strerror (errno)));
328 error (_f ("can't change user id (%s)", strerror (errno)));
334 error (_f ("can't change working directory (%s)", strerror (errno)));
341 main_with_guile (void *, int, char **)
343 /* Engravers use lily.scm contents, need to make Guile find it.
344 Prepend onto GUILE %load-path, very ugh. */
345 for (unsigned i = 0; prefix_directories[i]; i++)
347 prepend_load_path (prefix_directories[i]);
348 /* Junk this. We should make real modules iso. just loading files. */
349 prepend_load_path (String (prefix_directories[i]) + "/scm");
352 if (be_verbose_global)
356 call_constructors ();
357 init_global_tweak_registry ();
361 is_pango_format_global = (output_backend_global != "tex"
362 && output_backend_global != "texstr");
364 all_fonts_global = new All_font_metrics (global_path.to_string ());
366 init_scheme_code_string += ")";
367 scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
369 /* We accept multiple independent music files on the command line to
370 reduce compile time when processing lots of small files.
371 Starting the GUILE engine is very time consuming. */
375 while (char const *arg = option_parser->get_next_arg ())
377 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
378 tail = SCM_CDRLOC (*tail);
380 delete option_parser;
383 if (files == SCM_EOL)
385 /* No FILE arguments is now a usage error to help newbies. If you
386 want a filter, you're not a newbie and should know to use file
392 if (! jail_spec.is_empty ())
395 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
404 setup_localisation ()
408 setlocale (LC_ALL, "");
410 /* FIXME: check if this is still true.
411 Disable localisation of float values. This breaks TeX output. */
412 setlocale (LC_NUMERIC, "C");
414 String name (PACKAGE);
416 bindtextdomain (name.to_str0 (), LOCALEDIR);
417 textdomain (name.to_str0 ());
422 add_output_format (String format)
424 if (output_format_global != "")
425 output_format_global += ",";
426 output_format_global += format;
431 parse_argv (int argc, char **argv)
434 option_parser = new Getopt_long (argc, argv, options_static);
435 while (Long_option_init const *opt = (*option_parser) ())
437 switch (opt->shortname_char_)
440 if (String (opt->longname_str0_) == "png"
441 || String (opt->longname_str0_) == "pdf"
442 || String (opt->longname_str0_) == "ps"
443 || String (opt->longname_str0_) == "dvi"
444 || String (opt->longname_str0_) == "tex")
446 add_output_format (opt->longname_str0_);
448 else if (String (opt->longname_str0_) == "preview")
450 else if (String (opt->longname_str0_) == "no-pages")
460 String s = option_parser->optional_argument_str0_;
461 File_name file_name (s);
462 output_name_global = file_name.to_string ();
466 jail_spec = option_parser->optional_argument_str0_;
469 init_scheme_code_string += option_parser->optional_argument_str0_;
477 output_backend_global = option_parser->optional_argument_str0_;
481 output_format_global = option_parser->optional_argument_str0_;
485 dump_header_fieldnames_global
486 .push (option_parser->optional_argument_str0_);
489 global_path.append (option_parser->optional_argument_str0_);
492 init_name_global = option_parser->optional_argument_str0_;
498 be_verbose_global = true;
501 be_safe_global = true;
507 programming_error (to_string ("unhandled short option: %c",
508 opt->shortname_char_));
518 if (be_verbose_global)
525 main (int argc, char **argv)
527 setup_localisation ();
529 parse_argv (argc, argv);
531 initialize_kpathsea (argv[0]);
533 scm_boot_guile (argc, argv, main_with_guile, 0);