-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.c
199 lines (167 loc) · 5.28 KB
/
sample.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*! \brief test AT command library
* \author Shylock Hg
* \date 2018-04-29
* \email [email protected]
* */
#include <assert.h>
#include <ctype.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <at/at_command.h>
#include <at/at_fsm.h>
#include <at/at_param.h>
#include <at/at_table.h>
/********** test at commands **********/
#define TEST_AT_CMD_HELLO "HELLO" //!< AT_HELLO=xxx,yyy
int at_cmd_hello_handler0(char* parameter) {
if (NULL == parameter) return -1;
at_cmd_params_t* p_at_cmd_params = at_cmd_params_new(parameter, 2, 2);
assert(p_at_cmd_params);
printf("parameters :`%d`-`%s` `%d`-`%s`!\n",
p_at_cmd_params->params[0]->is_in_quote,
p_at_cmd_params->params[0]->param,
p_at_cmd_params->params[1]->is_in_quote,
p_at_cmd_params->params[1]->param);
at_cmd_params_release(p_at_cmd_params);
// printf("set hello %s!\n",parameter);
return 0;
}
int at_cmd_hello_handler1(void) {
printf("read hello!\n");
return 0;
}
/*
int at_cmd_hello_handler2(const char * parameter){
}
*/
int at_cmd_hello_handler3(void) {
printf("exec hello!\n");
return 0;
}
#define TEST_AT_CMD_HI "HI"
int at_cmd_hi_handler0(char* parameter) {
if (NULL == parameter) return -1;
at_cmd_params_t* p_at_cmd_params = at_cmd_params_new(parameter, 2, 2);
assert(p_at_cmd_params);
printf("parameters :`%d`-`%s` `%d`-`%s`!\n",
p_at_cmd_params->params[0]->is_in_quote,
p_at_cmd_params->params[0]->param,
p_at_cmd_params->params[1]->is_in_quote,
p_at_cmd_params->params[1]->param);
at_cmd_params_release(p_at_cmd_params);
return 0;
}
int at_cmd_hi_handler1(void) {
printf("read hi!\n");
return 0;
}
/*
int at_cmd_hi_handler2(const char * parameter){
}
*/
int at_cmd_hi_handler3(void) {
printf("exec hi!\n");
return 0;
}
/********** test at commands **********/
#define TEST_AT_CMD_MAX_LEN 20
#define TEST_AT_CMD_MAX_PARAM_LEN 512
#define TEST_AT_CMD_DELIMITER '\n'
#define TEST_HASH_TAB_SIZE 100
#define UNUSED(x) (void)(x)
static bool runcond = true;
void sighandler(int signum) {
if (SIGINT == signum) {
runcond = false;
}
}
#define STR_PARAM "f:h"
/* \brief mode flag
* \enum MODE_SCRIPT interpret a brainfuck script file
* \enum MODE_INTERACTIVE run the brainfuck interpreter in interactive mode
* */
typedef enum { MODE_SCRIPT, MODE_INTERACTIVE } app_mode_t;
typedef struct param {
char* file;
app_mode_t mode;
} param_t;
int parse_params(int argc, char* argv[], param_t* param) {
int c = 0; //!< opt character
//!< handle command line parameters
while ((c = getopt(argc, argv, STR_PARAM)) != -1) {
switch (c) {
case 'f':
//!< interpret brainfuck script
if (access(optarg, F_OK) != -1) {
param->mode = MODE_SCRIPT;
param->file = optarg;
} else {
fprintf(stderr, "Err:unexist file %s!\n", optarg);
return -EINVAL;
}
break;
case 'h':
fprintf(stderr,
"Usage: at -f <script>\n"
" at\n");
exit(0);
case '?':
if (optopt == 'f') {
fprintf(stderr, "Option -%c requires an argument.\n",
optopt);
} else if (isprint(optopt)) {
fprintf(stderr, "Unkown option `-%c`.\n", optopt);
} else {
fprintf(stderr, "Unkown option character `\\x%x`.\n",
optopt);
}
default:
return -EINVAL;
break;
}
}
return 0;
}
int main(int argc, char* argv[]) {
int err = 0;
//< parse command line input arguments
param_t param = {.file = NULL, .mode = MODE_INTERACTIVE};
err = parse_params(argc, argv, ¶m);
if (0 > err) {
return -err;
}
//< AT command register table
at_cmd_cb_t at_cmd_table[2] = {
{AT_FLAG_VISIABLE, TEST_AT_CMD_HI, at_cmd_hi_handler0,
at_cmd_hi_handler1, NULL, at_cmd_hi_handler3},
{AT_FLAG_VISIABLE, TEST_AT_CMD_HELLO, at_cmd_hello_handler0,
at_cmd_hello_handler1, NULL, at_cmd_hello_handler3},
};
//< create AT command parser context
at_cmd_context_t* context =
at_cmd_class_new(TEST_HASH_TAB_SIZE, TEST_AT_CMD_MAX_LEN,
TEST_AT_CMD_MAX_PARAM_LEN, TEST_AT_CMD_DELIMITER);
//< register AT command to context
at_table_register(context, at_cmd_table,
sizeof(at_cmd_table) / sizeof(at_cmd_table[0]));
if (MODE_INTERACTIVE == param.mode) { //!< REPL
signal(SIGINT, sighandler);
char buffer[1024] = {0};
while (runcond) {
if (NULL != fgets(buffer, sizeof(buffer), stdin)) {
at_cmd_execute_script_string(context, buffer);
} else {
fprintf(stderr, "Err: Read line failed!\n");
}
}
} else { //!< script
at_cmd_execute_script(context, param.file);
}
at_cmd_class_release(context);
return 0;
}