Line |
Branch |
Exec |
Source |
1 |
|
|
#ifndef COMMAND_RUN_H |
2 |
|
|
#define COMMAND_RUN_H |
3 |
|
|
|
4 |
|
|
#include <stdbool.h> |
5 |
|
|
#include <stddef.h> |
6 |
|
|
#include <stdint.h> |
7 |
|
|
#include <string.h> |
8 |
|
|
#include "util/macros.h" |
9 |
|
|
#include "util/string-view.h" |
10 |
|
|
|
11 |
|
|
typedef struct { |
12 |
|
|
char **args; // Positional args, with flag args moved to the front |
13 |
|
|
size_t nr_args; // Number of args (not including flag args) |
14 |
|
|
uint_least64_t flag_set; // Bitset of used flags |
15 |
|
|
char flags[10]; // Flags in parsed order |
16 |
|
|
uint8_t nr_flags; // Number of parsed flags |
17 |
|
|
uint8_t nr_flag_args; // Number of flag args |
18 |
|
|
} CommandArgs; |
19 |
|
|
|
20 |
|
|
// A set of flags associated with a Command that define how it may be |
21 |
|
|
// used in certain contexts (completely unrelated to CommandArgs::flags |
22 |
|
|
// or Command::flags, despite the terminology being somewhat ambiguous) |
23 |
|
|
typedef enum { |
24 |
|
|
CMDOPT_ALLOW_IN_RC = 1 << 0, // Allow command in rc files |
25 |
|
|
CMDOPT_NO_FLAGS_AFTER_ARGS = 1 << 1, // Stop parsing flags after first positional arg |
26 |
|
|
} CommandOptions; |
27 |
|
|
|
28 |
|
|
struct EditorState; |
29 |
|
|
|
30 |
|
|
typedef bool (*CommandFunc)(struct EditorState *e, const CommandArgs *args); |
31 |
|
|
|
32 |
|
|
typedef struct { |
33 |
|
|
const char name[15]; |
34 |
|
|
const char flags[14]; |
35 |
|
|
uint8_t cmdopts; // CommandOptions |
36 |
|
|
uint8_t min_args; |
37 |
|
|
uint8_t max_args; // 0xFF here means "no limit" (effectively SIZE_MAX) |
38 |
|
|
CommandFunc cmd; |
39 |
|
|
} Command; |
40 |
|
|
|
41 |
|
|
typedef struct { |
42 |
|
|
const Command* (*lookup)(const char *name); |
43 |
|
|
void (*macro_record)(struct EditorState *e, const Command *cmd, char **args); |
44 |
|
|
} CommandSet; |
45 |
|
|
|
46 |
|
|
typedef struct { |
47 |
|
|
const CommandSet *cmds; |
48 |
|
|
const char* (*lookup_alias)(const struct EditorState *e, const char *name); |
49 |
|
|
char* (*expand_variable)(const struct EditorState *e, const char *name); |
50 |
|
|
const StringView *home_dir; |
51 |
|
|
struct EditorState *e; |
52 |
|
|
unsigned int recursion_count; |
53 |
|
|
bool allow_recording; |
54 |
|
|
bool expand_tilde_slash; |
55 |
|
|
} CommandRunner; |
56 |
|
|
|
57 |
|
|
// NOLINTNEXTLINE(*-avoid-non-const-global-variables) |
58 |
|
|
extern const Command *current_command; |
59 |
|
|
|
60 |
|
37487 |
static inline int command_cmp(const void *key, const void *elem) |
61 |
|
|
{ |
62 |
|
37487 |
const char *name = key; |
63 |
|
37487 |
const Command *cmd = elem; |
64 |
|
37487 |
return strcmp(name, cmd->name); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
bool handle_command(CommandRunner *runner, const char *cmd) NONNULL_ARGS; |
68 |
|
|
|
69 |
|
|
#endif |
70 |
|
|
|