]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/main.cc
release: 0.0.45
[lilypond.git] / mi2mu / main.cc
1 //
2 // main.cc -- implement silly main() entry point
3 // should have Root class.
4 //
5 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
6
7 #include "mi2mu.hh"
8
9 Sources source;
10 Sources* source_l_g = &source;
11
12 Verbose level_ver = NORMAL_ver;
13
14 //ugh
15 char const* defined_ch_C = 0;
16
17 // ugh, another global
18 String
19 find_file( String str )
20 {
21     return str;
22 }
23
24 // ugh, copied from warn.cc, cannot use
25 void
26 message( String message_str, char const* context_ch_C )
27 {
28     String str = "mi2mu: ";
29     Source_file* sourcefile_l = source_l_g->sourcefile_l( context_ch_C );
30     if ( sourcefile_l ) {
31         str += sourcefile_l->file_line_no_str(context_ch_C) + String(": ");
32     }
33     str += message_str;
34     if ( sourcefile_l ) {
35         str += ":\n";
36         str += sourcefile_l->error_str( context_ch_C );
37     }
38 //    if ( busy_parsing() )
39     cerr << endl; // until we have fine output manager...
40     cerr << str << endl;
41 }
42
43 void
44 warning( String message_str, char const* context_ch_C )
45 {
46     message( "warning: " + message_str, context_ch_C );
47 }
48
49 void
50 error( String message_str, char const* context_ch_C )
51 {
52     message( message_str, context_ch_C );
53     // since when exits error again?
54     // i-d say: error: errorlevel |= 1; -> no output upon error
55     //          warning: recovery -> output (possibly wrong)
56     if ( midi_lexer_l_g )
57         midi_lexer_l_g->errorlevel_i_ |= 1;
58 }
59
60 void
61 usage()
62 {
63     tor( NORMAL_ver ) <<
64         "Usage: mi2mu [options] midi-file\n"
65         "Translate midi-file to mudela\n"
66         "\n"
67         "Options:\n"
68         "  -b, --no-quantify      write exact durations, e.g.: a4*385/384\n"
69         "  -d, --debug            print lots of debugging stuff\n"
70         "  -h, --help             this help\n"
71         "  -I, --include=DIR      add DIR to search path\n"
72         "  -n, --no-silly         assume no plets or double dots, smallest is 16\n"
73         "  -o, --output=FILE      set FILE as default output\n"
74         "  -p, --no-plets         assume no plets\n"
75         "  -q, --quiet            be quiet\n"
76         "  -s, --smallest=N       assume no shorter (reciprocal) durations than N\n"
77         "  -v, --verbose          be verbose\n"
78         "  -w, --warranty         show warranty and copyright\n"
79         "  -x, --no-double-dots   assume no double dotted notes\n"
80         ;
81 }
82
83 void
84 identify()
85 {
86         tor( NORMAL_ver ) << mi2mu_version_str() << endl;
87 }
88     
89 void 
90 notice()
91 {
92     tor( NORMAL_ver ) <<
93         "\n"
94         "Mi2mu, translate midi to mudela.\n"
95         "Copyright (C) 1997 by\n"
96         "  Han-Wen Nienhuys <hanwen@stack.nl>\n"
97 //      "Contributors\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 int
116 main( int argc_i, char* argv_sz_a[] )
117 {
118         Long_option_init long_option_init_a[] = {
119                 0, "be-blonde", 'b',
120                 0, "debug", 'd',
121                 0, "help", 'h',
122                 0, "no-silly", 'n',
123                 1, "output", 'o',
124                 1, "no-plets", 'p',
125                 0, "quiet", 'q',
126                 1, "smallest", 's',
127                 0, "verbose", 'v',
128                 0, "warranty", 'w',
129                 0, "no-double-dots", 'x',
130                 0,0,0
131         };
132         Getopt_long getopt_long( argc_i, argv_sz_a, long_option_init_a );
133         identify();
134
135         String output_str;
136         while ( Long_option_init* long_option_init_p = getopt_long() )
137                 switch ( long_option_init_p->shortname ) {
138                 case 'b':
139                         Duration_convert::no_quantify_b_s = true;
140                         break;
141                 case 'd':
142                         level_ver = DEBUG_ver;
143                         break;
144                 case 'h':
145                         usage();
146                         exit( 0 );
147                         break;
148 //              case 'I':
149 //                      path->push( getopt_long.optarg );
150 //                      break;
151                 case 'n':
152                         Duration_convert::no_double_dots_b_s = true;
153                         Duration_convert::no_triplets_b_s = true;
154                         Duration_convert::no_smaller_than_i_s = 16;
155                         break;
156                 case 'o':
157                         output_str = getopt_long.optarg;
158                         break;
159                 case 'p':
160                         Duration_convert::no_triplets_b_s = true;
161                         break;
162                 case 'q':
163                         level_ver = QUIET_ver;
164                         break;
165                 case 's': {
166                                 int i = String_convert::dec2_i( getopt_long.optarg );
167                                 if ( !i ) {
168                                         usage();
169                                         exit( 2 ); //usage
170                                 }
171                                 Duration_convert::no_smaller_than_i_s = i;
172                         }
173                         break;
174                 case 'v':
175                         level_ver = VERBOSE_ver;
176                         break;
177                 case 'w':
178                         notice();
179                         exit( 0 );
180                         break;
181                 case 'x':
182                         Duration_convert::no_double_dots_b_s = false;
183                         break;
184                 default:
185                         assert( 0 );
186                         break;
187                 }
188   
189         char* arg_sz = 0;
190         while ( ( arg_sz = getopt_long.get_next_arg() ) ) {
191                 My_midi_parser midi_parser( arg_sz, & source );
192                 midi_parser_l_g = &midi_parser;
193
194                 int error_i = midi_parser.parse();
195                 if ( error_i )
196                         return error_i;
197                 if ( !output_str.length_i() ) {
198                     String d, dir, base, ext;
199
200                     split_path(arg_sz, d, dir, base, ext);
201                     
202                     output_str = base + ext + ".ly";
203                 }
204                 error_i = midi_parser.output_mudela( output_str );
205                 if ( error_i )
206                         return error_i;
207                 midi_parser_l_g = 0;
208         }
209         return 0;
210 }