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