Re: [PATCH v3 1/2] printk: console: Create console= parser that supports named options

Chris Down
2022-09-05T14:46:38Z

Chris Down writes:
>[code]

Er, forgot to update that for the one used in the test:

---

#define _DEFAULT_SOURCE

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>

#define clamp(a, b, c) (a)
#define CON_LOGLEVEL 128

struct console_cmdline {
char *options;
int level;
short flags;
};

static void parse_console_cmdline_options(struct console_cmdline *c,
char *options)
{
bool seen_serial_opts = false;
char *key;

while ((key = strsep(&options, ",")) != NULL) {
char *value;

value = strchr(key, ':');
if (value)
*(value++) = '\0';

if (strcmp(key, "loglevel") == 0 && value &&
isdigit(value[0]) && strlen(value) == 1) {
c->level = clamp(value[0] - '0', LOGLEVEL_EMERG,
LOGLEVEL_DEBUG + 1);
c->flags |= CON_LOGLEVEL;
continue;
}

if (!seen_serial_opts && isdigit(key[0]) && !value) {
seen_serial_opts = true;
c->options = key;
continue;
}

fprintf(stderr,
"ignoring invalid console option: '%s%s%s'\n", key,
value ? ":" : "", value ?: "");
}
}

int main(int argc, char *argv[])
{
struct console_cmdline con = { 0 };

if (argc != 2)
return EXIT_FAILURE;

parse_console_cmdline_options(&con, argv[1]);
printf("options: %s\n", con.options);
printf("level: %d\n", con.level);
printf("level set: %d\n", !!(con.flags & CON_LOGLEVEL));
}