Merge libosmocore commit '972b502ecaf3b919b7e89c13257dd6ec97aaafb7' into master
[osmocom-bb.git] / src / shared / libosmocore / include / osmocom / vty / vty.h
blobe656abf60ce9e60cd854f1fbad19d4a4b939f90f
1 #ifndef _VTY_H
2 #define _VTY_H
4 #include <stdio.h>
5 #include <stdarg.h>
7 /*! \defgroup vty VTY (Virtual TTY) interface
8 * @{
9 */
10 /*! \file vty.h */
12 /* GCC have printf type attribute check. */
13 #ifdef __GNUC__
14 #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
15 #else
16 #define VTY_PRINTF_ATTRIBUTE(a,b)
17 #endif /* __GNUC__ */
19 /* Does the I/O error indicate that the operation should be retried later? */
20 #define ERRNO_IO_RETRY(EN) \
21 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
23 /* Vty read buffer size. */
24 #define VTY_READ_BUFSIZ 512
26 #define VTY_BUFSIZ 512
27 #define VTY_MAXHIST 20
29 /*! \brief VTY events */
30 enum event {
31 VTY_SERV,
32 VTY_READ,
33 VTY_WRITE,
34 VTY_CLOSED,
35 VTY_TIMEOUT_RESET,
36 #ifdef VTYSH
37 VTYSH_SERV,
38 VTYSH_READ,
39 VTYSH_WRITE
40 #endif /* VTYSH */
43 enum vty_type {
44 VTY_TERM,
45 VTY_FILE,
46 VTY_SHELL,
47 VTY_SHELL_SERV
50 /*! Internal representation of a single VTY */
51 struct vty {
52 /*! \brief underlying file (if any) */
53 FILE *file;
55 /*! \brief private data, specified by creator */
56 void *priv;
58 /*! \brief File descripter of this vty. */
59 int fd;
61 /*! \brief Is this vty connect to file or not */
62 enum vty_type type;
64 /*! \brief Node status of this vty */
65 int node;
67 /*! \brief Failure count */
68 int fail;
70 /*! \brief Output buffer. */
71 struct buffer *obuf;
73 /*! \brief Command input buffer */
74 char *buf;
76 /*! \brief Command cursor point */
77 int cp;
79 /*! \brief Command length */
80 int length;
82 /*! \brief Command max length. */
83 int max;
85 /*! \brief Histry of command */
86 char *hist[VTY_MAXHIST];
88 /*! \brief History lookup current point */
89 int hp;
91 /*! \brief History insert end point */
92 int hindex;
94 /*! \brief For current referencing point of interface, route-map,
95 access-list etc... */
96 void *index;
98 /*! \brief For multiple level index treatment such as key chain and key. */
99 void *index_sub;
101 /*! \brief For escape character. */
102 unsigned char escape;
104 /*! \brief Current vty status. */
105 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
107 /*! \brief IAC handling
109 * IAC handling: was the last character received the IAC
110 * (interpret-as-command) escape character (and therefore the next
111 * character will be the command code)? Refer to Telnet RFC 854. */
112 unsigned char iac;
114 /*! \brief IAC SB (option subnegotiation) handling */
115 unsigned char iac_sb_in_progress;
116 /* At the moment, we care only about the NAWS (window size) negotiation,
117 * and that requires just a 5-character buffer (RFC 1073):
118 * <NAWS char> <16-bit width> <16-bit height> */
119 #define TELNET_NAWS_SB_LEN 5
120 /*! \brief sub-negotiation buffer */
121 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
122 /*! \brief How many subnegotiation characters have we received?
124 * We just drop those that do not fit in the buffer. */
125 size_t sb_len;
127 /*! \brief Window width */
128 int width;
129 /*! \brief Widnow height */
130 int height;
132 /*! \brief Configure lines. */
133 int lines;
135 int monitor;
137 /*! \brief In configure mode. */
138 int config;
141 /* Small macro to determine newline is newline only or linefeed needed. */
142 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
144 static inline const char *vty_newline(struct vty *vty)
146 return VTY_NEWLINE;
149 /*! Information an application registers with the VTY */
150 struct vty_app_info {
151 /*! \brief name of the application */
152 const char *name;
153 /*! \brief version string of the application */
154 const char *version;
155 /*! \brief copyright string of the application */
156 const char *copyright;
157 /*! \brief \ref talloc context */
158 void *tall_ctx;
159 /*! \brief call-back for returning to parent n ode */
160 enum node_type (*go_parent_cb)(struct vty *vty);
161 /*! \brief call-back to determine if node is config node */
162 int (*is_config_node)(struct vty *vty, int node);
165 /* Prototypes. */
166 void vty_init(struct vty_app_info *app_info);
167 int vty_read_config_file(const char *file_name, void *priv);
168 void vty_init_vtysh (void);
169 void vty_reset (void);
170 struct vty *vty_new (void);
171 struct vty *vty_create (int vty_sock, void *priv);
172 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
173 int vty_out_newline(struct vty *);
174 int vty_read(struct vty *vty);
175 //void vty_time_print (struct vty *, int);
176 void vty_close (struct vty *);
177 char *vty_get_cwd (void);
178 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
179 int vty_config_lock (struct vty *);
180 int vty_config_unlock (struct vty *);
181 int vty_shell (struct vty *);
182 int vty_shell_serv (struct vty *);
183 void vty_hello (struct vty *);
184 void *vty_current_index(struct vty *);
185 int vty_current_node(struct vty *vty);
186 enum node_type vty_go_parent(struct vty *vty);
188 extern void *tall_vty_ctx;
190 extern struct cmd_element cfg_description_cmd;
191 extern struct cmd_element cfg_no_description_cmd;
193 /*! @} */
195 #endif