Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation.
3 : : * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4 : : * All rights reserved.
5 : : */
6 : :
7 : : #include <stdio.h>
8 : : #include <string.h>
9 : : #include <unistd.h>
10 : : #include <stdlib.h>
11 : : #include <stdarg.h>
12 : : #include <errno.h>
13 : :
14 : : #include <eal_export.h>
15 : : #include <rte_string_fns.h>
16 : :
17 : : #include "cmdline_private.h"
18 : :
19 : : static void
20 : 147 : cmdline_valid_buffer(struct rdline *rdl, const char *buf,
21 : : __rte_unused unsigned int size)
22 : : {
23 : 147 : struct cmdline *cl = rdl->opaque;
24 : : int ret;
25 : 147 : ret = cmdline_parse(cl, buf);
26 [ - + ]: 147 : if (ret == CMDLINE_PARSE_AMBIGUOUS)
27 : 0 : cmdline_printf(cl, "Ambiguous command\n");
28 [ - + ]: 147 : else if (ret == CMDLINE_PARSE_NOMATCH)
29 : 0 : cmdline_printf(cl, "Command not found\n");
30 [ - + ]: 147 : else if (ret == CMDLINE_PARSE_BAD_ARGS)
31 : 0 : cmdline_printf(cl, "Bad arguments\n");
32 : 147 : }
33 : :
34 : : static int
35 : 0 : cmdline_complete_buffer(struct rdline *rdl, const char *buf,
36 : : char *dstbuf, unsigned int dstsize,
37 : : int *state)
38 : : {
39 : 0 : struct cmdline *cl = rdl->opaque;
40 : 0 : return cmdline_complete(cl, buf, state, dstbuf, dstsize);
41 : : }
42 : :
43 : : RTE_EXPORT_SYMBOL(cmdline_write_char)
44 : : int
45 : 4490 : cmdline_write_char(struct rdline *rdl, char c)
46 : : {
47 : : int ret = -1;
48 : : struct cmdline *cl;
49 : :
50 [ + + ]: 4490 : if (!rdl)
51 : : return -1;
52 : :
53 : 4489 : cl = rdl->opaque;
54 : :
55 [ + + ]: 4489 : if (cl->s_out >= 0)
56 : 4473 : ret = write(cl->s_out, &c, 1);
57 : :
58 : : return ret;
59 : : }
60 : :
61 : :
62 : : RTE_EXPORT_SYMBOL(cmdline_set_prompt)
63 : : void
64 : 151 : cmdline_set_prompt(struct cmdline *cl, const char *prompt)
65 : : {
66 [ + + ]: 151 : if (!cl || !prompt)
67 : : return;
68 : 150 : strlcpy(cl->prompt, prompt, sizeof(cl->prompt));
69 : : }
70 : :
71 : : RTE_EXPORT_SYMBOL(cmdline_new)
72 : : struct cmdline *
73 : 156 : cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
74 : : {
75 : : struct cmdline *cl;
76 : : int ret;
77 : :
78 [ + + ]: 156 : if (!ctx || !prompt)
79 : : return NULL;
80 : :
81 : 150 : cl = malloc(sizeof(struct cmdline));
82 [ + - ]: 150 : if (cl == NULL)
83 : : return NULL;
84 : : memset(cl, 0, sizeof(struct cmdline));
85 : 150 : cl->s_in = s_in;
86 : 150 : cl->s_out = s_out;
87 : 150 : cl->ctx = ctx;
88 : :
89 : 150 : ret = rdline_init(&cl->rdl, cmdline_write_char, cmdline_valid_buffer,
90 : : cmdline_complete_buffer, cl);
91 [ - + ]: 150 : if (ret != 0) {
92 : 0 : free(cl);
93 : 0 : return NULL;
94 : : }
95 : :
96 : 150 : cmdline_set_prompt(cl, prompt);
97 : 150 : rdline_newline(&cl->rdl, cl->prompt);
98 : :
99 : 150 : return cl;
100 : : }
101 : :
102 : : RTE_EXPORT_SYMBOL(cmdline_get_rdline)
103 : : struct rdline*
104 : 0 : cmdline_get_rdline(struct cmdline *cl)
105 : : {
106 [ # # ]: 0 : if (!cl)
107 : : return NULL;
108 : :
109 : 0 : return &cl->rdl;
110 : : }
111 : :
112 : : RTE_EXPORT_SYMBOL(cmdline_free)
113 : : void
114 : 151 : cmdline_free(struct cmdline *cl)
115 : : {
116 : : dprintf("called\n");
117 : :
118 [ + + ]: 151 : if (!cl)
119 : : return;
120 : :
121 [ + + ]: 150 : if (cl->s_in > 2)
122 : 1 : close(cl->s_in);
123 [ + + - + ]: 150 : if (cl->s_out != cl->s_in && cl->s_out > 2)
124 : 0 : close(cl->s_out);
125 : 150 : free(cl);
126 : : }
127 : :
128 : : RTE_EXPORT_SYMBOL(cmdline_printf)
129 : : void
130 : 1 : cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
131 : : {
132 : : va_list ap;
133 : :
134 [ - + ]: 1 : if (!cl || !fmt)
135 : 1 : return;
136 : :
137 [ # # ]: 0 : if (cl->s_out < 0)
138 : : return;
139 : 0 : va_start(ap, fmt);
140 : 0 : cmdline_vdprintf(cl->s_out, fmt, ap);
141 : 0 : va_end(ap);
142 : : }
143 : :
144 : : RTE_EXPORT_SYMBOL(cmdline_in)
145 : : int
146 : 153 : cmdline_in(struct cmdline *cl, const char *buf, int size)
147 : : {
148 : : const char *history, *buffer;
149 : : size_t histlen, buflen;
150 : : int ret = 0;
151 : : int i, same;
152 : :
153 [ + + ]: 153 : if (!cl || !buf)
154 : : return -1;
155 : :
156 [ + + ]: 3011 : for (i=0; i<size; i++) {
157 : 2861 : ret = rdline_char_in(&cl->rdl, buf[i]);
158 : :
159 [ + + ]: 2861 : if (ret == RDLINE_RES_VALIDATED) {
160 : 146 : buffer = rdline_get_buffer(&cl->rdl);
161 : 146 : history = rdline_get_history_item(&cl->rdl, 0);
162 [ - + ]: 146 : if (history) {
163 : 0 : histlen = strnlen(history, RDLINE_BUF_SIZE);
164 [ # # ]: 0 : same = !memcmp(buffer, history, histlen) &&
165 [ # # ]: 0 : buffer[histlen] == '\n';
166 : : }
167 : : else
168 : : same = 0;
169 : 146 : buflen = strnlen(buffer, RDLINE_BUF_SIZE);
170 [ + - ]: 146 : if (buflen > 1 && !same)
171 : 146 : rdline_add_history(&cl->rdl, buffer);
172 : 146 : rdline_newline(&cl->rdl, cl->prompt);
173 : : }
174 [ + - ]: 2715 : else if (ret == RDLINE_RES_EOF)
175 : : return -1;
176 [ + + ]: 2715 : else if (ret == RDLINE_RES_EXITED)
177 : : return -1;
178 : : }
179 : : return i;
180 : : }
181 : :
182 : : RTE_EXPORT_SYMBOL(cmdline_quit)
183 : : void
184 : 2 : cmdline_quit(struct cmdline *cl)
185 : : {
186 [ + + ]: 2 : if (!cl)
187 : : return;
188 : 1 : cmdline_cancel(cl);
189 : 1 : rdline_quit(&cl->rdl);
190 : : }
191 : :
192 : : RTE_EXPORT_SYMBOL(cmdline_interact)
193 : : void
194 : 2 : cmdline_interact(struct cmdline *cl)
195 : : {
196 : : char c;
197 : :
198 [ + + ]: 2 : if (!cl)
199 : 1 : return;
200 : :
201 : 1 : c = -1;
202 : : while (1) {
203 [ + - ]: 5 : if (cmdline_read_char(cl, &c) <= 0)
204 : : break;
205 [ + + ]: 5 : if (cmdline_in(cl, &c, 1) < 0)
206 : : break;
207 : : }
208 : : }
|