1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
14 #include <rte_string_fns.h>
16 #include "cmdline_private.h"
19 cmdline_valid_buffer(struct rdline *rdl, const char *buf,
20 __rte_unused unsigned int size)
22 struct cmdline *cl = rdl->opaque;
24 ret = cmdline_parse(cl, buf);
25 if (ret == CMDLINE_PARSE_AMBIGUOUS)
26 cmdline_printf(cl, "Ambiguous command\n");
27 else if (ret == CMDLINE_PARSE_NOMATCH)
28 cmdline_printf(cl, "Command not found\n");
29 else if (ret == CMDLINE_PARSE_BAD_ARGS)
30 cmdline_printf(cl, "Bad arguments\n");
34 cmdline_complete_buffer(struct rdline *rdl, const char *buf,
35 char *dstbuf, unsigned int dstsize,
38 struct cmdline *cl = rdl->opaque;
39 return cmdline_complete(cl, buf, state, dstbuf, dstsize);
43 cmdline_write_char(struct rdline *rdl, char c)
54 ret = write(cl->s_out, &c, 1);
61 cmdline_set_prompt(struct cmdline *cl, const char *prompt)
65 strlcpy(cl->prompt, prompt, sizeof(cl->prompt));
69 cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
77 cl = malloc(sizeof(struct cmdline));
80 memset(cl, 0, sizeof(struct cmdline));
85 ret = rdline_init(&cl->rdl, cmdline_write_char, cmdline_valid_buffer,
86 cmdline_complete_buffer, cl);
92 cmdline_set_prompt(cl, prompt);
93 rdline_newline(&cl->rdl, cl->prompt);
99 cmdline_get_rdline(struct cmdline *cl)
105 cmdline_free(struct cmdline *cl)
114 if (cl->s_out != cl->s_in && cl->s_out > 2)
120 cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
130 cmdline_vdprintf(cl->s_out, fmt, ap);
135 cmdline_in(struct cmdline *cl, const char *buf, int size)
137 const char *history, *buffer;
138 size_t histlen, buflen;
145 for (i=0; i<size; i++) {
146 ret = rdline_char_in(&cl->rdl, buf[i]);
148 if (ret == RDLINE_RES_VALIDATED) {
149 buffer = rdline_get_buffer(&cl->rdl);
150 history = rdline_get_history_item(&cl->rdl, 0);
152 histlen = strnlen(history, RDLINE_BUF_SIZE);
153 same = !memcmp(buffer, history, histlen) &&
154 buffer[histlen] == '\n';
158 buflen = strnlen(buffer, RDLINE_BUF_SIZE);
159 if (buflen > 1 && !same)
160 rdline_add_history(&cl->rdl, buffer);
161 rdline_newline(&cl->rdl, cl->prompt);
163 else if (ret == RDLINE_RES_EOF)
165 else if (ret == RDLINE_RES_EXITED)
172 cmdline_quit(struct cmdline *cl)
176 rdline_quit(&cl->rdl);
180 cmdline_poll(struct cmdline *cl)
188 else if (cl->rdl.status == RDLINE_EXITED)
189 return RDLINE_EXITED;
191 status = cmdline_poll_char(cl);
194 else if (status > 0) {
196 read_status = cmdline_read_char(cl, &c);
200 status = cmdline_in(cl, &c, 1);
201 if (status < 0 && cl->rdl.status != RDLINE_EXITED)
205 return cl->rdl.status;
209 cmdline_interact(struct cmdline *cl)
218 if (cmdline_read_char(cl, &c) <= 0)
220 if (cmdline_in(cl, &c, 1) < 0)