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