WallShell
An easy-to-use, highly portable, CLI using C99.
wall_shell.h
Go to the documentation of this file.
1 
25 #ifndef COMMAND_HANDLER_H
26 #define COMMAND_HANDLER_H
27 
28 /* Freestanding headers. */
29 #include <stdint.h>
30 #include <stddef.h>
31 #include <stdbool.h>
32 #include <stdarg.h>
33 
34 /* Standard Library Headers */
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 
39 /* Config Header */
40 #include "wallshell_config.h"
41 
42 /*********************************************************************************
43  * Check for user definitions
44  *********************************************************************************/
45 #ifndef PREVIOUS_BUF_SIZE
46 #define PREVIOUS_BUF_SIZE 50
47 #endif // PREVIOUS_BUF_SIZE
48 
49 #ifndef MAX_COMMAND_BUF
50 #define MAX_COMMAND_BUF 256
51 #endif // MAX_COMMAND_BUF
52 
53 #ifdef THREADED_SUPPORT
54 #ifdef DISABLE_MALLOC
55 #error "Threaded support can't exist without malloc."
56 #endif
57 
58 #ifndef CUSTOM_THREADS
59 #ifdef _WIN32
60 #include <Windows.h>
65 typedef CRITICAL_SECTION ws_mutex_t;
70 typedef DWORD ws_thread_id_t;
71 
72 #else
73 #include <pthread.h>
78 typedef pthread_mutex_t ws_mutex_t;
83 typedef uint64_t ws_thread_id_t;
84 #endif // _WIN32
85 #endif
86 /* Mutex */
87 void ws_lockMutex(ws_mutex_t* mut);
88 void ws_unlockMutex(ws_mutex_t* mut);
90 void ws_destroyMutex(ws_mutex_t* mut);
91 
92 /* Thread ID */
93 ws_thread_id_t ws_getThreadID();
94 
95 /* Atomic Bool */
96 typedef struct {
97  bool b;
100 
102 void ws_setAtomicBool(ws_atomic_bool_t* ab, bool b);
105 
106 void ws_sleep(size_t ms);
107 
108 void ws_stopTerminal();
109 
110 /* Thread names for logging */
111 #ifndef NO_WS_LOGGING
112 void ws_setThreadName(char* name);
113 void ws_removeThreadName(const char* name);
114 void ws_printThreadID();
115 void ws_doPrintThreadID(bool b);
116 #endif // NO_WS_LOGGING
117 #endif // THREADED_SUPPORT
118 
119 #ifdef DISABLE_MALLOC
120 #ifndef COMMAND_LIMIT
121 #define COMMAND_LIMIT 25
122 #endif
123 #ifndef MAX_ARGS
124 #define MAX_ARGS 32
125 #endif
126 #endif // DISABLE_MALLOC
127 
128 typedef enum {
129  WS_NO_ERROR = 0,
130  WS_OUT_OF_MEMORY,
131  WS_COMMAND_LIMIT_REACHED,
132  WS_OUT_STREAM_NOT_SET,
133  WS_WS_SETUP_ERROR
134 } ws_error_t;
135 
136 typedef struct {
137  int (*mainCommand)(int argc, char** argv);
138  int (*helpCommand)(int argc, char** argv);
139  const char* commandName;
140  const char** aliases;
142 } ws_command_t;
143 
144 typedef struct {
145  const char* commandName;
146  const char* description;
147  const char** commands;
148  const int commands_count;
149  const char** aliases;
150  const int aliases_count;
152 
153 typedef struct {
154  const char* commandName;
155  const char* description;
156  const char** required;
157  const int required_count;
158  const char** optional;
159  const int optional_count;
161 
162 typedef enum {
163  WS_FG_DEFAULT = 0,
164  WS_FG_BLACK = 30,
165  WS_FG_RED = 31,
166  WS_FG_GREEN = 32,
167  WS_FG_YELLOW = 33,
168  WS_FG_BLUE = 34,
169  WS_FG_MAGENTA = 35,
170  WS_FG_CYAN = 36,
171  WS_FG_WHITE = 37,
172 
173  WS_FG_BRIGHT_BLACK = 90,
174  WS_FG_BRIGHT_RED = 91,
175  WS_FG_BRIGHT_GREEN = 92,
176  WS_FG_BRIGHT_YELLOW = 93,
177  WS_FG_BRIGHT_BLUE = 94,
178  WS_FG_BRIGHT_MAGENTA = 95,
179  WS_FG_BRIGHT_CYAN = 96,
180  WS_FG_BRIGHT_WHITE = 97,
181 } ws_fg_color_t;
182 
183 typedef enum {
184  WS_BG_DEFAULT = 0,
185  WS_BG_BLACK = 40,
186  WS_BG_RED = 41,
187  WS_BG_GREEN = 42,
188  WS_BG_YELLOW = 43,
189  WS_BG_BLUE = 44,
190  WS_BG_MAGENTA = 45,
191  WS_BG_CYAN = 46,
192  WS_BG_WHITE = 47,
193 
194  WS_BG_BRIGHT_BLACK = 100,
195  WS_BG_BRIGHT_RED = 101,
196  WS_BG_BRIGHT_GREEN = 102,
197  WS_BG_BRIGHT_YELLOW = 103,
198  WS_BG_BRIGHT_BLUE = 104,
199  WS_BG_BRIGHT_MAGENTA = 105,
200  WS_BG_BRIGHT_CYAN = 106,
201  WS_BG_BRIGHT_WHITE = 107,
202 } ws_bg_color_t;
203 
204 typedef struct {
207 } ws_color_t;
208 
209 /* Console Color Configuration */
218 
219 /* Stream configurations. */
220 typedef enum {
221  WS_INPUT, /* Input stream. Defaults to stdin. */
222  WS_OUTPUT, /* Output stream. Defaults to stdout. */
223  WS_ERROR_S /* Error stream. Defaults to stderr. */
224 } ws_stream;
225 
226 void ws_setStream(ws_stream type, FILE* stream);
227 
228 /* Cursors */
229 typedef enum {
230  WS_CURSOR_LEFT = 0x4b,
231  WS_CURSOR_RIGHT = 0x4d,
232  WS_CURSOR_UP = 0x48,
233  WS_CURSOR_DOWN = 0x50,
234 } ws_cursor_t;
235 
236 void ws_moveCursor(ws_cursor_t direction);
237 void ws_moveCursor_n(ws_cursor_t direction, size_t n);
238 
239 /* General operations */
241 void ws_deregisterCommand(const ws_command_t c);
242 ws_error_t ws_executeCommand(char* commandBuf);
244 
245 /* Console Setup */
246 void ws_setAsciiDeleteAsBackspace(bool b);
247 void ws_setConsoleLocale();
248 void ws_setConsolePrefix(const char* newPrefix);
250 
251 /* Utility functions */
254 bool ws_promptUser(const char* format, ...);
255 bool ws_compareCommands(const ws_command_t c1, const ws_command_t c2);
256 void ws_cleanAll();
257 
258 /* Logger */
259 #ifndef NO_WS_LOGGING
260 typedef enum {
261  WS_LOG,
262  WS_DEBUG,
263  WS_INFO,
264  WS_WARN,
265  WS_ERROR,
266  WS_FATAL
267 } ws_logtype_t;
268 
269 void ws_logger(ws_logtype_t type, const char* format, ...);
270 void ws_vlogger(ws_logtype_t type, const char* format, va_list args);
272 #endif // NO_WS_LOGGING
273 
274 #endif // COMMAND_HANDLER_H
Atomic bool structure provided by WallShell.
Definition: wall_shell.h:96
bool b
Bool currently stored by the atomic_bool.
Definition: wall_shell.h:97
ws_mutex_t * mut
Pointer to the mutex the bool uses.
Definition: wall_shell.h:98
Color structure used internally by WallShell.
Definition: wall_shell.h:204
ws_fg_color_t foreground
Foreground color of the structure.
Definition: wall_shell.h:205
ws_bg_color_t background
Background color of the structure.
Definition: wall_shell.h:206
Command Data Structure.
Definition: wall_shell.h:136
const char ** aliases
Any aliases you want your command to have.
Definition: wall_shell.h:140
const char * commandName
Name of the command.
Definition: wall_shell.h:139
size_t aliases_count
Amount of aliases in your alias array.
Definition: wall_shell.h:141
General help structure.
Definition: wall_shell.h:144
const char ** commands
Commands that are a subset of this command/topic.
Definition: wall_shell.h:147
const char * description
Description of the command.
Definition: wall_shell.h:146
const char ** aliases
Any aliases that this command has.
Definition: wall_shell.h:149
const char * commandName
Name of the command.
Definition: wall_shell.h:145
const int commands_count
Amount of commands in the commands part of this structure.
Definition: wall_shell.h:148
const int aliases_count
Amount of commands in the aliases part of this structure.
Definition: wall_shell.h:150
Specific help structure.
Definition: wall_shell.h:153
const char * description
Description of the command.
Definition: wall_shell.h:155
const char * commandName
Name of the command.
Definition: wall_shell.h:154
const int optional_count
Amount of flags/subcommands in the optional part of this structure.
Definition: wall_shell.h:159
const int required_count
Amount of flags/subcommands in the required part of this structure.
Definition: wall_shell.h:157
const char ** optional
Flags/subcommands that are optional when running this command.
Definition: wall_shell.h:158
const char ** required
Flags/subcommands that are a required when running this command.
Definition: wall_shell.h:156
ws_error_t ws_setConsoleColors(ws_color_t colors)
Set the background and foreground colors to the provided ones.
Definition: wall_shell.c:468
ws_error_t ws_registerCommand(const ws_command_t c)
Register the command to the command handler.
Definition: wall_shell.c:1160
void ws_destroyMutex(ws_mutex_t *mut)
Destroys the provided mutex.
Definition: wallshell_config.c:78
ws_fg_color_t
All built in foreground colors.
Definition: wall_shell.h:162
void ws_printSpecificHelp(ws_help_entry_specific_t *entry)
Prints the specific help entry.
Definition: wall_shell.c:2133
ws_thread_id_t ws_getThreadID()
Gets the threadID of the calling thread.
Definition: wall_shell.c:604
bool ws_getAtomicBool(ws_atomic_bool_t *ab)
Gets the value stored by the atomic bool.
Definition: wall_shell.c:632
ws_mutex_t * ws_createMutex()
Create a mutex.
Definition: wallshell_config.c:73
void ws_setStream(ws_stream type, FILE *stream)
Sets the stream to the provided one.
Definition: wall_shell.c:142
bool ws_promptUser(const char *format,...)
Prompts the user yes/no using the given prompt.
Definition: wall_shell.c:2177
void ws_setConsoleLocale()
Sets the console locale.
Definition: wall_shell.c:2083
uint64_t ws_thread_id_t
Wrapper around your system's thread hadnle.
Definition: wall_shell.h:83
ws_bg_color_t
All built in background colors.
Definition: wall_shell.h:183
void ws_deregisterCommand(const ws_command_t c)
Deregister the provided command.
Definition: wall_shell.c:1201
void ws_moveCursor_n(ws_cursor_t direction, size_t n)
Move the cursor n times in the provided direction.
Definition: wallshell_config.c:63
ws_error_t ws_setBackgroundColor(ws_bg_color_t color)
Sets the background color to the provided color.
Definition: wall_shell.c:495
void ws_setAtomicBool(ws_atomic_bool_t *ab, bool b)
Sets the value of the provided atomic bool.
Definition: wall_shell.c:646
void ws_setForegroundDefault(ws_fg_color_t c)
Set the default foreground color.
Definition: wall_shell.c:424
void ws_printThreadID()
Prints the threadID of the calling thread.
Definition: wall_shell.c:764
void ws_unlockMutex(ws_mutex_t *mut)
Unlocks the provided mutex.
Definition: wallshell_config.c:72
void ws_initializeDefaultStreams()
Initialize all streams to their defaults.
Definition: wall_shell.c:157
void ws_destroyAtomicBool(ws_atomic_bool_t *ab)
Destroys the provided atomic bool.
Definition: wall_shell.c:670
void ws_removeThreadName(const char *name)
Removes the name of the provided thread.
Definition: wall_shell.c:735
ws_error_t ws_executeCommand(char *commandBuf)
Execute a command with the provided buffer.
Definition: wall_shell.c:1663
void ws_setConsolePrefix(const char *newPrefix)
Set the prefix to the provided one.
Definition: wall_shell.c:1754
void ws_logger(ws_logtype_t type, const char *format,...)
Logger function for WallShell.
Definition: wall_shell.c:1030
ws_error_t ws_setForegroundColor(ws_fg_color_t color)
Sets the foreground color to the provided color.
Definition: wall_shell.c:482
ws_atomic_bool_t * ws_createAtomicBool(bool b)
Creates an atomic bool.
Definition: wall_shell.c:658
void ws_moveCursor(ws_cursor_t direction)
Moves the cursor once in the provided direction.
Definition: wallshell_config.c:59
void ws_sleep(size_t ms)
Sleep function wrapper.
Definition: wallshell_config.c:91
void ws_setDefaultColors(ws_color_t c)
Set the default colors to the provided ones.
Definition: wall_shell.c:446
void ws_setAsciiDeleteAsBackspace(bool b)
Some consoles send backspace as ASCII delete (0x7f) instead of '\b'.
Definition: wall_shell.c:317
void ws_cleanAll()
Cleans everything.
Definition: wall_shell.c:1763
bool ws_compareCommands(const ws_command_t c1, const ws_command_t c2)
Operator overloading isn't available in C.
Definition: wall_shell.c:2067
ws_error_t
All potential error returns by WallShell functions.
Definition: wall_shell.h:128
void ws_setBackgroundDefault(ws_bg_color_t c)
Set the default background color.
Definition: wall_shell.c:435
void ws_doPrintThreadID(bool b)
Set print threadID, which prints the threadID of function calling ws_logger.
Definition: wall_shell.c:787
void ws_lockMutex(ws_mutex_t *mut)
Locks the provided mutex.
Definition: wallshell_config.c:71
void ws_setLoggerColors(ws_logtype_t type, ws_fg_color_t fg, ws_bg_color_t bg)
Set the logger colors for the specified log type.
Definition: wall_shell.c:1043
void ws_setThreadName(char *name)
Sets the name of the calling thread.
Definition: wall_shell.c:703
ws_color_t ws_getDefaultColors()
Get the current default colors.
Definition: wall_shell.c:461
ws_color_t ws_getCurrentColors()
Get the current console colors.
Definition: wall_shell.c:455
pthread_mutex_t ws_mutex_t
Wrapper around your system's mutex type.
Definition: wall_shell.h:78
void ws_vlogger(ws_logtype_t type, const char *format, va_list args)
Logger function for WallShell.
Definition: wall_shell.c:992
void ws_printGeneralHelp(ws_help_entry_general_t *entry)
help
Definition: wall_shell.c:2090
ws_stream
Simple enum relating to stream types that WallShell uses.
Definition: wall_shell.h:220
void ws_stopTerminal()
Stops the currently running terminal.
Definition: wall_shell.c:1446
ws_error_t ws_terminalMain()
Main function for the terminal.
Definition: wall_shell.c:1785
ws_logtype_t
Logging types.
Definition: wall_shell.h:260
ws_cursor_t
Cursor directions.
Definition: wall_shell.h:229
Configuration file to put compile time options for WallShell.