]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.1.35
[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
156 void
157 about ()
158 {
159   cout << '\n';
160   cout << 
161 #include "BLURB.hh"
162     cout << '\n';
163   cout << _ ("GNU LilyPond is Free software, see --warranty");
164   cout << '\n';
165   cout << '\n';
166   cout << _f ("Copyright (c) %s by", "1996--1999");
167   cout << '\n';
168   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
169   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
170   cout << '\n';
171 }
172
173 void
174 notice ()
175 {
176   cout << '\n';
177   cout << _ ("GNU LilyPond -- The GNU Project music typesetter");
178   cout << '\n';
179   cout << _f ("Copyright (c) %s by", "1996--1999");
180   cout << '\n';
181   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
182   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
183   cout << '\n';
184   cout << _ (
185              "    This program is free software; you can redistribute it and/or\n"
186              "modify it under the terms of the GNU General Public License version 2\n"
187              "as published by the Free Software Foundation.\n"
188              "\n"
189              "    This program is distributed in the hope that it will be useful,\n"
190              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
191              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
192              "General Public License for more details.\n"
193              "\n"
194              "    You should have received a copy (refer to the file COPYING) of the\n"
195              "GNU General Public License along with this program; if not, write to\n"
196              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
197              "USA.\n");
198 }
199
200 void
201 identify ()
202 {
203   *mlog << gnu_lilypond_version_str () << endl;
204 }
205
206 void
207 setup_paths ()
208 {
209   // facilitate binary distributions
210   char const *env_lily = getenv ("LILYPONDPREFIX");
211   String prefix_directory;
212   if (env_lily)
213     prefix_directory = env_lily;
214
215 #if HAVE_GETTEXT
216   setlocale (LC_ALL, ""); /* enable locales */
217   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
218   String lily_locale_dir;
219   String name (PACKAGE);
220   name.to_lower ();
221   if (!prefix_directory.empty_b())
222     {
223       lily_locale_dir = prefix_directory + "/share/locale";
224       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C());
225     }
226   else
227     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
228   textdomain (name.ch_C ());
229 #endif
230
231   global_path.add ("");
232   // must override (come before) "/usr/local/share/lilypond"!
233   char const *env_sz = getenv ("LILYINCLUDE");
234   if (env_sz)
235     global_path.parse_path (env_sz);
236
237
238   char *suffixes[] = {"ly", "afm", "scm", "tfm", "cmtfm", 0};
239   for (char **s = suffixes; *s; s++){
240     if (!prefix_directory.empty_b())
241       global_path.add (prefix_directory + to_str ('/') + String (*s));
242     else
243       global_path.add (String (DIR_DATADIR) + to_str ('/') + String(*s));
244   }
245 }
246
247
248 void
249 main_prog (int argc, char **argv)
250 {
251   /*
252     need to do this first. Engravers use lily.scm contents.
253    */
254   init_symbols ();
255   read_lily_scm_file ( "lily.scm");
256   cout << endl;
257
258   call_constructors ();
259   default_outname_base_global = "lelie";
260   all_fonts_global_p = new All_font_metrics (global_path.str ());
261   
262   int p=0;
263   const char *arg ;
264   while ((arg= oparser_global_p->get_next_arg ()))
265     {
266       
267       if (outname_str_global == "")
268         {
269           Midi_def::reset_default_count ();
270           Paper_def::reset_default_count ();
271         }
272       String f (arg);
273       String i;
274       f = distill_inname_str (f, i);
275       if (f == "-")
276         default_outname_base_global = "-";
277       else
278         {
279           String a,b,c,d;
280           split_path (f, a, b, c, d);
281           default_outname_base_global = c;
282         }
283       if (outname_str_global.length_i ())
284         default_outname_base_global = outname_str_global;
285       if (init_str_global.length_i ())
286         i = init_str_global;
287       else
288         i = "init" + i;
289       do_one_file (i, f);
290       p++;
291     }
292   if (!p)
293     {
294       String i;
295       if (init_str_global.length_i ())
296         i = init_str_global;
297       else
298         i = "init.ly";
299       default_outname_base_global = "-";
300       if (outname_str_global.length_i ())
301         default_outname_base_global = outname_str_global;
302       do_one_file (i, default_outname_base_global);
303     }
304   delete oparser_global_p;
305   exit( exit_status_i_);
306 }
307
308
309 int
310 main (int argc, char **argv)
311 {
312   identify ();
313   debug_init ();                // should be first
314   setup_paths ();
315
316   oparser_global_p = new Getopt_long(argc, argv,theopts);
317   while (Long_option_init const * opt = (*oparser_global_p)())
318     {
319       switch (opt->shortname)
320         {
321         case 't':
322           experimental_features_global_b = true;
323           *mlog << "*** enabling experimental features, you're on your own now ***\n";
324           break;
325         case 'o':
326           outname_str_global = oparser_global_p->optional_argument_ch_C_;
327           break;
328         case 'w':
329           notice ();
330           exit (0);
331           break;
332         case 'f':
333           output_global_ch = oparser_global_p->optional_argument_ch_C_;
334           break;
335         case 'Q':
336           find_old_relative_b= true;
337           break;
338         case 'I':
339           global_path.push (oparser_global_p->optional_argument_ch_C_);
340           break;
341         case 'i':
342           init_str_global = oparser_global_p->optional_argument_ch_C_;
343           break;
344         case 'a':
345           about ();
346           exit (0);
347         case 'h':
348           usage ();
349           exit (0);
350           break;
351         case 'V':
352           version_ignore_global_b = true;
353           break;
354         case 's':
355           safe_global_b = true;
356           break;
357         case 'd':
358           dependency_global_b = true;
359           break; 
360         case 'D':
361           set_debug (true);
362           break;
363         case 'M':
364           no_paper_global_b = true;
365           break;
366         case 'T':
367           no_timestamps_global_b = true;
368           break;
369         default:
370           assert (false);
371           break;
372         }
373     }
374
375 #ifdef WINNT
376   gh_enter (argc, argv, main_prog);
377 #else
378   gh_enter (argc, argv, (void(*)())main_prog);
379 #endif
380
381   return 0;                     // unreachable
382 }
383
384 /**
385   make input file name from command arg.
386
387   @input file name
388
389   @output file name with added default extension. "" is stdin.
390           in reference argument: the extension. ".ly" if none
391  */
392 String
393 distill_inname_str (String name_str, String& ext_r)
394 {
395   String str = name_str;
396   if (str.length_i ())
397     {
398       if (str != "-")
399         {
400           String a,b,c;
401           split_path (str,a,b,c,ext_r);
402
403           // add extension if not present.
404           char const* extensions[] = {"", "", ".ly", ".fly", ".sly", 0};
405           extensions[0] = ext_r.ch_C ();
406           for (int i = 0; extensions[i]; i++)
407             {
408               if (!global_path.find (a+b+c+extensions[i]).empty_b ())
409                 {
410                   ext_r = extensions[i];
411                   break;
412                 }
413             }
414           str = a+b+c+ext_r;
415         }
416     }
417   else 
418     {
419       str = "-";
420       ext_r = ".ly";
421     }
422   return str;
423 }
424