dte test coverage


Directory: ./
File: src/command/run.h
Date: 2025-06-04 06:50:24
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 2 2 100.0%
Branches: 0 0 -%

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 "error.h"
9 #include "util/macros.h"
10 #include "util/string-view.h"
11
12 typedef uint_least64_t CommandFlagSet;
13
14 typedef struct {
15 char **args; // Positional args, with flag args moved to the front
16 size_t nr_args; // Number of args (not including flag args)
17 CommandFlagSet flag_set; // Bitset of used flags
18 char flags[14]; // Flags in parsed order
19 uint8_t nr_flags; // Number of parsed flags
20 uint8_t nr_flag_args; // Number of flag args
21 } CommandArgs;
22
23 // A set of flags associated with a Command that define how it may be
24 // used in certain contexts (completely unrelated to CommandArgs::flags
25 // or Command::flags, despite the terminology being somewhat ambiguous)
26 typedef enum {
27 CMDOPT_ALLOW_IN_RC = 1 << 0, // Allow command in rc files
28 CMDOPT_NO_FLAGS_AFTER_ARGS = 1 << 1, // Stop parsing flags after first positional arg
29 } CommandOptions;
30
31 struct EditorState;
32
33 typedef bool (*CommandFunc)(struct EditorState *e, const CommandArgs *args);
34
35 typedef struct {
36 const char name[15];
37 const char flags[14];
38 uint8_t cmdopts; // CommandOptions
39 uint8_t min_args;
40 uint8_t max_args; // 0xFF here means "no limit" (effectively SIZE_MAX)
41 CommandFunc cmd;
42 } Command;
43
44 typedef struct {
45 const Command* (*lookup)(const char *name);
46 void (*macro_record)(struct EditorState *e, const Command *cmd, char **args);
47 } CommandSet;
48
49 typedef enum {
50 CMDRUNNER_ALLOW_RECORDING = 1u << 0,
51 CMDRUNNER_EXPAND_TILDE_SLASH = 1u << 1,
52 } CommandRunnerFlags;
53
54 typedef struct {
55 const CommandSet *cmds;
56 const char* (*lookup_alias)(const struct EditorState *e, const char *name);
57 char* (*expand_variable)(const struct EditorState *e, const char *name);
58 const StringView *home_dir;
59 struct EditorState *e;
60 ErrorBuffer *ebuf;
61 unsigned int recursion_count;
62 CommandRunnerFlags flags;
63 } CommandRunner;
64
65 45376 static inline int command_cmp(const void *key, const void *elem)
66 {
67 45376 const char *name = key;
68 45376 const Command *cmd = elem;
69 45376 return strcmp(name, cmd->name);
70 }
71
72 8113 static inline bool command_func_call (
73 struct EditorState *e,
74 ErrorBuffer *ebuf,
75 const Command *cmd,
76 const CommandArgs *args
77 ) {
78 8113 const char *saved_cmd_name = ebuf->command_name;
79 8113 ebuf->command_name = cmd->name;
80 8113 bool r = cmd->cmd(e, args);
81 8113 ebuf->command_name = saved_cmd_name;
82 8113 return r;
83 }
84
85 bool handle_command(CommandRunner *runner, const char *cmd) NONNULL_ARGS;
86
87 #endif
88