-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.h
152 lines (124 loc) · 3.18 KB
/
commands.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
* commands.h
*
* Function prototypes and default configuration.
*
* NOTE This file is part of UniversalShellBuilder.
*
*******************************************************************************/
#ifndef _COMMANDS_H
#define _COMMANDS_H
#ifdef __cplusplus
extern "C" {
#endif
/* INCLUDES */
#include <my_commands.h>
#include <minimal_lib.h>
#include <getopt.h>
//#define CMD_DEBUG
/* DEFAULT WORKING MODES */
#ifndef CMD_NO_AUTOHELP
#define CMD_AUTOHELP
#endif
#ifndef CMD_SAVE_MEMORY
#define CMD_NO_SAVE_MEMORY
#endif
/* COMMAND STRUCTURES */
#ifdef CMD_AUTOHELP
struct _command {
char *name;
int (*function) (void);
const char *help;
};
#define COMMAND(NAME) { #NAME, cmd_ ## NAME, cmd_ ## NAME ## _help }
#else
struct _command {
char *name;
int (*function) (void);
};
#define COMMAND(NAME) { #NAME, cmd_ ## NAME }
#endif
//#define N_COMMANDS sizeof(commands) / sizeof(commands[0])
/* GOT OPTIONS */
union _option {
char value;
char *content;
};
/* ERROR CODES */
enum _error_code {
CMD_ERROR_COMMAND_NOT_VALID,
CMD_ERROR_OPTION_NOT_VALID,
CMD_ERROR_OPTION_EXPECTS_ARG
};
/* GENERAL FUNCTIONS */
inline unsigned separate_args(char *msg, char *argv[]);
unsigned separate_args_char(char *msg, char *argv[], char c);
void execute_command(int argc, char* argv[]);
char *arg(const unsigned n);
char opt(const char opt);
char *opt_content(const char opt);
union _option opt_union(const char opt);
int get_options(char *options);
unsigned command_name(char *name);
void default_cmd_error(enum _error_code error, char* arg);
/* COMMANDS */
extern const struct _command commands[];
extern const unsigned N_COMMANDS;
/* GLOBAL VARIABLES */
extern int noarg;
/* DEFAULT COMMANDS */
int default_cmd_help (char *command);
int default_cmd_not_valid (char *command);
/* SOME DEFINITIONS */
// NOTE Under construction...
#ifndef CMD_MAX_N_OPTIONS
#define CMD_MAX_N_OPTIONS 10
#endif
#ifndef CMD_MAX_N_OPTIONS_WITH_ARGS
#define CMD_MAX_N_OPTIONS_WITH_ARGS 5
#endif
#ifndef NL
#define NL "\n"
#endif
//TODO NOT GENERAL!!!
// Sugested definitions:
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
//TODO NOT GENERAL!!!
/* GPRINT */
#ifndef gprint
// On GNU/Linux
#include <stdio.h>
#define gprint(str) printf("%s", str)
#endif
// TODO shorter strings, more schematic!
/* STRINGS */
#ifndef CMD_STR_PROJECT_TITLE
#define CMD_STR_PROJECT_TITLE "UniversalCommandBuilder project" NL NL
#endif
#ifndef CMD_STR_NOT_VALID_COMMAND
#define CMD_STR_NOT_VALID_COMMAND "is not a valid command" NL NL
#endif
#ifndef CMD_STR_ADDITIONAL_HELP
#define CMD_STR_ADDITIONAL_HELP "Type 'help [command]' for additional help" NL
#endif
#ifndef CMD_STR_AVAILABLE_COMMANDS
#define CMD_STR_AVAILABLE_COMMANDS "Available commands:" NL
#endif
#ifndef CMD_STR_NOT_VALID_OPTION
#define CMD_STR_NOT_VALID_OPTION "is not a valid option" NL
#endif
#ifndef CMD_STR_ARG_EXPECTED
#define CMD_STR_ARG_EXPECTED "option expects an argument" NL
#endif
#ifndef CMD_STR_ERROR_WORD
#define CMD_STR_ERROR_WORD "Error: "
#endif
#ifdef __cplusplus
}
#endif
#endif /* _COMMANDS_H */