Debian Codegraph @

lib/common/Args.cpp

raw
1 #include "avm_args.h"
2 #include "plugin.h"
3 #include "configfile.h"
4 #include "aviplay.h"
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/utsname.h>
10 #include <ctype.h>
11
12
13 AVM_BEGIN_NAMESPACE;
14
15 static bool is_help(const char* str)
16 {
17 return (((toupper(str[0]) == 'H')
18 && (str[1] == 0 || (strcasecmp(str + 1, "ELP") == 0)))
19 || (str[0] == '?' && str[1] == 0));
20 }
21
22 static void split(avm::vector<avm::string>& arr, const char* str)
23 {
24 if (str)
25 {
26 //printf("SPLIT %s\n", str);
27 const char* b;
28 while ((b = strchr(str, ':')))
29 {
30 if (b > str)
31 arr.push_back(avm::string(str, (uint_t)(b - str)));
32 str = b + 1;
33 }
34 if (str[0])
35 arr.push_back(str);
36 for (unsigned i = 0; i < arr.size(); ++i)
37 printf("ARR %d %s\n", i, arr[i].c_str());
38 }
39 }
40
41 static void show_attrs(const CodecInfo& ci, const avm::vector<AttributeInfo>& attr, const char* title)
42 {
43 if (attr.size())
44 {
45 int val;
46 printf(" %s:\n", title);
47 for (unsigned i = 0; i < attr.size(); i++)
48 {
49 printf(" %20s", attr[i].GetName());
50 switch (attr[i].GetKind())
51 {
52 case AttributeInfo::Integer:
53 PluginGetAttrInt(ci, attr[i].GetName(), &val);
54 printf(" %d default: %d <%d, %d>", val,
55 attr[i].GetDefault(), attr[i].GetMin(), attr[i].GetMax());
56 break;
57 case AttributeInfo::Select:
58 PluginGetAttrInt(ci, attr[i].GetName(), &val);
59 printf(" %s default: %s <", attr[i].GetOptions()[val].c_str(),
60 attr[i].GetOptions()[attr[i].GetDefault()].c_str());
61 for (int j = attr[i].GetMin(); j < attr[i].GetMax(); j++)
62 {
63 if (j != attr[i].GetMin())
64 fputs(", ", stdout);
65 fputs(attr[i].GetOptions()[j].c_str(), stdout);
66 }
67 fputc('>', stdout);
68 break;
69 default:
70 ;
71 }
72 fputc('\n', stdout);
73 }
74 }
75 else
76 printf(" No %s\n", title);
77 }
78
79 static void set_codec_defaults(const CodecInfo& ci, const avm::vector<AttributeInfo>& attr)
80 {
81 for (unsigned i = 0; i < attr.size(); i++)
82 {
83 switch (attr[i].GetKind())
84 {
85 case AttributeInfo::Integer:
86 case AttributeInfo::Select:
87 PluginSetAttrInt(ci, attr[i].GetName(), attr[i].GetDefault());
88 default:
89 break;
90 }
91 }
92 }
93
94 static bool read_bool(const Args::Option& o, const char* arg, const char* par, const char* r)
95 {
96 bool b = true;
97 bool rs = false;
98
99 if (par)
100 {
101 if (!strcasecmp(par, "off") || !strcmp(par, "0")
102 || !strcasecmp(par, "false"))
103 {
104 b = false;
105 rs = true;
106 }
107 else if (!strcasecmp(par, "on") || !strcmp(par, "1")
108 || !strcasecmp(par, "true"))
109 {
110 rs = true;
111 }
112 }
113
114 if (o.is(Args::Option::REGBOOL))
115 RegWriteInt(r, o.getLongOption(), b ? 1 : 0);
116 else
117 o.setBool(b);
118 return rs;
119 }
120
121 static int read_double(const Args::Option& o, const char* arg, const char* par, const char* r)
122 {
123 if (!par)
124 {
125 printf("Option: %s - missing float value\n", arg);
126 return -1;
127 }
128 double d = atof(par);
129 if (!o.isInRange((int32_t)d))
130 {
131 printf("Option: %s - value: %f out of range <%d, %d>",
132 arg, d, o.getMin(), o.getMax());
133 return -1;
134 }
135 if (o.is(Args::Option::REGDOUBLE))
136 return RegWriteFloat(r, o.getLongOption(), (float)d);
137
138 return o.setDouble(d);
139 }
140
141 static int read_int(const Args::Option& o, const char* arg, const char* par, const char* r)
142 {
143 if (!par)
144 {
145 printf("Option: %s - missing integer value\n", arg);
146 return -1;
147 }
148 int v = 0;
149 sscanf(par, "%i", &v);
150 //printf("READINT %s %s %d\n", arg, par, v);
151 if (!o.isInRange(v))
152 {
153 printf("Option: %s - value: %d out of range <%d, %d>",
154 arg, v, o.getMin(), o.getMax());
155 return -1;
156 }
157 if (o.is(Args::Option::REGINT))
158 return RegWriteInt(r, o.getLongOption(), v);
159 return o.setInt(v);
160 }
161
162 static int read_string(const Args::Option& o, const char* arg, const char* par, const char* r)
163 {
164 if (!par)
165 {
166 printf("Option: %s - missing string value\n", arg);
167 return -1;
168 }
169 if (o.is(Args::Option::REGSTRING))
170 return RegWriteString(r, o.getLongOption(), par);
171
172 if (o.setString(strdup(par)))
173 return -1;
174
175 return 0;
176 }
177
178 static void show_help(const Args::Option* o, bool prefix)
179 {
180 size_t max = 0;
181 avm::vector<avm::string> l;
182 for (unsigned i = 0; !o[i].is(Args::Option::NONE); ++i)
183 {
184 avm::string s;
185 if (o[i].is(Args::Option::HELP))
186 s.sprintf(" -h --help");
187 else
188 s.sprintf(" %c%s %s%s",
189 (o[i].getShortOption()[0] && prefix) ? '-' : ' ',
190 o[i].getShortOption()[0] ? o[i].getShortOption() : " ",
191 (o[i].getLongOption()[0] && prefix) ? "--" : "",
192 o[i].getLongOption()[0] ? o[i].getLongOption() : ""
193 //opt[i].options ? opt[i].options : "",
194 //o[i].help ? o[i].help : ""
195 );
196 l.push_back(s);
197 size_t len = l.back().size();
198 if (max < len)
199 max = len;
200 }
201 for (unsigned i = 0; !o[i].is(Args::Option::NONE); ++i)
202 {
203 if (!o[i].getShortOption()[0] && !o[i].getLongOption()[0]
204 && !o[i].is(Args::Option::HELP)
205 && !o[i].is(Args::Option::OPTIONS))
206 continue;
207
208 if (!o[i].is(Args::Option::OPTIONS))
209 {
210 fputs(l[i].c_str(), stdout);
211 for (size_t s = l[i].size(); s <= max; s++)
212 fputc(' ', stdout);
213 if (o[i].is(Args::Option::HELP))
214 fputs("this help message", stdout);
215 }
216
217 if (o[i].getString())
218 {
219 switch (o[i].getType())
220 {
221 case Args::Option::INT:
222 case Args::Option::REGINT:
223 if (o[i].getHelp()[0])
224 printf(o[i].getHelp(), o[i].getInt(),
225 o[i].getMin(), o[i].getMax());
226 break;
227 case Args::Option::STRING:
228 case Args::Option::REGSTRING:
229 case Args::Option::SELECTSTRING:
230 if (o[i].getHelp()[0])
231 printf(o[i].getHelp(), o[i].getString());
232 break;
233 case Args::Option::OPTIONS:
234 show_help(o[i].getOptions(), prefix);
235 continue;
236 default:
237 if (o[i].getHelp()[0])
238 fputs(o[i].getHelp(), stdout);
239 break;
240 }
241 }
242 else if (o[i].getHelp()[0])
243 fputs(o[i].getHelp(), stdout);
244
245 fputs("\n", stdout);
246 }
247 }
248
249 static void parse_suboptions(const Args::Option* o, const char* oname, const char* pars, const char* r)
250 {
251 avm::vector<avm::string> arr;
252 split(arr, pars);
253
254 if (!arr.size() || is_help(arr[0].c_str()))
255 {
256 printf("Available options for '%s' (optA=x:optB=...)\n", oname);
257 show_help(o, false);
258 exit(0);
259 }
260 for (unsigned i = 0; i < arr.size(); ++i)
261 {
262 char* par = strchr(&arr[i][0], '=');
263 if (par)
264 {
265 *par = 0;
266 par++;
267 }
268
269 for (unsigned j = 0; !o[j].is(Args::Option::NONE); j++)
270 {
271 if ((arr[i] == o[j].getShortOption())
272 || (arr[i] == o[j].getLongOption()))
273 {
274 switch(o[j].getType())
275 {
276 case Args::Option::BOOL:
277 read_bool(o[j], arr[i].c_str(), par, r);
278 break;
279 case Args::Option::DOUBLE:
280 read_double(o[j], arr[i].c_str(), par, r);
281 break;
282 case Args::Option::INT:
283 read_int(o[j], arr[i].c_str(), par, r);
284 break;
285 case Args::Option::STRING:
286 read_string(o[j], arr[i].c_str(), par, r);
287 break;
288 default:
289 ;
290 }
291 }
292 }
293 }
294 }
295
296 static void parse_codec(const Args::Option* o, const char* a)
297 {
298 avm::vector<const CodecInfo*> ci;
299 CodecInfo::Get(ci, CodecInfo::Video, CodecInfo::Both);
300 CodecInfo::Get(ci, CodecInfo::Audio, CodecInfo::Both);
301
302 avm::vector<avm::string> arr;
303 split(arr, a);
304
305 if (!arr.size() || arr[0] == "help")
306 {
307 const char nm[][8] = { "audio", "video" };
308 const char nd[][12] = { "", "encoder", "decoder", "de/encoder" };
309 fputs("Available codecs:\n"
310 "Idx Short name Long name\n", stdout);
311 for (unsigned i = 0; i < ci.size(); i++)
312 printf("%3d %15s %s (%s %s)\n", i + 1, ci[i]->GetPrivateName(),
313 ci[i]->GetName(), nm[ci[i]->media], nd[ci[i]->direction]);
314 exit(0);
315 }
316
317 for (unsigned i = 0; i < ci.size(); ++i)
318 {
319 const char* cname = ci[i]->GetPrivateName();
320 if (arr[0] == cname)
321 {
322 if (arr[1] == "help")
323 {
324 printf(" Options for %s:\n", cname);
325 show_attrs(*ci[i], ci[i]->decoder_info, "Decoding Options");
326 show_attrs(*ci[i], ci[i]->encoder_info, "Encoding Options");
327 exit(0);
328 }
329 else if (arr[1] == "defaults")
330 {
331 set_codec_defaults(*ci[i], ci[i]->decoder_info);
332 set_codec_defaults(*ci[i], ci[i]->encoder_info);
333 }
334 else
335 {
336 for (unsigned j = 1; j < arr.size(); j++)
337 {
338 char* p = strchr(&arr[j][0], '=');
339 int val = 0;
340 bool valid = false;
341 if (p)
342 {
343 *p++ = 0;
344 if (sscanf(p, "%i", &val) > 0)
345 valid = true;
346 }
347 const AttributeInfo* ai = ci[i]->FindAttribute(arr[j].c_str());
348 if (ai)
349 {
350 switch (ai->GetKind())
351 {
352 case AttributeInfo::Integer:
353 if (!valid)
354 {
355 printf(" Option %s for %s needs integer value! (given: %s)\n",
356 arr[j].c_str(), cname, p);
357 exit(1);
358 }
359 printf("Setting %s = %d\n", arr[j].c_str(), val);
360 PluginSetAttrInt(*ci[i], arr[j].c_str(), val);
361 break;
362 case AttributeInfo::Select:
363 default:
364 ;
365 }
366 }
367 else
368 {
369 printf(" Unknown attribute name '%s' for '%s'\n",
370 arr[j].c_str(), cname);
371 exit(1);
372 }
373 }
374 }
375 if (o)
376 o->setString(strdup(cname)); // FIXME - use const ptr
377 break;
378 }
379 }
380 //for (unsigned i = 0; i < arr.size(); i++) printf("ARG %d %s\n", i, arr[i].c_str());
381 }
382
383 void Args::ParseCodecInfo(const char* str)
384 {
385 parse_codec(0, str);
386 }
387
388 Args::Args(const Option* _o, int* _argc, char** _argv,
389 const char* _help, const char* rname)
390 : opts(_o), argc(_argc), argv(_argv), help(_help), regname(rname)
391 {
392 int sidx = 1;
393
394 for (idx = 1; idx < *argc; ++idx)
395 {
396 if (argv[idx][0] == '-')
397 {
398 int olong = (argv[idx][1] == '-');
399 if (olong && argv[idx][2] == 0) {
400 while (++idx < *argc) /* copy after '--' */
401 argv[sidx++] = argv[idx];
402 break; // end of options
403 }
404 if (findOpt(olong) == 0)
405 continue;
406 }
407 else if (sidx != idx)
408 argv[sidx] = argv[idx];
409 sidx++;
410 }
411
412 *argc = sidx;
413 //for (idx = 1; idx < sidx; ++idx) printf("Left %d %s\n", idx, argv[idx]);
414 }
415
416 Args::~Args()
417 {
418 }
419
420 int Args::findOpt(int olong)
421 {
422 char* arg = argv[idx] + olong + 1;
423 char* par = strchr(arg, '=');
424 if (par)
425 *par++ = 0;
426 else if ((idx + 1) < *argc)
427 par = argv[++idx];
428
429 avm::vector<const Option*> ol;
430 ol.push_back(opts);
431 const Option* o = 0;
432 while (ol.size() && (!o || !o->is(Option::NONE)))
433 {
434 o = ol.front();
435 ol.pop_front();
436 for (; !o->is(Option::NONE); ++o)
437 {
438 //printf("OPTION %c '%s' %d '%s' '%s'\n", o->getType(), arg, olong, o->getShortOption(), o->getLongOption());
439 if (o->is(Option::OPTIONS))
440 {
441 ol.push_back(o->getOptions());
442 continue;
443 }
444 if (o->is(Option::HELP) && (is_help(arg)))
445 break;
446 const char* oarg = (olong) ? o->getLongOption() : o->getShortOption();
447 if (oarg && strcmp(arg, oarg) == 0)
448 break;
449 }
450 }
451
452 switch (o->getType())
453 {
454 default:
455 case Option::NONE:
456 return -1;
457 case Option::BOOL:
458 case Option::REGBOOL:
459 if (!read_bool(*o, arg, par, regname))
460 idx--; // no argument given
461 break;
462 case Option::DOUBLE:
463 case Option::REGDOUBLE:
464 read_double(*o, arg, par, regname);
465 break;
466 case Option::INT:
467 case Option::REGINT:
468 read_int(*o, arg, par, regname);
469 break;
470 case Option::STRING:
471 case Option::REGSTRING:
472 read_string(*o, arg, par, regname);
473 break;
474 case Option::CODEC:
475 parse_codec(o, par);
476 break;
477 case Option::SUBOPTIONS:
478 parse_suboptions(o->getOptions(), arg, par, regname);
479 break;
480 case Option::HELP:
481 printf("\nUsage: %s %s\n\n", argv[0], help);
482 show_help(opts, true);
483 exit(0);
484 }
485 return 0;
486 }
487
488 AVM_END_NAMESPACE;