]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
e8e55b5bafb752f75c8d3f4ead661cc89058d1d6
[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 "plist.hh"
20 #include "getopt-long.hh"
21 #include "misc.hh"
22 #include "string.hh"
23 #include "main.hh"
24 #include "file-path.hh"
25 #include "config.hh"
26 #include "file-results.hh"
27 #include "debug.hh"
28 #include "lily-guile.hh"
29 #include "paper-def.hh"
30 #include "midi-def.hh"
31 #include "global-ctor.hh"
32
33 #if HAVE_GETTEXT
34 #include <libintl.h>
35 #endif
36
37
38 bool version_ignore_global_b = false;
39 bool no_paper_global_b = false;
40 bool no_timestamps_global_b = false;
41 bool find_old_relative_b = false;
42
43 char const* output_global_ch = "tex";
44 All_font_metrics *all_fonts_global_p;
45
46 String default_outname_base_global =  "lelie";
47 String outname_str_global;
48 String init_str_global;
49
50 int default_count_global;
51 File_path global_path;
52
53 bool safe_global_b = false;
54 bool experimental_features_global_b = false;
55 bool dependency_global_b = false;
56
57 int exit_status_i_;
58
59 Getopt_long * oparser_global_p = 0;
60
61 String distill_inname_str (String name_str, String& ext_r);
62
63 Long_option_init theopts[] = {
64   {0, "about", 'a'},
65   {1, "output", 'o'},
66   {0, "warranty", 'w'},
67   {0, "help", 'h'},
68   {0, "test", 't'},
69   {0, "debug", 'D'},
70   {1, "init", 'i'},
71   {1, "include", 'I'},
72   {0, "no-paper", 'M'},
73   {0, "dependencies", 'd'},
74   {0, "no-timestamps", 'T'},
75   {0, "find-old-relative", 'Q'},
76   {0, "ignore-version", 'V'},
77   {1, "output-format", 'f'},
78   {0, "safe", 's'},
79   {0,0,0}
80 };
81
82 void
83 usage ()
84 {
85   cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond") << '\n';
86   cout << _ ("Typeset music and or play MIDI from FILE or <stdin>");
87   cout << '\n';
88   cout << '\n';
89   cout << _ ("Options:");
90   cout << '\n';
91   cout  << _ (
92     "  -a, --about            about LilyPond\n"
93     );
94   cout  << _ (
95     "  -D, --debug            enable debugging output\n"
96     );
97   cout  << _ (
98     "  -d, --dependencies     write Makefile dependencies for every input file\n"
99     );
100   cout  << _ (
101     "  -h, --help             this help\n"
102     );
103   cout  << _ (
104     "  -f, --output-format=X  use output format X\n"
105     );
106   cout  << _ (
107     "  -I, --include=DIR      add DIR to search path\n"
108     );
109   cout  << _ (
110     "  -i, --init=FILE        use FILE as init file\n"
111     );
112   cout  << _ (
113     "  -M, --no-paper         produce midi output only\n"
114     );
115   cout  << _ (
116     "  -o, --output=FILE      set FILE as default output base\n"
117     );
118   cout  << _ (
119     "  -Q, --find-old-relative show all changes in relative syntax\n"
120     );
121   cout << _ (
122     "  -s, --safe             inhibit file output naming and exporting TeX  macros\n");
123   cout  << _ (
124     "  -t, --test             switch on experimental features\n"
125     );
126   cout  << _ (
127     "  -T, --no-timestamps    don't timestamp the output\n"
128     );
129   cout  << _ (
130     "  -V, --ignore-version   ignore mudela version\n"
131     );
132   cout  << _ (
133               "  -w, --warranty         show warranty and copyright\n"
134               );
135   cout << '\n';
136   cout << _ ("GNU LilyPond was compiled with the following settings:");
137   cout << '\n';
138   cout <<
139 #ifdef NDEBUG
140     "NDEBUG "
141 #endif
142 #ifdef NPRINT
143     "NPRINT "
144 #endif
145 #ifdef STRING_UTILS_INLINED
146     "STRING_UTILS_INLINED "
147 #endif
148     "datadir=" DIR_DATADIR
149     "\n"
150     "localedir=" DIR_LOCALEDIR
151
152     "\n";
153
154   ;
155 }
156
157 void
158 about ()
159 {
160   cout << '\n';
161   cout << 
162 #include "BLURB.hh"
163     cout << '\n';
164   cout << _ ("GNU LilyPond is Free software, see --warranty");
165   cout << '\n';
166   cout << '\n';
167   cout << _f ("Copyright (c) %s by", "1996, 1997, 1998");
168   cout << '\n';
169   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
170   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
171   cout << '\n';
172 }
173
174 void
175 notice ()
176 {
177   cout << '\n';
178   cout << _ ("GNU LilyPond -- The GNU Project music typesetter");
179   cout << '\n';
180   cout << _f ("Copyright (c) %s by", "1996, 1997, 1998");
181   cout << '\n';
182   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
183   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
184   cout << '\n';
185   cout << _ (
186              "    This program is free software; you can redistribute it and/or\n"
187              "modify it under the terms of the GNU General Public License version 2\n"
188              "as published by the Free Software Foundation.\n"
189              "\n"
190              "    This program is distributed in the hope that it will be useful,\n"
191              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
192              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
193              "General Public License for more details.\n"
194              "\n"
195              "    You should have received a copy (refer to the file COPYING) of the\n"
196              "GNU General Public License along with this program; if not, write to\n"
197              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
198              "USA.\n");
199 }
200
201 void
202 identify ()
203 {
204   *mlog << gnu_lilypond_version_str () << endl;
205 }
206
207 void
208 setup_paths ()
209 {
210   // facilitate binary distributions
211   char const *env_lily = getenv ("LILYPONDPREFIX");
212   String prefix_directory;
213   if (env_lily)
214     prefix_directory = env_lily;
215
216 #if HAVE_GETTEXT
217   setlocale (LC_ALL, ""); /* enable locales */
218   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
219   String lily_locale_dir;
220   String name (PACKAGE);
221   name.to_lower ();
222   if (!prefix_directory.empty_b())
223     {
224       lily_locale_dir = prefix_directory + "/share/locale";
225       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C());
226     }
227   else
228     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
229   textdomain (name.ch_C ());
230 #endif
231
232   global_path.add ("");
233   // must override (come before) "/usr/local/share/lilypond"!
234   char const *env_sz = getenv ("LILYINCLUDE");
235   if (env_sz)
236     global_path.parse_path (env_sz);
237
238
239   char *suffixes[] = {"ly", "afm", "scm", "tfm", 0};
240   for (char **s = suffixes; *s; s++){
241     if (!prefix_directory.empty_b())
242       global_path.add (prefix_directory + to_str ('/') + String (*s));
243     else
244       global_path.add (String (DIR_DATADIR) + to_str ('/') + String(*s));
245   }
246 }
247
248
249 void
250 main_prog (int argc, char **argv)
251 {
252   /*
253     need to do this first. Engravers use lily.scm contents.
254    */
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           // UGH. Should parametrise in list of default extensions.
405           if (ext_r.empty_b ())
406             {
407               ext_r = ".fly";
408               if (global_path.find (a+b+c+ext_r).empty_b ())
409                 ext_r = ".ly";
410             }
411           str = a+b+c+ext_r;
412         }
413     }
414   else 
415     {
416       str = "-";
417       ext_r = ".ly";
418     }
419   return str;
420 }
421