5 #include <readline/readline.h>
6 #include <readline/history.h>
8 #define ULCMD_PROMPT "ulcmd>>> "
9 #define FAKE_DEV_IBUFF_LEN 12
10 #define END_CMD_CHAR '%'
19 static char *responses
[] = {
26 static char *commands
[] = {
33 static char *command_error
= "ERROR: sua mula\r";
35 static void fake_device_rst_counters(struct fake_device
*fake
, int print_msg
)
38 printf("-> Resetting counters\n");
40 fake
->len
= FAKE_DEV_IBUFF_LEN
;
43 static int fake_device_init(struct fake_device
*fake
, int print_msg
)
45 fake_device_rst_counters(fake
, print_msg
);
47 fake
->ibuf
= malloc(fake
->len
);
53 static void fake_device_del(struct fake_device
*fake
)
58 static void fake_device_ibuf_dump(struct fake_device
*fake
, FILE *stream
)
63 fprintf(stream
, "-> ibuf dump: Nothing to dump.\n");
67 fprintf(stream
, "-> ibuf dump:\n");
69 for (p
= fake
->ibuf
; (p
- fake
->ibuf
) < fake
->idx
; p
++)
70 fprintf(stream
, "--> (%ld) 0x%x %c\n", p
- fake
->ibuf
, *p
, *p
);
72 fprintf(stream
, "--> idx: %d | len: %d\n", fake
->idx
, (int) fake
->len
);
75 static int fake_device_handler(struct fake_device
*fake
)
80 for (p
= fake
->ibuf
; (p
- fake
->ibuf
) < fake
->idx
; p
++)
81 if (*p
== END_CMD_CHAR
) {
88 fake_device_rst_counters(fake
, 1);
93 * End of a command has been found
96 for (i
= 0; commands
[i
]; i
++)
97 if (!strncmp(fake
->ibuf
, commands
[i
],
98 (size_t) (p
- fake
->ibuf
) + 1)) {
99 fake
->lresult
= responses
[i
];
103 /* No valid command found */
104 fake
->lresult
= command_error
;
106 fake_device_rst_counters(fake
, 1);
115 struct fake_device fake
;
117 fake_device_init(&fake
, 0);
120 line
= readline(ULCMD_PROMPT
);
122 printf("\nExiting...\n");
125 printf("-> Line read is: %s\n", line
);
127 line_len
= strlen(line
);
128 strncpy(fake
.ibuf
+ fake
.idx
, line
, fake
.len
);
129 if (line_len
> fake
.len
) {
130 fake
.idx
+= fake
.len
;
133 fake
.idx
+= line_len
;
134 fake
.len
-= line_len
;
137 fake_device_ibuf_dump(&fake
, stdout
);
138 ret
= fake_device_handler(&fake
);
140 assert(fake
.lresult
!= NULL
);
141 printf("-> Command response: %s\n", fake
.lresult
);
146 fake_device_del(&fake
);