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