]> cloudbase.mooo.com Git - z180-stamp.git/blame - include/command.h
commandtable, flags: int --> uint8_t/uint_fast8_t. Macro UNUSED for Parameters/Variables
[z180-stamp.git] / include / command.h
CommitLineData
35edb766 1/*
04b3ea0e 2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
35edb766
L
3 *
4 * (C) Copyright 2000-2009
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
04b3ea0e 7 * SPDX-License-Identifier: GPL-2.0
35edb766 8 */
d684c216
L
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"
fcd2239e 18#include <setjmp.h>
d684c216
L
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
d0581f88
L
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 */
37typedef enum {
889202c4
L
38 CMD_RET_SUCCESS = 0, /* Success */
39 CMD_RET_FAILURE = 1, /* Failure */
d0581f88
L
40 CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
41} command_ret_t;
42
d684c216
L
43/*
44 * Monitor Command Table
45 */
46
47struct cmd_tbl_s {
48 const FLASH char *name; /* Command Name */
93ea25f2
L
49 uint8_t maxargs; /* maximum number of arguments */
50 uint8_t flags; /* autorepeat allowed? */
35edb766 51 /* Implementation function */
93ea25f2 52 command_ret_t (*cmd)(const FLASH struct cmd_tbl_s *, uint_fast8_t, int, char * const []);
35edb766 53 const FLASH char *usage; /* Usage message (short) */
d684c216
L
54#ifdef CONFIG_SYS_LONGHELP
55 const FLASH char *help; /* Help message (long) */
56#endif
57#ifdef CONFIG_AUTO_COMPLETE
58 /* do auto completion on the arguments */
59 int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
60#endif
61};
62
63typedef const FLASH struct cmd_tbl_s cmd_tbl_t;
64
d0581f88
L
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 */
77command_ret_t
93ea25f2 78cmd_process(uint_fast8_t flag, int argc, char * const argv[], uint_fast8_t *repeatable);
d0581f88 79
d684c216
L
80
81/* command.c */
93ea25f2 82command_ret_t _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
d684c216
L
83cmd_tbl_t *find_cmd(const char *cmd);
84cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
85
86int cmd_tbl_item_count(void);
d0581f88 87command_ret_t cmd_usage(cmd_tbl_t *cmdtp);
d684c216
L
88
89#ifdef CONFIG_AUTO_COMPLETE
90extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
dea9a315 91extern int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp);
d684c216
L
92#endif
93
94/**
95 * cmd_process_error() - report and process a possible error
96 *
97 * @cmdtp: Command which caused the error
98 * @err: Error code (0 if none, -ve for error, like -EIO)
99 * @return 0 if there is not error, 1 (CMD_RET_FAILURE) if an error is found
100 */
101int cmd_process_error(cmd_tbl_t *cmdtp, int err);
102
103/*
104 * Monitor Command
105 *
106 * All commands use a common argument format:
107 *
93ea25f2 108 * void function (cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
d684c216
L
109 */
110
111
112#ifdef CONFIG_CMD_BOOTD
93ea25f2 113extern command_ret_t do_bootd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
d684c216
L
114#endif
115#ifdef CONFIG_CMD_BOOTM
93ea25f2 116extern command_ret_t do_bootm(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
d684c216
L
117extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
118#else
93ea25f2 119static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp UNUSED, const char *cmd UNUSED)
d684c216 120{
d684c216
L
121 return 0;
122}
123#endif
124
125extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
126 char *const argv[]);
127
93ea25f2 128extern command_ret_t do_reset(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]);
d684c216
L
129
130/*
131 * Command Flags:
132 */
93ea25f2
L
133#define CMD_FLAG_REPEAT 0x01 /* repeat last command */
134#define CMD_FLAG_BOOTD 0x02 /* command is from bootd */
135
136/*
137 * Flags for command table:
138 */
139#define CTBL_REPEAT 0x01 /* command is repeatable */
140#define CTBL_SUBCMD 0x02 /* command has subcommands */
d684c216
L
141
142#ifdef CONFIG_AUTO_COMPLETE
143# define _CMD_COMPLETE(x) x,
144#else
145# define _CMD_COMPLETE(x)
146#endif
147#ifdef CONFIG_SYS_LONGHELP
148# define _CMD_HELP(x) x,
149#else
150# define _CMD_HELP(x)
151#endif
152
153
154#define CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
155 _usage, _help, _comp) \
156 { FSTR(#_name), _maxargs, _rep, _cmd, FSTR(_usage), \
157 _CMD_HELP(FSTR(_help)) _CMD_COMPLETE(_comp) }
158
159#define CMD_TBL_ITEM(_name, _maxargs, _rep, _cmd, _usage, _help) \
160 CMD_TBL_ITEM_COMPLETE(_name, _maxargs, _rep, _cmd, \
161 _usage, _help, NULL)
162
93ea25f2 163typedef command_ret_t (*do_cmd_t)(cmd_tbl_t *, uint_fast8_t, int, char * const []);
d684c216
L
164
165extern cmd_tbl_t cmd_tbl[];
166
fcd2239e
L
167extern jmp_buf cmd_jbuf;
168
d684c216
L
169
170#endif /* __COMMAND_H */