xref: /openbmc/u-boot/arch/sandbox/include/asm/getopt.h (revision 70db4212fcdb080444a23dccaf673b68a3ffc1fa)
1*70db4212SSimon Glass /*
2*70db4212SSimon Glass  * Code for setting up command line flags like `./u-boot --help`
3*70db4212SSimon Glass  *
4*70db4212SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
5*70db4212SSimon Glass  *
6*70db4212SSimon Glass  * Licensed under the GPL-2 or later.
7*70db4212SSimon Glass  */
8*70db4212SSimon Glass 
9*70db4212SSimon Glass #ifndef __SANDBOX_GETOPT_H
10*70db4212SSimon Glass #define __SANDBOX_GETOPT_H
11*70db4212SSimon Glass 
12*70db4212SSimon Glass struct sandbox_state;
13*70db4212SSimon Glass 
14*70db4212SSimon Glass /*
15*70db4212SSimon Glass  * Internal structure for storing details about the flag.
16*70db4212SSimon Glass  * Most people should not have to dig around in this as
17*70db4212SSimon Glass  * it only gets parsed by the core sandbox code.  End
18*70db4212SSimon Glass  * consumer code should focus on the macros below and
19*70db4212SSimon Glass  * the callback function.
20*70db4212SSimon Glass  */
21*70db4212SSimon Glass struct sb_cmdline_option {
22*70db4212SSimon Glass 	/* The long flag name: "help" for "--help" */
23*70db4212SSimon Glass 	const char *flag;
24*70db4212SSimon Glass 	/* The (optional) short flag name: "h" for "-h" */
25*70db4212SSimon Glass 	int flag_short;
26*70db4212SSimon Glass 	/* The help string shown to the user when processing --help */
27*70db4212SSimon Glass 	const char *help;
28*70db4212SSimon Glass 	/* Whether this flag takes an argument */
29*70db4212SSimon Glass 	int has_arg;
30*70db4212SSimon Glass 	/* Callback into the end consumer code with the option */
31*70db4212SSimon Glass 	int (*callback)(struct sandbox_state *state, const char *opt);
32*70db4212SSimon Glass };
33*70db4212SSimon Glass 
34*70db4212SSimon Glass /*
35*70db4212SSimon Glass  * Internal macro to expand the lower macros into the necessary
36*70db4212SSimon Glass  * magic junk that makes this all work.
37*70db4212SSimon Glass  */
38*70db4212SSimon Glass #define _SB_CMDLINE_OPT(f, s, ha, h) \
39*70db4212SSimon Glass 	static struct sb_cmdline_option sb_cmdline_option_##f = { \
40*70db4212SSimon Glass 		.flag = #f, \
41*70db4212SSimon Glass 		.flag_short = s, \
42*70db4212SSimon Glass 		.help = h, \
43*70db4212SSimon Glass 		.has_arg = ha, \
44*70db4212SSimon Glass 		.callback = sb_cmdline_cb_##f, \
45*70db4212SSimon Glass 	}; \
46*70db4212SSimon Glass 	/* Ppointer to the struct in a special section for the linker script */ \
47*70db4212SSimon Glass 	static __attribute__((section(".u_boot_sandbox_getopt"), used)) \
48*70db4212SSimon Glass 		struct sb_cmdline_option *sb_cmdline_option_##f##_ptr = \
49*70db4212SSimon Glass 		&sb_cmdline_option_##f
50*70db4212SSimon Glass 
51*70db4212SSimon Glass /**
52*70db4212SSimon Glass  * Macros for end code to declare new command line flags.
53*70db4212SSimon Glass  *
54*70db4212SSimon Glass  * @param f   The long flag name e.g. help
55*70db4212SSimon Glass  * @param ha  Does the flag have an argument e.g. 0/1
56*70db4212SSimon Glass  * @param h   The help string displayed when showing --help
57*70db4212SSimon Glass  *
58*70db4212SSimon Glass  * This invocation:
59*70db4212SSimon Glass  *   SB_CMDLINE_OPT(foo, 0, "The foo arg");
60*70db4212SSimon Glass  * Will create a new flag named "--foo" (no short option) that takes
61*70db4212SSimon Glass  * no argument.  If the user specifies "--foo", then the callback func
62*70db4212SSimon Glass  * sb_cmdline_cb_foo() will automatically be called.
63*70db4212SSimon Glass  */
64*70db4212SSimon Glass #define SB_CMDLINE_OPT(f, ha, h) _SB_CMDLINE_OPT(f, 0, ha, h)
65*70db4212SSimon Glass /*
66*70db4212SSimon Glass  * Same as above, but @s is used to specify a short flag e.g.
67*70db4212SSimon Glass  *   SB_CMDLINE_OPT(foo, 'f', 0, "The foo arg");
68*70db4212SSimon Glass  */
69*70db4212SSimon Glass #define SB_CMDLINE_OPT_SHORT(f, s, ha, h) _SB_CMDLINE_OPT(f, s, ha, h)
70*70db4212SSimon Glass 
71*70db4212SSimon Glass #endif
72