]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
ce313b93d17cc05ec35bdd166dc7e0fe0e3854cb
[lilypond.git] / lily / main.cc
1 /*
2   main.cc -- implement main () entrypoint.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "main.hh"
10
11 #include <cassert>
12 #include <clocale>
13 #include <cstring>
14
15 #include "config.hh"
16
17 #if HAVE_GETTEXT
18 #include <libintl.h>
19 #endif
20
21 #include "all-font-metrics.hh"
22 #include "file-name.hh"
23 #include "getopt-long.hh"
24 #include "global-ctor.hh"
25 #include "kpath.hh"
26 #include "lily-guile.hh"
27 #include "lily-version.hh"
28 #include "misc.hh"
29 #include "output-def.hh"
30 #include "warn.hh"
31 #include "freetype.hh"
32
33 /*
34  * Global options that can be overridden through command line.
35  */
36
37 /* Names of header fields to be dumped to a separate file. */
38 Array<String> dump_header_fieldnames_global;
39
40 /* Name of initialisation file. */
41 String init_name_global;
42
43 /* Selected output format.
44    One of tex, ps, scm, as.
45 */
46 String output_backend_global = "ps";
47 String output_format_global = "pdf";
48 bool is_pango_format_global;
49
50 /* Current output name. */
51 String output_name_global;
52
53 /* Run in safe mode? */
54 bool be_safe_global = false;
55
56 /* Verbose progress indication? */
57 bool be_verbose_global = false;
58
59 /* Scheme code to execute before parsing, after .scm init
60    This is where -e arguments are appended to.
61 */
62 String init_scheme_code_string = "(begin #t ";
63
64 bool make_preview = false;
65 bool make_pages = true;
66
67
68 /*
69  * Miscellaneous global stuff.
70  */
71 File_path global_path;
72
73
74 /*
75  * File globals.
76  */
77
78 static char const *AUTHORS =
79 "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
80 "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
81
82 static char const *PROGRAM_NAME = "lilypond";
83 static char const *PROGRAM_URL = "http://lilypond.org";
84
85 static char const *NOTICE =
86 _f ("This program is free software.  It is covered by the GNU General Public\n"
87     "License and you are welcome to change it and/or distribute copies of it\n"
88     "under certain conditions.  Invoke as `%s --warranty' for more\n"
89     "information.\n", "lilypond").to_str0 ();
90   
91 static char const *WARRANTY =
92 _i ("    This program is free software; you can redistribute it and/or\n"
93     "modify it under the terms of the GNU General Public License version 2\n"
94     "as published by the Free Software Foundation.\n"
95     "\n"
96     "    This program is distributed in the hope that it will be useful,\n"
97     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
98     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
99     "General Public License for more details.\n"
100     "\n"
101     "    You should have received a copy (refer to the file COPYING) of the\n"
102     "GNU General Public License along with this program; if not, write to\n"
103     "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
104     "Boston, MA 02111-1307, USA.\n");
105
106
107 /* Where the init files live.  Typically:
108    LILYPOND_DATADIR = /usr/share/lilypond
109    LOCAL_LILYPOND_DATADIR = /usr/share/lilypond/<VERSION> */
110 char const *prefix_directories[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
111
112 /*  The option parser */
113 static Getopt_long *option_parser = 0;
114
115 /* Internationalisation kludge in two steps:
116    * use _i () to get entry in POT file
117    * call gettext () explicitely for actual "translation"  */
118
119 static Long_option_init options_static[] =
120   {
121     {_i ("EXT"), "backend", 'b', _i ("select backend to use")},
122     {_i ("EXPR"), "evaluate", 'e',
123      _i ("set option, use -e '(ly:option-usage)' for help")},
124     /* Bug in option parser: --output =foe is taken as an abbreviation
125        for --output-format.  */
126     {_i ("EXTs"), "formats", 'f', _i ("list of formats to dump")},
127     {0, "help", 'h',  _i ("print this help")},
128     {_i ("FIELD"), "header", 'H',  _i ("write header field to BASENAME.FIELD")},
129     {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
130     {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
131     {_i ("FILE"), "output", 'o',  _i ("write output to FILE (suffix will be added)")},
132     {0, "preview", 'p',  _i ("generate a preview")},
133     {0, "no-pages", 0,  _i ("don't generate full pages")},
134     {0, "png", 0,  _i ("generate PNG")},
135     {0, "ps", 0,  _i ("generate PostScript")},
136     {0, "dvi", 0,  _i ("generate DVI")},
137     {0, "pdf", 0,  _i ("generate PDF (default)")},
138     {0, "tex", 0,  _i ("generate TeX")},
139     {0, "safe-mode", 's',  _i ("run in safe mode")},
140     {0, "version", 'v',  _i ("print version number")},
141     {0, "verbose", 'V', _i ("be verbose")},
142     {0, "warranty", 'w',  _i ("show warranty and copyright")},
143     {0, 0, 0, 0}
144   };
145
146 static void
147 dir_info (FILE *out)
148 {
149   fputs ("\n", out);
150   fprintf (out, "LILYPOND_DATADIR =\"%s\"\n", LILYPOND_DATADIR);
151   fprintf (out, "LOCAL_LILYPOND_DATADIR =\"\%s\"\n", LOCAL_LILYPOND_DATADIR);
152   fprintf (out, "LOCALEDIR =\"%s\"\n", LOCALEDIR);
153
154   char *lilypond_prefix = getenv ("LILYPONDPREFIX");
155   fprintf (out, "LILYPONDPREFIX =\"%s\"\n",
156            (lilypond_prefix ? lilypond_prefix : ""));
157 }
158
159 static void
160 copyright ()
161 {
162   printf (_f ("Copyright (c) %s by\n%s  and others.",
163               "1996--2005",
164               AUTHORS).to_str0 ());
165   printf ("\n");
166 }
167
168 static void
169 identify (FILE *out)
170 {
171   fputs (gnu_lilypond_version_string ().to_str0 (), out);
172   fputs ("\n", out);
173 }
174  
175 static void
176 notice ()
177 {
178   identify (stdout);
179   printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
180   printf ("\n");
181   copyright ();
182 }
183
184 static void
185 usage ()
186 {
187   /* No version number or newline here.  It confuses help2man.  */
188   printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
189   printf ("\n\n");
190   printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
191   printf ("\n\n");
192   printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
193   printf ("\n");
194   printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
195   printf ("\n\n");
196   printf (_ ("Options:").to_str0 ());
197   printf ("\n");
198   printf (Long_option_init::table_string (options_static).to_str0 ());
199   printf ("\n");
200   printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
201   printf ("\n");
202   printf ("\n");
203 }
204
205 static void
206 warranty ()
207 {
208   identify (stdout);
209   printf ("\n");
210   copyright ();
211   printf ("\n");
212   printf (_ (WARRANTY).to_str0 ());
213 }
214
215 static void
216 setup_paths ()
217 {
218   if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
219     prefix_directories[1] = lilypond_prefix;
220
221   global_path.append ("");
222
223   /* Adding mf/out make lilypond unchanged source directory, when setting
224      LILYPONDPREFIX to lilypond-x.y.z */
225   char *suffixes[] = {"ly", "cff", "otf", "mf/out", "scm", "tfm", "ps", "svg",
226                       0};
227
228   for (unsigned i = 0; prefix_directories[i]; i++)
229     for (char **s = suffixes; *s; s++)
230       {
231         String p = prefix_directories[i] + to_string ('/') + String (*s);
232         global_path.prepend (p);
233         
234 #if !KPATHSEA
235         /* Urg: GNU make's $ (word) index starts at 1 */
236         int i  = 1;
237         while (global_path.try_append (p + to_string (".") + to_string (i)))
238           i++;
239 #endif
240       }
241 }
242   
243 static void
244 prepend_load_path (String dir)
245 {
246   String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
247   scm_c_eval_string (s.to_str0 ());
248 }
249
250 void init_global_tweak_registry ();
251 void init_fontconfig ();
252
253
254 static void
255 main_with_guile (void *, int, char **)
256 {
257   /* Engravers use lily.scm contents, need to make Guile find it.
258      Prepend onto GUILE %load-path, very ugh. */
259   for (unsigned i = 0; prefix_directories[i]; i++)
260     {
261       prepend_load_path (prefix_directories[i]);
262       /* Junk this.  We should make real modules iso. just loading files. */
263       prepend_load_path (String (prefix_directories[i]) + "/scm");
264     }
265
266   if (be_verbose_global)
267     dir_info (stderr);
268
269   ly_c_init_guile ();
270   call_constructors ();
271   init_global_tweak_registry ();
272   init_fontconfig ();
273   init_freetype ();
274
275   is_pango_format_global = (output_backend_global != "tex"
276                             && output_backend_global != "texstr");
277
278   all_fonts_global = new All_font_metrics (global_path.to_string ());
279
280   init_scheme_code_string += ")";
281   scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
282
283   /* We accept multiple independent music files on the command line to
284      reduce compile time when processing lots of small files.
285      Starting the GUILE engine is very time consuming.  */
286
287   SCM files = SCM_EOL;
288   SCM *tail = &files;
289   while (char const *arg = option_parser->get_next_arg ())
290     {
291       *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
292       tail = SCM_CDRLOC (*tail);
293     }
294   delete option_parser;
295   option_parser = 0;
296
297   if (files == SCM_EOL)
298     {
299       /* No FILE arguments is now a usage error to help newbies.  If you
300          want a filter, you're not a newbie and should know to use file
301          argument `-'.  */
302       usage ();
303       exit (2);
304     }
305
306   SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
307   (void) result;
308
309
310   /* Unreachable.  */
311   exit (0);
312 }
313
314 static void
315 setup_localisation ()
316 {
317 #if HAVE_GETTEXT
318   /* Enable locales */
319   setlocale (LC_ALL, "");
320   
321   /* FIXME: check if this is still true.
322     Disable localisation of float values.  This breaks TeX output.  */
323   setlocale (LC_NUMERIC, "C");
324   
325   String name (PACKAGE);
326   name.to_lower ();
327   bindtextdomain (name.to_str0 (), LOCALEDIR);
328   textdomain (name.to_str0 ());
329 #endif
330 }
331
332 static void
333 add_output_format (String format)
334 {
335   if (output_format_global != "")
336     output_format_global += ",";
337   output_format_global += format;
338 }
339
340
341 static void
342 parse_argv (int argc, char **argv)
343 {
344   bool help_b = false;
345   option_parser = new Getopt_long (argc, argv, options_static);
346   while (Long_option_init const *opt = (*option_parser) ())
347     {
348       switch (opt->shortname_char_)
349         {
350         case 0:
351           if (String (opt->longname_str0_) == "png"
352               || String (opt->longname_str0_) == "pdf"
353               || String (opt->longname_str0_) == "ps"
354               || String (opt->longname_str0_) == "dvi"
355               || String (opt->longname_str0_) == "tex")
356             {
357               add_output_format (opt->longname_str0_);
358             }
359           else if (String (opt->longname_str0_) == "preview")
360             make_preview = true;
361           else if (String (opt->longname_str0_) == "no-pages")
362             make_pages = false;
363           break;
364           
365         case 'v':
366           notice ();
367           exit (0);
368           break;
369         case 'o':
370           {
371             String s = option_parser->optional_argument_str0_;
372             File_name file_name (s);
373             output_name_global = file_name.to_string ();
374           }
375           break;
376         case 'e':
377           init_scheme_code_string += option_parser->optional_argument_str0_;
378           break;
379         case 'w':
380           warranty ();
381           exit (0);
382           break;
383           
384         case 'b':
385           output_backend_global = option_parser->optional_argument_str0_;
386           break;
387
388         case 'f':
389           output_format_global = option_parser->optional_argument_str0_;
390           break;
391           
392         case 'H':
393           dump_header_fieldnames_global
394             .push (option_parser->optional_argument_str0_);
395           break;
396         case 'I':
397           global_path.append (option_parser->optional_argument_str0_);
398           break;
399         case 'i':
400           init_name_global = option_parser->optional_argument_str0_;
401           break;
402         case 'h':
403           help_b = true;
404           break;
405         case 'V':
406           be_verbose_global = true;
407           break;
408         case 's':
409           be_safe_global = true;
410           break;
411         case 'p':
412           make_preview = true;
413           break;
414         default:
415           programming_error (to_string ("unhandled short option: %c",
416                                         opt->shortname_char_));
417           assert (false);
418           break;
419         }
420     }
421
422   if (help_b)
423     {
424       identify (stdout);
425       usage ();
426       if (be_verbose_global)
427         dir_info (stdout);
428       exit (0);
429     }
430 }
431
432 int
433 main (int argc, char **argv)
434 {
435   setup_localisation ();
436   setup_paths ();
437   parse_argv (argc, argv);
438   identify (stderr);
439   initialize_kpathsea (argv[0]);
440
441   scm_boot_guile (argc, argv, main_with_guile, 0);
442
443   /* Unreachable */
444   return 0;
445 }