CNDP  22.08.0
cli_input.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2019-2022 Intel Corporation.
3  */
4 
5 #ifndef _CLI_INPUT_H_
6 #define _CLI_INPUT_H_
7 
8 #include <strings.h> // for index
9 #include <stdint.h> // for uint32_t
10 #include <stdio.h> // for FILE
11 #include <stdlib.h> // for atoi
12 #include <string.h> // for memset
13 
14 #include "cli.h" // for cli, this_cli, cli_clr_flag, cli_tst_flag
15 #include "cli_common.h"
16 #include "cli_gapbuf.h" // for gb_data_size, gb_left_data_size, gb_point_of...
17 #include "cne_log.h"
18 #include "cne_common.h" // for CNDP_API
19 #include "cne_stdio.h" // for cne_printf
20 #include "cne_tty.h" // for tty_write, tty_num_columns, tty_read
21 #include "vt100_out.h" // for vt_bol, vt_cnright, vt_cpos, vt100_clear_line
22 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
40 CNDP_API int cli_poll(char *c);
41 
42 /* Report Cursor Position <ESC>[{ROW};{COLUMN}R
43  Generated by the device in response to a Query Cursor Position request;
44  reports current cursor position. */
45 static inline void
46 cli_get_cursor(int *row, int *col)
47 {
48  char buf[32], *p, ch;
49  int r, c, l;
50 
51 again:
52  vt_cpos();
53 
54  memset(buf, 0, sizeof(buf));
55  p = buf;
56  l = sizeof(buf) - 1;
57 
58  do {
59  tty_read(&ch, 1);
60  if (ch == 'R')
61  break;
62  if (ch == '\0')
63  continue;
64  *p++ = ch;
65  } while (l--);
66 
67  p = index(buf, ';');
68  if (!p)
69  goto again;
70 
71  r = atoi(&buf[2]);
72  c = atoi(++p);
73  if (!r || !c)
74  goto again;
75 
76  *row = r;
77  *col = c;
78 }
79 
88 static inline void
90 {
91  tty_write(vt100_left_arr, -1);
92 }
93 
102 static inline void
104 {
105  tty_write(vt100_right_arr, -1);
106 }
107 
116 static inline void
118 {
119  tty_write(vt100_save_cursor, -1);
120 }
121 
130 static inline void
132 {
133  tty_write(vt100_restore_cursor, -1);
134 }
135 
144 static inline void
146 {
147  if (gb_left_data_size(this_cli->gb))
148  tty_write(gb_start_of_buf(this_cli->gb), gb_left_data_size(this_cli->gb));
149 }
150 
159 static inline void
161 {
162  if (gb_right_data_size(this_cli->gb))
163  tty_write(gb_end_of_gap(this_cli->gb), gb_right_data_size(this_cli->gb));
164 }
165 
174 static inline void
176 {
177  tty_write(vt100_clear_screen, -1);
178 }
179 
188 static inline void
190 {
191  tty_write(vt100_clear_right, -1);
192 }
193 
204 static inline void
205 cli_clear_line(int lineno)
206 {
207  if (lineno > 0)
208  cne_printf(vt100_pos_cursor, lineno, 0);
209  else
210  tty_write("\r", 1);
211 
212  tty_write(vt100_clear_line, -1);
213 }
214 
225 static inline void
227 {
228  while (lineno--)
229  cne_printf(vt100_up_arr);
230 }
231 
238 static inline void
240 {
241  tty_write("\r", 1);
242  this_cli->plen = this_cli->prompt(t);
244 }
245 
246 /* display all or part of the command line, while allowing the line to scroll */
247 static inline void
248 cli_display_line(void)
249 {
250  struct gapbuf *gb = this_cli->gb;
251  char buf[gb_data_size(gb) + 16];
252  int point = gb_point_offset(gb);
253  int len = gb_copy_to_buf(gb, buf, gb_data_size(gb));
254  int window = (tty_num_columns() - this_cli->plen) - 1;
255  int wstart, wend;
256 
257  if (cli_tst_flag(DELETE_CHAR)) {
258  cli_clr_flag(DELETE_CHAR);
259  tty_write(" \b", 2);
260  cli_set_flag(CLEAR_TO_EOL);
261  }
262  if (cli_tst_flag(CLEAR_LINE)) {
263  cli_clr_flag(CLEAR_LINE);
264  vt_bol();
266  cli_set_flag(DISPLAY_PROMPT);
267  }
268  if (cli_tst_flag(CLEAR_TO_EOL)) {
269  cli_clr_flag(CLEAR_TO_EOL);
271  }
272  if (cli_tst_flag(DISPLAY_PROMPT)) {
273  cli_clr_flag(DISPLAY_PROMPT | PROMPT_CONTINUE);
275  }
276 
277  if (point < window) {
278  wstart = 0;
279  if (len < window)
280  wend = point + (len - point);
281  else
282  wend = point + (window - point);
283  } else {
284  wstart = point - window;
285  wend = wstart + window;
286  }
287 
288  vt_bol();
289  vt_cnright(this_cli->plen);
290 
291  (void)wend;
292  tty_write(&buf[wstart], wend - wstart);
293 
295 
296  vt_bol();
297  vt_cnright(this_cli->plen + point);
298 }
299 
308 static inline void
310 {
311  uint32_t i;
312 
313  this_cli->flags |= DISPLAY_PROMPT;
314 
315  cli_display_line();
316 
317  gb_move_gap_to_point(this_cli->gb);
318 
319  for (i = 0; i < (gb_data_size(this_cli->gb) - gb_point_offset(this_cli->gb)); i++)
320  cli_cursor_left();
321 }
322 
337 CNDP_API void cli_input(char *str, int n, int silent);
338 
348 
361 CNDP_API void cli_set_io(FILE *in, FILE *out);
362 
371 CNDP_API int cli_stdin_setup(void);
372 
381 CNDP_API void cli_stdin_restore(void);
382 
395 CNDP_API char cli_pause(const char *msg, const char *keys);
396 
405 CNDP_API int cli_yield_io(void);
406 
407 #ifdef __cplusplus
408 }
409 #endif
410 
411 #endif /* _CLI_INPUT_H_ */
int(* cli_prompt_t)(int continuation)
Definition: cli.h:85
static void gb_move_gap_to_point(struct gapbuf *gb)
Definition: cli_gapbuf.h:322
CNDP_API uint32_t gb_copy_to_buf(struct gapbuf *gb, char *dst, uint32_t size)
static int gb_point_offset(struct gapbuf *gb)
Definition: cli_gapbuf.h:289
static char * gb_start_of_buf(struct gapbuf *gb)
Definition: cli_gapbuf.h:155
static char * gb_end_of_gap(struct gapbuf *gb)
Definition: cli_gapbuf.h:183
static uint32_t gb_left_data_size(struct gapbuf *gb)
Definition: cli_gapbuf.h:640
static uint32_t gb_right_data_size(struct gapbuf *gb)
Definition: cli_gapbuf.h:654
static uint32_t gb_data_size(struct gapbuf *gb)
Definition: cli_gapbuf.h:141
static void cli_display_left(void)
Definition: cli_input.h:145
static void cli_clear_to_eol(void)
Definition: cli_input.h:189
static void cli_save_cursor(void)
Definition: cli_input.h:117
CNDP_API char cli_pause(const char *msg, const char *keys)
static void cli_move_cursor_up(int lineno)
Definition: cli_input.h:226
static void cli_display_right(void)
Definition: cli_input.h:160
CNDP_API int cli_poll(char *c)
static void cli_restore_cursor(void)
Definition: cli_input.h:131
static void cli_display_prompt(int t)
Definition: cli_input.h:239
static void cli_clear_screen(void)
Definition: cli_input.h:175
CNDP_API void cli_stdin_restore(void)
CNDP_API void cli_set_io(FILE *in, FILE *out)
static void cli_cursor_left(void)
Definition: cli_input.h:89
static void cli_clear_line(int lineno)
Definition: cli_input.h:205
CNDP_API int cli_stdin_setup(void)
CNDP_API int cli_yield_io(void)
static void cli_cursor_right(void)
Definition: cli_input.h:103
CNDP_API cli_prompt_t cli_set_prompt(cli_prompt_t prompt)
CNDP_API void cli_input(char *str, int n, int silent)
static void cli_redisplay_line(void)
Definition: cli_input.h:309
CNDP_API int cne_printf(const char *fmt,...)
CNDP_API int tty_num_columns(void)
CNDP_API int tty_read(char *buf, int len)
CNDP_API int tty_write(const char *buf, int len)