libosmovty  0.9.3
Osmocom VTY library
include/osmocom/vty/vty.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <stdio.h>
00004 #include <stdarg.h>
00005 
00011 /* GCC have printf type attribute check.  */
00012 #ifdef __GNUC__
00013 #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
00014 #else
00015 #define VTY_PRINTF_ATTRIBUTE(a,b)
00016 #endif                          /* __GNUC__ */
00017 
00018 /* Does the I/O error indicate that the operation should be retried later? */
00019 #define ERRNO_IO_RETRY(EN) \
00020         (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
00021 
00022 /* Vty read buffer size. */
00023 #define VTY_READ_BUFSIZ 512
00024 
00025 #define VTY_BUFSIZ 512
00026 #define VTY_MAXHIST 20
00027 
00029 enum event {
00030         VTY_SERV,
00031         VTY_READ,
00032         VTY_WRITE,
00033         VTY_CLOSED,
00034         VTY_TIMEOUT_RESET,
00035 #ifdef VTYSH
00036         VTYSH_SERV,
00037         VTYSH_READ,
00038         VTYSH_WRITE
00039 #endif                          /* VTYSH */
00040 };
00041 
00042 enum vty_type {
00043         VTY_TERM,
00044         VTY_FILE,
00045         VTY_SHELL,
00046         VTY_SHELL_SERV
00047 };
00048 
00050 struct vty {
00052         FILE *file;
00053 
00055         void *priv;
00056 
00058         int fd;
00059 
00061         enum vty_type type;
00062 
00064         int node;
00065 
00067         int fail;
00068 
00070         struct buffer *obuf;
00071 
00073         char *buf;
00074 
00076         int cp;
00077 
00079         int length;
00080 
00082         int max;
00083 
00085         char *hist[VTY_MAXHIST];
00086 
00088         int hp;
00089 
00091         int hindex;
00092 
00095         void *index;
00096 
00098         void *index_sub;
00099 
00101         unsigned char escape;
00102 
00104         enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
00105 
00111         unsigned char iac;
00112 
00114         unsigned char iac_sb_in_progress;
00115         /* At the moment, we care only about the NAWS (window size) negotiation,
00116          * and that requires just a 5-character buffer (RFC 1073):
00117          * <NAWS char> <16-bit width> <16-bit height> */
00118 #define TELNET_NAWS_SB_LEN 5
00119 
00120         unsigned char sb_buf[TELNET_NAWS_SB_LEN];
00124         size_t sb_len;
00125 
00127         int width;
00129         int height;
00130 
00132         int lines;
00133 
00134         int monitor;
00135 
00137         int config;
00138 };
00139 
00140 /* Small macro to determine newline is newline only or linefeed needed. */
00141 #define VTY_NEWLINE  ((vty->type == VTY_TERM) ? "\r\n" : "\n")
00142 
00143 static inline const char *vty_newline(struct vty *vty)
00144 {
00145         return VTY_NEWLINE;
00146 }
00147 
00149 struct vty_app_info {
00151         const char *name;
00153         const char *version;
00155         const char *copyright;
00157         void *tall_ctx;
00159         int (*go_parent_cb)(struct vty *vty);
00161         int (*is_config_node)(struct vty *vty, int node);
00163         int (*config_is_consistent)(struct vty *vty);
00164 };
00165 
00166 /* Prototypes. */
00167 void vty_init(struct vty_app_info *app_info);
00168 int vty_read_config_file(const char *file_name, void *priv);
00169 void vty_init_vtysh (void);
00170 void vty_reset (void);
00171 struct vty *vty_new (void);
00172 struct vty *vty_create (int vty_sock, void *priv);
00173 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
00174 int vty_out_newline(struct vty *);
00175 int vty_read(struct vty *vty);
00176 //void vty_time_print (struct vty *, int);
00177 void vty_close (struct vty *);
00178 char *vty_get_cwd (void);
00179 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
00180 int vty_config_lock (struct vty *);
00181 int vty_config_unlock (struct vty *);
00182 int vty_shell (struct vty *);
00183 int vty_shell_serv (struct vty *);
00184 void vty_hello (struct vty *);
00185 void *vty_current_index(struct vty *);
00186 int vty_current_node(struct vty *vty);
00187 int vty_go_parent(struct vty *vty);
00188 
00189 extern void *tall_vty_ctx;
00190 
00191 extern struct cmd_element cfg_description_cmd;
00192 extern struct cmd_element cfg_no_description_cmd;
00193 
00194 
00198 enum signal_vty {
00199         S_VTY_EVENT,
00200 };
00201 
00202 struct vty_signal_data {
00203         enum event event;
00204         int sock;
00205         struct vty *vty;
00206 };
00207