]> rtime.felk.cvut.cz Git - sysless.git/blob - libs4c/cmdproc/cmdproc_test.c
cmdproc_test: Set terminal to non-canonical mode
[sysless.git] / libs4c / cmdproc / cmdproc_test.c
1 #include <cmd_proc.h>
2 #include <stdio.h>
3 #include <termios.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <signal.h>
7
8 int cmd_do_testopchar(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
9 {
10   int opchar;
11
12   opchar=cmd_opchar_check(cmd_io,des,param);
13   if(opchar<0) return opchar;
14
15   printf(
16       "cmd_do_testopchar called\n"
17       "param[0]=%s\n"
18       "param[1]=%s\n"
19       "param[2]=%s\n"
20       "param[3]=%s\n"
21       "opchar=%c\n",
22       param[0], param[1], param[2], param[3], opchar);
23
24   return 0;
25 }
26
27 int cmd_do_spacesep(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
28 {
29   if (des->mode & CDESM_OPCHR) {
30     int opchar;
31     opchar=cmd_opchar_check(cmd_io,des,param);
32     if(opchar<0) return opchar;
33   }
34
35   int i;
36   for (i = 0; param[i] || (des->mode & CDESM_OPCHR && i == 1); i++)
37     printf("param[%d]=%s\n", i, param[i]);
38
39   return 0;
40 }
41
42 int cmd_do_testparam(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
43 {
44   printf(
45       "cmd_do_testparam called\n"
46       "param[0]=%s\n"
47       "param[1]=%s\n"
48       "param[2]=%s\n",
49       param[0], param[1], param[1] == NULL ? "!!unset!!" : param[2]);
50
51   return 0;
52 }
53
54 int cmd_do_testerror(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
55 {
56   return -1234;
57 }
58
59 int cmd_do_test(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
60 {
61     printf("This is the simplest command\n");
62     return 0;
63 }
64
65 int cmd_do_testcmdio(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
66 {
67     cmd_io_puts(cmd_io, "The first line of text\n");
68     cmd_io_puts(cmd_io, "The second line of text\n");
69     /* Only ED_LINE_CHARS character can be sent. */
70     return 0;
71 }
72
73 cmd_des_t const **cmd_list;
74
75 cmd_des_t const cmd_des_help={
76     0, 0,
77     "help","prints help for commands",
78     cmd_do_help,{(char*)&cmd_list}};
79
80 int val;
81 cmd_des_t const cmd_des_val={
82     0, CDESM_OPCHR|CDESM_RW,
83     "val","use ':' or '?' to store/read value of an integer variable",
84     cmd_do_rw_int, {(char*)&val}};
85
86 cmd_des_t const cmd_des_valro={
87     0, CDESM_OPCHR|CDESM_RD,
88     "valro","read only access to an integer variable",
89     cmd_do_rw_int, {(char*)&val}};
90
91 cmd_des_t const cmd_des_valwo={
92     0, CDESM_OPCHR|CDESM_WR,
93     "valwo","write only access to an integer variable",
94     cmd_do_rw_int, {(char*)&val}};
95
96 cmd_des_t const cmd_des_opchar_test={
97     0, CDESM_OPCHR|CDESM_RW,
98     "opchar","opchar test (use ':' or '?' as suffix)",
99     cmd_do_testopchar};
100
101 cmd_des_t const cmd_des_opchar_testro={
102     0, CDESM_OPCHR|CDESM_RD,
103     "opcharro","opchar test (only '?' is allowed)",
104     cmd_do_testopchar};
105
106 cmd_des_t const cmd_des_test={
107     0, 0,
108     "test","the simplest command",
109     cmd_do_test};
110
111 cmd_des_t const cmd_des_testio={
112     0, 0,
113     "testio","test of cmd_io inside functions (universal way to print results)",
114     cmd_do_testcmdio};
115
116 cmd_des_t const cmd_des_param={
117     0, 0,
118     "param","test of parameters",
119     cmd_do_testparam};
120
121 cmd_des_t const cmd_des_prefix={
122     0, 0,
123     "prefix*","suffix of the command is supplied as a parametr",
124     cmd_do_testparam};
125
126 cmd_des_t const cmd_des_num={
127     0, 0,
128     "num##","suffix of the command (two digits) is supplied as a parametr",
129     cmd_do_testparam};
130
131 cmd_des_t const cmd_des_char={
132     0, 0,
133     "char?","suffix of the command (one character) is supplied as a parametr",
134     cmd_do_testparam};
135
136 cmd_des_t const cmd_des_charmid={
137     0, 0,
138     "char?mid","middle character of the command is supplied as a parametr",
139     cmd_do_testparam};
140
141 cmd_des_t const cmd_des_hiddedn={
142     0, 0,
143     "hidden","should not be available",
144     cmd_do_test};
145
146 cmd_des_t const cmd_des_error={
147     0, 0,
148     "testerror","should produce an error",
149     cmd_do_testerror};
150
151 cmd_des_t const cmd_des_spacesep={
152     0, CDESM_SPACE_SEP,
153     "spacesep","Test for space separated parameters",
154     cmd_do_spacesep};
155
156 cmd_des_t const cmd_des_spacesepop={
157     0, CDESM_SPACE_SEP|CDESM_OPCHR|CDESM_RW,
158     "spacesepop","Test for space separated parameters and an opchar",
159     cmd_do_spacesep};
160
161 cmd_des_t const cmd_des_spacesepwild={
162     0, CDESM_SPACE_SEP,
163     "spacesep#","Test for space separated parameters and a wildcard",
164     cmd_do_spacesep};
165
166 /* Command lists */
167
168 cmd_des_t const *cmd_list_1[]={
169     &cmd_des_val,
170     &cmd_des_valro,
171     &cmd_des_valwo,
172     &cmd_des_opchar_test,
173     &cmd_des_opchar_testro,
174     NULL
175     };
176
177 cmd_des_t const *cmd_list_2[]={
178     &cmd_des_test,
179     &cmd_des_testio,
180     &cmd_des_param,
181     &cmd_des_prefix,
182     &cmd_des_num,
183     &cmd_des_char,
184     &cmd_des_charmid,
185     &cmd_des_spacesep,
186     &cmd_des_spacesepop,
187     &cmd_des_spacesepwild,
188     NULL
189     };
190
191 cmd_des_t const *cmd_list_main[]={
192   &cmd_des_help,
193   &cmd_des_error,
194   CMD_DES_INCLUDE_SUBLIST(cmd_list_1),
195   CMD_DES_CONTINUE_AT(cmd_list_2),
196   &cmd_des_hiddedn,
197   NULL
198 };
199
200 cmd_des_t const **cmd_list = cmd_list_main;
201
202 cmd_io_t cmd_io_std_line;
203
204 struct termios tcbackup;
205
206 void sigint(int arg)
207 {
208     tcsetattr(0, TCSAFLUSH, &tcbackup);
209     exit(0);
210 }
211
212 int main()
213 {
214     struct termios tc;
215     tcgetattr(0, &tc);
216     tcbackup = tc;
217     tc.c_lflag &= ~(ICANON|ECHO);
218     tc.c_cc[VMIN] = 1;
219     tc.c_cc[VTIME] = 0;
220     tc.c_cc[VERASE] = '\b';
221     tcsetattr(0, TCSAFLUSH, &tc);
222     signal(SIGINT, sigint);
223
224     while (1) {
225         cmd_processor_run(&cmd_io_std_line, cmd_list_main);
226     }
227 }