File src/pms.cpp changed (mode: 100644) (index 1b407af..645ba29) |
... |
... |
int main(int argc, char *argv[]) |
45 |
45 |
int exitcode; |
int exitcode; |
46 |
46 |
|
|
47 |
47 |
pms = new Pms(argc, argv); |
pms = new Pms(argc, argv); |
48 |
|
if (!pms) |
|
49 |
|
{ |
|
|
48 |
|
|
|
49 |
|
if (!pms) { |
50 |
50 |
printf("Not enough memory, aborting.\n"); |
printf("Not enough memory, aborting.\n"); |
51 |
51 |
return PMS_EXIT_LOMEM; |
return PMS_EXIT_LOMEM; |
52 |
52 |
} |
} |
53 |
53 |
|
|
54 |
54 |
exitcode = pms->init(); |
exitcode = pms->init(); |
55 |
|
if (exitcode == 0) |
|
56 |
|
{ |
|
|
55 |
|
|
|
56 |
|
if (exitcode == -1) { |
|
57 |
|
return PMS_EXIT_SUCCESS; |
|
58 |
|
} else if (exitcode == 0) { |
57 |
59 |
exitcode = pms->main(); |
exitcode = pms->main(); |
58 |
60 |
} |
} |
|
61 |
|
|
59 |
62 |
delete pms; |
delete pms; |
60 |
63 |
return exitcode; |
return exitcode; |
61 |
64 |
} |
} |
|
... |
... |
int Pms::init() |
549 |
552 |
string str; |
string str; |
550 |
553 |
vector<string> * tok; |
vector<string> * tok; |
551 |
554 |
|
|
552 |
|
int exitcode = PMS_EXIT_SUCCESS; |
|
|
555 |
|
int exitcode; |
553 |
556 |
char * host; |
char * host; |
554 |
557 |
char * port; |
char * port; |
555 |
558 |
char * password; |
char * password; |
|
... |
... |
int Pms::init() |
635 |
638 |
} |
} |
636 |
639 |
|
|
637 |
640 |
/* Parse command-line */ |
/* Parse command-line */ |
638 |
|
if (parse_args(argc, argv) == false) |
|
639 |
|
{ |
|
640 |
|
return PMS_EXIT_BADARGS; |
|
|
641 |
|
if ((exitcode = parse_args(argc, argv)) != PMS_EXIT_SUCCESS) { |
|
642 |
|
return exitcode; |
641 |
643 |
} |
} |
642 |
644 |
|
|
643 |
|
if (!config->loadconfigs()) |
|
|
645 |
|
if (!config->loadconfigs()) { |
644 |
646 |
return PMS_EXIT_CONFIGERR; |
return PMS_EXIT_CONFIGERR; |
|
647 |
|
} |
645 |
648 |
|
|
646 |
649 |
/* Seed random number generator */ |
/* Seed random number generator */ |
647 |
650 |
srand(time(NULL)); |
srand(time(NULL)); |
|
... |
... |
Pms::print_version() |
1408 |
1411 |
/* |
/* |
1409 |
1412 |
* Print switch usage |
* Print switch usage |
1410 |
1413 |
*/ |
*/ |
1411 |
|
void Pms::print_usage() |
|
|
1414 |
|
void |
|
1415 |
|
Pms::print_usage() |
1412 |
1416 |
{ |
{ |
1413 |
1417 |
printf("Usage:\n"); |
printf("Usage:\n"); |
1414 |
1418 |
printf(" -%s\t\t\t%s\n", "v", "print version and exit"); |
printf(" -%s\t\t\t%s\n", "v", "print version and exit"); |
1415 |
|
printf(" -%s\t\t%s\n", "? --help", "display command-line options"); |
|
1416 |
|
printf(" -%s\t\t\t%s\n", "d", "turn on debugging to stderr"); |
|
|
1419 |
|
printf(" -%s\t\t\t%s\n", "h", "display this help screen and exit"); |
|
1420 |
|
printf(" -%s\t\t\t%s\n", "d", "turn on debugging to standard error"); |
1417 |
1421 |
printf(" -%s\t\t%s\n", "c <filename>", "use an alternative config file"); |
printf(" -%s\t\t%s\n", "c <filename>", "use an alternative config file"); |
1418 |
|
printf(" -%s\t\t%s\n", "h <host>", "connect to this MPD server"); |
|
|
1422 |
|
printf(" -%s\t\t%s\n", "H <host>", "connect to this MPD server"); |
1419 |
1423 |
printf(" -%s\t\t%s\n", "p <port>", "connect to this port"); |
printf(" -%s\t\t%s\n", "p <port>", "connect to this port"); |
1420 |
1424 |
printf(" -%s\t\t%s\n", "P <password>", "give this password to MPD server"); |
printf(" -%s\t\t%s\n", "P <password>", "give this password to MPD server"); |
1421 |
1425 |
} |
} |
1422 |
1426 |
|
|
1423 |
|
/* |
|
1424 |
|
* Helper function, prints an error |
|
1425 |
|
*/ |
|
1426 |
|
bool Pms::require_arg(char c) |
|
1427 |
|
{ |
|
1428 |
|
printf("Error: option '%c' requires an argument.\n", c); |
|
1429 |
|
print_usage(); |
|
1430 |
|
return false; |
|
1431 |
|
} |
|
1432 |
|
|
|
1433 |
1427 |
/* |
/* |
1434 |
1428 |
* Parse command-line arguments |
* Parse command-line arguments |
1435 |
1429 |
*/ |
*/ |
1436 |
|
bool Pms::parse_args(int argc, char * argv[]) |
|
|
1430 |
|
int |
|
1431 |
|
Pms::parse_args(int argc, char ** argv) |
1437 |
1432 |
{ |
{ |
1438 |
|
int argn; |
|
1439 |
|
string value = ""; |
|
1440 |
|
string arg = ""; |
|
1441 |
|
bool switched = false; |
|
1442 |
|
string s; |
|
1443 |
|
string::iterator i; |
|
1444 |
|
|
|
1445 |
|
if (argc <= 1) |
|
1446 |
|
return true; |
|
1447 |
|
|
|
1448 |
|
for (argn = 1; argn < argc; argn++) |
|
1449 |
|
{ |
|
1450 |
|
s = argv[argn]; |
|
1451 |
|
|
|
1452 |
|
if (s == "--help") |
|
1453 |
|
{ |
|
1454 |
|
print_usage(); |
|
1455 |
|
return false; |
|
1456 |
|
} |
|
1457 |
|
|
|
1458 |
|
i = s.begin(); |
|
1459 |
|
|
|
1460 |
|
while (i != s.end()) |
|
1461 |
|
{ |
|
1462 |
|
if (!switched) |
|
1463 |
|
if (*i != '-') |
|
1464 |
|
return false; |
|
1465 |
|
|
|
1466 |
|
switch (*i) |
|
1467 |
|
{ |
|
1468 |
|
case 'd': |
|
1469 |
|
options->set_bool("debug", true); |
|
1470 |
|
break; |
|
1471 |
|
case 'v': |
|
1472 |
|
print_version(); |
|
1473 |
|
return false; |
|
1474 |
|
case '?': |
|
1475 |
|
print_usage(); |
|
1476 |
|
return false; |
|
1477 |
|
case 'c': |
|
1478 |
|
if (++argn >= argc) |
|
1479 |
|
return require_arg(*i); |
|
1480 |
|
options->set_string("configfile", argv[argn]); |
|
1481 |
|
break; |
|
1482 |
|
case 'h': |
|
1483 |
|
if (++argn >= argc) |
|
1484 |
|
return require_arg(*i); |
|
1485 |
|
options->set_string("host", argv[argn]); |
|
1486 |
|
break; |
|
1487 |
|
case 'p': |
|
1488 |
|
if (++argn >= argc) |
|
1489 |
|
return require_arg(*i); |
|
1490 |
|
options->set_long("port", atoi(argv[argn])); |
|
1491 |
|
if (options->get_long("port") <= 0 || options->get_long("port") > 65535) |
|
1492 |
|
{ |
|
1493 |
|
printf(_("Error: port number must be from 1-65535\n")); |
|
1494 |
|
return false; |
|
1495 |
|
} |
|
1496 |
|
break; |
|
1497 |
|
case 'P': |
|
1498 |
|
if (++argn >= argc) |
|
1499 |
|
return require_arg(*i); |
|
1500 |
|
options->set_string("password", argv[argn]); |
|
1501 |
|
break; |
|
1502 |
|
case '-': |
|
1503 |
|
if (switched) |
|
1504 |
|
{ |
|
1505 |
|
print_usage(); |
|
1506 |
|
return false; |
|
1507 |
|
} |
|
1508 |
|
switched = true; |
|
1509 |
|
break; |
|
1510 |
|
default: |
|
1511 |
|
printf(_("Error: unknown option '%c'\n"), *i); |
|
1512 |
|
print_usage(); |
|
1513 |
|
return false; |
|
1514 |
|
} |
|
1515 |
|
++i; |
|
|
1433 |
|
int c; |
|
1434 |
|
|
|
1435 |
|
while ((c = getopt(argc, argv, "hdvc:H:p:P:")) != -1) { |
|
1436 |
|
switch(c) { |
|
1437 |
|
case 'd': |
|
1438 |
|
options->set_bool("debug", true); |
|
1439 |
|
break; |
|
1440 |
|
case 'v': |
|
1441 |
|
print_version(); |
|
1442 |
|
return -1; |
|
1443 |
|
case 'h': |
|
1444 |
|
print_usage(); |
|
1445 |
|
return -1; |
|
1446 |
|
case 'c': |
|
1447 |
|
options->set_string("configfile", optarg); |
|
1448 |
|
break; |
|
1449 |
|
case 'H': |
|
1450 |
|
options->set_string("host", optarg); |
|
1451 |
|
break; |
|
1452 |
|
case 'p': |
|
1453 |
|
options->set_long("port", atoi(optarg)); |
|
1454 |
|
if (options->get_long("port") <= 0 || options->get_long("port") > 65535) { |
|
1455 |
|
printf(_("Error: port number must be from 1-65535\n")); |
|
1456 |
|
return PMS_EXIT_BADARGS; |
|
1457 |
|
} |
|
1458 |
|
break; |
|
1459 |
|
case 'P': |
|
1460 |
|
options->set_string("password", optarg); |
|
1461 |
|
break; |
|
1462 |
|
case '?': |
|
1463 |
|
if (optopt == 'c' || optopt == 'H' || optopt == 'p' || optopt == 'P') { |
|
1464 |
|
printf(_("Error: option -%c requires an argument.\n"), optopt); |
|
1465 |
|
} else { |
|
1466 |
|
printf(_("Error: unknown option -%c.\n"), c); |
|
1467 |
|
} |
|
1468 |
|
print_usage(); |
|
1469 |
|
return PMS_EXIT_BADARGS; |
|
1470 |
|
default: |
|
1471 |
|
abort(); |
1516 |
1472 |
} |
} |
1517 |
|
|
|
1518 |
|
switched = false; |
|
1519 |
1473 |
} |
} |
1520 |
1474 |
|
|
1521 |
|
return true; |
|
|
1475 |
|
return PMS_EXIT_SUCCESS; |
1522 |
1476 |
} |
} |
1523 |
|
|
|
File src/pms.h changed (mode: 100644) (index 97d476b..e272cf2) |
1 |
1 |
/* vi:set ts=8 sts=8 sw=8 noet: |
/* vi:set ts=8 sts=8 sw=8 noet: |
2 |
2 |
* |
* |
3 |
3 |
* PMS <<Practical Music Search>> |
* PMS <<Practical Music Search>> |
4 |
|
* Copyright (C) 2006-2010 Kim Tore Jensen |
|
|
4 |
|
* Copyright (C) 2006-2015 Kim Tore Jensen |
5 |
5 |
* |
* |
6 |
6 |
* This program is free software: you can redistribute it and/or modify |
* This program is free software: you can redistribute it and/or modify |
7 |
7 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
|
25 |
25 |
#define _PMS_H_ |
#define _PMS_H_ |
26 |
26 |
|
|
27 |
27 |
#define PMS_NAME "Practical Music Search" |
#define PMS_NAME "Practical Music Search" |
28 |
|
#define PMS_COPYRIGHT "(c) 2006-2010 by Kim Tore Jensen <kimtjen@gmail.com>" |
|
|
28 |
|
#define PMS_COPYRIGHT "(c) 2006-2015 Kim Tore Jensen <kimtjen@gmail.com>" |
29 |
29 |
|
|
30 |
30 |
#define PMS_EXIT_SUCCESS 0 |
#define PMS_EXIT_SUCCESS 0 |
31 |
31 |
#define PMS_EXIT_NODISPLAY 1 |
#define PMS_EXIT_NODISPLAY 1 |
|
... |
... |
private: |
110 |
110 |
/* Options/arguments */ |
/* Options/arguments */ |
111 |
111 |
void print_version(); |
void print_version(); |
112 |
112 |
void print_usage(); |
void print_usage(); |
113 |
|
bool require_arg(char); |
|
114 |
|
bool parse_args(int, char **); |
|
|
113 |
|
int parse_args(int, char **); |
115 |
114 |
|
|
116 |
115 |
public: |
public: |
117 |
116 |
|
|