Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
ReadLine.h
1/*
2* If not stated otherwise in this file or this component's LICENSE file the
3* following copyright and licenses apply:
4*
5* Copyright 2015 Sky UK
6*
7* Licensed under the Apache License, Version 2.0 (the "License");
8* you may not use this file except in compliance with the License.
9* You may obtain a copy of the License at
10*
11* http://www.apache.org/licenses/LICENSE-2.0
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*/
19/*
20 * File: ReadLine.h
21 *
22 */
23#ifndef READLINE_H
24#define READLINE_H
25
26#include "IReadLine.h"
27#include <PollLoop.h>
28
29#include <condition_variable>
30#include <functional>
31#include <memory>
32#include <string>
33#include <vector>
34#include <mutex>
35#include <list>
36
37
38typedef char *rl_compentry_func_t(const char *, int);
39typedef int rl_command_func_t(int, int);
40typedef void rl_vcpfunc_t(char *);
41
42typedef int (*rl_crlf_t)();
43typedef int (*rl_on_new_line_t)();
44typedef int (*rl_forced_update_display_t)();
45typedef char** (*rl_completion_matches_t)(const char *, rl_compentry_func_t *);
46typedef int (*rl_bind_key_t)(int, rl_command_func_t *);
47typedef void (*rl_callback_handler_install_t)(const char *, rl_vcpfunc_t *);
48typedef void (*rl_callback_read_char_t)();
49typedef void (*rl_callback_handler_remove_t)();
50
51typedef void (*add_history_t)(const char *);
52
53
54
56 , public IReadLine
57 , public IReadLineContext
58 , public std::enable_shared_from_this<ReadLine>
59{
60public:
61 static std::shared_ptr<ReadLine> instance();
62 ~ReadLine() final;
63
64private:
65 ReadLine();
66
67public:
68 bool isValid() const override ;
69 void run() override;
70 bool addCommand(const std::string& name, CommandHandler handler,
71 const std::string& desc, const std::string& help,
72 const std::string& opts) override;
73
74public:
75 void runCommand(int argc, char* const *argv) override;
76
77public:
78 void process(const std::shared_ptr<AICommon::IPollLoop>& pollLoop, epoll_event event) override;
79
80public:
81 std::shared_ptr<const IReadLineContext> getContext() const override;
82
83 void quit() const override;
84 void printLn(const char *fmt, ...) const override;
85 void printLnError(const char *fmt, ...) const override;
86
87private:
88 static std::mutex mInstanceLock;
89 static std::shared_ptr<ReadLine> mInstance;
90
91private:
92 std::mutex mLock;
93 std::shared_ptr<AICommon::PollLoop> mPollLoop;
94
95private:
96 void initLib();
97
98 void* mLibHandle;
99
100 static rl_crlf_t _rl_crlf;
101 static rl_on_new_line_t _rl_on_new_line;
102 static rl_forced_update_display_t _rl_forced_update_display;
103 static rl_completion_matches_t _rl_completion_matches;
104 static rl_bind_key_t _rl_bind_key;
105 static rl_callback_handler_install_t _rl_callback_handler_install;
106 static rl_callback_read_char_t _rl_callback_read_char;
107 static rl_callback_handler_remove_t _rl_callback_handler_remove;
108 static add_history_t _add_history;
109
110 static char* _commandGenerator(const char *text, int state);
111 static char** _completionCallback(const char *text, int start, int end);
112 static void _commandLineHandler(char *text);
113
114 char* commandGenerator(const char *text, int state);
115 void commandLineHandler(const char *text);
116 void commandExecute(const std::string& cmdStr,
117 const std::vector<std::string>& args);
118
119private:
120 void helpCommand(const std::shared_ptr<const IReadLineContext>& context,
121 const std::vector<std::string>& args);
122 void quitCommand(const std::shared_ptr<const IReadLineContext>& context,
123 const std::vector<std::string>& args);
124
125private:
126 mutable bool mQuit;
127 mutable std::mutex mQuitLock;
128 mutable std::condition_variable mQuitConditional;
129
130 static void signalHandler(int dummy);
131
132private:
133 typedef struct _ReadLineCommand
134 {
135 const std::string name;
136 const CommandHandler handler;
137 const std::string desc;
138 const std::string help;
139 const std::string opts;
141
142 std::vector<ReadLine::ReadLineCommand> mCommands;
143};
144
145
146
147#endif // !defined(IPOLLLOOP_H)
Definition IPollLoop.h:40
Definition IReadLine.h:34
Definition IReadLine.h:45
Definition ReadLine.h:59
bool isValid() const override
Tests if the class is valid.
Definition ReadLine.cpp:144
void quitCommand(const std::shared_ptr< const IReadLineContext > &context, const std::vector< std::string > &args)
Called when the user types quit.
Definition ReadLine.cpp:651
void run() override
Run loop, blocks until quit is called.
Definition ReadLine.cpp:484
void quit() const override
Triggers an exit from the readline loop.
Definition ReadLine.cpp:543
void commandExecute(const std::string &cmdStr, const std::vector< std::string > &args)
Executes the given command, called from readline callback handler.
Definition ReadLine.cpp:397
void helpCommand(const std::shared_ptr< const IReadLineContext > &context, const std::vector< std::string > &args)
Called when the user types help.
Definition ReadLine.cpp:666
std::shared_ptr< const IReadLineContext > getContext() const override
Returns the print context.
Definition ReadLine.cpp:531
static void signalHandler(int dummy)
Signal handler for capturing ctrl-c and restoring the terminal state.
Definition ReadLine.cpp:470
static void _commandLineHandler(char *text)
Callback handler from the readline library.
Definition ReadLine.cpp:383
static char ** _completionCallback(const char *text, int start, int end)
Callback handler for the tab completion callback.
Definition ReadLine.cpp:307
void initLib()
Initialises access to the readline library.
Definition ReadLine.cpp:158
bool addCommand(const std::string &name, CommandHandler handler, const std::string &desc, const std::string &help, const std::string &opts) override
Adds a new command to the readline loop.
Definition ReadLine.cpp:628
void printLn(const char *fmt,...) const override
Prints out the message from the command handler.
Definition ReadLine.cpp:562
void process(const std::shared_ptr< AICommon::IPollLoop > &pollLoop, epoll_event event) override
Callback from the PollLoop that is listening on stdin.
Definition ReadLine.cpp:454
void printLnError(const char *fmt,...) const override
Prints out an error message from the command handler.
Definition ReadLine.cpp:580
void commandLineHandler(const char *text)
Callback handler from the readline library.
Definition ReadLine.cpp:328
static char * _commandGenerator(const char *text, int state)
Callback handler for the tab completion callback.
Definition ReadLine.cpp:292
char * commandGenerator(const char *text, int state)
Completion function for libreadline.
Definition ReadLine.cpp:253
Definition ReadLine.h:134