-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello-cli.c
More file actions
600 lines (519 loc) · 16.6 KB
/
hello-cli.c
File metadata and controls
600 lines (519 loc) · 16.6 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* The hello-cli demo program
*
* This file is part of the "u2up-cli" software project.
*
* Copyright (C) 2020 Samo Pogacnik <samo_pogacnik@t-2.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef U2UP_CLI_FILE_hello_cli_c
#define U2UP_CLI_FILE_hello_cli_c
#else
#error Preprocesor macro U2UP_CLI_FILE_hello_cli_c conflict!
#endif
#define _GNU_SOURCE
#if 0
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#include <getopt.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <u2up-cli/u2up-clisrv.h>
#include <u2up-cli/u2up-clicli.h>
#define U2UP_LOG_NAME DEMO_CLI
#include <u2up-log/u2up-log.h>
/* Declare all other used "u2up-log" modules: */
U2UP_LOG_DECLARE(U2CLICLI);
U2UP_LOG_DECLARE(U2CLISRV);
/*
* The MAIN part.
*/
static void usage_help(char *argv[])
{
printf("Usage:\n");
printf("\t%s [options]\n", argv[0]);
printf("options:\n");
printf("\t-q, --quiet Disable all output.\n");
printf("\t-p, --no-module NAME Disable all output from u2up_log module NAME prefix.\n");
printf("\t-v, --verbose Enable verbose output.\n");
#if (U2UP_LOG_MODULE_TRACE != 0)
printf("\t-t, --trace Enable trace output.\n");
#endif
#if (U2UP_LOG_MODULE_DEBUG != 0)
printf("\t-g, --debug Enable debug output.\n");
#endif
printf("\t-s, --syslog Enable syslog output (instead of stdout, stderr).\n");
printf("\t-n, --no-header No U2UP_LOG header added to every u2up_log_... output.\n");
printf("\t-h, --help Displays this text.\n");
}
static int usage_check(int argc, char *argv[])
{
int c;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"quiet", 0, 0, 'q'},
{"no-module", 1, 0, 'p'},
{"verbose", 0, 0, 'v'},
#if (U2UP_LOG_MODULE_TRACE != 0)
{"trace", 0, 0, 't'},
#endif
#if (U2UP_LOG_MODULE_DEBUG != 0)
{"debug", 0, 0, 'g'},
#endif
{"no-header", 0, 0, 'n'},
{"syslog", 0, 0, 's'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
#if (U2UP_LOG_MODULE_TRACE != 0) && (U2UP_LOG_MODULE_DEBUG != 0)
c = getopt_long(argc, argv, "qp:vltgnsh", long_options, &option_index);
#elif (U2UP_LOG_MODULE_TRACE == 0) && (U2UP_LOG_MODULE_DEBUG != 0)
c = getopt_long(argc, argv, "qp:vlgnsh", long_options, &option_index);
#elif (U2UP_LOG_MODULE_TRACE != 0) && (U2UP_LOG_MODULE_DEBUG == 0)
c = getopt_long(argc, argv, "qp:vltnsh", long_options, &option_index);
#else
c = getopt_long(argc, argv, "qp:vlnsh", long_options, &option_index);
#endif
if (c == -1)
break;
switch (c) {
case 'q':
U2UP_LOG_SET_NORMAL(0);
U2UP_LOG_SET_NORMAL2(U2CLICLI, 0);
U2UP_LOG_SET_NORMAL2(U2CLISRV, 0);
break;
case 'v':
U2UP_LOG_SET_VERBOSE(1);
U2UP_LOG_SET_VERBOSE2(U2CLICLI, 1);
U2UP_LOG_SET_VERBOSE2(U2CLISRV, 1);
break;
case 'p': {
size_t optlen = strlen(optarg);
//tst: printf("no-module: optlen=%zd, optarg=%s\n", optlen, optarg);
if (strlen(U2UP_LOG_GET_NAME()) >= optlen)
if (strncmp(U2UP_LOG_GET_NAME(), optarg, optlen) == 0) {
U2UP_LOG_SET_QUIET(1);
}
if (strlen(U2UP_LOG_GET_NAME2(U2CLICLI)) >= optlen)
if (strncmp(U2UP_LOG_GET_NAME2(U2CLICLI), optarg, optlen) == 0) {
U2UP_LOG_SET_QUIET2(U2CLICLI, 1);
}
if (strlen(U2UP_LOG_GET_NAME2(U2CLISRV)) >= optlen)
if (strncmp(U2UP_LOG_GET_NAME2(U2CLISRV), optarg, optlen) == 0) {
U2UP_LOG_SET_QUIET2(U2CLISRV, 1);
}
}
break;
#if (U2UP_LOG_MODULE_TRACE != 0)
case 't':
U2UP_LOG_SET_TRACE(1);
U2UP_LOG_SET_TRACE2(U2CLICLI, 1);
U2UP_LOG_SET_TRACE2(U2CLISRV, 1);
break;
#endif
#if (U2UP_LOG_MODULE_DEBUG != 0)
case 'g':
U2UP_LOG_SET_DEBUG(1);
U2UP_LOG_SET_DEBUG2(U2CLICLI, 1);
U2UP_LOG_SET_DEBUG2(U2CLISRV, 1);
break;
#endif
case 'n':
U2UP_LOG_SET_HEADER(0);
U2UP_LOG_SET_HEADER2(U2CLICLI, 0);
U2UP_LOG_SET_HEADER2(U2CLISRV, 0);
break;
case 's':
U2UP_LOG_SET_SYSLOG(1);
U2UP_LOG_SET_SYSLOG2(U2CLICLI, 1);
U2UP_LOG_SET_SYSLOG2(U2CLISRV, 1);
break;
case 'h':
usage_help(argv);
exit(EXIT_SUCCESS);
case '?':
exit(EXIT_FAILURE);
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
exit(EXIT_FAILURE);
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
exit(EXIT_FAILURE);
}
return 0;
}
static int *clisd;
static int *srvsd;
static clisrv_pconn_struct *pconn;
static char *clisrv_cmds[] = {
"help",
"action1 [str=%s]",
"action2 obj1 {aa=%8x|ab=%u}",
"action3 obj1 {aa=%8x|ab=%u | all}",
"action4 obj1 obj2 {aa=%8x|ab=%u}{cc}{cd}{e}",
"action2 obj2 {cc}{cd} {e}",
"obj1 action2 {aa=%8x|ab=%u}",
"obj1 action3 {aa=%8x | ab=%u | all}",
"obj2 action2 {cc} {cd} {e}",
"quit",
NULL
};
static int help_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int action1_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int action2_obj1_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int action3_obj1_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int action4_obj1_obj2_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int action2_obj2_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int obj1_action2_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int obj1_action3_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int obj2_action2_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int quit_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
#if 0 /*orig*/
static int test2_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
static int test3_handle(clisrv_token_struct *curr_tokens, char *buff, int size);
#endif
static int (*cmd_handle[])(clisrv_token_struct *curr_tokens, char *buff, int size) = {
help_handle,
action1_handle,
action2_obj1_handle,
action3_obj1_handle,
action4_obj1_obj2_handle,
action2_obj2_handle,
obj1_action2_handle,
obj1_action3_handle,
obj2_action2_handle,
quit_handle,
#if 0 /*orig*/
test2_handle,
test3_handle,
#endif
};
static int help_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
printf("help command handle called!'\n");
clisrv_strncat(buff, "\nPress TAB-TAB to display all available commands.\n", size);
clisrv_strncat(buff, "Use TAB for auto-complete.\n", size);
clisrv_strncat(buff, "Use TAB-TAB for auto-suggest.\n", size);
clisrv_strncat(buff, "Use UP and DOWN keys to walk the commands history.\n", size);
return 0;
}
static int action1_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *str_token;
printf("action1: command handle called!'\n");
if ((str_token = getCurrentToken(curr_tokens, "str")) != NULL) {
if ((str_token->eqval != NULL) && (strlen(str_token->eqval) > 0)) {
printf("action1 command handle: str='%s'\n", str_token->eqval);
}
}
return 0;
}
static int action2_obj1_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *aa_token;
clisrv_token_struct *ab_token;
uint32_t aa;
unsigned int ab;
printf("action2 obj1: command handle called!'\n");
if ((aa_token = getCurrentToken(curr_tokens, "aa")) != NULL) {
if ((aa_token->eqval != NULL) && (strlen(aa_token->eqval) > 0)) {
sscanf(aa_token->eqval, aa_token->eqspec, &aa);
}
printf("action2 obj1: command parameter (aa=%.8x)!'\n", aa);
} else
if ((ab_token = getCurrentToken(curr_tokens, "ab")) != NULL) {
if ((ab_token->eqval != NULL) && (strlen(ab_token->eqval) > 0)) {
sscanf(ab_token->eqval, ab_token->eqspec, &ab);
}
printf("action2 obj1: command parameter (ab=%u)!'\n", ab);
} else
return -3;
return 0;
}
static int action3_obj1_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *aa_token;
clisrv_token_struct *ab_token;
clisrv_token_struct *all_token;
uint32_t aa;
unsigned int ab;
printf("action3 obj1: command handle called!'\n");
if ((aa_token = getCurrentToken(curr_tokens, "aa")) != NULL) {
if ((aa_token->eqval != NULL) && (strlen(aa_token->eqval) > 0)) {
sscanf(aa_token->eqval, aa_token->eqspec, &aa);
}
printf("action3 obj1: command parameter (aa=%.8x)!'\n", aa);
} else
if ((ab_token = getCurrentToken(curr_tokens, "ab")) != NULL) {
if ((ab_token->eqval != NULL) && (strlen(ab_token->eqval) > 0)) {
sscanf(ab_token->eqval, ab_token->eqspec, &ab);
}
printf("action3 obj1: command parameter (ab=%u)!'\n", ab);
} else
if ((all_token = getCurrentToken(curr_tokens, "all")) != NULL) {
printf("action3 obj1: command parameter (all)!'\n");
} else
return -3;
return 0;
}
static int action4_obj1_obj2_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *aa_token;
clisrv_token_struct *ab_token;
clisrv_token_struct *cc_token;
clisrv_token_struct *cd_token;
clisrv_token_struct *e_token;
uint32_t aa;
unsigned int ab;
printf("action3 obj1: command handle called!'\n");
if ((aa_token = getCurrentToken(curr_tokens, "aa")) != NULL) {
if ((aa_token->eqval != NULL) && (strlen(aa_token->eqval) > 0)) {
sscanf(aa_token->eqval, aa_token->eqspec, &aa);
}
printf("action3 obj1: command parameter (aa=%.8x)!'\n", aa);
} else
if ((ab_token = getCurrentToken(curr_tokens, "ab")) != NULL) {
if ((ab_token->eqval != NULL) && (strlen(ab_token->eqval) > 0)) {
sscanf(ab_token->eqval, ab_token->eqspec, &ab);
}
printf("action3 obj1: command parameter (ab=%u)!'\n", ab);
} else
return -3;
if ((cc_token = getCurrentToken(curr_tokens, "cc")) != NULL) {
printf("action3 obj1: command parameter (cc)!'\n");
} else
return -3;
if ((cd_token = getCurrentToken(curr_tokens, "cd")) != NULL) {
printf("action3 obj1: command parameter (cd)!'\n");
} else
return -3;
if ((e_token = getCurrentToken(curr_tokens, "e")) != NULL) {
printf("action3 obj1: command parameter (e)!'\n");
} else
return -3;
return 0;
}
static int action2_obj2_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *cc_token;
clisrv_token_struct *cd_token;
clisrv_token_struct *e_token;
printf("action2 obj2: command handle called!'\n");
if ((cc_token = getCurrentToken(curr_tokens, "cc")) != NULL) {
printf("action2 obj2: command parameter (cc)!'\n");
} else
return -3;
if ((cd_token = getCurrentToken(curr_tokens, "cd")) != NULL) {
printf("action2 obj2: command parameter (cd)!'\n");
} else
return -3;
if ((e_token = getCurrentToken(curr_tokens, "e")) != NULL) {
printf("action2 obj2: command parameter (e)!'\n");
} else
return -3;
return 0;
}
static int obj1_action2_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *aa_token;
clisrv_token_struct *ab_token;
uint32_t aa;
unsigned int ab;
printf("obj1 action2: command handle called!'\n");
if ((aa_token = getCurrentToken(curr_tokens, "aa")) != NULL) {
if ((aa_token->eqval != NULL) && (strlen(aa_token->eqval) > 0)) {
sscanf(aa_token->eqval, aa_token->eqspec, &aa);
}
printf("obj1 action2: command parameter (aa=%.8x)!'\n", aa);
} else
if ((ab_token = getCurrentToken(curr_tokens, "ab")) != NULL) {
if ((ab_token->eqval != NULL) && (strlen(ab_token->eqval) > 0)) {
sscanf(ab_token->eqval, ab_token->eqspec, &ab);
}
printf("obj1 action2: command parameter (ab=%u)!'\n", ab);
} else
return -3;
return 0;
}
static int obj1_action3_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *aa_token;
clisrv_token_struct *ab_token;
clisrv_token_struct *all_token;
uint32_t aa;
unsigned int ab;
printf("obj1 action3: command handle called!'\n");
if ((aa_token = getCurrentToken(curr_tokens, "aa")) != NULL) {
if ((aa_token->eqval != NULL) && (strlen(aa_token->eqval) > 0)) {
sscanf(aa_token->eqval, aa_token->eqspec, &aa);
}
printf("obj1 action3: command parameter (aa=%.8x)!'\n", aa);
} else
if ((ab_token = getCurrentToken(curr_tokens, "ab")) != NULL) {
if ((ab_token->eqval != NULL) && (strlen(ab_token->eqval) > 0)) {
sscanf(ab_token->eqval, ab_token->eqspec, &ab);
}
printf("obj1 action3: command parameter (ab=%u)!'\n", ab);
} else
if ((all_token = getCurrentToken(curr_tokens, "all")) != NULL) {
printf("obj1 action3: command parameter (all)!'\n");
} else
return -3;
return 0;
}
static int obj2_action2_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
clisrv_token_struct *cc_token;
clisrv_token_struct *cd_token;
clisrv_token_struct *e_token;
printf("obj2 action2: command handle called!'\n");
if ((cc_token = getCurrentToken(curr_tokens, "cc")) != NULL) {
printf("obj2 action2: command parameter (cc)!'\n");
} else
return -3;
if ((cd_token = getCurrentToken(curr_tokens, "cd")) != NULL) {
printf("obj2 action2: command parameter (cd)!'\n");
} else
return -3;
if ((e_token = getCurrentToken(curr_tokens, "e")) != NULL) {
printf("obj2 action2: command parameter (e)!'\n");
} else
return -3;
return 0;
}
static int quit_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
printf("quit command handle called!'\n");
return 127;
}
#if 0 /*orig*/
static int test2_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
printf("test2 command handle called!'\n");
return 0;
}
static int test3_handle(clisrv_token_struct *curr_tokens, char *buff, int size)
{
printf("test3 command handle called!'\n");
return 0;
}
#endif
static int socketSendReceive(int sock, char *snd_str, char *rcv_buf, size_t rcv_buf_size)
{
int rv = 0, srv_rcvsz = 0;
char buffer[CLISRV_MAX_CMDSZ];
u2up_log_info("(entry) sock=%d\n", sock);
/* Send cmd-data string over the connection socket (including terminating null byte) */
rv = send(sock, snd_str, strlen(snd_str) + 1, 0);
if (rv != strlen(snd_str) + 1) {
u2up_log_system_error("send()\n");
return -1;
}
u2up_log_debug("%d bytes sent\n", rv);
#if 1 /* Simulate server side! */
/* srv_rcv_data: including '\0' string termination if all data received */
while ((srv_rcvsz += recv(*srvsd, buffer, sizeof(buffer), 0)) < 0) {
if (errno != EWOULDBLOCK) {
u2up_log_system_error("recv()\n");
return -1;
}
continue;
}
u2up_log_debug("srv: %d bytes received\n", srv_rcvsz);
/* Parse received data */
if ((rv = parseReceivedData(pconn, buffer, srv_rcvsz)) < 0) {
u2up_log_error("parseReceivedData()\n");
return -1;
}
/* Send response data (including terminating null byte) back to the client */
if ((rv = send(*srvsd, pconn->snd, pconn->sndsz, 0)) < 0) {
u2up_log_system_error("send()\n");
return -1;
}
#endif
/* Receive data from the connection socket (including terminating null byte) */
rv = recv(sock, rcv_buf, rcv_buf_size, 0);
if (rv <= 0) {
u2up_log_system_error("recv()\n");
return -1;
}
u2up_log_debug("%d bytes received\n", rv);
return 0;
}
int main(int argc, char *argv[])
{
int rv;
int sd[2];
int flags;
usage_check(argc, argv);
if ((clisrv_pcmds = tokenizeCliCmds(clisrv_cmds)) == NULL) {
u2up_log_error("tokenizeCliCmds() failed!\n");
exit(EXIT_FAILURE);
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sd) == -1) {
u2up_log_system_error("socketpair()\n");
exit(EXIT_FAILURE);
}
clisd = &sd[0];
srvsd = &sd[1];
/* Set server side socket nonblocking */
if ((flags = fcntl(*srvsd, F_GETFL, 0)) < 0) {
u2up_log_system_error("fcntl()\n");
close(sd[0]);
close(sd[1]);
exit(EXIT_FAILURE);
}
if ((rv = fcntl(*srvsd, F_SETFL, flags | O_NONBLOCK)) < 0) {
u2up_log_system_error("fcntl()\n");
close(sd[0]);
close(sd[1]);
exit(EXIT_FAILURE);
}
/* Initialize internal client-server connection structure */
if ((pconn = (clisrv_pconn_struct *)calloc(1, sizeof(clisrv_pconn_struct))) == NULL) {
u2up_log_system_error("calloc() - pconn\n");
close(sd[0]);
close(sd[1]);
exit(EXIT_FAILURE);
}
/* Initialize History-log file (and trim to the last 100 lines) */
if (initCmdLineLog(".u2up_cli.log", 10) < 0) {
u2up_log_error("initCmdLineLog()\n");
close(sd[0]);
close(sd[1]);
exit(EXIT_FAILURE);
}
/* Process CLI commands */
if (processCliCmds("hello-cli> ", *clisd, socketSendReceive) < 0) {
u2up_log_error("processCliCmds()\n");
close(sd[0]);
close(sd[1]);
exit(EXIT_FAILURE);
}
close(sd[0]);
close(sd[1]);
exit(EXIT_SUCCESS);
}