]> cloudbase.mooo.com Git - z180-stamp.git/blob - include/command.h
Print prefix of subcommands
[z180-stamp.git] / include / command.h
1 /*
2 * (C) Copyright 2014-2016, 2018 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * (C) Copyright 2000-2009
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10 /*
11 * Definitions for Command Processor
12 */
13 #ifndef __COMMAND_H
14 #define __COMMAND_H
15
16 #include "common.h"
17 #include "config.h"
18 #include <setjmp.h>
19
20 #ifndef NULL
21 #define NULL 0
22 #endif
23
24 /* Default to a width of 8 characters for help message command width */
25 #ifndef CONFIG_SYS_HELP_CMD_WIDTH
26 #define CONFIG_SYS_HELP_CMD_WIDTH 8
27 #endif
28
29 /*
30 * Error codes that commands return to cmd_process(). We use the standard 0
31 * and 1 for success and failure, but add one more case - failure with a
32 * request to call cmd_usage(). But the cmd_process() function handles
33 * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
34 * This is just a convenience for commands to avoid them having to call
35 * cmd_usage() all over the place.
36 */
37 typedef enum {
38 CMD_RET_SUCCESS = 0, /* Success */
39 CMD_RET_FAILURE = 1, /* Failure */
40 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
41 } command_ret_t;
42
43 /*
44 * Monitor Command Table
45 */
46
47 typedef const FLASH struct cmd_tbl_s cmd_tbl_t;
48 struct cmd_tbl_s {
49 const FLASH char *name; /* Command Name */
50 uint8_t maxargs; /* maximum number of arguments */
51 uint8_t flags; /* autorepeat allowed? */
52 /* Implementation function */
53 command_ret_t (*cmd)(cmd_tbl_t *, uint_fast8_t, int, char * const []);
54 const FLASH char *usage; /* Usage message (short) */
55 #ifdef CONFIG_SYS_LONGHELP
56 const FLASH char *help; /* Help message (long) */
57 #endif
58 cmd_tbl_t *subcmd;
59 #ifdef CONFIG_AUTO_COMPLETE
60 /* do auto completion on the arguments */
61 int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
62 #endif
63 };
64
65 /**
66 * Process a command with arguments. We look up the command and execute it
67 * if valid. Otherwise we print a usage message.
68 *
69 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
70 * @param argc Number of arguments (arg 0 must be the command text)
71 * @param argv Arguments
72 * @param repeatable This function sets this to 0 if the command is not
73 * repeatable. If the command is repeatable, the value
74 * is left unchanged.
75 * @return 0 if the command succeeded, 1 if it failed
76 */
77 command_ret_t
78 cmd_process(uint_fast8_t flag, int argc, char * const argv[], uint_fast8_t *repeatable);
79
80
81 /* command.c */
82 command_ret_t do_help(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
83
84 command_ret_t cmd_usage(cmd_tbl_t *cmdtp);
85
86 #ifdef CONFIG_AUTO_COMPLETE
87 extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
88 extern int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp);
89 #endif
90
91 /**
92 * cmd_process_error() - report and process a possible error
93 *
94 * @cmdtp: Command which caused the error
95 * @err: Error code (0 if none, -ve for error, like -EIO)
96 * @return 0 if there is not error, 1 (CMD_RET_FAILURE) if an error is found
97 */
98 int cmd_process_error(cmd_tbl_t *cmdtp, int err);
99
100 /*
101 * Monitor Command
102 *
103 * All commands use a common argument format:
104 *
105 * void function (cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
106 */
107
108
109 #ifdef CONFIG_CMD_BOOTD
110 extern command_ret_t do_bootd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
111 #endif
112 #ifdef CONFIG_CMD_BOOTM
113 extern command_ret_t do_bootm(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
114 extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
115 #else
116 static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp UNUSED, const char *cmd UNUSED)
117 {
118 return 0;
119 }
120 #endif
121
122 extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
123 char *const argv[]);
124
125 /*
126 * Command Flags:
127 */
128 #define CMD_FLAG_REPEAT 0x01 /* repeat last command */
129 #define CMD_FLAG_BOOTD 0x02 /* command is from bootd */
130
131 /*
132 * Flags for command table:
133 */
134 #define CTBL_RPT 0x01 /* command is repeatable */
135 #define CTBL_SUBCMD 0x02 /* command has subcommands */
136 #define CTBL_SUBCMDAUTO 0x04 /* execute subcommands whithout prefix */
137 #define CTBL_DBG 0x08 /* command is only for debugging */
138
139 #ifdef CONFIG_AUTO_COMPLETE
140 # define _CMD_COMPLETE(x) x,
141 #else
142 # define _CMD_COMPLETE(x)
143 #endif
144 #ifdef CONFIG_SYS_LONGHELP
145 # define _CMD_HELP(x) x,
146 #else
147 # define _CMD_HELP(x)
148 #endif
149
150
151 #define CMD_TBL_ITEM_FULL(_name, _maxargs, _rep, _cmd, \
152 _usage, _help, _subtbl, _comp) \
153 { FSTR(#_name), _maxargs, _rep, _cmd, FSTR(_usage), \
154 _CMD_HELP(FSTR(_help)) _subtbl, _CMD_COMPLETE(_comp) }
155
156 #define CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
157 _usage, _help, _comp) \
158 { FSTR(#_name), _maxargs, _rep, _cmd, FSTR(_usage), \
159 _CMD_HELP(FSTR(_help)) NULL, _CMD_COMPLETE(_comp) }
160
161 #define CMD_TBL_ITEM(_name, _maxargs, _rep, _cmd, _usage, _help) \
162 CMD_TBL_ITEM_FULL(_name, _maxargs, _rep, _cmd, \
163 _usage, _help, NULL, NULL)
164
165 #define CMD_TBL_ITEM_TOP(_name, _maxargs, _rep, _cmd, _usage, _help, _subtbl) \
166 CMD_TBL_ITEM_FULL(_name, _maxargs, _rep, _cmd, \
167 _usage, _help, _subtbl, NULL)
168
169 #define CMD_TBL_END(_table_start) { .subcmd = _table_start }
170
171 typedef command_ret_t (*do_cmd_t)(cmd_tbl_t *, uint_fast8_t, int, char * const []);
172
173 extern cmd_tbl_t cmd_tbl[];
174
175 extern jmp_buf cmd_jbuf;
176
177
178 #endif /* __COMMAND_H */