Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / client / birdcl.c @ 6b3f1a54

History | View | Annotate | Download (2.39 KB)

1
/*
2
 *        BIRD Client - Light variant I/O
3
 *
4
 *        (c) 1999--2004 Martin Mares <mj@ucw.cz>
5
 *      (c) 2013 Tomas Hlavacek <tomas.hlavacek@nic.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <unistd.h>
13
#include <termios.h>
14
#include <errno.h>
15

    
16
#include <sys/ioctl.h>
17
#include <signal.h>
18

    
19
#include "nest/bird.h"
20
#include "lib/resource.h"
21
#include "lib/string.h"
22
#include "client/client.h"
23
#include "sysdep/unix/unix.h"
24

    
25
#define INPUT_BUF_LEN 2048
26

    
27
struct termios tty_save;
28

    
29
void
30
input_start_list(void)
31
{
32
  /* Empty in non-ncurses version. */
33
}
34

    
35
void
36
input_stop_list(void)
37
{
38
  /* Empty in non-ncurses version. */
39
}
40

    
41
void
42
input_notify(int prompt)
43
{
44
  /* No ncurses -> no status to reveal/hide, print prompt manually. */
45
  if (!prompt)
46
    return;
47

    
48
  printf("bird> ");
49
  fflush(stdout);
50
}
51

    
52

    
53
static int
54
lastnb(char *str, int i)
55
{
56
  while (i--)
57
    if ((str[i] != ' ') && (str[i] != '\t'))
58
      return str[i];
59

    
60
  return 0;
61
}
62

    
63
void
64
input_read(void)
65
{
66
  char buf[INPUT_BUF_LEN];
67

    
68
  if ((fgets(buf, INPUT_BUF_LEN, stdin) == NULL) || (buf[0] == 0))
69
  {
70
    putchar('\n');
71
    cleanup();
72
    exit(0);
73
  }
74

    
75
  int l = strlen(buf);
76
  if ((l+1) == INPUT_BUF_LEN)
77
    {
78
      printf("Input too long.\n");
79
      return;
80
    }
81

    
82
  if (buf[l-1] == '\n')
83
    buf[--l] = '\0';
84

    
85
  if (!interactive)
86
    printf("%s\n", buf);
87

    
88
  if (l == 0)
89
    return;
90

    
91
  if (lastnb(buf, l) == '?')
92
    {
93
      cmd_help(buf, strlen(buf));
94
      return;
95
    }
96

    
97
  submit_command(buf);
98
}
99

    
100
static struct termios stored_tty;
101
static int more_active = 0;
102

    
103
void
104
more_begin(void)
105
{
106
  static struct termios tty;
107

    
108
  tty = stored_tty;
109
  tty.c_lflag &= (~ECHO);
110
  tty.c_lflag &= (~ICANON);
111

    
112
  if (tcsetattr (0, TCSANOW, &tty) < 0)
113
    DIE("tcsetattr");
114

    
115
  more_active = 1;
116
}
117

    
118
void
119
more_end(void)
120
{
121
  more_active = 0;
122

    
123
  if (tcsetattr (0, TCSANOW, &stored_tty) < 0)
124
    DIE("tcsetattr");
125
}
126

    
127
static void
128
sig_handler(int signal UNUSED)
129
{
130
  cleanup();
131
  exit(0);
132
}
133

    
134
void
135
input_init(void)
136
{
137
  if (!interactive)
138
    return;
139

    
140
  if (tcgetattr(0, &stored_tty) < 0)
141
    DIE("tcgetattr");
142

    
143
  if (signal(SIGINT, sig_handler) == SIG_IGN)
144
    signal(SIGINT, SIG_IGN);
145
  if (signal(SIGTERM, sig_handler) == SIG_IGN)
146
    signal(SIGTERM, SIG_IGN);
147

    
148
  struct winsize tws;
149
  if (ioctl(0, TIOCGWINSZ, &tws) == 0)
150
    {
151
      term_lns = tws.ws_row;
152
      term_cls = tws.ws_col;
153
    }
154
}
155

    
156
void
157
cleanup(void)
158
{
159
  if (more_active)
160
    more_end();
161
}