]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.1.43
[lilypond.git] / lily / main.cc
1 /*
2   main.cc -- implement main: entrypoints
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <stdlib.h>
10 #include <iostream.h>
11 #include <assert.h>
12 #include <locale.h>
13 #include "lily-guile.hh"
14 #include "lily-version.hh"
15
16 #include "all-fonts.hh"
17 #include "proto.hh"
18 #include "dimensions.hh"
19 #include "getopt-long.hh"
20 #include "misc.hh"
21 #include "string.hh"
22 #include "main.hh"
23 #include "file-path.hh"
24 #include "config.hh"
25 #include "file-results.hh"
26 #include "debug.hh"
27 #include "lily-guile.hh"
28 #include "paper-def.hh"
29 #include "midi-def.hh"
30 #include "global-ctor.hh"
31
32 #if HAVE_GETTEXT
33 #include <libintl.h>
34 #endif
35
36
37 bool version_ignore_global_b = false;
38 bool no_paper_global_b = false;
39 bool no_timestamps_global_b = false;
40 bool find_old_relative_b = false;
41
42 char const* output_global_ch = "tex";
43 All_font_metrics *all_fonts_global_p;
44
45 String default_outname_base_global =  "lelie";
46 String outname_str_global;
47 String init_str_global;
48
49 int default_count_global;
50 File_path global_path;
51
52 bool safe_global_b = false;
53 bool experimental_features_global_b = false;
54 bool dependency_global_b = false;
55
56 int exit_status_i_;
57
58 Getopt_long * oparser_global_p = 0;
59
60 String distill_inname_str (String name_str, String& ext_r);
61
62 Long_option_init theopts[] = {
63   {0, "about", 'a'},
64   {1, "output", 'o'},
65   {0, "warranty", 'w'},
66   {0, "help", 'h'},
67   {0, "test", 't'},
68   {0, "debug", 'D'},
69   {1, "init", 'i'},
70   {1, "include", 'I'},
71   {0, "no-paper", 'M'},
72   {0, "dependencies", 'd'},
73   {0, "no-timestamps", 'T'},    // why do we have this option?
74   {0, "find-old-relative", 'Q'},
75   {0, "ignore-version", 'V'},
76   {1, "output-format", 'f'},
77   {0, "safe", 's'},
78   {0,0,0}
79 };
80
81 void
82 usage ()
83 {
84   cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond") << '\n';
85   cout << _ ("Typeset music and or play MIDI from FILE or <stdin>");
86   cout << '\n';
87   cout << '\n';
88   cout << _ ("Options:");
89   cout << '\n';
90   cout  << _ (
91     "  -a, --about            about LilyPond\n"
92     );
93   cout  << _ (
94     "  -D, --debug            enable debugging output\n"
95     );
96   cout  << _ (
97     "  -d, --dependencies     write Makefile dependencies for every input file\n"
98     );
99   cout  << _ (
100     "  -h, --help             this help\n"
101     );
102   cout  << _ (
103     "  -f, --output-format=X  use output format X\n"
104     );
105   cout  << _ (
106     "  -I, --include=DIR      add DIR to search path\n"
107     );
108   cout  << _ (
109     "  -i, --init=FILE        use FILE as init file\n"
110     );
111   cout  << _ (
112     "  -M, --no-paper         produce midi output only\n"
113     );
114   cout  << _ (
115     "  -o, --output=FILE      set FILE as default output base\n"
116     );
117   cout  << _ (
118     "  -Q, --find-old-relative show all changes in relative syntax\n"
119     );
120   cout << _ (
121     "  -s, --safe             inhibit file output naming and exporting TeX  macros\n");
122   cout  << _ (
123     "  -t, --test             switch on experimental features\n"
124     );
125   cout  << _ (
126     "  -T, --no-timestamps    don't timestamp the output\n"
127     );
128   cout  << _ (
129     "  -V, --ignore-version   ignore mudela version\n"
130     );
131   cout  << _ (
132               "  -w, --warranty         show warranty and copyright\n"
133               );
134   cout << '\n';
135   cout << _ ("GNU LilyPond was compiled with the following settings:");
136   cout << '\n';
137   cout <<
138 #ifdef NDEBUG
139     "NDEBUG "
140 #endif
141 #ifdef NPRINT
142     "NPRINT "
143 #endif
144 #ifdef STRING_UTILS_INLINED
145     "STRING_UTILS_INLINED "
146 #endif
147     "datadir=" DIR_DATADIR
148     "\n"
149     "localedir=" DIR_LOCALEDIR
150
151     "\n";
152
153   ;
154
155   print_mudela_versions (cout);
156 }
157
158 void
159 about ()
160 {
161   cout << '\n';
162   cout << 
163 #include "BLURB.hh"
164     cout << '\n';
165   cout << _ ("GNU LilyPond is Free software, see --warranty");
166   cout << '\n';
167   cout << '\n';
168   cout << _f ("Copyright (c) %s by", "1996--1999");
169   cout << '\n';
170   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
171   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
172   cout << '\n';
173 }
174
175 void
176 notice ()
177 {
178   cout << '\n';
179   cout << _ ("GNU LilyPond -- The GNU Project music typesetter");
180   cout << '\n';
181   cout << _f ("Copyright (c) %s by", "1996--1999");
182   cout << '\n';
183   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
184   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
185   cout << '\n';
186   cout << _ (
187              "    This program is free software; you can redistribute it and/or\n"
188              "modify it under the terms of the GNU General Public License version 2\n"
189              "as published by the Free Software Foundation.\n"
190              "\n"
191              "    This program is distributed in the hope that it will be useful,\n"
192              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
193              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
194              "General Public License for more details.\n"
195              "\n"
196              "    You should have received a copy (refer to the file COPYING) of the\n"
197              "GNU General Public License along with this program; if not, write to\n"
198              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
199              "USA.\n");
200 }
201
202 void
203 identify ()
204 {
205   *mlog << gnu_lilypond_version_str () << endl;
206 }
207
208 void
209 setup_paths ()
210 {
211   // facilitate binary distributions
212   char const *env_lily = getenv ("LILYPONDPREFIX");
213   String prefix_directory;
214   if (env_lily)
215     prefix_directory = env_lily;
216
217 #if HAVE_GETTEXT
218   setlocale (LC_ALL, ""); /* enable locales */
219   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
220   String lily_locale_dir;
221   String name (PACKAGE);
222   name.to_lower ();
223   if (!prefix_directory.empty_b())
224     {
225       lily_locale_dir = prefix_directory + "/share/locale";
226       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C());
227     }
228   else
229     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
230   textdomain (name.ch_C ());
231 #endif
232
233   global_path.add ("");
234   // must override (come before) "/usr/local/share/lilypond"!
235   char const *env_sz = getenv ("LILYINCLUDE");
236   if (env_sz)
237     global_path.parse_path (env_sz);
238
239
240   char *suffixes[] = {"ly", "afm", "scm", "tfm", "cmtfm", 0};
241   for (char **s = suffixes; *s; s++){
242     if (!prefix_directory.empty_b())
243       global_path.add (prefix_directory + to_str ('/') + String (*s));
244     else
245       global_path.add (String (DIR_DATADIR) + to_str ('/') + String(*s));
246   }
247 }
248
249
250 void
251 main_prog (int argc, char **argv)
252 {
253   /*
254     need to do this first. Engravers use lily.scm contents.
255    */
256   extern void ly_init_protection();
257   ly_init_protection();  
258   init_symbols ();
259   read_lily_scm_file ( "lily.scm");
260   cout << endl;
261
262   call_constructors ();
263   default_outname_base_global = "lelie";
264   all_fonts_global_p = new All_font_metrics (global_path.str ());
265   
266   int p=0;
267   const char *arg ;
268   while ((arg= oparser_global_p->get_next_arg ()))
269     {
270       
271       if (outname_str_global == "")
272         {
273           Midi_def::reset_default_count ();
274           Paper_def::reset_default_count ();
275         }
276       String f (arg);
277       String i;
278       f = distill_inname_str (f, i);
279       if (f == "-")
280         default_outname_base_global = "-";
281       else
282         {
283           String a,b,c,d;
284           split_path (f, a, b, c, d);
285           default_outname_base_global = c;
286         }
287       if (outname_str_global.length_i ())
288         default_outname_base_global = outname_str_global;
289       if (init_str_global.length_i ())
290         i = init_str_global;
291       else
292         i = "init" + i;
293       do_one_file (i, f);
294       p++;
295     }
296   if (!p)
297     {
298       String i;
299       if (init_str_global.length_i ())
300         i = init_str_global;
301       else
302         i = "init.ly";
303       default_outname_base_global = "-";
304       if (outname_str_global.length_i ())
305         default_outname_base_global = outname_str_global;
306       do_one_file (i, default_outname_base_global);
307     }
308   delete oparser_global_p;
309   exit( exit_status_i_);
310 }
311
312
313 int
314 main (int argc, char **argv)
315 {
316   identify ();
317   debug_init ();                // should be first
318   setup_paths ();
319
320   oparser_global_p = new Getopt_long(argc, argv,theopts);
321   while (Long_option_init const * opt = (*oparser_global_p)())
322     {
323       switch (opt->shortname)
324         {
325         case 't':
326           experimental_features_global_b = true;
327           *mlog << "*** enabling experimental features, you're on your own now ***\n";
328           break;
329         case 'o':
330           outname_str_global = oparser_global_p->optional_argument_ch_C_;
331           break;
332         case 'w':
333           notice ();
334           exit (0);
335           break;
336         case 'f':
337           output_global_ch = oparser_global_p->optional_argument_ch_C_;
338           break;
339         case 'Q':
340           find_old_relative_b= true;
341           break;
342         case 'I':
343           global_path.push (oparser_global_p->optional_argument_ch_C_);
344           break;
345         case 'i':
346           init_str_global = oparser_global_p->optional_argument_ch_C_;
347           break;
348         case 'a':
349           about ();
350           exit (0);
351         case 'h':
352           usage ();
353           exit (0);
354           break;
355         case 'V':
356           version_ignore_global_b = true;
357           break;
358         case 's':
359           safe_global_b = true;
360           break;
361         case 'd':
362           dependency_global_b = true;
363           break; 
364         case 'D':
365           set_debug (true);
366           break;
367         case 'M':
368           no_paper_global_b = true;
369           break;
370         case 'T':
371           no_timestamps_global_b = true;
372           break;
373         default:
374           assert (false);
375           break;
376         }
377     }
378
379 #ifdef WINNT
380   gh_enter (argc, argv, main_prog);
381 #else
382   gh_enter (argc, argv, (void(*)())main_prog);
383 #endif
384
385   return 0;                     // unreachable
386 }
387
388 /**
389   make input file name from command arg.
390
391   @input file name
392
393   @output file name with added default extension. "" is stdin.
394           in reference argument: the extension. ".ly" if none
395  */
396 String
397 distill_inname_str (String name_str, String& ext_r)
398 {
399   String str = name_str;
400   if (str.length_i ())
401     {
402       if (str != "-")
403         {
404           String a,b,c;
405           split_path (str,a,b,c,ext_r);
406
407           // add extension if not present.
408           char const* extensions[] = {"", "", ".ly", ".fly", ".sly", 0};
409           extensions[0] = ext_r.ch_C ();
410           for (int i = 0; extensions[i]; i++)
411             {
412               if (!global_path.find (a+b+c+extensions[i]).empty_b ())
413                 {
414                   ext_r = extensions[i];
415                   break;
416                 }
417             }
418           str = a+b+c+ext_r;
419         }
420     }
421   else 
422     {
423       str = "-";
424       ext_r = ".ly";
425     }
426   return str;
427 }
428