dte test coverage


Directory: ./
File: src/command/run.h
Date: 2025-05-08 15:05:54
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 uint_least64_t 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 struct {
50 const CommandSet *cmds;
51 const char* (*lookup_alias)(const struct EditorState *e, const char *name);
52 char* (*expand_variable)(const struct EditorState *e, const char *name);
53 const StringView *home_dir;
54 struct EditorState *e;
55 ErrorBuffer *ebuf;
56 unsigned int recursion_count;
57 bool allow_recording;
58 bool expand_tilde_slash;
59 } CommandRunner;
60
61 42895 static inline int command_cmp(const void *key, const void *elem)
62 {
63 42895 const char *name = key;
64 42895 const Command *cmd = elem;
65 42895 return strcmp(name, cmd->name);
66 }
67
68 8011 static inline bool command_func_call (
69 struct EditorState *e,
70 ErrorBuffer *ebuf,
71 const Command *cmd,
72 const CommandArgs *args
73 ) {
74 8011 const char *saved_cmd_name = ebuf->command_name;
75 8011 ebuf->command_name = cmd->name;
76 8011 bool r = cmd->cmd(e, args);
77 8011 ebuf->command_name = saved_cmd_name;
78 8011 return r;
79 }
80
81 bool handle_command(CommandRunner *runner, const char *cmd) NONNULL_ARGS;
82
83 #endif
84