1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
16 #include <netinet/in.h>
18 #include <rte_string_fns.h>
23 cmdline_valid_buffer(struct rdline *rdl, const char *buf,
24 __rte_unused unsigned int size)
26 struct cmdline *cl = rdl->opaque;
28 ret = cmdline_parse(cl, buf);
29 if (ret == CMDLINE_PARSE_AMBIGUOUS)
30 cmdline_printf(cl, "Ambiguous command\n");
31 else if (ret == CMDLINE_PARSE_NOMATCH)
32 cmdline_printf(cl, "Command not found\n");
33 else if (ret == CMDLINE_PARSE_BAD_ARGS)
34 cmdline_printf(cl, "Bad arguments\n");
38 cmdline_complete_buffer(struct rdline *rdl, const char *buf,
39 char *dstbuf, unsigned int dstsize,
42 struct cmdline *cl = rdl->opaque;
43 return cmdline_complete(cl, buf, state, dstbuf, dstsize);
47 cmdline_write_char(struct rdline *rdl, char c)
58 ret = write(cl->s_out, &c, 1);
65 cmdline_set_prompt(struct cmdline *cl, const char *prompt)
69 strlcpy(cl->prompt, prompt, sizeof(cl->prompt));
73 cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
81 cl = malloc(sizeof(struct cmdline));
84 memset(cl, 0, sizeof(struct cmdline));
89 ret = rdline_init(&cl->rdl, cmdline_write_char, cmdline_valid_buffer,
90 cmdline_complete_buffer);
97 cmdline_set_prompt(cl, prompt);
98 rdline_newline(&cl->rdl, cl->prompt);
104 cmdline_get_rdline(struct cmdline *cl)
110 cmdline_free(struct cmdline *cl)
119 if (cl->s_out != cl->s_in && cl->s_out > 2)
125 cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
135 vdprintf(cl->s_out, fmt, ap);
140 cmdline_in(struct cmdline *cl, const char *buf, int size)
142 const char *history, *buffer;
143 size_t histlen, buflen;
150 for (i=0; i<size; i++) {
151 ret = rdline_char_in(&cl->rdl, buf[i]);
153 if (ret == RDLINE_RES_VALIDATED) {
154 buffer = rdline_get_buffer(&cl->rdl);
155 history = rdline_get_history_item(&cl->rdl, 0);
157 histlen = strnlen(history, RDLINE_BUF_SIZE);
158 same = !memcmp(buffer, history, histlen) &&
159 buffer[histlen] == '\n';
163 buflen = strnlen(buffer, RDLINE_BUF_SIZE);
164 if (buflen > 1 && !same)
165 rdline_add_history(&cl->rdl, buffer);
166 rdline_newline(&cl->rdl, cl->prompt);
168 else if (ret == RDLINE_RES_EOF)
170 else if (ret == RDLINE_RES_EXITED)
177 cmdline_quit(struct cmdline *cl)
181 rdline_quit(&cl->rdl);
185 cmdline_poll(struct cmdline *cl)
194 else if (cl->rdl.status == RDLINE_EXITED)
195 return RDLINE_EXITED;
201 status = poll(&pfd, 1, 0);
204 else if (status > 0) {
206 read_status = read(cl->s_in, &c, 1);
210 status = cmdline_in(cl, &c, 1);
211 if (status < 0 && cl->rdl.status != RDLINE_EXITED)
215 return cl->rdl.status;
219 cmdline_interact(struct cmdline *cl)
228 if (read(cl->s_in, &c, 1) <= 0)
230 if (cmdline_in(cl, &c, 1) < 0)