Statistics
| Branch: | Revision:

iof-bird-daemon / client / birdcl.c @ a5e9f3d2

History | View | Annotate | Download (2.44 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

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

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

    
24
#define INPUT_BUF_LEN 2048
25

    
26
struct termios tty_save;
27

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

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

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

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

    
51

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

    
59
  return 0;
60
}
61

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

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

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

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

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

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

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

    
96
  submit_command(buf);
97
}
98

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

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

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

    
111
  if (tcsetattr (0, TCSANOW, &tty) < 0)
112
    die("tcsetattr: %m");
113

    
114
  more_active = 1;
115
}
116

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

    
122
  if (tcsetattr (0, TCSANOW, &stored_tty) < 0)
123
    die("tcsetattr: %m");
124
}
125

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

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

    
139
  if (tcgetattr(0, &stored_tty) < 0)
140
    die("tcgetattr: %m");
141

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

    
147
  struct winsize tws;
148
  if (ioctl(0, TIOCGWINSZ, &tws) == 0)
149
    {
150
      term_lns = tws.ws_row;
151
      term_cls = tws.ws_col;
152
    }
153
  else
154
    {
155
       term_lns = 25;
156
       term_cls = 80;
157
    }
158
}
159

    
160
void
161
cleanup(void)
162
{
163
  if (more_active)
164
    more_end();
165
}