lilypond-1.5.37
[lilypond.git] / flower / lgetopt.cc
blob5aabe5f6825109656d89f3464a03a74205e8a5c5
1 /*
2 process command line, GNU style.
4 this is (Copyleft) 1996, Han-Wen Nienhuys, <hanwen@stack.nl>
5 */
7 #include <stdio.h>
8 #include <iostream.h>
9 #include <assert.h>
10 #include "lgetopt.hh"
11 #include "international.hh"
13 long
14 Getopt_long::argument_to_i()
16 long l;
17 if (!optional_argument_ch_C_
18 || sscanf (optional_argument_ch_C_, "%ld", &l) != 1)
19 report (E_ILLEGALARG);
21 return l;
24 const Long_option_init *
25 Getopt_long::parselong()
27 char const *optnm = arg_value_ch_a_a_[array_index_i_] + 2 ;
28 assert (*optnm);
30 char const *endopt = strchr (optnm, '=');
31 int searchlen = (endopt) ? endopt - optnm : strlen (optnm);
33 found_option_l_=0;
34 for (int i=0; i< table_len_i_; i++)
36 char const *ln = option_a_[i].longname;
38 if (ln && !strncmp (ln, optnm, searchlen))
40 found_option_l_ = option_a_+i;
41 break;
45 if (!found_option_l_)
47 report (E_UNKNOWNOPTION);
48 return 0;
50 array_index_i_++;
51 argument_index_i_ = 0;
54 if (found_option_l_->take_arg)
56 if (endopt)
57 optional_argument_ch_C_ = endopt +1; // a '='
58 else
60 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
61 array_index_i_++;
63 if (!optional_argument_ch_C_)
64 report (E_ARGEXPECT);
67 else
69 optional_argument_ch_C_ = 0;
70 if (endopt)
71 report (E_NOARGEXPECT);
74 return found_option_l_;
78 void
79 Long_option_init::printon (ostream &errorout) const
81 if (shortname)
82 errorout <<"-" << shortname;
83 if (shortname && longname)
84 errorout << ", ";
85 if (longname)
86 errorout << "`--" << longname << "'";
89 // report an error, GNU style.
90 void
91 Getopt_long::report (Errorcod c)
93 error_ = c;
94 if (!error_ostream_l_)
95 return;
97 *error_ostream_l_ << arg_value_ch_a_a_[0] << ": ";
98 switch (c)
100 case E_ARGEXPECT:
101 *error_ostream_l_<< _("option ");
102 found_option_l_->printon (*error_ostream_l_);
103 *error_ostream_l_ << _("requires an argument")<<endl;
104 break;
105 case E_NOARGEXPECT:
106 *error_ostream_l_ << _("option `--") <<
107 found_option_l_->longname << _("' does not allow an argument")<<endl;
108 break;
110 case E_UNKNOWNOPTION:
111 *error_ostream_l_ << _("unrecognized option ");
112 if (argument_index_i_)
113 *error_ostream_l_ << "-" << arg_value_ch_a_a_[array_index_i_][argument_index_i_] << endl;
114 else
115 *error_ostream_l_ << arg_value_ch_a_a_[array_index_i_] << endl;
117 break;
118 case E_ILLEGALARG:
119 *error_ostream_l_ << _("illegal argument `") << optional_argument_ch_C_ << _("\'to option ");
120 found_option_l_->printon (*error_ostream_l_);
121 *error_ostream_l_ << '\n';
122 default:
123 assert (false);
125 exit (2);
128 const Long_option_init *
129 Getopt_long::parseshort()
131 char c=arg_value_ch_a_a_[array_index_i_][argument_index_i_];
132 found_option_l_=0;
133 assert (c);
135 for (int i=0; i < table_len_i_; i++)
136 if (option_a_[i].shortname == c)
138 found_option_l_ = option_a_+i;
139 break;
142 if (!found_option_l_)
144 report (E_UNKNOWNOPTION);
145 return 0;
148 argument_index_i_++;
149 if (!found_option_l_->take_arg)
151 optional_argument_ch_C_ = 0;
152 return found_option_l_;
154 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_] + argument_index_i_;
156 array_index_i_ ++;
157 argument_index_i_ = 0;
159 if (!optional_argument_ch_C_[0])
161 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
162 array_index_i_ ++;
164 if (!optional_argument_ch_C_)
166 report (E_ARGEXPECT);
169 return found_option_l_;
172 const Long_option_init *
173 Getopt_long::operator()()
175 if (!ok())
176 return 0;
178 next();
179 if (!ok ())
180 return 0;
182 if (argument_index_i_)
183 return parseshort();
185 const char * argument_C = arg_value_ch_a_a_[array_index_i_];
187 if (argument_C[0] != '-')
188 return 0;
190 if (argument_C[1] == '-') {// what to do with "command -- bla"
191 if (argument_C[2])
192 return parselong();
193 else
194 return 0;
196 else
198 if (argument_C[ 1 ])
200 argument_index_i_ = 1;
201 return parseshort();
203 else
205 return 0;
210 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
212 option_a_ = lo;
213 error_ostream_l_ = &cerr;
214 arg_value_ch_a_a_ = v;
215 argument_count_i_ = c;
216 array_index_i_ = 1;
217 argument_index_i_ = 0;
219 // reached end of option table?
220 table_len_i_ =0;
221 for (int i = 0; option_a_[i].longname ||option_a_[i].shortname; i++)
222 table_len_i_ ++;
225 bool
226 Getopt_long::ok() const
228 return array_index_i_ < argument_count_i_;
231 void
232 Getopt_long::next()
234 error_ = E_NOERROR;
235 while (array_index_i_ < argument_count_i_
236 && !arg_value_ch_a_a_[array_index_i_][argument_index_i_])
238 array_index_i_++;
239 argument_index_i_ = 0;
243 char const *
244 Getopt_long::current_arg()
246 if (array_index_i_ >= argument_count_i_)
247 return 0;
248 char const * a = arg_value_ch_a_a_[array_index_i_];
249 return a + argument_index_i_;
252 char const *
253 Getopt_long::get_next_arg()
255 char const * a = current_arg();
256 if (a)
258 array_index_i_ ++;
259 argument_index_i_= 0;
261 return a;