example: add getopt_long option parsing
Signed-off-by: Patrick McHardy <kaber@trash.net>
This commit is contained in:
parent
ac6d2ef7ae
commit
54cf9fbbe4
|
@ -5,7 +5,8 @@
|
|||
#include <dect/libdect.h>
|
||||
#include "common.h"
|
||||
|
||||
const struct dect_ipui ipui = {
|
||||
const char *cluster;
|
||||
struct dect_ipui ipui = {
|
||||
.put = DECT_IPUI_N,
|
||||
.pun.n.ipei = {
|
||||
.emc = 0x0ba8,
|
||||
|
@ -13,6 +14,18 @@ const struct dect_ipui ipui = {
|
|||
},
|
||||
};
|
||||
|
||||
int dect_parse_ipui(struct dect_ipui *ipui, const char *optarg)
|
||||
{
|
||||
ipui->put = DECT_IPUI_N;
|
||||
if (sscanf(optarg, "%04hx%05x",
|
||||
&ipui->pun.n.ipei.emc,
|
||||
&ipui->pun.n.ipei.psn) != 2) {
|
||||
fprintf(stderr, "could not parse IPUI '%s'\n", optarg);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pexit(const char *str)
|
||||
{
|
||||
perror(str);
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <list.h>
|
||||
|
||||
extern struct dect_handle *dh;
|
||||
extern struct dect_ipui ipui;
|
||||
extern const char *cluster;
|
||||
|
||||
extern int dect_event_ops_init(struct dect_ops *ops);
|
||||
extern void dect_event_loop(void);
|
||||
|
@ -17,17 +19,18 @@ extern void dect_debug_init(void);
|
|||
extern void dect_common_init(struct dect_ops *ops, const char *cluster);
|
||||
extern void dect_common_cleanup(struct dect_handle *dh);
|
||||
|
||||
extern int dect_parse_ipui(struct dect_ipui *ipui, const char *optarg);
|
||||
|
||||
extern int dect_write_uak(const struct dect_ipui *ipui,
|
||||
const uint8_t uak[DECT_AUTH_KEY_LEN]);
|
||||
extern int dect_read_uak(const struct dect_ipui *ipui,
|
||||
uint8_t uak[DECT_AUTH_KEY_LEN]);
|
||||
|
||||
extern const struct dect_ipui ipui;
|
||||
|
||||
extern void dect_pp_auth_init(struct dect_ops *ops,
|
||||
const struct dect_ipui *ipui);
|
||||
extern void dect_pp_common_init(struct dect_ops *ops, const char *cluster,
|
||||
const struct dect_ipui *ipui);
|
||||
extern void dect_pp_common_options(int argc, char **argv);
|
||||
extern void dect_pp_init_terminal_capability(struct dect_ie_terminal_capability *tcap);
|
||||
|
||||
struct mm_auth_priv {
|
||||
|
|
|
@ -45,7 +45,8 @@ int main(int argc, char **argv)
|
|||
{
|
||||
struct dect_mm_endpoint *mme;
|
||||
|
||||
dect_pp_common_init(&ops, argv[1], &ipui);
|
||||
dect_pp_common_options(argc, argv);
|
||||
dect_pp_common_init(&ops, cluster, &ipui);
|
||||
|
||||
mme = dect_mm_endpoint_alloc(dh, &ipui);
|
||||
if (mme == NULL)
|
||||
|
|
|
@ -7,10 +7,10 @@
|
|||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
|
@ -173,15 +173,57 @@ static struct dect_ops ops = {
|
|||
.mm_ops = &mm_ops,
|
||||
};
|
||||
|
||||
enum {
|
||||
OPT_CLUSTER = 'c',
|
||||
OPT_IPUI = 'i',
|
||||
OPT_PIN = 'p',
|
||||
OPT_HELP = 'h',
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
{ .name = "cluster", .has_arg = true, .flag = NULL, .val = OPT_CLUSTER },
|
||||
{ .name = "ipui", .has_arg = true, .flag = NULL, .val = OPT_IPUI },
|
||||
{ .name = "pin", .has_arg = true, .flag = NULL, .val = OPT_PIN },
|
||||
{ .name = "help", .has_arg = false, .flag = NULL, .val = OPT_HELP },
|
||||
{ },
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct dect_mm_endpoint *mme;
|
||||
const char *cluster = NULL;
|
||||
int optidx = 0, c;
|
||||
|
||||
for (;;) {
|
||||
c = getopt_long(argc, argv, "c:p:h", options, &optidx);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
switch (c) {
|
||||
case OPT_CLUSTER:
|
||||
cluster = optarg;
|
||||
break;
|
||||
case OPT_IPUI:
|
||||
if (dect_parse_ipui(&ipui, optarg) < 0)
|
||||
exit(1);
|
||||
break;
|
||||
case OPT_PIN:
|
||||
pin = optarg;
|
||||
break;
|
||||
case OPT_HELP:
|
||||
printf("%s [ -c/--cluster NAME ] [ -p/--pin PIN ] [ -h/--help ]\n",
|
||||
argv[0]);
|
||||
exit(0);
|
||||
case '?':
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
rand_fd = open("/dev/urandom", O_RDONLY);
|
||||
if (rand_fd < 0)
|
||||
pexit("open /dev/urandom");
|
||||
|
||||
dect_common_init(&ops, argv[1]);
|
||||
dect_common_init(&ops, cluster);
|
||||
|
||||
if (!(dect_llme_fp_capabilities(dh)->hlc &
|
||||
DECT_HLC_ACCESS_RIGHTS_REQUESTS)) {
|
||||
|
|
|
@ -50,7 +50,8 @@ static struct dect_ops ops = {
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
dect_common_init(&ops, argv[1]);
|
||||
dect_pp_common_options(argc, argv);
|
||||
dect_common_init(&ops, cluster);
|
||||
|
||||
dect_event_loop();
|
||||
|
||||
|
|
|
@ -8,6 +8,11 @@
|
|||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <dect/libdect.h>
|
||||
#include <dect/auth.h>
|
||||
#include "common.h"
|
||||
|
@ -42,3 +47,42 @@ void dect_pp_common_init(struct dect_ops *ops, const char *cluster,
|
|||
dect_common_init(ops, cluster);
|
||||
dect_pp_set_ipui(dh, ipui);
|
||||
}
|
||||
|
||||
enum {
|
||||
OPT_CLUSTER = 'c',
|
||||
OPT_IPUI = 'i',
|
||||
OPT_HELP = 'h',
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
{ .name = "cluster", .has_arg = true, .flag = NULL, .val = OPT_CLUSTER },
|
||||
{ .name = "ipui", .has_arg = true, .flag = NULL, .val = OPT_IPUI },
|
||||
{ .name = "help", .has_arg = false, .flag = NULL, .val = OPT_HELP },
|
||||
{ }
|
||||
};
|
||||
|
||||
void dect_pp_common_options(int argc, char **argv)
|
||||
{
|
||||
int optidx = 0, c;
|
||||
|
||||
for (;;) {
|
||||
c = getopt_long(argc, argv, "c:h", options, &optidx);
|
||||
if (c == -1)
|
||||
break;
|
||||
|
||||
switch (c) {
|
||||
case OPT_CLUSTER:
|
||||
break;
|
||||
case OPT_IPUI:
|
||||
if (dect_parse_ipui(&ipui, optarg))
|
||||
exit(1);
|
||||
break;
|
||||
case OPT_HELP:
|
||||
printf("%s [ -c/--cluster NAME ] [ -h/--help ]\n",
|
||||
argv[0]);
|
||||
exit(0);
|
||||
case '?':
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,8 @@ int main(int argc, char **argv)
|
|||
{
|
||||
struct dect_mm_endpoint *mme;
|
||||
|
||||
dect_pp_common_init(&ops, argv[1], &ipui);
|
||||
dect_pp_common_options(argc, argv);
|
||||
dect_pp_common_init(&ops, cluster, &ipui);
|
||||
|
||||
mme = dect_mm_endpoint_alloc(dh, &ipui);
|
||||
if (mme == NULL)
|
||||
|
|
|
@ -49,7 +49,8 @@ int main(int argc, char **argv)
|
|||
{
|
||||
struct dect_mm_endpoint *mme;
|
||||
|
||||
dect_pp_common_init(&ops, argv[1], &ipui);
|
||||
dect_pp_common_options(argc, argv);
|
||||
dect_pp_common_init(&ops, cluster, &ipui);
|
||||
|
||||
mme = dect_mm_endpoint_alloc(dh, &ipui);
|
||||
if (mme == NULL)
|
||||
|
|
|
@ -71,7 +71,8 @@ int main(int argc, char **argv)
|
|||
{
|
||||
const struct dect_fp_capabilities *fpc;
|
||||
|
||||
dect_pp_common_init(&ops, argv[1], &ipui);
|
||||
dect_pp_common_options(argc, argv);
|
||||
dect_pp_common_init(&ops, cluster, &ipui);
|
||||
|
||||
fpc = dect_llme_fp_capabilities(dh);
|
||||
if (!(fpc->ehlc2 & DECT_EHLC2_LIST_ACCESS_FEATURES)) {
|
||||
|
|
|
@ -60,7 +60,8 @@ int main(int argc, char **argv)
|
|||
const struct dect_fp_capabilities *fpc;
|
||||
struct dect_mm_endpoint *mme;
|
||||
|
||||
dect_pp_common_init(&ops, argv[1], &ipui);
|
||||
dect_pp_common_options(argc, argv);
|
||||
dect_pp_common_init(&ops, cluster, &ipui);
|
||||
|
||||
fpc = dect_llme_fp_capabilities(dh);
|
||||
if (!(fpc->hlc & DECT_HLC_LOCATION_REGISTRATION)) {
|
||||
|
|
|
@ -16,7 +16,8 @@ static struct dect_ops ops;
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
dect_pp_common_init(&ops, argv[1], &ipui);
|
||||
dect_pp_common_options(argc, argv);
|
||||
dect_pp_common_init(&ops, cluster, &ipui);
|
||||
|
||||
dect_event_loop();
|
||||
|
||||
|
|
Reference in New Issue