-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_test.c
63 lines (53 loc) · 1.24 KB
/
console_test.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
/* Test out console API */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <sys/select.h>
#include "report.h"
#include "console.h"
/* Implement simple-minded calculator */
int value = 0;
bool app_quit(int argc, char *argv[]) {
report(0, "Quitting application. Value = %d", value);
return true;
}
bool do_times(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; i++) {
int oldval = value;
int arg;
if (!get_int(argv[i], &arg)) {
report(0, "Couldn't parse '%s' as int", argv[i]);
return false;
}
value *= arg;
report(0, "%d * %d --> %d", oldval, arg, value);
}
return true;
}
bool do_plus(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; i++) {
int oldval = value;
int arg;
if (!get_int(argv[i], &arg)) {
report(0, "Couldn't parse '%s' as int", argv[i]);
return false;
}
value += arg;
report(0, "%d + %d --> %d", oldval, arg, value);
}
return true;
}
int main(int argc, char *argv[]) {
init_cmd();
add_cmd("times", do_times, "Multiply");
add_cmd("plus", do_plus, "Add");
add_param("value", &value, "Value", NULL);
add_quit_helper(app_quit);
run_console(NULL);
finish_cmd();
return 0;
}