]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
74ac10ecd4a071b044e07601dcd1233784a99cd1
[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 "all-font-metrics.hh"
22 #include "file-name.hh"
23 #include "file-path.hh"
24 #include "getopt-long.hh"
25 #include "global-ctor.hh"
26 #include "kpath.hh"
27 #include "lily-guile.hh"
28 #include "lily-version.hh"
29 #include "main.hh"
30 #include "misc.hh"
31 #include "output-def.hh"
32 #include "string.hh"
33 #include "warn.hh"
34
35 /*
36  * Global options that can be overridden through command line.
37  */
38
39 /* Names of header fields to be dumped to a separate file. */
40 Array<String> dump_header_fieldnames_global;
41
42 /* Name of initialisation file. */
43 String init_name_global;
44
45 /* Do not calculate and write paper output? */
46 bool no_paper_global_b = false;
47
48 /* Selected output format.
49    One of tex, ps, scm, as. */
50 String output_format_global = "tex";
51
52 /* Current output name. */
53 String output_name_global;
54
55 /* Run in safe mode? */
56 bool safe_global_b = false;
57
58 /* Verbose progress indication? */
59 bool verbose_global_b = false;
60
61 /* Scheme code to execute before parsing, after .scm init
62    This is where -e arguments are appended to.
63 */
64 String init_scheme_code_string = "(begin #t ";
65
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-bin";
83 static char const *PROGRAM_URL = "http://lilypond.org";
84
85 static char const *NOTICE =
86 _i ("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 `lilypond-bin --warranty' for more\n"
89     "information.\n");
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_directory[] = {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 ("EXPR"), "evaluate", 'e',
122      _i ("set options, use -e '(ly-option-usage)' for help")},
123     /* Bug in option parser: --output=foe is taken as an abbreviation
124        for --output-format.  */
125     {_i ("EXT"), "format", 'f', _i ("use output format EXT")},
126     {0, "help", 'h',  _i ("print this help")},
127     {_i ("FIELD"), "header", 'H',  _i ("write header field to BASENAME.FIELD")},
128     {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
129     {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
130     {0, "no-paper", 'm',  _i ("produce MIDI output only")},
131     {_i ("FILE"), "output", 'o',  _i ("write output to FILE")},
132     {0, "safe-mode", 's',  _i ("run in safe mode")},
133     {0, "version", 'v',  _i ("print version number")},
134     {0, "verbose", 'V', _i ("be verbose")},
135     {0, "warranty", 'w',  _i ("show warranty and copyright")},
136     {0,0,0,0}
137   };
138
139 static void
140 dir_info (FILE *out)
141 {
142   fputs ("\n", out);
143   fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
144   fprintf (out, "LOCAL_LILYPOND_DATADIR=\%s\"\n", LOCAL_LILYPOND_DATADIR);
145   fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
146
147   char *lilypond_prefix = getenv ("LILYPONDPREFIX");
148   fprintf (out, "LILYPONDPREFIX=\"%s\"\n",
149            (lilypond_prefix ? lilypond_prefix : ""));
150 }
151
152 static void
153 copyright ()
154 {
155   printf (_f ("Copyright (c) %s by\n%s  and others.",
156               "1996--2004",
157               AUTHORS).to_str0 ());
158   printf ("\n");
159 }
160
161 static void
162 identify (FILE *out)
163 {
164   fputs (gnu_lilypond_version_string ().to_str0 (), out);
165 }
166  
167 static void
168 notice ()
169 {
170   identify (stdout);
171   printf ("\n");
172   printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
173   printf ("\n");
174   copyright ();
175 }
176
177 static void
178 usage ()
179 {
180   /* No version number or newline here.  It confuses help2man.  */
181   printf (_f ("Usage: %s [OPTIONS]... FILE...", PROGRAM_NAME).to_str0 ());
182   printf ("\n\n");
183   printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
184   printf ("\n\n");
185   printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
186   printf ("\n");
187   printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
188   printf ("\n\n");
189   printf (_ ("Options:").to_str0 ());
190   printf ("\n");
191   printf (Long_option_init::table_string (options_static).to_str0 ());
192   printf ("\n");
193   printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
194   printf ("\n");
195   printf ("\n");
196 }
197
198 static void
199 warranty ()
200 {
201   identify (stdout);
202   printf ("\n");
203   copyright ();
204   printf ("\n");
205   printf (_ (WARRANTY).to_str0 ());
206 }
207
208 static void
209 setup_paths ()
210 {
211   if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
212     prefix_directory[1] = lilypond_prefix;
213
214   global_path.append ("");
215
216   /* Adding mf/out make lilypond unchanged source directory, when setting
217      LILYPONDPREFIX to lilypond-x.y.z */
218   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
219
220   for (unsigned i = 0; prefix_directory[i]; i++)
221     for (char **s = suffixes; *s; s++)
222       {
223         String p = prefix_directory[i] + to_string ('/') + String (*s);
224         global_path.prepend (p);
225         
226 #if !KPATHSEA
227         /* Urg: GNU make's $ (word) index starts at 1 */
228         int i  = 1;
229         while (global_path.try_append (p + to_string (".") + to_string (i)))
230           i++;
231 #endif
232       }
233 }
234   
235 static void
236 prepend_load_path (String dir)
237 {
238   String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
239   scm_c_eval_string (s.to_str0 ());
240 }
241
242 static void
243 main_with_guile (void *, int, char **)
244 {
245   /* Engravers use lily.scm contents, need to make Guile find it.
246      Prepend onto GUILE %load-path, very ugh. */
247   for (unsigned i = 0; prefix_directory[i]; i++)
248     {
249       prepend_load_path (prefix_directory[i]);
250       /* Junk this.  We should make real modules iso. just loading files. */
251       prepend_load_path (String (prefix_directory[i]) + "/scm");
252     }
253
254   if (verbose_global_b)
255     dir_info (stderr);
256
257   ly_c_init_guile ();
258   call_constructors ();
259   progress_indication ("\n");
260
261   all_fonts_global = new All_font_metrics (global_path.to_string ());
262
263   init_scheme_code_string += ")";
264   scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
265
266   /* We accept multiple independent music files on the command line to
267      reduce compile time when processing lots of small files.
268      Starting the GUILE engine is very time consuming.  */
269
270   SCM files = SCM_EOL;
271   SCM *tail = &files;
272   while (char const *arg = option_parser->get_next_arg ())
273     {
274       *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
275       tail = SCM_CDRLOC (*tail);
276     }
277   delete option_parser;
278   option_parser = 0;
279
280   if (files == SCM_EOL)
281     {
282       /* No FILE arguments is now a usage error to help newbies.  If you
283          want a filter, you're not a newbie and should know to use file
284          argument `-'.  */
285       usage ();
286       exit (2);
287     }
288
289   SCM result = scm_call_1 (ly_scheme_function ("lilypond-main"), files);
290   (void) result;
291
292
293   /* Unreachable.  */
294   exit (0);
295 }
296
297 static void
298 setup_localisation ()
299 {
300 #if HAVE_GETTEXT
301   /* Enable locales */
302   setlocale (LC_ALL, "");
303   
304   /* FIXME: check if this is still true.
305     Disable localisation of float values.  This breaks TeX output.  */
306   setlocale (LC_NUMERIC, "C");
307   
308   String name (PACKAGE);
309   name.to_lower ();
310   bindtextdomain (name.to_str0 (), LOCALEDIR);
311   textdomain (name.to_str0 ());
312 #endif
313 }
314
315 static void
316 parse_argv (int argc, char **argv)
317 {
318   bool help_b = false;
319   option_parser = new Getopt_long (argc, argv, options_static);
320   while (Long_option_init const *opt = (*option_parser) ())
321     {
322       switch (opt->shortname_char_)
323         {
324         case 'v':
325           notice ();
326           exit (0);
327           break;
328         case 'o':
329           {
330             String s = option_parser->optional_argument_str0_;
331             File_name file_name (s);
332             output_name_global = file_name.to_string ();
333           }
334           break;
335         case 'e':
336           init_scheme_code_string += option_parser->optional_argument_str0_;
337           break;
338         case 'w':
339           warranty ();
340           exit (0);
341           break;
342         case 'f':
343           if (option_parser->optional_argument_str0_ == "help")
344             {
345               printf (_ ("This option is for developers only.").to_str0 ());
346               printf (_ ("Read the sources for more information.").to_str0 ());
347               exit (0);
348             }
349           output_format_global = option_parser->optional_argument_str0_;
350           break;
351         case 'H':
352           dump_header_fieldnames_global
353             .push (option_parser->optional_argument_str0_);
354           break;
355         case 'I':
356           global_path.append (option_parser->optional_argument_str0_);
357           break;
358         case 'i':
359           init_name_global = option_parser->optional_argument_str0_;
360           break;
361         case 'h':
362           help_b = true;
363           break;
364         case 'V':
365           verbose_global_b = true;
366           break;
367         case 's':
368           safe_global_b = true;
369           break;
370         case 'm':
371           no_paper_global_b = true;
372           break;
373         default:
374           assert (false);
375           break;
376         }
377     }
378
379   if (help_b)
380     {
381       usage ();
382       if (verbose_global_b)
383         dir_info (stdout);
384       exit (0);
385     }
386 }
387
388 int
389 main (int argc, char **argv)
390 {
391   setup_localisation ();  
392   setup_paths ();
393   parse_argv (argc, argv);
394   initialize_kpathsea (argv[0]);
395
396   scm_boot_guile (argc, argv, main_with_guile, 0);
397
398   /* Unreachable */
399   return 0;
400 }