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