1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1996--2021 Han-Wen Nienhuys, <hanwen@xs4all.nl>
5 
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "getopt-long.hh"
21 
22 #include <cstring>
23 #include <cassert>
24 #include <cstdlib>
25 
26 #include "config.hh"
27 #include "international.hh"
28 
29 #if !HAVE_GETTEXT
30 inline char *
gettext(char const * s)31 gettext (char const *s)
32 {
33   return const_cast<char *> (s);
34 }
35 #else
36 #include <libintl.h>
37 #endif
38 
39 using std::string;
40 
41 const Long_option_init *
parselong()42 Getopt_long::parselong ()
43 {
44   char const *optnm = arg_value_char_a_a_[array_index_] + 2;
45   assert (*optnm);
46 
47   char const *endopt = strchr (optnm, '=');
48   size_t searchlen = (endopt) ? (endopt - optnm) : strlen (optnm);
49 
50   found_option_ = 0;
51   for (int i = 0; i < table_len_; i++)
52     {
53       char const *ln = option_a_[i].longname_str0_;
54 
55       if (ln && !strncmp (ln, optnm, searchlen))
56         {
57           found_option_ = option_a_ + i;
58           break;
59         }
60     }
61 
62   if (!found_option_)
63     {
64       report (E_UNKNOWNOPTION);
65       return 0;
66     }
67   array_index_++;
68   argument_index_ = 0;
69 
70   if (found_option_->take_arg_str0_)
71     {
72       if (endopt)
73         optional_argument_str0_ = endopt + 1; // a '='
74       else
75         {
76           optional_argument_str0_ = arg_value_char_a_a_[array_index_];
77           array_index_++;
78         }
79       if (!optional_argument_str0_)
80         report (E_ARGEXPECT);
81     }
82   else
83     {
84       optional_argument_str0_ = 0;
85       if (endopt)
86         report (E_NOARGEXPECT);
87     }
88 
89   return found_option_;
90 }
91 
92 string
to_string() const93 Long_option_init::to_string () const
94 {
95   string str;
96   if (shortname_char_)
97     str += string ("-") + shortname_char_;
98   if (shortname_char_ && longname_str0_)
99     str += ", ";
100   if (longname_str0_)
101     str += string ("--") + longname_str0_;
102   return str;
103 }
104 
105 string
str_for_help() const106 Long_option_init::str_for_help () const
107 {
108   string s;
109   if (shortname_char_)
110     {
111       s += '-';
112       s += shortname_char_;
113     }
114   else
115     s = "  ";
116 
117   s = s + ((shortname_char_ && longname_str0_) ? ", " : "  ");
118 
119   if (longname_str0_)
120     s = s + "--" + longname_str0_;
121 
122   if (take_arg_str0_)
123     {
124       if (longname_str0_)
125         s = s + "=";
126       else
127         s = s + " ";
128 
129       s = s + gettext (take_arg_str0_);
130     }
131   return s;
132 }
133 
134 // report an error, GNU style.
135 void
report(Errorcod c)136 Getopt_long::report (Errorcod c)
137 {
138   string str = arg_value_char_a_a_[0];
139   str += ": ";
140   switch (c)
141     {
142     case E_ARGEXPECT:
143       str += _f ("option `%s' requires an argument",
144                  found_option_->to_string ());
145       break;
146     case E_NOARGEXPECT:
147       str += _f ("option `%s' does not allow an argument",
148                  found_option_->to_string ());
149       break;
150     case E_UNKNOWNOPTION:
151       str += _f ("unrecognized option: `%s'",
152                  argument_index_
153                  ? ("-" + string (1, arg_value_char_a_a_[array_index_][argument_index_]))
154                  : string (arg_value_char_a_a_[array_index_]));
155       break;
156     case E_ILLEGALARG:
157       str += _f ("invalid argument `%s' to option `%s'",
158                  optional_argument_str0_, found_option_->to_string ());
159       break;
160     default:
161       assert (false);
162     }
163   fprintf (stderr, "%s\n", str.c_str ());
164   exit (2);
165 }
166 
167 const Long_option_init *
parseshort()168 Getopt_long::parseshort ()
169 {
170   char c = arg_value_char_a_a_[array_index_][argument_index_];
171   found_option_ = 0;
172   assert (c);
173 
174   for (int i = 0; i < table_len_; i++)
175     if (option_a_[i].shortname_char_ == c)
176       {
177         found_option_ = option_a_ + i;
178         break;
179       }
180 
181   if (!found_option_)
182     {
183       report (E_UNKNOWNOPTION);
184       return 0;
185     }
186 
187   argument_index_++;
188   if (!found_option_->take_arg_str0_)
189     {
190       optional_argument_str0_ = 0;
191       return found_option_;
192     }
193   optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
194 
195   array_index_++;
196   argument_index_ = 0;
197 
198   if (!optional_argument_str0_[0])
199     {
200       optional_argument_str0_ = arg_value_char_a_a_[array_index_];
201       array_index_++;
202     }
203   if (!optional_argument_str0_)
204     report (E_ARGEXPECT);
205 
206   return found_option_;
207 }
208 
209 const Long_option_init *
operator ()()210 Getopt_long::operator () ()
211 {
212   if (!ok ())
213     return 0;
214 
215   next ();
216   if (!ok ())
217     return 0;
218 
219   if (argument_index_)
220     return parseshort ();
221 
222   char const *argument = arg_value_char_a_a_[array_index_];
223 
224   if (argument[0] != '-')
225     return 0;
226 
227   if (argument[1] == '-')  // what to do with "command  --  bla"
228     {
229       if (argument[2])
230         return parselong ();
231       else
232         return 0;
233     }
234   else
235     {
236       if (argument[ 1 ])
237         {
238           argument_index_ = 1;
239           return parseshort ();
240         }
241       else
242         return 0;
243     }
244 }
245 
Getopt_long(int c,char ** v,Long_option_init * lo)246 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
247 {
248   option_a_ = lo;
249   arg_value_char_a_a_ = v;
250   argument_count_ = c;
251   array_index_ = 1;
252   argument_index_ = 0;
253 
254   //    reached end of option table?
255   table_len_ = 0;
256   for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
257     table_len_++;
258 }
259 
260 bool
ok() const261 Getopt_long::ok () const
262 {
263   return array_index_ < argument_count_;
264 }
265 
266 void
next()267 Getopt_long::next ()
268 {
269   while (array_index_ < argument_count_
270          && !arg_value_char_a_a_[array_index_][argument_index_])
271     {
272       array_index_++;
273       argument_index_ = 0;
274     }
275 }
276 
277 char const *
current_arg()278 Getopt_long::current_arg ()
279 {
280   if (array_index_ >= argument_count_)
281     return 0;
282   char const *a = arg_value_char_a_a_[array_index_];
283   return a + argument_index_;
284 }
285 
286 char const *
get_next_arg()287 Getopt_long::get_next_arg ()
288 {
289   char const *a = current_arg ();
290   if (a)
291     {
292       array_index_++;
293       argument_index_ = 0;
294     }
295   return a;
296 }
297 
298 const int EXTRA_SPACES = 5;
299 
300 string
table_string(Long_option_init * l)301 Long_option_init::table_string (Long_option_init *l)
302 {
303   string tabstr = "";
304 
305   size_t wid = 0;
306   for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
307     wid = std::max (wid, l[i].str_for_help ().length ());
308 
309   for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
310     {
311       string s = "  " + l[i].str_for_help ();
312       s += string (wid - s.length () + EXTRA_SPACES, ' ');
313 
314       string help_text (gettext (l[i].help_str0_));
315       replace_all (&help_text, "\n",
316                    "\n" + string (wid + EXTRA_SPACES + 2, ' '));
317       tabstr += s + help_text + "\n";
318     }
319 
320   return tabstr;
321 }
322 
323 int
compare(Long_option_init const & a,Long_option_init const & b)324 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
325 {
326   if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_ - b.shortname_char_)
327     return a.shortname_char_ - b.shortname_char_;
328 
329   if (b.shortname_char_ && a.longname_str0_)
330     {
331       char s[2] = {b.shortname_char_, 0};
332       return strcmp (a.longname_str0_, s);
333     }
334   if (a.shortname_char_ && b.longname_str0_)
335     {
336       char s[2] = {a.shortname_char_, 0};
337       return strcmp (s, b.longname_str0_);
338     }
339 
340   return strcmp (a.longname_str0_, b.longname_str0_);
341 }
342