summaryrefslogtreecommitdiffstats
path: root/lib/vty_io_file.c
blob: 2d17276a1a4b7b3466cb9f1568d9c9ba5e8e1cdb (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* VTY I/O for Files
 *
 * 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.
 */
#include "misc.h"

#include "command_local.h"

#include "vty_io_file.h"
#include "vty_io_basic.h"

/*==============================================================================
 * VTY File Output
 *
 * This is for input and output of configuration files and piped stuff.
 *
 * When reading the configuration (and piped stuff in the configuration) I/O
 * is blocking... nothing else can run while this is going on.  Otherwise,
 * all I/O is non-blocking.
 */



/*==============================================================================
 * Prototypes.
 */

/*------------------------------------------------------------------------------
 *
 *
 */
extern cmd_return_code_t
uty_file_read_open(vty_io vio, qstring name, bool reflect)
{
  const char* name_str ;
  int   fd ;
  vio_vf vf ;
  vfd_io_type_t iot ;

  name_str = qs_make_string(name) ;

  iot =  vfd_io_read | vfd_io_blocking ;        /* TODO blocking        */

  /* Do the basic file open.                                            */
  fd = uty_vfd_file_open(name_str, iot) ;

  if (fd < 0)
    {
      uty_out(vio, "%% Could not open input file %s\n", name_str) ;

      return CMD_WARNING ;
    }

  /* OK -- now push the new input onto the vin_stack.                   */
  vf = uty_vf_new(vio, name_str, fd, vfd_file, iot) ;
  uty_vin_open(vio, vf, VIN_FILE, NULL, NULL, 16 * 1024) ;

  vf->parse_type      = cmd_parse_strict ;
  vf->reflect_enabled = reflect ;

  return CMD_SUCCESS ;
} ;

/*------------------------------------------------------------------------------
 *
 *
 */
extern cmd_return_code_t
uty_file_write_open(vty_io vio, qstring name, bool append)
{
  const char* name_str ;
  int   fd ;
  vio_vf vf ;
  vfd_io_type_t iot ;

  iot =  vfd_io_write | vfd_io_blocking ;       /* TODO blocking        */

  if (append)
    iot |= vfd_io_append ;

  name_str = qs_make_string(name) ;

  /* Do the basic file open.                                            */
  fd = uty_vfd_file_open(name_str, iot) ;

  if (fd < 0)
    {
      uty_out(vio, "%% Could not open output file %s\n", name_str) ;

      return CMD_WARNING ;
    }

  /* OK -- now push the new input onto the vin_stack.                   */
  vf = uty_vf_new(vio, name_str, fd, vfd_file, iot) ;
  uty_vout_open(vio, vf, VOUT_FILE, NULL, NULL, 16 * 1024) ;

  vf->out_enabled = true ;

  return CMD_SUCCESS ;
} ;

/*==============================================================================
 * Command line fetch from a file or pipe.
 *
 * Returns: CMD_SUCCESS  -- have another command line ready to go
 *          CMD_WAITING  -- do not have a command line at the moment
 *          CMD_EOF      -- ran into EOF
 *          CMD_IO_ERROR -- ran into an I/O error
 */
extern cmd_return_code_t
uty_file_fetch_command_line(vio_vf vf, qstring* line)
{
  assert(vf->vin_state == vf_open) ;

  if (vf->line_complete)
    {
      vio_fifo_set_hold_mark(vf->ibuf) ;        /* advance hold         */

      vf->line_complete = false ;
      vf->line_number  += vf->line_step ;

      qs_set_len_nn(vf->cl, 0) ;
      vf->line_step = 0 ;
    } ;

  while (1)
    {
      char* s, * p, * q, * e ;
      size_t  have ;
      ulen    len ;

      s = vio_fifo_get(vf->ibuf, &have) ;

      /* If nothing in hand, try and get some more.
       *
       * Either exits or loops back to set s & have.
       */
      if (have == 0)
        {
          int get ;

          get = vio_fifo_read_nb(vf->ibuf, vio_vfd_fd(vf->vfd), 100) ;

          if (get > 0)
            continue ;                  /* loop back                    */

          if (get == 0)
            return CMD_WAITING ;        /* need to set read ready !     */

          if (get == -1)
            ;                           /* register error               */

          if (get == -2)
            return (qs_len_nn(vf->cl) > 0) ? CMD_SUCCESS : CMD_EOF ;
        } ;

      /* Try to find a '\n' -- converting all other control chars to ' '
       *
       * When we find '\n' step back across any trailing ' ' (which includes
       * any control chars before the '\n').
       *
       * This means that we cope with "\r\n" line terminators.  But not anything
       * more exotic.
       */
      p = s ;
      e = s + have ;       /* have != 0    */
      q = NULL ;

      while (p < e)
        {
          if (*p++ < 0x20)
            {
              if (*(p-1) != '\n')
                {
                  *(p-1) = ' ' ;        /* everything other than '\n'   */
                  continue ;
                } ;

              ++vf->line_step ;         /* got a '\n'                   */

              q = p ;                   /* point just past '\n'         */
              do --q ; while ((q > s) && (*(q-1) == ' ')) ;
                                        /* discard trailing "spaces"    */
              break ;
            } ;
        } ;

      /* Step past what have just consumed -- we have a hold_mark, so
       * stuff is still in the fifo.
       */
      vio_fifo_step(vf->ibuf, p - s) ;

      /* If not found '\n', then we have a line fragment that needs to be
       * appended to any previous line fragments.
       *
       * Loops back to try to get some more form the fifo.
       */
      if (q == NULL)
        {
          qs_append_n(vf->cl, s, p - s) ;
          continue ;
        } ;

      /* If we have nothing so far, set alias to point at what we have in
       * the fifo.  Otherwise, append to what we have.
       *
       * End up with: s = start of entire line, so far.
       *              p = end of entire line so far.
       */
      len = q - s ;                     /* length to add        */
      if (qs_len_nn(vf->cl) == 0)
        {
          qs_set_alias_n(vf->cl, s, len) ;
          p = q ;
        }
      else
        {
          if (len != 0)
            qs_append_n(vf->cl, s, len) ;

          s = qs_char_nn(vf->cl) ;
          p = s + qs_len_nn(vf->cl) ;

          if ((len == 0) && (p > s) && (*(p-1) == ' '))
            {
              /* Have an empty end of line section, and the last character
               * of what we have so far is ' ', so need now to trim trailing
               * spaces off the stored stuff.
               */
              do --p ; while ((p > s) && (*(p-1) == ' ')) ;

              qs_set_len_nn(vf->cl, p - s) ;
            } ;
        } ;

      /* Now worry about we have a trailing '\'.                        */

      if ((p == s) || (*(p-1) != '\\'))
        break ;                 /* no \ => no continuation => success   */

      /* Have a trailing '\'.
       *
       * If there are an odd number of '\', strip the last one and loop
       * round to collect the continuation.
       *
       * If there are an even number of '\', then this is not a continuation.
       *
       * Note that this rule deals with the case of the continuation line
       * being empty... e.g. ....\\\     n     n  -- where n is '\n'
       */
      q = p ;
      do --q ; while ((q > s) && (*(q-1) == '\\')) ;

      if (((p - q) & 1) == 0)
        break ;                 /* even => no continuation => success   */

      qs_set_len_nn(vf->cl, p - s - 1) ;        /* strip odd '\'        */

      continue ;                /* loop back to fetch more              */
    } ;

  /* Success have a line in hand                                        */

  vf->line_complete = true ;
  *line = vf->cl ;

  return CMD_SUCCESS ;
} ;

/*------------------------------------------------------------------------------
 * Command output push to a file or pipe.
 *
 * Returns: CMD_SUCCESS  -- done
 *          CMD_IO_ERROR -- ran into an I/O error
 */
extern cmd_return_code_t
uty_file_out_push(vio_vf vf)
{
  assert(vf->vout_state == vf_open) ;

  if (vio_fifo_write_nb(vf->obuf, vio_vfd_fd(vf->vfd), false) >= 0)
    return CMD_SUCCESS ;
  else
    return CMD_IO_ERROR ;
} ;

/*------------------------------------------------------------------------------
 * Tidy up after input file has been closed
 */
extern void
uty_file_read_close(vio_vf vf)
{
  return ;
} ;

/*------------------------------------------------------------------------------
 * Flush output buffer and close.
 *
 * Returns: true <=> buffer (now) empty
 */
extern bool
uty_file_write_close(vio_vf vf, bool final)
{
  return vio_fifo_write_nb(vf->obuf, vio_vfd_fd(vf->vfd), true) == 0 ;
} ;