xref: /openbmc/u-boot/arch/sandbox/include/asm/getopt.h (revision a891601c)
170db4212SSimon Glass /*
270db4212SSimon Glass  * Code for setting up command line flags like `./u-boot --help`
370db4212SSimon Glass  *
470db4212SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
570db4212SSimon Glass  *
670db4212SSimon Glass  * Licensed under the GPL-2 or later.
770db4212SSimon Glass  */
870db4212SSimon Glass 
970db4212SSimon Glass #ifndef __SANDBOX_GETOPT_H
1070db4212SSimon Glass #define __SANDBOX_GETOPT_H
1170db4212SSimon Glass 
1270db4212SSimon Glass struct sandbox_state;
1370db4212SSimon Glass 
1470db4212SSimon Glass /*
1570db4212SSimon Glass  * Internal structure for storing details about the flag.
1670db4212SSimon Glass  * Most people should not have to dig around in this as
1770db4212SSimon Glass  * it only gets parsed by the core sandbox code.  End
1870db4212SSimon Glass  * consumer code should focus on the macros below and
1970db4212SSimon Glass  * the callback function.
2070db4212SSimon Glass  */
21*7b3efc66SSimon Glass struct sandbox_cmdline_option {
2270db4212SSimon Glass 	/* The long flag name: "help" for "--help" */
2370db4212SSimon Glass 	const char *flag;
2470db4212SSimon Glass 	/* The (optional) short flag name: "h" for "-h" */
2570db4212SSimon Glass 	int flag_short;
2670db4212SSimon Glass 	/* The help string shown to the user when processing --help */
2770db4212SSimon Glass 	const char *help;
2870db4212SSimon Glass 	/* Whether this flag takes an argument */
2970db4212SSimon Glass 	int has_arg;
3070db4212SSimon Glass 	/* Callback into the end consumer code with the option */
3170db4212SSimon Glass 	int (*callback)(struct sandbox_state *state, const char *opt);
3270db4212SSimon Glass };
3370db4212SSimon Glass 
3470db4212SSimon Glass /*
3570db4212SSimon Glass  * Internal macro to expand the lower macros into the necessary
3670db4212SSimon Glass  * magic junk that makes this all work.
3770db4212SSimon Glass  */
38*7b3efc66SSimon Glass #define _SANDBOX_CMDLINE_OPT(f, s, ha, h) \
39*7b3efc66SSimon Glass 	static struct sandbox_cmdline_option sandbox_cmdline_option_##f = { \
4070db4212SSimon Glass 		.flag = #f, \
4170db4212SSimon Glass 		.flag_short = s, \
4270db4212SSimon Glass 		.help = h, \
4370db4212SSimon Glass 		.has_arg = ha, \
44*7b3efc66SSimon Glass 		.callback = sandbox_cmdline_cb_##f, \
4570db4212SSimon Glass 	}; \
4670db4212SSimon Glass 	/* Ppointer to the struct in a special section for the linker script */ \
4770db4212SSimon Glass 	static __attribute__((section(".u_boot_sandbox_getopt"), used)) \
48*7b3efc66SSimon Glass 		struct sandbox_cmdline_option \
49*7b3efc66SSimon Glass 			*sandbox_cmdline_option_##f##_ptr = \
50*7b3efc66SSimon Glass 			&sandbox_cmdline_option_##f
5170db4212SSimon Glass 
5270db4212SSimon Glass /**
5370db4212SSimon Glass  * Macros for end code to declare new command line flags.
5470db4212SSimon Glass  *
5570db4212SSimon Glass  * @param f   The long flag name e.g. help
5670db4212SSimon Glass  * @param ha  Does the flag have an argument e.g. 0/1
5770db4212SSimon Glass  * @param h   The help string displayed when showing --help
5870db4212SSimon Glass  *
5970db4212SSimon Glass  * This invocation:
60*7b3efc66SSimon Glass  *   SANDBOX_CMDLINE_OPT(foo, 0, "The foo arg");
6170db4212SSimon Glass  * Will create a new flag named "--foo" (no short option) that takes
6270db4212SSimon Glass  * no argument.  If the user specifies "--foo", then the callback func
63*7b3efc66SSimon Glass  * sandbox_cmdline_cb_foo() will automatically be called.
6470db4212SSimon Glass  */
65*7b3efc66SSimon Glass #define SANDBOX_CMDLINE_OPT(f, ha, h) _SANDBOX_CMDLINE_OPT(f, 0, ha, h)
6670db4212SSimon Glass /*
6770db4212SSimon Glass  * Same as above, but @s is used to specify a short flag e.g.
68*7b3efc66SSimon Glass  *   SANDBOX_CMDLINE_OPT(foo, 'f', 0, "The foo arg");
6970db4212SSimon Glass  */
70*7b3efc66SSimon Glass #define SANDBOX_CMDLINE_OPT_SHORT(f, s, ha, h) _SANDBOX_CMDLINE_OPT(f, s, ha, h)
7170db4212SSimon Glass 
7270db4212SSimon Glass #endif
73