forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_libcmd.c
72 lines (63 loc) · 1.49 KB
/
test_libcmd.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
/******************************************************************************
* Copyright (C) 2014-2015
* file: test_libcmd.c
* author: gozfree <[email protected]>
* created: 2015-10-31 16:59
* updated: 2015-10-31 16:59
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <liblog.h>
#include <libosal.h>
#include "libcmd.h"
static struct cmd *g_cmd = NULL;
static int do_ls(int argc, char **argv)
{
char buf[100];
system_noblock_with_result(argv, buf, sizeof(buf));
printf("buf = %s\n", buf);
return 0;
}
static int do_df(int argc, char **argv)
{
char buf[1023];
system_noblock_with_result(argv, buf, sizeof(buf));
printf("buf = %s\n", buf);
return 0;
}
static int do_foo(int argc, char **argv)
{
return 0;
}
static int do_quit(int argc, char **argv)
{
logi("quit\n");
exit(0);
return 0;
}
static void ctrl_c_op(int signo)
{
if (g_cmd) {
cmd_deinit(g_cmd);
}
exit(0);
}
int main(int argc, char **argv)
{
signal(SIGINT, ctrl_c_op);
g_cmd = cmd_init(100);
if (!g_cmd) {
loge("cmd_init failed!\n");
return -1;
}
cmd_register(g_cmd, "ls", do_ls);
cmd_register(g_cmd, "foo", do_foo);
cmd_register(g_cmd, "df", do_df);
cmd_register(g_cmd, "q", do_quit);
int cnt = cmd_get_registered(g_cmd);
logi("cmd_get_registered = %d\n", cnt);
cmd_loop(g_cmd);
return 0;
}