]> rtime.felk.cvut.cz Git - sysless.git/blob - libs4c/cmdproc/cmdproc_test.c
Do not read unset parameter
[sysless.git] / libs4c / cmdproc / cmdproc_test.c
1 #include <cmd_proc.h>
2 #include <stdio.h>
3
4 int cmd_do_testopchar(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
5 {
6   int opchar;
7
8   opchar=cmd_opchar_check(cmd_io,des,param);
9   if(opchar<0) return opchar;
10
11   printf(
12       "cmd_do_testopchar called\n"
13       "param[0]=%s\n"
14       "param[1]=%s\n"
15       "param[2]=%s\n"
16       "param[3]=%s\n"
17       "opchar=%c\n", 
18       param[0], param[1], param[2], param[3], opchar);
19
20   return 0;
21 }
22
23 int cmd_do_testparam(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
24 {
25   printf(
26       "cmd_do_testparam called\n"
27       "param[0]=%s\n"
28       "param[1]=%s\n"
29       "param[2]=%s\n",
30       param[0], param[1], param[1] == NULL ? "!!unset!!" : param[2]);
31
32   return 0;
33 }
34
35 int cmd_do_testerror(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
36 {
37   return -1234;
38 }
39
40 int cmd_do_test(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
41 {
42     printf("This is the simplest command\n");
43     return 0;
44 }
45
46 int cmd_do_testcmdio(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
47 {
48     cmd_io_puts(cmd_io, "The first line of text\n");
49     cmd_io_puts(cmd_io, "The second line of text\n");
50     /* Only ED_LINE_CHARS character can be sent. */
51     return 0;
52 }
53
54 cmd_des_t const **cmd_list;
55
56 cmd_des_t const cmd_des_help={
57     0, 0,
58     "HELP","prints help for commands",
59     cmd_do_help,{(char*)&cmd_list}};
60
61 int val;
62 cmd_des_t const cmd_des_val={
63     0, CDESM_OPCHR|CDESM_RW,
64     "VAL","use ':' or '?' to store/read value of an integer variable",
65     cmd_do_rw_int, {(char*)&val}};
66
67 cmd_des_t const cmd_des_valro={
68     0, CDESM_OPCHR|CDESM_RD,
69     "VALRO","read only access to an integer variable",
70     cmd_do_rw_int, {(char*)&val}};
71
72 cmd_des_t const cmd_des_valwo={
73     0, CDESM_OPCHR|CDESM_WR,
74     "VALWO","write only access to an integer variable",
75     cmd_do_rw_int, {(char*)&val}};
76
77 cmd_des_t const cmd_des_opchar_test={
78     0, CDESM_OPCHR|CDESM_RW,
79     "OPCHAR","opchar test (use ':' or '?' as suffix)",
80     cmd_do_testopchar};
81
82 cmd_des_t const cmd_des_opchar_testro={
83     0, CDESM_OPCHR|CDESM_RD,
84     "OPCHARRO","opchar test (only '?' is allowed)",
85     cmd_do_testopchar};
86
87 cmd_des_t const cmd_des_test={
88     0, 0,
89     "TEST","the simplest command",
90     cmd_do_test};
91
92 cmd_des_t const cmd_des_testio={
93     0, 0,
94     "TESTIO","test of cmd_io inside functions (universal way to print results)",
95     cmd_do_testcmdio};
96
97 cmd_des_t const cmd_des_param={
98     0, 0,
99     "PARAM","test of parameters",
100     cmd_do_testparam};
101
102 cmd_des_t const cmd_des_prefix={
103     0, 0,
104     "PREFIX*","suffix of the command is supplied as a parametr",
105     cmd_do_testparam};
106
107 cmd_des_t const cmd_des_num={
108     0, 0,
109     "NUM##","suffix of the command (two digits) is supplied as a parametr",
110     cmd_do_testparam};
111
112 cmd_des_t const cmd_des_char={
113     0, 0,
114     "CHAR?","suffix of the command (one character) is supplied as a parametr",
115     cmd_do_testparam};
116
117 cmd_des_t const cmd_des_charmid={
118     0, 0,
119     "CHAR?MID","middle character of the command is supplied as a parametr",
120     cmd_do_testparam};
121
122 cmd_des_t const cmd_des_hiddedn={
123     0, 0,
124     "HIDDEN","should not be available",
125     cmd_do_test};
126
127 cmd_des_t const cmd_des_error={
128     0, 0,
129     "TESTERROR","should produce an error",
130     cmd_do_testerror};
131
132 /* Command lists */
133
134 cmd_des_t const *cmd_list_1[]={
135     &cmd_des_val,
136     &cmd_des_valro,
137     &cmd_des_valwo,
138     &cmd_des_opchar_test,
139     &cmd_des_opchar_testro,
140     NULL
141     };
142
143 cmd_des_t const *cmd_list_2[]={
144     &cmd_des_test,
145     &cmd_des_testio,
146     &cmd_des_param,
147     &cmd_des_prefix,
148     &cmd_des_num,
149     &cmd_des_char,
150     &cmd_des_charmid,
151     NULL
152     };
153
154 cmd_des_t const *cmd_list_main[]={
155   &cmd_des_help,
156   &cmd_des_error,
157   CMD_DES_INCLUDE_SUBLIST(cmd_list_1),
158   CMD_DES_CONTINUE_AT(cmd_list_2),
159   &cmd_des_hiddedn,
160   NULL
161 };
162
163 cmd_des_t const **cmd_list = cmd_list_main;
164
165 cmd_io_t cmd_io_std_line;
166
167 int main()
168 {
169     while (1) {
170         cmd_processor_run(&cmd_io_std_line, cmd_list_main);
171     }
172 }