-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqshell.h
More file actions
105 lines (74 loc) · 2.61 KB
/
qshell.h
File metadata and controls
105 lines (74 loc) · 2.61 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @ Author: luoqi
* @ Create Time: 2025-04-03 16:01
* @ Modified by: luoqi
* @ Modified time: 2025-04-29 17:10
* @ Description:
*/
#ifndef _QSHELL_H_
#define _QSHELL_H_
#pragma once
#include <thread>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <functional>
#define QCLI_USE_STDLIBC
#include "qcli.h"
#define ISARG(str1, str2) ((str1) != nullptr && (str2) != nullptr && strcmp((str1), (str2)) == 0)
#define ISARGV(n, str) (argv[n] != nullptr && strcmp(argv[n], #str) == 0)
#define ISARGC(n) (argc == n)
class QShell {
public:
// Constructor for QShell, initializes the shell with a print function and a get character function
using ArgsTable = QcliTable;
typedef int (*GetChFunc)(void);
using Hook = std::function<void()>;
QShell(QcliPrint print, GetChFunc getch);
QShell() = default;
// Destructor for QShell, cleans up resources
~QShell();
void init(QcliPrint print, GetChFunc getch);
void exit_hook_set(Hook hook);
// Starts the shell thread
int start();
// Typedef for command handler function
typedef int (*QShellCmdHandler)(int argc, char **argv);
// Adds a command to the shell with its handler and desc description
int cmd_add(const char *name, QShellCmdHandler handler, const char *desc);
// Deletes a command from the shell by its name
int cmd_del(const char *name);
// Adds a subcommand to a parent command
int cmd_sub_add(const char *parent_name, const char *subcmd_name, QShellCmdHandler handler, const char *desc);
// Stops the shell thread
int exit();
// Function to display help for command arguments
// typedef QCliArgsTable QCliArgsTable;
int args_help(ArgsTable *table, size_t sz);
// Function to handle command arguments
// argc: Number of this second arguments, skip the first argument(command name)
// argv: Array of argument strings, skip the first argument(command name)
int args_exec(int argc, char **argv, const ArgsTable *table, size_t table_size);
// Prints a string to the shell output
int println(const char *fmt, ...);
int print(const char *fmt, ...);
int xstr(std::string str);
void exec();
int execc(char c);
void title();
private:
// Shell initialization flag
bool inited = false;
Hook on_exit;
// Thread object for running the shell
std::thread thr;
bool is_exit{ false };
// CLI object for handling command line interface operations
Qcli cli;
std::vector<uintptr_t> cmds_addr;
// Function pointer to the get character function
GetChFunc getch;
};
#endif