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