summaryrefslogtreecommitdiffstats
path: root/lib/vty_cli.h
blob: 13d394aa08ddd48e5573bf24181124418b428e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* VTY Command Line Handler
 * Copyright (C) 1997 Kunihiro Ishiguro
 *
 * Revisions: Copyright (C) 2010 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifndef _ZEBRA_VTY_CLI_H
#define _ZEBRA_VTY_CLI_H

#include "misc.h"
#include "vargs.h"

#include "command_local.h"
#include "vty_io.h"
#include "vty_io_basic.h"
#include "vio_fifo.h"
#include "vio_lines.h"
#include "qstring.h"
#include "qtimers.h"
#include "keystroke.h"

/*------------------------------------------------------------------------------
 * States of the CLI and related output.
 */
typedef enum cli_state
{
  cst_active      = 0,
  cst_dispatched  = 1,
  cst_in_progress = 2,
  cst_complete    = 3,

} cli_state_t ;

typedef enum cli_out_state
{
  cos_idle        = 0,

  cos_active      = 1,
  cos_cancel      = 2,

  cos_more_enter  = 3,
  cos_more_wait   = 4,

  cos_mask        = BIT(4) - 1,

  cos_monitor     = BIT(6),
  cos_paused      = BIT(7),

} cli_out_state_t ;

/*------------------------------------------------------------------------------
 * The vty_cli structure pointed to by the vty_io structure.
 */
typedef struct vty_cli* vty_cli ;

struct vty_cli
{
  vio_vf        vf ;            /* parent                               */

  /* History of commands                                                */
  vector        hist ;          /* embedded                     */
  uint          hp ;            /* current place in history     */
  uint          h_now ;         /* the present moment           */
  bool          h_repeat ;      /* latest entry is repeat       */

  /* Window width/height as reported by Telnet.  0 => unknown           */
  int           width;
  int           height;

  /* Configure lines.                                                   */
  int           lines;
  bool          lines_set ;     /* true <=> explicitly set              */

  /* Terminal monitor.                                                  */
  bool          monitor ;
  bool          monitor_busy ;

  /* The incoming stuff                                                 */
  keystroke_stream key_stream ;

  /* drawn           <=> the current prompt and user input occupy the current
   *                     line on the screen.
   *
   *                     This flag <=> the CLI "owns" the screen.  This flag
   *                     must be cleared -- by wiping the command line -- before
   *                     any other output can use the screen.
   *
   *                     In particular, must be cleared before setting
   *                     cos_active -- see below.
   *
   * more_drawn      <=> what is drawn is the "--more--" prompt.
   *                  => drawn
   *
   * If drawn is true, the following are valid:
   *
   *   prompt_len    -- the length of the prompt part.
   *                    (will be the "--more--" prompt in cli_more_wait)
   *
   *   echo_suppress -- the user part of the command line is suppressed
   *
   * NB: echo_suppress is only used for password entry.
   */
  bool          drawn ;
  bool          more_drawn ;
  int           prompt_len ;

  /* "cache" for prompt -- when node or host name changes, prompt does  */
  node_type_t   prompt_node ;
  name_gen_t    prompt_gen ;
  qstring       prompt_for_node ;

  /* State of the CLI and its output
   */
  cli_state_t      state ;
  cli_out_state_t  out_state ;

  /* This is set only if the "--more--" handling is enabled             */
  bool          more_enabled ;

  /* Timer for paused state -- multi-threaded only                      */
  qtimer        pause_timer ;

  /* Command Line(s)
   *
   * context  -- the node etc. that the CLI is in.  This may be some way behind
   *             the VTY, but is updated when the CLI level command completes.
   *
   *             Note that this is a copy of the state of exec->context when
   *             uty_want_command() was last called.
   *
   * auth_context  -- true <=> context->node is AUTH_NODE or AUTH_ENABLE_NODE
   *
   * parsed   -- the parsed object used to parse command for cli help
   *
   * to_do    -- when current command being prepared is completed (by
   *             CR/LF or otherwise) this says what there now is to be done.
   *
   * cl       -- current command line being prepared.
   * cls      -- current command line on the screen
   *
   * clx      -- current command line being executed.
   *
   * dispatch -- the last action dispatched.
   *
   * NB: during command execution vty->buf is set to point at the '\0'
   *     terminated current command line being executed.
   */
  cmd_context   context ;
  bool          auth_context ;

  cmd_parsed    parsed ;

  cmd_do_t      to_do ;
  qstring       cl ;
  qstring       cls ;

  qstring       clx ;

  cmd_action_t  dispatch ;

  /* CLI line buffering and line control                                */
  vio_fifo          cbuf ;
  vio_line_control  olc ;
} ;

/*------------------------------------------------------------------------------
 * Functions
 */
extern vty_cli uty_cli_new(vio_vf vf) ;
extern vty_cli uty_cli_close(vty_cli cli, bool final) ;

extern void uty_cli_hist_show(vty_cli cli) ;
extern ulen uty_cli_prompt_len(vty_cli cli) ;

extern vty_readiness_t uty_cli(vty_cli cli) ;

extern cmd_return_code_t uty_cli_want_command(vty_cli cli, cmd_action action) ;
extern void uty_cli_out(vty_cli cli, const char *format, ...)
                                                        PRINTF_ATTRIBUTE(2, 3) ;
extern void uty_cli_out_newline(vty_cli cli) ;
extern void uty_cli_write(vty_cli cli, const char *this, int len) ;
extern void uty_cli_wipe(vty_cli cli, int len) ;
extern void uty_cli_cancel(vty_cli cli, bool cntrl_c) ;

extern void uty_cli_set_lines(vty_cli cli, int lines, bool explicit) ;
extern void uty_cli_set_window(vty_cli cli, int width, int height) ;
extern void uty_cli_enter_more_wait(vty_cli cli) ;
extern void uty_cli_exit_more_wait(vty_cli cli) ;

extern void uty_cli_pre_monitor(vty_cli cli) ;
extern void uty_cli_post_monitor(vty_cli cli) ;

/*------------------------------------------------------------------------------
 * Pro tem -- "\r\n" string
 */
extern const char* uty_cli_newline ;

#endif /* _ZEBRA_VTY_CLI_H */