Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add early command line parsing #2543

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

pjonsson
Copy link
Contributor

This is a different approach for the infrastructure parts of #755.

This pull request allows each compilation unit to specify their own command line options, and switches the border-router to use the new API.

The compilation units add their options before main is run, and parsing command line options is the first thing that happen inside main, so command line options can be used to configure things before processes are started.

The code uses getopt_long_only so legacy parameters (-v0..-v5) still work. The getopt_long_only function is widely available, but if the SDK for some platform does not provide that function, users can put in the code from newlib/picolibc or one of the BSDs and things should work.

This PR is already quite a few lines so there is no conflict detection, I'm sure calling getopt_long with two options that have the same name will give "interesting" results.

@pjonsson
Copy link
Contributor Author

@tdesmet do you think this PR and adding the following to some compilation unit in your software would work for your use case from #755?

CC_CONSTRUCTOR(CONTIKI_MIN_INIT_PRIO - 1) static void
init_key_config_data(void)
{
  // Do whatever initialization before main().
}

static int
key_callback(char *optarg)
{
  if(strlen(optarg) < 17) {
    fprintf(stderr, "Key must be at least 17 characters long\n");
    return 1;
  }
  key_value_thing = optarg;
  return 0;
}

CONTIKI_OPTION(CONTIKI_VERBOSE_PRIO + 6, {"key", required_argument, NULL, 0},
               key_callback, "key value\n");

@simonduq simonduq deleted the branch contiki-ng:develop August 8, 2023 14:27
@simonduq simonduq closed this Aug 8, 2023
@simonduq
Copy link
Member

simonduq commented Aug 8, 2023

Hi!

This was my mistake, very sorry about it, re-opening this PR now.

What happened is the following: I was switching the base branch for this repository https://github.com/wittra/contiki-ng from develop to wittra. But accidentally, I made the change on the wrong repo (this repo) and instead of switching the base branch I renamed it. And somehow github deleted develop and closed all PRs...

Many apologies for this mishap 🙏; I haven't contributed in a while.. but now at least everybody got some notification from me :p

@pjonsson
Copy link
Contributor Author

The ordering guarantees for the priority to CC_CONSTRUCTOR on macOS is weaker, only guaranteed within the module. I have updated the PR so it also works on macOS, even if more modules with command line parameters are added.

This adds code for letting each
compilation unit add some command
line option, independently of
other modules.

The parsing uses getopt_long_only,
which is available on Linux,
macOS, FreeBSD/NetBSD/OpenBSD, and
the widely used C libraries musl,
newlib and picolibc.

The functionality is currently
disconnected, the next commit will
convert native-border-router to use
the functionality and enable this
code.
This enables the modular command
line parsing, and converts rpl-border-router
to use it.

This allows rpl-border-router to exit
when detecting bad parameter combinations,
and report which parameter that was bad.
The commit also adds some validation
of the return values of atoi().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants