dte test coverage


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