comparison libutil/stdin_handler.c @ 1:e5527fc2050b

libutil: copy from ice1-trau-tester
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 29 Aug 2024 12:58:56 +0000
parents
children
comparison
equal deleted inserted replaced
0:06c795d4781d 1:e5527fc2050b
1 /*
2 * Stdin handler function: gets called from Osmocom select loop for stdin,
3 * does line read and initial parsing into arguments, then calls
4 * program-supplied handler.
5 */
6
7 #include <ctype.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13
14 #include <osmocom/core/select.h>
15
16 #include "stdin_handler.h"
17
18 #define MAX_ARGS 16
19
20 int stdin_select_cb(struct osmo_fd *ofd, unsigned int what)
21 {
22 stdin_dispatch_t dispatch_func = ofd->data;
23 char buf[256], *argv[MAX_ARGS+1], *cp;
24 int argc;
25
26 fgets(buf, sizeof buf, stdin);
27 argc = 0;
28 for (cp = buf; ; ) {
29 while (isspace(*cp))
30 cp++;
31 if (*cp == '\0' || *cp == '#')
32 break;
33 if (argc >= MAX_ARGS) {
34 printf("error: input command exceeds MAX_ARGS\n");
35 return 0;
36 }
37 argv[argc] = cp;
38 while (*cp && !isspace(*cp))
39 cp++;
40 if (*cp)
41 *cp++ = '\0';
42 argc++;
43 }
44 if (!argc)
45 return 0;
46 argv[argc] = NULL;
47 dispatch_func(argc, argv);
48 return 0;
49 }