pmc: add long options and configuration file

[ RC: - Add new flag into usage message.
      - Fix up coding style of the automatic variables. ]

Signed-off-by: Peter Schneider <peter@psch.de>
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Peter Schneider 2018-03-22 00:28:33 +01:00 committed by Richard Cochran
parent ea9dda30e2
commit c96dea72f1
1 changed files with 54 additions and 10 deletions

64
pmc.c
View File

@ -665,6 +665,7 @@ static void usage(char *progname)
" Other Options\n\n" " Other Options\n\n"
" -b [num] boundary hops, default 1\n" " -b [num] boundary hops, default 1\n"
" -d [num] domain number, default 0\n" " -d [num] domain number, default 0\n"
" -f [file] read configuration from 'file'\n"
" -h prints this message and exits\n" " -h prints this message and exits\n"
" -i [dev] interface device to use, default 'eth0'\n" " -i [dev] interface device to use, default 'eth0'\n"
" for network and '/var/run/pmc.$pid' for UDS.\n" " for network and '/var/run/pmc.$pid' for UDS.\n"
@ -679,13 +680,14 @@ static void usage(char *progname)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
const char *iface_name = NULL; const char *iface_name = NULL;
char *progname; char *config = NULL, *progname;
int c, cnt, length, tmo = -1, batch_mode = 0, zero_datalen = 0; int c, cnt, index, length, tmo = -1, batch_mode = 0, zero_datalen = 0;
int ret = 0; int ret = 0;
char line[1024], *command = NULL, uds_local[MAX_IFNAME_SIZE + 1]; char line[1024], *command = NULL, uds_local[MAX_IFNAME_SIZE + 1];
enum transport_type transport_type = TRANS_UDP_IPV4; enum transport_type transport_type = TRANS_UDP_IPV4;
UInteger8 boundary_hops = 1, domain_number = 0, transport_specific = 0; UInteger8 boundary_hops = 1, domain_number = 0, transport_specific = 0;
struct ptp_message *msg; struct ptp_message *msg;
struct option *opts;
struct config *cfg; struct config *cfg;
#define N_FD 2 #define N_FD 2
struct pollfd pollfd[N_FD]; struct pollfd pollfd[N_FD];
@ -697,28 +699,55 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
opts = config_long_options(cfg);
/* Process the command line arguments. */ /* Process the command line arguments. */
progname = strrchr(argv[0], '/'); progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0]; progname = progname ? 1+progname : argv[0];
while (EOF != (c = getopt(argc, argv, "246u""b:d:hi:s:t:vz"))) { while (EOF != (c = getopt_long(argc, argv, "246u""b:d:f:hi:s:t:vz",
opts, &index))) {
switch (c) { switch (c) {
case 0:
if (config_parse_option(cfg, opts[index].name, optarg)) {
ret = -1;
goto out;
}
break;
case '2': case '2':
transport_type = TRANS_IEEE_802_3; if (config_set_int(cfg, "network_transport", TRANS_IEEE_802_3)) {
ret = -1;
goto out;
}
break; break;
case '4': case '4':
transport_type = TRANS_UDP_IPV4; if (config_set_int(cfg, "network_transport", TRANS_UDP_IPV4)) {
ret = -1;
goto out;
}
break; break;
case '6': case '6':
transport_type = TRANS_UDP_IPV6; if (config_set_int(cfg, "network_transport", TRANS_UDP_IPV6)) {
ret = -1;
goto out;
}
break; break;
case 'u': case 'u':
transport_type = TRANS_UDS; if (config_set_int(cfg, "network_transport", TRANS_UDS)) {
ret = -1;
goto out;
}
break; break;
case 'b': case 'b':
boundary_hops = atoi(optarg); boundary_hops = atoi(optarg);
break; break;
case 'd': case 'd':
domain_number = atoi(optarg); if (config_set_int(cfg, "domainNumber", atoi(optarg))) {
ret = -1;
goto out;
}
break;
case 'f':
config = optarg;
break; break;
case 'i': case 'i':
iface_name = optarg; iface_name = optarg;
@ -736,8 +765,12 @@ int main(int argc, char *argv[])
} }
break; break;
case 't': case 't':
if (1 == sscanf(optarg, "%x", &c)) if (1 == sscanf(optarg, "%x", &c)) {
transport_specific = c << 4; if (config_set_int(cfg, "transportSpecific", c)) {
ret = -1;
goto out;
}
}
break; break;
case 'v': case 'v':
version_show(stdout); version_show(stdout);
@ -758,6 +791,15 @@ int main(int argc, char *argv[])
} }
} }
if (config && (c = config_read(config, cfg))) {
config_destroy(cfg);
return -1;
}
transport_type = config_get_int(cfg, NULL, "network_transport");
transport_specific = config_get_int(cfg, NULL, "transportSpecific") << 4;
domain_number = config_get_int(cfg, NULL, "domainNumber");
if (!iface_name) { if (!iface_name) {
if (transport_type == TRANS_UDS) { if (transport_type == TRANS_UDS) {
snprintf(uds_local, sizeof(uds_local), snprintf(uds_local, sizeof(uds_local),
@ -855,6 +897,8 @@ int main(int argc, char *argv[])
pmc_destroy(pmc); pmc_destroy(pmc);
msg_cleanup(); msg_cleanup();
out:
config_destroy(cfg); config_destroy(cfg);
return ret; return ret;
} }