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