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