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