- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmicrocli.h
More file actions
Latest commit
89 lines (73 loc) · 2.35 KB
/
Copy pathmicrocli.h
File metadata and controls
89 lines (73 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef_MICROCLI_H_
#define_MICROCLI_H_
#include"microcli_config.h"
#include"microcli_verbosity.h"
#include<stdbool.h>
// Error codes
typedefenum {
MICROCLI_ERR_UNKNOWN=-128,
MICROCLI_ERR_BUSY,
MICROCLI_ERR_BAD_ARG,
MICROCLI_ERR_OVERFLOW,
MICROCLI_ERR_OUT_OF_BOUNDS,
MICROCLI_ERR_CMD_NOT_FOUND,
MICROCLI_ERR_NO_DATA,
MICROCLI_ERR_MAX
} MicroCLIErr_t;
_Static_assert(MICROCLI_ERR_MAX<0, "Some MicroCLI error codes are non-negative!");
// Typedefs
typedefstructmicrocliHistoryEntryMicroCLIHistoryEntry_t;
typedefstructmicrocliCmdEntryMicroCLICmdEntry_t;
typedefstructmicrocliCfgMicroCLICfg_t;
typedefstructmicrocliCtxMicroCLI_t;
// Command function prototype
typedefint (*MicroCLICmd_t)(MicroCLI_t*ctx, constchar*args);
// I/O function prototypes
typedefint (*Printf_t)(constchar*fmt, ...);
typedefint (*Getchar_t)(void);
structmicrocliCmdEntry {
MicroCLICmd_tcmd;
constchar*name;
constchar*help;
};
structmicrocliHistoryEntry {
char*str;
MicroCLIHistoryEntry_t*newer;
MicroCLIHistoryEntry_t*older;
};
structmicrocliCfg {
struct {
Printf_tprintf;
Getchar_tgetchar; // Note: should return < 0 when no more data available
} io;
constchar*bannerText;
constchar*promptText;
constMicroCLICmdEntry_t*cmdTable;
unsigned intcmdCount;
};
structmicrocliCtx {
MicroCLICfg_tcfg;
intverbosity;
boolprompted;
struct {
charbuffer[MAX_CLI_INPUT_LEN+1];
unsigned intlen;
boolready;
} input;
MicroCLIHistoryEntry_t*history;
unsigned inthistorySize;
};
// Control
voidmicrocli_init(MicroCLI_t*ctx, constMicroCLICfg_t*cfg);
voidmicrocli_set_verbosity(MicroCLI_t*ctx, intverbosity);
voidmicrocli_interpreter_tick(MicroCLI_t*ctx);
// Input
intmicrocli_interpret_string(MicroCLI_t*ctx, constchar*str, boolprint);
// Output
intmicrocli_banner(MicroCLI_t*ctx);
intmicrocli_help(MicroCLI_t*ctx);
#definemicrocli_error(ctx, fmt, ...) MICROCLI_ERROR(ctx, fmt, ##__VA_ARGS__)
#definemicrocli_warn(ctx, fmt, ...) MICROCLI_WARN(ctx, fmt, ##__VA_ARGS__)
#definemicrocli_log(ctx, fmt, ...) MICROCLI_LOG(ctx, fmt, ##__VA_ARGS__)
#definemicrocli_debug(ctx, fmt, ...) MICROCLI_DEBUG(ctx, fmt, ##__VA_ARGS__)
#endif/* _MICROCLI_H_ */