]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 0.1.57
[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 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include <stdlib.h>
10 #include <iostream.h>
11 #include <assert.h>
12 #include "proto.hh"
13 #include "plist.hh"
14 #include "lgetopt.hh"
15 #include "misc.hh"
16 #include "string.hh"
17 #include "main.hh"
18 #include "path.hh"
19 #include "config.hh"
20 #include "source.hh"
21 #include "debug.hh"
22 #include "my-lily-parser.hh"
23
24 static bool version_ignore_b = false;
25 Sources* source_global_l = 0;
26 bool no_paper_global_b = false;
27 bool no_timestamps_global_b = false;
28
29 bool embedded_mf_global_b = false;
30 bool experimental_features_global_b = false;
31 bool postscript_global_b = true;
32 int exit_status_i_;
33
34 void destill_inname (String &name_str_r);
35
36 Long_option_init theopts[] = {
37   {1, "output", 'o'},
38   {0, "warranty", 'w'},
39   {0, "help", 'h'},
40   {0, "test", 't'},
41   {0, "debug", 'd'},
42   {1, "init", 'i'},
43   {1, "include", 'I'},
44   {0, "no-paper", 'M'},
45   {0, "no-postscript", 'P'},
46   {0, "no-timestamps", 'T'},
47   {0, "ignore-version", 'V'},
48   {0,0,0}
49 };
50
51 void
52 usage ()
53 {
54   cout <<
55     _("Usage: lilypond [options] [mudela-files]\n"
56     "Typeset and or produce midi output from mudela-file or stdin\n"
57     "\n"
58     "Options:\n"
59     "  -d, --debug            enable debugging output\n"
60     "  -I, --include=DIR      add DIR to search path\n"
61     "  -i, --init=FILE        use FILE as init file\n"
62     "  -h, --help             this help\n"
63     "  -w, --warranty         show warranty and copyright\n"
64     "  -o, --output=FILE      set FILE as default output\n"
65     "  -t, --test             switch on experimental features\n"
66     "  -M, --no-paper         produce midi output only\n"
67     "  -V, --ignore-version   ignore mudela version\n"
68     "  -P, --no-postscript    don't use PostScript\n"
69     "  -T, --no-timestamps    don't timestamp the output\n"
70     "\n"
71     "GNU LilyPond was compiled with the following settings:\n")
72 #ifdef NDEBUG
73     "NDEBUG "
74 #endif
75 #ifdef NPRINT
76     "NPRINT "
77 #endif
78 #ifdef STRING_UTILS_INLINED
79     "STRING_UTILS_INLINED "
80 #endif
81         "datadir= " DIR_DATADIR
82
83     "\n";
84
85   ;
86
87
88 }
89
90 void
91 notice ()
92 {
93   cout <<
94     _("\n"
95     "GNU LilyPond -- The GNU Project music typesetter.\n"
96     "Copyright 1996, 97, 98 by\n"
97     "  Han-Wen Nienhuys <hanwen@stack.nl>\n"
98     "  Jan Nieuwenhuizen <jan@digicash.com>\n"
99     "\n"
100     "    This program is free software; you can redistribute it and/or\n"
101     "modify it under the terms of the GNU General Public License version 2\n"
102     "as published by the Free Software Foundation.\n"
103     "\n"
104     "    This program is distributed in the hope that it will be useful,\n"
105     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
106     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
107     "General Public License for more details.\n"
108     "\n"
109     "    You should have received a copy (refer to the file COPYING) of the\n"
110     "GNU General Public License along with this program; if not, write to\n"
111     "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
112     "USA.\n");
113 }
114
115
116 static File_path path;
117
118 void
119 do_one_file (String init_str, String file_str)
120 {
121   if (init_str.length_i () && path.find (init_str).empty_b ())
122     {
123       error (_("Can not find `") + init_str +"\'");
124       return ;
125     }
126   if (file_str.length_i () && path.find (file_str).empty_b ())
127     {
128       error (_("Can not find `") + file_str + "'");
129       return ;
130     }
131
132   Sources sources;
133   source_global_l = &sources;
134   source_global_l->set_path (&path);
135   {
136     My_lily_parser parser (source_global_l);
137     parser.set_version_check (version_ignore_b);
138     parser.parse_file (init_str, file_str);
139     
140     if (file_str.length_i () && file_str[0] != '-')
141       {
142         String a,b,c,d;
143         split_path (file_str, a, b, c, d);
144         default_outname_base_global = c;
145       }
146     else
147       default_outname_base_global = "lelie";
148   
149     if (parser.error_level_i_)
150       {
151         exit_status_i_  = 1;
152       }
153     else
154       do_scores ();
155     clear_scores ();
156   }
157   source_global_l = 0;
158 }
159
160 void
161 identify ()
162 {
163   cout << get_version_str () << endl;
164 }
165
166 int
167 main (int argc, char **argv)
168 {
169   identify ();
170   call_constructors ();
171   debug_init ();                // should be first
172
173
174   
175   path.add ("");
176   // must override (come before) "/usr/local/share/lilypond"!
177   char const *env_sz = getenv ("LILYINCLUDE");
178   if (env_sz)
179     path.parse_path (env_sz);
180
181   path.add (String (DIR_DATADIR) + "/init/");
182
183   path.push (DIR_DATADIR);
184
185   Getopt_long oparser (argc, argv,theopts);
186   String init_str ("lily-init.ly");
187
188   while (Long_option_init const * opt = oparser ())
189     {
190       switch (opt->shortname)
191         {
192         case 't':
193           experimental_features_global_b = true;
194           break;
195         case 'o':
196           default_outname_base_global = oparser.optional_argument_ch_C_;
197           break;
198         case 'w':
199           notice ();
200           exit (0);
201           break;
202         case 'I':
203           path.push (oparser.optional_argument_ch_C_);
204           break;
205         case 'i':
206           init_str = oparser.optional_argument_ch_C_;
207           break;
208         case 'h':
209           usage ();
210           exit (0);
211           break;
212         case 'V':
213           version_ignore_b = true;
214           break;
215         case 'P':
216           postscript_global_b = false;
217           break;
218         case 'd':
219           set_debug (true);
220           break;
221         case 'M':
222           no_paper_global_b = true;
223           break;
224         case 'T':
225           no_timestamps_global_b = true;
226           break;
227         default:
228           assert (false);
229           break;
230         }
231     }
232
233   int p=0;
234   const char *arg ;
235   while ((arg= oparser.get_next_arg ()))
236     {
237       String f (arg);
238       destill_inname (f);
239       do_one_file (init_str,f);
240       p++;
241     }
242   if (!p)
243     {
244       do_one_file (init_str, "");
245     }
246
247   return exit_status_i_;
248 }
249
250 /// make input file name: add default extension. "" is stdin.
251 void
252 destill_inname (String &name_str_r)
253 {
254   if (name_str_r.length_i ())
255     {
256       if (name_str_r[ 0 ] != '-')
257         {
258           String a,b,c,d;
259           split_path (name_str_r,a,b,c,d);
260
261           // add extension if not present.
262           if (d.empty_b ())
263             d = ".ly";
264           name_str_r = a+b+c+d;
265         }
266     }
267   else name_str_r = "";
268 }